Fixed the EnvP array passed to Proc_StartUser not being NULL terminated
[tpg/acess2.git] / Kernel / arch / x86 / start.asm
index 4135160..8b3e18c 100644 (file)
@@ -5,21 +5,55 @@
 \r
 KERNEL_BASE    equ 0xC0000000\r
 \r
+[extern __load_addr]\r
+[extern __bss_start]\r
+[extern gKernelEnd]\r
 [section .multiboot]\r
 mboot:\r
-    ; Multiboot macros to make a few lines later more readable\r
-    MULTIBOOT_PAGE_ALIGN       equ 1<<0\r
-    MULTIBOOT_MEMORY_INFO      equ 1<<1\r
-    MULTIBOOT_HEADER_MAGIC     equ 0x1BADB002\r
-    MULTIBOOT_HEADER_FLAGS     equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO\r
-    MULTIBOOT_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)\r
+       ; Multiboot macros to make a few lines later more readable\r
+       MULTIBOOT_PAGE_ALIGN    equ 1<<0\r
+       MULTIBOOT_MEMORY_INFO   equ 1<<1\r
+       MULTIBOOT_HEADER_MAGIC  equ 0x1BADB002\r
+       MULTIBOOT_HEADER_FLAGS  equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO\r
+       MULTIBOOT_CHECKSUM      equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)\r
        \r
-    ; This is the GRUB Multiboot header. A boot signature\r
-    dd MULTIBOOT_HEADER_MAGIC\r
-    dd MULTIBOOT_HEADER_FLAGS\r
-    dd MULTIBOOT_CHECKSUM\r
+       ; This is the GRUB Multiboot header. A boot signature\r
+       dd MULTIBOOT_HEADER_MAGIC\r
+       dd MULTIBOOT_HEADER_FLAGS\r
+       dd MULTIBOOT_CHECKSUM\r
        dd mboot - KERNEL_BASE  ;Location of Multiboot Header\r
        \r
+; Multiboot 2 Header\r
+mboot2:\r
+       MULTIBOOT2_HEADER_MAGIC equ 0xE85250D6\r
+       MULTIBOOT2_HEADER_ARCH  equ 0\r
+       MULTIBOOT2_HEADER_LENGTH        equ (mboot2_end-mboot2)\r
+       MULTIBOOT2_CHECKSUM     equ -(MULTIBOOT2_HEADER_MAGIC + MULTIBOOT2_HEADER_ARCH + MULTIBOOT2_HEADER_LENGTH)\r
+       \r
+       dd MULTIBOOT2_HEADER_MAGIC\r
+       dd MULTIBOOT2_HEADER_ARCH\r
+       dd MULTIBOOT2_HEADER_LENGTH\r
+       dd MULTIBOOT2_CHECKSUM\r
+       ; MBoot2 Address Header\r
+       dw      2, 0\r
+       dd      8 + 16\r
+       dd      mboot2  ; Location of Multiboot Header\r
+       dd      __load_addr - KERNEL_BASE       ; Kernel Load base\r
+       dd      __bss_start - KERNEL_BASE       ; Kernel Data End\r
+       dd      gKernelEnd - KERNEL_BASE        ; Kernel BSS End\r
+       ; MBoot2 Entry Point Tag\r
+       dw      3, 0\r
+       dd      8 + 4\r
+       dd      start - KERNEL_BASE\r
+       ; MBoot2 Module Alignment Tag\r
+       dw      6, 0\r
+       dd      12      ; ???\r
+       dd      0       ; Search me, seems it wants padding\r
+       ; Terminator\r
+       dw      0, 0\r
+       dd      8\r
+mboot2_end:\r
+       \r
 [section .text]\r
 [extern kmain]\r
 [global start]\r
@@ -39,8 +73,6 @@ start:
        jmp ecx\r
 .higherHalf:\r
 \r
-       mov DWORD [gaInitPageDir], 0\r
-\r
        ; Call the kernel\r
        push ebx        ; Multiboot Info\r
        push eax        ; Multiboot Magic Value\r
@@ -52,34 +84,88 @@ start:
        hlt\r
        jmp .hlt\r
 \r
+; \r
+; Multiprocessing AP Startup Code (Must be within 0x10FFF0)\r
+;\r
+%if USE_MP\r
+[extern gGDT]\r
+[extern gGDTPtr]\r
+[extern gIDTPtr]\r
+[extern gpMP_LocalAPIC]\r
+[extern gaAPIC_to_CPU]\r
+[extern gaCPUs]\r
+[extern giNumInitingCPUs]\r
+lGDTPtr:       ; Local GDT Pointer\r
+       dw      2*8-1\r
+       dd      gGDT-KERNEL_BASE\r
+\r
+[bits 16]\r
+[global APStartup]\r
+APStartup:\r
+       xchg bx, bx     ; MAGIC BREAK!\r
+       mov ax, 0xFFFF\r
+       mov ds, ax\r
+       lgdt [DWORD ds:lGDTPtr-KERNEL_BASE-0xFFFF0]\r
+       mov eax, cr0\r
+       or      al, 1\r
+       mov     cr0, eax\r
+       jmp 08h:DWORD .ProtectedMode-KERNEL_BASE\r
+[bits 32]\r
+.ProtectedMode:\r
+       ; Start Paging\r
+       mov eax, gaInitPageDir - KERNEL_BASE\r
+       mov cr3, eax\r
+       mov eax, cr0\r
+       or      eax, 0x80010000 ; PG and WP\r
+       mov cr0, eax\r
+       ; Jump to higher half\r
+       lea eax, [.higherHalf]\r
+       jmp eax\r
+.higherHalf:\r
+       ; Load True GDT & IDT\r
+       lgdt [gGDTPtr]\r
+       lidt [gIDTPtr]\r
+\r
+       mov eax, [gpMP_LocalAPIC]\r
+       mov DWORD [eax], 0\r
+       xor ecx, ecx\r
+       mov cl, BYTE [eax+0x10]\r
+       ; CL is now local APIC ID\r
+       mov cl, BYTE [gaAPIC_to_CPU+ecx]\r
+       ; CL is now the CPU ID\r
+       mov BYTE [gaCPUs+ecx*8+1], 1\r
+       ; Decrement the remaining CPU count\r
+       dec DWORD [giNumInitingCPUs]\r
+       ; CPU is now marked as initialised\r
+       sti\r
+.hlt:\r
+       hlt\r
+       jmp .hlt\r
+%endif\r
+\r
 [global GetEIP]\r
 GetEIP:\r
        mov eax, [esp]\r
        ret\r
 \r
-[extern Proc_Clone]\r
-[extern Threads_Exit]\r
-[global SpawnTask]\r
-SpawnTask:\r
-       ; Call Proc_Clone with Flags=0\r
-       xor eax, eax\r
-       push eax
+; int CallWithArgArray(void *Ptr, int NArgs, Uint *Args)\r
+; Call a function passing the array as arguments\r
+[global CallWithArgArray]\r
+CallWithArgArray:\r
+       push ebp\r
+       mov ebp, esp\r
+       mov ecx, [ebp+12]       ; Get NArgs\r
+       mov edx, [ebp+16]\r
+\r
+.top:\r
+       mov eax, [edx+ecx*4-4]\r
        push eax\r
-       call Proc_Clone\r
-       add esp, 8      ; Remove arguments from stack\r
-       \r
-       test eax, eax\r
-       jnz .parent\r
-       \r
-       ; In child, so now set up stack frame\r
-       mov ebx, [esp+4]        ; Child Function\r
-       mov edx, [esp+8]        ; Argument\r
-       ; Child\r
-       push edx        ; Argument\r
-       call ebx        ; Function\r
-       call Threads_Exit       ; Kill Thread\r
+       loop .top\r
        \r
-.parent:\r
+       mov eax, [ebp+8]\r
+       call eax\r
+       lea esp, [ebp]\r
+       pop ebp\r
        ret\r
 \r
 [section .initpd]\r

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