Skip to content

Commit

Permalink
Lesson 23, step 6
Browse files Browse the repository at this point in the history
  • Loading branch information
cfenollosa committed Aug 18, 2015
1 parent d9ee254 commit aa7a783
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
21 changes: 18 additions & 3 deletions 23-fixes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ C99 introduces standard fixed-width data types like `uint32_t`
We need to include `<stdint.h>` which works even in `-ffreestanding` (but requires stdlibs)
and use those data types instead of our own, then delete them on `type.h`

We also delete the underscores around `__asm__` and `__volatile__` since they aren't needed.


4. Improperly aligned `kmalloc`
-------------------------------

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

We will implement the missing `mem*` functions in following lessons


6. Interrupt handlers
---------------------
- also cli, sti in interrupt handlers
`cli` is redundant, since we already established on the IDT entries if interrupts
are enabled within a handler using the `idt_gate_t` flags.

`sti` is also redundant, as `iret` loads the eflags value from the stack, which contains a
bit telling whether interrupts are on or off.
In other words the interrupt handler automatically restores interrupts whether or not
interrupts were enabled before this interrupt


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

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

2 changes: 1 addition & 1 deletion 23-fixes/cpu/idt.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ void set_idt() {
idt_reg.base = (uint32_t) &idt;
idt_reg.limit = IDT_ENTRIES * sizeof(idt_gate_t) - 1;
/* Don't make the mistake of loading &idt -- always load &idt_reg */
__asm__ __volatile__("lidtl (%0)" : : "r" (&idt_reg));
asm volatile("lidtl (%0)" : : "r" (&idt_reg));
}
19 changes: 1 addition & 18 deletions 23-fixes/cpu/interrupt.asm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ isr_common_stub:
mov gs, ax
; 2. Call C handler
cld ; C code following the sysV ABI requires DF to be clear on function entry
call isr_handler
; 3. Restore state
Expand All @@ -25,7 +26,6 @@ isr_common_stub:
mov gs, ax
popa
add esp, 8 ; Cleans up the pushed error code and pushed ISR number
sti
iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP

; Common IRQ code. Identical to ISR code except for the 'call'
Expand All @@ -47,7 +47,6 @@ irq_common_stub:
mov gs, bx
popa
add esp, 8
sti
iret
; We don't get information about which interrupt was caller
Expand Down Expand Up @@ -328,97 +327,81 @@ isr31:

; IRQ handlers
irq0:
cli
push byte 0
push byte 32
jmp irq_common_stub

irq1:
cli
push byte 1
push byte 33
jmp irq_common_stub

irq2:
cli
push byte 2
push byte 34
jmp irq_common_stub

irq3:
cli
push byte 3
push byte 35
jmp irq_common_stub

irq4:
cli
push byte 4
push byte 36
jmp irq_common_stub

irq5:
cli
push byte 5
push byte 37
jmp irq_common_stub

irq6:
cli
push byte 6
push byte 38
jmp irq_common_stub

irq7:
cli
push byte 7
push byte 39
jmp irq_common_stub

irq8:
cli
push byte 8
push byte 40
jmp irq_common_stub

irq9:
cli
push byte 9
push byte 41
jmp irq_common_stub

irq10:
cli
push byte 10
push byte 42
jmp irq_common_stub

irq11:
cli
push byte 11
push byte 43
jmp irq_common_stub

irq12:
cli
push byte 12
push byte 44
jmp irq_common_stub

irq13:
cli
push byte 13
push byte 45
jmp irq_common_stub

irq14:
cli
push byte 14
push byte 46
jmp irq_common_stub

irq15:
cli
push byte 15
push byte 47
jmp irq_common_stub
Expand Down
4 changes: 2 additions & 2 deletions 23-fixes/cpu/isr.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ extern void irq15();
/* Struct which aggregates many registers */
typedef struct {
uint32_t ds; /* Data segment selector */
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; /* Pushed by pusha. */
uint32_t edi, esi, ebp, useless, ebx, edx, ecx, eax; /* Pushed by pusha. */
uint32_t int_no, err_code; /* Interrupt number and error code (if applicable) */
uint32_t eip, cs, eflags, useresp, ss; /* Pushed by the processor automatically */
uint32_t eip, cs, eflags, esp, ss; /* Pushed by the processor automatically */
} registers_t;

void isr_install();
Expand Down
8 changes: 4 additions & 4 deletions 23-fixes/cpu/ports.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ uint8_t port_byte_in (uint16_t port) {
*
* Inputs and outputs are separated by colons
*/
__asm__("in %%dx, %%al" : "=a" (result) : "d" (port));
asm("in %%dx, %%al" : "=a" (result) : "d" (port));
return result;
}

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

uint16_t port_word_in (uint16_t port) {
uint16_t result;
__asm__("in %%dx, %%ax" : "=a" (result) : "d" (port));
asm("in %%dx, %%ax" : "=a" (result) : "d" (port));
return result;
}

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

0 comments on commit aa7a783

Please sign in to comment.