Revert Intel-style assembly back to AT&T-style.
[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 # Code runs in real mode, which is a 16-bit segment.
60
61 .globl start
62 start:
63         .code16
64
65 # Disable interrupts.
66 # String instructions go upward.
67
68         cli
69         cld
70
71 # Set up data segments.
72
73         subw %ax, %ax
74         movw %ax, %es
75         movw %ax, %ds
76
77 # Set up stack segment.
78 # Stack grows downward starting from us.
79 # We don't ever use the stack, but we call into the BIOS,
80 # which might.
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.  We cap memory at 64 MB because that's all we
115 #### prepare page tables for, below.
116         
117         movb $0x88, %ah
118         int $0x15
119         jc panic
120         cli                     # BIOS might have enabled interrupts
121         addl $1024, %eax        # Total kB memory
122         cmp $0x10000, %eax      # Cap at 64 MB
123         jbe 1f
124         mov $0x10000, %eax
125 1:      shrl $2, %eax           # Total 4 kB pages
126         movl %eax, ram_pgs
127         
128 #### Create temporary page directory and page table and set page
129 #### directory base register.
130
131 # Create page directory at 64 kB and fill with zeroes.
132         mov $0x1000, %ax
133         mov %ax, %es
134         subl %eax, %eax
135         subl %edi, %edi
136         movl $0x400, %ecx
137         rep stosl
138
139 # Add PDEs to point to PTEs for the first 64 MB of RAM.
140 # Also add identical PDEs starting at LOADER_PHYS_BASE.
141 # See [IA32-v3] section 3.7.6 for a description of the bits in %eax.
142
143         movl $0x11007, %eax
144         movl $0x11, %ecx
145         subl %edi, %edi
146 1:      movl %eax, %es:(%di)
147         movl %eax, %es:LOADER_PHYS_BASE >> 20(%di)
148         addw $4, %di
149         addl $0x1000, %eax
150         loop 1b
151
152 # Set up one-to-map linear to physical map for the first 64 MB of RAM.
153 # See [IA32-v3] section 3.7.6 for a description of the bits in %eax.
154
155         movw $0x1100, %ax
156         movw %ax, %es
157         movl $0x7, %eax
158         movl $0x4000, %ecx
159         subl %edi, %edi
160 1:      movl %eax, %es:(%di)
161         addw $4, %di
162         addl $0x1000, %eax
163         loop 1b
164
165 # Set page directory base register.
166
167         movl $0x10000, %eax
168         movl %eax, %cr3
169         
170 #### Switch to protected mode.
171
172 # Note that interrupts are still off.
173
174 # Point the GDTR to our GDT.  Protected mode requires a GDT.
175 # We need a data32 prefix to ensure that all 32 bits of the GDT
176 # descriptor are loaded (default is to load only 24 bits).
177
178         data32 lgdt gdtdesc
179
180 # Then we turn on the following bits in CR0:
181 #    PE (Protect Enable): this turns on protected mode.
182 #    PG (Paging): turns on paging.
183 #    WP (Write Protect): if unset, ring 0 code ignores
184 #       write-protect bits in page tables (!).
185 #    EM (Emulation): forces floating-point instructions to trap.
186 #       We don't support floating point. 
187         
188         movl %cr0, %eax
189         orl $CR0_PE | CR0_PG | CR0_WP | CR0_EM, %eax
190         movl %eax, %cr0
191         
192 # We're now in protected mode in a 16-bit segment.  The CPU still has
193 # the real-mode code segment cached in %cs's segment descriptor.  We
194 # need to reload %cs, and the easiest way is to use a far jump.
195 # Because we're not in a 32-bit segment the data32 prefix is needed to
196 # jump to a 32-bit offset.
197
198         data32 ljmp $SEL_KCSEG, $1f + LOADER_PHYS_BASE
199         
200 # We're now in protected mode in a 32-bit segment.
201
202         .code32
203
204 # Reload all the other segment registers and the stack pointer to
205 # point into our new GDT.
206
207 1:      movw $SEL_KDSEG, %ax
208         movw %ax, %ds           
209         movw %ax, %es           
210         movw %ax, %fs           
211         movw %ax, %gs           
212         movw %ax, %ss
213         movl $LOADER_PHYS_BASE + 0x30000, %esp
214
215 #### Load kernel starting at physical address LOADER_KERN_BASE by
216 #### frobbing the IDE controller directly.
217
218         movl $1, %ebx
219         movl $LOADER_KERN_BASE + LOADER_PHYS_BASE, %edi
220 read_sector:
221
222 # Poll status register while controller busy.
223
224         movl $0x1f7, %edx
225 1:      inb %dx, %al
226         testb $0x80, %al
227         jnz 1b
228
229 # Read a single sector.
230
231         movl $0x1f2, %edx
232         movb $1, %al
233         outb %al, %dx
234
235 # Sector number to write in low 28 bits.
236 # LBA mode, device 0 in top 4 bits.
237
238         movl %ebx, %eax
239         andl $0x0fffffff, %eax
240         orl $0xe0000000, %eax
241
242 # Dump %eax to ports 0x1f3...0x1f6.
243
244         movl $4, %ecx
245 1:      incw %dx
246         outb %al, %dx
247         shrl $8, %eax
248         loop 1b
249
250 # READ command to command register.
251
252         incw %dx
253         movb $0x20, %al
254         outb %al, %dx
255
256 # Poll status register while controller busy.
257
258 1:      inb %dx, %al
259         testb $0x80, %al
260         jnz 1b
261
262 # Poll status register until data ready.
263
264 1:      inb %dx, %al
265         testb $0x08, %al
266         jz 1b
267
268 # Transfer sector.
269
270         movl $256, %ecx
271         movl $0x1f0, %edx
272         rep insw
273
274 # Next sector.
275
276         incl %ebx
277         cmpl $KERNEL_LOAD_PAGES*8 + 1, %ebx
278         jnz read_sector
279
280 #### Jump to kernel entry point.
281
282         movl $LOADER_PHYS_BASE + LOADER_KERN_BASE, %eax
283         call *%eax
284         jmp panic
285
286 #### GDT
287
288 gdt:
289         .quad 0x0000000000000000        # null seg
290         .quad 0x00cf9a000000ffff        # code seg
291         .quad 0x00cf92000000ffff        # data seg
292         
293 gdtdesc:
294         .word   0x17                    # sizeof (gdt) - 1
295         .long   gdt + LOADER_PHYS_BASE  # address gdt
296
297 #### Fatal error.
298 #### Print panic_message (with help from the BIOS) and spin.
299
300 panic:  .code16                 # We only panic in real mode.
301         movw $panic_message, %si
302         movb $0xe, %ah
303         subb %bh, %bh
304 1:      lodsb
305         test %al, %al
306 2:      jz 2b                   # Spin.
307         int $0x10
308         jmp 1b
309
310 panic_message:
311         .ascii "Panic!"
312         .byte 0
313
314 #### Physical memory size in 4 kB pages.
315 #### This is initialized by the loader and read by the kernel.
316         .org LOADER_RAM_PGS - LOADER_BASE
317 ram_pgs:
318         .long 0
319
320 #### Command-line arguments and their count.
321 #### This is written by the `pintos' utility and read by the kernel.
322 #### The loader itself does not do anything with the command line.
323         .org LOADER_ARG_CNT - LOADER_BASE
324 arg_cnt:
325         .long 0
326         .org LOADER_ARGS - LOADER_BASE
327 args:
328         .fill 0x80, 1, 0
329
330 #### Boot-sector signature.
331 #### The BIOS checks that this is set properly.
332         .org LOADER_SIG - LOADER_BASE
333         .word 0xaa55