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