2 * Copyright (c) 1990 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * from: @(#)locore.s 7.3 (Berkeley) 5/13/91
37 * $Id: locore.s,v 1.121 1999/04/28 01:03:20 luoqi Exp $
39 * originally from: locore.s, by William F. Jolitz
41 * Substantially rewritten by David Greenman, Rod Grimes,
42 * Bruce Evans, Wolfgang Solfrank, Poul-Henning Kamp
47 #include "opt_bootp.h"
49 #include "opt_nfsroot.h"
50 #include "opt_userconfig.h"
53 #include <sys/syscall.h>
54 #include <sys/reboot.h>
56 #include <machine/asmacros.h>
57 #include <machine/cputypes.h>
58 #include <machine/psl.h>
59 #include <machine/pmap.h>
60 #include <machine/specialreg.h>
67 * Note: This version greatly munged to avoid various assembler errors
68 * that may be fixed in newer versions of gas. Perhaps newer versions
69 * will have more pleasant appearance.
73 * PTmap is recursive pagemap at top of virtual address space.
74 * Within PTmap, the page directory can be found (third indirection).
76 .globl _PTmap,_PTD,_PTDpde
77 .set _PTmap,(PTDPTDI << PDRSHIFT)
78 .set _PTD,_PTmap + (PTDPTDI * PAGE_SIZE)
79 .set _PTDpde,_PTD + (PTDPTDI * PDESIZE)
82 * APTmap, APTD is the alternate recursive pagemap.
83 * It's used when modifying another process's page tables.
85 .globl _APTmap,_APTD,_APTDpde
86 .set _APTmap,APTDPTDI << PDRSHIFT
87 .set _APTD,_APTmap + (APTDPTDI * PAGE_SIZE)
88 .set _APTDpde,_PTD + (APTDPTDI * PDESIZE)
94 ALIGN_DATA /* just to be sure */
96 .globl HIDENAME(tmpstk)
97 .space 0x2000 /* space for tmpstk - temporary stack */
100 .globl _boothowto,_bootdev
102 .globl _cpu,_cpu_vendor,_cpu_id,_bootinfo
103 .globl _cpu_high, _cpu_feature
105 _cpu: .long 0 /* are we 386, 386sx, or 486 */
106 _cpu_id: .long 0 /* stepping ID */
107 _cpu_high: .long 0 /* highest arg to CPUID */
108 _cpu_feature: .long 0 /* features */
109 _cpu_vendor: .space 20 /* CPU origin code */
110 _bootinfo: .space BOOTINFO_SIZE /* bootinfo that we can handle */
112 _KERNend: .long 0 /* phys addr end of kernel (just after bss) */
113 physfree: .long 0 /* phys addr of next free page */
117 cpu0pp: .long 0 /* phys addr cpu0 private pg */
118 _cpu0prvpage: .long 0 /* relocated version */
121 SMPptpa: .long 0 /* phys addr SMP page table */
122 _SMPpt: .long 0 /* relocated version */
126 _IdlePTD: .long 0 /* phys addr of kernel PTD */
131 _KPTphys: .long 0 /* phys addr of kernel page tables */
134 _proc0paddr: .long 0 /* address of proc 0 address space */
135 p0upa: .long 0 /* phys addr of proc0's UPAGES */
138 vm86phystk: .long 0 /* PA of vm86/bios stack */
140 .globl _vm86paddr, _vm86pa
141 _vm86paddr: .long 0 /* address of vm86 region */
142 _vm86pa: .long 0 /* phys addr of vm86 region */
146 .globl _bdb_exists /* flag to indicate BDE debugger is present */
151 .globl _pc98_system_parameter
152 _pc98_system_parameter:
156 /**********************************************************************
162 #define R(foo) ((foo)-KERNBASE)
164 #define ALLOCPAGES(foo) \
165 movl R(physfree), %esi ; \
166 movl $((foo)*PAGE_SIZE), %eax ; \
168 movl %eax, R(physfree) ; \
170 movl $((foo)*PAGE_SIZE),%ecx ; \
178 * eax = page frame address
179 * ebx = index into page table
180 * ecx = how many pages to map
181 * base = base address of page dir/table
182 * prot = protection bits
184 #define fillkpt(base, prot) \
189 1: movl %eax,(%ebx) ; \
190 addl $PAGE_SIZE,%eax ; /* increment physical address */ \
191 addl $4,%ebx ; /* next pte */ \
196 * eax = physical address
197 * ecx = how many pages to map
198 * prot = protection bits
200 #define fillkptphys(prot) \
202 shrl $PAGE_SHIFT, %ebx ; \
203 fillkpt(R(_KPTphys), prot)
206 /**********************************************************************
208 * This is where the bootblocks start us, set the ball rolling...
211 NON_GPROF_ENTRY(btext)
214 /* save SYSTEM PARAMETER for resume (NS/T or other) */
216 movl $R(_pc98_system_parameter),%edi
223 #ifdef BIOS_STEALS_3K
224 cmpl $0x0375c339,0x95504
226 cmpl $0x0375c339,0x96104 /* XXX - debugger signature */
229 movb $1,R(_bdb_exists)
234 /* Tell the bios to warmboot next time */
237 /* Set up a real frame in case the double return in newboot is executed. */
241 /* Don't trust what the BIOS gives for eflags. */
246 * Don't trust what the BIOS gives for %fs and %gs. Trust the bootstrap
247 * to set %cs, %ds, %es and %ss.
253 call recover_bootinfo
255 /* Get onto a stack that we can trust. */
257 * XXX this step is delayed in case recover_bootinfo needs to return via
258 * the old stack, but it need not be, since recover_bootinfo actually
259 * returns via the old frame.
261 movl $R(HIDENAME(tmpstk)),%esp
264 /* pc98_machine_type & M_EPSON_PC98 */
265 testb $0x02,R(_pc98_system_parameter)+220
267 /* epson_machine_id <= 0x0b */
268 cmpb $0x0b,R(_pc98_system_parameter)+224
271 /* count up memory */
272 movl $0x100000,%eax /* next, talley remaining memory */
273 movl $0xFFF-0x100,%ecx
274 1: movl 0(%eax),%ebx /* save location to check */
275 movl $0xa55a5aa5,0(%eax) /* write test pattern */
276 cmpl $0xa55a5aa5,0(%eax) /* does not check yet for rollover */
278 movl %ebx,0(%eax) /* restore memory */
281 2: subl $0x100000,%eax
283 movb %al,R(_pc98_system_parameter)+1
291 * XXX this should be done a little earlier.
293 * XXX we don't check that there is memory for our bss and page tables
296 * XXX the boot program somewhat bogusly clears the bss. We still have
297 * to do it in case we were unzipped by kzipboot. Then the boot program
298 * only clears kzipboot's bss.
300 * XXX the gdt and idt are still somewhere in the boot program. We
301 * depend on the convention that the boot program is below 1MB and we
302 * are above 1MB to keep the gdt and idt away from the bss and page
303 * tables. The idt is only used if BDE_DEBUGGER is enabled.
316 * XXX it's not clear that APM can live in the current environonment.
317 * Only pc-relative addressing works.
323 call create_pagetables
327 * If the CPU has support for VME, turn it on.
329 testl $CPUID_VME, R(_cpu_feature)
339 * Adjust as much as possible for paging before enabling paging so that the
340 * adjustments can be traced.
342 call bdb_prepare_paging
345 /* Now enable paging */
346 movl R(_IdlePTD), %eax
347 movl %eax,%cr3 /* load ptd addr into mmu */
348 movl %cr0,%eax /* get control word */
349 orl $CR0_PE|CR0_PG,%eax /* enable paging */
350 movl %eax,%cr0 /* and let's page NOW! */
354 * Complete the adjustments for paging so that we can keep tracing through
355 * initi386() after the low (physical) addresses for the gdt and idt become
358 call bdb_commit_paging
361 pushl $begin /* jump to high virtualized address */
364 /* now running relocated at KERNBASE where the system is linked to run */
366 /* set up bootstrap stack */
367 movl _proc0paddr,%esp /* location of in-kernel pages */
368 addl $UPAGES*PAGE_SIZE,%esp /* bootstrap stack end location */
369 xorl %eax,%eax /* mark end of frames */
371 movl _proc0paddr,%eax
373 movl %esi,PCB_CR3(%eax)
376 pushl %esi /* value of first for init386(first) */
377 call _init386 /* wire 386 chip for unix operation */
380 .globl __ucodesel,__udatasel
382 pushl $0 /* unused */
383 pushl __udatasel /* ss */
384 pushl $0 /* esp - filled in by execve() */
385 pushl $PSL_USER /* eflags (IOPL 0, int enab) */
386 pushl __ucodesel /* cs */
387 pushl $0 /* eip - filled in by execve() */
388 subl $(13*4),%esp /* space for rest of registers */
390 pushl %esp /* call main with frame pointer */
391 call _mi_startup /* autoconfiguration, mountroot etc */
393 hlt /* never returns to here */
396 * When starting init, call this to configure the process for user
397 * mode. This will be inherited by other processes.
399 NON_GPROF_ENTRY(prepare_usermode)
401 * Now we've run main() and determined what cpu-type we are, we can
402 * enable write protection and alignment checking on i486 cpus and
405 #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
406 cmpl $CPUCLASS_386,_cpu_class
408 movl %cr0,%eax /* get control word */
409 orl $CR0_WP|CR0_AM,%eax /* enable i486 features */
410 movl %eax,%cr0 /* and do it */
414 * on return from main(), we are process 1
415 * set up address space and stack so that we can 'return' to user mode
420 #if 0 /* ds/es/fs are in trap frame */
425 movl %cx,%gs /* and ds to gs */
429 #define LCALL(x,y) .byte 0x9a ; .long y ; .word x
432 * Signal trampoline, copied to top of user stack
434 NON_GPROF_ENTRY(sigcode)
435 call SIGF_HANDLER(%esp)
436 lea SIGF_SC(%esp),%eax /* scp (the call may have clobbered the */
437 /* copy at 8(%esp)) */
439 pushl %eax /* junk to fake return address */
440 movl $SYS_sigreturn,%eax /* sigreturn() */
441 LCALL(0x7,0) /* enter kernel with args on stack */
442 hlt /* never gets here */
449 .long _esigcode-_sigcode
452 /**********************************************************************
454 * Recover the bootinfo passed to us from the boot program
459 * This code is called in different ways depending on what loaded
460 * and started the kernel. This is used to detect how we get the
461 * arguments from the other code and what we do with them.
463 * Old disk boot blocks:
464 * (*btext)(howto, bootdev, cyloffset, esym);
465 * [return address == 0, and can NOT be returned to]
466 * [cyloffset was not supported by the FreeBSD boot code
467 * and always passed in as 0]
468 * [esym is also known as total in the boot code, and
469 * was never properly supported by the FreeBSD boot code]
471 * Old diskless netboot code:
472 * (*btext)(0,0,0,0,&nfsdiskless,0,0,0);
473 * [return address != 0, and can NOT be returned to]
474 * If we are being booted by this code it will NOT work,
475 * so we are just going to halt if we find this case.
477 * New uniform boot code:
478 * (*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
479 * [return address != 0, and can be returned to]
481 * There may seem to be a lot of wasted arguments in here, but
482 * that is so the newer boot code can still load very old kernels
483 * and old boot code can load new kernels.
487 * The old style disk boot blocks fake a frame on the stack and
488 * did an lret to get here. The frame on the stack has a return
495 * We have some form of return address, so this is either the
496 * old diskless netboot code, or the new uniform code. That can
497 * be detected by looking at the 5th argument, if it is 0
498 * we are being booted by the new uniform boot code.
504 * Seems we have been loaded by the old diskless boot code, we
505 * don't stand a chance of running as the diskless structure
506 * changed considerably between the two, so just halt.
511 * We have been loaded by the new uniform boot code.
512 * Let's check the bootinfo version, and if we do not understand
513 * it we return to the loader with a status of 1 to indicate this error
516 movl 28(%ebp),%ebx /* &bootinfo.version */
517 movl BI_VERSION(%ebx),%eax
518 cmpl $1,%eax /* We only understand version 1 */
520 movl $1,%eax /* Return status */
523 * XXX this returns to our caller's caller (as is required) since
524 * we didn't set up a frame and our caller did.
530 * If we have a kernelname copy it in
532 movl BI_KERNELNAME(%ebx),%esi
534 je 2f /* No kernelname */
535 movl $MAXPATHLEN,%ecx /* Brute force!!! */
536 movl $R(_kernelname),%edi
537 cmpb $'/',(%esi) /* Make sure it starts with a slash */
549 * Determine the size of the boot loader's copy of the bootinfo
550 * struct. This is impossible to do properly because old versions
551 * of the struct don't contain a size field and there are 2 old
552 * versions with the same version number.
554 movl $BI_ENDCOMMON,%ecx /* prepare for sizeless version */
555 testl $RB_BOOTINFO,8(%ebp) /* bi_size (and bootinfo) valid? */
556 je got_bi_size /* no, sizeless version */
557 movl BI_SIZE(%ebx),%ecx
561 * Copy the common part of the bootinfo struct
564 movl $R(_bootinfo),%edi
565 cmpl $BOOTINFO_SIZE,%ecx
566 jbe got_common_bi_size
567 movl $BOOTINFO_SIZE,%ecx
576 * If we have a nfs_diskless structure copy it in
578 movl BI_NFS_DISKLESS(%ebx),%esi
581 movl $R(_nfs_diskless),%edi
582 movl $NFSDISKLESS_SIZE,%ecx
586 movl $R(_nfs_diskless_valid),%edi
592 * The old style disk boot.
593 * (*btext)(howto, bootdev, cyloffset, esym);
594 * Note that the newer boot code just falls into here to pick
595 * up howto and bootdev, cyloffset and esym are no longer used
599 movl %eax,R(_boothowto)
601 movl %eax,R(_bootdev)
606 /**********************************************************************
608 * Identify the CPU and initialize anything special about it
613 /* Try to toggle alignment check flag; does not exist on 386. */
630 /* NexGen CPU does not have aligment check flag. */
639 movl $CPU_386,R(_cpu)
644 movl $CPU_NX586,R(_cpu)
645 movl $0x4778654e,R(_cpu_vendor) # store vendor string
646 movl $0x72446e65,R(_cpu_vendor+4)
647 movl $0x6e657669,R(_cpu_vendor+8)
648 movl $0,R(_cpu_vendor+12)
651 try486: /* Try to toggle identification flag; does not exist on early 486s. */
667 movl $CPU_486,R(_cpu)
671 * Cyrix CPUs do not change the undefined flags following
672 * execution of the divide instruction which divides 5 by 2.
674 * Note: CPUID is enabled on M2, so it passes another way.
684 jmp 3f /* You may use Intel CPU. */
689 * IBM Bluelighting CPU also doesn't change the undefined flags.
690 * Because IBM doesn't disclose the information for Bluelighting
691 * CPU, we couldn't distinguish it from Cyrix's (including IBM
692 * brand of Cyrix CPUs).
694 movl $0x69727943,R(_cpu_vendor) # store vendor string
695 movl $0x736e4978,R(_cpu_vendor+4)
696 movl $0x64616574,R(_cpu_vendor+8)
699 trycpuid: /* Use the `cpuid' instruction. */
701 .byte 0x0f,0xa2 # cpuid 0
702 movl %eax,R(_cpu_high) # highest capability
703 movl %ebx,R(_cpu_vendor) # store vendor string
704 movl %edx,R(_cpu_vendor+4)
705 movl %ecx,R(_cpu_vendor+8)
706 movb $0,R(_cpu_vendor+12)
709 .byte 0x0f,0xa2 # cpuid 1
710 movl %eax,R(_cpu_id) # store cpu_id
711 movl %edx,R(_cpu_feature) # store cpu_feature
712 rorl $8,%eax # extract family type
717 /* less than Pentium; must be 486 */
718 movl $CPU_486,R(_cpu)
724 movl $CPU_586,R(_cpu)
727 /* Greater than Pentium...call it a Pentium Pro */
728 movl $CPU_686,R(_cpu)
733 /**********************************************************************
735 * Create the first page directory and its page tables.
741 testl $CPUID_PGE, R(_cpu_feature)
748 /* Find end of kernel image (rounded up to a page boundary). */
751 /* include symbols if loaded and useful */
753 movl R(_bootinfo+BI_ESYMTAB),%edi
758 addl %edi,R(_bootinfo+BI_SYMTAB)
759 addl %edi,R(_bootinfo+BI_ESYMTAB)
763 /* If we are told where the end of the kernel space is, believe it. */
764 movl R(_bootinfo+BI_KERNEND),%edi
771 andl $~PAGE_MASK,%esi
772 movl %esi,R(_KERNend) /* save end of kernel */
773 movl %esi,R(physfree) /* next free page is at end of kernel */
775 /* Allocate Kernel Page Tables */
777 movl %esi,R(_KPTphys)
779 /* Allocate Page Table Directory */
781 movl %esi,R(_IdlePTD)
783 /* Allocate UPAGES */
787 movl %esi, R(_proc0paddr)
790 ALLOCPAGES(1) /* vm86/bios stack */
791 movl %esi,R(vm86phystk)
793 ALLOCPAGES(3) /* pgtable + ext + IOPAGES */
796 movl %esi, R(_vm86paddr)
800 /* Allocate cpu0's private data page */
804 movl %esi, R(_cpu0prvpage) /* relocated to KVM space */
806 /* Allocate SMP page table page */
810 movl %esi, R(_SMPpt) /* relocated to KVM space */
813 /* Map read-only from zero to the end of the kernel text section */
816 /* If the debugger is present, actually map everything read-write. */
817 cmpl $0,R(_bdb_exists)
823 testl $CPUID_PGE, R(_cpu_feature)
828 2: movl $R(_etext),%ecx
830 shrl $PAGE_SHIFT,%ecx
833 /* Map read-write, data, bss and symbols */
835 addl $PAGE_MASK, %eax
836 andl $~PAGE_MASK, %eax
840 testl $CPUID_PGE, R(_cpu_feature)
845 1: movl R(_KERNend),%ecx
847 shrl $PAGE_SHIFT,%ecx
850 /* Map page directory. */
851 movl R(_IdlePTD), %eax
855 /* Map proc0's UPAGES in the physical way ... */
861 movl $ISA_HOLE_START, %eax
862 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
866 /* Map space for the vm86 region */
867 movl R(vm86phystk), %eax
871 /* Map page 0 into the vm86 page table */
875 fillkpt(R(_vm86pa), $PG_RW|PG_U)
877 /* ...likewise for the ISA hole */
878 movl $ISA_HOLE_START, %eax
879 movl $ISA_HOLE_START>>PAGE_SHIFT, %ebx
880 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
881 fillkpt(R(_vm86pa), $PG_RW|PG_U)
885 /* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */
890 /* Map SMP page table page into global kmem FWIW */
891 movl R(SMPptpa), %eax
895 /* Map the private page into the SMP page table */
897 movl $0, %ebx /* pte offset = 0 */
898 movl $1, %ecx /* one private page coming right up */
899 fillkpt(R(SMPptpa), $PG_RW)
901 /* ... and put the page table table in the pde. */
902 movl R(SMPptpa), %eax
905 fillkpt(R(_IdlePTD), $PG_RW)
907 /* Fakeup VA for the local apic to allow early traps. */
910 movl $(NPTEPG-1), %ebx /* pte offset = NTEPG-1 */
911 movl $1, %ecx /* one private pt coming right up */
912 fillkpt(R(SMPptpa), $PG_RW)
914 /* Initialize mp lock to allow early traps */
918 /* install a pde for temporary double map of bottom of VA */
919 movl R(_KPTphys), %eax
922 fillkpt(R(_IdlePTD), $PG_RW)
924 /* install pde's for pt's */
925 movl R(_KPTphys), %eax
928 fillkpt(R(_IdlePTD), $PG_RW)
930 /* install a pde recursively mapping page directory as a page table */
931 movl R(_IdlePTD), %eax
934 fillkpt(R(_IdlePTD), $PG_RW)
940 cmpl $0,R(_bdb_exists)
941 je bdb_prepare_paging_exit
946 * Copy and convert debugger entries from the bootstrap gdt and idt
947 * to the kernel gdt and idt. Everything is still in low memory.
948 * Tracing continues to work after paging is enabled because the
949 * low memory addresses remain valid until everything is relocated.
950 * However, tracing through the setidt() that initializes the trace
954 movl 2(%esp),%esi /* base address of bootstrap gdt */
956 movl %edi,2(%esp) /* prepare to load kernel gdt */
961 movl $R(_gdt),-8+2(%edi) /* adjust gdt self-ptr */
962 movb $0x92,-8+5(%edi)
966 movl 2(%esp),%esi /* base address of current idt */
967 movl 8+4(%esi),%eax /* convert dbg descriptor to ... */
969 movl %eax,R(bdb_dbg_ljmp+1) /* ... immediate offset ... */
971 movw %ax,R(bdb_dbg_ljmp+5) /* ... and selector for ljmp */
972 movl 24+4(%esi),%eax /* same for bpt descriptor */
974 movl %eax,R(bdb_bpt_ljmp+1)
976 movw %ax,R(bdb_bpt_ljmp+5)
978 movl %edi,2(%esp) /* prepare to load kernel idt */
987 bdb_prepare_paging_exit:
990 /* Relocate debugger gdt entries and gdt and idt pointers. */
993 je bdb_commit_paging_exit
995 movl $_gdt+8*9,%eax /* adjust slots 9-17 */
998 movb $KERNBASE>>24,7(%eax) /* top byte of base addresses, was 0, */
999 addl $8,%eax /* now KERNBASE>>24 */
1004 addl $KERNBASE,2(%esp)
1007 addl $KERNBASE,2(%esp)
1013 bdb_commit_paging_exit:
1016 #endif /* BDE_DEBUGGER */