Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exception & syscall handler improvements #91

Merged
merged 8 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions common/tdx-types/src/tdcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ impl MdFieldId {
pub const LSTAR_WRITE: Self = Self::msr_bitmaps1(0xC000_0082, true);
pub const LSTAR_WRITE_MASK: u64 = Self::msr_bitmaps_mask(0xC000_0082);

pub const SFMASK_WRITE: Self = Self::msr_bitmaps1(0xC000_0084, true);
pub const SFMASK_WRITE_MASK: u64 = Self::msr_bitmaps_mask(0xC000_0084);

pub const TDVPS_L2_CTLS1: Self = Self::new(
81,
ElementSizeCode::SixtyFour,
Expand Down
27 changes: 17 additions & 10 deletions tee/kernel/src/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use core::{
};

use crate::spin::lazy::Lazy;
use crate::user::process::syscall::cpu_state::exception_entry;
use alloc::alloc::alloc;
use log::{debug, error, trace};
use snp_types::intercept::VMEXIT_CPUID;
Expand Down Expand Up @@ -170,6 +171,7 @@ pub fn load_idt() {
extern "x86-interrupt" fn divide_error_handler(frame: InterruptStackFrame) {
unsafe {
naked_asm!(
"cld",
// Check whether the exception happened in userspace.
"test word ptr [rsp+16], 3",
"je {kernel_divide_error_handler}",
Expand All @@ -179,11 +181,11 @@ extern "x86-interrupt" fn divide_error_handler(frame: InterruptStackFrame) {
// Store the error code.
"mov byte ptr gs:[{VECTOR_OFFSET}], 0x0",
// Jump to the userspace exit point.
"jmp gs:[{HANDLER_OFFSET}]",
"jmp {exception_entry}",

kernel_divide_error_handler = sym kernel_divide_error_handler,
VECTOR_OFFSET = const offset_of!(PerCpu, vector),
HANDLER_OFFSET = const offset_of!(PerCpu, userspace_exception_exit_point),
exception_entry = sym exception_entry,
);
}
}
Expand All @@ -199,6 +201,7 @@ extern "x86-interrupt" fn page_fault_handler(
) {
unsafe {
naked_asm!(
"cld",
// Check whether the exception happened in userspace.
"test word ptr [rsp+16], 3",
"je 66f",
Expand All @@ -209,7 +212,7 @@ extern "x86-interrupt" fn page_fault_handler(
"mov byte ptr gs:[{VECTOR_OFFSET}], 0xe",
"pop qword ptr gs:[{ERROR_CODE_OFFSET}]",
// Jump to the userspace exit point.
"jmp gs:[{HANDLER_OFFSET}]",
"jmp {exception_entry}",

// Kernel code path:
"66:",
Expand Down Expand Up @@ -258,7 +261,7 @@ extern "x86-interrupt" fn page_fault_handler(
kernel_page_fault_handler = sym kernel_page_fault_handler,
VECTOR_OFFSET = const offset_of!(PerCpu, vector),
ERROR_CODE_OFFSET = const offset_of!(PerCpu, error_code),
HANDLER_OFFSET = const offset_of!(PerCpu, userspace_exception_exit_point),
exception_entry = sym exception_entry,
);
}
}
Expand Down Expand Up @@ -303,6 +306,7 @@ extern "x86-interrupt" fn general_protection_fault_handler(
) {
unsafe {
naked_asm!(
"cld",
// Check whether the exception happened in userspace.
"test word ptr [rsp+16], 3",
"je {kernel_general_protection_fault_handler}",
Expand All @@ -313,12 +317,12 @@ extern "x86-interrupt" fn general_protection_fault_handler(
"mov byte ptr gs:[{VECTOR_OFFSET}], 0xd",
"pop qword ptr gs:[{ERROR_CODE_OFFSET}]",
// Jump to the userspace exit point.
"jmp gs:[{HANDLER_OFFSET}]",
"jmp {exception_entry}",

kernel_general_protection_fault_handler = sym kernel_general_protection_fault_handler,
VECTOR_OFFSET = const offset_of!(PerCpu, vector),
ERROR_CODE_OFFSET = const offset_of!(PerCpu, error_code),
HANDLER_OFFSET = const offset_of!(PerCpu, userspace_exception_exit_point),
exception_entry = sym exception_entry,
);
}
}
Expand All @@ -338,6 +342,7 @@ extern "x86-interrupt" fn double_fault_handler(frame: InterruptStackFrame, code:
extern "x86-interrupt" fn vc_handler(frame: InterruptStackFrame, error_code: u64) {
unsafe {
naked_asm!(
"cld",
// Check whether the exception happened in userspace.
"test word ptr [rsp+16], 3",
"je {kernel_vc_handler}",
Expand All @@ -348,12 +353,12 @@ extern "x86-interrupt" fn vc_handler(frame: InterruptStackFrame, error_code: u64
"mov byte ptr gs:[{VECTOR_OFFSET}], 0x1d",
"pop qword ptr gs:[{ERROR_CODE_OFFSET}]",
// Jump to the userspace exit point.
"jmp gs:[{HANDLER_OFFSET}]",
"jmp {exception_entry}",

kernel_vc_handler = sym kernel_vc_handler,
VECTOR_OFFSET = const offset_of!(PerCpu, vector),
ERROR_CODE_OFFSET = const offset_of!(PerCpu, error_code),
HANDLER_OFFSET = const offset_of!(PerCpu, userspace_exception_exit_point),
exception_entry = sym exception_entry,
);
}
}
Expand All @@ -362,6 +367,7 @@ extern "x86-interrupt" fn vc_handler(frame: InterruptStackFrame, error_code: u64
extern "x86-interrupt" fn kernel_vc_handler(frame: InterruptStackFrame, code: u64) {
unsafe {
naked_asm!(
"cld",
"push r11",
"push r10",
"push r9",
Expand Down Expand Up @@ -464,11 +470,12 @@ extern "x86-interrupt" fn int0x80_handler(frame: InterruptStackFrame) {
// continue when userspace exits.
unsafe {
naked_asm!(
"cld",
"swapgs",
"mov byte ptr gs:[{VECTOR_OFFSET}], 0x80",
"jmp gs:[{HANDLER_OFFSET}]",
"jmp {exception_entry}",
VECTOR_OFFSET = const offset_of!(PerCpu, vector),
HANDLER_OFFSET = const offset_of!(PerCpu, userspace_exception_exit_point),
exception_entry = sym exception_entry,
);
}
}
2 changes: 0 additions & 2 deletions tee/kernel/src/per_cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub struct PerCpu {
pub gdt: OnceCell<GlobalDescriptorTable>,
pub int0x80_handler: Cell<u64>,
pub exit_with_sysret: Cell<bool>,
pub userspace_exception_exit_point: Cell<u64>,
pub exit: Cell<RawExit>,
pub vector: Cell<u8>,
pub error_code: Cell<u64>,
Expand All @@ -53,7 +52,6 @@ impl PerCpu {
gdt: OnceCell::new(),
int0x80_handler: Cell::new(0),
exit_with_sysret: Cell::new(false),
userspace_exception_exit_point: Cell::new(0),
exit: Cell::new(RawExit::Syscall),
vector: Cell::new(0),
error_code: Cell::new(0),
Expand Down
4 changes: 4 additions & 0 deletions tee/kernel/src/user.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use process::syscall;

use crate::{memory::frame, rt::poll, supervisor::halt, time::advance_time};

pub mod process;

pub fn run() -> ! {
syscall::init();

loop {
while poll() {}

Expand Down
2 changes: 2 additions & 0 deletions tee/kernel/src/user/process/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub mod args;
pub mod cpu_state;
pub mod traits;

pub use cpu_state::init;

impl Thread {
/// Returns true if the thread should continue to run.
pub async fn execute_syscall(self: Arc<Self>, args: SyscallArgs) {
Expand Down
Loading