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