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