1 /* 2 * linux/boot/head.s 3 * 4 * (C) 1991 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,_pg_dir,_tmp_floppy_area,_floppy_track_buffer
16 _pg_dir:
17 startup_32:
18 cld
19 movl $0x10,%eax
20 mov %ax,%ds
21 mov %ax,%es
22 mov %ax,%fs
23 mov %ax,%gs
24 lss _stack_start,%esp
25 call setup_idt
26 call setup_gdt
27 movl $0x10,%eax # reload all the segment registers
28 mov %ax,%ds # after changing gdt. CS was already
29 mov %ax,%es # reloaded in 'setup_gdt'
30 mov %ax,%fs
31 mov %ax,%gs
32 lss _stack_start,%esp
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 fninit
79 fstsw %ax
80 cmpb $0,%al
81 je 1f
82 movl %cr0,%eax /* no coprocessor: have to set bits */ 83 xorl $6,%eax /* reset MP, set EM */ 84 movl %eax,%cr0
85 ret
86 .align 2
87 1: .byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */ 88 ret
89
90 /* 91 * setup_idt 92 * 93 * sets up a idt with 256 entries pointing to 94 * ignore_int, interrupt gates. It then loads 95 * idt. Everything that wants to install itself 96 * in the idt-table may do so themselves. Interrupts 97 * are enabled elsewhere, when we can be relatively 98 * sure everything is ok. This routine will be over- 99 * written by the page tables. 100 */ 101 setup_idt:
102 lea ignore_int,%edx
103 movl $0x00080000,%eax
104 movw %dx,%ax /* selector = 0x0008 = cs */ 105 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */ 106
107 lea _idt,%edi
108 mov $256,%ecx
109 rp_sidt:
110 movl %eax,(%edi)
111 movl %edx,4(%edi)
112 addl $8,%edi
113 dec %ecx
114 jne rp_sidt
115 lidt idt_descr
116 ret
117
118 /* 119 * setup_gdt 120 * 121 * This routines sets up a new gdt and loads it. 122 * Only two entries are currently built, the same 123 * ones that were built in init.s. The routine 124 * is VERY complicated at two whole lines, so this 125 * rather long comment is certainly needed :-). 126 * This routine will beoverwritten by the page tables. 127 */ 128 setup_gdt:
129 lgdt gdt_descr
130 ret
131
132 /* 133 * I put the kernel page tables right after the page directory, 134 * using 4 of them to span 16 Mb of physical memory. People with 135 * more than 16MB will have to expand this. 136 */ 137 .org 0x1000
138 pg0:
139
140 .org 0x2000
141 pg1:
142
143 .org 0x3000
144 pg2:
145
146 .org 0x4000
147 pg3:
148
149 .org 0x5000
150 /* 151 * tmp_floppy_area is used by the floppy-driver when DMA cannot 152 * reach to a buffer-block. It needs to be aligned, so that it isn't 153 * on a 64kB border. 154 */ 155 _tmp_floppy_area:
156 .fill 1024,1,0
157 /* 158 * floppy_track_buffer is used to buffer one track of floppy data: it 159 * has to be separate from the tmp_floppy area, as otherwise a single- 160 * sector read/write can mess it up. It can contain one full track of 161 * data (18*2*512 bytes). 162 */ 163 _floppy_track_buffer:
164 .fill 512*2*18,1,0
165
166 after_page_tables:
167 call setup_paging
168 pushl $0 # These are the parameters to main :-)
169 pushl $0
170 pushl $0
171 cld # gcc2 wants the direction flag cleared at all times
172 call _start_kernel
173 L6:
174 jmp L6 # main should never return here, but
175 # just in case, we know what happens. 176
177 /* This is the default interrupt "handler" :-) */ 178 int_msg:
179 .asciz "Unknown interrupt\n\r"
180 .align 2
181 ignore_int:
182 cld
183 pushl %eax
184 pushl %ecx
185 pushl %edx
186 push %ds
187 push %es
188 push %fs
189 movl $0x10,%eax
190 mov %ax,%ds
191 mov %ax,%es
192 mov %ax,%fs
193 pushl $int_msg
194 call _printk
195 popl %eax
196 pop %fs
197 pop %es
198 pop %ds
199 popl %edx
200 popl %ecx
201 popl %eax
202 iret
203
204
205 /* 206 * Setup_paging 207 * 208 * This routine sets up paging by setting the page bit 209 * in cr0. The page tables are set up, identity-mapping 210 * the first 16MB. The pager assumes that no illegal 211 * addresses are produced (ie >4Mb on a 4Mb machine). 212 * 213 * NOTE! Although all physical memory should be identity 214 * mapped by this routine, only the kernel page functions 215 * use the >1Mb addresses directly. All "normal" functions 216 * use just the lower 1Mb, or the local data space, which 217 * will be mapped to some other place - mm keeps track of 218 * that. 219 * 220 * For those with more memory than 16 Mb - tough luck. I've 221 * not got it, why should you :-) The source is here. Change 222 * it. (Seriously - it shouldn't be too difficult. Mostly 223 * change some constants etc. I left it at 16Mb, as my machine 224 * even cannot be extended past that (ok, but it was cheap :-) 225 * I've tried to show which constants to change by having 226 * some kind of marker at them (search for "16Mb"), but I 227 * won't guarantee that's all :-( ) 228 */ 229 .align 2
230 setup_paging:
231 movl $1024*5,%ecx /* 5 pages - pg_dir+4 page tables */ 232 xorl %eax,%eax
233 xorl %edi,%edi /* pg_dir is at 0x000 */ 234 cld;rep;stosl
235 movl $pg0+7,_pg_dir /* set present bit/user r/w */ 236 movl $pg1+7,_pg_dir+4 /* --------- " " --------- */ 237 movl $pg2+7,_pg_dir+8 /* --------- " " --------- */ 238 movl $pg3+7,_pg_dir+12 /* --------- " " --------- */ 239 movl $pg3+4092,%edi
240 movl $0xfff007,%eax /* 16Mb - 4096 + 7 (r/w user,p) */ 241 std
242 1: stosl /* fill pages backwards - more efficient :-) */ 243 subl $0x1000,%eax
244 jge 1b
245 cld
246 xorl %eax,%eax /* pg_dir is at 0x0000 */ 247 movl %eax,%cr3 /* cr3 - page directory start */ 248 movl %cr0,%eax
249 orl $0x80000000,%eax
250 movl %eax,%cr0 /* set paging (PG) bit */ 251 ret /* this also flushes prefetch-queue */ 252
253 .align 2
254 .word 0
255 idt_descr:
256 .word 256*8-1 # idt contains 256 entries
257 .long _idt
258 .align 2
259 .word 0
260 gdt_descr:
261 .word 256*8-1 # so does gdt (not that that's any
262 .long _gdt # magic number, but it works for me :^)
263
264 .align 3
265 _idt: .fill 256,8,0 # idt is uninitialized
266
267 _gdt: .quad 0x0000000000000000 /* NULL descriptor */ 268 .quad 0x00c09a0000000fff /* 16Mb */ 269 .quad 0x00c0920000000fff /* 16Mb */ 270 .quad 0x0000000000000000 /* TEMPORARY - don't use */ 271 .fill 252,8,0 /* space for LDT's and TSS's etc */