Skip to content

Commit 289dc94

Browse files
Merge branch 'coconut-svsm:main' into main
2 parents d962f9c + 697e6c5 commit 289dc94

4 files changed

Lines changed: 30 additions & 40 deletions

File tree

kernel/src/cpu/idt/common.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,19 @@
44
//
55
// Author: Joerg Roedel <jroedel@suse.de>
66

7-
extern crate alloc;
8-
97
use crate::address::{Address, VirtAddr};
108
use crate::cpu::control_regs::{read_cr0, read_cr4};
119
use crate::cpu::efer::read_efer;
1210
use crate::cpu::gdt::GLOBAL_GDT;
1311
use crate::cpu::registers::{X86GeneralRegs, X86InterruptFrame};
1412
use crate::cpu::shadow_stack::is_cet_ss_enabled;
1513
use crate::error::SvsmError;
16-
use crate::insn_decode::{InsnError, InsnMachineCtx, InsnMachineMem, Register, SegRegister};
14+
use crate::insn_decode::{InsnError, InsnMachineCtx, Register, SegRegister};
1715
use crate::mm::ro_after_init::make_ro;
1816
use crate::mm::{GuestPtr, PAGE_SIZE, PageBox};
1917
use crate::platform::SVSM_PLATFORM;
2018
use crate::types::{Bytes, SVSM_CS};
2119
use crate::utils::{MemoryRegion, is_aligned};
22-
use alloc::boxed::Box;
2320
use core::arch::asm;
2421
use core::mem;
2522
use zerocopy::{FromBytes, IntoBytes};
@@ -100,6 +97,8 @@ impl X86ExceptionContext {
10097
}
10198

10299
impl InsnMachineCtx for X86ExceptionContext {
100+
type Ptr<T: FromBytes + IntoBytes> = GuestPtr<T>;
101+
103102
fn read_efer(&self) -> u64 {
104103
read_efer().bits()
105104
}
@@ -171,16 +170,16 @@ impl InsnMachineCtx for X86ExceptionContext {
171170
self.frame.cs & 3
172171
}
173172

174-
fn map_linear_addr<T: FromBytes + IntoBytes + 'static>(
173+
fn map_linear_addr<T: FromBytes + IntoBytes>(
175174
&self,
176175
la: usize,
177176
_write: bool,
178177
_fetch: bool,
179-
) -> Result<Box<dyn InsnMachineMem<Item = T>>, InsnError> {
178+
) -> Result<Self::Ptr<T>, InsnError> {
180179
if user_mode(self) {
181180
todo!();
182181
} else {
183-
Ok(Box::new(GuestPtr::<T>::new(VirtAddr::from(la))))
182+
Ok(GuestPtr::<T>::new(VirtAddr::from(la)))
184183
}
185184
}
186185

kernel/src/insn_decode/decode.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,13 @@
3939
// https://github.com/projectacrn/acrn-hypervisor/blob/master/hypervisor/
4040
// arch/x86/guest/instr_emul.c
4141

42-
extern crate alloc;
43-
4442
use super::insn::{DecodedInsn, Immediate, MAX_INSN_SIZE, Operand};
4543
use super::opcode::{OpCodeClass, OpCodeDesc, OpCodeFlags};
4644
use super::{InsnError, Register, SegRegister};
4745
use crate::cpu::control_regs::{CR0Flags, CR4Flags};
4846
use crate::cpu::efer::EFERFlags;
4947
use crate::cpu::registers::{RFlags, SegDescAttrFlags};
5048
use crate::types::Bytes;
51-
use alloc::boxed::Box;
5249
use bitflags::bitflags;
5350
use zerocopy::{FromBytes, IntoBytes};
5451

@@ -139,6 +136,8 @@ struct DecodedBytes(InsnBytes);
139136
/// instruction decoder can access specific registers and state that may
140137
/// influence the decoding from the machine (such as a CPU or VMM).
141138
pub trait InsnMachineCtx: core::fmt::Debug {
139+
type Ptr<T: FromBytes + IntoBytes>: InsnMachineMem<T>;
140+
142141
/// Read EFER register
143142
fn read_efer(&self) -> u64;
144143
/// Read a code segment register
@@ -179,14 +178,14 @@ pub trait InsnMachineCtx: core::fmt::Debug {
179178
///
180179
/// # Returns
181180
///
182-
/// A `Result` containing a boxed trait object representing the mapped
183-
/// memory, or an `InsnError` if mapping fails.
184-
fn map_linear_addr<T: FromBytes + IntoBytes + 'static>(
181+
/// A pointer-like type that allows reading and writing via [`InsnMachineMem`],
182+
/// or an `InsnError` if mapping fails.
183+
fn map_linear_addr<T: FromBytes + IntoBytes>(
185184
&self,
186185
_la: usize,
187186
_write: bool,
188187
_fetch: bool,
189-
) -> Result<Box<dyn InsnMachineMem<Item = T>>, InsnError> {
188+
) -> Result<Self::Ptr<T>, InsnError> {
190189
Err(InsnError::MapLinearAddr)
191190
}
192191

@@ -312,9 +311,7 @@ pub trait InsnMachineCtx: core::fmt::Debug {
312311
}
313312

314313
/// Trait representing a machine memory for instruction decoding.
315-
pub trait InsnMachineMem {
316-
type Item;
317-
314+
pub trait InsnMachineMem<T: FromBytes + IntoBytes> {
318315
/// Read data from the memory at the specified offset.
319316
///
320317
/// # Safety
@@ -326,7 +323,7 @@ pub trait InsnMachineMem {
326323
///
327324
/// Returns the read data on success, or an `InsnError` if the read
328325
/// operation fails.
329-
unsafe fn mem_read(&self) -> Result<Self::Item, InsnError> {
326+
unsafe fn mem_read(&self) -> Result<T, InsnError> {
330327
Err(InsnError::MemRead)
331328
}
332329

@@ -344,7 +341,7 @@ pub trait InsnMachineMem {
344341
/// # Returns
345342
///
346343
/// Returns `Ok`on success, or an `InsnError` if the write operation fails.
347-
unsafe fn mem_write(&mut self, _data: Self::Item) -> Result<(), InsnError> {
344+
unsafe fn mem_write(&mut self, _data: T) -> Result<(), InsnError> {
348345
Err(InsnError::MemWrite)
349346
}
350347
}

kernel/src/insn_decode/insn.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,10 @@ impl Instruction {
100100

101101
#[cfg(any(test, fuzzing))]
102102
pub mod test_utils {
103-
extern crate alloc;
104-
105103
use crate::cpu::control_regs::{CR0Flags, CR4Flags};
106104
use crate::cpu::efer::EFERFlags;
107105
use crate::insn_decode::*;
108106
use crate::types::Bytes;
109-
use alloc::boxed::Box;
110107
use zerocopy::{FromBytes, IntoBytes};
111108

112109
pub const TEST_PORT: u16 = 0xE0;
@@ -174,12 +171,15 @@ pub mod test_utils {
174171
}
175172
}
176173

174+
#[derive(Debug, Clone, Copy)]
177175
#[cfg_attr(not(test), expect(dead_code))]
178-
struct TestMem<T> {
176+
pub struct TestMem<T> {
179177
ptr: *mut T,
180178
}
181179

182180
impl InsnMachineCtx for TestCtx {
181+
type Ptr<T: FromBytes + IntoBytes> = TestMem<T>;
182+
183183
fn read_efer(&self) -> u64 {
184184
self.efer
185185
}
@@ -251,13 +251,13 @@ pub mod test_utils {
251251
self.flags
252252
}
253253

254-
fn map_linear_addr<T: FromBytes + IntoBytes + 'static>(
254+
fn map_linear_addr<T: FromBytes + IntoBytes>(
255255
&self,
256256
la: usize,
257257
_write: bool,
258258
_fetch: bool,
259-
) -> Result<Box<dyn InsnMachineMem<Item = T>>, InsnError> {
260-
Ok(Box::new(TestMem { ptr: la as *mut T }))
259+
) -> Result<Self::Ptr<T>, InsnError> {
260+
Ok(TestMem { ptr: la as *mut T })
261261
}
262262

263263
fn ioio_in(&self, _port: u16, size: Bytes) -> Result<u64, InsnError> {
@@ -345,13 +345,11 @@ pub mod test_utils {
345345
}
346346

347347
#[cfg(test)]
348-
impl<T: FromBytes + IntoBytes> InsnMachineMem for TestMem<T> {
349-
type Item = T;
350-
348+
impl<T: FromBytes + IntoBytes> InsnMachineMem<T> for TestMem<T> {
351349
/// # Safety
352350
/// The caller is required to ensure the validity of the address
353351
/// this object was initialized with.
354-
unsafe fn mem_read(&self) -> Result<Self::Item, InsnError> {
352+
unsafe fn mem_read(&self) -> Result<T, InsnError> {
355353
// SAFETY: caller must ensure `ptr` was initialized with a valid
356354
// address.
357355
Ok(unsafe { self.ptr.read() })
@@ -360,7 +358,7 @@ pub mod test_utils {
360358
/// # Safety
361359
/// The caller is required to ensure the validity of the address
362360
/// this object was initialized with.
363-
unsafe fn mem_write(&mut self, data: Self::Item) -> Result<(), InsnError> {
361+
unsafe fn mem_write(&mut self, data: T) -> Result<(), InsnError> {
364362
// SAFETY: caller must ensure `ptr` was initialized with a valid
365363
// address.
366364
unsafe {
@@ -371,12 +369,10 @@ pub mod test_utils {
371369
}
372370

373371
#[cfg(fuzzing)]
374-
impl<T: FromBytes + IntoBytes> InsnMachineMem for TestMem<T> {
375-
type Item = T;
376-
372+
impl<T: FromBytes + IntoBytes> InsnMachineMem<T> for TestMem<T> {
377373
/// # Safety
378374
/// No safety concerns under fuzzing.
379-
unsafe fn mem_write(&mut self, _data: Self::Item) -> Result<(), InsnError> {
375+
unsafe fn mem_write(&mut self, _data: T) -> Result<(), InsnError> {
380376
Ok(())
381377
}
382378
}

kernel/src/mm/guestmem.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,21 +418,19 @@ impl<T> From<NonNull<T>> for GuestPtr<T> {
418418
}
419419
}
420420

421-
impl<T: FromBytes + IntoBytes> InsnMachineMem for GuestPtr<T> {
422-
type Item = T;
423-
421+
impl<T: FromBytes + IntoBytes> InsnMachineMem<T> for GuestPtr<T> {
424422
/// # Safety
425423
///
426424
/// See the GuestPtr's read() method documentation for safety requirements.
427-
unsafe fn mem_read(&self) -> Result<Self::Item, InsnError> {
425+
unsafe fn mem_read(&self) -> Result<T, InsnError> {
428426
// SAFETY: Safe when GuestPtr::read safety requirements are met.
429427
unsafe { self.read().map_err(|_| InsnError::MemRead) }
430428
}
431429

432430
/// # Safety
433431
///
434432
/// See the GuestPtr's write() method documentation for safety requirements.
435-
unsafe fn mem_write(&mut self, data: Self::Item) -> Result<(), InsnError> {
433+
unsafe fn mem_write(&mut self, data: T) -> Result<(), InsnError> {
436434
// SAFETY: Safe when GuestPtr::write safety requirements are met.
437435
unsafe { self.write(data).map_err(|_| InsnError::MemWrite) }
438436
}

0 commit comments

Comments
 (0)