Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


(no commit message)
[palacios.git] / palacios / src / geekos / setup.asm
1 ; -*- fundamental -*-
2 ; GeekOS setup code
3 ; Copyright (c) 2001,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4 ; $Revision: 1.1.1.1 $
5
6 ; This is free software.  You are permitted to use,
7 ; redistribute, and modify it as specified in the file "COPYING".
8
9 ; A lot of this code is adapted from Kernel Toolkit 0.2
10 ; and Linux version 2.2.x, so the following copyrights apply:
11
12 ; Copyright (C) 1991, 1992 Linus Torvalds
13 ; modified by Drew Eckhardt
14 ; modified by Bruce Evans (bde)
15 ; adapted for Kernel Toolkit by Luigi Sgro
16
17 %include "defs.asm"
18
19 [BITS 16]
20 [ORG 0x0]
21
22 start_setup:
23
24         ; Redefine the data segment so we can access variables
25         ; declared in this file.
26         mov     ax, SETUPSEG
27         mov     ds, ax
28
29         ; Use int 15h to find out size of extended memory in KB.
30         ; Extended memory is the memory above 1MB.  So by
31         ; adding 1MB to this amount, we get the total amount
32         ; of system memory.  We can only detect 64MB this way,
33         ; but that's OK for now.
34         ;mov    ah, 0x88
35         ;int    0x15
36         ;add    ax, 1024        ; 1024 KB == 1 MB
37         mov     ax, 0xe801
38         int     0x15
39         add     ax, 1024        ; 1024 KB == 1 MB
40         mov     [mem_size_kbytes], ax
41         mov     [mem_size_eblocks], bx
42
43         ; Kill the floppy motor.
44         call    Kill_Motor
45
46         ; Block interrupts, since we can't meaningfully handle them yet
47         ; and we no longer need BIOS services.
48         cli
49
50         ; Set up IDT and GDT registers
51         lidt    [IDT_Pointer]
52         lgdt    [GDT_Pointer]
53
54         ; Initialize the interrupt controllers, and enable the
55         ; A20 address line
56         call    Init_PIC
57         call    Enable_A20
58
59         ; Switch to protected mode!
60         mov     ax, 0x01
61         lmsw    ax
62
63         ; Jump to 32 bit code.
64         jmp     dword KERNEL_CS:(SETUPSEG << 4) + setup_32
65
66 [BITS 32]
67 setup_32:
68
69         ; set up data segment registers
70         mov     ax, KERNEL_DS
71         mov     ds, ax
72         mov     es, ax
73         mov     fs, ax
74         mov     gs, ax
75         mov     ss, ax
76
77         ; Create the stack for the initial kernel thread.
78         mov     esp, KERN_STACK + 4096
79
80         ; Build Boot_Info struct on stack.
81         ; Note that we push the fields on in reverse order,
82         ; since the stack grows downwards.
83         xor     eax, eax
84         mov     ax, [(SETUPSEG<<4)+mem_size_kbytes]
85         xor     ebx, ebx
86         mov     bx, [(SETUPSEG<<4)+mem_size_eblocks]
87         shl     ebx, 6
88         add     eax, ebx
89         push    eax             ; memSizeKB
90         push    dword 8         ; bootInfoSize
91
92         ; Pass pointer to Boot_Info struct as argument to kernel
93         ; entry point.
94         push    esp
95
96         ; Push return address to make this look like a call
97         ; XXX - untested
98         push    dword (SETUPSEG<<4)+.returnAddr
99
100
101         ; Copy VMM kernel and VMM Boot Package to final location
102         call    copy_vmm
103
104         ; Far jump into kernel
105         jmp     KERNEL_CS:ENTRY_POINT
106
107 .returnAddr:
108         ; We shouldn't return here.
109 .here:  jmp .here
110
111
112 copy_vmm:
113         pusha
114
115         mov     ebx, KERNSEG<<4
116         mov     ecx, VMM_FINAL_ADDR
117         mov     edx, VMM_SIZE
118 repeat:
119         mov     eax, [ebx]
120         mov     [ecx], eax
121         add     ebx, $4
122         add     ecx, $4
123         sub     edx, $4
124         jnz     repeat
125
126         popa
127
128         ret
129
130
131
132 [BITS 16]
133
134 ; Kill the floppy motor.
135 ; This code was shamelessly stolen from Linux.
136 Kill_Motor:
137         mov     dx, 0x3f2
138         xor     al, al
139         out     dx, al
140         ret
141
142 Init_PIC:
143         ; Initialize master and slave PIC!
144         mov     al, ICW1
145         out     0x20, al                ; ICW1 to master
146         call    Delay
147         out     0xA0, al                ; ICW1 to slave
148         call    Delay
149         mov     al, ICW2_MASTER
150         out     0x21, al                ; ICW2 to master
151         call    Delay
152         mov     al, ICW2_SLAVE
153         out     0xA1, al                ; ICW2 to slave
154         call    Delay
155         mov     al, ICW3_MASTER
156         out     0x21, al                ; ICW3 to master
157         call    Delay
158         mov     al, ICW3_SLAVE
159         out     0xA1, al                ; ICW3 to slave
160         call    Delay
161         mov     al, ICW4
162         out     0x21, al                ; ICW4 to master
163         call    Delay
164         out     0xA1, al                ; ICW4 to slave
165         call    Delay
166         mov     al, 0xff                ; mask all ints in slave
167         out     0xA1, al                ; OCW1 to slave
168         call    Delay
169         mov     al, 0xfb                ; mask all ints but 2 in master
170         out     0x21, al                ; OCW1 to master
171         call    Delay
172         ret
173
174 ; Linux uses this code.
175 ; The idea is that some systems issue port I/O instructions
176 ; faster than the device hardware can deal with them.
177 Delay:
178         jmp     .done
179 .done:  ret
180
181 ; Enable the A20 address line, so we can correctly address
182 ; memory above 1MB.
183 Enable_A20:
184         mov     al, 0xD1
185         out     0x64, al
186         call    Delay
187         mov     al, 0xDF
188         out     0x60, al
189         call    Delay
190         ret
191
192
193 ; ----------------------------------------------------------------------
194 ; Setup data
195 ; ----------------------------------------------------------------------
196
197 mem_size_kbytes: dw 0
198 mem_size_eblocks: dw 0
199
200 ; ----------------------------------------------------------------------
201 ; The GDT.  Creates flat 32-bit address space for the kernel
202 ; code, data, and stack.  Note that this GDT is just used
203 ; to create an environment where we can start running 32 bit
204 ; code.  The kernel will create and manage its own GDT.
205 ; ----------------------------------------------------------------------
206
207 ; GDT initialization stuff
208 NUM_GDT_ENTRIES equ 3           ; number of entries in GDT
209 GDT_ENTRY_SZ equ 8              ; size of a single GDT entry
210
211 align 8, db 0
212 GDT:
213         ; Descriptor 0 is not used
214         dw 0
215         dw 0
216         dw 0
217         dw 0
218
219         ; Descriptor 1: kernel code segment
220         dw 0xFFFF       ; bytes 0 and 1 of segment size
221         dw 0x0000       ; bytes 0 and 1 of segment base address
222         db 0x00         ; byte 2 of segment base address
223         db 0x9A         ; present, DPL=0, non-system, code, non-conforming,
224                         ;   readable, not accessed
225         db 0xCF         ; granularity=page, 32 bit code, upper nibble of size
226         db 0x00         ; byte 3 of segment base address
227
228         ; Descriptor 2: kernel data and stack segment
229         ; NOTE: what Intel calls an "expand-up" segment
230         ; actually means that the stack will grow DOWN,
231         ; towards lower memory.  So, we can use this descriptor
232         ; for both data and stack references.
233         dw 0xFFFF       ; bytes 0 and 1 of segment size
234         dw 0x0000       ; bytes 0 and 1 of segment base address
235         db 0x00         ; byte 2 of segment base address
236         db 0x92         ; present, DPL=0, non-system, data, expand-up,
237                         ;   writable, not accessed
238         db 0xCF         ; granularity=page, big, upper nibble of size
239         db 0x00         ; byte 3 of segment base address
240
241 GDT_Pointer:
242         dw NUM_GDT_ENTRIES*GDT_ENTRY_SZ ; limit
243         dd (SETUPSEG<<4) + GDT          ; base address
244
245 IDT_Pointer:
246         dw 0
247         dd 00