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

Merged
merged 5 commits into from
Feb 24, 2025

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 For PRs adding features, improving functionality, docs, tests, etc. label Feb 8, 2025
@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
ludfjig
ludfjig previously approved these changes 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

@danbugs danbugs force-pushed the danbugs/interrupt-handlers branch from bf3ceb8 to 1070168 Compare February 18, 2025 21:27
@danbugs danbugs marked this pull request as ready for review February 18, 2025 21:28
@danbugs danbugs force-pushed the danbugs/interrupt-handlers branch 3 times, most recently from a8b548a to 6240501 Compare February 18, 2025 21:56
Copy link
Contributor

@syntactically syntactically left a comment

Choose a reason for hiding this comment

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

This looks to be in pretty good shape overall. A general comment: for structure, bitfield, etc. layouts, please cite the Intel or AMD manuals (either is fine, but we should pick one and stick with it, unless the explanation in the other is far better), instead of the osdev wiki. The manuals, unlike the wiki, are authoritative and unlikely to change :)

On a procedural note: can we squash the "fixes" commits into the original commits (or at least turn them into fixup!s immediately after the original commit, that we have checked will squash nicely without conflicts, and then run rebase --autosquash before merging?)

@danbugs danbugs force-pushed the danbugs/interrupt-handlers branch 4 times, most recently from 28cbf38 to 4a9b587 Compare February 21, 2025 06:43
@syntactically syntactically self-requested a review February 21, 2025 13:58
syntactically
syntactically previously approved these changes Feb 21, 2025
@danbugs danbugs force-pushed the danbugs/interrupt-handlers branch 7 times, most recently from 6755f02 to 4b88c02 Compare February 21, 2025 21:50
…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 <danilochiarlone@gmail.com>
- added IdtEntry structure,
- created IDT static mut array, and
- made fxns to init_idt and set_idt_entry.

Signed-off-by: danbugs <danilochiarlone@gmail.com>
- 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 <danilochiarlone@gmail.com>
- 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 <danilochiarlone@gmail.com>
- 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 <danilochiarlone@gmail.com>
@danbugs danbugs force-pushed the danbugs/interrupt-handlers branch from 4b88c02 to 8eafa0d Compare February 24, 2025 19:33
@danbugs danbugs merged commit 8eafa0d into hyperlight-dev:main Feb 24, 2025
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement For PRs adding features, improving functionality, docs, tests, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement exception/interrupt handlers in hyperlight-guest
4 participants