Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ tests/kernel/lzmat
__pycache__/
tests/kernel/tbi
tests/kernel/vector-state
tests/kernel/syscall-state
65 changes: 56 additions & 9 deletions overlay/rust/kemu_core/src/jit/stubs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,16 @@ pub fn gen_stubs(out: &mut [u8], mode: ExitMode, table_base: u64, table_mask8: u
o.push(s2, a.pos, STUB_PINNED_LIVE);
let _ = s;

// miss: %rax = guest PC (already stored), pinned live.
// Once spill_all completes, recover from the page even before the
// host exit. A runtime can restart a syscall at its instruction with
// RCX/R11 already clobbered; those are no longer valid pinned values.
// miss: %rax = guest PC (already stored), pinned live until spilled.
a.align(16);
o.miss = a.pos as u32;
let s = a.pos;
spill_all(&mut a);
exit(&mut a, mode, OP_TRANSLATE, slot);
o.push(s, a.pos, STUB_PINNED_LIVE);
exit(&mut a, mode, OP_TRANSLATE, slot);
reload_all(&mut a);
a.jmp_rm(Rm::R(RAX));

Expand All @@ -186,8 +189,8 @@ pub fn gen_stubs(out: &mut [u8], mode: ExitMode, table_base: u64, table_mask8: u
let s = a.pos;
a.store(Sz::Q, Mem::gs(OFF_EXIT_INFO), RDX);
spill_all(&mut a);
exit(&mut a, mode, OP_CHAIN, slot);
o.push(s, a.pos, STUB_PINNED_LIVE);
exit(&mut a, mode, OP_CHAIN, slot);
reload_all(&mut a);
a.jmp_rm(Rm::R(RAX));

Expand All @@ -196,8 +199,8 @@ pub fn gen_stubs(out: &mut [u8], mode: ExitMode, table_base: u64, table_mask8: u
o.helper = a.pos as u32;
let s = a.pos;
spill_all(&mut a);
exit(&mut a, mode, OP_HELPER, slot);
o.push(s, a.pos, STUB_PINNED_LIVE);
exit(&mut a, mode, OP_HELPER, slot);
reload_all(&mut a);
a.ret();

Expand All @@ -206,8 +209,10 @@ pub fn gen_stubs(out: &mut [u8], mode: ExitMode, table_base: u64, table_mask8: u
o.svc = a.pos as u32;
let s = a.pos;
spill_all(&mut a);
exit(&mut a, mode, OP_SVC, slot);
o.push(s, a.pos, STUB_PINNED_LIVE_SVC);
let s = a.pos;
exit(&mut a, mode, OP_SVC, slot);
o.push(s, a.pos, STUB_PAGE_STATE_SVC);
reload_all(&mut a);
a.ret();

Expand Down Expand Up @@ -239,6 +244,40 @@ pub fn gen_stubs(out: &mut [u8], mode: ExitMode, table_base: u64, table_mask8: u
mod tests {
use super::*;

#[test]
fn recover_spilled_stubs_with_clobbered_syscall_registers() {
let mut page = [0u8; 4096];
let o = gen_stubs(&mut page, ExitMode::Syscall, 0x7000_1000_0000, 0xfff8).unwrap();
for (start, end, svc) in [
(o.miss, o.chain, false),
(o.chain, o.helper, false),
(o.helper, o.svc, false),
(o.svc, o.interp_svc, true),
] {
let bytes = &page[start as usize..end as usize];
let syscall = start + bytes.windows(2).position(|b| b == [0x0f, 0x05]).unwrap() as u32;
let mut saved = CpuState::new();
for (r, v) in saved.x.iter_mut().enumerate() { *v = 0x1000 + r as u64; }
saved.sp = 0x8000;
saved.pc = 0x4008c8;
saved.flags_x86 = image_from_nzcv(0xa000_0000);
let mut host = HostRegs::default();
host.gpr.fill(0xdead_beef);
// A restarted host syscall can expose the syscall return
// address and flags in the registers that pin X1 and X5.
host.gpr[RCX as usize] = 0x4000_0000 + syscall as u64 + 2;
host.gpr[R11 as usize] = 0x207;
for off in syscall - 5..end {
let mut st = saved;
recover(o.info(off), &host, &mut st);
assert_eq!(st.x, saved.x, "GPRs at stub offset {off:#x}");
assert_eq!(st.sp, saved.sp, "SP at stub offset {off:#x}");
assert_eq!(st.pc, saved.pc - if svc && off < syscall + 2 { 4 } else { 0 });
assert_eq!(st.nzcv, 0xa000_0000);
}
}
}

#[test]
fn recover_svc_entry_and_return_boundaries() {
let mut page = [0u8; 4096];
Expand All @@ -249,24 +288,32 @@ mod tests {
let mut st = CpuState::new();
st.pc = pc;
st.x[0] = 0x1234;
st.x[1] = 0x1111;
st.x[5] = 0x5555;
st.v[0] = [0x5678, 0x9abc];
st.flags_x86 = image_from_nzcv(0xa000_0000);
st
};
for (start, end, pending, pinned) in [
(o.svc, o.interp_svc, STUB_PINNED_LIVE_SVC, true),
(o.interp_svc, o.step, STUB_PAGE_STATE_SVC, false),
for (start, end, pinned) in [
(o.svc, o.interp_svc, true),
(o.interp_svc, o.step, false),
] {
let bytes = &page[start as usize..end as usize];
let syscall = start + bytes.windows(2).position(|b| b == [0x0f, 0x05]).unwrap() as u32;
// Include the syscall instruction itself: an interrupt at
// its IP is still before the guest syscall takes effect.
for off in start..syscall + 2 {
// exit() starts with a five-byte mov eax,OP_SVC; all
// guest registers have been spilled before that point.
let live = pinned && off < syscall - 5;
let pending = if live { STUB_PINNED_LIVE_SVC } else { STUB_PAGE_STATE_SVC };
assert_eq!(o.info(off), pending, "at {off:#x}");
let mut st = state(0x6404_0008);
recover(o.info(off), &host, &mut st);
assert_eq!(st.pc, 0x6404_0004, "pending SVC at {off:#x}");
assert_eq!(st.x[0], if pinned { 0xdead_beef } else { 0x1234 });
assert_eq!(st.x[0], if live { 0xdead_beef } else { 0x1234 });
assert_eq!(st.x[1], if live { 0xdead_beef } else { 0x1111 });
assert_eq!(st.x[5], if live { 0xdead_beef } else { 0x5555 });
assert_eq!(st.v[0], [0x5678, 0x9abc]);
assert_eq!(st.nzcv, 0xa000_0000);
}
Expand Down
3 changes: 2 additions & 1 deletion scripts/vm-smoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ echo "== mtstress"; ./mtstress | tail -3 | tee /tmp/mt.log; grep -q "mtstress: o
echo "== tbi"; ./tbi > /tmp/tbi.log 2>&1; grep FAIL /tmp/tbi.log; tail -1 /tmp/tbi.log
grep -q "TBI ALL PASSED" /tmp/tbi.log && echo "SMOKE ok tbi"
echo "== vector-state"; ./vector-state && echo "SMOKE ok vector-state"
echo "== syscall-state"; ./syscall-state && echo "SMOKE ok syscall-state"
echo "== intbench"; ../bench/intbench.arm64 && echo "SMOKE ok intbench"
' 2>&1 | grep -vE '^\[ |iPXE|SeaBIOS|Press|Booting|^.\[\?7l' | tee "$log"
want="hello abi tbi vector-state mtstress intbench"
want="hello abi tbi vector-state syscall-state mtstress intbench"
rc=0
for t in $want; do
grep -q "SMOKE ok $t" "$log" || { echo "FAILED: $t"; rc=1; }
Expand Down
4 changes: 2 additions & 2 deletions tests/kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
CC := aarch64-linux-gnu-gcc
CFLAGS := -O2 -Wall -Wno-unused-result

STATIC := abi auxv cat crash hello-static tbi vector-state
STATIC := abi auxv cat crash hello-static tbi vector-state syscall-state
DYN := hello-dyn mtstress

all: $(STATIC) $(DYN) ../bench/intbench.arm64

abi: abi.c
abi syscall-state: %: %.c
$(CC) $(CFLAGS) -static -pthread -o $@ $<
auxv cat crash tbi vector-state: %: %.c
$(CC) $(CFLAGS) -static -o $@ $<
Expand Down
91 changes: 91 additions & 0 deletions tests/kernel/syscall-state.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// SPDX-License-Identifier: GPL-2.0
/* Preserve guest syscall arguments across signals and JIT epoch recovery. */
#define _GNU_SOURCE
#include <assert.h>
#include <pthread.h>
#include <signal.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>

#define THREADS 4
#define ITERATIONS 100000

static atomic_uint done;
static _Thread_local volatile sig_atomic_t signals;
static struct result {
uint64_t regs[6];
unsigned pid, remaining, signals;
} results[THREADS];

static void handler(int sig)
{
(void)sig;
signals++;
}

extern unsigned syscall_loop(unsigned pid, unsigned count, uint64_t regs[6]);
__asm__(
".text\n.globl syscall_loop\n.type syscall_loop,%function\nsyscall_loop:\n"
"mov x6,x0\nmov x7,x1\nmov x9,x2\n"
"mov x1,#0x1111\nmov x2,#0x2222\nmov x3,#0x3333\n"
"mov x4,#0x4444\nmov x5,#0x5555\n"
"1: mov x8,#172\nsvc #0\n" /* getpid */
"stp x0,x1,[x9]\nstp x2,x3,[x9,#16]\nstp x4,x5,[x9,#32]\n"
"cmp x0,x6\nb.ne 2f\n"
"mov x10,#0x1111\ncmp x1,x10\nb.ne 2f\n"
"mov x10,#0x2222\ncmp x2,x10\nb.ne 2f\n"
"mov x10,#0x3333\ncmp x3,x10\nb.ne 2f\n"
"mov x10,#0x4444\ncmp x4,x10\nb.ne 2f\n"
"mov x10,#0x5555\ncmp x5,x10\nb.ne 2f\n"
"subs x7,x7,#1\nb.ne 1b\nmov x0,#0\nret\n"
"2: mov x0,x7\nret\n.size syscall_loop,.-syscall_loop\n"
);

static void *worker(void *arg)
{
uintptr_t id = (uintptr_t)arg;
struct result *r = &results[id];

r->pid = getpid();
r->remaining = syscall_loop(r->pid, ITERATIONS, r->regs);
r->signals = signals;
atomic_fetch_or(&done, 1u << id);
return NULL;
}

int main(void)
{
pthread_t threads[THREADS];
struct sigaction sa = { .sa_handler = handler };
const struct timespec pause = { .tv_nsec = 100000 };
unsigned failures = 0, total_signals = 0;

sigemptyset(&sa.sa_mask);
assert(!sigaction(SIGUSR1, &sa, NULL));
for (uintptr_t i = 0; i < THREADS; i++)
assert(!pthread_create(&threads[i], NULL, worker, (void *)i));
while (atomic_load(&done) != (1u << THREADS) - 1) {
for (unsigned i = 0; i < THREADS; i++)
if (!(atomic_load(&done) & (1u << i)))
assert(!pthread_kill(threads[i], SIGUSR1));
nanosleep(&pause, NULL);
}
for (unsigned i = 0; i < THREADS; i++) {
struct result *r = &results[i];

assert(!pthread_join(threads[i], NULL));
total_signals += r->signals;
if (!r->remaining)
continue;
failures++;
printf("FAIL syscall-state thread %u, pid %u, %u calls remaining: "
"x0..x5=%lx,%lx,%lx,%lx,%lx,%lx\n", i, r->pid, r->remaining,
r->regs[0], r->regs[1], r->regs[2], r->regs[3], r->regs[4], r->regs[5]);
}
assert(total_signals);
printf("%s syscall-state (%u signals)\n", failures ? "FAIL" : "PASS", total_signals);
return failures != 0;
}
Loading