|
| 1 | +//! An implementation of [`HostInterface`] for (Virtual) Machine |
| 2 | +
|
| 3 | +use crate::host::per_cpu_variables::with_per_cpu_variables_mut; |
| 4 | + |
| 5 | +pub type Machine = crate::LinuxKernel; |
| 6 | + |
| 7 | +#[cfg(not(test))] |
| 8 | +mod alloc { |
| 9 | + const HEAP_ORDER: usize = 25; |
| 10 | + |
| 11 | + #[global_allocator] |
| 12 | + static MACHINE_ALLOCATOR: litebox::mm::allocator::SafeZoneAllocator< |
| 13 | + 'static, |
| 14 | + HEAP_ORDER, |
| 15 | + crate::host::Machine, |
| 16 | + > = litebox::mm::allocator::SafeZoneAllocator::new(); |
| 17 | + |
| 18 | + impl litebox::mm::allocator::MemoryProvider for super::Machine { |
| 19 | + fn alloc(_layout: &core::alloc::Layout) -> Option<(usize, usize)> { |
| 20 | + // dynamic memory allocation here implies memory ballooning or hotplugging |
| 21 | + unimplemented!() |
| 22 | + } |
| 23 | + |
| 24 | + unsafe fn free(_addr: usize) { |
| 25 | + unimplemented!() |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + impl crate::mm::MemoryProvider for super::Machine { |
| 30 | + const GVA_OFFSET: x86_64::VirtAddr = x86_64::VirtAddr::new(0x18000000000); |
| 31 | + const PRIVATE_PTE_MASK: u64 = 0; |
| 32 | + |
| 33 | + fn mem_allocate_pages(order: u32) -> Option<*mut u8> { |
| 34 | + MACHINE_ALLOCATOR.allocate_pages(order) |
| 35 | + } |
| 36 | + |
| 37 | + unsafe fn mem_free_pages(ptr: *mut u8, order: u32) { |
| 38 | + unsafe { |
| 39 | + MACHINE_ALLOCATOR.free_pages(ptr, order); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + unsafe fn mem_fill_pages(start: usize, size: usize) { |
| 44 | + unsafe { MACHINE_ALLOCATOR.fill_pages(start, size) }; |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +unsafe impl litebox::platform::ThreadLocalStorageProvider for Machine { |
| 50 | + fn get_thread_local_storage() -> *mut () { |
| 51 | + let tls = with_per_cpu_variables_mut(|pcv| pcv.tls); |
| 52 | + tls.as_mut_ptr::<()>() |
| 53 | + } |
| 54 | + |
| 55 | + unsafe fn replace_thread_local_storage(value: *mut ()) -> *mut () { |
| 56 | + let tls = with_per_cpu_variables_mut(|pcv| pcv.tls); |
| 57 | + core::mem::replace(&mut tls.as_mut_ptr::<()>(), value.cast()).cast() |
| 58 | + } |
| 59 | +} |
0 commit comments