3 ; Copyright (c) 2001,2004 David H. Hovemeyer <daveho@cs.umd.edu>
6 ; This is free software. You are permitted to use,
7 ; redistribute, and modify it as specified in the file "COPYING".
9 ; A lot of this code is adapted from Kernel Toolkit 0.2
10 ; and Linux version 2.2.x, so the following copyrights apply:
12 ; Copyright (C) 1991, 1992 Linus Torvalds
13 ; modified by Drew Eckhardt
14 ; modified by Bruce Evans (bde)
15 ; adapted for Kernel Toolkit by Luigi Sgro
20 ; DON'T EVER INCLUDE CODE AT THE TOP LEVEL AGAIN
28 ; Redefine the data segment so we can access variables
29 ; declared in this file.
34 ; Use int 15h to find out size of extended memory in KB.
35 ; Extended memory is the memory above 1MB. So by
36 ; adding 1MB to this amount, we get the total amount
37 ; of system memory. We can only detect 64MB this way,
38 ; but that's OK for now.
41 ;add ax, 1024 ; 1024 KB == 1 MB
44 add ax, 1024 ; 1024 KB == 1 MB
45 mov [mem_size_kbytes], ax
46 mov [mem_size_eblocks], bx
48 ; Kill the floppy motor.
53 ; Block interrupts, since we can't meaningfully handle them yet
54 ; and we no longer need BIOS services.
57 ; Set up IDT and GDT registers
61 ; Initialize the interrupt controllers, and enable the
66 ; Switch to protected mode!
70 ; Jump to 32 bit code.
71 jmp dword KERNEL_CS:(SETUPSEG << 4) + setup_32
77 ; set up data segment registers
85 ; Create the stack for the initial kernel thread.
86 mov esp, KERN_STACK + 4096
88 ; Build Boot_Info struct on stack.
89 ; Note that we push the fields on in reverse order,
90 ; since the stack grows downwards.
92 mov ax, [(SETUPSEG<<4)+mem_size_kbytes]
94 mov bx, [(SETUPSEG<<4)+mem_size_eblocks]
98 push dword 8 ; bootInfoSize
100 ; Pass pointer to Boot_Info struct as argument to kernel
104 ; Push return address to make this look like a call
106 push dword (SETUPSEG<<4)+.returnAddr
110 ; Far jump into kernel
111 jmp KERNEL_CS:ENTRY_POINT
114 ; We shouldn't return here.
127 ; Kill the floppy motor.
128 ; This code was shamelessly stolen from Linux.
136 ; Initialize master and slave PIC!
138 out 0x20, al ; ICW1 to master
140 out 0xA0, al ; ICW1 to slave
143 out 0x21, al ; ICW2 to master
146 out 0xA1, al ; ICW2 to slave
149 out 0x21, al ; ICW3 to master
152 out 0xA1, al ; ICW3 to slave
155 out 0x21, al ; ICW4 to master
157 out 0xA1, al ; ICW4 to slave
159 mov al, 0xff ; mask all ints in slave
160 out 0xA1, al ; OCW1 to slave
162 mov al, 0xfb ; mask all ints but 2 in master
163 out 0x21, al ; OCW1 to master
167 ; Linux uses this code.
168 ; The idea is that some systems issue port I/O instructions
169 ; faster than the device hardware can deal with them.
174 ; Enable the A20 address line, so we can correctly address
186 ; ----------------------------------------------------------------------
188 ; ----------------------------------------------------------------------
190 mem_size_kbytes: dw 0
191 mem_size_eblocks: dw 0
193 ; ----------------------------------------------------------------------
194 ; The GDT. Creates flat 32-bit address space for the kernel
195 ; code, data, and stack. Note that this GDT is just used
196 ; to create an environment where we can start running 32 bit
197 ; code. The kernel will create and manage its own GDT.
198 ; ----------------------------------------------------------------------
200 ; GDT initialization stuff
201 NUM_GDT_ENTRIES equ 3 ; number of entries in GDT
202 GDT_ENTRY_SZ equ 8 ; size of a single GDT entry
206 ; Descriptor 0 is not used
212 ; Descriptor 1: kernel code segment
213 dw 0xFFFF ; bytes 0 and 1 of segment size
214 dw 0x0000 ; bytes 0 and 1 of segment base address
215 db 0x00 ; byte 2 of segment base address
216 db 0x9A ; present, DPL=0, non-system, code, non-conforming,
217 ; readable, not accessed
218 db 0xCF ; granularity=page, 32 bit code, upper nibble of size
219 db 0x00 ; byte 3 of segment base address
221 ; Descriptor 2: kernel data and stack segment
222 ; NOTE: what Intel calls an "expand-up" segment
223 ; actually means that the stack will grow DOWN,
224 ; towards lower memory. So, we can use this descriptor
225 ; for both data and stack references.
226 dw 0xFFFF ; bytes 0 and 1 of segment size
227 dw 0x0000 ; bytes 0 and 1 of segment base address
228 db 0x00 ; byte 2 of segment base address
229 db 0x92 ; present, DPL=0, non-system, data, expand-up,
230 ; writable, not accessed
231 db 0xCF ; granularity=page, big, upper nibble of size
232 db 0x00 ; byte 3 of segment base address
235 dw NUM_GDT_ENTRIES*GDT_ENTRY_SZ ; limit
236 dd (SETUPSEG<<4) + GDT ; base address