@@ -13,12 +13,11 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
16-
1716use core:: arch:: asm;
18- use core:: ffi:: { c_char, c_void , CStr } ;
17+ use core:: ffi:: { c_char, CStr } ;
1918use core:: ptr:: copy_nonoverlapping;
2019
21- use hyperlight_common:: mem :: { HyperlightPEB , RunMode } ;
20+ use hyperlight_common:: hyperlight_peb :: { HyperlightPEB , RunMode } ;
2221use log:: LevelFilter ;
2322use spin:: Once ;
2423
@@ -28,10 +27,7 @@ use crate::guest_function_call::dispatch_function;
2827use crate :: guest_logger:: init_logger;
2928use crate :: host_function_call:: { outb, OutBAction } ;
3029use crate :: idtr:: load_idt;
31- use crate :: {
32- __security_cookie, HEAP_ALLOCATOR , MIN_STACK_ADDRESS , OS_PAGE_SIZE , OUTB_PTR ,
33- OUTB_PTR_WITH_CONTEXT , P_PEB , RUNNING_MODE ,
34- } ;
30+ use crate :: { __security_cookie, HEAP_ALLOCATOR , MIN_STACK_ADDRESS , PEB , RUNNING_MODE } ;
3531
3632#[ inline( never) ]
3733pub fn halt ( ) {
@@ -57,10 +53,9 @@ pub fn abort_with_code(code: i32) -> ! {
5753/// # Safety
5854/// This function is unsafe because it dereferences a raw pointer.
5955pub unsafe fn abort_with_code_and_message ( code : i32 , message_ptr : * const c_char ) -> ! {
60- let peb_ptr = P_PEB . unwrap ( ) ;
6156 copy_nonoverlapping (
6257 message_ptr,
63- ( * peb_ptr ) . guestPanicContextData . guestPanicContextDataBuffer as * mut c_char ,
58+ ( * PEB ) . guest_panic_context_ptr as * mut c_char ,
6459 CStr :: from_ptr ( message_ptr) . count_bytes ( ) + 1 , // +1 for null terminator
6560 ) ;
6661 outb ( OutBAction :: Abort as u16 , code as u8 ) ;
@@ -77,79 +72,64 @@ static INIT: Once = Once::new();
7772// Note: entrypoint cannot currently have a stackframe >4KB, as that will invoke __chkstk on msvc
7873// target without first having setup global `RUNNING_MODE` variable, which __chkstk relies on.
7974#[ no_mangle]
80- pub extern "win64" fn entrypoint ( peb_address : u64 , seed : u64 , ops : u64 , max_log_level : u64 ) {
81- if peb_address == 0 {
82- panic ! ( "PEB address is null" ) ;
83- }
84-
85- INIT . call_once ( || {
86- unsafe {
87- P_PEB = Some ( peb_address as * mut HyperlightPEB ) ;
88- let peb_ptr = P_PEB . unwrap ( ) ;
89- __security_cookie = peb_address ^ seed;
90-
91- let srand_seed = ( ( peb_address << 8 ^ seed >> 4 ) >> 32 ) as u32 ;
92-
93- // Set the seed for the random number generator for C code using rand;
94- srand ( srand_seed) ;
95-
96- // set up the logger
97- let max_log_level = LevelFilter :: iter ( )
98- . nth ( max_log_level as usize )
99- . expect ( "Invalid log level" ) ;
100- init_logger ( max_log_level) ;
101-
102- match ( * peb_ptr) . runMode {
103- RunMode :: Hypervisor => {
104- RUNNING_MODE = RunMode :: Hypervisor ;
105- // This static is to make it easier to implement the __chkstk function in assembly.
106- // It also means that should we change the layout of the struct in the future, we
107- // don't have to change the assembly code.
108- MIN_STACK_ADDRESS = ( * peb_ptr) . gueststackData . minUserStackAddress ;
109-
110- // Setup GDT and IDT
111- load_gdt ( ) ;
112- load_idt ( ) ;
113- }
114- RunMode :: InProcessLinux | RunMode :: InProcessWindows => {
115- RUNNING_MODE = ( * peb_ptr) . runMode ;
116-
117- OUTB_PTR = {
118- let outb_ptr: extern "win64" fn ( u16 , u8 ) =
119- core:: mem:: transmute ( ( * peb_ptr) . pOutb ) ;
120- Some ( outb_ptr)
121- } ;
122-
123- if ( * peb_ptr) . pOutbContext . is_null ( ) {
124- panic ! ( "OutbContext is null" ) ;
125- }
126-
127- OUTB_PTR_WITH_CONTEXT = {
128- let outb_ptr_with_context: extern "win64" fn ( * mut c_void , u16 , u8 ) =
129- core:: mem:: transmute ( ( * peb_ptr) . pOutb ) ;
130- Some ( outb_ptr_with_context)
131- } ;
132- }
133- _ => {
134- panic ! ( "Invalid runmode in PEB" ) ;
135- }
136- }
137-
138- let heap_start = ( * peb_ptr) . guestheapData . guestHeapBuffer as usize ;
139- let heap_size = ( * peb_ptr) . guestheapData . guestHeapSize as usize ;
140- HEAP_ALLOCATOR
141- . try_lock ( )
142- . expect ( "Failed to access HEAP_ALLOCATOR" )
143- . init ( heap_start, heap_size) ;
144-
145- OS_PAGE_SIZE = ops as u32 ;
146-
147- ( * peb_ptr) . guest_function_dispatch_ptr = dispatch_function as usize as u64 ;
148-
149- reset_error ( ) ;
150-
151- hyperlight_main ( ) ;
75+ pub extern "win64" fn entrypoint (
76+ peb_address : u64 ,
77+ // TODO(danbugs:297): remove the extra arg
78+ _: u64 ,
79+ seed : u64 ,
80+ max_log_level : u64 ,
81+ ) {
82+ INIT . call_once ( || unsafe {
83+ PEB = peb_address as * mut HyperlightPEB ;
84+ RUNNING_MODE = ( * PEB ) . clone ( ) . run_mode ;
85+
86+ // The guest receives an undifferentiated block of memory that it can address as it sees fit.
87+ // This 'addressing' is done by writing to the PEB the guest's memory layout via this function,
88+ // or by directly altering the PEB. `set_default_memory_layout` will configure the PEB to
89+ // with a memory layout that is compatible with the expectations of guests that use the
90+ // `hyperlight_guest` library (e.g., simpleguest, and callbackguest).
91+ ( * PEB ) . set_default_memory_layout ( ) ;
92+
93+ // The guest sets the address to a "guest function dispatch" function, which is a function
94+ // that is called by the host to dispatch calls to guest functions.
95+ ( * PEB ) . guest_function_dispatch_ptr = dispatch_function as usize as u64 ;
96+
97+ // Set up the guest heap
98+ HEAP_ALLOCATOR
99+ . try_lock ( )
100+ . expect ( "Failed to access HEAP_ALLOCATOR" )
101+ . init (
102+ ( * PEB ) . guest_heap_data_ptr as usize ,
103+ ( * PEB ) . guest_heap_data_size as usize ,
104+ ) ;
105+
106+ __security_cookie = seed;
107+
108+ // Set the seed for the random number generator for C code using rand;
109+ let srand_seed = ( ( peb_address << 8 ^ seed >> 4 ) >> 32 ) as u32 ;
110+ srand ( srand_seed) ;
111+
112+ // Set up the logger
113+ let max_log_level = LevelFilter :: iter ( )
114+ . nth ( max_log_level as usize )
115+ . expect ( "Invalid log level" ) ;
116+ init_logger ( max_log_level) ;
117+
118+ if ( * PEB ) . run_mode == RunMode :: Hypervisor {
119+ // This static is to make it easier to implement the __chkstk function in assembly.
120+ // It also means that, should we change the layout of the struct in the future, we
121+ // don't have to change the assembly code. Plus, while this could be accessible via
122+ // the PEB, we don't want to expose it entirely to user code.
123+ MIN_STACK_ADDRESS = ( * PEB ) . guest_stack_data_ptr ;
124+
125+ // Setup GDT and IDT
126+ load_gdt ( ) ;
127+ load_idt ( ) ;
152128 }
129+
130+ reset_error ( ) ;
131+
132+ hyperlight_main ( ) ;
153133 } ) ;
154134
155135 halt ( ) ;
0 commit comments