Declare start symbol for linker's use.
[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
137         movl $0x11000 | PG_U | PG_W | PG_P, %eax
138         movl %eax, %es:0
139         movl %eax, %es:LOADER_PHYS_BASE >> 20
140
141 # Initialize page table.
142
143         movl $PG_U | PG_W | PG_P, %eax
144         movl $0x400, %ecx
145 1:      stosl
146         addl $0x1000, %eax
147         loop 1b
148
149 # Set page directory base register.
150
151         movl $0x10000, %eax
152         movl %eax, %cr3
153         
154 #### Switch to protected mode.
155
156 # First we turn off interrupts because we don't set up an IDT.
157
158         cli
159
160 # Then we point the GDTR to our GDT.  Protected mode requires a GDT.
161 # We need a data32 prefix to ensure that all 32 bits of the GDT
162 # descriptor are loaded (default is to load only 24 bits).
163
164         data32 lgdt gdtdesc
165
166 # Then we turn on the following bits in CR0:
167 #    PE (Protect Enable): this turns on protected mode.
168 #    PG (Paging): turns on paging.
169 #    WP (Write Protect): if unset, ring 0 code ignores
170 #       write-protect bits in page tables (!).
171 #    EM (Emulation): forces floating-point instructions to trap.
172 #       We don't support floating point. 
173         
174         movl %cr0, %eax
175         orl $CR0_PE | CR0_PG | CR0_WP | CR0_EM, %eax
176         movl %eax, %cr0
177         
178 # We're now in protected mode in a 16-bit segment.  The CPU still has
179 # the real-mode code segment cached in %cs's segment descriptor.  We
180 # need to reload %cs, and the easiest way is to use a far jump.
181 # Because we're not in a 32-bit segment the data32 prefix is needed to
182 # jump to a 32-bit offset.
183
184         data32 ljmp $SEL_KCSEG, $1f + LOADER_PHYS_BASE
185         
186 # We're now in protected mode in a 32-bit segment.
187
188         .code32
189
190 # Reload all the other segment registers and the stack pointer to
191 # point into our new GDT.
192
193 1:      movw $SEL_KDSEG, %ax
194         movw %ax, %ds           
195         movw %ax, %es           
196         movw %ax, %fs           
197         movw %ax, %gs           
198         movw %ax, %ss
199         movl $LOADER_PHYS_BASE + 0x20000, %esp
200
201 #### Load kernel starting at physical address LOADER_KERN_BASE by
202 #### frobbing the IDE controller directly.
203
204         movl $1, %ebx
205         movl $LOADER_KERN_BASE + LOADER_PHYS_BASE, %edi
206 read_sector:
207
208 # Poll status register while controller busy.
209
210         movl $0x1f7, %edx
211 1:      inb %dx, %al
212         testb $0x80, %al
213         jnz 1b
214
215 # Read a single sector.
216
217         movl $0x1f2, %edx
218         movb $1, %al
219         outb %al, %dx
220
221 # Sector number to write in low 28 bits.
222 # LBA mode, device 0 in top 4 bits.
223
224         movl %ebx, %eax
225         andl $0x0fffffff, %eax
226         orl $0xe0000000, %eax
227
228 # Dump %eax to ports 0x1f3...0x1f6.
229
230         movl $4, %ecx
231 1:      incw %dx
232         outb %al, %dx
233         shrl $8, %eax
234         loop 1b
235
236 # READ command to command register.
237
238         incw %dx
239         movb $0x20, %al
240         outb %al, %dx
241
242 # Poll status register while controller busy.
243
244 1:      inb %dx, %al
245         testb $0x80, %al
246         jnz 1b
247
248 # Poll status register until data ready.
249
250 1:      inb %dx, %al
251         testb $0x08, %al
252         jz 1b
253
254 # Transfer sector.
255
256         movl $512 / 4, %ecx
257         movl $0x1f0, %edx
258         rep insl
259
260 # Next sector.
261
262         incl %ebx
263         cmpl $KERNEL_LOAD_PAGES*8 + 1, %ebx
264         jnz read_sector
265
266 #### Jump to kernel entry point.
267
268         movl $LOADER_PHYS_BASE + LOADER_KERN_BASE, %eax
269         call *%eax
270         jmp panic
271
272 #### GDT
273
274 gdt:
275         .quad 0x0000000000000000        # null seg
276         .quad 0x00cf9a000000ffff        # code seg
277         .quad 0x00cf92000000ffff        # data seg
278         
279 gdtdesc:
280         .word   0x17                    # sizeof (gdt) - 1
281         .long   gdt + LOADER_PHYS_BASE  # address gdt
282
283 #### Fatal error.
284 #### Print panicmsg (with help from the BIOS) and spin.
285
286 panic:  .code16                 # We only panic in real mode.
287         movw $panicmsg, %si
288         movb $0xe, %ah
289         subb %bh, %bh
290 1:      lodsb
291         test %al, %al
292 2:      jz 2b                   # Spin.
293         int $0x10
294         jmp 1b
295
296 panicmsg:
297         .ascii "Loader panic!\r\n"
298         .byte 0
299
300 #### Memory size in 4 kB pages.
301         .org LOADER_RAM_PAGES - LOADER_BASE
302 ram_pages:
303         .long 0
304
305 #### Command-line arguments inserted by another utility.
306 #### The loader doesn't use these, but we note their
307 #### location here for easy reference.
308         .org LOADER_CMD_LINE - LOADER_BASE
309 cmd_line:
310         .fill 0x80, 1, 0
311
312 #### Boot-sector signature for BIOS inspection.
313         .org LOADER_BIOS_SIG - LOADER_BASE
314         .word 0xaa55