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 $
38 * $DragonFly: src/sys/platform/pc32/i386/locore.s,v 1.8 2003/10/24 14:10:45 daver Exp $
40 * originally from: locore.s, by William F. Jolitz
42 * Substantially rewritten by David Greenman, Rod Grimes,
43 * Bruce Evans, Wolfgang Solfrank, Poul-Henning Kamp
47 #include "opt_bootp.h"
48 #include "opt_nfsroot.h"
50 #include <sys/syscall.h>
51 #include <sys/reboot.h>
53 #include <machine/asmacros.h>
54 #include <machine/cputypes.h>
55 #include <machine/psl.h>
56 #include <machine/pmap.h>
57 #include <machine/specialreg.h>
64 * Note: This version greatly munged to avoid various assembler errors
65 * that may be fixed in newer versions of gas. Perhaps newer versions
66 * will have more pleasant appearance.
70 * PTmap is recursive pagemap at top of virtual address space.
71 * Within PTmap, the page directory can be found (third indirection).
73 .globl PTmap,PTD,PTDpde
74 .set PTmap,(PTDPTDI << PDRSHIFT)
75 .set PTD,PTmap + (PTDPTDI * PAGE_SIZE)
76 .set PTDpde,PTD + (PTDPTDI * PDESIZE)
79 * APTmap, APTD is the alternate recursive pagemap.
80 * It's used when modifying another process's page tables.
82 .globl APTmap,APTD,APTDpde
83 .set APTmap,APTDPTDI << PDRSHIFT
84 .set APTD,APTmap + (APTDPTDI * PAGE_SIZE)
85 .set APTDpde,PTD + (APTDPTDI * PDESIZE)
88 * Compiled KERNBASE location
91 .set kernbase,KERNBASE
97 ALIGN_DATA /* just to be sure */
99 .globl HIDENAME(tmpstk)
100 .space 0x2000 /* space for tmpstk - temporary stack */
103 .globl boothowto,bootdev
105 .globl cpu,cpu_vendor,cpu_id,bootinfo
106 .globl cpu_high, cpu_feature, cpu_procinfo
108 cpu: .long 0 /* are we 386, 386sx, or 486 */
109 cpu_id: .long 0 /* stepping ID */
110 cpu_high: .long 0 /* highest arg to CPUID */
111 cpu_feature: .long 0 /* features */
112 cpu_procinfo: .long 0 /* brand index / HTT info */
113 cpu_vendor: .space 20 /* CPU origin code */
114 bootinfo: .space BOOTINFO_SIZE /* bootinfo buffer space */
116 KERNend: .long 0 /* phys addr end of kernel (just after bss) */
117 physfree: .long 0 /* phys addr of next free page */
120 cpu0pp: .long 0 /* phys addr cpu0 private pg */
121 cpu0prvpage: .long 0 /* relocated version */
122 cpu0idlestk: .long 0 /* stack for the idle thread */
125 SMPptpa: .long 0 /* phys addr SMP page table */
126 SMPpt: .long 0 /* relocated version */
129 IdlePTD: .long 0 /* phys addr of kernel PTD */
132 KPTphys: .long 0 /* PA of kernel page tables */
135 proc0paddr: .long 0 /* VA of proc 0 address space */
136 p0upa: .long 0 /* PA 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 */
145 .globl bdb_exists /* BDE debugger is present */
150 .globl pc98_system_parameter
151 pc98_system_parameter:
155 /**********************************************************************
161 #define R(foo) ((foo)-KERNBASE)
163 #define ALLOCPAGES(foo) \
164 movl R(physfree), %esi ; \
165 movl $((foo)*PAGE_SIZE), %eax ; \
167 movl %eax, R(physfree) ; \
169 movl $((foo)*PAGE_SIZE),%ecx ; \
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
183 #define fillkpt(base, prot) \
188 1: movl %eax,(%ebx) ; \
189 addl $PAGE_SIZE,%eax ; /* increment physical address */ \
190 addl $4,%ebx ; /* next pte */ \
195 * eax = physical address
196 * ecx = how many pages to map
197 * prot = protection bits
199 #define fillkptphys(prot) \
201 shrl $PAGE_SHIFT, %ebx ; \
202 fillkpt(R(KPTphys), prot)
205 /**********************************************************************
207 * This is where the bootblocks start us, set the ball rolling...
210 NON_GPROF_ENTRY(btext)
213 /* save SYSTEM PARAMETER for resume (NS/T or other) */
215 movl $R(pc98_system_parameter),%edi
222 #ifdef BIOS_STEALS_3K
223 cmpl $0x0375c339,0x95504
225 cmpl $0x0375c339,0x96104 /* XXX - debugger signature */
228 movb $1,R(bdb_exists)
231 /* Tell the bios to warmboot next time */
235 /* Set up a real frame in case the double return in newboot is executed. */
239 /* Don't trust what the BIOS gives for eflags. */
244 * Don't trust what the BIOS gives for %fs and %gs. Trust the bootstrap
245 * to set %cs, %ds, %es and %ss.
252 * Clear the bss. Not all boot programs do it, and it is our job anyway.
254 * XXX we don't check that there is memory for our bss and page tables
257 * Note: we must be careful to not overwrite an active gdt or idt. They
258 * inactive from now until we switch to new ones, since we don't load any
259 * more segment registers or permit interrupts until after the switch.
269 call recover_bootinfo
271 /* Get onto a stack that we can trust. */
273 * XXX this step is delayed in case recover_bootinfo needs to return via
274 * the old stack, but it need not be, since recover_bootinfo actually
275 * returns via the old frame.
277 movl $R(HIDENAME(tmpstk)),%esp
280 /* pc98_machine_type & M_EPSON_PC98 */
281 testb $0x02,R(_pc98_system_parameter)+220
283 /* epson_machine_id <= 0x0b */
284 cmpb $0x0b,R(_pc98_system_parameter)+224
287 /* count up memory */
288 movl $0x100000,%eax /* next, talley remaining memory */
289 movl $0xFFF-0x100,%ecx
290 1: movl 0(%eax),%ebx /* save location to check */
291 movl $0xa55a5aa5,0(%eax) /* write test pattern */
292 cmpl $0xa55a5aa5,0(%eax) /* does not check yet for rollover */
294 movl %ebx,0(%eax) /* restore memory */
297 2: subl $0x100000,%eax
299 movb %al,R(_pc98_system_parameter)+1
302 movw R(_pc98_system_parameter+0x86),%ax
308 call create_pagetables
311 * If the CPU has support for VME, turn it on.
313 testl $CPUID_VME, R(cpu_feature)
322 * Adjust as much as possible for paging before enabling paging so that the
323 * adjustments can be traced.
325 call bdb_prepare_paging
328 /* Now enable paging */
329 movl R(IdlePTD), %eax
330 movl %eax,%cr3 /* load ptd addr into mmu */
331 movl %cr0,%eax /* get control word */
332 orl $CR0_PE|CR0_PG,%eax /* enable paging */
333 movl %eax,%cr0 /* and let's page NOW! */
338 * Complete the adjustments for paging so that we can keep tracing through
339 * initi386() after the low (physical) addresses for the gdt and idt become
342 call bdb_commit_paging
345 pushl $begin /* jump to high virtualized address */
348 /* now running relocated at KERNBASE where the system is linked to run */
352 * set up the bootstrap stack. The pcb sits at the end of the
355 /* set up bootstrap stack */
356 movl proc0paddr,%esp /* location of in-kernel pages */
357 addl $UPAGES*PAGE_SIZE-PCB_SIZE,%esp
358 xorl %eax,%eax /* mark end of frames */
360 /*movl proc0paddr,%eax*/
362 movl %esi,PCB_CR3(%esp)
364 testl $CPUID_PGE, R(cpu_feature)
372 pushl %esi /* value of first for init386(first) */
374 call init386 /* wire 386 chip for unix operation */
377 call mi_startup /* autoconfiguration, mountroot etc */
379 hlt /* never returns to here */
382 * Signal trampoline, copied to top of user stack
384 NON_GPROF_ENTRY(sigcode)
385 call *SIGF_HANDLER(%esp) /* call signal handler */
386 lea SIGF_UC(%esp),%eax /* get ucontext_t */
388 testl $PSL_VM,UC_EFLAGS(%eax)
390 movl UC_GS(%eax),%gs /* restore %gs */
392 movl $SYS_sigreturn,%eax
393 pushl %eax /* junk to fake return addr. */
394 int $0x80 /* enter kernel with args */
403 .long esigcode - sigcode
406 /**********************************************************************
408 * Recover the bootinfo passed to us from the boot program
413 * This code is called in different ways depending on what loaded
414 * and started the kernel. This is used to detect how we get the
415 * arguments from the other code and what we do with them.
417 * Old disk boot blocks:
418 * (*btext)(howto, bootdev, cyloffset, esym);
419 * [return address == 0, and can NOT be returned to]
420 * [cyloffset was not supported by the FreeBSD boot code
421 * and always passed in as 0]
422 * [esym is also known as total in the boot code, and
423 * was never properly supported by the FreeBSD boot code]
425 * Old diskless netboot code:
426 * (*btext)(0,0,0,0,&nfsdiskless,0,0,0);
427 * [return address != 0, and can NOT be returned to]
428 * If we are being booted by this code it will NOT work,
429 * so we are just going to halt if we find this case.
431 * New uniform boot code:
432 * (*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
433 * [return address != 0, and can be returned to]
435 * There may seem to be a lot of wasted arguments in here, but
436 * that is so the newer boot code can still load very old kernels
437 * and old boot code can load new kernels.
441 * The old style disk boot blocks fake a frame on the stack and
442 * did an lret to get here. The frame on the stack has a return
449 * We have some form of return address, so this is either the
450 * old diskless netboot code, or the new uniform code. That can
451 * be detected by looking at the 5th argument, if it is 0
452 * we are being booted by the new uniform boot code.
458 * Seems we have been loaded by the old diskless boot code, we
459 * don't stand a chance of running as the diskless structure
460 * changed considerably between the two, so just halt.
465 * We have been loaded by the new uniform boot code.
466 * Let's check the bootinfo version, and if we do not understand
467 * it we return to the loader with a status of 1 to indicate this error
470 movl 28(%ebp),%ebx /* &bootinfo.version */
471 movl BI_VERSION(%ebx),%eax
472 cmpl $1,%eax /* We only understand version 1 */
474 movl $1,%eax /* Return status */
477 * XXX this returns to our caller's caller (as is required) since
478 * we didn't set up a frame and our caller did.
484 * If we have a kernelname copy it in
486 movl BI_KERNELNAME(%ebx),%esi
488 je 2f /* No kernelname */
489 movl $MAXPATHLEN,%ecx /* Brute force!!! */
490 movl $R(kernelname),%edi
491 cmpb $'/',(%esi) /* Make sure it starts with a slash */
503 * Determine the size of the boot loader's copy of the bootinfo
504 * struct. This is impossible to do properly because old versions
505 * of the struct don't contain a size field and there are 2 old
506 * versions with the same version number.
508 movl $BI_ENDCOMMON,%ecx /* prepare for sizeless version */
509 testl $RB_BOOTINFO,8(%ebp) /* bi_size (and bootinfo) valid? */
510 je got_bi_size /* no, sizeless version */
511 movl BI_SIZE(%ebx),%ecx
515 * Copy the common part of the bootinfo struct
518 movl $R(bootinfo),%edi
519 cmpl $BOOTINFO_SIZE,%ecx
520 jbe got_common_bi_size
521 movl $BOOTINFO_SIZE,%ecx
530 * If we have a nfs_diskless structure copy it in
532 movl BI_NFS_DISKLESS(%ebx),%esi
535 movl $R(nfs_diskless),%edi
536 movl $NFSDISKLESS_SIZE,%ecx
540 movl $R(nfs_diskless_valid),%edi
546 * The old style disk boot.
547 * (*btext)(howto, bootdev, cyloffset, esym);
548 * Note that the newer boot code just falls into here to pick
549 * up howto and bootdev, cyloffset and esym are no longer used
553 movl %eax,R(boothowto)
560 /**********************************************************************
562 * Identify the CPU and initialize anything special about it
567 /* Try to toggle alignment check flag; does not exist on 386. */
584 /* NexGen CPU does not have aligment check flag. */
598 movl $CPU_NX586,R(cpu)
599 movl $0x4778654e,R(cpu_vendor) # store vendor string
600 movl $0x72446e65,R(cpu_vendor+4)
601 movl $0x6e657669,R(cpu_vendor+8)
602 movl $0,R(cpu_vendor+12)
605 try486: /* Try to toggle identification flag; does not exist on early 486s. */
625 * Cyrix CPUs do not change the undefined flags following
626 * execution of the divide instruction which divides 5 by 2.
628 * Note: CPUID is enabled on M2, so it passes another way.
638 jmp 3f /* You may use Intel CPU. */
643 * IBM Bluelighting CPU also doesn't change the undefined flags.
644 * Because IBM doesn't disclose the information for Bluelighting
645 * CPU, we couldn't distinguish it from Cyrix's (including IBM
646 * brand of Cyrix CPUs).
648 movl $0x69727943,R(cpu_vendor) # store vendor string
649 movl $0x736e4978,R(cpu_vendor+4)
650 movl $0x64616574,R(cpu_vendor+8)
653 trycpuid: /* Use the `cpuid' instruction. */
656 movl %eax,R(cpu_high) # highest capability
657 movl %ebx,R(cpu_vendor) # store vendor string
658 movl %edx,R(cpu_vendor+4)
659 movl %ecx,R(cpu_vendor+8)
660 movb $0,R(cpu_vendor+12)
664 movl %eax,R(cpu_id) # store cpu_id
665 movl %ebx,R(cpu_procinfo) # store cpu_procinfo
666 movl %edx,R(cpu_feature) # store cpu_feature
667 rorl $8,%eax # extract family type
672 /* less than Pentium; must be 486 */
682 /* Greater than Pentium...call it a Pentium Pro */
688 /**********************************************************************
690 * Create the first page directory and its page tables.
696 /* Find end of kernel image (rounded up to a page boundary). */
699 /* Include symbols, if any. */
700 movl R(bootinfo+BI_ESYMTAB),%edi
705 addl %edi,R(bootinfo+BI_SYMTAB)
706 addl %edi,R(bootinfo+BI_ESYMTAB)
709 /* If we are told where the end of the kernel space is, believe it. */
710 movl R(bootinfo+BI_KERNEND),%edi
717 andl $~PAGE_MASK,%esi
718 movl %esi,R(KERNend) /* save end of kernel */
719 movl %esi,R(physfree) /* next free page is at end of kernel */
722 /* Allocate Kernel Page Tables */
726 /* Allocate Page Table Directory */
730 /* Allocate UPAGES */
734 movl %esi, R(proc0paddr)
736 ALLOCPAGES(1) /* vm86/bios stack */
737 movl %esi,R(vm86phystk)
739 ALLOCPAGES(3) /* pgtable + ext + IOPAGES */
742 movl %esi, R(vm86paddr)
744 /* Allocate cpu0's private data page */
748 movl %esi, R(cpu0prvpage) /* relocated to KVM space */
750 /* Allocate cpu0's idle stack */
752 movl %esi,R(cpu0idlestk)
754 /* Allocate SMP page table page */
758 movl %esi, R(SMPpt) /* relocated to KVM space */
760 /* Map read-only from zero to the end of the kernel text section */
763 /* If the debugger is present, actually map everything read-write. */
764 cmpl $0,R(bdb_exists)
770 testl $CPUID_PGE, R(cpu_feature)
775 2: movl $R(etext),%ecx
777 shrl $PAGE_SHIFT,%ecx
780 /* Map read-write, data, bss and symbols */
782 addl $PAGE_MASK, %eax
783 andl $~PAGE_MASK, %eax
787 testl $CPUID_PGE, R(cpu_feature)
792 1: movl R(KERNend),%ecx
794 shrl $PAGE_SHIFT,%ecx
797 /* Map page directory. */
798 movl R(IdlePTD), %eax
802 /* Map proc0's UPAGES in the physical way ... */
808 movl $ISA_HOLE_START, %eax
809 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
812 /* Map space for the vm86 region */
813 movl R(vm86phystk), %eax
817 /* Map page 0 into the vm86 page table */
821 fillkpt(R(vm86pa), $PG_RW|PG_U)
823 /* ...likewise for the ISA hole */
824 movl $ISA_HOLE_START, %eax
825 movl $ISA_HOLE_START>>PAGE_SHIFT, %ebx
826 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
827 fillkpt(R(vm86pa), $PG_RW|PG_U)
829 /* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */
834 /* Map SMP page table page into global kmem FWIW */
835 movl R(SMPptpa), %eax
839 /* Map the private page into the SMP page table */
841 movl $0, %ebx /* pte offset = 0 */
842 movl $1, %ecx /* one private page coming right up */
843 fillkpt(R(SMPptpa), $PG_RW)
845 /* Map the cpu0's idle thread stack */
846 movl R(cpu0idlestk), %eax
847 movl $PS_IDLESTACK_PAGE, %ebx
849 fillkpt(R(SMPptpa), $PG_RW)
851 /* ... and put the page table table in the pde. */
852 movl R(SMPptpa), %eax
855 fillkpt(R(IdlePTD), $PG_RW)
857 /* Fakeup VA for the local apic to allow early traps. */
860 movl $(NPTEPG-1), %ebx /* pte offset = NTEPG-1 */
861 movl $1, %ecx /* one private pt coming right up */
862 fillkpt(R(SMPptpa), $PG_RW)
865 /* Initialize mp lock to allow early traps */
869 /* install a pde for temporary double map of bottom of VA */
870 movl R(KPTphys), %eax
873 fillkpt(R(IdlePTD), $PG_RW)
875 /* install pde's for pt's */
876 movl R(KPTphys), %eax
879 fillkpt(R(IdlePTD), $PG_RW)
881 /* install a pde recursively mapping page directory as a page table */
882 movl R(IdlePTD), %eax
885 fillkpt(R(IdlePTD), $PG_RW)
891 cmpl $0,R(bdb_exists)
892 je bdb_prepare_paging_exit
897 * Copy and convert debugger entries from the bootstrap gdt and idt
898 * to the kernel gdt and idt. Everything is still in low memory.
899 * Tracing continues to work after paging is enabled because the
900 * low memory addresses remain valid until everything is relocated.
901 * However, tracing through the setidt() that initializes the trace
905 movl 2(%esp),%esi /* base address of bootstrap gdt */
907 movl %edi,2(%esp) /* prepare to load kernel gdt */
912 movl $R(gdt),-8+2(%edi) /* adjust gdt self-ptr */
913 movb $0x92,-8+5(%edi)
917 movl 2(%esp),%esi /* base address of current idt */
918 movl 8+4(%esi),%eax /* convert dbg descriptor to ... */
920 movl %eax,R(bdb_dbg_ljmp+1) /* ... immediate offset ... */
922 movw %ax,R(bdb_dbg_ljmp+5) /* ... and selector for ljmp */
923 movl 24+4(%esi),%eax /* same for bpt descriptor */
925 movl %eax,R(bdb_bpt_ljmp+1)
927 movw %ax,R(bdb_bpt_ljmp+5)
929 movl %edi,2(%esp) /* prepare to load kernel idt */
938 bdb_prepare_paging_exit:
941 /* Relocate debugger gdt entries and gdt and idt pointers. */
944 je bdb_commit_paging_exit
946 movl $_gdt+8*9,%eax /* adjust slots 9-17 */
949 movb $KERNBASE>>24,7(%eax) /* top byte of base addresses, was 0, */
950 addl $8,%eax /* now KERNBASE>>24 */
955 addl $KERNBASE,2(%esp)
958 addl $KERNBASE,2(%esp)
964 bdb_commit_paging_exit:
967 #endif /* BDE_DEBUGGER */