1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-23 00:16:17 +04:00

aarch64: implement syscall

This commit is contained in:
equation314 2018-11-07 01:05:43 +08:00
parent a91534e34d
commit 9fc13c8ebb
5 changed files with 75 additions and 22 deletions

View File

@ -3,15 +3,15 @@
#[repr(C)] #[repr(C)]
#[derive(Default, Debug, Copy, Clone)] #[derive(Default, Debug, Copy, Clone)]
pub struct TrapFrame { pub struct TrapFrame {
pub elr: u64, pub elr: usize,
pub spsr: u64, pub spsr: usize,
pub sp: u64, pub sp: usize,
pub tpidr: u64, pub tpidr: usize,
// pub q0to31: [u128; 32], // disable SIMD/FP registers // pub q0to31: [u128; 32], // disable SIMD/FP registers
pub x1to29: [u64; 29], pub x1to29: [usize; 29],
pub __reserved: u64, pub __reserved: usize,
pub x30: u64, // lr pub x30: usize, // lr
pub x0: u64, pub x0: usize,
} }
/// 用于在内核栈中构造新线程的中断帧 /// 用于在内核栈中构造新线程的中断帧
@ -19,17 +19,17 @@ impl TrapFrame {
fn new_kernel_thread(entry: extern fn(usize) -> !, arg: usize, sp: usize) -> Self { fn new_kernel_thread(entry: extern fn(usize) -> !, arg: usize, sp: usize) -> Self {
use core::mem::zeroed; use core::mem::zeroed;
let mut tf: Self = unsafe { zeroed() }; let mut tf: Self = unsafe { zeroed() };
tf.x0 = arg as u64; tf.x0 = arg;
tf.sp = sp as u64; tf.sp = sp;
tf.elr = entry as u64; tf.elr = entry as usize;
tf.spsr = 0b1101_00_0101; // To EL 1, enable IRQ tf.spsr = 0b1101_00_0101; // To EL 1, enable IRQ
tf tf
} }
fn new_user_thread(entry_addr: usize, sp: usize) -> Self { fn new_user_thread(entry_addr: usize, sp: usize) -> Self {
use core::mem::zeroed; use core::mem::zeroed;
let mut tf: Self = unsafe { zeroed() }; let mut tf: Self = unsafe { zeroed() };
tf.sp = sp as u64; tf.sp = sp;
tf.elr = entry_addr as u64; tf.elr = entry_addr;
tf.spsr = 0b1101_00_0000; // To EL 0, enable IRQ tf.spsr = 0b1101_00_0000; // To EL 0, enable IRQ
tf tf
} }

View File

@ -45,7 +45,7 @@ pub extern "C" fn rust_trap(info: Info, esr: u32, tf: &mut TrapFrame) {
// syndrome is only valid with sync // syndrome is only valid with sync
match syndrome { match syndrome {
Syndrome::Brk(brk) => handle_break(brk, tf), Syndrome::Brk(brk) => handle_break(brk, tf),
Syndrome::Svc(syscall) => handle_syscall(syscall, tf), Syndrome::Svc(_) => handle_syscall(tf),
_ => ::trap::error(tf), _ => ::trap::error(tf),
} }
} }
@ -57,10 +57,23 @@ pub extern "C" fn rust_trap(info: Info, esr: u32, tf: &mut TrapFrame) {
} }
fn handle_break(num: u16, tf: &mut TrapFrame) { fn handle_break(num: u16, tf: &mut TrapFrame) {
tf.elr += 4; // Skip the current brk instruction // Skip the current brk instruction (ref: J1.1.2, page 6147)
tf.elr += 4;
} }
fn handle_syscall(num: u16, tf: &mut TrapFrame) { fn handle_syscall(tf: &mut TrapFrame) {
// svc instruction has been skipped in syscall // svc instruction has been skipped in syscall (ref: J1.1.2, page 6152)
println!("syscall {}", num); let ret = ::syscall::syscall(
tf.x1to29[7] as usize,
[
tf.x0,
tf.x1to29[0],
tf.x1to29[1],
tf.x1to29[2],
tf.x1to29[3],
tf.x1to29[4],
],
tf,
);
tf.x0 = ret as usize;
} }

View File

@ -43,8 +43,12 @@ pub extern "C" fn rust_main() -> ! {
asm!("brk 233"); asm!("brk 233");
}, },
'c' => unsafe { 'c' => unsafe {
println!("svc 666"); println!("sys_putc");
asm!("svc 666"); asm!(
"mov x8, #30
mov x0, #65
svc 0"
);
}, },
't' => unsafe { 't' => unsafe {
println!("{}", timer::get_cycle()); println!("{}", timer::get_cycle());

30
user/aarch64-ucore.json Normal file
View File

@ -0,0 +1,30 @@
{
"abi-blacklist": [
"stdcall",
"fastcall",
"vectorcall",
"thiscall",
"win64",
"sysv64"
],
"arch": "aarch64",
"data-layout": "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128",
"executables": true,
"linker": "rust-lld",
"linker-flavor": "ld.lld",
"linker-is-gnu": true,
"llvm-target": "aarch64-unknown-none",
"no-compiler-rt": true,
"features": "+a53,+strict-align,-neon",
"max-atomic-width": 128,
"os": "none",
"panic": "abort",
"panic-strategy": "abort",
"relocation-model": "static",
"position-independent-executables": true,
"target-c-int-width": "32",
"target-endian": "little",
"target-pointer-width": "64",
"target-family": "unix",
"disable-redzone": true
}

View File

@ -44,6 +44,12 @@ fn sys_call(id: usize, arg0: usize, arg1: usize, arg2: usize, arg3: usize, arg4:
: "{rax}" (id), "{rdi}" (arg0), "{rsi}" (arg1), "{rdx}" (arg2), "{rcx}" (arg3), "{r8}" (arg4), "{r9}" (arg5) : "{rax}" (id), "{rdi}" (arg0), "{rsi}" (arg1), "{rdx}" (arg2), "{rcx}" (arg3), "{r8}" (arg4), "{r9}" (arg5)
: "memory" : "memory"
: "intel" "volatile"); : "intel" "volatile");
#[cfg(target_arch = "aarch64")]
asm!("svc 0"
: "={x0}" (ret)
: "{x8}" (id), "{x0}" (arg0), "{x1}" (arg1), "{x2}" (arg2), "{x3}" (arg3), "{x4}" (arg4), "{x5}" (arg5)
: "memory"
: "volatile");
} }
ret ret
} }
@ -68,7 +74,7 @@ pub fn sys_open(path: &str, flags: usize) -> i32 {
} }
pub fn sys_close(fd: usize) -> i32 { pub fn sys_close(fd: usize) -> i32 {
sys_call(SYS_CLOSE, fd, 0 , 0, 0, 0, 0) sys_call(SYS_CLOSE, fd, 0, 0, 0, 0, 0)
} }
/// Fork the current process. Return the child's PID. /// Fork the current process. Return the child's PID.