locore.s revision 35072
14Srgrimes/*- 24Srgrimes * Copyright (c) 1990 The Regents of the University of California. 34Srgrimes * All rights reserved. 44Srgrimes * 54Srgrimes * This code is derived from software contributed to Berkeley by 64Srgrimes * William Jolitz. 74Srgrimes * 84Srgrimes * Redistribution and use in source and binary forms, with or without 94Srgrimes * modification, are permitted provided that the following conditions 104Srgrimes * are met: 114Srgrimes * 1. Redistributions of source code must retain the above copyright 124Srgrimes * notice, this list of conditions and the following disclaimer. 134Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 144Srgrimes * notice, this list of conditions and the following disclaimer in the 154Srgrimes * documentation and/or other materials provided with the distribution. 164Srgrimes * 3. All advertising materials mentioning features or use of this software 174Srgrimes * must display the following acknowledgement: 184Srgrimes * This product includes software developed by the University of 194Srgrimes * California, Berkeley and its contributors. 204Srgrimes * 4. Neither the name of the University nor the names of its contributors 214Srgrimes * may be used to endorse or promote products derived from this software 224Srgrimes * without specific prior written permission. 234Srgrimes * 244Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 254Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 264Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 274Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 284Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 294Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 304Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 314Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 324Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 334Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 344Srgrimes * SUCH DAMAGE. 354Srgrimes * 36556Srgrimes * from: @(#)locore.s 7.3 (Berkeley) 5/13/91 3735072Speter * $Id: locore.s,v 1.106 1998/04/04 13:24:11 phk Exp $ 3815392Sphk * 39757Sdg * originally from: locore.s, by William F. Jolitz 40757Sdg * 41757Sdg * Substantially rewritten by David Greenman, Rod Grimes, 4215392Sphk * Bruce Evans, Wolfgang Solfrank, Poul-Henning Kamp 4315392Sphk * and many others. 444Srgrimes */ 454Srgrimes 4614835Sbde#include "apm.h" 4732358Seivind#include "opt_bootp.h" 4818842Sbde#include "opt_cpu.h" 4913228Swollman#include "opt_ddb.h" 5032917Seivind#include "opt_nfs.h" 5118702Sjkh#include "opt_userconfig.h" 5230265Speter#include "opt_vm86.h" 5314835Sbde 5414835Sbde#include <sys/syscall.h> 555908Sbde#include <sys/reboot.h> 564Srgrimes 5714835Sbde#include <machine/asmacros.h> 5814835Sbde#include <machine/cputypes.h> 5914835Sbde#include <machine/psl.h> 6015543Sphk#include <machine/pmap.h> 6114835Sbde#include <machine/specialreg.h> 6214835Sbde 6314835Sbde#include "assym.s" 6414835Sbde 654Srgrimes/* 66757Sdg * XXX 67757Sdg * 684Srgrimes * Note: This version greatly munged to avoid various assembler errors 694Srgrimes * that may be fixed in newer versions of gas. Perhaps newer versions 704Srgrimes * will have more pleasant appearance. 714Srgrimes */ 724Srgrimes 73200Sdg/* 744Srgrimes * PTmap is recursive pagemap at top of virtual address space. 754Srgrimes * Within PTmap, the page directory can be found (third indirection). 764Srgrimes */ 773861Sbde .globl _PTmap,_PTD,_PTDpde 7815543Sphk .set _PTmap,(PTDPTDI << PDRSHIFT) 7915543Sphk .set _PTD,_PTmap + (PTDPTDI * PAGE_SIZE) 80757Sdg .set _PTDpde,_PTD + (PTDPTDI * PDESIZE) 81592Srgrimes 823861Sbde/* 834Srgrimes * APTmap, APTD is the alternate recursive pagemap. 844Srgrimes * It's used when modifying another process's page tables. 854Srgrimes */ 86592Srgrimes .globl _APTmap,_APTD,_APTDpde 87592Srgrimes .set _APTmap,APTDPTDI << PDRSHIFT 8815543Sphk .set _APTD,_APTmap + (APTDPTDI * PAGE_SIZE) 89757Sdg .set _APTDpde,_PTD + (APTDPTDI * PDESIZE) 904Srgrimes 914Srgrimes/* 92556Srgrimes * Globals 93556Srgrimes */ 94556Srgrimes .data 9514835Sbde ALIGN_DATA /* just to be sure */ 96134Sdg 9725083Sjdp .globl HIDENAME(tmpstk) 9813729Sdg .space 0x2000 /* space for tmpstk - temporary stack */ 9925083SjdpHIDENAME(tmpstk): 1003842Sdg 1013861Sbde .globl _boothowto,_bootdev 102134Sdg 10315565Sphk .globl _cpu,_cpu_vendor,_cpu_id,_bootinfo 1046308Sphk .globl _cpu_high, _cpu_feature 1052783Ssos 106757Sdg_cpu: .long 0 /* are we 386, 386sx, or 486 */ 1072216Sbde_cpu_id: .long 0 /* stepping ID */ 1086308Sphk_cpu_high: .long 0 /* highest arg to CPUID */ 1096308Sphk_cpu_feature: .long 0 /* features */ 1102216Sbde_cpu_vendor: .space 20 /* CPU origin code */ 1115908Sbde_bootinfo: .space BOOTINFO_SIZE /* bootinfo that we can handle */ 1124Srgrimes 113757Sdg_KERNend: .long 0 /* phys addr end of kernel (just after bss) */ 11415428Sphkphysfree: .long 0 /* phys addr of next free page */ 115757Sdg 11626812Speter#ifdef SMP 11725164Spetercpu0pp: .long 0 /* phys addr cpu0 private pg */ 11825164Spetercpu0pt: .long 0 /* phys addr cpu0 private pt */ 11925164Speter 12025164Speter .globl _cpu0prvpage,_cpu0prvpt 12125164Speter_cpu0prvpage: .long 0 /* relocated version */ 12225164Speter_cpu0prvpt: .long 0 /* relocated version */ 12326812Speter#endif /* SMP */ 12425164Speter 1253861Sbde .globl _IdlePTD 126757Sdg_IdlePTD: .long 0 /* phys addr of kernel PTD */ 1273861Sbde 12826812Speter#ifdef SMP 12926812Speter .globl _KPTphys 13026812Speter#endif 131757Sdg_KPTphys: .long 0 /* phys addr of kernel page tables */ 1324Srgrimes 133757Sdg .globl _proc0paddr 134757Sdg_proc0paddr: .long 0 /* address of proc 0 address space */ 13524693Speterp0upa: .long 0 /* phys addr of proc0's UPAGES */ 136134Sdg 13734840Sjlemon#ifdef VM86 13834840Sjlemon .globl _vm86paddr, _vm86pa 13934840Sjlemon_vm86paddr: .long 0 /* address of vm86 region */ 14034840Sjlemon_vm86pa: .long 0 /* phys addr of vm86 region */ 14134840Sjlemon#endif 14234840Sjlemon 14315428Sphk#ifdef BDE_DEBUGGER 14415428Sphk .globl _bdb_exists /* flag to indicate BDE debugger is present */ 14515428Sphk_bdb_exists: .long 0 14615428Sphk#endif 147718Swollman 14815428Sphk 14915392Sphk/********************************************************************** 15015392Sphk * 15115392Sphk * Some handy macros 15215392Sphk * 153556Srgrimes */ 154134Sdg 15515392Sphk#define R(foo) ((foo)-KERNBASE) 15615392Sphk 15715392Sphk#define ALLOCPAGES(foo) \ 15815392Sphk movl R(physfree), %esi ; \ 15915543Sphk movl $((foo)*PAGE_SIZE), %eax ; \ 16015392Sphk addl %esi, %eax ; \ 16115392Sphk movl %eax, R(physfree) ; \ 16215392Sphk movl %esi, %edi ; \ 16315543Sphk movl $((foo)*PAGE_SIZE),%ecx ; \ 16415392Sphk xorl %eax,%eax ; \ 16515428Sphk cld ; \ 16615428Sphk rep ; \ 16715428Sphk stosb 16815392Sphk 169134Sdg/* 17015392Sphk * fillkpt 17115565Sphk * eax = page frame address 17215565Sphk * ebx = index into page table 17315392Sphk * ecx = how many pages to map 17415565Sphk * base = base address of page dir/table 17515565Sphk * prot = protection bits 176134Sdg */ 17715565Sphk#define fillkpt(base, prot) \ 17819621Sdyson shll $2,%ebx ; \ 17919621Sdyson addl base,%ebx ; \ 18019621Sdyson orl $PG_V,%eax ; \ 18119621Sdyson orl prot,%eax ; \ 18215565Sphk1: movl %eax,(%ebx) ; \ 18315565Sphk addl $PAGE_SIZE,%eax ; /* increment physical address */ \ 18415565Sphk addl $4,%ebx ; /* next pte */ \ 18515428Sphk loop 1b 18615392Sphk 18715565Sphk/* 18815565Sphk * fillkptphys(prot) 18915565Sphk * eax = physical address 19015565Sphk * ecx = how many pages to map 19115565Sphk * prot = protection bits 19215565Sphk */ 19315565Sphk#define fillkptphys(prot) \ 19415565Sphk movl %eax, %ebx ; \ 19515565Sphk shrl $PAGE_SHIFT, %ebx ; \ 19615565Sphk fillkpt(R(_KPTphys), prot) 19715565Sphk 19815392Sphk .text 19915392Sphk/********************************************************************** 20015392Sphk * 20115392Sphk * This is where the bootblocks start us, set the ball rolling... 20215392Sphk * 20315392Sphk */ 2041321SdgNON_GPROF_ENTRY(btext) 2054Srgrimes 20624112Skato#ifdef PC98 20724112Skato jmp 1f 20824112Skato .globl _pc98_system_parameter 20924112Skato .org 0x400 21024112Skato_pc98_system_parameter: 21124112Skato .space 0x240 /* BIOS parameter block */ 21224112Skato1: 21324112Skato /* save SYSTEM PARAMETER for resume (NS/T or other) */ 21424112Skato movl $0xa1000,%esi 21524112Skato movl $0x100000,%edi 21624112Skato movl $0x0630,%ecx 21724112Skato cld 21824112Skato rep 21924112Skato movsb 22024112Skato#else /* IBM-PC */ 22115428Sphk#ifdef BDE_DEBUGGER 22215428Sphk#ifdef BIOS_STEALS_3K 22315428Sphk cmpl $0x0375c339,0x95504 22415428Sphk#else 22515428Sphk cmpl $0x0375c339,0x96104 /* XXX - debugger signature */ 22615428Sphk#endif 22715428Sphk jne 1f 22815428Sphk movb $1,R(_bdb_exists) 22915428Sphk1: 23015428Sphk#endif 23115428Sphk 23215392Sphk/* Tell the bios to warmboot next time */ 23315392Sphk movw $0x1234,0x472 23424112Skato#endif /* PC98 */ 23515392Sphk 23615428Sphk/* Set up a real frame in case the double return in newboot is executed. */ 2373384Srgrimes pushl %ebp 2383384Srgrimes movl %esp, %ebp 2393384Srgrimes 24015392Sphk/* Don't trust what the BIOS gives for eflags. */ 2415603Sbde pushl $PSL_KERNEL 2422486Sdg popfl 24315428Sphk 24415428Sphk/* 24515428Sphk * Don't trust what the BIOS gives for %fs and %gs. Trust the bootstrap 24615428Sphk * to set %cs, %ds, %es and %ss. 24715428Sphk */ 24815428Sphk mov %ds, %ax 2494217Sphk mov %ax, %fs 2504217Sphk mov %ax, %gs 2514217Sphk 25215392Sphk call recover_bootinfo 25315392Sphk 25415428Sphk/* Get onto a stack that we can trust. */ 25515428Sphk/* 25615428Sphk * XXX this step is delayed in case recover_bootinfo needs to return via 25715428Sphk * the old stack, but it need not be, since recover_bootinfo actually 25815428Sphk * returns via the old frame. 25915428Sphk */ 26025083Sjdp movl $R(HIDENAME(tmpstk)),%esp 26115392Sphk 26224112Skato#ifdef PC98 26324112Skato testb $0x02,0x100620 /* pc98_machine_type & M_EPSON_PC98 */ 26424112Skato jz 3f 26524112Skato cmpb $0x0b,0x100624 /* epson_machine_id <= 0x0b */ 26624112Skato ja 3f 26724112Skato 26824112Skato /* count up memory */ 26924112Skato movl $0x100000,%eax /* next, talley remaining memory */ 27024112Skato movl $0xFFF-0x100,%ecx 27124112Skato1: movl 0(%eax),%ebx /* save location to check */ 27224112Skato movl $0xa55a5aa5,0(%eax) /* write test pattern */ 27324112Skato cmpl $0xa55a5aa5,0(%eax) /* does not check yet for rollover */ 27424112Skato jne 2f 27524112Skato movl %ebx,0(%eax) /* restore memory */ 27624112Skato addl $PAGE_SIZE,%eax 27724112Skato loop 1b 27824112Skato2: subl $0x100000,%eax 27924112Skato shrl $17,%eax 28024112Skato movb %al,0x100401 28124112Skato3: 28224112Skato#endif 28324112Skato 28415392Sphk call identify_cpu 28515392Sphk 28615392Sphk/* clear bss */ 28715428Sphk/* 28817120Sbde * XXX this should be done a little earlier. 28915428Sphk * 29017120Sbde * XXX we don't check that there is memory for our bss and page tables 29117120Sbde * before using it. 29215428Sphk * 29315428Sphk * XXX the boot program somewhat bogusly clears the bss. We still have 29415428Sphk * to do it in case we were unzipped by kzipboot. Then the boot program 29515428Sphk * only clears kzipboot's bss. 29615428Sphk * 29715428Sphk * XXX the gdt and idt are still somewhere in the boot program. We 29815428Sphk * depend on the convention that the boot program is below 1MB and we 29915428Sphk * are above 1MB to keep the gdt and idt away from the bss and page 30017120Sbde * tables. The idt is only used if BDE_DEBUGGER is enabled. 30115428Sphk */ 30215392Sphk movl $R(_end),%ecx 30315392Sphk movl $R(_edata),%edi 30415392Sphk subl %edi,%ecx 30515392Sphk xorl %eax,%eax 30615428Sphk cld 30715428Sphk rep 30815428Sphk stosb 30915392Sphk 31015392Sphk#if NAPM > 0 31115428Sphk/* 31215428Sphk * XXX it's not clear that APM can live in the current environonment. 31315428Sphk * Only pc-relative addressing works. 31415428Sphk */ 31515428Sphk call _apm_setup 31615428Sphk#endif 31715392Sphk 31815392Sphk call create_pagetables 31915392Sphk 32027993Sdyson#ifdef VM86 32127993Sdyson/* 32227993Sdyson * If the CPU has support for VME, turn it on. 32327993Sdyson */ 32427993Sdyson testl $CPUID_VME, R(_cpu_feature) 32527993Sdyson jz 1f 32627993Sdyson movl %cr4, %eax 32727993Sdyson orl $CR4_VME, %eax 32827993Sdyson movl %eax, %cr4 32927993Sdyson1: 33027993Sdyson#endif /* VM86 */ 33127993Sdyson 33215428Sphk#ifdef BDE_DEBUGGER 33315428Sphk/* 33415428Sphk * Adjust as much as possible for paging before enabling paging so that the 33515428Sphk * adjustments can be traced. 33615428Sphk */ 33715428Sphk call bdb_prepare_paging 33815428Sphk#endif 33915428Sphk 34015392Sphk/* Now enable paging */ 34115392Sphk movl R(_IdlePTD), %eax 34215392Sphk movl %eax,%cr3 /* load ptd addr into mmu */ 34315392Sphk movl %cr0,%eax /* get control word */ 34415392Sphk orl $CR0_PE|CR0_PG,%eax /* enable paging */ 34515392Sphk movl %eax,%cr0 /* and let's page NOW! */ 34615392Sphk 34715428Sphk#ifdef BDE_DEBUGGER 34815428Sphk/* 34915428Sphk * Complete the adjustments for paging so that we can keep tracing through 35017120Sbde * initi386() after the low (physical) addresses for the gdt and idt become 35115428Sphk * invalid. 35215428Sphk */ 35315428Sphk call bdb_commit_paging 35415428Sphk#endif 35515428Sphk 35615428Sphk pushl $begin /* jump to high virtualized address */ 35715392Sphk ret 35815392Sphk 35915392Sphk/* now running relocated at KERNBASE where the system is linked to run */ 36015392Sphkbegin: 36115392Sphk /* set up bootstrap stack */ 36224691Speter movl _proc0paddr,%esp /* location of in-kernel pages */ 36324691Speter addl $UPAGES*PAGE_SIZE,%esp /* bootstrap stack end location */ 36415392Sphk xorl %eax,%eax /* mark end of frames */ 36515392Sphk movl %eax,%ebp 36615392Sphk movl _proc0paddr,%eax 36715392Sphk movl _IdlePTD, %esi 36815392Sphk movl %esi,PCB_CR3(%eax) 36935072Speter movl $_proc0,_curproc 37015392Sphk 37115392Sphk movl physfree, %esi 37215392Sphk pushl %esi /* value of first for init386(first) */ 37315392Sphk call _init386 /* wire 386 chip for unix operation */ 37415392Sphk popl %esi 37515392Sphk 37615392Sphk .globl __ucodesel,__udatasel 37715392Sphk 37815392Sphk pushl $0 /* unused */ 37915392Sphk pushl __udatasel /* ss */ 38015392Sphk pushl $0 /* esp - filled in by execve() */ 38115392Sphk pushl $PSL_USER /* eflags (IOPL 0, int enab) */ 38215392Sphk pushl __ucodesel /* cs */ 38315392Sphk pushl $0 /* eip - filled in by execve() */ 38415392Sphk subl $(12*4),%esp /* space for rest of registers */ 38515392Sphk 38615392Sphk pushl %esp /* call main with frame pointer */ 38715392Sphk call _main /* autoconfiguration, mountroot etc */ 38815392Sphk 38924691Speter hlt /* never returns to here */ 39015392Sphk 39124691Speter/* 39224691Speter * When starting init, call this to configure the process for user 39324691Speter * mode. This will be inherited by other processes. 39424691Speter */ 39524691SpeterNON_GPROF_ENTRY(prepare_usermode) 39615392Sphk /* 39724112Skato * Now we've run main() and determined what cpu-type we are, we can 39815392Sphk * enable write protection and alignment checking on i486 cpus and 39915392Sphk * above. 40015392Sphk */ 40115392Sphk#if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU) 40215392Sphk cmpl $CPUCLASS_386,_cpu_class 40315392Sphk je 1f 40415392Sphk movl %cr0,%eax /* get control word */ 40515392Sphk orl $CR0_WP|CR0_AM,%eax /* enable i486 features */ 40615392Sphk movl %eax,%cr0 /* and do it */ 40715428Sphk1: 40815392Sphk#endif 40915392Sphk /* 41015392Sphk * on return from main(), we are process 1 41115392Sphk * set up address space and stack so that we can 'return' to user mode 41215392Sphk */ 41315392Sphk movl __ucodesel,%eax 41415392Sphk movl __udatasel,%ecx 41515392Sphk 41624691Speter#if 0 41715392Sphk movl %cx,%ds 41824691Speter#endif 41915392Sphk movl %cx,%es 42015392Sphk movl %ax,%fs /* double map cs to fs */ 42115392Sphk movl %cx,%gs /* and ds to gs */ 42224691Speter ret /* goto user! */ 42315392Sphk 42424691Speter 42515392Sphk#define LCALL(x,y) .byte 0x9a ; .long y ; .word x 42615392Sphk 42715392Sphk/* 42815392Sphk * Signal trampoline, copied to top of user stack 42915392Sphk */ 43015392SphkNON_GPROF_ENTRY(sigcode) 43115392Sphk call SIGF_HANDLER(%esp) 43215392Sphk lea SIGF_SC(%esp),%eax /* scp (the call may have clobbered the */ 43315392Sphk /* copy at 8(%esp)) */ 43415392Sphk pushl %eax 43515392Sphk pushl %eax /* junk to fake return address */ 43615392Sphk movl $SYS_sigreturn,%eax /* sigreturn() */ 43715392Sphk LCALL(0x7,0) /* enter kernel with args on stack */ 43815392Sphk hlt /* never gets here */ 43925083Sjdp ALIGN_TEXT 44015392Sphk_esigcode: 44115392Sphk 44215392Sphk .data 44315392Sphk .globl _szsigcode 44415392Sphk_szsigcode: 44515392Sphk .long _esigcode-_sigcode 44615428Sphk .text 44715392Sphk 44815392Sphk/********************************************************************** 44915392Sphk * 45015392Sphk * Recover the bootinfo passed to us from the boot program 45115392Sphk * 45215392Sphk */ 45315392Sphkrecover_bootinfo: 45415392Sphk /* 4553284Srgrimes * This code is called in different ways depending on what loaded 4563284Srgrimes * and started the kernel. This is used to detect how we get the 4573284Srgrimes * arguments from the other code and what we do with them. 4583284Srgrimes * 4593284Srgrimes * Old disk boot blocks: 4603284Srgrimes * (*btext)(howto, bootdev, cyloffset, esym); 4613284Srgrimes * [return address == 0, and can NOT be returned to] 4623284Srgrimes * [cyloffset was not supported by the FreeBSD boot code 4633284Srgrimes * and always passed in as 0] 4643284Srgrimes * [esym is also known as total in the boot code, and 4653284Srgrimes * was never properly supported by the FreeBSD boot code] 4663284Srgrimes * 4673284Srgrimes * Old diskless netboot code: 4683284Srgrimes * (*btext)(0,0,0,0,&nfsdiskless,0,0,0); 4693284Srgrimes * [return address != 0, and can NOT be returned to] 4703284Srgrimes * If we are being booted by this code it will NOT work, 4713284Srgrimes * so we are just going to halt if we find this case. 4723284Srgrimes * 4733284Srgrimes * New uniform boot code: 4743284Srgrimes * (*btext)(howto, bootdev, 0, 0, 0, &bootinfo) 4753284Srgrimes * [return address != 0, and can be returned to] 4763284Srgrimes * 4773284Srgrimes * There may seem to be a lot of wasted arguments in here, but 4783384Srgrimes * that is so the newer boot code can still load very old kernels 4793384Srgrimes * and old boot code can load new kernels. 4804Srgrimes */ 4813284Srgrimes 4823284Srgrimes /* 4833284Srgrimes * The old style disk boot blocks fake a frame on the stack and 4843284Srgrimes * did an lret to get here. The frame on the stack has a return 4853284Srgrimes * address of 0. 4863284Srgrimes */ 4873384Srgrimes cmpl $0,4(%ebp) 48815392Sphk je olddiskboot 4893284Srgrimes 4903284Srgrimes /* 4913284Srgrimes * We have some form of return address, so this is either the 4923284Srgrimes * old diskless netboot code, or the new uniform code. That can 49315428Sphk * be detected by looking at the 5th argument, if it is 0 49415428Sphk * we are being booted by the new uniform boot code. 4953284Srgrimes */ 4963384Srgrimes cmpl $0,24(%ebp) 49715392Sphk je newboot 4983284Srgrimes 4993284Srgrimes /* 5003284Srgrimes * Seems we have been loaded by the old diskless boot code, we 5013284Srgrimes * don't stand a chance of running as the diskless structure 5023284Srgrimes * changed considerably between the two, so just halt. 5033284Srgrimes */ 5043284Srgrimes hlt 5053284Srgrimes 5063284Srgrimes /* 5073384Srgrimes * We have been loaded by the new uniform boot code. 50815428Sphk * Let's check the bootinfo version, and if we do not understand 5093384Srgrimes * it we return to the loader with a status of 1 to indicate this error 5103284Srgrimes */ 51115392Sphknewboot: 5123384Srgrimes movl 28(%ebp),%ebx /* &bootinfo.version */ 5135908Sbde movl BI_VERSION(%ebx),%eax 5143384Srgrimes cmpl $1,%eax /* We only understand version 1 */ 5153384Srgrimes je 1f 5163384Srgrimes movl $1,%eax /* Return status */ 5173384Srgrimes leave 51815428Sphk /* 51915428Sphk * XXX this returns to our caller's caller (as is required) since 52015428Sphk * we didn't set up a frame and our caller did. 52115428Sphk */ 5223384Srgrimes ret 5233284Srgrimes 5243384Srgrimes1: 5253284Srgrimes /* 5263384Srgrimes * If we have a kernelname copy it in 5273384Srgrimes */ 5285908Sbde movl BI_KERNELNAME(%ebx),%esi 5293384Srgrimes cmpl $0,%esi 5309344Sdg je 2f /* No kernelname */ 5319344Sdg movl $MAXPATHLEN,%ecx /* Brute force!!! */ 53215926Sphk movl $R(_kernelname),%edi 5339344Sdg cmpb $'/',(%esi) /* Make sure it starts with a slash */ 5349344Sdg je 1f 5359344Sdg movb $'/',(%edi) 5369344Sdg incl %edi 5379344Sdg decl %ecx 5389344Sdg1: 5393384Srgrimes cld 5403384Srgrimes rep 5413384Srgrimes movsb 5423384Srgrimes 5439344Sdg2: 54415428Sphk /* 5455908Sbde * Determine the size of the boot loader's copy of the bootinfo 5465908Sbde * struct. This is impossible to do properly because old versions 5475908Sbde * of the struct don't contain a size field and there are 2 old 5485908Sbde * versions with the same version number. 5494600Sphk */ 5505908Sbde movl $BI_ENDCOMMON,%ecx /* prepare for sizeless version */ 5515908Sbde testl $RB_BOOTINFO,8(%ebp) /* bi_size (and bootinfo) valid? */ 5525908Sbde je got_bi_size /* no, sizeless version */ 5535908Sbde movl BI_SIZE(%ebx),%ecx 5545908Sbdegot_bi_size: 5555908Sbde 55615428Sphk /* 5575908Sbde * Copy the common part of the bootinfo struct 5585908Sbde */ 5594600Sphk movl %ebx,%esi 56015926Sphk movl $R(_bootinfo),%edi 5615908Sbde cmpl $BOOTINFO_SIZE,%ecx 5625908Sbde jbe got_common_bi_size 5634600Sphk movl $BOOTINFO_SIZE,%ecx 5645908Sbdegot_common_bi_size: 5654600Sphk cld 5664600Sphk rep 5674600Sphk movsb 5684600Sphk 5693426Srgrimes#ifdef NFS 57025837Stegge#ifndef BOOTP_NFSV3 5713384Srgrimes /* 5723384Srgrimes * If we have a nfs_diskless structure copy it in 5733384Srgrimes */ 5745908Sbde movl BI_NFS_DISKLESS(%ebx),%esi 5753384Srgrimes cmpl $0,%esi 57615428Sphk je olddiskboot 57715926Sphk movl $R(_nfs_diskless),%edi 5783384Srgrimes movl $NFSDISKLESS_SIZE,%ecx 5793384Srgrimes cld 5803384Srgrimes rep 5813384Srgrimes movsb 58215926Sphk movl $R(_nfs_diskless_valid),%edi 5833795Sphk movl $1,(%edi) 5843406Sdg#endif 58525837Stegge#endif 5863384Srgrimes 5873384Srgrimes /* 5883284Srgrimes * The old style disk boot. 5893284Srgrimes * (*btext)(howto, bootdev, cyloffset, esym); 5903384Srgrimes * Note that the newer boot code just falls into here to pick 5913384Srgrimes * up howto and bootdev, cyloffset and esym are no longer used 5923284Srgrimes */ 59315392Sphkolddiskboot: 5943384Srgrimes movl 8(%ebp),%eax 59515926Sphk movl %eax,R(_boothowto) 5963384Srgrimes movl 12(%ebp),%eax 59715926Sphk movl %eax,R(_bootdev) 5982783Ssos 59918702Sjkh#if defined(USERCONFIG_BOOT) && defined(USERCONFIG) 60018702Sjkh movl $0x10200, %esi 60119772Sjkh movl $R(_userconfig_from_boot),%edi 60218702Sjkh movl $512,%ecx 60318702Sjkh cld 60418702Sjkh rep 60518702Sjkh movsb 60618702Sjkh#endif /* USERCONFIG_BOOT */ 60718702Sjkh 60815392Sphk ret 6093258Sdg 6101321Sdg 61115392Sphk/********************************************************************** 61215392Sphk * 61315392Sphk * Identify the CPU and initialize anything special about it 61415392Sphk * 61515392Sphk */ 61615392Sphkidentify_cpu: 61715392Sphk 6181998Swollman /* Try to toggle alignment check flag; does not exist on 386. */ 6191998Swollman pushfl 6201998Swollman popl %eax 6211998Swollman movl %eax,%ecx 6221998Swollman orl $PSL_AC,%eax 6231998Swollman pushl %eax 6241998Swollman popfl 6251998Swollman pushfl 6261998Swollman popl %eax 6271998Swollman xorl %ecx,%eax 6281998Swollman andl $PSL_AC,%eax 6291998Swollman pushl %ecx 6301998Swollman popfl 6311998Swollman 6321998Swollman testl %eax,%eax 63324112Skato jnz try486 63424112Skato 63524112Skato /* NexGen CPU does not have aligment check flag. */ 63624112Skato pushfl 63724112Skato movl $0x5555, %eax 63824112Skato xorl %edx, %edx 63924112Skato movl $2, %ecx 64024112Skato clc 64124112Skato divl %ecx 64224112Skato jz trynexgen 64324112Skato popfl 64415926Sphk movl $CPU_386,R(_cpu) 64513081Sdg jmp 3f 6461998Swollman 64724112Skatotrynexgen: 64827424Skato popfl 64924112Skato movl $CPU_NX586,R(_cpu) 65024112Skato movl $0x4778654e,R(_cpu_vendor) # store vendor string 65124112Skato movl $0x72446e65,R(_cpu_vendor+4) 65224112Skato movl $0x6e657669,R(_cpu_vendor+8) 65324112Skato movl $0,R(_cpu_vendor+12) 65424112Skato jmp 3f 65524112Skato 65624112Skatotry486: /* Try to toggle identification flag; does not exist on early 486s. */ 6571998Swollman pushfl 6581998Swollman popl %eax 6591998Swollman movl %eax,%ecx 6601998Swollman xorl $PSL_ID,%eax 6611998Swollman pushl %eax 6621998Swollman popfl 6631998Swollman pushfl 6641998Swollman popl %eax 6651998Swollman xorl %ecx,%eax 6661998Swollman andl $PSL_ID,%eax 6671998Swollman pushl %ecx 6681998Swollman popfl 6691998Swollman 6701998Swollman testl %eax,%eax 67124112Skato jnz trycpuid 67215926Sphk movl $CPU_486,R(_cpu) 6732495Spst 67424112Skato /* 67524112Skato * Check Cyrix CPU 67624112Skato * Cyrix CPUs do not change the undefined flags following 67724112Skato * execution of the divide instruction which divides 5 by 2. 67824112Skato * 67924112Skato * Note: CPUID is enabled on M2, so it passes another way. 68024112Skato */ 68124112Skato pushfl 68224112Skato movl $0x5555, %eax 68324112Skato xorl %edx, %edx 68424112Skato movl $2, %ecx 68524112Skato clc 68624112Skato divl %ecx 68724112Skato jnc trycyrix 68824112Skato popfl 68924112Skato jmp 3f /* You may use Intel CPU. */ 6902495Spst 69124112Skatotrycyrix: 69224112Skato popfl 69324112Skato /* 69424112Skato * IBM Bluelighting CPU also doesn't change the undefined flags. 69524112Skato * Because IBM doesn't disclose the information for Bluelighting 69624112Skato * CPU, we couldn't distinguish it from Cyrix's (including IBM 69724112Skato * brand of Cyrix CPUs). 69824112Skato */ 69915926Sphk movl $0x69727943,R(_cpu_vendor) # store vendor string 70024112Skato movl $0x736e4978,R(_cpu_vendor+4) 70124112Skato movl $0x64616574,R(_cpu_vendor+8) 70213014Sdg jmp 3f 7031998Swollman 70424112Skatotrycpuid: /* Use the `cpuid' instruction. */ 7051998Swollman xorl %eax,%eax 7066308Sphk .byte 0x0f,0xa2 # cpuid 0 70715926Sphk movl %eax,R(_cpu_high) # highest capability 70815926Sphk movl %ebx,R(_cpu_vendor) # store vendor string 70915926Sphk movl %edx,R(_cpu_vendor+4) 71015926Sphk movl %ecx,R(_cpu_vendor+8) 71115926Sphk movb $0,R(_cpu_vendor+12) 7121998Swollman 7131998Swollman movl $1,%eax 7146308Sphk .byte 0x0f,0xa2 # cpuid 1 71515926Sphk movl %eax,R(_cpu_id) # store cpu_id 71615926Sphk movl %edx,R(_cpu_feature) # store cpu_feature 7176308Sphk rorl $8,%eax # extract family type 7181998Swollman andl $15,%eax 7191998Swollman cmpl $5,%eax 7201998Swollman jae 1f 7211998Swollman 7221998Swollman /* less than Pentium; must be 486 */ 72315926Sphk movl $CPU_486,R(_cpu) 72413000Sdg jmp 3f 72513000Sdg1: 72613000Sdg /* a Pentium? */ 72713000Sdg cmpl $5,%eax 72813000Sdg jne 2f 72915926Sphk movl $CPU_586,R(_cpu) 73013000Sdg jmp 3f 731556Srgrimes2: 73213000Sdg /* Greater than Pentium...call it a Pentium Pro */ 73315926Sphk movl $CPU_686,R(_cpu) 73413000Sdg3: 73515392Sphk ret 736556Srgrimes 7374Srgrimes 73815392Sphk/********************************************************************** 739570Srgrimes * 74015428Sphk * Create the first page directory and its page tables. 74115392Sphk * 742570Srgrimes */ 743570Srgrimes 74415392Sphkcreate_pagetables: 74515392Sphk 74619621Sdyson testl $CPUID_PGE, R(_cpu_feature) 74719621Sdyson jz 1f 74819621Sdyson movl %cr4, %eax 74919621Sdyson orl $CR4_PGE, %eax 75019621Sdyson movl %eax, %cr4 75119621Sdyson1: 75219621Sdyson 75315428Sphk/* Find end of kernel image (rounded up to a page boundary). */ 75415392Sphk movl $R(_end),%esi 7554Srgrimes 75615392Sphk/* include symbols in "kernel image" if they are loaded and useful */ 7575908Sbde#ifdef DDB 75815392Sphk movl R(_bootinfo+BI_ESYMTAB),%edi 7595908Sbde testl %edi,%edi 76015428Sphk je over_symalloc 7615908Sbde movl %edi,%esi 7625908Sbde movl $KERNBASE,%edi 76315392Sphk addl %edi,R(_bootinfo+BI_SYMTAB) 76415392Sphk addl %edi,R(_bootinfo+BI_ESYMTAB) 76515428Sphkover_symalloc: 7665908Sbde#endif 7675908Sbde 76815565Sphk addl $PAGE_MASK,%esi 76915565Sphk andl $~PAGE_MASK,%esi 77015392Sphk movl %esi,R(_KERNend) /* save end of kernel */ 77115428Sphk movl %esi,R(physfree) /* next free page is at end of kernel */ 772608Srgrimes 77315392Sphk/* Allocate Kernel Page Tables */ 77415392Sphk ALLOCPAGES(NKPT) 77515392Sphk movl %esi,R(_KPTphys) 776757Sdg 77715392Sphk/* Allocate Page Table Directory */ 77815392Sphk ALLOCPAGES(1) 77915392Sphk movl %esi,R(_IdlePTD) 7804Srgrimes 78115392Sphk/* Allocate UPAGES */ 78215392Sphk ALLOCPAGES(UPAGES) 78317120Sbde movl %esi,R(p0upa) 78415392Sphk addl $KERNBASE, %esi 78515392Sphk movl %esi, R(_proc0paddr) 7864Srgrimes 78734840Sjlemon#ifdef VM86 78834840Sjlemon ALLOCPAGES(4) /* IOPAGES + ext + stack */ 78934840Sjlemon movl %esi,R(_vm86pa) 79034840Sjlemon addl $KERNBASE, %esi 79134840Sjlemon movl %esi, R(_vm86paddr) 79234840Sjlemon#endif /* VM86 */ 79334840Sjlemon 79426812Speter#ifdef SMP 79525164Speter/* Allocate cpu0's private data page */ 79625164Speter ALLOCPAGES(1) 79725164Speter movl %esi,R(cpu0pp) 79825164Speter addl $KERNBASE, %esi 79925164Speter movl %esi, R(_cpu0prvpage) /* relocated to KVM space */ 80025164Speter 80125164Speter/* Allocate cpu0's private page table for mapping priv page, apic, etc */ 80225164Speter ALLOCPAGES(1) 80325164Speter movl %esi,R(cpu0pt) 80425164Speter addl $KERNBASE, %esi 80525164Speter movl %esi, R(_cpu0prvpt) /* relocated to KVM space */ 80626812Speter#endif /* SMP */ 80725164Speter 80815392Sphk/* Map read-only from zero to the end of the kernel text section */ 80915565Sphk xorl %eax, %eax 81015428Sphk#ifdef BDE_DEBUGGER 81115428Sphk/* If the debugger is present, actually map everything read-write. */ 81215428Sphk cmpl $0,R(_bdb_exists) 81315428Sphk jne map_read_write 81415428Sphk#endif 81519621Sdyson xorl %edx,%edx 81627484Sdyson 81727484Sdyson#if !defined(SMP) 81819621Sdyson testl $CPUID_PGE, R(_cpu_feature) 81919621Sdyson jz 2f 82019621Sdyson orl $PG_G,%edx 82127484Sdyson#endif 82219621Sdyson 82319621Sdyson2: movl $R(_etext),%ecx 82415565Sphk addl $PAGE_MASK,%ecx 82515565Sphk shrl $PAGE_SHIFT,%ecx 82619621Sdyson fillkptphys(%edx) 827757Sdg 82815392Sphk/* Map read-write, data, bss and symbols */ 82915565Sphk movl $R(_etext),%eax 83015694Sphk addl $PAGE_MASK, %eax 83115694Sphk andl $~PAGE_MASK, %eax 83215428Sphkmap_read_write: 83319621Sdyson movl $PG_RW,%edx 83427484Sdyson#if !defined(SMP) 83519621Sdyson testl $CPUID_PGE, R(_cpu_feature) 83619621Sdyson jz 1f 83719621Sdyson orl $PG_G,%edx 83827484Sdyson#endif 83919621Sdyson 84019621Sdyson1: movl R(_KERNend),%ecx 841757Sdg subl %eax,%ecx 84215543Sphk shrl $PAGE_SHIFT,%ecx 84319621Sdyson fillkptphys(%edx) 844757Sdg 84515428Sphk/* Map page directory. */ 84615392Sphk movl R(_IdlePTD), %eax 84715392Sphk movl $1, %ecx 84819621Sdyson fillkptphys($PG_RW) 849757Sdg 85017120Sbde/* Map proc0's UPAGES in the physical way ... */ 85117120Sbde movl R(p0upa), %eax 85215392Sphk movl $UPAGES, %ecx 85319621Sdyson fillkptphys($PG_RW) 8544Srgrimes 85515565Sphk/* Map ISA hole */ 85615565Sphk movl $ISA_HOLE_START, %eax 85715565Sphk movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx 85822130Sdg fillkptphys($PG_RW) 85915565Sphk 86034840Sjlemon#ifdef VM86 86134840Sjlemon/* Map space for the vm86 region */ 86234840Sjlemon movl R(_vm86pa), %eax 86334840Sjlemon movl $4, %ecx 86434840Sjlemon fillkptphys($PG_RW) 86534840Sjlemon 86634840Sjlemon/* Map page 0 into the vm86 page table */ 86734840Sjlemon movl $0, %eax 86834840Sjlemon movl $0, %ebx 86934840Sjlemon movl $1, %ecx 87034840Sjlemon fillkpt(R(_vm86pa), $PG_RW|PG_U) 87134840Sjlemon 87234840Sjlemon/* ...likewise for the ISA hole */ 87334840Sjlemon movl $ISA_HOLE_START, %eax 87434840Sjlemon movl $ISA_HOLE_START>>PAGE_SHIFT, %ebx 87534840Sjlemon movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx 87634840Sjlemon fillkpt(R(_vm86pa), $PG_RW|PG_U) 87734840Sjlemon#endif /* VM86 */ 87834840Sjlemon 87926812Speter#ifdef SMP 88026812Speter/* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */ 88125164Speter movl R(cpu0pp), %eax 88225164Speter movl $1, %ecx 88325164Speter fillkptphys($PG_RW) 88425164Speter 88526812Speter/* Map cpu0's private page table into global kmem FWIW */ 88625164Speter movl R(cpu0pt), %eax 88725164Speter movl $1, %ecx 88825164Speter fillkptphys($PG_RW) 88925164Speter 89026812Speter/* Map the private page into the private page table into private space */ 89125164Speter movl R(cpu0pp), %eax 89225164Speter movl $0, %ebx /* pte offset = 0 */ 89325164Speter movl $1, %ecx /* one private page coming right up */ 89425164Speter fillkpt(R(cpu0pt), $PG_RW) 89525164Speter 89626812Speter/* Map the page table page into private space */ 89726812Speter movl R(cpu0pt), %eax 89825164Speter movl $1, %ebx /* pte offset = 1 */ 89926812Speter movl $1, %ecx /* one private pt coming right up */ 90026812Speter fillkpt(R(cpu0pt), $PG_RW) 90125164Speter 90226812Speter/* ... and put the page table table in the pde. */ 90325164Speter movl R(cpu0pt), %eax 90425164Speter movl $MPPTDI, %ebx 90525164Speter movl $1, %ecx 90625164Speter fillkpt(R(_IdlePTD), $PG_RW) 90734840Sjlemon 90834840Sjlemon/* Fakeup VA for the local apic to allow early traps. */ 90934840Sjlemon ALLOCPAGES(1) 91034840Sjlemon movl %esi, %eax 91134840Sjlemon movl $2, %ebx /* pte offset = 2 */ 91234840Sjlemon movl $1, %ecx /* one private pt coming right up */ 91334840Sjlemon fillkpt(R(cpu0pt), $PG_RW) 91434840Sjlemon 91534840Sjlemon/* Initialize mp lock to allow early traps */ 91634840Sjlemon movl $1, R(_mp_lock) 91734840Sjlemon 91835072Speter/* Initialize my_idlePTD to IdlePTD */ 91934840Sjlemon movl R(cpu0pp), %eax 92034840Sjlemon movl R(_IdlePTD), %ecx 92135072Speter movl %ecx,GD_MY_IDLEPTD(%eax) 92234840Sjlemon 92326812Speter#endif /* SMP */ 92425164Speter 92515392Sphk/* install a pde for temporary double map of bottom of VA */ 92615392Sphk movl R(_KPTphys), %eax 92715565Sphk xorl %ebx, %ebx 92815565Sphk movl $1, %ecx 92919621Sdyson fillkpt(R(_IdlePTD), $PG_RW) 9304Srgrimes 93115392Sphk/* install pde's for pt's */ 93215392Sphk movl R(_KPTphys), %eax 93315565Sphk movl $KPTDI, %ebx 93415565Sphk movl $NKPT, %ecx 93519621Sdyson fillkpt(R(_IdlePTD), $PG_RW) 9364Srgrimes 93715392Sphk/* install a pde recursively mapping page directory as a page table */ 93815565Sphk movl R(_IdlePTD), %eax 93915565Sphk movl $PTDPTDI, %ebx 94015565Sphk movl $1,%ecx 94119621Sdyson fillkpt(R(_IdlePTD), $PG_RW) 9424Srgrimes 9434Srgrimes ret 94415428Sphk 94515428Sphk#ifdef BDE_DEBUGGER 94615428Sphkbdb_prepare_paging: 94715428Sphk cmpl $0,R(_bdb_exists) 94815428Sphk je bdb_prepare_paging_exit 94915428Sphk 95015428Sphk subl $6,%esp 95115428Sphk 95215428Sphk /* 95315428Sphk * Copy and convert debugger entries from the bootstrap gdt and idt 95415428Sphk * to the kernel gdt and idt. Everything is still in low memory. 95515428Sphk * Tracing continues to work after paging is enabled because the 95615428Sphk * low memory addresses remain valid until everything is relocated. 95715428Sphk * However, tracing through the setidt() that initializes the trace 95815428Sphk * trap will crash. 95915428Sphk */ 96015428Sphk sgdt (%esp) 96115428Sphk movl 2(%esp),%esi /* base address of bootstrap gdt */ 96215428Sphk movl $R(_gdt),%edi 96315428Sphk movl %edi,2(%esp) /* prepare to load kernel gdt */ 96415428Sphk movl $8*18/4,%ecx 96515428Sphk cld 96615428Sphk rep /* copy gdt */ 96715428Sphk movsl 96815428Sphk movl $R(_gdt),-8+2(%edi) /* adjust gdt self-ptr */ 96915428Sphk movb $0x92,-8+5(%edi) 97015428Sphk lgdt (%esp) 97115428Sphk 97215428Sphk sidt (%esp) 97315428Sphk movl 2(%esp),%esi /* base address of current idt */ 97415428Sphk movl 8+4(%esi),%eax /* convert dbg descriptor to ... */ 97515428Sphk movw 8(%esi),%ax 97615428Sphk movl %eax,R(bdb_dbg_ljmp+1) /* ... immediate offset ... */ 97715428Sphk movl 8+2(%esi),%eax 97815428Sphk movw %ax,R(bdb_dbg_ljmp+5) /* ... and selector for ljmp */ 97915428Sphk movl 24+4(%esi),%eax /* same for bpt descriptor */ 98015428Sphk movw 24(%esi),%ax 98115428Sphk movl %eax,R(bdb_bpt_ljmp+1) 98215428Sphk movl 24+2(%esi),%eax 98315428Sphk movw %ax,R(bdb_bpt_ljmp+5) 98415428Sphk movl $R(_idt),%edi 98515428Sphk movl %edi,2(%esp) /* prepare to load kernel idt */ 98615428Sphk movl $8*4/4,%ecx 98715428Sphk cld 98815428Sphk rep /* copy idt */ 98915428Sphk movsl 99015428Sphk lidt (%esp) 99115428Sphk 99215428Sphk addl $6,%esp 99315428Sphk 99415428Sphkbdb_prepare_paging_exit: 99515428Sphk ret 99615428Sphk 99715428Sphk/* Relocate debugger gdt entries and gdt and idt pointers. */ 99815428Sphkbdb_commit_paging: 99915428Sphk cmpl $0,_bdb_exists 100015428Sphk je bdb_commit_paging_exit 100115428Sphk 100215428Sphk movl $_gdt+8*9,%eax /* adjust slots 9-17 */ 100315428Sphk movl $9,%ecx 100415428Sphkreloc_gdt: 100515428Sphk movb $KERNBASE>>24,7(%eax) /* top byte of base addresses, was 0, */ 100615428Sphk addl $8,%eax /* now KERNBASE>>24 */ 100715428Sphk loop reloc_gdt 100815428Sphk 100915428Sphk subl $6,%esp 101015428Sphk sgdt (%esp) 101115428Sphk addl $KERNBASE,2(%esp) 101215428Sphk lgdt (%esp) 101315428Sphk sidt (%esp) 101415428Sphk addl $KERNBASE,2(%esp) 101515428Sphk lidt (%esp) 101615428Sphk addl $6,%esp 101715428Sphk 101815428Sphk int $3 101915428Sphk 102015428Sphkbdb_commit_paging_exit: 102115428Sphk ret 102215428Sphk 102315428Sphk#endif /* BDE_DEBUGGER */ 1024