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