@@ -3,12 +3,12 @@ use crate::vmcs;
33use alloc:: boxed:: Box ;
44use alloc:: vec:: Vec ;
55use bitflags:: bitflags;
6- use core:: borrow:: { Borrow , BorrowMut } ;
76use core:: convert:: TryFrom ;
87use core:: default:: Default ;
98use core:: fmt;
109use core:: ops:: { Add , Deref , Index , IndexMut } ;
1110use num_enum:: TryFromPrimitive ;
11+ use spin:: RwLock ;
1212use ux;
1313use x86:: bits64:: paging:: * ;
1414use x86:: controlregs:: Cr0 ;
@@ -255,7 +255,7 @@ impl HostPhysFrame {
255255}
256256
257257pub struct GuestAddressSpace {
258- root : Box < EptPml4Table > ,
258+ root : RwLock < Box < EptPml4Table > > ,
259259}
260260
261261#[ derive( Copy , Clone , Debug ) ]
@@ -271,21 +271,26 @@ pub enum GuestAccess {
271271impl GuestAddressSpace {
272272 pub fn new ( ) -> Result < Self > {
273273 Ok ( GuestAddressSpace {
274- root : Box :: new ( EptPml4Table :: default ( ) ) ,
274+ root : RwLock :: new ( Box :: new ( EptPml4Table :: default ( ) ) ) ,
275275 } )
276276 }
277277
278278 pub fn map_frame (
279- & mut self ,
279+ & self ,
280280 guest_addr : GuestPhysAddr ,
281281 host_frame : HostPhysFrame ,
282282 readonly : bool ,
283283 ) -> Result < ( ) > {
284- map_guest_memory ( & mut self . root , guest_addr, host_frame, readonly)
284+ map_guest_memory (
285+ & mut self . root . write ( ) ,
286+ guest_addr,
287+ host_frame,
288+ readonly,
289+ )
285290 }
286291
287292 pub fn map_new_frame (
288- & mut self ,
293+ & self ,
289294 guest_addr : GuestPhysAddr ,
290295 readonly : bool ,
291296 ) -> Result < ( ) > {
@@ -297,7 +302,7 @@ impl GuestAddressSpace {
297302
298303 pub fn eptp ( & self ) -> u64 {
299304 // //TODO: check available memory types
300- ( & * self . root as * const _ as u64 ) | ( 4 - 1 ) << 3 | 6
305+ ( & * ( * self . root . read ( ) ) as * const _ as u64 ) | ( 4 - 1 ) << 3 | 6
301306 }
302307
303308 pub fn translate_linear_address (
@@ -360,7 +365,7 @@ impl GuestAddressSpace {
360365 & self ,
361366 addr : GuestPhysAddr ,
362367 ) -> Result < HostPhysFrame > {
363- let ept_pml4e = & self . root [ addr. p4_index ( ) ] ;
368+ let ept_pml4e = & self . root . read ( ) [ addr. p4_index ( ) ] ;
364369 if ept_pml4e. is_unused ( ) {
365370 return Err ( Error :: InvalidValue (
366371 "No PML4 entry for GuestPhysAddr" . into ( ) ,
@@ -440,7 +445,7 @@ impl GuestAddressSpace {
440445 }
441446
442447 pub fn write_bytes (
443- & mut self ,
448+ & self ,
444449 cr3 : GuestPhysAddr ,
445450 addr : GuestVirtAddr ,
446451 mut bytes : & [ u8 ] ,
@@ -471,25 +476,20 @@ impl GuestAddressSpace {
471476 }
472477}
473478
474- pub type GuestAddressSpaceView < ' a > =
475- GuestAddressSpaceWrapper < & ' a GuestAddressSpace > ;
476- pub type GuestAddressSpaceViewMut < ' a > =
477- GuestAddressSpaceWrapper < & ' a mut GuestAddressSpace > ;
478-
479- pub struct GuestAddressSpaceWrapper < T > {
480- space : T ,
479+ pub struct GuestAddressSpaceView < ' a > {
480+ space : & ' a GuestAddressSpace ,
481481 cr3 : GuestPhysAddr ,
482482}
483483
484- impl < T > GuestAddressSpaceWrapper < T >
485- where
486- T : Borrow < GuestAddressSpace > ,
487- {
488- pub fn new ( cr3 : GuestPhysAddr , space : T ) -> Self {
484+ impl < ' a > GuestAddressSpaceView < ' a > {
485+ pub fn new ( cr3 : GuestPhysAddr , space : & ' a GuestAddressSpace ) -> Self {
489486 Self { space, cr3 }
490487 }
491488
492- pub fn from_vmcs ( vmcs : & vmcs:: ActiveVmcs , space : T ) -> Result < Self > {
489+ pub fn from_vmcs (
490+ vmcs : & vmcs:: ActiveVmcs ,
491+ space : & ' a GuestAddressSpace ,
492+ ) -> Result < Self > {
493493 let cr3 = vmcs. read_field ( vmcs:: VmcsField :: GuestCr3 ) ?;
494494 let cr3 = GuestPhysAddr :: new ( cr3) ;
495495 Ok ( Self { space, cr3 } )
@@ -500,7 +500,7 @@ where
500500 addr : GuestVirtAddr ,
501501 access : GuestAccess ,
502502 ) -> Result < FrameIter > {
503- self . space . borrow ( ) . frame_iter ( self . cr3 , addr, access)
503+ self . space . frame_iter ( self . cr3 , addr, access)
504504 }
505505
506506 pub fn read_bytes (
@@ -509,46 +509,32 @@ where
509509 length : usize ,
510510 access : GuestAccess ,
511511 ) -> Result < Vec < u8 > > {
512- self . space
513- . borrow ( )
514- . read_bytes ( self . cr3 , addr, length, access)
512+ self . space . read_bytes ( self . cr3 , addr, length, access)
515513 }
516514
517515 pub fn translate_linear_address (
518516 & self ,
519517 addr : GuestVirtAddr ,
520518 access : GuestAccess ,
521519 ) -> Result < GuestPhysAddr > {
522- self . space
523- . borrow ( )
524- . translate_linear_address ( self . cr3 , addr, access)
520+ self . space . translate_linear_address ( self . cr3 , addr, access)
525521 }
526- }
527522
528- impl < T > GuestAddressSpaceWrapper < T >
529- where
530- T : BorrowMut < GuestAddressSpace > ,
531- {
532523 pub fn write_bytes (
533- & mut self ,
524+ & self ,
534525 addr : GuestVirtAddr ,
535526 bytes : & [ u8 ] ,
536527 access : GuestAccess ,
537528 ) -> Result < ( ) > {
538- self . space
539- . borrow_mut ( )
540- . write_bytes ( self . cr3 , addr, bytes, access)
529+ self . space . write_bytes ( self . cr3 , addr, bytes, access)
541530 }
542531}
543532
544- impl < T > Deref for GuestAddressSpaceWrapper < T >
545- where
546- T : Borrow < GuestAddressSpace > ,
547- {
548- type Target = T ;
533+ impl < ' a > Deref for GuestAddressSpaceView < ' a > {
534+ type Target = GuestAddressSpace ;
549535
550- fn deref ( & self ) -> & Self :: Target {
551- & self . space
536+ fn deref ( & self ) -> & ' a Self :: Target {
537+ self . space
552538 }
553539}
554540
0 commit comments