1 /* 2 * linux/boot/head.s 3 * 4 * Copyright (C) 1991, 1992 Linus Torvalds 5 */ 6
7 /* 8 * head.s contains the 32-bit startup code. 9 * 10 * NOTE!!! Startup happens at absolute address 0x00000000, which is also where 11 * the page directory will exist. The startup code will be overwritten by 12 * the page directory. 13 */ 14 .text
15 .globl _idt,_gdt,
16 .globl _swapper_pg_dir,_pg0
17 .globl _empty_bad_page
18 .globl _empty_bad_page_table
19 .globl _tmp_floppy_area,_floppy_track_buffer
20
21 #include <linux/tasks.h>
22
23 /* 24 * swapper_pg_dir is the main page directory, address 0x00001000 25 */ 26 startup_32:
27 cld
28 movl $0x10,%eax
29 mov %ax,%ds
30 mov %ax,%es
31 mov %ax,%fs
32 mov %ax,%gs
33 lss _stack_start,%esp
34 call setup_idt
35 xorl %eax,%eax
36 1: incl %eax # check that A20 really IS enabled
37 movl %eax,0x000000 # loop forever if it isn't
38 cmpl %eax,0x100000
39 je 1b
40 /* 41 * Initialize eflags. Some BIOS's leave bits like NT set. This would 42 * confuse the debugger if this code is traced. 43 * XXX - best to initialize before switching to protected mode. 44 */ 45 pushl $0
46 popfl
47 /* check if it is 486 or 386. */ 48 /* 49 * XXX - this does a lot of unnecessary setup. Alignment checks don't 50 * apply at our cpl of 0 and the stack ought to be aligned already, and 51 * we don't need to preserve eflags. 52 */ 53 movl %esp,%edi # save stack pointer
54 andl $0xfffffffc,%esp # align stack to avoid AC fault
55 pushfl # push EFLAGS
56 popl %eax # get EFLAGS
57 movl %eax,%ecx # save original EFLAGS
58 xorl $0x40000,%eax # flip AC bit in EFLAGS
59 pushl %eax # copy to EFLAGS
60 popfl # set EFLAGS
61 pushfl # get new EFLAGS
62 popl %eax # put it in eax
63 xorl %ecx,%eax # change in flags
64 andl $0x40000,%eax # check if AC bit changed
65 jnz 1f # 486
66 pushl %ecx # restore original EFLAGS
67 popfl
68 movl %edi,%esp # restore esp
69 movl %cr0,%eax # 386
70 andl $0x80000011,%eax # Save PG,PE,ET
71 orl $2,%eax # set MP
72 jmp 2f
73 /* 74 * NOTE! 486 should set bit 16, to check for write-protect in supervisor 75 * mode. Then it would be unnecessary with the "verify_area()"-calls. 76 * 486 users probably want to set the NE (#5) bit also, so as to use 77 * int 16 for math errors. 78 * XXX - the above is out of date. We set all the bits, but don't take 79 * advantage of WP (26 Dec 92). 80 */ 81 1: pushl %ecx # restore original EFLAGS
82 popfl
83 movl %edi,%esp # restore esp
84 movl %cr0,%eax # 486
85 andl $0x80000011,%eax # Save PG,PE,ET
86 orl $0x50022,%eax # set AM, WP, NE and MP
87 2: movl %eax,%cr0
88 call check_x87
89 call setup_paging
90 lgdt gdt_descr
91 lidt idt_descr
92 ljmp $0x08,$1f
93 1: movl $0x10,%eax # reload all the segment registers
94 mov %ax,%ds # after changing gdt.
95 mov %ax,%es
96 mov %ax,%fs
97 mov %ax,%gs
98 lss _stack_start,%esp
99 pushl $0 # These are the parameters to main :-)
100 pushl $0
101 pushl $0
102 cld # gcc2 wants the direction flag cleared at all times
103 call _start_kernel
104 L6:
105 jmp L6 # main should never return here, but
106 # just in case, we know what happens. 107
108 /* 109 * We depend on ET to be correct. This checks for 287/387. 110 */ 111 check_x87:
112 movl $0,_hard_math
113 fninit
114 fstsw %ax
115 cmpb $0,%al
116 je 1f
117 movl %cr0,%eax /* no coprocessor: have to set bits */ 118 xorl $6,%eax /* reset MP, set EM */ 119 movl %eax,%cr0
120 ret
121 .align 2
122 1: movl $1,_hard_math
123 .byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */ 124 ret
125
126 /* 127 * setup_idt 128 * 129 * sets up a idt with 256 entries pointing to 130 * ignore_int, interrupt gates. It doesn't actually load 131 * idt - that can be done only after paging has been enabled 132 * and the kernel moved to 0xC0000000. Interrupts 133 * are enabled elsewhere, when we can be relatively 134 * sure everything is ok. This routine will be over- 135 * written by the page tables. 136 */ 137 setup_idt:
138 lea ignore_int,%edx
139 movl $0x00080000,%eax
140 movw %dx,%ax /* selector = 0x0008 = cs */ 141 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */ 142
143 lea _idt,%edi
144 mov $256,%ecx
145 rp_sidt:
146 movl %eax,(%edi)
147 movl %edx,4(%edi)
148 addl $8,%edi
149 dec %ecx
150 jne rp_sidt
151 ret
152
153
154 /* 155 * Setup_paging 156 * 157 * This routine sets up paging by setting the page bit 158 * in cr0. The page tables are set up, identity-mapping 159 * the first 4MB. The rest are initialized later. 160 * 161 * (ref: added support for up to 32mb, 17Apr92) -- Rik Faith 162 * (ref: update, 25Sept92) -- croutons@crunchy.uucp 163 * (ref: 92.10.11 - Linus Torvalds. Corrected 16M limit - no upper memory limit) 164 */ 165 .align 2
166 setup_paging:
167 movl $1024*2,%ecx /* 2 pages - swapper_pg_dir+1 page table */ 168 xorl %eax,%eax
169 movl $_swapper_pg_dir,%edi /* swapper_pg_dir is at 0x1000 */ 170 cld;rep;stosl
171 /* Identity-map the kernel in low 4MB memory for ease of transition */ 172 movl $_pg0+7,_swapper_pg_dir /* set present bit/user r/w */ 173 /* But the real place is at 0xC0000000 */ 174 movl $_pg0+7,_swapper_pg_dir+3072 /* set present bit/user r/w */ 175 movl $_pg0+4092,%edi
176 movl $0x03ff007,%eax /* 4Mb - 4096 + 7 (r/w user,p) */ 177 std
178 1: stosl /* fill the page backwards - more efficient :-) */ 179 subl $0x1000,%eax
180 jge 1b
181 cld
182 movl $_swapper_pg_dir,%eax
183 movl %eax,%cr3 /* cr3 - page directory start */ 184 movl %cr0,%eax
185 orl $0x80000000,%eax
186 movl %eax,%cr0 /* set paging (PG) bit */ 187 ret /* this also flushes the prefetch-queue */ 188
189 /* 190 * page 0 is made non-existent, so that kernel NULL pointer references get 191 * caught. Thus the swapper page directory has been moved to 0x1000 192 */ 193 .org 0x1000
194 _swapper_pg_dir:
195 /* 196 * The page tables are initialized to only 4MB here - the final page 197 * tables are set up later depending on memory size. 198 */ 199 .org 0x2000
200 _pg0:
201
202 .org 0x3000
203 _empty_bad_page:
204
205 .org 0x4000
206 _empty_bad_page_table:
207
208 .org 0x5000
209 /* 210 * tmp_floppy_area is used by the floppy-driver when DMA cannot 211 * reach to a buffer-block. It needs to be aligned, so that it isn't 212 * on a 64kB border. 213 */ 214 _tmp_floppy_area:
215 .fill 1024,1,0
216 /* 217 * floppy_track_buffer is used to buffer one track of floppy data: it 218 * has to be separate from the tmp_floppy area, as otherwise a single- 219 * sector read/write can mess it up. It can contain one full track of 220 * data (18*2*512 bytes). 221 */ 222 _floppy_track_buffer:
223 .fill 512*2*18,1,0
224
225 /* This is the default interrupt "handler" :-) */ 226 int_msg:
227 .asciz "Unknown interrupt\n\r"
228 .align 2
229 ignore_int:
230 cld
231 pushl %eax
232 pushl %ecx
233 pushl %edx
234 push %ds
235 push %es
236 push %fs
237 movl $0x10,%eax
238 mov %ax,%ds
239 mov %ax,%es
240 mov %ax,%fs
241 pushl $int_msg
242 call _printk
243 popl %eax
244 pop %fs
245 pop %es
246 pop %ds
247 popl %edx
248 popl %ecx
249 popl %eax
250 iret
251
252 /* 253 * The interrupt descriptor table has room for 256 idt's 254 */ 255 .align 4
256 .word 0
257 idt_descr:
258 .word 256*8-1 # idt contains 256 entries
259 .long 0xc0000000+_idt
260
261 .align 4
262 _idt:
263 .fill 256,8,0 # idt is uninitialized
264
265 /* 266 * The real GDT is also 256 entries long - no real reason 267 */ 268 .align 4
269 .word 0
270 gdt_descr:
271 .word (4+2*NR_TASKS)*8-1
272 .long 0xc0000000+_gdt
273
274 /* 275 * This gdt setup gives the kernel a 1GB address space at virtual 276 * address 0xC0000000 - space enough for expansion, I hope. 277 */ 278 .align 4
279 _gdt:
280 .quad 0x0000000000000000 /* NULL descriptor */ 281 .quad 0xc0c39a000000ffff /* 1GB at 0xC0000000 */ 282 .quad 0xc0c392000000ffff /* 1GB */ 283 .quad 0x0000000000000000 /* TEMPORARY - don't use */ 284 .fill 2*NR_TASKS,8,0 /* space for LDT's and TSS's etc */