Remove panic code.
[pintos-anon] / src / threads / start.S
1 #### The loader needs to have some way to know the kernel's entry
2 #### point, that is, the address to which it should jump to start the
3 #### kernel.  We handle this by writing the linker script kernel.lds.S
4 #### so that this module appears at the very beginning of the kernel
5 #### image, and then using that as the entry point.
6
7 #include "threads/loader.h"
8         .intel_syntax noprefix
9
10 .section .start
11         
12 # Code runs in real mode, which is a 16-bit segment.
13         .code16
14
15 .globl start
16 start:
17
18 # Disable interrupts.
19 # String instructions go upward.
20
21         cli
22         cld
23
24 # Set up data segments.
25
26         sub ax, ax
27         mov ds, ax
28         mov es, ax
29
30 #### Enable A20.  Address line 20 is tied to low when the machine
31 #### boots, which prevents addressing memory about 1 MB.  This code
32 #### fixes it.
33         
34 # Poll status register while busy.
35
36 1:      in al, 0x64
37         test al, 0x2
38         jnz 1b
39
40 # Send command for writing output port.
41
42         mov al, 0xd1
43         outb 0x64, al
44
45 # Poll status register while busy.
46
47 1:      in al, 0x64
48         test al, 0x2
49         jnz 1b
50
51 # Enable A20 line.
52
53         mov al, 0xdf
54         out 0x60, al
55
56 #### Get memory size, via interrupt 15h function 88h, which returns CF
57 #### clear if successful, with AX = (kB of physical memory) - 1024.
58 #### This only works for memory sizes <= 65 MB, which should be fine
59 #### for our purposes.  We only reserve enough memory for page tables
60 #### for 64 MB of RAM, so we cap it at that value.
61         
62         mov ah, 0x88
63         int 0x15
64         cli                     # BIOS might have enabled interrupts
65         add eax, 1024           # Total kB memory
66         cmp eax, 64 * 1024
67         jbe 1f
68         mov eax, 64 * 1024
69 1:      shr eax, 2              # Total 4 kB pages
70         mov [ram_pages], eax 
71         
72 #### Create temporary page directory and page table and set page
73 #### directory base register.
74
75 # Create page directory at 64 kB and fill with zeroes.
76         mov ax, LOADER_PD_BASE / 16
77         mov es, ax
78         sub eax, eax
79         sub edi, edi
80         mov ecx, 0x400
81         rep stosd
82
83 # Add a PDE mapping both virtual addresses 0 and LOADER_PHYS_BASE
84 # to a single PTE.
85 # See [IA32-v3] section 3.7.6 for a description of the bits in eax.
86
87         mov eax, LOADER_PT_BASE / 16 + 7
88         mov es:[0], eax
89         mov es:[LOADER_PHYS_BASE / 1024 / 1024], eax
90
91 # Set up one-to-map linear to physical map for the first 4 MB of RAM.
92 # See [IA32-v3] section 3.7.6 for a description of the bits in eax.
93
94         mov ax, LOADER_PT_BASE / 16
95         mov es, ax
96         mov eax, 0x7
97         mov cx, 0x400
98 1:      stosd
99         add eax, 0x1000
100         loop 1b
101
102 # Set page directory base register.
103
104         mov eax, LOADER_PD_BASE
105         mov cr3, eax
106         
107 #### Switch to protected mode.
108
109 # Then we point the GDTR to our GDT.  Protected mode requires a GDT.
110 # We need a data32 prefix to ensure that all 32 bits of the GDT
111 # descriptor are loaded (default is to load only 24 bits).
112
113         data32 lgdt gdtdesc
114
115 # Then we turn on the following bits in CR0:
116 #    PE (Protect Enable): this turns on protected mode.
117 #    PG (Paging): turns on paging.
118 #    WP (Write Protect): if unset, ring 0 code ignores
119 #       write-protect bits in page tables (!).
120 #    EM (Emulation): forces floating-point instructions to trap.
121 #       We don't support floating point. 
122         
123 #define CR0_PE 0x00000001
124 #define CR0_EM 0x00000004
125 #define CR0_PG 0x80000000
126 #define CR0_WP 0x00010000
127         
128         mov eax, cr0
129         or eax, CR0_PE + CR0_PG + CR0_WP + CR0_EM
130         mov cr0, eax
131         
132 # We're now in protected mode in a 16-bit segment.  The CPU still has
133 # the real-mode code segment cached in cs's segment descriptor.  We
134 # need to reload cs, and the easiest way is to use a far jump.
135 # Because we're not in a 32-bit segment the data32 prefix is needed to
136 # jump to a 32-bit offset.
137
138         data32 ljmp SEL_KCSEG, 1f + LOADER_PHYS_BASE
139
140 # We're now in protected mode in a 32-bit segment.
141
142         .code32
143
144 # Reload all the other segment registers and the stack pointer to
145 # point into our new GDT.
146
147 1:      mov ax, SEL_KDSEG
148         mov ds, ax              
149         mov es, ax              
150         mov fs, ax              
151         mov gs, ax              
152         mov ss, ax
153         add esp, LOADER_PHYS_BASE
154
155 #### Jump to kernel entry point.
156
157         mov eax, main
158         call eax
159 1:      jmp 1b
160
161 #### GDT
162
163 gdt:
164         .quad 0x0000000000000000        # null seg
165         .quad 0x00cf9a000000ffff        # code seg
166         .quad 0x00cf92000000ffff        # data seg
167         
168 gdtdesc:
169         .word   0x17                    # sizeof (gdt) - 1
170         .long   gdt + LOADER_PHYS_BASE  # address gdt