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