1- use crate :: PAGE_SIZE ;
1+ use crate :: { PAGE_SIZE , RUNNING_MODE } ;
22
33/// Hyperlight supports 2 primary modes:
44/// 1. Hypervisor mode
@@ -34,8 +34,12 @@ pub struct HyperlightPEB {
3434 /// Hyperlight, but with no hypervisor isolation. When we
3535 /// run in-process, we can't rely on the usual mechanism for
3636 /// host function calls (i.e., `outb`). Instead, we call a
37- /// function directly, which is represented by this pointer .
37+ /// function directly, which is represented by these pointers .
3838 pub outb_ptr : u64 ,
39+ pub outb_ptr_ctx : u64 ,
40+
41+ /// The host base address for the custom guest memory region.
42+ pub guest_memory_host_base_address : u64 ,
3943
4044 /// The base address for the guest memory region.
4145 pub guest_memory_base_address : u64 ,
@@ -52,43 +56,43 @@ pub struct HyperlightPEB {
5256
5357 /// Guest error data can be used to pass guest error information
5458 /// between host and the guest.
55- pub guest_error_data_ptr : u64 ,
59+ pub guest_error_data_offset : u64 ,
5660 pub guest_error_data_size : u64 ,
5761
5862 /// Host error data can be used to pass host error information
5963 /// between the host and the guest.
60- pub host_error_data_ptr : u64 ,
64+ pub host_error_data_offset : u64 ,
6165 pub host_error_data_size : u64 ,
6266
6367 /// The input data pointer is used to pass data from
6468 /// the host to the guest.
65- pub input_data_ptr : u64 ,
69+ pub input_data_offset : u64 ,
6670 pub input_data_size : u64 ,
6771
6872 /// The output data pointer is used to pass data from
6973 /// the guest to the host.
70- pub output_data_ptr : u64 ,
74+ pub output_data_offset : u64 ,
7175 pub output_data_size : u64 ,
7276
7377 /// The guest panic context pointer can be used to pass
7478 /// panic context data from the guest to the host.
75- pub guest_panic_context_ptr : u64 ,
79+ pub guest_panic_context_offset : u64 ,
7680 pub guest_panic_context_size : u64 ,
7781
7882 /// The guest heap data pointer points to a region of
7983 /// memory in the guest that is used for heap allocations.
80- pub guest_heap_data_ptr : u64 ,
84+ pub guest_heap_data_offset : u64 ,
8185 pub guest_heap_data_size : u64 ,
8286
8387 /// The guest stack data pointer points to a region of
8488 /// memory in the guest that is used for stack allocations.
85- pub guest_stack_data_ptr : u64 ,
89+ pub guest_stack_data_offset : u64 ,
8690 pub guest_stack_data_size : u64 ,
8791
8892 // Host function details may be used in the guest before
8993 // issuing a host function call to validate it before
9094 // ensuing a `VMEXIT`.
91- pub host_function_details_ptr : u64 ,
95+ pub host_function_details_offset : u64 ,
9296 pub host_function_details_size : u64 ,
9397}
9498
@@ -97,8 +101,8 @@ impl HyperlightPEB {
97101 // we set the guest stack at the start of the guest memory region to leverage
98102 // the stack guard page before it
99103 self . set_guest_stack_data_region (
100- self . guest_memory_base_address , // start at base of custom guest memory region,
101- None , // don't override the stack size
104+ 0x0 , // start at base of custom guest memory region,
105+ None , // don't override the stack size
102106 ) ;
103107
104108 let guest_stack_size = self . get_guest_stack_data_size ( ) ;
@@ -143,31 +147,31 @@ impl HyperlightPEB {
143147
144148 // Sets the guest error data region, where the guest can write errors to.
145149 pub fn set_guest_error_data_region ( & mut self , ptr : u64 , size : u64 ) {
146- self . guest_error_data_ptr = self . guest_memory_base_address + ptr;
150+ self . guest_error_data_offset = ptr;
147151 self . guest_error_data_size = size;
148152 }
149153
150154 // Sets the host error data region, where the host can write errors to.
151155 pub fn set_host_error_data_region ( & mut self , ptr : u64 , size : u64 ) {
152- self . host_error_data_ptr = self . guest_memory_base_address + ptr;
156+ self . host_error_data_offset = ptr;
153157 self . host_error_data_size = size;
154158 }
155159
156160 // Sets the input data region, where the host can write things like function calls to.
157161 pub fn set_input_data_region ( & mut self , ptr : u64 , size : u64 ) {
158- self . input_data_ptr = self . guest_memory_base_address + ptr;
162+ self . input_data_offset = ptr;
159163 self . input_data_size = size;
160164 }
161165
162166 // Sets the output data region, where the guest can write things like function return values to.
163167 pub fn set_output_data_region ( & mut self , ptr : u64 , size : u64 ) {
164- self . output_data_ptr = self . guest_memory_base_address + ptr;
168+ self . output_data_offset = ptr;
165169 self . output_data_size = size;
166170 }
167171
168172 // Sets the guest panic context region, where the guest can write panic context data to.
169173 pub fn set_guest_panic_context_region ( & mut self , ptr : u64 , size : u64 ) {
170- self . guest_panic_context_ptr = self . guest_memory_base_address + ptr;
174+ self . guest_panic_context_offset = ptr;
171175 self . guest_panic_context_size = size;
172176 }
173177
@@ -184,7 +188,7 @@ impl HyperlightPEB {
184188
185189 // Sets the guest heap data region.
186190 pub fn set_guest_heap_data_region ( & mut self , ptr : u64 , size_override : Option < u64 > ) {
187- self . guest_heap_data_ptr = self . guest_memory_base_address + ptr;
191+ self . guest_heap_data_offset = ptr;
188192 // the Hyperlight host always sets the heap data size to a default value, the
189193 // guest has the option to override it.
190194 if let Some ( size) = size_override {
@@ -211,7 +215,7 @@ impl HyperlightPEB {
211215
212216 // Sets the guest stack data region.
213217 pub fn set_guest_stack_data_region ( & mut self , ptr : u64 , size_override : Option < u64 > ) {
214- self . guest_stack_data_ptr = self . guest_memory_base_address + ptr;
218+ self . guest_stack_data_offset = ptr;
215219
216220 // the Hyperlight host always sets the stack data size to a default value, the
217221 // guest has the option to override it.
@@ -228,17 +232,148 @@ impl HyperlightPEB {
228232
229233 // Sets the host function details region, where the guest can write host function details to.
230234 pub fn set_host_function_details_region ( & mut self , ptr : u64 , size : u64 ) {
231- self . host_function_details_ptr = self . guest_memory_base_address + ptr;
235+ self . host_function_details_offset = ptr;
232236 self . host_function_details_size = size;
233237 }
234238
235- // Gets the input data region, where the host can write things like function calls to.
239+ // Gets the input data guest region, where the host can write things like function calls to.
240+ pub fn get_input_data_guest_region ( & self ) -> ( u64 , u64 ) {
241+ (
242+ self . input_data_offset + self . guest_memory_base_address ,
243+ self . input_data_size ,
244+ )
245+ }
246+
247+ // Gets input data host region.
248+ pub fn get_input_data_host_region ( & self ) -> ( u64 , u64 ) {
249+ (
250+ self . input_data_offset + self . guest_memory_host_base_address ,
251+ self . input_data_size ,
252+ )
253+ }
254+
255+ // Gets input data region.
236256 pub fn get_input_data_region ( & self ) -> ( u64 , u64 ) {
237- ( self . input_data_ptr , self . input_data_size )
257+ unsafe {
258+ match RUNNING_MODE {
259+ RunMode :: Hypervisor => self . get_input_data_guest_region ( ) ,
260+ RunMode :: InProcessWindows | RunMode :: InProcessLinux => {
261+ self . get_input_data_host_region ( )
262+ }
263+ _ => panic ! ( "Invalid running mode" ) ,
264+ }
265+ }
238266 }
239267
240268 // Gets the output data region, where the guest can write things like function return values to.
269+ pub fn get_output_data_guest_region ( & self ) -> ( u64 , u64 ) {
270+ (
271+ self . output_data_offset + self . guest_memory_base_address ,
272+ self . output_data_size ,
273+ )
274+ }
275+
276+ // Gets output data host region.
277+ pub fn get_output_data_host_region ( & self ) -> ( u64 , u64 ) {
278+ (
279+ self . output_data_offset + self . guest_memory_host_base_address ,
280+ self . output_data_size ,
281+ )
282+ }
283+
284+ // Gets output data region.
241285 pub fn get_output_data_region ( & self ) -> ( u64 , u64 ) {
242- ( self . output_data_ptr , self . output_data_size )
286+ unsafe {
287+ match RUNNING_MODE {
288+ RunMode :: Hypervisor => self . get_output_data_guest_region ( ) ,
289+ RunMode :: InProcessWindows | RunMode :: InProcessLinux => {
290+ self . get_output_data_host_region ( )
291+ }
292+ _ => panic ! ( "Invalid running mode" ) ,
293+ }
294+ }
295+ }
296+
297+ // Gets the guest heap data address.
298+ pub fn get_heap_data_address ( & self ) -> u64 {
299+ unsafe {
300+ match RUNNING_MODE {
301+ RunMode :: Hypervisor => self . guest_heap_data_offset + self . guest_memory_base_address ,
302+ RunMode :: InProcessWindows | RunMode :: InProcessLinux => {
303+ self . guest_heap_data_offset + self . guest_memory_host_base_address
304+ }
305+ _ => panic ! ( "Invalid running mode" ) ,
306+ }
307+ }
308+ }
309+
310+ // Gets the guest stack data address.
311+ pub fn get_stack_data_address ( & self ) -> u64 {
312+ unsafe {
313+ match RUNNING_MODE {
314+ RunMode :: Hypervisor => {
315+ self . guest_stack_data_offset + self . guest_memory_base_address
316+ }
317+ RunMode :: InProcessWindows | RunMode :: InProcessLinux => {
318+ self . guest_stack_data_offset + self . guest_memory_host_base_address
319+ }
320+ _ => panic ! ( "Invalid running mode" ) ,
321+ }
322+ }
323+ }
324+
325+ // Gets the guest error data address.
326+ pub fn get_guest_error_data_address ( & self ) -> u64 {
327+ unsafe {
328+ match RUNNING_MODE {
329+ RunMode :: Hypervisor => {
330+ self . guest_error_data_offset + self . guest_memory_base_address
331+ }
332+ RunMode :: InProcessWindows | RunMode :: InProcessLinux => {
333+ self . guest_error_data_offset + self . guest_memory_host_base_address
334+ }
335+ _ => panic ! ( "Invalid running mode" ) ,
336+ }
337+ }
338+ }
339+
340+ // Gets the guest panic context address.
341+ pub fn get_guest_panic_context_address ( & self ) -> u64 {
342+ unsafe {
343+ match RUNNING_MODE {
344+ RunMode :: Hypervisor => {
345+ self . guest_panic_context_offset + self . guest_memory_base_address
346+ }
347+ RunMode :: InProcessWindows | RunMode :: InProcessLinux => {
348+ self . guest_panic_context_offset + self . guest_memory_host_base_address
349+ }
350+ _ => panic ! ( "Invalid running mode" ) ,
351+ }
352+ }
353+ }
354+
355+ // Get host error guest offset.
356+ pub fn get_host_error_guest_offset ( & self ) -> u64 {
357+ self . host_error_data_offset + self . guest_memory_base_address
358+ }
359+
360+ // Get guest error guest offset.
361+ pub fn get_guest_error_guest_offset ( & self ) -> u64 {
362+ self . guest_error_data_offset + self . guest_memory_base_address
363+ }
364+
365+ // Get guest panic context offset.
366+ pub fn get_guest_panic_context_offset ( & self ) -> u64 {
367+ self . guest_panic_context_offset + self . guest_memory_base_address
368+ }
369+
370+ // Sets the outb pointer, which is used for in-process execution.
371+ pub fn set_outb_ptr ( & mut self , ptr : u64 ) {
372+ self . outb_ptr = ptr;
373+ }
374+
375+ // Sets the outb pointer context, which is used for in-process execution.
376+ pub fn set_outb_ptr_ctx ( & mut self , ptr : u64 ) {
377+ self . outb_ptr_ctx = ptr;
243378 }
244379}
0 commit comments