Skip to content

Commit aa7a783

Browse files
committed
Lesson 23, step 6
1 parent d9ee254 commit aa7a783

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed

23-fixes/README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ C99 introduces standard fixed-width data types like `uint32_t`
3939
We need to include `<stdint.h>` which works even in `-ffreestanding` (but requires stdlibs)
4040
and use those data types instead of our own, then delete them on `type.h`
4141

42+
We also delete the underscores around `__asm__` and `__volatile__` since they aren't needed.
43+
44+
4245
4. Improperly aligned `kmalloc`
4346
-------------------------------
4447

@@ -53,12 +56,24 @@ For now, it will always return a new page-aligned memory block.
5356
5. Missing functions
5457
--------------------
5558

59+
We will implement the missing `mem*` functions in following lessons
60+
61+
5662
6. Interrupt handlers
5763
---------------------
58-
- also cli, sti in interrupt handlers
64+
`cli` is redundant, since we already established on the IDT entries if interrupts
65+
are enabled within a handler using the `idt_gate_t` flags.
66+
67+
`sti` is also redundant, as `iret` loads the eflags value from the stack, which contains a
68+
bit telling whether interrupts are on or off.
69+
In other words the interrupt handler automatically restores interrupts whether or not
70+
interrupts were enabled before this interrupt
5971

6072

61-
7. Structs and attributes
62-
-------------------------
73+
On `cpu/isr.h`, `struct registers_t` has a couple issues.
74+
First, the alleged `esp` is renamed to `useless`.
75+
The value is useless because it has to do with the current stack context, not what was interrupted.
76+
Then, we rename `useresp` to `esp`
6377

78+
Finally, we add `cld` just before `call isr_handler` on `cpu/interrupt.asm`
6479

23-fixes/cpu/idt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ void set_idt() {
1313
idt_reg.base = (uint32_t) &idt;
1414
idt_reg.limit = IDT_ENTRIES * sizeof(idt_gate_t) - 1;
1515
/* Don't make the mistake of loading &idt -- always load &idt_reg */
16-
__asm__ __volatile__("lidtl (%0)" : : "r" (&idt_reg));
16+
asm volatile("lidtl (%0)" : : "r" (&idt_reg));
1717
}

23-fixes/cpu/interrupt.asm

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ isr_common_stub:
1515
mov gs, ax
1616
1717
; 2. Call C handler
18+
cld ; C code following the sysV ABI requires DF to be clear on function entry
1819
call isr_handler
1920
2021
; 3. Restore state
@@ -25,7 +26,6 @@ isr_common_stub:
2526
mov gs, ax
2627
popa
2728
add esp, 8 ; Cleans up the pushed error code and pushed ISR number
28-
sti
2929
iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP
3030

3131
; Common IRQ code. Identical to ISR code except for the 'call'
@@ -47,7 +47,6 @@ irq_common_stub:
4747
mov gs, bx
4848
popa
4949
add esp, 8
50-
sti
5150
iret
5251
5352
; We don't get information about which interrupt was caller
@@ -328,97 +327,81 @@ isr31:
328327

329328
; IRQ handlers
330329
irq0:
331-
cli
332330
push byte 0
333331
push byte 32
334332
jmp irq_common_stub
335333

336334
irq1:
337-
cli
338335
push byte 1
339336
push byte 33
340337
jmp irq_common_stub
341338

342339
irq2:
343-
cli
344340
push byte 2
345341
push byte 34
346342
jmp irq_common_stub
347343

348344
irq3:
349-
cli
350345
push byte 3
351346
push byte 35
352347
jmp irq_common_stub
353348

354349
irq4:
355-
cli
356350
push byte 4
357351
push byte 36
358352
jmp irq_common_stub
359353

360354
irq5:
361-
cli
362355
push byte 5
363356
push byte 37
364357
jmp irq_common_stub
365358

366359
irq6:
367-
cli
368360
push byte 6
369361
push byte 38
370362
jmp irq_common_stub
371363

372364
irq7:
373-
cli
374365
push byte 7
375366
push byte 39
376367
jmp irq_common_stub
377368

378369
irq8:
379-
cli
380370
push byte 8
381371
push byte 40
382372
jmp irq_common_stub
383373

384374
irq9:
385-
cli
386375
push byte 9
387376
push byte 41
388377
jmp irq_common_stub
389378

390379
irq10:
391-
cli
392380
push byte 10
393381
push byte 42
394382
jmp irq_common_stub
395383

396384
irq11:
397-
cli
398385
push byte 11
399386
push byte 43
400387
jmp irq_common_stub
401388

402389
irq12:
403-
cli
404390
push byte 12
405391
push byte 44
406392
jmp irq_common_stub
407393

408394
irq13:
409-
cli
410395
push byte 13
411396
push byte 45
412397
jmp irq_common_stub
413398

414399
irq14:
415-
cli
416400
push byte 14
417401
push byte 46
418402
jmp irq_common_stub
419403

420404
irq15:
421-
cli
422405
push byte 15
423406
push byte 47
424407
jmp irq_common_stub

23-fixes/cpu/isr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ extern void irq15();
7474
/* Struct which aggregates many registers */
7575
typedef struct {
7676
uint32_t ds; /* Data segment selector */
77-
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; /* Pushed by pusha. */
77+
uint32_t edi, esi, ebp, useless, ebx, edx, ecx, eax; /* Pushed by pusha. */
7878
uint32_t int_no, err_code; /* Interrupt number and error code (if applicable) */
79-
uint32_t eip, cs, eflags, useresp, ss; /* Pushed by the processor automatically */
79+
uint32_t eip, cs, eflags, esp, ss; /* Pushed by the processor automatically */
8080
} registers_t;
8181

8282
void isr_install();

23-fixes/cpu/ports.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ uint8_t port_byte_in (uint16_t port) {
1313
*
1414
* Inputs and outputs are separated by colons
1515
*/
16-
__asm__("in %%dx, %%al" : "=a" (result) : "d" (port));
16+
asm("in %%dx, %%al" : "=a" (result) : "d" (port));
1717
return result;
1818
}
1919

@@ -23,15 +23,15 @@ void port_byte_out (uint16_t port, uint8_t data) {
2323
* However we see a comma since there are two variables in the input area
2424
* and none in the 'return' area
2525
*/
26-
__asm__ __volatile__("out %%al, %%dx" : : "a" (data), "d" (port));
26+
asm volatile("out %%al, %%dx" : : "a" (data), "d" (port));
2727
}
2828

2929
uint16_t port_word_in (uint16_t port) {
3030
uint16_t result;
31-
__asm__("in %%dx, %%ax" : "=a" (result) : "d" (port));
31+
asm("in %%dx, %%ax" : "=a" (result) : "d" (port));
3232
return result;
3333
}
3434

3535
void port_word_out (uint16_t port, uint16_t data) {
36-
__asm__ __volatile__("out %%ax, %%dx" : : "a" (data), "d" (port));
36+
asm volatile("out %%ax, %%dx" : : "a" (data), "d" (port));
3737
}

0 commit comments

Comments
 (0)