@@ -18,9 +18,9 @@ limitations under the License.
1818// === Dependencies ===
1919extern crate alloc;
2020
21+ use core:: ffi:: c_void;
2122use alloc:: string:: ToString ;
2223
23- use buddy_system_allocator:: LockedHeap ;
2424#[ cfg( target_arch = "x86_64" ) ]
2525use exceptions:: { gdt:: load_gdt, idtr:: load_idt} ;
2626use guest_function:: call:: dispatch_function;
@@ -34,6 +34,7 @@ use hyperlight_guest::exit::{abort_with_code_and_message, halt};
3434use hyperlight_guest:: guest_handle:: handle:: GuestHandle ;
3535use hyperlight_guest_tracing:: { trace, trace_function} ;
3636use log:: LevelFilter ;
37+ use libc_alloc:: LibcAlloc ;
3738use spin:: Once ;
3839
3940// === Modules ===
@@ -54,75 +55,17 @@ pub mod guest_function {
5455
5556pub mod guest_logger;
5657pub mod host_comm;
57- pub mod memory;
5858pub mod paging;
5959
6060#[ cfg( feature = "libc" ) ]
6161mod host_bridge;
6262
63- // Globals
64- #[ cfg( feature = "mem_profile" ) ]
65- struct ProfiledLockedHeap < const ORDER : usize > ( LockedHeap < ORDER > ) ;
66- #[ cfg( feature = "mem_profile" ) ]
67- unsafe impl < const ORDER : usize > alloc:: alloc:: GlobalAlloc for ProfiledLockedHeap < ORDER > {
68- unsafe fn alloc ( & self , layout : core:: alloc:: Layout ) -> * mut u8 {
69- let addr = unsafe { self . 0 . alloc ( layout) } ;
70- unsafe {
71- core:: arch:: asm!( "out dx, al" ,
72- in( "dx" ) OutBAction :: TraceMemoryAlloc as u16 ,
73- in( "rax" ) layout. size( ) as u64 ,
74- in( "rcx" ) addr as u64 ) ;
75- }
76- addr
77- }
78- unsafe fn dealloc ( & self , ptr : * mut u8 , layout : core:: alloc:: Layout ) {
79- unsafe {
80- core:: arch:: asm!( "out dx, al" ,
81- in( "dx" ) OutBAction :: TraceMemoryFree as u16 ,
82- in( "rax" ) layout. size( ) as u64 ,
83- in( "rcx" ) ptr as u64 ) ;
84- self . 0 . dealloc ( ptr, layout)
85- }
86- }
87- unsafe fn alloc_zeroed ( & self , layout : core:: alloc:: Layout ) -> * mut u8 {
88- let addr = unsafe { self . 0 . alloc_zeroed ( layout) } ;
89- unsafe {
90- core:: arch:: asm!( "out dx, al" ,
91- in( "dx" ) OutBAction :: TraceMemoryAlloc as u16 ,
92- in( "rax" ) layout. size( ) as u64 ,
93- in( "rcx" ) addr as u64 ) ;
94- }
95- addr
96- }
97- unsafe fn realloc (
98- & self ,
99- ptr : * mut u8 ,
100- layout : core:: alloc:: Layout ,
101- new_size : usize ,
102- ) -> * mut u8 {
103- let new_ptr = unsafe { self . 0 . realloc ( ptr, layout, new_size) } ;
104- unsafe {
105- core:: arch:: asm!( "out dx, al" ,
106- in( "dx" ) OutBAction :: TraceMemoryFree as u16 ,
107- in( "rax" ) layout. size( ) as u64 ,
108- in( "rcx" ) ptr) ;
109- core:: arch:: asm!( "out dx, al" ,
110- in( "dx" ) OutBAction :: TraceMemoryAlloc as u16 ,
111- in( "rax" ) new_size as u64 ,
112- in( "rcx" ) new_ptr) ;
113- }
114- new_ptr
115- }
116- }
63+
64+
11765
11866// === Globals ===
119- #[ cfg( not( feature = "mem_profile" ) ) ]
120- #[ global_allocator]
121- pub ( crate ) static HEAP_ALLOCATOR : LockedHeap < 32 > = LockedHeap :: < 32 > :: empty ( ) ;
122- #[ cfg( feature = "mem_profile" ) ]
12367#[ global_allocator]
124- pub ( crate ) static HEAP_ALLOCATOR : ProfiledLockedHeap < 32 > =
125- ProfiledLockedHeap ( LockedHeap :: < 32 > :: empty ( ) ) ;
68+ static ALLOCATOR : LibcAlloc = LibcAlloc ;
12669
12770pub ( crate ) static mut GUEST_HANDLE : GuestHandle = GuestHandle :: new ( ) ;
12871pub ( crate ) static mut REGISTERED_GUEST_FUNCTIONS : GuestFunctionRegister =
@@ -154,6 +97,8 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
15497unsafe extern "C" {
15598 fn hyperlight_main ( ) ;
15699 fn srand ( seed : u32 ) ;
100+ fn init_sbrk ( donated_ptr : * const c_void , donated_size : usize ) ;
101+ fn init_arena ( donated_ptr : * const c_void , donated_size : usize ) ;
157102}
158103
159104static INIT : Once = Once :: new ( ) ;
@@ -188,16 +133,10 @@ pub extern "C" fn entrypoint(peb_address: u64, seed: u64, ops: u64, max_log_leve
188133 load_idt ( ) ;
189134 }
190135
191- let heap_start = ( * peb_ptr) . guest_heap . ptr as usize ;
136+ let heap_start = ( * peb_ptr) . guest_heap . ptr as * const c_void ;
192137 let heap_size = ( * peb_ptr) . guest_heap . size as usize ;
193- #[ cfg( not( feature = "mem_profile" ) ) ]
194- let heap_allocator = & HEAP_ALLOCATOR ;
195- #[ cfg( feature = "mem_profile" ) ]
196- let heap_allocator = & HEAP_ALLOCATOR . 0 ;
197- heap_allocator
198- . try_lock ( )
199- . expect ( "Failed to access HEAP_ALLOCATOR" )
200- . init ( heap_start, heap_size) ;
138+ init_arena ( heap_start, heap_size) ;
139+ init_sbrk ( heap_start, heap_size) ;
201140
202141 OS_PAGE_SIZE = ops as u32 ;
203142
0 commit comments