locore.s revision 24112
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 *	$Id: locore.s,v 1.81 1997/02/22 09:32:22 peter Exp $
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 "apm.h"
47#include "opt_cpu.h"
48#include "opt_ddb.h"
49#include "opt_userconfig.h"
50
51#include <sys/errno.h>
52#include <sys/syscall.h>
53#include <sys/reboot.h>
54
55#include <machine/asmacros.h>
56#include <machine/cputypes.h>
57#include <machine/psl.h>
58#include <machine/pmap.h>
59#include <machine/specialreg.h>
60
61#include "assym.s"
62
63/*
64 *	XXX
65 *
66 * Note: This version greatly munged to avoid various assembler errors
67 * that may be fixed in newer versions of gas. Perhaps newer versions
68 * will have more pleasant appearance.
69 */
70
71/*
72 * PTmap is recursive pagemap at top of virtual address space.
73 * Within PTmap, the page directory can be found (third indirection).
74 */
75	.globl	_PTmap,_PTD,_PTDpde
76	.set	_PTmap,(PTDPTDI << PDRSHIFT)
77	.set	_PTD,_PTmap + (PTDPTDI * PAGE_SIZE)
78	.set	_PTDpde,_PTD + (PTDPTDI * PDESIZE)
79
80/*
81 * APTmap, APTD is the alternate recursive pagemap.
82 * It's used when modifying another process's page tables.
83 */
84	.globl	_APTmap,_APTD,_APTDpde
85	.set	_APTmap,APTDPTDI << PDRSHIFT
86	.set	_APTD,_APTmap + (APTDPTDI * PAGE_SIZE)
87	.set	_APTDpde,_PTD + (APTDPTDI * PDESIZE)
88
89/*
90 * Access to each processes kernel stack is via a region of
91 * per-process address space (at the beginning), immediately above
92 * the user process stack.
93 */
94	.set	_kstack,USRSTACK
95	.globl	_kstack
96
97/*
98 * Globals
99 */
100	.data
101	ALIGN_DATA		/* just to be sure */
102
103	.globl	tmpstk
104	.space	0x2000		/* space for tmpstk - temporary stack */
105tmpstk:
106
107	.globl	_boothowto,_bootdev
108
109	.globl	_cpu,_cpu_vendor,_cpu_id,_bootinfo
110	.globl	_cpu_high, _cpu_feature
111
112_cpu:	.long	0				/* are we 386, 386sx, or 486 */
113_cpu_id:	.long	0			/* stepping ID */
114_cpu_high:	.long	0			/* highest arg to CPUID */
115_cpu_feature:	.long	0			/* features */
116_cpu_vendor:	.space	20			/* CPU origin code */
117_bootinfo:	.space	BOOTINFO_SIZE		/* bootinfo that we can handle */
118
119_KERNend:	.long	0			/* phys addr end of kernel (just after bss) */
120physfree:	.long	0			/* phys addr of next free page */
121p0upa:	.long	0				/* phys addr of proc0's UPAGES */
122p0upt:	.long	0				/* phys addr of proc0's UPAGES page table */
123
124	.globl	_IdlePTD
125_IdlePTD:	.long	0			/* phys addr of kernel PTD */
126
127_KPTphys:	.long	0			/* phys addr of kernel page tables */
128
129	.globl	_proc0paddr
130_proc0paddr:	.long	0			/* address of proc 0 address space */
131
132#ifdef BDE_DEBUGGER
133	.globl	_bdb_exists			/* flag to indicate BDE debugger is present */
134_bdb_exists:	.long	0
135#endif
136
137
138/**********************************************************************
139 *
140 * Some handy macros
141 *
142 */
143
144#define R(foo) ((foo)-KERNBASE)
145
146#define ALLOCPAGES(foo) \
147	movl	R(physfree), %esi ; \
148	movl	$((foo)*PAGE_SIZE), %eax ; \
149	addl	%esi, %eax ; \
150	movl	%eax, R(physfree) ; \
151	movl	%esi, %edi ; \
152	movl	$((foo)*PAGE_SIZE),%ecx ; \
153	xorl	%eax,%eax ; \
154	cld ; \
155	rep ; \
156	stosb
157
158/*
159 * fillkpt
160 *	eax = page frame address
161 *	ebx = index into page table
162 *	ecx = how many pages to map
163 * 	base = base address of page dir/table
164 *	prot = protection bits
165 */
166#define	fillkpt(base, prot)		  \
167	shll	$2,%ebx			; \
168	addl	base,%ebx		; \
169	orl	$PG_V,%eax		; \
170	orl	prot,%eax		; \
1711:	movl	%eax,(%ebx)		; \
172	addl	$PAGE_SIZE,%eax		; /* increment physical address */ \
173	addl	$4,%ebx			; /* next pte */ \
174	loop	1b
175
176/*
177 * fillkptphys(prot)
178 *	eax = physical address
179 *	ecx = how many pages to map
180 *	prot = protection bits
181 */
182#define	fillkptphys(prot)		  \
183	movl	%eax, %ebx		; \
184	shrl	$PAGE_SHIFT, %ebx	; \
185	fillkpt(R(_KPTphys), prot)
186
187	.text
188/**********************************************************************
189 *
190 * This is where the bootblocks start us, set the ball rolling...
191 *
192 */
193NON_GPROF_ENTRY(btext)
194
195#ifdef PC98
196	jmp	1f
197	.globl	_pc98_system_parameter
198	.org	0x400
199_pc98_system_parameter:
200	.space	0x240		/* BIOS parameter block */
2011:
202	/* save SYSTEM PARAMETER for resume (NS/T or other) */
203	movl	$0xa1000,%esi
204	movl	$0x100000,%edi
205	movl	$0x0630,%ecx
206	cld
207	rep
208	movsb
209#else	/* IBM-PC */
210#ifdef BDE_DEBUGGER
211#ifdef BIOS_STEALS_3K
212	cmpl	$0x0375c339,0x95504
213#else
214	cmpl	$0x0375c339,0x96104	/* XXX - debugger signature */
215#endif
216	jne	1f
217	movb	$1,R(_bdb_exists)
2181:
219#endif
220
221/* Tell the bios to warmboot next time */
222	movw	$0x1234,0x472
223#endif	/* PC98 */
224
225/* Set up a real frame in case the double return in newboot is executed. */
226	pushl	%ebp
227	movl	%esp, %ebp
228
229/* Don't trust what the BIOS gives for eflags. */
230	pushl	$PSL_KERNEL
231	popfl
232
233/*
234 * Don't trust what the BIOS gives for %fs and %gs.  Trust the bootstrap
235 * to set %cs, %ds, %es and %ss.
236 */
237	mov	%ds, %ax
238	mov	%ax, %fs
239	mov	%ax, %gs
240
241	call	recover_bootinfo
242
243/* Get onto a stack that we can trust. */
244/*
245 * XXX this step is delayed in case recover_bootinfo needs to return via
246 * the old stack, but it need not be, since recover_bootinfo actually
247 * returns via the old frame.
248 */
249	movl	$R(tmpstk),%esp
250
251#ifdef PC98
252	testb	$0x02,0x100620		/* pc98_machine_type & M_EPSON_PC98 */
253	jz	3f
254	cmpb	$0x0b,0x100624		/* epson_machine_id <= 0x0b */
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,0x100401
2703:
271#endif
272
273	call	identify_cpu
274
275/* clear bss */
276/*
277 * XXX this should be done a little earlier.
278 *
279 * XXX we don't check that there is memory for our bss and page tables
280 * before using it.
281 *
282 * XXX the boot program somewhat bogusly clears the bss.  We still have
283 * to do it in case we were unzipped by kzipboot.  Then the boot program
284 * only clears kzipboot's bss.
285 *
286 * XXX the gdt and idt are still somewhere in the boot program.  We
287 * depend on the convention that the boot program is below 1MB and we
288 * are above 1MB to keep the gdt and idt  away from the bss and page
289 * tables.  The idt is only used if BDE_DEBUGGER is enabled.
290 */
291	movl	$R(_end),%ecx
292	movl	$R(_edata),%edi
293	subl	%edi,%ecx
294	xorl	%eax,%eax
295	cld
296	rep
297	stosb
298
299#if NAPM > 0
300/*
301 * XXX it's not clear that APM can live in the current environonment.
302 * Only pc-relative addressing works.
303 */
304	call	_apm_setup
305#endif
306
307	call	create_pagetables
308
309#ifdef BDE_DEBUGGER
310/*
311 * Adjust as much as possible for paging before enabling paging so that the
312 * adjustments can be traced.
313 */
314	call	bdb_prepare_paging
315#endif
316
317/* Now enable paging */
318	movl	R(_IdlePTD), %eax
319	movl	%eax,%cr3			/* load ptd addr into mmu */
320	movl	%cr0,%eax			/* get control word */
321	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
322	movl	%eax,%cr0			/* and let's page NOW! */
323
324#ifdef BDE_DEBUGGER
325/*
326 * Complete the adjustments for paging so that we can keep tracing through
327 * initi386() after the low (physical) addresses for the gdt and idt become
328 * invalid.
329 */
330	call	bdb_commit_paging
331#endif
332
333	pushl	$begin				/* jump to high virtualized address */
334	ret
335
336/* now running relocated at KERNBASE where the system is linked to run */
337begin:
338	/* set up bootstrap stack */
339	movl	$_kstack+UPAGES*PAGE_SIZE,%esp	/* bootstrap stack end location */
340	xorl	%eax,%eax			/* mark end of frames */
341	movl	%eax,%ebp
342	movl	_proc0paddr,%eax
343	movl	_IdlePTD, %esi
344	movl	%esi,PCB_CR3(%eax)
345
346	movl	physfree, %esi
347	pushl	%esi				/* value of first for init386(first) */
348	call	_init386			/* wire 386 chip for unix operation */
349	popl	%esi
350
351	.globl	__ucodesel,__udatasel
352
353	pushl	$0				/* unused */
354	pushl	__udatasel			/* ss */
355	pushl	$0				/* esp - filled in by execve() */
356	pushl	$PSL_USER			/* eflags (IOPL 0, int enab) */
357	pushl	__ucodesel			/* cs */
358	pushl	$0				/* eip - filled in by execve() */
359	subl	$(12*4),%esp			/* space for rest of registers */
360
361	pushl	%esp				/* call main with frame pointer */
362	call	_main				/* autoconfiguration, mountroot etc */
363
364	addl	$(13*4),%esp			/* back to a frame we can return with */
365
366	/*
367	 * Now we've run main() and determined what cpu-type we are, we can
368	 * enable write protection and alignment checking on i486 cpus and
369	 * above.
370	 */
371#if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
372	cmpl    $CPUCLASS_386,_cpu_class
373	je	1f
374	movl	%cr0,%eax			/* get control word */
375	orl	$CR0_WP|CR0_AM,%eax		/* enable i486 features */
376	movl	%eax,%cr0			/* and do it */
3771:
378#endif
379	/*
380	 * on return from main(), we are process 1
381	 * set up address space and stack so that we can 'return' to user mode
382	 */
383	movl	__ucodesel,%eax
384	movl	__udatasel,%ecx
385
386	movl	%cx,%ds
387	movl	%cx,%es
388	movl	%ax,%fs				/* double map cs to fs */
389	movl	%cx,%gs				/* and ds to gs */
390	iret					/* goto user! */
391
392#define LCALL(x,y)	.byte 0x9a ; .long y ; .word x
393
394/*
395 * Signal trampoline, copied to top of user stack
396 */
397NON_GPROF_ENTRY(sigcode)
398	call	SIGF_HANDLER(%esp)
399	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
400						/* copy at 8(%esp)) */
401	pushl	%eax
402	pushl	%eax				/* junk to fake return address */
403	movl	$SYS_sigreturn,%eax		/* sigreturn() */
404	LCALL(0x7,0)				/* enter kernel with args on stack */
405	hlt					/* never gets here */
406	.align	2,0x90				/* long word text-align */
407_esigcode:
408
409	.data
410	.globl	_szsigcode
411_szsigcode:
412	.long	_esigcode-_sigcode
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
537	/*
538	 * If we have a nfs_diskless structure copy it in
539	 */
540	movl	BI_NFS_DISKLESS(%ebx),%esi
541	cmpl	$0,%esi
542	je	olddiskboot
543	movl	$R(_nfs_diskless),%edi
544	movl	$NFSDISKLESS_SIZE,%ecx
545	cld
546	rep
547	movsb
548	movl	$R(_nfs_diskless_valid),%edi
549	movl	$1,(%edi)
550#endif
551
552	/*
553	 * The old style disk boot.
554	 *	(*btext)(howto, bootdev, cyloffset, esym);
555	 * Note that the newer boot code just falls into here to pick
556	 * up howto and bootdev, cyloffset and esym are no longer used
557	 */
558olddiskboot:
559	movl	8(%ebp),%eax
560	movl	%eax,R(_boothowto)
561	movl	12(%ebp),%eax
562	movl	%eax,R(_bootdev)
563
564#if defined(USERCONFIG_BOOT) && defined(USERCONFIG)
565#ifdef PC98
566	movl	$0x90200, %esi
567#else
568	movl	$0x10200, %esi
569#endif
570	movl	$R(_userconfig_from_boot),%edi
571	movl	$512,%ecx
572	cld
573	rep
574	movsb
575#endif /* USERCONFIG_BOOT */
576
577	ret
578
579
580/**********************************************************************
581 *
582 * Identify the CPU and initialize anything special about it
583 *
584 */
585identify_cpu:
586
587	/* Try to toggle alignment check flag; does not exist on 386. */
588	pushfl
589	popl	%eax
590	movl	%eax,%ecx
591	orl	$PSL_AC,%eax
592	pushl	%eax
593	popfl
594	pushfl
595	popl	%eax
596	xorl	%ecx,%eax
597	andl	$PSL_AC,%eax
598	pushl	%ecx
599	popfl
600
601	testl	%eax,%eax
602	jnz	try486
603
604	/* NexGen CPU does not have aligment check flag. */
605	pushfl
606	movl	$0x5555, %eax
607	xorl	%edx, %edx
608	movl	$2, %ecx
609	clc
610	divl	%ecx
611	jz	trynexgen
612	popfl
613	movl	$CPU_386,R(_cpu)
614	jmp	3f
615
616trynexgen:
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	.byte	0x0f,0xa2			# 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	.byte	0x0f,0xa2			# cpuid 1
683	movl	%eax,R(_cpu_id)			# store cpu_id
684	movl	%edx,R(_cpu_feature)		# store cpu_feature
685	rorl	$8,%eax				# extract family type
686	andl	$15,%eax
687	cmpl	$5,%eax
688	jae	1f
689
690	/* less than Pentium; must be 486 */
691	movl	$CPU_486,R(_cpu)
692	jmp	3f
6931:
694	/* a Pentium? */
695	cmpl	$5,%eax
696	jne	2f
697	movl	$CPU_586,R(_cpu)
698	jmp	3f
6992:
700	/* Greater than Pentium...call it a Pentium Pro */
701	movl	$CPU_686,R(_cpu)
7023:
703	ret
704
705
706/**********************************************************************
707 *
708 * Create the first page directory and its page tables.
709 *
710 */
711
712create_pagetables:
713
714	testl	$CPUID_PGE, R(_cpu_feature)
715	jz	1f
716	movl	%cr4, %eax
717	orl	$CR4_PGE, %eax
718	movl	%eax, %cr4
7191:
720
721/* Find end of kernel image (rounded up to a page boundary). */
722	movl	$R(_end),%esi
723
724/* include symbols in "kernel image" if they are loaded and useful */
725#ifdef DDB
726	movl	R(_bootinfo+BI_ESYMTAB),%edi
727	testl	%edi,%edi
728	je	over_symalloc
729	movl	%edi,%esi
730	movl	$KERNBASE,%edi
731	addl	%edi,R(_bootinfo+BI_SYMTAB)
732	addl	%edi,R(_bootinfo+BI_ESYMTAB)
733over_symalloc:
734#endif
735
736	addl	$PAGE_MASK,%esi
737	andl	$~PAGE_MASK,%esi
738	movl	%esi,R(_KERNend)	/* save end of kernel */
739	movl	%esi,R(physfree)	/* next free page is at end of kernel */
740
741/* Allocate Kernel Page Tables */
742	ALLOCPAGES(NKPT)
743	movl	%esi,R(_KPTphys)
744
745/* Allocate Page Table Directory */
746	ALLOCPAGES(1)
747	movl	%esi,R(_IdlePTD)
748
749/* Allocate UPAGES */
750	ALLOCPAGES(UPAGES)
751	movl	%esi,R(p0upa)
752	addl	$KERNBASE, %esi
753	movl	%esi, R(_proc0paddr)
754
755/* Allocate proc0's page table for the UPAGES. */
756	ALLOCPAGES(1)
757	movl	%esi,R(p0upt)
758
759/* Map read-only from zero to the end of the kernel text section */
760	xorl	%eax, %eax
761#ifdef BDE_DEBUGGER
762/* If the debugger is present, actually map everything read-write. */
763	cmpl	$0,R(_bdb_exists)
764	jne	map_read_write
765#endif
766	xorl	%edx,%edx
767	testl	$CPUID_PGE, R(_cpu_feature)
768	jz	2f
769	orl	$PG_G,%edx
770
7712:	movl	$R(_etext),%ecx
772	addl	$PAGE_MASK,%ecx
773	shrl	$PAGE_SHIFT,%ecx
774	fillkptphys(%edx)
775
776/* Map read-write, data, bss and symbols */
777	movl	$R(_etext),%eax
778	addl	$PAGE_MASK, %eax
779	andl	$~PAGE_MASK, %eax
780map_read_write:
781	movl	$PG_RW,%edx
782	testl	$CPUID_PGE, R(_cpu_feature)
783	jz	1f
784	orl	$PG_G,%edx
785
7861:	movl	R(_KERNend),%ecx
787	subl	%eax,%ecx
788	shrl	$PAGE_SHIFT,%ecx
789	fillkptphys(%edx)
790
791/* Map page directory. */
792	movl	R(_IdlePTD), %eax
793	movl	$1, %ecx
794	fillkptphys($PG_RW)
795
796/* Map proc0's page table for the UPAGES. */
797	movl	R(p0upt), %eax
798	movl	$1, %ecx
799	fillkptphys($PG_RW)
800
801/* Map proc0's UPAGES in the physical way ... */
802	movl	R(p0upa), %eax
803	movl	$UPAGES, %ecx
804	fillkptphys($PG_RW)
805
806/* Map ISA hole */
807	movl	$ISA_HOLE_START, %eax
808	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
809	fillkptphys($PG_RW)
810
811/* Map proc0s UPAGES in the special page table for this purpose ... */
812	movl	R(p0upa), %eax
813	movl	$KSTKPTEOFF, %ebx
814	movl	$UPAGES, %ecx
815	fillkpt(R(p0upt), $PG_RW)
816
817/* ... and put the page table in the pde. */
818	movl	R(p0upt), %eax
819	movl	$KSTKPTDI, %ebx
820	movl	$1, %ecx
821	fillkpt(R(_IdlePTD), $PG_RW)
822
823/* install a pde for temporary double map of bottom of VA */
824	movl	R(_KPTphys), %eax
825	xorl	%ebx, %ebx
826	movl	$1, %ecx
827	fillkpt(R(_IdlePTD), $PG_RW)
828
829/* install pde's for pt's */
830	movl	R(_KPTphys), %eax
831	movl	$KPTDI, %ebx
832	movl	$NKPT, %ecx
833	fillkpt(R(_IdlePTD), $PG_RW)
834
835/* install a pde recursively mapping page directory as a page table */
836	movl	R(_IdlePTD), %eax
837	movl	$PTDPTDI, %ebx
838	movl	$1,%ecx
839	fillkpt(R(_IdlePTD), $PG_RW)
840
841	ret
842
843#ifdef BDE_DEBUGGER
844bdb_prepare_paging:
845	cmpl	$0,R(_bdb_exists)
846	je	bdb_prepare_paging_exit
847
848	subl	$6,%esp
849
850	/*
851	 * Copy and convert debugger entries from the bootstrap gdt and idt
852	 * to the kernel gdt and idt.  Everything is still in low memory.
853	 * Tracing continues to work after paging is enabled because the
854	 * low memory addresses remain valid until everything is relocated.
855	 * However, tracing through the setidt() that initializes the trace
856	 * trap will crash.
857	 */
858	sgdt	(%esp)
859	movl	2(%esp),%esi		/* base address of bootstrap gdt */
860	movl	$R(_gdt),%edi
861	movl	%edi,2(%esp)		/* prepare to load kernel gdt */
862	movl	$8*18/4,%ecx
863	cld
864	rep				/* copy gdt */
865	movsl
866	movl	$R(_gdt),-8+2(%edi)	/* adjust gdt self-ptr */
867	movb	$0x92,-8+5(%edi)
868	lgdt	(%esp)
869
870	sidt	(%esp)
871	movl	2(%esp),%esi		/* base address of current idt */
872	movl	8+4(%esi),%eax		/* convert dbg descriptor to ... */
873	movw	8(%esi),%ax
874	movl	%eax,R(bdb_dbg_ljmp+1)	/* ... immediate offset ... */
875	movl	8+2(%esi),%eax
876	movw	%ax,R(bdb_dbg_ljmp+5)	/* ... and selector for ljmp */
877	movl	24+4(%esi),%eax		/* same for bpt descriptor */
878	movw	24(%esi),%ax
879	movl	%eax,R(bdb_bpt_ljmp+1)
880	movl	24+2(%esi),%eax
881	movw	%ax,R(bdb_bpt_ljmp+5)
882	movl	$R(_idt),%edi
883	movl	%edi,2(%esp)		/* prepare to load kernel idt */
884	movl	$8*4/4,%ecx
885	cld
886	rep				/* copy idt */
887	movsl
888	lidt	(%esp)
889
890	addl	$6,%esp
891
892bdb_prepare_paging_exit:
893	ret
894
895/* Relocate debugger gdt entries and gdt and idt pointers. */
896bdb_commit_paging:
897	cmpl	$0,_bdb_exists
898	je	bdb_commit_paging_exit
899
900	movl	$_gdt+8*9,%eax		/* adjust slots 9-17 */
901	movl	$9,%ecx
902reloc_gdt:
903	movb	$KERNBASE>>24,7(%eax)	/* top byte of base addresses, was 0, */
904	addl	$8,%eax			/* now KERNBASE>>24 */
905	loop	reloc_gdt
906
907	subl	$6,%esp
908	sgdt	(%esp)
909	addl	$KERNBASE,2(%esp)
910	lgdt	(%esp)
911	sidt	(%esp)
912	addl	$KERNBASE,2(%esp)
913	lidt	(%esp)
914	addl	$6,%esp
915
916	int	$3
917
918bdb_commit_paging_exit:
919	ret
920
921#endif /* BDE_DEBUGGER */
922