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
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ jobs:
key: cargo-${{ runner.os }}-${{ env.RUST_TOOLCHAIN }}-${{ hashFiles('overlay/rust/kemu_core/src/**', 'tools/*/src/**', 'tools/*/Cargo.toml') }}
restore-keys: cargo-${{ runner.os }}-${{ env.RUST_TOOLCHAIN }}-
- name: kemu_core unit tests
run: cargo test --manifest-path overlay/rust/kemu_core/Cargo.toml --features std
run: |
cargo test --manifest-path overlay/rust/kemu_core/Cargo.toml --features std
cargo test --manifest-path tools/kemu-user/Cargo.toml
- name: Build tools
run: |
for t in kemu-user jit-test difftest isa-audit; do
Expand All @@ -51,6 +53,12 @@ jobs:
INTR=20 RELOC=1 $j 10000 10 8
NO_VCACHE=1 INTR=20 RELOC=1 $j 10000 10 8
INTR=20 RELOC=1 $j 10000 32 3
INTR=20 RELOC=1 $j 10000 11 9
NO_VCACHE=1 INTR=20 RELOC=1 $j 10000 11 9
INTR=20 RELOC=1 $j 10000 12 10
NO_VCACHE=1 INTR=20 RELOC=1 $j 10000 12 10
INTR=20 RELOC=1 $j 10000 13 11
NO_VCACHE=1 INTR=20 RELOC=1 $j 10000 13 11
- name: kemu-user smoke test
run: |
u=tools/kemu-user/target/release/kemu-user
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ __pycache__/
tests/kernel/tbi
tests/kernel/vector-state
tests/kernel/syscall-state
tests/kernel/fp-status
55 changes: 53 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,15 @@ The native implementation lives in `overlay/arch/x86/`; other x86_64 kernel
ports supply their own architecture code and select `HAVE_KEMU`.

To enable the vector register cache, a port defines `KEMU_ARCH_VECTOR_CACHE`
as 1 and provides `kemu_arch_read_xmm(u64 xmm[8][2])` in `asm/kemu.h`. It must
capture the interrupted task's user XMM0..7, synchronizing live FPU state
as 1 and provides `u32 kemu_arch_read_fpu(u64 xmm[8][2])` in `asm/kemu.h`. It must
capture the interrupted task's user XMM0..7 and return MXCSR, synchronizing live FPU state
first, and work in task context with IRQs either enabled or disabled. Without
this opt-in, vectors remain in the state page. The native implementation
handles lazy FPU state and XSAVE's init-state optimization. Userspace callers
opt in with `Cfg::vector_cache` and supply `HostRegs::xmm` during recovery.
Recovery must also merge live MXCSR exception flags into FPSR; in page-state
stub ranges, use the saved MXCSR image instead (live state may precede a reload
or signal return).

Host atomic access, the virtual counter and random-number generation use
the `kemu_arch_*` services declared in `include/linux/kemu.h`. The common
Expand Down Expand Up @@ -183,6 +186,9 @@ INTR=50 RELOC=1 tools/jit-test/target/release/jit-test 50000 # async interrupt
INTR=50 RELOC=1 tools/jit-test/target/release/jit-test 20000 1 6 # SHA256, with no interpreter fallback
INTR=50 RELOC=1 tools/jit-test/target/release/jit-test 20000 1 7 # AES, PMULL, SHA1
NO_AES=1 NO_PCLMUL=1 tools/jit-test/target/release/jit-test 20000 1 7 # optional-feature fallback
INTR=20 RELOC=1 tools/jit-test/target/release/jit-test 10000 1 9 # FP min/max and indexed multiply/FMA
INTR=20 RELOC=1 tools/jit-test/target/release/jit-test 10000 1 10 # TBL/TBX and byte/halfword ZIP/UZP
INTR=20 RELOC=1 tools/jit-test/target/release/jit-test 10000 1 11 # full FP catalog, exact status and native-path checks
scripts/vm-smoke.sh # in-kernel ABI suite etc.
```

Expand All @@ -204,6 +210,51 @@ The benchmark warms translations before timing, reports time per crypto
instruction including loop/operand overhead, and prints deterministic
checksums. It is not a complete cipher or hash throughput test.

`tests/bench/fp.c` exercises SAXPY, a double dot product, 48×48 matrix
multiplication, clamping, range counting, Mandelbrot, SIMD square roots,
quantization, and rounding. `tests/bench/zlib.c`
measures compression (level 6), decompression, Adler-32, and CRC-32 over
deterministic mixed text/random data, and verifies the compression round trip.
Both accept a case name (or `all`) and iteration count; zlib also accepts the
buffer size in bytes (up to its 1 MiB default).

```sh
aarch64-linux-gnu-gcc -O3 -fno-math-errno -static tests/bench/fp.c -lm -o build/fp.arm64
# ZLIB_ARM64 is an extracted arm64 zlib development package or sysroot.
aarch64-linux-gnu-gcc -O3 -static tests/bench/zlib.c -I$ZLIB_ARM64/usr/include \
$ZLIB_ARM64/usr/lib/aarch64-linux-gnu/libz.a -o build/zlib.arm64
qemu-aarch64 build/fp.arm64 all 20
qemu-aarch64 build/zlib.arm64 all 2 131072
tools/kemu-user/target/release/kemu-user --jit build/fp.arm64 all 20
tools/kemu-user/target/release/kemu-user --jit build/zlib.arm64 all 2 131072
```

Use the same binaries on baseline and patched kernels and compare checksums
against QEMU. For a native comparison, compile the same sources with host GCC
and the same optimization level, linking the same zlib version, and run them
in the same VM. Compiler vectorization can differ across architectures.
Run helper profiling separately with `KEMU_JIT_STATS=1`; its per-helper
formatting and counters distort timings.

T1 lowers every currently decoded S/D FP instruction family: scalar and SIMD
arithmetic, comparisons (including conditional comparisons), rounding,
pairwise/across-lane reductions, integer/fixed-point conversions, precision
conversions, reciprocal estimates, and reciprocal refinement steps. Half
precision **storage conversions** are lowered; FP16 arithmetic is unsupported.
Packed signed int32 conversions stay packed on AVX2; 64-bit/unsigned conversion
forms use native scalar lanes and commit the vector once. Reciprocal estimates
use ARM's exact tables embedded in the translated block. Unusual inputs use
softfloat where host behavior differs: NaN propagation, FPCR.FZ/AHP,
saturation/modulo cases, and underflow/overflow boundaries. FCVTXN also uses the
helper outside its normal-result fast range. Thus instruction coverage does
not imply every input executes without a helper.

Mode 11 iterates the readable opcode catalog in `tools/jit-test/src/fp-catalog.txt`.
It checks all four rounding modes, scalar/vector arrangements, aliasing, exact
FPCR/FPSR/NZCV, exceptional inputs and inactive lanes. Ordinary-input cases
must execute without helper calls (apart from the terminating BRK). Run with
`NO_VCACHE=1` as well to check state-page and cached-vector paths.

`tests/jit-workloads/run.sh` runs JIT-heavy programs for Java, Node, LuaJIT,
PyPy and Ruby inside an arm64 userland, each with the guest's JIT on and off,
and compares the checksums with each other and with native x86_64 runs:
Expand Down
4 changes: 2 additions & 2 deletions overlay/arch/x86/include/asm/kemu.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include <asm/prctl.h>
#include <asm/proto.h>

/* Opt in only when asynchronous recovery can capture user XMM0..7. */
/* Capture user XMM0..7 and return MXCSR for asynchronous recovery. */
#define KEMU_ARCH_VECTOR_CACHE 1
void kemu_arch_read_xmm(u64 xmm[8][2]);
u32 kemu_arch_read_fpu(u64 xmm[8][2]);

#define kemu_reg(regs, name) ((regs)->name)

Expand Down
3 changes: 2 additions & 1 deletion overlay/arch/x86/kemu/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <asm/fpu/sched.h>
#include <asm/kemu.h>

void kemu_arch_read_xmm(u64 xmm[8][2])
u32 kemu_arch_read_fpu(u64 xmm[8][2])
{
struct fpu *fpu = x86_task_fpu(current);
const void *src;
Expand All @@ -36,6 +36,7 @@ void kemu_arch_read_xmm(u64 xmm[8][2])
memcpy(xmm, src, 8 * 16);
else
memset(xmm, 0, 8 * 16);
return src ? fpu->fpstate->regs.fxsave.mxcsr : 0x1f80;
}

static int kemu_cmpxchg128(u64 addr, const u64 *exp, const u64 *new, u64 *old)
Expand Down
21 changes: 16 additions & 5 deletions overlay/kernel/kemu/jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ void kemu_enter_op(struct kemu_thread *kt)
kt->flags |= KEMU_TF_KSTATE;
}

static void kemu_host_regs(struct pt_regs *regs, struct kemu_host_regs *h)
static u32 kemu_host_regs(struct pt_regs *regs, struct kemu_host_regs *h)
{
h->gpr[0] = kemu_reg(regs, ax);
h->gpr[1] = kemu_reg(regs, cx);
Expand All @@ -710,9 +710,10 @@ static void kemu_host_regs(struct pt_regs *regs, struct kemu_host_regs *h)
h->gpr[15] = kemu_reg(regs, r15);
h->rflags = kemu_reg(regs, flags);
#if KEMU_ARCH_VECTOR_CACHE
kemu_arch_read_xmm(h->xmm);
return kemu_arch_read_fpu(h->xmm);
#else
memset(h->xmm, 0, sizeof(h->xmm));
return 0;
#endif
}

Expand All @@ -739,14 +740,20 @@ void kemu_materialize(struct pt_regs *regs)
unsigned long ip = kemu_reg(regs, ip), off, c;
u8 *blk;
size_t len;
u32 mxcsr;

if (kt->flags & KEMU_TF_KSTATE)
goto out;
kemu_host_regs(regs, &h);
mxcsr = kemu_host_regs(regs, &h);
kt->shadow = *cpu;
if (ip >= km->code_base && ip < km->code_base + KEMU_STUBS_SIZE) {
kemu_rs_recover_stub(kemu_stub_info(km, ip - km->code_base),
&h, &kt->shadow);
u32 info = kemu_stub_info(km, ip - km->code_base);

kemu_rs_recover_stub(info, &h, &kt->shadow);
/* During reload (or after rt_sigreturn), live MXCSR can still
* describe the previous state. Use the page when authoritative. */
if (info == KEMU_STUB_PAGE_STATE || info == KEMU_STUB_PAGE_STATE_SVC)
mxcsr = cpu->scratch[KEMU_MXCSR_SLOT];
} else if (ip >= km->code_base && ip < km->code_base + KEMU_CODE_SIZE) {
u64 retired;

Expand Down Expand Up @@ -774,7 +781,11 @@ void kemu_materialize(struct pt_regs *regs)
} else {
/* Interpreter mode keeps the page authoritative at all times. */
kt->shadow.nzcv = kemu_rs_nzcv_from_image(kt->shadow.flags_x86);
mxcsr = cpu->scratch[KEMU_MXCSR_SLOT];
}
/* Stubs may not have spilled MXCSR yet. Preserve live cumulative
* exceptions before exit_to_user rebuilds MXCSR from FPSR. */
kt->shadow.fpsr = kemu_rs_fpsr_merge(kt->shadow.fpsr, mxcsr);
*cpu = kt->shadow;
kt->flags |= KEMU_TF_KSTATE;
out:
Expand Down
1 change: 1 addition & 0 deletions overlay/kernel/kemu/kemu_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ struct kemu_stubs {
};

#define KEMU_STUB_PAGE_STATE 0
#define KEMU_STUB_PAGE_STATE_SVC 4

struct kemu_mm {
struct mutex lock; /* translation, chunks, block map */
Expand Down
17 changes: 12 additions & 5 deletions overlay/rust/kemu_core/src/jit/t1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ struct Gen<'s, 'b> {
body_off: usize,
vectors: [u8; meta::VECTOR_REGS],
vlive: bool,
fp_tables: [Option<usize>; 2],
}

pub const MAX_REMAP: usize = 4;
Expand Down Expand Up @@ -2215,7 +2216,8 @@ fn defs_flags(i: &Insn) -> bool {
| Insn::AddSubExt { setflags, .. }
| Insn::AddSubCarry { setflags, .. } => *setflags,
Insn::LogicImm { op, .. } | Insn::LogicShift { op, .. } => *op == LogicOp::Ands,
Insn::CondCmp { .. } | Insn::FpCmp { .. } => true,
Insn::CondCmp { .. } | Insn::FpCmp { .. } | Insn::FpCCmp { .. }
| Insn::FpCvt { op: crate::decode::FcvtOp::Fjcvt, .. } => true,
_ => false,
}
}
Expand All @@ -2229,11 +2231,7 @@ fn is_native(i: &Insn) -> bool {
| Insn::Hlt { .. }
| Insn::Brk { .. }
| Insn::IcIvau { .. }
| Insn::FpCCmp { .. }
| Insn::SimdAcross { .. }
| Insn::SimdSmov { .. }
| Insn::SimdTbl { .. }
| Insn::SimdIndexed { .. }
| Insn::Rmif { .. }
| Insn::SetfFlags { .. }
| Insn::MsrImm { .. }
Expand Down Expand Up @@ -2375,6 +2373,7 @@ fn translate_n<E: Env>(env: &mut E, pc: u64, out: &mut [u8], cfg: &Cfg, s: &mut
vector::choose(&insns[..n.min(MAX_INSNS)])
} else { [meta::NO_VECTOR; meta::VECTOR_REGS] },
vlive: false,
fp_tables: [None; 2],
live_in0,
};
if jit_off() & 32 == 0 {
Expand Down Expand Up @@ -2427,9 +2426,12 @@ fn translate_n<E: Env>(env: &mut E, pc: u64, out: &mut [u8], cfg: &Cfg, s: &mut
| Insn::Fp3 { .. }
| Insn::Fp1 { .. }
| Insn::FpCmp { .. }
| Insn::FpCCmp { .. }
| Insn::FpCSel { .. }
| Insn::FpCvt { .. }
| Insn::SimdThree { .. }
| Insn::SimdAcross { .. }
| Insn::SimdIndexed { .. }
| Insn::SimdMisc { .. }
| Insn::SimdDup { .. }
| Insn::SimdScalarCopy { .. }
Expand All @@ -2441,6 +2443,7 @@ fn translate_n<E: Env>(env: &mut E, pc: u64, out: &mut [u8], cfg: &Cfg, s: &mut
| Insn::SimdInsElt { .. }
| Insn::SimdLdSt { .. }
| Insn::SimdPermute { .. }
| Insn::SimdTbl { .. }
| Insn::Sha256 { .. }
| Insn::Sha1 { .. }
| Insn::Aese { .. }
Expand Down Expand Up @@ -2602,9 +2605,12 @@ fn translate_n<E: Env>(env: &mut E, pc: u64, out: &mut [u8], cfg: &Cfg, s: &mut
Insn::Fp3 { ftype, neg, sub, rd, rn, rm, ra } => g.fp3(ftype, neg, sub, rd, rn, rm, ra),
Insn::Fp1 { ftype, op, to, rd, rn } => g.fp1(ftype, op, to, rd, rn),
Insn::FpCmp { ftype, rn, rm, zero, signal } => g.fp_cmp(ftype, rn, rm, zero, signal),
Insn::FpCCmp { ftype, rn, rm, nzcv, cond, signal } => g.fp_ccmp(ftype, rn, rm, nzcv, cond, signal),
Insn::FpCSel { ftype, rd, rn, rm, cond } => g.fp_csel(ftype, rd, rn, rm, cond),
Insn::FpCvt { ftype, sf, op, rd, rn } => g.fp_cvt(ftype, sf, op, rd, rn),
Insn::SimdThree { q, u, size, op, rd, rn, rm, scalar } => g.simd_three(q, u, size, op, rd, rn, rm, scalar),
Insn::SimdAcross { q, size, op, rd, rn } => g.fp_across(q, size, op, rd, rn),
Insn::SimdIndexed { q, u, size, opcode, rd, rn, rm, index, scalar } => g.simd_indexed(q, u, size, opcode, rd, rn, rm, index, scalar),
Insn::SimdMisc { q, u, size, op, rd, rn, scalar } => g.simd_misc(q, u, size, op, rd, rn, scalar),
Insn::SimdDup { q, imm5, rd, rn, from_gpr } => g.simd_dup(q, imm5, rd, rn, from_gpr),
Insn::SimdScalarCopy { imm5, rd, rn } => g.simd_scalar_copy(imm5, rd, rn),
Expand All @@ -2621,6 +2627,7 @@ fn translate_n<E: Env>(env: &mut E, pc: u64, out: &mut [u8], cfg: &Cfg, s: &mut
g.simd_ldst(load, q, opcode, rt, rn, rm, single, post)
}
Insn::SimdPermute { q, size, opcode, rd, rn, rm } => g.simd_permute(q, size, opcode, rd, rn, rm),
Insn::SimdTbl { q, len, tbx, rd, rn, rm } => g.simd_tbl(q, len, tbx, rd, rn, rm),
Insn::Sha256 { op, rd, rn, rm } => g.sha256(op, rd, rn, rm),
Insn::Sha1 { op, rd, rn, rm } => g.sha1(op, rd, rn, rm),
Insn::Aese { rd, rn } => g.aes(false, rd, rn),
Expand Down
Loading
Loading