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 call setup_paging
73 lgdt gdt_descr
74 lidt idt_descr
75 ljmp $0x08,$1f
76 1: movl $0x10,%eax # reload all the segment registers
77 mov %ax,%ds # after changing gdt.
78 mov %ax,%es
79 mov %ax,%fs
80 mov %ax,%gs
81 lss _stack_start,%esp
82 pushl $0 # These are the parameters to main :-)
83 pushl $0
84 pushl $0
85 cld # gcc2 wants the direction flag cleared at all times
86 call _start_kernel
87 L6:
88 jmp L6 # main should never return here, but
89 # just in case, we know what happens. 90
91 /* 92 * We depend on ET to be correct. This checks for 287/387. 93 */ 94 check_x87:
95 movl $0,_hard_math
96 fninit
97 fstsw %ax
98 cmpb $0,%al
99 je 1f
100 movl %cr0,%eax /* no coprocessor: have to set bits */ 101 xorl $6,%eax /* reset MP, set EM */ 102 movl %eax,%cr0
103 ret
104 .align 2
105 1: movl $1,_hard_math
106 .byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */ 107 ret
108
109 /* 110 * setup_idt 111 * 112 * sets up a idt with 256 entries pointing to 113 * ignore_int, interrupt gates. It doesn't actually load 114 * idt - that can be done only after paging has been enabled 115 * and the kernel moved to 0xC0000000. Interrupts 116 * are enabled elsewhere, when we can be relatively 117 * sure everything is ok. This routine will be over- 118 * written by the page tables. 119 */ 120 setup_idt:
121 lea ignore_int,%edx
122 movl $0x00080000,%eax
123 movw %dx,%ax /* selector = 0x0008 = cs */ 124 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */ 125
126 lea _idt,%edi
127 mov $256,%ecx
128 rp_sidt:
129 movl %eax,(%edi)
130 movl %edx,4(%edi)
131 addl $8,%edi
132 dec %ecx
133 jne rp_sidt
134 ret
135
136
137 /* 138 * Setup_paging 139 * 140 * This routine sets up paging by setting the page bit 141 * in cr0. The page tables are set up, identity-mapping 142 * the first 4MB. The rest are initialized later. 143 * 144 * (ref: added support for up to 32mb, 17Apr92) -- Rik Faith 145 * (ref: update, 25Sept92) -- croutons@crunchy.uucp 146 * (ref: 92.10.11 - Linus Torvalds. Corrected 16M limit - no upper memory limit) 147 */ 148 .align 2
149 setup_paging:
150 movl $1024*2,%ecx /* 2 pages - swapper_pg_dir+1 page table */ 151 xorl %eax,%eax
152 movl $_swapper_pg_dir,%edi /* swapper_pg_dir is at 0x1000 */ 153 cld;rep;stosl
154 /* Identity-map the kernel in low 4MB memory for ease of transition */ 155 movl $_pg0+7,_swapper_pg_dir /* set present bit/user r/w */ 156 /* But the real place is at 0xC0000000 */ 157 movl $_pg0+7,_swapper_pg_dir+3072 /* set present bit/user r/w */ 158 movl $_pg0+4092,%edi
159 movl $0x03ff007,%eax /* 4Mb - 4096 + 7 (r/w user,p) */ 160 std
161 1: stosl /* fill the page backwards - more efficient :-) */ 162 subl $0x1000,%eax
163 jge 1b
164 cld
165 movl $_swapper_pg_dir,%eax
166 movl %eax,%cr3 /* cr3 - page directory start */ 167 movl %cr0,%eax
168 orl $0x80000000,%eax
169 movl %eax,%cr0 /* set paging (PG) bit */ 170 ret /* this also flushes the prefetch-queue */ 171
172 /* 173 * page 0 is made non-existent, so that kernel NULL pointer references get 174 * caught. Thus the swapper page directory has been moved to 0x1000 175 */ 176 .org 0x1000
177 _swapper_pg_dir:
178 /* 179 * The page tables are initialized to only 4MB here - the final page 180 * tables are set up later depending on memory size. 181 */ 182 .org 0x2000
183 _pg0:
184
185 .org 0x3000
186 _empty_bad_page:
187
188 .org 0x4000
189 _empty_bad_page_table:
190
191 .org 0x5000
192 /* 193 * tmp_floppy_area is used by the floppy-driver when DMA cannot 194 * reach to a buffer-block. It needs to be aligned, so that it isn't 195 * on a 64kB border. 196 */ 197 _tmp_floppy_area:
198 .fill 1024,1,0
199 /* 200 * floppy_track_buffer is used to buffer one track of floppy data: it 201 * has to be separate from the tmp_floppy area, as otherwise a single- 202 * sector read/write can mess it up. It can contain one full track of 203 * data (18*2*512 bytes). 204 */ 205 _floppy_track_buffer:
206 .fill 512*2*18,1,0
207
208 /* This is the default interrupt "handler" :-) */ 209 int_msg:
210 .asciz "Unknown interrupt\n\r"
211 .align 2
212 ignore_int:
213 cld
214 pushl %eax
215 pushl %ecx
216 pushl %edx
217 push %ds
218 push %es
219 push %fs
220 movl $0x10,%eax
221 mov %ax,%ds
222 mov %ax,%es
223 mov %ax,%fs
224 pushl $int_msg
225 call _printk
226 popl %eax
227 pop %fs
228 pop %es
229 pop %ds
230 popl %edx
231 popl %ecx
232 popl %eax
233 iret
234
235 /* 236 * The interrupt descriptor table has room for 256 idt's 237 */ 238 .align 4
239 .word 0
240 idt_descr:
241 .word 256*8-1 # idt contains 256 entries
242 .long 0xc0000000+_idt
243
244 .align 4
245 _idt:
246 .fill 256,8,0 # idt is uninitialized
247
248 /* 249 * The real GDT is also 256 entries long - no real reason 250 */ 251 .align 4
252 .word 0
253 gdt_descr:
254 .word 256*8-1
255 .long 0xc0000000+_gdt
256
257 /* 258 * This gdt setup gives the kernel a 1GB address space at virtual 259 * address 0xC0000000 - space enough for expansion, I hope. 260 */ 261 .align 4
262 _gdt:
263 .quad 0x0000000000000000 /* NULL descriptor */ 264 .quad 0xc0c39a000000ffff /* 1GB at 0xC0000000 */ 265 .quad 0xc0c392000000ffff /* 1GB */ 266 .quad 0x0000000000000000 /* TEMPORARY - don't use */ 267 .fill 252,8,0 /* space for LDT's and TSS's etc */