Bugfixes, Cleanup and Rewite started on UDI interface
[tpg/acess2.git] / Kernel / arch / x86 / start.asm
1 ; AcessOS Microkernel Version\r
2 ; Start.asm\r
3 \r
4 [bits 32]\r
5 \r
6 KERNEL_BASE     equ 0xC0000000\r
7 \r
8 [section .multiboot]\r
9 mboot:\r
10     ; Multiboot macros to make a few lines later more readable\r
11     MULTIBOOT_PAGE_ALIGN        equ 1<<0\r
12     MULTIBOOT_MEMORY_INFO       equ 1<<1\r
13     MULTIBOOT_HEADER_MAGIC      equ 0x1BADB002\r
14     MULTIBOOT_HEADER_FLAGS      equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO\r
15     MULTIBOOT_CHECKSUM  equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)\r
16         \r
17     ; This is the GRUB Multiboot header. A boot signature\r
18     dd MULTIBOOT_HEADER_MAGIC\r
19     dd MULTIBOOT_HEADER_FLAGS\r
20     dd MULTIBOOT_CHECKSUM\r
21         dd mboot - KERNEL_BASE  ;Location of Multiboot Header\r
22         \r
23 [section .text]\r
24 [extern kmain]\r
25 [global start]\r
26 start:\r
27         ; Set up stack\r
28         mov esp, Kernel_Stack_Top\r
29         \r
30         ; Start Paging\r
31         mov ecx, gaInitPageDir - KERNEL_BASE\r
32         mov cr3, ecx\r
33         \r
34         mov ecx, cr0\r
35         or      ecx, 0x80010000 ; PG and WP\r
36         mov cr0, ecx\r
37         \r
38         lea ecx, [.higherHalf]\r
39         jmp ecx\r
40 .higherHalf:\r
41 \r
42         ; Call the kernel\r
43         push ebx        ; Multiboot Info\r
44         push eax        ; Multiboot Magic Value\r
45         call kmain
46
47         ; Halt the Machine\r
48         cli\r
49 .hlt:\r
50         hlt\r
51         jmp .hlt\r
52 \r
53\r
54 ; Multiprocessing AP Startup Code (Must be within 0x10FFF0)\r
55 ;\r
56 %if USE_MP\r
57 [extern gGDT]\r
58 [extern gGDTPtr]\r
59 [extern gIDTPtr]\r
60 [extern gpMP_LocalAPIC]\r
61 [extern gaAPIC_to_CPU]\r
62 [extern gaCPUs]\r
63 [extern giNumInitingCPUs]\r
64 lGDTPtr:        ; Local GDT Pointer\r
65         dw      2*8-1\r
66         dd      gGDT-KERNEL_BASE\r
67 \r
68 [bits 16]\r
69 [global APStartup]\r
70 APStartup:\r
71         xchg bx, bx     ; MAGIC BREAK!\r
72         mov ax, 0xFFFF\r
73         mov ds, ax\r
74         lgdt [DWORD ds:lGDTPtr-KERNEL_BASE-0xFFFF0]\r
75         mov eax, cr0\r
76         or      al, 1\r
77         mov     cr0, eax\r
78         jmp 08h:DWORD .ProtectedMode-KERNEL_BASE\r
79 [bits 32]\r
80 .ProtectedMode:\r
81         ; Start Paging\r
82         mov eax, gaInitPageDir - KERNEL_BASE\r
83         mov cr3, eax\r
84         mov eax, cr0\r
85         or      eax, 0x80010000 ; PG and WP\r
86         mov cr0, eax\r
87         ; Jump to higher half\r
88         lea eax, [.higherHalf]\r
89         jmp eax\r
90 .higherHalf:\r
91         ; Load True GDT & IDT\r
92         lgdt [gGDTPtr]\r
93         lidt [gIDTPtr]\r
94 \r
95         mov eax, [gpMP_LocalAPIC]\r
96         mov DWORD [eax], 0\r
97         xor ecx, ecx\r
98         mov cl, BYTE [eax+0x10]\r
99         ; CL is now local APIC ID\r
100         mov cl, BYTE [gaAPIC_to_CPU+ecx]\r
101         ; CL is now the CPU ID\r
102         mov BYTE [gaCPUs+ecx*8+1], 1\r
103         ; Decrement the remaining CPU count\r
104         dec DWORD [giNumInitingCPUs]\r
105         ; CPU is now marked as initialised\r
106         sti\r
107 .hlt:\r
108         hlt\r
109         jmp .hlt\r
110 %endif\r
111 \r
112 [global GetEIP]\r
113 GetEIP:\r
114         mov eax, [esp]\r
115         ret\r
116 \r
117 [extern Proc_Clone]\r
118 [extern Threads_Exit]\r
119 [global SpawnTask]\r
120 SpawnTask:\r
121         ; Call Proc_Clone with Flags=0\r
122         xor eax, eax\r
123         push eax
124         push eax\r
125         call Proc_Clone\r
126         add esp, 8      ; Remove arguments from stack\r
127         \r
128         test eax, eax\r
129         jnz .parent\r
130         \r
131         ; In child, so now set up stack frame\r
132         mov ebx, [esp+4]        ; Child Function\r
133         mov edx, [esp+8]        ; Argument\r
134         ; Child\r
135         push edx        ; Argument\r
136         call ebx        ; Function\r
137         call Threads_Exit       ; Kill Thread\r
138         \r
139 .parent:\r
140         ret\r
141 \r
142 [section .initpd]\r
143 [global gaInitPageDir]\r
144 [global gaInitPageTable]\r
145 align 0x1000\r
146 gaInitPageDir:\r
147         dd      gaInitPageTable-KERNEL_BASE+3   ; 0x00\r
148         times 1024-256-1        dd      0\r
149         dd      gaInitPageTable-KERNEL_BASE+3   ; 0xC0\r
150         times 256-1     dd      0\r
151 align 0x1000\r
152 gaInitPageTable:\r
153         %assign i 0\r
154         %rep 1024\r
155         dd      i*0x1000+3\r
156         %assign i i+1\r
157         %endrep\r
158 [global Kernel_Stack_Top]\r
159 ALIGN 0x1000\r
160         times 1024      dd      0\r
161 Kernel_Stack_Top:\r
162         

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