@@ -54,7 +54,6 @@ use crate::bitmap::{Bitmap, BS, MS};
5454use crate :: bytes:: { AtomicAccess , Bytes } ;
5555use crate :: io:: { ReadVolatile , WriteVolatile } ;
5656use crate :: volatile_memory:: { self , VolatileSlice } ;
57- use crate :: GuestMemoryError ;
5857
5958/// Errors associated with handling guest memory accesses.
6059#[ allow( missing_docs) ]
@@ -538,137 +537,6 @@ pub trait GuestMemory {
538537 }
539538 }
540539
541- /// Reads up to `count` bytes from an object and writes them into guest memory at `addr`.
542- ///
543- /// Returns the number of bytes written into guest memory.
544- ///
545- /// # Arguments
546- /// * `addr` - Begin writing at this address.
547- /// * `src` - Copy from `src` into the container.
548- /// * `count` - Copy `count` bytes from `src` into the container.
549- ///
550- /// # Examples
551- ///
552- /// * Read bytes from /dev/urandom (uses the `backend-mmap` feature)
553- ///
554- /// ```
555- /// # #[cfg(feature = "backend-mmap")]
556- /// # {
557- /// # use vm_memory::{Address, GuestMemory, Bytes, GuestAddress, GuestMemoryMmap};
558- /// # use std::fs::File;
559- /// # use std::path::Path;
560- /// #
561- /// # let start_addr = GuestAddress(0x1000);
562- /// # let gm = GuestMemoryMmap::<()>::from_ranges(&vec![(start_addr, 0x400)])
563- /// # .expect("Could not create guest memory");
564- /// # let addr = GuestAddress(0x1010);
565- /// # let mut file = if cfg!(unix) {
566- /// let mut file = File::open(Path::new("/dev/urandom")).expect("Could not open /dev/urandom");
567- /// # file
568- /// # } else {
569- /// # File::open(Path::new("c:\\Windows\\system32\\ntoskrnl.exe"))
570- /// # .expect("Could not open c:\\Windows\\system32\\ntoskrnl.exe")
571- /// # };
572- ///
573- /// gm.read_volatile_from(addr, &mut file, 128)
574- /// .expect("Could not read from /dev/urandom into guest memory");
575- ///
576- /// let read_addr = addr.checked_add(8).expect("Could not compute read address");
577- /// let rand_val: u32 = gm
578- /// .read_obj(read_addr)
579- /// .expect("Could not read u32 val from /dev/urandom");
580- /// # }
581- /// ```
582- fn read_volatile_from < F > ( & self , addr : GuestAddress , src : & mut F , count : usize ) -> Result < usize >
583- where
584- F : ReadVolatile ,
585- {
586- self . try_access ( count, addr, |_, len, caddr, region| -> Result < usize > {
587- let mut vslice = region. get_slice ( caddr, len) ?;
588-
589- src. read_volatile ( & mut vslice)
590- . map_err ( GuestMemoryError :: from)
591- } )
592- }
593-
594- /// Reads up to `count` bytes from guest memory at `addr` and writes them it into an object.
595- ///
596- /// Returns the number of bytes copied from guest memory.
597- ///
598- /// # Arguments
599- /// * `addr` - Begin reading from this address.
600- /// * `dst` - Copy from guest memory to `dst`.
601- /// * `count` - Copy `count` bytes from guest memory to `dst`.
602- fn write_volatile_to < F > ( & self , addr : GuestAddress , dst : & mut F , count : usize ) -> Result < usize >
603- where
604- F : WriteVolatile ,
605- {
606- self . try_access ( count, addr, |_, len, caddr, region| -> Result < usize > {
607- let vslice = region. get_slice ( caddr, len) ?;
608-
609- // For a non-RAM region, reading could have side effects, so we
610- // must use write_all().
611- dst. write_all_volatile ( & vslice) ?;
612-
613- Ok ( len)
614- } )
615- }
616-
617- /// Reads exactly `count` bytes from an object and writes them into guest memory at `addr`.
618- ///
619- /// # Errors
620- ///
621- /// Returns an error if `count` bytes couldn't have been copied from `src` to guest memory.
622- /// Part of the data may have been copied nevertheless.
623- ///
624- /// # Arguments
625- /// * `addr` - Begin writing at this address.
626- /// * `src` - Copy from `src` into guest memory.
627- /// * `count` - Copy exactly `count` bytes from `src` into guest memory.
628- fn read_exact_volatile_from < F > (
629- & self ,
630- addr : GuestAddress ,
631- src : & mut F ,
632- count : usize ,
633- ) -> Result < ( ) >
634- where
635- F : ReadVolatile ,
636- {
637- let res = self . read_volatile_from ( addr, src, count) ?;
638- if res != count {
639- return Err ( Error :: PartialBuffer {
640- expected : count,
641- completed : res,
642- } ) ;
643- }
644- Ok ( ( ) )
645- }
646-
647- /// Reads exactly `count` bytes from guest memory at `addr` and writes them into an object.
648- ///
649- /// # Errors
650- ///
651- /// Returns an error if `count` bytes couldn't have been copied from guest memory to `dst`.
652- /// Part of the data may have been copied nevertheless.
653- ///
654- /// # Arguments
655- /// * `addr` - Begin reading from this address.
656- /// * `dst` - Copy from guest memory to `dst`.
657- /// * `count` - Copy exactly `count` bytes from guest memory to `dst`.
658- fn write_all_volatile_to < F > ( & self , addr : GuestAddress , dst : & mut F , count : usize ) -> Result < ( ) >
659- where
660- F : WriteVolatile ,
661- {
662- let res = self . write_volatile_to ( addr, dst, count) ?;
663- if res != count {
664- return Err ( Error :: PartialBuffer {
665- expected : count,
666- completed : res,
667- } ) ;
668- }
669- Ok ( ( ) )
670- }
671-
672540 /// Get the host virtual address corresponding to the guest address.
673541 ///
674542 /// Some [`GuestMemory`](trait.GuestMemory.html) implementations, like `GuestMemoryMmap`,
@@ -796,6 +664,59 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
796664 Ok ( ( ) )
797665 }
798666
667+ fn read_volatile_from < F > ( & self , addr : GuestAddress , src : & mut F , count : usize ) -> Result < usize >
668+ where
669+ F : ReadVolatile ,
670+ {
671+ self . try_access ( count, addr, |_, len, caddr, region| -> Result < usize > {
672+ region. read_volatile_from ( caddr, src, len)
673+ } )
674+ }
675+
676+ fn read_exact_volatile_from < F > (
677+ & self ,
678+ addr : GuestAddress ,
679+ src : & mut F ,
680+ count : usize ,
681+ ) -> Result < ( ) >
682+ where
683+ F : ReadVolatile ,
684+ {
685+ let res = self . read_volatile_from ( addr, src, count) ?;
686+ if res != count {
687+ return Err ( Error :: PartialBuffer {
688+ expected : count,
689+ completed : res,
690+ } ) ;
691+ }
692+ Ok ( ( ) )
693+ }
694+
695+ fn write_volatile_to < F > ( & self , addr : GuestAddress , dst : & mut F , count : usize ) -> Result < usize >
696+ where
697+ F : WriteVolatile ,
698+ {
699+ self . try_access ( count, addr, |_, len, caddr, region| -> Result < usize > {
700+ // For a non-RAM region, reading could have side effects, so we
701+ // must use write_all().
702+ region. write_all_volatile_to ( caddr, dst, len) . map ( |( ) | len)
703+ } )
704+ }
705+
706+ fn write_all_volatile_to < F > ( & self , addr : GuestAddress , dst : & mut F , count : usize ) -> Result < ( ) >
707+ where
708+ F : WriteVolatile ,
709+ {
710+ let res = self . write_volatile_to ( addr, dst, count) ?;
711+ if res != count {
712+ return Err ( Error :: PartialBuffer {
713+ expected : count,
714+ completed : res,
715+ } ) ;
716+ }
717+ Ok ( ( ) )
718+ }
719+
799720 fn store < O : AtomicAccess > ( & self , val : O , addr : GuestAddress , order : Ordering ) -> Result < ( ) > {
800721 // `find_region` should really do what `to_region_addr` is doing right now, except
801722 // it should keep returning a `Result`.
0 commit comments