Skip to content

Commit 002d18d

Browse files
committed
fixup! amd64: add exception stack at top of scratch memory
Add comments Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com>
1 parent 05fc2a5 commit 002d18d

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/hyperlight_guest_bin/src/arch/amd64/init.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ use core::mem;
2020
use super::exception::entry::init_idt;
2121
use super::machine::{GDT, GdtEntry, GdtPointer, ProcCtrl, TSS};
2222

23+
/// See AMD64 Architecture Programmer's Manual, Volume 2: System Programming
24+
/// Section 4: Segmented Virtual Memory
25+
/// §4.6: Descriptor Tables
26+
/// for the functions of the GDT.
27+
///
28+
/// Hyperlight's GDT consists of:
29+
/// - A null first entry, which is architecturally required
30+
/// - A single code segment descriptor, used for all code accesses
31+
/// - A single data segment descriptor, used for all data accesses
32+
/// - A TSS System descriptor that outlines the location of the TSS
33+
/// (see [`init_tss`], below)
2334
#[repr(C)]
2435
struct HyperlightGDT {
2536
null: GdtEntry,
@@ -68,6 +79,14 @@ unsafe fn init_gdt(pc: *mut ProcCtrl) {
6879
}
6980
}
7081

82+
/// Hyperlight's TSS contains only a single IST entry, which is used
83+
/// to set up the stack switch to the exception stack whenever we take
84+
/// an exception (including page faults, which are important, since
85+
/// the fault might be due to needing to grow the stack!)
86+
///
87+
/// This function sets up the TSS and then points the processor at the
88+
/// system segment descriptor, initialized in [`init_gdt`] above,
89+
/// which describes the location of the TSS.
7190
unsafe fn init_tss(pc: *mut ProcCtrl) {
7291
unsafe {
7392
let tss_ptr = &raw mut (*pc).tss;
@@ -77,7 +96,6 @@ unsafe fn init_tss(pc: *mut ProcCtrl) {
7796
- hyperlight_common::layout::SCRATCH_TOP_EXN_STACK_OFFSET
7897
+ 1;
7998
ist1_ptr.write_volatile(exn_stack.to_ne_bytes());
80-
// see init_gdt: 0x18 points to the tss set up above
8199
asm!(
82100
"ltr ax",
83101
in("ax") core::mem::offset_of!(HyperlightGDT, tss),

src/hyperlight_guest_bin/src/arch/amd64/machine.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ impl GdtEntry {
5656
}
5757
}
5858

59+
/// Create a new entry that describes the Task State Segment
60+
/// (TSS).
61+
///
62+
/// The segment descriptor for the TSS needs to be wider than
63+
/// other segments, because its base address is actually used &
64+
/// must therefore be able to encode an entire 64-bit VA. Because
65+
/// of this, it uses two adjacent descriptor entries.
66+
///
67+
/// See AMD64 Architecture Programmer's Manual, Volume 2: System Programming
68+
/// Section 4: Segmented Virtual Memory
69+
/// §4.8: Long-Mod Segment Descriptors
70+
/// §4.8.3: System Descriptors
71+
/// for details of the layout
5972
pub const fn tss(base: u64, limit: u32) -> [Self; 2] {
6073
[
6174
Self {
@@ -89,6 +102,12 @@ pub(super) struct GdtPointer {
89102
pub(super) base: u64,
90103
}
91104

105+
/// Task State Segment
106+
///
107+
/// See AMD64 Architecture Programmer's Manual, Volume 2: System Programming
108+
/// Section 12: Task Management
109+
/// §12.2: Task-Management Resources
110+
/// §12.2.5: 64-bit Task State Segment
92111
#[allow(clippy::upper_case_acronyms)]
93112
#[repr(C, packed)]
94113
pub(super) struct TSS {

0 commit comments

Comments
 (0)