@@ -23,14 +23,16 @@ use std::{
23
23
ptr:: { null_mut, NonNull } ,
24
24
slice,
25
25
} ;
26
- pub use xenctrl_sys:: { hvm_hw_cpu, hvm_save_descriptor, __HVM_SAVE_TYPE_CPU} ;
27
- use xenctrl_sys:: { xc_error_code, xc_interface, xenmem_access_t, xentoollog_logger} ;
26
+ pub use xenctrl_sys:: { hvm_hw_cpu, hvm_save_descriptor, xentoollog_logger, __HVM_SAVE_TYPE_CPU} ;
27
+ use xenctrl_sys:: { xc_error_code, xc_interface, xenmem_access_t} ;
28
+ pub use xenvmevent_sys:: {
29
+ vm_event_back_ring, vm_event_request_t, vm_event_response_t, vm_event_sring,
30
+ } ;
28
31
use xenvmevent_sys:: {
29
- vm_event_back_ring, vm_event_request_t, vm_event_response_t, vm_event_sring, MEM_ACCESS_R ,
30
- MEM_ACCESS_RW , MEM_ACCESS_RWX , MEM_ACCESS_RX , MEM_ACCESS_W , MEM_ACCESS_WX , MEM_ACCESS_X ,
31
- VM_EVENT_REASON_MEM_ACCESS , VM_EVENT_REASON_MOV_TO_MSR , VM_EVENT_REASON_SINGLESTEP ,
32
- VM_EVENT_REASON_SOFTWARE_BREAKPOINT , VM_EVENT_REASON_WRITE_CTRLREG , VM_EVENT_X86_CR0 ,
33
- VM_EVENT_X86_CR3 , VM_EVENT_X86_CR4 ,
32
+ MEM_ACCESS_R , MEM_ACCESS_RW , MEM_ACCESS_RWX , MEM_ACCESS_RX , MEM_ACCESS_W , MEM_ACCESS_WX ,
33
+ MEM_ACCESS_X , VM_EVENT_REASON_MEM_ACCESS , VM_EVENT_REASON_MOV_TO_MSR ,
34
+ VM_EVENT_REASON_SINGLESTEP , VM_EVENT_REASON_SOFTWARE_BREAKPOINT , VM_EVENT_REASON_WRITE_CTRLREG ,
35
+ VM_EVENT_X86_CR0 , VM_EVENT_X86_CR3 , VM_EVENT_X86_CR4 ,
34
36
} ;
35
37
36
38
#[ derive( Copy , Clone , Debug ) ]
@@ -134,38 +136,96 @@ pub struct XenControl {
134
136
libxenctrl : LibXenCtrl ,
135
137
}
136
138
137
- impl XenControl {
138
- pub fn new (
139
+ pub trait XenIntrospectable : std:: fmt:: Debug {
140
+ fn init (
141
+ & mut self ,
139
142
logger : Option < & mut xentoollog_logger > ,
140
143
dombuild_logger : Option < & mut xentoollog_logger > ,
141
144
open_flags : u32 ,
142
- ) -> Result < Self , XcError > {
143
- let libxenctrl = unsafe { LibXenCtrl :: new ( ) } ;
145
+ ) -> Result < ( ) , XcError > ;
146
+ fn domain_hvm_getcontext_partial ( & self , domid : u32 , vcpu : u16 ) -> Result < hvm_hw_cpu , XcError > ;
147
+ fn domain_hvm_setcontext (
148
+ & self ,
149
+ domid : u32 ,
150
+ buffer : * mut c_uint ,
151
+ size : usize ,
152
+ ) -> Result < ( ) , XcError > ;
153
+ fn domain_hvm_getcontext (
154
+ & self ,
155
+ domid : u32 ,
156
+ vcpu : u16 ,
157
+ ) -> Result < ( * mut c_uint , hvm_hw_cpu , u32 ) , XcError > ;
158
+ fn monitor_enable (
159
+ & mut self ,
160
+ domid : u32 ,
161
+ ) -> Result < ( vm_event_sring , vm_event_back_ring , u32 ) , XcError > ;
162
+ fn get_request (
163
+ & self ,
164
+ back_ring : & mut vm_event_back_ring ,
165
+ ) -> Result < vm_event_request_t , XcError > ;
166
+ fn put_response (
167
+ & self ,
168
+ rsp : & mut vm_event_response_t ,
169
+ back_ring : & mut vm_event_back_ring ,
170
+ ) -> Result < ( ) , XcError > ;
171
+ fn get_event_type ( & self , req : vm_event_request_t ) -> Result < XenEventType , XcError > ;
172
+ fn monitor_disable ( & self , domid : u32 ) -> Result < ( ) , XcError > ;
173
+ fn domain_pause ( & self , domid : u32 ) -> Result < ( ) , XcError > ;
174
+ fn domain_unpause ( & self , domid : u32 ) -> Result < ( ) , XcError > ;
175
+ fn monitor_software_breakpoint ( & self , domid : u32 , enable : bool ) -> Result < ( ) , XcError > ;
176
+ fn monitor_singlestep ( & self , domid : u32 , enable : bool ) -> Result < ( ) , XcError > ;
177
+ fn monitor_mov_to_msr ( & self , domid : u32 , msr : u32 , enable : bool ) -> Result < ( ) , XcError > ;
178
+ fn monitor_write_ctrlreg (
179
+ & self ,
180
+ domid : u32 ,
181
+ index : XenCr ,
182
+ enable : bool ,
183
+ sync : bool ,
184
+ onchangeonly : bool ,
185
+ ) -> Result < ( ) , XcError > ;
186
+ fn set_mem_access (
187
+ & self ,
188
+ domid : u32 ,
189
+ access : XenPageAccess ,
190
+ first_pfn : u64 ,
191
+ ) -> Result < ( ) , XcError > ;
192
+ fn get_mem_access ( & self , domid : u32 , pfn : u64 ) -> Result < XenPageAccess , XcError > ;
193
+ fn domain_maximum_gpfn ( & self , domid : u32 ) -> Result < u64 , XcError > ;
194
+ fn close ( & mut self ) -> Result < ( ) , XcError > ;
195
+ }
196
+
197
+ pub fn create_xen_control ( ) -> XenControl {
198
+ XenControl :: new ( unsafe { LibXenCtrl :: new ( ) } )
199
+ }
200
+
201
+ impl XenControl {
202
+ fn new ( libxenctrl : LibXenCtrl ) -> XenControl {
203
+ XenControl {
204
+ handle : NonNull :: dangling ( ) ,
205
+ libxenctrl,
206
+ }
207
+ }
208
+ }
144
209
210
+ impl XenIntrospectable for XenControl {
211
+ fn init (
212
+ & mut self ,
213
+ logger : Option < & mut xentoollog_logger > ,
214
+ dombuild_logger : Option < & mut xentoollog_logger > ,
215
+ open_flags : u32 ,
216
+ ) -> Result < ( ) , XcError > {
145
217
#[ allow( clippy:: redundant_closure) ]
146
- let xc_handle = ( libxenctrl. interface_open ) (
218
+ let xc_handle = ( self . libxenctrl . interface_open ) (
147
219
logger. map_or_else ( || null_mut ( ) , |l| l as * mut _ ) ,
148
220
dombuild_logger. map_or_else ( || null_mut ( ) , |l| l as * mut _ ) ,
149
221
open_flags,
150
222
) ;
151
223
152
- NonNull :: new ( xc_handle)
153
- . ok_or_else ( || {
154
- let desc = ( libxenctrl. error_code_to_desc ) ( xc_error_code:: XC_INTERNAL_ERROR as _ ) ;
155
- XcError :: new ( unsafe { ffi:: CStr :: from_ptr ( desc) } . to_str ( ) . unwrap ( ) )
156
- } )
157
- . map ( |handle| XenControl { handle, libxenctrl } )
158
- }
159
-
160
- pub fn default ( ) -> Result < Self , XcError > {
161
- Self :: new ( None , None , 0 )
224
+ self . handle = NonNull :: new ( xc_handle) . unwrap ( ) ;
225
+ last_error ! ( self , ( ) )
162
226
}
163
227
164
- pub fn domain_hvm_getcontext_partial (
165
- & self ,
166
- domid : u32 ,
167
- vcpu : u16 ,
168
- ) -> Result < hvm_hw_cpu , XcError > {
228
+ fn domain_hvm_getcontext_partial ( & self , domid : u32 , vcpu : u16 ) -> Result < hvm_hw_cpu , XcError > {
169
229
let xc = self . handle . as_ptr ( ) ;
170
230
let mut hvm_cpu: hvm_hw_cpu =
171
231
unsafe { mem:: MaybeUninit :: < hvm_hw_cpu > :: zeroed ( ) . assume_init ( ) } ;
@@ -188,7 +248,7 @@ impl XenControl {
188
248
last_error ! ( self , hvm_cpu)
189
249
}
190
250
191
- pub fn domain_hvm_setcontext (
251
+ fn domain_hvm_setcontext (
192
252
& self ,
193
253
domid : u32 ,
194
254
buffer : * mut c_uint ,
@@ -200,7 +260,7 @@ impl XenControl {
200
260
last_error ! ( self , ( ) )
201
261
}
202
262
203
- pub fn domain_hvm_getcontext (
263
+ fn domain_hvm_getcontext (
204
264
& self ,
205
265
domid : u32 ,
206
266
vcpu : u16 ,
@@ -238,7 +298,7 @@ impl XenControl {
238
298
last_error ! ( self , ( buffer, * cpu_ptr, size. try_into( ) . unwrap( ) ) )
239
299
}
240
300
241
- pub fn monitor_enable (
301
+ fn monitor_enable (
242
302
& mut self ,
243
303
domid : u32 ,
244
304
) -> Result < ( vm_event_sring , vm_event_back_ring , u32 ) , XcError > {
@@ -266,7 +326,7 @@ impl XenControl {
266
326
last_error ! ( self , ( * ring_page, back_ring, remote_port) )
267
327
}
268
328
269
- pub fn get_request (
329
+ fn get_request (
270
330
& self ,
271
331
back_ring : & mut vm_event_back_ring ,
272
332
) -> Result < vm_event_request_t , XcError > {
@@ -280,7 +340,7 @@ impl XenControl {
280
340
last_error ! ( self , req_from_ring)
281
341
}
282
342
283
- pub fn put_response (
343
+ fn put_response (
284
344
& self ,
285
345
rsp : & mut vm_event_response_t ,
286
346
back_ring : & mut vm_event_back_ring ,
@@ -294,7 +354,7 @@ impl XenControl {
294
354
last_error ! ( self , ( ) )
295
355
}
296
356
297
- pub fn get_event_type ( & self , req : vm_event_request_t ) -> Result < XenEventType , XcError > {
357
+ fn get_event_type ( & self , req : vm_event_request_t ) -> Result < XenEventType , XcError > {
298
358
let ev_type: XenEventType ;
299
359
unsafe {
300
360
ev_type = match req. reason {
@@ -338,28 +398,28 @@ impl XenControl {
338
398
last_error ! ( self , ev_type)
339
399
}
340
400
341
- pub fn monitor_disable ( & self , domid : u32 ) -> Result < ( ) , XcError > {
401
+ fn monitor_disable ( & self , domid : u32 ) -> Result < ( ) , XcError > {
342
402
let xc = self . handle . as_ptr ( ) ;
343
403
( self . libxenctrl . clear_last_error ) ( xc) ;
344
404
( self . libxenctrl . monitor_disable ) ( xc, domid. try_into ( ) . unwrap ( ) ) ;
345
405
last_error ! ( self , ( ) )
346
406
}
347
407
348
- pub fn domain_pause ( & self , domid : u32 ) -> Result < ( ) , XcError > {
408
+ fn domain_pause ( & self , domid : u32 ) -> Result < ( ) , XcError > {
349
409
let xc = self . handle . as_ptr ( ) ;
350
410
( self . libxenctrl . clear_last_error ) ( xc) ;
351
411
( self . libxenctrl . domain_pause ) ( xc, domid) ;
352
412
last_error ! ( self , ( ) )
353
413
}
354
414
355
- pub fn domain_unpause ( & self , domid : u32 ) -> Result < ( ) , XcError > {
415
+ fn domain_unpause ( & self , domid : u32 ) -> Result < ( ) , XcError > {
356
416
let xc = self . handle . as_ptr ( ) ;
357
417
( self . libxenctrl . clear_last_error ) ( xc) ;
358
418
( self . libxenctrl . domain_unpause ) ( xc, domid) ;
359
419
last_error ! ( self , ( ) )
360
420
}
361
421
362
- pub fn monitor_software_breakpoint ( & self , domid : u32 , enable : bool ) -> Result < ( ) , XcError > {
422
+ fn monitor_software_breakpoint ( & self , domid : u32 , enable : bool ) -> Result < ( ) , XcError > {
363
423
let xc = self . handle . as_ptr ( ) ;
364
424
( self . libxenctrl . clear_last_error ) ( xc) ;
365
425
let rc = ( self . libxenctrl . monitor_software_breakpoint ) ( xc, domid, enable) ;
@@ -369,7 +429,7 @@ impl XenControl {
369
429
last_error ! ( self , ( ) )
370
430
}
371
431
372
- pub fn monitor_singlestep ( & self , domid : u32 , enable : bool ) -> Result < ( ) , XcError > {
432
+ fn monitor_singlestep ( & self , domid : u32 , enable : bool ) -> Result < ( ) , XcError > {
373
433
let xc = self . handle . as_ptr ( ) ;
374
434
( self . libxenctrl . clear_last_error ) ( xc) ;
375
435
let rc = ( self . libxenctrl . monitor_singlestep ) ( xc, domid, enable) ;
@@ -379,7 +439,7 @@ impl XenControl {
379
439
last_error ! ( self , ( ) )
380
440
}
381
441
382
- pub fn monitor_mov_to_msr ( & self , domid : u32 , msr : u32 , enable : bool ) -> Result < ( ) , XcError > {
442
+ fn monitor_mov_to_msr ( & self , domid : u32 , msr : u32 , enable : bool ) -> Result < ( ) , XcError > {
383
443
let xc = self . handle . as_ptr ( ) ;
384
444
( self . libxenctrl . clear_last_error ) ( xc) ;
385
445
let rc = ( self . libxenctrl . monitor_mov_to_msr ) ( xc, domid. try_into ( ) . unwrap ( ) , msr, enable) ;
@@ -389,7 +449,7 @@ impl XenControl {
389
449
last_error ! ( self , ( ) )
390
450
}
391
451
392
- pub fn monitor_write_ctrlreg (
452
+ fn monitor_write_ctrlreg (
393
453
& self ,
394
454
domid : u32 ,
395
455
index : XenCr ,
@@ -413,7 +473,7 @@ impl XenControl {
413
473
last_error ! ( self , ( ) )
414
474
}
415
475
416
- pub fn set_mem_access (
476
+ fn set_mem_access (
417
477
& self ,
418
478
domid : u32 ,
419
479
access : XenPageAccess ,
@@ -431,15 +491,15 @@ impl XenControl {
431
491
last_error ! ( self , ( ) )
432
492
}
433
493
434
- pub fn get_mem_access ( & self , domid : u32 , pfn : u64 ) -> Result < XenPageAccess , XcError > {
494
+ fn get_mem_access ( & self , domid : u32 , pfn : u64 ) -> Result < XenPageAccess , XcError > {
435
495
let xc = self . handle . as_ptr ( ) ;
436
496
let mut access: xenmem_access_t = xenmem_access_t:: XENMEM_access_n ;
437
497
( self . libxenctrl . clear_last_error ) ( xc) ;
438
498
( self . libxenctrl . get_mem_access ) ( xc, domid. try_into ( ) . unwrap ( ) , pfn, & mut access) ;
439
499
last_error ! ( self , access. try_into( ) . unwrap( ) )
440
500
}
441
501
442
- pub fn domain_maximum_gpfn ( & self , domid : u32 ) -> Result < u64 , XcError > {
502
+ fn domain_maximum_gpfn ( & self , domid : u32 ) -> Result < u64 , XcError > {
443
503
let xc = self . handle . as_ptr ( ) ;
444
504
#[ allow( unused_assignments) ]
445
505
let mut max_gpfn = mem:: MaybeUninit :: < u64 > :: uninit ( ) ;
0 commit comments