locore.s revision 52140
160484Sobrien/*- 260484Sobrien * Copyright (c) 1990 The Regents of the University of California. 360484Sobrien * All rights reserved. 460484Sobrien * 560484Sobrien * This code is derived from software contributed to Berkeley by 660484Sobrien * William Jolitz. 760484Sobrien * 860484Sobrien * Redistribution and use in source and binary forms, with or without 960484Sobrien * modification, are permitted provided that the following conditions 1060484Sobrien * are met: 1160484Sobrien * 1. Redistributions of source code must retain the above copyright 1260484Sobrien * notice, this list of conditions and the following disclaimer. 1360484Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1460484Sobrien * notice, this list of conditions and the following disclaimer in the 1560484Sobrien * documentation and/or other materials provided with the distribution. 1660484Sobrien * 3. All advertising materials mentioning features or use of this software 1760484Sobrien * must display the following acknowledgement: 1860484Sobrien * This product includes software developed by the University of 1960484Sobrien * California, Berkeley and its contributors. 2060484Sobrien * 4. Neither the name of the University nor the names of its contributors 2160484Sobrien * may be used to endorse or promote products derived from this software 2260484Sobrien * without specific prior written permission. 2360484Sobrien * 2460484Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2560484Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2660484Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2760484Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2860484Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2960484Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3060484Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3160484Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3260484Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3360484Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3460484Sobrien * SUCH DAMAGE. 3560484Sobrien * 3660484Sobrien * from: @(#)locore.s 7.3 (Berkeley) 5/13/91 3760484Sobrien * $FreeBSD: head/sys/i386/i386/locore.s 52140 1999-10-11 20:33:17Z luoqi $ 3860484Sobrien * 3960484Sobrien * originally from: locore.s, by William F. Jolitz 4060484Sobrien * 4160484Sobrien * Substantially rewritten by David Greenman, Rod Grimes, 4260484Sobrien * Bruce Evans, Wolfgang Solfrank, Poul-Henning Kamp 4360484Sobrien * and many others. 4460484Sobrien */ 4560484Sobrien 4660484Sobrien#include "opt_bootp.h" 4760484Sobrien#include "opt_ddb.h" 4860484Sobrien#include "opt_nfsroot.h" 4960484Sobrien#include "opt_userconfig.h" 5060484Sobrien 5160484Sobrien#include <sys/syscall.h> 5260484Sobrien#include <sys/reboot.h> 5360484Sobrien 5460484Sobrien#include <machine/asmacros.h> 5560484Sobrien#include <machine/cputypes.h> 5660484Sobrien#include <machine/psl.h> 5760484Sobrien#include <machine/pmap.h> 5860484Sobrien#include <machine/specialreg.h> 5960484Sobrien 6060484Sobrien#include "assym.s" 6160484Sobrien 6260484Sobrien/* 6360484Sobrien * XXX 6460484Sobrien * 6560484Sobrien * Note: This version greatly munged to avoid various assembler errors 6660484Sobrien * that may be fixed in newer versions of gas. Perhaps newer versions 6761843Sobrien * will have more pleasant appearance. 6861843Sobrien */ 6961843Sobrien 7061843Sobrien/* 7161843Sobrien * PTmap is recursive pagemap at top of virtual address space. 7260484Sobrien * Within PTmap, the page directory can be found (third indirection). 7360484Sobrien */ 7460484Sobrien .globl _PTmap,_PTD,_PTDpde 7560484Sobrien .set _PTmap,(PTDPTDI << PDRSHIFT) 7660484Sobrien .set _PTD,_PTmap + (PTDPTDI * PAGE_SIZE) 7760484Sobrien .set _PTDpde,_PTD + (PTDPTDI * PDESIZE) 7860484Sobrien 7960484Sobrien/* 8060484Sobrien * APTmap, APTD is the alternate recursive pagemap. 8160484Sobrien * It's used when modifying another process's page tables. 8260484Sobrien */ 8360484Sobrien .globl _APTmap,_APTD,_APTDpde 8460484Sobrien .set _APTmap,APTDPTDI << PDRSHIFT 8560484Sobrien .set _APTD,_APTmap + (APTDPTDI * PAGE_SIZE) 8660484Sobrien .set _APTDpde,_PTD + (APTDPTDI * PDESIZE) 8760484Sobrien 8860484Sobrien/* 8960484Sobrien * Globals 9060484Sobrien */ 9160484Sobrien .data 9260484Sobrien ALIGN_DATA /* just to be sure */ 9360484Sobrien 9460484Sobrien .globl HIDENAME(tmpstk) 9560484Sobrien .space 0x2000 /* space for tmpstk - temporary stack */ 9660484SobrienHIDENAME(tmpstk): 9760484Sobrien 9860484Sobrien .globl _boothowto,_bootdev 9960484Sobrien 10060484Sobrien .globl _cpu,_cpu_vendor,_cpu_id,_bootinfo 10160484Sobrien .globl _cpu_high, _cpu_feature 10260484Sobrien 10360484Sobrien_cpu: .long 0 /* are we 386, 386sx, or 486 */ 10460484Sobrien_cpu_id: .long 0 /* stepping ID */ 10560484Sobrien_cpu_high: .long 0 /* highest arg to CPUID */ 10660484Sobrien_cpu_feature: .long 0 /* features */ 10760484Sobrien_cpu_vendor: .space 20 /* CPU origin code */ 10860484Sobrien_bootinfo: .space BOOTINFO_SIZE /* bootinfo that we can handle */ 10960484Sobrien 11060484Sobrien_KERNend: .long 0 /* phys addr end of kernel (just after bss) */ 11160484Sobrienphysfree: .long 0 /* phys addr of next free page */ 11260484Sobrien 11360484Sobrien#ifdef SMP 11460484Sobrien .globl _cpu0prvpage 11560484Sobriencpu0pp: .long 0 /* phys addr cpu0 private pg */ 11660484Sobrien_cpu0prvpage: .long 0 /* relocated version */ 11760484Sobrien 11860484Sobrien .globl _SMPpt 11960484SobrienSMPptpa: .long 0 /* phys addr SMP page table */ 12060484Sobrien_SMPpt: .long 0 /* relocated version */ 12160484Sobrien#endif /* SMP */ 12260484Sobrien 12360484Sobrien .globl _IdlePTD 12460484Sobrien_IdlePTD: .long 0 /* phys addr of kernel PTD */ 12560484Sobrien 12660484Sobrien#ifdef SMP 12760484Sobrien .globl _KPTphys 12860484Sobrien#endif 12960484Sobrien_KPTphys: .long 0 /* phys addr of kernel page tables */ 13060484Sobrien 13160484Sobrien .globl _proc0paddr 13260484Sobrien_proc0paddr: .long 0 /* address of proc 0 address space */ 13360484Sobrienp0upa: .long 0 /* phys addr of proc0's UPAGES */ 13460484Sobrien 13560484Sobrienvm86phystk: .long 0 /* PA of vm86/bios stack */ 13660484Sobrien 13760484Sobrien .globl _vm86paddr, _vm86pa 13860484Sobrien_vm86paddr: .long 0 /* address of vm86 region */ 13960484Sobrien_vm86pa: .long 0 /* phys addr of vm86 region */ 14060484Sobrien 14160484Sobrien#ifdef BDE_DEBUGGER 14260484Sobrien .globl _bdb_exists /* flag to indicate BDE debugger is present */ 14360484Sobrien_bdb_exists: .long 0 14460484Sobrien#endif 14560484Sobrien 14660484Sobrien#ifdef PC98 14760484Sobrien .globl _pc98_system_parameter 14860484Sobrien_pc98_system_parameter: 14960484Sobrien .space 0x240 15060484Sobrien#endif 15160484Sobrien 15260484Sobrien/********************************************************************** 15360484Sobrien * 15460484Sobrien * Some handy macros 15560484Sobrien * 15660484Sobrien */ 15760484Sobrien 15860484Sobrien#define R(foo) ((foo)-KERNBASE) 15960484Sobrien 16060484Sobrien#define ALLOCPAGES(foo) \ 16160484Sobrien movl R(physfree), %esi ; \ 16260484Sobrien movl $((foo)*PAGE_SIZE), %eax ; \ 16360484Sobrien addl %esi, %eax ; \ 16460484Sobrien movl %eax, R(physfree) ; \ 16560484Sobrien movl %esi, %edi ; \ 16660484Sobrien movl $((foo)*PAGE_SIZE),%ecx ; \ 16760484Sobrien xorl %eax,%eax ; \ 16860484Sobrien cld ; \ 16960484Sobrien rep ; \ 17060484Sobrien stosb 17160484Sobrien 17260484Sobrien/* 17360484Sobrien * fillkpt 17460484Sobrien * eax = page frame address 17560484Sobrien * ebx = index into page table 17660484Sobrien * ecx = how many pages to map 17760484Sobrien * base = base address of page dir/table 17860484Sobrien * prot = protection bits 17960484Sobrien */ 18060484Sobrien#define fillkpt(base, prot) \ 18160484Sobrien shll $2,%ebx ; \ 18260484Sobrien addl base,%ebx ; \ 18360484Sobrien orl $PG_V,%eax ; \ 18460484Sobrien orl prot,%eax ; \ 18560484Sobrien1: movl %eax,(%ebx) ; \ 18660484Sobrien addl $PAGE_SIZE,%eax ; /* increment physical address */ \ 18760484Sobrien addl $4,%ebx ; /* next pte */ \ 18860484Sobrien loop 1b 18960484Sobrien 19060484Sobrien/* 19160484Sobrien * fillkptphys(prot) 19260484Sobrien * eax = physical address 19360484Sobrien * ecx = how many pages to map 19460484Sobrien * prot = protection bits 19560484Sobrien */ 19660484Sobrien#define fillkptphys(prot) \ 19760484Sobrien movl %eax, %ebx ; \ 19860484Sobrien shrl $PAGE_SHIFT, %ebx ; \ 19960484Sobrien fillkpt(R(_KPTphys), prot) 20060484Sobrien 20160484Sobrien .text 20260484Sobrien/********************************************************************** 20360484Sobrien * 20460484Sobrien * This is where the bootblocks start us, set the ball rolling... 20560484Sobrien * 20660484Sobrien */ 20760484SobrienNON_GPROF_ENTRY(btext) 20860484Sobrien 20960484Sobrien#ifdef PC98 21060484Sobrien /* save SYSTEM PARAMETER for resume (NS/T or other) */ 21160484Sobrien movl $0xa1400,%esi 21260484Sobrien movl $R(_pc98_system_parameter),%edi 21360484Sobrien movl $0x0240,%ecx 21460484Sobrien cld 21560484Sobrien rep 21660484Sobrien movsb 21760484Sobrien#else /* IBM-PC */ 21860484Sobrien#ifdef BDE_DEBUGGER 21960484Sobrien#ifdef BIOS_STEALS_3K 22060484Sobrien cmpl $0x0375c339,0x95504 22160484Sobrien#else 22260484Sobrien cmpl $0x0375c339,0x96104 /* XXX - debugger signature */ 22360484Sobrien#endif 22460484Sobrien jne 1f 22560484Sobrien movb $1,R(_bdb_exists) 22660484Sobrien1: 22760484Sobrien#endif 22860484Sobrien#endif /* PC98 */ 22960484Sobrien 23060484Sobrien/* Tell the bios to warmboot next time */ 23160484Sobrien movw $0x1234,0x472 23260484Sobrien 23360484Sobrien/* Set up a real frame in case the double return in newboot is executed. */ 23460484Sobrien pushl %ebp 23560484Sobrien movl %esp, %ebp 23660484Sobrien 23760484Sobrien/* Don't trust what the BIOS gives for eflags. */ 23860484Sobrien pushl $PSL_KERNEL 23960484Sobrien popfl 24060484Sobrien 24160484Sobrien/* 24260484Sobrien * Don't trust what the BIOS gives for %fs and %gs. Trust the bootstrap 24360484Sobrien * to set %cs, %ds, %es and %ss. 24460484Sobrien */ 24560484Sobrien mov %ds, %ax 24660484Sobrien mov %ax, %fs 24760484Sobrien mov %ax, %gs 24860484Sobrien 24960484Sobrien call recover_bootinfo 25060484Sobrien 25160484Sobrien/* Get onto a stack that we can trust. */ 25260484Sobrien/* 25360484Sobrien * XXX this step is delayed in case recover_bootinfo needs to return via 25460484Sobrien * the old stack, but it need not be, since recover_bootinfo actually 25560484Sobrien * returns via the old frame. 25660484Sobrien */ 25760484Sobrien movl $R(HIDENAME(tmpstk)),%esp 25860484Sobrien 25960484Sobrien#ifdef PC98 26060484Sobrien /* pc98_machine_type & M_EPSON_PC98 */ 26160484Sobrien testb $0x02,R(_pc98_system_parameter)+220 26260484Sobrien jz 3f 26360484Sobrien /* epson_machine_id <= 0x0b */ 26460484Sobrien cmpb $0x0b,R(_pc98_system_parameter)+224 26560484Sobrien ja 3f 26660484Sobrien 26760484Sobrien /* count up memory */ 26860484Sobrien movl $0x100000,%eax /* next, talley remaining memory */ 26960484Sobrien movl $0xFFF-0x100,%ecx 27060484Sobrien1: movl 0(%eax),%ebx /* save location to check */ 27160484Sobrien movl $0xa55a5aa5,0(%eax) /* write test pattern */ 27260484Sobrien cmpl $0xa55a5aa5,0(%eax) /* does not check yet for rollover */ 27360484Sobrien jne 2f 27460484Sobrien movl %ebx,0(%eax) /* restore memory */ 27560484Sobrien addl $PAGE_SIZE,%eax 27660484Sobrien loop 1b 27760484Sobrien2: subl $0x100000,%eax 27860484Sobrien shrl $17,%eax 27960484Sobrien movb %al,R(_pc98_system_parameter)+1 28060484Sobrien3: 28160484Sobrien#endif 28260484Sobrien 28360484Sobrien call identify_cpu 28460484Sobrien 28560484Sobrien/* clear bss */ 28660484Sobrien/* 28760484Sobrien * XXX this should be done a little earlier. 28860484Sobrien * 28960484Sobrien * XXX we don't check that there is memory for our bss and page tables 29060484Sobrien * before using it. 29160484Sobrien * 29260484Sobrien * XXX the boot program somewhat bogusly clears the bss. We still have 29360484Sobrien * to do it in case we were unzipped by kzipboot. Then the boot program 29460484Sobrien * only clears kzipboot's bss. 29560484Sobrien * 29660484Sobrien * XXX the gdt and idt are still somewhere in the boot program. We 29760484Sobrien * depend on the convention that the boot program is below 1MB and we 29860484Sobrien * are above 1MB to keep the gdt and idt away from the bss and page 29960484Sobrien * tables. The idt is only used if BDE_DEBUGGER is enabled. 30060484Sobrien */ 30160484Sobrien movl $R(_end),%ecx 30260484Sobrien movl $R(_edata),%edi 30360484Sobrien subl %edi,%ecx 30460484Sobrien xorl %eax,%eax 30560484Sobrien cld 30660484Sobrien rep 30760484Sobrien stosb 30860484Sobrien 30960484Sobrien call create_pagetables 31060484Sobrien 31160484Sobrien/* 31260484Sobrien * If the CPU has support for VME, turn it on. 31360484Sobrien */ 31460484Sobrien testl $CPUID_VME, R(_cpu_feature) 31560484Sobrien jz 1f 31660484Sobrien movl %cr4, %eax 31760484Sobrien orl $CR4_VME, %eax 31860484Sobrien movl %eax, %cr4 31960484Sobrien1: 32060484Sobrien 32160484Sobrien#ifdef BDE_DEBUGGER 32260484Sobrien/* 32360484Sobrien * Adjust as much as possible for paging before enabling paging so that the 32460484Sobrien * adjustments can be traced. 32560484Sobrien */ 32660484Sobrien call bdb_prepare_paging 32760484Sobrien#endif 32860484Sobrien 32960484Sobrien/* Now enable paging */ 33060484Sobrien movl R(_IdlePTD), %eax 33160484Sobrien movl %eax,%cr3 /* load ptd addr into mmu */ 33260484Sobrien movl %cr0,%eax /* get control word */ 33360484Sobrien orl $CR0_PE|CR0_PG,%eax /* enable paging */ 33460484Sobrien movl %eax,%cr0 /* and let's page NOW! */ 33560484Sobrien 33660484Sobrien#ifdef BDE_DEBUGGER 33760484Sobrien/* 33860484Sobrien * Complete the adjustments for paging so that we can keep tracing through 33960484Sobrien * initi386() after the low (physical) addresses for the gdt and idt become 34060484Sobrien * invalid. 34160484Sobrien */ 34260484Sobrien call bdb_commit_paging 34360484Sobrien#endif 34460484Sobrien 34560484Sobrien pushl $begin /* jump to high virtualized address */ 34660484Sobrien ret 34760484Sobrien 34860484Sobrien/* now running relocated at KERNBASE where the system is linked to run */ 34960484Sobrienbegin: 35060484Sobrien /* set up bootstrap stack */ 35160484Sobrien movl _proc0paddr,%esp /* location of in-kernel pages */ 35260484Sobrien addl $UPAGES*PAGE_SIZE,%esp /* bootstrap stack end location */ 35360484Sobrien xorl %eax,%eax /* mark end of frames */ 35460484Sobrien movl %eax,%ebp 35560484Sobrien movl _proc0paddr,%eax 35660484Sobrien movl _IdlePTD, %esi 35760484Sobrien movl %esi,PCB_CR3(%eax) 35860484Sobrien 35960484Sobrien movl physfree, %esi 36060484Sobrien pushl %esi /* value of first for init386(first) */ 36160484Sobrien call _init386 /* wire 386 chip for unix operation */ 36260484Sobrien popl %esi 36360484Sobrien 36460484Sobrien .globl __ucodesel,__udatasel 36560484Sobrien 36660484Sobrien pushl $0 /* unused */ 36760484Sobrien pushl __udatasel /* ss */ 36860484Sobrien pushl $0 /* esp - filled in by execve() */ 36960484Sobrien pushl $PSL_USER /* eflags (IOPL 0, int enab) */ 37060484Sobrien pushl __ucodesel /* cs */ 37160484Sobrien pushl $0 /* eip - filled in by execve() */ 37260484Sobrien subl $(13*4),%esp /* space for rest of registers */ 37360484Sobrien 37460484Sobrien pushl %esp /* call main with frame pointer */ 37560484Sobrien call _mi_startup /* autoconfiguration, mountroot etc */ 37660484Sobrien 37760484Sobrien hlt /* never returns to here */ 37860484Sobrien 37960484Sobrien/* 38060484Sobrien * When starting init, call this to configure the process for user 38160484Sobrien * mode. This will be inherited by other processes. 38260484Sobrien */ 38360484SobrienNON_GPROF_ENTRY(prepare_usermode) 38460484Sobrien /* 38560484Sobrien * Now we've run main() and determined what cpu-type we are, we can 38660484Sobrien * enable write protection and alignment checking on i486 cpus and 38760484Sobrien * above. 38860484Sobrien */ 38960484Sobrien#if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU) 39060484Sobrien cmpl $CPUCLASS_386,_cpu_class 39160484Sobrien je 1f 39260484Sobrien movl %cr0,%eax /* get control word */ 39360484Sobrien orl $CR0_WP|CR0_AM,%eax /* enable i486 features */ 39460484Sobrien movl %eax,%cr0 /* and do it */ 39560484Sobrien1: 39660484Sobrien#endif 39760484Sobrien /* 39860484Sobrien * on return from main(), we are process 1 39960484Sobrien * set up address space and stack so that we can 'return' to user mode 40060484Sobrien */ 40160484Sobrien movl __ucodesel,%eax 40260484Sobrien movl __udatasel,%ecx 40360484Sobrien 40460484Sobrien#if 0 /* ds/es/fs are in trap frame */ 40560484Sobrien movl %cx,%ds 40660484Sobrien movl %cx,%es 40760484Sobrien movl %cx,%fs 40860484Sobrien#endif 40960484Sobrien movl %cx,%gs /* and ds to gs */ 41060484Sobrien ret /* goto user! */ 41160484Sobrien 41260484Sobrien 41360484Sobrien/* 41460484Sobrien * Signal trampoline, copied to top of user stack 41560484Sobrien */ 41660484SobrienNON_GPROF_ENTRY(sigcode) 41760484Sobrien call SIGF_HANDLER(%esp) /* call signal handler */ 41860484Sobrien lea SIGF_UC(%esp),%eax /* get ucontext_t */ 41960484Sobrien pushl %eax 42060484Sobrien testl $PSL_VM,UC_EFLAGS(%eax) 42160484Sobrien jne 9f 42260484Sobrien movl UC_GS(%eax),%gs /* restore %gs */ 42360484Sobrien9: 42460484Sobrien movl $SYS_sigreturn,%eax 42560484Sobrien pushl %eax /* junk to fake return addr. */ 42660484Sobrien int $0x80 /* enter kernel with args */ 42760484Sobrien0: jmp 0b 42860484Sobrien 42960484Sobrien ALIGN_TEXT 43060484Sobrien_osigcode: 43160484Sobrien call SIGF_HANDLER(%esp) /* call signal handler */ 43260484Sobrien lea SIGF_SC(%esp),%eax /* get sigcontext */ 43360484Sobrien pushl %eax 43460484Sobrien testl $PSL_VM,SC_PS(%eax) 43560484Sobrien jne 9f 43660484Sobrien movl SC_GS(%eax),%gs /* restore %gs */ 43760484Sobrien9: 43860484Sobrien movl $0x01d516,SC_TRAPNO(%eax) /* magic: 0ldSiG */ 43960484Sobrien movl $SYS_sigreturn,%eax 44060484Sobrien pushl %eax /* junk to fake return addr. */ 44160484Sobrien int $0x80 /* enter kernel with args */ 44260484Sobrien0: jmp 0b 44360484Sobrien 44460484Sobrien ALIGN_TEXT 44560484Sobrien_esigcode: 44660484Sobrien 44760484Sobrien .data 44860484Sobrien .globl _szsigcode, _oszsigcode 44960484Sobrien_szsigcode: 45060484Sobrien .long _esigcode-_sigcode 45160484Sobrien_oszsigcode: 45260484Sobrien .long _esigcode-_osigcode 45360484Sobrien .text 45460484Sobrien 45560484Sobrien/********************************************************************** 45660484Sobrien * 45760484Sobrien * Recover the bootinfo passed to us from the boot program 45860484Sobrien * 45960484Sobrien */ 46060484Sobrienrecover_bootinfo: 46160484Sobrien /* 46260484Sobrien * This code is called in different ways depending on what loaded 46360484Sobrien * and started the kernel. This is used to detect how we get the 46460484Sobrien * arguments from the other code and what we do with them. 46560484Sobrien * 46660484Sobrien * Old disk boot blocks: 46760484Sobrien * (*btext)(howto, bootdev, cyloffset, esym); 46860484Sobrien * [return address == 0, and can NOT be returned to] 46960484Sobrien * [cyloffset was not supported by the FreeBSD boot code 47060484Sobrien * and always passed in as 0] 47160484Sobrien * [esym is also known as total in the boot code, and 47260484Sobrien * was never properly supported by the FreeBSD boot code] 47360484Sobrien * 47460484Sobrien * Old diskless netboot code: 47560484Sobrien * (*btext)(0,0,0,0,&nfsdiskless,0,0,0); 47660484Sobrien * [return address != 0, and can NOT be returned to] 47760484Sobrien * If we are being booted by this code it will NOT work, 47860484Sobrien * so we are just going to halt if we find this case. 47960484Sobrien * 48060484Sobrien * New uniform boot code: 48160484Sobrien * (*btext)(howto, bootdev, 0, 0, 0, &bootinfo) 48260484Sobrien * [return address != 0, and can be returned to] 48360484Sobrien * 48460484Sobrien * There may seem to be a lot of wasted arguments in here, but 48560484Sobrien * that is so the newer boot code can still load very old kernels 48660484Sobrien * and old boot code can load new kernels. 48760484Sobrien */ 48860484Sobrien 48960484Sobrien /* 49060484Sobrien * The old style disk boot blocks fake a frame on the stack and 49160484Sobrien * did an lret to get here. The frame on the stack has a return 49260484Sobrien * address of 0. 49360484Sobrien */ 49460484Sobrien cmpl $0,4(%ebp) 49560484Sobrien je olddiskboot 49660484Sobrien 49760484Sobrien /* 49860484Sobrien * We have some form of return address, so this is either the 49960484Sobrien * old diskless netboot code, or the new uniform code. That can 50060484Sobrien * be detected by looking at the 5th argument, if it is 0 50160484Sobrien * we are being booted by the new uniform boot code. 50260484Sobrien */ 50360484Sobrien cmpl $0,24(%ebp) 50460484Sobrien je newboot 50560484Sobrien 50660484Sobrien /* 50760484Sobrien * Seems we have been loaded by the old diskless boot code, we 50860484Sobrien * don't stand a chance of running as the diskless structure 50960484Sobrien * changed considerably between the two, so just halt. 51060484Sobrien */ 51160484Sobrien hlt 51260484Sobrien 51360484Sobrien /* 51460484Sobrien * We have been loaded by the new uniform boot code. 51560484Sobrien * Let's check the bootinfo version, and if we do not understand 51660484Sobrien * it we return to the loader with a status of 1 to indicate this error 51760484Sobrien */ 51860484Sobriennewboot: 51960484Sobrien movl 28(%ebp),%ebx /* &bootinfo.version */ 52060484Sobrien movl BI_VERSION(%ebx),%eax 52160484Sobrien cmpl $1,%eax /* We only understand version 1 */ 52260484Sobrien je 1f 52360484Sobrien movl $1,%eax /* Return status */ 52460484Sobrien leave 52560484Sobrien /* 52660484Sobrien * XXX this returns to our caller's caller (as is required) since 52760484Sobrien * we didn't set up a frame and our caller did. 52860484Sobrien */ 52960484Sobrien ret 53060484Sobrien 53160484Sobrien1: 53260484Sobrien /* 53360484Sobrien * If we have a kernelname copy it in 53460484Sobrien */ 53560484Sobrien movl BI_KERNELNAME(%ebx),%esi 53660484Sobrien cmpl $0,%esi 53760484Sobrien je 2f /* No kernelname */ 53860484Sobrien movl $MAXPATHLEN,%ecx /* Brute force!!! */ 53960484Sobrien movl $R(_kernelname),%edi 54060484Sobrien cmpb $'/',(%esi) /* Make sure it starts with a slash */ 54160484Sobrien je 1f 542 movb $'/',(%edi) 543 incl %edi 544 decl %ecx 5451: 546 cld 547 rep 548 movsb 549 5502: 551 /* 552 * Determine the size of the boot loader's copy of the bootinfo 553 * struct. This is impossible to do properly because old versions 554 * of the struct don't contain a size field and there are 2 old 555 * versions with the same version number. 556 */ 557 movl $BI_ENDCOMMON,%ecx /* prepare for sizeless version */ 558 testl $RB_BOOTINFO,8(%ebp) /* bi_size (and bootinfo) valid? */ 559 je got_bi_size /* no, sizeless version */ 560 movl BI_SIZE(%ebx),%ecx 561got_bi_size: 562 563 /* 564 * Copy the common part of the bootinfo struct 565 */ 566 movl %ebx,%esi 567 movl $R(_bootinfo),%edi 568 cmpl $BOOTINFO_SIZE,%ecx 569 jbe got_common_bi_size 570 movl $BOOTINFO_SIZE,%ecx 571got_common_bi_size: 572 cld 573 rep 574 movsb 575 576#ifdef NFS_ROOT 577#ifndef BOOTP_NFSV3 578 /* 579 * If we have a nfs_diskless structure copy it in 580 */ 581 movl BI_NFS_DISKLESS(%ebx),%esi 582 cmpl $0,%esi 583 je olddiskboot 584 movl $R(_nfs_diskless),%edi 585 movl $NFSDISKLESS_SIZE,%ecx 586 cld 587 rep 588 movsb 589 movl $R(_nfs_diskless_valid),%edi 590 movl $1,(%edi) 591#endif 592#endif 593 594 /* 595 * The old style disk boot. 596 * (*btext)(howto, bootdev, cyloffset, esym); 597 * Note that the newer boot code just falls into here to pick 598 * up howto and bootdev, cyloffset and esym are no longer used 599 */ 600olddiskboot: 601 movl 8(%ebp),%eax 602 movl %eax,R(_boothowto) 603 movl 12(%ebp),%eax 604 movl %eax,R(_bootdev) 605 606 ret 607 608 609/********************************************************************** 610 * 611 * Identify the CPU and initialize anything special about it 612 * 613 */ 614identify_cpu: 615 616 /* Try to toggle alignment check flag; does not exist on 386. */ 617 pushfl 618 popl %eax 619 movl %eax,%ecx 620 orl $PSL_AC,%eax 621 pushl %eax 622 popfl 623 pushfl 624 popl %eax 625 xorl %ecx,%eax 626 andl $PSL_AC,%eax 627 pushl %ecx 628 popfl 629 630 testl %eax,%eax 631 jnz try486 632 633 /* NexGen CPU does not have aligment check flag. */ 634 pushfl 635 movl $0x5555, %eax 636 xorl %edx, %edx 637 movl $2, %ecx 638 clc 639 divl %ecx 640 jz trynexgen 641 popfl 642 movl $CPU_386,R(_cpu) 643 jmp 3f 644 645trynexgen: 646 popfl 647 movl $CPU_NX586,R(_cpu) 648 movl $0x4778654e,R(_cpu_vendor) # store vendor string 649 movl $0x72446e65,R(_cpu_vendor+4) 650 movl $0x6e657669,R(_cpu_vendor+8) 651 movl $0,R(_cpu_vendor+12) 652 jmp 3f 653 654try486: /* Try to toggle identification flag; does not exist on early 486s. */ 655 pushfl 656 popl %eax 657 movl %eax,%ecx 658 xorl $PSL_ID,%eax 659 pushl %eax 660 popfl 661 pushfl 662 popl %eax 663 xorl %ecx,%eax 664 andl $PSL_ID,%eax 665 pushl %ecx 666 popfl 667 668 testl %eax,%eax 669 jnz trycpuid 670 movl $CPU_486,R(_cpu) 671 672 /* 673 * Check Cyrix CPU 674 * Cyrix CPUs do not change the undefined flags following 675 * execution of the divide instruction which divides 5 by 2. 676 * 677 * Note: CPUID is enabled on M2, so it passes another way. 678 */ 679 pushfl 680 movl $0x5555, %eax 681 xorl %edx, %edx 682 movl $2, %ecx 683 clc 684 divl %ecx 685 jnc trycyrix 686 popfl 687 jmp 3f /* You may use Intel CPU. */ 688 689trycyrix: 690 popfl 691 /* 692 * IBM Bluelighting CPU also doesn't change the undefined flags. 693 * Because IBM doesn't disclose the information for Bluelighting 694 * CPU, we couldn't distinguish it from Cyrix's (including IBM 695 * brand of Cyrix CPUs). 696 */ 697 movl $0x69727943,R(_cpu_vendor) # store vendor string 698 movl $0x736e4978,R(_cpu_vendor+4) 699 movl $0x64616574,R(_cpu_vendor+8) 700 jmp 3f 701 702trycpuid: /* Use the `cpuid' instruction. */ 703 xorl %eax,%eax 704 .byte 0x0f,0xa2 # cpuid 0 705 movl %eax,R(_cpu_high) # highest capability 706 movl %ebx,R(_cpu_vendor) # store vendor string 707 movl %edx,R(_cpu_vendor+4) 708 movl %ecx,R(_cpu_vendor+8) 709 movb $0,R(_cpu_vendor+12) 710 711 movl $1,%eax 712 .byte 0x0f,0xa2 # cpuid 1 713 movl %eax,R(_cpu_id) # store cpu_id 714 movl %edx,R(_cpu_feature) # store cpu_feature 715 rorl $8,%eax # extract family type 716 andl $15,%eax 717 cmpl $5,%eax 718 jae 1f 719 720 /* less than Pentium; must be 486 */ 721 movl $CPU_486,R(_cpu) 722 jmp 3f 7231: 724 /* a Pentium? */ 725 cmpl $5,%eax 726 jne 2f 727 movl $CPU_586,R(_cpu) 728 jmp 3f 7292: 730 /* Greater than Pentium...call it a Pentium Pro */ 731 movl $CPU_686,R(_cpu) 7323: 733 ret 734 735 736/********************************************************************** 737 * 738 * Create the first page directory and its page tables. 739 * 740 */ 741 742create_pagetables: 743 744 testl $CPUID_PGE, R(_cpu_feature) 745 jz 1f 746 movl %cr4, %eax 747 orl $CR4_PGE, %eax 748 movl %eax, %cr4 7491: 750 751/* Find end of kernel image (rounded up to a page boundary). */ 752 movl $R(_end),%esi 753 754/* include symbols if loaded and useful */ 755#ifdef DDB 756 movl R(_bootinfo+BI_ESYMTAB),%edi 757 testl %edi,%edi 758 je over_symalloc 759 movl %edi,%esi 760 movl $KERNBASE,%edi 761 addl %edi,R(_bootinfo+BI_SYMTAB) 762 addl %edi,R(_bootinfo+BI_ESYMTAB) 763over_symalloc: 764#endif 765 766/* If we are told where the end of the kernel space is, believe it. */ 767 movl R(_bootinfo+BI_KERNEND),%edi 768 testl %edi,%edi 769 je no_kernend 770 movl %edi,%esi 771no_kernend: 772 773 addl $PAGE_MASK,%esi 774 andl $~PAGE_MASK,%esi 775 movl %esi,R(_KERNend) /* save end of kernel */ 776 movl %esi,R(physfree) /* next free page is at end of kernel */ 777 778/* Allocate Kernel Page Tables */ 779 ALLOCPAGES(NKPT) 780 movl %esi,R(_KPTphys) 781 782/* Allocate Page Table Directory */ 783 ALLOCPAGES(1) 784 movl %esi,R(_IdlePTD) 785 786/* Allocate UPAGES */ 787 ALLOCPAGES(UPAGES) 788 movl %esi,R(p0upa) 789 addl $KERNBASE, %esi 790 movl %esi, R(_proc0paddr) 791 792 ALLOCPAGES(1) /* vm86/bios stack */ 793 movl %esi,R(vm86phystk) 794 795 ALLOCPAGES(3) /* pgtable + ext + IOPAGES */ 796 movl %esi,R(_vm86pa) 797 addl $KERNBASE, %esi 798 movl %esi, R(_vm86paddr) 799 800#ifdef SMP 801/* Allocate cpu0's private data page */ 802 ALLOCPAGES(1) 803 movl %esi,R(cpu0pp) 804 addl $KERNBASE, %esi 805 movl %esi, R(_cpu0prvpage) /* relocated to KVM space */ 806 807/* Allocate SMP page table page */ 808 ALLOCPAGES(1) 809 movl %esi,R(SMPptpa) 810 addl $KERNBASE, %esi 811 movl %esi, R(_SMPpt) /* relocated to KVM space */ 812#endif /* SMP */ 813 814/* Map read-only from zero to the end of the kernel text section */ 815 xorl %eax, %eax 816#ifdef BDE_DEBUGGER 817/* If the debugger is present, actually map everything read-write. */ 818 cmpl $0,R(_bdb_exists) 819 jne map_read_write 820#endif 821 xorl %edx,%edx 822 823#if !defined(SMP) 824 testl $CPUID_PGE, R(_cpu_feature) 825 jz 2f 826 orl $PG_G,%edx 827#endif 828 8292: movl $R(_etext),%ecx 830 addl $PAGE_MASK,%ecx 831 shrl $PAGE_SHIFT,%ecx 832 fillkptphys(%edx) 833 834/* Map read-write, data, bss and symbols */ 835 movl $R(_etext),%eax 836 addl $PAGE_MASK, %eax 837 andl $~PAGE_MASK, %eax 838map_read_write: 839 movl $PG_RW,%edx 840#if !defined(SMP) 841 testl $CPUID_PGE, R(_cpu_feature) 842 jz 1f 843 orl $PG_G,%edx 844#endif 845 8461: movl R(_KERNend),%ecx 847 subl %eax,%ecx 848 shrl $PAGE_SHIFT,%ecx 849 fillkptphys(%edx) 850 851/* Map page directory. */ 852 movl R(_IdlePTD), %eax 853 movl $1, %ecx 854 fillkptphys($PG_RW) 855 856/* Map proc0's UPAGES in the physical way ... */ 857 movl R(p0upa), %eax 858 movl $UPAGES, %ecx 859 fillkptphys($PG_RW) 860 861/* Map ISA hole */ 862 movl $ISA_HOLE_START, %eax 863 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx 864 fillkptphys($PG_RW) 865 866/* Map space for the vm86 region */ 867 movl R(vm86phystk), %eax 868 movl $4, %ecx 869 fillkptphys($PG_RW) 870 871/* Map page 0 into the vm86 page table */ 872 movl $0, %eax 873 movl $0, %ebx 874 movl $1, %ecx 875 fillkpt(R(_vm86pa), $PG_RW|PG_U) 876 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) 882 883#ifdef SMP 884/* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */ 885 movl R(cpu0pp), %eax 886 movl $1, %ecx 887 fillkptphys($PG_RW) 888 889/* Map SMP page table page into global kmem FWIW */ 890 movl R(SMPptpa), %eax 891 movl $1, %ecx 892 fillkptphys($PG_RW) 893 894/* Map the private page into the SMP page table */ 895 movl R(cpu0pp), %eax 896 movl $0, %ebx /* pte offset = 0 */ 897 movl $1, %ecx /* one private page coming right up */ 898 fillkpt(R(SMPptpa), $PG_RW) 899 900/* ... and put the page table table in the pde. */ 901 movl R(SMPptpa), %eax 902 movl $MPPTDI, %ebx 903 movl $1, %ecx 904 fillkpt(R(_IdlePTD), $PG_RW) 905 906/* Fakeup VA for the local apic to allow early traps. */ 907 ALLOCPAGES(1) 908 movl %esi, %eax 909 movl $(NPTEPG-1), %ebx /* pte offset = NTEPG-1 */ 910 movl $1, %ecx /* one private pt coming right up */ 911 fillkpt(R(SMPptpa), $PG_RW) 912 913/* Initialize mp lock to allow early traps */ 914 movl $1, R(_mp_lock) 915#endif /* SMP */ 916 917/* install a pde for temporary double map of bottom of VA */ 918 movl R(_KPTphys), %eax 919 xorl %ebx, %ebx 920 movl $1, %ecx 921 fillkpt(R(_IdlePTD), $PG_RW) 922 923/* install pde's for pt's */ 924 movl R(_KPTphys), %eax 925 movl $KPTDI, %ebx 926 movl $NKPT, %ecx 927 fillkpt(R(_IdlePTD), $PG_RW) 928 929/* install a pde recursively mapping page directory as a page table */ 930 movl R(_IdlePTD), %eax 931 movl $PTDPTDI, %ebx 932 movl $1,%ecx 933 fillkpt(R(_IdlePTD), $PG_RW) 934 935 ret 936 937#ifdef BDE_DEBUGGER 938bdb_prepare_paging: 939 cmpl $0,R(_bdb_exists) 940 je bdb_prepare_paging_exit 941 942 subl $6,%esp 943 944 /* 945 * Copy and convert debugger entries from the bootstrap gdt and idt 946 * to the kernel gdt and idt. Everything is still in low memory. 947 * Tracing continues to work after paging is enabled because the 948 * low memory addresses remain valid until everything is relocated. 949 * However, tracing through the setidt() that initializes the trace 950 * trap will crash. 951 */ 952 sgdt (%esp) 953 movl 2(%esp),%esi /* base address of bootstrap gdt */ 954 movl $R(_gdt),%edi 955 movl %edi,2(%esp) /* prepare to load kernel gdt */ 956 movl $8*18/4,%ecx 957 cld 958 rep /* copy gdt */ 959 movsl 960 movl $R(_gdt),-8+2(%edi) /* adjust gdt self-ptr */ 961 movb $0x92,-8+5(%edi) 962 lgdt (%esp) 963 964 sidt (%esp) 965 movl 2(%esp),%esi /* base address of current idt */ 966 movl 8+4(%esi),%eax /* convert dbg descriptor to ... */ 967 movw 8(%esi),%ax 968 movl %eax,R(bdb_dbg_ljmp+1) /* ... immediate offset ... */ 969 movl 8+2(%esi),%eax 970 movw %ax,R(bdb_dbg_ljmp+5) /* ... and selector for ljmp */ 971 movl 24+4(%esi),%eax /* same for bpt descriptor */ 972 movw 24(%esi),%ax 973 movl %eax,R(bdb_bpt_ljmp+1) 974 movl 24+2(%esi),%eax 975 movw %ax,R(bdb_bpt_ljmp+5) 976 movl R(_idt),%edi 977 movl %edi,2(%esp) /* prepare to load kernel idt */ 978 movl $8*4/4,%ecx 979 cld 980 rep /* copy idt */ 981 movsl 982 lidt (%esp) 983 984 addl $6,%esp 985 986bdb_prepare_paging_exit: 987 ret 988 989/* Relocate debugger gdt entries and gdt and idt pointers. */ 990bdb_commit_paging: 991 cmpl $0,_bdb_exists 992 je bdb_commit_paging_exit 993 994 movl $_gdt+8*9,%eax /* adjust slots 9-17 */ 995 movl $9,%ecx 996reloc_gdt: 997 movb $KERNBASE>>24,7(%eax) /* top byte of base addresses, was 0, */ 998 addl $8,%eax /* now KERNBASE>>24 */ 999 loop reloc_gdt 1000 1001 subl $6,%esp 1002 sgdt (%esp) 1003 addl $KERNBASE,2(%esp) 1004 lgdt (%esp) 1005 sidt (%esp) 1006 addl $KERNBASE,2(%esp) 1007 lidt (%esp) 1008 addl $6,%esp 1009 1010 int $3 1011 1012bdb_commit_paging_exit: 1013 ret 1014 1015#endif /* BDE_DEBUGGER */ 1016