@@ -18,9 +18,9 @@ limitations under the License.
18
18
// === Dependencies ===
19
19
extern crate alloc;
20
20
21
+ use core:: ffi:: c_void;
21
22
use alloc:: string:: ToString ;
22
23
23
- use buddy_system_allocator:: LockedHeap ;
24
24
#[ cfg( target_arch = "x86_64" ) ]
25
25
use exceptions:: { gdt:: load_gdt, idtr:: load_idt} ;
26
26
use guest_function:: call:: dispatch_function;
@@ -34,6 +34,7 @@ use hyperlight_guest::exit::{abort_with_code_and_message, halt};
34
34
use hyperlight_guest:: guest_handle:: handle:: GuestHandle ;
35
35
use hyperlight_guest_tracing:: { trace, trace_function} ;
36
36
use log:: LevelFilter ;
37
+ use libc_alloc:: LibcAlloc ;
37
38
use spin:: Once ;
38
39
39
40
// === Modules ===
@@ -54,75 +55,17 @@ pub mod guest_function {
54
55
55
56
pub mod guest_logger;
56
57
pub mod host_comm;
57
- pub mod memory;
58
58
pub mod paging;
59
59
60
60
#[ cfg( feature = "libc" ) ]
61
61
mod host_bridge;
62
62
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
+
117
65
118
66
// === 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" ) ]
123
67
#[ global_allocator]
124
- pub ( crate ) static HEAP_ALLOCATOR : ProfiledLockedHeap < 32 > =
125
- ProfiledLockedHeap ( LockedHeap :: < 32 > :: empty ( ) ) ;
68
+ static ALLOCATOR : LibcAlloc = LibcAlloc ;
126
69
127
70
pub ( crate ) static mut GUEST_HANDLE : GuestHandle = GuestHandle :: new ( ) ;
128
71
pub ( crate ) static mut REGISTERED_GUEST_FUNCTIONS : GuestFunctionRegister =
@@ -156,6 +99,8 @@ unsafe extern "C" {
156
99
157
100
#[ cfg( feature = "libc" ) ]
158
101
fn srand ( seed : u32 ) ;
102
+ fn init_sbrk ( donated_ptr : * const c_void , donated_size : usize ) ;
103
+ fn init_arena ( donated_ptr : * const c_void , donated_size : usize ) ;
159
104
}
160
105
161
106
static INIT : Once = Once :: new ( ) ;
@@ -192,16 +137,10 @@ pub extern "C" fn entrypoint(peb_address: u64, _seed: u64, ops: u64, max_log_lev
192
137
load_idt ( ) ;
193
138
}
194
139
195
- let heap_start = ( * peb_ptr) . guest_heap . ptr as usize ;
140
+ let heap_start = ( * peb_ptr) . guest_heap . ptr as * const c_void ;
196
141
let heap_size = ( * peb_ptr) . guest_heap . size as usize ;
197
- #[ cfg( not( feature = "mem_profile" ) ) ]
198
- let heap_allocator = & HEAP_ALLOCATOR ;
199
- #[ cfg( feature = "mem_profile" ) ]
200
- let heap_allocator = & HEAP_ALLOCATOR . 0 ;
201
- heap_allocator
202
- . try_lock ( )
203
- . expect ( "Failed to access HEAP_ALLOCATOR" )
204
- . init ( heap_start, heap_size) ;
142
+ init_arena ( heap_start, heap_size) ;
143
+ init_sbrk ( heap_start, heap_size) ;
205
144
206
145
OS_PAGE_SIZE = ops as u32 ;
207
146
0 commit comments