Skip to content

Commit a0e4e5f

Browse files
committed
prepare wasm support for aarch64 and riscv64
1 parent d358204 commit a0e4e5f

File tree

8 files changed

+174
-2
lines changed

8 files changed

+174
-2
lines changed

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,26 @@ free-list = { version = "0.3", features = ["x86_64"] }
171171
raw-cpuid = "11"
172172
uart_16550 = "0.4"
173173
x86_64 = "0.15"
174+
<<<<<<< HEAD
174175
memory_addresses = { version = "0.2.3", default-features = false, features = [
175176
"x86_64",
176177
"conv-x86_64",
177178
] }
179+
=======
180+
wasmtime = { version = "22.0", default-features = false, features = ["runtime", "gc", "component-model"], optional = true }
181+
>>>>>>> cd1d8b6fa (prepare wasm support for aarch64 and riscv64)
178182

179183
[target.'cfg(target_arch = "aarch64")'.dependencies]
180184
aarch64 = { version = "0.0.14", default-features = false }
181185
arm-gic = { version = "0.6" }
182186
semihosting = { version = "0.1", optional = true }
187+
<<<<<<< HEAD
183188
memory_addresses = { version = "0.2.3", default-features = false, features = [
184189
"aarch64",
185190
] }
191+
=======
192+
wasmtime = { version = "22.0", default-features = false, features = ["runtime", "gc", "component-model"], optional = true }
193+
>>>>>>> cd1d8b6fa (prepare wasm support for aarch64 and riscv64)
186194

187195
[target.'cfg(target_arch = "riscv64")'.dependencies]
188196
riscv = "0.14"

src/arch/aarch64/kernel/longjmp.s

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# The code is derived from the musl implementation
2+
# of longjmp.
3+
.section .text
4+
.global longjmp
5+
longjmp:
6+
# IHI0055B_aapcs64.pdf 5.1.1, 5.1.2 callee saved registers
7+
ldp x19, x20, [x0,#0]
8+
ldp x21, x22, [x0,#16]
9+
ldp x23, x24, [x0,#32]
10+
ldp x25, x26, [x0,#48]
11+
ldp x27, x28, [x0,#64]
12+
ldp x29, x30, [x0,#80]
13+
ldr x2, [x0,#104]
14+
mov sp, x2
15+
16+
cmp w1, 0
17+
csinc w0, w1, wzr, ne
18+
br x30

src/arch/aarch64/kernel/mod.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,55 @@ use crate::arch::aarch64::mm::paging::{BasePageSize, PageSize};
3131
use crate::config::*;
3232
use crate::env;
3333

34+
const SERIAL_PORT_BAUDRATE: u32 = 115_200;
35+
36+
global_asm!(include_str!("setjmp.s"));
37+
global_asm!(include_str!("longjmp.s"));
38+
39+
pub(crate) struct Console {
40+
serial_port: SerialPort,
41+
}
42+
43+
impl Console {
44+
pub fn new() -> Self {
45+
CoreLocal::install();
46+
47+
let base = env::boot_info()
48+
.hardware_info
49+
.serial_port_base
50+
.map(|uartport| uartport.get())
51+
.unwrap_or_default()
52+
.try_into()
53+
.unwrap();
54+
55+
let serial_port = SerialPort::new(base);
56+
57+
serial_port.init(SERIAL_PORT_BAUDRATE);
58+
59+
Self { serial_port }
60+
}
61+
62+
pub fn write(&mut self, buf: &[u8]) {
63+
self.serial_port.write_buf(buf);
64+
}
65+
66+
pub fn read(&mut self) -> Option<u8> {
67+
None
68+
}
69+
70+
pub fn is_empty(&self) -> bool {
71+
true
72+
}
73+
74+
pub fn register_waker(&mut self, _waker: &Waker) {}
75+
}
76+
77+
impl Default for Console {
78+
fn default() -> Self {
79+
Self::new()
80+
}
81+
}
82+
3483
#[repr(align(8))]
3584
pub(crate) struct AlignedAtomicU32(AtomicU32);
3685

src/arch/aarch64/kernel/setjmp.s

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# The code is derived from the musl implementation
2+
# of setjmp.
3+
.section .text
4+
.global setjmp
5+
setjmp:
6+
# IHI0055B_aapcs64.pdf 5.1.1, 5.1.2 callee saved registers
7+
stp x19, x20, [x0,#0]
8+
stp x21, x22, [x0,#16]
9+
stp x23, x24, [x0,#32]
10+
stp x25, x26, [x0,#48]
11+
stp x27, x28, [x0,#64]
12+
stp x29, x30, [x0,#80]
13+
mov x2, sp
14+
str x2, [x0,#104]
15+
mov x0, #0
16+
ret

src/arch/riscv64/kernel/longjmp.s

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# The code is derived from the musl implementation
2+
# of longjmp.
3+
.section .text
4+
.global longjmp
5+
longjmp:
6+
ld s0, 0(a0)
7+
ld s1, 8(a0)
8+
ld s2, 16(a0)
9+
ld s3, 24(a0)
10+
ld s4, 32(a0)
11+
ld s5, 40(a0)
12+
ld s6, 48(a0)
13+
ld s7, 56(a0)
14+
ld s8, 64(a0)
15+
ld s9, 72(a0)
16+
ld s10, 80(a0)
17+
ld s11, 88(a0)
18+
ld sp, 96(a0)
19+
ld ra, 104(a0)
20+
21+
seqz a0, a1
22+
add a0, a0, a1
23+
ret

src/arch/riscv64/kernel/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod start;
1919
pub mod switch;
2020
pub mod systemtime;
2121
use alloc::vec::Vec;
22+
use core::arch::global_asm;
2223
use core::ptr;
2324
use core::sync::atomic::{AtomicPtr, AtomicU32, AtomicU64, Ordering};
2425

@@ -35,6 +36,41 @@ use crate::env;
3536
use crate::init_cell::InitCell;
3637
use crate::mm::physicalmem::PHYSICAL_FREE_LIST;
3738

39+
pub(crate) struct Console {}
40+
41+
impl Console {
42+
pub fn new() -> Self {
43+
CoreLocal::install();
44+
45+
Self {}
46+
}
47+
48+
pub fn write(&mut self, buf: &[u8]) {
49+
for byte in buf {
50+
sbi_rt::console_write_byte(*byte);
51+
}
52+
}
53+
54+
pub fn read(&mut self) -> Option<u8> {
55+
None
56+
}
57+
58+
pub fn is_empty(&self) -> bool {
59+
true
60+
}
61+
62+
pub fn register_waker(&mut self, _waker: &Waker) {}
63+
}
64+
65+
impl Default for Console {
66+
fn default() -> Self {
67+
Self::new()
68+
}
69+
}
70+
71+
global_asm!(include_str!("setjmp.s"));
72+
global_asm!(include_str!("longjmp.s"));
73+
3874
// Used to store information about available harts. The index of the hart in the vector
3975
// represents its CpuId and does not need to match its hart_id
4076
pub(crate) static HARTS_AVAILABLE: InitCell<Vec<usize>> = InitCell::new(Vec::new());

src/arch/riscv64/kernel/setjmp.s

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# The code is derived from the musl implementation
2+
# of setjmp.
3+
.section .text
4+
.global setjmp
5+
setjmp:
6+
sd s0, 0(a0)
7+
sd s1, 8(a0)
8+
sd s2, 16(a0)
9+
sd s3, 24(a0)
10+
sd s4, 32(a0)
11+
sd s5, 40(a0)
12+
sd s6, 48(a0)
13+
sd s7, 56(a0)
14+
sd s8, 64(a0)
15+
sd s9, 72(a0)
16+
sd s10, 80(a0)
17+
sd s11, 88(a0)
18+
sd sp, 96(a0)
19+
sd ra, 104(a0)
20+
21+
li a0, 0
22+
ret

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ mod shell;
8282
mod synch;
8383
pub mod syscalls;
8484
pub mod time;
85-
#[cfg(all(target_arch = "x86_64", feature = "wasm"))]
85+
#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64"), feature = "wasm"))]
8686
mod wasm;
8787

8888
mod built_info {
@@ -149,7 +149,7 @@ extern "C" fn initd(_arg: usize) {
149149
#[cfg(not(test))]
150150
let (argc, argv, environ) = syscalls::get_application_parameters();
151151

152-
#[cfg(all(target_arch = "x86_64", feature = "wasm"))]
152+
#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64"), feature = "wasm"))]
153153
if crate::wasm::init().is_err() {
154154
error!("Unable to initialized wasm support")
155155
}

0 commit comments

Comments
 (0)