import assert from 'node:assert/strict'; export function k2100UdpCpuSource(original){ const start=original.indexOf(' pub fn new(local:'),end=original.indexOf(' pub fn snapshot(',start);assert.ok(start>=0&&end>start); let constructor=original.slice(start,end);constructor=constructor.replace('pub fn new(local:[u8;4])','pub unsafe fn new_on_cpu(local:[u8;4],expected_cpu:u64)');assert.equal(constructor.split('namespace:identity()?').length,2);constructor=constructor.replace('namespace:identity()?','namespace:identity_on_cpu(expected_cpu)?'); const begin=original.indexOf(' let id=unsafe{core::ptr::read_volatile(core::ptr::addr_of!(NEXT))}'),finish=original.indexOf('\n}',begin);assert.ok(begin>=0&&finish>begin);const counter=original.slice(begin,finish); return original.slice(0,end)+` /// # Safety /// Retain the exact online CPU owner and exclusive Shared serialization of /// this global namespace issuer. Binding operations stay under the same /// owner until all live ports close. Credentials must name actual processes. `+constructor+original.slice(end)+` fn identity_on_cpu(expected_cpu:u64)->Result{ let(daif,mpidr,current_el)=os_k2100_udp_registers(); if expected_cpu>3||mpidr&0x000000ff00ffffff!=expected_cpu||daif!=0x3c0||current_el!=4{return Err(Error::Configuration);} unsafe{core::ptr::write_volatile(core::ptr::addr_of_mut!(OS_K1865_NONCE_MASK),daif);} ${counter} } fn os_k2100_udp_registers()->(u64,u64,u64){ let(daif,mpidr,current_el):(u64,u64,u64); unsafe{core::arch::asm!("mrs {d},DAIF","mrs {m},MPIDR_EL1","mrs {e},CurrentEL",d=out(reg)daif,m=out(reg)mpidr,e=out(reg)current_el,options(nomem,nostack,preserves_flags));} (daif,mpidr,current_el) } `; }