Move pagedir stuff into userprog.
[pintos-anon] / src / threads / loader.S
1 /* This file is derived from source code used in MIT's 6.828
2    course.  The original copyright notice is reproduced in full
3    below. */
4
5 /*
6  * Copyright (C) 1997 Massachusetts Institute of Technology 
7  *
8  * This software is being provided by the copyright holders under the
9  * following license. By obtaining, using and/or copying this software,
10  * you agree that you have read, understood, and will comply with the
11  * following terms and conditions:
12  *
13  * Permission to use, copy, modify, distribute, and sell this software
14  * and its documentation for any purpose and without fee or royalty is
15  * hereby granted, provided that the full text of this NOTICE appears on
16  * ALL copies of the software and documentation or portions thereof,
17  * including modifications, that you make.
18  *
19  * THIS SOFTWARE IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
20  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE,
21  * BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
22  * WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
23  * THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY
24  * THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. COPYRIGHT
25  * HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE OR
26  * DOCUMENTATION.
27  *
28  * The name and trademarks of copyright holders may NOT be used in
29  * advertising or publicity pertaining to the software without specific,
30  * written prior permission. Title to copyright in this software and any
31  * associated documentation will at all times remain with copyright
32  * holders. See the file AUTHORS which should have accompanied this software
33  * for a list of all copyright holders.
34  *
35  * This file may be derived from previously copyrighted software. This
36  * copyright applies only to those changes made by the copyright
37  * holders listed in the AUTHORS file. The rest of this file is covered by
38  * the copyright notices, if any, listed below.
39  */
40
41 #include "threads/loader.h"
42 /*#include "threads/mmu.h"*/
43         
44 #### Kernel loader.
45
46 #### This code should be stored in the first sector of the hard disk.
47 #### When the BIOS runs, it loads this code at physical address
48 #### 0x7c00-0x7e00 (512 bytes).  Then it jumps to the beginning of it,
49 #### in real mode.  This code switches into protected mode (32-bit
50 #### mode) so that all of memory can accessed, loads the kernel into
51 #### memory, and jumps to the first byte of the kernel, where start.S
52 #### is linked.
53         
54 /* Flags in control register 0 */
55 #define CR0_PE 0x00000001      /* Protection Enable. */
56 #define CR0_EM 0x00000004      /* (Floating-point) Emulation. */
57 #define CR0_PG 0x80000000      /* Paging. */
58 #define CR0_WP 0x00010000      /* Write-Protect enable in kernel mode. */
59
60 # Code runs in real mode, which is a 16-bit segment.
61
62 .globl start
63 start:
64         .code16
65
66 # Disable interrupts.
67 # String instructions go upward.
68
69         cli
70         cld
71
72 # Set up data segments and stack.
73
74         subw %ax, %ax
75         movw %ax, %es
76         movw %ax, %ds
77
78 # Stack grows downward starting from us.
79 # We don't ever use the stack so this is strictly speaking
80 # unnecessary.
81
82         movw %ax, %ss
83         movw $0x7c00, %sp
84         
85 #### Enable A20.  Address line 20 is tied to low when the machine
86 #### boots, which prevents addressing memory about 1 MB.  This code
87 #### fixes it.
88         
89 # Poll status register while busy.
90
91 1:      inb $0x64, %al
92         testb $0x2, %al
93         jnz 1b
94
95 # Send command for writing output port.
96
97         movb $0xd1, %al
98         outb %al, $0x64
99
100 # Poll status register while busy.
101
102 1:      inb $0x64, %al
103         testb $0x2, %al
104         jnz 1b
105
106 # Enable A20 line.
107
108         movb $0xdf, %al
109         outb %al, $0x60
110
111 #### Get memory size, via interrupt 15h function 88h.  Returns CF
112 #### clear if successful, with AX = (kB of physical memory) - 1024.
113 #### This only works for memory sizes <= 65 MB, which should be fine
114 #### for our purposes.
115         
116         movb $0x88, %ah
117         int $0x15
118         jc panic                # Carry flag set on error
119         addl $1024, %eax        # Total kB
120         shrl $2, %eax           # Total 4 kB pages
121         movl %eax, ram_pages
122         
123 #### Create temporary page directory and page table and set page
124 #### directory base register.
125
126 # Create page directory at 64 kB and fill with zeroes.
127
128         mov $0x1000, %ax
129         mov %ax, %es
130         subl %eax, %eax
131         subl %edi, %edi
132         movl $0x400, %ecx
133         rep stosl
134
135 # Set PDEs for 0 and LOADER_PHYS_BASE to point to the page table.
136 # See comments near the PG_* macros in paging.h for a description of
137 # the values stored here.
138
139         movl $0x11007, %eax
140         movl %eax, %es:0
141         movl %eax, %es:LOADER_PHYS_BASE >> 20
142
143 # Initialize page table.
144
145         movl $7, %eax
146         movl $0x400, %ecx
147 1:      stosl
148         addl $0x1000, %eax
149         loop 1b
150
151 # Set page directory base register.
152
153         movl $0x10000, %eax
154         movl %eax, %cr3
155         
156 #### Switch to protected mode.
157
158 # First we turn off interrupts because we don't set up an IDT.
159
160         cli
161
162 # Then we point the GDTR to our GDT.  Protected mode requires a GDT.
163 # We need a data32 prefix to ensure that all 32 bits of the GDT
164 # descriptor are loaded (default is to load only 24 bits).
165
166         data32 lgdt gdtdesc
167
168 # Then we turn on the following bits in CR0:
169 #    PE (Protect Enable): this turns on protected mode.
170 #    PG (Paging): turns on paging.
171 #    WP (Write Protect): if unset, ring 0 code ignores
172 #       write-protect bits in page tables (!).
173 #    EM (Emulation): forces floating-point instructions to trap.
174 #       We don't support floating point. 
175         
176         movl %cr0, %eax
177         orl $CR0_PE | CR0_PG | CR0_WP | CR0_EM, %eax
178         movl %eax, %cr0
179         
180 # We're now in protected mode in a 16-bit segment.  The CPU still has
181 # the real-mode code segment cached in %cs's segment descriptor.  We
182 # need to reload %cs, and the easiest way is to use a far jump.
183 # Because we're not in a 32-bit segment the data32 prefix is needed to
184 # jump to a 32-bit offset.
185
186         data32 ljmp $SEL_KCSEG, $1f + LOADER_PHYS_BASE
187         
188 # We're now in protected mode in a 32-bit segment.
189
190         .code32
191
192 # Reload all the other segment registers and the stack pointer to
193 # point into our new GDT.
194
195 1:      movw $SEL_KDSEG, %ax
196         movw %ax, %ds           
197         movw %ax, %es           
198         movw %ax, %fs           
199         movw %ax, %gs           
200         movw %ax, %ss
201         movl $LOADER_PHYS_BASE + 0x20000, %esp
202
203 #### Load kernel starting at physical address LOADER_KERN_BASE by
204 #### frobbing the IDE controller directly.
205
206         movl $1, %ebx
207         movl $LOADER_KERN_BASE + LOADER_PHYS_BASE, %edi
208 read_sector:
209
210 # Poll status register while controller busy.
211
212         movl $0x1f7, %edx
213 1:      inb %dx, %al
214         testb $0x80, %al
215         jnz 1b
216
217 # Read a single sector.
218
219         movl $0x1f2, %edx
220         movb $1, %al
221         outb %al, %dx
222
223 # Sector number to write in low 28 bits.
224 # LBA mode, device 0 in top 4 bits.
225
226         movl %ebx, %eax
227         andl $0x0fffffff, %eax
228         orl $0xe0000000, %eax
229
230 # Dump %eax to ports 0x1f3...0x1f6.
231
232         movl $4, %ecx
233 1:      incw %dx
234         outb %al, %dx
235         shrl $8, %eax
236         loop 1b
237
238 # READ command to command register.
239
240         incw %dx
241         movb $0x20, %al
242         outb %al, %dx
243
244 # Poll status register while controller busy.
245
246 1:      inb %dx, %al
247         testb $0x80, %al
248         jnz 1b
249
250 # Poll status register until data ready.
251
252 1:      inb %dx, %al
253         testb $0x08, %al
254         jz 1b
255
256 # Transfer sector.
257
258         movl $256, %ecx
259         movl $0x1f0, %edx
260         rep insw
261
262 # Next sector.
263
264         incl %ebx
265         cmpl $KERNEL_LOAD_PAGES*8 + 1, %ebx
266         jnz read_sector
267
268 #### Jump to kernel entry point.
269
270         movl $LOADER_PHYS_BASE + LOADER_KERN_BASE, %eax
271         call *%eax
272         jmp panic
273
274 #### GDT
275
276 gdt:
277         .quad 0x0000000000000000        # null seg
278         .quad 0x00cf9a000000ffff        # code seg
279         .quad 0x00cf92000000ffff        # data seg
280         
281 gdtdesc:
282         .word   0x17                    # sizeof (gdt) - 1
283         .long   gdt + LOADER_PHYS_BASE  # address gdt
284
285 #### Fatal error.
286 #### Print panicmsg (with help from the BIOS) and spin.
287
288 panic:  .code16                 # We only panic in real mode.
289         movw $panicmsg, %si
290         movb $0xe, %ah
291         subb %bh, %bh
292 1:      lodsb
293         test %al, %al
294 2:      jz 2b                   # Spin.
295         int $0x10
296         jmp 1b
297
298 panicmsg:
299         .ascii "Loader panic!\r\n"
300         .byte 0
301
302 #### Memory size in 4 kB pages.
303         .org LOADER_RAM_PAGES - LOADER_BASE
304 ram_pages:
305         .long 0
306
307 #### Command-line arguments inserted by another utility.
308 #### The loader doesn't use these, but we note their
309 #### location here for easy reference.
310         .org LOADER_CMD_LINE - LOADER_BASE
311 cmd_line:
312         .fill 0x80, 1, 0
313
314 #### Boot-sector signature for BIOS inspection.
315         .org LOADER_BIOS_SIG - LOADER_BASE
316         .word 0xaa55