302d1c3c089d2c7eb7bb341a79313a99050a5dc7
[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
8 [extern __load_addr]
9 [extern __bss_start]
10 [extern gKernelEnd]
11 [section .multiboot]
12 mboot:
13         ; Multiboot macros to make a few lines later more readable
14         MULTIBOOT_PAGE_ALIGN    equ 1<<0
15         MULTIBOOT_MEMORY_INFO   equ 1<<1
16         MULTIBOOT_HEADER_MAGIC  equ 0x1BADB002
17         MULTIBOOT_HEADER_FLAGS  equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO
18         MULTIBOOT_CHECKSUM      equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
19         
20         ; This is the GRUB Multiboot header. A boot signature
21         dd MULTIBOOT_HEADER_MAGIC
22         dd MULTIBOOT_HEADER_FLAGS
23         dd MULTIBOOT_CHECKSUM
24         dd mboot - KERNEL_BASE  ;Location of Multiboot Header
25         
26 ; Multiboot 2 Header
27 ;mboot2:
28 ;       MULTIBOOT2_HEADER_MAGIC equ 0xE85250D6
29 ;       MULTIBOOT2_HEADER_ARCH  equ 0
30 ;       MULTIBOOT2_HEADER_LENGTH        equ (mboot2_end-mboot2)
31 ;       MULTIBOOT2_CHECKSUM     equ -(MULTIBOOT2_HEADER_MAGIC + MULTIBOOT2_HEADER_ARCH + MULTIBOOT2_HEADER_LENGTH)
32 ;       
33 ;       dd MULTIBOOT2_HEADER_MAGIC
34 ;       dd MULTIBOOT2_HEADER_ARCH
35 ;       dd MULTIBOOT2_HEADER_LENGTH
36 ;       dd MULTIBOOT2_CHECKSUM
37 ;       ; MBoot2 Address Header
38 ;       dw      2, 0
39 ;       dd      8 + 16
40 ;       dd      mboot2  ; Location of Multiboot Header
41 ;       dd      __load_addr - KERNEL_BASE       ; Kernel Load base
42 ;       dd      __bss_start - KERNEL_BASE       ; Kernel Data End
43 ;       dd      gKernelEnd - KERNEL_BASE        ; Kernel BSS End
44 ;       ; MBoot2 Entry Point Tag
45 ;       dw      3, 0
46 ;       dd      8 + 4
47 ;       dd      start - KERNEL_BASE
48 ;       ; MBoot2 Module Alignment Tag
49 ;       dw      6, 0
50 ;       dd      12      ; ???
51 ;       dd      0       ; Search me, seems it wants padding
52 ;       ; Terminator
53 ;       dw      0, 0
54 ;       dd      8
55 ;mboot2_end:
56         
57 [section .text]
58 [extern kmain]
59 [global start]
60 start:
61         
62         ; Set up stack
63         mov esp, Kernel_Stack_Top
64         
65         ; Start Paging
66         mov ecx, gaInitPageDir - KERNEL_BASE
67         mov cr3, ecx
68         
69         mov ecx, cr0
70         or      ecx, 0x80010000 ; PG and WP
71         mov cr0, ecx
72         
73         lea ecx, [.higherHalf]
74         jmp ecx
75 .higherHalf:
76
77         ; Call the kernel
78         push ebx        ; Multiboot Info
79         push eax        ; Multiboot Magic Value
80         call kmain
81
82         ; Halt the Machine
83         cli
84 .hlt:
85         hlt
86         jmp .hlt
87
88
89 ; Multiprocessing AP Startup Code (Must be within 0x10FFF0)
90 ;
91 %if USE_MP
92 [extern gGDT]
93 [extern gGDTPtr]
94 [extern gIDTPtr]
95 [extern gpMP_LocalAPIC]
96 [extern gaAPIC_to_CPU]
97 [extern gaCPUs]
98 [extern giNumInitingCPUs]
99 lGDTPtr:        ; Local GDT Pointer
100         dw      3*8-1
101         dd      gGDT-KERNEL_BASE
102
103 [bits 16]
104 [global APWait]
105 APWait:
106         ;xchg bx, bx
107 .hlt:
108         ;hlt
109         jmp .hlt
110 [global APStartup]
111 APStartup:
112         ;xchg bx, bx    ; MAGIC BREAK!
113         mov ax, 0xFFFF
114         mov ds, ax
115         lgdt [DWORD ds:lGDTPtr-KERNEL_BASE-0xFFFF0]
116         mov eax, cr0
117         or      al, 1
118         mov     cr0, eax
119         jmp 08h:DWORD .ProtectedMode-KERNEL_BASE
120 [bits 32]
121 .ProtectedMode:
122         ; Load segment registers
123         mov ax, 0x10
124         mov ss, ax
125         mov ds, ax
126         mov es, ax
127         mov fs, ax
128         mov gs, ax
129         ; Start Paging
130         mov eax, gaInitPageDir - KERNEL_BASE
131         mov cr3, eax
132         mov eax, cr0
133         or      eax, 0x80010000 ; PG and WP
134         mov cr0, eax
135         ; Jump to higher half
136         lea eax, [.higherHalf]
137         jmp eax
138 .higherHalf:
139         ; Load True GDT & IDT
140         lgdt [gGDTPtr]
141         lidt [gIDTPtr]
142
143         mov eax, [gpMP_LocalAPIC]
144         mov ecx, [eax+0x20]     ; Read ID
145         shr ecx, 24
146         ;xchg bx, bx    ; MAGIC BREAK
147         ; CL is now local APIC ID
148         mov cl, BYTE [gaAPIC_to_CPU+ecx]
149         ; CL is now the CPU ID
150         mov BYTE [gaCPUs+ecx*8+1], 1
151         ; Decrement the remaining CPU count
152         dec DWORD [giNumInitingCPUs]
153         ; Set TSS
154         shl cx, 3
155         add cx, 0x30
156         ltr cx
157         ; CPU is now marked as initialised
158         sti
159         ;xchg bx, bx    ; MAGIC BREAK
160 .hlt:
161         hlt
162         jmp .hlt
163 %endif
164
165 [global GetEIP]
166 GetEIP:
167         mov eax, [esp]
168         ret
169
170 ; int CallWithArgArray(void *Ptr, int NArgs, Uint *Args)
171 ; Call a function passing the array as arguments
172 [global CallWithArgArray]
173 CallWithArgArray:
174         push ebp
175         mov ebp, esp
176         mov ecx, [ebp+12]       ; Get NArgs
177         mov edx, [ebp+16]
178
179 .top:
180         mov eax, [edx+ecx*4-4]
181         push eax
182         loop .top
183         
184         mov eax, [ebp+8]
185         call eax
186         lea esp, [ebp]
187         pop ebp
188         ret
189
190 [section .initpd]
191 [global gaInitPageDir]
192 [global gaInitPageTable]
193 align 0x1000
194 gaInitPageDir:
195         dd      gaInitPageTable-KERNEL_BASE+3   ; 0x00
196         times 1024-256-1        dd      0
197         dd      gaInitPageTable-KERNEL_BASE+3   ; 0xC0
198         times 256-1     dd      0
199 align 0x1000
200 gaInitPageTable:
201         %assign i 0
202         %rep 1024
203         dd      i*0x1000+3
204         %assign i i+1
205         %endrep
206 [global Kernel_Stack_Top]
207 ALIGN 0x1000
208         times 1024      dd      0
209 Kernel_Stack_Top:
210         

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