summaryrefslogtreecommitdiff
path: root/kernel/src/interrupts/apic.rs
blob: 836af200ec5b89928c5748d822237407e8b283c3 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use core::sync::atomic::{AtomicU64, Ordering};
use x86_64::registers::model_specific::Msr;

#[cfg(target_arch = "x86_64")]
const APIC_FLAG: u64 = 0x0800;
const APIC_BASE_MSR: u32 = 0x001b;
const CPU_COUNT: usize = 255;
const APIC_BASE_ADDR: u64 = 0xfee0_0000;

// APIC constant register values
const APIC_DISABLE: u32 = 0x0001_0000;
const APIC_NMI: u32 = 0x0000_0400;

pub fn is_x2apic() -> bool {
    let info: u32;
    unsafe { asm!("cpuid", inout("eax") 1 => _, out("ecx") info) }
    info & (1 << 21) != 0
}

pub fn is_apic() -> bool {
    let info: u32;
    unsafe { asm!("cpuid", inout("eax") 1 => _, out("edx") info) }
    info & (1 << 9) != 0
}

pub fn cpu_id() -> u32 {
    let mut info: u32;
    unsafe { asm!("cpuid", inout("eax") 1 => _, out("ebx") info) }
    if !is_x2apic() {
        info >>= 24;
    }
    info
}

#[repr(C)]
pub struct InterruptCommand {
    low: u32,
    _rsv: [u32; 3],
    high: u32,
}

#[repr(isize)]
pub enum ApicRegister {
    ApicId = 8,
    ApicVer = 12,
    TaskPriority = 32,
    ArbitrationPriority = 36,
    ProcessorPriority = 40,
    EndOfInterrupt = 44,
    LogicalDst = 52,
    DstFmt = 56,
    SpuriousInterruptVec = 60,
    InService = 64,
    TriggerMode = 96,
    InterruptRequest = 128,
    ErrorStatus = 160,
    InterruptCommand = 192,
    InterruptCommandHigh = 196,
    LvtTimer = 200,
    LvtThermalSensor = 204,
    LvtPerformanceMonitor = 208,
    LvtLint0 = 212,
    LvtLint1 = 216,
    LvtError = 220,
    TimerInitialCount = 224,
    TimerCurrentCount = 228,
    TimerDivideConfig = 248,
    ExtApicFeature = 256,
    ExtApicControl = 260,
    SpecificEndOfInterrupt = 264,
    InterruptEnable = 288,
    ExtInterruptLocalVectorTable = 320,
}

#[repr(u32)]
pub enum TimerDivideConfig {
    Div2 = 0,
    Div4 = 1,
    Div8 = 2,
    Div16 = 3,
    Div32 = 8,
    Div64 = 9,
    Div128 = 10,
    NoDivide = 11,
}
const NONE_APIC: Option<Apic> = None;

static mut APIC_INIT: [Option<Apic>; CPU_COUNT] = [NONE_APIC; CPU_COUNT];

pub fn move_local_apic(v: u64) -> bool {
    match get_local_apic() {
        None => return false,
        Some(apic) => apic.regs.store(v, Ordering::Release),
    }
    unsafe { Msr::new(APIC_BASE_MSR).write(v) };
    true
}

/// Tries to initialize the local apic at it's default location.
/// returns false if no apic is available
///
/// # Safety
/// Calling this function in a running system might cause the loss of interrupts or other weird
/// behaviour
pub unsafe fn try_initialize_local_apic() -> &'static mut Option<Apic> {
    if is_apic() {
        let cpu_id = cpu_id() as usize;
        if APIC_INIT[cpu_id].is_none() {
            APIC_INIT[cpu_id] = Some(Apic::from_base_apic(APIC_BASE_ADDR));
            Apic::disable_pic();
        }
        APIC_INIT[cpu_id].as_mut().map(|x| x.initialize());
    }
    get_local_apic()
}

pub fn get_local_apic() -> &'static mut Option<Apic> {
    let cpu_id = cpu_id() as usize;
    unsafe { &mut APIC_INIT[cpu_id] }
}

pub struct Apic {
    regs: AtomicU64,
}

impl Apic {
    fn initialize(&mut self) -> Self {
        Self::disable_pic();
        let base_apic = unsafe { Msr::new(APIC_BASE_MSR).read() };
        let mut apic = Self::from_base_apic(base_apic);

        apic.set(ApicRegister::LogicalDst, 0);

        apic.set(ApicRegister::TaskPriority, 0);
        apic.set(ApicRegister::LvtTimer, APIC_DISABLE);
        apic.set(ApicRegister::LvtPerformanceMonitor, APIC_NMI);
        apic.set(ApicRegister::LvtLint0, APIC_DISABLE);
        apic.set(ApicRegister::LvtLint1, APIC_DISABLE);
        apic.set(ApicRegister::DstFmt, 0x0FFFFFFF);
        apic.set(
            ApicRegister::LvtError,
            super::InterruptType::ApicError.as_u8().into(),
        );
        apic.set(ApicRegister::ErrorStatus, 0);

        unsafe { Msr::new(APIC_BASE_MSR).write(base_apic | APIC_FLAG) };

        apic.set(ApicRegister::SpuriousInterruptVec, 0x1ff);

        apic
    }

    fn disable_pic() {
        unsafe {
            x86_64::instructions::port::PortWriteOnly::new(0xa1).write(0xffu8);
            x86_64::instructions::port::PortWriteOnly::new(0x21).write(0xffu8);
        }
    }

    const fn from_base_apic(base_apic: u64) -> Self {
        Self {
            regs: AtomicU64::new((base_apic) & 0xfffffffff << 12),
        }
    }

    pub fn ptr(&self) -> *const u32 {
        self.regs.load(Ordering::Acquire) as *const u32
    }

    fn ptr_mut(&mut self) -> *mut u32 {
        self.regs.load(Ordering::SeqCst) as *mut u32
    }

    pub fn get(&self, register: ApicRegister) -> u32 {
        unsafe { self.ptr().offset(register as isize).read_volatile() }
    }

    pub fn set(&mut self, register: ApicRegister, val: u32) {
        unsafe { self.ptr_mut().offset(register as isize).write_volatile(val) }
    }

    pub fn set_interrupt_command(&mut self, dst: u8, v: u8) {
        let (dst, v): (u32, u32) = (dst.into(), v.into());
        self.set(ApicRegister::InterruptCommandHigh, dst << 24);
        self.set(ApicRegister::InterruptCommand, v)
    }

    pub fn end_of_interrupt(&mut self) {
        self.set(ApicRegister::EndOfInterrupt, 0);
    }

    pub fn set_timer_interrupt_handler(
        &mut self,
        divide: TimerDivideConfig,
        intr: super::InterruptType,
    ) {
        self.set(ApicRegister::LvtTimer, intr.as_u8() as u32 | (1 << 17));
        self.set(ApicRegister::TimerDivideConfig, divide as u32);
        self.set(ApicRegister::TimerInitialCount, 1 << 27);
    }

    pub fn get_timer_value(&self) -> u32 {
        self.get(ApicRegister::TimerCurrentCount)
    }

    pub fn get_error_code(&mut self) -> u32 {
        self.set(ApicRegister::ErrorStatus, 0);
        // write contents of apic error register to memory
        self.get(ApicRegister::ErrorStatus)
    }

    pub fn get_id(&self) -> u32 {
        self.get(ApicRegister::ApicId)
    }
}