Skip to content

Commit cce9787

Browse files
jakecorrentitylerfanelli
authored andcommitted
launch: update KVM_TDX_INIT_MEM_REGION iotl
With the update to 6.16, KVM_TDX_INIT_MEM_REGION went from a VM ioctl to a vCPU ioctl. Reflect that in the user API for the library. Signed-off-by: Jake Correnti <[email protected]>
1 parent 43d1f33 commit cce9787

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

src/launch/mod.rs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/launch.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ fn launch() {
122122
// TODO(jakecorrenti): the current CentOS SIG doesn't support the KVM_MEMORY_MAPPING or
123123
// KVM_TDX_EXTEND_MEMORY ioctls, which is what we would typically use here.
124124
} else {
125-
tdx_vm
126-
.init_mem_region(&vm_fd, guest_addr, 1, 1, firmware_userspace)
127-
.unwrap();
125+
TdxVcpu::init_mem_region(&vcpufd, guest_addr, 1, 1, firmware_userspace).unwrap();
128126
}
129127

130128
// finalize measurement

0 commit comments

Comments
 (0)