//! Process-scoped UDP port ownership. Caller credentials are trusted kernel //! inputs, not fields an EL0 request may choose. No packet I/O is provided here. #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(u64)] pub enum Error { Configuration=1, Exhausted=2, Credentials=3, Access=4, Address=5, InUse=6, Full=7, Foreign=8, Stale=9 } #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(C)] pub struct Principal { pub table:u64,pub slot:u64,pub generation:u64 } impl Principal { fn valid(self)->bool { self.table!=0&&self.slot<16&&self.generation!=0 } } #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Credentials { pub principal:Principal,pub privileged_ports:bool } #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(C)] pub struct Handle { namespace:u64,slot:u64,generation:u64,principal:Principal } impl Handle { pub fn slot(self)->u64{self.slot} pub fn generation(self)->u64{self.generation} pub fn principal(self)->Principal{self.principal} } #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(C)] pub struct Entry { pub live:u64,pub generation:u64,pub principal:Principal,pub address:u64,pub port:u64 } const EMPTY:Entry=Entry{live:0,generation:0,principal:Principal{table:0,slot:0,generation:0},address:0,port:0}; #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Snapshot{pub namespace:u64,pub local:u64,pub next_ephemeral:u64,pub entries:[Entry;N]} #[repr(C)] pub struct Bindings { entries:[Entry;N],namespace:u64,local:u64,next_ephemeral:u64 } static mut NEXT:u64=1; #[unsafe(no_mangle)]pub static mut OS_K1865_NONCE_MASK:u64=0; fn identity()->Result{ let daif:u64;let mpidr:u64;unsafe{core::arch::asm!("mrs {d}, DAIF","mrs {m}, MPIDR_EL1",d=out(reg)daif,m=out(reg)mpidr,options(nomem,nostack,preserves_flags));} if daif&0x80==0||mpidr&0x000000ff00ffffff!=0{return Err(Error::Configuration);} unsafe{core::ptr::write_volatile(core::ptr::addr_of_mut!(OS_K1865_NONCE_MASK),daif);} let id=unsafe{core::ptr::read_volatile(core::ptr::addr_of!(NEXT))};let next=id.checked_add(1).ok_or(Error::Exhausted)?; unsafe{core::ptr::write_volatile(core::ptr::addr_of_mut!(NEXT),next);}Ok(id) } fn unicast(ip:u32)->bool { let first=ip>>24;first!=0&&first!=127&&first<224 } impl Bindings { pub fn new(local:[u8;4])->Result{let local=u32::from_be_bytes(local);if N==0||N>64||!unicast(local){return Err(Error::Configuration);}Ok(Self{entries:[EMPTY;N],namespace:identity()?,local:local as u64,next_ephemeral:49152})} /// # 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. pub unsafe fn new_on_cpu(local:[u8;4],expected_cpu:u64)->Result{let local=u32::from_be_bytes(local);if N==0||N>64||!unicast(local){return Err(Error::Configuration);}Ok(Self{entries:[EMPTY;N],namespace:identity_on_cpu(expected_cpu)?,local:local as u64,next_ephemeral:49152})} pub fn snapshot(&self)->Snapshot{Snapshot{namespace:self.namespace,local:self.local,next_ephemeral:self.next_ephemeral,entries:self.entries}} /// Wildcard and exact-local binds conflict on the same port. No port sharing /// or SO_REUSEADDR behavior is implied. Port zero requests an ephemeral port. pub fn bind(&mut self,credentials:Credentials,address:[u8;4],port:u16)->Result{ let principal=credentials.principal;if !principal.valid(){return Err(Error::Credentials);} if port!=0&&port<1024&&!credentials.privileged_ports{return Err(Error::Access);} let address=u32::from_be_bytes(address)as u64;if address!=0&&address!=self.local{return Err(Error::Address);} let slot=self.entries.iter().position(|e|e.live==0&&e.generation!=u64::MAX).ok_or(Error::Full)?; let mut selected=u64::from(port);let mut next=self.next_ephemeral; if port==0{let mut found=None;for _ in 0..=N{let probe=next;next=if next==65535{49152}else{next+1};if !self.entries.iter().any(|e|e.live==1&&e.port==probe){found=Some(probe);break;}}selected=found.ok_or(Error::Full)?;} if self.entries.iter().any(|e|e.live==1&&e.port==selected){return Err(Error::InUse);} let generation=self.entries[slot].generation.checked_add(1).ok_or(Error::Exhausted)?; self.entries[slot]=Entry{live:1,generation,principal,address,port:selected};self.next_ephemeral=next;Ok(Handle{namespace:self.namespace,slot:slot as u64,generation,principal}) } pub fn lookup(&self,principal:Principal,handle:Handle)->Result{ if !principal.valid(){return Err(Error::Credentials);} if handle.namespace!=self.namespace||handle.principal!=principal{return Err(Error::Foreign);} let e=self.entries.get(handle.slot as usize).ok_or(Error::Stale)?; if e.live!=1||e.generation!=handle.generation||e.principal!=principal{return Err(Error::Stale);}Ok(*e) } pub fn close(&mut self,principal:Principal,handle:Handle)->Result<(),Error>{let e=self.lookup(principal,handle)?;self.entries[handle.slot as usize]=Entry{generation:e.generation,..EMPTY};Ok(())} /// Kernel exit cleanup uses the actual process generation, so reused process /// slots cannot close a different owner's bindings. pub fn close_principal(&mut self,principal:Principal)->Result{if !principal.valid(){return Err(Error::Credentials);}let mut closed=0;for e in &mut self.entries{if e.live==1&&e.principal==principal{*e=Entry{generation:e.generation,..EMPTY};closed+=1;}}Ok(closed)} } impl Bindings{pub const fn os_k1865_physical_layout()->[u64;14]{[core::mem::size_of::()as u64,core::mem::offset_of!(Self,entries)as u64,core::mem::size_of::()as u64,N as u64,core::mem::offset_of!(Entry,live)as u64,core::mem::offset_of!(Entry,generation)as u64,core::mem::offset_of!(Entry,principal)as u64+core::mem::offset_of!(Principal,table)as u64,core::mem::offset_of!(Entry,principal)as u64+core::mem::offset_of!(Principal,slot)as u64,core::mem::offset_of!(Entry,principal)as u64+core::mem::offset_of!(Principal,generation)as u64,core::mem::offset_of!(Entry,address)as u64,core::mem::offset_of!(Entry,port)as u64,core::mem::offset_of!(Self,namespace)as u64,core::mem::offset_of!(Self,local)as u64,core::mem::offset_of!(Self,next_ephemeral)as u64]} } 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);} let id=unsafe{core::ptr::read_volatile(core::ptr::addr_of!(NEXT))};let next=id.checked_add(1).ok_or(Error::Exhausted)?; unsafe{core::ptr::write_volatile(core::ptr::addr_of_mut!(NEXT),next);}Ok(id) } 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) }