Skip to content

Commit

Permalink
Merge pull request #3 from Foundation-Devices/1.66.0-SFT-1139
Browse files Browse the repository at this point in the history
[SFT-1139] Fix the rest of ARM stubs
  • Loading branch information
eupn authored Feb 17, 2023
2 parents 440c96e + 1986790 commit d5381e3
Show file tree
Hide file tree
Showing 5 changed files with 333 additions and 178 deletions.
16 changes: 13 additions & 3 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ checksum = "8c97b9233581d84b8e1e689cdd3a47b6f69770084fc246e86a7f78b0d9c1d4a5"
[[package]]
name = "dlmalloc"
version = "0.2.4"
source = "git+https://github.com/Foundation-Devices/dlmalloc-rs.git?branch=xous-fixes-arm#9d58282c1c0c317c07b38b8f4e9af246738775a3"
source = "git+ssh://git@github.com/Foundation-Devices/dlmalloc-rs.git?branch=xous-fixes-arm#9d58282c1c0c317c07b38b8f4e9af246738775a3"
dependencies = [
"compiler_builtins",
"libc",
"rustc-std-workspace-core",
"xous",
"xous 0.9.28 (git+https://github.com/Foundation-Devices/keyOS.git?branch=arm-keyos)",
]

[[package]]
Expand Down Expand Up @@ -2488,7 +2488,7 @@ dependencies = [
"std_detect",
"unwind",
"wasi 0.11.0+wasi-snapshot-preview1",
"xous",
"xous 0.9.28 (git+ssh://[email protected]/Foundation-Devices/keyOS.git?branch=arm-keyos)",
]

[[package]]
Expand Down Expand Up @@ -2977,6 +2977,16 @@ version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680"

[[package]]
name = "xous"
version = "0.9.28"
source = "git+ssh://[email protected]/Foundation-Devices/keyOS.git?branch=arm-keyos#0030842dab01260db8060c8084b670e5b30ae05c"
dependencies = [
"compiler_builtins",
"lazy_static",
"rustc-std-workspace-core",
]

[[package]]
name = "xous"
version = "0.9.28"
Expand Down
212 changes: 150 additions & 62 deletions library/std/src/os/xous/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ pub enum SyscallResult {
MemoryReturned = 18,
}

#[stable(feature = "rust1", since = "1.0.0")]
#[repr(C)]
#[derive(Debug)]
pub struct Arguments {
pub a0: usize,
pub a1: usize,
pub a2: usize,
pub a3: usize,
pub a4: usize,
pub a5: usize,
pub a6: usize,
pub a7: usize,
}

#[stable(feature = "rust1", since = "1.0.0")]
pub fn lend_mut(
connection: Connection,
Expand Down Expand Up @@ -158,18 +172,31 @@ pub fn lend_mut(
};

#[cfg(target_arch = "arm")]
unsafe {
core::arch::asm!(
"svc 0",
inlateout("r0") a0,
inlateout("r1") a1,
inlateout("r2") a2,
inlateout("r3") a3 => _,
inlateout("r4") a4 => _,
inlateout("r5") a5 => _,
// inlateout("r6") a6 => _,
inlateout("r7") a7 => _,
)
let (a0, a1, a2) = {
let res = [0usize; 8];
let args = Arguments {
a0,
a1,
a2,
a3,
a4,
a5,
a6,
a7,
};

unsafe {
let args = &args as *const Arguments;
core::arch::asm!(
"svc #0",
"stm r9, {{ r0-r7 }}",
in("r0") args,
in("r9") &res,
options(nostack)
);
};

(res[0], res[1], res[2])
};

let result = a0;
Expand Down Expand Up @@ -216,18 +243,31 @@ pub fn lend(
};

#[cfg(target_arch = "arm")]
unsafe {
core::arch::asm!(
"svc 0",
inlateout("r0") a0,
inlateout("r1") a1 => _,
inlateout("r2") a2 => _,
inlateout("r3") a3 => _,
inlateout("r4") a4 => _,
inlateout("r5") a5 => _,
// inlateout("r6") a6,
inlateout("r7") a7,
)
let (a0, a6, a7) = {
let res = [0usize; 8];
let args = Arguments {
a0,
a1,
a2,
a3,
a4,
a5,
a6,
a7,
};

unsafe {
let args = &args as *const Arguments;
core::arch::asm!(
"svc #0",
"stm r9, {{ r0-r7 }}",
in("r0") args,
in("r9") &res,
options(nostack)
);
};

(res[0], res[6], res[7])
};

let result = a0;
Expand All @@ -252,9 +292,10 @@ pub fn return_memory(
let a6 = 0;
let a7 = 0;

#[cfg(target_arch = "riscv32")]
let mut result: usize;
#[cfg(target_arch = "riscv32")]
let mut error: usize;

#[cfg(target_arch = "riscv32")]
unsafe {
core::arch::asm!(
Expand All @@ -269,19 +310,33 @@ pub fn return_memory(
inlateout("a7") a7 => _,
)
};

#[cfg(target_arch = "arm")]
unsafe {
core::arch::asm!(
"svc 0",
inlateout("r0") a0 => result,
inlateout("r1") a1 => error,
inlateout("r2") memory.as_ptr() => _,
inlateout("r3") memory.len()=> _,
inlateout("r4") arg1 => _,
inlateout("r5") arg2 => _,
// inlateout("r6") a6 => _,
inlateout("r7") a7 => _,
)
let (result, error) = {
let res = [0usize; 8];
let args = Arguments {
a0,
a1,
a2: memory.as_ptr() as _,
a3: memory.len(),
a4: arg1,
a5: arg2,
a6,
a7,
};

unsafe {
let args = &args as *const Arguments;
core::arch::asm!(
"svc #0",
"stm r9, {{ r0-r7 }}",
in("r0") args,
in("r9") &res,
options(nostack)
);
};

(res[0], res[1])
};

if result == SyscallResult::MemoryReturned as usize {
Expand All @@ -302,9 +357,10 @@ pub fn return_scalar(message_id: MessageId, args: [usize; 5]) -> Result<(), usiz
let a6 = args.get(4).map(|v| *v).unwrap_or_default();
let a7 = 0;

#[cfg(target_arch = "riscv32")]
let mut result: usize;
#[cfg(target_arch = "riscv32")]
let mut error: usize;

#[cfg(target_arch = "riscv32")]
unsafe {
core::arch::asm!(
Expand All @@ -319,20 +375,35 @@ pub fn return_scalar(message_id: MessageId, args: [usize; 5]) -> Result<(), usiz
inlateout("a7") a7 => _,
)
};

#[cfg(target_arch = "arm")]
unsafe {
core::arch::asm!(
"svc 0",
inlateout("r0") a0 => result,
inlateout("r1") a1 => error,
inlateout("r2") a2 => _,
inlateout("r3") a3 => _,
inlateout("r4") a4 => _,
inlateout("r5") a5 => _,
// inlateout("r6") a6 => _,
inlateout("r7") a7 => _,
)
let (result, error) = {
let res = [0usize; 8];
let args = Arguments {
a0,
a1,
a2,
a3,
a4,
a5,
a6,
a7,
};

unsafe {
let args = &args as *const Arguments;
core::arch::asm!(
"svc #0",
"stm r9, {{ r0-r7 }}",
in("r0") args,
in("r9") &res,
options(nostack)
);
};

(res[0], res[1])
};

if result == SyscallResult::Ok as usize {
Ok(())
} else {
Expand All @@ -351,7 +422,9 @@ pub fn connect(address: ServerAddress) -> Result<Connection, usize> {
let a6 = 0;
let a7 = 0;

#[cfg(target_arch = "riscv32")]
let mut result: usize;
#[cfg(target_arch = "riscv32")]
let mut value: usize;

#[cfg(target_arch = "riscv32")]
Expand All @@ -368,20 +441,35 @@ pub fn connect(address: ServerAddress) -> Result<Connection, usize> {
inlateout("a7") a7 => _,
)
};

#[cfg(target_arch = "arm")]
unsafe {
core::arch::asm!(
"svc 0",
inlateout("r0") a0 => result,
inlateout("r1") a1 => value,
inlateout("r2") a2 => _,
inlateout("r3") a3 => _,
inlateout("r4") a4 => _,
inlateout("r5") a5 => _,
// inlateout("r6") a6 => _,
inlateout("r7") a7 => _,
)
let (result, value) = {
let res = [0usize; 8];
let args = Arguments {
a0,
a1,
a2,
a3,
a4,
a5,
a6,
a7,
};

unsafe {
let args = &args as *const Arguments;
core::arch::asm!(
"svc #0",
"stm r9, {{ r0-r7 }}",
in("r0") args,
in("r9") &res,
options(nostack)
);
};

(res[0], res[1])
};

if result == SyscallResult::ConnectionID as usize {
Ok(value.try_into().unwrap())
} else {
Expand Down
Loading

0 comments on commit d5381e3

Please sign in to comment.