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