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

[interrupt-handlers] adds exception handling to the hyperlight guest #250

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

danbugs
Copy link
Contributor

@danbugs danbugs commented Feb 8, 2025

This PR introduces exception handling to the Hyperlight guest by setting up and loading a Global Descriptor Table (GDT) and an Interrupt Descriptor Table (IDT) onto the CPU.

GDT Setup & Loading

  • Defines a minimal GDT with code and data segments.
  • Loads the GDT using lgdt.

IDT Setup & Exception Handlers

  • Defines an IDT with exception handlers for all CPU-defined exceptions (e.g., invalid opcode, page fault, general protection fault).
  • Each exception handler saves context, calls a high-level exception handler, and restores execution using iretq.
  • Implements context_save and context_restore to properly preserve CPU state.
  • Loads the IDT using lidt.

This PR closes #228.

@danbugs danbugs added the kind/enhancement New feature or request label Feb 8, 2025
…n handler

- interrupt handler entry stubs =
-- added macros to save/restore register context
-- added defintions for different exceptions (_do_excp0-20+30),
which save context, set params per SysV ABI, call handler,
restore context, then iretq via macros.
- added an exception handler that are used by the _do_excp* fxns.

Note: this code is specific for x86_64

Signed-off-by: danbugs <[email protected]>
- added IdtEntry structure,
- created IDT static mut array, and
- made fxns to init_idt and set_idt_entry.

Signed-off-by: danbugs <[email protected]>
- made Idtr structure (+ its static mut global var) to house the IDT, and
- made load_idt fxn that uses global_asm's lidt to load the IDT.

Signed-off-by: danbugs <[email protected]>
- added GdtEntry struct for GDT static mut global var,
- added null, kernel code, and kernel data segments to GDT,
- added code to load the GDT, and
- added calls to init_idt, load_idt, and load_gdt to entrypoint.

Signed-off-by: danbugs <[email protected]>
- added new guest fxn to simpleguest that triggers an invalid
opcode exception
- added test in guest_dispatch that calls the new guest fxn
to test exception handling

Signed-off-by: danbugs <[email protected]>
@danbugs danbugs force-pushed the danbugs/interrupt-handlers branch from f441a46 to 918bc74 Compare February 14, 2025 07:09
@danbugs danbugs changed the title [interrupt-handlers] adding exception/interrupt handler support for hyperlight guest [interrupt-handlers] adds exception handling to the hyperlight guest Feb 14, 2025
Copy link
Contributor

@ludfjig ludfjig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

generate_excp!(11),
generate_excp!(12),
generate_excp!(13),
generate_excp!(14, pagefault),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe more exception types than pagefault take additional parameters

@@ -86,6 +89,14 @@ pub extern "win64" fn entrypoint(peb_address: u64, seed: u64, ops: u64, max_log_
let peb_ptr = P_PEB.unwrap();
__security_cookie = peb_address ^ seed;

// Set up GDT/IDT
load_gdt();
init_idt();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: does it make sense to distinguish init_idt from load_idt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement exception/interrupt handlers in hyperlight-guest
2 participants