-
Notifications
You must be signed in to change notification settings - Fork 0
/
apic.c
229 lines (189 loc) · 5.03 KB
/
apic.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
extern unsigned char inb(unsigned short port);
extern unsigned int ind(unsigned short port);
extern unsigned short inw(unsigned short port);
extern void outb(unsigned short port, unsigned char val);
extern void outd(unsigned short port, unsigned int val);
extern void outw(unsigned short port, unsigned short val);
extern void printki(long n);
extern void printkid(long n);
extern void printk(char* s);
extern void intq();
extern void testq();
extern void disable_pic();
extern void enable_ioapic_maybe();
extern void disable_int();
extern void enable_int();
extern void readthemsrthing();
unsigned int* ioapic1;
void write_ioapic(unsigned int regsel, unsigned int regwin)
{
*ioapic1 = regsel;
*(ioapic1 + 0x10) = regwin;
}
unsigned int read_ioapic(unsigned int regsel)
{
*(unsigned int*)(ioapic1) = regsel;
//printk("-- ");
//printki(((void*)ioapic1) + 0x10);
//printk(" --\n");
return *(unsigned int*)(((void*)ioapic1) + 0x10); // 10);
}
void setup_apic()
{
//disable_pic();
char ioapicnum = *((char*)0x5031);
printk("IOAPICNUM: ");
printki(ioapicnum);
printk("\n");
int j; int k;
if (ioapicnum > 0) {
printk("looking at i/o apic #1:\n");
ioapic1 = *((unsigned int**)0x5068);
printk("base addr ");
printki((long) ioapic1);
printk("\n");
printk("IOAPICID: ");
printki(read_ioapic(0x0));
printk("\nIOAPICVER: ");
printki(read_ioapic(0x1));
printk("\n");
for (j = 0; j < 24; j++) {
printk("IRQ ");
printkid(j);
printk(": ");
k = 0x10 + 2 * j;
printki((((long)read_ioapic(k + 1)) << 32) + ((long)read_ioapic(k)));
if ((j % 4) == 3) {
printk("\n");
} else {
printk(" ");
}
}
//printk("0x11:0x10 :: ");
//printki((((long)read_ioapic(0x11)) << 32) + ((long)read_ioapic(0x10)));
//printk("\n");
} else {
printk("No I/O APICs found. Not setting them up, obv.\n");
printk("If boot fails retry with an i/o apic\n");
}
printk("---------------------\n");
printk("Local APIC setup tiem\n");
printk("Spurious Interrupt Vector Register: ");
printki(*(unsigned int*)(0xfee000f0));
printk("\nLocal APIC Version Register: ");
unsigned int localApicVersion = *(unsigned int*)(0xfee00030);
unsigned int version = localApicVersion & 0xff;
unsigned int maxLvtEntry = (localApicVersion >> 16) & 0xff;
printki(*(unsigned int*)(0xfee00030));
printk("\n");
printk(" ^ version: ");
printki(version);
printk(" maxLvtEntry minus one: ");
printkid(maxLvtEntry);
printk("\n");
printk("LVT LINT0: ");
printki(*(unsigned int*)(0xfee00350));
printk("\n");
printk("LVT LINT1: ");
printki(*(unsigned int*)(0xfee00370));
printk("\n");
printk("int 0: ");
printki(*(unsigned long*)(0x0));
printk(" ");
printki(*(unsigned long*)(0x8));
printk("\nint 1: ");
printki(*(unsigned long*)(0x10));
printk(" ");
printki(*(unsigned long*)(0x18));
printk("\nint 33: ");
printki(*(unsigned long*)(0x10 * 33));
printk(" ");
printki(*(unsigned long*)(0x10 * 33 + 0x8));
printk("\nint 231: ");
printki(*(unsigned long*)(0x10 * 230));
printk(" ");
printki(*(unsigned long*)(0x10 * 230 + 0x8));
printk("\n");
// set up int 0x80
printk("setting up int 0x80\n");
unsigned long mn = (unsigned long)(&intq);
unsigned long p = (mn & 0xffff0000);
printk("p: ");
printki(p);
p = p >> 16;
printk("\np: ");
printki(p);
printk("\n");
printk("p<<48: ");
printki(p << 48);
printk("\n");
unsigned long l = ((mn & 0xffff) + (p << 48) + 0x80000 + 0x8f0000000000);
printki(l);
printk("\n");
//int x = 3 / 0;
printk("writing to ");
printki((unsigned long)(unsigned long*)(0x10 * 0x7f));
printk("\n");
//*(unsigned long*)(0x10 * 0x80) = l;
*(unsigned long*)(0x10 * 0x1) = l;
*(unsigned long*)(0x10 * 33) = l;
*(unsigned long*)(0x10 * 231) = l;
*(unsigned long*)(0x10 * 0x80) = l;
int x;
for (x = 0; x < 256; x++) {
*((unsigned long*)((long)(0x10 * x))) = l;
}
/*printk(" - ");
printki(*(unsigned long*)(0));
printk("\n - ");
*(unsigned long*)(0) = l;
printki(*(unsigned long*)(0));
printk("\n");*/
printk("intq: ");
printki((unsigned long)(&intq));
printk("\n");
*(char*)(0x200000) = 0;
printk("apic id for cpu core 0 ");
printki(*(char*)(0x5100));
printk("\n");
printk("PIC1_DATA ");
printki(inb(0x21));
printk("\n");
printk("PIC2_DATA ");
printki(inb(0xa1));
printk("\n");
// may as well. why the hell not(!)
outb(0x21, 0);
outb(0xa1, 0);
enable_ioapic_maybe();
enable_int();
printk("the msr thing says: ");
readthemsrthing();
printki(*(unsigned int*)(0x200000));
printk(" ");
printki(*(unsigned int*)(0x200004));
printk("\n");
// do test int 0x80
testq();
testq();
/*printk("in 0x200000: ");
printki(*(char*)(0x200000));
printk("\n");*/
for (j = 0; j < 24; j++) {
k = 0x10 + 2 * j;
//printki((((long)read_ioapic(k + 1)) << 32) + ((long)read_ioapic(k)));
write_ioapic(k, 0x21);
write_ioapic(k + 1, 0);
}
}
void setup_apic_notreally()
{
int j; int k;
printk("Setting up ioapic (but not really)\n");
for (j = 0; j < 24; j++) {
k = 0x10 + 2 * j;
//printki((((long)read_ioapic(k + 1)) << 32) + ((long)read_ioapic(k)));
write_ioapic(k, 0x21);
write_ioapic(k + 1, 0);
}
}