Initial checkin of toy OS project.
[os.git] / boot1 / boot1.asm
1 [BITS 16]
2 [ORG 0]
3
4 START:
5         ;; initialize segments
6         mov     ax, BOOT1SEG
7         mov     ds, ax
8         mov es, ax
9         mov ss, ax
10
11         ;; make sure we are running on a 386+
12
13         ;; while we're still in real mode, detect system hardware via BIOS
14         call PROBE_VIDEO
15         call GET_EQUIPMENT_LIST
16         call GET_MEMORY_SIZE
17         call GET_TIME
18         
19         ;; switch the processor into protected mode, keep ints disabled
20         cli
21         lidt [IDT_Pointer]
22         lgdt [GDT_Pointer]
23         call ENABLE_A20
24         mov ax, 0x01
25         lmsw ax
26
27         ;; Jump to 32 bit code, this completes the transition to protected mode
28         db 0x66                 ; operand size override prefix
29         db 0xea                 ; jmp opcode
30         dd (BOOT1SEG<<4) + setup_32 ; want to jump here
31         dw (1<<3)               ; ...in the kernel code segment
32
33 [BITS 32]
34 setup_32:
35         ;; set up data segment registers
36         mov     ax, (2<<3)
37         mov     ds, ax
38         mov     es, ax
39         mov     fs, ax
40         mov     gs, ax
41         mov     ss, ax
42
43         ;; create an initial stack
44         mov esp, KERN_STACK + 4096
45         mov ebp, esp
46
47         ;; create a stack frame, pass ptr to hardware block as param
48         mov eax, (BOOT1SEG<<4)+BIOS_HARDWARE_BLOCK
49         push eax
50         push (BOOT1SEG<<4)+.HANG
51         jmp (1<<3):0x00010000     ; entry point of kernel
52
53 .HANG:  
54         nop
55         jmp .HANG
56
57
58 ; Setup data
59 ; ----------------------------------------------------------------------
60
61 BIOS_HARDWARE_BLOCK:
62 bDisplayType            db      0x00
63 bFontSize               db      0x00
64 wEquipmentBitvector     dw      0x0000
65 wMemorySize             dw      0x0000
66 wSystemClock            dw      0x0000        
67         
68 NUM_GDT_ENTRIES equ 3           ; number of entries in GDT
69 GDT_ENTRY_SZ equ 8              ; size of a single GDT entry
70
71 align 8, db 0
72 GDT:
73         ; Descriptor 0 is not used
74         dw 0
75         dw 0
76         dw 0
77         dw 0
78
79         ; Descriptor 1: kernel code segment
80         dw 0xFFFF       ; bytes 0 and 1 of segment size
81         dw 0x0000       ; bytes 0 and 1 of segment base address
82         db 0x00         ; byte 2 of segment base address
83         db 0x9A         ; present, DPL=0, non-system, code, non-conforming,
84                         ;   readable, not accessed
85         db 0xCF         ; granularity=page, 32 bit code, upper nibble of size
86         db 0x00         ; byte 3 of segment base address
87
88         ; Descriptor 2: kernel data and stack segment
89         ; NOTE: what Intel calls an "expand-up" segment
90         ; actually means that the stack will grow DOWN,
91         ; towards lower memory.  So, we can use this descriptor
92         ; for both data and stack references.
93         dw 0xFFFF       ; bytes 0 and 1 of segment size
94         dw 0x0000       ; bytes 0 and 1 of segment base address
95         db 0x00         ; byte 2 of segment base address
96         db 0x92         ; present, DPL=0, non-system, data, expand-up,
97                         ;   writable, not accessed
98         db 0xCF         ; granularity=page, big, upper nibble of size
99         db 0x00         ; byte 3 of segment base address
100
101 GDT_Pointer:
102         dw NUM_GDT_ENTRIES*GDT_ENTRY_SZ ; limit
103         dd (BOOT1SEG<<4) + GDT          ; base address
104
105 IDT_Pointer:
106         dw 0
107         dd 00
108
109 %include "defs.asm"
110 %include "hardware.asm"
111 %include "video.asm"
112         
113