Kernel/x86 - Debugging SMP bugs, lead to some fixes and cleanups
[tpg/acess2.git] / Kernel / arch / x86 / start.asm
index 0a4f920..b6026de 100644 (file)
@@ -59,6 +59,8 @@ mboot:
 [extern kmain]
 [global start]
 start:
+       ; Just show we're here
+       mov WORD [0xB8000], 0x0741      ; 'A'
        
        ; Set up stack
        mov esp, Kernel_Stack_Top
@@ -66,18 +68,31 @@ start:
        ; Start Paging
        mov ecx, gaInitPageDir - KERNEL_BASE
        mov cr3, ecx
-       
        mov ecx, cr0
-       or      ecx, 0x80010000 ; PG and WP
+       or ecx, 0x80010000      ; PG and WP
        mov cr0, ecx
        
-       lea ecx, [.higherHalf]
-       jmp ecx
-.higherHalf:
+       mov WORD [0xB8002], 0x0763      ; 'c'
+       
+       ; Set GDT
+       lgdt [gGDTPtr]
+       mov cx, 0x10    ; PL0 Data
+       mov ss, cx
+       mov ds, cx
+       mov es, cx
+       mov gs, cx
+       mov fs, cx
+       mov WORD [0xB8004], 0x0765      ; 'e'
+       jmp 0x08:.higher_half
+.higher_half:
+       
+       mov WORD [0xB8006], 0x0773      ; 's'
+       mov WORD [0xB8008], 0x0773      ; 's'
 
        ; Call the kernel
        push ebx        ; Multiboot Info
        push eax        ; Multiboot Magic Value
+       mov WORD [0xB800A], 0x0732      ; '2'
        call kmain
 
        ; Halt the Machine
@@ -87,11 +102,9 @@ start:
        jmp .hlt
 
 ; 
-; Multiprocessing AP Startup Code (Must be within 0x10FFF0)
+; Multiprocessing AP Startup Code (Must be within 0 - 0x10FFF0)
 ;
 %if USE_MP
-[extern gGDT]
-[extern gGDTPtr]
 [extern gIDTPtr]
 [extern gpMP_LocalAPIC]
 [extern giMP_TimerCount]
@@ -99,6 +112,7 @@ start:
 [extern gaCPUs]
 [extern giNumInitingCPUs]
 [extern MM_NewKStack]
+[extern Proc_InitialiseSSE]
 
 lGDTPtr:       ; Local GDT Pointer
        dw      3*8-1
@@ -111,15 +125,19 @@ APWait:
 .hlt:
        ;hlt
        jmp .hlt
+[extern Proc_Reschedule]
 [global APStartup]
 APStartup:
        ;xchg bx, bx    ; MAGIC BREAK!
+       ; Load initial GDT
        mov ax, 0xFFFF
        mov ds, ax
        lgdt [DWORD ds:lGDTPtr-KERNEL_BASE-0xFFFF0]
+       ; Enable PMode in CR0
        mov eax, cr0
-       or      al, 1
-       mov     cr0, eax
+       or al, 1
+       mov cr0, eax
+       ; Jump into PMode
        jmp 08h:DWORD .ProtectedMode-KERNEL_BASE
 [bits 32]
 .ProtectedMode:
@@ -134,7 +152,7 @@ APStartup:
        mov eax, gaInitPageDir - KERNEL_BASE
        mov cr3, eax
        mov eax, cr0
-       or      eax, 0x80010000 ; PG and WP
+       or eax, 0x80010000      ; PG and WP
        mov cr0, eax
        ; Jump to higher half
        lea eax, [.higherHalf]
@@ -148,11 +166,13 @@ APStartup:
        mov ebx, [ebp+0x20]     ; Read ID
        shr ebx, 24
        ;xchg bx, bx    ; MAGIC BREAK
-       ; CL is now local APIC ID
+       ; BL is now local APIC ID
        mov cl, BYTE [gaAPIC_to_CPU+ebx]
        xor ebx, ebx
        mov bl, cl
        ; BL is now the CPU ID
+       mov dr1, ebx    ; Save the CPU number to a debug register
+       ; Mark the CPU as up
        mov BYTE [gaCPUs+ebx*8+1], 1
        ; Decrement the remaining CPU count
        dec DWORD [giNumInitingCPUs]
@@ -167,8 +187,6 @@ APStartup:
        ; Set TSS
        lea ecx, [ebx*8+0x30]
        ltr cx
-       ; Save the CPU number to a debug register
-       mov dr1, ebx
        
        ;xchg bx, bx    ; MAGIC_BREAK
        ; Enable Local APIC
@@ -182,11 +200,15 @@ APStartup:
        mov DWORD [ebp+0x360], 0x000100D2       ; ##Enable LINT1 on IVT#0xD2
        mov DWORD [ebp+0x370], 0x000100E1       ; ##Enable Error on IVT#0xE1
        mov DWORD [ebp+0x0B0], 0        ; Send an EOI (just in case)
+
+       ; Initialise SSE support
+       call Proc_InitialiseSSE
        
        ; CPU is now marked as initialised
-       sti
-       ;xchg bx, bx    ; MAGIC BREAK
+
 .hlt:
+       sti
+       call Proc_Reschedule    
        hlt
        jmp .hlt
 %endif
@@ -216,18 +238,37 @@ CallWithArgArray:
        pop ebp
        ret
 
+[section .data]
+; GDT
+GDT_SIZE       equ     (1+2*2+1+MAX_CPUS)*8
+[global gGDT]
+gGDT:
+       ; PL0 - Kernel
+       ; PL3 - User
+       dd 0x00000000, 0x00000000       ; 00 NULL Entry
+       dd 0x0000FFFF, 0x00CF9A00       ; 08 PL0 Code
+       dd 0x0000FFFF, 0x00CF9200       ; 10 PL0 Data
+       dd 0x0000FFFF, 0x00CFFA00       ; 18 PL3 Code
+       dd 0x0000FFFF, 0x00CFF200       ; 20 PL3 Data
+       dd 26*4-1, 0x00408900   ; 28 Double Fault TSS
+       times MAX_CPUS  dd 26*4-1, 0x00408900   ; 30+ TSSes
+[global gGDTPtr]
+gGDTPtr:
+       dw      GDT_SIZE-1
+       dd      gGDT
+
 [section .initpd]
 [global gaInitPageDir]
 [global gaInitPageTable]
-align 0x1000
+align 4096
 gaInitPageDir:
        dd      gaInitPageTable-KERNEL_BASE+3   ; 0x000 - Low kernel
-       times 0x300-1   dd      0
+       times 0x300-0x000-1     dd      0
        dd      gaInitPageTable-KERNEL_BASE+3   ; 0xC00 - High kernel
        times 0x3F0-0x300-1     dd      0
        dd      gaInitPageDir-KERNEL_BASE+3     ; 0xFC0 - Fractal
        times 0x400-0x3F0-1     dd      0
-align 0x1000
+align 4096
 gaInitPageTable:
        %assign i 0
        %rep 1024
@@ -235,9 +276,10 @@ gaInitPageTable:
        %assign i i+1
        %endrep
 [global Kernel_Stack_Top]
-ALIGN 0x1000
+ALIGN 4096
        times 1024      dd      0
 Kernel_Stack_Top:
 gInitAPStacks:
        times 32*MAX_CPUS       dd      0
-       
+
+; vim: ft=nasm ts=8    

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