This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
lib.rs
125 lines (105 loc) · 3.12 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! This module contains methods and macros to create and register interrupt descriptors and
//! interrupt handlers
#![feature(asm)]
#![feature(naked_functions)]
#![feature(const_fn)]
#![no_std]
extern crate x86;
extern crate pic;
extern crate spin;
use spin::Mutex;
use x86::shared::dtables;
use x86::shared::dtables::DescriptorTablePointer;
use x86::bits64::irq::IdtEntry;
/// Creates an IDT entry.
///
/// Creates an IDT entry that executes the expression in `body`.
#[macro_export]
macro_rules! make_idt_entry {
($name:ident, $body:expr) => {{
fn body() {
$body
}
#[naked]
unsafe extern fn $name() {
asm!("push rbp
push r15
push r14
push r13
push r12
push r11
push r10
push r9
push r8
push rsi
push rdi
push rdx
push rcx
push rbx
push rax
mov rsi, rsp
push rsi
cli
call $0
sti
add rsp, 8
pop rax
pop rbx
pop rcx
pop rdx
pop rdi
pop rsi
pop r8
pop r9
pop r10
pop r11
pop r12
pop r13
pop r14
pop r15
pop rbp
iretq" :: "s"(body as fn()) :: "volatile", "intel");
intrinsics::unreachable();
}
use x86::shared::paging::VAddr;
use x86::shared::PrivilegeLevel;
let handler = VAddr::from_usize($name as usize);
// last is "block", idk
IdtEntry::new(handler, 0x8, PrivilegeLevel::Ring0, false)
}};
}
/// The Interrupt Descriptor Table
///
/// The CPU will look at this table to find the appropriate interrupt handler.
static IDT: Mutex<[IdtEntry; 256]> = Mutex::new([IdtEntry::MISSING; 256]);
/// Pointer to the Interrupt Descriptor Table
pub struct IdtRef {
ptr: DescriptorTablePointer<IdtEntry>,
idt: &'static Mutex<[IdtEntry; 256]>,
}
unsafe impl Sync for IdtRef {}
impl IdtRef {
/// Creates a new pointer struct to the IDT.
pub fn new() -> IdtRef {
let r = IdtRef {
ptr: DescriptorTablePointer::new_idtp(&IDT.lock()[..]),
idt: &IDT,
};
// This block is safe because by referencing IDT above, we know that we've constructed an
// IDT.
unsafe { dtables::lidt(&r.ptr) };
r
}
/// Sets an IdtEntry as a handler for interrupt specified by `index`.
pub fn set_handler(&self, index: usize, entry: IdtEntry) {
self.idt.lock()[index] = entry;
}
/// Enables interrupts.
pub fn enable_interrupts(&self) {
// This unsafe fn is okay becuase, by virtue of having an IdtRef, we know that we have a
// valid Idt.
unsafe {
x86::shared::irq::enable();
}
}
}