File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -27,9 +27,12 @@ add_compile_options(
2727include_directories (include )
2828
2929# Source discovery
30- file (GLOB_RECURSE ASM_SOURCES "src/*.s " )
30+ file (GLOB_RECURSE ASM_SOURCES "src/*.asm " )
3131file (GLOB_RECURSE C_SOURCES "src/*.c" )
3232
33+ # NASM flags for 32-bit ELF
34+ set (CMAKE_ASM_NASM_FLAGS "-f elf32" )
35+
3336# Exclude specific files if needed
3437list (FILTER C_SOURCES EXCLUDE REGEX "src/drivers/vga.c" )
3538
Original file line number Diff line number Diff line change 1+ [bits 32]
2+ global gdt_flush
3+
4+ gdt_flush:
5+ mov eax , [ esp + 4 ]
6+ lgdt [ eax ]
7+ mov ax , 0x10
8+ mov ds , ax
9+ mov es , ax
10+ mov fs , ax
11+ mov gs , ax
12+ mov ss , ax
13+ jmp 0x08 :flush_cs
14+ flush_cs:
15+ ret
Original file line number Diff line number Diff line change 1+ [bits 32]
2+
3+ section .multiboot
4+ align 4
5+ dd 0x1BADB002
6+ dd 0x00000000
7+ dd 0xE4524FFE
8+
9+ section .text
10+ global _start
11+ extern kernel_main
12+
13+ _start:
14+ cli
15+ mov esp , stack_top
16+ push ebx
17+ push eax
18+ call kernel_main
19+ add esp , 8
20+ jmp $
21+
22+ section .bss
23+ align 16
24+ stack_bottom:
25+ resb 16384
26+ stack_top:
Original file line number Diff line number Diff line change 1+ [bits 32]
2+ global idt_load
3+
4+ idt_load:
5+ mov eax , [ esp + 4 ]
6+ lidt [ eax ]
7+ ret
Original file line number Diff line number Diff line change 1+ [bits 32]
2+
3+ extern isr_handler
4+ extern irq_handler
5+
6+ %macro ISR_NOERR 1
7+ global isr% 1
8+ isr% 1 :
9+ cli
10+ push dword 0
11+ push dword % 1
12+ jmp isr_common_stub
13+ %endmacro
14+
15+ %macro ISR_ERR 1
16+ global isr% 1
17+ isr% 1 :
18+ cli
19+ push dword % 1
20+ jmp isr_common_stub
21+ %endmacro
22+
23+ %macro IRQ 2
24+ global irq% 1
25+ irq% 1 :
26+ cli
27+ push dword 0
28+ push dword % 2
29+ jmp irq_common_stub
30+ %endmacro
31+
32+ ISR_NOERR 0
33+ ISR_NOERR 1
34+ ISR_NOERR 2
35+ ISR_NOERR 3
36+ ISR_NOERR 4
37+ ISR_NOERR 5
38+ ISR_NOERR 6
39+ ISR_NOERR 7
40+ ISR_ERR 8
41+ ISR_NOERR 9
42+ ISR_ERR 10
43+ ISR_ERR 11
44+ ISR_ERR 12
45+ ISR_ERR 13
46+ ISR_ERR 14
47+ ISR_NOERR 15
48+ ISR_NOERR 16
49+ ISR_ERR 17
50+ ISR_NOERR 18
51+ ISR_NOERR 19
52+ ISR_NOERR 20
53+ ISR_NOERR 21
54+ ISR_NOERR 22
55+ ISR_NOERR 23
56+ ISR_NOERR 24
57+ ISR_NOERR 25
58+ ISR_NOERR 26
59+ ISR_NOERR 27
60+ ISR_NOERR 28
61+ ISR_NOERR 29
62+ ISR_ERR 30
63+ ISR_NOERR 31
64+ ISR_NOERR 128
65+
66+ IRQ 0 , 32
67+ IRQ 1 , 33
68+ IRQ 2 , 34
69+ IRQ 3 , 35
70+ IRQ 4 , 36
71+ IRQ 5 , 37
72+ IRQ 6 , 38
73+ IRQ 7 , 39
74+ IRQ 8 , 40
75+ IRQ 9 , 41
76+ IRQ 10 , 42
77+ IRQ 11 , 43
78+ IRQ 12 , 44
79+ IRQ 13 , 45
80+ IRQ 14 , 46
81+ IRQ 15 , 47
82+
83+ isr_common_stub:
84+ pusha
85+ mov ax , ds
86+ push eax
87+ mov ax , es
88+ push eax
89+ mov ax , fs
90+ push eax
91+ mov ax , gs
92+ push eax
93+
94+ mov ax , 0x10
95+ mov ds , ax
96+ mov es , ax
97+ mov fs , ax
98+ mov gs , ax
99+ push esp
100+ call isr_handler
101+ mov esp , eax ; IMPORTANT: use the returned (potentially switched) stack pointer
102+
103+ pop eax
104+ mov gs , ax
105+ pop eax
106+ mov fs , ax
107+ pop eax
108+ mov es , ax
109+ pop eax
110+ mov ds , ax
111+
112+ popa
113+ add esp , 8
114+ iret
115+
116+ irq_common_stub:
117+ pusha
118+ mov ax , ds
119+ push eax
120+ mov ax , es
121+ push eax
122+ mov ax , fs
123+ push eax
124+ mov ax , gs
125+ push eax
126+
127+ mov ax , 0x10
128+ mov ds , ax
129+ mov es , ax
130+ mov fs , ax
131+ mov gs , ax
132+ push esp
133+ call irq_handler
134+ mov esp , eax ; IMPORTANT: use the returned (potentially switched) stack pointer
135+
136+ pop eax
137+ mov gs , ax
138+ pop eax
139+ mov fs , ax
140+ pop eax
141+ mov es , ax
142+ pop eax
143+ mov ds , ax
144+
145+ popa
146+ add esp , 8
147+ iret
Original file line number Diff line number Diff line change 1+ [bits 32]
2+ global switch_to
3+ global context_switch
4+ extern current_task_ptr
5+
6+ %define CTX_EAX 0
7+ %define CTX_EBX 4
8+ %define CTX_ECX 8
9+ %define CTX_EDX 12
10+ %define CTX_ESI 16
11+ %define CTX_EDI 20
12+ %define CTX_ESP 24
13+ %define CTX_EBP 28
14+ %define CTX_EIP 32
15+ %define CTX_EFLAGS 36
16+
17+ switch_to:
18+ pushfd
19+ pushad
20+ mov eax , [ current_task_ptr ]
21+ test eax , eax
22+ jz .load_next
23+ mov edx , [ esp + 36 ]
24+ mov [ eax + CTX_EIP ], edx
25+ mov edx , [ esp + 32 ]
26+ mov [ eax + CTX_EFLAGS ], edx
27+ mov edx , [ esp + 28 ]
28+ mov [ eax + CTX_EAX ], edx
29+ mov edx , [ esp + 24 ]
30+ mov [ eax + CTX_ECX ], edx
31+ mov edx , [ esp + 20 ]
32+ mov [ eax + CTX_EDX ], edx
33+ mov edx , [ esp + 16 ]
34+ mov [ eax + CTX_EBX ], edx
35+ mov edx , [ esp + 12 ]
36+ mov [ eax + CTX_ESP ], edx
37+ mov edx , [ esp + 8 ]
38+ mov [ eax + CTX_EBP ], edx
39+ mov edx , [ esp + 4 ]
40+ mov [ eax + CTX_ESI ], edx
41+ mov edx , [ esp ]
42+ mov [ eax + CTX_EDI ], edx
43+ .load_next:
44+ mov eax , [ esp + 40 ]
45+ mov [ current_task_ptr ], eax
46+ mov esp , [ eax + CTX_ESP ]
47+ push dword [ eax + CTX_EFLAGS ]
48+ popfd
49+ push dword [ eax + CTX_EIP ]
50+ mov ebp , [ eax + CTX_EBP ]
51+ mov ebx , [ eax + CTX_EBX ]
52+ mov ecx , [ eax + CTX_ECX ]
53+ mov edx , [ eax + CTX_EDX ]
54+ mov esi , [ eax + CTX_ESI ]
55+ mov edi , [ eax + CTX_EDI ]
56+ mov eax , [ eax + CTX_EAX ]
57+ ret
58+
59+ %define PROC_ESP 36
60+ %define PROC_EBP 40
61+
62+ context_switch:
63+ ; [esp+4] = previous process (process_t*)
64+ ; [esp+8] = next process (process_t*)
65+ ; [esp+12] = registers (registers_t*)
66+
67+ mov eax , [ esp + 4 ] ; eax = previous
68+ mov edx , [ esp + 8 ] ; edx = next
69+
70+ test eax , eax
71+ jz .load_next
72+
73+ ; Save current ESP and EBP to previous task
74+ mov [ eax + PROC_ESP ], esp
75+ mov [ eax + PROC_EBP ], ebp
76+
77+ .load_next:
78+ ; Switch to next task's ESP and EBP
79+ mov esp , [ edx + PROC_ESP ]
80+ mov ebp , [ edx + PROC_EBP ]
81+
82+ ; Note: If this was called from irq0_handler, the 'esp' we just restored
83+ ; points to a registers_t struct. The assembly stub in isr.s will
84+ ; then pop these registers and iret.
85+
86+ ret
You can’t perform that action at this time.
0 commit comments