locore.s revision 4031
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
374031Sjoerg *	$Id: locore.s,v 1.42 1994/10/30 20:09:12 bde 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 "assym.s"			/* system definitions */
492056Swollman#include <machine/psl.h>		/* processor status longword defs */
502056Swollman#include <machine/pte.h>		/* page table entry definitions */
512056Swollman#include <sys/errno.h>			/* error return codes */
522056Swollman#include <machine/specialreg.h>		/* x86 special registers */
532056Swollman#include <machine/cputypes.h>		/* x86 cpu type definitions */
542056Swollman#include <sys/syscall.h>		/* system call numbers */
552056Swollman#include <machine/asmacros.h>		/* miscellaneous asm macros */
563489Sphk#include "apm.h"
573489Sphk#if NAPM > 0
583258Sdg#define ASM
593258Sdg#include <machine/apm_bios.h>
603258Sdg#include <machine/apm_segments.h>
613489Sphk#endif /* NAPM */
624Srgrimes
634Srgrimes/*
64757Sdg *	XXX
65757Sdg *
664Srgrimes * Note: This version greatly munged to avoid various assembler errors
674Srgrimes * that may be fixed in newer versions of gas. Perhaps newer versions
684Srgrimes * will have more pleasant appearance.
694Srgrimes */
704Srgrimes
71200Sdg/*
724Srgrimes * PTmap is recursive pagemap at top of virtual address space.
734Srgrimes * Within PTmap, the page directory can be found (third indirection).
744Srgrimes */
753861Sbde	.globl	_PTmap,_PTD,_PTDpde
76592Srgrimes	.set	_PTmap,PTDPTDI << PDRSHIFT
77592Srgrimes	.set	_PTD,_PTmap + (PTDPTDI * NBPG)
78757Sdg	.set	_PTDpde,_PTD + (PTDPTDI * PDESIZE)
79592Srgrimes
803861Sbde/*
813861Sbde * Sysmap is the base address of the kernel page tables.
823861Sbde * It is a bogus interface for kgdb and isn't used by the kernel itself.
833861Sbde */
84608Srgrimes	.set	_Sysmap,_PTmap + (KPTDI * NBPG)
854Srgrimes
864Srgrimes/*
874Srgrimes * APTmap, APTD is the alternate recursive pagemap.
884Srgrimes * It's used when modifying another process's page tables.
894Srgrimes */
90592Srgrimes	.globl	_APTmap,_APTD,_APTDpde
91592Srgrimes	.set	_APTmap,APTDPTDI << PDRSHIFT
92592Srgrimes	.set	_APTD,_APTmap + (APTDPTDI * NBPG)
93757Sdg	.set	_APTDpde,_PTD + (APTDPTDI * PDESIZE)
944Srgrimes
954Srgrimes/*
964Srgrimes * Access to each processes kernel stack is via a region of
974Srgrimes * per-process address space (at the beginning), immediatly above
984Srgrimes * the user process stack.
994Srgrimes */
100570Srgrimes	.set	_kstack,USRSTACK
101134Sdg	.globl	_kstack
1024Srgrimes
103556Srgrimes/*
104556Srgrimes * Globals
105556Srgrimes */
106556Srgrimes	.data
107134Sdg
1083842Sdg	.globl	tmpstk
1093842Sdg	.space	0x1000		/* space for tmpstk - temporary stack */
1103842Sdgtmpstk:
1114012Sbde/*
1124012Sbde * Dummy frame at top of tmpstk to help debuggers print a nice stack trace.
1134012Sbde */
1144012Sbde	.long	tmpstk+8	/* caller's %ebp */
1154012Sbde	.long	_cpu_switch	/* caller */
1164012Sbde	.long	0		/* %ebp == 0 should terminate trace */
1174012Sbde	.long	_mvesp		/* in case %ebp == 0 doesn't work ... */
1184012Sbde	.long	0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555
1193842Sdg
1203861Sbde	.globl	_boothowto,_bootdev
121134Sdg
1221998Swollman	.globl	_cpu,_cold,_atdevbase,_cpu_vendor,_cpu_id
1232783Ssos
124757Sdg_cpu:	.long	0				/* are we 386, 386sx, or 486 */
1252216Sbde_cpu_id:	.long	0			/* stepping ID */
1262216Sbde_cpu_vendor:	.space	20			/* CPU origin code */
127757Sdg_cold:	.long	1				/* cold till we are not */
128757Sdg_atdevbase:	.long	0			/* location of start of iomem in virtual */
129757Sdg_atdevphys:	.long	0			/* location of device mapping ptes (phys) */
1304Srgrimes
131757Sdg_KERNend:	.long	0			/* phys addr end of kernel (just after bss) */
132757Sdg
1333861Sbde	.globl	_IdlePTD
134757Sdg_IdlePTD:	.long	0			/* phys addr of kernel PTD */
1353861Sbde
136757Sdg_KPTphys:	.long	0			/* phys addr of kernel page tables */
1374Srgrimes
138757Sdg	.globl	_proc0paddr
139757Sdg_proc0paddr:	.long	0			/* address of proc 0 address space */
140134Sdg
141757Sdg#ifdef BDE_DEBUGGER
142757Sdg	.globl	_bdb_exists			/* flag to indicate BDE debugger is available */
1431321Sdg_bdb_exists:	.long	0
144757Sdg#endif
1453489Sphk#if NAPM > 0
1463258Sdg	.globl	_apm_current_gdt_pdesc		/* current GDT pseudo desc. */
1473258Sdg_apm_current_gdt_pdesc:
1483314Sdg	.word	0, 0, 0
149718Swollman
1503258Sdg	.globl	_bootstrap_gdt
1513258Sdg_bootstrap_gdt:
1523258Sdg	.space	SIZEOF_GDT * BOOTSTRAP_GDT_NUM
1533489Sphk#endif /* NAPM */
154134Sdg
155556Srgrimes/*
156556Srgrimes * System Initialization
157556Srgrimes */
1584Srgrimes	.text
159134Sdg
160134Sdg/*
161200Sdg * btext: beginning of text section.
162200Sdg * Also the entry point (jumped to directly from the boot blocks).
163134Sdg */
1641321SdgNON_GPROF_ENTRY(btext)
165757Sdg	movw	$0x1234,0x472			/* warm boot */
1664Srgrimes	jmp	1f
1673284Srgrimes	/*
1683284Srgrimes	 * XXX now that we load at 1MB is this still really used?
1693284Srgrimes	 */
1701321Sdg	.org	0x500				/* space for BIOS variables */
1714Srgrimes
1723284Srgrimes1:
1733384Srgrimes	/* Set up a real frame, some day we will be doing returns */
1743384Srgrimes	pushl	%ebp
1753384Srgrimes	movl	%esp, %ebp
1763384Srgrimes
1772512Sbde	/* Don't trust what the BIOS gives for eflags. */
1782512Sbde	pushl	$PSL_MBO
1792486Sdg	popfl
1802486Sdg
1814Srgrimes	/*
1823284Srgrimes	 * This code is called in different ways depending on what loaded
1833284Srgrimes	 * and started the kernel.  This is used to detect how we get the
1843284Srgrimes	 * arguments from the other code and what we do with them.
1853284Srgrimes	 *
1863284Srgrimes	 * Old disk boot blocks:
1873284Srgrimes	 *	(*btext)(howto, bootdev, cyloffset, esym);
1883284Srgrimes	 *	[return address == 0, and can NOT be returned to]
1893284Srgrimes	 *	[cyloffset was not supported by the FreeBSD boot code
1903284Srgrimes	 *	 and always passed in as 0]
1913284Srgrimes	 *	[esym is also known as total in the boot code, and
1923284Srgrimes	 *	 was never properly supported by the FreeBSD boot code]
1933284Srgrimes	 *
1943284Srgrimes	 * Old diskless netboot code:
1953284Srgrimes	 *	(*btext)(0,0,0,0,&nfsdiskless,0,0,0);
1963284Srgrimes	 *	[return address != 0, and can NOT be returned to]
1973284Srgrimes	 *	If we are being booted by this code it will NOT work,
1983284Srgrimes	 *	so we are just going to halt if we find this case.
1993284Srgrimes	 *
2003284Srgrimes	 * New uniform boot code:
2013284Srgrimes	 *	(*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
2023284Srgrimes	 *	[return address != 0, and can be returned to]
2033284Srgrimes	 *
2043284Srgrimes	 * There may seem to be a lot of wasted arguments in here, but
2053384Srgrimes	 * that is so the newer boot code can still load very old kernels
2063384Srgrimes	 * and old boot code can load new kernels.
2074Srgrimes	 */
2083284Srgrimes
2093284Srgrimes	/*
2103284Srgrimes	 * The old style disk boot blocks fake a frame on the stack and
2113284Srgrimes	 * did an lret to get here.  The frame on the stack has a return
2123284Srgrimes	 * address of 0.
2133284Srgrimes	 */
2143384Srgrimes	cmpl	$0,4(%ebp)
2153284Srgrimes	je	2f				/* olddiskboot: */
2163284Srgrimes
2173284Srgrimes	/*
2183284Srgrimes	 * We have some form of return address, so this is either the
2193284Srgrimes	 * old diskless netboot code, or the new uniform code.  That can
2203284Srgrimes	 * be detected by looking at the 5th argument, it if is 0 we
2213284Srgrimes	 * we are being booted by the new unifrom boot code.
2223284Srgrimes	 */
2233384Srgrimes	cmpl	$0,24(%ebp)
2243284Srgrimes	je	1f				/* newboot: */
2253284Srgrimes
2263284Srgrimes	/*
2273284Srgrimes	 * Seems we have been loaded by the old diskless boot code, we
2283284Srgrimes	 * don't stand a chance of running as the diskless structure
2293284Srgrimes	 * changed considerably between the two, so just halt.
2303284Srgrimes	 */
2313284Srgrimes	 hlt
2323284Srgrimes
2333284Srgrimes	/*
2343384Srgrimes	 * We have been loaded by the new uniform boot code.
2353384Srgrimes	 * Lets check the bootinfo version, and if we do not understand
2363384Srgrimes	 * it we return to the loader with a status of 1 to indicate this error
2373284Srgrimes	 */
2383284Srgrimes1:	/* newboot: */
2393384Srgrimes	movl	28(%ebp),%ebx		/* &bootinfo.version */
2403384Srgrimes	movl	BOOTINFO_VERSION(%ebx),%eax
2413384Srgrimes	cmpl	$1,%eax			/* We only understand version 1 */
2423384Srgrimes	je	1f
2433384Srgrimes	movl	$1,%eax			/* Return status */
2443384Srgrimes	leave
2453384Srgrimes	ret
2463284Srgrimes
2473384Srgrimes1:
2483284Srgrimes	/*
2493384Srgrimes	 * If we have a kernelname copy it in
2503384Srgrimes	 */
2513384Srgrimes	movl	BOOTINFO_KERNELNAME(%ebx),%esi
2523384Srgrimes	cmpl	$0,%esi
2533384Srgrimes	je	1f			/* No kernelname */
2543384Srgrimes	lea	_kernelname-KERNBASE,%edi
2553384Srgrimes	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
2563384Srgrimes	cld
2573384Srgrimes	rep
2583384Srgrimes	movsb
2593384Srgrimes
2603384Srgrimes1:
2613426Srgrimes#ifdef NFS
2623384Srgrimes	/*
2633384Srgrimes	 * If we have a nfs_diskless structure copy it in
2643384Srgrimes	 */
2653384Srgrimes	movl	BOOTINFO_NFS_DISKLESS(%ebx),%esi
2663384Srgrimes	cmpl	$0,%esi
2673384Srgrimes	je	2f
2683384Srgrimes	lea	_nfs_diskless-KERNBASE,%edi
2693384Srgrimes	movl	$NFSDISKLESS_SIZE,%ecx
2703384Srgrimes	cld
2713384Srgrimes	rep
2723384Srgrimes	movsb
2733795Sphk	lea	_nfs_diskless_valid-KERNBASE,%edi
2743795Sphk	movl	$1,(%edi)
2753406Sdg#endif
2763384Srgrimes
2773384Srgrimes	/*
2783284Srgrimes	 * The old style disk boot.
2793284Srgrimes	 *	(*btext)(howto, bootdev, cyloffset, esym);
2803384Srgrimes	 * Note that the newer boot code just falls into here to pick
2813384Srgrimes	 * up howto and bootdev, cyloffset and esym are no longer used
2823284Srgrimes	 */
2833284Srgrimes2:	/* olddiskboot: */
2843384Srgrimes	movl	8(%ebp),%eax
285570Srgrimes	movl	%eax,_boothowto-KERNBASE
2863384Srgrimes	movl	12(%ebp),%eax
287570Srgrimes	movl	%eax,_bootdev-KERNBASE
2882783Ssos
2893489Sphk#if NAPM > 0
2903258Sdg	/*
2913258Sdg	 * Setup APM BIOS:
2923258Sdg	 *
2933258Sdg	 * APM BIOS initialization should be done from real mode or V86 mode.
2943258Sdg	 *
2953258Sdg	 * (by HOSOKAWA, Tatsumi <hosokawa@mt.cs.keio.ac.jp>)
2963258Sdg	 */
2974Srgrimes
2983258Sdg	/*
2993258Sdg         * Copy APM initializer under 1MB boundary:
3003258Sdg	 *
3013258Sdg	 * APM initializer program must switch the CPU to real mode.
3023258Sdg	 * But FreeBSD kernel runs above 1MB boundary. So we must
3033258Sdg	 * copy the initializer code to conventional memory.
3043258Sdg	 */
3053258Sdg	movl	_apm_init_image_size-KERNBASE, %ecx	/* size */
3063258Sdg	lea	_apm_init_image-KERNBASE, %esi		/* source */
3073258Sdg	movl	$ APM_OURADDR, %edi			/* destination */
3083258Sdg	cld
3093258Sdg	rep
3103258Sdg	movsb
3113258Sdg
3123258Sdg	/* get GDT base */
3133258Sdg	sgdt	_apm_current_gdt_pdesc-KERNBASE
3143258Sdg
3153258Sdg	/* copy GDT to _bootstrap_gdt */
3163258Sdg	xorl	%ecx, %ecx
3173258Sdg	movw	_apm_current_gdt_pdesc-KERNBASE, %cx
3183258Sdg	movl	_apm_current_gdt_pdesc-KERNBASE+2, %esi
3193258Sdg	lea	_bootstrap_gdt-KERNBASE, %edi
3203258Sdg	cld
3213258Sdg	rep
3223258Sdg	movsb
3233258Sdg
3243258Sdg	/* setup GDT pseudo descriptor */
3253258Sdg	movw	$(SIZEOF_GDT*BOOTSTRAP_GDT_NUM), %ax
3263258Sdg	movw	%ax, _apm_current_gdt_pdesc-KERNBASE
3273258Sdg	leal	_bootstrap_gdt-KERNBASE, %eax
3283258Sdg	movl	%eax, _apm_current_gdt_pdesc-KERNBASE+2
3293258Sdg
3303258Sdg	/* load new GDTR */
3313258Sdg	lgdt	_apm_current_gdt_pdesc-KERNBASE
3323258Sdg
3333258Sdg	/* setup GDT for APM initializer */
3343258Sdg	lea	_bootstrap_gdt-KERNBASE, %ecx
3353258Sdg	movl	$(APM_OURADDR), %eax	/* use %ax for 15..0 */
3363258Sdg	movl	%eax, %ebx
3373258Sdg	shrl	$16, %ebx		/* use %bl for 23..16 */
3383258Sdg					/* use %bh for 31..24 */
3393258Sdg#define APM_SETUP_GDT(index, attrib) \
3403258Sdg	movl	$(index), %si ; \
3413258Sdg	lea	0(%ecx,%esi,8), %edx ; \
3423258Sdg	movw	$0xffff, (%edx) ; \
3433258Sdg	movw	%ax, 2(%edx) ; \
3443258Sdg	movb	%bl, 4(%edx) ; \
3453258Sdg	movw	$(attrib), 5(%edx) ; \
3463258Sdg	movb	%bh, 7(%edx)
3473258Sdg
3483258Sdg	APM_SETUP_GDT(APM_INIT_CS_INDEX  , CS32_ATTRIB)
3493258Sdg	APM_SETUP_GDT(APM_INIT_DS_INDEX  , DS32_ATTRIB)
3503258Sdg	APM_SETUP_GDT(APM_INIT_CS16_INDEX, CS16_ATTRIB)
3513258Sdg
3523258Sdg	/*
3533258Sdg	 * Call the initializer:
3543258Sdg	 *
3553258Sdg	 * direct intersegment call to conventional memory code
3563258Sdg	 */
3573258Sdg	.byte	0x9a		/* actually, lcall $APM_INIT_CS_SEL, $0 */
3583258Sdg	.long	0
3593258Sdg	.word	APM_INIT_CS_SEL
3603258Sdg
3613258Sdg	movw	%ax, _apm_version-KERNBASE
3623258Sdg	movl	%ebx, _apm_cs_entry-KERNBASE
3633258Sdg	movw	%cx, _apm_cs32_base-KERNBASE
3643258Sdg	shrl	$16, %ecx
3653258Sdg	movw	%cx, _apm_cs16_base-KERNBASE
3663258Sdg	movw	%dx, _apm_ds_base-KERNBASE
3673258Sdg	movw	%si, _apm_cs_limit-KERNBASE
3683258Sdg	shrl	$16, %esi
3693258Sdg	movw	%si, _apm_ds_limit-KERNBASE
3703258Sdg	movw	%di, _apm_flags-KERNBASE
3713489Sphk#endif /* NAPM */
3723258Sdg
3731998Swollman	/* Find out our CPU type. */
3741321Sdg
3751998Swollman	/* Try to toggle alignment check flag; does not exist on 386. */
3761998Swollman	pushfl
3771998Swollman	popl	%eax
3781998Swollman	movl	%eax,%ecx
3791998Swollman	orl	$PSL_AC,%eax
3801998Swollman	pushl	%eax
3811998Swollman	popfl
3821998Swollman	pushfl
3831998Swollman	popl	%eax
3841998Swollman	xorl	%ecx,%eax
3851998Swollman	andl	$PSL_AC,%eax
3861998Swollman	pushl	%ecx
3871998Swollman	popfl
3881998Swollman
3891998Swollman	testl	%eax,%eax
3901998Swollman	jnz	1f
3911998Swollman	movl	$CPU_386,_cpu-KERNBASE
392556Srgrimes	jmp	2f
3931998Swollman
3941998Swollman1:	/* Try to toggle identification flag; does not exist on early 486s. */
3951998Swollman	pushfl
3961998Swollman	popl	%eax
3971998Swollman	movl	%eax,%ecx
3981998Swollman	xorl	$PSL_ID,%eax
3991998Swollman	pushl	%eax
4001998Swollman	popfl
4011998Swollman	pushfl
4021998Swollman	popl	%eax
4031998Swollman	xorl	%ecx,%eax
4041998Swollman	andl	$PSL_ID,%eax
4051998Swollman	pushl	%ecx
4061998Swollman	popfl
4071998Swollman
4081998Swollman	testl	%eax,%eax
4091998Swollman	jnz	1f
4101998Swollman	movl	$CPU_486,_cpu-KERNBASE
4112495Spst
4122495Spst	/* check for Cyrix 486DLC -- based on check routine  */
4132495Spst	/* documented in "Cx486SLC/e SMM Programmer's Guide" */
4142495Spst	xorw	%dx,%dx
4152495Spst	cmpw	%dx,%dx			# set flags to known state
4162495Spst	pushfw
4172495Spst	popw	%cx			# store flags in ecx
4182495Spst	movw	$0xffff,%ax
4192495Spst	movw	$0x0004,%bx
4202495Spst	divw	%bx
4212495Spst	pushfw
4222495Spst	popw	%ax
4232495Spst	andw	$0x08d5,%ax		# mask off important bits
4242495Spst	andw	$0x08d5,%cx
4252495Spst	cmpw	%ax,%cx
4262495Spst
4272495Spst	jnz	2f			# if flags changed, Intel chip
4282495Spst
4292495Spst	movl	$CPU_486DLC,_cpu-KERNBASE # set CPU value for Cyrix
4302495Spst	movl	$0x69727943,_cpu_vendor-KERNBASE	# store vendor string
4312495Spst	movw	$0x0078,_cpu_vendor-KERNBASE+4
4322495Spst
4332495Spst	invd				# Start with guaranteed clean cache
4342495Spst	/* Disable caching of the ISA hole only. */
4352495Spst	movb	$CCR0,%al		# Configuration Register index (CCR0)
4362495Spst	outb	%al,$0x22
4372495Spst	inb	$0x23,%al
4383117Spst	orb	$(CCR0_NC1|CCR0_BARB),%al
4392495Spst	outb	%al,$0x23
4402495Spst	invd
4411998Swollman	jmp	2f
4421998Swollman
4431998Swollman1:	/* Use the `cpuid' instruction. */
4441998Swollman	xorl	%eax,%eax
4451998Swollman	.byte	0x0f,0xa2		# cpuid 0
4461998Swollman	movl	%ebx,_cpu_vendor-KERNBASE	# store vendor string
4471998Swollman	movl	%edx,_cpu_vendor+4-KERNBASE
4481998Swollman	movl	%ecx,_cpu_vendor+8-KERNBASE
4491998Swollman	movb	$0,_cpu_vendor+12-KERNBASE
4501998Swollman
4511998Swollman	movl	$1,%eax
4521998Swollman	.byte	0x0f,0xa2		# cpuid 1
4531998Swollman	movl	%eax,_cpu_id-KERNBASE		# store cpu_id
4541998Swollman	rorl	$8,%eax			# extract family type
4551998Swollman	andl	$15,%eax
4561998Swollman	cmpl	$5,%eax
4571998Swollman	jae	1f
4581998Swollman
4591998Swollman	/* less than Pentium; must be 486 */
4601998Swollman	movl	$CPU_486,_cpu-KERNBASE
4611998Swollman	jmp	2f
4621998Swollman
4631998Swollman1:	movl	$CPU_586,_cpu-KERNBASE
464556Srgrimes2:
465556Srgrimes
4664Srgrimes	/*
4674Srgrimes	 * Finished with old stack; load new %esp now instead of later so
4684Srgrimes	 * we can trace this code without having to worry about the trace
4694Srgrimes	 * trap clobbering the memory test or the zeroing of the bss+bootstrap
4704Srgrimes	 * page tables.
4714Srgrimes	 *
4724Srgrimes	 * XXX - wdboot clears the bss after testing that this is safe.
4734Srgrimes	 * This is too wasteful - memory below 640K is scarce.  The boot
4744Srgrimes	 * program should check:
4754Srgrimes	 *	text+data <= &stack_variable - more_space_for_stack
4764Srgrimes	 *	text+data+bss+pad+space_for_page_tables <= end_of_memory
4774Srgrimes	 * Oops, the gdt is in the carcass of the boot program so clearing
4784Srgrimes	 * the rest of memory is still not possible.
4794Srgrimes	 */
480757Sdg	movl	$tmpstk-KERNBASE,%esp		/* bootstrap stack end location */
4814Srgrimes
482570Srgrimes/*
483570Srgrimes * Virtual address space of kernel:
484570Srgrimes *
485570Srgrimes *	text | data | bss | [syms] | page dir | proc0 kernel stack | usr stk map | Sysmap
486974Sdg *      pages:                          1         UPAGES (2)             1         NKPT (7)
487570Srgrimes */
488570Srgrimes
4894Srgrimes/* find end of kernel image */
490570Srgrimes	movl	$_end-KERNBASE,%ecx
491757Sdg	addl	$NBPG-1,%ecx			/* page align up */
4924Srgrimes	andl	$~(NBPG-1),%ecx
4931321Sdg	movl	%ecx,%esi			/* esi = start of free memory */
494757Sdg	movl	%ecx,_KERNend-KERNBASE		/* save end of kernel */
4954Srgrimes
496757Sdg/* clear bss */
497570Srgrimes	movl	$_edata-KERNBASE,%edi
498760Srgrimes	subl	%edi,%ecx			/* get amount to clear */
499757Sdg	xorl	%eax,%eax			/* specify zero fill */
5004Srgrimes	cld
5014Srgrimes	rep
5024Srgrimes	stosb
5034Srgrimes
504608Srgrimes/*
505974Sdg * The value in esi is both the end of the kernel bss and a pointer to
506974Sdg * the kernel page directory, and is used by the rest of locore to build
507974Sdg * the tables.
508974Sdg * esi + 1(page dir) + 2(UPAGES) + 1(p0stack) + NKPT(number of kernel
509757Sdg * page table pages) is then passed on the stack to init386(first) as
510757Sdg * the value first. esi should ALWAYS be page aligned!!
511608Srgrimes */
512757Sdg	movl	%esi,%ecx			/* Get current first availiable address */
513608Srgrimes
514757Sdg/* clear pagetables, page directory, stack, etc... */
515757Sdg	movl	%esi,%edi			/* base (page directory) */
516974Sdg	movl	$((1+UPAGES+1+NKPT)*NBPG),%ecx	/* amount to clear */
517757Sdg	xorl	%eax,%eax			/* specify zero fill */
518757Sdg	cld
519757Sdg	rep
520757Sdg	stosb
521757Sdg
522757Sdg/* physical address of Idle proc/kernel page directory */
523570Srgrimes	movl	%esi,_IdlePTD-KERNBASE
5244Srgrimes
525592Srgrimes/*
526592Srgrimes * fillkpt
527592Srgrimes *	eax = (page frame address | control | status) == pte
528592Srgrimes *	ebx = address of page table
529592Srgrimes *	ecx = how many pages to map
530592Srgrimes */
5314Srgrimes#define	fillkpt		\
5324Srgrimes1:	movl	%eax,(%ebx)	; \
533570Srgrimes	addl	$NBPG,%eax	; /* increment physical address */ \
5344Srgrimes	addl	$4,%ebx		; /* next pte */ \
5354Srgrimes	loop	1b		;
5364Srgrimes
5374Srgrimes/*
5384Srgrimes * Map Kernel
5394Srgrimes *
5404Srgrimes * First step - build page tables
5414Srgrimes */
542757Sdg#if defined (KGDB) || defined (BDE_DEBUGGER)
543757Sdg	movl	_KERNend-KERNBASE,%ecx		/* this much memory, */
544757Sdg	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
545757Sdg#ifdef BDE_DEBUGGER
546757Sdg	cmpl	$0xa0,%ecx			/* XXX - cover debugger pages */
547200Sdg	jae	1f
548200Sdg	movl	$0xa0,%ecx
549200Sdg1:
550757Sdg#endif /* BDE_DEBUGGER */
5512512Sbde	movl	$PG_V|PG_KW,%eax		/* kernel R/W, valid */
552757Sdg	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
553757Sdg	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
5544Srgrimes	fillkpt
5554Srgrimes
556757Sdg#else /* !KGDB && !BDE_DEBUGGER */
557757Sdg	/* write protect kernel text (doesn't do a thing for 386's - only 486's) */
558757Sdg	movl	$_etext-KERNBASE,%ecx		/* get size of text */
5593842Sdg	addl	$NBPG-1,%ecx			/* round up to page */
560757Sdg	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
561757Sdg	movl	$PG_V|PG_KR,%eax		/* specify read only */
5623842Sdg#if 0
5633842Sdg	movl	$_etext,%ecx			/* get size of text */
5643842Sdg	subl	$_btext,%ecx
5653842Sdg	addl	$NBPG-1,%ecx			/* round up to page */
5663842Sdg	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
5673842Sdg	movl	$_btext-KERNBASE,%eax		/* get offset to physical memory */
5683842Sdg	orl	$PG_V|PG_KR,%eax		/* specify read only */
5693842Sdg#endif
570757Sdg	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
571757Sdg	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
572757Sdg	fillkpt
573757Sdg
574757Sdg	/* data and bss are r/w */
575757Sdg	andl	$PG_FRAME,%eax			/* strip to just addr of bss */
576757Sdg	movl	_KERNend-KERNBASE,%ecx		/* calculate size */
577757Sdg	subl	%eax,%ecx
578757Sdg	shrl	$PGSHIFT,%ecx
579757Sdg	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
580757Sdg	fillkpt
5811321Sdg#endif /* KGDB || BDE_DEBUGGER */
582757Sdg
583757Sdg/* now initialize the page dir, upages, p0stack PT, and page tables */
584757Sdg
585974Sdg	movl	$(1+UPAGES+1+NKPT),%ecx	/* number of PTEs */
586757Sdg	movl	%esi,%eax			/* phys address of PTD */
587757Sdg	andl	$PG_FRAME,%eax			/* convert to PFN, should be a NOP */
5882512Sbde	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
589757Sdg	movl	%esi,%ebx			/* calculate pte offset to ptd */
590757Sdg	shrl	$PGSHIFT-2,%ebx
591757Sdg	addl	%esi,%ebx			/* address of page directory */
592757Sdg	addl	$((1+UPAGES+1)*NBPG),%ebx	/* offset to kernel page tables */
593757Sdg	fillkpt
5941321Sdg
5954Srgrimes/* map I/O memory map */
5964Srgrimes
597757Sdg	movl    _KPTphys-KERNBASE,%ebx		/* base of kernel page tables */
598757Sdg	lea     (0xa0 * PTESIZE)(%ebx),%ebx	/* hardwire ISA hole at KERNBASE + 0xa0000 */
599757Sdg	movl	$0x100-0xa0,%ecx		/* for this many pte s, */
6001046Sdg	movl	$(0xa0000|PG_V|PG_KW|PG_N),%eax	/* valid, kernel read/write, non-cacheable */
601757Sdg	movl	%ebx,_atdevphys-KERNBASE	/* save phys addr of ptes */
6024Srgrimes	fillkpt
6034Srgrimes
6044Srgrimes /* map proc 0's kernel stack into user page table page */
6054Srgrimes
606757Sdg	movl	$UPAGES,%ecx			/* for this many pte s, */
607757Sdg	lea	(1*NBPG)(%esi),%eax		/* physical address in proc 0 */
608757Sdg	lea	(KERNBASE)(%eax),%edx		/* change into virtual addr */
609757Sdg	movl	%edx,_proc0paddr-KERNBASE	/* save VA for proc 0 init */
610757Sdg	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
611757Sdg	lea	((1+UPAGES)*NBPG)(%esi),%ebx	/* addr of stack page table in proc 0 */
612757Sdg	addl	$(KSTKPTEOFF * PTESIZE),%ebx	/* offset to kernel stack PTE */
6134Srgrimes	fillkpt
6144Srgrimes
6154Srgrimes/*
616757Sdg * Initialize kernel page table directory
6174Srgrimes */
6184Srgrimes	/* install a pde for temporary double map of bottom of VA */
619757Sdg	movl	_KPTphys-KERNBASE,%eax
620757Sdg	orl     $PG_V|PG_KW,%eax		/* valid, kernel read/write */
621757Sdg	movl	%eax,(%esi)			/* which is where temp maps! */
6224Srgrimes
623757Sdg	/* initialize kernel pde's */
624974Sdg	movl	$(NKPT),%ecx			/* for this many PDEs */
625757Sdg	lea	(KPTDI*PDESIZE)(%esi),%ebx	/* offset of pde for kernel */
6264Srgrimes	fillkpt
6274Srgrimes
6284Srgrimes	/* install a pde recursively mapping page directory as a page table! */
629757Sdg	movl	%esi,%eax			/* phys address of ptd in proc 0 */
630757Sdg	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
631757Sdg	movl	%eax,PTDPTDI*PDESIZE(%esi)	/* which is where PTmap maps! */
6324Srgrimes
6334Srgrimes	/* install a pde to map kernel stack for proc 0 */
634757Sdg	lea	((1+UPAGES)*NBPG)(%esi),%eax	/* physical address of pt in proc 0 */
635757Sdg	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
636757Sdg	movl	%eax,KSTKPTDI*PDESIZE(%esi)	/* which is where kernel stack maps! */
6374Srgrimes
638757Sdg#ifdef BDE_DEBUGGER
6394Srgrimes	/* copy and convert stuff from old gdt and idt for debugger */
6404Srgrimes
641757Sdg	cmpl	$0x0375c339,0x96104		/* XXX - debugger signature */
6424Srgrimes	jne	1f
643570Srgrimes	movb	$1,_bdb_exists-KERNBASE
6444Srgrimes1:
6454Srgrimes	pushal
6464Srgrimes	subl	$2*6,%esp
6474Srgrimes
6484Srgrimes	sgdt	(%esp)
649757Sdg	movl	2(%esp),%esi			/* base address of current gdt */
650570Srgrimes	movl	$_gdt-KERNBASE,%edi
6514Srgrimes	movl	%edi,2(%esp)
6524Srgrimes	movl	$8*18/4,%ecx
6531688Sdg	cld
654757Sdg	rep					/* copy gdt */
6554Srgrimes	movsl
656570Srgrimes	movl	$_gdt-KERNBASE,-8+2(%edi)	/* adjust gdt self-ptr */
6574Srgrimes	movb	$0x92,-8+5(%edi)
6584Srgrimes
6594Srgrimes	sidt	6(%esp)
660757Sdg	movl	6+2(%esp),%esi			/* base address of current idt */
661757Sdg	movl	8+4(%esi),%eax			/* convert dbg descriptor to ... */
6624Srgrimes	movw	8(%esi),%ax
663570Srgrimes	movl	%eax,bdb_dbg_ljmp+1-KERNBASE	/* ... immediate offset ... */
6644Srgrimes	movl	8+2(%esi),%eax
665570Srgrimes	movw	%ax,bdb_dbg_ljmp+5-KERNBASE	/* ... and selector for ljmp */
666757Sdg	movl	24+4(%esi),%eax			/* same for bpt descriptor */
6674Srgrimes	movw	24(%esi),%ax
668570Srgrimes	movl	%eax,bdb_bpt_ljmp+1-KERNBASE
6694Srgrimes	movl	24+2(%esi),%eax
670570Srgrimes	movw	%ax,bdb_bpt_ljmp+5-KERNBASE
6714Srgrimes
672570Srgrimes	movl	$_idt-KERNBASE,%edi
6734Srgrimes	movl	%edi,6+2(%esp)
6744Srgrimes	movl	$8*4/4,%ecx
6751688Sdg	cld
676757Sdg	rep					/* copy idt */
6774Srgrimes	movsl
6784Srgrimes
6794Srgrimes	lgdt	(%esp)
6804Srgrimes	lidt	6(%esp)
6814Srgrimes
6824Srgrimes	addl	$2*6,%esp
6834Srgrimes	popal
6841321Sdg#endif /* BDE_DEBUGGER */
6854Srgrimes
686592Srgrimes	/* load base of page directory and enable mapping */
687757Sdg	movl	%esi,%eax			/* phys address of ptd in proc 0 */
688757Sdg	movl	%eax,%cr3			/* load ptd addr into mmu */
689757Sdg	movl	%cr0,%eax			/* get control word */
690757Sdg	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
691757Sdg	movl	%eax,%cr0			/* and let's page NOW! */
6924Srgrimes
693757Sdg	pushl	$begin				/* jump to high mem */
6944Srgrimes	ret
6954Srgrimes
696570Srgrimesbegin: /* now running relocated at KERNBASE where the system is linked to run */
697757Sdg	movl	_atdevphys,%edx			/* get pte PA */
698757Sdg	subl	_KPTphys,%edx			/* remove base of ptes, now have phys offset */
699757Sdg	shll	$PGSHIFT-2,%edx			/* corresponding to virt offset */
700757Sdg	addl	$KERNBASE,%edx			/* add virtual base */
701570Srgrimes	movl	%edx,_atdevbase
7023728Sphk
7033861Sbde#include "sc.h"
7044031Sjoerg#include "vt.h"
7054031Sjoerg#if NSC > 0 || NVT > 0
7063728Sphk	/* XXX: can't scinit relocate Crtat relative to atdevbase itself? */
7073728Sphk	.globl _Crtat				/* XXX - locore should not know about */
7083728Sphk	movl	_Crtat,%eax			/* variables of device drivers (pccons)! */
7093728Sphk	subl	$(KERNBASE+0xA0000),%eax
7104Srgrimes	addl	%eax,%edx
7114Srgrimes	movl	%edx,_Crtat
7123728Sphk#endif
7134Srgrimes
714757Sdg	/* set up bootstrap stack - 48 bytes */
715570Srgrimes	movl	$_kstack+UPAGES*NBPG-4*12,%esp	/* bootstrap stack end location */
716757Sdg	xorl	%eax,%eax			/* mark end of frames */
7174Srgrimes	movl	%eax,%ebp
718570Srgrimes	movl	_proc0paddr,%eax
719570Srgrimes	movl	%esi,PCB_CR3(%eax)
7204Srgrimes
721757Sdg#ifdef BDE_DEBUGGER
7224Srgrimes	/* relocate debugger gdt entries */
7234Srgrimes
724757Sdg	movl	$_gdt+8*9,%eax			/* adjust slots 9-17 */
7254Srgrimes	movl	$9,%ecx
7264Srgrimesreloc_gdt:
7271321Sdg	movb	$KERNBASE>>24,7(%eax)		/* top byte of base addresses, was 0, */
728757Sdg	addl	$8,%eax				/* now KERNBASE>>24 */
7294Srgrimes	loop	reloc_gdt
7304Srgrimes
7314Srgrimes	cmpl	$0,_bdb_exists
7324Srgrimes	je	1f
7334Srgrimes	int	$3
7344Srgrimes1:
7351321Sdg#endif /* BDE_DEBUGGER */
7364Srgrimes
737608Srgrimes	/*
738608Srgrimes	 * Skip over the page tables and the kernel stack
739608Srgrimes	 */
740974Sdg	lea	((1+UPAGES+1+NKPT)*NBPG)(%esi),%esi
741608Srgrimes
742757Sdg	pushl	%esi				/* value of first for init386(first) */
743757Sdg	call	_init386			/* wire 386 chip for unix operation */
7441549Srgrimes	popl	%esi
745200Sdg
7461549Srgrimes	.globl	__ucodesel,__udatasel
7471549Srgrimes
7481549Srgrimes	pushl	$0				/* unused */
7491549Srgrimes	pushl	__udatasel			/* ss */
7501549Srgrimes	pushl	$0				/* esp - filled in by execve() */
7512500Sdg	pushl	$PSL_USERSET			/* eflags (ring 0, int enab) */
7521549Srgrimes	pushl	__ucodesel			/* cs */
7531549Srgrimes	pushl	$0				/* eip - filled in by execve() */
7541549Srgrimes	subl	$(12*4),%esp			/* space for rest of registers */
7551549Srgrimes
7561549Srgrimes	pushl	%esp				/* call main with frame pointer */
757757Sdg	call	_main				/* autoconfiguration, mountroot etc */
7584Srgrimes
7591549Srgrimes	addl	$(13*4),%esp			/* back to a frame we can return with */
7601549Srgrimes
761134Sdg	/*
762570Srgrimes	 * now we've run main() and determined what cpu-type we are, we can
763570Srgrimes	 * enable WP mode on i486 cpus and above.
7641058Sdg	 */
7651058Sdg#if defined(I486_CPU) || defined(I586_CPU)
7661058Sdg	cmpl    $CPUCLASS_386,_cpu_class
7671058Sdg	je	1f
7681058Sdg	movl	%cr0,%eax			/* get control word */
7691058Sdg	orl	$CR0_WP,%eax			/* enable write protect for all modes */
7701058Sdg	movl	%eax,%cr0			/* and do it */
7711058Sdg#endif
7721058Sdg	/*
773134Sdg	 * on return from main(), we are process 1
774134Sdg	 * set up address space and stack so that we can 'return' to user mode
775134Sdg	 */
7761058Sdg1:
7774Srgrimes	movl	__ucodesel,%eax
7784Srgrimes	movl	__udatasel,%ecx
7791549Srgrimes
7804Srgrimes	movl	%cx,%ds
7814Srgrimes	movl	%cx,%es
782757Sdg	movl	%ax,%fs				/* double map cs to fs */
783757Sdg	movl	%cx,%gs				/* and ds to gs */
7841549Srgrimes	iret					/* goto user! */
7854Srgrimes
7861549Srgrimes#define LCALL(x,y)	.byte 0x9a ; .long y ; .word x
7874Srgrimes
788200SdgNON_GPROF_ENTRY(sigcode)
789592Srgrimes	call	SIGF_HANDLER(%esp)
790757Sdg	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
791757Sdg						/* copy at 8(%esp)) */
7924Srgrimes	pushl	%eax
793757Sdg	pushl	%eax				/* junk to fake return address */
794757Sdg	movl	$103,%eax			/* XXX sigreturn() */
795757Sdg	LCALL(0x7,0)				/* enter kernel with args on stack */
796757Sdg	hlt					/* never gets here */
7974Srgrimes
7984Srgrimes	.globl	_szsigcode
7994Srgrimes_szsigcode:
8004Srgrimes	.long	_szsigcode-_sigcode
801