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