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!
41 mov esp, _sys_stack
; This points the stack to our new stack area
44 ; This part MUST be 4byte aligned, so we solve that issue using 'ALIGN 4'
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
)
56 ; This is the GRUB Multiboot header. A boot signature
57 dd MULTIBOOT_HEADER_MAGIC
58 dd MULTIBOOT_HEADER_FLAGS
61 ; AOUT kludge - must be physical addresses. Make a note of these:
62 ; The linker script fills in the data for these ones!
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
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
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
91 jmp 0x08:flush2
; 0x08 is the offset to our code segment: Far jump!
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();'
102 ; 0: Divide By Zero Exception
116 ; 2: Non Maskable Interrupt Exception
137 ; 5: Out of Bounds Exception
144 ; 6: Invalid Opcode Exception
151 ; 7: Coprocessor Not Available Exception
158 ; 8: Double Fault Exception (With Error Code!)
164 ; 9: Coprocessor Segment Overrun Exception
171 ; 10: Bad TSS Exception (With Error Code!)
177 ; 11: Segment Not Present Exception (With Error Code!)
183 ; 12: Stack Fault Exception (With Error Code!)
189 ; 13: General Protection Fault Exception (With Error Code!)
195 ; 14: Page Fault Exception (With Error Code!)
201 ; 15: Reserved Exception
208 ; 16: Floating Point Exception
215 ; 17: Alignment Check Exception
222 ; 18: Machine Check Exception
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.
342 mov eax, fault_handler
518 ; syscall with 0 args
522 ; push ds ; switch to kernel space
528 ; call syscall_handler ; make call
530 ; pop es ; back to userland
556 call syscall_handler
; make call
595 ; mov eax, syscall_handler
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'
615 resb
8192 ; This reserves 8KBytes of memory here