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