Build fixes
[tpg/acess2.git] / Kernel / arch / x86 / start.asm
1 ; AcessOS Microkernel Version
2 ; Start.asm
3
4 [bits 32]
5
6 KERNEL_BASE     equ 0xC0000000
7 %define MAX_CPUS        16
8
9 [extern __load_addr]
10 [extern __bss_start]
11 [extern gKernelEnd]
12 [section .multiboot]
13 mboot:
14         ; Multiboot macros to make a few lines later more readable
15         MULTIBOOT_PAGE_ALIGN    equ 1<<0
16         MULTIBOOT_MEMORY_INFO   equ 1<<1
17         MULTIBOOT_HEADER_MAGIC  equ 0x1BADB002
18         MULTIBOOT_HEADER_FLAGS  equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO
19         MULTIBOOT_CHECKSUM      equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
20         
21         ; This is the GRUB Multiboot header. A boot signature
22         dd MULTIBOOT_HEADER_MAGIC
23         dd MULTIBOOT_HEADER_FLAGS
24         dd MULTIBOOT_CHECKSUM
25         dd mboot; - KERNEL_BASE ;Location of Multiboot Header
26         
27 ; Multiboot 2 Header
28 ;mboot2:
29 ;       MULTIBOOT2_HEADER_MAGIC equ 0xE85250D6
30 ;       MULTIBOOT2_HEADER_ARCH  equ 0
31 ;       MULTIBOOT2_HEADER_LENGTH        equ (mboot2_end-mboot2)
32 ;       MULTIBOOT2_CHECKSUM     equ -(MULTIBOOT2_HEADER_MAGIC + MULTIBOOT2_HEADER_ARCH + MULTIBOOT2_HEADER_LENGTH)
33 ;       
34 ;       dd MULTIBOOT2_HEADER_MAGIC
35 ;       dd MULTIBOOT2_HEADER_ARCH
36 ;       dd MULTIBOOT2_HEADER_LENGTH
37 ;       dd MULTIBOOT2_CHECKSUM
38 ;       ; MBoot2 Address Header
39 ;       dw      2, 0
40 ;       dd      8 + 16
41 ;       dd      mboot2  ; Location of Multiboot Header
42 ;       dd      __load_addr - KERNEL_BASE       ; Kernel Load base
43 ;       dd      __bss_start - KERNEL_BASE       ; Kernel Data End
44 ;       dd      gKernelEnd - KERNEL_BASE        ; Kernel BSS End
45 ;       ; MBoot2 Entry Point Tag
46 ;       dw      3, 0
47 ;       dd      8 + 4
48 ;       dd      start - KERNEL_BASE
49 ;       ; MBoot2 Module Alignment Tag
50 ;       dw      6, 0
51 ;       dd      12      ; ???
52 ;       dd      0       ; Search me, seems it wants padding
53 ;       ; Terminator
54 ;       dw      0, 0
55 ;       dd      8
56 ;mboot2_end:
57         
58 [section .text]
59 [extern kmain]
60 [global start]
61 start:
62         
63         ; Set up stack
64         mov esp, Kernel_Stack_Top
65         
66         ; Start Paging
67         mov ecx, gaInitPageDir - KERNEL_BASE
68         mov cr3, ecx
69         
70         mov ecx, cr0
71         or      ecx, 0x80010000 ; PG and WP
72         mov cr0, ecx
73         
74         lea ecx, [.higherHalf]
75         jmp ecx
76 .higherHalf:
77
78         ; Call the kernel
79         push ebx        ; Multiboot Info
80         push eax        ; Multiboot Magic Value
81         call kmain
82
83         ; Halt the Machine
84         cli
85 .hlt:
86         hlt
87         jmp .hlt
88
89
90 ; Multiprocessing AP Startup Code (Must be within 0x10FFF0)
91 ;
92 %if USE_MP
93 [extern gGDT]
94 [extern gGDTPtr]
95 [extern gIDTPtr]
96 [extern gpMP_LocalAPIC]
97 [extern giMP_TimerCount]
98 [extern gaAPIC_to_CPU]
99 [extern gaCPUs]
100 [extern giNumInitingCPUs]
101 [extern MM_NewKStack]
102
103 lGDTPtr:        ; Local GDT Pointer
104         dw      3*8-1
105         dd      gGDT-KERNEL_BASE
106
107 [bits 16]
108 [global APWait]
109 APWait:
110         ;xchg bx, bx
111 .hlt:
112         ;hlt
113         jmp .hlt
114 [global APStartup]
115 APStartup:
116         ;xchg bx, bx    ; MAGIC BREAK!
117         mov ax, 0xFFFF
118         mov ds, ax
119         lgdt [DWORD ds:lGDTPtr-KERNEL_BASE-0xFFFF0]
120         mov eax, cr0
121         or      al, 1
122         mov     cr0, eax
123         jmp 08h:DWORD .ProtectedMode-KERNEL_BASE
124 [bits 32]
125 .ProtectedMode:
126         ; Load segment registers
127         mov ax, 0x10
128         mov ss, ax
129         mov ds, ax
130         mov es, ax
131         mov fs, ax
132         mov gs, ax
133         ; Start Paging
134         mov eax, gaInitPageDir - KERNEL_BASE
135         mov cr3, eax
136         mov eax, cr0
137         or      eax, 0x80010000 ; PG and WP
138         mov cr0, eax
139         ; Jump to higher half
140         lea eax, [.higherHalf]
141         jmp eax
142 .higherHalf:
143         ; Load True GDT & IDT
144         lgdt [gGDTPtr]
145         lidt [gIDTPtr]
146
147         mov ebp, [gpMP_LocalAPIC]
148         mov ebx, [ebp+0x20]     ; Read ID
149         shr ebx, 24
150         ;xchg bx, bx    ; MAGIC BREAK
151         ; CL is now local APIC ID
152         mov cl, BYTE [gaAPIC_to_CPU+ebx]
153         xor ebx, ebx
154         mov bl, cl
155         ; BL is now the CPU ID
156         mov BYTE [gaCPUs+ebx*8+1], 1
157         ; Decrement the remaining CPU count
158         dec DWORD [giNumInitingCPUs]
159         
160         ; Create a stack
161         lea edx, [ebx+1]
162         shl edx, 5+2    ; *32 *4
163         lea esp, [gInitAPStacks+edx]
164         call MM_NewKStack
165         mov esp, eax
166         
167         ; Set TSS
168         lea ecx, [ebx*8+0x30]
169         ltr cx
170         ; Save the CPU number to a debug register
171         mov dr1, ebx
172         
173         ;xchg bx, bx    ; MAGIC_BREAK
174         ; Enable Local APIC
175         mov DWORD [ebp+0x0F0], 0x000001EF       ; Spurious Interrupt Vector (0xEF)
176         mov ecx, DWORD [giMP_TimerCount]
177         mov DWORD [ebp+0x380], ecx      ; Set Initial Count
178         mov DWORD [ebp+0x320], 0x000200EE       ; Enable timer on IVT#0xEE
179         mov DWORD [ebp+0x330], 0x000100E0       ; ##Enable thermal sensor on IVT#0xE0
180         mov DWORD [ebp+0x340], 0x000100D0       ; ##Enable performance counters on IVT#0xD0
181         mov DWORD [ebp+0x350], 0x000100D1       ; ##Enable LINT0 on IVT#0xD1
182         mov DWORD [ebp+0x360], 0x000100D2       ; ##Enable LINT1 on IVT#0xD2
183         mov DWORD [ebp+0x370], 0x000100E1       ; ##Enable Error on IVT#0xE1
184         mov DWORD [ebp+0x0B0], 0        ; Send an EOI (just in case)
185         
186         ; CPU is now marked as initialised
187         sti
188         ;xchg bx, bx    ; MAGIC BREAK
189 .hlt:
190         hlt
191         jmp .hlt
192 %endif
193
194 [global GetEIP]
195 GetEIP:
196         mov eax, [esp]
197         ret
198
199 ; int CallWithArgArray(void *Ptr, int NArgs, Uint *Args)
200 ; Call a function passing the array as arguments
201 [global CallWithArgArray]
202 CallWithArgArray:
203         push ebp
204         mov ebp, esp
205         mov ecx, [ebp+12]       ; Get NArgs
206         mov edx, [ebp+16]
207
208 .top:
209         mov eax, [edx+ecx*4-4]
210         push eax
211         loop .top
212         
213         mov eax, [ebp+8]
214         call eax
215         lea esp, [ebp]
216         pop ebp
217         ret
218
219 [section .initpd]
220 [global gaInitPageDir]
221 [global gaInitPageTable]
222 align 0x1000
223 gaInitPageDir:
224         dd      gaInitPageTable-KERNEL_BASE+3   ; 0x00
225         times 1024-256-1        dd      0
226         dd      gaInitPageTable-KERNEL_BASE+3   ; 0xC0
227         times 256-1     dd      0
228 align 0x1000
229 gaInitPageTable:
230         %assign i 0
231         %rep 1024
232         dd      i*0x1000+3
233         %assign i i+1
234         %endrep
235 [global Kernel_Stack_Top]
236 ALIGN 0x1000
237         times 1024      dd      0
238 Kernel_Stack_Top:
239 gInitAPStacks:
240         times 32*MAX_CPUS       dd      0
241         

UCC git Repository :: git.ucc.asn.au