locore.s revision 29213
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.5 1997/09/02 04:38:32 smp Exp smp $
38 *
39 *		originally from: locore.s, by William F. Jolitz
40 *
41 *		Substantially rewritten by David Greenman, Rod Grimes,
42 *			Bruce Evans, Wolfgang Solfrank, Poul-Henning Kamp
43 *			and many others.
44 */
45
46#include "apm.h"
47#include "opt_cpu.h"
48#include "opt_ddb.h"
49#include "opt_userconfig.h"
50
51#include <sys/syscall.h>
52#include <sys/reboot.h>
53
54#include <machine/asmacros.h>
55#include <machine/cputypes.h>
56#include <machine/psl.h>
57#include <machine/pmap.h>
58#include <machine/specialreg.h>
59
60#include "assym.s"
61
62/*
63 *	XXX
64 *
65 * Note: This version greatly munged to avoid various assembler errors
66 * that may be fixed in newer versions of gas. Perhaps newer versions
67 * will have more pleasant appearance.
68 */
69
70/*
71 * PTmap is recursive pagemap at top of virtual address space.
72 * Within PTmap, the page directory can be found (third indirection).
73 */
74	.globl	_PTmap,_PTD,_PTDpde
75	.set	_PTmap,(PTDPTDI << PDRSHIFT)
76	.set	_PTD,_PTmap + (PTDPTDI * PAGE_SIZE)
77	.set	_PTDpde,_PTD + (PTDPTDI * PDESIZE)
78
79/*
80 * APTmap, APTD is the alternate recursive pagemap.
81 * It's used when modifying another process's page tables.
82 */
83	.globl	_APTmap,_APTD,_APTDpde
84	.set	_APTmap,APTDPTDI << PDRSHIFT
85	.set	_APTD,_APTmap + (APTDPTDI * PAGE_SIZE)
86	.set	_APTDpde,_PTD + (APTDPTDI * PDESIZE)
87
88#ifdef SMP
89	.globl	_SMP_prvstart
90	.set	_SMP_prvstart,(MPPTDI << PDRSHIFT)
91
92	.globl	_SMP_prvpage,_SMP_prvpt,_lapic,_SMP_ioapic
93	.globl	_prv_CPAGE1,_prv_CPAGE2,_prv_CPAGE3
94	.globl	_idlestack,_idlestack_top
95	.set	_SMP_prvpage,_SMP_prvstart
96	.set	_SMP_prvpt,_SMP_prvstart + PAGE_SIZE
97	.set	_lapic,_SMP_prvstart + (2 * PAGE_SIZE)
98	.set	_idlestack,_SMP_prvstart + (3 * PAGE_SIZE)
99	.set	_idlestack_top,_SMP_prvstart + (4 * PAGE_SIZE)
100	.set	_prv_CPAGE1,_SMP_prvstart + (4 * PAGE_SIZE)
101	.set	_prv_CPAGE2,_SMP_prvstart + (5 * PAGE_SIZE)
102	.set	_prv_CPAGE3,_SMP_prvstart + (6 * PAGE_SIZE)
103	.set	_SMP_ioapic,_SMP_prvstart + (16 * PAGE_SIZE)
104
105	.globl	_cpuid,_curproc,_curpcb,_npxproc,_runtime,_cpu_lockid
106	.globl	_common_tss,_other_cpus,_my_idlePTD,_ss_tpr
107	.globl	_prv_CMAP1,_prv_CMAP2,_prv_CMAP3
108	.globl	_inside_intr
109	.set	_cpuid,_SMP_prvpage+0		/* [0] */
110	.set	_curproc,_SMP_prvpage+4		/* [1] */
111	.set	_curpcb,_SMP_prvpage+8		/* [2] */
112	.set	_npxproc,_SMP_prvpage+12	/* [3] */
113	.set	_runtime,_SMP_prvpage+16	/* [4,5] */
114	.set	_cpu_lockid,_SMP_prvpage+24	/* [6] */
115	.set	_other_cpus,_SMP_prvpage+28	/* [7] bitmap of available CPUs,
116						    excluding ourself */
117	.set	_my_idlePTD,_SMP_prvpage+32	/* [8] */
118	.set	_ss_tpr,_SMP_prvpage+36		/* [9] */
119	.set	_prv_CMAP1,_SMP_prvpage+40	/* [10] */
120	.set	_prv_CMAP2,_SMP_prvpage+44	/* [11] */
121	.set	_prv_CMAP3,_SMP_prvpage+48	/* [12] */
122	.set	_inside_intr,_SMP_prvpage+52	/* [13] */
123	.set	_common_tss,_SMP_prvpage+56	/* 102 (ie: 104) bytes long */
124
125/* Fetch the .set's for the local apic */
126#include "i386/i386/mp_apicdefs.s"
127
128#endif
129
130/*
131 * Globals
132 */
133	.data
134	ALIGN_DATA		/* just to be sure */
135
136	.globl	HIDENAME(tmpstk)
137	.space	0x2000		/* space for tmpstk - temporary stack */
138HIDENAME(tmpstk):
139
140	.globl	_boothowto,_bootdev
141
142	.globl	_cpu,_cpu_vendor,_cpu_id,_bootinfo
143	.globl	_cpu_high, _cpu_feature
144
145_cpu:	.long	0				/* are we 386, 386sx, or 486 */
146_cpu_id:	.long	0			/* stepping ID */
147_cpu_high:	.long	0			/* highest arg to CPUID */
148_cpu_feature:	.long	0			/* features */
149_cpu_vendor:	.space	20			/* CPU origin code */
150_bootinfo:	.space	BOOTINFO_SIZE		/* bootinfo that we can handle */
151
152_KERNend:	.long	0			/* phys addr end of kernel (just after bss) */
153physfree:	.long	0			/* phys addr of next free page */
154
155#ifdef SMP
156cpu0pp:		.long	0			/* phys addr cpu0 private pg */
157cpu0pt:		.long	0			/* phys addr cpu0 private pt */
158
159		.globl	_cpu0prvpage,_cpu0prvpt
160_cpu0prvpage:	.long	0			/* relocated version */
161_cpu0prvpt:	.long	0			/* relocated version */
162#endif /* SMP */
163
164	.globl	_IdlePTD
165_IdlePTD:	.long	0			/* phys addr of kernel PTD */
166
167#ifdef SMP
168	.globl	_KPTphys
169#endif
170_KPTphys:	.long	0			/* phys addr of kernel page tables */
171
172	.globl	_proc0paddr
173_proc0paddr:	.long	0			/* address of proc 0 address space */
174p0upa:		.long	0			/* phys addr of proc0's UPAGES */
175
176#ifdef BDE_DEBUGGER
177	.globl	_bdb_exists			/* flag to indicate BDE debugger is present */
178_bdb_exists:	.long	0
179#endif
180
181
182/**********************************************************************
183 *
184 * Some handy macros
185 *
186 */
187
188#define R(foo) ((foo)-KERNBASE)
189
190#define ALLOCPAGES(foo) \
191	movl	R(physfree), %esi ; \
192	movl	$((foo)*PAGE_SIZE), %eax ; \
193	addl	%esi, %eax ; \
194	movl	%eax, R(physfree) ; \
195	movl	%esi, %edi ; \
196	movl	$((foo)*PAGE_SIZE),%ecx ; \
197	xorl	%eax,%eax ; \
198	cld ; \
199	rep ; \
200	stosb
201
202/*
203 * fillkpt
204 *	eax = page frame address
205 *	ebx = index into page table
206 *	ecx = how many pages to map
207 * 	base = base address of page dir/table
208 *	prot = protection bits
209 */
210#define	fillkpt(base, prot)		  \
211	shll	$2,%ebx			; \
212	addl	base,%ebx		; \
213	orl	$PG_V,%eax		; \
214	orl	prot,%eax		; \
2151:	movl	%eax,(%ebx)		; \
216	addl	$PAGE_SIZE,%eax		; /* increment physical address */ \
217	addl	$4,%ebx			; /* next pte */ \
218	loop	1b
219
220/*
221 * fillkptphys(prot)
222 *	eax = physical address
223 *	ecx = how many pages to map
224 *	prot = protection bits
225 */
226#define	fillkptphys(prot)		  \
227	movl	%eax, %ebx		; \
228	shrl	$PAGE_SHIFT, %ebx	; \
229	fillkpt(R(_KPTphys), prot)
230
231	.text
232/**********************************************************************
233 *
234 * This is where the bootblocks start us, set the ball rolling...
235 *
236 */
237NON_GPROF_ENTRY(btext)
238
239#ifdef PC98
240	jmp	1f
241	.globl	_pc98_system_parameter
242	.org	0x400
243_pc98_system_parameter:
244	.space	0x240		/* BIOS parameter block */
2451:
246	/* save SYSTEM PARAMETER for resume (NS/T or other) */
247	movl	$0xa1000,%esi
248	movl	$0x100000,%edi
249	movl	$0x0630,%ecx
250	cld
251	rep
252	movsb
253#else	/* IBM-PC */
254#ifdef BDE_DEBUGGER
255#ifdef BIOS_STEALS_3K
256	cmpl	$0x0375c339,0x95504
257#else
258	cmpl	$0x0375c339,0x96104	/* XXX - debugger signature */
259#endif
260	jne	1f
261	movb	$1,R(_bdb_exists)
2621:
263#endif
264
265/* Tell the bios to warmboot next time */
266	movw	$0x1234,0x472
267#endif	/* PC98 */
268
269/* Set up a real frame in case the double return in newboot is executed. */
270	pushl	%ebp
271	movl	%esp, %ebp
272
273/* Don't trust what the BIOS gives for eflags. */
274	pushl	$PSL_KERNEL
275	popfl
276
277/*
278 * Don't trust what the BIOS gives for %fs and %gs.  Trust the bootstrap
279 * to set %cs, %ds, %es and %ss.
280 */
281	mov	%ds, %ax
282	mov	%ax, %fs
283	mov	%ax, %gs
284
285	call	recover_bootinfo
286
287/* Get onto a stack that we can trust. */
288/*
289 * XXX this step is delayed in case recover_bootinfo needs to return via
290 * the old stack, but it need not be, since recover_bootinfo actually
291 * returns via the old frame.
292 */
293	movl	$R(HIDENAME(tmpstk)),%esp
294
295#ifdef PC98
296	testb	$0x02,0x100620		/* pc98_machine_type & M_EPSON_PC98 */
297	jz	3f
298	cmpb	$0x0b,0x100624		/* epson_machine_id <= 0x0b */
299	ja	3f
300
301	/* count up memory */
302	movl	$0x100000,%eax		/* next, talley remaining memory */
303	movl	$0xFFF-0x100,%ecx
3041:	movl	0(%eax),%ebx		/* save location to check */
305	movl	$0xa55a5aa5,0(%eax)	/* write test pattern */
306	cmpl	$0xa55a5aa5,0(%eax)	/* does not check yet for rollover */
307	jne	2f
308	movl	%ebx,0(%eax)		/* restore memory */
309	addl	$PAGE_SIZE,%eax
310	loop	1b
3112:	subl	$0x100000,%eax
312	shrl	$17,%eax
313	movb	%al,0x100401
3143:
315#endif
316
317	call	identify_cpu
318
319/* clear bss */
320/*
321 * XXX this should be done a little earlier.
322 *
323 * XXX we don't check that there is memory for our bss and page tables
324 * before using it.
325 *
326 * XXX the boot program somewhat bogusly clears the bss.  We still have
327 * to do it in case we were unzipped by kzipboot.  Then the boot program
328 * only clears kzipboot's bss.
329 *
330 * XXX the gdt and idt are still somewhere in the boot program.  We
331 * depend on the convention that the boot program is below 1MB and we
332 * are above 1MB to keep the gdt and idt  away from the bss and page
333 * tables.  The idt is only used if BDE_DEBUGGER is enabled.
334 */
335	movl	$R(_end),%ecx
336	movl	$R(_edata),%edi
337	subl	%edi,%ecx
338	xorl	%eax,%eax
339	cld
340	rep
341	stosb
342
343#if NAPM > 0
344/*
345 * XXX it's not clear that APM can live in the current environonment.
346 * Only pc-relative addressing works.
347 */
348	call	_apm_setup
349#endif
350
351	call	create_pagetables
352
353#ifdef VM86
354/*
355 * If the CPU has support for VME, turn it on.
356 */
357	testl	$CPUID_VME, R(_cpu_feature)
358	jz	1f
359	movl	%cr4, %eax
360	orl	$CR4_VME, %eax
361	movl	%eax, %cr4
3621:
363#endif /* VM86 */
364
365#ifdef BDE_DEBUGGER
366/*
367 * Adjust as much as possible for paging before enabling paging so that the
368 * adjustments can be traced.
369 */
370	call	bdb_prepare_paging
371#endif
372
373/* Now enable paging */
374	movl	R(_IdlePTD), %eax
375	movl	%eax,%cr3			/* load ptd addr into mmu */
376	movl	%cr0,%eax			/* get control word */
377	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
378	movl	%eax,%cr0			/* and let's page NOW! */
379
380#ifdef BDE_DEBUGGER
381/*
382 * Complete the adjustments for paging so that we can keep tracing through
383 * initi386() after the low (physical) addresses for the gdt and idt become
384 * invalid.
385 */
386	call	bdb_commit_paging
387#endif
388
389	pushl	$begin				/* jump to high virtualized address */
390	ret
391
392/* now running relocated at KERNBASE where the system is linked to run */
393begin:
394	/* set up bootstrap stack */
395	movl	_proc0paddr,%esp	/* location of in-kernel pages */
396	addl	$UPAGES*PAGE_SIZE,%esp	/* bootstrap stack end location */
397	xorl	%eax,%eax			/* mark end of frames */
398	movl	%eax,%ebp
399	movl	_proc0paddr,%eax
400	movl	_IdlePTD, %esi
401	movl	%esi,PCB_CR3(%eax)
402
403	movl	physfree, %esi
404	pushl	%esi				/* value of first for init386(first) */
405	call	_init386			/* wire 386 chip for unix operation */
406	popl	%esi
407
408	.globl	__ucodesel,__udatasel
409
410	pushl	$0				/* unused */
411	pushl	__udatasel			/* ss */
412	pushl	$0				/* esp - filled in by execve() */
413	pushl	$PSL_USER			/* eflags (IOPL 0, int enab) */
414	pushl	__ucodesel			/* cs */
415	pushl	$0				/* eip - filled in by execve() */
416	subl	$(12*4),%esp			/* space for rest of registers */
417
418	pushl	%esp				/* call main with frame pointer */
419	call	_main				/* autoconfiguration, mountroot etc */
420
421	hlt		/* never returns to here */
422
423/*
424 * When starting init, call this to configure the process for user
425 * mode.  This will be inherited by other processes.
426 */
427NON_GPROF_ENTRY(prepare_usermode)
428	/*
429	 * Now we've run main() and determined what cpu-type we are, we can
430	 * enable write protection and alignment checking on i486 cpus and
431	 * above.
432	 */
433#if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
434	cmpl    $CPUCLASS_386,_cpu_class
435	je	1f
436	movl	%cr0,%eax			/* get control word */
437	orl	$CR0_WP|CR0_AM,%eax		/* enable i486 features */
438	movl	%eax,%cr0			/* and do it */
4391:
440#endif
441	/*
442	 * on return from main(), we are process 1
443	 * set up address space and stack so that we can 'return' to user mode
444	 */
445	movl	__ucodesel,%eax
446	movl	__udatasel,%ecx
447
448#if 0
449	movl	%cx,%ds
450#endif
451	movl	%cx,%es
452	movl	%ax,%fs				/* double map cs to fs */
453	movl	%cx,%gs				/* and ds to gs */
454	ret					/* goto user! */
455
456
457#define LCALL(x,y)	.byte 0x9a ; .long y ; .word x
458
459/*
460 * Signal trampoline, copied to top of user stack
461 */
462NON_GPROF_ENTRY(sigcode)
463	call	SIGF_HANDLER(%esp)
464	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
465						/* copy at 8(%esp)) */
466	pushl	%eax
467	pushl	%eax				/* junk to fake return address */
468	movl	$SYS_sigreturn,%eax		/* sigreturn() */
469	LCALL(0x7,0)				/* enter kernel with args on stack */
470	hlt					/* never gets here */
471	ALIGN_TEXT
472_esigcode:
473
474	.data
475	.globl	_szsigcode
476_szsigcode:
477	.long	_esigcode-_sigcode
478	.text
479
480/**********************************************************************
481 *
482 * Recover the bootinfo passed to us from the boot program
483 *
484 */
485recover_bootinfo:
486	/*
487	 * This code is called in different ways depending on what loaded
488	 * and started the kernel.  This is used to detect how we get the
489	 * arguments from the other code and what we do with them.
490	 *
491	 * Old disk boot blocks:
492	 *	(*btext)(howto, bootdev, cyloffset, esym);
493	 *	[return address == 0, and can NOT be returned to]
494	 *	[cyloffset was not supported by the FreeBSD boot code
495	 *	 and always passed in as 0]
496	 *	[esym is also known as total in the boot code, and
497	 *	 was never properly supported by the FreeBSD boot code]
498	 *
499	 * Old diskless netboot code:
500	 *	(*btext)(0,0,0,0,&nfsdiskless,0,0,0);
501	 *	[return address != 0, and can NOT be returned to]
502	 *	If we are being booted by this code it will NOT work,
503	 *	so we are just going to halt if we find this case.
504	 *
505	 * New uniform boot code:
506	 *	(*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
507	 *	[return address != 0, and can be returned to]
508	 *
509	 * There may seem to be a lot of wasted arguments in here, but
510	 * that is so the newer boot code can still load very old kernels
511	 * and old boot code can load new kernels.
512	 */
513
514	/*
515	 * The old style disk boot blocks fake a frame on the stack and
516	 * did an lret to get here.  The frame on the stack has a return
517	 * address of 0.
518	 */
519	cmpl	$0,4(%ebp)
520	je	olddiskboot
521
522	/*
523	 * We have some form of return address, so this is either the
524	 * old diskless netboot code, or the new uniform code.  That can
525	 * be detected by looking at the 5th argument, if it is 0
526	 * we are being booted by the new uniform boot code.
527	 */
528	cmpl	$0,24(%ebp)
529	je	newboot
530
531	/*
532	 * Seems we have been loaded by the old diskless boot code, we
533	 * don't stand a chance of running as the diskless structure
534	 * changed considerably between the two, so just halt.
535	 */
536	 hlt
537
538	/*
539	 * We have been loaded by the new uniform boot code.
540	 * Let's check the bootinfo version, and if we do not understand
541	 * it we return to the loader with a status of 1 to indicate this error
542	 */
543newboot:
544	movl	28(%ebp),%ebx		/* &bootinfo.version */
545	movl	BI_VERSION(%ebx),%eax
546	cmpl	$1,%eax			/* We only understand version 1 */
547	je	1f
548	movl	$1,%eax			/* Return status */
549	leave
550	/*
551	 * XXX this returns to our caller's caller (as is required) since
552	 * we didn't set up a frame and our caller did.
553	 */
554	ret
555
5561:
557	/*
558	 * If we have a kernelname copy it in
559	 */
560	movl	BI_KERNELNAME(%ebx),%esi
561	cmpl	$0,%esi
562	je	2f			/* No kernelname */
563	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
564	movl	$R(_kernelname),%edi
565	cmpb	$'/',(%esi)		/* Make sure it starts with a slash */
566	je	1f
567	movb	$'/',(%edi)
568	incl	%edi
569	decl	%ecx
5701:
571	cld
572	rep
573	movsb
574
5752:
576	/*
577	 * Determine the size of the boot loader's copy of the bootinfo
578	 * struct.  This is impossible to do properly because old versions
579	 * of the struct don't contain a size field and there are 2 old
580	 * versions with the same version number.
581	 */
582	movl	$BI_ENDCOMMON,%ecx	/* prepare for sizeless version */
583	testl	$RB_BOOTINFO,8(%ebp)	/* bi_size (and bootinfo) valid? */
584	je	got_bi_size		/* no, sizeless version */
585	movl	BI_SIZE(%ebx),%ecx
586got_bi_size:
587
588	/*
589	 * Copy the common part of the bootinfo struct
590	 */
591	movl	%ebx,%esi
592	movl	$R(_bootinfo),%edi
593	cmpl	$BOOTINFO_SIZE,%ecx
594	jbe	got_common_bi_size
595	movl	$BOOTINFO_SIZE,%ecx
596got_common_bi_size:
597	cld
598	rep
599	movsb
600
601#ifdef NFS
602#ifndef BOOTP_NFSV3
603	/*
604	 * If we have a nfs_diskless structure copy it in
605	 */
606	movl	BI_NFS_DISKLESS(%ebx),%esi
607	cmpl	$0,%esi
608	je	olddiskboot
609	movl	$R(_nfs_diskless),%edi
610	movl	$NFSDISKLESS_SIZE,%ecx
611	cld
612	rep
613	movsb
614	movl	$R(_nfs_diskless_valid),%edi
615	movl	$1,(%edi)
616#endif
617#endif
618
619	/*
620	 * The old style disk boot.
621	 *	(*btext)(howto, bootdev, cyloffset, esym);
622	 * Note that the newer boot code just falls into here to pick
623	 * up howto and bootdev, cyloffset and esym are no longer used
624	 */
625olddiskboot:
626	movl	8(%ebp),%eax
627	movl	%eax,R(_boothowto)
628	movl	12(%ebp),%eax
629	movl	%eax,R(_bootdev)
630
631#if defined(USERCONFIG_BOOT) && defined(USERCONFIG)
632#ifdef PC98
633	movl	$0x90200, %esi
634#else
635	movl	$0x10200, %esi
636#endif
637	movl	$R(_userconfig_from_boot),%edi
638	movl	$512,%ecx
639	cld
640	rep
641	movsb
642#endif /* USERCONFIG_BOOT */
643
644	ret
645
646
647/**********************************************************************
648 *
649 * Identify the CPU and initialize anything special about it
650 *
651 */
652identify_cpu:
653
654	/* Try to toggle alignment check flag; does not exist on 386. */
655	pushfl
656	popl	%eax
657	movl	%eax,%ecx
658	orl	$PSL_AC,%eax
659	pushl	%eax
660	popfl
661	pushfl
662	popl	%eax
663	xorl	%ecx,%eax
664	andl	$PSL_AC,%eax
665	pushl	%ecx
666	popfl
667
668	testl	%eax,%eax
669	jnz	try486
670
671	/* NexGen CPU does not have aligment check flag. */
672	pushfl
673	movl	$0x5555, %eax
674	xorl	%edx, %edx
675	movl	$2, %ecx
676	clc
677	divl	%ecx
678	jz	trynexgen
679	popfl
680	movl	$CPU_386,R(_cpu)
681	jmp	3f
682
683trynexgen:
684	popfl
685	movl	$CPU_NX586,R(_cpu)
686	movl	$0x4778654e,R(_cpu_vendor)	# store vendor string
687	movl	$0x72446e65,R(_cpu_vendor+4)
688	movl	$0x6e657669,R(_cpu_vendor+8)
689	movl	$0,R(_cpu_vendor+12)
690	jmp	3f
691
692try486:	/* Try to toggle identification flag; does not exist on early 486s. */
693	pushfl
694	popl	%eax
695	movl	%eax,%ecx
696	xorl	$PSL_ID,%eax
697	pushl	%eax
698	popfl
699	pushfl
700	popl	%eax
701	xorl	%ecx,%eax
702	andl	$PSL_ID,%eax
703	pushl	%ecx
704	popfl
705
706	testl	%eax,%eax
707	jnz	trycpuid
708	movl	$CPU_486,R(_cpu)
709
710	/*
711	 * Check Cyrix CPU
712	 * Cyrix CPUs do not change the undefined flags following
713	 * execution of the divide instruction which divides 5 by 2.
714	 *
715	 * Note: CPUID is enabled on M2, so it passes another way.
716	 */
717	pushfl
718	movl	$0x5555, %eax
719	xorl	%edx, %edx
720	movl	$2, %ecx
721	clc
722	divl	%ecx
723	jnc	trycyrix
724	popfl
725	jmp	3f		/* You may use Intel CPU. */
726
727trycyrix:
728	popfl
729	/*
730	 * IBM Bluelighting CPU also doesn't change the undefined flags.
731	 * Because IBM doesn't disclose the information for Bluelighting
732	 * CPU, we couldn't distinguish it from Cyrix's (including IBM
733	 * brand of Cyrix CPUs).
734	 */
735	movl	$0x69727943,R(_cpu_vendor)	# store vendor string
736	movl	$0x736e4978,R(_cpu_vendor+4)
737	movl	$0x64616574,R(_cpu_vendor+8)
738	jmp	3f
739
740trycpuid:	/* Use the `cpuid' instruction. */
741	xorl	%eax,%eax
742	.byte	0x0f,0xa2			# cpuid 0
743	movl	%eax,R(_cpu_high)		# highest capability
744	movl	%ebx,R(_cpu_vendor)		# store vendor string
745	movl	%edx,R(_cpu_vendor+4)
746	movl	%ecx,R(_cpu_vendor+8)
747	movb	$0,R(_cpu_vendor+12)
748
749	movl	$1,%eax
750	.byte	0x0f,0xa2			# cpuid 1
751	movl	%eax,R(_cpu_id)			# store cpu_id
752	movl	%edx,R(_cpu_feature)		# store cpu_feature
753	rorl	$8,%eax				# extract family type
754	andl	$15,%eax
755	cmpl	$5,%eax
756	jae	1f
757
758	/* less than Pentium; must be 486 */
759	movl	$CPU_486,R(_cpu)
760	jmp	3f
7611:
762	/* a Pentium? */
763	cmpl	$5,%eax
764	jne	2f
765	movl	$CPU_586,R(_cpu)
766	jmp	3f
7672:
768	/* Greater than Pentium...call it a Pentium Pro */
769	movl	$CPU_686,R(_cpu)
7703:
771	ret
772
773
774/**********************************************************************
775 *
776 * Create the first page directory and its page tables.
777 *
778 */
779
780create_pagetables:
781
782	testl	$CPUID_PGE, R(_cpu_feature)
783	jz	1f
784	movl	%cr4, %eax
785	orl	$CR4_PGE, %eax
786	movl	%eax, %cr4
7871:
788
789/* Find end of kernel image (rounded up to a page boundary). */
790	movl	$R(_end),%esi
791
792/* include symbols in "kernel image" if they are loaded and useful */
793#ifdef DDB
794	movl	R(_bootinfo+BI_ESYMTAB),%edi
795	testl	%edi,%edi
796	je	over_symalloc
797	movl	%edi,%esi
798	movl	$KERNBASE,%edi
799	addl	%edi,R(_bootinfo+BI_SYMTAB)
800	addl	%edi,R(_bootinfo+BI_ESYMTAB)
801over_symalloc:
802#endif
803
804	addl	$PAGE_MASK,%esi
805	andl	$~PAGE_MASK,%esi
806	movl	%esi,R(_KERNend)	/* save end of kernel */
807	movl	%esi,R(physfree)	/* next free page is at end of kernel */
808
809/* Allocate Kernel Page Tables */
810	ALLOCPAGES(NKPT)
811	movl	%esi,R(_KPTphys)
812
813/* Allocate Page Table Directory */
814	ALLOCPAGES(1)
815	movl	%esi,R(_IdlePTD)
816
817/* Allocate UPAGES */
818	ALLOCPAGES(UPAGES)
819	movl	%esi,R(p0upa)
820	addl	$KERNBASE, %esi
821	movl	%esi, R(_proc0paddr)
822
823#ifdef SMP
824/* Allocate cpu0's private data page */
825	ALLOCPAGES(1)
826	movl	%esi,R(cpu0pp)
827	addl	$KERNBASE, %esi
828	movl	%esi, R(_cpu0prvpage)	/* relocated to KVM space */
829
830/* Allocate cpu0's private page table for mapping priv page, apic, etc */
831	ALLOCPAGES(1)
832	movl	%esi,R(cpu0pt)
833	addl	$KERNBASE, %esi
834	movl	%esi, R(_cpu0prvpt)	/* relocated to KVM space */
835#endif	/* SMP */
836
837/* Map read-only from zero to the end of the kernel text section */
838	xorl	%eax, %eax
839#ifdef BDE_DEBUGGER
840/* If the debugger is present, actually map everything read-write. */
841	cmpl	$0,R(_bdb_exists)
842	jne	map_read_write
843#endif
844	xorl	%edx,%edx
845
846#if !defined(SMP)
847	testl	$CPUID_PGE, R(_cpu_feature)
848	jz	2f
849	orl	$PG_G,%edx
850#endif
851
8522:	movl	$R(_etext),%ecx
853	addl	$PAGE_MASK,%ecx
854	shrl	$PAGE_SHIFT,%ecx
855	fillkptphys(%edx)
856
857/* Map read-write, data, bss and symbols */
858	movl	$R(_etext),%eax
859	addl	$PAGE_MASK, %eax
860	andl	$~PAGE_MASK, %eax
861map_read_write:
862	movl	$PG_RW,%edx
863#if !defined(SMP)
864	testl	$CPUID_PGE, R(_cpu_feature)
865	jz	1f
866	orl	$PG_G,%edx
867#endif
868
8691:	movl	R(_KERNend),%ecx
870	subl	%eax,%ecx
871	shrl	$PAGE_SHIFT,%ecx
872	fillkptphys(%edx)
873
874/* Map page directory. */
875	movl	R(_IdlePTD), %eax
876	movl	$1, %ecx
877	fillkptphys($PG_RW)
878
879/* Map proc0's UPAGES in the physical way ... */
880	movl	R(p0upa), %eax
881	movl	$UPAGES, %ecx
882	fillkptphys($PG_RW)
883
884/* Map ISA hole */
885	movl	$ISA_HOLE_START, %eax
886	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
887	fillkptphys($PG_RW)
888
889#ifdef SMP
890/* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */
891	movl	R(cpu0pp), %eax
892	movl	$1, %ecx
893	fillkptphys($PG_RW)
894
895/* Map cpu0's private page table into global kmem FWIW */
896	movl	R(cpu0pt), %eax
897	movl	$1, %ecx
898	fillkptphys($PG_RW)
899
900/* Map the private page into the private page table into private space */
901	movl	R(cpu0pp), %eax
902	movl	$0, %ebx		/* pte offset = 0 */
903	movl	$1, %ecx		/* one private page coming right up */
904	fillkpt(R(cpu0pt), $PG_RW)
905
906/* Map the page table page into private space */
907	movl	R(cpu0pt), %eax
908	movl	$1, %ebx		/* pte offset = 1 */
909	movl	$1, %ecx		/* one private pt coming right up */
910	fillkpt(R(cpu0pt), $PG_RW)
911
912/* ... and put the page table table in the pde. */
913	movl	R(cpu0pt), %eax
914	movl	$MPPTDI, %ebx
915	movl	$1, %ecx
916	fillkpt(R(_IdlePTD), $PG_RW)
917#endif	/* SMP */
918
919/* install a pde for temporary double map of bottom of VA */
920	movl	R(_KPTphys), %eax
921	xorl	%ebx, %ebx
922	movl	$1, %ecx
923	fillkpt(R(_IdlePTD), $PG_RW)
924
925/* install pde's for pt's */
926	movl	R(_KPTphys), %eax
927	movl	$KPTDI, %ebx
928	movl	$NKPT, %ecx
929	fillkpt(R(_IdlePTD), $PG_RW)
930
931/* install a pde recursively mapping page directory as a page table */
932	movl	R(_IdlePTD), %eax
933	movl	$PTDPTDI, %ebx
934	movl	$1,%ecx
935	fillkpt(R(_IdlePTD), $PG_RW)
936
937	ret
938
939#ifdef BDE_DEBUGGER
940bdb_prepare_paging:
941	cmpl	$0,R(_bdb_exists)
942	je	bdb_prepare_paging_exit
943
944	subl	$6,%esp
945
946	/*
947	 * Copy and convert debugger entries from the bootstrap gdt and idt
948	 * to the kernel gdt and idt.  Everything is still in low memory.
949	 * Tracing continues to work after paging is enabled because the
950	 * low memory addresses remain valid until everything is relocated.
951	 * However, tracing through the setidt() that initializes the trace
952	 * trap will crash.
953	 */
954	sgdt	(%esp)
955	movl	2(%esp),%esi		/* base address of bootstrap gdt */
956	movl	$R(_gdt),%edi
957	movl	%edi,2(%esp)		/* prepare to load kernel gdt */
958	movl	$8*18/4,%ecx
959	cld
960	rep				/* copy gdt */
961	movsl
962	movl	$R(_gdt),-8+2(%edi)	/* adjust gdt self-ptr */
963	movb	$0x92,-8+5(%edi)
964	lgdt	(%esp)
965
966	sidt	(%esp)
967	movl	2(%esp),%esi		/* base address of current idt */
968	movl	8+4(%esi),%eax		/* convert dbg descriptor to ... */
969	movw	8(%esi),%ax
970	movl	%eax,R(bdb_dbg_ljmp+1)	/* ... immediate offset ... */
971	movl	8+2(%esi),%eax
972	movw	%ax,R(bdb_dbg_ljmp+5)	/* ... and selector for ljmp */
973	movl	24+4(%esi),%eax		/* same for bpt descriptor */
974	movw	24(%esi),%ax
975	movl	%eax,R(bdb_bpt_ljmp+1)
976	movl	24+2(%esi),%eax
977	movw	%ax,R(bdb_bpt_ljmp+5)
978	movl	$R(_idt),%edi
979	movl	%edi,2(%esp)		/* prepare to load kernel idt */
980	movl	$8*4/4,%ecx
981	cld
982	rep				/* copy idt */
983	movsl
984	lidt	(%esp)
985
986	addl	$6,%esp
987
988bdb_prepare_paging_exit:
989	ret
990
991/* Relocate debugger gdt entries and gdt and idt pointers. */
992bdb_commit_paging:
993	cmpl	$0,_bdb_exists
994	je	bdb_commit_paging_exit
995
996	movl	$_gdt+8*9,%eax		/* adjust slots 9-17 */
997	movl	$9,%ecx
998reloc_gdt:
999	movb	$KERNBASE>>24,7(%eax)	/* top byte of base addresses, was 0, */
1000	addl	$8,%eax			/* now KERNBASE>>24 */
1001	loop	reloc_gdt
1002
1003	subl	$6,%esp
1004	sgdt	(%esp)
1005	addl	$KERNBASE,2(%esp)
1006	lgdt	(%esp)
1007	sidt	(%esp)
1008	addl	$KERNBASE,2(%esp)
1009	lidt	(%esp)
1010	addl	$6,%esp
1011
1012	int	$3
1013
1014bdb_commit_paging_exit:
1015	ret
1016
1017#endif /* BDE_DEBUGGER */
1018