FreeBSD/Linux Kernel Cross Reference
sys/i386/i386/locore.s
1 /*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: @(#)locore.s 7.3 (Berkeley) 5/13/91
37 * $FreeBSD$
38 *
39 * originally from: locore.s, by William F. Jolitz
40 *
41 * Substantially rewritten by David Greenman, Rod Grimes,
42 * Bruce Evans, Wolfgang Solfrank, Poul-Henning Kamp
43 * and many others.
44 */
45
46 #include "opt_bootp.h"
47 #include "opt_nfsroot.h"
48
49 #include <sys/syscall.h>
50 #include <sys/reboot.h>
51
52 #include <machine/asmacros.h>
53 #include <machine/cputypes.h>
54 #include <machine/psl.h>
55 #include <machine/pmap.h>
56 #include <machine/specialreg.h>
57
58 #include "assym.s"
59
60 /*
61 * XXX
62 *
63 * Note: This version greatly munged to avoid various assembler errors
64 * that may be fixed in newer versions of gas. Perhaps newer versions
65 * will have more pleasant appearance.
66 */
67
68 /*
69 * PTmap is recursive pagemap at top of virtual address space.
70 * Within PTmap, the page directory can be found (third indirection).
71 */
72 .globl _PTmap,_PTD,_PTDpde
73 .set _PTmap,(PTDPTDI << PDRSHIFT)
74 .set _PTD,_PTmap + (PTDPTDI * PAGE_SIZE)
75 .set _PTDpde,_PTD + (PTDPTDI * PDESIZE)
76
77 /*
78 * Compiled KERNBASE location
79 */
80 .globl _kernbase
81 .set _kernbase,KERNBASE
82
83 /*
84 * Globals
85 */
86 .data
87 ALIGN_DATA /* just to be sure */
88
89 .globl tmpstk
90 .space 0x2000 /* space for tmpstk - temporary stack */
91 tmpstk:
92
93 .globl _boothowto,_bootdev
94
95 .globl _cpu,_cpu_vendor,_cpu_id,_bootinfo
96 .globl _cpu_high, _cpu_feature, _cpu_procinfo
97
98 _cpu: .long 0 /* are we 386, 386sx, or 486 */
99 _cpu_id: .long 0 /* stepping ID */
100 _cpu_high: .long 0 /* highest arg to CPUID */
101 _cpu_feature: .long 0 /* features */
102 _cpu_procinfo: .long 0 /* brand index / HTT info */
103 _cpu_vendor: .space 20 /* CPU origin code */
104 _bootinfo: .space BOOTINFO_SIZE /* bootinfo that we can handle */
105
106 .globl _KERNend
107 _KERNend: .long 0 /* phys addr end of kernel (just after bss) */
108 physfree: .long 0 /* phys addr of next free page */
109
110 #ifdef SMP
111 .globl _cpu0prvpage
112 cpu0pp: .long 0 /* phys addr cpu0 private pg */
113 _cpu0prvpage: .long 0 /* relocated version */
114
115 .globl _SMPpt
116 SMPptpa: .long 0 /* phys addr SMP page table */
117 _SMPpt: .long 0 /* relocated version */
118 #endif /* SMP */
119
120 .globl _IdlePTD
121 _IdlePTD: .long 0 /* phys addr of kernel PTD */
122
123 #ifdef PAE
124 .globl _IdlePDPT
125 .p2align 5
126 _IdlePDPT: .space 32
127 #endif
128
129 #ifdef SMP
130 .globl _KPTphys
131 #endif
132 _KPTphys: .long 0 /* phys addr of kernel page tables */
133
134 .globl _proc0paddr
135 _proc0paddr: .long 0 /* address of proc 0 address space */
136 p0upa: .long 0 /* phys addr of proc0's UPAGES */
137
138 vm86phystk: .long 0 /* PA of vm86/bios stack */
139
140 .globl _vm86paddr, _vm86pa
141 _vm86paddr: .long 0 /* address of vm86 region */
142 _vm86pa: .long 0 /* phys addr of vm86 region */
143
144 #ifdef BDE_DEBUGGER
145 .globl _bdb_exists /* flag to indicate BDE debugger is present */
146 _bdb_exists: .long 0
147 #endif
148
149 #ifdef PC98
150 .globl _pc98_system_parameter
151 _pc98_system_parameter:
152 .space 0x240
153 #endif
154
155 /**********************************************************************
156 *
157 * Some handy macros
158 *
159 */
160
161 #define R(foo) ((foo)-KERNBASE)
162
163 #define ALLOCPAGES(foo) \
164 movl R(physfree), %esi ; \
165 movl $((foo)*PAGE_SIZE), %eax ; \
166 addl %esi, %eax ; \
167 movl %eax, R(physfree) ; \
168 movl %esi, %edi ; \
169 movl $((foo)*PAGE_SIZE),%ecx ; \
170 xorl %eax,%eax ; \
171 cld ; \
172 rep ; \
173 stosb
174
175 /*
176 * fillkpt
177 * eax = page frame address
178 * ebx = index into page table
179 * ecx = how many pages to map
180 * base = base address of page dir/table
181 * prot = protection bits
182 */
183 #define fillkpt(base, prot) \
184 shll $PTESHIFT,%ebx ; \
185 addl base,%ebx ; \
186 orl $PG_V,%eax ; \
187 orl prot,%eax ; \
188 1: movl %eax,(%ebx) ; \
189 addl $PAGE_SIZE,%eax ; /* increment physical address */ \
190 addl $PTESIZE,%ebx ; /* next pte */ \
191 loop 1b
192
193 /*
194 * fillkptphys(prot)
195 * eax = physical address
196 * ecx = how many pages to map
197 * prot = protection bits
198 */
199 #define fillkptphys(prot) \
200 movl %eax, %ebx ; \
201 shrl $PAGE_SHIFT, %ebx ; \
202 fillkpt(R(_KPTphys), prot)
203
204 .text
205 /**********************************************************************
206 *
207 * This is where the bootblocks start us, set the ball rolling...
208 *
209 */
210 NON_GPROF_ENTRY(btext)
211
212 #ifdef PC98
213 /* save SYSTEM PARAMETER for resume (NS/T or other) */
214 movl $0xa1400,%esi
215 movl $R(_pc98_system_parameter),%edi
216 movl $0x0240,%ecx
217 cld
218 rep
219 movsb
220 #else /* IBM-PC */
221 #ifdef BDE_DEBUGGER
222 #ifdef BIOS_STEALS_3K
223 cmpl $0x0375c339,0x95504
224 #else
225 cmpl $0x0375c339,0x96104 /* XXX - debugger signature */
226 #endif
227 jne 1f
228 movb $1,R(_bdb_exists)
229 1:
230 #endif
231 /* Tell the bios to warmboot next time */
232 movw $0x1234,0x472
233 #endif /* PC98 */
234
235 /* Set up a real frame in case the double return in newboot is executed. */
236 pushl %ebp
237 movl %esp, %ebp
238
239 /* Don't trust what the BIOS gives for eflags. */
240 pushl $PSL_KERNEL
241 popfl
242
243 /*
244 * Don't trust what the BIOS gives for %fs and %gs. Trust the bootstrap
245 * to set %cs, %ds, %es and %ss.
246 */
247 mov %ds, %ax
248 mov %ax, %fs
249 mov %ax, %gs
250
251 /*
252 * Clear the bss. Not all boot programs do it, and it is our job anyway.
253 *
254 * XXX we don't check that there is memory for our bss and page tables
255 * before using it.
256 *
257 * Note: we must be careful to not overwrite an active gdt or idt. In
258 * the !BDE_DEBUGGER case they are inactive from now until we switch to
259 * new ones, since we don't load any more segment registers or permit
260 * interrupts until after the switch. In the BDE_DEBUGGER case, we depend
261 * on the convention that the boot program is below 1MB and we are above
262 * 1MB to keep the gdt and idt away from the bss and page tables.
263 */
264 movl $R(_end),%ecx
265 movl $R(_edata),%edi
266 subl %edi,%ecx
267 xorl %eax,%eax
268 cld
269 rep
270 stosb
271
272 call recover_bootinfo
273
274 /* Get onto a stack that we can trust. */
275 /*
276 * XXX this step is delayed in case recover_bootinfo needs to return via
277 * the old stack, but it need not be, since recover_bootinfo actually
278 * returns via the old frame.
279 */
280 movl $R(tmpstk),%esp
281
282 #ifdef PC98
283 /* pc98_machine_type & M_EPSON_PC98 */
284 testb $0x02,R(_pc98_system_parameter)+220
285 jz 3f
286 /* epson_machine_id <= 0x0b */
287 cmpb $0x0b,R(_pc98_system_parameter)+224
288 ja 3f
289
290 /* count up memory */
291 movl $0x100000,%eax /* next, talley remaining memory */
292 movl $0xFFF-0x100,%ecx
293 1: movl 0(%eax),%ebx /* save location to check */
294 movl $0xa55a5aa5,0(%eax) /* write test pattern */
295 cmpl $0xa55a5aa5,0(%eax) /* does not check yet for rollover */
296 jne 2f
297 movl %ebx,0(%eax) /* restore memory */
298 addl $PAGE_SIZE,%eax
299 loop 1b
300 2: subl $0x100000,%eax
301 shrl $17,%eax
302 movb %al,R(_pc98_system_parameter)+1
303 3:
304
305 movw R(_pc98_system_parameter+0x86),%ax
306 movw %ax,R(_cpu_id)
307 #endif
308
309 call identify_cpu
310 call create_pagetables
311
312 /*
313 * If the CPU has support for VME, turn it on.
314 */
315 testl $CPUID_VME, R(_cpu_feature)
316 jz 1f
317 movl %cr4, %eax
318 orl $CR4_VME, %eax
319 movl %eax, %cr4
320 1:
321
322 #ifdef BDE_DEBUGGER
323 /*
324 * Adjust as much as possible for paging before enabling paging so that the
325 * adjustments can be traced.
326 */
327 call bdb_prepare_paging
328 #endif
329
330 /* Now enable paging */
331 #ifdef PAE
332 movl %cr4,%eax
333 orl $CR4_PAE,%eax
334 movl %eax,%cr4
335 movl $R(_IdlePDPT),%eax
336 #else
337 movl R(_IdlePTD),%eax
338 #endif
339 movl %eax,%cr3 /* load ptd addr into mmu */
340 movl %cr0,%eax /* get control word */
341 orl $CR0_PE|CR0_PG,%eax /* enable paging */
342 movl %eax,%cr0 /* and let's page NOW! */
343
344 #ifdef BDE_DEBUGGER
345 /*
346 * Complete the adjustments for paging so that we can keep tracing through
347 * initi386() after the low (physical) addresses for the gdt and idt become
348 * invalid.
349 */
350 call bdb_commit_paging
351 #endif
352
353 pushl $begin /* jump to high virtualized address */
354 ret
355
356 /* now running relocated at KERNBASE where the system is linked to run */
357 begin:
358 /* set up bootstrap stack */
359 movl _proc0paddr,%esp /* location of in-kernel pages */
360 addl $UPAGES*PAGE_SIZE,%esp /* bootstrap stack end location */
361 xorl %eax,%eax /* mark end of frames */
362 movl %eax,%ebp
363 movl _proc0paddr,%eax
364 movl %cr3,%esi
365 movl %esi,PCB_CR3(%eax)
366
367 testl $CPUID_PGE, R(_cpu_feature)
368 jz 1f
369 movl %cr4, %eax
370 orl $CR4_PGE, %eax
371 movl %eax, %cr4
372 1:
373
374 movl physfree, %esi
375 pushl %esi /* value of first for init386(first) */
376 call _init386 /* wire 386 chip for unix operation */
377 popl %esi
378
379 call _mi_startup /* autoconfiguration, mountroot etc */
380
381 hlt /* never returns to here */
382
383 /*
384 * Signal trampoline, copied to top of user stack
385 */
386 NON_GPROF_ENTRY(sigcode)
387 call *SIGF_HANDLER(%esp) /* call signal handler */
388 lea SIGF_UC(%esp),%eax /* get ucontext_t */
389 pushl %eax
390 testl $PSL_VM,UC_EFLAGS(%eax)
391 jne 9f
392 movl UC_GS(%eax),%gs /* restore %gs */
393 9:
394 movl $SYS_sigreturn,%eax
395 pushl %eax /* junk to fake return addr. */
396 int $0x80 /* enter kernel with args */
397 0: jmp 0b
398
399 ALIGN_TEXT
400 _osigcode:
401 call *SIGF_HANDLER(%esp) /* call signal handler */
402 lea SIGF_SC(%esp),%eax /* get sigcontext */
403 pushl %eax
404 testl $PSL_VM,SC_PS(%eax)
405 jne 9f
406 movl SC_GS(%eax),%gs /* restore %gs */
407 9:
408 movl $0x01d516,SC_TRAPNO(%eax) /* magic: 0ldSiG */
409 movl $SYS_sigreturn,%eax
410 pushl %eax /* junk to fake return addr. */
411 int $0x80 /* enter kernel with args */
412 0: jmp 0b
413
414 ALIGN_TEXT
415 _esigcode:
416
417 .data
418 .globl _szsigcode, _szosigcode
419 _szsigcode:
420 .long _esigcode-_sigcode
421 _szosigcode:
422 .long _esigcode-_osigcode
423 .text
424
425 /**********************************************************************
426 *
427 * Recover the bootinfo passed to us from the boot program
428 *
429 */
430 recover_bootinfo:
431 /*
432 * This code is called in different ways depending on what loaded
433 * and started the kernel. This is used to detect how we get the
434 * arguments from the other code and what we do with them.
435 *
436 * Old disk boot blocks:
437 * (*btext)(howto, bootdev, cyloffset, esym);
438 * [return address == 0, and can NOT be returned to]
439 * [cyloffset was not supported by the FreeBSD boot code
440 * and always passed in as 0]
441 * [esym is also known as total in the boot code, and
442 * was never properly supported by the FreeBSD boot code]
443 *
444 * Old diskless netboot code:
445 * (*btext)(0,0,0,0,&nfsdiskless,0,0,0);
446 * [return address != 0, and can NOT be returned to]
447 * If we are being booted by this code it will NOT work,
448 * so we are just going to halt if we find this case.
449 *
450 * New uniform boot code:
451 * (*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
452 * [return address != 0, and can be returned to]
453 *
454 * There may seem to be a lot of wasted arguments in here, but
455 * that is so the newer boot code can still load very old kernels
456 * and old boot code can load new kernels.
457 */
458
459 /*
460 * The old style disk boot blocks fake a frame on the stack and
461 * did an lret to get here. The frame on the stack has a return
462 * address of 0.
463 */
464 cmpl $0,4(%ebp)
465 je olddiskboot
466
467 /*
468 * We have some form of return address, so this is either the
469 * old diskless netboot code, or the new uniform code. That can
470 * be detected by looking at the 5th argument, if it is 0
471 * we are being booted by the new uniform boot code.
472 */
473 cmpl $0,24(%ebp)
474 je newboot
475
476 /*
477 * Seems we have been loaded by the old diskless boot code, we
478 * don't stand a chance of running as the diskless structure
479 * changed considerably between the two, so just halt.
480 */
481 hlt
482
483 /*
484 * We have been loaded by the new uniform boot code.
485 * Let's check the bootinfo version, and if we do not understand
486 * it we return to the loader with a status of 1 to indicate this error
487 */
488 newboot:
489 movl 28(%ebp),%ebx /* &bootinfo.version */
490 movl BI_VERSION(%ebx),%eax
491 cmpl $1,%eax /* We only understand version 1 */
492 je 1f
493 movl $1,%eax /* Return status */
494 leave
495 /*
496 * XXX this returns to our caller's caller (as is required) since
497 * we didn't set up a frame and our caller did.
498 */
499 ret
500
501 1:
502 /*
503 * If we have a kernelname copy it in
504 */
505 movl BI_KERNELNAME(%ebx),%esi
506 cmpl $0,%esi
507 je 2f /* No kernelname */
508 movl $MAXPATHLEN,%ecx /* Brute force!!! */
509 movl $R(_kernelname),%edi
510 cmpb $'/',(%esi) /* Make sure it starts with a slash */
511 je 1f
512 movb $'/',(%edi)
513 incl %edi
514 decl %ecx
515 1:
516 cld
517 rep
518 movsb
519
520 2:
521 /*
522 * Determine the size of the boot loader's copy of the bootinfo
523 * struct. This is impossible to do properly because old versions
524 * of the struct don't contain a size field and there are 2 old
525 * versions with the same version number.
526 */
527 movl $BI_ENDCOMMON,%ecx /* prepare for sizeless version */
528 testl $RB_BOOTINFO,8(%ebp) /* bi_size (and bootinfo) valid? */
529 je got_bi_size /* no, sizeless version */
530 movl BI_SIZE(%ebx),%ecx
531 got_bi_size:
532
533 /*
534 * Copy the common part of the bootinfo struct
535 */
536 movl %ebx,%esi
537 movl $R(_bootinfo),%edi
538 cmpl $BOOTINFO_SIZE,%ecx
539 jbe got_common_bi_size
540 movl $BOOTINFO_SIZE,%ecx
541 got_common_bi_size:
542 cld
543 rep
544 movsb
545
546 #ifdef NFS_ROOT
547 #ifndef BOOTP_NFSV3
548 /*
549 * If we have a nfs_diskless structure copy it in
550 */
551 movl BI_NFS_DISKLESS(%ebx),%esi
552 cmpl $0,%esi
553 je olddiskboot
554 movl $R(_nfs_diskless),%edi
555 movl $NFSDISKLESS_SIZE,%ecx
556 cld
557 rep
558 movsb
559 movl $R(_nfs_diskless_valid),%edi
560 movl $1,(%edi)
561 #endif
562 #endif
563
564 /*
565 * The old style disk boot.
566 * (*btext)(howto, bootdev, cyloffset, esym);
567 * Note that the newer boot code just falls into here to pick
568 * up howto and bootdev, cyloffset and esym are no longer used
569 */
570 olddiskboot:
571 movl 8(%ebp),%eax
572 movl %eax,R(_boothowto)
573 movl 12(%ebp),%eax
574 movl %eax,R(_bootdev)
575
576 ret
577
578
579 /**********************************************************************
580 *
581 * Identify the CPU and initialize anything special about it
582 *
583 */
584 identify_cpu:
585
586 /* Try to toggle alignment check flag; does not exist on 386. */
587 pushfl
588 popl %eax
589 movl %eax,%ecx
590 orl $PSL_AC,%eax
591 pushl %eax
592 popfl
593 pushfl
594 popl %eax
595 xorl %ecx,%eax
596 andl $PSL_AC,%eax
597 pushl %ecx
598 popfl
599
600 testl %eax,%eax
601 jnz try486
602
603 /* NexGen CPU does not have aligment check flag. */
604 pushfl
605 movl $0x5555, %eax
606 xorl %edx, %edx
607 movl $2, %ecx
608 clc
609 divl %ecx
610 jz trynexgen
611 popfl
612 movl $CPU_386,R(_cpu)
613 jmp 3f
614
615 trynexgen:
616 popfl
617 movl $CPU_NX586,R(_cpu)
618 movl $0x4778654e,R(_cpu_vendor) # store vendor string
619 movl $0x72446e65,R(_cpu_vendor+4)
620 movl $0x6e657669,R(_cpu_vendor+8)
621 movl $0,R(_cpu_vendor+12)
622 jmp 3f
623
624 try486: /* Try to toggle identification flag; does not exist on early 486s. */
625 pushfl
626 popl %eax
627 movl %eax,%ecx
628 xorl $PSL_ID,%eax
629 pushl %eax
630 popfl
631 pushfl
632 popl %eax
633 xorl %ecx,%eax
634 andl $PSL_ID,%eax
635 pushl %ecx
636 popfl
637
638 testl %eax,%eax
639 jnz trycpuid
640 movl $CPU_486,R(_cpu)
641
642 /*
643 * Check Cyrix CPU
644 * Cyrix CPUs do not change the undefined flags following
645 * execution of the divide instruction which divides 5 by 2.
646 *
647 * Note: CPUID is enabled on M2, so it passes another way.
648 */
649 pushfl
650 movl $0x5555, %eax
651 xorl %edx, %edx
652 movl $2, %ecx
653 clc
654 divl %ecx
655 jnc trycyrix
656 popfl
657 jmp 3f /* You may use Intel CPU. */
658
659 trycyrix:
660 popfl
661 /*
662 * IBM Bluelighting CPU also doesn't change the undefined flags.
663 * Because IBM doesn't disclose the information for Bluelighting
664 * CPU, we couldn't distinguish it from Cyrix's (including IBM
665 * brand of Cyrix CPUs).
666 */
667 movl $0x69727943,R(_cpu_vendor) # store vendor string
668 movl $0x736e4978,R(_cpu_vendor+4)
669 movl $0x64616574,R(_cpu_vendor+8)
670 jmp 3f
671
672 trycpuid: /* Use the `cpuid' instruction. */
673 xorl %eax,%eax
674 cpuid # cpuid 0
675 movl %eax,R(_cpu_high) # highest capability
676 movl %ebx,R(_cpu_vendor) # store vendor string
677 movl %edx,R(_cpu_vendor+4)
678 movl %ecx,R(_cpu_vendor+8)
679 movb $0,R(_cpu_vendor+12)
680
681 movl $1,%eax
682 cpuid # cpuid 1
683 movl %eax,R(_cpu_id) # store cpu_id
684 movl %ebx,R(_cpu_procinfo) # store cpu_procinfo
685 movl %edx,R(_cpu_feature) # store cpu_feature
686 rorl $8,%eax # extract family type
687 andl $15,%eax
688 cmpl $5,%eax
689 jae 1f
690
691 /* less than Pentium; must be 486 */
692 movl $CPU_486,R(_cpu)
693 jmp 3f
694 1:
695 /* a Pentium? */
696 cmpl $5,%eax
697 jne 2f
698 movl $CPU_586,R(_cpu)
699 jmp 3f
700 2:
701 /* Greater than Pentium...call it a Pentium Pro */
702 movl $CPU_686,R(_cpu)
703 3:
704 ret
705
706
707 /**********************************************************************
708 *
709 * Create the first page directory and its page tables.
710 *
711 */
712
713 create_pagetables:
714
715 /* Find end of kernel image (rounded up to a page boundary). */
716 movl $R(_end),%esi
717
718 /* Include symbols, if any. */
719 movl R(_bootinfo+BI_ESYMTAB),%edi
720 testl %edi,%edi
721 je over_symalloc
722 movl %edi,%esi
723 movl $KERNBASE,%edi
724 addl %edi,R(_bootinfo+BI_SYMTAB)
725 addl %edi,R(_bootinfo+BI_ESYMTAB)
726 over_symalloc:
727
728 /* If we are told where the end of the kernel space is, believe it. */
729 movl R(_bootinfo+BI_KERNEND),%edi
730 testl %edi,%edi
731 je no_kernend
732 movl %edi,%esi
733 no_kernend:
734
735 addl $PAGE_MASK,%esi
736 andl $~PAGE_MASK,%esi
737 movl %esi,R(_KERNend) /* save end of kernel */
738 movl %esi,R(physfree) /* next free page is at end of kernel */
739
740 /* Allocate Kernel Page Tables */
741 ALLOCPAGES(NKPT)
742 movl %esi,R(_KPTphys)
743
744 /* Allocate Page Table Directory */
745 ALLOCPAGES(NPGPTD)
746 movl %esi,R(_IdlePTD)
747
748 /* Allocate UPAGES */
749 ALLOCPAGES(UPAGES)
750 movl %esi,R(p0upa)
751 addl $KERNBASE, %esi
752 movl %esi, R(_proc0paddr)
753
754 ALLOCPAGES(1) /* vm86/bios stack */
755 movl %esi,R(vm86phystk)
756
757 ALLOCPAGES(3) /* pgtable + ext + IOPAGES */
758 movl %esi,R(_vm86pa)
759 addl $KERNBASE, %esi
760 movl %esi, R(_vm86paddr)
761
762 #ifdef SMP
763 /* Allocate cpu0's private data page */
764 ALLOCPAGES(1)
765 movl %esi,R(cpu0pp)
766 addl $KERNBASE, %esi
767 movl %esi, R(_cpu0prvpage) /* relocated to KVM space */
768
769 /* Allocate SMP page table page */
770 ALLOCPAGES(1)
771 movl %esi,R(SMPptpa)
772 addl $KERNBASE, %esi
773 movl %esi, R(_SMPpt) /* relocated to KVM space */
774 #endif /* SMP */
775
776 /* Map page zero read-write so bios32 calls can use it */
777 xorl %eax, %eax
778 #ifdef BDE_DEBUGGER
779 /* If the debugger is present, actually map everything read-write. */
780 cmpl $0,R(_bdb_exists)
781 jne map_read_write
782 #endif
783 movl $PG_RW,%edx
784 movl $1,%ecx
785 fillkptphys(%edx)
786
787 /* Map read-only from page 1 to the end of the kernel text section */
788 movl $PAGE_SIZE, %eax
789 xorl %edx,%edx
790 #if !defined(SMP)
791 testl $CPUID_PGE, R(_cpu_feature)
792 jz 2f
793 orl $PG_G,%edx
794 #endif
795
796 2: movl $R(_etext),%ecx
797 addl $PAGE_MASK,%ecx
798 subl %eax,%ecx
799 shrl $PAGE_SHIFT,%ecx
800 fillkptphys(%edx)
801
802 /* Map read-write, data, bss and symbols */
803 movl $R(_etext),%eax
804 addl $PAGE_MASK, %eax
805 andl $~PAGE_MASK, %eax
806 map_read_write:
807 movl $PG_RW,%edx
808 #if !defined(SMP)
809 testl $CPUID_PGE, R(_cpu_feature)
810 jz 1f
811 orl $PG_G,%edx
812 #endif
813
814 1: movl R(_KERNend),%ecx
815 subl %eax,%ecx
816 shrl $PAGE_SHIFT,%ecx
817 fillkptphys(%edx)
818
819 /* Map page directory. */
820 movl R(_IdlePTD), %eax
821 movl $NPGPTD, %ecx
822 fillkptphys($PG_RW)
823
824 /* Map proc0's UPAGES in the physical way ... */
825 movl R(p0upa), %eax
826 movl $UPAGES, %ecx
827 fillkptphys($PG_RW)
828
829 /* Map ISA hole */
830 movl $ISA_HOLE_START, %eax
831 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
832 fillkptphys($PG_RW)
833
834 /* Map space for the vm86 region */
835 movl R(vm86phystk), %eax
836 movl $4, %ecx
837 fillkptphys($PG_RW)
838
839 /* Map page 0 into the vm86 page table */
840 movl $0, %eax
841 movl $0, %ebx
842 movl $1, %ecx
843 fillkpt(R(_vm86pa), $PG_RW|PG_U)
844
845 /* ...likewise for the ISA hole */
846 movl $ISA_HOLE_START, %eax
847 movl $ISA_HOLE_START>>PAGE_SHIFT, %ebx
848 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
849 fillkpt(R(_vm86pa), $PG_RW|PG_U)
850
851 #ifdef SMP
852 /* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */
853 movl R(cpu0pp), %eax
854 movl $1, %ecx
855 fillkptphys($PG_RW)
856
857 /* Map SMP page table page into global kmem FWIW */
858 movl R(SMPptpa), %eax
859 movl $1, %ecx
860 fillkptphys($PG_RW)
861
862 /* Map the private page into the SMP page table */
863 movl R(cpu0pp), %eax
864 movl $0, %ebx /* pte offset = 0 */
865 movl $1, %ecx /* one private page coming right up */
866 fillkpt(R(SMPptpa), $PG_RW)
867
868 /* ... and put the page table table in the pde. */
869 movl R(SMPptpa), %eax
870 movl $MPPTDI, %ebx
871 movl $1, %ecx
872 fillkpt(R(_IdlePTD), $PG_RW)
873
874 /* Fakeup VA for the local apic to allow early traps. */
875 ALLOCPAGES(1)
876 movl %esi, %eax
877 movl $(NPTEPG-1), %ebx /* pte offset = NTEPG-1 */
878 movl $1, %ecx /* one private pt coming right up */
879 fillkpt(R(SMPptpa), $PG_RW)
880
881 /* Initialize mp lock to allow early traps */
882 movl $1, R(_mp_lock)
883 #endif /* SMP */
884
885 /* install a pde for temporary double map of bottom of VA */
886 movl R(_KPTphys), %eax
887 xorl %ebx, %ebx
888 movl $NKPT, %ecx
889 fillkpt(R(_IdlePTD), $PG_RW)
890
891 /* install pde's for pt's */
892 movl R(_KPTphys), %eax
893 movl $KPTDI, %ebx
894 movl $NKPT, %ecx
895 fillkpt(R(_IdlePTD), $PG_RW)
896
897 /* install a pde recursively mapping page directory as a page table */
898 movl R(_IdlePTD), %eax
899 movl $PTDPTDI, %ebx
900 movl $NPGPTD, %ecx
901 fillkpt(R(_IdlePTD), $PG_RW)
902
903 #ifdef PAE
904 movl R(_IdlePTD),%eax
905 xorl %ebx,%ebx
906 movl $NPGPTD,%ecx
907 fillkpt($R(_IdlePDPT), $0)
908 #endif
909 ret
910
911 #ifdef BDE_DEBUGGER
912 bdb_prepare_paging:
913 cmpl $0,R(_bdb_exists)
914 je bdb_prepare_paging_exit
915
916 subl $6,%esp
917
918 /*
919 * Copy and convert debugger entries from the bootstrap gdt and idt
920 * to the kernel gdt and idt. Everything is still in low memory.
921 * Tracing continues to work after paging is enabled because the
922 * low memory addresses remain valid until everything is relocated.
923 * However, tracing through the setidt() that initializes the trace
924 * trap will crash.
925 */
926 sgdt (%esp)
927 movl 2(%esp),%esi /* base address of bootstrap gdt */
928 movl $R(_gdt),%edi
929 movl %edi,2(%esp) /* prepare to load kernel gdt */
930 movl $8*18/4,%ecx
931 cld
932 rep /* copy gdt */
933 movsl
934 movl $R(_gdt),-8+2(%edi) /* adjust gdt self-ptr */
935 movb $0x92,-8+5(%edi)
936 lgdt (%esp)
937
938 sidt (%esp)
939 movl 2(%esp),%esi /* base address of current idt */
940 movl 8+4(%esi),%eax /* convert dbg descriptor to ... */
941 movw 8(%esi),%ax
942 movl %eax,R(bdb_dbg_ljmp+1) /* ... immediate offset ... */
943 movl 8+2(%esi),%eax
944 movw %ax,R(bdb_dbg_ljmp+5) /* ... and selector for ljmp */
945 movl 24+4(%esi),%eax /* same for bpt descriptor */
946 movw 24(%esi),%ax
947 movl %eax,R(bdb_bpt_ljmp+1)
948 movl 24+2(%esi),%eax
949 movw %ax,R(bdb_bpt_ljmp+5)
950 movl R(_idt),%edi
951 movl %edi,2(%esp) /* prepare to load kernel idt */
952 movl $8*4/4,%ecx
953 cld
954 rep /* copy idt */
955 movsl
956 lidt (%esp)
957
958 addl $6,%esp
959
960 bdb_prepare_paging_exit:
961 ret
962
963 /* Relocate debugger gdt entries and gdt and idt pointers. */
964 bdb_commit_paging:
965 cmpl $0,_bdb_exists
966 je bdb_commit_paging_exit
967
968 movl $_gdt+8*9,%eax /* adjust slots 9-17 */
969 movl $9,%ecx
970 reloc_gdt:
971 movb $KERNBASE>>24,7(%eax) /* top byte of base addresses, was 0, */
972 addl $8,%eax /* now KERNBASE>>24 */
973 loop reloc_gdt
974
975 subl $6,%esp
976 sgdt (%esp)
977 addl $KERNBASE,2(%esp)
978 lgdt (%esp)
979 sidt (%esp)
980 addl $KERNBASE,2(%esp)
981 lidt (%esp)
982 addl $6,%esp
983
984 int $3
985
986 bdb_commit_paging_exit:
987 ret
988
989 #endif /* BDE_DEBUGGER */
Cache object: 718af8a6f0dcd5c308175da603f9b14c
|