; ndk - [ start.asm ]
;
; The assembly language stub which defines its
; sections to the multiboot loader, and sets up
; the environment so that a transfer to the C
; source code can be made.
;
; Please see:
;   /src/ndk.c
;
; (c)2002 dcipher / neuraldk
;           www.neuraldk.org

%include "generated/properties.inc"

section .text
bits 32

; defined in the linker script
extern textEnd
extern dataEnd
extern bssEnd
extern ndkStart
extern mb_main

; multi boot header defines
MBOOT_PAGE_ALIGN   equ 1 << 0
MBOOT_MEM_INFO     equ 1 << 1
MBOOT_AOUT_KLUDGE  equ 1 << 16

MBOOT_MAGIC equ 0x1BADB002
MBOOT_FLAGS equ MBOOT_PAGE_ALIGN | MBOOT_MEM_INFO | MBOOT_AOUT_KLUDGE
CHECKSUM    equ -(MBOOT_MAGIC + MBOOT_FLAGS)

; this is technically 0x1,0000,0000 - base_linear + base_phsyical
UPPER_HALF_BASE equ (0xffffffff - Property_kernel_base_linear + 1 + Property_kernel_base_physical)

STACK_SIZE  equ 0x1000

global _start
global start
global gdtBase
global idtBase
global realModeIdtBase
global pageDirectoryBase
global pageTableBase
global systemStack

entry:
    jmp start

    ; The Multiboot header
align 4, db 0
mBootHeader:
    dd MBOOT_MAGIC
    dd MBOOT_FLAGS
    dd CHECKSUM
    ; fields used if MBOOT_AOUT_KLUDGE is set in
    ; MBOOT_HEADER_FLAGS
    dd mBootHeader                     ; these are PHYSICAL addresses
    dd entry                           ; start of kernel .text (code) section
    dd dataEnd                         ; end of kernel .data section
    dd bssEnd                          ; end of kernel BSS
    dd entry                           ; kernel entry point (initial EIP)

 start:
_start:
    ; make sure we're going in the right direction!
    cld

    ; copy the current real mode idt + bios info
    ; do we still need to do this?
    xor esi, esi
    mov edi, realModeIdtBase
    mov ecx, 0x1000
    rep movsb

    lgdt    [pGDT]                  ; load the GDT
    lidt    [pIDT]                  ; load the IDT
    lldt    [pLDT]                  ; load the LDT (0)

    mov     dx, 0x08        ; 0x08 - kernel data segment
    mov     ds, dx
    mov     es, dx
    mov     fs, dx
    mov     gs, dx
    mov     dx, 0x18        ; 0x18 - kernel stack segment
    mov     ss, dx

    mov    esp, (systemStack + STACK_SIZE)

    ; push the multiboot info structure, and magic
    push ebx
    push eax

    ; load cs with new selector
    jmp 0x10:new_gdt

  new_gdt:
    ; time for some C!

    ; enable the alignment check bit, for fun :)
    ; Personally, I think every single program should be
    ; properly aligned, but too many people don't know about
    ; the performance penalties!  With this bit enabled, the
    ; processor will interrupt on any unaligned memory
    ; access (assuming eflags also contains the same bit)
    ; In time, I'll probably just contain a running count
    ; of unaligned access per task... just for show (it'll
    ; slow unaligned programs even more... but... tough!)
    mov eax, cr0
    or eax, 262144
    mov cr0, eax

    call ndkStart
    ;call mb_main
    jmp short $

; note that the idt and gdt below are still in the .text section, not the .data section
; bad things happened at bootup when this was in the .data section, but I'm lazy and didn't
; bother to investigate.  It's fine in .text, and aesthetically pleasing (to myself) to have
; these structures at the beginning of the kernel, anyway

; page align from here down, this will become our idt and gdt
align 4096

idtBase:
        ; zero fill the idt
        times 800h db 0

; begin the gdt
gdtBase:
        ; Null descriptor
        ;   base : 0x00000000
        ;   limit: 0x00000000 ( 0.0 MB)
        dd 0
        dd 0

        ; 0x08 descriptor - Kernel data segment
        ;   base : 0x00000000
        ;   limit: 0xfffff pages (4 GB)
        ;   DPL  : 0
        ;   32 bit, present, system, expand-up, writeable
        dd 0x0000ffff
        dd 0x00cf9200

        ; 0x10 descriptor - Kernel code segment
        ;   base : 0x00000000
        ;   limit: 0xfffff (4 GB)
        ;   DPL  : 0
        ;   32 bit, present, system, non-conforming, readable
        dd 0x0000ffff
        dd 0x00cf9A00

        ; 0x18 descriptor - Kernel stack segment
        ;   base : 0x00000000 //0x00080000
        ;   limit: 0xfffff (4 GB)
        ;   DPL  : 0
        ;   32 bit, present, system, expand-up, writeable
        dd 0x0000ffff
        dd 0x00cf9200
        ;dd 0x00cb9200

        ; ----

        ; 0x20 descriptor - Task data segment
        ;   base : 0x00000000
        ;   limit: 0xfffff pages (4 GB)
        ;   DPL  : 3
        ;   32 bit, present, system, expand-up, writeable
        dd 0x0000ffff
        dd 0x00cff200

        ; 0x28 descriptor - Task code segment
        ;   base : 0x00000000
        ;   limit: 0xfffff (4 GB)
        ;   DPL  : 3
        ;   32 bit, present, system, non-conforming, readable
        dd 0x0000ffff
        dd 0x00cffA00

        ; 0x30 descriptor - Task stack segment
        ;   base : 0x00000000 //0x00080000
        ;   limit: 0xfffff (4 GB)
        ;   DPL  : 3
        ;   32 bit, present, system, expand-up, writeable
        dd 0x0000ffff
        dd 0x00cff200


        ; 0x38 descriptor - Upper-Half Kernel code segment (Tim Robinson trick)
        ;   base : UPPER_HALF_BASE
        ;   limit: 0xfffff (4 GB)
        ;   DPL  : 0
        ;   32 bit, present, system, non-conforming, readable
        dd 0x0000ffff + ((UPPER_HALF_BASE & 0x0000ffff) << 16)
        dd 0x00cf9A00 + ((UPPER_HALF_BASE & 0x00ff0000) >> 16) + (UPPER_HALF_BASE & 0xff000000)

        ; and pad it to the end of the section with 0's
        times 800h-($-gdtBase) db 0

; due to the above IDT and GDT spanning one page (and being page aligned), this
; next section is also page aligned (will contain page directory, and first page table
pageDirectoryBase:
        times 0x1000 db 0

pageTableBase:
        times 0x1000 db 0

; realModeIdtBase is also page aligned
realModeIdtBase:
        times 0x1000 db 0

section .data

pIDT            dw 800h         ; limit of 256 IDT slots
                dd idtBase      ; contained below

pGDT            dw 800h         ; 256 GDT slots
                dd gdtBase      ; contained below (after IDT)

pLDT            dw 0            ; no LDT at this time, but must still load
                dd 0            ; with something (else multitasking might fail)

section .bss
    align 4, db 0
    common systemStack 0x4000
    resb 0x4000

