// Independent physical-byte checker. A frame copied to a log is insufficient: // the live SP_EL1 frame, GIC active bit, owner object and callback flag must agree. import assert from 'node:assert/strict'; import {checkDmaSnapshots,DMA_STAGES,DMA_FORGERIES} from './k2155-dma-check-v2.mjs'; export {DMA_FORGERIES}; const W=b=>Array.from({length:b.length/8},(_,i)=>b.readBigUInt64LE(i*8)); const n=(b,at)=>b.readBigUInt64LE(at),u=(b,at)=>b.readUInt32LE(at),zero=b=>assert.ok(b.every(x=>x===0),'unwritten evidence'); export function irqPhysicalSpans(s){ const v=W(s.OS_K2155_IRQ_STATE).map(Number),l=W(s.OS_K2155_IRQ_LAYOUT).map(Number); const spans=[['irq-owner',v[25],v[26]],['irq-active',v[23],4],['irq-context',v[27],8],['gic-control',v[0],4],['gic-typer',v[0]+4,4]]; for(let d=0;d<2;d++){const id=v[d===0?5:8],word=4*Math.floor(id/32); for(const[name,offset,size]of [['enable',0x100+word,4],['active',0x300+word,4],['pending',0x200+word,4], ['config',0xc00+4*Math.floor(id/16),4],['group',0x80+word,4],['priority',0x400+id,1],['route',0x6000+id*8,8]])spans.push(['gic-'+d+'-'+name,v[0]+offset,size]); } const stage=Number(n(s.OS_K2155_EVIDENCE,8)); if([100,101,110,111].includes(stage)){const count=v[10]+v[12],events=W(s.OS_K2155_IRQ_EVENTS).map(Number);assert.ok(count>=1&&count<=2);spans.push(['irq-frame',events[(count-1)*16+3],l[21]]);} return spans; } export function checkIrqDmaSnapshots(input){ const {snapshots,compiled,symbols,bindings,bootId}=input;assert.equal(snapshots.length,14); const stages=snapshots.map(s=>Number(n(s.OS_K2155_EVIDENCE,8))),first=stages[2];assert.ok([100,110].includes(first)); const other=first===100?110:100;assert.deepEqual(stages,[11,12,first,first+1,other,other+1,...DMA_STAGES.slice(2)]); const base=W(snapshots[0].OS_K2155_IRQ_STATE),l=W(compiled.OS_K2155_IRQ_LAYOUT).map(Number),epoch=BigInt('0x'+bootId); assert.equal(l.length,39);assert.equal(l[21],848);assert.equal(l[38],1);assert.ok(l[0]>0&&l[0]<1024); assert.equal(base[0],BigInt(bindings.distributor));assert.equal(base[1],epoch);assert.equal(base[2],0n); assert.equal(base[27],BigInt(symbols.OS_K2155_IRQ_CONTEXT.address)); assert.ok(base[25]>=BigInt(symbols.OS_K900_STACK.address)+4096n&&base[25]+base[26]<=BigInt(symbols.OS_K900_STACK.address)+65536n+4096n-16n); assert.equal(base[23],BigInt(symbols.OS_K660_IRQ_ACTIVE.address));assert.equal(base[24],BigInt(symbols.OS_K2155_IRQ_FRAMES.address)); for(const [at,name]of [[28,'__os_k2155_irq_vectors'],[29,'__os_k2155_irq_window_begin'],[30,'__os_k2155_irq_window_end'],[31,'OS_K2155_IRQ_LAYOUT']])assert.equal(base[at],BigInt(symbols[name].address)); assert.ok(base[29]0n&&base[21]%2048n===0n);assert.ok(base[22]===0n||base[22]===1n); const ids=[base[5],base[8]],triggers=[base[6],base[9]],deviceBases=[base[4],base[7]],previous=first===100?0:1; assert.notEqual(ids[0],ids[1]); for(let d=0;d<2;d++){const binding=bindings.slots.find(b=>BigInt(b.base)===deviceBases[d]);assert.ok(binding);assert.equal(ids[d],BigInt(binding.intid));assert.equal(triggers[d],BigInt(binding.trigger));} const finalFrames=snapshots[5].OS_K2155_IRQ_FRAMES,finalEvents=snapshots[5].OS_K2155_IRQ_EVENTS; for(const [step,s]of snapshots.entries()){ const v=W(s.OS_K2155_IRQ_STATE),e=W(s.OS_K2155_EVIDENCE),stage=stages[step],inside=step>=2&&step<=5,after=step>=6; const current=inside?Math.floor((stage-100)/10):-1,second=inside&&step>=4,post=inside&&stage%10===1; 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[previous]=returned[previous]=1n;seen=1n<=2?1:0],[16,after?1:0],[17,after?1:0],[18,after?1:0],[20,Number(base[22])]])assert.equal(raw[l[field]],value,'actual owner flag '+field); for(let d=0;d<2;d++){ const at=l[5]+d*l[6];assert.equal(n(raw,at+l[7]),deviceBases[d]);assert.equal(u(raw,at+l[8]),Number(ids[d]));assert.equal(u(raw,at+l[9]),Number(triggers[d])); assert.equal(u(raw,at+l[10]),d===0?2:1);assert.equal(u(raw,at+l[11]),0x554d4551); const id=Number(ids[d]),bit=(2**(id%32))>>>0,word=name=>u(s['gic-'+d+'-'+name],0),isSet=name=>(word(name)&bit)!==0; assert.equal(isSet('enable'),!after);assert.equal(isSet('active'),inside&¤t===d&&!post);assert.equal(isSet('group'),true); assert.equal((word('config')>>>(2*(id%16)))&3,triggers[d]===1n?2:0);assert.equal(s['gic-'+d+'-priority'][0],0x80);assert.equal(n(s['gic-'+d+'-route'],0),base[2]); assert.equal(u(s['gic-typer'],0),Number(base[3]));assert.ok(((u(s['gic-control'],0)>>>31)&1)===0);assert.ok(id<((Number(base[3])&31)+1)*32); if(inside){assert.equal(u(s[`mmio-${d}-interrupt`],0),((d===current&&post)||(second&&d===previous))?0:1);assert.equal(u(s[`mmio-${d}-status`],0),15);assert.equal(u(s[`mmio-${d}-ready`],0),1);} } assert.equal(e[11],after?1n:0n); if(inside){ // The IRQ callback has no allocator access and must retain all DMA frames. assert.deepEqual(s.authority,snapshots[0].authority); for(let d=0;d<2;d++)for(let q=0;q<(d===0?1:2);q++)for(let p=0;p<4;p++)assert.deepEqual(s[`dma-${d}-${q}-${p}`],snapshots[6][`dma-${d}-${q}-${p}`]); const index=second?1:0,event=W(s.OS_K2155_IRQ_EVENTS).slice(index*16,(index+1)*16),frame=s['irq-frame'];assert.equal(frame.length,848); assert.deepEqual(frame,s.OS_K2155_IRQ_FRAMES.subarray(index*848,(index+1)*848));assert.deepEqual(frame,finalFrames.subarray(index*848,(index+1)*848)); assert.deepEqual(event.slice(0,3),[epoch,BigInt(current),ids[current]]);assert.equal(event[3]%16n,0n); assert.deepEqual(event.slice(4,8),[n(frame,l[24]),n(frame,l[22]),n(frame,l[25]),5n]); assert.ok(event[4]>=base[29]&&event[4]=low&&event[5]<=top); const bit=1n<<(ids[current]%32n);assert.ok((event[8]&bit)!==0n);assert.deepEqual(event.slice(9,11),[1n,0n]);assert.deepEqual(event.slice(12),[1n,0n,base[22],post?1n:0n]);if(!post)assert.equal(event[11],0n); // Active readback word may contain unrelated routes, but never the owned bit. if(post)assert.ok((event[11]&bit)===0n); if(index===1){assert.deepEqual(s.OS_K2155_IRQ_FRAMES.subarray(0,848),finalFrames.subarray(0,848));assert.deepEqual(s.OS_K2155_IRQ_EVENTS.subarray(0,128),finalEvents.subarray(0,128));} zero(s.OS_K2155_IRQ_FRAMES.subarray((index+1)*848));zero(s.OS_K2155_IRQ_EVENTS.subarray((index+1)*128)); }else if(after){assert.deepEqual(s.OS_K2155_IRQ_FRAMES,finalFrames);assert.deepEqual(s.OS_K2155_IRQ_EVENTS,finalEvents);} else{zero(s.OS_K2155_IRQ_FRAMES);zero(s.OS_K2155_IRQ_EVENTS);} } // Only the extension's separately verified reserved flag is normalized for // reuse of the unchanged v2 DMA checker. All physical bytes remain untouched. const dma=snapshots.filter((_,i)=>i<2||i>5).map(s=>{const e=Buffer.from(s.OS_K2155_EVIDENCE);e.writeBigUInt64LE(0n,88);return {...s,OS_K2155_EVIDENCE:e};}); const result=checkDmaSnapshots({...input,snapshots:dma}); return {...result,component:'native-irq-block-network-dma',stages:14,physicalIrqFrames:2,liveIrqCheckpoints:4,ownedSourcesMasked:2,handlerEretVerified:true,roadmapAccepted:false}; } export const IRQ_FORGERIES=[ ['owner-entered-counter',2,'irq-owner','entered'],['fake-owner-return',2,'irq-owner','returned'], ['owner-window-returned-in-handler',2,'irq-owner','window-returned'],['wrong-context-owner',2,'irq-context',0], ['callback-cleared-before-return',2,'irq-active',0],['missing-hardware-active',2,'gic-current-active',0], ['fake-device-ack',2,'mmio-current-interrupt',0],['hardware-active-after-eoi',3,'gic-current-active',0], ['live-frame-elr',2,'irq-frame','elr'],['live-frame-canary',2,'irq-frame','canary'], ['live-frame-spsr',2,'irq-frame','spsr'],['live-frame-reserved-status',2,'irq-frame','reserved-status'],['live-frame-irq-masked',2,'irq-frame','irq-mask'],['live-frame-stack',2,'irq-frame','sp'], ['masked-source-enabled',6,'gic-0-enable',0],['changed-dtb-trigger',6,'gic-0-config',0], ['wrong-route',6,'gic-0-route',0],['wrong-priority',6,'gic-0-priority',0], ['wrong-gic-capacity',6,'gic-typer',0],['false-mask-extension-flag',6,'OS_K2155_EVIDENCE',88], ['allocator-mutated-in-handler',2,'authority',0],['stale-copied-frame',6,'OS_K2155_IRQ_FRAMES',0], ['changed-irq-layout',0,'OS_K2155_IRQ_LAYOUT',0],['callback-active-after-window',6,'irq-active',0], ]; export function irqForgery(snapshots,entry,compiled){ const[name,step,original,location]=entry,copy=snapshots.map(s=>({...s})),s=copy[step],v=W(s.OS_K2155_IRQ_STATE),l=W(compiled.OS_K2155_IRQ_LAYOUT).map(Number); const current=Math.floor((Number(n(s.OS_K2155_EVIDENCE,8))-100)/10);let key=original.replace('gic-current','gic-'+current).replace('mmio-current','mmio-'+current),offset=location; if(typeof location==='string')offset=({'entered':l[12]+current*8,'returned':l[13]+current*8,'window-returned':l[16],elr:l[24],canary:l[33],spsr:l[25],'reserved-status':l[25]+3,'irq-mask':l[25],sp:l[22]})[location]; s[key]=Buffer.from(s[key]); if(key.startsWith('gic-')&&(key.endsWith('-active')||key.endsWith('-enable'))){const d=Number(key.split('-')[1]),id=Number(v[d===0?5:8]);s[key].writeUInt32LE((u(s[key],0)^(2**(id%32)))>>>0,0);} else if(key.endsWith('-config')){const id=Number(v[5]);s[key].writeUInt32LE((u(s[key],0)^(2**(2*(id%16))))>>>0,0);} else s[key][offset]^=location==='irq-mask'?0x80:1; return copy; }