mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-05 22:49:54 +08:00
a64/jit: translate LD2-4/ST2-4 (multiple structures) inline (#35)
* a64/jit: translate LD2-4/ST2-4 (multiple structures) inline * a64/jit: run the LD2-4/ST2-4 unit test from a warm data TLB --------- Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
@@ -26,12 +26,12 @@ What is translated:
|
||||
| `branch.zig` | side exits, terminators, b_f/bl_f, br/blr, goto; the region hooks `selfLoop`, `targets`, `memberTail`, `indirect` |
|
||||
| `generic.zig` | frequent `generic` ops inline: FP/general moves, MOVI, DUP, UMOV, INS, vector logic, MRS of FPCR/FPSR/NZCV/DCZID, FCMP, scalar FP arithmetic and conversions with fast paths, loads into XZR |
|
||||
| `atomic.zig` | exclusives, LDAR/STLR, LSE LD<op>/SWP/CAS, CLREX, barriers, DC ZVA |
|
||||
| `vec.zig` | with SIMD: integer AdvSIMD, AdvSIMD FP fast paths, permutes, LD1/ST1 and V-register pairs, AES, PMULL, SHA1, SHA256, SHA512 |
|
||||
| `vec.zig` | with SIMD: integer AdvSIMD, AdvSIMD FP fast paths, permutes, LD1-4/ST1-4 (multiple structures), LD1/ST1 single lane, LD1R, V-register pairs, AES, PMULL, SHA1, SHA256, SHA512 |
|
||||
| `emit.zig` | the core's hooks, family dispatch, `generic` calls |
|
||||
| `common.zig` | locals, slot access, `Use` helpers |
|
||||
| `kindtest.zig`, `tests.zig` | test helpers and the core's tests |
|
||||
|
||||
The other `generic` ops (LD2-4/ST2-4, the rest of AdvSIMD
|
||||
The other `generic` ops (LD2-4/ST2-4 single lane and replicate, the rest of AdvSIMD
|
||||
(also all of it without SIMD), other system ops, exclusive pairs, CASP,
|
||||
SHA3, FP off its fast path) are calls of the reference interpreter through the
|
||||
kernel's function table (Generic ops, below); a host without the table
|
||||
@@ -410,10 +410,11 @@ exactly that meaning.
|
||||
decode on numbered bytes, so they are the reference's by construction.
|
||||
TBL/TBX: `i8x16.swizzle` per table register on the index minus 16 j
|
||||
(swizzle gives 0 from 16 on, TBL's rule), TBX a bitselect.
|
||||
- **Loads and stores.** LD1/ST1 (1-4 registers, one lane), LD1R,
|
||||
LDP/STP/LDNP/STNP of S, D and Q: one TLB check over the whole access
|
||||
(else handed back), then v128 loads and stores, writeback last. LD2-4/
|
||||
ST2-4 are calls.
|
||||
- **Loads and stores.** LD1-4/ST1-4 (multiple structures, 1-4 registers),
|
||||
LD1/ST1 (one lane), LD1R, LDP/STP/LDNP/STNP of S, D and Q: one TLB check
|
||||
over the whole access (else handed back), then v128 loads and stores
|
||||
(with byte shuffles for LD2-4/ST2-4), writeback last. LD2-4/ST2-4 single
|
||||
lane and replicate are calls.
|
||||
- **AES.** Table lookups in `jc.AesTab` (cpu-relative, filled from core.aes
|
||||
by `jitcore.init`). AESE/AESD do AddRoundKey and leave the state pending
|
||||
in a v128 scratch (`vec.Ext`); if the next op is AESMC (AESIMC) of that
|
||||
|
||||
@@ -730,3 +730,52 @@ test "a64 jit: code a module stores into the next module's page runs at once (ch
|
||||
try t.expectEqual(@as(u64, if (i < 30) 1 else if (i % 2 == 0) 2 else 7), ms[0].regs.x[0]);
|
||||
}
|
||||
}
|
||||
|
||||
test "a64 jit: LD2-4/ST2-4 24-byte, register wrap past V31, post-index and .1D illegal" {
|
||||
// LD3 {v30.8b, v31.8b, v0.8b}, [x0], #24 (24 bytes, wraps past V31, zeroes high halves)
|
||||
// ST3 {v30.8b, v31.8b, v0.8b}, [x1], x2
|
||||
// LD4 {v30.4s, v31.4s, v0.4s, v1.4s}, [x3], #64 (64 bytes, wraps past V31)
|
||||
// ST4 {v30.4s, v31.4s, v0.4s, v1.4s}, [x4]
|
||||
const ld3_wrap_post: u32 = 0x0cdf_401e; // Q=0 L=1 post=1 Rm=31 opcode=0100 size=00 Rn=0 Rt=30
|
||||
const st3_wrap_reg: u32 = 0x0c82_403e; // Q=0 L=0 post=1 Rm=2 opcode=0100 size=00 Rn=1 Rt=30
|
||||
const ld4_wrap_post: u32 = 0x4cdf_087e; // Q=1 L=1 post=1 Rm=31 opcode=0000 size=10 Rn=3 Rt=30
|
||||
const st4_wrap: u32 = 0x4c00_089e; // Q=1 L=0 post=0 Rm=0 opcode=0000 size=10 Rn=4 Rt=30
|
||||
{
|
||||
var ms: [2]Machine = .{ try Machine.init(t.allocator), try Machine.init(t.allocator) };
|
||||
defer for (&ms) |*m| m.deinit();
|
||||
var pas: [2]u64 = undefined;
|
||||
for (&ms, 0..) |*m, mi| {
|
||||
const pa = m.frame();
|
||||
pas[mi] = pa;
|
||||
m.map(0x40000, pa, 2 | 4); // R W
|
||||
m.code(0x10000, &.{ ld3_wrap_post, st3_wrap_reg, ld4_wrap_post, st4_wrap, svc });
|
||||
const page = m.bytes(pa);
|
||||
for (0..128) |i| page[i] = @truncate(i * 3 + 7);
|
||||
m.regs.x[0] = 0x40000;
|
||||
m.regs.x[1] = 0x40100;
|
||||
m.regs.x[2] = 24;
|
||||
m.regs.x[3] = 0x40020;
|
||||
m.regs.x[4] = 0x40200;
|
||||
m.regs.v[30] = ~@as(u128, 0);
|
||||
m.regs.v[31] = ~@as(u128, 0);
|
||||
m.regs.v[0] = ~@as(u128, 0);
|
||||
m.regs.v[1] = ~@as(u128, 0);
|
||||
// A warm data TLB, so that the ops run compiled: from a cold
|
||||
// one the first hands back and the rest run in the tier.
|
||||
m.cpu.probe(0x40000, .read) catch unreachable;
|
||||
m.cpu.probe(0x40000, .write) catch unreachable;
|
||||
}
|
||||
try t.expectEqual(ms[1].runRef(100), ms[0].run(100));
|
||||
try t.expectEqual(ms[1].cpu.icount, ms[0].cpu.icount);
|
||||
try t.expect(std.mem.eql(u8, std.mem.asBytes(&ms[0].regs), std.mem.asBytes(&ms[1].regs)));
|
||||
try t.expect(std.mem.eql(u8, ms[0].bytes(pas[0]), ms[1].bytes(pas[1])));
|
||||
}
|
||||
|
||||
// LD2 {v0.1d, v1.1d}, [x0] (Q=0, size=3) is illegal and must raise EV_ILLEGAL.
|
||||
var m = try Machine.init(t.allocator);
|
||||
defer m.deinit();
|
||||
m.mapData(0x40000, 1);
|
||||
m.regs.x[0] = 0x40000;
|
||||
m.code(0x10000, &.{ 0x0c40_8c00, svc });
|
||||
try t.expectEqual(@as(u32, mach.EV_ILLEGAL), m.run(100));
|
||||
}
|
||||
|
||||
@@ -102,6 +102,9 @@ pub const Kind = enum(u8) {
|
||||
/// post-index by imm, 2: by Xm
|
||||
ld1,
|
||||
st1,
|
||||
/// LD2-LD4, ST2-ST4 (multiple structures, sh = selem in 2..4); x as for ld1
|
||||
ldn,
|
||||
stn,
|
||||
/// LD1, ST1 (single structure: lane `idx[0]` of Vt, 1 << size bytes),
|
||||
/// LD1R (Q: both halves); x as for ld1
|
||||
ld1s,
|
||||
@@ -893,14 +896,20 @@ fn ldst(raw: u32) ?V {
|
||||
if (bit(raw, 24)) return single(raw);
|
||||
const post = bit(raw, 23);
|
||||
if (if (post) bit(raw, 21) else bits(raw, 21, 16) != 0) return null;
|
||||
const nregs: u8 = switch (bits(raw, 15, 12)) {
|
||||
0b0111 => 1,
|
||||
0b1010 => 2,
|
||||
0b0110 => 3,
|
||||
0b0010 => 4,
|
||||
else => return null, // LD2-LD4, ST2-ST4: calls
|
||||
const load = bit(raw, 22);
|
||||
const nregs: u8, const interleave: bool = switch (bits(raw, 15, 12)) {
|
||||
0b0111 => .{ 1, false },
|
||||
0b1010 => .{ 2, false },
|
||||
0b0110 => .{ 3, false },
|
||||
0b0010 => .{ 4, false },
|
||||
0b1000 => .{ 2, true },
|
||||
0b0100 => .{ 3, true },
|
||||
0b0000 => .{ 4, true },
|
||||
else => return null,
|
||||
};
|
||||
var v = regs3(raw, if (bit(raw, 22)) .ld1 else .st1);
|
||||
var v = regs3(raw, if (interleave) (if (load) .ldn else .stn) else (if (load) .ld1 else .st1));
|
||||
v.size = @intCast(bits(raw, 11, 10));
|
||||
if (interleave and v.size == 3 and !v.q) return null; // LD2-4/ST2-4 .1d is illegal
|
||||
v.sh = nregs;
|
||||
if (post) {
|
||||
if (v.m == 31) {
|
||||
@@ -1065,11 +1074,11 @@ pub fn usage(v: V) Use {
|
||||
if (v.x == 1 or v.x == 3) u = u.with(cm.wr(v.n));
|
||||
break :blk u.with(if (v.k == .ldpv) vw(v.d).with(vw(v.m)) else vr(v.d).with(vr(v.m)));
|
||||
},
|
||||
.ld1, .st1, .ld1s, .st1s, .ld1r => blk: {
|
||||
.ld1, .st1, .ldn, .stn, .ld1s, .st1s, .ld1r => blk: {
|
||||
var u = cm.rd(v.n); // Rn = 31 is SP (slot 31)
|
||||
if (v.x != 0) u = u.with(cm.wr(v.n));
|
||||
if (v.x == 2) u = u.with(cm.rd(v.m));
|
||||
for (0..v.sh) |j| u = u.with(if (v.k == .ld1 or v.k == .ld1r) vw(v.d + @as(u8, @intCast(j))) else vr(v.d + @as(u8, @intCast(j))));
|
||||
for (0..v.sh) |j| u = u.with(if (v.k == .ld1 or v.k == .ldn or v.k == .ld1r) vw(v.d + @as(u8, @intCast(j))) else vr(v.d + @as(u8, @intCast(j))));
|
||||
if (v.k == .ld1s) u = u.with(vr(v.d)).with(vw(v.d));
|
||||
break :blk u;
|
||||
},
|
||||
@@ -1411,6 +1420,7 @@ pub fn emit(e: *Emit, v: V, at: At) void {
|
||||
cm.setV(e, v.d);
|
||||
},
|
||||
.ld1, .st1 => ldst1(e, v, at),
|
||||
.ldn, .stn => ldstN(e, v, at),
|
||||
.ld1s, .st1s, .ld1r => ldstSingle(e, v, at),
|
||||
.ldpv, .stpv => pairEmit(e, v, at),
|
||||
.aese, .aesd => {
|
||||
@@ -2367,6 +2377,169 @@ fn ldst1(e: *Emit, v: V, at: At) void {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- LD2-LD4, ST2-ST4 (multiple structures) -------------------------------------------
|
||||
|
||||
fn ldstN(e: *Emit, v: V, at: At) void {
|
||||
const selem: usize = v.sh; // 2..4
|
||||
const per: usize = if (v.q) 16 else 8;
|
||||
const total: usize = per * selem; // 16, 24, 32, 48, 64
|
||||
const chunks: usize = (total + 15) / 16; // 1..4
|
||||
const esz: usize = @as(usize, 1) << v.size;
|
||||
const load = v.k == .ldn;
|
||||
cm.get(e, v.n);
|
||||
e.set(cm.l_va);
|
||||
e.tlbCheck(at.k, at.pc, if (load) jc.tlb_r_off else jc.tlb_w_off, total);
|
||||
if (e.mem32) e.op(W.i64_ext_u);
|
||||
e.set(lu);
|
||||
if (load) {
|
||||
for (0..chunks) |ci| {
|
||||
e.get(lu);
|
||||
e.addr();
|
||||
if (16 * ci + 16 <= total) {
|
||||
e.vop(cm.S.v128_load);
|
||||
} else {
|
||||
e.vop(cm.S.v128_load64_zero);
|
||||
}
|
||||
e.uleb(0);
|
||||
e.uleb(16 * ci);
|
||||
e.set(lq + @as(u32, @intCast(ci)));
|
||||
}
|
||||
for (0..selem) |r| {
|
||||
var src: [16]u8 = @splat(0);
|
||||
for (0..per) |b| {
|
||||
const elem = b / esz;
|
||||
src[b] = @intCast((elem * selem + r) * esz + (b % esz));
|
||||
}
|
||||
if (chunks == 1) {
|
||||
e.get(lq + 0);
|
||||
e.get(lq + 0);
|
||||
shuffle(e, src);
|
||||
} else if (chunks == 2) {
|
||||
e.get(lq + 0);
|
||||
e.get(lq + 1);
|
||||
shuffle(e, src);
|
||||
} else if (chunks == 3) {
|
||||
var idx1: [16]u8 = @splat(0);
|
||||
var idx2: [16]u8 = @splat(0);
|
||||
for (0..per) |b| {
|
||||
if (src[b] < 32) {
|
||||
idx1[b] = src[b];
|
||||
idx2[b] = @intCast(b);
|
||||
} else {
|
||||
idx2[b] = 16 + (src[b] - 32);
|
||||
}
|
||||
}
|
||||
e.get(lq + 0);
|
||||
e.get(lq + 1);
|
||||
shuffle(e, idx1);
|
||||
e.get(lq + 2);
|
||||
shuffle(e, idx2);
|
||||
} else {
|
||||
var idx1: [16]u8 = @splat(0);
|
||||
var idx2: [16]u8 = @splat(0);
|
||||
var idx3: [16]u8 = @splat(0);
|
||||
for (0..per) |b| {
|
||||
if (src[b] < 32) {
|
||||
idx1[b] = src[b];
|
||||
idx3[b] = @intCast(b);
|
||||
} else {
|
||||
idx2[b] = src[b] - 32;
|
||||
idx3[b] = @intCast(16 + b);
|
||||
}
|
||||
}
|
||||
e.get(lq + 0);
|
||||
e.get(lq + 1);
|
||||
shuffle(e, idx1);
|
||||
e.get(lq + 2);
|
||||
e.get(lq + 3);
|
||||
shuffle(e, idx2);
|
||||
shuffle(e, idx3);
|
||||
}
|
||||
cm.setVq(e, @intCast((v.d + r) & 31), v.q);
|
||||
}
|
||||
} else {
|
||||
for (0..chunks) |ci| {
|
||||
const base_b = 16 * ci;
|
||||
const clen = @min(16, total - base_b);
|
||||
e.get(lu);
|
||||
e.addr();
|
||||
if (selem == 2) {
|
||||
var idx: [16]u8 = @splat(0);
|
||||
for (0..clen) |j| {
|
||||
const mb = base_b + j;
|
||||
const ge = mb / esz;
|
||||
const r = ge % 2;
|
||||
const rb = (ge / 2) * esz + (mb % esz);
|
||||
idx[j] = @intCast(r * 16 + rb);
|
||||
}
|
||||
get(e, v.d);
|
||||
get(e, v.d + 1);
|
||||
shuffle(e, idx);
|
||||
} else if (selem == 3) {
|
||||
var idx1: [16]u8 = @splat(0);
|
||||
var idx2: [16]u8 = @splat(0);
|
||||
for (0..clen) |j| {
|
||||
const mb = base_b + j;
|
||||
const ge = mb / esz;
|
||||
const r = ge % 3;
|
||||
const rb = (ge / 3) * esz + (mb % esz);
|
||||
if (r < 2) {
|
||||
idx1[j] = @intCast(r * 16 + rb);
|
||||
idx2[j] = @intCast(j);
|
||||
} else {
|
||||
idx2[j] = @intCast(16 + rb);
|
||||
}
|
||||
}
|
||||
get(e, v.d);
|
||||
get(e, v.d + 1);
|
||||
shuffle(e, idx1);
|
||||
get(e, v.d + 2);
|
||||
shuffle(e, idx2);
|
||||
} else {
|
||||
var idx1: [16]u8 = @splat(0);
|
||||
var idx2: [16]u8 = @splat(0);
|
||||
var idx3: [16]u8 = @splat(0);
|
||||
for (0..clen) |j| {
|
||||
const mb = base_b + j;
|
||||
const ge = mb / esz;
|
||||
const r = ge % 4;
|
||||
const rb = (ge / 4) * esz + (mb % esz);
|
||||
if (r < 2) {
|
||||
idx1[j] = @intCast(r * 16 + rb);
|
||||
idx3[j] = @intCast(j);
|
||||
} else {
|
||||
idx2[j] = @intCast((r - 2) * 16 + rb);
|
||||
idx3[j] = @intCast(16 + j);
|
||||
}
|
||||
}
|
||||
get(e, v.d);
|
||||
get(e, v.d + 1);
|
||||
shuffle(e, idx1);
|
||||
get(e, v.d + 2);
|
||||
get(e, v.d + 3);
|
||||
shuffle(e, idx2);
|
||||
shuffle(e, idx3);
|
||||
}
|
||||
if (clen == 16) {
|
||||
e.vop(cm.S.v128_store);
|
||||
e.uleb(0);
|
||||
e.uleb(16 * ci);
|
||||
} else {
|
||||
e.vop(cm.S.v128_store64_lane);
|
||||
e.uleb(0);
|
||||
e.uleb(16 * ci);
|
||||
e.op(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (v.x != 0) {
|
||||
cm.get(e, v.n);
|
||||
if (v.x == 1) e.k64(v.imm) else cm.get(e, v.m);
|
||||
e.op(W.i64_add);
|
||||
cm.set(e, v.n);
|
||||
}
|
||||
}
|
||||
|
||||
/// LDP/STP of S, D, Q registers: one TLB check over both, loads zero the
|
||||
/// rest of each register; the writeback after the accesses (the base is
|
||||
/// an X register, the data V registers: no overlap); Rt2 written after Rt.
|
||||
@@ -3005,7 +3178,7 @@ fn genLd1(r: std.Random, out: []u32, pc: u64) usize {
|
||||
const t = vreg(r);
|
||||
const q = r.uintLessThan(u32, 2) << 30;
|
||||
const l = r.uintLessThan(u32, 2) << 22;
|
||||
const opc = pick(r, u32, &.{ 0b0111, 0b1010, 0b0110, 0b0010, 0b1000 });
|
||||
const opc = pick(r, u32, &.{ 0b0111, 0b1010, 0b0110, 0b0010, 0b1000, 0b0100, 0b0000 });
|
||||
const base = pick(r, u32, &.{ 20, 21, 22, 31, 19 });
|
||||
const post: u32 = switch (r.uintLessThan(u32, 3)) {
|
||||
0 => 0,
|
||||
|
||||
Reference in New Issue
Block a user