Skip to content

Commit fdb3a65

Browse files
finally make msi allocate vectors from a pool being as ahci now uses it if it can
1 parent f8e0b8b commit fdb3a65

File tree

5 files changed

+354
-16
lines changed

5 files changed

+354
-16
lines changed

asm/loader.S

Lines changed: 234 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// loader.S - safer, looped version with automatic gate count
2-
31
.code64
42
.extern Interrupt
53
.extern IRQ
@@ -10,10 +8,6 @@
108

119
.text
1210

13-
// Compute number of handlers by label arithmetic
14-
gate_count = (. - exception_handlers) / 8
15-
.global gate_count
16-
1711
idt_init:
1812
push %rdi
1913
push %rsi
@@ -22,7 +16,7 @@ idt_init:
2216
lea idt64(%rip), %r11
2317
mov 2(%r11), %rdi // load .base
2418

25-
mov $94, %rcx
19+
mov $256, %rcx // 256 total entries (0–255)
2620

2721
idt_loop:
2822
mov (%r10), %rax
@@ -49,7 +43,6 @@ idt_loop:
4943
pop %rdi
5044
retq
5145

52-
// Macros (unchanged)
5346
.macro ISR_NOERR isr
5447
isr\isr:
5548
MPUSHA
@@ -112,6 +105,7 @@ irq\num:
112105
.endm
113106

114107
isrs:
108+
// CPU exceptions 0–31
115109
ISR_NOERR 0
116110
ISR_NOERR 1
117111
ISR_NOERR 2
@@ -144,6 +138,8 @@ isrs:
144138
ISR_ERR 29
145139
ISR_ERR 30
146140
ISR_NOERR 31
141+
142+
// Hardware IRQs 32–47
147143
IRQ 0, 32
148144
IRQ 1, 33
149145
IRQ 2, 34
@@ -177,6 +173,204 @@ isrs:
177173
IRQ 28, 62
178174
IRQ 29, 63
179175

176+
// User-defined ISRs 64–255 (MSI/MSI-X)
177+
ISR_NOERR 64
178+
ISR_NOERR 65
179+
ISR_NOERR 66
180+
ISR_NOERR 67
181+
ISR_NOERR 68
182+
ISR_NOERR 69
183+
ISR_NOERR 70
184+
ISR_NOERR 71
185+
ISR_NOERR 72
186+
ISR_NOERR 73
187+
ISR_NOERR 74
188+
ISR_NOERR 75
189+
ISR_NOERR 76
190+
ISR_NOERR 77
191+
ISR_NOERR 78
192+
ISR_NOERR 79
193+
ISR_NOERR 80
194+
ISR_NOERR 81
195+
ISR_NOERR 82
196+
ISR_NOERR 83
197+
ISR_NOERR 84
198+
ISR_NOERR 85
199+
ISR_NOERR 86
200+
ISR_NOERR 87
201+
ISR_NOERR 88
202+
ISR_NOERR 89
203+
ISR_NOERR 90
204+
ISR_NOERR 91
205+
ISR_NOERR 92
206+
ISR_NOERR 93
207+
ISR_NOERR 94
208+
ISR_NOERR 95
209+
ISR_NOERR 96
210+
ISR_NOERR 97
211+
ISR_NOERR 98
212+
ISR_NOERR 99
213+
ISR_NOERR 100
214+
ISR_NOERR 101
215+
ISR_NOERR 102
216+
ISR_NOERR 103
217+
ISR_NOERR 104
218+
ISR_NOERR 105
219+
ISR_NOERR 106
220+
ISR_NOERR 107
221+
ISR_NOERR 108
222+
ISR_NOERR 109
223+
ISR_NOERR 110
224+
ISR_NOERR 111
225+
ISR_NOERR 112
226+
ISR_NOERR 113
227+
ISR_NOERR 114
228+
ISR_NOERR 115
229+
ISR_NOERR 116
230+
ISR_NOERR 117
231+
ISR_NOERR 118
232+
ISR_NOERR 119
233+
ISR_NOERR 120
234+
ISR_NOERR 121
235+
ISR_NOERR 122
236+
ISR_NOERR 123
237+
ISR_NOERR 124
238+
ISR_NOERR 125
239+
ISR_NOERR 126
240+
ISR_NOERR 127
241+
ISR_NOERR 128
242+
ISR_NOERR 129
243+
ISR_NOERR 130
244+
ISR_NOERR 131
245+
ISR_NOERR 132
246+
ISR_NOERR 133
247+
ISR_NOERR 134
248+
ISR_NOERR 135
249+
ISR_NOERR 136
250+
ISR_NOERR 137
251+
ISR_NOERR 138
252+
ISR_NOERR 139
253+
ISR_NOERR 140
254+
ISR_NOERR 141
255+
ISR_NOERR 142
256+
ISR_NOERR 143
257+
ISR_NOERR 144
258+
ISR_NOERR 145
259+
ISR_NOERR 146
260+
ISR_NOERR 147
261+
ISR_NOERR 148
262+
ISR_NOERR 149
263+
ISR_NOERR 150
264+
ISR_NOERR 151
265+
ISR_NOERR 152
266+
ISR_NOERR 153
267+
ISR_NOERR 154
268+
ISR_NOERR 155
269+
ISR_NOERR 156
270+
ISR_NOERR 157
271+
ISR_NOERR 158
272+
ISR_NOERR 159
273+
ISR_NOERR 160
274+
ISR_NOERR 161
275+
ISR_NOERR 162
276+
ISR_NOERR 163
277+
ISR_NOERR 164
278+
ISR_NOERR 165
279+
ISR_NOERR 166
280+
ISR_NOERR 167
281+
ISR_NOERR 168
282+
ISR_NOERR 169
283+
ISR_NOERR 170
284+
ISR_NOERR 171
285+
ISR_NOERR 172
286+
ISR_NOERR 173
287+
ISR_NOERR 174
288+
ISR_NOERR 175
289+
ISR_NOERR 176
290+
ISR_NOERR 177
291+
ISR_NOERR 178
292+
ISR_NOERR 179
293+
ISR_NOERR 180
294+
ISR_NOERR 181
295+
ISR_NOERR 182
296+
ISR_NOERR 183
297+
ISR_NOERR 184
298+
ISR_NOERR 185
299+
ISR_NOERR 186
300+
ISR_NOERR 187
301+
ISR_NOERR 188
302+
ISR_NOERR 189
303+
ISR_NOERR 190
304+
ISR_NOERR 191
305+
ISR_NOERR 192
306+
ISR_NOERR 193
307+
ISR_NOERR 194
308+
ISR_NOERR 195
309+
ISR_NOERR 196
310+
ISR_NOERR 197
311+
ISR_NOERR 198
312+
ISR_NOERR 199
313+
ISR_NOERR 200
314+
ISR_NOERR 201
315+
ISR_NOERR 202
316+
ISR_NOERR 203
317+
ISR_NOERR 204
318+
ISR_NOERR 205
319+
ISR_NOERR 206
320+
ISR_NOERR 207
321+
ISR_NOERR 208
322+
ISR_NOERR 209
323+
ISR_NOERR 210
324+
ISR_NOERR 211
325+
ISR_NOERR 212
326+
ISR_NOERR 213
327+
ISR_NOERR 214
328+
ISR_NOERR 215
329+
ISR_NOERR 216
330+
ISR_NOERR 217
331+
ISR_NOERR 218
332+
ISR_NOERR 219
333+
ISR_NOERR 220
334+
ISR_NOERR 221
335+
ISR_NOERR 222
336+
ISR_NOERR 223
337+
ISR_NOERR 224
338+
ISR_NOERR 225
339+
ISR_NOERR 226
340+
ISR_NOERR 227
341+
ISR_NOERR 228
342+
ISR_NOERR 229
343+
ISR_NOERR 230
344+
ISR_NOERR 231
345+
ISR_NOERR 232
346+
ISR_NOERR 233
347+
ISR_NOERR 234
348+
ISR_NOERR 235
349+
ISR_NOERR 236
350+
ISR_NOERR 237
351+
ISR_NOERR 238
352+
ISR_NOERR 239
353+
ISR_NOERR 240
354+
ISR_NOERR 241
355+
ISR_NOERR 242
356+
ISR_NOERR 243
357+
ISR_NOERR 244
358+
ISR_NOERR 245
359+
ISR_NOERR 246
360+
ISR_NOERR 247
361+
ISR_NOERR 248
362+
ISR_NOERR 249
363+
ISR_NOERR 250
364+
ISR_NOERR 251
365+
ISR_NOERR 252
366+
ISR_NOERR 253
367+
ISR_NOERR 254
368+
ISR_NOERR 255
369+
370+
// ----------------------
371+
// Common stubs
372+
// ----------------------
373+
180374
irq_stub:
181375
call IRQ
182376
MPOPA
@@ -194,9 +388,40 @@ exception_handlers:
194388
.quad isr24, isr25, isr26, isr27, isr28, isr29, isr30, isr31
195389
.quad irq0, irq1, irq2, irq3, irq4, irq5, irq6, irq7
196390
.quad irq8, irq9, irq10, irq11, irq12, irq13, irq14, irq15
391+
392+
// IRQ remaps for IOAPIC
197393
.quad isr48, isr49
198394
.quad irq16, irq17, irq18, irq19, irq20, irq21, irq22, irq23
199395
.quad irq24, irq25, irq26, irq27, irq28, irq29
200396

201-
.section .note.GNU-stack
397+
// Vectors 64–255
398+
.quad isr64, isr65, isr66, isr67, isr68, isr69, isr70, isr71
399+
.quad isr72, isr73, isr74, isr75, isr76, isr77, isr78, isr79
400+
.quad isr80, isr81, isr82, isr83, isr84, isr85, isr86, isr87
401+
.quad isr88, isr89, isr90, isr91, isr92, isr93, isr94, isr95
402+
.quad isr96, isr97, isr98, isr99, isr100, isr101, isr102, isr103
403+
.quad isr104, isr105, isr106, isr107, isr108, isr109, isr110, isr111
404+
.quad isr112, isr113, isr114, isr115, isr116, isr117, isr118, isr119
405+
.quad isr120, isr121, isr122, isr123, isr124, isr125, isr126, isr127
406+
.quad isr128, isr129, isr130, isr131, isr132, isr133, isr134, isr135
407+
.quad isr136, isr137, isr138, isr139, isr140, isr141, isr142, isr143
408+
.quad isr144, isr145, isr146, isr147, isr148, isr149, isr150, isr151
409+
.quad isr152, isr153, isr154, isr155, isr156, isr157, isr158, isr159
410+
.quad isr160, isr161, isr162, isr163, isr164, isr165, isr166, isr167
411+
.quad isr168, isr169, isr170, isr171, isr172, isr173, isr174, isr175
412+
.quad isr176, isr177, isr178, isr179, isr180, isr181, isr182, isr183
413+
.quad isr184, isr185, isr186, isr187, isr188, isr189, isr190, isr191
414+
.quad isr192, isr193, isr194, isr195, isr196, isr197, isr198, isr199
415+
.quad isr200, isr201, isr202, isr203, isr204, isr205, isr206, isr207
416+
.quad isr208, isr209, isr210, isr211, isr212, isr213, isr214, isr215
417+
.quad isr216, isr217, isr218, isr219, isr220, isr221, isr222, isr223
418+
.quad isr224, isr225, isr226, isr227, isr228, isr229, isr230, isr231
419+
.quad isr232, isr233, isr234, isr235, isr236, isr237, isr238, isr239
420+
.quad isr240, isr241, isr242, isr243, isr244, isr245, isr246, isr247
421+
.quad isr248, isr249, isr250, isr251, isr252, isr253, isr254, isr255
422+
423+
// Total count
424+
gate_count = (. - exception_handlers) / 8
425+
.global gate_count
202426

427+
.section .note.GNU-stack

include/idt.h

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,82 @@ typedef struct idt_entry_t {
2626

2727
extern volatile idt_ptr_t idt64;
2828

29-
void init_idt();
29+
/**
30+
* @brief Initialise the IDT and enable interrupts.
31+
*
32+
* Sets up the full 256-entry Interrupt Descriptor Table (IDT),
33+
* remaps or disables the legacy PIC depending on configuration,
34+
* configures the PIT to 50Hz (if APIC timer is not used), and
35+
* loads the IDT register. CPU interrupts are enabled at the end.
36+
*
37+
* @note Called once during kernel bootstrap. Drivers must not
38+
* call this directly.
39+
*/
40+
void init_idt(void);
3041

31-
/* Function body defined in asm/loader.S */
42+
/**
43+
* @brief Low-level assembly routine to populate the IDT.
44+
*
45+
* Fills the IDT with ISR and IRQ stub addresses defined in
46+
* loader.S.
47+
*
48+
* @param idt Pointer to the IDT base.
49+
*
50+
* @note Invoked internally by init_idt(). Not for driver use.
51+
*/
3252
void idt_init(void* idt);
3353

54+
/**
55+
* @brief Send End-of-Interrupt (EOI) to the PIC.
56+
*
57+
* Notifies the PIC(s) that interrupt handling has finished.
58+
* For IRQs >= 8, signals both slave and master PICs.
59+
*
60+
* @param irq The IRQ line number (0–15).
61+
*
62+
* @note Normally issued automatically by the kernel after
63+
* invoking registered handlers. Only call directly when
64+
* writing low-level PIC-specific code.
65+
*/
3466
void pic_eoi(int irq);
3567

36-
void io_wait();
68+
/**
69+
* @brief Small I/O delay for hardware synchronisation.
70+
*
71+
* Performs an I/O write to port 0x80 to allow hardware devices
72+
* (such as the PIC) time to settle after reprogramming.
73+
*
74+
* @note Typically only needed by low-level kernel code. Most
75+
* drivers do not need to call this.
76+
*/
77+
void io_wait(void);
78+
79+
/**
80+
* @brief Allocate a free MSI interrupt vector.
81+
*
82+
* Allocates an IDT vector in the range 64–255 for use with
83+
* Message Signalled Interrupts (MSI/MSI-X).
84+
*
85+
* @return Vector number (64–255) on success,
86+
* -1 if no free vector is available.
87+
*
88+
* @note After allocation, the driver must program the device’s
89+
* MSI capability structure and register an interrupt
90+
* handler for the vector.
91+
*/
92+
int alloc_msi_vector(void);
93+
94+
/**
95+
* @brief Free a previously allocated MSI interrupt vector.
96+
*
97+
* Marks the given MSI vector as available for reuse.
98+
*
99+
* @param vec The MSI vector to free (64–255).
100+
*
101+
* @warning Behaviour is undefined if freeing a vector that
102+
* was never allocated or is still in use.
103+
*
104+
* @note Drivers should call this during teardown to avoid
105+
* leaking interrupt vectors.
106+
*/
107+
void free_msi_vector(int vec);

0 commit comments

Comments
 (0)