Skip to content

Commit

Permalink
init template of EPT violation handler
Browse files Browse the repository at this point in the history
Signed-off-by: smallkirby <[email protected]>
  • Loading branch information
smallkirby committed Aug 30, 2024
1 parent 251cd35 commit 8ba3596
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ymir/arch/x86/vmx.zig
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ pub const Vcpu = struct {
/// Handle the VM-exit.
fn handleExit(_: *Self, exit_info: ExitInformation) VmxError!void {
switch (exit_info.basic_reason) {
.ept => {
const qual = try getExitQual(ext.QualEptViolation);
log.err("EPT violation: {?}", .{qual});
@panic("Aborting Ymir...");
},
else => {
log.err("Unhandled VM-exit: reason={?}", .{exit_info.basic_reason});
@panic("Aborting Ymir...");
Expand Down Expand Up @@ -688,6 +693,11 @@ pub fn getExitReason() VmxError!ExitInformation {
return @bitCast(@as(u32, @truncate(try vmread(vmcs.Ro.vmexit_reason))));
}

/// Get a VM-exit qualification from VMCS.
fn getExitQual(T: anytype) VmxError!T {
return @bitCast(@as(u64, try vmread(vmcs.Ro.exit_qual)));
}

const VmxonRegion = packed struct {
vmcs_revision_id: u31,
zero: u1 = 0,
Expand Down
5 changes: 4 additions & 1 deletion ymir/arch/x86/vmx/ept.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ const num_table_entries: usize = 512;

/// Init guest EPT.
pub fn initEpt(
/// Guest physical address to map.
guest_start: Phys,
/// Host physical address to map.
host_start: Phys,
/// Size in bytes of the memory region to map.
size: usize,
/// Page allocator that returns physical pages.
/// Page allocator.
page_allocator: Allocator,
) ![]Lv4EptEntry {
if (size & page_mask_2mb != 0) {
Expand Down
42 changes: 42 additions & 0 deletions ymir/arch/x86/vmx/exit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,45 @@ pub const QualIo = packed struct(u64) {
imm = 1,
};
};

/// Exit qualification for EPT violations.
/// cf. SDM Vol.3C Table 28-7.
pub const QualEptViolation = packed struct(u64) {
/// The violation was data read.
read: bool,
/// The violation was data write.
write: bool,
/// The violation was instruction fetch.
fetch: bool,
/// The page is readable.
readable: bool,
/// The page is writable.
writable: bool,
/// The page is executable.
executable: bool,
/// The page is executable for user-mode linear addresses.
/// Undefined if "mode-based execute control" is 0.
executable_user: bool,
/// Guest linear-address field is valid.
valid_linear: bool,
/// The violation occurred during a guest page table walk.
during_walk: bool,
///
linear_user: bool,
///
rw: bool,
///
exec_disabled: bool,
///
nmi_unblocking: bool,
///
shadow_stack: bool,
///
b60: bool,
///
verification: bool,
///
trace: bool,
/// Reserved.
reserved: u47,
};

0 comments on commit 8ba3596

Please sign in to comment.