Skip to content

Commit

Permalink
Merge pull request #637 from peterfang/umip
Browse files Browse the repository at this point in the history
Enable UMIP
  • Loading branch information
joergroedel authored Mar 6, 2025
2 parents 6b17933 + 1de86ab commit a31e621
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
6 changes: 5 additions & 1 deletion kernel/src/cpu/control_regs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use super::features::cpu_has_pge;
use crate::address::{Address, PhysAddr};
use crate::cpu::features::{cpu_has_smap, cpu_has_smep};
use crate::cpu::features::{cpu_has_smap, cpu_has_smep, cpu_has_umip};
use crate::platform::SvsmPlatform;
use bitflags::bitflags;
use core::arch::asm;
Expand Down Expand Up @@ -48,6 +48,10 @@ pub fn cr4_init(platform: &dyn SvsmPlatform) {
cr4.insert(CR4Flags::SMAP);
}

if cpu_has_umip(platform) {
cr4.insert(CR4Flags::UMIP);
}

// SAFETY: we are not changing any execution-state relevant flags
unsafe {
write_cr4(cr4);
Expand Down
32 changes: 15 additions & 17 deletions kernel/src/cpu/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,28 @@ use crate::platform::SvsmPlatform;
const X86_FEATURE_PGE: u32 = 13;
const X86_FEATURE_SMEP: u32 = 7;
const X86_FEATURE_SMAP: u32 = 20;
const X86_FEATURE_UMIP: u32 = 2;

pub fn cpu_has_pge(platform: &dyn SvsmPlatform) -> bool {
let ret = platform.cpuid(0x00000001);

match ret {
None => false,
Some(c) => (c.edx >> X86_FEATURE_PGE) & 1 == 1,
}
platform
.cpuid(0x0000_0001)
.map_or_else(|| false, |c| (c.edx >> X86_FEATURE_PGE) & 1 == 1)
}

pub fn cpu_has_smep(platform: &dyn SvsmPlatform) -> bool {
let ret = platform.cpuid(0x0000_0007);

match ret {
None => false,
Some(c) => (c.ebx >> X86_FEATURE_SMEP & 1) == 1,
}
platform
.cpuid(0x0000_0007)
.map_or_else(|| false, |c| (c.ebx >> X86_FEATURE_SMEP & 1) == 1)
}

pub fn cpu_has_smap(platform: &dyn SvsmPlatform) -> bool {
let ret = platform.cpuid(0x0000_0007);
platform
.cpuid(0x0000_0007)
.map_or_else(|| false, |c| (c.ebx >> X86_FEATURE_SMAP & 1) == 1)
}

match ret {
None => false,
Some(c) => (c.ebx >> X86_FEATURE_SMAP & 1) == 1,
}
pub fn cpu_has_umip(platform: &dyn SvsmPlatform) -> bool {
platform
.cpuid(0x0000_0007)
.map_or_else(|| false, |c| (c.ecx >> X86_FEATURE_UMIP & 1) == 1)
}
2 changes: 2 additions & 0 deletions kernel/src/cpu/gdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ struct GDTDesc {
addr: VirtAddr,
}

// The base address of the GDT should be aligned on an 8-byte boundary
// to yield the best processor performance.
#[derive(Copy, Clone, Debug, Default)]
pub struct GDTEntry(u64);

Expand Down
6 changes: 4 additions & 2 deletions kernel/src/cpu/idt/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,10 @@ pub fn user_mode(ctxt: &X86ExceptionContext) -> bool {
(ctxt.frame.cs & 3) == 3
}

// The base addresses of the IDT should be aligned on an 8-byte boundary
// to maximize performance of cache line fills.
#[derive(Copy, Clone, Default, Debug)]
#[repr(C, packed)]
#[repr(C, packed(8))]
pub struct IdtEntry {
low: u64,
high: u64,
Expand Down Expand Up @@ -306,7 +308,7 @@ impl IdtEntry {

const IDT_ENTRIES: usize = 256;

#[repr(C, packed)]
#[repr(C, packed(2))]
#[derive(Default, Clone, Copy, Debug)]
struct IdtDesc {
size: u16,
Expand Down

0 comments on commit a31e621

Please sign in to comment.