locore.s revision 1321
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
371321Sdg *	$Id: locore.s,v 1.15 1994/02/01 04:08:54 davidg 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
48757Sdg#include "npx.h"				/* for NNPX */
49757Sdg#include "assym.s"				/* system definitions */
50757Sdg#include "machine/psl.h"			/* processor status longword defs */
51757Sdg#include "machine/pte.h"			/* page table entry definitions */
52757Sdg#include "errno.h"				/* error return codes */
53757Sdg#include "machine/specialreg.h"			/* x86 special registers */
54757Sdg#include "machine/cputypes.h"			/* x86 cpu type definitions */
55757Sdg#include "syscall.h"				/* system call numbers */
56757Sdg#include "machine/asmacros.h"			/* miscellaneous asm macros */
574Srgrimes
584Srgrimes/*
59757Sdg *	XXX
60757Sdg *
614Srgrimes * Note: This version greatly munged to avoid various assembler errors
624Srgrimes * that may be fixed in newer versions of gas. Perhaps newer versions
634Srgrimes * will have more pleasant appearance.
644Srgrimes */
654Srgrimes
66200Sdg/*
674Srgrimes * PTmap is recursive pagemap at top of virtual address space.
684Srgrimes * Within PTmap, the page directory can be found (third indirection).
694Srgrimes */
70592Srgrimes	.globl	_PTmap,_PTD,_PTDpde,_Sysmap
71592Srgrimes	.set	_PTmap,PTDPTDI << PDRSHIFT
72592Srgrimes	.set	_PTD,_PTmap + (PTDPTDI * NBPG)
73757Sdg	.set	_PTDpde,_PTD + (PTDPTDI * PDESIZE)
74592Srgrimes
75757Sdg/* Sysmap is the base address of the kernel page tables */
76608Srgrimes	.set	_Sysmap,_PTmap + (KPTDI * NBPG)
774Srgrimes
784Srgrimes/*
794Srgrimes * APTmap, APTD is the alternate recursive pagemap.
804Srgrimes * It's used when modifying another process's page tables.
814Srgrimes */
82592Srgrimes	.globl	_APTmap,_APTD,_APTDpde
83592Srgrimes	.set	_APTmap,APTDPTDI << PDRSHIFT
84592Srgrimes	.set	_APTD,_APTmap + (APTDPTDI * NBPG)
85757Sdg	.set	_APTDpde,_PTD + (APTDPTDI * PDESIZE)
864Srgrimes
874Srgrimes/*
884Srgrimes * Access to each processes kernel stack is via a region of
894Srgrimes * per-process address space (at the beginning), immediatly above
904Srgrimes * the user process stack.
914Srgrimes */
92570Srgrimes	.set	_kstack,USRSTACK
93134Sdg	.globl	_kstack
944Srgrimes
95556Srgrimes/*
96556Srgrimes * Globals
97556Srgrimes */
98556Srgrimes	.data
99556Srgrimes	.globl	_esym
100757Sdg_esym:	.long	0				/* ptr to end of syms */
101134Sdg
102592Srgrimes	.globl	_boothowto,_bootdev,_curpcb
103134Sdg
104592Srgrimes	.globl	_cpu,_cold,_atdevbase
105757Sdg_cpu:	.long	0				/* are we 386, 386sx, or 486 */
106757Sdg_cold:	.long	1				/* cold till we are not */
107757Sdg_atdevbase:	.long	0			/* location of start of iomem in virtual */
108757Sdg_atdevphys:	.long	0			/* location of device mapping ptes (phys) */
1094Srgrimes
110757Sdg	.globl	_KERNend
111757Sdg_KERNend:	.long	0			/* phys addr end of kernel (just after bss) */
112757Sdg
113592Srgrimes	.globl	_IdlePTD,_KPTphys
114757Sdg_IdlePTD:	.long	0			/* phys addr of kernel PTD */
115757Sdg_KPTphys:	.long	0			/* phys addr of kernel page tables */
1164Srgrimes
117757Sdg	.globl	_cyloffset
118757Sdg_cyloffset:	.long	0			/* cylinder offset from boot blocks */
119718Swollman
120757Sdg	.globl	_proc0paddr
121757Sdg_proc0paddr:	.long	0			/* address of proc 0 address space */
122134Sdg
123757Sdg#ifdef BDE_DEBUGGER
124757Sdg	.globl	_bdb_exists			/* flag to indicate BDE debugger is available */
1251321Sdg_bdb_exists:	.long	0
126757Sdg#endif
127718Swollman
128757Sdg	.globl	tmpstk
129974Sdg	.space	0x1000
1304Srgrimestmpstk:
131134Sdg
132134Sdg
133556Srgrimes/*
134556Srgrimes * System Initialization
135556Srgrimes */
1364Srgrimes	.text
137134Sdg
138134Sdg/*
139200Sdg * btext: beginning of text section.
140200Sdg * Also the entry point (jumped to directly from the boot blocks).
141134Sdg */
1421321SdgNON_GPROF_ENTRY(btext)
143757Sdg	movw	$0x1234,0x472			/* warm boot */
1444Srgrimes	jmp	1f
1451321Sdg	.org	0x500				/* space for BIOS variables */
1464Srgrimes
1474Srgrimes	/*
148556Srgrimes	 * pass parameters on stack (howto, bootdev, unit, cyloffset, esym)
1494Srgrimes	 * note: (%esp) is return address of boot
1504Srgrimes	 * ( if we want to hold onto /boot, it's physical %esp up to _end)
1514Srgrimes	 */
1524Srgrimes
1534Srgrimes 1:	movl	4(%esp),%eax
154570Srgrimes	movl	%eax,_boothowto-KERNBASE
1554Srgrimes	movl	8(%esp),%eax
156570Srgrimes	movl	%eax,_bootdev-KERNBASE
1574Srgrimes	movl	12(%esp),%eax
158570Srgrimes	movl	%eax,_cyloffset-KERNBASE
159556Srgrimes	movl	16(%esp),%eax
160570Srgrimes	addl	$KERNBASE,%eax
161570Srgrimes	movl	%eax,_esym-KERNBASE
162975Smartin#ifdef DISKLESS					/* Copy diskless structure */
163975Smartin	movl	_nfs_diskless_size-KERNBASE,%ecx
164975Smartin	movl	20(%esp),%esi
165975Smartin	movl	$(_nfs_diskless-KERNBASE),%edi
166975Smartin	rep
167975Smartin	movsb
168975Smartin#endif
1694Srgrimes
170556Srgrimes	/* find out our CPU type. */
171556Srgrimes        pushfl
172556Srgrimes        popl    %eax
173556Srgrimes        movl    %eax,%ecx
174556Srgrimes        xorl    $0x40000,%eax
175556Srgrimes        pushl   %eax
176556Srgrimes        popfl
177556Srgrimes        pushfl
178556Srgrimes        popl    %eax
179556Srgrimes        xorl    %ecx,%eax
180556Srgrimes        shrl    $18,%eax
181556Srgrimes        andl    $1,%eax
182556Srgrimes        push    %ecx
183556Srgrimes        popfl
1841321Sdg
185556Srgrimes        cmpl    $0,%eax
186556Srgrimes        jne     1f
187570Srgrimes        movl    $CPU_386,_cpu-KERNBASE
188556Srgrimes	jmp	2f
189570Srgrimes1:      movl    $CPU_486,_cpu-KERNBASE
190556Srgrimes2:
191556Srgrimes
1924Srgrimes	/*
1934Srgrimes	 * Finished with old stack; load new %esp now instead of later so
1944Srgrimes	 * we can trace this code without having to worry about the trace
1954Srgrimes	 * trap clobbering the memory test or the zeroing of the bss+bootstrap
1964Srgrimes	 * page tables.
1974Srgrimes	 *
1984Srgrimes	 * XXX - wdboot clears the bss after testing that this is safe.
1994Srgrimes	 * This is too wasteful - memory below 640K is scarce.  The boot
2004Srgrimes	 * program should check:
2014Srgrimes	 *	text+data <= &stack_variable - more_space_for_stack
2024Srgrimes	 *	text+data+bss+pad+space_for_page_tables <= end_of_memory
2034Srgrimes	 * Oops, the gdt is in the carcass of the boot program so clearing
2044Srgrimes	 * the rest of memory is still not possible.
2054Srgrimes	 */
206757Sdg	movl	$tmpstk-KERNBASE,%esp		/* bootstrap stack end location */
2074Srgrimes
208570Srgrimes/*
209570Srgrimes * Virtual address space of kernel:
210570Srgrimes *
211570Srgrimes *	text | data | bss | [syms] | page dir | proc0 kernel stack | usr stk map | Sysmap
212974Sdg *      pages:                          1         UPAGES (2)             1         NKPT (7)
213570Srgrimes */
214570Srgrimes
2154Srgrimes/* find end of kernel image */
216570Srgrimes	movl	$_end-KERNBASE,%ecx
217757Sdg	addl	$NBPG-1,%ecx			/* page align up */
2184Srgrimes	andl	$~(NBPG-1),%ecx
2191321Sdg	movl	%ecx,%esi			/* esi = start of free memory */
220757Sdg	movl	%ecx,_KERNend-KERNBASE		/* save end of kernel */
2214Srgrimes
222757Sdg/* clear bss */
223570Srgrimes	movl	$_edata-KERNBASE,%edi
224760Srgrimes	subl	%edi,%ecx			/* get amount to clear */
225757Sdg	xorl	%eax,%eax			/* specify zero fill */
2264Srgrimes	cld
2274Srgrimes	rep
2284Srgrimes	stosb
2294Srgrimes
230608Srgrimes/*
231974Sdg * The value in esi is both the end of the kernel bss and a pointer to
232974Sdg * the kernel page directory, and is used by the rest of locore to build
233974Sdg * the tables.
234974Sdg * esi + 1(page dir) + 2(UPAGES) + 1(p0stack) + NKPT(number of kernel
235757Sdg * page table pages) is then passed on the stack to init386(first) as
236757Sdg * the value first. esi should ALWAYS be page aligned!!
237608Srgrimes */
238757Sdg	movl	%esi,%ecx			/* Get current first availiable address */
239608Srgrimes
240757Sdg/* clear pagetables, page directory, stack, etc... */
241757Sdg	movl	%esi,%edi			/* base (page directory) */
242974Sdg	movl	$((1+UPAGES+1+NKPT)*NBPG),%ecx	/* amount to clear */
243757Sdg	xorl	%eax,%eax			/* specify zero fill */
244757Sdg	cld
245757Sdg	rep
246757Sdg	stosb
247757Sdg
248757Sdg/* physical address of Idle proc/kernel page directory */
249570Srgrimes	movl	%esi,_IdlePTD-KERNBASE
2504Srgrimes
251592Srgrimes/*
252592Srgrimes * fillkpt
253592Srgrimes *	eax = (page frame address | control | status) == pte
254592Srgrimes *	ebx = address of page table
255592Srgrimes *	ecx = how many pages to map
256592Srgrimes */
2574Srgrimes#define	fillkpt		\
2584Srgrimes1:	movl	%eax,(%ebx)	; \
259570Srgrimes	addl	$NBPG,%eax	; /* increment physical address */ \
2604Srgrimes	addl	$4,%ebx		; /* next pte */ \
2614Srgrimes	loop	1b		;
2624Srgrimes
2634Srgrimes/*
2644Srgrimes * Map Kernel
2654Srgrimes *
2664Srgrimes * First step - build page tables
2674Srgrimes */
268757Sdg#if defined (KGDB) || defined (BDE_DEBUGGER)
269757Sdg	movl	_KERNend-KERNBASE,%ecx		/* this much memory, */
270757Sdg	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
271757Sdg#ifdef BDE_DEBUGGER
272757Sdg	cmpl	$0xa0,%ecx			/* XXX - cover debugger pages */
273200Sdg	jae	1f
274200Sdg	movl	$0xa0,%ecx
275200Sdg1:
276757Sdg#endif /* BDE_DEBUGGER */
277757Sdg	movl	$PG_V|PG_KW,%eax		/* having these bits set, */
278757Sdg	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
279757Sdg	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
2804Srgrimes	fillkpt
2814Srgrimes
282757Sdg#else /* !KGDB && !BDE_DEBUGGER */
283757Sdg	/* write protect kernel text (doesn't do a thing for 386's - only 486's) */
284757Sdg	movl	$_etext-KERNBASE,%ecx		/* get size of text */
285757Sdg	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
286757Sdg	movl	$PG_V|PG_KR,%eax		/* specify read only */
287757Sdg	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
288757Sdg	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
289757Sdg	fillkpt
290757Sdg
291757Sdg	/* data and bss are r/w */
292757Sdg	andl	$PG_FRAME,%eax			/* strip to just addr of bss */
293757Sdg	movl	_KERNend-KERNBASE,%ecx		/* calculate size */
294757Sdg	subl	%eax,%ecx
295757Sdg	shrl	$PGSHIFT,%ecx
296757Sdg	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
297757Sdg	fillkpt
2981321Sdg#endif /* KGDB || BDE_DEBUGGER */
299757Sdg
300757Sdg/* now initialize the page dir, upages, p0stack PT, and page tables */
301757Sdg
302974Sdg	movl	$(1+UPAGES+1+NKPT),%ecx	/* number of PTEs */
303757Sdg	movl	%esi,%eax			/* phys address of PTD */
304757Sdg	andl	$PG_FRAME,%eax			/* convert to PFN, should be a NOP */
305757Sdg	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
306757Sdg	movl	%esi,%ebx			/* calculate pte offset to ptd */
307757Sdg	shrl	$PGSHIFT-2,%ebx
308757Sdg	addl	%esi,%ebx			/* address of page directory */
309757Sdg	addl	$((1+UPAGES+1)*NBPG),%ebx	/* offset to kernel page tables */
310757Sdg	fillkpt
3111321Sdg
3124Srgrimes/* map I/O memory map */
3134Srgrimes
314757Sdg	movl    _KPTphys-KERNBASE,%ebx		/* base of kernel page tables */
315757Sdg	lea     (0xa0 * PTESIZE)(%ebx),%ebx	/* hardwire ISA hole at KERNBASE + 0xa0000 */
316757Sdg	movl	$0x100-0xa0,%ecx		/* for this many pte s, */
3171046Sdg	movl	$(0xa0000|PG_V|PG_KW|PG_N),%eax	/* valid, kernel read/write, non-cacheable */
318757Sdg	movl	%ebx,_atdevphys-KERNBASE	/* save phys addr of ptes */
3194Srgrimes	fillkpt
3204Srgrimes
3214Srgrimes /* map proc 0's kernel stack into user page table page */
3224Srgrimes
323757Sdg	movl	$UPAGES,%ecx			/* for this many pte s, */
324757Sdg	lea	(1*NBPG)(%esi),%eax		/* physical address in proc 0 */
325757Sdg	lea	(KERNBASE)(%eax),%edx		/* change into virtual addr */
326757Sdg	movl	%edx,_proc0paddr-KERNBASE	/* save VA for proc 0 init */
327757Sdg	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
328757Sdg	lea	((1+UPAGES)*NBPG)(%esi),%ebx	/* addr of stack page table in proc 0 */
329757Sdg	addl	$(KSTKPTEOFF * PTESIZE),%ebx	/* offset to kernel stack PTE */
3304Srgrimes	fillkpt
3314Srgrimes
3324Srgrimes/*
333757Sdg * Initialize kernel page table directory
3344Srgrimes */
3354Srgrimes	/* install a pde for temporary double map of bottom of VA */
336757Sdg	movl	_KPTphys-KERNBASE,%eax
337757Sdg	orl     $PG_V|PG_KW,%eax		/* valid, kernel read/write */
338757Sdg	movl	%eax,(%esi)			/* which is where temp maps! */
3394Srgrimes
340757Sdg	/* initialize kernel pde's */
341974Sdg	movl	$(NKPT),%ecx			/* for this many PDEs */
342757Sdg	lea	(KPTDI*PDESIZE)(%esi),%ebx	/* offset of pde for kernel */
3434Srgrimes	fillkpt
3444Srgrimes
3454Srgrimes	/* install a pde recursively mapping page directory as a page table! */
346757Sdg	movl	%esi,%eax			/* phys address of ptd in proc 0 */
347757Sdg	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
348757Sdg	movl	%eax,PTDPTDI*PDESIZE(%esi)	/* which is where PTmap maps! */
3494Srgrimes
3504Srgrimes	/* install a pde to map kernel stack for proc 0 */
351757Sdg	lea	((1+UPAGES)*NBPG)(%esi),%eax	/* physical address of pt in proc 0 */
352757Sdg	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
353757Sdg	movl	%eax,KSTKPTDI*PDESIZE(%esi)	/* which is where kernel stack maps! */
3544Srgrimes
355757Sdg#ifdef BDE_DEBUGGER
3564Srgrimes	/* copy and convert stuff from old gdt and idt for debugger */
3574Srgrimes
358757Sdg	cmpl	$0x0375c339,0x96104		/* XXX - debugger signature */
3594Srgrimes	jne	1f
360570Srgrimes	movb	$1,_bdb_exists-KERNBASE
3614Srgrimes1:
3624Srgrimes	pushal
3634Srgrimes	subl	$2*6,%esp
3644Srgrimes
3654Srgrimes	sgdt	(%esp)
366757Sdg	movl	2(%esp),%esi			/* base address of current gdt */
367570Srgrimes	movl	$_gdt-KERNBASE,%edi
3684Srgrimes	movl	%edi,2(%esp)
3694Srgrimes	movl	$8*18/4,%ecx
370757Sdg	rep					/* copy gdt */
3714Srgrimes	movsl
372570Srgrimes	movl	$_gdt-KERNBASE,-8+2(%edi)	/* adjust gdt self-ptr */
3734Srgrimes	movb	$0x92,-8+5(%edi)
3744Srgrimes
3754Srgrimes	sidt	6(%esp)
376757Sdg	movl	6+2(%esp),%esi			/* base address of current idt */
377757Sdg	movl	8+4(%esi),%eax			/* convert dbg descriptor to ... */
3784Srgrimes	movw	8(%esi),%ax
379570Srgrimes	movl	%eax,bdb_dbg_ljmp+1-KERNBASE	/* ... immediate offset ... */
3804Srgrimes	movl	8+2(%esi),%eax
381570Srgrimes	movw	%ax,bdb_dbg_ljmp+5-KERNBASE	/* ... and selector for ljmp */
382757Sdg	movl	24+4(%esi),%eax			/* same for bpt descriptor */
3834Srgrimes	movw	24(%esi),%ax
384570Srgrimes	movl	%eax,bdb_bpt_ljmp+1-KERNBASE
3854Srgrimes	movl	24+2(%esi),%eax
386570Srgrimes	movw	%ax,bdb_bpt_ljmp+5-KERNBASE
3874Srgrimes
388570Srgrimes	movl	$_idt-KERNBASE,%edi
3894Srgrimes	movl	%edi,6+2(%esp)
3904Srgrimes	movl	$8*4/4,%ecx
391757Sdg	rep					/* copy idt */
3924Srgrimes	movsl
3934Srgrimes
3944Srgrimes	lgdt	(%esp)
3954Srgrimes	lidt	6(%esp)
3964Srgrimes
3974Srgrimes	addl	$2*6,%esp
3984Srgrimes	popal
3991321Sdg#endif /* BDE_DEBUGGER */
4004Srgrimes
401592Srgrimes	/* load base of page directory and enable mapping */
402757Sdg	movl	%esi,%eax			/* phys address of ptd in proc 0 */
403570Srgrimes	orl	$I386_CR3PAT,%eax
404757Sdg	movl	%eax,%cr3			/* load ptd addr into mmu */
405757Sdg	movl	%cr0,%eax			/* get control word */
406757Sdg	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
407757Sdg	movl	%eax,%cr0			/* and let's page NOW! */
4084Srgrimes
409757Sdg	pushl	$begin				/* jump to high mem */
4104Srgrimes	ret
4114Srgrimes
412570Srgrimesbegin: /* now running relocated at KERNBASE where the system is linked to run */
4134Srgrimes
414757Sdg	.globl _Crtat				/* XXX - locore should not know about */
415757Sdg	movl	_Crtat,%eax			/* variables of device drivers (pccons)! */
416592Srgrimes	subl	$(KERNBASE+0xA0000),%eax
417757Sdg	movl	_atdevphys,%edx			/* get pte PA */
418757Sdg	subl	_KPTphys,%edx			/* remove base of ptes, now have phys offset */
419757Sdg	shll	$PGSHIFT-2,%edx			/* corresponding to virt offset */
420757Sdg	addl	$KERNBASE,%edx			/* add virtual base */
421570Srgrimes	movl	%edx,_atdevbase
4224Srgrimes	addl	%eax,%edx
4234Srgrimes	movl	%edx,_Crtat
4244Srgrimes
425757Sdg	/* set up bootstrap stack - 48 bytes */
426570Srgrimes	movl	$_kstack+UPAGES*NBPG-4*12,%esp	/* bootstrap stack end location */
427757Sdg	xorl	%eax,%eax			/* mark end of frames */
4284Srgrimes	movl	%eax,%ebp
429570Srgrimes	movl	_proc0paddr,%eax
430570Srgrimes	movl	%esi,PCB_CR3(%eax)
4314Srgrimes
432757Sdg#ifdef BDE_DEBUGGER
4334Srgrimes	/* relocate debugger gdt entries */
4344Srgrimes
435757Sdg	movl	$_gdt+8*9,%eax			/* adjust slots 9-17 */
4364Srgrimes	movl	$9,%ecx
4374Srgrimesreloc_gdt:
4381321Sdg	movb	$KERNBASE>>24,7(%eax)		/* top byte of base addresses, was 0, */
439757Sdg	addl	$8,%eax				/* now KERNBASE>>24 */
4404Srgrimes	loop	reloc_gdt
4414Srgrimes
4424Srgrimes	cmpl	$0,_bdb_exists
4434Srgrimes	je	1f
4444Srgrimes	int	$3
4454Srgrimes1:
4461321Sdg#endif /* BDE_DEBUGGER */
4474Srgrimes
448608Srgrimes	/*
449608Srgrimes	 * Skip over the page tables and the kernel stack
450608Srgrimes	 */
451974Sdg	lea	((1+UPAGES+1+NKPT)*NBPG)(%esi),%esi
452608Srgrimes
453757Sdg	pushl	%esi				/* value of first for init386(first) */
454757Sdg	call	_init386			/* wire 386 chip for unix operation */
455200Sdg
4564Srgrimes	movl	$0,_PTD
457757Sdg	call	_main				/* autoconfiguration, mountroot etc */
4584Srgrimes	popl	%esi
4594Srgrimes
460134Sdg	/*
461570Srgrimes	 * now we've run main() and determined what cpu-type we are, we can
462570Srgrimes	 * enable WP mode on i486 cpus and above.
4631058Sdg	 */
4641058Sdg#if defined(I486_CPU) || defined(I586_CPU)
4651058Sdg	cmpl    $CPUCLASS_386,_cpu_class
4661058Sdg	je	1f
4671058Sdg	movl	%cr0,%eax			/* get control word */
4681058Sdg	orl	$CR0_WP,%eax			/* enable write protect for all modes */
4691058Sdg	movl	%eax,%cr0			/* and do it */
4701058Sdg#endif
4711058Sdg	/*
472134Sdg	 * on return from main(), we are process 1
473134Sdg	 * set up address space and stack so that we can 'return' to user mode
474134Sdg	 */
4751058Sdg1:
476570Srgrimes	.globl	__ucodesel,__udatasel
4774Srgrimes	movl	__ucodesel,%eax
4784Srgrimes	movl	__udatasel,%ecx
479570Srgrimes	/* build outer stack frame */
480757Sdg	pushl	%ecx				/* user ss */
481757Sdg	pushl	$USRSTACK			/* user esp */
482757Sdg	pushl	%eax				/* user cs */
483757Sdg	pushl	$0				/* user ip */
4844Srgrimes	movl	%cx,%ds
4854Srgrimes	movl	%cx,%es
486757Sdg	movl	%ax,%fs				/* double map cs to fs */
487757Sdg	movl	%cx,%gs				/* and ds to gs */
488757Sdg	lret					/* goto user! */
4894Srgrimes
490757Sdg	pushl	$lretmsg1			/* "should never get here!" */
4914Srgrimes	call	_panic
4924Srgrimeslretmsg1:
4934Srgrimes	.asciz	"lret: toinit\n"
4944Srgrimes
4954Srgrimes
4961321Sdg#define	LCALL(x,y)	.byte 0x9a ; .long y ; .word x
4974Srgrimes/*
498134Sdg * Icode is copied out to process 1 and executed in user mode:
499134Sdg *	execve("/sbin/init", argv, envp); exit(0);
500200Sdg * If the execve fails, process 1 exits and the system panics.
5014Srgrimes */
502200SdgNON_GPROF_ENTRY(icode)
503757Sdg	pushl	$0				/* envp for execve() */
504200Sdg
505757Sdg#	pushl	$argv-_icode			/* can't do this 'cos gas 1.38 is broken */
5064Srgrimes	movl	$argv,%eax
5074Srgrimes	subl	$_icode,%eax
508757Sdg	pushl	%eax				/* argp for execve() */
5094Srgrimes
510570Srgrimes#	pushl	$init-_icode
5114Srgrimes	movl	$init,%eax
5124Srgrimes	subl	$_icode,%eax
513757Sdg	pushl	%eax				/* fname for execve() */
5144Srgrimes
515757Sdg	pushl	%eax				/* dummy return address */
516200Sdg
517757Sdg	movl	$SYS_execve,%eax
5184Srgrimes	LCALL(0x7,0x0)
519200Sdg
520570Srgrimes	/* exit if something botches up in the above execve() */
521757Sdg	pushl	%eax				/* execve failed, the errno will do for an */
522757Sdg						/* exit code because errnos are < 128 */
523757Sdg	pushl	%eax				/* dummy return address */
524757Sdg	movl	$SYS_exit,%eax
5254Srgrimes	LCALL(0x7,0x0)
5264Srgrimes
5274Srgrimesinit:
5284Srgrimes	.asciz	"/sbin/init"
5294Srgrimes	ALIGN_DATA
5304Srgrimesargv:
531757Sdg	.long	init+6-_icode			/* argv[0] = "init" ("/sbin/init" + 6) */
532757Sdg	.long	eicode-_icode			/* argv[1] follows icode after copyout */
5334Srgrimes	.long	0
5344Srgrimeseicode:
5354Srgrimes
5364Srgrimes	.globl	_szicode
5374Srgrimes_szicode:
5384Srgrimes	.long	_szicode-_icode
5394Srgrimes
540200SdgNON_GPROF_ENTRY(sigcode)
541592Srgrimes	call	SIGF_HANDLER(%esp)
542757Sdg	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
543757Sdg						/* copy at 8(%esp)) */
5444Srgrimes	pushl	%eax
545757Sdg	pushl	%eax				/* junk to fake return address */
546757Sdg	movl	$103,%eax			/* XXX sigreturn() */
547757Sdg	LCALL(0x7,0)				/* enter kernel with args on stack */
548757Sdg	hlt					/* never gets here */
5494Srgrimes
5504Srgrimes	.globl	_szsigcode
5514Srgrimes_szsigcode:
5524Srgrimes	.long	_szsigcode-_sigcode
553