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
11 .text
12 .globl _idt,_gdt,
13 .globl _swapper_pg_dir,_pg0
14 .globl _empty_bad_page
15 .globl _empty_bad_page_table
16 .globl _empty_zero_page
17 .globl _tmp_floppy_area,_floppy_track_buffer
18
19 #include <linux/tasks.h>
20 #include <linux/segment.h>
21
22 #define CL_MAGIC_ADDR 0x90020
23 #define CL_MAGIC 0xA33F
24 #define CL_BASE_ADDR 0x90000
25 #define CL_OFFSET 0x90022
26
27 /* 28 * swapper_pg_dir is the main page directory, address 0x00001000 (or at 29 * address 0x00101000 for a compressed boot). 30 */ 31 startup_32:
32 cld
33 movl $(KERNEL_DS),%eax
34 mov %ax,%ds
35 mov %ax,%es
36 mov %ax,%fs
37 mov %ax,%gs
38 lss _stack_start,%esp
39 /* 40 * Clear BSS first so that there are no surprises... 41 */ 42 xorl %eax,%eax
43 movl $__edata,%edi
44 movl $__end,%ecx
45 subl %edi,%ecx
46 cld
47 rep
48 stosb
49 /* 50 * start system 32-bit setup. We need to re-do some of the things done 51 * in 16-bit mode for the "real" operations. 52 */ 53 call setup_idt
54 xorl %eax,%eax
55 1: incl %eax # check that A20 really IS enabled
56 movl %eax,0x000000 # loop forever if it isn't
57 cmpl %eax,0x100000
58 je 1b
59 /* 60 * Initialize eflags. Some BIOS's leave bits like NT set. This would 61 * confuse the debugger if this code is traced. 62 * XXX - best to initialize before switching to protected mode. 63 */ 64 pushl $0
65 popfl
66 /* 67 * Copy bootup parameters out of the way. First 2kB of 68 * _empty_zero_page is for boot parameters, second 2kB 69 * is for the command line. 70 */ 71 movl $0x90000,%esi
72 movl $_empty_zero_page,%edi
73 movl $512,%ecx
74 cld
75 rep
76 movsl
77 xorl %eax,%eax
78 movl $512,%ecx
79 rep
80 stosl
81 cmpw $(CL_MAGIC),CL_MAGIC_ADDR
82 jne 1f
83 movl $_empty_zero_page+2048,%edi
84 movzwl CL_OFFSET,%esi
85 addl $(CL_BASE_ADDR),%esi
86 movl $2048,%ecx
87 rep
88 movsb
89 1:
90 /* check if it is 486 or 386. */ 91 /* 92 * XXX - this does a lot of unnecessary setup. Alignment checks don't 93 * apply at our cpl of 0 and the stack ought to be aligned already, and 94 * we don't need to preserve eflags. 95 */ 96 movl %esp,%edi # save stack pointer
97 andl $0xfffffffc,%esp # align stack to avoid AC fault
98 movl $3,_x86
99 pushfl # push EFLAGS
100 popl %eax # get EFLAGS
101 movl %eax,%ecx # save original EFLAGS
102 xorl $0x40000,%eax # flip AC bit in EFLAGS
103 pushl %eax # copy to EFLAGS
104 popfl # set EFLAGS
105 pushfl # get new EFLAGS
106 popl %eax # put it in eax
107 xorl %ecx,%eax # change in flags
108 andl $0x40000,%eax # check if AC bit changed
109 je is386
110 movl $4,_x86
111 movl %ecx,%eax
112 xorl $0x200000,%eax # check ID flag
113 pushl %eax
114 popfl # if we are on a 486,
115 pushfl # we can't change it
116 popl %eax
117 xorl %ecx,%eax
118 andl $0x200000,%eax
119 je is486
120 movl $5,_x86 # 586 setup same as 486 at least for now
121 is486: pushl %ecx # restore original EFLAGS
122 popfl
123 movl %edi,%esp # restore esp
124 movl %cr0,%eax # 486
125 andl $0x80000011,%eax # Save PG,PE,ET
126 orl $0x50022,%eax # set AM, WP, NE and MP
127 jmp 2f
128 is386: pushl %ecx # restore original EFLAGS
129 popfl
130 movl %edi,%esp # restore esp
131 movl %cr0,%eax # 386
132 andl $0x80000011,%eax # Save PG,PE,ET
133 orl $2,%eax # set MP
134 2: movl %eax,%cr0
135 call check_x87
136 call setup_paging
137 lgdt gdt_descr
138 lidt idt_descr
139 ljmp $(KERNEL_CS),$1f
140 1: movl $(KERNEL_DS),%eax # reload all the segment registers
141 mov %ax,%ds # after changing gdt.
142 mov %ax,%es
143 mov %ax,%fs
144 mov %ax,%gs
145 lss _stack_start,%esp
146 xorl %eax,%eax
147 lldt %ax
148 pushl %eax # These are the parameters to main :-)
149 pushl %eax
150 pushl %eax
151 cld # gcc2 wants the direction flag cleared at all times
152 call _start_kernel
153 L6:
154 jmp L6 # main should never return here, but
155 # just in case, we know what happens. 156
157 /* 158 * We depend on ET to be correct. This checks for 287/387. 159 */ 160 check_x87:
161 movl $0,_hard_math
162 clts
163 fninit
164 fstsw %ax
165 cmpb $0,%al
166 je 1f
167 movl %cr0,%eax /* no coprocessor: have to set bits */ 168 xorl $4,%eax /* set EM */ 169 movl %eax,%cr0
170 ret
171 .align 2
172 1: movl $1,_hard_math
173 .byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */ 174 ret
175
176 /* 177 * setup_idt 178 * 179 * sets up a idt with 256 entries pointing to 180 * ignore_int, interrupt gates. It doesn't actually load 181 * idt - that can be done only after paging has been enabled 182 * and the kernel moved to 0xC0000000. Interrupts 183 * are enabled elsewhere, when we can be relatively 184 * sure everything is ok. 185 */ 186 setup_idt:
187 lea ignore_int,%edx
188 movl $(KERNEL_CS << 16),%eax
189 movw %dx,%ax /* selector = 0x0010 = cs */ 190 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */ 191
192 lea _idt,%edi
193 mov $256,%ecx
194 rp_sidt:
195 movl %eax,(%edi)
196 movl %edx,4(%edi)
197 addl $8,%edi
198 dec %ecx
199 jne rp_sidt
200 ret
201
202
203 /* 204 * Setup_paging 205 * 206 * This routine sets up paging by setting the page bit 207 * in cr0. The page tables are set up, identity-mapping 208 * the first 4MB. The rest are initialized later. 209 * 210 * (ref: added support for up to 32mb, 17Apr92) -- Rik Faith 211 * (ref: update, 25Sept92) -- croutons@crunchy.uucp 212 * (ref: 92.10.11 - Linus Torvalds. Corrected 16M limit - no upper memory limit) 213 */ 214 .align 2
215 setup_paging:
216 movl $1024*2,%ecx /* 2 pages - swapper_pg_dir+1 page table */ 217 xorl %eax,%eax
218 movl $_swapper_pg_dir,%edi /* swapper_pg_dir is at 0x1000 */ 219 cld;rep;stosl
220 /* Identity-map the kernel in low 4MB memory for ease of transition */ 221 movl $_pg0+7,_swapper_pg_dir /* set present bit/user r/w */ 222 /* But the real place is at 0xC0000000 */ 223 movl $_pg0+7,_swapper_pg_dir+3072 /* set present bit/user r/w */ 224 movl $_pg0+4092,%edi
225 movl $0x03ff007,%eax /* 4Mb - 4096 + 7 (r/w user,p) */ 226 std
227 1: stosl /* fill the page backwards - more efficient :-) */ 228 subl $0x1000,%eax
229 jge 1b
230 cld
231 movl $_swapper_pg_dir,%eax
232 movl %eax,%cr3 /* cr3 - page directory start */ 233 movl %cr0,%eax
234 orl $0x80000000,%eax
235 movl %eax,%cr0 /* set paging (PG) bit */ 236 ret /* this also flushes the prefetch-queue */ 237
238 /* 239 * page 0 is made non-existent, so that kernel NULL pointer references get 240 * caught. Thus the swapper page directory has been moved to 0x1000 241 * 242 * XXX Actually, the swapper page directory is at 0x1000 plus 1 megabyte, 243 * with the introduction of the compressed boot code. Theoretically, 244 * the original design of overlaying the startup code with the swapper 245 * page directory is still possible --- it would reduce the size of the kernel 246 * by 2-3k. This would be a good thing to do at some point..... 247 */ 248 .org 0x1000
249 _swapper_pg_dir:
250 /* 251 * The page tables are initialized to only 4MB here - the final page 252 * tables are set up later depending on memory size. 253 */ 254 .org 0x2000
255 _pg0:
256
257 .org 0x3000
258 _empty_bad_page:
259
260 .org 0x4000
261 _empty_bad_page_table:
262
263 .org 0x5000
264 _empty_zero_page:
265
266 .org 0x6000
267 /* 268 * tmp_floppy_area is used by the floppy-driver when DMA cannot 269 * reach to a buffer-block. It needs to be aligned, so that it isn't 270 * on a 64kB border. 271 */ 272 _tmp_floppy_area:
273 .fill 1024,1,0
274 /* 275 * floppy_track_buffer is used to buffer one track of floppy data: it 276 * has to be separate from the tmp_floppy area, as otherwise a single- 277 * sector read/write can mess it up. It can contain one full track of 278 * data (18*2*512 bytes). 279 */ 280 _floppy_track_buffer:
281 .fill 512*2*18,1,0
282
283 /* This is the default interrupt "handler" :-) */ 284 int_msg:
285 .asciz "Unknown interrupt\n"
286 .align 2
287 ignore_int:
288 cld
289 pushl %eax
290 pushl %ecx
291 pushl %edx
292 push %ds
293 push %es
294 push %fs
295 movl $(KERNEL_DS),%eax
296 mov %ax,%ds
297 mov %ax,%es
298 mov %ax,%fs
299 pushl $int_msg
300 call _printk
301 popl %eax
302 pop %fs
303 pop %es
304 pop %ds
305 popl %edx
306 popl %ecx
307 popl %eax
308 iret
309
310 /* 311 * The interrupt descriptor table has room for 256 idt's 312 */ 313 .align 4
314 .word 0
315 idt_descr:
316 .word 256*8-1 # idt contains 256 entries
317 .long 0xc0000000+_idt
318
319 .align 4
320 _idt:
321 .fill 256,8,0 # idt is uninitialized
322
323 .align 4
324 .word 0
325 gdt_descr:
326 .word (8+2*NR_TASKS)*8-1
327 .long 0xc0000000+_gdt
328
329 /* 330 * This gdt setup gives the kernel a 1GB address space at virtual 331 * address 0xC0000000 - space enough for expansion, I hope. 332 */ 333 .align 4
334 _gdt:
335 .quad 0x0000000000000000 /* NULL descriptor */ 336 .quad 0x0000000000000000 /* not used */ 337 .quad 0xc0c39a000000ffff /* 0x10 kernel 1GB code at 0xC0000000 */ 338 .quad 0xc0c392000000ffff /* 0x18 kernel 1GB data at 0xC0000000 */ 339 .quad 0x00cbfa000000ffff /* 0x23 user 3GB code at 0x00000000 */ 340 .quad 0x00cbf2000000ffff /* 0x2b user 3GB data at 0x00000000 */ 341 .quad 0x0000000000000000 /* not used */ 342 .quad 0x0000000000000000 /* not used */ 343 .fill 2*NR_TASKS,8,0 /* space for LDT's and TSS's etc */