locore.s revision 37272
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 3737272Sjmg * $Id: locore.s,v 1.109 1998/06/21 18:02:34 bde 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" 4813228Swollman#include "opt_ddb.h" 4937272Sjmg#include "opt_nfsroot.h" 5018702Sjkh#include "opt_userconfig.h" 5130265Speter#include "opt_vm86.h" 5214835Sbde 5314835Sbde#include <sys/syscall.h> 545908Sbde#include <sys/reboot.h> 554Srgrimes 5614835Sbde#include <machine/asmacros.h> 5714835Sbde#include <machine/cputypes.h> 5814835Sbde#include <machine/psl.h> 5915543Sphk#include <machine/pmap.h> 6014835Sbde#include <machine/specialreg.h> 6114835Sbde 6214835Sbde#include "assym.s" 6314835Sbde 644Srgrimes/* 65757Sdg * XXX 66757Sdg * 674Srgrimes * Note: This version greatly munged to avoid various assembler errors 684Srgrimes * that may be fixed in newer versions of gas. Perhaps newer versions 694Srgrimes * will have more pleasant appearance. 704Srgrimes */ 714Srgrimes 72200Sdg/* 734Srgrimes * PTmap is recursive pagemap at top of virtual address space. 744Srgrimes * Within PTmap, the page directory can be found (third indirection). 754Srgrimes */ 763861Sbde .globl _PTmap,_PTD,_PTDpde 7715543Sphk .set _PTmap,(PTDPTDI << PDRSHIFT) 7815543Sphk .set _PTD,_PTmap + (PTDPTDI * PAGE_SIZE) 79757Sdg .set _PTDpde,_PTD + (PTDPTDI * PDESIZE) 80592Srgrimes 813861Sbde/* 824Srgrimes * APTmap, APTD is the alternate recursive pagemap. 834Srgrimes * It's used when modifying another process's page tables. 844Srgrimes */ 85592Srgrimes .globl _APTmap,_APTD,_APTDpde 86592Srgrimes .set _APTmap,APTDPTDI << PDRSHIFT 8715543Sphk .set _APTD,_APTmap + (APTDPTDI * PAGE_SIZE) 88757Sdg .set _APTDpde,_PTD + (APTDPTDI * PDESIZE) 894Srgrimes 904Srgrimes/* 91556Srgrimes * Globals 92556Srgrimes */ 93556Srgrimes .data 9414835Sbde ALIGN_DATA /* just to be sure */ 95134Sdg 9625083Sjdp .globl HIDENAME(tmpstk) 9713729Sdg .space 0x2000 /* space for tmpstk - temporary stack */ 9825083SjdpHIDENAME(tmpstk): 993842Sdg 1003861Sbde .globl _boothowto,_bootdev 101134Sdg 10215565Sphk .globl _cpu,_cpu_vendor,_cpu_id,_bootinfo 1036308Sphk .globl _cpu_high, _cpu_feature 1042783Ssos 105757Sdg_cpu: .long 0 /* are we 386, 386sx, or 486 */ 1062216Sbde_cpu_id: .long 0 /* stepping ID */ 1076308Sphk_cpu_high: .long 0 /* highest arg to CPUID */ 1086308Sphk_cpu_feature: .long 0 /* features */ 1092216Sbde_cpu_vendor: .space 20 /* CPU origin code */ 1105908Sbde_bootinfo: .space BOOTINFO_SIZE /* bootinfo that we can handle */ 1114Srgrimes 112757Sdg_KERNend: .long 0 /* phys addr end of kernel (just after bss) */ 11315428Sphkphysfree: .long 0 /* phys addr of next free page */ 114757Sdg 11526812Speter#ifdef SMP 11625164Spetercpu0pp: .long 0 /* phys addr cpu0 private pg */ 11725164Spetercpu0pt: .long 0 /* phys addr cpu0 private pt */ 11825164Speter 11925164Speter .globl _cpu0prvpage,_cpu0prvpt 12025164Speter_cpu0prvpage: .long 0 /* relocated version */ 12125164Speter_cpu0prvpt: .long 0 /* relocated version */ 12226812Speter#endif /* SMP */ 12325164Speter 1243861Sbde .globl _IdlePTD 125757Sdg_IdlePTD: .long 0 /* phys addr of kernel PTD */ 1263861Sbde 12726812Speter#ifdef SMP 12826812Speter .globl _KPTphys 12926812Speter#endif 130757Sdg_KPTphys: .long 0 /* phys addr of kernel page tables */ 1314Srgrimes 132757Sdg .globl _proc0paddr 133757Sdg_proc0paddr: .long 0 /* address of proc 0 address space */ 13424693Speterp0upa: .long 0 /* phys addr of proc0's UPAGES */ 135134Sdg 13634840Sjlemon#ifdef VM86 13734840Sjlemon .globl _vm86paddr, _vm86pa 13834840Sjlemon_vm86paddr: .long 0 /* address of vm86 region */ 13934840Sjlemon_vm86pa: .long 0 /* phys addr of vm86 region */ 14034840Sjlemon#endif 14134840Sjlemon 14215428Sphk#ifdef BDE_DEBUGGER 14315428Sphk .globl _bdb_exists /* flag to indicate BDE debugger is present */ 14415428Sphk_bdb_exists: .long 0 14515428Sphk#endif 146718Swollman 14715428Sphk 14815392Sphk/********************************************************************** 14915392Sphk * 15015392Sphk * Some handy macros 15115392Sphk * 152556Srgrimes */ 153134Sdg 15415392Sphk#define R(foo) ((foo)-KERNBASE) 15515392Sphk 15615392Sphk#define ALLOCPAGES(foo) \ 15715392Sphk movl R(physfree), %esi ; \ 15815543Sphk movl $((foo)*PAGE_SIZE), %eax ; \ 15915392Sphk addl %esi, %eax ; \ 16015392Sphk movl %eax, R(physfree) ; \ 16115392Sphk movl %esi, %edi ; \ 16215543Sphk movl $((foo)*PAGE_SIZE),%ecx ; \ 16315392Sphk xorl %eax,%eax ; \ 16415428Sphk cld ; \ 16515428Sphk rep ; \ 16615428Sphk stosb 16715392Sphk 168134Sdg/* 16915392Sphk * fillkpt 17015565Sphk * eax = page frame address 17115565Sphk * ebx = index into page table 17215392Sphk * ecx = how many pages to map 17315565Sphk * base = base address of page dir/table 17415565Sphk * prot = protection bits 175134Sdg */ 17615565Sphk#define fillkpt(base, prot) \ 17719621Sdyson shll $2,%ebx ; \ 17819621Sdyson addl base,%ebx ; \ 17919621Sdyson orl $PG_V,%eax ; \ 18019621Sdyson orl prot,%eax ; \ 18115565Sphk1: movl %eax,(%ebx) ; \ 18215565Sphk addl $PAGE_SIZE,%eax ; /* increment physical address */ \ 18315565Sphk addl $4,%ebx ; /* next pte */ \ 18415428Sphk loop 1b 18515392Sphk 18615565Sphk/* 18715565Sphk * fillkptphys(prot) 18815565Sphk * eax = physical address 18915565Sphk * ecx = how many pages to map 19015565Sphk * prot = protection bits 19115565Sphk */ 19215565Sphk#define fillkptphys(prot) \ 19315565Sphk movl %eax, %ebx ; \ 19415565Sphk shrl $PAGE_SHIFT, %ebx ; \ 19515565Sphk fillkpt(R(_KPTphys), prot) 19615565Sphk 19715392Sphk .text 19815392Sphk/********************************************************************** 19915392Sphk * 20015392Sphk * This is where the bootblocks start us, set the ball rolling... 20115392Sphk * 20215392Sphk */ 2031321SdgNON_GPROF_ENTRY(btext) 2044Srgrimes 20524112Skato#ifdef PC98 20624112Skato jmp 1f 20724112Skato .globl _pc98_system_parameter 20824112Skato .org 0x400 20924112Skato_pc98_system_parameter: 21024112Skato .space 0x240 /* BIOS parameter block */ 21124112Skato1: 21224112Skato /* save SYSTEM PARAMETER for resume (NS/T or other) */ 21324112Skato movl $0xa1000,%esi 21424112Skato movl $0x100000,%edi 21524112Skato movl $0x0630,%ecx 21624112Skato cld 21724112Skato rep 21824112Skato movsb 21924112Skato#else /* IBM-PC */ 22015428Sphk#ifdef BDE_DEBUGGER 22115428Sphk#ifdef BIOS_STEALS_3K 22215428Sphk cmpl $0x0375c339,0x95504 22315428Sphk#else 22415428Sphk cmpl $0x0375c339,0x96104 /* XXX - debugger signature */ 22515428Sphk#endif 22615428Sphk jne 1f 22715428Sphk movb $1,R(_bdb_exists) 22815428Sphk1: 22915428Sphk#endif 23015428Sphk 23115392Sphk/* Tell the bios to warmboot next time */ 23215392Sphk movw $0x1234,0x472 23324112Skato#endif /* PC98 */ 23415392Sphk 23515428Sphk/* Set up a real frame in case the double return in newboot is executed. */ 2363384Srgrimes pushl %ebp 2373384Srgrimes movl %esp, %ebp 2383384Srgrimes 23915392Sphk/* Don't trust what the BIOS gives for eflags. */ 2405603Sbde pushl $PSL_KERNEL 2412486Sdg popfl 24215428Sphk 24315428Sphk/* 24415428Sphk * Don't trust what the BIOS gives for %fs and %gs. Trust the bootstrap 24515428Sphk * to set %cs, %ds, %es and %ss. 24615428Sphk */ 24715428Sphk mov %ds, %ax 2484217Sphk mov %ax, %fs 2494217Sphk mov %ax, %gs 2504217Sphk 25115392Sphk call recover_bootinfo 25215392Sphk 25315428Sphk/* Get onto a stack that we can trust. */ 25415428Sphk/* 25515428Sphk * XXX this step is delayed in case recover_bootinfo needs to return via 25615428Sphk * the old stack, but it need not be, since recover_bootinfo actually 25715428Sphk * returns via the old frame. 25815428Sphk */ 25925083Sjdp movl $R(HIDENAME(tmpstk)),%esp 26015392Sphk 26124112Skato#ifdef PC98 26224112Skato testb $0x02,0x100620 /* pc98_machine_type & M_EPSON_PC98 */ 26324112Skato jz 3f 26424112Skato cmpb $0x0b,0x100624 /* epson_machine_id <= 0x0b */ 26524112Skato ja 3f 26624112Skato 26724112Skato /* count up memory */ 26824112Skato movl $0x100000,%eax /* next, talley remaining memory */ 26924112Skato movl $0xFFF-0x100,%ecx 27024112Skato1: movl 0(%eax),%ebx /* save location to check */ 27124112Skato movl $0xa55a5aa5,0(%eax) /* write test pattern */ 27224112Skato cmpl $0xa55a5aa5,0(%eax) /* does not check yet for rollover */ 27324112Skato jne 2f 27424112Skato movl %ebx,0(%eax) /* restore memory */ 27524112Skato addl $PAGE_SIZE,%eax 27624112Skato loop 1b 27724112Skato2: subl $0x100000,%eax 27824112Skato shrl $17,%eax 27924112Skato movb %al,0x100401 28024112Skato3: 28124112Skato#endif 28224112Skato 28315392Sphk call identify_cpu 28415392Sphk 28515392Sphk/* clear bss */ 28615428Sphk/* 28717120Sbde * XXX this should be done a little earlier. 28815428Sphk * 28917120Sbde * XXX we don't check that there is memory for our bss and page tables 29017120Sbde * before using it. 29115428Sphk * 29215428Sphk * XXX the boot program somewhat bogusly clears the bss. We still have 29315428Sphk * to do it in case we were unzipped by kzipboot. Then the boot program 29415428Sphk * only clears kzipboot's bss. 29515428Sphk * 29615428Sphk * XXX the gdt and idt are still somewhere in the boot program. We 29715428Sphk * depend on the convention that the boot program is below 1MB and we 29815428Sphk * are above 1MB to keep the gdt and idt away from the bss and page 29917120Sbde * tables. The idt is only used if BDE_DEBUGGER is enabled. 30015428Sphk */ 30115392Sphk movl $R(_end),%ecx 30215392Sphk movl $R(_edata),%edi 30315392Sphk subl %edi,%ecx 30415392Sphk xorl %eax,%eax 30515428Sphk cld 30615428Sphk rep 30715428Sphk stosb 30815392Sphk 30915392Sphk#if NAPM > 0 31036596Smsmith#ifndef VM86 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 31736596Smsmith#endif 31815392Sphk 31915392Sphk call create_pagetables 32015392Sphk 32127993Sdyson#ifdef VM86 32227993Sdyson/* 32327993Sdyson * If the CPU has support for VME, turn it on. 32427993Sdyson */ 32527993Sdyson testl $CPUID_VME, R(_cpu_feature) 32627993Sdyson jz 1f 32727993Sdyson movl %cr4, %eax 32827993Sdyson orl $CR4_VME, %eax 32927993Sdyson movl %eax, %cr4 33027993Sdyson1: 33127993Sdyson#endif /* VM86 */ 33227993Sdyson 33315428Sphk#ifdef BDE_DEBUGGER 33415428Sphk/* 33515428Sphk * Adjust as much as possible for paging before enabling paging so that the 33615428Sphk * adjustments can be traced. 33715428Sphk */ 33815428Sphk call bdb_prepare_paging 33915428Sphk#endif 34015428Sphk 34115392Sphk/* Now enable paging */ 34215392Sphk movl R(_IdlePTD), %eax 34315392Sphk movl %eax,%cr3 /* load ptd addr into mmu */ 34415392Sphk movl %cr0,%eax /* get control word */ 34515392Sphk orl $CR0_PE|CR0_PG,%eax /* enable paging */ 34615392Sphk movl %eax,%cr0 /* and let's page NOW! */ 34715392Sphk 34815428Sphk#ifdef BDE_DEBUGGER 34915428Sphk/* 35015428Sphk * Complete the adjustments for paging so that we can keep tracing through 35117120Sbde * initi386() after the low (physical) addresses for the gdt and idt become 35215428Sphk * invalid. 35315428Sphk */ 35415428Sphk call bdb_commit_paging 35515428Sphk#endif 35615428Sphk 35715428Sphk pushl $begin /* jump to high virtualized address */ 35815392Sphk ret 35915392Sphk 36015392Sphk/* now running relocated at KERNBASE where the system is linked to run */ 36115392Sphkbegin: 36215392Sphk /* set up bootstrap stack */ 36324691Speter movl _proc0paddr,%esp /* location of in-kernel pages */ 36424691Speter addl $UPAGES*PAGE_SIZE,%esp /* bootstrap stack end location */ 36515392Sphk xorl %eax,%eax /* mark end of frames */ 36615392Sphk movl %eax,%ebp 36715392Sphk movl _proc0paddr,%eax 36815392Sphk movl _IdlePTD, %esi 36915392Sphk movl %esi,PCB_CR3(%eax) 37035072Speter movl $_proc0,_curproc 37115392Sphk 37215392Sphk movl physfree, %esi 37315392Sphk pushl %esi /* value of first for init386(first) */ 37415392Sphk call _init386 /* wire 386 chip for unix operation */ 37515392Sphk popl %esi 37615392Sphk 37715392Sphk .globl __ucodesel,__udatasel 37815392Sphk 37915392Sphk pushl $0 /* unused */ 38015392Sphk pushl __udatasel /* ss */ 38115392Sphk pushl $0 /* esp - filled in by execve() */ 38215392Sphk pushl $PSL_USER /* eflags (IOPL 0, int enab) */ 38315392Sphk pushl __ucodesel /* cs */ 38415392Sphk pushl $0 /* eip - filled in by execve() */ 38515392Sphk subl $(12*4),%esp /* space for rest of registers */ 38615392Sphk 38715392Sphk pushl %esp /* call main with frame pointer */ 38815392Sphk call _main /* autoconfiguration, mountroot etc */ 38915392Sphk 39024691Speter hlt /* never returns to here */ 39115392Sphk 39224691Speter/* 39324691Speter * When starting init, call this to configure the process for user 39424691Speter * mode. This will be inherited by other processes. 39524691Speter */ 39624691SpeterNON_GPROF_ENTRY(prepare_usermode) 39715392Sphk /* 39824112Skato * Now we've run main() and determined what cpu-type we are, we can 39915392Sphk * enable write protection and alignment checking on i486 cpus and 40015392Sphk * above. 40115392Sphk */ 40215392Sphk#if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU) 40315392Sphk cmpl $CPUCLASS_386,_cpu_class 40415392Sphk je 1f 40515392Sphk movl %cr0,%eax /* get control word */ 40615392Sphk orl $CR0_WP|CR0_AM,%eax /* enable i486 features */ 40715392Sphk movl %eax,%cr0 /* and do it */ 40815428Sphk1: 40915392Sphk#endif 41015392Sphk /* 41115392Sphk * on return from main(), we are process 1 41215392Sphk * set up address space and stack so that we can 'return' to user mode 41315392Sphk */ 41415392Sphk movl __ucodesel,%eax 41515392Sphk movl __udatasel,%ecx 41615392Sphk 41724691Speter#if 0 41815392Sphk movl %cx,%ds 41924691Speter#endif 42015392Sphk movl %cx,%es 42115392Sphk movl %ax,%fs /* double map cs to fs */ 42215392Sphk movl %cx,%gs /* and ds to gs */ 42324691Speter ret /* goto user! */ 42415392Sphk 42524691Speter 42615392Sphk#define LCALL(x,y) .byte 0x9a ; .long y ; .word x 42715392Sphk 42815392Sphk/* 42915392Sphk * Signal trampoline, copied to top of user stack 43015392Sphk */ 43115392SphkNON_GPROF_ENTRY(sigcode) 43215392Sphk call SIGF_HANDLER(%esp) 43315392Sphk lea SIGF_SC(%esp),%eax /* scp (the call may have clobbered the */ 43415392Sphk /* copy at 8(%esp)) */ 43515392Sphk pushl %eax 43615392Sphk pushl %eax /* junk to fake return address */ 43715392Sphk movl $SYS_sigreturn,%eax /* sigreturn() */ 43815392Sphk LCALL(0x7,0) /* enter kernel with args on stack */ 43915392Sphk hlt /* never gets here */ 44025083Sjdp ALIGN_TEXT 44115392Sphk_esigcode: 44215392Sphk 44315392Sphk .data 44415392Sphk .globl _szsigcode 44515392Sphk_szsigcode: 44615392Sphk .long _esigcode-_sigcode 44715428Sphk .text 44815392Sphk 44915392Sphk/********************************************************************** 45015392Sphk * 45115392Sphk * Recover the bootinfo passed to us from the boot program 45215392Sphk * 45315392Sphk */ 45415392Sphkrecover_bootinfo: 45515392Sphk /* 4563284Srgrimes * This code is called in different ways depending on what loaded 4573284Srgrimes * and started the kernel. This is used to detect how we get the 4583284Srgrimes * arguments from the other code and what we do with them. 4593284Srgrimes * 4603284Srgrimes * Old disk boot blocks: 4613284Srgrimes * (*btext)(howto, bootdev, cyloffset, esym); 4623284Srgrimes * [return address == 0, and can NOT be returned to] 4633284Srgrimes * [cyloffset was not supported by the FreeBSD boot code 4643284Srgrimes * and always passed in as 0] 4653284Srgrimes * [esym is also known as total in the boot code, and 4663284Srgrimes * was never properly supported by the FreeBSD boot code] 4673284Srgrimes * 4683284Srgrimes * Old diskless netboot code: 4693284Srgrimes * (*btext)(0,0,0,0,&nfsdiskless,0,0,0); 4703284Srgrimes * [return address != 0, and can NOT be returned to] 4713284Srgrimes * If we are being booted by this code it will NOT work, 4723284Srgrimes * so we are just going to halt if we find this case. 4733284Srgrimes * 4743284Srgrimes * New uniform boot code: 4753284Srgrimes * (*btext)(howto, bootdev, 0, 0, 0, &bootinfo) 4763284Srgrimes * [return address != 0, and can be returned to] 4773284Srgrimes * 4783284Srgrimes * There may seem to be a lot of wasted arguments in here, but 4793384Srgrimes * that is so the newer boot code can still load very old kernels 4803384Srgrimes * and old boot code can load new kernels. 4814Srgrimes */ 4823284Srgrimes 4833284Srgrimes /* 4843284Srgrimes * The old style disk boot blocks fake a frame on the stack and 4853284Srgrimes * did an lret to get here. The frame on the stack has a return 4863284Srgrimes * address of 0. 4873284Srgrimes */ 4883384Srgrimes cmpl $0,4(%ebp) 48915392Sphk je olddiskboot 4903284Srgrimes 4913284Srgrimes /* 4923284Srgrimes * We have some form of return address, so this is either the 4933284Srgrimes * old diskless netboot code, or the new uniform code. That can 49415428Sphk * be detected by looking at the 5th argument, if it is 0 49515428Sphk * we are being booted by the new uniform boot code. 4963284Srgrimes */ 4973384Srgrimes cmpl $0,24(%ebp) 49815392Sphk je newboot 4993284Srgrimes 5003284Srgrimes /* 5013284Srgrimes * Seems we have been loaded by the old diskless boot code, we 5023284Srgrimes * don't stand a chance of running as the diskless structure 5033284Srgrimes * changed considerably between the two, so just halt. 5043284Srgrimes */ 5053284Srgrimes hlt 5063284Srgrimes 5073284Srgrimes /* 5083384Srgrimes * We have been loaded by the new uniform boot code. 50915428Sphk * Let's check the bootinfo version, and if we do not understand 5103384Srgrimes * it we return to the loader with a status of 1 to indicate this error 5113284Srgrimes */ 51215392Sphknewboot: 5133384Srgrimes movl 28(%ebp),%ebx /* &bootinfo.version */ 5145908Sbde movl BI_VERSION(%ebx),%eax 5153384Srgrimes cmpl $1,%eax /* We only understand version 1 */ 5163384Srgrimes je 1f 5173384Srgrimes movl $1,%eax /* Return status */ 5183384Srgrimes leave 51915428Sphk /* 52015428Sphk * XXX this returns to our caller's caller (as is required) since 52115428Sphk * we didn't set up a frame and our caller did. 52215428Sphk */ 5233384Srgrimes ret 5243284Srgrimes 5253384Srgrimes1: 5263284Srgrimes /* 5273384Srgrimes * If we have a kernelname copy it in 5283384Srgrimes */ 5295908Sbde movl BI_KERNELNAME(%ebx),%esi 5303384Srgrimes cmpl $0,%esi 5319344Sdg je 2f /* No kernelname */ 5329344Sdg movl $MAXPATHLEN,%ecx /* Brute force!!! */ 53315926Sphk movl $R(_kernelname),%edi 5349344Sdg cmpb $'/',(%esi) /* Make sure it starts with a slash */ 5359344Sdg je 1f 5369344Sdg movb $'/',(%edi) 5379344Sdg incl %edi 5389344Sdg decl %ecx 5399344Sdg1: 5403384Srgrimes cld 5413384Srgrimes rep 5423384Srgrimes movsb 5433384Srgrimes 5449344Sdg2: 54515428Sphk /* 5465908Sbde * Determine the size of the boot loader's copy of the bootinfo 5475908Sbde * struct. This is impossible to do properly because old versions 5485908Sbde * of the struct don't contain a size field and there are 2 old 5495908Sbde * versions with the same version number. 5504600Sphk */ 5515908Sbde movl $BI_ENDCOMMON,%ecx /* prepare for sizeless version */ 5525908Sbde testl $RB_BOOTINFO,8(%ebp) /* bi_size (and bootinfo) valid? */ 5535908Sbde je got_bi_size /* no, sizeless version */ 5545908Sbde movl BI_SIZE(%ebx),%ecx 5555908Sbdegot_bi_size: 5565908Sbde 55715428Sphk /* 5585908Sbde * Copy the common part of the bootinfo struct 5595908Sbde */ 5604600Sphk movl %ebx,%esi 56115926Sphk movl $R(_bootinfo),%edi 5625908Sbde cmpl $BOOTINFO_SIZE,%ecx 5635908Sbde jbe got_common_bi_size 5644600Sphk movl $BOOTINFO_SIZE,%ecx 5655908Sbdegot_common_bi_size: 5664600Sphk cld 5674600Sphk rep 5684600Sphk movsb 5694600Sphk 5703426Srgrimes#ifdef NFS 57125837Stegge#ifndef BOOTP_NFSV3 5723384Srgrimes /* 5733384Srgrimes * If we have a nfs_diskless structure copy it in 5743384Srgrimes */ 5755908Sbde movl BI_NFS_DISKLESS(%ebx),%esi 5763384Srgrimes cmpl $0,%esi 57715428Sphk je olddiskboot 57815926Sphk movl $R(_nfs_diskless),%edi 5793384Srgrimes movl $NFSDISKLESS_SIZE,%ecx 5803384Srgrimes cld 5813384Srgrimes rep 5823384Srgrimes movsb 58315926Sphk movl $R(_nfs_diskless_valid),%edi 5843795Sphk movl $1,(%edi) 5853406Sdg#endif 58625837Stegge#endif 5873384Srgrimes 5883384Srgrimes /* 5893284Srgrimes * The old style disk boot. 5903284Srgrimes * (*btext)(howto, bootdev, cyloffset, esym); 5913384Srgrimes * Note that the newer boot code just falls into here to pick 5923384Srgrimes * up howto and bootdev, cyloffset and esym are no longer used 5933284Srgrimes */ 59415392Sphkolddiskboot: 5953384Srgrimes movl 8(%ebp),%eax 59615926Sphk movl %eax,R(_boothowto) 5973384Srgrimes movl 12(%ebp),%eax 59815926Sphk movl %eax,R(_bootdev) 5992783Ssos 60018702Sjkh#if defined(USERCONFIG_BOOT) && defined(USERCONFIG) 60118702Sjkh movl $0x10200, %esi 60219772Sjkh movl $R(_userconfig_from_boot),%edi 60318702Sjkh movl $512,%ecx 60418702Sjkh cld 60518702Sjkh rep 60618702Sjkh movsb 60718702Sjkh#endif /* USERCONFIG_BOOT */ 60818702Sjkh 60915392Sphk ret 6103258Sdg 6111321Sdg 61215392Sphk/********************************************************************** 61315392Sphk * 61415392Sphk * Identify the CPU and initialize anything special about it 61515392Sphk * 61615392Sphk */ 61715392Sphkidentify_cpu: 61815392Sphk 6191998Swollman /* Try to toggle alignment check flag; does not exist on 386. */ 6201998Swollman pushfl 6211998Swollman popl %eax 6221998Swollman movl %eax,%ecx 6231998Swollman orl $PSL_AC,%eax 6241998Swollman pushl %eax 6251998Swollman popfl 6261998Swollman pushfl 6271998Swollman popl %eax 6281998Swollman xorl %ecx,%eax 6291998Swollman andl $PSL_AC,%eax 6301998Swollman pushl %ecx 6311998Swollman popfl 6321998Swollman 6331998Swollman testl %eax,%eax 63424112Skato jnz try486 63524112Skato 63624112Skato /* NexGen CPU does not have aligment check flag. */ 63724112Skato pushfl 63824112Skato movl $0x5555, %eax 63924112Skato xorl %edx, %edx 64024112Skato movl $2, %ecx 64124112Skato clc 64224112Skato divl %ecx 64324112Skato jz trynexgen 64424112Skato popfl 64515926Sphk movl $CPU_386,R(_cpu) 64613081Sdg jmp 3f 6471998Swollman 64824112Skatotrynexgen: 64927424Skato popfl 65024112Skato movl $CPU_NX586,R(_cpu) 65124112Skato movl $0x4778654e,R(_cpu_vendor) # store vendor string 65224112Skato movl $0x72446e65,R(_cpu_vendor+4) 65324112Skato movl $0x6e657669,R(_cpu_vendor+8) 65424112Skato movl $0,R(_cpu_vendor+12) 65524112Skato jmp 3f 65624112Skato 65724112Skatotry486: /* Try to toggle identification flag; does not exist on early 486s. */ 6581998Swollman pushfl 6591998Swollman popl %eax 6601998Swollman movl %eax,%ecx 6611998Swollman xorl $PSL_ID,%eax 6621998Swollman pushl %eax 6631998Swollman popfl 6641998Swollman pushfl 6651998Swollman popl %eax 6661998Swollman xorl %ecx,%eax 6671998Swollman andl $PSL_ID,%eax 6681998Swollman pushl %ecx 6691998Swollman popfl 6701998Swollman 6711998Swollman testl %eax,%eax 67224112Skato jnz trycpuid 67315926Sphk movl $CPU_486,R(_cpu) 6742495Spst 67524112Skato /* 67624112Skato * Check Cyrix CPU 67724112Skato * Cyrix CPUs do not change the undefined flags following 67824112Skato * execution of the divide instruction which divides 5 by 2. 67924112Skato * 68024112Skato * Note: CPUID is enabled on M2, so it passes another way. 68124112Skato */ 68224112Skato pushfl 68324112Skato movl $0x5555, %eax 68424112Skato xorl %edx, %edx 68524112Skato movl $2, %ecx 68624112Skato clc 68724112Skato divl %ecx 68824112Skato jnc trycyrix 68924112Skato popfl 69024112Skato jmp 3f /* You may use Intel CPU. */ 6912495Spst 69224112Skatotrycyrix: 69324112Skato popfl 69424112Skato /* 69524112Skato * IBM Bluelighting CPU also doesn't change the undefined flags. 69624112Skato * Because IBM doesn't disclose the information for Bluelighting 69724112Skato * CPU, we couldn't distinguish it from Cyrix's (including IBM 69824112Skato * brand of Cyrix CPUs). 69924112Skato */ 70015926Sphk movl $0x69727943,R(_cpu_vendor) # store vendor string 70124112Skato movl $0x736e4978,R(_cpu_vendor+4) 70224112Skato movl $0x64616574,R(_cpu_vendor+8) 70313014Sdg jmp 3f 7041998Swollman 70524112Skatotrycpuid: /* Use the `cpuid' instruction. */ 7061998Swollman xorl %eax,%eax 7076308Sphk .byte 0x0f,0xa2 # cpuid 0 70815926Sphk movl %eax,R(_cpu_high) # highest capability 70915926Sphk movl %ebx,R(_cpu_vendor) # store vendor string 71015926Sphk movl %edx,R(_cpu_vendor+4) 71115926Sphk movl %ecx,R(_cpu_vendor+8) 71215926Sphk movb $0,R(_cpu_vendor+12) 7131998Swollman 7141998Swollman movl $1,%eax 7156308Sphk .byte 0x0f,0xa2 # cpuid 1 71615926Sphk movl %eax,R(_cpu_id) # store cpu_id 71715926Sphk movl %edx,R(_cpu_feature) # store cpu_feature 7186308Sphk rorl $8,%eax # extract family type 7191998Swollman andl $15,%eax 7201998Swollman cmpl $5,%eax 7211998Swollman jae 1f 7221998Swollman 7231998Swollman /* less than Pentium; must be 486 */ 72415926Sphk movl $CPU_486,R(_cpu) 72513000Sdg jmp 3f 72613000Sdg1: 72713000Sdg /* a Pentium? */ 72813000Sdg cmpl $5,%eax 72913000Sdg jne 2f 73015926Sphk movl $CPU_586,R(_cpu) 73113000Sdg jmp 3f 732556Srgrimes2: 73313000Sdg /* Greater than Pentium...call it a Pentium Pro */ 73415926Sphk movl $CPU_686,R(_cpu) 73513000Sdg3: 73615392Sphk ret 737556Srgrimes 7384Srgrimes 73915392Sphk/********************************************************************** 740570Srgrimes * 74115428Sphk * Create the first page directory and its page tables. 74215392Sphk * 743570Srgrimes */ 744570Srgrimes 74515392Sphkcreate_pagetables: 74615392Sphk 74719621Sdyson testl $CPUID_PGE, R(_cpu_feature) 74819621Sdyson jz 1f 74919621Sdyson movl %cr4, %eax 75019621Sdyson orl $CR4_PGE, %eax 75119621Sdyson movl %eax, %cr4 75219621Sdyson1: 75319621Sdyson 75415428Sphk/* Find end of kernel image (rounded up to a page boundary). */ 75515392Sphk movl $R(_end),%esi 7564Srgrimes 75715392Sphk/* include symbols in "kernel image" if they are loaded and useful */ 7585908Sbde#ifdef DDB 75915392Sphk movl R(_bootinfo+BI_ESYMTAB),%edi 7605908Sbde testl %edi,%edi 76115428Sphk je over_symalloc 7625908Sbde movl %edi,%esi 7635908Sbde movl $KERNBASE,%edi 76415392Sphk addl %edi,R(_bootinfo+BI_SYMTAB) 76515392Sphk addl %edi,R(_bootinfo+BI_ESYMTAB) 76615428Sphkover_symalloc: 7675908Sbde#endif 7685908Sbde 76915565Sphk addl $PAGE_MASK,%esi 77015565Sphk andl $~PAGE_MASK,%esi 77115392Sphk movl %esi,R(_KERNend) /* save end of kernel */ 77215428Sphk movl %esi,R(physfree) /* next free page is at end of kernel */ 773608Srgrimes 77415392Sphk/* Allocate Kernel Page Tables */ 77515392Sphk ALLOCPAGES(NKPT) 77615392Sphk movl %esi,R(_KPTphys) 777757Sdg 77815392Sphk/* Allocate Page Table Directory */ 77915392Sphk ALLOCPAGES(1) 78015392Sphk movl %esi,R(_IdlePTD) 7814Srgrimes 78215392Sphk/* Allocate UPAGES */ 78315392Sphk ALLOCPAGES(UPAGES) 78417120Sbde movl %esi,R(p0upa) 78515392Sphk addl $KERNBASE, %esi 78615392Sphk movl %esi, R(_proc0paddr) 7874Srgrimes 78834840Sjlemon#ifdef VM86 78934840Sjlemon ALLOCPAGES(4) /* IOPAGES + ext + stack */ 79034840Sjlemon movl %esi,R(_vm86pa) 79134840Sjlemon addl $KERNBASE, %esi 79234840Sjlemon movl %esi, R(_vm86paddr) 79334840Sjlemon#endif /* VM86 */ 79434840Sjlemon 79526812Speter#ifdef SMP 79625164Speter/* Allocate cpu0's private data page */ 79725164Speter ALLOCPAGES(1) 79825164Speter movl %esi,R(cpu0pp) 79925164Speter addl $KERNBASE, %esi 80025164Speter movl %esi, R(_cpu0prvpage) /* relocated to KVM space */ 80125164Speter 80225164Speter/* Allocate cpu0's private page table for mapping priv page, apic, etc */ 80325164Speter ALLOCPAGES(1) 80425164Speter movl %esi,R(cpu0pt) 80525164Speter addl $KERNBASE, %esi 80625164Speter movl %esi, R(_cpu0prvpt) /* relocated to KVM space */ 80726812Speter#endif /* SMP */ 80825164Speter 80915392Sphk/* Map read-only from zero to the end of the kernel text section */ 81015565Sphk xorl %eax, %eax 81115428Sphk#ifdef BDE_DEBUGGER 81215428Sphk/* If the debugger is present, actually map everything read-write. */ 81315428Sphk cmpl $0,R(_bdb_exists) 81415428Sphk jne map_read_write 81515428Sphk#endif 81619621Sdyson xorl %edx,%edx 81727484Sdyson 81827484Sdyson#if !defined(SMP) 81919621Sdyson testl $CPUID_PGE, R(_cpu_feature) 82019621Sdyson jz 2f 82119621Sdyson orl $PG_G,%edx 82227484Sdyson#endif 82319621Sdyson 82419621Sdyson2: movl $R(_etext),%ecx 82515565Sphk addl $PAGE_MASK,%ecx 82615565Sphk shrl $PAGE_SHIFT,%ecx 82719621Sdyson fillkptphys(%edx) 828757Sdg 82915392Sphk/* Map read-write, data, bss and symbols */ 83015565Sphk movl $R(_etext),%eax 83115694Sphk addl $PAGE_MASK, %eax 83215694Sphk andl $~PAGE_MASK, %eax 83315428Sphkmap_read_write: 83419621Sdyson movl $PG_RW,%edx 83527484Sdyson#if !defined(SMP) 83619621Sdyson testl $CPUID_PGE, R(_cpu_feature) 83719621Sdyson jz 1f 83819621Sdyson orl $PG_G,%edx 83927484Sdyson#endif 84019621Sdyson 84119621Sdyson1: movl R(_KERNend),%ecx 842757Sdg subl %eax,%ecx 84315543Sphk shrl $PAGE_SHIFT,%ecx 84419621Sdyson fillkptphys(%edx) 845757Sdg 84615428Sphk/* Map page directory. */ 84715392Sphk movl R(_IdlePTD), %eax 84815392Sphk movl $1, %ecx 84919621Sdyson fillkptphys($PG_RW) 850757Sdg 85117120Sbde/* Map proc0's UPAGES in the physical way ... */ 85217120Sbde movl R(p0upa), %eax 85315392Sphk movl $UPAGES, %ecx 85419621Sdyson fillkptphys($PG_RW) 8554Srgrimes 85615565Sphk/* Map ISA hole */ 85715565Sphk movl $ISA_HOLE_START, %eax 85815565Sphk movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx 85922130Sdg fillkptphys($PG_RW) 86015565Sphk 86134840Sjlemon#ifdef VM86 86234840Sjlemon/* Map space for the vm86 region */ 86334840Sjlemon movl R(_vm86pa), %eax 86434840Sjlemon movl $4, %ecx 86534840Sjlemon fillkptphys($PG_RW) 86634840Sjlemon 86734840Sjlemon/* Map page 0 into the vm86 page table */ 86834840Sjlemon movl $0, %eax 86934840Sjlemon movl $0, %ebx 87034840Sjlemon movl $1, %ecx 87134840Sjlemon fillkpt(R(_vm86pa), $PG_RW|PG_U) 87234840Sjlemon 87334840Sjlemon/* ...likewise for the ISA hole */ 87434840Sjlemon movl $ISA_HOLE_START, %eax 87534840Sjlemon movl $ISA_HOLE_START>>PAGE_SHIFT, %ebx 87634840Sjlemon movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx 87734840Sjlemon fillkpt(R(_vm86pa), $PG_RW|PG_U) 87834840Sjlemon#endif /* VM86 */ 87934840Sjlemon 88026812Speter#ifdef SMP 88126812Speter/* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */ 88225164Speter movl R(cpu0pp), %eax 88325164Speter movl $1, %ecx 88425164Speter fillkptphys($PG_RW) 88525164Speter 88626812Speter/* Map cpu0's private page table into global kmem FWIW */ 88725164Speter movl R(cpu0pt), %eax 88825164Speter movl $1, %ecx 88925164Speter fillkptphys($PG_RW) 89025164Speter 89126812Speter/* Map the private page into the private page table into private space */ 89225164Speter movl R(cpu0pp), %eax 89325164Speter movl $0, %ebx /* pte offset = 0 */ 89425164Speter movl $1, %ecx /* one private page coming right up */ 89525164Speter fillkpt(R(cpu0pt), $PG_RW) 89625164Speter 89726812Speter/* Map the page table page into private space */ 89826812Speter movl R(cpu0pt), %eax 89925164Speter movl $1, %ebx /* pte offset = 1 */ 90026812Speter movl $1, %ecx /* one private pt coming right up */ 90126812Speter fillkpt(R(cpu0pt), $PG_RW) 90225164Speter 90326812Speter/* ... and put the page table table in the pde. */ 90425164Speter movl R(cpu0pt), %eax 90525164Speter movl $MPPTDI, %ebx 90625164Speter movl $1, %ecx 90725164Speter fillkpt(R(_IdlePTD), $PG_RW) 90834840Sjlemon 90934840Sjlemon/* Fakeup VA for the local apic to allow early traps. */ 91034840Sjlemon ALLOCPAGES(1) 91134840Sjlemon movl %esi, %eax 91234840Sjlemon movl $2, %ebx /* pte offset = 2 */ 91334840Sjlemon movl $1, %ecx /* one private pt coming right up */ 91434840Sjlemon fillkpt(R(cpu0pt), $PG_RW) 91534840Sjlemon 91634840Sjlemon/* Initialize mp lock to allow early traps */ 91734840Sjlemon movl $1, R(_mp_lock) 91834840Sjlemon 91935072Speter/* Initialize my_idlePTD to IdlePTD */ 92034840Sjlemon movl R(cpu0pp), %eax 92134840Sjlemon movl R(_IdlePTD), %ecx 92235072Speter movl %ecx,GD_MY_IDLEPTD(%eax) 92334840Sjlemon 92426812Speter#endif /* SMP */ 92525164Speter 92615392Sphk/* install a pde for temporary double map of bottom of VA */ 92715392Sphk movl R(_KPTphys), %eax 92815565Sphk xorl %ebx, %ebx 92915565Sphk movl $1, %ecx 93019621Sdyson fillkpt(R(_IdlePTD), $PG_RW) 9314Srgrimes 93215392Sphk/* install pde's for pt's */ 93315392Sphk movl R(_KPTphys), %eax 93415565Sphk movl $KPTDI, %ebx 93515565Sphk movl $NKPT, %ecx 93619621Sdyson fillkpt(R(_IdlePTD), $PG_RW) 9374Srgrimes 93815392Sphk/* install a pde recursively mapping page directory as a page table */ 93915565Sphk movl R(_IdlePTD), %eax 94015565Sphk movl $PTDPTDI, %ebx 94115565Sphk movl $1,%ecx 94219621Sdyson fillkpt(R(_IdlePTD), $PG_RW) 9434Srgrimes 9444Srgrimes ret 94515428Sphk 94615428Sphk#ifdef BDE_DEBUGGER 94715428Sphkbdb_prepare_paging: 94815428Sphk cmpl $0,R(_bdb_exists) 94915428Sphk je bdb_prepare_paging_exit 95015428Sphk 95115428Sphk subl $6,%esp 95215428Sphk 95315428Sphk /* 95415428Sphk * Copy and convert debugger entries from the bootstrap gdt and idt 95515428Sphk * to the kernel gdt and idt. Everything is still in low memory. 95615428Sphk * Tracing continues to work after paging is enabled because the 95715428Sphk * low memory addresses remain valid until everything is relocated. 95815428Sphk * However, tracing through the setidt() that initializes the trace 95915428Sphk * trap will crash. 96015428Sphk */ 96115428Sphk sgdt (%esp) 96215428Sphk movl 2(%esp),%esi /* base address of bootstrap gdt */ 96315428Sphk movl $R(_gdt),%edi 96415428Sphk movl %edi,2(%esp) /* prepare to load kernel gdt */ 96515428Sphk movl $8*18/4,%ecx 96615428Sphk cld 96715428Sphk rep /* copy gdt */ 96815428Sphk movsl 96915428Sphk movl $R(_gdt),-8+2(%edi) /* adjust gdt self-ptr */ 97015428Sphk movb $0x92,-8+5(%edi) 97115428Sphk lgdt (%esp) 97215428Sphk 97315428Sphk sidt (%esp) 97415428Sphk movl 2(%esp),%esi /* base address of current idt */ 97515428Sphk movl 8+4(%esi),%eax /* convert dbg descriptor to ... */ 97615428Sphk movw 8(%esi),%ax 97715428Sphk movl %eax,R(bdb_dbg_ljmp+1) /* ... immediate offset ... */ 97815428Sphk movl 8+2(%esi),%eax 97915428Sphk movw %ax,R(bdb_dbg_ljmp+5) /* ... and selector for ljmp */ 98015428Sphk movl 24+4(%esi),%eax /* same for bpt descriptor */ 98115428Sphk movw 24(%esi),%ax 98215428Sphk movl %eax,R(bdb_bpt_ljmp+1) 98315428Sphk movl 24+2(%esi),%eax 98415428Sphk movw %ax,R(bdb_bpt_ljmp+5) 98515428Sphk movl $R(_idt),%edi 98615428Sphk movl %edi,2(%esp) /* prepare to load kernel idt */ 98715428Sphk movl $8*4/4,%ecx 98815428Sphk cld 98915428Sphk rep /* copy idt */ 99015428Sphk movsl 99115428Sphk lidt (%esp) 99215428Sphk 99315428Sphk addl $6,%esp 99415428Sphk 99515428Sphkbdb_prepare_paging_exit: 99615428Sphk ret 99715428Sphk 99815428Sphk/* Relocate debugger gdt entries and gdt and idt pointers. */ 99915428Sphkbdb_commit_paging: 100015428Sphk cmpl $0,_bdb_exists 100115428Sphk je bdb_commit_paging_exit 100215428Sphk 100315428Sphk movl $_gdt+8*9,%eax /* adjust slots 9-17 */ 100415428Sphk movl $9,%ecx 100515428Sphkreloc_gdt: 100615428Sphk movb $KERNBASE>>24,7(%eax) /* top byte of base addresses, was 0, */ 100715428Sphk addl $8,%eax /* now KERNBASE>>24 */ 100815428Sphk loop reloc_gdt 100915428Sphk 101015428Sphk subl $6,%esp 101115428Sphk sgdt (%esp) 101215428Sphk addl $KERNBASE,2(%esp) 101315428Sphk lgdt (%esp) 101415428Sphk sidt (%esp) 101515428Sphk addl $KERNBASE,2(%esp) 101615428Sphk lidt (%esp) 101715428Sphk addl $6,%esp 101815428Sphk 101915428Sphk int $3 102015428Sphk 102115428Sphkbdb_commit_paging_exit: 102215428Sphk ret 102315428Sphk 102415428Sphk#endif /* BDE_DEBUGGER */ 1025