Initial public release
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
// 宿主门功能回归:逐个门执行宿主现行 _dm_gate(entry.S),核对寄存器回写契约。
|
||||
// 用法:gate_test <gates.tsv>... 每行 {返回点, 种类, arg, 原址};退出码 0 = 全过
|
||||
// 每个门:x0..x29(x18 除外)设成各不相同的图案 → blr _dm_gate → 桩 gate_dispatch 按门种类写标记值:
|
||||
// cntpct / cntvct:regs[arg] = 标记(原指令 mrs x<arg>, cntpct_el0);svc:x0..x7 = 标记 + i(出参位);obs:不写
|
||||
// 返回后核对:桩写过的寄存器 == 标记,其余 == 原图案(桩故意破坏 x9–x17 与 NZCV),NZCV == 调用前,x30 == 调用返回点。任何一项不符报红。
|
||||
// Host gate functional regression: run the host's current _dm_gate (entry.S) for each gate and check the register write-back contract.
|
||||
// Usage: gate_test <gates.tsv>... each line is {return point, kind, arg, original address}; exit code 0 = all pass
|
||||
// For each gate: set x0..x29 (except x18) to mutually distinct patterns → blr _dm_gate → the stub gate_dispatch writes marker values according to the gate kind:
|
||||
// cntpct / cntvct: regs[arg] = marker (original instruction mrs x<arg>, cntpct_el0); svc: x0..x7 = marker + i (output argument positions); obs: writes nothing
|
||||
// After return, check: registers written by the stub == marker, the rest == original pattern (the stub deliberately clobbers x9–x17 and NZCV), NZCV == value before the call, x30 == the call's return point. Any mismatch reports a failure.
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void dm_gate(void);
|
||||
void gate_call(uint64_t *in, uint64_t *out); // gate_tramp.S:in[0..29] 装进 x0..x29(跳过 x18)→ blr dm_gate → 全部存进 out[0..30] | gate_tramp.S: load in[0..29] into x0..x29 (skipping x18) → blr dm_gate → store everything into out[0..30]
|
||||
|
||||
static int g_kind, g_arg; // 当前被测门 | The gate currently under test
|
||||
enum { K_CNT = 1, K_SVC = 2, K_OBS = 3 };
|
||||
static const uint64_t MARK = 0x5157000000000000ull;
|
||||
static uint64_t g_ret_seen;
|
||||
|
||||
uint64_t gate_dispatch(uint64_t ret_addr, uint64_t *regs) {
|
||||
g_ret_seen = ret_addr;
|
||||
uint64_t r = 0;
|
||||
if (g_kind == K_CNT) { regs[g_arg] = MARK | (uint64_t)g_arg; r = regs[g_arg]; }
|
||||
if (g_kind == K_SVC) { for (int i = 0; i < 8; i++) regs[i] = MARK | 0x100 | (uint64_t)i; r = regs[0]; }
|
||||
// 宿主 C 代码按 AAPCS 可随意破坏 x9–x17 与 NZCV:桩在返回前最后一步故意全破坏,核 _dm_gate 从帧里恢复(观测门 / 分支门可放在函数中间的前提)
|
||||
// Under AAPCS, host C code may freely clobber x9–x17 and NZCV: the stub deliberately clobbers all of them as its last step before returning, to check that _dm_gate restores them from the frame (the precondition for placing observation gates / branch gates in the middle of a function)
|
||||
__asm__ volatile("mov x9, #0xdead\n mov x10, x9\n mov x11, x9\n mov x12, x9\n mov x13, x9\n mov x14, x9\n mov x15, x9\n mov x16, x9\n mov x17, x9\n"
|
||||
"mov x9, #0x90000000\n msr nzcv, x9\n mov x9, #0xdead" ::: "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "cc");
|
||||
return r;
|
||||
}
|
||||
void dm_thread_returned(void) { abort(); }
|
||||
void unimpl_c(void) { abort(); }
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int n = 0, bad = 0, nc = 0, ns = 0, no = 0;
|
||||
for (int f = 1; f < argc; f++) {
|
||||
FILE *fp = fopen(argv[f], "r"); if (!fp) { perror(argv[f]); return 2; }
|
||||
char kind[16]; unsigned long long ret, orig; int arg;
|
||||
while (fscanf(fp, "%llx %15s %d %llx", &ret, kind, &arg, &orig) == 4) {
|
||||
g_kind = !strcmp(kind, "svc") ? K_SVC : !strcmp(kind, "obs") ? K_OBS : (!strcmp(kind, "cntpct") || !strcmp(kind, "cntvct")) ? K_CNT : 0;
|
||||
if (!g_kind) { printf("FAIL 未知门种类 %s @%llx\n", kind, orig); bad++; continue; }
|
||||
g_arg = arg;
|
||||
uint64_t in[30], out[32], want[30];
|
||||
for (int i = 0; i < 30; i++) in[i] = 0xA000000000000000ull | ((uint64_t)i << 32) | (uint64_t)(n * 31 + i);
|
||||
memcpy(want, in, sizeof want);
|
||||
if (g_kind == K_CNT) want[arg] = MARK | (uint64_t)arg;
|
||||
if (g_kind == K_SVC) for (int i = 0; i < 8; i++) want[i] = MARK | 0x100 | (uint64_t)i;
|
||||
gate_call(in, out);
|
||||
int ok = 1;
|
||||
for (int i = 0; i < 30; i++) {
|
||||
if (i == 18) continue;
|
||||
if (out[i] != want[i]) {
|
||||
if (ok) printf("FAIL %s 门 @%#llx arg=%d:", kind, orig, arg);
|
||||
printf(" x%d=%#llx(应 %#llx)", i, (unsigned long long)out[i], (unsigned long long)want[i]); ok = 0;
|
||||
}
|
||||
}
|
||||
if (out[31] != 0x60000000ull) { printf("%sNZCV=%#llx(应 0x60000000)", ok ? "FAIL NZCV " : " ", (unsigned long long)out[31]); ok = 0; } // 调用前 cmp xzr,xzr 置 Z/C | cmp xzr,xzr before the call sets Z/C
|
||||
if (out[30] != g_ret_seen) { printf("%s x30=%#llx(应 %#llx)", ok ? "FAIL x30 " : "", (unsigned long long)out[30], (unsigned long long)g_ret_seen); ok = 0; }
|
||||
if (!ok) { printf("\n"); bad++; }
|
||||
n++; nc += g_kind == K_CNT; ns += g_kind == K_SVC; no += g_kind == K_OBS;
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
printf("GATEFN %s 门=%d(svc %d / 计时 %d / 观测 %d)失败=%d\n", bad ? "FAIL" : "OK", n, ns, nc, no, bad);
|
||||
return bad ? 1 : 0;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// gate_call(in, out):保存宿主被调用者保存寄存器 → in[0..29] 装进 x0..x29(跳过 x18:苹果平台寄存器)→ blr _dm_gate
|
||||
// → x0..x30 存进 out[0..30]、NZCV 存 out[31] → 恢复宿主寄存器返回。out 指针放在栈上(调用期间所有通用寄存器都被占用)。
|
||||
// gate_call(in, out): save the host's callee-saved registers → load in[0..29] into x0..x29 (skipping x18: the Apple platform register) → blr _dm_gate
|
||||
// → store x0..x30 into out[0..30] and NZCV into out[31] → restore the host registers and return. The out pointer is kept on the stack (all general-purpose registers are occupied during the call).
|
||||
.text
|
||||
.globl _gate_call
|
||||
.p2align 2
|
||||
_gate_call:
|
||||
stp x29, x30, [sp, #-112]!
|
||||
stp x19, x20, [sp, #16]
|
||||
stp x21, x22, [sp, #32]
|
||||
stp x23, x24, [sp, #48]
|
||||
stp x25, x26, [sp, #64]
|
||||
stp x27, x28, [sp, #80]
|
||||
str x1, [sp, #96] // out
|
||||
mov x16, x0 // in
|
||||
ldp x0, x1, [x16, #0]
|
||||
ldp x2, x3, [x16, #16]
|
||||
ldp x4, x5, [x16, #32]
|
||||
ldp x6, x7, [x16, #48]
|
||||
ldp x8, x9, [x16, #64]
|
||||
ldp x10, x11, [x16, #80]
|
||||
ldp x12, x13, [x16, #96]
|
||||
ldr x14, [x16, #112]
|
||||
ldr x15, [x16, #120]
|
||||
ldr x17, [x16, #136]
|
||||
ldr x19, [x16, #152]
|
||||
ldp x20, x21, [x16, #160]
|
||||
ldp x22, x23, [x16, #176]
|
||||
ldp x24, x25, [x16, #192]
|
||||
ldp x26, x27, [x16, #208]
|
||||
ldp x28, x29, [x16, #224]
|
||||
ldr x16, [x16, #128] // 最后装 x16 本身 | Load x16 itself last
|
||||
cmp xzr, xzr // NZCV = Z|C(0x60000000),返回后核门是否恢复 | NZCV = Z|C (0x60000000); after return, check whether the gate restored it
|
||||
bl _dm_gate // x30 = 返回点(门标识) | x30 = return point (gate identifier)
|
||||
str x16, [sp, #-16]! // 暂存 x16,拿 out 指针 | Stash x16, get the out pointer
|
||||
ldr x16, [sp, #112]
|
||||
stp x0, x1, [x16, #0]
|
||||
stp x2, x3, [x16, #16]
|
||||
stp x4, x5, [x16, #32]
|
||||
stp x6, x7, [x16, #48]
|
||||
stp x8, x9, [x16, #64]
|
||||
stp x10, x11, [x16, #80]
|
||||
stp x12, x13, [x16, #96]
|
||||
stp x14, x15, [x16, #112]
|
||||
ldr x0, [sp], #16 // 原 x16 | Original x16
|
||||
str x0, [x16, #128]
|
||||
str x17, [x16, #136]
|
||||
str x19, [x16, #152]
|
||||
stp x20, x21, [x16, #160]
|
||||
stp x22, x23, [x16, #176]
|
||||
stp x24, x25, [x16, #192]
|
||||
stp x26, x27, [x16, #208]
|
||||
stp x28, x29, [x16, #224]
|
||||
str x30, [x16, #240]
|
||||
mrs x0, nzcv // 存取指令不改标志位,此处仍是门返回时的 NZCV | Load/store instructions do not change the flags; here it is still the NZCV as of gate return
|
||||
str x0, [x16, #248]
|
||||
ldp x19, x20, [sp, #16]
|
||||
ldp x21, x22, [sp, #32]
|
||||
ldp x23, x24, [sp, #48]
|
||||
ldp x25, x26, [sp, #64]
|
||||
ldp x27, x28, [sp, #80]
|
||||
ldp x29, x30, [sp], #112
|
||||
ret
|
||||
Reference in New Issue
Block a user