|
1 | 1 | use align_address::Align; |
| 2 | +use free_list::{PageLayout, PageRange}; |
2 | 3 | use hermit_sync::InterruptTicketMutex; |
3 | | -use memory_addresses::VirtAddr; |
| 4 | +use memory_addresses::{PhysAddr, VirtAddr}; |
4 | 5 |
|
5 | 6 | use crate::arch; |
6 | 7 | #[cfg(target_arch = "x86_64")] |
@@ -93,12 +94,22 @@ pub extern "C" fn wasmtime_page_size() -> usize { |
93 | 94 | #[unsafe(no_mangle)] |
94 | 95 | pub extern "C" fn wasmtime_mmap_new(size: usize, prot_flags: WasmProt, ret: &mut *mut u8) -> i32 { |
95 | 96 | let size = size.align_up(BasePageSize::SIZE as usize); |
96 | | - let virtual_address = crate::mm::virtualmem::allocate(size).unwrap(); |
| 97 | + let layout = PageLayout::from_size(size).unwrap(); |
| 98 | + let page_range = crate::mm::virtualmem::KERNEL_FREE_LIST |
| 99 | + .lock() |
| 100 | + .allocate(layout) |
| 101 | + .unwrap(); |
| 102 | + let virtual_address = VirtAddr::from(page_range.start()); |
97 | 103 | if prot_flags.is_empty() { |
98 | 104 | *ret = virtual_address.as_mut_ptr(); |
99 | 105 | return 0; |
100 | 106 | } |
101 | | - let physical_address = crate::mm::physicalmem::allocate(size).unwrap(); |
| 107 | + let frame_layout = PageLayout::from_size(size).unwrap(); |
| 108 | + let frame_range = crate::mm::physicalmem::PHYSICAL_FREE_LIST |
| 109 | + .lock() |
| 110 | + .allocate(frame_layout) |
| 111 | + .expect("Failed to allocate Physical Memory for wasmtime"); |
| 112 | + let physical_address = PhysAddr::from(frame_range.start()); |
102 | 113 |
|
103 | 114 | let count = size / BasePageSize::SIZE as usize; |
104 | 115 | let mut flags = PageTableEntryFlags::empty(); |
@@ -151,10 +162,22 @@ pub extern "C" fn wasmtime_munmap(ptr: *mut u8, size: usize) -> i32 { |
151 | 162 | virtual_address, |
152 | 163 | size / BasePageSize::SIZE as usize, |
153 | 164 | ); |
154 | | - crate::mm::physicalmem::deallocate(phys_addr, size); |
| 165 | + let range = PageRange::from_start_len(phys_addr.as_usize(), size).unwrap(); |
| 166 | + unsafe { |
| 167 | + crate::mm::physicalmem::PHYSICAL_FREE_LIST |
| 168 | + .lock() |
| 169 | + .deallocate(range) |
| 170 | + .unwrap(); |
| 171 | + } |
155 | 172 | } |
156 | 173 |
|
157 | | - crate::mm::virtualmem::deallocate(virtual_address, size); |
| 174 | + let range = PageRange::from_start_len(virtual_address.as_usize(), size).unwrap(); |
| 175 | + unsafe { |
| 176 | + crate::mm::virtualmem::KERNEL_FREE_LIST |
| 177 | + .lock() |
| 178 | + .deallocate(range) |
| 179 | + .unwrap(); |
| 180 | + } |
158 | 181 |
|
159 | 182 | 0 |
160 | 183 | } |
@@ -183,7 +206,12 @@ pub extern "C" fn wasmtime_mprotect(ptr: *mut u8, size: usize, prot_flags: WasmP |
183 | 206 | arch::mm::paging::map::<BasePageSize>(virtual_address, physical_address, count, flags); |
184 | 207 | 0 |
185 | 208 | } else { |
186 | | - let physical_address = crate::mm::physicalmem::allocate(size).unwrap(); |
| 209 | + let frame_layout = PageLayout::from_size(size).unwrap(); |
| 210 | + let frame_range = crate::mm::physicalmem::PHYSICAL_FREE_LIST |
| 211 | + .lock() |
| 212 | + .allocate(frame_layout) |
| 213 | + .expect("Failed to allocate Physical Memory for TaskStacks"); |
| 214 | + let physical_address = PhysAddr::from(frame_range.start()); |
187 | 215 | arch::mm::paging::map::<BasePageSize>(virtual_address, physical_address, count, flags); |
188 | 216 | 0 |
189 | 217 | } |
|
0 commit comments