ostd/arch/x86/vm/context.rs
1// SPDX-License-Identifier: MPL-2.0
2
3use super::{VcpuMsrs, VcpuRegs, VcpuSregs};
4
5/// The guest-visible architectural state of an x86 vCPU.
6///
7/// Register values can be modified without affecting the host CPU. The caller
8/// is responsible for initializing them for the guest's execution mode.
9pub struct GuestContext {
10 regs: VcpuRegs,
11 sregs: VcpuSregs,
12 msrs: VcpuMsrs,
13}
14
15impl GuestContext {
16 /// Creates a guest vCPU context from initialized register state.
17 pub fn new(regs: VcpuRegs, sregs: VcpuSregs, msrs: VcpuMsrs) -> Self {
18 Self { regs, sregs, msrs }
19 }
20
21 /// Returns the guest's general-purpose registers, instruction pointer, and flags.
22 pub fn regs(&self) -> &VcpuRegs {
23 &self.regs
24 }
25
26 /// Returns a mutable reference to the guest's general-purpose registers,
27 /// instruction pointer, and flags.
28 pub fn regs_mut(&mut self) -> &mut VcpuRegs {
29 &mut self.regs
30 }
31
32 /// Returns the guest's special registers.
33 pub fn sregs(&self) -> &VcpuSregs {
34 &self.sregs
35 }
36
37 /// Returns a mutable reference to the guest's special registers.
38 pub fn sregs_mut(&mut self) -> &mut VcpuSregs {
39 &mut self.sregs
40 }
41
42 /// Returns the guest's MSRs that are not part of [`VcpuSregs`].
43 pub fn msrs(&self) -> &VcpuMsrs {
44 &self.msrs
45 }
46
47 /// Returns a mutable reference to the guest's MSRs that are not part of [`VcpuSregs`].
48 pub fn msrs_mut(&mut self) -> &mut VcpuMsrs {
49 &mut self.msrs
50 }
51}