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