Increased user stack size, fiddling with error recovery
[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      2*8-1
101         dd      gGDT-KERNEL_BASE
102
103 [bits 16]
104 [global APStartup]
105 APStartup:
106         xchg bx, bx     ; MAGIC BREAK!
107         mov ax, 0xFFFF
108         mov ds, ax
109         lgdt [DWORD ds:lGDTPtr-KERNEL_BASE-0xFFFF0]
110         mov eax, cr0
111         or      al, 1
112         mov     cr0, eax
113         jmp 08h:DWORD .ProtectedMode-KERNEL_BASE
114 [bits 32]
115 .ProtectedMode:
116         ; Start Paging
117         mov eax, gaInitPageDir - KERNEL_BASE
118         mov cr3, eax
119         mov eax, cr0
120         or      eax, 0x80010000 ; PG and WP
121         mov cr0, eax
122         ; Jump to higher half
123         lea eax, [.higherHalf]
124         jmp eax
125 .higherHalf:
126         ; Load True GDT & IDT
127         lgdt [gGDTPtr]
128         lidt [gIDTPtr]
129
130         mov eax, [gpMP_LocalAPIC]
131         mov DWORD [eax], 0
132         xor ecx, ecx
133         mov cl, BYTE [eax+0x10]
134         ; CL is now local APIC ID
135         mov cl, BYTE [gaAPIC_to_CPU+ecx]
136         ; CL is now the CPU ID
137         mov BYTE [gaCPUs+ecx*8+1], 1
138         ; Decrement the remaining CPU count
139         dec DWORD [giNumInitingCPUs]
140         ; CPU is now marked as initialised
141         sti
142 .hlt:
143         hlt
144         jmp .hlt
145 %endif
146
147 [global GetEIP]
148 GetEIP:
149         mov eax, [esp]
150         ret
151
152 ; int CallWithArgArray(void *Ptr, int NArgs, Uint *Args)
153 ; Call a function passing the array as arguments
154 [global CallWithArgArray]
155 CallWithArgArray:
156         push ebp
157         mov ebp, esp
158         mov ecx, [ebp+12]       ; Get NArgs
159         mov edx, [ebp+16]
160
161 .top:
162         mov eax, [edx+ecx*4-4]
163         push eax
164         loop .top
165         
166         mov eax, [ebp+8]
167         call eax
168         lea esp, [ebp]
169         pop ebp
170         ret
171
172 [section .initpd]
173 [global gaInitPageDir]
174 [global gaInitPageTable]
175 align 0x1000
176 gaInitPageDir:
177         dd      gaInitPageTable-KERNEL_BASE+3   ; 0x00
178         times 1024-256-1        dd      0
179         dd      gaInitPageTable-KERNEL_BASE+3   ; 0xC0
180         times 256-1     dd      0
181 align 0x1000
182 gaInitPageTable:
183         %assign i 0
184         %rep 1024
185         dd      i*0x1000+3
186         %assign i i+1
187         %endrep
188 [global Kernel_Stack_Top]
189 ALIGN 0x1000
190         times 1024      dd      0
191 Kernel_Stack_Top:
192         

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