Skip to content

Commit 8e108e6

Browse files
committedDec 6, 2022
Make it buildable for aarch64-unknown-linux-musl
This should allow testing this on linux x86-64 runners using qemu-aarch64 in user-mode emulation
1 parent 61f115c commit 8e108e6

File tree

3 files changed

+22
-55
lines changed

3 files changed

+22
-55
lines changed
 

‎.cargo/config.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.aarch64-unknown-linux-musl]
2+
linker = "aarch64-linux-gnu-gcc"

‎src/dynamic_compiler.rs

+6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use crate::mos6502;
77
core::arch::global_asm!(include_str!("dynamic_compiler.S"));
88

99
extern "C" {
10+
#[cfg(target_os = "macos")]
1011
fn jumpToEmulatedCode(ptr: *const (), state: *mut CpuState, memory: *mut u8);
12+
#[cfg(not(target_os = "macos"))]
13+
fn _jumpToEmulatedCode(ptr: *const (), state: *mut CpuState, memory: *mut u8);
1114
}
1215

1316
#[derive(Debug)]
@@ -539,7 +542,10 @@ impl Compiler {
539542
/// Must guarantee that the compiled code is valid and will naturally complete execution
540543
pub unsafe fn run(&mut self, state: &mut CpuState, memory: &mut [u8]) {
541544
self.page.run(|ptr| {
545+
#[cfg(target_os = "macos")]
542546
jumpToEmulatedCode(ptr, state as *mut CpuState, memory.as_mut_ptr());
547+
#[cfg(not(target_os = "macos"))]
548+
_jumpToEmulatedCode(ptr, state as *mut CpuState, memory.as_mut_ptr());
543549
})
544550
}
545551
}

‎src/jit.rs

+14-55
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use std::mem::MaybeUninit;
22

3-
use crate::arm_asm::{Add, Immediate, OpCode, Register, Ret, Udf};
4-
use region::{alloc, page, protect, Allocation, Protection};
5-
6-
use libc::{sigaction, SIGILL};
3+
use crate::arm_asm::{OpCode, Udf};
4+
use region::{alloc, protect, Allocation, Protection};
75

86
#[derive(Clone)]
97
pub struct Marker {
@@ -108,64 +106,25 @@ impl JitPage {
108106
}
109107
}
110108

111-
macro_rules! invoke {
112-
($jit:expr, $fn_type:ty $(, $args:expr)*) => {
113-
$jit.run(|ptr: *const _| {
114-
let func: $fn_type = std::mem::transmute_copy(&ptr);
115-
func($($args),*)
116-
})
117-
};
118-
}
119-
120-
unsafe fn handler(signal: libc::c_int, siginfo: *const libc::siginfo_t, _: *const libc::c_void) {
121-
let siginfo = &*siginfo;
122-
println!("Handled signal: {}!", signal);
123-
println!("Address: {:?}", siginfo.si_addr());
124-
libc::exit(libc::EXIT_FAILURE);
125-
}
126-
127-
unsafe fn register_sigill_handler() {
128-
let fn_ptr: unsafe fn(libc::c_int, *const libc::siginfo_t, *const libc::c_void) = handler;
129-
let action: libc::sighandler_t = std::mem::transmute_copy(&fn_ptr);
130-
let act = sigaction {
131-
sa_mask: 0,
132-
sa_flags: 0,
133-
sa_sigaction: action,
134-
};
135-
136-
let result = sigaction(SIGILL, &act as *const _, std::ptr::null_mut());
137-
if result < 0 {
138-
panic!("Error registering hander: {}", result);
139-
}
140-
}
141-
142-
pub fn run_demo() {
143-
unsafe { register_sigill_handler() };
144-
145-
let mut jit_page = JitPage::allocate(page::size()).unwrap();
146-
147-
jit_page.populate(|opcode_stream| {
148-
opcode_stream.push_opcode(
149-
Add::new(Register::X0, Register::X0)
150-
.with_immediate(Immediate::new(24))
151-
.generate(),
152-
);
153-
opcode_stream.push_opcode(Ret::new().generate());
154-
});
155-
156-
let val = unsafe { invoke!(jit_page, extern "C" fn(u64) -> u64, 10) };
157-
println!("Value {}", val);
158-
}
159-
160109
#[cfg(all(test, target_arch = "aarch64"))]
161110
mod test {
162111
use super::*;
163112
use crate::arm_asm::{
164-
Adc, And, Branch, Condition, Ldrd, MemoryAccessMode, Mov, MovShift, Movk, Movn, Movz, Mrs,
165-
Msr, OpSize, Or, RegShift, Sbc, SetF8, SignedImmediate, Strd, Sub, Sxtb, Xor, NZCV,
113+
Adc, Add, And, Branch, Condition, Immediate, Ldrd, MemoryAccessMode, Mov, MovShift, Movk,
114+
Movn, Movz, Mrs, Msr, OpSize, Or, RegShift, Register, Ret, Sbc, SetF8, SignedImmediate,
115+
Strd, Sub, Sxtb, Xor, NZCV,
166116
};
167117
use region::page;
168118

119+
macro_rules! invoke {
120+
($jit:expr, $fn_type:ty $(, $args:expr)*) => {
121+
$jit.run(|ptr: *const _| {
122+
let func: $fn_type = std::mem::transmute_copy(&ptr);
123+
func($($args),*)
124+
})
125+
};
126+
}
127+
169128
#[test]
170129
fn test_add_immediate() {
171130
let mut jit_page = JitPage::allocate(page::size()).unwrap();

0 commit comments

Comments
 (0)