// Explicit synthetic checker fixture. This is not a VM execution or evidence // suitable for guest admission. It exercises the independent raw-byte checker. import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import {DMA_STAGES,DMA_FORGERIES} from '../../scripts/os-acceptance/features/k2155-dma-check-v2.mjs'; import {ledger,released} from '../../scripts/os-acceptance/features/k2155-dma-authority-check.mjs'; import {dmaPacket} from '../../scripts/os-acceptance/dma-network.mjs'; const nativePacket=(bootId,queue)=>dmaPacket((BigInt('0x'+bootId)^0x4b32313535444d41n).toString(16).padStart(16,'0'),queue); const candidate=JSON.parse(readFileSync(process.argv[2])),image=readFileSync(candidate.artifacts.image.path),nm=readFileSync(candidate.artifacts.nm.path,'utf8'); const name='OS_K1000_AUTHORITY_LAYOUT_EXPECTED',match=nm.match(new RegExp('^([a-f0-9]{16}) ([a-f0-9]{16}) [A-Za-z] '+name+'$','m')); assert.ok(match);const symbol={address:parseInt(match[1],16),size:parseInt(match[2],16)},at=symbol.address-candidate.imageSpan.start; const compiled={[name]:image.subarray(at,at+symbol.size)},symbols={[name]:symbol},l=Array.from({length:24},(_,i)=>Number(compiled[name].readBigUInt64LE(i*8))); for(const line of nm.split('\n')){const m=line.match(/^([a-f0-9]{16}) (?:([a-f0-9]{16}) )?[A-Za-z] (\S+)$/);if(m)symbols[m[3]]={address:parseInt(m[1],16),size:m[2]?parseInt(m[2],16):0};} const activeSymbols=Object.entries(symbols).filter(([name,value])=>name.endsWith('18OS_K660_IRQ_ACTIVE')&&value.size===4);assert.equal(activeSymbols.length,1);symbols.OS_K660_IRQ_ACTIVE=activeSymbols[0][1]; const il=symbols.OS_K2155_IRQ_LAYOUT;assert.ok(il);compiled.OS_K2155_IRQ_LAYOUT=image.subarray(il.address-candidate.imageSpan.start,il.address-candidate.imageSpan.start+il.size); const bootId='1122334455667788',freeBefore=64n,freeHeld=52n,addresses=Array.from({length:12},(_,i)=>0x48000000n+BigInt(i)*4096n),authority=Buffer.alloc(l[0]); authority.writeBigUInt64LE(1n,l[1]+l[3]);authority.writeBigUInt64LE(0x4800c000n,l[1]+l[2]+l[21]);authority.writeBigUInt64LE(0x48040000n,l[1]+l[2]+l[22]); for(const [i,address]of addresses.entries()){const at=l[5]+i*l[6];authority[at+l[9]]=1;authority.writeBigUInt64LE(address,at+l[7]);authority.writeBigUInt64LE(BigInt(i+1),at+l[8]);} const after=released(authority,l,ledger(authority,l),addresses),sector=Buffer.alloc(512,0x55); const baseline={sent:1,received:2},exchange={afterCapturedStage:22,tx:nativePacket(bootId,1).toString('hex'),rx:nativePacket(bootId,0).toString('hex'),sentCount:2,receivedCount:3}; function words(values){const b=Buffer.alloc(values.length*8);values.forEach((v,i)=>b.writeBigUInt64LE(BigInt(v),i*8));return b;} function desc(b,at,address,length,flags,next){b.writeBigUInt64LE(address,at);b.writeUInt32LE(length,at+8);b.writeUInt16LE(flags,at+12);b.writeUInt16LE(next,at+14);} const snapshots=[]; for(const stage of DMA_STAGES){ const phase=Math.floor(stage/10),selected=stage%10-1; const e=Array(32).fill(0n);e[0]=0x4b32313535444d41n;e[1]=BigInt(stage);e[2]=freeBefore;e[3]=freeHeld;e[4]=BigInt('0x'+bootId); if(phase>=2)e[5]=e[6]=1n;if(phase>=3)e[7]=1n;if(phase>=4)e[8]=1n;if(phase===5){e[9]=freeBefore;e[10]=1n;} const s={OS_K2155_EVIDENCE:words(e),[name]:Buffer.from(compiled[name]),OS_K2155_DMA_AUTHORITY:words([0x41000000,l[0],symbol.address,192]),authority:Buffer.from(phase===5?after:authority)}; const all=Array(128).fill(0n); for(let d=0;d<2;d++){ const count=d===0?1:2,row=Array(64).fill(0n),id=d===0?2:1,base=d===0?0xa003e00:0xa003c00; row.splice(0,12,...[base,BigInt('0x'+bootId),id,0x554d4551,phase>=4?0:15,phase>=4?0:1,phase>=4||d===0?0:1,phase>=2&&phase<4?1:0,phase>=2?1:0,phase>=3?1:0,phase===5?1:0,d===0?0:selected].map(BigInt)); for(const [label,value]of [['magic',0x74726976],['version',2],['device',id],['vendor',0x554d4551],['ready',row[5+(d===0?0:selected)]],['status',row[4]],['interrupt',row[7]]]){ const b=Buffer.alloc(4);b.writeUInt32LE(Number(value));s[`mmio-${d}-${label}`]=b; } for(let q=0;qBuffer.alloc(4096)); row.splice(12+q*20,4,...frames); if(phase>=2){const len=pending?0:d===0?513:q===0?72:0;row.splice(16+q*20,6,1n,1n,pending?0n:1n,0n,BigInt(len),0n);} if(phase>=2&&phase<5){const [de,av,us,bu]=pages;av.writeUInt16LE(1,2);us.writeUInt16LE(pending?0:1,2);us.writeUInt32LE(pending?0:d===0?513:q===0?72:0,8); if(d===0){desc(de,0,frames[3],16,1,1);desc(de,16,frames[3]+512n,512,3,2);desc(de,32,frames[3]+16n,1,2,0);bu.writeBigUInt64LE(111n,8);sector.copy(bu,512);} else{desc(de,0,frames[3],q===0?2048:72,q===0?2:0,0);if(!pending)nativePacket(bootId,q).copy(bu,12);bu[2048]=0xa5;} } for(let p=0;p<4;p++)s[`dma-${d}-${q}-${p}`]=pages[p]; } all.splice(d*64,64,...row); } s.OS_K2155_DMA_DEVICES=words(all);snapshots.push(s); } import {checkIrqDmaSnapshots,IRQ_FORGERIES,irqForgery} from '../../scripts/os-acceptance/features/k2155-irq-dma-check.mjs'; const W=b=>Array.from({length:b.length/8},(_,i)=>b.readBigUInt64LE(i*8)); const ilayout=W(compiled.OS_K2155_IRQ_LAYOUT).map(Number),stack=BigInt(symbols.OS_K900_STACK.address)+4096n,owner=stack+32768n; const bindings={distributor:0x8000000,slots:[{base:0xa003e00,intid:79,trigger:1},{base:0xa003c00,intid:78,trigger:1}]}; function number32(v){const b=Buffer.alloc(4);b.writeUInt32LE(v>>>0);return b;} const ids=[79,78],frames=Buffer.alloc(8*848),events=Buffer.alloc(8*128),original=snapshots.map(s=>({...s})); let count=0; function prepare(first,tco=0n){ const other=first===100?110:100,prior=(first-100)/10,stages=[11,12,first,first+1,other,other+1,...DMA_STAGES.slice(2)],out=[]; frames.fill(0);events.fill(0);count=0; for(const[step,stage]of stages.entries()){ const inside=step>=2&&step<=5,after=step>=6,current=inside?Math.floor((stage-100)/10):-1,second=inside&&step>=4,post=inside&&stage%10===1; let s; if(inside){s=Object.fromEntries(Object.entries(original[2]).map(([k,b])=>[k,Buffer.from(b)]));s.OS_K2155_EVIDENCE=Buffer.from(original[0].OS_K2155_EVIDENCE);s.OS_K2155_EVIDENCE.writeBigUInt64LE(BigInt(stage),8);} else{s=Object.fromEntries(Object.entries(original[DMA_STAGES.indexOf(stage)]).map(([k,b])=>[k,Buffer.from(b)]));} s.OS_K2155_EVIDENCE.writeBigUInt64LE(after?1n:0n,88); const entered=after?[1n,1n]:[0n,0n],returned=after?[1n,1n]:[0n,0n];let seen=after?3n:0n; if(inside){entered[current]=1n;if(second){entered[prior]=returned[prior]=1n;seen=1n<=2?1:0],[16,after?1:0],[17,after?1:0],[18,after?1:0],[20,1]])raw[ilayout[field]]=value; const active=inside&&!post?(2**(ids[current]%32)):0; for(let d=0;d<2;d++){ const at=ilayout[5]+d*ilayout[6];raw.writeBigUInt64LE(BigInt(bindings.slots[d].base),at+ilayout[7]); for(const[field,value]of [[8,ids[d]],[9,1],[10,d===0?2:1],[11,0x554d4551]])raw.writeUInt32LE(value,at+ilayout[field]); for(const[name,value]of [['enable',after?0:0xc000],['active',active],['pending',0],['config',0xa0000000],['group',0xc000]])s['gic-'+d+'-'+name]=number32(value); s['gic-'+d+'-priority']=Buffer.from([0x80]);s['gic-'+d+'-route']=words([0]); if(inside){const interrupt=((d===current&&post)||(second&&d===prior))?0:1;s['mmio-'+d+'-interrupt']=number32(interrupt);} if(after&&stage<30){s['mmio-'+d+'-interrupt']=number32(0);s.OS_K2155_DMA_DEVICES.writeBigUInt64LE(0n,(d*64+7)*8);} } s['gic-typer']=number32(3);s['gic-control']=number32(1);s['irq-owner']=raw;s['irq-active']=number32(inside?1:0);s['irq-context']=words([inside?owner:0]); if(inside){ const index=second?1:0,address=stack+24576n-BigInt(index*1024),pc=BigInt(symbols.__os_k2155_irq_window_begin.address)+8n,frame=Buffer.alloc(848); for(const[field,value]of [[22,address+848n],[24,pc],[25,5n|tco],[28,5n],[32,4n],[33,0x4b3932304652414dn]])frame.writeBigUInt64LE(value,ilayout[field]); frame.copy(frames,index*848);words([BigInt('0x'+bootId),current,ids[current],address,pc,address+848n,5n|tco,5,2**(ids[current]%32),1,0,0,1,0,1,post?1:0]).copy(events,index*128);s['irq-frame']=frame; } s.OS_K2155_IRQ_FRAMES=Buffer.from(frames);s.OS_K2155_IRQ_EVENTS=Buffer.from(events);out.push(s); } return out; } export {prepare,compiled,symbols,bindings,bootId};