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.


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