@@ -14,11 +14,48 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- use alloc:: alloc:: { alloc, dealloc, Layout } ;
1817use core:: ffi:: c_void;
1918use core:: sync:: atomic:: { AtomicPtr , AtomicU64 , Ordering } ;
2019
2120use hyperlight_guest_bin:: exceptions:: handler;
21+ use hyperlight_guest_bin:: paging;
22+
23+ // Extremely stupid virtual address allocator
24+ // 0x1_0000_0000 is where the module is
25+ // we start at
26+ // 0x100_0000_0000 and go up from there
27+ static FIRST_VADDR : AtomicU64 = AtomicU64 :: new ( 0x100_0000_0000u64 ) ;
28+ fn page_fault_handler (
29+ _exception_number : u64 ,
30+ info : * mut handler:: ExceptionInfo ,
31+ _ctx : * mut handler:: Context ,
32+ page_fault_address : u64 ,
33+ ) -> bool {
34+ let error_code = unsafe { ( & raw const ( * info) . error_code ) . read_volatile ( ) } ;
35+ // TODO: check if this is a guard-region trap (which can't happen
36+ // right now since we don't actually set the permissions properly
37+ // in mprotect)
38+
39+ // TODO: replace this with some generic virtual memory area data
40+ // structure in hyperlight core
41+ if ( error_code & 0x1 ) == 0x0 && page_fault_address >= 0x100_0000_0000u64 {
42+ unsafe {
43+ let phys_page = paging:: alloc_phys_pages ( 1 ) ;
44+ let virt_base = ( page_fault_address & !0xFFF ) as * mut u8 ;
45+ paging:: map_region (
46+ phys_page,
47+ virt_base,
48+ hyperlight_guest_bin:: OS_PAGE_SIZE as u64 ,
49+ ) ;
50+ virt_base. write_bytes ( 0u8 , hyperlight_guest_bin:: OS_PAGE_SIZE as usize ) ;
51+ }
52+ return true ; // Try again!
53+ }
54+ false
55+ }
56+ pub ( crate ) fn register_page_fault_handler ( ) {
57+ handler:: handlers[ 14 ] . store ( page_fault_handler as usize as u64 , Ordering :: Release ) ;
58+ }
2259
2360// Wasmtime Embedding Interface
2461
@@ -27,24 +64,26 @@ use hyperlight_guest_bin::exceptions::handler;
2764 * appropriate interrupt handler yet. Consequently, we configure
2865 * wasmtime not to use any guard region, and precommit memory. */
2966#[ no_mangle]
30- pub extern "C" fn wasmtime_mmap_new ( size : usize , _prot_flags : u32 , ret : & mut * mut u8 ) -> i32 {
31- * ret = unsafe { alloc ( Layout :: from_size_align ( size , 0x1000 ) . unwrap ( ) ) } ;
67+ pub extern "C" fn wasmtime_mmap_new ( _size : usize , _prot_flags : u32 , ret : & mut * mut u8 ) -> i32 {
68+ * ret = FIRST_VADDR . fetch_add ( 0x100_0000_0000 , Ordering :: Relaxed ) as * mut u8 ;
3269 0
3370}
3471
3572/* Because of the precommitted memory strategy, we can't generally
3673 * support remap */
3774#[ no_mangle]
3875pub extern "C" fn wasmtime_mmap_remap ( addr : * mut u8 , size : usize , prot_flags : u32 ) -> i32 {
39- panic ! (
40- "wasmtime_mmap_remap {:x} {:x} {:x}" ,
41- addr as usize , size, prot_flags
42- ) ;
76+ if size > 0x100_0000_0000 {
77+ panic ! (
78+ "wasmtime_mmap_remap {:x} {:x} {:x}" ,
79+ addr as usize , size, prot_flags
80+ ) ;
81+ }
82+ 0
4383}
4484
4585#[ no_mangle]
46- pub extern "C" fn wasmtime_munmap ( ptr : * mut u8 , size : usize ) -> i32 {
47- unsafe { dealloc ( ptr, Layout :: from_size_align ( size, 0x1000 ) . unwrap ( ) ) } ;
86+ pub extern "C" fn wasmtime_munmap ( _ptr : * mut u8 , _size : usize ) -> i32 {
4887 0
4988}
5089
0 commit comments