Skip to content

Commit c5d033e

Browse files
bors[bot]MikaelUrankar
andauthored
Merge #1120
1120: Port to FreeBSD r=MarkMcCaskey a=syrusakbary <!-- Prior to submitting a PR, review the CONTRIBUTING.md document for recommendations on how to test: https://github.com/wasmerio/wasmer/blob/master/CONTRIBUTING.md#pull-requests --> # Description I disovered this great patch today: https://github.com/MikaelUrankar/webasm/blob/master/www/wasmer/files/patch-freebsd And then realized that @MikaelUrankar forked wasmer to add support for FreeBSD. I'm creating this PR to follow up on that fork and merge the changes upstream. @MikaelUrankar let us know if everything is good to go with this PR! <!-- Provide details regarding the change including motivation, links to related issues, and the context of the PR. --> # Review - [ ] Add a short description of the the change to the CHANGELOG.md file Co-authored-by: MikaelUrankar <mikael.urankar@gmail.com>
2 parents 589a994 + fcbdada commit c5d033e

12 files changed

Lines changed: 509 additions & 12 deletions

File tree

lib/clif-backend/src/libcalls.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ pub extern "C" fn nearbyintf64(x: f64) -> f64 {
7777
}
7878

7979
// FIXME: Is there a replacement on AArch64?
80-
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
80+
#[cfg(all(
81+
any(target_os = "freebsd", target_os = "linux"),
82+
target_arch = "aarch64"
83+
))]
8184
#[no_mangle]
8285
pub extern "C" fn __rust_probestack() {}

lib/clif-backend/src/signal/unix.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,84 @@ pub unsafe fn do_unwind(signum: i32, siginfo: *const c_void, ucontext: *const c_
141141
longjmp(jmp_buf as *mut ::nix::libc::c_void, signum)
142142
}
143143

144+
#[cfg(all(target_os = "freebsd", target_arch = "aarch64"))]
145+
unsafe fn get_faulting_addr_and_ip(
146+
_siginfo: *const c_void,
147+
_ucontext: *const c_void,
148+
) -> (*const c_void, *const c_void) {
149+
(::std::ptr::null(), ::std::ptr::null())
150+
}
151+
152+
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
153+
unsafe fn get_faulting_addr_and_ip(
154+
siginfo: *const c_void,
155+
ucontext: *const c_void,
156+
) -> (*const c_void, *const c_void) {
157+
#[repr(C)]
158+
pub struct ucontext_t {
159+
uc_sigmask: libc::sigset_t,
160+
uc_mcontext: mcontext_t,
161+
uc_link: *mut ucontext_t,
162+
uc_stack: libc::stack_t,
163+
uc_flags: i32,
164+
__spare__: [i32; 4],
165+
}
166+
167+
#[repr(C)]
168+
pub struct mcontext_t {
169+
mc_onstack: u64,
170+
mc_rdi: u64,
171+
mc_rsi: u64,
172+
mc_rdx: u64,
173+
mc_rcx: u64,
174+
mc_r8: u64,
175+
mc_r9: u64,
176+
mc_rax: u64,
177+
mc_rbx: u64,
178+
mc_rbp: u64,
179+
mc_r10: u64,
180+
mc_r11: u64,
181+
mc_r12: u64,
182+
mc_r13: u64,
183+
mc_r14: u64,
184+
mc_r15: u64,
185+
mc_trapno: u32,
186+
mc_fs: u16,
187+
mc_gs: u16,
188+
mc_addr: u64,
189+
mc_flags: u32,
190+
mc_es: u16,
191+
mc_ds: u16,
192+
mc_err: u64,
193+
mc_rip: u64,
194+
mc_cs: u64,
195+
mc_rflags: u64,
196+
mc_rsp: u64,
197+
mc_ss: u64,
198+
mc_len: i64,
199+
200+
mc_fpformat: i64,
201+
mc_ownedfp: i64,
202+
mc_fpstate: [i64; 64], // mc_fpstate[0] is a pointer to savefpu
203+
204+
mc_fsbase: u64,
205+
mc_gsbase: u64,
206+
207+
mc_xfpustate: u64,
208+
mc_xfpustate_len: u64,
209+
210+
mc_spare: [i64; 4],
211+
}
212+
213+
let siginfo = siginfo as *const siginfo_t;
214+
let si_addr = (*siginfo).si_addr;
215+
216+
let ucontext = ucontext as *const ucontext_t;
217+
let rip = (*ucontext).uc_mcontext.mc_rip;
218+
219+
(si_addr, rip as _)
220+
}
221+
144222
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
145223
unsafe fn get_faulting_addr_and_ip(
146224
_siginfo: *const c_void,
@@ -239,6 +317,8 @@ unsafe fn get_faulting_addr_and_ip(
239317
}
240318

241319
#[cfg(not(any(
320+
all(target_os = "freebsd", target_arch = "aarch64"),
321+
all(target_os = "freebsd", target_arch = "x86_64"),
242322
all(target_os = "macos", target_arch = "x86_64"),
243323
all(target_os = "linux", target_arch = "x86_64"),
244324
all(target_os = "linux", target_arch = "aarch64"),

lib/emscripten/src/syscalls/unix.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,19 @@ extern "C" {
130130
pub fn lstat64(path: *const libc::c_char, buf: *mut c_void) -> c_int;
131131
}
132132

133-
#[cfg(not(target_os = "macos"))]
133+
// Linking to functions that are not provided by rust libc
134+
#[cfg(target_os = "freebsd")]
135+
#[link(name = "c")]
136+
extern "C" {
137+
pub fn wait4(pid: pid_t, status: *mut c_int, options: c_int, rusage: *mut rusage) -> pid_t;
138+
pub fn fdatasync(fd: c_int) -> c_int;
139+
pub fn ftruncate(fd: c_int, length: i64) -> c_int;
140+
pub fn lstat(path: *const libc::c_char, buf: *mut stat) -> c_int;
141+
}
142+
143+
#[cfg(target_os = "freebsd")]
144+
use libc::madvise;
145+
#[cfg(not(any(target_os = "freebsd", target_os = "macos")))]
134146
use libc::{fallocate, fdatasync, ftruncate64, lstat, madvise, wait4};
135147

136148
// Another conditional constant for name resolution: Macos et iOS use
@@ -255,10 +267,14 @@ pub fn ___syscall194(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in
255267
debug!("emscripten::___syscall194 (ftruncate64) {}", _which);
256268
let _fd: c_int = varargs.get(ctx);
257269
let _length: i64 = varargs.get(ctx);
258-
#[cfg(not(target_os = "macos"))]
270+
#[cfg(not(any(target_os = "freebsd", target_os = "macos")))]
259271
unsafe {
260272
ftruncate64(_fd, _length)
261273
}
274+
#[cfg(target_os = "freebsd")]
275+
unsafe {
276+
ftruncate(_fd, _length)
277+
}
262278
#[cfg(target_os = "macos")]
263279
unimplemented!("emscripten::___syscall194 (ftruncate64) {}", _which)
264280
}
@@ -621,7 +637,7 @@ pub fn ___syscall102(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in
621637
let mut host_address: sockaddr = sockaddr {
622638
sa_family: Default::default(),
623639
sa_data: Default::default(),
624-
#[cfg(target_os = "macos")]
640+
#[cfg(any(target_os = "freebsd", target_os = "macos"))]
625641
sa_len: Default::default(),
626642
};
627643
let fd = unsafe { accept(socket, &mut host_address, address_len_addr) };
@@ -655,7 +671,7 @@ pub fn ___syscall102(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in
655671
let mut sock_addr_host: sockaddr = sockaddr {
656672
sa_family: Default::default(),
657673
sa_data: Default::default(),
658-
#[cfg(target_os = "macos")]
674+
#[cfg(any(target_os = "freebsd", target_os = "macos"))]
659675
sa_len: Default::default(),
660676
};
661677
let ret = unsafe {
@@ -1056,8 +1072,17 @@ pub fn ___syscall220(ctx: &mut Ctx, _which: i32, mut varargs: VarArgs) -> i32 {
10561072
break;
10571073
}
10581074
#[allow(clippy::cast_ptr_alignment)]
1075+
#[cfg(not(target_os = "freebsd"))]
10591076
unsafe {
10601077
*(dirp.add(pos) as *mut u32) = (*dirent).d_ino as u32;
1078+
}
1079+
#[allow(clippy::cast_ptr_alignment)]
1080+
#[cfg(target_os = "freebsd")]
1081+
unsafe {
1082+
*(dirp.add(pos) as *mut u32) = (*dirent).d_fileno as u32;
1083+
}
1084+
#[allow(clippy::cast_ptr_alignment)]
1085+
unsafe {
10611086
*(dirp.add(pos + 4) as *mut u32) = pos as u32;
10621087
*(dirp.add(pos + 8) as *mut u16) = offset as u16;
10631088
*(dirp.add(pos + 10) as *mut u8) = (*dirent).d_type;
@@ -1106,11 +1131,11 @@ pub fn ___syscall324(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in
11061131
let _mode: c_int = varargs.get(ctx);
11071132
let _offset: off_t = varargs.get(ctx);
11081133
let _len: off_t = varargs.get(ctx);
1109-
#[cfg(not(target_os = "macos"))]
1134+
#[cfg(not(any(target_os = "freebsd", target_os = "macos")))]
11101135
unsafe {
11111136
fallocate(_fd, _mode, _offset, _len)
11121137
}
1113-
#[cfg(target_os = "macos")]
1138+
#[cfg(any(target_os = "freebsd", target_os = "macos"))]
11141139
{
11151140
unimplemented!("emscripten::___syscall324 (fallocate) {}", _which)
11161141
}

lib/emscripten/src/time.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ use wasmer_runtime_core::vm::Ctx;
3131
#[cfg(target_os = "linux")]
3232
use libc::{CLOCK_MONOTONIC, CLOCK_MONOTONIC_COARSE, CLOCK_REALTIME};
3333

34+
#[cfg(target_os = "freebsd")]
35+
use libc::{CLOCK_MONOTONIC, CLOCK_REALTIME};
36+
#[cfg(target_os = "freebsd")]
37+
const CLOCK_MONOTONIC_COARSE: clockid_t = 6;
38+
3439
#[cfg(target_os = "macos")]
3540
use libc::CLOCK_REALTIME;
3641
#[cfg(target_os = "macos")]

lib/llvm-backend/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ lazy_static! {
5858
"llvm-config".into(),
5959
format!("llvm-config-{}", CRATE_VERSION.major),
6060
format!("llvm-config-{}.{}", CRATE_VERSION.major, CRATE_VERSION.minor),
61+
format!("llvm-config{}{}", CRATE_VERSION.major, CRATE_VERSION.minor),
6162
]
6263
};
6364

lib/llvm-backend/src/backend.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ impl LLVMBackend {
206206

207207
let buffer = Arc::new(Buffer::LlvmMemory(memory_buffer));
208208

209-
#[cfg(all(any(target_os = "linux", target_os = "macos"), target_arch = "x86_64"))]
209+
#[cfg(all(
210+
any(target_os = "freebsd", target_os = "linux", target_os = "macos"),
211+
target_arch = "x86_64"
212+
))]
210213
{
211214
use super::stackmap::{self, StkMapRecord, StkSizeRecord};
212215
use std::collections::BTreeMap;

lib/llvm-backend/src/stackmap.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ pub enum StackmapEntryKind {
5353
}
5454

5555
impl StackmapEntry {
56-
#[cfg(all(any(target_os = "linux", target_os = "macos"), target_arch = "x86_64"))]
56+
#[cfg(all(
57+
any(target_os = "freebsd", target_os = "linux", target_os = "macos"),
58+
target_arch = "x86_64"
59+
))]
5760
pub fn populate_msm(
5861
&self,
5962
module_info: &ModuleInfo,

lib/runtime-core/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ fn main() {
3232
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
3333

3434
match (target_os.as_str(), target_arch.as_str()) {
35+
("freebsd", "x86_64") => {
36+
cc::Build::new()
37+
.file("image-loading-freebsd-x86-64.s")
38+
.compile("image-loading");
39+
}
3540
("linux", "x86_64") => {
3641
cc::Build::new()
3742
.file("image-loading-linux-x86-64.s")
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# NOTE: Keep this consistent with `fault.rs`.
2+
3+
.globl run_on_alternative_stack
4+
run_on_alternative_stack:
5+
# (stack_end, stack_begin)
6+
# We need to ensure 16-byte alignment here.
7+
pushq %r15
8+
pushq %r14
9+
pushq %r13
10+
pushq %r12
11+
pushq %rbx
12+
pushq %rbp
13+
movq %rsp, -16(%rdi)
14+
15+
leaq run_on_alternative_stack.returning(%rip), %rax
16+
movq %rax, -24(%rdi)
17+
18+
movq %rsi, %rsp
19+
20+
movq (%rsp), %xmm0
21+
add $8, %rsp
22+
23+
movq (%rsp), %xmm1
24+
add $8, %rsp
25+
26+
movq (%rsp), %xmm2
27+
add $8, %rsp
28+
29+
movq (%rsp), %xmm3
30+
add $8, %rsp
31+
32+
movq (%rsp), %xmm4
33+
add $8, %rsp
34+
35+
movq (%rsp), %xmm5
36+
add $8, %rsp
37+
38+
movq (%rsp), %xmm6
39+
add $8, %rsp
40+
41+
movq (%rsp), %xmm7
42+
add $8, %rsp
43+
44+
movq (%rsp), %xmm8
45+
add $8, %rsp
46+
47+
movq (%rsp), %xmm9
48+
add $8, %rsp
49+
50+
movq (%rsp), %xmm10
51+
add $8, %rsp
52+
53+
movq (%rsp), %xmm11
54+
add $8, %rsp
55+
56+
movq (%rsp), %xmm12
57+
add $8, %rsp
58+
59+
movq (%rsp), %xmm13
60+
add $8, %rsp
61+
62+
movq (%rsp), %xmm14
63+
add $8, %rsp
64+
65+
movq (%rsp), %xmm15
66+
add $8, %rsp
67+
68+
popq %rbp
69+
popq %rax
70+
popq %rbx
71+
popq %rcx
72+
popq %rdx
73+
popq %rdi
74+
popq %rsi
75+
popq %r8
76+
popq %r9
77+
popq %r10
78+
popq %r11
79+
popq %r12
80+
popq %r13
81+
popq %r14
82+
popq %r15
83+
retq
84+
85+
run_on_alternative_stack.returning:
86+
movq (%rsp), %rsp
87+
popq %rbp
88+
popq %rbx
89+
popq %r12
90+
popq %r13
91+
popq %r14
92+
popq %r15
93+
retq
94+
95+
# For switching into a backend without information about where registers are preserved.
96+
.globl register_preservation_trampoline
97+
register_preservation_trampoline:
98+
subq $8, %rsp
99+
pushq %rax
100+
pushq %rcx
101+
pushq %rdx
102+
pushq %rdi
103+
pushq %rsi
104+
pushq %r8
105+
pushq %r9
106+
pushq %r10
107+
108+
callq get_boundary_register_preservation@PLT
109+
110+
# Keep this consistent with BoundaryRegisterPreservation
111+
movq %r15, 0(%rax)
112+
movq %r14, 8(%rax)
113+
movq %r13, 16(%rax)
114+
movq %r12, 24(%rax)
115+
movq %rbx, 32(%rax)
116+
117+
popq %r10
118+
popq %r9
119+
popq %r8
120+
popq %rsi
121+
popq %rdi
122+
popq %rdx
123+
popq %rcx
124+
popq %rax
125+
addq $8, %rsp
126+
127+
jmpq *%rax

0 commit comments

Comments
 (0)