locore.s revision 6512
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.48 1995/02/11 04:21:22 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 "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 <sys/reboot.h>
57#include "apm.h"
58
59/*
60 *	XXX
61 *
62 * Note: This version greatly munged to avoid various assembler errors
63 * that may be fixed in newer versions of gas. Perhaps newer versions
64 * will have more pleasant appearance.
65 */
66
67/*
68 * PTmap is recursive pagemap at top of virtual address space.
69 * Within PTmap, the page directory can be found (third indirection).
70 */
71	.globl	_PTmap,_PTD,_PTDpde
72	.set	_PTmap,PTDPTDI << PDRSHIFT
73	.set	_PTD,_PTmap + (PTDPTDI * NBPG)
74	.set	_PTDpde,_PTD + (PTDPTDI * PDESIZE)
75
76/*
77 * Sysmap is the base address of the kernel page tables.
78 * It is a bogus interface for kgdb and isn't used by the kernel itself.
79 */
80	.set	_Sysmap,_PTmap + (KPTDI * NBPG)
81
82/*
83 * APTmap, APTD is the alternate recursive pagemap.
84 * It's used when modifying another process's page tables.
85 */
86	.globl	_APTmap,_APTD,_APTDpde
87	.set	_APTmap,APTDPTDI << PDRSHIFT
88	.set	_APTD,_APTmap + (APTDPTDI * NBPG)
89	.set	_APTDpde,_PTD + (APTDPTDI * PDESIZE)
90
91/*
92 * Access to each processes kernel stack is via a region of
93 * per-process address space (at the beginning), immediatly above
94 * the user process stack.
95 */
96	.set	_kstack,USRSTACK
97	.globl	_kstack
98
99/*
100 * Globals
101 */
102	.data
103
104	.globl	tmpstk
105	.space	0x1000		/* space for tmpstk - temporary stack */
106tmpstk:
107/*
108 * Dummy frame at top of tmpstk to help debuggers print a nice stack trace.
109 */
110	.long	tmpstk+8	/* caller's %ebp */
111	.long	_cpu_switch	/* caller */
112	.long	0		/* %ebp == 0 should terminate trace */
113	.long	_mvesp		/* in case %ebp == 0 doesn't work ... */
114	.long	0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555
115
116	.globl	_boothowto,_bootdev
117
118	.globl	_cpu,_cold,_atdevbase,_cpu_vendor,_cpu_id,_bootinfo
119	.globl	_cpu_high, _cpu_feature
120
121_cpu:	.long	0				/* are we 386, 386sx, or 486 */
122_cpu_id:	.long	0			/* stepping ID */
123_cpu_high:	.long	0			/* highest arg to CPUID */
124_cpu_feature:	.long	0			/* features */
125_cpu_vendor:	.space	20			/* CPU origin code */
126_bootinfo:	.space	BOOTINFO_SIZE		/* bootinfo that we can handle */
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
146/*
147 * System Initialization
148 */
149	.text
150
151/*
152 * btext: beginning of text section.
153 * Also the entry point (jumped to directly from the boot blocks).
154 */
155NON_GPROF_ENTRY(btext)
156	movw	$0x1234,0x472			/* warm boot */
157	jmp	1f
158	/*
159	 * XXX now that we load at 1MB is this still really used?
160	 */
161	.org	0x500				/* space for BIOS variables */
162
1631:
164	/* Set up a real frame, some day we will be doing returns */
165	pushl	%ebp
166	movl	%esp, %ebp
167
168	/* Don't trust what the BIOS gives for eflags. */
169	pushl	$PSL_KERNEL
170	popfl
171
172	/* Don't trust what the BIOS gives for %fs and %gs. */
173	mov	%ds, %ax
174	mov	%ax, %fs
175	mov	%ax, %gs
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	BI_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	BI_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	/*
258	 * Determine the size of the boot loader's copy of the bootinfo
259	 * struct.  This is impossible to do properly because old versions
260	 * of the struct don't contain a size field and there are 2 old
261	 * versions with the same version number.
262	 */
263	movl	$BI_ENDCOMMON,%ecx	/* prepare for sizeless version */
264	testl	$RB_BOOTINFO,8(%ebp)	/* bi_size (and bootinfo) valid? */
265	je	got_bi_size		/* no, sizeless version */
266	movl	BI_SIZE(%ebx),%ecx
267got_bi_size:
268
269	/*
270	 * Copy the common part of the bootinfo struct
271	 */
272	movl	%ebx,%esi
273	movl	$_bootinfo-KERNBASE,%edi
274	cmpl	$BOOTINFO_SIZE,%ecx
275	jbe	got_common_bi_size
276	movl	$BOOTINFO_SIZE,%ecx
277got_common_bi_size:
278	cld
279	rep
280	movsb
281
282#ifdef NFS
283	/*
284	 * If we have a nfs_diskless structure copy it in
285	 */
286	movl	BI_NFS_DISKLESS(%ebx),%esi
287	cmpl	$0,%esi
288	je	2f
289	lea	_nfs_diskless-KERNBASE,%edi
290	movl	$NFSDISKLESS_SIZE,%ecx
291	cld
292	rep
293	movsb
294	lea	_nfs_diskless_valid-KERNBASE,%edi
295	movl	$1,(%edi)
296#endif
297
298	/*
299	 * The old style disk boot.
300	 *	(*btext)(howto, bootdev, cyloffset, esym);
301	 * Note that the newer boot code just falls into here to pick
302	 * up howto and bootdev, cyloffset and esym are no longer used
303	 */
3042:	/* olddiskboot: */
305	movl	8(%ebp),%eax
306	movl	%eax,_boothowto-KERNBASE
307	movl	12(%ebp),%eax
308	movl	%eax,_bootdev-KERNBASE
309
310#if NAPM > 0
311	/* call APM BIOS driver setup (i386/apm/apm_setup.s) */
312	call	_apm_setup
313#endif /* NAPM */
314
315	/* Find out our CPU type. */
316
317	/* Try to toggle alignment check flag; does not exist on 386. */
318	pushfl
319	popl	%eax
320	movl	%eax,%ecx
321	orl	$PSL_AC,%eax
322	pushl	%eax
323	popfl
324	pushfl
325	popl	%eax
326	xorl	%ecx,%eax
327	andl	$PSL_AC,%eax
328	pushl	%ecx
329	popfl
330
331	testl	%eax,%eax
332	jnz	1f
333	movl	$CPU_386,_cpu-KERNBASE
334	jmp	2f
335
3361:	/* Try to toggle identification flag; does not exist on early 486s. */
337	pushfl
338	popl	%eax
339	movl	%eax,%ecx
340	xorl	$PSL_ID,%eax
341	pushl	%eax
342	popfl
343	pushfl
344	popl	%eax
345	xorl	%ecx,%eax
346	andl	$PSL_ID,%eax
347	pushl	%ecx
348	popfl
349
350	testl	%eax,%eax
351	jnz	1f
352	movl	$CPU_486,_cpu-KERNBASE
353
354	/* check for Cyrix 486DLC -- based on check routine  */
355	/* documented in "Cx486SLC/e SMM Programmer's Guide" */
356	xorw	%dx,%dx
357	cmpw	%dx,%dx			# set flags to known state
358	pushfw
359	popw	%cx			# store flags in ecx
360	movw	$0xffff,%ax
361	movw	$0x0004,%bx
362	divw	%bx
363	pushfw
364	popw	%ax
365	andw	$0x08d5,%ax		# mask off important bits
366	andw	$0x08d5,%cx
367	cmpw	%ax,%cx
368
369	jnz	2f			# if flags changed, Intel chip
370
371	movl	$CPU_486DLC,_cpu-KERNBASE # set CPU value for Cyrix
372	movl	$0x69727943,_cpu_vendor-KERNBASE	# store vendor string
373	movw	$0x0078,_cpu_vendor-KERNBASE+4
374
375	invd				# Start with guaranteed clean cache
376	/* Disable caching of the ISA hole only. */
377	movb	$CCR0,%al		# Configuration Register index (CCR0)
378	outb	%al,$0x22
379	inb	$0x23,%al
380	orb	$(CCR0_NC1|CCR0_BARB),%al
381	outb	%al,$0x23
382	invd
383	jmp	2f
384
3851:	/* Use the `cpuid' instruction. */
386	xorl	%eax,%eax
387	.byte	0x0f,0xa2			# cpuid 0
388	movl	%eax,_cpu_high-KERNBASE		# highest capability
389	movl	%ebx,_cpu_vendor-KERNBASE	# store vendor string
390	movl	%edx,_cpu_vendor+4-KERNBASE
391	movl	%ecx,_cpu_vendor+8-KERNBASE
392	movb	$0,_cpu_vendor+12-KERNBASE
393
394	movl	$1,%eax
395	.byte	0x0f,0xa2			# cpuid 1
396	movl	%eax,_cpu_id-KERNBASE		# store cpu_id
397	movl	%edx,_cpu_feature-KERNBASE	# store cpu_feature
398	rorl	$8,%eax				# extract family type
399	andl	$15,%eax
400	cmpl	$5,%eax
401	jae	1f
402
403	/* less than Pentium; must be 486 */
404	movl	$CPU_486,_cpu-KERNBASE
405	jmp	2f
406
4071:	movl	$CPU_586,_cpu-KERNBASE
4082:
409
410	/*
411	 * Finished with old stack; load new %esp now instead of later so
412	 * we can trace this code without having to worry about the trace
413	 * trap clobbering the memory test or the zeroing of the bss+bootstrap
414	 * page tables.
415	 *
416	 * XXX - wdboot clears the bss after testing that this is safe.
417	 * This is too wasteful - memory below 640K is scarce.  The boot
418	 * program should check:
419	 *	text+data <= &stack_variable - more_space_for_stack
420	 *	text+data+bss+pad+space_for_page_tables <= end_of_memory
421	 * Oops, the gdt is in the carcass of the boot program so clearing
422	 * the rest of memory is still not possible.
423	 */
424	movl	$tmpstk-KERNBASE,%esp		/* bootstrap stack end location */
425
426/*
427 * Virtual address space of kernel:
428 *
429 *	text | data | bss | [syms] | page dir | proc0 kernel stack | usr stk map | Sysmap
430 *      pages:                          1         UPAGES (2)             1         NKPT (7)
431 */
432
433/* find end of kernel image */
434	movl	$_end-KERNBASE,%ecx
435	addl	$NBPG-1,%ecx			/* page align up */
436	andl	$~(NBPG-1),%ecx
437	movl	%ecx,%esi			/* esi = start of free memory */
438	movl	%ecx,_KERNend-KERNBASE		/* save end of kernel */
439
440/* clear bss */
441	movl	$_edata-KERNBASE,%edi
442	subl	%edi,%ecx			/* get amount to clear */
443	xorl	%eax,%eax			/* specify zero fill */
444	cld
445	rep
446	stosb
447
448#ifdef DDB
449/* include symbols in "kernel image" if they are loaded */
450	movl	_bootinfo+BI_ESYMTAB-KERNBASE,%edi
451	testl	%edi,%edi
452	je	over_symalloc
453	addl	$NBPG-1,%edi
454	andl	$~(NBPG-1),%edi
455	movl	%edi,%esi
456	movl	%esi,_KERNend-KERNBASE
457	movl	$KERNBASE,%edi
458	addl	%edi,_bootinfo+BI_SYMTAB-KERNBASE
459	addl	%edi,_bootinfo+BI_ESYMTAB-KERNBASE
460over_symalloc:
461#endif
462
463/*
464 * The value in esi is both the end of the kernel bss and a pointer to
465 * the kernel page directory, and is used by the rest of locore to build
466 * the tables.
467 * esi + 1(page dir) + 2(UPAGES) + 1(p0stack) + NKPT(number of kernel
468 * page table pages) is then passed on the stack to init386(first) as
469 * the value first. esi should ALWAYS be page aligned!!
470 */
471	movl	%esi,%ecx			/* Get current first availiable address */
472
473/* clear pagetables, page directory, stack, etc... */
474	movl	%esi,%edi			/* base (page directory) */
475	movl	$((1+UPAGES+1+NKPT)*NBPG),%ecx	/* amount to clear */
476	xorl	%eax,%eax			/* specify zero fill */
477	cld
478	rep
479	stosb
480
481/* physical address of Idle proc/kernel page directory */
482	movl	%esi,_IdlePTD-KERNBASE
483
484/*
485 * fillkpt
486 *	eax = (page frame address | control | status) == pte
487 *	ebx = address of page table
488 *	ecx = how many pages to map
489 */
490#define	fillkpt		\
4911:	movl	%eax,(%ebx)	; \
492	addl	$NBPG,%eax	; /* increment physical address */ \
493	addl	$4,%ebx		; /* next pte */ \
494	loop	1b		;
495
496/*
497 * Map Kernel
498 *
499 * First step - build page tables
500 */
501#if defined (KGDB) || defined (BDE_DEBUGGER)
502	movl	_KERNend-KERNBASE,%ecx		/* this much memory, */
503	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
504#ifdef BDE_DEBUGGER
505	cmpl	$0xa0,%ecx			/* XXX - cover debugger pages */
506	jae	1f
507	movl	$0xa0,%ecx
5081:
509#endif /* BDE_DEBUGGER */
510	movl	$PG_V|PG_KW,%eax		/* kernel R/W, valid */
511	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
512	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
513	fillkpt
514
515#else /* !KGDB && !BDE_DEBUGGER */
516	/* write protect kernel text (doesn't do a thing for 386's - only 486's) */
517	movl	$_etext-KERNBASE,%ecx		/* get size of text */
518	addl	$NBPG-1,%ecx			/* round up to page */
519	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
520	movl	$PG_V|PG_KR,%eax		/* specify read only */
521#if 0
522	movl	$_etext,%ecx			/* get size of text */
523	subl	$_btext,%ecx
524	addl	$NBPG-1,%ecx			/* round up to page */
525	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
526	movl	$_btext-KERNBASE,%eax		/* get offset to physical memory */
527	orl	$PG_V|PG_KR,%eax		/* specify read only */
528#endif
529	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
530	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
531	fillkpt
532
533	/* data and bss are r/w */
534	andl	$PG_FRAME,%eax			/* strip to just addr of bss */
535	movl	_KERNend-KERNBASE,%ecx		/* calculate size */
536	subl	%eax,%ecx
537	shrl	$PGSHIFT,%ecx
538	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
539	fillkpt
540#endif /* KGDB || BDE_DEBUGGER */
541
542/* now initialize the page dir, upages, p0stack PT, and page tables */
543
544	movl	$(1+UPAGES+1+NKPT),%ecx	/* number of PTEs */
545	movl	%esi,%eax			/* phys address of PTD */
546	andl	$PG_FRAME,%eax			/* convert to PFN, should be a NOP */
547	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
548	movl	%esi,%ebx			/* calculate pte offset to ptd */
549	shrl	$PGSHIFT-2,%ebx
550	addl	%esi,%ebx			/* address of page directory */
551	addl	$((1+UPAGES+1)*NBPG),%ebx	/* offset to kernel page tables */
552	fillkpt
553
554/* map I/O memory map */
555
556	movl    _KPTphys-KERNBASE,%ebx		/* base of kernel page tables */
557	lea     (0xa0 * PTESIZE)(%ebx),%ebx	/* hardwire ISA hole at KERNBASE + 0xa0000 */
558	movl	$0x100-0xa0,%ecx		/* for this many pte s, */
559	movl	$(0xa0000|PG_V|PG_KW|PG_N),%eax	/* valid, kernel read/write, non-cacheable */
560	movl	%ebx,_atdevphys-KERNBASE	/* save phys addr of ptes */
561	fillkpt
562
563 /* map proc 0's kernel stack into user page table page */
564
565	movl	$UPAGES,%ecx			/* for this many pte s, */
566	lea	(1*NBPG)(%esi),%eax		/* physical address in proc 0 */
567	lea	(KERNBASE)(%eax),%edx		/* change into virtual addr */
568	movl	%edx,_proc0paddr-KERNBASE	/* save VA for proc 0 init */
569	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
570	lea	((1+UPAGES)*NBPG)(%esi),%ebx	/* addr of stack page table in proc 0 */
571	addl	$(KSTKPTEOFF * PTESIZE),%ebx	/* offset to kernel stack PTE */
572	fillkpt
573
574/*
575 * Initialize kernel page table directory
576 */
577	/* install a pde for temporary double map of bottom of VA */
578	movl	_KPTphys-KERNBASE,%eax
579	orl     $PG_V|PG_KW,%eax		/* valid, kernel read/write */
580	movl	%eax,(%esi)			/* which is where temp maps! */
581
582	/* initialize kernel pde's */
583	movl	$(NKPT),%ecx			/* for this many PDEs */
584	lea	(KPTDI*PDESIZE)(%esi),%ebx	/* offset of pde for kernel */
585	fillkpt
586
587	/* install a pde recursively mapping page directory as a page table! */
588	movl	%esi,%eax			/* phys address of ptd in proc 0 */
589	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
590	movl	%eax,PTDPTDI*PDESIZE(%esi)	/* which is where PTmap maps! */
591
592	/* install a pde to map kernel stack for proc 0 */
593	lea	((1+UPAGES)*NBPG)(%esi),%eax	/* physical address of pt in proc 0 */
594	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
595	movl	%eax,KSTKPTDI*PDESIZE(%esi)	/* which is where kernel stack maps! */
596
597#ifdef BDE_DEBUGGER
598	/* copy and convert stuff from old gdt and idt for debugger */
599
600	cmpl	$0x0375c339,0x96104		/* XXX - debugger signature */
601	jne	1f
602	movb	$1,_bdb_exists-KERNBASE
6031:
604	pushal
605	subl	$2*6,%esp
606
607	sgdt	(%esp)
608	movl	2(%esp),%esi			/* base address of current gdt */
609	movl	$_gdt-KERNBASE,%edi
610	movl	%edi,2(%esp)
611	movl	$8*18/4,%ecx
612	cld
613	rep					/* copy gdt */
614	movsl
615	movl	$_gdt-KERNBASE,-8+2(%edi)	/* adjust gdt self-ptr */
616	movb	$0x92,-8+5(%edi)
617
618	sidt	6(%esp)
619	movl	6+2(%esp),%esi			/* base address of current idt */
620	movl	8+4(%esi),%eax			/* convert dbg descriptor to ... */
621	movw	8(%esi),%ax
622	movl	%eax,bdb_dbg_ljmp+1-KERNBASE	/* ... immediate offset ... */
623	movl	8+2(%esi),%eax
624	movw	%ax,bdb_dbg_ljmp+5-KERNBASE	/* ... and selector for ljmp */
625	movl	24+4(%esi),%eax			/* same for bpt descriptor */
626	movw	24(%esi),%ax
627	movl	%eax,bdb_bpt_ljmp+1-KERNBASE
628	movl	24+2(%esi),%eax
629	movw	%ax,bdb_bpt_ljmp+5-KERNBASE
630
631	movl	$_idt-KERNBASE,%edi
632	movl	%edi,6+2(%esp)
633	movl	$8*4/4,%ecx
634	cld
635	rep					/* copy idt */
636	movsl
637
638	lgdt	(%esp)
639	lidt	6(%esp)
640
641	addl	$2*6,%esp
642	popal
643#endif /* BDE_DEBUGGER */
644
645	/* load base of page directory and enable mapping */
646	movl	%esi,%eax			/* phys address of ptd in proc 0 */
647	movl	%eax,%cr3			/* load ptd addr into mmu */
648	movl	%cr0,%eax			/* get control word */
649	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
650	movl	%eax,%cr0			/* and let's page NOW! */
651
652	pushl	$begin				/* jump to high mem */
653	ret
654
655begin: /* now running relocated at KERNBASE where the system is linked to run */
656	movl	_atdevphys,%edx			/* get pte PA */
657	subl	_KPTphys,%edx			/* remove base of ptes, now have phys offset */
658	shll	$PGSHIFT-2,%edx			/* corresponding to virt offset */
659	addl	$KERNBASE,%edx			/* add virtual base */
660	movl	%edx,_atdevbase
661
662#include "sc.h"
663#include "vt.h"
664#if NSC > 0 || NVT > 0
665	/* XXX: can't scinit relocate Crtat relative to atdevbase itself? */
666	.globl _Crtat				/* XXX - locore should not know about */
667	movl	_Crtat,%eax			/* variables of device drivers (pccons)! */
668	subl	$(KERNBASE+0xA0000),%eax
669	addl	%eax,%edx
670	movl	%edx,_Crtat
671#endif
672
673	/* set up bootstrap stack - 48 bytes */
674	movl	$_kstack+UPAGES*NBPG-4*12,%esp	/* bootstrap stack end location */
675	xorl	%eax,%eax			/* mark end of frames */
676	movl	%eax,%ebp
677	movl	_proc0paddr,%eax
678	movl	%esi,PCB_CR3(%eax)
679
680#ifdef BDE_DEBUGGER
681	/* relocate debugger gdt entries */
682
683	movl	$_gdt+8*9,%eax			/* adjust slots 9-17 */
684	movl	$9,%ecx
685reloc_gdt:
686	movb	$KERNBASE>>24,7(%eax)		/* top byte of base addresses, was 0, */
687	addl	$8,%eax				/* now KERNBASE>>24 */
688	loop	reloc_gdt
689
690	cmpl	$0,_bdb_exists
691	je	1f
692	int	$3
6931:
694#endif /* BDE_DEBUGGER */
695
696	/*
697	 * Skip over the page tables and the kernel stack
698	 */
699	lea	((1+UPAGES+1+NKPT)*NBPG)(%esi),%esi
700
701	pushl	%esi				/* value of first for init386(first) */
702	call	_init386			/* wire 386 chip for unix operation */
703	popl	%esi
704
705	.globl	__ucodesel,__udatasel
706
707	pushl	$0				/* unused */
708	pushl	__udatasel			/* ss */
709	pushl	$0				/* esp - filled in by execve() */
710	pushl	$PSL_USER			/* eflags (IOPL 0, int enab) */
711	pushl	__ucodesel			/* cs */
712	pushl	$0				/* eip - filled in by execve() */
713	subl	$(12*4),%esp			/* space for rest of registers */
714
715	pushl	%esp				/* call main with frame pointer */
716	call	_main				/* autoconfiguration, mountroot etc */
717
718	addl	$(13*4),%esp			/* back to a frame we can return with */
719
720	/*
721	 * now we've run main() and determined what cpu-type we are, we can
722	 * enable write protection and alignment checking on i486 cpus and
723	 * above.
724	 */
725#if defined(I486_CPU) || defined(I586_CPU)
726	cmpl    $CPUCLASS_386,_cpu_class
727	je	1f
728	movl	%cr0,%eax			/* get control word */
729	orl	$CR0_WP|CR0_AM,%eax		/* enable i486 features */
730	movl	%eax,%cr0			/* and do it */
731#endif
732	/*
733	 * on return from main(), we are process 1
734	 * set up address space and stack so that we can 'return' to user mode
735	 */
7361:
737	movl	__ucodesel,%eax
738	movl	__udatasel,%ecx
739
740	movl	%cx,%ds
741	movl	%cx,%es
742	movl	%ax,%fs				/* double map cs to fs */
743	movl	%cx,%gs				/* and ds to gs */
744	iret					/* goto user! */
745
746#define LCALL(x,y)	.byte 0x9a ; .long y ; .word x
747
748NON_GPROF_ENTRY(sigcode)
749	call	SIGF_HANDLER(%esp)
750	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
751						/* copy at 8(%esp)) */
752	pushl	%eax
753	pushl	%eax				/* junk to fake return address */
754	movl	$103,%eax			/* XXX sigreturn() */
755	LCALL(0x7,0)				/* enter kernel with args on stack */
756	hlt					/* never gets here */
757
758	.globl	_szsigcode
759_szsigcode:
760	.long	_szsigcode-_sigcode
761