Skip to content
Closed
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
2 changes: 2 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub fn build(b: *std.Build) void {
});
for (a64_guests) |name| addA64Guest(b, a64_target, name, b.fmt("test/guest/a64/{s}.c", .{name}));
for (a64_ports) |p| addA64Guest(b, a64_target, p[0], p[1]);
addA64Guest(b, a64_target, "paravirt_bench", "test/guest/a64/paravirt_bench.c");

// A64 coverage classifier (test/a64-coverage.mjs): which raw words the
// interpreter rejects as ILLEGAL, and the group each one is routed to.
Expand Down Expand Up @@ -251,6 +252,7 @@ fn kwemuModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.bu

fn addA64Guest(b: *std.Build, target: std.Build.ResolvedTarget, name: []const u8, src: []const u8) void {
const mod = b.createModule(.{ .target = target, .optimize = .ReleaseFast, .link_libc = true });
mod.addIncludePath(b.path("include"));
mod.addCSourceFile(.{ .file = b.path(src), .flags = &.{ "-std=gnu11", "-Wall", "-fno-vectorize", "-fno-slp-vectorize" } });
mod.linkSystemLibrary("m", .{});
const exe = b.addExecutable(.{ .name = b.fmt("a64-{s}", .{name}), .root_module = mod, .linkage = .static });
Expand Down
102 changes: 102 additions & 0 deletions include/kw_paravirt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
*
* Paravirt bulk-op helpers for AArch64 guests running under kwemu.
*
* Reserved permanently-undefined AArch64 UDF opcodes 0x0000_ff00..0x0000_ff0f:
* 0x0000_ff00 KW_MEMCPY x0=dst, x1=src, x2=len -> x0=dst (overlap-safe)
* 0x0000_ff01 KW_MEMSET x0=dst, x1=byte(u8), x2=len -> x0=dst
* 0x0000_ff02 KW_MEMCMP x0=a, x1=b, x2=len -> x0=signed diff
* 0x0000_ff03 KW_DAXPY x0=y, x1=x, x2=n, d0=a -> y[i] += a * x[i]
* 0x0000_ff04 KW_DDOT x0=a, x1=b, x2=n -> d0=sum(a[i] * b[i])
*
* Example LD_PRELOAD / IFUNC usage:
* void *memcpy(void *dst, const void *src, size_t n) {
* return kw_memcpy(dst, src, n);
* }
* void *memset(void *dst, int c, size_t n) {
* return kw_memset(dst, c, n);
* }
* int memcmp(const void *a, const void *b, size_t n) {
* return kw_memcmp(a, b, n);
* }
*/
#ifndef KW_PARAVIRT_H
#define KW_PARAVIRT_H

#include <stddef.h>
#include <stdint.h>

#define KW_PV_MEMCPY 0x0000ff00u
#define KW_PV_MEMSET 0x0000ff01u
#define KW_PV_MEMCMP 0x0000ff02u
#define KW_PV_DAXPY 0x0000ff03u
#define KW_PV_DDOT 0x0000ff04u

#if defined(__aarch64__)

static inline void *kw_memcpy(void *dst, const void *src, size_t len)
{
register void *x0 __asm__("x0") = dst;
register const void *x1 __asm__("x1") = src;
register size_t x2 __asm__("x2") = len;
__asm__ volatile(".inst 0x0000ff00"
: "+r"(x0)
: "r"(x1), "r"(x2)
: "memory");
return x0;
}

static inline void *kw_memset(void *dst, int byte, size_t len)
{
register void *x0 __asm__("x0") = dst;
register uint64_t x1 __asm__("x1") = (uint8_t)byte;
register size_t x2 __asm__("x2") = len;
__asm__ volatile(".inst 0x0000ff01"
: "+r"(x0)
: "r"(x1), "r"(x2)
: "memory");
return x0;
}

static inline int kw_memcmp(const void *a, const void *b, size_t len)
{
register intptr_t x0 __asm__("x0") = (intptr_t)a;
register const void *x1 __asm__("x1") = b;
register size_t x2 __asm__("x2") = len;
__asm__ volatile(".inst 0x0000ff02"
: "+r"(x0)
: "r"(x1), "r"(x2)
: "memory");
return (int)x0;
}

static inline void kw_daxpy(double *y, const double *x, size_t n, double a)
{
register double *x0 __asm__("x0") = y;
register const double *x1 __asm__("x1") = x;
register size_t x2 __asm__("x2") = n;
register double d0 __asm__("d0") = a;
__asm__ volatile(".inst 0x0000ff03"
:
: "r"(x0), "r"(x1), "r"(x2), "w"(d0)
: "memory");
}

static inline double kw_ddot(const double *a, const double *b, size_t n)
{
register const double *x0 __asm__("x0") = a;
register const double *x1 __asm__("x1") = b;
register size_t x2 __asm__("x2") = n;
register double d0 __asm__("d0");
__asm__ volatile(".inst 0x0000ff04"
: "=w"(d0)
: "r"(x0), "r"(x1), "r"(x2)
: "memory");
return d0;
}

#endif /* __aarch64__ */

#endif /* KW_PARAVIRT_H */
9 changes: 7 additions & 2 deletions src/a64/decode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ const fp = @import("fp.zig");
const simd_move = @import("simd_move.zig");
const simd_arith = @import("simd_arith.zig");
const crypto = @import("crypto.zig");
const paravirt = @import("paravirt.zig");

/// The file an encoding belongs to. `unallocated`: reserved, SME, SVE and
/// unallocated top-level space (always ILLEGAL here).
pub const Group = enum {
unallocated,
paravirt,
dp_imm,
branch,
ldst,
Expand All @@ -37,8 +39,10 @@ pub const Group = enum {
pub fn route(inst: u32) Group {
const op0 = c.bits(inst, 28, 25);
return switch (op0) {
// 0000 reserved (UDF) / SME, 0001 unallocated, 0010 SVE, 0011 unallocated
0b0000, 0b0001, 0b0010, 0b0011 => .unallocated,
// 0000 reserved (UDF) / SME (0x0000_ff00..0x0000_ff0f: paravirt),
// 0001 unallocated, 0010 SVE, 0011 unallocated
0b0000 => if (paravirt.isParavirt(inst)) .paravirt else .unallocated,
0b0001, 0b0010, 0b0011 => .unallocated,
0b1000, 0b1001 => .dp_imm,
0b1010, 0b1011 => .branch,
0b0100, 0b0110, 0b1100, 0b1110 => routeLdSt(inst),
Expand Down Expand Up @@ -89,6 +93,7 @@ fn routeSimdFp(inst: u32) Group {
pub fn exec(s: c.St, inst: u32) c.Error!c.Exit {
const e = switch (route(inst)) {
.unallocated => return c.ill(s, inst),
.paravirt => try paravirt.exec(s, inst),
.dp_imm => try dp_imm.exec(s, inst),
.branch => try branch.exec(s, inst),
.ldst => try ldst.exec(s, inst),
Expand Down
4 changes: 3 additions & 1 deletion src/a64/ops.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

const std = @import("std");
const c = @import("common.zig");
const paravirt = @import("paravirt.zig");

/// Register-file slots, as u64 indexes from &regs.x (struct kw_regs).
pub const sp: u8 = 31;
Expand Down Expand Up @@ -348,7 +349,8 @@ fn extendXform(option: u32, shift: u32) Xform {
/// ISB).
pub fn decode(inst: u32, pc: u64) Decoded {
return switch (bits(inst, 28, 25)) {
0b0000, 0b0001, 0b0010, 0b0011 => generic(inst, pc, true), // UDF, SME, SVE, unallocated
0b0000 => generic(inst, pc, !paravirt.isDefined(inst)), // UDF (paravirt stays in block), SME
0b0001, 0b0010, 0b0011 => generic(inst, pc, true), // SVE, unallocated
0b1000, 0b1001 => dpImm(inst, pc),
0b1010, 0b1011 => branch(inst, pc),
0b0100, 0b0110, 0b1100, 0b1110 => ldst(inst, pc),
Expand Down
Loading