Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit eae5189

Browse files
awortman-fastlyiximeow
authored andcommitted
wiggle things around to use pinned heap reg
1 parent d78cccf commit eae5189

9 files changed

Lines changed: 26 additions & 7 deletions

File tree

benchmarks/lucet-benchmarks/src/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn context_init(c: &mut Criterion) {
99

1010
c.bench_function("context_init", move |b| {
1111
b.iter(|| {
12-
ContextHandle::create_and_init(&mut *stack, f as usize, &[]).unwrap();
12+
ContextHandle::create_and_init(&mut *stack, f as usize, &[], std::ptr::null_mut()).unwrap();
1313
})
1414
});
1515
}
@@ -22,7 +22,7 @@ fn context_swap_return(c: &mut Criterion) {
2222
b.iter_batched(
2323
|| {
2424
let mut stack = vec![0u64; 1024].into_boxed_slice();
25-
let child = ContextHandle::create_and_init(&mut *stack, f as usize, &[]).unwrap();
25+
let child = ContextHandle::create_and_init(&mut *stack, f as usize, &[], std::ptr::null_mut()).unwrap();
2626
(stack, child)
2727
},
2828
|(stack, mut child)| unsafe {
@@ -45,7 +45,7 @@ fn context_init_swap_return(c: &mut Criterion) {
4545
|mut stack| {
4646
let mut parent = ContextHandle::new();
4747
let mut child =
48-
ContextHandle::create_and_init(&mut *stack, f as usize, &[]).unwrap();
48+
ContextHandle::create_and_init(&mut *stack, f as usize, &[], std::ptr::null_mut()).unwrap();
4949
unsafe { Context::swap(&mut parent, &mut child) };
5050
stack
5151
},
@@ -333,7 +333,7 @@ fn context_init_swap_return_many_args(c: &mut Criterion) {
333333
|mut stack| {
334334
let mut parent = ContextHandle::new();
335335
let mut child =
336-
ContextHandle::create_and_init(&mut *stack, f as usize, &args).unwrap();
336+
ContextHandle::create_and_init(&mut *stack, f as usize, &args, std::ptr::null_mut()).unwrap();
337337
unsafe { Context::swap(&mut parent, &mut child) };
338338
stack
339339
},

lucet-runtime/lucet-runtime-internals/src/alloc/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ macro_rules! alloc_tests {
610610
inst.alloc_mut().stack_u64_mut(),
611611
heap_touching_child as usize,
612612
&[Val::CPtr(heap_ptr)],
613+
heap_ptr,
613614
)
614615
.expect("context init succeeds");
615616
Context::swap(&mut parent, &mut child);
@@ -659,6 +660,7 @@ macro_rules! alloc_tests {
659660
inst.alloc_mut().stack_u64_mut(),
660661
stack_pattern_child as usize,
661662
&[Val::CPtr(heap_ptr)],
663+
heap_ptr,
662664
)
663665
.expect("context init succeeds");
664666
Context::swap(&mut parent, &mut child);

lucet-runtime/lucet-runtime-internals/src/context/context_asm.S

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ _lucet_context_swap:
118118
mov %r14, (6*8)(%rdi)
119119
mov %r15, (7*8)(%rdi)
120120
mov %rsi, (8*8)(%rdi)
121+
mov %r8, (9*8)(%rdi)
121122

122123
movdqu %xmm0, (10*8 + 0*16)(%rdi)
123124
movdqu %xmm1, (10*8 + 1*16)(%rdi)
@@ -137,6 +138,7 @@ _lucet_context_swap:
137138
mov (5*8)(%rsi), %r13
138139
mov (6*8)(%rsi), %r14
139140
mov (7*8)(%rsi), %r15
141+
mov (9*8)(%rsi), %r8
140142

141143
movdqu (10*8 + 0*16)(%rsi), %xmm0
142144
movdqu (10*8 + 1*16)(%rsi), %xmm1

lucet-runtime/lucet-runtime-internals/src/context/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(crate) struct GpRegs {
3535
r14: u64,
3636
r15: u64,
3737
pub(crate) rsi: u64,
38+
r8: u64
3839
}
3940

4041
impl GpRegs {
@@ -49,6 +50,7 @@ impl GpRegs {
4950
r14: 0,
5051
r15: 0,
5152
rsi: 0,
53+
r8: 0,
5254
}
5355
}
5456
}
@@ -206,9 +208,10 @@ impl ContextHandle {
206208
stack: &mut [u64],
207209
fptr: usize,
208210
args: &[Val],
211+
heap: *mut core::ffi::c_void,
209212
) -> Result<ContextHandle, Error> {
210213
let mut child = ContextHandle::new();
211-
Context::init(stack, &mut child, fptr, args)?;
214+
Context::init(stack, &mut child, fptr, args, heap)?;
212215
Ok(child)
213216
}
214217
}
@@ -365,6 +368,7 @@ impl Context {
365368
child: &mut Context,
366369
fptr: usize,
367370
args: &[Val],
371+
heap: *mut core::ffi::c_void,
368372
) -> Result<(), Error> {
369373
Context::init_with_callback(
370374
stack,
@@ -373,6 +377,7 @@ impl Context {
373377
ptr::null_mut(),
374378
fptr,
375379
args,
380+
heap,
376381
)
377382
}
378383

@@ -391,6 +396,7 @@ impl Context {
391396
backstop_data: *mut Instance,
392397
fptr: usize,
393398
args: &[Val],
399+
heap: *mut core::ffi::c_void,
394400
) -> Result<(), Error> {
395401
if !stack_is_aligned(stack) {
396402
return Err(Error::UnalignedStack);
@@ -471,6 +477,9 @@ impl Context {
471477

472478
child.gpr.rbp = child as *const Context as u64;
473479

480+
// testing out heap pinning
481+
child.gpr.r15 = heap as u64;
482+
474483
// Read the mask to be restored if we ever need to jump out of a signal handler. If this
475484
// isn't possible, die.
476485
signal::pthread_sigmask(

lucet-runtime/lucet-runtime-internals/src/context/tests/c_child.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ macro_rules! init_and_swap {
5555
&mut *$stack,
5656
$fn as usize,
5757
&[$( $args ),*],
58+
std::ptr::null_mut(),
5859
).unwrap()));
5960

6061
child_regs = child;

lucet-runtime/lucet-runtime-internals/src/context/tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn init_rejects_unaligned() {
3131
let mut stack_unaligned = unsafe { slice::from_raw_parts_mut(ptr, len) };
3232

3333
// now we have the unaligned stack, let's make sure it blows up right
34-
let res = ContextHandle::create_and_init(&mut stack_unaligned, dummy as usize, &[]);
34+
let res = ContextHandle::create_and_init(&mut stack_unaligned, dummy as usize, &[], std::ptr::null_mut());
3535

3636
if let Err(Error::UnalignedStack) = res {
3737
assert!(true);

lucet-runtime/lucet-runtime-internals/src/context/tests/rust_child.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ macro_rules! init_and_swap {
5151
&mut *$stack,
5252
$fn as usize,
5353
&[$( $args ),*],
54+
std::ptr::null_mut(),
5455
).unwrap();
5556
CHILD = Some(child);
5657

lucet-runtime/lucet-runtime-internals/src/instance.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,8 @@ impl Instance {
843843

844844
self.entrypoint = Some(func.ptr);
845845

846-
let mut args_with_vmctx = vec![Val::from(self.alloc.slot().heap)];
846+
let heap = self.alloc.slot().heap;
847+
let mut args_with_vmctx = vec![Val::from(heap)];
847848
args_with_vmctx.extend_from_slice(args);
848849

849850
let self_ptr = self as *mut _;
@@ -854,6 +855,7 @@ impl Instance {
854855
self_ptr,
855856
func.ptr.as_usize(),
856857
&args_with_vmctx,
858+
heap,
857859
)?;
858860

859861
// Set up the guest to set itself as terminable, then continue to

lucetc/src/compiler.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ impl<'a> Compiler<'a> {
253253
let isa_builder = cpu_features.isa_builder(target)?;
254254
flags_builder.enable("enable_verifier").unwrap();
255255
flags_builder.enable("is_pic").unwrap();
256+
flags_builder.enable("enable_pinned_reg");
257+
flags_builder.enable("use_pinned_reg_as_heap_base");
256258
flags_builder.set("opt_level", opt_level.to_flag()).unwrap();
257259
if canonicalize_nans {
258260
flags_builder.enable("enable_nan_canonicalization").unwrap();

0 commit comments

Comments
 (0)