1 ; =============================================================================
2 ; BareMetal -- a 64-bit OS written in Assembly for x86-64 systems
3 ; Copyright (C) 2008-2011 Return Infinity -- see LICENSE.TXT
6 ; =============================================================================
14 ; Make sure that memory range 0x110000 - 0x200000 is cleared
15 mov rdi
, os_SystemVariables
21 cmp rcx
, 122880 ; Clear 960 KiB
24 mov ax, 0x000A ; Set the cursor to 0,10 and clear the screen (needs to happen before anything is printed to the screen)
27 xor rdi
, rdi
; create the 64-bit IDT (at linear address 0x0000000000000000) as defined by Pure64
29 ; Create exception gate stubs (Pure64 has already set the correct gate markers)
31 mov rax
, exception_gate
32 make_exception_gate_stubs:
36 jnz make_exception_gate_stubs
38 ; Create interrupt gate stubs (Pure64 has already set the correct gate markers)
40 mov rax
, interrupt_gate
41 make_interrupt_gate_stubs:
45 jnz make_interrupt_gate_stubs
47 ; Set up the exception gates for all of the CPU exceptions
50 mov rax
, exception_gate_00
54 add rax
, 16 ; The exception gates are aligned at 16 bytes
56 jnz make_exception_gates
58 ; Set up the IRQ handlers
76 ; Rate defines how often the RTC interrupt is triggered
77 ; Rate is a 4-bit value from 1 to 15. 1 = 32768Hz, 6 = 1024Hz, 15 = 2Hz
78 ; RTC value must stay at 32.768KHz or the computer will not keep the correct time
79 ; http://wiki.osdev.org/RTC
82 mov al, 00101101b ; RTC@32.768KHz (0010), Rate@8Hz (1101)
86 mov al, 01000010b ; Periodic(6), 24H clock(2)
88 mov al, 0x0C ; Acknowledge the RTC
92 mov al, 0x0B ; Set RTC to binary mode
116 mov dx, 0x03C8 ; DAC Address Write Mode Register
118 mov dx, 0x03C9 ; DAC Data Register
119 mov rbx
, 16 ; 16 lines
121 mov rcx
, 16 ; 16 colors
135 jne nextline
; Set the next 16 colors to the same
136 mov eax, 0x14 ; Fix for color 6
137 mov dx, 0x03c8 ; DAC Address Write Mode Register
139 mov dx, 0x03c9 ; DAC Data Register
149 ; Grab data from Pure64's infomap
152 mov [os_LocalAPICAddress
], rax
154 mov [os_IOAPICAddress
], rax
158 mov [os_NumCores
], ax
162 mov [os_MemAmount
], ax ; In MiB's
164 ; Build the OS memory table
167 ; Initialize all AP's to run our reset code. Skip the BSP
170 mov rsi
, 0x0000000000005700 ; Location in memory of the Pure64 CPU data
173 cmp rsi
, 0x0000000000005800 ; Enable up to 256 CPU Cores
175 lodsb ; Load the CPU parameters
176 bt rax
, 0 ; Check if the CPU is enabled
178 bt rax
, 1 ; Test to see if this is the BSP (Do not init!)
181 call os_smp_reset
; Reset the CPU
188 ; Enable specific interrupts
190 mov al, 11111001b ; Enable Cascade, Keyboard
193 mov al, 11111110b ; Enable RTC
196 call os_seed_random
; Seed the RNG
198 ; Reset keyboard and empty the buffer
199 mov al, 0x20 ; Command to read byte of keyboard controller RAM
200 out 0x64, al ; Send command
201 in al, 0x60 ; Grab the keyboard controller command byte
202 or al, 00000001b ; Enable interrupts
203 and al, 11101111b ; Enable keyboard
205 mov al, 0x60 ; Command to write byte of keyboard controller RAM
206 out 0x64, al ; Send command
208 out 0x60, al ; Send new keyboard controller command byte
213 ; rax = address of handler
214 ; rdi = gate # to configure
219 shl rdi
, 4 ; quickly multiply rdi by 16
220 stosw ; store the low word (15..0)
222 add rdi
, 4 ; skip the gate marker
223 stosw ; store the high word (31..16)
225 stosd ; store the high dword (63..32)
232 init_memory_map: ; Build the OS memory table
237 ; Build a fresh memory map for the system
238 mov rdi
, os_MemoryMap
241 mov cx, [os_MemAmount
]
242 shr cx, 1 ; Divide actual memory by 2
247 stosb ; Mark the first 2 MiB as in use (by Kernel and system buffers)
248 stosb ; As well as the second 2 MiB (by loaded application)
249 ; The CLI should take care of the Application memory
251 ; Allocate memory for CPU stacks (2 MiB's for each core)
253 mov cx, [os_NumCores
] ; Get the amount of cores in the system
254 call os_mem_allocate
; Allocate a page for each core
255 cmp rcx
, 0 ; os_mem_allocate returns 0 on failure
258 mov [os_StackBase
], rax
; Store the Stack base address
269 mov rsi
, memory_message
271 call os_print_string_with_color
274 jmp system_failure_hang
277 ; =============================================================================