Initial git release
[ZeXOS.git] / boot / start.asm
blob2fcb75ff5a6e8bad3fcce45ae0daa7f9254b2fee
1 ; ZeX/OS
2 ; Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
4 ; This program is free software: you can redistribute it and/or modify
5 ; it under the terms of the GNU General Public License as published by
6 ; the Free Software Foundation, either version 3 of the License, or
7 ; (at your option) any later version.
9 ; This program is distributed in the hope that it will be useful,
10 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ; GNU General Public License for more details.
14 ; You should have received a copy of the GNU General Public License
15 ; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 ; This is the kernel's entry point. We could either call main here,
19 ; or we can use this to setup the stack or other nice stuff, like
20 ; perhaps setting up the GDT and segments. Please note that interrupts
21 ; are disabled at this point: More on interrupts later!
22 %macro IMP 1
23 %ifdef UNDERBARS
24 EXTERN _%1
25 %define %1 _%1
26 %else
27 EXTERN %1
28 %endif
29 %endmacro
31 %macro EXP 1
32 GLOBAL $_%1
33 $_%1:
34 GLOBAL $%1
35 $%1:
36 %endmacro
38 [BITS 32]
39 global start
40 start:
41 mov esp, _sys_stack ; This points the stack to our new stack area
42 jmp stublet
44 ; This part MUST be 4byte aligned, so we solve that issue using 'ALIGN 4'
45 ALIGN 4
46 mboot:
47 ; Multiboot macros to make a few lines later more readable
48 MULTIBOOT_PAGE_ALIGN equ 1<<0
49 MULTIBOOT_MEMORY_INFO equ 1<<1
50 MULTIBOOT_AOUT_KLUDGE equ 1<<16
51 MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
52 MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
53 MULTIBOOT_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
54 EXTERN code, bss, end
56 ; This is the GRUB Multiboot header. A boot signature
57 dd MULTIBOOT_HEADER_MAGIC
58 dd MULTIBOOT_HEADER_FLAGS
59 dd MULTIBOOT_CHECKSUM
61 ; AOUT kludge - must be physical addresses. Make a note of these:
62 ; The linker script fills in the data for these ones!
63 dd mboot
64 dd code
65 dd bss
66 dd end
67 dd start
69 ; This is an endless loop here. Make a note of this: Later on, we
70 ; will insert an 'extern _main', followed by 'call _main', right
71 ; before the 'jmp $'.
72 stublet:
73 IMP main
74 call main
75 jmp $
78 ; This will set up our new segment registers. We need to do
79 ; something special in order to set CS. We do what is called a
80 ; far jump. A jump that includes a segment as well as an offset.
81 ; This is declared in C as 'extern void gdt_flush();'
82 EXP gdt_flush ; Allows the C code to link to this
83 IMP gp ; Says that '_gp' is in another file
84 ;_gdt_flush:
85 lgdt [gp] ; Load the GDT with our '_gp' which is a special pointer
86 mov ax, 0x10 ; 0x10 is the offset in the GDT to our data segment
87 mov ds, ax
88 mov es, ax
89 mov fs, ax
90 mov gs, ax
91 jmp 0x08:flush2 ; 0x08 is the offset to our code segment: Far jump!
92 flush2:
93 ret ; Returns back to the C code!
95 ; Loads the IDT defined in '_idtp' into the processor.
96 ; This is declared in C as 'extern void idt_load();'
97 EXP idt_load
98 IMP idtp
99 lidt [idtp]
102 ; 0: Divide By Zero Exception
103 EXP isr0
105 push byte 0
106 push byte 0
107 jmp isr_common_stub
109 ; 1: Debug Exception
110 EXP isr1
112 push byte 0
113 push byte 1
114 jmp isr_common_stub
116 ; 2: Non Maskable Interrupt Exception
117 EXP isr2
119 push byte 0
120 push byte 2
121 jmp isr_common_stub
123 ; 3: Int 3 Exception
124 EXP isr3
126 push byte 0
127 push byte 3
128 jmp isr_common_stub
130 ; 4: INTO Exception
131 EXP isr4
133 push byte 0
134 push byte 4
135 jmp isr_common_stub
137 ; 5: Out of Bounds Exception
138 EXP isr5
140 push byte 0
141 push byte 5
142 jmp isr_common_stub
144 ; 6: Invalid Opcode Exception
145 EXP isr6
146 cli
147 push byte 0
148 push byte 6
149 jmp isr_common_stub
151 ; 7: Coprocessor Not Available Exception
152 EXP isr7
154 push byte 0
155 push byte 7
156 jmp isr_common_stub
158 ; 8: Double Fault Exception (With Error Code!)
159 EXP isr8
161 push byte 8
162 jmp isr_common_stub
164 ; 9: Coprocessor Segment Overrun Exception
165 EXP isr9
167 push byte 0
168 push byte 9
169 jmp isr_common_stub
171 ; 10: Bad TSS Exception (With Error Code!)
172 EXP isr10
174 push byte 10
175 jmp isr_common_stub
177 ; 11: Segment Not Present Exception (With Error Code!)
178 EXP isr11
180 push byte 11
181 jmp isr_common_stub
183 ; 12: Stack Fault Exception (With Error Code!)
184 EXP isr12
186 push byte 12
187 jmp isr_common_stub
189 ; 13: General Protection Fault Exception (With Error Code!)
190 EXP isr13
192 push byte 13
193 jmp isr_common_stub
195 ; 14: Page Fault Exception (With Error Code!)
196 EXP isr14
198 push byte 14
199 jmp isr_common_stub
201 ; 15: Reserved Exception
202 EXP isr15
204 push byte 0
205 push byte 15
206 jmp isr_common_stub
208 ; 16: Floating Point Exception
209 EXP isr16
211 push byte 0
212 push byte 16
213 jmp isr_common_stub
215 ; 17: Alignment Check Exception
216 EXP isr17
218 push byte 0
219 push byte 17
220 jmp isr_common_stub
222 ; 18: Machine Check Exception
223 EXP isr18
225 push byte 0
226 push byte 18
227 jmp isr_common_stub
229 ; 19: Reserved
230 EXP isr19
232 push byte 0
233 push byte 19
234 jmp isr_common_stub
236 ; 20: Reserved
237 EXP isr20
239 push byte 0
240 push byte 20
241 jmp isr_common_stub
243 ; 21: Reserved
244 EXP isr21
246 push byte 0
247 push byte 21
248 jmp isr_common_stub
250 ; 22: Reserved
251 EXP isr22
253 push byte 0
254 push byte 22
255 jmp isr_common_stub
257 ; 23: Reserved
258 EXP isr23
260 push byte 0
261 push byte 23
262 jmp isr_common_stub
264 ; 24: Reserved
265 EXP isr24
267 push byte 0
268 push byte 24
269 jmp isr_common_stub
271 ; 25: Reserved
272 EXP isr25
274 push byte 0
275 push byte 25
276 jmp isr_common_stub
278 ; 26: Reserved
279 EXP isr26
281 push byte 0
282 push byte 26
283 jmp isr_common_stub
285 ; 27: Reserved
286 EXP isr27
288 push byte 0
289 push byte 27
290 jmp isr_common_stub
292 ; 28: Reserved
293 EXP isr28
295 push byte 0
296 push byte 28
297 jmp isr_common_stub
299 ; 29: Reserved
300 EXP isr29
302 push byte 0
303 push byte 29
304 jmp isr_common_stub
306 ; 30: Reserved
307 EXP isr30
309 push byte 0
310 push byte 30
311 jmp isr_common_stub
313 ; 31: Reserved
314 EXP isr31
316 push byte 0
317 push byte 31
318 jmp isr_common_stub
321 ; We call a C function in here. We need to let the assembler know
322 ; that '_fault_handler' exists in another file
325 ; This is our common ISR stub. It saves the processor state, sets
326 ; up for kernel mode segments, calls the C-level fault handler,
327 ; and finally restores the stack frame.
328 isr_common_stub:
329 IMP fault_handler
330 pusha
331 push ds
332 push es
333 push fs
334 push gs
335 mov ax, 0x10
336 mov ds, ax
337 mov es, ax
338 mov fs, ax
339 mov gs, ax
340 mov eax, esp
341 push eax
342 mov eax, fault_handler
343 call eax
344 pop eax
345 pop gs
346 pop fs
347 pop es
348 pop ds
349 popa
350 add esp, 8
351 iret
353 ;global _irq0
354 ;global _irq1
355 ;global _irq2
356 ;global _irq3
357 ;global _irq4
358 ;global _irq5
359 ;global _irq6
360 ;global _irq7
361 ;global _irq8
362 ;global _irq9
363 ;global _irq10
364 ;global _irq11
365 ;global _irq12
366 ;global _irq13
367 ;global _irq14
368 ;global _irq15
370 ; 32: IRQ0
371 EXP irq0
373 push byte 0
374 push byte 32
375 jmp irq_common_stub
377 ; 33: IRQ1
378 EXP irq1
380 push byte 0
381 push byte 33
382 jmp irq_common_stub
384 ; 34: IRQ2
385 EXP irq2
387 push byte 0
388 push byte 34
389 jmp irq_common_stub
391 ; 35: IRQ3
392 EXP irq3
394 push byte 0
395 push byte 35
396 jmp irq_common_stub
398 ; 36: IRQ4
399 EXP irq4
401 push byte 0
402 push byte 36
403 jmp irq_common_stub
405 ; 37: IRQ5
406 EXP irq5
408 push byte 0
409 push byte 37
410 jmp irq_common_stub
412 ; 38: IRQ6
413 EXP irq6
415 push byte 0
416 push byte 38
417 jmp irq_common_stub
419 ; 39: IRQ7
420 EXP irq7
422 push byte 0
423 push byte 39
424 jmp irq_common_stub
426 ; 40: IRQ8
427 EXP irq8
429 push byte 0
430 push byte 40
431 jmp irq_common_stub
433 ; 41: IRQ9
434 EXP irq9
436 push byte 0
437 push byte 41
438 jmp irq_common_stub
440 ; 42: IRQ10
441 EXP irq10
443 push byte 0
444 push byte 42
445 jmp irq_common_stub
447 ; 43: IRQ11
448 EXP irq11
450 push byte 0
451 push byte 43
452 jmp irq_common_stub
454 ; 44: IRQ12
455 EXP irq12
457 push byte 0
458 push byte 44
459 jmp irq_common_stub
461 ; 45: IRQ13
462 EXP irq13
464 push byte 0
465 push byte 45
466 jmp irq_common_stub
468 ; 46: IRQ14
469 EXP irq14
471 push byte 0
472 push byte 46
473 jmp irq_common_stub
475 ; 47: IRQ15
476 EXP irq15
478 push byte 0
479 push byte 47
480 jmp irq_common_stub
482 IMP irq_handler
484 irq_common_stub:
485 pusha
486 push ds
487 push es
488 push fs
489 push gs
491 mov ax, 0x10
492 mov ds, ax
493 mov es, ax
494 mov fs, ax
495 mov gs, ax
496 mov eax, esp
498 push eax
499 mov eax, irq_handler
500 call eax
501 pop eax
503 pop gs
504 pop fs
505 pop es
506 pop ds
507 popa
508 add esp, 8
509 iret
511 ;extern testf
512 ;global testf_c
514 ;testf_c:
515 ; mov ecx, testf
516 ; jmp syscall0
518 ; syscall with 0 args
520 ;IMP syscall_handler
521 ;EXP system_call
522 ; push ds ; switch to kernel space
523 ; push es
524 ; mov eax, 0x10
525 ; mov ds, eax
526 ; mov es, eax
528 ; call syscall_handler ; make call
530 ; pop es ; back to userland
531 ; pop ds
532 ; retf 0
533 ; iret
535 IMP syscall_handler
536 EXP system_call
537 pusha
538 push ds
539 push es
540 push fs
541 push gs
543 mov ax, 0x10
544 mov ds, ax
545 mov es, ax
546 mov fs, ax
547 mov gs, ax
549 mov eax, esp
551 push edx
552 push ecx
553 push ebx
554 push eax
556 call syscall_handler ; make call
558 ;add esp, byte 4
560 pop eax
561 pop ebx
562 pop ecx
563 pop edx
565 pop gs
566 pop fs
567 pop es
568 pop ds
570 popa
572 ;mov eax, 'D'
573 iret
576 ;IMP syscall_handler
577 ;EXP system_call
578 ; pusha
579 ; push ds
580 ; push es
581 ; push fs
582 ; push gs
583 ; push edx
584 ; push ecx
585 ; push ebx
587 ; mov ax, 0x10
588 ; mov ds, ax
589 ; mov es, ax
590 ; mov fs, ax
591 ; mov gs, ax
592 ; mov eax, esp
594 ; push eax
595 ; mov eax, syscall_handler
596 ; call eax
598 ; pop eax
599 ; pop ebx
600 ; pop ecx
601 ; pop edx
602 ; pop gs
603 ; pop fs
604 ; pop es
605 ; pop ds
606 ; popa
607 ;add esp, 8
608 ; iret
610 ; Here is the definition of our BSS section. Right now, we'll use
611 ; it just to store the stack. Remember that a stack actually grows
612 ; downwards, so we declare the size of the data before declaring
613 ; the identifier '_sys_stack'
614 SECTION .bss
615 resb 8192 ; This reserves 8KBytes of memory here
616 _sys_stack: