// Independent allocation and EL0 subproof for the integrated image. This does // not prove IRQ delivery, worker stop, DMA completion or the full K2155 gate. import assert from 'node:assert/strict'; import {ledger,released} from './k2155-dma-authority-check.mjs'; import {faultStageOrder} from './k2155-native-fault-stages.mjs'; const W=b=>Array.from({length:b.length/8},(_,i)=>b.readBigUInt64LE(i*8)); const u=(b,at)=>b.readBigUInt64LE(at),zero=b=>assert.ok(b.every(v=>v===0),'erased or unwritten bytes'); const mask=0x0000fffffffff000n,CODE=0x80000000n,GEN=0x21550001n,REFUSED=1n<<63n; export function checkFaultOwnership({snapshots,compiled,symbols,memoryMiB,bootId,programs,faultId}){ const stages=snapshots.map(s=>Number(u(s.OS_K2155_EVIDENCE,8)));const scenario=faultStageOrder(stages,faultId); assert.ok([256,512,1024].includes(memoryMiB));assert.match(bootId,/^[0-9a-f]{16}$/); const l=W(compiled.OS_K1000_AUTHORITY_LAYOUT_EXPECTED).map(Number);assert.equal(l.length,24); const first=snapshots[0],held=snapshots[1],before=ledger(first.authority,l),initial=ledger(held.authority,l); const end=0x40000000n+BigInt(memoryMiB)*1048576n,root=W(first.OS_K2155_CPU_STATE)[0]; const row0=W(first.OS_K2155_DMA_AUTHORITY);assert.equal(row0[1],BigInt(l[0])); assert.equal(row0[2],BigInt(symbols.OS_K1000_AUTHORITY_LAYOUT_EXPECTED.address));assert.equal(row0[3],192n); const range=a=>assert.ok(a>=0x40000000n&&a+4096n<=end&&a%4096n===0n); range(root);assert.ok(!before.free.some(([a,b])=>root>=a&&root0&&program.length<=4096); assert.deepEqual(held[prefix+'code'].subarray(0,program.length),program);zero(held[prefix+'code'].subarray(program.length)); zero(held[prefix+'data']);zero(held[prefix+'stack']); for(let p=0;p<5;p++)workerPages.push(guards[i*6]+BigInt(p)*4096n); } for(let d=0;d<2;d++)for(let q=0;q<(d===0?1:2);q++)for(let p=0;p<4;p++)dmaPages.push(devices[d*64+12+q*20+p]); const all=[...userPages.flat(),...workerPages,...dmaPages];assert.equal(all.length,34);assert.equal(new Set(all).size,34); for(const a of all){range(a);assert.ok(!before.live.has(a)&&initial.live.has(a));assert.ok(before.free.some(([lo,hi])=>a>=lo&&ar.active&&r.address===a);assert.ok(slot>=0&&!before.slots[slot].active); const g=initial.slots[slot].generation;assert.ok(g>oldGeneration&&g<=newGeneration);assert.ok(!generations.has(g));generations.add(g); first.authority.copy(restored,l[5]+slot*l[6],l[5]+slot*l[6],l[5]+(slot+1)*l[6]);} restored.writeBigUInt64LE(oldGeneration,l[17]);assert.deepEqual(restored,first.authority,'unrelated allocator bytes changed during allocation'); const afterWorkers=released(held.authority,l,initial,workerPages),afterDevices=released(held.authority,l,initial,[...workerPages,...dmaPages]); const requests=W(snapshots.at(-1).OS_K2155_EL0_REQUESTS);assert.equal(requests.length,192); const expected=[]; for(const id of [[9n,0x21550009n],[1n,GEN-1n]]){ for(const [call,args,result]of [[16n,[0n,0n,0n],REFUSED|1n],[16n,[1n,GEN,0xffffffffffffffffn],REFUSED|1n],[17n,[0n,0n,0n],0n],[1n,[0x2159n,0n,0n],0x2159n]])expected.push([...id,call,...args,result,0n]); } expected.push([1n,GEN,16n,1n,0n,0n,REFUSED|22n,0n],[1n,GEN,17n,0n,0n,0n,0n,0n],[1n,GEN,16n,0n,0n,0n,0x215500acn,1n]); const stackLow=BigInt(symbols.OS_K900_STACK.address)+4096n,stackTop=stackLow+65536n-16n; for(let i=0;i<11;i++){ const r=requests.slice(i*16,(i+1)*16),program=programs[i<8?0:1];assert.deepEqual(r.slice(0,8),expected[i]); assert.ok(r[8]>=CODE+4n&&r[8]<=CODE+BigInt(program.length)&&r[8]%4n===0n); assert.equal(program.readUInt32LE(Number(r[8]-CODE-4n)),0xd4000001,'actual SVC instruction'); assert.ok(r[9]>=CODE+12304n&&r[9]<=CODE+16384n&&r[9]%16n===0n); assert.equal(r[10]&~(0xf00003c0n|(1n<<25n)),0n);assert.equal(r[10]&0x3c0n,0x3c0n); assert.equal(r[11],0x56000000n);assert.equal(r[12],8n);assert.equal(r[13],BigInt(i+1));assert.equal(r[14],i===10?1n:0n); assert.ok(r[15]>=stackLow&&r[15]+832n<=stackTop&&r[15]%16n===0n); }zero(snapshots.at(-1).OS_K2155_EL0_REQUESTS.subarray(11*128)); let calls=0,frames=0,lastTicks=-1n; const cpu0=W(first.OS_K2155_CPU_STATE);assert.ok(cpu0[1]>0n&&cpu0[1]%2048n===0n&&cpu0[6]>0n); for(const [step,s]of snapshots.entries()){ const stage=stages[step],e=W(s.OS_K2155_EVIDENCE),cpu=W(s.OS_K2155_CPU_STATE),svc=[330,331,332].includes(stage); assert.equal(e.length,64);assert.equal(e[0],0x4b32313535494e54n);assert.equal(e[1],BigInt(stage));assert.equal(e[2],before.pages);assert.equal(e[4],BigInt('0x'+bootId)); assert.deepEqual(s.OS_K1000_AUTHORITY_LAYOUT_EXPECTED,compiled.OS_K1000_AUTHORITY_LAYOUT_EXPECTED);assert.deepEqual(W(s.OS_K2155_DMA_AUTHORITY),row0); assert.deepEqual(s['root-page'],first['root-page'],'kernel L1 root changed'); assert.equal(cpu.length,8);assert.equal(cpu[0],svc?users[stage===332?8:0]:root);assert.equal(cpu[1],svc?BigInt(symbols.__os_fx_vectors.address):[100,101,110,111].includes(stage)?BigInt(symbols.__os_k2155_irq_vectors.address):cpu0[1]); assert.equal(cpu[2]&0x3c0n,0x3c0n);assert.equal(cpu[4]&0xff00ffffffn,0n);assert.equal(cpu[6],cpu0[6]);assert.equal(cpu[7],4n);assert.ok(cpu[5]>lastTicks);lastTicks=cpu[5]; if([210,211,220,221].includes(stage)){const i=stage%10,guard=guards[i*6];assert.ok(cpu[3]>=guard+4112n&&cpu[3]=stackLow&&cpu[3]<=stackTop&&cpu[3]%16n===0n); assert.deepEqual(s.authority,stage===0?first.authority:stage===230||(stage===80&&faultId===5)?afterWorkers:held.authority); if(stage===0){zero(s.OS_K2155_USER_SPACES);assert.equal(e[7],0n);} else{ assert.equal(e[5],users[0]);assert.equal(e[6],users[8]);assert.equal(e[7],root);assert.deepEqual(s.OS_K2155_USER_SPACES,held.OS_K2155_USER_SPACES); for(let i=0;i<2;i++)for(const label of ['root','l2','l3','code','data','stack']){ const bytes=s['user-'+i+'-'+label];assert.equal(bytes.length,4096); if(stage===70)zero(bytes);else if(label!=='stack')assert.deepEqual(bytes,held['user-'+i+'-'+label]); } } if(stage===330){calls=1;frames=1;}if(stage===300)calls=4;if(stage===331){calls=5;frames=2;}if(stage===301)calls=8;if(stage===332){calls=11;frames=3;} assert.deepEqual(s.OS_K2155_EL0_REQUESTS.subarray(0,calls*128),snapshots.at(-1).OS_K2155_EL0_REQUESTS.subarray(0,calls*128));zero(s.OS_K2155_EL0_REQUESTS.subarray(calls*128)); assert.equal(s.OS_K2155_EL0_FRAMES.length,2496);assert.deepEqual(s.OS_K2155_EL0_FRAMES.subarray(0,frames*832),snapshots.at(-1).OS_K2155_EL0_FRAMES.subarray(0,frames*832));zero(s.OS_K2155_EL0_FRAMES.subarray(frames*832)); if(svc){ const index=stage-330,call=[0,4,10][index],r=requests.slice(call*16,(call+1)*16),live=s['el0-frame'];assert.equal(live.length,824); assert.deepEqual(live,s.OS_K2155_EL0_FRAMES.subarray(index*832,index*832+824));zero(s.OS_K2155_EL0_FRAMES.subarray(index*832+824,(index+1)*832)); assert.deepEqual(W(live.subarray(0,24)),r.slice(3,6));assert.equal(u(live,64),16n); for(const [at,field]of [[248,9],[256,8],[264,10],[272,11],[816,12]])assert.equal(u(live,at),r[field]); assert.ok(cpu[3]=stackLow&&u(context,96)<=stackTop&&u(context,96)%16n===0n); } assert.equal(e[3],stage===70?before.pages:0n);assert.equal(e[17],stage===70?1n:0n); assert.deepEqual(e.slice(8,10),[0n,0n]);zero(s.OS_K2155_EVIDENCE.subarray(18*8)); assert.deepEqual(e.slice(10,17),[step>=10?4n:0n,step>=12?4n:0n,step>=14?3n:0n,step>=14?11n:0n,step>=14?1n:0n,stage===70?22n:0n,stage===70?before.pages-12n:0n]); } const e=W(snapshots.at(-1).OS_K2155_EVIDENCE);assert.deepEqual(e.slice(10,18),[4n,4n,3n,11n,1n,0n,0n,0n]); return {scope:'native-fault-allocation-and-EL0-subproof',checkpoints:stages.length,retainedPages:34-scenario.releasedPages,ownedPages:34,userPages:12,deviceWorkerPages:22,liveSvcFrames:3,requests:11,guestAccepted:false,productAccepted:false,fullNativeShutdownVerified:false}; }