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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from: @(#)locore.s 7.3 (Berkeley) 5/13/91
35 * originally from: locore.s, by William F. Jolitz
37 * Substantially rewritten by David Greenman, Rod Grimes,
38 * Bruce Evans, Wolfgang Solfrank, Poul-Henning Kamp
42 #include "opt_bootp.h"
43 #include "opt_compat.h"
44 #include "opt_nfsroot.h"
47 #include <sys/syscall.h>
48 #include <sys/reboot.h>
50 #include <machine/asmacros.h>
51 #include <machine/cputypes.h>
52 #include <machine/psl.h>
53 #include <machine/pmap.h>
54 #include <machine/specialreg.h>
61 * Note: This version greatly munged to avoid various assembler errors
62 * that may be fixed in newer versions of gas. Perhaps newer versions
63 * will have more pleasant appearance.
67 * PTmap is recursive pagemap at top of virtual address space.
68 * Within PTmap, the page directory can be found (third indirection).
70 .globl PTmap,PTD,PTDpde
71 .set PTmap,(PTDPTDI << PDRSHIFT)
72 .set PTD,PTmap + (PTDPTDI * PAGE_SIZE)
73 .set PTDpde,PTD + (PTDPTDI * PDESIZE)
76 * Compiled KERNBASE location and the kernel load address
79 .set kernbase,KERNBASE
81 .set kernload,KERNLOAD
87 ALIGN_DATA /* just to be sure */
89 .space 0x2000 /* space for tmpstk - temporary stack */
93 bootinfo: .space BOOTINFO_SIZE /* bootinfo that we can handle */
96 KERNend: .long 0 /* phys addr end of kernel (just after bss) */
97 physfree: .long 0 /* phys addr of next free page */
100 IdlePTD: .long 0 /* phys addr of kernel PTD */
104 IdlePDPT: .long 0 /* phys addr of kernel PDPT */
110 KPTphys: .long 0 /* phys addr of kernel page tables */
113 proc0uarea: .long 0 /* address of proc 0 uarea (unused)*/
114 proc0kstack: .long 0 /* address of proc 0 kstack space */
115 p0upa: .long 0 /* phys addr of proc0 UAREA (unused) */
116 p0kpa: .long 0 /* phys addr of proc0's STACK */
118 vm86phystk: .long 0 /* PA of vm86/bios stack */
120 .globl vm86paddr, vm86pa
121 vm86paddr: .long 0 /* address of vm86 region */
122 vm86pa: .long 0 /* phys addr of vm86 region */
125 .globl pc98_system_parameter
126 pc98_system_parameter:
130 /**********************************************************************
136 #define R(foo) ((foo)-KERNBASE)
138 #define ALLOCPAGES(foo) \
139 movl R(physfree), %esi ; \
140 movl $((foo)*PAGE_SIZE), %eax ; \
142 movl %eax, R(physfree) ; \
144 movl $((foo)*PAGE_SIZE),%ecx ; \
152 * eax = page frame address
153 * ebx = index into page table
154 * ecx = how many pages to map
155 * base = base address of page dir/table
156 * prot = protection bits
158 #define fillkpt(base, prot) \
159 shll $PTESHIFT,%ebx ; \
163 1: movl %eax,(%ebx) ; \
164 addl $PAGE_SIZE,%eax ; /* increment physical address */ \
165 addl $PTESIZE,%ebx ; /* next pte */ \
170 * eax = physical address
171 * ecx = how many pages to map
172 * prot = protection bits
174 #define fillkptphys(prot) \
176 shrl $PAGE_SHIFT, %ebx ; \
177 fillkpt(R(KPTphys), prot)
180 /**********************************************************************
182 * This is where the bootblocks start us, set the ball rolling...
185 NON_GPROF_ENTRY(btext)
188 /* save SYSTEM PARAMETER for resume (NS/T or other) */
190 movl $R(pc98_system_parameter),%edi
196 /* Tell the bios to warmboot next time */
200 /* Set up a real frame in case the double return in newboot is executed. */
204 /* Don't trust what the BIOS gives for eflags. */
209 * Don't trust what the BIOS gives for %fs and %gs. Trust the bootstrap
210 * to set %cs, %ds, %es and %ss.
217 * Clear the bss. Not all boot programs do it, and it is our job anyway.
219 * XXX we don't check that there is memory for our bss and page tables
222 * Note: we must be careful to not overwrite an active gdt or idt. They
223 * inactive from now until we switch to new ones, since we don't load any
224 * more segment registers or permit interrupts until after the switch.
234 call recover_bootinfo
236 /* Get onto a stack that we can trust. */
238 * XXX this step is delayed in case recover_bootinfo needs to return via
239 * the old stack, but it need not be, since recover_bootinfo actually
240 * returns via the old frame.
245 /* pc98_machine_type & M_EPSON_PC98 */
246 testb $0x02,R(pc98_system_parameter)+220
248 /* epson_machine_id <= 0x0b */
249 cmpb $0x0b,R(pc98_system_parameter)+224
252 /* count up memory */
253 movl $0x100000,%eax /* next, talley remaining memory */
254 movl $0xFFF-0x100,%ecx
255 1: movl 0(%eax),%ebx /* save location to check */
256 movl $0xa55a5aa5,0(%eax) /* write test pattern */
257 cmpl $0xa55a5aa5,0(%eax) /* does not check yet for rollover */
259 movl %ebx,0(%eax) /* restore memory */
262 2: subl $0x100000,%eax
264 movb %al,R(pc98_system_parameter)+1
267 movw R(pc98_system_parameter+0x86),%ax
272 call create_pagetables
275 * If the CPU has support for VME, turn it on.
277 testl $CPUID_VME, R(cpu_feature)
284 /* Now enable paging */
286 movl R(IdlePDPT), %eax
292 movl R(IdlePTD), %eax
293 movl %eax,%cr3 /* load ptd addr into mmu */
295 movl %cr0,%eax /* get control word */
296 orl $CR0_PE|CR0_PG,%eax /* enable paging */
297 movl %eax,%cr0 /* and let's page NOW! */
299 pushl $begin /* jump to high virtualized address */
302 /* now running relocated at KERNBASE where the system is linked to run */
304 /* set up bootstrap stack */
305 movl proc0kstack,%eax /* location of in-kernel stack */
306 /* bootstrap stack end location */
307 leal (KSTACK_PAGES*PAGE_SIZE-PCB_SIZE)(%eax),%esp
309 xorl %ebp,%ebp /* mark end of frames */
316 movl %esi,(KSTACK_PAGES*PAGE_SIZE-PCB_SIZE+PCB_CR3)(%eax)
318 pushl physfree /* value of first for init386(first) */
319 call init386 /* wire 386 chip for unix operation */
322 * Clean up the stack in a way that db_numargs() understands, so
323 * that backtraces in ddb don't underrun the stack. Traps for
324 * inaccessible memory are more fatal than usual this early.
328 call mi_startup /* autoconfiguration, mountroot etc */
330 addl $0,%esp /* for db_numargs() again */
333 * Signal trampoline, copied to top of user stack
335 NON_GPROF_ENTRY(sigcode)
336 calll *SIGF_HANDLER(%esp)
337 leal SIGF_UC(%esp),%eax /* get ucontext */
339 testl $PSL_VM,UC_EFLAGS(%eax)
341 mov UC_GS(%eax),%gs /* restore %gs */
343 movl $SYS_sigreturn,%eax
344 pushl %eax /* junk to fake return addr. */
345 int $0x80 /* enter kernel with args */
350 #ifdef COMPAT_FREEBSD4
353 calll *SIGF_HANDLER(%esp)
354 leal SIGF_UC4(%esp),%eax /* get ucontext */
356 testl $PSL_VM,UC4_EFLAGS(%eax)
358 mov UC4_GS(%eax),%gs /* restore %gs */
360 movl $344,%eax /* 4.x SYS_sigreturn */
361 pushl %eax /* junk to fake return addr. */
362 int $0x80 /* enter kernel with args */
371 call *SIGF_HANDLER(%esp) /* call signal handler */
372 lea SIGF_SC(%esp),%eax /* get sigcontext */
374 testl $PSL_VM,SC_PS(%eax)
376 mov SC_GS(%eax),%gs /* restore %gs */
378 movl $103,%eax /* 3.x SYS_sigreturn */
379 pushl %eax /* junk to fake return addr. */
380 int $0x80 /* enter kernel with args */
382 #endif /* COMPAT_43 */
390 .long esigcode-sigcode
391 #ifdef COMPAT_FREEBSD4
392 .globl szfreebsd4_sigcode
394 .long esigcode-freebsd4_sigcode
399 .long esigcode-osigcode
403 /**********************************************************************
405 * Recover the bootinfo passed to us from the boot program
410 * This code is called in different ways depending on what loaded
411 * and started the kernel. This is used to detect how we get the
412 * arguments from the other code and what we do with them.
414 * Old disk boot blocks:
415 * (*btext)(howto, bootdev, cyloffset, esym);
416 * [return address == 0, and can NOT be returned to]
417 * [cyloffset was not supported by the FreeBSD boot code
418 * and always passed in as 0]
419 * [esym is also known as total in the boot code, and
420 * was never properly supported by the FreeBSD boot code]
422 * Old diskless netboot code:
423 * (*btext)(0,0,0,0,&nfsdiskless,0,0,0);
424 * [return address != 0, and can NOT be returned to]
425 * If we are being booted by this code it will NOT work,
426 * so we are just going to halt if we find this case.
428 * New uniform boot code:
429 * (*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
430 * [return address != 0, and can be returned to]
432 * There may seem to be a lot of wasted arguments in here, but
433 * that is so the newer boot code can still load very old kernels
434 * and old boot code can load new kernels.
438 * The old style disk boot blocks fake a frame on the stack and
439 * did an lret to get here. The frame on the stack has a return
446 * We have some form of return address, so this is either the
447 * old diskless netboot code, or the new uniform code. That can
448 * be detected by looking at the 5th argument, if it is 0
449 * we are being booted by the new uniform boot code.
455 * Seems we have been loaded by the old diskless boot code, we
456 * don't stand a chance of running as the diskless structure
457 * changed considerably between the two, so just halt.
462 * We have been loaded by the new uniform boot code.
463 * Let's check the bootinfo version, and if we do not understand
464 * it we return to the loader with a status of 1 to indicate this error
467 movl 28(%ebp),%ebx /* &bootinfo.version */
468 movl BI_VERSION(%ebx),%eax
469 cmpl $1,%eax /* We only understand version 1 */
471 movl $1,%eax /* Return status */
474 * XXX this returns to our caller's caller (as is required) since
475 * we didn't set up a frame and our caller did.
481 * If we have a kernelname copy it in
483 movl BI_KERNELNAME(%ebx),%esi
485 je 2f /* No kernelname */
486 movl $MAXPATHLEN,%ecx /* Brute force!!! */
487 movl $R(kernelname),%edi
488 cmpb $'/',(%esi) /* Make sure it starts with a slash */
500 * Determine the size of the boot loader's copy of the bootinfo
501 * struct. This is impossible to do properly because old versions
502 * of the struct don't contain a size field and there are 2 old
503 * versions with the same version number.
505 movl $BI_ENDCOMMON,%ecx /* prepare for sizeless version */
506 testl $RB_BOOTINFO,8(%ebp) /* bi_size (and bootinfo) valid? */
507 je got_bi_size /* no, sizeless version */
508 movl BI_SIZE(%ebx),%ecx
512 * Copy the common part of the bootinfo struct
515 movl $R(bootinfo),%edi
516 cmpl $BOOTINFO_SIZE,%ecx
517 jbe got_common_bi_size
518 movl $BOOTINFO_SIZE,%ecx
527 * If we have a nfs_diskless structure copy it in
529 movl BI_NFS_DISKLESS(%ebx),%esi
532 movl $R(nfs_diskless),%edi
533 movl $NFSDISKLESS_SIZE,%ecx
537 movl $R(nfs_diskless_valid),%edi
543 * The old style disk boot.
544 * (*btext)(howto, bootdev, cyloffset, esym);
545 * Note that the newer boot code just falls into here to pick
546 * up howto and bootdev, cyloffset and esym are no longer used
550 movl %eax,R(boothowto)
557 /**********************************************************************
559 * Identify the CPU and initialize anything special about it
564 /* Try to toggle alignment check flag; does not exist on 386. */
581 /* NexGen CPU does not have aligment check flag. */
595 movl $CPU_NX586,R(cpu)
596 movl $0x4778654e,R(cpu_vendor) # store vendor string
597 movl $0x72446e65,R(cpu_vendor+4)
598 movl $0x6e657669,R(cpu_vendor+8)
599 movl $0,R(cpu_vendor+12)
602 try486: /* Try to toggle identification flag; does not exist on early 486s. */
622 * Cyrix CPUs do not change the undefined flags following
623 * execution of the divide instruction which divides 5 by 2.
625 * Note: CPUID is enabled on M2, so it passes another way.
635 jmp 3f /* You may use Intel CPU. */
640 * IBM Bluelighting CPU also doesn't change the undefined flags.
641 * Because IBM doesn't disclose the information for Bluelighting
642 * CPU, we couldn't distinguish it from Cyrix's (including IBM
643 * brand of Cyrix CPUs).
645 movl $0x69727943,R(cpu_vendor) # store vendor string
646 movl $0x736e4978,R(cpu_vendor+4)
647 movl $0x64616574,R(cpu_vendor+8)
650 trycpuid: /* Use the `cpuid' instruction. */
653 movl %eax,R(cpu_high) # highest capability
654 movl %ebx,R(cpu_vendor) # store vendor string
655 movl %edx,R(cpu_vendor+4)
656 movl %ecx,R(cpu_vendor+8)
657 movb $0,R(cpu_vendor+12)
661 movl %eax,R(cpu_id) # store cpu_id
662 movl %ebx,R(cpu_procinfo) # store cpu_procinfo
663 movl %edx,R(cpu_feature) # store cpu_feature
664 movl %ecx,R(cpu_feature2) # store cpu_feature2
665 rorl $8,%eax # extract family type
670 /* less than Pentium; must be 486 */
680 /* Greater than Pentium...call it a Pentium Pro */
686 /**********************************************************************
688 * Create the first page directory and its page tables.
694 /* Find end of kernel image (rounded up to a page boundary). */
697 /* Include symbols, if any. */
698 movl R(bootinfo+BI_ESYMTAB),%edi
703 addl %edi,R(bootinfo+BI_SYMTAB)
704 addl %edi,R(bootinfo+BI_ESYMTAB)
707 /* If we are told where the end of the kernel space is, believe it. */
708 movl R(bootinfo+BI_KERNEND),%edi
714 addl $PDRMASK,%esi /* Play conservative for now, and */
715 andl $~PDRMASK,%esi /* ... wrap to next 4M. */
716 movl %esi,R(KERNend) /* save end of kernel */
717 movl %esi,R(physfree) /* next free page is at end of kernel */
719 /* Allocate Kernel Page Tables */
723 /* Allocate Page Table Directory */
725 /* XXX only need 32 bytes (easier for now) */
727 movl %esi,R(IdlePDPT)
732 /* Allocate KSTACK */
733 ALLOCPAGES(KSTACK_PAGES)
736 movl %esi, R(proc0kstack)
738 ALLOCPAGES(1) /* vm86/bios stack */
739 movl %esi,R(vm86phystk)
741 ALLOCPAGES(3) /* pgtable + ext + IOPAGES */
744 movl %esi, R(vm86paddr)
747 * Enable PSE and PGE.
750 testl $CPUID_PSE, R(cpu_feature)
752 movl $PG_PS, R(pseflag)
759 testl $CPUID_PGE, R(cpu_feature)
761 movl $PG_G, R(pgeflag)
769 * Initialize page table pages mapping physical address zero through the
770 * end of the kernel. All of the page table entries allow read and write
771 * access. Write access to the first physical page is required by bios32
772 * calls, and write access to the first 1 MB of physical memory is required
773 * by ACPI for implementing suspend and resume. We do this even
774 * if we've enabled PSE above, we'll just switch the corresponding kernel
775 * PDEs before we turn on paging.
777 * XXX: We waste some pages here in the PSE case! DON'T BLINDLY REMOVE
778 * THIS! SMP needs the page table to be there to map the kernel P==V.
782 shrl $PAGE_SHIFT,%ecx
785 /* Map page directory. */
787 movl R(IdlePDPT), %eax
792 movl R(IdlePTD), %eax
796 /* Map proc0's KSTACK in the physical way ... */
798 movl $(KSTACK_PAGES), %ecx
802 movl $ISA_HOLE_START, %eax
803 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
806 /* Map space for the vm86 region */
807 movl R(vm86phystk), %eax
811 /* Map page 0 into the vm86 page table */
815 fillkpt(R(vm86pa), $PG_RW|PG_U)
817 /* ...likewise for the ISA hole */
818 movl $ISA_HOLE_START, %eax
819 movl $ISA_HOLE_START>>PAGE_SHIFT, %ebx
820 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
821 fillkpt(R(vm86pa), $PG_RW|PG_U)
824 * Create an identity mapping for low physical memory, including the kernel.
825 * The part of this mapping that covers the first 1 MB of physical memory
826 * becomes a permanent part of the kernel's address space. The rest of this
827 * mapping is destroyed in pmap_bootstrap(). Ordinarily, the same page table
828 * pages are shared by the identity mapping and the kernel's native mapping.
829 * However, the permanent identity mapping cannot contain PG_G mappings.
830 * Thus, if the kernel is loaded within the permanent identity mapping, that
831 * page table page must be duplicated and not shared.
833 * N.B. Due to errata concerning large pages and physical address zero,
834 * a PG_PS mapping is not used.
836 movl R(KPTphys), %eax
839 fillkpt(R(IdlePTD), $PG_RW)
840 #if KERNLOAD < (1 << PDRSHIFT)
841 testl $PG_G, R(pgeflag)
845 movl R(IdlePTD), %eax
848 movl $PAGE_SIZE, %ecx
856 * For the non-PSE case, install PDEs for PTs covering the KVA.
857 * For the PSE case, do the same, but clobber the ones corresponding
858 * to the kernel (from btext to KERNend) with 4M (2M for PAE) ('PS')
859 * PDEs immediately after.
861 movl R(KPTphys), %eax
864 fillkpt(R(IdlePTD), $PG_RW)
868 movl R(KERNend), %ecx
872 movl $(KPTDI+(KERNLOAD/(1 << PDRSHIFT))), %ebx
874 addl R(IdlePTD), %ebx
875 orl $(PG_V|PG_RW|PG_PS), %eax
877 addl $(1 << PDRSHIFT), %eax
882 /* install a pde recursively mapping page directory as a page table */
883 movl R(IdlePTD), %eax
886 fillkpt(R(IdlePTD), $PG_RW)
889 movl R(IdlePTD), %eax
892 fillkpt(R(IdlePDPT), $0x0)