locore.s revision 3728
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.36 1994/10/10 01:10:21 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#endif
267
268	/*
269	 * The old style disk boot.
270	 *	(*btext)(howto, bootdev, cyloffset, esym);
271	 * Note that the newer boot code just falls into here to pick
272	 * up howto and bootdev, cyloffset and esym are no longer used
273	 */
2742:	/* olddiskboot: */
275	movl	8(%ebp),%eax
276	movl	%eax,_boothowto-KERNBASE
277	movl	12(%ebp),%eax
278	movl	%eax,_bootdev-KERNBASE
279
280	/* get the BIOS video mode pointer */
281 	movl	$0x4a8, %ecx
282 	movl	(%ecx), %eax
283 	movl	%eax, %ecx
284 	shrl	$12, %ecx
285 	andl	$0xffff0000, %ecx
286 	andl	$0x0000ffff, %eax
287 	orl	%ecx, %eax
288 	movl	(%eax), %eax
289 	movl	%eax, %ecx
290 	shrl	$12, %ecx
291 	andl	$0xffff0000, %ecx
292 	andl	$0x0000ffff, %eax
293 	orl	%ecx, %eax
294 	addl	$KERNBASE, %eax
295 	movl	%eax, _video_mode_ptr-KERNBASE
296
297#if NAPM > 0
298	/*
299	 * Setup APM BIOS:
300	 *
301	 * APM BIOS initialization should be done from real mode or V86 mode.
302	 *
303	 * (by HOSOKAWA, Tatsumi <hosokawa@mt.cs.keio.ac.jp>)
304	 */
305
306	/*
307         * Copy APM initializer under 1MB boundary:
308	 *
309	 * APM initializer program must switch the CPU to real mode.
310	 * But FreeBSD kernel runs above 1MB boundary. So we must
311	 * copy the initializer code to conventional memory.
312	 */
313	movl	_apm_init_image_size-KERNBASE, %ecx	/* size */
314	lea	_apm_init_image-KERNBASE, %esi		/* source */
315	movl	$ APM_OURADDR, %edi			/* destination */
316	cld
317	rep
318	movsb
319
320	/* get GDT base */
321	sgdt	_apm_current_gdt_pdesc-KERNBASE
322
323	/* copy GDT to _bootstrap_gdt */
324	xorl	%ecx, %ecx
325	movw	_apm_current_gdt_pdesc-KERNBASE, %cx
326	movl	_apm_current_gdt_pdesc-KERNBASE+2, %esi
327	lea	_bootstrap_gdt-KERNBASE, %edi
328	cld
329	rep
330	movsb
331
332	/* setup GDT pseudo descriptor */
333	movw	$(SIZEOF_GDT*BOOTSTRAP_GDT_NUM), %ax
334	movw	%ax, _apm_current_gdt_pdesc-KERNBASE
335	leal	_bootstrap_gdt-KERNBASE, %eax
336	movl	%eax, _apm_current_gdt_pdesc-KERNBASE+2
337
338	/* load new GDTR */
339	lgdt	_apm_current_gdt_pdesc-KERNBASE
340
341	/* setup GDT for APM initializer */
342	lea	_bootstrap_gdt-KERNBASE, %ecx
343	movl	$(APM_OURADDR), %eax	/* use %ax for 15..0 */
344	movl	%eax, %ebx
345	shrl	$16, %ebx		/* use %bl for 23..16 */
346					/* use %bh for 31..24 */
347#define APM_SETUP_GDT(index, attrib) \
348	movl	$(index), %si ; \
349	lea	0(%ecx,%esi,8), %edx ; \
350	movw	$0xffff, (%edx) ; \
351	movw	%ax, 2(%edx) ; \
352	movb	%bl, 4(%edx) ; \
353	movw	$(attrib), 5(%edx) ; \
354	movb	%bh, 7(%edx)
355
356	APM_SETUP_GDT(APM_INIT_CS_INDEX  , CS32_ATTRIB)
357	APM_SETUP_GDT(APM_INIT_DS_INDEX  , DS32_ATTRIB)
358	APM_SETUP_GDT(APM_INIT_CS16_INDEX, CS16_ATTRIB)
359
360	/*
361	 * Call the initializer:
362	 *
363	 * direct intersegment call to conventional memory code
364	 */
365	.byte	0x9a		/* actually, lcall $APM_INIT_CS_SEL, $0 */
366	.long	0
367	.word	APM_INIT_CS_SEL
368
369	movw	%ax, _apm_version-KERNBASE
370	movl	%ebx, _apm_cs_entry-KERNBASE
371	movw	%cx, _apm_cs32_base-KERNBASE
372	shrl	$16, %ecx
373	movw	%cx, _apm_cs16_base-KERNBASE
374	movw	%dx, _apm_ds_base-KERNBASE
375	movw	%si, _apm_cs_limit-KERNBASE
376	shrl	$16, %esi
377	movw	%si, _apm_ds_limit-KERNBASE
378	movw	%di, _apm_flags-KERNBASE
379#endif /* NAPM */
380
381	/* Find out our CPU type. */
382
383	/* Try to toggle alignment check flag; does not exist on 386. */
384	pushfl
385	popl	%eax
386	movl	%eax,%ecx
387	orl	$PSL_AC,%eax
388	pushl	%eax
389	popfl
390	pushfl
391	popl	%eax
392	xorl	%ecx,%eax
393	andl	$PSL_AC,%eax
394	pushl	%ecx
395	popfl
396
397	testl	%eax,%eax
398	jnz	1f
399	movl	$CPU_386,_cpu-KERNBASE
400	jmp	2f
401
4021:	/* Try to toggle identification flag; does not exist on early 486s. */
403	pushfl
404	popl	%eax
405	movl	%eax,%ecx
406	xorl	$PSL_ID,%eax
407	pushl	%eax
408	popfl
409	pushfl
410	popl	%eax
411	xorl	%ecx,%eax
412	andl	$PSL_ID,%eax
413	pushl	%ecx
414	popfl
415
416	testl	%eax,%eax
417	jnz	1f
418	movl	$CPU_486,_cpu-KERNBASE
419
420	/* check for Cyrix 486DLC -- based on check routine  */
421	/* documented in "Cx486SLC/e SMM Programmer's Guide" */
422	xorw	%dx,%dx
423	cmpw	%dx,%dx			# set flags to known state
424	pushfw
425	popw	%cx			# store flags in ecx
426	movw	$0xffff,%ax
427	movw	$0x0004,%bx
428	divw	%bx
429	pushfw
430	popw	%ax
431	andw	$0x08d5,%ax		# mask off important bits
432	andw	$0x08d5,%cx
433	cmpw	%ax,%cx
434
435	jnz	2f			# if flags changed, Intel chip
436
437	movl	$CPU_486DLC,_cpu-KERNBASE # set CPU value for Cyrix
438	movl	$0x69727943,_cpu_vendor-KERNBASE	# store vendor string
439	movw	$0x0078,_cpu_vendor-KERNBASE+4
440
441	invd				# Start with guaranteed clean cache
442	/* Disable caching of the ISA hole only. */
443	movb	$CCR0,%al		# Configuration Register index (CCR0)
444	outb	%al,$0x22
445	inb	$0x23,%al
446	orb	$(CCR0_NC1|CCR0_BARB),%al
447	outb	%al,$0x23
448	invd
449	jmp	2f
450
4511:	/* Use the `cpuid' instruction. */
452	xorl	%eax,%eax
453	.byte	0x0f,0xa2		# cpuid 0
454	movl	%ebx,_cpu_vendor-KERNBASE	# store vendor string
455	movl	%edx,_cpu_vendor+4-KERNBASE
456	movl	%ecx,_cpu_vendor+8-KERNBASE
457	movb	$0,_cpu_vendor+12-KERNBASE
458
459	movl	$1,%eax
460	.byte	0x0f,0xa2		# cpuid 1
461	movl	%eax,_cpu_id-KERNBASE		# store cpu_id
462	rorl	$8,%eax			# extract family type
463	andl	$15,%eax
464	cmpl	$5,%eax
465	jae	1f
466
467	/* less than Pentium; must be 486 */
468	movl	$CPU_486,_cpu-KERNBASE
469	jmp	2f
470
4711:	movl	$CPU_586,_cpu-KERNBASE
4722:
473
474	/*
475	 * Finished with old stack; load new %esp now instead of later so
476	 * we can trace this code without having to worry about the trace
477	 * trap clobbering the memory test or the zeroing of the bss+bootstrap
478	 * page tables.
479	 *
480	 * XXX - wdboot clears the bss after testing that this is safe.
481	 * This is too wasteful - memory below 640K is scarce.  The boot
482	 * program should check:
483	 *	text+data <= &stack_variable - more_space_for_stack
484	 *	text+data+bss+pad+space_for_page_tables <= end_of_memory
485	 * Oops, the gdt is in the carcass of the boot program so clearing
486	 * the rest of memory is still not possible.
487	 */
488	movl	$tmpstk-KERNBASE,%esp		/* bootstrap stack end location */
489
490/*
491 * Virtual address space of kernel:
492 *
493 *	text | data | bss | [syms] | page dir | proc0 kernel stack | usr stk map | Sysmap
494 *      pages:                          1         UPAGES (2)             1         NKPT (7)
495 */
496
497/* find end of kernel image */
498	movl	$_end-KERNBASE,%ecx
499	addl	$NBPG-1,%ecx			/* page align up */
500	andl	$~(NBPG-1),%ecx
501	movl	%ecx,%esi			/* esi = start of free memory */
502	movl	%ecx,_KERNend-KERNBASE		/* save end of kernel */
503
504/* clear bss */
505	movl	$_edata-KERNBASE,%edi
506	subl	%edi,%ecx			/* get amount to clear */
507	xorl	%eax,%eax			/* specify zero fill */
508	cld
509	rep
510	stosb
511
512/*
513 * The value in esi is both the end of the kernel bss and a pointer to
514 * the kernel page directory, and is used by the rest of locore to build
515 * the tables.
516 * esi + 1(page dir) + 2(UPAGES) + 1(p0stack) + NKPT(number of kernel
517 * page table pages) is then passed on the stack to init386(first) as
518 * the value first. esi should ALWAYS be page aligned!!
519 */
520	movl	%esi,%ecx			/* Get current first availiable address */
521
522/* clear pagetables, page directory, stack, etc... */
523	movl	%esi,%edi			/* base (page directory) */
524	movl	$((1+UPAGES+1+NKPT)*NBPG),%ecx	/* amount to clear */
525	xorl	%eax,%eax			/* specify zero fill */
526	cld
527	rep
528	stosb
529
530/* physical address of Idle proc/kernel page directory */
531	movl	%esi,_IdlePTD-KERNBASE
532
533/*
534 * fillkpt
535 *	eax = (page frame address | control | status) == pte
536 *	ebx = address of page table
537 *	ecx = how many pages to map
538 */
539#define	fillkpt		\
5401:	movl	%eax,(%ebx)	; \
541	addl	$NBPG,%eax	; /* increment physical address */ \
542	addl	$4,%ebx		; /* next pte */ \
543	loop	1b		;
544
545/*
546 * Map Kernel
547 *
548 * First step - build page tables
549 */
550#if defined (KGDB) || defined (BDE_DEBUGGER)
551	movl	_KERNend-KERNBASE,%ecx		/* this much memory, */
552	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
553#ifdef BDE_DEBUGGER
554	cmpl	$0xa0,%ecx			/* XXX - cover debugger pages */
555	jae	1f
556	movl	$0xa0,%ecx
5571:
558#endif /* BDE_DEBUGGER */
559	movl	$PG_V|PG_KW,%eax		/* kernel R/W, valid */
560	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
561	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
562	fillkpt
563
564#else /* !KGDB && !BDE_DEBUGGER */
565	/* write protect kernel text (doesn't do a thing for 386's - only 486's) */
566	movl	$_etext-KERNBASE,%ecx		/* get size of text */
567	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
568	movl	$PG_V|PG_KR,%eax		/* specify read only */
569	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
570	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
571	fillkpt
572
573	/* data and bss are r/w */
574	andl	$PG_FRAME,%eax			/* strip to just addr of bss */
575	movl	_KERNend-KERNBASE,%ecx		/* calculate size */
576	subl	%eax,%ecx
577	shrl	$PGSHIFT,%ecx
578	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
579	fillkpt
580#endif /* KGDB || BDE_DEBUGGER */
581
582/* now initialize the page dir, upages, p0stack PT, and page tables */
583
584	movl	$(1+UPAGES+1+NKPT),%ecx	/* number of PTEs */
585	movl	%esi,%eax			/* phys address of PTD */
586	andl	$PG_FRAME,%eax			/* convert to PFN, should be a NOP */
587	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
588	movl	%esi,%ebx			/* calculate pte offset to ptd */
589	shrl	$PGSHIFT-2,%ebx
590	addl	%esi,%ebx			/* address of page directory */
591	addl	$((1+UPAGES+1)*NBPG),%ebx	/* offset to kernel page tables */
592	fillkpt
593
594/* map I/O memory map */
595
596	movl    _KPTphys-KERNBASE,%ebx		/* base of kernel page tables */
597	lea     (0xa0 * PTESIZE)(%ebx),%ebx	/* hardwire ISA hole at KERNBASE + 0xa0000 */
598	movl	$0x100-0xa0,%ecx		/* for this many pte s, */
599	movl	$(0xa0000|PG_V|PG_KW|PG_N),%eax	/* valid, kernel read/write, non-cacheable */
600	movl	%ebx,_atdevphys-KERNBASE	/* save phys addr of ptes */
601	fillkpt
602
603 /* map proc 0's kernel stack into user page table page */
604
605	movl	$UPAGES,%ecx			/* for this many pte s, */
606	lea	(1*NBPG)(%esi),%eax		/* physical address in proc 0 */
607	lea	(KERNBASE)(%eax),%edx		/* change into virtual addr */
608	movl	%edx,_proc0paddr-KERNBASE	/* save VA for proc 0 init */
609	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
610	lea	((1+UPAGES)*NBPG)(%esi),%ebx	/* addr of stack page table in proc 0 */
611	addl	$(KSTKPTEOFF * PTESIZE),%ebx	/* offset to kernel stack PTE */
612	fillkpt
613
614/*
615 * Initialize kernel page table directory
616 */
617	/* install a pde for temporary double map of bottom of VA */
618	movl	_KPTphys-KERNBASE,%eax
619	orl     $PG_V|PG_KW,%eax		/* valid, kernel read/write */
620	movl	%eax,(%esi)			/* which is where temp maps! */
621
622	/* initialize kernel pde's */
623	movl	$(NKPT),%ecx			/* for this many PDEs */
624	lea	(KPTDI*PDESIZE)(%esi),%ebx	/* offset of pde for kernel */
625	fillkpt
626
627	/* install a pde recursively mapping page directory as a page table! */
628	movl	%esi,%eax			/* phys address of ptd in proc 0 */
629	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
630	movl	%eax,PTDPTDI*PDESIZE(%esi)	/* which is where PTmap maps! */
631
632	/* install a pde to map kernel stack for proc 0 */
633	lea	((1+UPAGES)*NBPG)(%esi),%eax	/* physical address of pt in proc 0 */
634	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
635	movl	%eax,KSTKPTDI*PDESIZE(%esi)	/* which is where kernel stack maps! */
636
637#ifdef BDE_DEBUGGER
638	/* copy and convert stuff from old gdt and idt for debugger */
639
640	cmpl	$0x0375c339,0x96104		/* XXX - debugger signature */
641	jne	1f
642	movb	$1,_bdb_exists-KERNBASE
6431:
644	pushal
645	subl	$2*6,%esp
646
647	sgdt	(%esp)
648	movl	2(%esp),%esi			/* base address of current gdt */
649	movl	$_gdt-KERNBASE,%edi
650	movl	%edi,2(%esp)
651	movl	$8*18/4,%ecx
652	cld
653	rep					/* copy gdt */
654	movsl
655	movl	$_gdt-KERNBASE,-8+2(%edi)	/* adjust gdt self-ptr */
656	movb	$0x92,-8+5(%edi)
657
658	sidt	6(%esp)
659	movl	6+2(%esp),%esi			/* base address of current idt */
660	movl	8+4(%esi),%eax			/* convert dbg descriptor to ... */
661	movw	8(%esi),%ax
662	movl	%eax,bdb_dbg_ljmp+1-KERNBASE	/* ... immediate offset ... */
663	movl	8+2(%esi),%eax
664	movw	%ax,bdb_dbg_ljmp+5-KERNBASE	/* ... and selector for ljmp */
665	movl	24+4(%esi),%eax			/* same for bpt descriptor */
666	movw	24(%esi),%ax
667	movl	%eax,bdb_bpt_ljmp+1-KERNBASE
668	movl	24+2(%esi),%eax
669	movw	%ax,bdb_bpt_ljmp+5-KERNBASE
670
671	movl	$_idt-KERNBASE,%edi
672	movl	%edi,6+2(%esp)
673	movl	$8*4/4,%ecx
674	cld
675	rep					/* copy idt */
676	movsl
677
678	lgdt	(%esp)
679	lidt	6(%esp)
680
681	addl	$2*6,%esp
682	popal
683#endif /* BDE_DEBUGGER */
684
685	/* load base of page directory and enable mapping */
686	movl	%esi,%eax			/* phys address of ptd in proc 0 */
687	movl	%eax,%cr3			/* load ptd addr into mmu */
688	movl	%cr0,%eax			/* get control word */
689	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
690	movl	%eax,%cr0			/* and let's page NOW! */
691
692	pushl	$begin				/* jump to high mem */
693	ret
694
695begin: /* now running relocated at KERNBASE where the system is linked to run */
696	movl	_atdevphys,%edx			/* get pte PA */
697	subl	_KPTphys,%edx			/* remove base of ptes, now have phys offset */
698	shll	$PGSHIFT-2,%edx			/* corresponding to virt offset */
699	addl	$KERNBASE,%edx			/* add virtual base */
700	movl	%edx,_atdevbase
701
702#if N_SC > 0
703	/* XXX: can't scinit relocate Crtat relative to atdevbase itself? */
704	.globl _Crtat				/* XXX - locore should not know about */
705	movl	_Crtat,%eax			/* variables of device drivers (pccons)! */
706	subl	$(KERNBASE+0xA0000),%eax
707	addl	%eax,%edx
708	movl	%edx,_Crtat
709#endif
710
711	/* set up bootstrap stack - 48 bytes */
712	movl	$_kstack+UPAGES*NBPG-4*12,%esp	/* bootstrap stack end location */
713	xorl	%eax,%eax			/* mark end of frames */
714	movl	%eax,%ebp
715	movl	_proc0paddr,%eax
716	movl	%esi,PCB_CR3(%eax)
717
718#ifdef BDE_DEBUGGER
719	/* relocate debugger gdt entries */
720
721	movl	$_gdt+8*9,%eax			/* adjust slots 9-17 */
722	movl	$9,%ecx
723reloc_gdt:
724	movb	$KERNBASE>>24,7(%eax)		/* top byte of base addresses, was 0, */
725	addl	$8,%eax				/* now KERNBASE>>24 */
726	loop	reloc_gdt
727
728	cmpl	$0,_bdb_exists
729	je	1f
730	int	$3
7311:
732#endif /* BDE_DEBUGGER */
733
734	/*
735	 * Skip over the page tables and the kernel stack
736	 */
737	lea	((1+UPAGES+1+NKPT)*NBPG)(%esi),%esi
738
739	pushl	%esi				/* value of first for init386(first) */
740	call	_init386			/* wire 386 chip for unix operation */
741	popl	%esi
742
743	.globl	__ucodesel,__udatasel
744
745	pushl	$0				/* unused */
746	pushl	__udatasel			/* ss */
747	pushl	$0				/* esp - filled in by execve() */
748	pushl	$PSL_USERSET			/* eflags (ring 0, int enab) */
749	pushl	__ucodesel			/* cs */
750	pushl	$0				/* eip - filled in by execve() */
751	subl	$(12*4),%esp			/* space for rest of registers */
752
753	pushl	%esp				/* call main with frame pointer */
754	call	_main				/* autoconfiguration, mountroot etc */
755
756	addl	$(13*4),%esp			/* back to a frame we can return with */
757
758	/*
759	 * now we've run main() and determined what cpu-type we are, we can
760	 * enable WP mode on i486 cpus and above.
761	 */
762#if defined(I486_CPU) || defined(I586_CPU)
763	cmpl    $CPUCLASS_386,_cpu_class
764	je	1f
765	movl	%cr0,%eax			/* get control word */
766	orl	$CR0_WP,%eax			/* enable write protect for all modes */
767	movl	%eax,%cr0			/* and do it */
768#endif
769	/*
770	 * on return from main(), we are process 1
771	 * set up address space and stack so that we can 'return' to user mode
772	 */
7731:
774	movl	__ucodesel,%eax
775	movl	__udatasel,%ecx
776
777	movl	%cx,%ds
778	movl	%cx,%es
779	movl	%ax,%fs				/* double map cs to fs */
780	movl	%cx,%gs				/* and ds to gs */
781	iret					/* goto user! */
782
783#define LCALL(x,y)	.byte 0x9a ; .long y ; .word x
784
785NON_GPROF_ENTRY(sigcode)
786	call	SIGF_HANDLER(%esp)
787	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
788						/* copy at 8(%esp)) */
789	pushl	%eax
790	pushl	%eax				/* junk to fake return address */
791	movl	$103,%eax			/* XXX sigreturn() */
792	LCALL(0x7,0)				/* enter kernel with args on stack */
793	hlt					/* never gets here */
794
795	.globl	_szsigcode
796_szsigcode:
797	.long	_szsigcode-_sigcode
798