locore.s revision 113040
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 * $FreeBSD: head/sys/i386/i386/locore.s 113040 2003-04-03 23:44:35Z jake $
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 "opt_bootp.h"
47#include "opt_compat.h"
48#include "opt_nfsroot.h"
49
50#include <sys/syscall.h>
51#include <sys/reboot.h>
52
53#include <machine/asmacros.h>
54#include <machine/cputypes.h>
55#include <machine/psl.h>
56#include <machine/pmap.h>
57#include <machine/specialreg.h>
58
59#include "assym.s"
60
61/*
62 *	XXX
63 *
64 * Note: This version greatly munged to avoid various assembler errors
65 * that may be fixed in newer versions of gas. Perhaps newer versions
66 * will have more pleasant appearance.
67 */
68
69/*
70 * PTmap is recursive pagemap at top of virtual address space.
71 * Within PTmap, the page directory can be found (third indirection).
72 */
73	.globl	PTmap,PTD,PTDpde
74	.set	PTmap,(PTDPTDI << PDRSHIFT)
75	.set	PTD,PTmap + (PTDPTDI * PAGE_SIZE)
76	.set	PTDpde,PTD + (PTDPTDI * PDESIZE)
77
78#ifdef SMP
79/*
80 * Define layout of per-cpu address space.
81 * This is "constructed" in locore.s on the BSP and in mp_machdep.c
82 * for each AP.  DO NOT REORDER THESE WITHOUT UPDATING THE REST!
83 */
84	.globl	SMP_prvspace, lapic
85	.set	SMP_prvspace,(MPPTDI << PDRSHIFT)
86	.set	lapic,SMP_prvspace + (NPTEPG-1) * PAGE_SIZE
87#endif /* SMP */
88
89/*
90 * Compiled KERNBASE location
91 */
92	.globl	kernbase
93	.set	kernbase,KERNBASE
94
95/*
96 * Globals
97 */
98	.data
99	ALIGN_DATA			/* just to be sure */
100
101	.globl	HIDENAME(tmpstk)
102	.space	0x2000			/* space for tmpstk - temporary stack */
103HIDENAME(tmpstk):
104
105	.globl	bootinfo
106bootinfo:	.space	BOOTINFO_SIZE	/* bootinfo that we can handle */
107
108		.globl KERNend
109KERNend:	.long	0		/* phys addr end of kernel (just after bss) */
110physfree:	.long	0		/* phys addr of next free page */
111
112#ifdef SMP
113		.globl	cpu0prvpage
114cpu0pp:		.long	0		/* phys addr cpu0 private pg */
115cpu0prvpage:	.long	0		/* relocated version */
116
117		.globl	SMPpt
118SMPptpa:	.long	0		/* phys addr SMP page table */
119SMPpt:		.long	0		/* relocated version */
120#endif /* SMP */
121
122	.globl	IdlePTD
123IdlePTD:	.long	0		/* phys addr of kernel PTD */
124
125#ifdef PAE
126	.globl	IdlePDPT
127IdlePDPT:	.long	0		/* phys addr of kernel PDPT */
128#endif
129
130#ifdef SMP
131	.globl	KPTphys
132#endif
133KPTphys:	.long	0		/* phys addr of kernel page tables */
134
135	.globl	proc0uarea, proc0kstack
136proc0uarea:	.long	0		/* address of proc 0 uarea space */
137proc0kstack:	.long	0		/* address of proc 0 kstack space */
138p0upa:		.long	0		/* phys addr of proc0's UAREA */
139p0kpa:		.long	0		/* phys addr of proc0's STACK */
140
141vm86phystk:	.long	0		/* PA of vm86/bios stack */
142
143	.globl	vm86paddr, vm86pa
144vm86paddr:	.long	0		/* address of vm86 region */
145vm86pa:		.long	0		/* phys addr of vm86 region */
146
147#ifdef PC98
148	.globl	pc98_system_parameter
149pc98_system_parameter:
150	.space	0x240
151#endif
152
153/**********************************************************************
154 *
155 * Some handy macros
156 *
157 */
158
159#define R(foo) ((foo)-KERNBASE)
160
161#define ALLOCPAGES(foo) \
162	movl	R(physfree), %esi ; \
163	movl	$((foo)*PAGE_SIZE), %eax ; \
164	addl	%esi, %eax ; \
165	movl	%eax, R(physfree) ; \
166	movl	%esi, %edi ; \
167	movl	$((foo)*PAGE_SIZE),%ecx ; \
168	xorl	%eax,%eax ; \
169	cld ; \
170	rep ; \
171	stosb
172
173/*
174 * fillkpt
175 *	eax = page frame address
176 *	ebx = index into page table
177 *	ecx = how many pages to map
178 * 	base = base address of page dir/table
179 *	prot = protection bits
180 */
181#define	fillkpt(base, prot)		  \
182	shll	$PTESHIFT,%ebx		; \
183	addl	base,%ebx		; \
184	orl	$PG_V,%eax		; \
185	orl	prot,%eax		; \
1861:	movl	%eax,(%ebx)		; \
187	addl	$PAGE_SIZE,%eax		; /* increment physical address */ \
188	addl	$PTESIZE,%ebx		; /* next pte */ \
189	loop	1b
190
191/*
192 * fillkptphys(prot)
193 *	eax = physical address
194 *	ecx = how many pages to map
195 *	prot = protection bits
196 */
197#define	fillkptphys(prot)		  \
198	movl	%eax, %ebx		; \
199	shrl	$PAGE_SHIFT, %ebx	; \
200	fillkpt(R(KPTphys), prot)
201
202	.text
203/**********************************************************************
204 *
205 * This is where the bootblocks start us, set the ball rolling...
206 *
207 */
208NON_GPROF_ENTRY(btext)
209
210#ifdef PC98
211	/* save SYSTEM PARAMETER for resume (NS/T or other) */
212	movl	$0xa1400,%esi
213	movl	$R(pc98_system_parameter),%edi
214	movl	$0x0240,%ecx
215	cld
216	rep
217	movsb
218#else	/* IBM-PC */
219/* Tell the bios to warmboot next time */
220	movw	$0x1234,0x472
221#endif	/* PC98 */
222
223/* Set up a real frame in case the double return in newboot is executed. */
224	pushl	%ebp
225	movl	%esp, %ebp
226
227/* Don't trust what the BIOS gives for eflags. */
228	pushl	$PSL_KERNEL
229	popfl
230
231/*
232 * Don't trust what the BIOS gives for %fs and %gs.  Trust the bootstrap
233 * to set %cs, %ds, %es and %ss.
234 */
235	mov	%ds, %ax
236	mov	%ax, %fs
237	mov	%ax, %gs
238
239	call	recover_bootinfo
240
241/* Get onto a stack that we can trust. */
242/*
243 * XXX this step is delayed in case recover_bootinfo needs to return via
244 * the old stack, but it need not be, since recover_bootinfo actually
245 * returns via the old frame.
246 */
247	movl	$R(HIDENAME(tmpstk)),%esp
248
249#ifdef PC98
250	/* pc98_machine_type & M_EPSON_PC98 */
251	testb	$0x02,R(pc98_system_parameter)+220
252	jz	3f
253	/* epson_machine_id <= 0x0b */
254	cmpb	$0x0b,R(pc98_system_parameter)+224
255	ja	3f
256
257	/* count up memory */
258	movl	$0x100000,%eax		/* next, talley remaining memory */
259	movl	$0xFFF-0x100,%ecx
2601:	movl	0(%eax),%ebx		/* save location to check */
261	movl	$0xa55a5aa5,0(%eax)	/* write test pattern */
262	cmpl	$0xa55a5aa5,0(%eax)	/* does not check yet for rollover */
263	jne	2f
264	movl	%ebx,0(%eax)		/* restore memory */
265	addl	$PAGE_SIZE,%eax
266	loop	1b
2672:	subl	$0x100000,%eax
268	shrl	$17,%eax
269	movb	%al,R(pc98_system_parameter)+1
2703:
271
272	movw	R(pc98_system_parameter+0x86),%ax
273	movw	%ax,R(cpu_id)
274#endif
275
276	call	identify_cpu
277
278/* clear bss */
279/*
280 * XXX this should be done a little earlier.
281 *
282 * XXX we don't check that there is memory for our bss and page tables
283 * before using it.
284 *
285 * XXX the boot program somewhat bogusly clears the bss.  We still have
286 * to do it in case we were unzipped by kzipboot.  Then the boot program
287 * only clears kzipboot's bss.
288 *
289 * XXX the gdt and idt are still somewhere in the boot program.  We
290 * depend on the convention that the boot program is below 1MB and we
291 * are above 1MB to keep the gdt and idt away from the bss and page
292 * tables.
293 */
294	movl	$R(end),%ecx
295	movl	$R(edata),%edi
296	subl	%edi,%ecx
297	xorl	%eax,%eax
298	cld
299	rep
300	stosb
301
302	call	create_pagetables
303
304/*
305 * If the CPU has support for VME, turn it on.
306 */
307	testl	$CPUID_VME, R(cpu_feature)
308	jz	1f
309	movl	%cr4, %eax
310	orl	$CR4_VME, %eax
311	movl	%eax, %cr4
3121:
313
314/* Now enable paging */
315#ifdef PAE
316	movl	R(IdlePDPT), %eax
317	movl	%eax, %cr3
318	movl	%cr4, %eax
319	orl	$CR4_PAE, %eax
320	movl	%eax, %cr4
321#else
322	movl	R(IdlePTD), %eax
323	movl	%eax,%cr3		/* load ptd addr into mmu */
324#endif
325	movl	%cr0,%eax		/* get control word */
326	orl	$CR0_PE|CR0_PG,%eax	/* enable paging */
327	movl	%eax,%cr0		/* and let's page NOW! */
328
329	pushl	$begin			/* jump to high virtualized address */
330	ret
331
332/* now running relocated at KERNBASE where the system is linked to run */
333begin:
334	/* set up bootstrap stack */
335	movl	proc0kstack,%eax	/* location of in-kernel stack */
336			/* bootstrap stack end location */
337	leal	(KSTACK_PAGES*PAGE_SIZE-PCB_SIZE)(%eax),%esp
338
339	xorl	%ebp,%ebp		/* mark end of frames */
340
341#ifdef PAE
342	movl	IdlePDPT,%esi
343#else
344	movl	IdlePTD,%esi
345#endif
346	movl	%esi,(KSTACK_PAGES*PAGE_SIZE-PCB_SIZE+PCB_CR3)(%eax)
347
348	pushl	physfree		/* value of first for init386(first) */
349	call	init386			/* wire 386 chip for unix operation */
350
351	/*
352	 * Clean up the stack in a way that db_numargs() understands, so
353	 * that backtraces in ddb don't underrun the stack.  Traps for
354	 * inaccessible memory are more fatal than usual this early.
355	 */
356	addl	$4,%esp
357
358	call	mi_startup		/* autoconfiguration, mountroot etc */
359	/* NOTREACHED */
360	addl	$0,%esp			/* for db_numargs() again */
361
362/*
363 * Signal trampoline, copied to top of user stack
364 */
365NON_GPROF_ENTRY(sigcode)
366	calll	*SIGF_HANDLER(%esp)
367	leal	SIGF_UC(%esp),%eax	/* get ucontext */
368	pushl	%eax
369	testl	$PSL_VM,UC_EFLAGS(%eax)
370	jne	1f
371	movl	UC_GS(%eax),%gs		/* restore %gs */
3721:
373	movl	$SYS_sigreturn,%eax
374	pushl	%eax			/* junk to fake return addr. */
375	int	$0x80			/* enter kernel with args */
376					/* on stack */
3771:
378	jmp	1b
379
380#ifdef COMPAT_FREEBSD4
381	ALIGN_TEXT
382freebsd4_sigcode:
383	calll	*SIGF_HANDLER(%esp)
384	leal	SIGF_UC4(%esp),%eax	/* get ucontext */
385	pushl	%eax
386	testl	$PSL_VM,UC4_EFLAGS(%eax)
387	jne	1f
388	movl	UC4_GS(%eax),%gs	/* restore %gs */
3891:
390	movl	$344,%eax		/* 4.x SYS_sigreturn */
391	pushl	%eax			/* junk to fake return addr. */
392	int	$0x80			/* enter kernel with args */
393					/* on stack */
3941:
395	jmp	1b
396#endif
397
398#ifdef COMPAT_43
399	ALIGN_TEXT
400osigcode:
401	call	*SIGF_HANDLER(%esp)	/* call signal handler */
402	lea	SIGF_SC(%esp),%eax	/* get sigcontext */
403	pushl	%eax
404	testl	$PSL_VM,SC_PS(%eax)
405	jne	9f
406	movl	SC_GS(%eax),%gs		/* restore %gs */
4079:
408	movl	$103,%eax		/* 3.x SYS_sigreturn */
409	pushl	%eax			/* junk to fake return addr. */
410	int	$0x80			/* enter kernel with args */
4110:	jmp	0b
412#endif /* COMPAT_43 */
413
414	ALIGN_TEXT
415esigcode:
416
417	.data
418	.globl	szsigcode
419szsigcode:
420	.long	esigcode-sigcode
421#ifdef COMPAT_FREEBSD4
422	.globl	szfreebsd4_sigcode
423szfreebsd4_sigcode:
424	.long	esigcode-freebsd4_sigcode
425#endif
426#ifdef COMPAT_43
427	.globl	szosigcode
428szosigcode:
429	.long	esigcode-osigcode
430#endif
431	.text
432
433/**********************************************************************
434 *
435 * Recover the bootinfo passed to us from the boot program
436 *
437 */
438recover_bootinfo:
439	/*
440	 * This code is called in different ways depending on what loaded
441	 * and started the kernel.  This is used to detect how we get the
442	 * arguments from the other code and what we do with them.
443	 *
444	 * Old disk boot blocks:
445	 *	(*btext)(howto, bootdev, cyloffset, esym);
446	 *	[return address == 0, and can NOT be returned to]
447	 *	[cyloffset was not supported by the FreeBSD boot code
448	 *	 and always passed in as 0]
449	 *	[esym is also known as total in the boot code, and
450	 *	 was never properly supported by the FreeBSD boot code]
451	 *
452	 * Old diskless netboot code:
453	 *	(*btext)(0,0,0,0,&nfsdiskless,0,0,0);
454	 *	[return address != 0, and can NOT be returned to]
455	 *	If we are being booted by this code it will NOT work,
456	 *	so we are just going to halt if we find this case.
457	 *
458	 * New uniform boot code:
459	 *	(*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
460	 *	[return address != 0, and can be returned to]
461	 *
462	 * There may seem to be a lot of wasted arguments in here, but
463	 * that is so the newer boot code can still load very old kernels
464	 * and old boot code can load new kernels.
465	 */
466
467	/*
468	 * The old style disk boot blocks fake a frame on the stack and
469	 * did an lret to get here.  The frame on the stack has a return
470	 * address of 0.
471	 */
472	cmpl	$0,4(%ebp)
473	je	olddiskboot
474
475	/*
476	 * We have some form of return address, so this is either the
477	 * old diskless netboot code, or the new uniform code.  That can
478	 * be detected by looking at the 5th argument, if it is 0
479	 * we are being booted by the new uniform boot code.
480	 */
481	cmpl	$0,24(%ebp)
482	je	newboot
483
484	/*
485	 * Seems we have been loaded by the old diskless boot code, we
486	 * don't stand a chance of running as the diskless structure
487	 * changed considerably between the two, so just halt.
488	 */
489	 hlt
490
491	/*
492	 * We have been loaded by the new uniform boot code.
493	 * Let's check the bootinfo version, and if we do not understand
494	 * it we return to the loader with a status of 1 to indicate this error
495	 */
496newboot:
497	movl	28(%ebp),%ebx		/* &bootinfo.version */
498	movl	BI_VERSION(%ebx),%eax
499	cmpl	$1,%eax			/* We only understand version 1 */
500	je	1f
501	movl	$1,%eax			/* Return status */
502	leave
503	/*
504	 * XXX this returns to our caller's caller (as is required) since
505	 * we didn't set up a frame and our caller did.
506	 */
507	ret
508
5091:
510	/*
511	 * If we have a kernelname copy it in
512	 */
513	movl	BI_KERNELNAME(%ebx),%esi
514	cmpl	$0,%esi
515	je	2f			/* No kernelname */
516	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
517	movl	$R(kernelname),%edi
518	cmpb	$'/',(%esi)		/* Make sure it starts with a slash */
519	je	1f
520	movb	$'/',(%edi)
521	incl	%edi
522	decl	%ecx
5231:
524	cld
525	rep
526	movsb
527
5282:
529	/*
530	 * Determine the size of the boot loader's copy of the bootinfo
531	 * struct.  This is impossible to do properly because old versions
532	 * of the struct don't contain a size field and there are 2 old
533	 * versions with the same version number.
534	 */
535	movl	$BI_ENDCOMMON,%ecx	/* prepare for sizeless version */
536	testl	$RB_BOOTINFO,8(%ebp)	/* bi_size (and bootinfo) valid? */
537	je	got_bi_size		/* no, sizeless version */
538	movl	BI_SIZE(%ebx),%ecx
539got_bi_size:
540
541	/*
542	 * Copy the common part of the bootinfo struct
543	 */
544	movl	%ebx,%esi
545	movl	$R(bootinfo),%edi
546	cmpl	$BOOTINFO_SIZE,%ecx
547	jbe	got_common_bi_size
548	movl	$BOOTINFO_SIZE,%ecx
549got_common_bi_size:
550	cld
551	rep
552	movsb
553
554#ifdef NFS_ROOT
555#ifndef BOOTP_NFSV3
556	/*
557	 * If we have a nfs_diskless structure copy it in
558	 */
559	movl	BI_NFS_DISKLESS(%ebx),%esi
560	cmpl	$0,%esi
561	je	olddiskboot
562	movl	$R(nfs_diskless),%edi
563	movl	$NFSDISKLESS_SIZE,%ecx
564	cld
565	rep
566	movsb
567	movl	$R(nfs_diskless_valid),%edi
568	movl	$1,(%edi)
569#endif
570#endif
571
572	/*
573	 * The old style disk boot.
574	 *	(*btext)(howto, bootdev, cyloffset, esym);
575	 * Note that the newer boot code just falls into here to pick
576	 * up howto and bootdev, cyloffset and esym are no longer used
577	 */
578olddiskboot:
579	movl	8(%ebp),%eax
580	movl	%eax,R(boothowto)
581	movl	12(%ebp),%eax
582	movl	%eax,R(bootdev)
583
584	ret
585
586
587/**********************************************************************
588 *
589 * Identify the CPU and initialize anything special about it
590 *
591 */
592identify_cpu:
593
594	/* Try to toggle alignment check flag; does not exist on 386. */
595	pushfl
596	popl	%eax
597	movl	%eax,%ecx
598	orl	$PSL_AC,%eax
599	pushl	%eax
600	popfl
601	pushfl
602	popl	%eax
603	xorl	%ecx,%eax
604	andl	$PSL_AC,%eax
605	pushl	%ecx
606	popfl
607
608	testl	%eax,%eax
609	jnz	try486
610
611	/* NexGen CPU does not have aligment check flag. */
612	pushfl
613	movl	$0x5555, %eax
614	xorl	%edx, %edx
615	movl	$2, %ecx
616	clc
617	divl	%ecx
618	jz	trynexgen
619	popfl
620	movl	$CPU_386,R(cpu)
621	jmp	3f
622
623trynexgen:
624	popfl
625	movl	$CPU_NX586,R(cpu)
626	movl	$0x4778654e,R(cpu_vendor)	# store vendor string
627	movl	$0x72446e65,R(cpu_vendor+4)
628	movl	$0x6e657669,R(cpu_vendor+8)
629	movl	$0,R(cpu_vendor+12)
630	jmp	3f
631
632try486:	/* Try to toggle identification flag; does not exist on early 486s. */
633	pushfl
634	popl	%eax
635	movl	%eax,%ecx
636	xorl	$PSL_ID,%eax
637	pushl	%eax
638	popfl
639	pushfl
640	popl	%eax
641	xorl	%ecx,%eax
642	andl	$PSL_ID,%eax
643	pushl	%ecx
644	popfl
645
646	testl	%eax,%eax
647	jnz	trycpuid
648	movl	$CPU_486,R(cpu)
649
650	/*
651	 * Check Cyrix CPU
652	 * Cyrix CPUs do not change the undefined flags following
653	 * execution of the divide instruction which divides 5 by 2.
654	 *
655	 * Note: CPUID is enabled on M2, so it passes another way.
656	 */
657	pushfl
658	movl	$0x5555, %eax
659	xorl	%edx, %edx
660	movl	$2, %ecx
661	clc
662	divl	%ecx
663	jnc	trycyrix
664	popfl
665	jmp	3f		/* You may use Intel CPU. */
666
667trycyrix:
668	popfl
669	/*
670	 * IBM Bluelighting CPU also doesn't change the undefined flags.
671	 * Because IBM doesn't disclose the information for Bluelighting
672	 * CPU, we couldn't distinguish it from Cyrix's (including IBM
673	 * brand of Cyrix CPUs).
674	 */
675	movl	$0x69727943,R(cpu_vendor)	# store vendor string
676	movl	$0x736e4978,R(cpu_vendor+4)
677	movl	$0x64616574,R(cpu_vendor+8)
678	jmp	3f
679
680trycpuid:	/* Use the `cpuid' instruction. */
681	xorl	%eax,%eax
682	cpuid					# cpuid 0
683	movl	%eax,R(cpu_high)		# highest capability
684	movl	%ebx,R(cpu_vendor)		# store vendor string
685	movl	%edx,R(cpu_vendor+4)
686	movl	%ecx,R(cpu_vendor+8)
687	movb	$0,R(cpu_vendor+12)
688
689	movl	$1,%eax
690	cpuid					# cpuid 1
691	movl	%eax,R(cpu_id)			# store cpu_id
692	movl	%ebx,R(cpu_procinfo)		# store cpu_procinfo
693	movl	%edx,R(cpu_feature)		# store cpu_feature
694	rorl	$8,%eax				# extract family type
695	andl	$15,%eax
696	cmpl	$5,%eax
697	jae	1f
698
699	/* less than Pentium; must be 486 */
700	movl	$CPU_486,R(cpu)
701	jmp	3f
7021:
703	/* a Pentium? */
704	cmpl	$5,%eax
705	jne	2f
706	movl	$CPU_586,R(cpu)
707	jmp	3f
7082:
709	/* Greater than Pentium...call it a Pentium Pro */
710	movl	$CPU_686,R(cpu)
7113:
712	ret
713
714
715/**********************************************************************
716 *
717 * Create the first page directory and its page tables.
718 *
719 */
720
721create_pagetables:
722
723/* Find end of kernel image (rounded up to a page boundary). */
724	movl	$R(_end),%esi
725
726/* Include symbols, if any. */
727	movl	R(bootinfo+BI_ESYMTAB),%edi
728	testl	%edi,%edi
729	je	over_symalloc
730	movl	%edi,%esi
731	movl	$KERNBASE,%edi
732	addl	%edi,R(bootinfo+BI_SYMTAB)
733	addl	%edi,R(bootinfo+BI_ESYMTAB)
734over_symalloc:
735
736/* If we are told where the end of the kernel space is, believe it. */
737	movl	R(bootinfo+BI_KERNEND),%edi
738	testl	%edi,%edi
739	je	no_kernend
740	movl	%edi,%esi
741no_kernend:
742
743	addl	$PAGE_MASK,%esi
744	andl	$~PAGE_MASK,%esi
745	movl	%esi,R(KERNend)		/* save end of kernel */
746	movl	%esi,R(physfree)	/* next free page is at end of kernel */
747
748/* Allocate Kernel Page Tables */
749	ALLOCPAGES(NKPT)
750	movl	%esi,R(KPTphys)
751
752/* Allocate Page Table Directory */
753#ifdef PAE
754	/* XXX only need 32 bytes (easier for now) */
755	ALLOCPAGES(1)
756	movl	%esi,R(IdlePDPT)
757#endif
758	ALLOCPAGES(NPGPTD)
759	movl	%esi,R(IdlePTD)
760
761/* Allocate UPAGES */
762	ALLOCPAGES(UAREA_PAGES)
763	movl	%esi,R(p0upa)
764	addl	$KERNBASE, %esi
765	movl	%esi, R(proc0uarea)
766
767	ALLOCPAGES(KSTACK_PAGES)
768	movl	%esi,R(p0kpa)
769	addl	$KERNBASE, %esi
770	movl	%esi, R(proc0kstack)
771
772	ALLOCPAGES(1)			/* vm86/bios stack */
773	movl	%esi,R(vm86phystk)
774
775	ALLOCPAGES(3)			/* pgtable + ext + IOPAGES */
776	movl	%esi,R(vm86pa)
777	addl	$KERNBASE, %esi
778	movl	%esi, R(vm86paddr)
779
780#ifdef SMP
781/* Allocate cpu0's private data page */
782	ALLOCPAGES(1)
783	movl	%esi,R(cpu0pp)
784	addl	$KERNBASE, %esi
785	movl	%esi, R(cpu0prvpage)	/* relocated to KVM space */
786
787/* Allocate SMP page table page */
788	ALLOCPAGES(1)
789	movl	%esi,R(SMPptpa)
790	addl	$KERNBASE, %esi
791	movl	%esi, R(SMPpt)		/* relocated to KVM space */
792#endif	/* SMP */
793
794/* Map read-only from zero to the end of the kernel text section */
795	xorl	%eax, %eax
796	xorl	%edx,%edx
797	movl	$R(etext),%ecx
798	addl	$PAGE_MASK,%ecx
799	shrl	$PAGE_SHIFT,%ecx
800	fillkptphys(%edx)
801
802/* Map read-write, data, bss and symbols */
803	movl	$R(etext),%eax
804	addl	$PAGE_MASK, %eax
805	andl	$~PAGE_MASK, %eax
806	movl	$PG_RW,%edx
807	movl	R(KERNend),%ecx
808	subl	%eax,%ecx
809	shrl	$PAGE_SHIFT,%ecx
810	fillkptphys(%edx)
811
812/* Map page directory. */
813#ifdef PAE
814	movl	R(IdlePDPT), %eax
815	movl	$1, %ecx
816	fillkptphys($PG_RW)
817#endif
818
819	movl	R(IdlePTD), %eax
820	movl	$NPGPTD, %ecx
821	fillkptphys($PG_RW)
822
823/* Map proc0's UPAGES in the physical way ... */
824	movl	R(p0upa), %eax
825	movl	$(UAREA_PAGES), %ecx
826	fillkptphys($PG_RW)
827
828/* Map proc0's KSTACK in the physical way ... */
829	movl	R(p0kpa), %eax
830	movl	$(KSTACK_PAGES), %ecx
831	fillkptphys($PG_RW)
832
833/* Map ISA hole */
834	movl	$ISA_HOLE_START, %eax
835	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
836	fillkptphys($PG_RW)
837
838/* Map space for the vm86 region */
839	movl	R(vm86phystk), %eax
840	movl	$4, %ecx
841	fillkptphys($PG_RW)
842
843/* Map page 0 into the vm86 page table */
844	movl	$0, %eax
845	movl	$0, %ebx
846	movl	$1, %ecx
847	fillkpt(R(vm86pa), $PG_RW|PG_U)
848
849/* ...likewise for the ISA hole */
850	movl	$ISA_HOLE_START, %eax
851	movl	$ISA_HOLE_START>>PAGE_SHIFT, %ebx
852	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
853	fillkpt(R(vm86pa), $PG_RW|PG_U)
854
855#ifdef SMP
856/* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */
857	movl	R(cpu0pp), %eax
858	movl	$1, %ecx
859	fillkptphys($PG_RW)
860
861/* Map SMP page table page into global kmem FWIW */
862	movl	R(SMPptpa), %eax
863	movl	$1, %ecx
864	fillkptphys($PG_RW)
865
866/* Map the private page into the SMP page table */
867	movl	R(cpu0pp), %eax
868	movl	$0, %ebx		/* pte offset = 0 */
869	movl	$1, %ecx		/* one private page coming right up */
870	fillkpt(R(SMPptpa), $PG_RW)
871
872/* ... and put the page table table in the pde. */
873	movl	R(SMPptpa), %eax
874	movl	$MPPTDI, %ebx
875	movl	$1, %ecx
876	fillkpt(R(IdlePTD), $PG_RW)
877
878/* Fakeup VA for the local apic to allow early traps. */
879	ALLOCPAGES(1)
880	movl	%esi, %eax
881	movl	$(NPTEPG-1), %ebx	/* pte offset = NTEPG-1 */
882	movl	$1, %ecx		/* one private pt coming right up */
883	fillkpt(R(SMPptpa), $PG_RW)
884#endif	/* SMP */
885
886/* install a pde for temporary double map of bottom of VA */
887	movl	R(KPTphys), %eax
888	xorl	%ebx, %ebx
889	movl	$NKPT, %ecx
890	fillkpt(R(IdlePTD), $PG_RW)
891
892/* install pde's for pt's */
893	movl	R(KPTphys), %eax
894	movl	$KPTDI, %ebx
895	movl	$NKPT, %ecx
896	fillkpt(R(IdlePTD), $PG_RW)
897
898/* install a pde recursively mapping page directory as a page table */
899	movl	R(IdlePTD), %eax
900	movl	$PTDPTDI, %ebx
901	movl	$NPGPTD,%ecx
902	fillkpt(R(IdlePTD), $PG_RW)
903
904#ifdef PAE
905	movl	R(IdlePTD), %eax
906	xorl	%ebx, %ebx
907	movl	$NPGPTD, %ecx
908	fillkpt(R(IdlePDPT), $0x0)
909#endif
910
911	ret
912