Skip to content

Commit de3d442

Browse files
committed
lesson 23, step 3
1 parent 92ff191 commit de3d442

File tree

18 files changed

+124
-127
lines changed

18 files changed

+124
-127
lines changed

23-fixes/README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@ We add `-ffreestanding` when compiling `.o` files, which includes `kernel_entry
1616
Before, we disabled libgcc (not libc) through the use of `-nostdlib` and we didn't re-enable
1717
it for linking. Since this is tricky, we'll delete `-nostdlib`
1818

19+
`-nostdinc` was also pased to gcc, but we will need it for step 3.
1920

20-
2. Not setting a stack
21-
----------------------
22-
23-
24-
25-
3. kernel.c `main()` function
21+
2. kernel.c `main()` function
2622
-----------------------------
2723

2824
Modify `kernel/kernel.c` and change `main()` to `kernel_main()` since gcc recognizes "main" as
@@ -33,8 +29,16 @@ Change `boot/kernel_entry.asm` to point to the new name accordingly.
3329
To fix the `i386-elf-ld: warning: cannot find entry symbol _start; defaulting to 0000000000001000`
3430
warning message, add a `global _start;` and define the `_start:` label in `boot/kernel_entry.asm`.
3531

36-
4. Reinvented datatypes
32+
33+
3. Reinvented datatypes
3734
-----------------------
35+
36+
It looks like it was a bad idea to define non-standard data types like `u32` and such, since
37+
C99 introduces standard fixed-width data types like `uint32_t`
38+
39+
We need to include `<stdint.h>` which works even in `-ffreestanding` (but requires stdlibs)
40+
and use those data types instead of our own, then delete them on `type.h`
41+
3842
<stddef.h> to provide size\_t
3943

4044

23-fixes/cpu/idt.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "idt.h"
2+
#include "type.h"
23

3-
void set_idt_gate(int n, u32 handler) {
4+
void set_idt_gate(int n, uint32_t handler) {
45
idt[n].low_offset = low_16(handler);
56
idt[n].sel = KERNEL_CS;
67
idt[n].always0 = 0;
@@ -9,7 +10,7 @@ void set_idt_gate(int n, u32 handler) {
910
}
1011

1112
void set_idt() {
12-
idt_reg.base = (u32) &idt;
13+
idt_reg.base = (uint32_t) &idt;
1314
idt_reg.limit = IDT_ENTRIES * sizeof(idt_gate_t) - 1;
1415
/* Don't make the mistake of loading &idt -- always load &idt_reg */
1516
__asm__ __volatile__("lidtl (%0)" : : "r" (&idt_reg));

23-fixes/cpu/idt.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
#ifndef IDT_H
22
#define IDT_H
33

4-
#include "type.h"
4+
#include <stdint.h>
55

66
/* Segment selectors */
77
#define KERNEL_CS 0x08
88

99
/* How every interrupt gate (handler) is defined */
1010
typedef struct {
11-
u16 low_offset; /* Lower 16 bits of handler function address */
12-
u16 sel; /* Kernel segment selector */
13-
u8 always0;
11+
uint16_t low_offset; /* Lower 16 bits of handler function address */
12+
uint16_t sel; /* Kernel segment selector */
13+
uint8_t always0;
1414
/* First byte
1515
* Bit 7: "Interrupt is present"
1616
* Bits 6-5: Privilege level of caller (0=kernel..3=user)
1717
* Bit 4: Set to 0 for interrupt gates
1818
* Bits 3-0: bits 1110 = decimal 14 = "32 bit interrupt gate" */
19-
u8 flags;
20-
u16 high_offset; /* Higher 16 bits of handler function address */
19+
uint8_t flags;
20+
uint16_t high_offset; /* Higher 16 bits of handler function address */
2121
} __attribute__((packed)) idt_gate_t ;
2222

2323
/* A pointer to the array of interrupt handlers.
2424
* Assembly instruction 'lidt' will read it */
2525
typedef struct {
26-
u16 limit;
27-
u32 base;
26+
uint16_t limit;
27+
uint32_t base;
2828
} __attribute__((packed)) idt_register_t;
2929

3030
#define IDT_ENTRIES 256
@@ -33,7 +33,7 @@ idt_register_t idt_reg;
3333

3434

3535
/* Functions implemented in idt.c */
36-
void set_idt_gate(int n, u32 handler);
36+
void set_idt_gate(int n, uint32_t handler);
3737
void set_idt();
3838

3939
#endif

23-fixes/cpu/isr.c

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,38 @@ isr_t interrupt_handlers[256];
1111
/* Can't do this with a loop because we need the address
1212
* of the function names */
1313
void isr_install() {
14-
set_idt_gate(0, (u32)isr0);
15-
set_idt_gate(1, (u32)isr1);
16-
set_idt_gate(2, (u32)isr2);
17-
set_idt_gate(3, (u32)isr3);
18-
set_idt_gate(4, (u32)isr4);
19-
set_idt_gate(5, (u32)isr5);
20-
set_idt_gate(6, (u32)isr6);
21-
set_idt_gate(7, (u32)isr7);
22-
set_idt_gate(8, (u32)isr8);
23-
set_idt_gate(9, (u32)isr9);
24-
set_idt_gate(10, (u32)isr10);
25-
set_idt_gate(11, (u32)isr11);
26-
set_idt_gate(12, (u32)isr12);
27-
set_idt_gate(13, (u32)isr13);
28-
set_idt_gate(14, (u32)isr14);
29-
set_idt_gate(15, (u32)isr15);
30-
set_idt_gate(16, (u32)isr16);
31-
set_idt_gate(17, (u32)isr17);
32-
set_idt_gate(18, (u32)isr18);
33-
set_idt_gate(19, (u32)isr19);
34-
set_idt_gate(20, (u32)isr20);
35-
set_idt_gate(21, (u32)isr21);
36-
set_idt_gate(22, (u32)isr22);
37-
set_idt_gate(23, (u32)isr23);
38-
set_idt_gate(24, (u32)isr24);
39-
set_idt_gate(25, (u32)isr25);
40-
set_idt_gate(26, (u32)isr26);
41-
set_idt_gate(27, (u32)isr27);
42-
set_idt_gate(28, (u32)isr28);
43-
set_idt_gate(29, (u32)isr29);
44-
set_idt_gate(30, (u32)isr30);
45-
set_idt_gate(31, (u32)isr31);
14+
set_idt_gate(0, (uint32_t)isr0);
15+
set_idt_gate(1, (uint32_t)isr1);
16+
set_idt_gate(2, (uint32_t)isr2);
17+
set_idt_gate(3, (uint32_t)isr3);
18+
set_idt_gate(4, (uint32_t)isr4);
19+
set_idt_gate(5, (uint32_t)isr5);
20+
set_idt_gate(6, (uint32_t)isr6);
21+
set_idt_gate(7, (uint32_t)isr7);
22+
set_idt_gate(8, (uint32_t)isr8);
23+
set_idt_gate(9, (uint32_t)isr9);
24+
set_idt_gate(10, (uint32_t)isr10);
25+
set_idt_gate(11, (uint32_t)isr11);
26+
set_idt_gate(12, (uint32_t)isr12);
27+
set_idt_gate(13, (uint32_t)isr13);
28+
set_idt_gate(14, (uint32_t)isr14);
29+
set_idt_gate(15, (uint32_t)isr15);
30+
set_idt_gate(16, (uint32_t)isr16);
31+
set_idt_gate(17, (uint32_t)isr17);
32+
set_idt_gate(18, (uint32_t)isr18);
33+
set_idt_gate(19, (uint32_t)isr19);
34+
set_idt_gate(20, (uint32_t)isr20);
35+
set_idt_gate(21, (uint32_t)isr21);
36+
set_idt_gate(22, (uint32_t)isr22);
37+
set_idt_gate(23, (uint32_t)isr23);
38+
set_idt_gate(24, (uint32_t)isr24);
39+
set_idt_gate(25, (uint32_t)isr25);
40+
set_idt_gate(26, (uint32_t)isr26);
41+
set_idt_gate(27, (uint32_t)isr27);
42+
set_idt_gate(28, (uint32_t)isr28);
43+
set_idt_gate(29, (uint32_t)isr29);
44+
set_idt_gate(30, (uint32_t)isr30);
45+
set_idt_gate(31, (uint32_t)isr31);
4646

4747
// Remap the PIC
4848
port_byte_out(0x20, 0x11);
@@ -57,22 +57,22 @@ void isr_install() {
5757
port_byte_out(0xA1, 0x0);
5858

5959
// Install the IRQs
60-
set_idt_gate(32, (u32)irq0);
61-
set_idt_gate(33, (u32)irq1);
62-
set_idt_gate(34, (u32)irq2);
63-
set_idt_gate(35, (u32)irq3);
64-
set_idt_gate(36, (u32)irq4);
65-
set_idt_gate(37, (u32)irq5);
66-
set_idt_gate(38, (u32)irq6);
67-
set_idt_gate(39, (u32)irq7);
68-
set_idt_gate(40, (u32)irq8);
69-
set_idt_gate(41, (u32)irq9);
70-
set_idt_gate(42, (u32)irq10);
71-
set_idt_gate(43, (u32)irq11);
72-
set_idt_gate(44, (u32)irq12);
73-
set_idt_gate(45, (u32)irq13);
74-
set_idt_gate(46, (u32)irq14);
75-
set_idt_gate(47, (u32)irq15);
60+
set_idt_gate(32, (uint32_t)irq0);
61+
set_idt_gate(33, (uint32_t)irq1);
62+
set_idt_gate(34, (uint32_t)irq2);
63+
set_idt_gate(35, (uint32_t)irq3);
64+
set_idt_gate(36, (uint32_t)irq4);
65+
set_idt_gate(37, (uint32_t)irq5);
66+
set_idt_gate(38, (uint32_t)irq6);
67+
set_idt_gate(39, (uint32_t)irq7);
68+
set_idt_gate(40, (uint32_t)irq8);
69+
set_idt_gate(41, (uint32_t)irq9);
70+
set_idt_gate(42, (uint32_t)irq10);
71+
set_idt_gate(43, (uint32_t)irq11);
72+
set_idt_gate(44, (uint32_t)irq12);
73+
set_idt_gate(45, (uint32_t)irq13);
74+
set_idt_gate(46, (uint32_t)irq14);
75+
set_idt_gate(47, (uint32_t)irq15);
7676

7777
set_idt(); // Load with ASM
7878
}
@@ -126,7 +126,7 @@ void isr_handler(registers_t r) {
126126
kprint("\n");
127127
}
128128

129-
void register_interrupt_handler(u8 n, isr_t handler) {
129+
void register_interrupt_handler(uint8_t n, isr_t handler) {
130130
interrupt_handlers[n] = handler;
131131
}
132132

23-fixes/cpu/isr.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef ISR_H
22
#define ISR_H
33

4-
#include "type.h"
4+
#include <stdint.h>
55

66
/* ISRs reserved for CPU exceptions */
77
extern void isr0();
@@ -73,17 +73,17 @@ extern void irq15();
7373

7474
/* Struct which aggregates many registers */
7575
typedef struct {
76-
u32 ds; /* Data segment selector */
77-
u32 edi, esi, ebp, esp, ebx, edx, ecx, eax; /* Pushed by pusha. */
78-
u32 int_no, err_code; /* Interrupt number and error code (if applicable) */
79-
u32 eip, cs, eflags, useresp, ss; /* Pushed by the processor automatically */
76+
uint32_t ds; /* Data segment selector */
77+
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; /* Pushed by pusha. */
78+
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 */
8080
} registers_t;
8181

8282
void isr_install();
8383
void isr_handler(registers_t r);
8484
void irq_install();
8585

8686
typedef void (*isr_t)(registers_t);
87-
void register_interrupt_handler(u8 n, isr_t handler);
87+
void register_interrupt_handler(uint8_t n, isr_t handler);
8888

8989
#endif

23-fixes/cpu/ports.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Read a byte from the specified port
55
*/
6-
u8 port_byte_in (u16 port) {
7-
u8 result;
6+
uint8_t port_byte_in (uint16_t port) {
7+
uint8_t result;
88
/* Inline assembler syntax
99
* !! Notice how the source and destination registers are switched from NASM !!
1010
*
@@ -17,7 +17,7 @@ u8 port_byte_in (u16 port) {
1717
return result;
1818
}
1919

20-
void port_byte_out (u16 port, u8 data) {
20+
void port_byte_out (uint16_t port, uint8_t data) {
2121
/* Notice how here both registers are mapped to C variables and
2222
* nothing is returned, thus, no equals '=' in the asm syntax
2323
* However we see a comma since there are two variables in the input area
@@ -26,12 +26,12 @@ void port_byte_out (u16 port, u8 data) {
2626
__asm__ __volatile__("out %%al, %%dx" : : "a" (data), "d" (port));
2727
}
2828

29-
u16 port_word_in (u16 port) {
30-
u16 result;
29+
uint16_t port_word_in (uint16_t port) {
30+
uint16_t result;
3131
__asm__("in %%dx, %%ax" : "=a" (result) : "d" (port));
3232
return result;
3333
}
3434

35-
void port_word_out (u16 port, u16 data) {
35+
void port_word_out (uint16_t port, uint16_t data) {
3636
__asm__ __volatile__("out %%ax, %%dx" : : "a" (data), "d" (port));
3737
}

23-fixes/cpu/ports.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#ifndef PORTS_H
22
#define PORTS_H
33

4-
#include "../cpu/type.h"
4+
#include <stdint.h>
55

6-
unsigned char port_byte_in (u16 port);
7-
void port_byte_out (u16 port, u8 data);
8-
unsigned short port_word_in (u16 port);
9-
void port_word_out (u16 port, u16 data);
6+
unsigned char port_byte_in (uint16_t port);
7+
void port_byte_out (uint16_t port, uint8_t data);
8+
unsigned short port_word_in (uint16_t port);
9+
void port_word_out (uint16_t port, uint16_t data);
1010

1111
#endif

23-fixes/cpu/timer.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
#include "ports.h"
44
#include "../libc/function.h"
55

6-
u32 tick = 0;
6+
uint32_t tick = 0;
77

88
static void timer_callback(registers_t regs) {
99
tick++;
1010
UNUSED(regs);
1111
}
1212

13-
void init_timer(u32 freq) {
13+
void init_timer(uint32_t freq) {
1414
/* Install the function we just wrote */
1515
register_interrupt_handler(IRQ0, timer_callback);
1616

1717
/* Get the PIT value: hardware clock at 1193180 Hz */
18-
u32 divisor = 1193180 / freq;
19-
u8 low = (u8)(divisor & 0xFF);
20-
u8 high = (u8)( (divisor >> 8) & 0xFF);
18+
uint32_t divisor = 1193180 / freq;
19+
uint8_t low = (uint8_t)(divisor & 0xFF);
20+
uint8_t high = (uint8_t)( (divisor >> 8) & 0xFF);
2121
/* Send the command */
2222
port_byte_out(0x43, 0x36); /* Command port */
2323
port_byte_out(0x40, low);

23-fixes/cpu/timer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef TIMER_H
22
#define TIMER_H
33

4-
#include "type.h"
4+
#include <stdint.h>
55

6-
void init_timer(u32 freq);
6+
void init_timer(uint32_t freq);
77

88
#endif

23-fixes/cpu/type.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
#ifndef TYPE_H
22
#define TYPE_H
33

4-
/* Instead of using 'chars' to allocate non-character bytes,
5-
* we will use these new type with no semantic meaning */
6-
typedef unsigned int u32;
7-
typedef int s32;
8-
typedef unsigned short u16;
9-
typedef short s16;
10-
typedef unsigned char u8;
11-
typedef char s8;
4+
#include <stdint.h>
125

13-
#define low_16(address) (u16)((address) & 0xFFFF)
14-
#define high_16(address) (u16)(((address) >> 16) & 0xFFFF)
6+
#define low_16(address) (uint16_t)((address) & 0xFFFF)
7+
#define high_16(address) (uint16_t)(((address) >> 16) & 0xFFFF)
158

169
#endif

0 commit comments

Comments
 (0)