b7842d3840743c6f25ab0bceaafcbd97e74e65fd
[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         
43 #### Kernel loader.
44
45 #### This code should be stored in the first sector of the hard disk.
46 #### When the BIOS runs, it loads this code at physical address
47 #### 0x7c00-0x7e00 (512 bytes).  Then it jumps to the beginning of it,
48 #### in real mode.  This code switches into protected mode (32-bit
49 #### mode) so that all of memory can accessed, loads the kernel into
50 #### memory, and jumps to the first byte of the kernel, where start.S
51 #### is linked.
52         
53 /* Flags in control register 0. */
54 #define CR0_PE 0x00000001      /* Protection Enable. */
55 #define CR0_EM 0x00000004      /* (Floating-point) Emulation. */
56 #define CR0_PG 0x80000000      /* Paging. */
57 #define CR0_WP 0x00010000      /* Write-Protect enable in kernel mode. */
58
59
60 .globl start
61 start:
62         
63 # Code runs in real mode, which is a 16-bit segment.
64         .code16
65
66 # Disable interrupts, because we will not be prepared to handle them
67 # in protected mode until much later.
68 # String instructions go upward (e.g. for "rep stosl" below).
69
70         cli
71         cld
72
73 # Set up data segments.
74
75         subw %ax, %ax
76         movw %ax, %es
77         movw %ax, %ds
78
79 # Set up stack segment.
80 # Stack grows downward starting from us.
81 # We don't ever use the stack, but we call into the BIOS,
82 # which might.
83
84         movw %ax, %ss
85         movw $0x7c00, %sp
86         
87 #### Enable A20.  Address line 20 is tied to low when the machine
88 #### boots, which prevents addressing memory about 1 MB.  This code
89 #### fixes it.
90         
91 # Poll status register while busy.
92
93 1:      inb $0x64, %al
94         testb $0x2, %al
95         jnz 1b
96
97 # Send command for writing output port.
98
99         movb $0xd1, %al
100         outb %al, $0x64
101
102 # Poll status register while busy.
103
104 1:      inb $0x64, %al
105         testb $0x2, %al
106         jnz 1b
107
108 # Enable A20 line.
109
110         movb $0xdf, %al
111         outb %al, $0x60
112
113 #### Get memory size, via interrupt 15h function 88h.  Returns CF
114 #### clear if successful, with AX = (kB of physical memory) - 1024.
115 #### This only works for memory sizes <= 65 MB, which should be fine
116 #### for our purposes.  We cap memory at 64 MB because that's all we
117 #### prepare page tables for, below.
118         
119         movb $0x88, %ah
120         int $0x15
121         jc panic
122         cli                     # BIOS might have enabled interrupts
123         addl $1024, %eax        # Total kB memory
124         cmp $0x10000, %eax      # Cap at 64 MB
125         jbe 1f
126         mov $0x10000, %eax
127 1:      shrl $2, %eax           # Total 4 kB pages
128         movl %eax, ram_pgs
129         
130 #### Create temporary page directory and page table and set page
131 #### directory base register.
132
133 # Create page directory at 64 kB and fill with zeroes.
134         mov $0x1000, %ax
135         mov %ax, %es
136         subl %eax, %eax
137         subl %edi, %edi
138         movl $0x400, %ecx
139         rep stosl
140
141 # Add PDEs to point to PTEs for the first 64 MB of RAM.
142 # Also add identical PDEs starting at LOADER_PHYS_BASE.
143 # See [IA32-v3a] section 3.7.6 "Page-Directory and Page-Table Entries"
144 # for a description of the bits in %eax.
145         
146
147         movl $0x11007, %eax
148         movl $0x11, %ecx
149         subl %edi, %edi
150 1:      movl %eax, %es:(%di)
151         movl %eax, %es:LOADER_PHYS_BASE >> 20(%di)
152         addw $4, %di
153         addl $0x1000, %eax
154         loop 1b
155
156 # Set up one-to-map linear to physical map for the first 64 MB of RAM.
157 # See [IA32-v3a] section 3.7.6 "Page-Directory and Page-Table Entries"
158 # for a description of the bits in %eax.
159
160         movw $0x1100, %ax
161         movw %ax, %es
162         movl $0x7, %eax
163         movl $0x4000, %ecx
164         subl %edi, %edi
165 1:      movl %eax, %es:(%di)
166         addw $4, %di
167         addl $0x1000, %eax
168         loop 1b
169
170 # Set page directory base register.
171
172         movl $0x10000, %eax
173         movl %eax, %cr3
174         
175 #### Switch to protected mode.
176
177 # Note that interrupts are still off.
178
179 # Point the GDTR to our GDT.  Protected mode requires a GDT.
180 # We need a data32 prefix to ensure that all 32 bits of the GDT
181 # descriptor are loaded (default is to load only 24 bits).
182
183         data32 lgdt gdtdesc
184
185 # Then we turn on the following bits in CR0:
186 #    PE (Protect Enable): this turns on protected mode.
187 #    PG (Paging): turns on paging.
188 #    WP (Write Protect): if unset, ring 0 code ignores
189 #       write-protect bits in page tables (!).
190 #    EM (Emulation): forces floating-point instructions to trap.
191 #       We don't support floating point. 
192         
193         movl %cr0, %eax
194         orl $CR0_PE | CR0_PG | CR0_WP | CR0_EM, %eax
195         movl %eax, %cr0
196         
197 # We're now in protected mode in a 16-bit segment.  The CPU still has
198 # the real-mode code segment cached in %cs's segment descriptor.  We
199 # need to reload %cs, and the easiest way is to use a far jump.
200 # Because we're not in a 32-bit segment the data32 prefix is needed to
201 # jump to a 32-bit offset.
202
203         data32 ljmp $SEL_KCSEG, $1f + LOADER_PHYS_BASE
204         
205 # We're now in protected mode in a 32-bit segment.
206
207         .code32
208
209 # Reload all the other segment registers and the stack pointer to
210 # point into our new GDT.
211
212 1:      movw $SEL_KDSEG, %ax
213         movw %ax, %ds           
214         movw %ax, %es           
215         movw %ax, %fs           
216         movw %ax, %gs           
217         movw %ax, %ss
218         movl $LOADER_PHYS_BASE + 0x30000, %esp
219
220 #### Load kernel starting at physical address LOADER_KERN_BASE by
221 #### frobbing the IDE controller directly.
222
223         movl $1, %ebx
224         movl $LOADER_KERN_BASE + LOADER_PHYS_BASE, %edi
225
226 # Disable interrupt delivery by IDE controller, because we will be
227 # polling for data.
228 # (If we don't do this, Bochs 2.2.6 will never deliver any IDE
229 # interrupt to us later after we reset the interrupt controller during
230 # boot, even if we also reset the IDE controller.)
231
232         movw $0x3f6, %dx
233         movb $0x02, %al
234         outb %al, %dx
235         
236 read_sector:
237
238 # Poll status register while controller busy.
239
240         movl $0x1f7, %edx
241 1:      inb %dx, %al
242         testb $0x80, %al
243         jnz 1b
244
245 # Read a single sector.
246
247         movl $0x1f2, %edx
248         movb $1, %al
249         outb %al, %dx
250
251 # Sector number to write in low 28 bits.
252 # LBA mode, device 0 in top 4 bits.
253
254         movl %ebx, %eax
255         andl $0x0fffffff, %eax
256         orl $0xe0000000, %eax
257
258 # Dump %eax to ports 0x1f3...0x1f6.
259
260         movl $4, %ecx
261 1:      incw %dx
262         outb %al, %dx
263         shrl $8, %eax
264         loop 1b
265
266 # READ command to command register.
267
268         incw %dx
269         movb $0x20, %al
270         outb %al, %dx
271
272 # Poll status register while controller busy.
273
274 1:      inb %dx, %al
275         testb $0x80, %al
276         jnz 1b
277
278 # Poll status register until data ready.
279
280 1:      inb %dx, %al
281         testb $0x08, %al
282         jz 1b
283
284 # Transfer sector.
285
286         movl $256, %ecx
287         movl $0x1f0, %edx
288         rep insw
289
290 # Next sector.
291
292         incl %ebx
293         cmpl $KERNEL_LOAD_PAGES*8 + 1, %ebx
294         jnz read_sector
295
296 #### Jump to kernel entry point.
297
298         movl $LOADER_PHYS_BASE + LOADER_KERN_BASE, %eax
299         call *%eax
300         jmp panic
301
302 #### GDT
303
304 gdt:
305         .quad 0x0000000000000000        # null seg
306         .quad 0x00cf9a000000ffff        # code seg
307         .quad 0x00cf92000000ffff        # data seg
308         
309 gdtdesc:
310         .word   0x17                    # sizeof (gdt) - 1
311         .long   gdt + LOADER_PHYS_BASE  # address gdt
312
313 #### Fatal error.
314 #### Print panic_message (with help from the BIOS) and spin.
315
316 panic:  .code16                 # We only panic in real mode.
317         movw $panic_message, %si
318         movb $0xe, %ah
319         subb %bh, %bh
320 1:      lodsb
321         test %al, %al
322 2:      jz 2b                   # Spin.
323         int $0x10
324         jmp 1b
325
326 panic_message:
327         .ascii "Panic!"
328         .byte 0
329
330 #### Physical memory size in 4 kB pages.
331 #### This is initialized by the loader and read by the kernel.
332         .org LOADER_RAM_PGS - LOADER_BASE
333 ram_pgs:
334         .long 0
335
336 #### Command-line arguments and their count.
337 #### This is written by the `pintos' utility and read by the kernel.
338 #### The loader itself does not do anything with the command line.
339         .org LOADER_ARG_CNT - LOADER_BASE
340 arg_cnt:
341         .long 0
342         .org LOADER_ARGS - LOADER_BASE
343 args:
344         .fill 0x80, 1, 0
345
346 #### Boot-sector signature.
347 #### The BIOS checks that this is set properly.
348         .org LOADER_SIG - LOADER_BASE
349         .word 0xaa55