Skip to content

Commit 5bfb7c1

Browse files
committed
revise interface to allocate physical / virtual memory regions
1 parent f3a39af commit 5bfb7c1

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/wasm/capi.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use align_address::Align;
2+
use free_list::{PageLayout, PageRange};
23
use hermit_sync::InterruptTicketMutex;
3-
use memory_addresses::VirtAddr;
4+
use memory_addresses::{PhysAddr, VirtAddr};
45

56
use crate::arch;
67
#[cfg(target_arch = "x86_64")]
@@ -93,12 +94,22 @@ pub extern "C" fn wasmtime_page_size() -> usize {
9394
#[unsafe(no_mangle)]
9495
pub extern "C" fn wasmtime_mmap_new(size: usize, prot_flags: WasmProt, ret: &mut *mut u8) -> i32 {
9596
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());
97103
if prot_flags.is_empty() {
98104
*ret = virtual_address.as_mut_ptr();
99105
return 0;
100106
}
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());
102113

103114
let count = size / BasePageSize::SIZE as usize;
104115
let mut flags = PageTableEntryFlags::empty();
@@ -151,10 +162,22 @@ pub extern "C" fn wasmtime_munmap(ptr: *mut u8, size: usize) -> i32 {
151162
virtual_address,
152163
size / BasePageSize::SIZE as usize,
153164
);
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+
}
155172
}
156173

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+
}
158181

159182
0
160183
}
@@ -183,7 +206,12 @@ pub extern "C" fn wasmtime_mprotect(ptr: *mut u8, size: usize, prot_flags: WasmP
183206
arch::mm::paging::map::<BasePageSize>(virtual_address, physical_address, count, flags);
184207
0
185208
} 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());
187215
arch::mm::paging::map::<BasePageSize>(virtual_address, physical_address, count, flags);
188216
0
189217
}

src/wasm/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ async fn wasm_run() {
274274
loop {
275275
let obj = crate::core_scheduler()
276276
.get_object(fd::STDOUT_FILENO)
277-
.await
278277
.unwrap();
279278

280279
while let Some(data) = OUTPUT.lock().data.pop_front() {

0 commit comments

Comments
 (0)