Comments.
[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         .code16
63
64 # Disable interrupts.
65 # String instructions go upward.
66
67         cli
68         cld
69
70 # Set up data segments and stack.
71
72         subw %ax, %ax
73         movw %ax, %es
74         movw %ax, %ds
75
76 # Stack grows downward starting from us.
77 # We don't ever use the stack so this is strictly speaking
78 # unnecessary.
79
80         movw %ax, %ss
81         movw $0x7c00, %sp
82         
83 #### Enable A20.  Address line 20 is tied to low when the machine
84 #### boots, which prevents addressing memory about 1 MB.  This code
85 #### fixes it.
86         
87 # Poll status register while busy.
88
89 1:      inb $0x64, %al
90         testb $0x2, %al
91         jnz 1b
92
93 # Send command for writing output port.
94
95         movb $0xd1, %al
96         outb %al, $0x64
97
98 # Poll status register while busy.
99
100 1:      inb $0x64, %al
101         testb $0x2, %al
102         jnz 1b
103
104 # Enable A20 line.
105
106         movb $0xdf, %al
107         outb %al, $0x60
108
109 #### Get memory size, via interrupt 15h function 88h.  Returns CF
110 #### clear if successful, with AX = (kB of physical memory) - 1024.
111 #### This only works for memory sizes <= 65 MB, which should be fine
112 #### for our purposes.
113         
114         movb $0x88, %ah
115         int $0x15
116         jc panic                # Carry flag set on error
117         addl $1024, %eax        # Total kB
118         shrl $2, %eax           # Total 4 kB pages
119         movl %eax, ram_pages
120         
121 #### Create temporary page directory and page table and set page
122 #### directory base register.
123
124 # Create page directory at 64 kB and fill with zeroes.
125
126         mov $0x1000, %ax
127         mov %ax, %es
128         subl %eax, %eax
129         subl %edi, %edi
130         movl $0x400, %ecx
131         rep stosl
132
133 # Set PDEs for 0 and LOADER_PHYS_BASE to point to the page table.
134
135         movl $0x11000 | PG_U | PG_W | PG_P, %eax
136         movl %eax, %es:0
137         movl %eax, %es:LOADER_PHYS_BASE >> 20
138
139 # Initialize page table.
140
141         movl $PG_U | PG_W | PG_P, %eax
142         movl $0x400, %ecx
143 1:      stosl
144         addl $0x1000, %eax
145         loop 1b
146
147 # Set page directory base register.
148
149         movl $0x10000, %eax
150         movl %eax, %cr3
151         
152 #### Switch to protected mode.
153
154 # First we turn off interrupts because we don't set up an IDT.
155
156         cli
157
158 # Then we point the GDTR to our GDT.  Protected mode requires a GDT.
159 # We need a data32 prefix to ensure that all 32 bits of the GDT
160 # descriptor are loaded (default is to load only 24 bits).
161
162         data32 lgdt gdtdesc
163
164 # Then we turn on the following bits in CR0:
165 #    PE (Protect Enable): this turns on protected mode.
166 #    PG (Paging): turns on paging.
167 #    WP (Write Protect): if unset, ring 0 code ignores
168 #       write-protect bits in page tables (!).
169 #    EM (Emulation): forces floating-point instructions to trap.
170 #       We don't support floating point. 
171         
172         movl %cr0, %eax
173         orl $CR0_PE | CR0_PG | CR0_WP | CR0_EM, %eax
174         movl %eax, %cr0
175         
176 # We're now in protected mode in a 16-bit segment.  The CPU still has
177 # the real-mode code segment cached in %cs's segment descriptor.  We
178 # need to reload %cs, and the easiest way is to use a far jump.
179 # Because we're not in a 32-bit segment the data32 prefix is needed to
180 # jump to a 32-bit offset.
181
182         data32 ljmp $SEL_KCSEG, $1f + LOADER_PHYS_BASE
183         
184 # We're now in protected mode in a 32-bit segment.
185
186         .code32
187
188 # Reload all the other segment registers and the stack pointer to
189 # point into our new GDT.
190
191 1:      movw $SEL_KDSEG, %ax
192         movw %ax, %ds           
193         movw %ax, %es           
194         movw %ax, %fs           
195         movw %ax, %gs           
196         movw %ax, %ss
197         movl $LOADER_PHYS_BASE + 0x20000, %esp
198
199 #### Load kernel starting at physical address LOADER_KERN_BASE by
200 #### frobbing the IDE controller directly.
201
202         movl $1, %ebx
203         movl $LOADER_KERN_BASE + LOADER_PHYS_BASE, %edi
204 read_sector:
205
206 # Poll status register while controller busy.
207
208         movl $0x1f7, %edx
209 1:      inb %dx, %al
210         testb $0x80, %al
211         jnz 1b
212
213 # Read a single sector.
214
215         movl $0x1f2, %edx
216         movb $1, %al
217         outb %al, %dx
218
219 # Sector number to write in low 28 bits.
220 # LBA mode, device 0 in top 4 bits.
221
222         movl %ebx, %eax
223         andl $0x0fffffff, %eax
224         orl $0xe0000000, %eax
225
226 # Dump %eax to ports 0x1f3...0x1f6.
227
228         movl $4, %ecx
229 1:      incw %dx
230         outb %al, %dx
231         shrl $8, %eax
232         loop 1b
233
234 # READ command to command register.
235
236         incw %dx
237         movb $0x20, %al
238         outb %al, %dx
239
240 # Poll status register while controller busy.
241
242 1:      inb %dx, %al
243         testb $0x80, %al
244         jnz 1b
245
246 # Poll status register until data ready.
247
248 1:      inb %dx, %al
249         testb $0x08, %al
250         jz 1b
251
252 # Transfer sector.
253
254         movl $512 / 4, %ecx
255         movl $0x1f0, %edx
256         rep insl
257
258 # Next sector.
259
260         incl %ebx
261         cmpl $KERNEL_LOAD_PAGES*8 + 1, %ebx
262         jnz read_sector
263
264 #### Jump to kernel entry point.
265
266         movl $LOADER_PHYS_BASE + LOADER_KERN_BASE, %eax
267         call *%eax
268         jmp panic
269
270 #### GDT
271
272 gdt:
273         .quad 0x0000000000000000        # null seg
274         .quad 0x00cf9a000000ffff        # code seg
275         .quad 0x00cf92000000ffff        # data seg
276         
277 gdtdesc:
278         .word   0x17                    # sizeof (gdt) - 1
279         .long   gdt + LOADER_PHYS_BASE  # address gdt
280
281 #### Fatal error.
282 #### Print panicmsg (with help from the BIOS) and spin.
283
284 panic:  .code16                 # We only panic in real mode.
285         movw $panicmsg, %si
286         movb $0xe, %ah
287         subb %bh, %bh
288 1:      lodsb
289         test %al, %al
290 2:      jz 2b                   # Spin.
291         int $0x10
292         jmp 1b
293
294 panicmsg:
295         .ascii "Loader panic!\r\n"
296         .byte 0
297
298 #### Memory size in 4 kB pages.
299         .org LOADER_RAM_PAGES - LOADER_BASE
300 ram_pages:
301         .long 0
302
303 #### Command-line arguments inserted by another utility.
304 #### The loader doesn't use these, but we note their
305 #### location here for easy reference.
306         .org LOADER_CMD_LINE - LOADER_BASE
307 cmd_line:
308         .fill 0x80, 1, 0
309
310 #### Boot-sector signature for BIOS inspection.
311         .org LOADER_BIOS_SIG - LOADER_BASE
312         .word 0xaa55