locore.s revision 10826
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	from: @(#)locore.s	7.3 (Berkeley) 5/13/91
37 *	$Id: locore.s,v 1.55 1995/09/15 02:13:18 davidg Exp $
38 */
39
40/*
41 * locore.s:	FreeBSD machine support for the Intel 386
42 *		originally from: locore.s, by William F. Jolitz
43 *
44 *		Substantially rewritten by David Greenman, Rod Grimes,
45 *			Bruce Evans, Wolfgang Solfrank, and many others.
46 */
47
48#include "assym.s"			/* system definitions */
49#include <machine/psl.h>		/* processor status longword defs */
50#include <machine/pte.h>		/* page table entry definitions */
51#include <sys/errno.h>			/* error return codes */
52#include <machine/specialreg.h>		/* x86 special registers */
53#include <machine/cputypes.h>		/* x86 cpu type definitions */
54#include <sys/syscall.h>		/* system call numbers */
55#include <machine/asmacros.h>		/* miscellaneous asm macros */
56#include <sys/reboot.h>
57#include "apm.h"
58
59/*
60 *	XXX
61 *
62 * Note: This version greatly munged to avoid various assembler errors
63 * that may be fixed in newer versions of gas. Perhaps newer versions
64 * will have more pleasant appearance.
65 */
66
67/*
68 * PTmap is recursive pagemap at top of virtual address space.
69 * Within PTmap, the page directory can be found (third indirection).
70 */
71	.globl	_PTmap,_PTD,_PTDpde
72	.set	_PTmap,PTDPTDI << PDRSHIFT
73	.set	_PTD,_PTmap + (PTDPTDI * NBPG)
74	.set	_PTDpde,_PTD + (PTDPTDI * PDESIZE)
75
76/*
77 * Sysmap is the base address of the kernel page tables.
78 * It is a bogus interface for kgdb and isn't used by the kernel itself.
79 */
80	.set	_Sysmap,_PTmap + (KPTDI * NBPG)
81
82/*
83 * APTmap, APTD is the alternate recursive pagemap.
84 * It's used when modifying another process's page tables.
85 */
86	.globl	_APTmap,_APTD,_APTDpde
87	.set	_APTmap,APTDPTDI << PDRSHIFT
88	.set	_APTD,_APTmap + (APTDPTDI * NBPG)
89	.set	_APTDpde,_PTD + (APTDPTDI * PDESIZE)
90
91/*
92 * Access to each processes kernel stack is via a region of
93 * per-process address space (at the beginning), immediatly above
94 * the user process stack.
95 */
96	.set	_kstack,USRSTACK
97	.globl	_kstack
98
99/*
100 * Globals
101 */
102	.data
103
104	.globl	tmpstk
105	.space	0x1000		/* space for tmpstk - temporary stack */
106tmpstk:
107/*
108 * Dummy frame at top of tmpstk to help debuggers print a nice stack trace.
109 */
110	.long	tmpstk+8	/* caller's %ebp */
111	.long	_cpu_switch	/* caller */
112	.long	0		/* %ebp == 0 should terminate trace */
113	.long	_mvesp		/* in case %ebp == 0 doesn't work ... */
114	.long	0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555
115
116	.globl	_boothowto,_bootdev
117
118	.globl	_cpu,_cold,_atdevbase,_cpu_vendor,_cpu_id,_bootinfo
119	.globl	_cpu_high, _cpu_feature
120
121_cpu:	.long	0				/* are we 386, 386sx, or 486 */
122_cpu_id:	.long	0			/* stepping ID */
123_cpu_high:	.long	0			/* highest arg to CPUID */
124_cpu_feature:	.long	0			/* features */
125_cpu_vendor:	.space	20			/* CPU origin code */
126_bootinfo:	.space	BOOTINFO_SIZE		/* bootinfo that we can handle */
127_cold:	.long	1				/* cold till we are not */
128_atdevbase:	.long	0			/* location of start of iomem in virtual */
129_atdevphys:	.long	0			/* location of device mapping ptes (phys) */
130
131_KERNend:	.long	0			/* phys addr end of kernel (just after bss) */
132
133	.globl	_IdlePTD
134_IdlePTD:	.long	0			/* phys addr of kernel PTD */
135
136_KPTphys:	.long	0			/* phys addr of kernel page tables */
137
138	.globl	_proc0paddr
139_proc0paddr:	.long	0			/* address of proc 0 address space */
140
141#ifdef BDE_DEBUGGER
142	.globl	_bdb_exists			/* flag to indicate BDE debugger is available */
143_bdb_exists:	.long	0
144#endif
145
146/*
147 * System Initialization
148 */
149	.text
150
151/*
152 * btext: beginning of text section.
153 * Also the entry point (jumped to directly from the boot blocks).
154 */
155NON_GPROF_ENTRY(btext)
156	movw	$0x1234,0x472			/* warm boot */
157
158	/* Set up a real frame, some day we will be doing returns */
159	pushl	%ebp
160	movl	%esp, %ebp
161
162	/* Don't trust what the BIOS gives for eflags. */
163	pushl	$PSL_KERNEL
164	popfl
165
166	/* Don't trust what the BIOS gives for %fs and %gs. */
167	mov	%ds, %ax
168	mov	%ax, %fs
169	mov	%ax, %gs
170
171	/*
172	 * This code is called in different ways depending on what loaded
173	 * and started the kernel.  This is used to detect how we get the
174	 * arguments from the other code and what we do with them.
175	 *
176	 * Old disk boot blocks:
177	 *	(*btext)(howto, bootdev, cyloffset, esym);
178	 *	[return address == 0, and can NOT be returned to]
179	 *	[cyloffset was not supported by the FreeBSD boot code
180	 *	 and always passed in as 0]
181	 *	[esym is also known as total in the boot code, and
182	 *	 was never properly supported by the FreeBSD boot code]
183	 *
184	 * Old diskless netboot code:
185	 *	(*btext)(0,0,0,0,&nfsdiskless,0,0,0);
186	 *	[return address != 0, and can NOT be returned to]
187	 *	If we are being booted by this code it will NOT work,
188	 *	so we are just going to halt if we find this case.
189	 *
190	 * New uniform boot code:
191	 *	(*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
192	 *	[return address != 0, and can be returned to]
193	 *
194	 * There may seem to be a lot of wasted arguments in here, but
195	 * that is so the newer boot code can still load very old kernels
196	 * and old boot code can load new kernels.
197	 */
198
199	/*
200	 * The old style disk boot blocks fake a frame on the stack and
201	 * did an lret to get here.  The frame on the stack has a return
202	 * address of 0.
203	 */
204	cmpl	$0,4(%ebp)
205	je	2f				/* olddiskboot: */
206
207	/*
208	 * We have some form of return address, so this is either the
209	 * old diskless netboot code, or the new uniform code.  That can
210	 * be detected by looking at the 5th argument, it if is 0 we
211	 * we are being booted by the new unifrom boot code.
212	 */
213	cmpl	$0,24(%ebp)
214	je	1f				/* newboot: */
215
216	/*
217	 * Seems we have been loaded by the old diskless boot code, we
218	 * don't stand a chance of running as the diskless structure
219	 * changed considerably between the two, so just halt.
220	 */
221	 hlt
222
223	/*
224	 * We have been loaded by the new uniform boot code.
225	 * Lets check the bootinfo version, and if we do not understand
226	 * it we return to the loader with a status of 1 to indicate this error
227	 */
2281:	/* newboot: */
229	movl	28(%ebp),%ebx		/* &bootinfo.version */
230	movl	BI_VERSION(%ebx),%eax
231	cmpl	$1,%eax			/* We only understand version 1 */
232	je	1f
233	movl	$1,%eax			/* Return status */
234	leave
235	ret
236
2371:
238	/*
239	 * If we have a kernelname copy it in
240	 */
241	movl	BI_KERNELNAME(%ebx),%esi
242	cmpl	$0,%esi
243	je	2f			/* No kernelname */
244	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
245	lea	_kernelname-KERNBASE,%edi
246	cmpb	$'/',(%esi)		/* Make sure it starts with a slash */
247	je	1f
248	movb	$'/',(%edi)
249	incl	%edi
250	decl	%ecx
2511:
252	cld
253	rep
254	movsb
255
2562:
257	/*
258	 * Determine the size of the boot loader's copy of the bootinfo
259	 * struct.  This is impossible to do properly because old versions
260	 * of the struct don't contain a size field and there are 2 old
261	 * versions with the same version number.
262	 */
263	movl	$BI_ENDCOMMON,%ecx	/* prepare for sizeless version */
264	testl	$RB_BOOTINFO,8(%ebp)	/* bi_size (and bootinfo) valid? */
265	je	got_bi_size		/* no, sizeless version */
266	movl	BI_SIZE(%ebx),%ecx
267got_bi_size:
268
269	/*
270	 * Copy the common part of the bootinfo struct
271	 */
272	movl	%ebx,%esi
273	movl	$_bootinfo-KERNBASE,%edi
274	cmpl	$BOOTINFO_SIZE,%ecx
275	jbe	got_common_bi_size
276	movl	$BOOTINFO_SIZE,%ecx
277got_common_bi_size:
278	cld
279	rep
280	movsb
281
282#ifdef NFS
283	/*
284	 * If we have a nfs_diskless structure copy it in
285	 */
286	movl	BI_NFS_DISKLESS(%ebx),%esi
287	cmpl	$0,%esi
288	je	2f
289	lea	_nfs_diskless-KERNBASE,%edi
290	movl	$NFSDISKLESS_SIZE,%ecx
291	cld
292	rep
293	movsb
294	lea	_nfs_diskless_valid-KERNBASE,%edi
295	movl	$1,(%edi)
296#endif
297
298	/*
299	 * The old style disk boot.
300	 *	(*btext)(howto, bootdev, cyloffset, esym);
301	 * Note that the newer boot code just falls into here to pick
302	 * up howto and bootdev, cyloffset and esym are no longer used
303	 */
3042:	/* olddiskboot: */
305	movl	8(%ebp),%eax
306	movl	%eax,_boothowto-KERNBASE
307	movl	12(%ebp),%eax
308	movl	%eax,_bootdev-KERNBASE
309
310#if NAPM > 0
311	/* call APM BIOS driver setup (i386/apm/apm_setup.s) */
312	call	_apm_setup
313#endif /* NAPM */
314
315	/* Find out our CPU type. */
316
317	/* Try to toggle alignment check flag; does not exist on 386. */
318	pushfl
319	popl	%eax
320	movl	%eax,%ecx
321	orl	$PSL_AC,%eax
322	pushl	%eax
323	popfl
324	pushfl
325	popl	%eax
326	xorl	%ecx,%eax
327	andl	$PSL_AC,%eax
328	pushl	%ecx
329	popfl
330
331	testl	%eax,%eax
332	jnz	1f
333	movl	$CPU_386,_cpu-KERNBASE
334	jmp	2f
335
3361:	/* Try to toggle identification flag; does not exist on early 486s. */
337	pushfl
338	popl	%eax
339	movl	%eax,%ecx
340	xorl	$PSL_ID,%eax
341	pushl	%eax
342	popfl
343	pushfl
344	popl	%eax
345	xorl	%ecx,%eax
346	andl	$PSL_ID,%eax
347	pushl	%ecx
348	popfl
349
350	testl	%eax,%eax
351	jnz	1f
352	movl	$CPU_486,_cpu-KERNBASE
353
354	/* check for Cyrix 486DLC -- based on check routine  */
355	/* documented in "Cx486SLC/e SMM Programmer's Guide" */
356	xorw	%dx,%dx
357	cmpw	%dx,%dx			# set flags to known state
358	pushfw
359	popw	%cx			# store flags in ecx
360	movw	$0xffff,%ax
361	movw	$0x0004,%bx
362	divw	%bx
363	pushfw
364	popw	%ax
365	andw	$0x08d5,%ax		# mask off important bits
366	andw	$0x08d5,%cx
367	cmpw	%ax,%cx
368
369	jnz	2f			# if flags changed, Intel chip
370
371	movl	$CPU_486DLC,_cpu-KERNBASE # set CPU value for Cyrix
372	movl	$0x69727943,_cpu_vendor-KERNBASE	# store vendor string
373	movw	$0x0078,_cpu_vendor-KERNBASE+4
374
375#ifndef CYRIX_CACHE_WORKS
376	/* Disable caching of the ISA hole only. */
377	invd
378	movb	$CCR0,%al		# Configuration Register index (CCR0)
379	outb	%al,$0x22
380	inb	$0x23,%al
381	orb	$(CCR0_NC1|CCR0_BARB),%al
382	movb	%al,%ah
383	movb	$CCR0,%al
384	outb	%al,$0x22
385	movb	%ah,%al
386	outb	%al,$0x23
387	invd
388#else /* CYRIX_CACHE_WORKS */
389	/* Set cache parameters */
390	invd				# Start with guaranteed clean cache
391	movb	$CCR0,%al		# Configuration Register index (CCR0)
392	outb	%al,$0x22
393	inb	$0x23,%al
394	andb	$~CCR0_NC0,%al
395#ifndef CYRIX_CACHE_REALLY_WORKS
396	orb	$(CCR0_NC1|CCR0_BARB),%al
397#else
398	orb	$CCR0_NC1,%al
399#endif
400	movb	%al,%ah
401	movb	$CCR0,%al
402	outb	%al,$0x22
403	movb	%ah,%al
404	outb	%al,$0x23
405	/* clear non-cacheable region 1	*/
406	movb	$(NCR1+2),%al
407	outb	%al,$0x22
408	movb	$NCR_SIZE_0K,%al
409	outb	%al,$0x23
410	/* clear non-cacheable region 2	*/
411	movb	$(NCR2+2),%al
412	outb	%al,$0x22
413	movb	$NCR_SIZE_0K,%al
414	outb	%al,$0x23
415	/* clear non-cacheable region 3	*/
416	movb	$(NCR3+2),%al
417	outb	%al,$0x22
418	movb	$NCR_SIZE_0K,%al
419	outb	%al,$0x23
420	/* clear non-cacheable region 4	*/
421	movb	$(NCR4+2),%al
422	outb	%al,$0x22
423	movb	$NCR_SIZE_0K,%al
424	outb	%al,$0x23
425	/* enable caching in CR0 */
426	movl	%cr0,%eax
427	andl	$~(CR0_CD|CR0_NW),%eax
428	movl	%eax,%cr0
429	invd
430#endif /* CYRIX_CACHE_WORKS */
431	jmp	2f
432
4331:	/* Use the `cpuid' instruction. */
434	xorl	%eax,%eax
435	.byte	0x0f,0xa2			# cpuid 0
436	movl	%eax,_cpu_high-KERNBASE		# highest capability
437	movl	%ebx,_cpu_vendor-KERNBASE	# store vendor string
438	movl	%edx,_cpu_vendor+4-KERNBASE
439	movl	%ecx,_cpu_vendor+8-KERNBASE
440	movb	$0,_cpu_vendor+12-KERNBASE
441
442	movl	$1,%eax
443	.byte	0x0f,0xa2			# cpuid 1
444	movl	%eax,_cpu_id-KERNBASE		# store cpu_id
445	movl	%edx,_cpu_feature-KERNBASE	# store cpu_feature
446	rorl	$8,%eax				# extract family type
447	andl	$15,%eax
448	cmpl	$5,%eax
449	jae	1f
450
451	/* less than Pentium; must be 486 */
452	movl	$CPU_486,_cpu-KERNBASE
453	jmp	2f
454
4551:	movl	$CPU_586,_cpu-KERNBASE
4562:
457
458	/*
459	 * Finished with old stack; load new %esp now instead of later so
460	 * we can trace this code without having to worry about the trace
461	 * trap clobbering the memory test or the zeroing of the bss+bootstrap
462	 * page tables.
463	 *
464	 * XXX - wdboot clears the bss after testing that this is safe.
465	 * This is too wasteful - memory below 640K is scarce.  The boot
466	 * program should check:
467	 *	text+data <= &stack_variable - more_space_for_stack
468	 *	text+data+bss+pad+space_for_page_tables <= end_of_memory
469	 * Oops, the gdt is in the carcass of the boot program so clearing
470	 * the rest of memory is still not possible.
471	 */
472	movl	$tmpstk-KERNBASE,%esp		/* bootstrap stack end location */
473
474/*
475 * Virtual address space of kernel:
476 *
477 *	text | data | bss | [syms] | page dir | proc0 kernel stack | usr stk map | Sysmap
478 *      pages:                          1         UPAGES (2)             1         NKPT (7)
479 */
480
481/* find end of kernel image */
482	movl	$_end-KERNBASE,%ecx
483	addl	$NBPG-1,%ecx			/* page align up */
484	andl	$~(NBPG-1),%ecx
485	movl	%ecx,%esi			/* esi = start of free memory */
486	movl	%ecx,_KERNend-KERNBASE		/* save end of kernel */
487
488/* clear bss */
489	movl	$_edata-KERNBASE,%edi
490	subl	%edi,%ecx			/* get amount to clear */
491	xorl	%eax,%eax			/* specify zero fill */
492	cld
493	rep
494	stosb
495
496#ifdef DDB
497/* include symbols in "kernel image" if they are loaded */
498	movl	_bootinfo+BI_ESYMTAB-KERNBASE,%edi
499	testl	%edi,%edi
500	je	over_symalloc
501	addl	$NBPG-1,%edi
502	andl	$~(NBPG-1),%edi
503	movl	%edi,%esi
504	movl	%esi,_KERNend-KERNBASE
505	movl	$KERNBASE,%edi
506	addl	%edi,_bootinfo+BI_SYMTAB-KERNBASE
507	addl	%edi,_bootinfo+BI_ESYMTAB-KERNBASE
508over_symalloc:
509#endif
510
511/*
512 * The value in esi is both the end of the kernel bss and a pointer to
513 * the kernel page directory, and is used by the rest of locore to build
514 * the tables.
515 * esi + 1(page dir) + 2(UPAGES) + 1(p0stack) + NKPT(number of kernel
516 * page table pages) is then passed on the stack to init386(first) as
517 * the value first. esi should ALWAYS be page aligned!!
518 */
519	movl	%esi,%ecx			/* Get current first availiable address */
520
521/* clear pagetables, page directory, stack, etc... */
522	movl	%esi,%edi			/* base (page directory) */
523	movl	$((1+UPAGES+1+NKPT)*NBPG),%ecx	/* amount to clear */
524	xorl	%eax,%eax			/* specify zero fill */
525	cld
526	rep
527	stosb
528
529/* physical address of Idle proc/kernel page directory */
530	movl	%esi,_IdlePTD-KERNBASE
531
532/*
533 * fillkpt
534 *	eax = (page frame address | control | status) == pte
535 *	ebx = address of page table
536 *	ecx = how many pages to map
537 */
538#define	fillkpt		\
5391:	movl	%eax,(%ebx)	; \
540	addl	$NBPG,%eax	; /* increment physical address */ \
541	addl	$4,%ebx		; /* next pte */ \
542	loop	1b		;
543
544/*
545 * Map Kernel
546 *
547 * First step - build page tables
548 */
549#if defined (KGDB) || defined (BDE_DEBUGGER)
550	movl	_KERNend-KERNBASE,%ecx		/* this much memory, */
551	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
552#ifdef BDE_DEBUGGER
553	cmpl	$0xa0,%ecx			/* XXX - cover debugger pages */
554	jae	1f
555	movl	$0xa0,%ecx
5561:
557#endif /* BDE_DEBUGGER */
558	movl	$PG_V|PG_KW,%eax		/* kernel R/W, valid */
559	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
560	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
561	fillkpt
562
563#else /* !KGDB && !BDE_DEBUGGER */
564	/* write protect kernel text (doesn't do a thing for 386's - only 486's) */
565	movl	$_etext-KERNBASE,%ecx		/* get size of text */
566	addl	$NBPG-1,%ecx			/* round up to page */
567	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
568	movl	$PG_V|PG_KR,%eax		/* specify read only */
569#if 0
570	movl	$_etext,%ecx			/* get size of text */
571	subl	$_btext,%ecx
572	addl	$NBPG-1,%ecx			/* round up to page */
573	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
574	movl	$_btext-KERNBASE,%eax		/* get offset to physical memory */
575	orl	$PG_V|PG_KR,%eax		/* specify read only */
576#endif
577	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
578	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
579	fillkpt
580
581	/* data and bss are r/w */
582	andl	$PG_FRAME,%eax			/* strip to just addr of bss */
583	movl	_KERNend-KERNBASE,%ecx		/* calculate size */
584	subl	%eax,%ecx
585	shrl	$PGSHIFT,%ecx
586	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
587	fillkpt
588#endif /* KGDB || BDE_DEBUGGER */
589
590/* now initialize the page dir, upages, and p0stack PT */
591
592	movl	$(1+UPAGES+1),%ecx		/* number of PTEs */
593	movl	%esi,%eax			/* phys address of PTD */
594	andl	$PG_FRAME,%eax			/* convert to PFN, should be a NOP */
595	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
596	movl	%esi,%ebx			/* calculate pte offset to ptd */
597	shrl	$PGSHIFT-2,%ebx
598	addl	%esi,%ebx			/* address of page directory */
599	addl	$((1+UPAGES+1)*NBPG),%ebx	/* offset to kernel page tables */
600	fillkpt
601
602/* map I/O memory map */
603
604	movl    _KPTphys-KERNBASE,%ebx		/* base of kernel page tables */
605	lea     (0xa0 * PTESIZE)(%ebx),%ebx	/* hardwire ISA hole at KERNBASE + 0xa0000 */
606	movl	$0x100-0xa0,%ecx		/* for this many pte s, */
607	movl	$(0xa0000|PG_V|PG_KW|PG_N),%eax	/* valid, kernel read/write, non-cacheable */
608	movl	%ebx,_atdevphys-KERNBASE	/* save phys addr of ptes */
609	fillkpt
610
611 /* map proc 0's kernel stack into user page table page */
612
613	movl	$UPAGES,%ecx			/* for this many pte s, */
614	lea	(1*NBPG)(%esi),%eax		/* physical address in proc 0 */
615	lea	(KERNBASE)(%eax),%edx		/* change into virtual addr */
616	movl	%edx,_proc0paddr-KERNBASE	/* save VA for proc 0 init */
617	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
618	lea	((1+UPAGES)*NBPG)(%esi),%ebx	/* addr of stack page table in proc 0 */
619	addl	$(KSTKPTEOFF * PTESIZE),%ebx	/* offset to kernel stack PTE */
620	fillkpt
621
622/*
623 * Initialize kernel page table directory
624 */
625	/* install a pde for temporary double map of bottom of VA */
626	movl	_KPTphys-KERNBASE,%eax
627	orl     $PG_V|PG_KW,%eax		/* valid, kernel read/write */
628	movl	%eax,(%esi)			/* which is where temp maps! */
629
630	/* initialize kernel pde's */
631	movl	$(NKPT),%ecx			/* for this many PDEs */
632	lea	(KPTDI*PDESIZE)(%esi),%ebx	/* offset of pde for kernel */
633	fillkpt
634
635	/* install a pde recursively mapping page directory as a page table! */
636	movl	%esi,%eax			/* phys address of ptd in proc 0 */
637	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
638	movl	%eax,PTDPTDI*PDESIZE(%esi)	/* which is where PTmap maps! */
639
640	/* install a pde to map kernel stack for proc 0 */
641	lea	((1+UPAGES)*NBPG)(%esi),%eax	/* physical address of pt in proc 0 */
642	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
643	movl	%eax,KSTKPTDI*PDESIZE(%esi)	/* which is where kernel stack maps! */
644
645#ifdef BDE_DEBUGGER
646	/* copy and convert stuff from old gdt and idt for debugger */
647
648	cmpl	$0x0375c339,0x96104		/* XXX - debugger signature */
649	jne	1f
650	movb	$1,_bdb_exists-KERNBASE
6511:
652	pushal
653	subl	$2*6,%esp
654
655	sgdt	(%esp)
656	movl	2(%esp),%esi			/* base address of current gdt */
657	movl	$_gdt-KERNBASE,%edi
658	movl	%edi,2(%esp)
659	movl	$8*18/4,%ecx
660	cld
661	rep					/* copy gdt */
662	movsl
663	movl	$_gdt-KERNBASE,-8+2(%edi)	/* adjust gdt self-ptr */
664	movb	$0x92,-8+5(%edi)
665
666	sidt	6(%esp)
667	movl	6+2(%esp),%esi			/* base address of current idt */
668	movl	8+4(%esi),%eax			/* convert dbg descriptor to ... */
669	movw	8(%esi),%ax
670	movl	%eax,bdb_dbg_ljmp+1-KERNBASE	/* ... immediate offset ... */
671	movl	8+2(%esi),%eax
672	movw	%ax,bdb_dbg_ljmp+5-KERNBASE	/* ... and selector for ljmp */
673	movl	24+4(%esi),%eax			/* same for bpt descriptor */
674	movw	24(%esi),%ax
675	movl	%eax,bdb_bpt_ljmp+1-KERNBASE
676	movl	24+2(%esi),%eax
677	movw	%ax,bdb_bpt_ljmp+5-KERNBASE
678
679	movl	$_idt-KERNBASE,%edi
680	movl	%edi,6+2(%esp)
681	movl	$8*4/4,%ecx
682	cld
683	rep					/* copy idt */
684	movsl
685
686	lgdt	(%esp)
687	lidt	6(%esp)
688
689	addl	$2*6,%esp
690	popal
691#endif /* BDE_DEBUGGER */
692
693	/* load base of page directory and enable mapping */
694	movl	%esi,%eax			/* phys address of ptd in proc 0 */
695	movl	%eax,%cr3			/* load ptd addr into mmu */
696	movl	%cr0,%eax			/* get control word */
697	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
698	movl	%eax,%cr0			/* and let's page NOW! */
699
700	pushl	$begin				/* jump to high mem */
701	ret
702
703begin: /* now running relocated at KERNBASE where the system is linked to run */
704	movl	_atdevphys,%edx			/* get pte PA */
705	subl	_KPTphys,%edx			/* remove base of ptes, now have phys offset */
706	shll	$PGSHIFT-2,%edx			/* corresponding to virt offset */
707	addl	$KERNBASE,%edx			/* add virtual base */
708	movl	%edx,_atdevbase
709
710	/* set up bootstrap stack */
711	movl	$_kstack+UPAGES*NBPG,%esp	/* bootstrap stack end location */
712	xorl	%eax,%eax			/* mark end of frames */
713	movl	%eax,%ebp
714	movl	_proc0paddr,%eax
715	movl	%esi,PCB_CR3(%eax)
716
717#ifdef BDE_DEBUGGER
718	/* relocate debugger gdt entries */
719
720	movl	$_gdt+8*9,%eax			/* adjust slots 9-17 */
721	movl	$9,%ecx
722reloc_gdt:
723	movb	$KERNBASE>>24,7(%eax)		/* top byte of base addresses, was 0, */
724	addl	$8,%eax				/* now KERNBASE>>24 */
725	loop	reloc_gdt
726
727	cmpl	$0,_bdb_exists
728	je	1f
729	int	$3
7301:
731#endif /* BDE_DEBUGGER */
732
733	/*
734	 * Prepare "first" - physical address of first available page
735	 * after the kernel+pdir+upages+p0stack+page tables
736	 */
737	lea	((1+UPAGES+1+NKPT)*NBPG)(%esi),%esi
738
739	pushl	%esi				/* value of first for init386(first) */
740	call	_init386			/* wire 386 chip for unix operation */
741	popl	%esi
742
743	.globl	__ucodesel,__udatasel
744
745	pushl	$0				/* unused */
746	pushl	__udatasel			/* ss */
747	pushl	$0				/* esp - filled in by execve() */
748	pushl	$PSL_USER			/* eflags (IOPL 0, int enab) */
749	pushl	__ucodesel			/* cs */
750	pushl	$0				/* eip - filled in by execve() */
751	subl	$(12*4),%esp			/* space for rest of registers */
752
753	pushl	%esp				/* call main with frame pointer */
754	call	_main				/* autoconfiguration, mountroot etc */
755
756	addl	$(13*4),%esp			/* back to a frame we can return with */
757
758	/*
759	 * now we've run main() and determined what cpu-type we are, we can
760	 * enable write protection and alignment checking on i486 cpus and
761	 * above.
762	 */
763#if defined(I486_CPU) || defined(I586_CPU)
764	cmpl    $CPUCLASS_386,_cpu_class
765	je	1f
766	movl	%cr0,%eax			/* get control word */
767	orl	$CR0_WP|CR0_AM,%eax		/* enable i486 features */
768	movl	%eax,%cr0			/* and do it */
769#endif
770	/*
771	 * on return from main(), we are process 1
772	 * set up address space and stack so that we can 'return' to user mode
773	 */
7741:
775	movl	__ucodesel,%eax
776	movl	__udatasel,%ecx
777
778	movl	%cx,%ds
779	movl	%cx,%es
780	movl	%ax,%fs				/* double map cs to fs */
781	movl	%cx,%gs				/* and ds to gs */
782	iret					/* goto user! */
783
784#define LCALL(x,y)	.byte 0x9a ; .long y ; .word x
785
786NON_GPROF_ENTRY(sigcode)
787	call	SIGF_HANDLER(%esp)
788	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
789						/* copy at 8(%esp)) */
790	pushl	%eax
791	pushl	%eax				/* junk to fake return address */
792	movl	$103,%eax			/* XXX sigreturn() */
793	LCALL(0x7,0)				/* enter kernel with args on stack */
794	hlt					/* never gets here */
795
796	.globl	_szsigcode
797_szsigcode:
798	.long	_szsigcode-_sigcode
799