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
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! APIC-related definitions.

use bitfield_struct::bitfield;
use open_enum::open_enum;

/// The physical address of the APIC at reset.
pub const APIC_BASE_ADDRESS: u32 = 0xfee00000;
/// The 4KB page number of the physical address of the APIC at reset.
pub const APIC_BASE_PAGE: u32 = APIC_BASE_ADDRESS >> 12;

pub const APIC_LEGACY_ID_COUNT: u32 = 255;

/// The APIC base MSR.
#[bitfield(u64)]
pub struct ApicBase {
    _reserved: u8,
    /// True if this processor is the BSP.
    pub bsp: bool,
    _reserved2: bool,
    pub x2apic: bool,
    pub enable: bool,
    /// The page number of the APIC (usually APIC_BASE_PAGE).
    #[bits(24)]
    pub base_page: u32,
    #[bits(28)]
    _reserved3: u64,
}

/// Spurious vector register.
#[bitfield(u32)]
pub struct Svr {
    pub vector: u8,
    pub enable: bool,
    pub focus_processor_checking: bool,
    #[bits(2)]
    _rsvd: u32,
    pub eoi_broadcast_suppression: bool,
    #[bits(19)]
    _rsvd2: u32,
}

/// Local vector table
#[bitfield(u32)]
pub struct Lvt {
    pub vector: u8,
    #[bits(3)]
    pub delivery_mode: u8,
    _rsvd: bool,
    pub delivery_status: bool,
    pub input_pin_polarity: bool,
    pub remote_irr: bool,
    pub trigger_mode_level: bool,
    pub masked: bool,
    #[bits(2)]
    pub timer_mode: u8,
    #[bits(13)]
    _rsvd2: u32,
}

open_enum! {
    pub enum TimerMode: u8 {
        ONE_SHOT = 0,
        PERIODIC = 1,
        TSC_DEADLINE = 2,
    }
}

#[bitfield(u32)]
pub struct Dcr {
    #[bits(2)]
    pub value_low: u8,
    _rsvd: bool,
    #[bits(1)]
    pub value_high: u8,
    #[bits(28)]
    _rsvd2: u32,
}

open_enum! {
    pub enum Dfr: u32 {
        FLAT_MODE = 0xffff_ffff,
        CLUSTERED_MODE = 0x0fff_ffff,
    }
}

#[bitfield(u64)]
pub struct Icr {
    pub vector: u8,
    #[bits(3)]
    pub delivery_mode: u8,
    pub destination_mode_logical: bool,
    pub delivery_pending: bool,
    pub reserved1: bool,
    pub level_assert: bool,
    pub trigger_mode_level: bool,
    #[bits(2)]
    pub remote_read_status: u8,
    #[bits(2)]
    pub destination_shorthand: u8,
    #[bits(12)]
    pub reserved3: u16,
    pub x2apic_mda: u32,
}

impl Icr {
    pub const fn xapic_mda(&self) -> u8 {
        (self.x2apic_mda() >> 24) as u8
    }

    pub fn set_xapic_mda(&mut self, value: u8) {
        *self = self.with_xapic_mda(value);
    }

    pub const fn with_xapic_mda(self, value: u8) -> Self {
        self.with_x2apic_mda((value as u32) << 24)
    }
}

#[bitfield(u32)]
pub struct X2ApicLogicalId {
    pub logical_id: u16,
    pub cluster_id: u16,
}

#[bitfield(u8)]
pub struct XApicClusterLogicalId {
    #[bits(4)]
    pub logical_id: u8,
    #[bits(4)]
    pub cluster_id: u8,
}

open_enum! {
    pub enum DeliveryMode: u8 {
        FIXED = 0,
        LOWEST_PRIORITY = 1,
        SMI = 2,
        REMOTE_READ = 3,
        NMI = 4,
        INIT = 5,
        SIPI = 6,
        EXTINT = 7,
    }
}

open_enum! {
    pub enum DestinationShorthand: u8 {
        NONE = 0,
        SELF = 1,
        ALL_INCLUDING_SELF = 2,
        ALL_EXCLUDING_SELF = 3,
    }
}

open_enum! {
    pub enum ApicRegister: u8 {
        ID = 0x2,               // RW
        VERSION = 0x3,          // RO
        TPR = 0x8,              // RW
        APR = 0x9,              // RO
        PPR = 0xa,              // RO
        EOI = 0xb,              // WO
        RRD = 0xc,              // RO
        LDR = 0xd,              // RW
        DFR = 0xe,              // RW
        SVR = 0xf,              // RW
        ISR0 = 0x10,            // RO
        ISR1 = 0x11,
        ISR2 = 0x12,
        ISR3 = 0x13,
        ISR4 = 0x14,
        ISR5 = 0x15,
        ISR6 = 0x16,
        ISR7 = 0x17,
        TMR0 = 0x18,            // RO
        TMR1 = 0x19,
        TMR2 = 0x1a,
        TMR3 = 0x1b,
        TMR4 = 0x1c,
        TMR5 = 0x1d,
        TMR6 = 0x1e,
        TMR7 = 0x1f,
        IRR0 = 0x20,            // RO
        IRR1 = 0x21,
        IRR2 = 0x22,
        IRR3 = 0x23,
        IRR4 = 0x24,
        IRR5 = 0x25,
        IRR6 = 0x26,
        IRR7 = 0x27,
        ESR = 0x28,             // RW
        INTEL_LVT_CMCI = 0x2f,  // RW
        ICR0 = 0x30,            // RW
        ICR1 = 0x31,            // RW, XAPIC only
        LVT_TIMER = 0x32,       // RW
        LVT_THERMAL = 0x33,     // RW
        LVT_PMC = 0x34,         // RW
        LVT_LINT0 = 0x35,       // RW
        LVT_LINT1 = 0x36,       // RW
        LVT_ERROR = 0x37,       // RW
        TIMER_ICR = 0x38,       // RW
        TIMER_CCR = 0x39,       // RO
        TIMER_DCR = 0x3e,       // RW
        SELF_IPI = 0x3f,        // WO, X2APIC only
    }
}

pub const X2APIC_MSR_BASE: u32 = 0x800;
pub const X2APIC_MSR_END: u32 = 0x83f;

impl ApicRegister {
    pub fn x2apic_msr(&self) -> u32 {
        X2APIC_MSR_BASE + self.0 as u32
    }
}

#[bitfield(u32)]
pub struct ApicVersion {
    pub version: u8,
    _rsvd: u8,
    pub max_lvt_entry: u8,
    pub eoi_broadcast_suppression: bool,
    #[bits(7)]
    _rsvd: u8,
}