1
- use crate :: PAGE_SIZE ;
1
+ use crate :: { PAGE_SIZE , RUNNING_MODE } ;
2
2
3
3
/// Hyperlight supports 2 primary modes:
4
4
/// 1. Hypervisor mode
@@ -34,8 +34,12 @@ pub struct HyperlightPEB {
34
34
/// Hyperlight, but with no hypervisor isolation. When we
35
35
/// run in-process, we can't rely on the usual mechanism for
36
36
/// 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 .
38
38
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 ,
39
43
40
44
/// The base address for the guest memory region.
41
45
pub guest_memory_base_address : u64 ,
@@ -52,43 +56,43 @@ pub struct HyperlightPEB {
52
56
53
57
/// Guest error data can be used to pass guest error information
54
58
/// between host and the guest.
55
- pub guest_error_data_ptr : u64 ,
59
+ pub guest_error_data_offset : u64 ,
56
60
pub guest_error_data_size : u64 ,
57
61
58
62
/// Host error data can be used to pass host error information
59
63
/// between the host and the guest.
60
- pub host_error_data_ptr : u64 ,
64
+ pub host_error_data_offset : u64 ,
61
65
pub host_error_data_size : u64 ,
62
66
63
67
/// The input data pointer is used to pass data from
64
68
/// the host to the guest.
65
- pub input_data_ptr : u64 ,
69
+ pub input_data_offset : u64 ,
66
70
pub input_data_size : u64 ,
67
71
68
72
/// The output data pointer is used to pass data from
69
73
/// the guest to the host.
70
- pub output_data_ptr : u64 ,
74
+ pub output_data_offset : u64 ,
71
75
pub output_data_size : u64 ,
72
76
73
77
/// The guest panic context pointer can be used to pass
74
78
/// panic context data from the guest to the host.
75
- pub guest_panic_context_ptr : u64 ,
79
+ pub guest_panic_context_offset : u64 ,
76
80
pub guest_panic_context_size : u64 ,
77
81
78
82
/// The guest heap data pointer points to a region of
79
83
/// memory in the guest that is used for heap allocations.
80
- pub guest_heap_data_ptr : u64 ,
84
+ pub guest_heap_data_offset : u64 ,
81
85
pub guest_heap_data_size : u64 ,
82
86
83
87
/// The guest stack data pointer points to a region of
84
88
/// memory in the guest that is used for stack allocations.
85
- pub guest_stack_data_ptr : u64 ,
89
+ pub guest_stack_data_offset : u64 ,
86
90
pub guest_stack_data_size : u64 ,
87
91
88
92
// Host function details may be used in the guest before
89
93
// issuing a host function call to validate it before
90
94
// ensuing a `VMEXIT`.
91
- pub host_function_details_ptr : u64 ,
95
+ pub host_function_details_offset : u64 ,
92
96
pub host_function_details_size : u64 ,
93
97
}
94
98
@@ -97,8 +101,8 @@ impl HyperlightPEB {
97
101
// we set the guest stack at the start of the guest memory region to leverage
98
102
// the stack guard page before it
99
103
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
102
106
) ;
103
107
104
108
let guest_stack_size = self . get_guest_stack_data_size ( ) ;
@@ -143,31 +147,31 @@ impl HyperlightPEB {
143
147
144
148
// Sets the guest error data region, where the guest can write errors to.
145
149
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;
147
151
self . guest_error_data_size = size;
148
152
}
149
153
150
154
// Sets the host error data region, where the host can write errors to.
151
155
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;
153
157
self . host_error_data_size = size;
154
158
}
155
159
156
160
// Sets the input data region, where the host can write things like function calls to.
157
161
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;
159
163
self . input_data_size = size;
160
164
}
161
165
162
166
// Sets the output data region, where the guest can write things like function return values to.
163
167
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;
165
169
self . output_data_size = size;
166
170
}
167
171
168
172
// Sets the guest panic context region, where the guest can write panic context data to.
169
173
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;
171
175
self . guest_panic_context_size = size;
172
176
}
173
177
@@ -184,7 +188,7 @@ impl HyperlightPEB {
184
188
185
189
// Sets the guest heap data region.
186
190
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;
188
192
// the Hyperlight host always sets the heap data size to a default value, the
189
193
// guest has the option to override it.
190
194
if let Some ( size) = size_override {
@@ -211,7 +215,7 @@ impl HyperlightPEB {
211
215
212
216
// Sets the guest stack data region.
213
217
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;
215
219
216
220
// the Hyperlight host always sets the stack data size to a default value, the
217
221
// guest has the option to override it.
@@ -228,17 +232,148 @@ impl HyperlightPEB {
228
232
229
233
// Sets the host function details region, where the guest can write host function details to.
230
234
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;
232
236
self . host_function_details_size = size;
233
237
}
234
238
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.
236
256
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
+ }
238
266
}
239
267
240
268
// 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.
241
285
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;
243
378
}
244
379
}
0 commit comments