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