Skip to content

Commit

Permalink
vcpu: nfc: minor refactor
Browse files Browse the repository at this point in the history
Signed-off-by: smallkirby <[email protected]>
  • Loading branch information
smallkirby committed Nov 3, 2024
1 parent 54b6c04 commit f06b5f4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
21 changes: 9 additions & 12 deletions ymir/arch/x86/arch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,17 @@ pub inline fn enableCpuid() void {
}
}

/// Get CPU Vendr ID string.
/// Note that the string is not null-terminated.
pub fn getCpuVendorId() [12]u8 {
var ret: [12]u8 = undefined;
const regs = cpuid.Leaf.maximum_input.query(null);
const regs = cpuid.Leaf.query(.maximum_input, null);

for (0..4) |i| {
const b: usize = (regs.ebx >> @truncate(i * 8));
ret[0 + i] = @as(u8, @truncate(b));
}
for (0..4) |i| {
const b: usize = (regs.edx >> @truncate(i * 8));
ret[4 + i] = @as(u8, @truncate(b));
}
for (0..4) |i| {
const b: usize = (regs.ecx >> @truncate(i * 8));
ret[8 + i] = @as(u8, @truncate(b));
for ([_]u32{ regs.ebx, regs.edx, regs.ecx }, 0..) |reg, i| {
for (0..4) |j| {
const b: usize = (reg >> @truncate(j * 8));
ret[i * 4 + j] = @as(u8, @truncate(b));
}
}
return ret;
}
Expand All @@ -93,6 +89,7 @@ pub fn isVmxSupported() bool {
// Enable VMX outside SMX.
if (msr_fctl.lock) @panic("IA32_FEATURE_CONTROL is locked while VMX outside SMX is disabled");
msr_fctl.vmx_outside_smx = true;
msr_fctl.lock = true;
am.writeMsrFeatureControl(msr_fctl);
}
msr_fctl = am.readMsrFeatureControl();
Expand Down
2 changes: 1 addition & 1 deletion ymir/arch/x86/asm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ pub fn writeMsrFeatureControl(value: MsrFeatureControl) void {
}

pub fn readMsrVmxBasic() MsrVmxBasic {
const val = readMsr(Msr.vmx_basic);
const val = readMsr(.vmx_basic);
return @bitCast(val);
}

Expand Down
22 changes: 12 additions & 10 deletions ymir/arch/x86/vmx/vcpu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,8 @@ fn adjustControlRegisters() void {
}

/// Read VMCS revision identifier.
fn getVmcsRevisionId() u31 {
const vmx_basic = am.readMsrVmxBasic();
return vmx_basic.vmcs_revision_id;
inline fn getVmcsRevisionId() u31 {
return am.readMsrVmxBasic().vmcs_revision_id;
}

/// Puts the logical processor in VMX operation with no VMCS loaded.
Expand Down Expand Up @@ -784,9 +783,10 @@ const VmxonRegion = packed struct {
zero: u1 = 0,

/// Allocate VMXON region.
pub fn new(page_allocator: Allocator) VmxError!*align(4096) VmxonRegion {
const page = page_allocator.alloc(u8, 4096) catch return VmxError.OutOfMemory;
if (page.len != 4096 or @intFromPtr(page.ptr) % 4096 != 0) {
pub fn new(page_allocator: Allocator) VmxError!*align(mem.page_size) VmxonRegion {
const size = am.readMsrVmxBasic().vmxon_region_size;
const page = page_allocator.alloc(u8, size) catch return VmxError.OutOfMemory;
if (@intFromPtr(page.ptr) % mem.page_size != 0) {
return error.OutOfMemory;
}
@memset(page, 0);
Expand All @@ -795,7 +795,7 @@ const VmxonRegion = packed struct {

pub fn deinit(self: *VmxonRegion, page_allocator: Allocator) void {
const ptr: [*]u8 = @ptrCast(self);
page_allocator.free(ptr[0..4096]);
page_allocator.free(ptr[0..mem.page_size]);
}
};

Expand All @@ -813,17 +813,19 @@ const VmcsRegion = packed struct {

/// Allocate a VMCS region.
pub fn new(page_allocator: Allocator) VmxError!*align(4096) VmcsRegion {
const page = try page_allocator.alloc(u8, 4096);
if (page.len != 4096 or @intFromPtr(page.ptr) % 4096 != 0) {
const size = am.readMsrVmxBasic().vmxon_region_size;
const page = try page_allocator.alloc(u8, size);
if (@intFromPtr(page.ptr) % mem.page_size != 0) {
return error.OutOfMemory;
}
@memset(page, 0);
return @alignCast(@ptrCast(page.ptr));
}

pub fn deinit(self: *VmcsRegion, page_allocator: Allocator) void {
const size = am.readMsrVmxBasic().vmxon_region_size;
const ptr: [*]u8 = @ptrCast(self);
page_allocator.free(ptr[0..4096]);
page_allocator.free(ptr[0..size]);
}
};

Expand Down

0 comments on commit f06b5f4

Please sign in to comment.