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 * $FreeBSD: src/sys/i386/i386/locore.s,v 1.132.2.10 2003/02/03 20:54:49 jhb 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
46 #include "opt_bootp.h"
47 #include "opt_nfsroot.h"
49 #include <sys/syscall.h>
50 #include <sys/reboot.h>
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>
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.
69 * PTmap is recursive pagemap at top of virtual address space.
70 * Within PTmap, the page directory can be found (third indirection).
72 .globl PTmap,PTD,PTDpde
73 .set PTmap,(PTDPTDI << PDRSHIFT)
74 .set PTD,PTmap + (PTDPTDI * PAGE_SIZE)
75 .set PTDpde,PTD + (PTDPTDI * PDESIZE)
78 * APTmap, APTD is the alternate recursive pagemap.
79 * It's used when modifying another process's page tables.
81 .globl APTmap,APTD,APTDpde
82 .set APTmap,APTDPTDI << PDRSHIFT
83 .set APTD,APTmap + (APTDPTDI * PAGE_SIZE)
84 .set APTDpde,PTD + (APTDPTDI * PDESIZE)
87 * Compiled KERNBASE location
90 .set kernbase,KERNBASE
96 ALIGN_DATA /* just to be sure */
99 .space 0x2000 /* space for tmpstk - temporary stack */
102 .globl boothowto,bootdev,bootinfo
104 bootinfo: .space BOOTINFO_SIZE /* bootinfo buffer space */
106 KERNend: .long 0 /* phys addr end of kernel (just after bss) */
107 physfree: .long 0 /* phys addr of next free page */
111 cpu0prvpage: .long 0 /* relocated version */
113 cpu0pp: .long 0 /* phys addr cpu0 private pg */
114 cpu0idlestk: .long 0 /* stack for the idle thread */
117 SMPptpa: .long 0 /* phys addr SMP page table */
118 SMPpt: .long 0 /* relocated version */
121 IdlePTD: .long 0 /* phys addr of kernel PTD */
124 KPTphys: .long 0 /* PA of kernel page tables */
127 proc0paddr: .long 0 /* VA of proc 0 address space */
128 p0upa: .long 0 /* PA of proc0's UPAGES */
130 vm86phystk: .long 0 /* PA of vm86/bios stack */
132 .globl vm86paddr, vm86pa
133 vm86paddr: .long 0 /* address of vm86 region */
134 vm86pa: .long 0 /* phys addr of vm86 region */
136 /**********************************************************************
142 #define R(foo) ((foo)-KERNBASE)
144 #define ALLOCPAGES(foo) \
145 movl R(physfree), %esi ; \
146 movl $((foo)*PAGE_SIZE), %eax ; \
148 movl %eax, R(physfree) ; \
150 movl $((foo)*PAGE_SIZE),%ecx ; \
158 * eax = page frame address
159 * ebx = index into page table
160 * ecx = how many pages to map
161 * base = base address of page dir/table
162 * prot = protection bits
164 #define fillkpt(base, prot) \
169 1: movl %eax,(%ebx) ; \
170 addl $PAGE_SIZE,%eax ; /* increment physical address */ \
171 addl $4,%ebx ; /* next pte */ \
176 * eax = physical address
177 * ecx = how many pages to map
178 * prot = protection bits
180 #define fillkptphys(prot) \
182 shrl $PAGE_SHIFT, %ebx ; \
183 fillkpt(R(KPTphys), prot)
186 /**********************************************************************
188 * This is where the bootblocks start us, set the ball rolling...
191 NON_GPROF_ENTRY(btext)
193 /* Tell the bios to warmboot next time */
196 /* Set up a real frame in case the double return in newboot is executed. */
200 /* Don't trust what the BIOS gives for eflags. */
205 * Don't trust what the BIOS gives for %fs and %gs. Trust the bootstrap
206 * to set %cs, %ds, %es and %ss.
213 * Clear the bss. Not all boot programs do it, and it is our job anyway.
215 * XXX we don't check that there is memory for our bss and page tables
218 * Note: we must be careful to not overwrite an active gdt or idt. They
219 * inactive from now until we switch to new ones, since we don't load any
220 * more segment registers or permit interrupts until after the switch.
230 call recover_bootinfo
232 /* Get onto a stack that we can trust. */
234 * XXX this step is delayed in case recover_bootinfo needs to return via
235 * the old stack, but it need not be, since recover_bootinfo actually
236 * returns via the old frame.
238 movl $R(.tmpstk),%esp
242 call create_pagetables
245 * If the CPU has support for VME, turn it on.
247 testl $CPUID_VME, R(cpu_feature)
254 /* Now enable paging */
255 movl R(IdlePTD), %eax
256 movl %eax,%cr3 /* load ptd addr into mmu */
257 movl %cr0,%eax /* get control word */
258 orl $CR0_PE|CR0_PG,%eax /* enable paging */
259 movl %eax,%cr0 /* and let's page NOW! */
260 pushl $begin /* jump to high virtualized address */
263 /* now running relocated at KERNBASE where the system is linked to run */
267 * set up the bootstrap stack. The pcb sits at the end of the
270 /* set up bootstrap stack */
271 movl proc0paddr,%esp /* location of in-kernel pages */
272 addl $UPAGES*PAGE_SIZE-PCB_SIZE,%esp
273 xorl %eax,%eax /* mark end of frames */
275 /*movl proc0paddr,%eax*/
277 movl %esi,PCB_CR3(%esp)
279 testl $CPUID_PGE, R(cpu_feature)
287 pushl %esi /* value of first for init386(first) */
289 call init386 /* wire 386 chip for unix operation */
292 call mi_startup /* autoconfiguration, mountroot etc */
294 hlt /* never returns to here */
297 * Signal trampoline, copied to top of user stack
299 NON_GPROF_ENTRY(sigcode)
300 call *SIGF_HANDLER(%esp) /* call signal handler */
301 lea SIGF_UC(%esp),%eax /* get ucontext_t */
303 testl $PSL_VM,UC_EFLAGS(%eax)
306 movl $SYS_sigreturn,%eax
307 pushl %eax /* junk to fake return addr. */
308 int $0x80 /* enter kernel with args */
317 .long esigcode - sigcode
320 /**********************************************************************
322 * Recover the bootinfo passed to us from the boot program
327 * This code is called in different ways depending on what loaded
328 * and started the kernel. This is used to detect how we get the
329 * arguments from the other code and what we do with them.
331 * Old disk boot blocks:
332 * (*btext)(howto, bootdev, cyloffset, esym);
333 * [return address == 0, and can NOT be returned to]
334 * [cyloffset was not supported by the FreeBSD boot code
335 * and always passed in as 0]
336 * [esym is also known as total in the boot code, and
337 * was never properly supported by the FreeBSD boot code]
339 * Old diskless netboot code:
340 * (*btext)(0,0,0,0,&nfsdiskless,0,0,0);
341 * [return address != 0, and can NOT be returned to]
342 * If we are being booted by this code it will NOT work,
343 * so we are just going to halt if we find this case.
345 * New uniform boot code:
346 * (*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
347 * [return address != 0, and can be returned to]
349 * There may seem to be a lot of wasted arguments in here, but
350 * that is so the newer boot code can still load very old kernels
351 * and old boot code can load new kernels.
355 * The old style disk boot blocks fake a frame on the stack and
356 * did an lret to get here. The frame on the stack has a return
363 * We have some form of return address, so this is either the
364 * old diskless netboot code, or the new uniform code. That can
365 * be detected by looking at the 5th argument, if it is 0
366 * we are being booted by the new uniform boot code.
372 * Seems we have been loaded by the old diskless boot code, we
373 * don't stand a chance of running as the diskless structure
374 * changed considerably between the two, so just halt.
379 * We have been loaded by the new uniform boot code.
380 * Let's check the bootinfo version, and if we do not understand
381 * it we return to the loader with a status of 1 to indicate this error
384 movl 28(%ebp),%ebx /* &bootinfo.version */
385 movl BI_VERSION(%ebx),%eax
386 cmpl $1,%eax /* We only understand version 1 */
388 movl $1,%eax /* Return status */
391 * XXX this returns to our caller's caller (as is required) since
392 * we didn't set up a frame and our caller did.
398 * If we have a kernelname copy it in
400 movl BI_KERNELNAME(%ebx),%esi
402 je 2f /* No kernelname */
403 movl $MAXPATHLEN,%ecx /* Brute force!!! */
404 movl $R(kernelname),%edi
405 cmpb $'/',(%esi) /* Make sure it starts with a slash */
417 * Determine the size of the boot loader's copy of the bootinfo
418 * struct. This is impossible to do properly because old versions
419 * of the struct don't contain a size field and there are 2 old
420 * versions with the same version number.
422 movl $BI_ENDCOMMON,%ecx /* prepare for sizeless version */
423 testl $RB_BOOTINFO,8(%ebp) /* bi_size (and bootinfo) valid? */
424 je got_bi_size /* no, sizeless version */
425 movl BI_SIZE(%ebx),%ecx
429 * Copy the common part of the bootinfo struct
432 movl $R(bootinfo),%edi
433 cmpl $BOOTINFO_SIZE,%ecx
434 jbe got_common_bi_size
435 movl $BOOTINFO_SIZE,%ecx
444 * If we have a nfs_diskless structure copy it in
446 movl BI_NFS_DISKLESS(%ebx),%esi
449 movl $R(nfs_diskless),%edi
450 movl $NFSDISKLESS_SIZE,%ecx
454 movl $R(nfs_diskless_valid),%edi
460 * The old style disk boot.
461 * (*btext)(howto, bootdev, cyloffset, esym);
462 * Note that the newer boot code just falls into here to pick
463 * up howto and bootdev, cyloffset and esym are no longer used
467 movl %eax,R(boothowto)
474 /**********************************************************************
476 * Identify the CPU and initialize anything special about it
481 /* Try to toggle alignment check flag; does not exist on 386. */
498 /* NexGen CPU does not have aligment check flag. */
512 movl $CPU_NX586,R(cpu)
513 movl $0x4778654e,R(cpu_vendor) # store vendor string
514 movl $0x72446e65,R(cpu_vendor+4)
515 movl $0x6e657669,R(cpu_vendor+8)
516 movl $0,R(cpu_vendor+12)
519 try486: /* Try to toggle identification flag; does not exist on early 486s. */
539 * Cyrix CPUs do not change the undefined flags following
540 * execution of the divide instruction which divides 5 by 2.
542 * Note: CPUID is enabled on M2, so it passes another way.
552 jmp 3f /* You may use Intel CPU. */
557 * IBM Bluelighting CPU also doesn't change the undefined flags.
558 * Because IBM doesn't disclose the information for Bluelighting
559 * CPU, we couldn't distinguish it from Cyrix's (including IBM
560 * brand of Cyrix CPUs).
562 movl $0x69727943,R(cpu_vendor) # store vendor string
563 movl $0x736e4978,R(cpu_vendor+4)
564 movl $0x64616574,R(cpu_vendor+8)
567 trycpuid: /* Use the `cpuid' instruction. */
570 movl %eax,R(cpu_high) # highest capability
571 movl %ebx,R(cpu_vendor) # store vendor string
572 movl %edx,R(cpu_vendor+4)
573 movl %ecx,R(cpu_vendor+8)
574 movb $0,R(cpu_vendor+12)
578 movl %eax,R(cpu_id) # store cpu_id
579 movl %ebx,R(cpu_procinfo) # store cpu_procinfo
580 movl %edx,R(cpu_feature) # store cpu_feature
581 movl %ecx,R(cpu_feature2) # store cpu_feature2
582 rorl $8,%eax # extract family type
587 /* less than Pentium; must be 486 */
597 /* Greater than Pentium...call it a Pentium Pro */
603 /**********************************************************************
605 * Create the first page directory and its page tables.
611 /* Find end of kernel image (rounded up to a page boundary). */
614 /* Include symbols, if any. */
615 movl R(bootinfo+BI_ESYMTAB),%edi
620 addl %edi,R(bootinfo+BI_SYMTAB)
621 addl %edi,R(bootinfo+BI_ESYMTAB)
624 /* If we are told where the end of the kernel space is, believe it. */
625 movl R(bootinfo+BI_KERNEND),%edi
632 andl $~PAGE_MASK,%esi
633 movl %esi,R(KERNend) /* save end of kernel */
634 movl %esi,R(physfree) /* next free page is at end of kernel */
637 /* Allocate Kernel Page Tables */
641 /* Allocate Page Table Directory */
645 /* Allocate UPAGES */
649 movl %esi, R(proc0paddr)
651 ALLOCPAGES(1) /* vm86/bios stack */
652 movl %esi,R(vm86phystk)
654 ALLOCPAGES(3) /* pgtable + ext + IOPAGES */
657 movl %esi, R(vm86paddr)
659 /* Allocate cpu0's mdglobaldata */
660 ALLOCPAGES(MDGLOBALDATA_BASEALLOC_PAGES)
664 movl %esi, R(cpu0prvpage) /* relocated to KVM space */
667 /* Allocate cpu0's idle stack */
669 movl %esi,R(cpu0idlestk)
671 /* Allocate SMP page table page */
675 movl %esi, R(SMPpt) /* relocated to KVM space */
677 /* Map read-only from zero to the end of the kernel text section */
681 2: movl $R(etext),%ecx
683 shrl $PAGE_SHIFT,%ecx
686 /* Map read-write, data, bss and symbols */
688 addl $PAGE_MASK, %eax
689 andl $~PAGE_MASK, %eax
693 1: movl R(KERNend),%ecx
695 shrl $PAGE_SHIFT,%ecx
698 /* Map page directory. */
699 movl R(IdlePTD), %eax
703 /* Map proc0's UPAGES in the physical way ... */
709 movl $ISA_HOLE_START, %eax
710 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
713 /* Map space for the vm86 region */
714 movl R(vm86phystk), %eax
718 /* Map page 0 into the vm86 page table */
722 fillkpt(R(vm86pa), $PG_RW|PG_U)
724 /* ...likewise for the ISA hole */
725 movl $ISA_HOLE_START, %eax
726 movl $ISA_HOLE_START>>PAGE_SHIFT, %ebx
727 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
728 fillkpt(R(vm86pa), $PG_RW|PG_U)
731 /* Map cpu0's mdglobaldata into global kmem (N pages @ cpu0pp) */
733 movl $MDGLOBALDATA_BASEALLOC_PAGES, %ecx
737 /* Map SMP page table page into global kmem FWIW */
738 movl R(SMPptpa), %eax
742 /* Map the private page into the SMP page table */
744 movl $0, %ebx /* pte offset = 0 */
745 /* N private pages coming right up */
746 movl $MDGLOBALDATA_BASEALLOC_PAGES, %ecx
747 fillkpt(R(SMPptpa), $PG_RW)
749 /* Map the cpu0's idle thread stack */
750 movl R(cpu0idlestk), %eax
751 movl $PS_IDLESTACK_PAGE, %ebx
753 fillkpt(R(SMPptpa), $PG_RW)
755 /* ... and put the page table table in the pde. */
756 movl R(SMPptpa), %eax
759 fillkpt(R(IdlePTD), $PG_RW)
761 /* Fakeup VA for the local apic to allow early traps. */
764 movl $(NPTEPG-1), %ebx /* pte offset = NTEPG-1 */
765 movl $1, %ecx /* one private pt coming right up */
766 fillkpt(R(SMPptpa), $PG_RW)
768 /* install a pde for temporary double map of bottom of VA */
769 movl R(KPTphys), %eax
772 fillkpt(R(IdlePTD), $PG_RW)
774 /* install pde's for pt's */
775 movl R(KPTphys), %eax
778 fillkpt(R(IdlePTD), $PG_RW)
780 /* install a pde recursively mapping page directory as a page table */
781 movl R(IdlePTD), %eax
784 fillkpt(R(IdlePTD), $PG_RW)