-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx2apic.c
310 lines (250 loc) · 8.08 KB
/
x2apic.c
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "utils.h"
#include "cpuid.h"
#include "acpi.h"
#include "vm.h"
#define MAXCPUS 0xff
#define PIC_8259_SLAVE_DATA 0xa1
#define PIC_8259_MASTER_DATA 0x21
// manual https://www.naic.edu/~phil/software/intel/318148.pdf
#define IA32_APIC_BASE_ADDR 0x1b
#define IA32_APIC_BASE_BSP 0x8
#define X2APIC_OFFSET 0x800
#define LOCAL_X2APIC_ID_REG 0x02
#define LOGICAL_X2APIC_ID_REG 0x0d
#define LOCAL_X2APIC_VERSION_REG 0x03
#define SVR_X2APIC_REG 0x0f
#define LOCAL_X2API_ICR 0x30
#define LOCAL_X2API_ESR 0x28
enum interrupt_ctl_types { processor_local_apic, io_apic, interrupt_source_override, nmi_source, local_apic_nmi };
struct cpuLocalAPIC {
u32_t flags;
u8_t apic_ID;
u8_t apic_cpu_UID;
u8_t length;
u8_t type;
};
struct IOAPIC {
u32_t gsib; /* global system interrupt base */
u32_t io_apic_addr;
u8_t reserved;
u8_t io_apic_ID;
u8_t length;
u8_t type;
};
bool x2APICmode = false;
u64_t r_IA32apic_base() {
return _rdmsr(IA32_APIC_BASE_ADDR);
}
void w_IA32apic_base(u64_t reg) {
return _wrmsr(IA32_APIC_BASE_ADDR, reg & 0xffffffff00000000, reg & 0x00000000ffffffff);
}
u64_t r_LOCALVersion_x2APIC_reg() {
return _rdmsr(X2APIC_OFFSET + LOCAL_X2APIC_VERSION_REG);
}
u32_t r_LOCAL_x2APIC_ID_reg() {
return _rdmsr(X2APIC_OFFSET + LOCAL_X2APIC_ID_REG);
}
u32_t r_LOGICAL_x2APIC_ID_reg() {
return _rdmsr(X2APIC_OFFSET + LOGICAL_X2APIC_ID_REG);
}
u64_t r_SVR_x2APIC_reg() {
return _rdmsr(X2APIC_OFFSET + SVR_X2APIC_REG);
}
u64_t w_SVR_x2APIC_reg(u64_t reg) {
return _wrmsr(X2APIC_OFFSET + SVR_X2APIC_REG, reg & 0xffffffff00000000, reg & 0x00000000ffffffff);
}
bool x2APIC_isBSP() {
return check_bit(r_IA32apic_base(), IA32_APIC_BASE_BSP);
}
bool enable_x2APICmode() {
if (cpuid_feature(CPUID_ECX, CPU_flag_x2apic)) {
u64_t IA32_APIC_BASE = r_IA32apic_base();
if (check_bit(IA32_APIC_BASE, 11)) {
// disable 8259PIC
PIC_8259A_shutdown();
// enable x2apic
w_IA32apic_base(set_bit(IA32_APIC_BASE, 10, true));
IA32_APIC_BASE = r_IA32apic_base();
// check EXTD and EN bits
if (check_bit(IA32_APIC_BASE, 10) && check_bit(IA32_APIC_BASE, 11))
x2APICmode = true;
}
else
x2APICmode = false;
}
return x2APICmode;
}
// disable 8259PIC.
void PIC_8259A_shutdown() {
_outb(PIC_8259_SLAVE_DATA, 0xff);
_outb(PIC_8259_MASTER_DATA, 0xff);
}
bool cpuEnabled(struct cpuLocalAPIC *clapic) {
if (clapic->type == processor_local_apic)
if (check_bit(clapic->flags, 0))
return true;
return false;
}
int loadLocalAPIC(struct acpi_RSDheader *madt, struct cpuLocalAPIC clapic[]) {
if (strncmp(madt->rsd_signature, MADT_SIG, strlen(MADT_SIG) ) != 0)
return -1;
u64_t *b = (u32_t*) madt->entry;
u8_t n = 0;
while (((*b >> 32) & 0xff) == processor_local_apic)
{
clapic[n].apic_cpu_UID = (*b >> 48) & 0xff;
clapic[n].apic_ID = (*b >> 56) & 0xff;
clapic[n].length = (*b >> 40) & 0xff;
clapic[n].type = (*b >> 32) & 0xff;
clapic[n].flags = *b & 0xffffffff;
n++; b++;
}
return 0;
}
int numCPULocalAPIC(struct acpi_RSDheader *madt) {
u64_t *b = (u32_t*) madt->entry;
u8_t n = 0;
if (strncmp(madt->rsd_signature, MADT_SIG, strlen(MADT_SIG) ) != 0)
return -1;
while (((*b++ >> 32) & 0xff) == processor_local_apic)
n++;
return n;
}
int loadIOAPIC(struct acpi_RSDheader *madt, struct IOAPIC *ioapic) {
if (strncmp(madt->rsd_signature, MADT_SIG, strlen(MADT_SIG) ) != 0)
return -1;
u64_t *b = (u32_t*) (madt->entry); // ((*madt->entry >> 40) & 0xff);
b += numCPULocalAPIC(madt);
ioapic->type = (*b >> 32) & 0xff;
ioapic->length = (*b >> 40) & 0xff;
ioapic->io_apic_ID = (*b >> 48) & 0xff;
ioapic->reserved = 0;
ioapic->io_apic_addr = *(++b) & 0xffffffff;
ioapic->gsib = (*b >> 32) & 0xffffffff;
return 0;
}
void testa() {
kprintf("\n -- ACPI RSDptr\n");
struct acpi_RSDptr *rsdp = NULL;
rsdp = acpi_RSDP_table(rsdp);
kprintf("xsdt_addr: 0x%x, rsdt_addr: 0x%x, rsd_signature: %s", rsdp->xsdt_addr, rsdp->rsdt_addr, rsdp->rsd_signature);
map_addr((u32_t*)rsdp->rsdt_addr);
volatile struct acpi_RSDheader *rsdheader = rsdp->rsdt_addr;
volatile struct acpi_RSDheader *madt = (u32_t*) (rsdheader->entry[1]);
volatile struct u32_t *svradd = (u32_t*) 0xfee000f0;
map_addr(svradd);
map_addr(madt);
// test
//map_addr(0xc0000000);
map_addr(0xf1001000);
map_addr(0xd199ff1f);
map_addr(0xc0000000);
/*
kprint("\n");
kprint(rsdheader->rsd_signature[1]);
kprint("\n");
kprint(itoa(rsdheader->length,16));
kprint("\n");
kprint(itoa(rsdheader->revision,16));
kprint("\n");
kprint(rsdheader->OEM_ID);
kprint("\n");
kprint(rsdheader->OEM_TABLE_ID);
kprint("\n");
kprint(rsdheader->creator_ID);
kprint("\n");
kprint(itoa(rsdheader->entry,16));
kprint("\n");
kprint(itoa(sizeof(rsdheader->entry),16));
*/
kprintf("\n -- LOCAL APIC\n");
// can change the SVR vector without problems.
/*
u32_t svr = r_SVR_x2APIC_reg();
kprint("\nSVR = ");
kprint(itoa(svr, 2));
kprint("\nSVR = ");
svr = set_bit(svr,5,0);
kprint(itoa(svr, 16));
w_SVR_x2APIC_reg(svr);
*/
struct cpuLocalAPIC local_apic[16];
loadLocalAPIC(madt, local_apic);
int ncpu = numCPULocalAPIC(madt);
/*
kprint("\nnumber of CPUs: ");
kprint(itoa(ncpu,16));
for (int i = 0; i < ncpu; i++)
{
kprint("\n");
kprint(" [ cpu_uid: ");
kprint(itoa(local_apic[i].apic_cpu_UID,16));
kprint(", apic_id: ");
kprint(itoa(local_apic[i].apic_ID,16));
if (cpuEnabled(&local_apic[i]))
kprint(" ] enabled");
else
kprint(" ]");
}
kprint("\n -- IOAPIC\n");
*/
struct IOAPIC *ioapic;
loadIOAPIC(madt, ioapic);
kprintf("id: 0x%x, type: 0x%x, length: 0x%x, io_apic_addr: 0x%x, gsib: 0x%x\n",
ioapic->io_apic_ID,
ioapic->type,
ioapic->length,
ioapic->io_apic_addr,
ioapic->gsib);
kprintf("yolt: boostrap processor: %d\n", x2APIC_isBSP());
u64_t msrlapic = _rdmsr(0x800 + 0x30);
kprintf("%s -> ", itoa(msrlapic, 2));
// vector at address 0x0f -> 0x0000f000
msrlapic = set_bit(msrlapic, 8, 1);
msrlapic = set_bit(msrlapic, 9, 0);
msrlapic = set_bit(msrlapic, 10, 1);
msrlapic = set_bit(msrlapic, 14, 0); // level : de-assert
msrlapic = set_bit(msrlapic, 15, 1); // trigger mode: level
kprintf("%s\n", itoa(msrlapic, 2));
//kprint(itoa(msrlapic, 2));
kprintf("IIPI");
_wrmsr(0x800+0x30, msrlapic);
u64_t msrlapic2 = _rdmsr(0x800 + 0x30);
kprintf(itoa(msrlapic2, 2));
msrlapic = set_bit(msrlapic, 8, 0);
msrlapic = set_bit(msrlapic, 9, 1);
msrlapic = set_bit(msrlapic, 10, 1);
msrlapic = set_bit(msrlapic, 14, 1); // level : assert
msrlapic = set_bit(msrlapic, 15, 0); // tigger mode: edge
kprintf("\n");
kprintf(itoa(msrlapic, 2));
kprintf("\nSIPI");
_wrmsr(0x800+0x30, msrlapic);
kprintf("\nSIPI\n");
_wrmsr(0x800+0x30, msrlapic);
/*
w = 300000;
while( w > 0 ){
w--;
};
*/
memset(msrlapic2, 0, sizeof(msrlapic2));
u64_t msrlapic3 = _rdmsr(0x800 + 0x28);
kprintf(itoa(msrlapic3, 2));
//kprint("\n");
//u64_t svr = r_SVR_x2APIC_reg();
//kprint(itoa(svr, 2));
/*
for (int i=0 ; i < 144; i++)
pt_bitmap_set(i,true);
pt_bitmap_set(33,true);
pt_bitmap_set(8,true);
pt_bitmap_set(11,true);
pt_bitmap_set(71,false);
pt_bitmap_set(71,true);
*/
//pt_bitmap_set(0,true);
for (int i=0 ; i < 32; i++)
kprintf(itoa(pt_bitmap_get(i),10));
flush_pt(0x73000);
}