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 */
108 KPTphys: .long 0 /* phys addr of kernel page tables */
111 proc0uarea: .long 0 /* address of proc 0 uarea (unused)*/
112 proc0kstack: .long 0 /* address of proc 0 kstack space */
113 p0upa: .long 0 /* phys addr of proc0 UAREA (unused) */
114 p0kpa: .long 0 /* phys addr of proc0's STACK */
116 vm86phystk: .long 0 /* PA of vm86/bios stack */
118 .globl vm86paddr, vm86pa
119 vm86paddr: .long 0 /* address of vm86 region */
120 vm86pa: .long 0 /* phys addr of vm86 region */
123 .globl pc98_system_parameter
124 pc98_system_parameter:
128 /**********************************************************************
134 #define R(foo) ((foo)-KERNBASE)
136 #define ALLOCPAGES(foo) \
137 movl R(physfree), %esi ; \
138 movl $((foo)*PAGE_SIZE), %eax ; \
140 movl %eax, R(physfree) ; \
142 movl $((foo)*PAGE_SIZE),%ecx ; \
150 * eax = page frame address
151 * ebx = index into page table
152 * ecx = how many pages to map
153 * base = base address of page dir/table
154 * prot = protection bits
156 #define fillkpt(base, prot) \
157 shll $PTESHIFT,%ebx ; \
161 1: movl %eax,(%ebx) ; \
162 addl $PAGE_SIZE,%eax ; /* increment physical address */ \
163 addl $PTESIZE,%ebx ; /* next pte */ \
168 * eax = physical address
169 * ecx = how many pages to map
170 * prot = protection bits
172 #define fillkptphys(prot) \
174 shrl $PAGE_SHIFT, %ebx ; \
175 fillkpt(R(KPTphys), prot)
178 /**********************************************************************
180 * This is where the bootblocks start us, set the ball rolling...
183 NON_GPROF_ENTRY(btext)
186 /* save SYSTEM PARAMETER for resume (NS/T or other) */
188 movl $R(pc98_system_parameter),%edi
194 /* Tell the bios to warmboot next time */
198 /* Set up a real frame in case the double return in newboot is executed. */
202 /* Don't trust what the BIOS gives for eflags. */
207 * Don't trust what the BIOS gives for %fs and %gs. Trust the bootstrap
208 * to set %cs, %ds, %es and %ss.
215 * Clear the bss. Not all boot programs do it, and it is our job anyway.
217 * XXX we don't check that there is memory for our bss and page tables
220 * Note: we must be careful to not overwrite an active gdt or idt. They
221 * inactive from now until we switch to new ones, since we don't load any
222 * more segment registers or permit interrupts until after the switch.
232 call recover_bootinfo
234 /* Get onto a stack that we can trust. */
236 * XXX this step is delayed in case recover_bootinfo needs to return via
237 * the old stack, but it need not be, since recover_bootinfo actually
238 * returns via the old frame.
243 /* pc98_machine_type & M_EPSON_PC98 */
244 testb $0x02,R(pc98_system_parameter)+220
246 /* epson_machine_id <= 0x0b */
247 cmpb $0x0b,R(pc98_system_parameter)+224
250 /* count up memory */
251 movl $0x100000,%eax /* next, talley remaining memory */
252 movl $0xFFF-0x100,%ecx
253 1: movl 0(%eax),%ebx /* save location to check */
254 movl $0xa55a5aa5,0(%eax) /* write test pattern */
255 cmpl $0xa55a5aa5,0(%eax) /* does not check yet for rollover */
257 movl %ebx,0(%eax) /* restore memory */
260 2: subl $0x100000,%eax
262 movb %al,R(pc98_system_parameter)+1
265 movw R(pc98_system_parameter+0x86),%ax
270 call create_pagetables
273 * If the CPU has support for VME, turn it on.
275 testl $CPUID_VME, R(cpu_feature)
282 /* Now enable paging */
284 movl R(IdlePDPT), %eax
290 movl R(IdlePTD), %eax
291 movl %eax,%cr3 /* load ptd addr into mmu */
293 movl %cr0,%eax /* get control word */
294 orl $CR0_PE|CR0_PG,%eax /* enable paging */
295 movl %eax,%cr0 /* and let's page NOW! */
297 pushl $begin /* jump to high virtualized address */
300 /* now running relocated at KERNBASE where the system is linked to run */
302 /* set up bootstrap stack */
303 movl proc0kstack,%eax /* location of in-kernel stack */
304 /* bootstrap stack end location */
305 leal (KSTACK_PAGES*PAGE_SIZE-PCB_SIZE)(%eax),%esp
307 xorl %ebp,%ebp /* mark end of frames */
314 movl %esi,(KSTACK_PAGES*PAGE_SIZE-PCB_SIZE+PCB_CR3)(%eax)
316 pushl physfree /* value of first for init386(first) */
317 call init386 /* wire 386 chip for unix operation */
320 * Clean up the stack in a way that db_numargs() understands, so
321 * that backtraces in ddb don't underrun the stack. Traps for
322 * inaccessible memory are more fatal than usual this early.
326 call mi_startup /* autoconfiguration, mountroot etc */
328 addl $0,%esp /* for db_numargs() again */
331 * Signal trampoline, copied to top of user stack
333 NON_GPROF_ENTRY(sigcode)
334 calll *SIGF_HANDLER(%esp)
335 leal SIGF_UC(%esp),%eax /* get ucontext */
337 testl $PSL_VM,UC_EFLAGS(%eax)
339 mov UC_GS(%eax),%gs /* restore %gs */
341 movl $SYS_sigreturn,%eax
342 pushl %eax /* junk to fake return addr. */
343 int $0x80 /* enter kernel with args */
348 #ifdef COMPAT_FREEBSD4
351 calll *SIGF_HANDLER(%esp)
352 leal SIGF_UC4(%esp),%eax /* get ucontext */
354 testl $PSL_VM,UC4_EFLAGS(%eax)
356 mov UC4_GS(%eax),%gs /* restore %gs */
358 movl $344,%eax /* 4.x SYS_sigreturn */
359 pushl %eax /* junk to fake return addr. */
360 int $0x80 /* enter kernel with args */
369 call *SIGF_HANDLER(%esp) /* call signal handler */
370 lea SIGF_SC(%esp),%eax /* get sigcontext */
372 testl $PSL_VM,SC_PS(%eax)
374 mov SC_GS(%eax),%gs /* restore %gs */
376 movl $103,%eax /* 3.x SYS_sigreturn */
377 pushl %eax /* junk to fake return addr. */
378 int $0x80 /* enter kernel with args */
380 #endif /* COMPAT_43 */
388 .long esigcode-sigcode
389 #ifdef COMPAT_FREEBSD4
390 .globl szfreebsd4_sigcode
392 .long esigcode-freebsd4_sigcode
397 .long esigcode-osigcode
401 /**********************************************************************
403 * Recover the bootinfo passed to us from the boot program
408 * This code is called in different ways depending on what loaded
409 * and started the kernel. This is used to detect how we get the
410 * arguments from the other code and what we do with them.
412 * Old disk boot blocks:
413 * (*btext)(howto, bootdev, cyloffset, esym);
414 * [return address == 0, and can NOT be returned to]
415 * [cyloffset was not supported by the FreeBSD boot code
416 * and always passed in as 0]
417 * [esym is also known as total in the boot code, and
418 * was never properly supported by the FreeBSD boot code]
420 * Old diskless netboot code:
421 * (*btext)(0,0,0,0,&nfsdiskless,0,0,0);
422 * [return address != 0, and can NOT be returned to]
423 * If we are being booted by this code it will NOT work,
424 * so we are just going to halt if we find this case.
426 * New uniform boot code:
427 * (*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
428 * [return address != 0, and can be returned to]
430 * There may seem to be a lot of wasted arguments in here, but
431 * that is so the newer boot code can still load very old kernels
432 * and old boot code can load new kernels.
436 * The old style disk boot blocks fake a frame on the stack and
437 * did an lret to get here. The frame on the stack has a return
444 * We have some form of return address, so this is either the
445 * old diskless netboot code, or the new uniform code. That can
446 * be detected by looking at the 5th argument, if it is 0
447 * we are being booted by the new uniform boot code.
453 * Seems we have been loaded by the old diskless boot code, we
454 * don't stand a chance of running as the diskless structure
455 * changed considerably between the two, so just halt.
460 * We have been loaded by the new uniform boot code.
461 * Let's check the bootinfo version, and if we do not understand
462 * it we return to the loader with a status of 1 to indicate this error
465 movl 28(%ebp),%ebx /* &bootinfo.version */
466 movl BI_VERSION(%ebx),%eax
467 cmpl $1,%eax /* We only understand version 1 */
469 movl $1,%eax /* Return status */
472 * XXX this returns to our caller's caller (as is required) since
473 * we didn't set up a frame and our caller did.
479 * If we have a kernelname copy it in
481 movl BI_KERNELNAME(%ebx),%esi
483 je 2f /* No kernelname */
484 movl $MAXPATHLEN,%ecx /* Brute force!!! */
485 movl $R(kernelname),%edi
486 cmpb $'/',(%esi) /* Make sure it starts with a slash */
498 * Determine the size of the boot loader's copy of the bootinfo
499 * struct. This is impossible to do properly because old versions
500 * of the struct don't contain a size field and there are 2 old
501 * versions with the same version number.
503 movl $BI_ENDCOMMON,%ecx /* prepare for sizeless version */
504 testl $RB_BOOTINFO,8(%ebp) /* bi_size (and bootinfo) valid? */
505 je got_bi_size /* no, sizeless version */
506 movl BI_SIZE(%ebx),%ecx
510 * Copy the common part of the bootinfo struct
513 movl $R(bootinfo),%edi
514 cmpl $BOOTINFO_SIZE,%ecx
515 jbe got_common_bi_size
516 movl $BOOTINFO_SIZE,%ecx
525 * If we have a nfs_diskless structure copy it in
527 movl BI_NFS_DISKLESS(%ebx),%esi
530 movl $R(nfs_diskless),%edi
531 movl $NFSDISKLESS_SIZE,%ecx
535 movl $R(nfs_diskless_valid),%edi
541 * The old style disk boot.
542 * (*btext)(howto, bootdev, cyloffset, esym);
543 * Note that the newer boot code just falls into here to pick
544 * up howto and bootdev, cyloffset and esym are no longer used
548 movl %eax,R(boothowto)
555 /**********************************************************************
557 * Identify the CPU and initialize anything special about it
562 /* Try to toggle alignment check flag; does not exist on 386. */
579 /* NexGen CPU does not have aligment check flag. */
593 movl $CPU_NX586,R(cpu)
594 movl $0x4778654e,R(cpu_vendor) # store vendor string
595 movl $0x72446e65,R(cpu_vendor+4)
596 movl $0x6e657669,R(cpu_vendor+8)
597 movl $0,R(cpu_vendor+12)
600 try486: /* Try to toggle identification flag; does not exist on early 486s. */
620 * Cyrix CPUs do not change the undefined flags following
621 * execution of the divide instruction which divides 5 by 2.
623 * Note: CPUID is enabled on M2, so it passes another way.
633 jmp 3f /* You may use Intel CPU. */
638 * IBM Bluelighting CPU also doesn't change the undefined flags.
639 * Because IBM doesn't disclose the information for Bluelighting
640 * CPU, we couldn't distinguish it from Cyrix's (including IBM
641 * brand of Cyrix CPUs).
643 movl $0x69727943,R(cpu_vendor) # store vendor string
644 movl $0x736e4978,R(cpu_vendor+4)
645 movl $0x64616574,R(cpu_vendor+8)
648 trycpuid: /* Use the `cpuid' instruction. */
651 movl %eax,R(cpu_high) # highest capability
652 movl %ebx,R(cpu_vendor) # store vendor string
653 movl %edx,R(cpu_vendor+4)
654 movl %ecx,R(cpu_vendor+8)
655 movb $0,R(cpu_vendor+12)
659 movl %eax,R(cpu_id) # store cpu_id
660 movl %ebx,R(cpu_procinfo) # store cpu_procinfo
661 movl %edx,R(cpu_feature) # store cpu_feature
662 movl %ecx,R(cpu_feature2) # store cpu_feature2
663 rorl $8,%eax # extract family type
668 /* less than Pentium; must be 486 */
678 /* Greater than Pentium...call it a Pentium Pro */
684 /**********************************************************************
686 * Create the first page directory and its page tables.
692 /* Find end of kernel image (rounded up to a page boundary). */
695 /* Include symbols, if any. */
696 movl R(bootinfo+BI_ESYMTAB),%edi
701 addl %edi,R(bootinfo+BI_SYMTAB)
702 addl %edi,R(bootinfo+BI_ESYMTAB)
705 /* If we are told where the end of the kernel space is, believe it. */
706 movl R(bootinfo+BI_KERNEND),%edi
712 addl $PDRMASK,%esi /* Play conservative for now, and */
713 andl $~PDRMASK,%esi /* ... wrap to next 4M. */
714 movl %esi,R(KERNend) /* save end of kernel */
715 movl %esi,R(physfree) /* next free page is at end of kernel */
717 /* Allocate Kernel Page Tables */
721 /* Allocate Page Table Directory */
723 /* XXX only need 32 bytes (easier for now) */
725 movl %esi,R(IdlePDPT)
730 /* Allocate KSTACK */
731 ALLOCPAGES(KSTACK_PAGES)
734 movl %esi, R(proc0kstack)
736 ALLOCPAGES(1) /* vm86/bios stack */
737 movl %esi,R(vm86phystk)
739 ALLOCPAGES(3) /* pgtable + ext + IOPAGES */
742 movl %esi, R(vm86paddr)
745 * Enable PSE and PGE.
748 testl $CPUID_PSE, R(cpu_feature)
750 movl $PG_PS, R(pseflag)
757 testl $CPUID_PGE, R(cpu_feature)
759 movl $PG_G, R(pgeflag)
767 * Initialize page table pages mapping physical address zero through the
768 * end of the kernel. All of the page table entries allow read and write
769 * access. Write access to the first physical page is required by bios32
770 * calls, and write access to the first 1 MB of physical memory is required
771 * by ACPI for implementing suspend and resume. We do this even
772 * if we've enabled PSE above, we'll just switch the corresponding kernel
773 * PDEs before we turn on paging.
775 * XXX: We waste some pages here in the PSE case! DON'T BLINDLY REMOVE
776 * THIS! SMP needs the page table to be there to map the kernel P==V.
780 shrl $PAGE_SHIFT,%ecx
783 /* Map page directory. */
785 movl R(IdlePDPT), %eax
790 movl R(IdlePTD), %eax
794 /* Map proc0's KSTACK in the physical way ... */
796 movl $(KSTACK_PAGES), %ecx
800 movl $ISA_HOLE_START, %eax
801 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
804 /* Map space for the vm86 region */
805 movl R(vm86phystk), %eax
809 /* Map page 0 into the vm86 page table */
813 fillkpt(R(vm86pa), $PG_RW|PG_U)
815 /* ...likewise for the ISA hole */
816 movl $ISA_HOLE_START, %eax
817 movl $ISA_HOLE_START>>PAGE_SHIFT, %ebx
818 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
819 fillkpt(R(vm86pa), $PG_RW|PG_U)
822 * Create an identity mapping for low physical memory, including the kernel.
823 * The part of this mapping that covers the first 1 MB of physical memory
824 * becomes a permanent part of the kernel's address space. The rest of this
825 * mapping is destroyed in pmap_bootstrap(). Ordinarily, the same page table
826 * pages are shared by the identity mapping and the kernel's native mapping.
827 * However, the permanent identity mapping cannot contain PG_G mappings.
828 * Thus, if the kernel is loaded within the permanent identity mapping, that
829 * page table page must be duplicated and not shared.
831 * N.B. Due to errata concerning large pages and physical address zero,
832 * a PG_PS mapping is not used.
834 movl R(KPTphys), %eax
837 fillkpt(R(IdlePTD), $PG_RW)
838 #if KERNLOAD < (1 << PDRSHIFT)
839 testl $PG_G, R(pgeflag)
843 movl R(IdlePTD), %eax
846 movl $PAGE_SIZE, %ecx
854 * For the non-PSE case, install PDEs for PTs covering the KVA.
855 * For the PSE case, do the same, but clobber the ones corresponding
856 * to the kernel (from btext to KERNend) with 4M (2M for PAE) ('PS')
857 * PDEs immediately after.
859 movl R(KPTphys), %eax
862 fillkpt(R(IdlePTD), $PG_RW)
866 movl R(KERNend), %ecx
870 movl $(KPTDI+(KERNLOAD/(1 << PDRSHIFT))), %ebx
872 addl R(IdlePTD), %ebx
873 orl $(PG_V|PG_RW|PG_PS), %eax
875 addl $(1 << PDRSHIFT), %eax
880 /* install a pde recursively mapping page directory as a page table */
881 movl R(IdlePTD), %eax
884 fillkpt(R(IdlePTD), $PG_RW)
887 movl R(IdlePTD), %eax
890 fillkpt(R(IdlePDPT), $0x0)