Skip to content

Commit db91d1f

Browse files
committed
[interrupt-handlers] added IDTR
- made IDTR structure to house the IDT, - made load_idt fxn that uses global_asm's lidt to load the IDT, and - added fxn calls to entrypoint to init_idt and load_idt (location debatable?). Signed-off-by: danbugs <[email protected]>
1 parent bae7f9a commit db91d1f

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/hyperlight_guest/src/entrypoint.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ use crate::{
3030
__security_cookie, HEAP_ALLOCATOR, MIN_STACK_ADDRESS, OS_PAGE_SIZE, OUTB_PTR,
3131
OUTB_PTR_WITH_CONTEXT, P_PEB, RUNNING_MODE,
3232
};
33+
use crate::idt::init_idt;
34+
use crate::idtr::load_idt;
3335

3436
#[inline(never)]
3537
pub fn halt() {
@@ -86,6 +88,10 @@ pub extern "win64" fn entrypoint(peb_address: u64, seed: u64, ops: u64, max_log_
8688
let peb_ptr = P_PEB.unwrap();
8789
__security_cookie = peb_address ^ seed;
8890

91+
// Set up IDT
92+
init_idt();
93+
load_idt();
94+
8995
let srand_seed = ((peb_address << 8 ^ seed >> 4) >> 32) as u32;
9096

9197
// Set the seed for the random number generator for C code using rand;

src/hyperlight_guest/src/idtr.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2024 The Hyperlight Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
use core::arch::global_asm;
18+
use core::ptr::addr_of;
19+
use crate::idt::{IdtEntry, IDT};
20+
21+
global_asm!(
22+
"
23+
.global lidt
24+
lidt:
25+
# SysV64 calling convention: first argument (IDTR) is in RDI
26+
lidt [rdi] # Load IDT from memory address in RDI
27+
ret
28+
"
29+
);
30+
31+
extern "sysv64" {
32+
fn lidt(idt_ptr: *const Idtr);
33+
}
34+
35+
36+
// The location of the IDT is stored in the IDTR.
37+
#[repr(C)]
38+
struct Idtr {
39+
limit: u16,
40+
base: u64,
41+
}
42+
43+
pub(crate) unsafe fn load_idt() {
44+
let idt_ptr = Idtr {
45+
limit: (size_of::<[IdtEntry; 256]>() - 1) as u16,
46+
base: addr_of!(IDT) as *const _ as u64,
47+
};
48+
49+
lidt(&idt_ptr);
50+
}

src/hyperlight_guest/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub mod logging;
5454
pub mod interrupt_entry;
5555
pub mod interrupt_handlers;
5656
pub mod idt;
57+
pub mod idtr;
5758

5859
// Unresolved symbols
5960
///cbindgen:ignore

0 commit comments

Comments
 (0)