@@ -182,38 +182,6 @@ impl TdxVm {
182182 None
183183 }
184184
185- /// Encrypt a memory continuous region
186- pub fn init_mem_region (
187- & self ,
188- fd : & VmFd ,
189- gpa : u64 ,
190- nr_pages : u64 ,
191- attributes : u32 ,
192- source_addr : u64 ,
193- ) -> Result < ( ) , TdxError > {
194- const TDVF_SECTION_ATTRIBUTES_MR_EXTEND : u32 = 1u32 << 0 ;
195- let mem_region = kvm_tdx_init_mem_region {
196- source_addr,
197- gpa,
198- nr_pages,
199- } ;
200-
201- let mut cmd: Cmd < kvm_tdx_init_mem_region > = Cmd :: from ( CmdId :: InitMemRegion , & mem_region) ;
202-
203- // determines if we also extend the measurement
204- cmd. flags = if attributes & TDVF_SECTION_ATTRIBUTES_MR_EXTEND > 0 {
205- 1
206- } else {
207- 0
208- } ;
209-
210- unsafe {
211- fd. encrypt_op ( & mut cmd) ?;
212- }
213-
214- Ok ( ( ) )
215- }
216-
217185 /// Complete measurement of the initial TD contents and mark it ready to run
218186 pub fn finalize ( & self , fd : & VmFd ) -> Result < ( ) , TdxError > {
219187 let mut cmd: Cmd < u64 > = Cmd :: from ( CmdId :: FinalizeVm , & 0 ) ;
@@ -375,4 +343,38 @@ impl TdxVcpu {
375343 }
376344 Ok ( ( ) )
377345 }
346+
347+ /// Encrypt a memory continuous region
348+ pub fn init_mem_region (
349+ fd : & kvm_ioctls:: VcpuFd ,
350+ gpa : u64 ,
351+ nr_pages : u64 ,
352+ attributes : u32 ,
353+ source_addr : u64 ,
354+ ) -> Result < ( ) , TdxError > {
355+ const TDVF_SECTION_ATTRIBUTES_MR_EXTEND : u32 = 1u32 << 0 ;
356+ let mem_region = kvm_tdx_init_mem_region {
357+ source_addr,
358+ gpa,
359+ nr_pages,
360+ } ;
361+
362+ let mut cmd: Cmd < kvm_tdx_init_mem_region > = Cmd :: from ( CmdId :: InitMemRegion , & mem_region) ;
363+
364+ // determines if we also extend the measurement
365+ cmd. flags = if attributes & TDVF_SECTION_ATTRIBUTES_MR_EXTEND > 0 {
366+ 1
367+ } else {
368+ 0
369+ } ;
370+
371+ let ret = unsafe { ioctl:: ioctl_with_mut_ptr ( fd, KVM_MEMORY_ENCRYPT_OP ( ) , & mut cmd) } ;
372+ if ret < 0 {
373+ // can't return `ret` because it will just return -1 and not give the error
374+ // code. `cmd.error` will also just be 0.
375+ return Err ( TdxError :: from ( errno:: Error :: last ( ) ) ) ;
376+ }
377+
378+ Ok ( ( ) )
379+ }
378380}
0 commit comments