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