locore.s revision 3842
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
373842Sdg *	$Id: locore.s,v 1.38 1994/10/22 17:51:46 phk Exp $
384Srgrimes */
394Srgrimes
404Srgrimes/*
41757Sdg * locore.s:	FreeBSD machine support for the Intel 386
42757Sdg *		originally from: locore.s, by William F. Jolitz
43757Sdg *
44757Sdg *		Substantially rewritten by David Greenman, Rod Grimes,
45757Sdg *			Bruce Evans, Wolfgang Solfrank, and many others.
464Srgrimes */
474Srgrimes
482056Swollman#include "npx.h"			/* for NNPX */
492056Swollman#include "assym.s"			/* system definitions */
502056Swollman#include <machine/psl.h>		/* processor status longword defs */
512056Swollman#include <machine/pte.h>		/* page table entry definitions */
522056Swollman#include <sys/errno.h>			/* error return codes */
532056Swollman#include <machine/specialreg.h>		/* x86 special registers */
542056Swollman#include <machine/cputypes.h>		/* x86 cpu type definitions */
552056Swollman#include <sys/syscall.h>		/* system call numbers */
562056Swollman#include <machine/asmacros.h>		/* miscellaneous asm macros */
573489Sphk#include "apm.h"
583489Sphk#if NAPM > 0
593258Sdg#define ASM
603258Sdg#include <machine/apm_bios.h>
613258Sdg#include <machine/apm_segments.h>
623489Sphk#endif /* NAPM */
634Srgrimes
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 */
76592Srgrimes	.globl	_PTmap,_PTD,_PTDpde,_Sysmap
77592Srgrimes	.set	_PTmap,PTDPTDI << PDRSHIFT
78592Srgrimes	.set	_PTD,_PTmap + (PTDPTDI * NBPG)
79757Sdg	.set	_PTDpde,_PTD + (PTDPTDI * PDESIZE)
80592Srgrimes
81757Sdg/* Sysmap is the base address of the kernel page tables */
82608Srgrimes	.set	_Sysmap,_PTmap + (KPTDI * NBPG)
834Srgrimes
844Srgrimes/*
854Srgrimes * APTmap, APTD is the alternate recursive pagemap.
864Srgrimes * It's used when modifying another process's page tables.
874Srgrimes */
88592Srgrimes	.globl	_APTmap,_APTD,_APTDpde
89592Srgrimes	.set	_APTmap,APTDPTDI << PDRSHIFT
90592Srgrimes	.set	_APTD,_APTmap + (APTDPTDI * NBPG)
91757Sdg	.set	_APTDpde,_PTD + (APTDPTDI * PDESIZE)
924Srgrimes
934Srgrimes/*
944Srgrimes * Access to each processes kernel stack is via a region of
954Srgrimes * per-process address space (at the beginning), immediatly above
964Srgrimes * the user process stack.
974Srgrimes */
98570Srgrimes	.set	_kstack,USRSTACK
99134Sdg	.globl	_kstack
1004Srgrimes
101556Srgrimes/*
102556Srgrimes * Globals
103556Srgrimes */
104556Srgrimes	.data
105134Sdg
1063842Sdg	.globl	tmpstk
1073842Sdg	.space	0x1000		/* space for tmpstk - temporary stack */
1083842Sdgtmpstk:
1093842Sdg	.long	0		/* for debugging tmpstk stack underflow */
1103842Sdg
111592Srgrimes	.globl	_boothowto,_bootdev,_curpcb
112134Sdg
1131998Swollman	.globl	_cpu,_cold,_atdevbase,_cpu_vendor,_cpu_id
1142783Ssos
1152783Ssos 	.globl	_video_mode_ptr
1162783Ssos
117757Sdg_cpu:	.long	0				/* are we 386, 386sx, or 486 */
1182216Sbde_cpu_id:	.long	0			/* stepping ID */
1192216Sbde_cpu_vendor:	.space	20			/* CPU origin code */
1202783Ssos_video_mode_ptr:	.long 0
121757Sdg_cold:	.long	1				/* cold till we are not */
122757Sdg_atdevbase:	.long	0			/* location of start of iomem in virtual */
123757Sdg_atdevphys:	.long	0			/* location of device mapping ptes (phys) */
1244Srgrimes
125757Sdg	.globl	_KERNend
126757Sdg_KERNend:	.long	0			/* phys addr end of kernel (just after bss) */
127757Sdg
128592Srgrimes	.globl	_IdlePTD,_KPTphys
129757Sdg_IdlePTD:	.long	0			/* phys addr of kernel PTD */
130757Sdg_KPTphys:	.long	0			/* phys addr of kernel page tables */
1314Srgrimes
132757Sdg	.globl	_proc0paddr
133757Sdg_proc0paddr:	.long	0			/* address of proc 0 address space */
134134Sdg
135757Sdg#ifdef BDE_DEBUGGER
136757Sdg	.globl	_bdb_exists			/* flag to indicate BDE debugger is available */
1371321Sdg_bdb_exists:	.long	0
138757Sdg#endif
1393489Sphk#if NAPM > 0
1403258Sdg	.globl	_apm_current_gdt_pdesc		/* current GDT pseudo desc. */
1413258Sdg_apm_current_gdt_pdesc:
1423314Sdg	.word	0, 0, 0
143718Swollman
1443258Sdg	.globl	_bootstrap_gdt
1453258Sdg_bootstrap_gdt:
1463258Sdg	.space	SIZEOF_GDT * BOOTSTRAP_GDT_NUM
1473489Sphk#endif /* NAPM */
148134Sdg
149556Srgrimes/*
150556Srgrimes * System Initialization
151556Srgrimes */
1524Srgrimes	.text
153134Sdg
154134Sdg/*
155200Sdg * btext: beginning of text section.
156200Sdg * Also the entry point (jumped to directly from the boot blocks).
157134Sdg */
1581321SdgNON_GPROF_ENTRY(btext)
159757Sdg	movw	$0x1234,0x472			/* warm boot */
1604Srgrimes	jmp	1f
1613284Srgrimes	/*
1623284Srgrimes	 * XXX now that we load at 1MB is this still really used?
1633284Srgrimes	 */
1641321Sdg	.org	0x500				/* space for BIOS variables */
1654Srgrimes
1663284Srgrimes1:
1673384Srgrimes	/* Set up a real frame, some day we will be doing returns */
1683384Srgrimes	pushl	%ebp
1693384Srgrimes	movl	%esp, %ebp
1703384Srgrimes
1712512Sbde	/* Don't trust what the BIOS gives for eflags. */
1722512Sbde	pushl	$PSL_MBO
1732486Sdg	popfl
1742486Sdg
1754Srgrimes	/*
1763284Srgrimes	 * This code is called in different ways depending on what loaded
1773284Srgrimes	 * and started the kernel.  This is used to detect how we get the
1783284Srgrimes	 * arguments from the other code and what we do with them.
1793284Srgrimes	 *
1803284Srgrimes	 * Old disk boot blocks:
1813284Srgrimes	 *	(*btext)(howto, bootdev, cyloffset, esym);
1823284Srgrimes	 *	[return address == 0, and can NOT be returned to]
1833284Srgrimes	 *	[cyloffset was not supported by the FreeBSD boot code
1843284Srgrimes	 *	 and always passed in as 0]
1853284Srgrimes	 *	[esym is also known as total in the boot code, and
1863284Srgrimes	 *	 was never properly supported by the FreeBSD boot code]
1873284Srgrimes	 *
1883284Srgrimes	 * Old diskless netboot code:
1893284Srgrimes	 *	(*btext)(0,0,0,0,&nfsdiskless,0,0,0);
1903284Srgrimes	 *	[return address != 0, and can NOT be returned to]
1913284Srgrimes	 *	If we are being booted by this code it will NOT work,
1923284Srgrimes	 *	so we are just going to halt if we find this case.
1933284Srgrimes	 *
1943284Srgrimes	 * New uniform boot code:
1953284Srgrimes	 *	(*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
1963284Srgrimes	 *	[return address != 0, and can be returned to]
1973284Srgrimes	 *
1983284Srgrimes	 * There may seem to be a lot of wasted arguments in here, but
1993384Srgrimes	 * that is so the newer boot code can still load very old kernels
2003384Srgrimes	 * and old boot code can load new kernels.
2014Srgrimes	 */
2023284Srgrimes
2033284Srgrimes	/*
2043284Srgrimes	 * The old style disk boot blocks fake a frame on the stack and
2053284Srgrimes	 * did an lret to get here.  The frame on the stack has a return
2063284Srgrimes	 * address of 0.
2073284Srgrimes	 */
2083384Srgrimes	cmpl	$0,4(%ebp)
2093284Srgrimes	je	2f				/* olddiskboot: */
2103284Srgrimes
2113284Srgrimes	/*
2123284Srgrimes	 * We have some form of return address, so this is either the
2133284Srgrimes	 * old diskless netboot code, or the new uniform code.  That can
2143284Srgrimes	 * be detected by looking at the 5th argument, it if is 0 we
2153284Srgrimes	 * we are being booted by the new unifrom boot code.
2163284Srgrimes	 */
2173384Srgrimes	cmpl	$0,24(%ebp)
2183284Srgrimes	je	1f				/* newboot: */
2193284Srgrimes
2203284Srgrimes	/*
2213284Srgrimes	 * Seems we have been loaded by the old diskless boot code, we
2223284Srgrimes	 * don't stand a chance of running as the diskless structure
2233284Srgrimes	 * changed considerably between the two, so just halt.
2243284Srgrimes	 */
2253284Srgrimes	 hlt
2263284Srgrimes
2273284Srgrimes	/*
2283384Srgrimes	 * We have been loaded by the new uniform boot code.
2293384Srgrimes	 * Lets check the bootinfo version, and if we do not understand
2303384Srgrimes	 * it we return to the loader with a status of 1 to indicate this error
2313284Srgrimes	 */
2323284Srgrimes1:	/* newboot: */
2333384Srgrimes	movl	28(%ebp),%ebx		/* &bootinfo.version */
2343384Srgrimes	movl	BOOTINFO_VERSION(%ebx),%eax
2353384Srgrimes	cmpl	$1,%eax			/* We only understand version 1 */
2363384Srgrimes	je	1f
2373384Srgrimes	movl	$1,%eax			/* Return status */
2383384Srgrimes	leave
2393384Srgrimes	ret
2403284Srgrimes
2413384Srgrimes1:
2423284Srgrimes	/*
2433384Srgrimes	 * If we have a kernelname copy it in
2443384Srgrimes	 */
2453384Srgrimes	movl	BOOTINFO_KERNELNAME(%ebx),%esi
2463384Srgrimes	cmpl	$0,%esi
2473384Srgrimes	je	1f			/* No kernelname */
2483384Srgrimes	lea	_kernelname-KERNBASE,%edi
2493384Srgrimes	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
2503384Srgrimes	cld
2513384Srgrimes	rep
2523384Srgrimes	movsb
2533384Srgrimes
2543384Srgrimes1:
2553426Srgrimes#ifdef NFS
2563384Srgrimes	/*
2573384Srgrimes	 * If we have a nfs_diskless structure copy it in
2583384Srgrimes	 */
2593384Srgrimes	movl	BOOTINFO_NFS_DISKLESS(%ebx),%esi
2603384Srgrimes	cmpl	$0,%esi
2613384Srgrimes	je	2f
2623384Srgrimes	lea	_nfs_diskless-KERNBASE,%edi
2633384Srgrimes	movl	$NFSDISKLESS_SIZE,%ecx
2643384Srgrimes	cld
2653384Srgrimes	rep
2663384Srgrimes	movsb
2673795Sphk	lea	_nfs_diskless_valid-KERNBASE,%edi
2683795Sphk	movl	$1,(%edi)
2693406Sdg#endif
2703384Srgrimes
2713384Srgrimes	/*
2723284Srgrimes	 * The old style disk boot.
2733284Srgrimes	 *	(*btext)(howto, bootdev, cyloffset, esym);
2743384Srgrimes	 * Note that the newer boot code just falls into here to pick
2753384Srgrimes	 * up howto and bootdev, cyloffset and esym are no longer used
2763284Srgrimes	 */
2773284Srgrimes2:	/* olddiskboot: */
2783384Srgrimes	movl	8(%ebp),%eax
279570Srgrimes	movl	%eax,_boothowto-KERNBASE
2803384Srgrimes	movl	12(%ebp),%eax
281570Srgrimes	movl	%eax,_bootdev-KERNBASE
2822783Ssos
2832783Ssos	/* get the BIOS video mode pointer */
2842783Ssos 	movl	$0x4a8, %ecx
2852783Ssos 	movl	(%ecx), %eax
2862783Ssos 	movl	%eax, %ecx
2872783Ssos 	shrl	$12, %ecx
2882783Ssos 	andl	$0xffff0000, %ecx
2892783Ssos 	andl	$0x0000ffff, %eax
2902783Ssos 	orl	%ecx, %eax
2912783Ssos 	movl	(%eax), %eax
2922783Ssos 	movl	%eax, %ecx
2932783Ssos 	shrl	$12, %ecx
2942783Ssos 	andl	$0xffff0000, %ecx
2952783Ssos 	andl	$0x0000ffff, %eax
2962783Ssos 	orl	%ecx, %eax
2972783Ssos 	addl	$KERNBASE, %eax
2982783Ssos 	movl	%eax, _video_mode_ptr-KERNBASE
2992783Ssos
3003489Sphk#if NAPM > 0
3013258Sdg	/*
3023258Sdg	 * Setup APM BIOS:
3033258Sdg	 *
3043258Sdg	 * APM BIOS initialization should be done from real mode or V86 mode.
3053258Sdg	 *
3063258Sdg	 * (by HOSOKAWA, Tatsumi <hosokawa@mt.cs.keio.ac.jp>)
3073258Sdg	 */
3084Srgrimes
3093258Sdg	/*
3103258Sdg         * Copy APM initializer under 1MB boundary:
3113258Sdg	 *
3123258Sdg	 * APM initializer program must switch the CPU to real mode.
3133258Sdg	 * But FreeBSD kernel runs above 1MB boundary. So we must
3143258Sdg	 * copy the initializer code to conventional memory.
3153258Sdg	 */
3163258Sdg	movl	_apm_init_image_size-KERNBASE, %ecx	/* size */
3173258Sdg	lea	_apm_init_image-KERNBASE, %esi		/* source */
3183258Sdg	movl	$ APM_OURADDR, %edi			/* destination */
3193258Sdg	cld
3203258Sdg	rep
3213258Sdg	movsb
3223258Sdg
3233258Sdg	/* get GDT base */
3243258Sdg	sgdt	_apm_current_gdt_pdesc-KERNBASE
3253258Sdg
3263258Sdg	/* copy GDT to _bootstrap_gdt */
3273258Sdg	xorl	%ecx, %ecx
3283258Sdg	movw	_apm_current_gdt_pdesc-KERNBASE, %cx
3293258Sdg	movl	_apm_current_gdt_pdesc-KERNBASE+2, %esi
3303258Sdg	lea	_bootstrap_gdt-KERNBASE, %edi
3313258Sdg	cld
3323258Sdg	rep
3333258Sdg	movsb
3343258Sdg
3353258Sdg	/* setup GDT pseudo descriptor */
3363258Sdg	movw	$(SIZEOF_GDT*BOOTSTRAP_GDT_NUM), %ax
3373258Sdg	movw	%ax, _apm_current_gdt_pdesc-KERNBASE
3383258Sdg	leal	_bootstrap_gdt-KERNBASE, %eax
3393258Sdg	movl	%eax, _apm_current_gdt_pdesc-KERNBASE+2
3403258Sdg
3413258Sdg	/* load new GDTR */
3423258Sdg	lgdt	_apm_current_gdt_pdesc-KERNBASE
3433258Sdg
3443258Sdg	/* setup GDT for APM initializer */
3453258Sdg	lea	_bootstrap_gdt-KERNBASE, %ecx
3463258Sdg	movl	$(APM_OURADDR), %eax	/* use %ax for 15..0 */
3473258Sdg	movl	%eax, %ebx
3483258Sdg	shrl	$16, %ebx		/* use %bl for 23..16 */
3493258Sdg					/* use %bh for 31..24 */
3503258Sdg#define APM_SETUP_GDT(index, attrib) \
3513258Sdg	movl	$(index), %si ; \
3523258Sdg	lea	0(%ecx,%esi,8), %edx ; \
3533258Sdg	movw	$0xffff, (%edx) ; \
3543258Sdg	movw	%ax, 2(%edx) ; \
3553258Sdg	movb	%bl, 4(%edx) ; \
3563258Sdg	movw	$(attrib), 5(%edx) ; \
3573258Sdg	movb	%bh, 7(%edx)
3583258Sdg
3593258Sdg	APM_SETUP_GDT(APM_INIT_CS_INDEX  , CS32_ATTRIB)
3603258Sdg	APM_SETUP_GDT(APM_INIT_DS_INDEX  , DS32_ATTRIB)
3613258Sdg	APM_SETUP_GDT(APM_INIT_CS16_INDEX, CS16_ATTRIB)
3623258Sdg
3633258Sdg	/*
3643258Sdg	 * Call the initializer:
3653258Sdg	 *
3663258Sdg	 * direct intersegment call to conventional memory code
3673258Sdg	 */
3683258Sdg	.byte	0x9a		/* actually, lcall $APM_INIT_CS_SEL, $0 */
3693258Sdg	.long	0
3703258Sdg	.word	APM_INIT_CS_SEL
3713258Sdg
3723258Sdg	movw	%ax, _apm_version-KERNBASE
3733258Sdg	movl	%ebx, _apm_cs_entry-KERNBASE
3743258Sdg	movw	%cx, _apm_cs32_base-KERNBASE
3753258Sdg	shrl	$16, %ecx
3763258Sdg	movw	%cx, _apm_cs16_base-KERNBASE
3773258Sdg	movw	%dx, _apm_ds_base-KERNBASE
3783258Sdg	movw	%si, _apm_cs_limit-KERNBASE
3793258Sdg	shrl	$16, %esi
3803258Sdg	movw	%si, _apm_ds_limit-KERNBASE
3813258Sdg	movw	%di, _apm_flags-KERNBASE
3823489Sphk#endif /* NAPM */
3833258Sdg
3841998Swollman	/* Find out our CPU type. */
3851321Sdg
3861998Swollman	/* Try to toggle alignment check flag; does not exist on 386. */
3871998Swollman	pushfl
3881998Swollman	popl	%eax
3891998Swollman	movl	%eax,%ecx
3901998Swollman	orl	$PSL_AC,%eax
3911998Swollman	pushl	%eax
3921998Swollman	popfl
3931998Swollman	pushfl
3941998Swollman	popl	%eax
3951998Swollman	xorl	%ecx,%eax
3961998Swollman	andl	$PSL_AC,%eax
3971998Swollman	pushl	%ecx
3981998Swollman	popfl
3991998Swollman
4001998Swollman	testl	%eax,%eax
4011998Swollman	jnz	1f
4021998Swollman	movl	$CPU_386,_cpu-KERNBASE
403556Srgrimes	jmp	2f
4041998Swollman
4051998Swollman1:	/* Try to toggle identification flag; does not exist on early 486s. */
4061998Swollman	pushfl
4071998Swollman	popl	%eax
4081998Swollman	movl	%eax,%ecx
4091998Swollman	xorl	$PSL_ID,%eax
4101998Swollman	pushl	%eax
4111998Swollman	popfl
4121998Swollman	pushfl
4131998Swollman	popl	%eax
4141998Swollman	xorl	%ecx,%eax
4151998Swollman	andl	$PSL_ID,%eax
4161998Swollman	pushl	%ecx
4171998Swollman	popfl
4181998Swollman
4191998Swollman	testl	%eax,%eax
4201998Swollman	jnz	1f
4211998Swollman	movl	$CPU_486,_cpu-KERNBASE
4222495Spst
4232495Spst	/* check for Cyrix 486DLC -- based on check routine  */
4242495Spst	/* documented in "Cx486SLC/e SMM Programmer's Guide" */
4252495Spst	xorw	%dx,%dx
4262495Spst	cmpw	%dx,%dx			# set flags to known state
4272495Spst	pushfw
4282495Spst	popw	%cx			# store flags in ecx
4292495Spst	movw	$0xffff,%ax
4302495Spst	movw	$0x0004,%bx
4312495Spst	divw	%bx
4322495Spst	pushfw
4332495Spst	popw	%ax
4342495Spst	andw	$0x08d5,%ax		# mask off important bits
4352495Spst	andw	$0x08d5,%cx
4362495Spst	cmpw	%ax,%cx
4372495Spst
4382495Spst	jnz	2f			# if flags changed, Intel chip
4392495Spst
4402495Spst	movl	$CPU_486DLC,_cpu-KERNBASE # set CPU value for Cyrix
4412495Spst	movl	$0x69727943,_cpu_vendor-KERNBASE	# store vendor string
4422495Spst	movw	$0x0078,_cpu_vendor-KERNBASE+4
4432495Spst
4442495Spst	invd				# Start with guaranteed clean cache
4452495Spst	/* Disable caching of the ISA hole only. */
4462495Spst	movb	$CCR0,%al		# Configuration Register index (CCR0)
4472495Spst	outb	%al,$0x22
4482495Spst	inb	$0x23,%al
4493117Spst	orb	$(CCR0_NC1|CCR0_BARB),%al
4502495Spst	outb	%al,$0x23
4512495Spst	invd
4521998Swollman	jmp	2f
4531998Swollman
4541998Swollman1:	/* Use the `cpuid' instruction. */
4551998Swollman	xorl	%eax,%eax
4561998Swollman	.byte	0x0f,0xa2		# cpuid 0
4571998Swollman	movl	%ebx,_cpu_vendor-KERNBASE	# store vendor string
4581998Swollman	movl	%edx,_cpu_vendor+4-KERNBASE
4591998Swollman	movl	%ecx,_cpu_vendor+8-KERNBASE
4601998Swollman	movb	$0,_cpu_vendor+12-KERNBASE
4611998Swollman
4621998Swollman	movl	$1,%eax
4631998Swollman	.byte	0x0f,0xa2		# cpuid 1
4641998Swollman	movl	%eax,_cpu_id-KERNBASE		# store cpu_id
4651998Swollman	rorl	$8,%eax			# extract family type
4661998Swollman	andl	$15,%eax
4671998Swollman	cmpl	$5,%eax
4681998Swollman	jae	1f
4691998Swollman
4701998Swollman	/* less than Pentium; must be 486 */
4711998Swollman	movl	$CPU_486,_cpu-KERNBASE
4721998Swollman	jmp	2f
4731998Swollman
4741998Swollman1:	movl	$CPU_586,_cpu-KERNBASE
475556Srgrimes2:
476556Srgrimes
4774Srgrimes	/*
4784Srgrimes	 * Finished with old stack; load new %esp now instead of later so
4794Srgrimes	 * we can trace this code without having to worry about the trace
4804Srgrimes	 * trap clobbering the memory test or the zeroing of the bss+bootstrap
4814Srgrimes	 * page tables.
4824Srgrimes	 *
4834Srgrimes	 * XXX - wdboot clears the bss after testing that this is safe.
4844Srgrimes	 * This is too wasteful - memory below 640K is scarce.  The boot
4854Srgrimes	 * program should check:
4864Srgrimes	 *	text+data <= &stack_variable - more_space_for_stack
4874Srgrimes	 *	text+data+bss+pad+space_for_page_tables <= end_of_memory
4884Srgrimes	 * Oops, the gdt is in the carcass of the boot program so clearing
4894Srgrimes	 * the rest of memory is still not possible.
4904Srgrimes	 */
491757Sdg	movl	$tmpstk-KERNBASE,%esp		/* bootstrap stack end location */
4924Srgrimes
493570Srgrimes/*
494570Srgrimes * Virtual address space of kernel:
495570Srgrimes *
496570Srgrimes *	text | data | bss | [syms] | page dir | proc0 kernel stack | usr stk map | Sysmap
497974Sdg *      pages:                          1         UPAGES (2)             1         NKPT (7)
498570Srgrimes */
499570Srgrimes
5004Srgrimes/* find end of kernel image */
501570Srgrimes	movl	$_end-KERNBASE,%ecx
502757Sdg	addl	$NBPG-1,%ecx			/* page align up */
5034Srgrimes	andl	$~(NBPG-1),%ecx
5041321Sdg	movl	%ecx,%esi			/* esi = start of free memory */
505757Sdg	movl	%ecx,_KERNend-KERNBASE		/* save end of kernel */
5064Srgrimes
507757Sdg/* clear bss */
508570Srgrimes	movl	$_edata-KERNBASE,%edi
509760Srgrimes	subl	%edi,%ecx			/* get amount to clear */
510757Sdg	xorl	%eax,%eax			/* specify zero fill */
5114Srgrimes	cld
5124Srgrimes	rep
5134Srgrimes	stosb
5144Srgrimes
515608Srgrimes/*
516974Sdg * The value in esi is both the end of the kernel bss and a pointer to
517974Sdg * the kernel page directory, and is used by the rest of locore to build
518974Sdg * the tables.
519974Sdg * esi + 1(page dir) + 2(UPAGES) + 1(p0stack) + NKPT(number of kernel
520757Sdg * page table pages) is then passed on the stack to init386(first) as
521757Sdg * the value first. esi should ALWAYS be page aligned!!
522608Srgrimes */
523757Sdg	movl	%esi,%ecx			/* Get current first availiable address */
524608Srgrimes
525757Sdg/* clear pagetables, page directory, stack, etc... */
526757Sdg	movl	%esi,%edi			/* base (page directory) */
527974Sdg	movl	$((1+UPAGES+1+NKPT)*NBPG),%ecx	/* amount to clear */
528757Sdg	xorl	%eax,%eax			/* specify zero fill */
529757Sdg	cld
530757Sdg	rep
531757Sdg	stosb
532757Sdg
533757Sdg/* physical address of Idle proc/kernel page directory */
534570Srgrimes	movl	%esi,_IdlePTD-KERNBASE
5354Srgrimes
536592Srgrimes/*
537592Srgrimes * fillkpt
538592Srgrimes *	eax = (page frame address | control | status) == pte
539592Srgrimes *	ebx = address of page table
540592Srgrimes *	ecx = how many pages to map
541592Srgrimes */
5424Srgrimes#define	fillkpt		\
5434Srgrimes1:	movl	%eax,(%ebx)	; \
544570Srgrimes	addl	$NBPG,%eax	; /* increment physical address */ \
5454Srgrimes	addl	$4,%ebx		; /* next pte */ \
5464Srgrimes	loop	1b		;
5474Srgrimes
5484Srgrimes/*
5494Srgrimes * Map Kernel
5504Srgrimes *
5514Srgrimes * First step - build page tables
5524Srgrimes */
553757Sdg#if defined (KGDB) || defined (BDE_DEBUGGER)
554757Sdg	movl	_KERNend-KERNBASE,%ecx		/* this much memory, */
555757Sdg	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
556757Sdg#ifdef BDE_DEBUGGER
557757Sdg	cmpl	$0xa0,%ecx			/* XXX - cover debugger pages */
558200Sdg	jae	1f
559200Sdg	movl	$0xa0,%ecx
560200Sdg1:
561757Sdg#endif /* BDE_DEBUGGER */
5622512Sbde	movl	$PG_V|PG_KW,%eax		/* kernel R/W, valid */
563757Sdg	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
564757Sdg	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
5654Srgrimes	fillkpt
5664Srgrimes
567757Sdg#else /* !KGDB && !BDE_DEBUGGER */
568757Sdg	/* write protect kernel text (doesn't do a thing for 386's - only 486's) */
569757Sdg	movl	$_etext-KERNBASE,%ecx		/* get size of text */
5703842Sdg	addl	$NBPG-1,%ecx			/* round up to page */
571757Sdg	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
572757Sdg	movl	$PG_V|PG_KR,%eax		/* specify read only */
5733842Sdg#if 0
5743842Sdg	movl	$_etext,%ecx			/* get size of text */
5753842Sdg	subl	$_btext,%ecx
5763842Sdg	addl	$NBPG-1,%ecx			/* round up to page */
5773842Sdg	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
5783842Sdg	movl	$_btext-KERNBASE,%eax		/* get offset to physical memory */
5793842Sdg	orl	$PG_V|PG_KR,%eax		/* specify read only */
5803842Sdg#endif
581757Sdg	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
582757Sdg	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
583757Sdg	fillkpt
584757Sdg
585757Sdg	/* data and bss are r/w */
586757Sdg	andl	$PG_FRAME,%eax			/* strip to just addr of bss */
587757Sdg	movl	_KERNend-KERNBASE,%ecx		/* calculate size */
588757Sdg	subl	%eax,%ecx
589757Sdg	shrl	$PGSHIFT,%ecx
590757Sdg	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
591757Sdg	fillkpt
5921321Sdg#endif /* KGDB || BDE_DEBUGGER */
593757Sdg
594757Sdg/* now initialize the page dir, upages, p0stack PT, and page tables */
595757Sdg
596974Sdg	movl	$(1+UPAGES+1+NKPT),%ecx	/* number of PTEs */
597757Sdg	movl	%esi,%eax			/* phys address of PTD */
598757Sdg	andl	$PG_FRAME,%eax			/* convert to PFN, should be a NOP */
5992512Sbde	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
600757Sdg	movl	%esi,%ebx			/* calculate pte offset to ptd */
601757Sdg	shrl	$PGSHIFT-2,%ebx
602757Sdg	addl	%esi,%ebx			/* address of page directory */
603757Sdg	addl	$((1+UPAGES+1)*NBPG),%ebx	/* offset to kernel page tables */
604757Sdg	fillkpt
6051321Sdg
6064Srgrimes/* map I/O memory map */
6074Srgrimes
608757Sdg	movl    _KPTphys-KERNBASE,%ebx		/* base of kernel page tables */
609757Sdg	lea     (0xa0 * PTESIZE)(%ebx),%ebx	/* hardwire ISA hole at KERNBASE + 0xa0000 */
610757Sdg	movl	$0x100-0xa0,%ecx		/* for this many pte s, */
6111046Sdg	movl	$(0xa0000|PG_V|PG_KW|PG_N),%eax	/* valid, kernel read/write, non-cacheable */
612757Sdg	movl	%ebx,_atdevphys-KERNBASE	/* save phys addr of ptes */
6134Srgrimes	fillkpt
6144Srgrimes
6154Srgrimes /* map proc 0's kernel stack into user page table page */
6164Srgrimes
617757Sdg	movl	$UPAGES,%ecx			/* for this many pte s, */
618757Sdg	lea	(1*NBPG)(%esi),%eax		/* physical address in proc 0 */
619757Sdg	lea	(KERNBASE)(%eax),%edx		/* change into virtual addr */
620757Sdg	movl	%edx,_proc0paddr-KERNBASE	/* save VA for proc 0 init */
621757Sdg	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
622757Sdg	lea	((1+UPAGES)*NBPG)(%esi),%ebx	/* addr of stack page table in proc 0 */
623757Sdg	addl	$(KSTKPTEOFF * PTESIZE),%ebx	/* offset to kernel stack PTE */
6244Srgrimes	fillkpt
6254Srgrimes
6264Srgrimes/*
627757Sdg * Initialize kernel page table directory
6284Srgrimes */
6294Srgrimes	/* install a pde for temporary double map of bottom of VA */
630757Sdg	movl	_KPTphys-KERNBASE,%eax
631757Sdg	orl     $PG_V|PG_KW,%eax		/* valid, kernel read/write */
632757Sdg	movl	%eax,(%esi)			/* which is where temp maps! */
6334Srgrimes
634757Sdg	/* initialize kernel pde's */
635974Sdg	movl	$(NKPT),%ecx			/* for this many PDEs */
636757Sdg	lea	(KPTDI*PDESIZE)(%esi),%ebx	/* offset of pde for kernel */
6374Srgrimes	fillkpt
6384Srgrimes
6394Srgrimes	/* install a pde recursively mapping page directory as a page table! */
640757Sdg	movl	%esi,%eax			/* phys address of ptd in proc 0 */
641757Sdg	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
642757Sdg	movl	%eax,PTDPTDI*PDESIZE(%esi)	/* which is where PTmap maps! */
6434Srgrimes
6444Srgrimes	/* install a pde to map kernel stack for proc 0 */
645757Sdg	lea	((1+UPAGES)*NBPG)(%esi),%eax	/* physical address of pt in proc 0 */
646757Sdg	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
647757Sdg	movl	%eax,KSTKPTDI*PDESIZE(%esi)	/* which is where kernel stack maps! */
6484Srgrimes
649757Sdg#ifdef BDE_DEBUGGER
6504Srgrimes	/* copy and convert stuff from old gdt and idt for debugger */
6514Srgrimes
652757Sdg	cmpl	$0x0375c339,0x96104		/* XXX - debugger signature */
6534Srgrimes	jne	1f
654570Srgrimes	movb	$1,_bdb_exists-KERNBASE
6554Srgrimes1:
6564Srgrimes	pushal
6574Srgrimes	subl	$2*6,%esp
6584Srgrimes
6594Srgrimes	sgdt	(%esp)
660757Sdg	movl	2(%esp),%esi			/* base address of current gdt */
661570Srgrimes	movl	$_gdt-KERNBASE,%edi
6624Srgrimes	movl	%edi,2(%esp)
6634Srgrimes	movl	$8*18/4,%ecx
6641688Sdg	cld
665757Sdg	rep					/* copy gdt */
6664Srgrimes	movsl
667570Srgrimes	movl	$_gdt-KERNBASE,-8+2(%edi)	/* adjust gdt self-ptr */
6684Srgrimes	movb	$0x92,-8+5(%edi)
6694Srgrimes
6704Srgrimes	sidt	6(%esp)
671757Sdg	movl	6+2(%esp),%esi			/* base address of current idt */
672757Sdg	movl	8+4(%esi),%eax			/* convert dbg descriptor to ... */
6734Srgrimes	movw	8(%esi),%ax
674570Srgrimes	movl	%eax,bdb_dbg_ljmp+1-KERNBASE	/* ... immediate offset ... */
6754Srgrimes	movl	8+2(%esi),%eax
676570Srgrimes	movw	%ax,bdb_dbg_ljmp+5-KERNBASE	/* ... and selector for ljmp */
677757Sdg	movl	24+4(%esi),%eax			/* same for bpt descriptor */
6784Srgrimes	movw	24(%esi),%ax
679570Srgrimes	movl	%eax,bdb_bpt_ljmp+1-KERNBASE
6804Srgrimes	movl	24+2(%esi),%eax
681570Srgrimes	movw	%ax,bdb_bpt_ljmp+5-KERNBASE
6824Srgrimes
683570Srgrimes	movl	$_idt-KERNBASE,%edi
6844Srgrimes	movl	%edi,6+2(%esp)
6854Srgrimes	movl	$8*4/4,%ecx
6861688Sdg	cld
687757Sdg	rep					/* copy idt */
6884Srgrimes	movsl
6894Srgrimes
6904Srgrimes	lgdt	(%esp)
6914Srgrimes	lidt	6(%esp)
6924Srgrimes
6934Srgrimes	addl	$2*6,%esp
6944Srgrimes	popal
6951321Sdg#endif /* BDE_DEBUGGER */
6964Srgrimes
697592Srgrimes	/* load base of page directory and enable mapping */
698757Sdg	movl	%esi,%eax			/* phys address of ptd in proc 0 */
699757Sdg	movl	%eax,%cr3			/* load ptd addr into mmu */
700757Sdg	movl	%cr0,%eax			/* get control word */
701757Sdg	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
702757Sdg	movl	%eax,%cr0			/* and let's page NOW! */
7034Srgrimes
704757Sdg	pushl	$begin				/* jump to high mem */
7054Srgrimes	ret
7064Srgrimes
707570Srgrimesbegin: /* now running relocated at KERNBASE where the system is linked to run */
708757Sdg	movl	_atdevphys,%edx			/* get pte PA */
709757Sdg	subl	_KPTphys,%edx			/* remove base of ptes, now have phys offset */
710757Sdg	shll	$PGSHIFT-2,%edx			/* corresponding to virt offset */
711757Sdg	addl	$KERNBASE,%edx			/* add virtual base */
712570Srgrimes	movl	%edx,_atdevbase
7133728Sphk
7143728Sphk#if N_SC > 0
7153728Sphk	/* XXX: can't scinit relocate Crtat relative to atdevbase itself? */
7163728Sphk	.globl _Crtat				/* XXX - locore should not know about */
7173728Sphk	movl	_Crtat,%eax			/* variables of device drivers (pccons)! */
7183728Sphk	subl	$(KERNBASE+0xA0000),%eax
7194Srgrimes	addl	%eax,%edx
7204Srgrimes	movl	%edx,_Crtat
7213728Sphk#endif
7224Srgrimes
723757Sdg	/* set up bootstrap stack - 48 bytes */
724570Srgrimes	movl	$_kstack+UPAGES*NBPG-4*12,%esp	/* bootstrap stack end location */
725757Sdg	xorl	%eax,%eax			/* mark end of frames */
7264Srgrimes	movl	%eax,%ebp
727570Srgrimes	movl	_proc0paddr,%eax
728570Srgrimes	movl	%esi,PCB_CR3(%eax)
7294Srgrimes
730757Sdg#ifdef BDE_DEBUGGER
7314Srgrimes	/* relocate debugger gdt entries */
7324Srgrimes
733757Sdg	movl	$_gdt+8*9,%eax			/* adjust slots 9-17 */
7344Srgrimes	movl	$9,%ecx
7354Srgrimesreloc_gdt:
7361321Sdg	movb	$KERNBASE>>24,7(%eax)		/* top byte of base addresses, was 0, */
737757Sdg	addl	$8,%eax				/* now KERNBASE>>24 */
7384Srgrimes	loop	reloc_gdt
7394Srgrimes
7404Srgrimes	cmpl	$0,_bdb_exists
7414Srgrimes	je	1f
7424Srgrimes	int	$3
7434Srgrimes1:
7441321Sdg#endif /* BDE_DEBUGGER */
7454Srgrimes
746608Srgrimes	/*
747608Srgrimes	 * Skip over the page tables and the kernel stack
748608Srgrimes	 */
749974Sdg	lea	((1+UPAGES+1+NKPT)*NBPG)(%esi),%esi
750608Srgrimes
751757Sdg	pushl	%esi				/* value of first for init386(first) */
752757Sdg	call	_init386			/* wire 386 chip for unix operation */
7531549Srgrimes	popl	%esi
754200Sdg
7551549Srgrimes	.globl	__ucodesel,__udatasel
7561549Srgrimes
7571549Srgrimes	pushl	$0				/* unused */
7581549Srgrimes	pushl	__udatasel			/* ss */
7591549Srgrimes	pushl	$0				/* esp - filled in by execve() */
7602500Sdg	pushl	$PSL_USERSET			/* eflags (ring 0, int enab) */
7611549Srgrimes	pushl	__ucodesel			/* cs */
7621549Srgrimes	pushl	$0				/* eip - filled in by execve() */
7631549Srgrimes	subl	$(12*4),%esp			/* space for rest of registers */
7641549Srgrimes
7651549Srgrimes	pushl	%esp				/* call main with frame pointer */
766757Sdg	call	_main				/* autoconfiguration, mountroot etc */
7674Srgrimes
7681549Srgrimes	addl	$(13*4),%esp			/* back to a frame we can return with */
7691549Srgrimes
770134Sdg	/*
771570Srgrimes	 * now we've run main() and determined what cpu-type we are, we can
772570Srgrimes	 * enable WP mode on i486 cpus and above.
7731058Sdg	 */
7741058Sdg#if defined(I486_CPU) || defined(I586_CPU)
7751058Sdg	cmpl    $CPUCLASS_386,_cpu_class
7761058Sdg	je	1f
7771058Sdg	movl	%cr0,%eax			/* get control word */
7781058Sdg	orl	$CR0_WP,%eax			/* enable write protect for all modes */
7791058Sdg	movl	%eax,%cr0			/* and do it */
7801058Sdg#endif
7811058Sdg	/*
782134Sdg	 * on return from main(), we are process 1
783134Sdg	 * set up address space and stack so that we can 'return' to user mode
784134Sdg	 */
7851058Sdg1:
7864Srgrimes	movl	__ucodesel,%eax
7874Srgrimes	movl	__udatasel,%ecx
7881549Srgrimes
7894Srgrimes	movl	%cx,%ds
7904Srgrimes	movl	%cx,%es
791757Sdg	movl	%ax,%fs				/* double map cs to fs */
792757Sdg	movl	%cx,%gs				/* and ds to gs */
7931549Srgrimes	iret					/* goto user! */
7944Srgrimes
7951549Srgrimes#define LCALL(x,y)	.byte 0x9a ; .long y ; .word x
7964Srgrimes
797200SdgNON_GPROF_ENTRY(sigcode)
798592Srgrimes	call	SIGF_HANDLER(%esp)
799757Sdg	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
800757Sdg						/* copy at 8(%esp)) */
8014Srgrimes	pushl	%eax
802757Sdg	pushl	%eax				/* junk to fake return address */
803757Sdg	movl	$103,%eax			/* XXX sigreturn() */
804757Sdg	LCALL(0x7,0)				/* enter kernel with args on stack */
805757Sdg	hlt					/* never gets here */
8064Srgrimes
8074Srgrimes	.globl	_szsigcode
8084Srgrimes_szsigcode:
8094Srgrimes	.long	_szsigcode-_sigcode
810