locore.s revision 3406
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.33 1994/10/06 09:22:47 rgrimes 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#ifdef APM
58#define ASM
59#include <machine/apm_bios.h>
60#include <machine/apm_segments.h>
61#endif
62
63/*
64 *	XXX
65 *
66 * Note: This version greatly munged to avoid various assembler errors
67 * that may be fixed in newer versions of gas. Perhaps newer versions
68 * will have more pleasant appearance.
69 */
70
71/*
72 * PTmap is recursive pagemap at top of virtual address space.
73 * Within PTmap, the page directory can be found (third indirection).
74 */
75	.globl	_PTmap,_PTD,_PTDpde,_Sysmap
76	.set	_PTmap,PTDPTDI << PDRSHIFT
77	.set	_PTD,_PTmap + (PTDPTDI * NBPG)
78	.set	_PTDpde,_PTD + (PTDPTDI * PDESIZE)
79
80/* Sysmap is the base address of the kernel page tables */
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
105	.globl	_boothowto,_bootdev,_curpcb
106
107	.globl	_cpu,_cold,_atdevbase,_cpu_vendor,_cpu_id
108
109 	.globl	_video_mode_ptr
110
111_cpu:	.long	0				/* are we 386, 386sx, or 486 */
112_cpu_id:	.long	0			/* stepping ID */
113_cpu_vendor:	.space	20			/* CPU origin code */
114_video_mode_ptr:	.long 0
115_cold:	.long	1				/* cold till we are not */
116_atdevbase:	.long	0			/* location of start of iomem in virtual */
117_atdevphys:	.long	0			/* location of device mapping ptes (phys) */
118
119	.globl	_KERNend
120_KERNend:	.long	0			/* phys addr end of kernel (just after bss) */
121
122	.globl	_IdlePTD,_KPTphys
123_IdlePTD:	.long	0			/* phys addr of kernel PTD */
124_KPTphys:	.long	0			/* phys addr of kernel page tables */
125
126	.globl	_proc0paddr
127_proc0paddr:	.long	0			/* address of proc 0 address space */
128
129#ifdef BDE_DEBUGGER
130	.globl	_bdb_exists			/* flag to indicate BDE debugger is available */
131_bdb_exists:	.long	0
132#endif
133#ifdef APM
134	.globl	_apm_current_gdt_pdesc		/* current GDT pseudo desc. */
135_apm_current_gdt_pdesc:
136	.word	0, 0, 0
137
138	.globl	_bootstrap_gdt
139_bootstrap_gdt:
140	.space	SIZEOF_GDT * BOOTSTRAP_GDT_NUM
141#endif /* APM */
142	.globl	tmpstk
143	.space	0x1000
144tmpstk:
145
146
147/*
148 * System Initialization
149 */
150	.text
151
152/*
153 * btext: beginning of text section.
154 * Also the entry point (jumped to directly from the boot blocks).
155 */
156NON_GPROF_ENTRY(btext)
157	movw	$0x1234,0x472			/* warm boot */
158	jmp	1f
159	/*
160	 * XXX now that we load at 1MB is this still really used?
161	 */
162	.org	0x500				/* space for BIOS variables */
163
1641:
165	/* Set up a real frame, some day we will be doing returns */
166	pushl	%ebp
167	movl	%esp, %ebp
168
169	/* Don't trust what the BIOS gives for eflags. */
170	pushl	$PSL_MBO
171	popfl
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	BOOTINFO_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	BOOTINFO_KERNELNAME(%ebx),%esi
244	cmpl	$0,%esi
245	je	1f			/* No kernelname */
246	lea	_kernelname-KERNBASE,%edi
247	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
248	cld
249	rep
250	movsb
251
2521:
253#ifdef DISKLESS
254	/*
255	 * If we have a nfs_diskless structure copy it in
256	 */
257	movl	BOOTINFO_NFS_DISKLESS(%ebx),%esi
258	cmpl	$0,%esi
259	je	2f
260	lea	_nfs_diskless-KERNBASE,%edi
261	movl	$NFSDISKLESS_SIZE,%ecx
262	cld
263	rep
264	movsb
265#endif
266
267	/*
268	 * The old style disk boot.
269	 *	(*btext)(howto, bootdev, cyloffset, esym);
270	 * Note that the newer boot code just falls into here to pick
271	 * up howto and bootdev, cyloffset and esym are no longer used
272	 */
2732:	/* olddiskboot: */
274	movl	8(%ebp),%eax
275	movl	%eax,_boothowto-KERNBASE
276	movl	12(%ebp),%eax
277	movl	%eax,_bootdev-KERNBASE
278
279	/* get the BIOS video mode pointer */
280 	movl	$0x4a8, %ecx
281 	movl	(%ecx), %eax
282 	movl	%eax, %ecx
283 	shrl	$12, %ecx
284 	andl	$0xffff0000, %ecx
285 	andl	$0x0000ffff, %eax
286 	orl	%ecx, %eax
287 	movl	(%eax), %eax
288 	movl	%eax, %ecx
289 	shrl	$12, %ecx
290 	andl	$0xffff0000, %ecx
291 	andl	$0x0000ffff, %eax
292 	orl	%ecx, %eax
293 	addl	$KERNBASE, %eax
294 	movl	%eax, _video_mode_ptr-KERNBASE
295
296#ifdef APM
297	/*
298	 * Setup APM BIOS:
299	 *
300	 * APM BIOS initialization should be done from real mode or V86 mode.
301	 *
302	 * (by HOSOKAWA, Tatsumi <hosokawa@mt.cs.keio.ac.jp>)
303	 */
304
305	/*
306         * Copy APM initializer under 1MB boundary:
307	 *
308	 * APM initializer program must switch the CPU to real mode.
309	 * But FreeBSD kernel runs above 1MB boundary. So we must
310	 * copy the initializer code to conventional memory.
311	 */
312	movl	_apm_init_image_size-KERNBASE, %ecx	/* size */
313	lea	_apm_init_image-KERNBASE, %esi		/* source */
314	movl	$ APM_OURADDR, %edi			/* destination */
315	cld
316	rep
317	movsb
318
319	/* get GDT base */
320	sgdt	_apm_current_gdt_pdesc-KERNBASE
321
322	/* copy GDT to _bootstrap_gdt */
323	xorl	%ecx, %ecx
324	movw	_apm_current_gdt_pdesc-KERNBASE, %cx
325	movl	_apm_current_gdt_pdesc-KERNBASE+2, %esi
326	lea	_bootstrap_gdt-KERNBASE, %edi
327	cld
328	rep
329	movsb
330
331	/* setup GDT pseudo descriptor */
332	movw	$(SIZEOF_GDT*BOOTSTRAP_GDT_NUM), %ax
333	movw	%ax, _apm_current_gdt_pdesc-KERNBASE
334	leal	_bootstrap_gdt-KERNBASE, %eax
335	movl	%eax, _apm_current_gdt_pdesc-KERNBASE+2
336
337	/* load new GDTR */
338	lgdt	_apm_current_gdt_pdesc-KERNBASE
339
340	/* setup GDT for APM initializer */
341	lea	_bootstrap_gdt-KERNBASE, %ecx
342	movl	$(APM_OURADDR), %eax	/* use %ax for 15..0 */
343	movl	%eax, %ebx
344	shrl	$16, %ebx		/* use %bl for 23..16 */
345					/* use %bh for 31..24 */
346#define APM_SETUP_GDT(index, attrib) \
347	movl	$(index), %si ; \
348	lea	0(%ecx,%esi,8), %edx ; \
349	movw	$0xffff, (%edx) ; \
350	movw	%ax, 2(%edx) ; \
351	movb	%bl, 4(%edx) ; \
352	movw	$(attrib), 5(%edx) ; \
353	movb	%bh, 7(%edx)
354
355	APM_SETUP_GDT(APM_INIT_CS_INDEX  , CS32_ATTRIB)
356	APM_SETUP_GDT(APM_INIT_DS_INDEX  , DS32_ATTRIB)
357	APM_SETUP_GDT(APM_INIT_CS16_INDEX, CS16_ATTRIB)
358
359	/*
360	 * Call the initializer:
361	 *
362	 * direct intersegment call to conventional memory code
363	 */
364	.byte	0x9a		/* actually, lcall $APM_INIT_CS_SEL, $0 */
365	.long	0
366	.word	APM_INIT_CS_SEL
367
368	movw	%ax, _apm_version-KERNBASE
369	movl	%ebx, _apm_cs_entry-KERNBASE
370	movw	%cx, _apm_cs32_base-KERNBASE
371	shrl	$16, %ecx
372	movw	%cx, _apm_cs16_base-KERNBASE
373	movw	%dx, _apm_ds_base-KERNBASE
374	movw	%si, _apm_cs_limit-KERNBASE
375	shrl	$16, %esi
376	movw	%si, _apm_ds_limit-KERNBASE
377	movw	%di, _apm_flags-KERNBASE
378#endif /* APM */
379
380	/* Find out our CPU type. */
381
382	/* Try to toggle alignment check flag; does not exist on 386. */
383	pushfl
384	popl	%eax
385	movl	%eax,%ecx
386	orl	$PSL_AC,%eax
387	pushl	%eax
388	popfl
389	pushfl
390	popl	%eax
391	xorl	%ecx,%eax
392	andl	$PSL_AC,%eax
393	pushl	%ecx
394	popfl
395
396	testl	%eax,%eax
397	jnz	1f
398	movl	$CPU_386,_cpu-KERNBASE
399	jmp	2f
400
4011:	/* Try to toggle identification flag; does not exist on early 486s. */
402	pushfl
403	popl	%eax
404	movl	%eax,%ecx
405	xorl	$PSL_ID,%eax
406	pushl	%eax
407	popfl
408	pushfl
409	popl	%eax
410	xorl	%ecx,%eax
411	andl	$PSL_ID,%eax
412	pushl	%ecx
413	popfl
414
415	testl	%eax,%eax
416	jnz	1f
417	movl	$CPU_486,_cpu-KERNBASE
418
419	/* check for Cyrix 486DLC -- based on check routine  */
420	/* documented in "Cx486SLC/e SMM Programmer's Guide" */
421	xorw	%dx,%dx
422	cmpw	%dx,%dx			# set flags to known state
423	pushfw
424	popw	%cx			# store flags in ecx
425	movw	$0xffff,%ax
426	movw	$0x0004,%bx
427	divw	%bx
428	pushfw
429	popw	%ax
430	andw	$0x08d5,%ax		# mask off important bits
431	andw	$0x08d5,%cx
432	cmpw	%ax,%cx
433
434	jnz	2f			# if flags changed, Intel chip
435
436	movl	$CPU_486DLC,_cpu-KERNBASE # set CPU value for Cyrix
437	movl	$0x69727943,_cpu_vendor-KERNBASE	# store vendor string
438	movw	$0x0078,_cpu_vendor-KERNBASE+4
439
440	invd				# Start with guaranteed clean cache
441	/* Disable caching of the ISA hole only. */
442	movb	$CCR0,%al		# Configuration Register index (CCR0)
443	outb	%al,$0x22
444	inb	$0x23,%al
445	orb	$(CCR0_NC1|CCR0_BARB),%al
446	outb	%al,$0x23
447	invd
448	jmp	2f
449
4501:	/* Use the `cpuid' instruction. */
451	xorl	%eax,%eax
452	.byte	0x0f,0xa2		# cpuid 0
453	movl	%ebx,_cpu_vendor-KERNBASE	# store vendor string
454	movl	%edx,_cpu_vendor+4-KERNBASE
455	movl	%ecx,_cpu_vendor+8-KERNBASE
456	movb	$0,_cpu_vendor+12-KERNBASE
457
458	movl	$1,%eax
459	.byte	0x0f,0xa2		# cpuid 1
460	movl	%eax,_cpu_id-KERNBASE		# store cpu_id
461	rorl	$8,%eax			# extract family type
462	andl	$15,%eax
463	cmpl	$5,%eax
464	jae	1f
465
466	/* less than Pentium; must be 486 */
467	movl	$CPU_486,_cpu-KERNBASE
468	jmp	2f
469
4701:	movl	$CPU_586,_cpu-KERNBASE
4712:
472
473	/*
474	 * Finished with old stack; load new %esp now instead of later so
475	 * we can trace this code without having to worry about the trace
476	 * trap clobbering the memory test or the zeroing of the bss+bootstrap
477	 * page tables.
478	 *
479	 * XXX - wdboot clears the bss after testing that this is safe.
480	 * This is too wasteful - memory below 640K is scarce.  The boot
481	 * program should check:
482	 *	text+data <= &stack_variable - more_space_for_stack
483	 *	text+data+bss+pad+space_for_page_tables <= end_of_memory
484	 * Oops, the gdt is in the carcass of the boot program so clearing
485	 * the rest of memory is still not possible.
486	 */
487	movl	$tmpstk-KERNBASE,%esp		/* bootstrap stack end location */
488
489/*
490 * Virtual address space of kernel:
491 *
492 *	text | data | bss | [syms] | page dir | proc0 kernel stack | usr stk map | Sysmap
493 *      pages:                          1         UPAGES (2)             1         NKPT (7)
494 */
495
496/* find end of kernel image */
497	movl	$_end-KERNBASE,%ecx
498	addl	$NBPG-1,%ecx			/* page align up */
499	andl	$~(NBPG-1),%ecx
500	movl	%ecx,%esi			/* esi = start of free memory */
501	movl	%ecx,_KERNend-KERNBASE		/* save end of kernel */
502
503/* clear bss */
504	movl	$_edata-KERNBASE,%edi
505	subl	%edi,%ecx			/* get amount to clear */
506	xorl	%eax,%eax			/* specify zero fill */
507	cld
508	rep
509	stosb
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	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
567	movl	$PG_V|PG_KR,%eax		/* specify read only */
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	/* data and bss are r/w */
573	andl	$PG_FRAME,%eax			/* strip to just addr of bss */
574	movl	_KERNend-KERNBASE,%ecx		/* calculate size */
575	subl	%eax,%ecx
576	shrl	$PGSHIFT,%ecx
577	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
578	fillkpt
579#endif /* KGDB || BDE_DEBUGGER */
580
581/* now initialize the page dir, upages, p0stack PT, and page tables */
582
583	movl	$(1+UPAGES+1+NKPT),%ecx	/* number of PTEs */
584	movl	%esi,%eax			/* phys address of PTD */
585	andl	$PG_FRAME,%eax			/* convert to PFN, should be a NOP */
586	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
587	movl	%esi,%ebx			/* calculate pte offset to ptd */
588	shrl	$PGSHIFT-2,%ebx
589	addl	%esi,%ebx			/* address of page directory */
590	addl	$((1+UPAGES+1)*NBPG),%ebx	/* offset to kernel page tables */
591	fillkpt
592
593/* map I/O memory map */
594
595	movl    _KPTphys-KERNBASE,%ebx		/* base of kernel page tables */
596	lea     (0xa0 * PTESIZE)(%ebx),%ebx	/* hardwire ISA hole at KERNBASE + 0xa0000 */
597	movl	$0x100-0xa0,%ecx		/* for this many pte s, */
598	movl	$(0xa0000|PG_V|PG_KW|PG_N),%eax	/* valid, kernel read/write, non-cacheable */
599	movl	%ebx,_atdevphys-KERNBASE	/* save phys addr of ptes */
600	fillkpt
601
602 /* map proc 0's kernel stack into user page table page */
603
604	movl	$UPAGES,%ecx			/* for this many pte s, */
605	lea	(1*NBPG)(%esi),%eax		/* physical address in proc 0 */
606	lea	(KERNBASE)(%eax),%edx		/* change into virtual addr */
607	movl	%edx,_proc0paddr-KERNBASE	/* save VA for proc 0 init */
608	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
609	lea	((1+UPAGES)*NBPG)(%esi),%ebx	/* addr of stack page table in proc 0 */
610	addl	$(KSTKPTEOFF * PTESIZE),%ebx	/* offset to kernel stack PTE */
611	fillkpt
612
613/*
614 * Initialize kernel page table directory
615 */
616	/* install a pde for temporary double map of bottom of VA */
617	movl	_KPTphys-KERNBASE,%eax
618	orl     $PG_V|PG_KW,%eax		/* valid, kernel read/write */
619	movl	%eax,(%esi)			/* which is where temp maps! */
620
621	/* initialize kernel pde's */
622	movl	$(NKPT),%ecx			/* for this many PDEs */
623	lea	(KPTDI*PDESIZE)(%esi),%ebx	/* offset of pde for kernel */
624	fillkpt
625
626	/* install a pde recursively mapping page directory as a page table! */
627	movl	%esi,%eax			/* phys address of ptd in proc 0 */
628	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
629	movl	%eax,PTDPTDI*PDESIZE(%esi)	/* which is where PTmap maps! */
630
631	/* install a pde to map kernel stack for proc 0 */
632	lea	((1+UPAGES)*NBPG)(%esi),%eax	/* physical address of pt in proc 0 */
633	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
634	movl	%eax,KSTKPTDI*PDESIZE(%esi)	/* which is where kernel stack maps! */
635
636#ifdef BDE_DEBUGGER
637	/* copy and convert stuff from old gdt and idt for debugger */
638
639	cmpl	$0x0375c339,0x96104		/* XXX - debugger signature */
640	jne	1f
641	movb	$1,_bdb_exists-KERNBASE
6421:
643	pushal
644	subl	$2*6,%esp
645
646	sgdt	(%esp)
647	movl	2(%esp),%esi			/* base address of current gdt */
648	movl	$_gdt-KERNBASE,%edi
649	movl	%edi,2(%esp)
650	movl	$8*18/4,%ecx
651	cld
652	rep					/* copy gdt */
653	movsl
654	movl	$_gdt-KERNBASE,-8+2(%edi)	/* adjust gdt self-ptr */
655	movb	$0x92,-8+5(%edi)
656
657	sidt	6(%esp)
658	movl	6+2(%esp),%esi			/* base address of current idt */
659	movl	8+4(%esi),%eax			/* convert dbg descriptor to ... */
660	movw	8(%esi),%ax
661	movl	%eax,bdb_dbg_ljmp+1-KERNBASE	/* ... immediate offset ... */
662	movl	8+2(%esi),%eax
663	movw	%ax,bdb_dbg_ljmp+5-KERNBASE	/* ... and selector for ljmp */
664	movl	24+4(%esi),%eax			/* same for bpt descriptor */
665	movw	24(%esi),%ax
666	movl	%eax,bdb_bpt_ljmp+1-KERNBASE
667	movl	24+2(%esi),%eax
668	movw	%ax,bdb_bpt_ljmp+5-KERNBASE
669
670	movl	$_idt-KERNBASE,%edi
671	movl	%edi,6+2(%esp)
672	movl	$8*4/4,%ecx
673	cld
674	rep					/* copy idt */
675	movsl
676
677	lgdt	(%esp)
678	lidt	6(%esp)
679
680	addl	$2*6,%esp
681	popal
682#endif /* BDE_DEBUGGER */
683
684	/* load base of page directory and enable mapping */
685	movl	%esi,%eax			/* phys address of ptd in proc 0 */
686	movl	%eax,%cr3			/* load ptd addr into mmu */
687	movl	%cr0,%eax			/* get control word */
688	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
689	movl	%eax,%cr0			/* and let's page NOW! */
690
691	pushl	$begin				/* jump to high mem */
692	ret
693
694begin: /* now running relocated at KERNBASE where the system is linked to run */
695
696	.globl _Crtat				/* XXX - locore should not know about */
697	movl	_Crtat,%eax			/* variables of device drivers (pccons)! */
698	subl	$(KERNBASE+0xA0000),%eax
699	movl	_atdevphys,%edx			/* get pte PA */
700	subl	_KPTphys,%edx			/* remove base of ptes, now have phys offset */
701	shll	$PGSHIFT-2,%edx			/* corresponding to virt offset */
702	addl	$KERNBASE,%edx			/* add virtual base */
703	movl	%edx,_atdevbase
704	addl	%eax,%edx
705	movl	%edx,_Crtat
706
707	/* set up bootstrap stack - 48 bytes */
708	movl	$_kstack+UPAGES*NBPG-4*12,%esp	/* bootstrap stack end location */
709	xorl	%eax,%eax			/* mark end of frames */
710	movl	%eax,%ebp
711	movl	_proc0paddr,%eax
712	movl	%esi,PCB_CR3(%eax)
713
714#ifdef BDE_DEBUGGER
715	/* relocate debugger gdt entries */
716
717	movl	$_gdt+8*9,%eax			/* adjust slots 9-17 */
718	movl	$9,%ecx
719reloc_gdt:
720	movb	$KERNBASE>>24,7(%eax)		/* top byte of base addresses, was 0, */
721	addl	$8,%eax				/* now KERNBASE>>24 */
722	loop	reloc_gdt
723
724	cmpl	$0,_bdb_exists
725	je	1f
726	int	$3
7271:
728#endif /* BDE_DEBUGGER */
729
730	/*
731	 * Skip over the page tables and the kernel stack
732	 */
733	lea	((1+UPAGES+1+NKPT)*NBPG)(%esi),%esi
734
735	pushl	%esi				/* value of first for init386(first) */
736	call	_init386			/* wire 386 chip for unix operation */
737	popl	%esi
738
739	.globl	__ucodesel,__udatasel
740
741	pushl	$0				/* unused */
742	pushl	__udatasel			/* ss */
743	pushl	$0				/* esp - filled in by execve() */
744	pushl	$PSL_USERSET			/* eflags (ring 0, int enab) */
745	pushl	__ucodesel			/* cs */
746	pushl	$0				/* eip - filled in by execve() */
747	subl	$(12*4),%esp			/* space for rest of registers */
748
749	pushl	%esp				/* call main with frame pointer */
750	call	_main				/* autoconfiguration, mountroot etc */
751
752	addl	$(13*4),%esp			/* back to a frame we can return with */
753
754	/*
755	 * now we've run main() and determined what cpu-type we are, we can
756	 * enable WP mode on i486 cpus and above.
757	 */
758#if defined(I486_CPU) || defined(I586_CPU)
759	cmpl    $CPUCLASS_386,_cpu_class
760	je	1f
761	movl	%cr0,%eax			/* get control word */
762	orl	$CR0_WP,%eax			/* enable write protect for all modes */
763	movl	%eax,%cr0			/* and do it */
764#endif
765	/*
766	 * on return from main(), we are process 1
767	 * set up address space and stack so that we can 'return' to user mode
768	 */
7691:
770	movl	__ucodesel,%eax
771	movl	__udatasel,%ecx
772
773	movl	%cx,%ds
774	movl	%cx,%es
775	movl	%ax,%fs				/* double map cs to fs */
776	movl	%cx,%gs				/* and ds to gs */
777	iret					/* goto user! */
778
779#define LCALL(x,y)	.byte 0x9a ; .long y ; .word x
780
781NON_GPROF_ENTRY(sigcode)
782	call	SIGF_HANDLER(%esp)
783	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
784						/* copy at 8(%esp)) */
785	pushl	%eax
786	pushl	%eax				/* junk to fake return address */
787	movl	$103,%eax			/* XXX sigreturn() */
788	LCALL(0x7,0)				/* enter kernel with args on stack */
789	hlt					/* never gets here */
790
791	.globl	_szsigcode
792_szsigcode:
793	.long	_szsigcode-_sigcode
794