locore.s revision 22130
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 22130 1997-01-30 20:22:02Z dg $
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 BDE_DEBUGGER
196#ifdef BIOS_STEALS_3K
197	cmpl	$0x0375c339,0x95504
198#else
199	cmpl	$0x0375c339,0x96104	/* XXX - debugger signature */
200#endif
201	jne	1f
202	movb	$1,R(_bdb_exists)
2031:
204#endif
205
206/* Tell the bios to warmboot next time */
207	movw	$0x1234,0x472
208
209/* Set up a real frame in case the double return in newboot is executed. */
210	pushl	%ebp
211	movl	%esp, %ebp
212
213/* Don't trust what the BIOS gives for eflags. */
214	pushl	$PSL_KERNEL
215	popfl
216
217/*
218 * Don't trust what the BIOS gives for %fs and %gs.  Trust the bootstrap
219 * to set %cs, %ds, %es and %ss.
220 */
221	mov	%ds, %ax
222	mov	%ax, %fs
223	mov	%ax, %gs
224
225	call	recover_bootinfo
226
227/* Get onto a stack that we can trust. */
228/*
229 * XXX this step is delayed in case recover_bootinfo needs to return via
230 * the old stack, but it need not be, since recover_bootinfo actually
231 * returns via the old frame.
232 */
233	movl	$R(tmpstk),%esp
234
235	call	identify_cpu
236
237/* clear bss */
238/*
239 * XXX this should be done a little earlier.
240 *
241 * XXX we don't check that there is memory for our bss and page tables
242 * before using it.
243 *
244 * XXX the boot program somewhat bogusly clears the bss.  We still have
245 * to do it in case we were unzipped by kzipboot.  Then the boot program
246 * only clears kzipboot's bss.
247 *
248 * XXX the gdt and idt are still somewhere in the boot program.  We
249 * depend on the convention that the boot program is below 1MB and we
250 * are above 1MB to keep the gdt and idt  away from the bss and page
251 * tables.  The idt is only used if BDE_DEBUGGER is enabled.
252 */
253	movl	$R(_end),%ecx
254	movl	$R(_edata),%edi
255	subl	%edi,%ecx
256	xorl	%eax,%eax
257	cld
258	rep
259	stosb
260
261#if NAPM > 0
262/*
263 * XXX it's not clear that APM can live in the current environonment.
264 * Only pc-relative addressing works.
265 */
266	call	_apm_setup
267#endif
268
269	call	create_pagetables
270
271#ifdef BDE_DEBUGGER
272/*
273 * Adjust as much as possible for paging before enabling paging so that the
274 * adjustments can be traced.
275 */
276	call	bdb_prepare_paging
277#endif
278
279/* Now enable paging */
280	movl	R(_IdlePTD), %eax
281	movl	%eax,%cr3			/* load ptd addr into mmu */
282	movl	%cr0,%eax			/* get control word */
283	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
284	movl	%eax,%cr0			/* and let's page NOW! */
285
286#ifdef BDE_DEBUGGER
287/*
288 * Complete the adjustments for paging so that we can keep tracing through
289 * initi386() after the low (physical) addresses for the gdt and idt become
290 * invalid.
291 */
292	call	bdb_commit_paging
293#endif
294
295	pushl	$begin				/* jump to high virtualized address */
296	ret
297
298/* now running relocated at KERNBASE where the system is linked to run */
299begin:
300	/* set up bootstrap stack */
301	movl	$_kstack+UPAGES*PAGE_SIZE,%esp	/* bootstrap stack end location */
302	xorl	%eax,%eax			/* mark end of frames */
303	movl	%eax,%ebp
304	movl	_proc0paddr,%eax
305	movl	_IdlePTD, %esi
306	movl	%esi,PCB_CR3(%eax)
307
308	movl	physfree, %esi
309	pushl	%esi				/* value of first for init386(first) */
310	call	_init386			/* wire 386 chip for unix operation */
311	popl	%esi
312
313	.globl	__ucodesel,__udatasel
314
315	pushl	$0				/* unused */
316	pushl	__udatasel			/* ss */
317	pushl	$0				/* esp - filled in by execve() */
318	pushl	$PSL_USER			/* eflags (IOPL 0, int enab) */
319	pushl	__ucodesel			/* cs */
320	pushl	$0				/* eip - filled in by execve() */
321	subl	$(12*4),%esp			/* space for rest of registers */
322
323	pushl	%esp				/* call main with frame pointer */
324	call	_main				/* autoconfiguration, mountroot etc */
325
326	addl	$(13*4),%esp			/* back to a frame we can return with */
327
328	/*
329	 * now we've run main() and determined what cpu-type we are, we can
330	 * enable write protection and alignment checking on i486 cpus and
331	 * above.
332	 */
333#if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
334	cmpl    $CPUCLASS_386,_cpu_class
335	je	1f
336	movl	%cr0,%eax			/* get control word */
337	orl	$CR0_WP|CR0_AM,%eax		/* enable i486 features */
338	movl	%eax,%cr0			/* and do it */
3391:
340#endif
341	/*
342	 * on return from main(), we are process 1
343	 * set up address space and stack so that we can 'return' to user mode
344	 */
345	movl	__ucodesel,%eax
346	movl	__udatasel,%ecx
347
348	movl	%cx,%ds
349	movl	%cx,%es
350	movl	%ax,%fs				/* double map cs to fs */
351	movl	%cx,%gs				/* and ds to gs */
352	iret					/* goto user! */
353
354#define LCALL(x,y)	.byte 0x9a ; .long y ; .word x
355
356/*
357 * Signal trampoline, copied to top of user stack
358 */
359NON_GPROF_ENTRY(sigcode)
360	call	SIGF_HANDLER(%esp)
361	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
362						/* copy at 8(%esp)) */
363	pushl	%eax
364	pushl	%eax				/* junk to fake return address */
365	movl	$SYS_sigreturn,%eax		/* sigreturn() */
366	LCALL(0x7,0)				/* enter kernel with args on stack */
367	hlt					/* never gets here */
368	.align	2,0x90				/* long word text-align */
369_esigcode:
370
371	.data
372	.globl	_szsigcode
373_szsigcode:
374	.long	_esigcode-_sigcode
375	.text
376
377/**********************************************************************
378 *
379 * Recover the bootinfo passed to us from the boot program
380 *
381 */
382recover_bootinfo:
383	/*
384	 * This code is called in different ways depending on what loaded
385	 * and started the kernel.  This is used to detect how we get the
386	 * arguments from the other code and what we do with them.
387	 *
388	 * Old disk boot blocks:
389	 *	(*btext)(howto, bootdev, cyloffset, esym);
390	 *	[return address == 0, and can NOT be returned to]
391	 *	[cyloffset was not supported by the FreeBSD boot code
392	 *	 and always passed in as 0]
393	 *	[esym is also known as total in the boot code, and
394	 *	 was never properly supported by the FreeBSD boot code]
395	 *
396	 * Old diskless netboot code:
397	 *	(*btext)(0,0,0,0,&nfsdiskless,0,0,0);
398	 *	[return address != 0, and can NOT be returned to]
399	 *	If we are being booted by this code it will NOT work,
400	 *	so we are just going to halt if we find this case.
401	 *
402	 * New uniform boot code:
403	 *	(*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
404	 *	[return address != 0, and can be returned to]
405	 *
406	 * There may seem to be a lot of wasted arguments in here, but
407	 * that is so the newer boot code can still load very old kernels
408	 * and old boot code can load new kernels.
409	 */
410
411	/*
412	 * The old style disk boot blocks fake a frame on the stack and
413	 * did an lret to get here.  The frame on the stack has a return
414	 * address of 0.
415	 */
416	cmpl	$0,4(%ebp)
417	je	olddiskboot
418
419	/*
420	 * We have some form of return address, so this is either the
421	 * old diskless netboot code, or the new uniform code.  That can
422	 * be detected by looking at the 5th argument, if it is 0
423	 * we are being booted by the new uniform boot code.
424	 */
425	cmpl	$0,24(%ebp)
426	je	newboot
427
428	/*
429	 * Seems we have been loaded by the old diskless boot code, we
430	 * don't stand a chance of running as the diskless structure
431	 * changed considerably between the two, so just halt.
432	 */
433	 hlt
434
435	/*
436	 * We have been loaded by the new uniform boot code.
437	 * Let's check the bootinfo version, and if we do not understand
438	 * it we return to the loader with a status of 1 to indicate this error
439	 */
440newboot:
441	movl	28(%ebp),%ebx		/* &bootinfo.version */
442	movl	BI_VERSION(%ebx),%eax
443	cmpl	$1,%eax			/* We only understand version 1 */
444	je	1f
445	movl	$1,%eax			/* Return status */
446	leave
447	/*
448	 * XXX this returns to our caller's caller (as is required) since
449	 * we didn't set up a frame and our caller did.
450	 */
451	ret
452
4531:
454	/*
455	 * If we have a kernelname copy it in
456	 */
457	movl	BI_KERNELNAME(%ebx),%esi
458	cmpl	$0,%esi
459	je	2f			/* No kernelname */
460	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
461	movl	$R(_kernelname),%edi
462	cmpb	$'/',(%esi)		/* Make sure it starts with a slash */
463	je	1f
464	movb	$'/',(%edi)
465	incl	%edi
466	decl	%ecx
4671:
468	cld
469	rep
470	movsb
471
4722:
473	/*
474	 * Determine the size of the boot loader's copy of the bootinfo
475	 * struct.  This is impossible to do properly because old versions
476	 * of the struct don't contain a size field and there are 2 old
477	 * versions with the same version number.
478	 */
479	movl	$BI_ENDCOMMON,%ecx	/* prepare for sizeless version */
480	testl	$RB_BOOTINFO,8(%ebp)	/* bi_size (and bootinfo) valid? */
481	je	got_bi_size		/* no, sizeless version */
482	movl	BI_SIZE(%ebx),%ecx
483got_bi_size:
484
485	/*
486	 * Copy the common part of the bootinfo struct
487	 */
488	movl	%ebx,%esi
489	movl	$R(_bootinfo),%edi
490	cmpl	$BOOTINFO_SIZE,%ecx
491	jbe	got_common_bi_size
492	movl	$BOOTINFO_SIZE,%ecx
493got_common_bi_size:
494	cld
495	rep
496	movsb
497
498#ifdef NFS
499	/*
500	 * If we have a nfs_diskless structure copy it in
501	 */
502	movl	BI_NFS_DISKLESS(%ebx),%esi
503	cmpl	$0,%esi
504	je	olddiskboot
505	movl	$R(_nfs_diskless),%edi
506	movl	$NFSDISKLESS_SIZE,%ecx
507	cld
508	rep
509	movsb
510	movl	$R(_nfs_diskless_valid),%edi
511	movl	$1,(%edi)
512#endif
513
514	/*
515	 * The old style disk boot.
516	 *	(*btext)(howto, bootdev, cyloffset, esym);
517	 * Note that the newer boot code just falls into here to pick
518	 * up howto and bootdev, cyloffset and esym are no longer used
519	 */
520olddiskboot:
521	movl	8(%ebp),%eax
522	movl	%eax,R(_boothowto)
523	movl	12(%ebp),%eax
524	movl	%eax,R(_bootdev)
525
526#if defined(USERCONFIG_BOOT) && defined(USERCONFIG)
527	movl	$0x10200, %esi
528	movl	$R(_userconfig_from_boot),%edi
529	movl	$512,%ecx
530	cld
531	rep
532	movsb
533#endif /* USERCONFIG_BOOT */
534
535	ret
536
537
538/**********************************************************************
539 *
540 * Identify the CPU and initialize anything special about it
541 *
542 */
543identify_cpu:
544
545	/* Try to toggle alignment check flag; does not exist on 386. */
546	pushfl
547	popl	%eax
548	movl	%eax,%ecx
549	orl	$PSL_AC,%eax
550	pushl	%eax
551	popfl
552	pushfl
553	popl	%eax
554	xorl	%ecx,%eax
555	andl	$PSL_AC,%eax
556	pushl	%ecx
557	popfl
558
559	testl	%eax,%eax
560	jnz	1f
561	movl	$CPU_386,R(_cpu)
562	jmp	3f
563
5641:	/* Try to toggle identification flag; does not exist on early 486s. */
565	pushfl
566	popl	%eax
567	movl	%eax,%ecx
568	xorl	$PSL_ID,%eax
569	pushl	%eax
570	popfl
571	pushfl
572	popl	%eax
573	xorl	%ecx,%eax
574	andl	$PSL_ID,%eax
575	pushl	%ecx
576	popfl
577
578	testl	%eax,%eax
579	jnz	1f
580	movl	$CPU_486,R(_cpu)
581
582	/* check for Cyrix 486DLC -- based on check routine  */
583	/* documented in "Cx486SLC/e SMM Programmer's Guide" */
584	xorw	%dx,%dx
585	cmpw	%dx,%dx			# set flags to known state
586	pushfw
587	popw	%cx			# store flags in ecx
588	movw	$0xffff,%ax
589	movw	$0x0004,%bx
590	divw	%bx
591	pushfw
592	popw	%ax
593	andw	$0x08d5,%ax		# mask off important bits
594	andw	$0x08d5,%cx
595	cmpw	%ax,%cx
596
597	jnz	3f			# if flags changed, Intel chip
598
599	movl	$CPU_486DLC,R(_cpu) # set CPU value for Cyrix
600	movl	$0x69727943,R(_cpu_vendor)	# store vendor string
601	movw	$0x0078,R(_cpu_vendor+4)
602
603#ifndef CYRIX_CACHE_WORKS
604	/* Disable caching of the ISA hole only. */
605	invd
606	movb	$CCR0,%al		# Configuration Register index (CCR0)
607	outb	%al,$0x22
608	inb	$0x23,%al
609	orb	$(CCR0_NC1|CCR0_BARB),%al
610	movb	%al,%ah
611	movb	$CCR0,%al
612	outb	%al,$0x22
613	movb	%ah,%al
614	outb	%al,$0x23
615	invd
616#else /* CYRIX_CACHE_WORKS */
617	/* Set cache parameters */
618	invd				# Start with guaranteed clean cache
619	movb	$CCR0,%al		# Configuration Register index (CCR0)
620	outb	%al,$0x22
621	inb	$0x23,%al
622	andb	$~CCR0_NC0,%al
623#ifndef CYRIX_CACHE_REALLY_WORKS
624	orb	$(CCR0_NC1|CCR0_BARB),%al
625#else /* CYRIX_CACHE_REALLY_WORKS */
626	orb	$CCR0_NC1,%al
627#endif /* !CYRIX_CACHE_REALLY_WORKS */
628	movb	%al,%ah
629	movb	$CCR0,%al
630	outb	%al,$0x22
631	movb	%ah,%al
632	outb	%al,$0x23
633	/* clear non-cacheable region 1	*/
634	movb	$(NCR1+2),%al
635	outb	%al,$0x22
636	movb	$NCR_SIZE_0K,%al
637	outb	%al,$0x23
638	/* clear non-cacheable region 2	*/
639	movb	$(NCR2+2),%al
640	outb	%al,$0x22
641	movb	$NCR_SIZE_0K,%al
642	outb	%al,$0x23
643	/* clear non-cacheable region 3	*/
644	movb	$(NCR3+2),%al
645	outb	%al,$0x22
646	movb	$NCR_SIZE_0K,%al
647	outb	%al,$0x23
648	/* clear non-cacheable region 4	*/
649	movb	$(NCR4+2),%al
650	outb	%al,$0x22
651	movb	$NCR_SIZE_0K,%al
652	outb	%al,$0x23
653	/* enable caching in CR0 */
654	movl	%cr0,%eax
655	andl	$~(CR0_CD|CR0_NW),%eax
656	movl	%eax,%cr0
657	invd
658#endif /* !CYRIX_CACHE_WORKS */
659	jmp	3f
660
6611:	/* Use the `cpuid' instruction. */
662	xorl	%eax,%eax
663	.byte	0x0f,0xa2			# cpuid 0
664	movl	%eax,R(_cpu_high)		# highest capability
665	movl	%ebx,R(_cpu_vendor)		# store vendor string
666	movl	%edx,R(_cpu_vendor+4)
667	movl	%ecx,R(_cpu_vendor+8)
668	movb	$0,R(_cpu_vendor+12)
669
670	movl	$1,%eax
671	.byte	0x0f,0xa2			# cpuid 1
672	movl	%eax,R(_cpu_id)			# store cpu_id
673	movl	%edx,R(_cpu_feature)		# store cpu_feature
674	rorl	$8,%eax				# extract family type
675	andl	$15,%eax
676	cmpl	$5,%eax
677	jae	1f
678
679	/* less than Pentium; must be 486 */
680	movl	$CPU_486,R(_cpu)
681	jmp	3f
6821:
683	/* a Pentium? */
684	cmpl	$5,%eax
685	jne	2f
686	movl	$CPU_586,R(_cpu)
687	jmp	3f
6882:
689	/* Greater than Pentium...call it a Pentium Pro */
690	movl	$CPU_686,R(_cpu)
6913:
692	ret
693
694
695/**********************************************************************
696 *
697 * Create the first page directory and its page tables.
698 *
699 */
700
701create_pagetables:
702
703	testl	$CPUID_PGE, R(_cpu_feature)
704	jz	1f
705	movl	%cr4, %eax
706	orl	$CR4_PGE, %eax
707	movl	%eax, %cr4
7081:
709
710/* Find end of kernel image (rounded up to a page boundary). */
711	movl	$R(_end),%esi
712
713/* include symbols in "kernel image" if they are loaded and useful */
714#ifdef DDB
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#endif
724
725	addl	$PAGE_MASK,%esi
726	andl	$~PAGE_MASK,%esi
727	movl	%esi,R(_KERNend)	/* save end of kernel */
728	movl	%esi,R(physfree)	/* next free page is at end of kernel */
729
730/* Allocate Kernel Page Tables */
731	ALLOCPAGES(NKPT)
732	movl	%esi,R(_KPTphys)
733
734/* Allocate Page Table Directory */
735	ALLOCPAGES(1)
736	movl	%esi,R(_IdlePTD)
737
738/* Allocate UPAGES */
739	ALLOCPAGES(UPAGES)
740	movl	%esi,R(p0upa)
741	addl	$KERNBASE, %esi
742	movl	%esi, R(_proc0paddr)
743
744/* Allocate proc0's page table for the UPAGES. */
745	ALLOCPAGES(1)
746	movl	%esi,R(p0upt)
747
748/* Map read-only from zero to the end of the kernel text section */
749	xorl	%eax, %eax
750#ifdef BDE_DEBUGGER
751/* If the debugger is present, actually map everything read-write. */
752	cmpl	$0,R(_bdb_exists)
753	jne	map_read_write
754#endif
755	xorl	%edx,%edx
756	testl	$CPUID_PGE, R(_cpu_feature)
757	jz	2f
758	orl	$PG_G,%edx
759
7602:	movl	$R(_etext),%ecx
761	addl	$PAGE_MASK,%ecx
762	shrl	$PAGE_SHIFT,%ecx
763	fillkptphys(%edx)
764
765/* Map read-write, data, bss and symbols */
766	movl	$R(_etext),%eax
767	addl	$PAGE_MASK, %eax
768	andl	$~PAGE_MASK, %eax
769map_read_write:
770	movl	$PG_RW,%edx
771	testl	$CPUID_PGE, R(_cpu_feature)
772	jz	1f
773	orl	$PG_G,%edx
774
7751:	movl	R(_KERNend),%ecx
776	subl	%eax,%ecx
777	shrl	$PAGE_SHIFT,%ecx
778	fillkptphys(%edx)
779
780/* Map page directory. */
781	movl	R(_IdlePTD), %eax
782	movl	$1, %ecx
783	fillkptphys($PG_RW)
784
785/* Map proc0's page table for the UPAGES. */
786	movl	R(p0upt), %eax
787	movl	$1, %ecx
788	fillkptphys($PG_RW)
789
790/* Map proc0's UPAGES in the physical way ... */
791	movl	R(p0upa), %eax
792	movl	$UPAGES, %ecx
793	fillkptphys($PG_RW)
794
795/* Map ISA hole */
796	movl	$ISA_HOLE_START, %eax
797	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
798	fillkptphys($PG_RW)
799
800/* Map proc0s UPAGES in the special page table for this purpose ... */
801	movl	R(p0upa), %eax
802	movl	$KSTKPTEOFF, %ebx
803	movl	$UPAGES, %ecx
804	fillkpt(R(p0upt), $PG_RW)
805
806/* ... and put the page table in the pde. */
807	movl	R(p0upt), %eax
808	movl	$KSTKPTDI, %ebx
809	movl	$1, %ecx
810	fillkpt(R(_IdlePTD), $PG_RW)
811
812/* install a pde for temporary double map of bottom of VA */
813	movl	R(_KPTphys), %eax
814	xorl	%ebx, %ebx
815	movl	$1, %ecx
816	fillkpt(R(_IdlePTD), $PG_RW)
817
818/* install pde's for pt's */
819	movl	R(_KPTphys), %eax
820	movl	$KPTDI, %ebx
821	movl	$NKPT, %ecx
822	fillkpt(R(_IdlePTD), $PG_RW)
823
824/* install a pde recursively mapping page directory as a page table */
825	movl	R(_IdlePTD), %eax
826	movl	$PTDPTDI, %ebx
827	movl	$1,%ecx
828	fillkpt(R(_IdlePTD), $PG_RW)
829
830	ret
831
832#ifdef BDE_DEBUGGER
833bdb_prepare_paging:
834	cmpl	$0,R(_bdb_exists)
835	je	bdb_prepare_paging_exit
836
837	subl	$6,%esp
838
839	/*
840	 * Copy and convert debugger entries from the bootstrap gdt and idt
841	 * to the kernel gdt and idt.  Everything is still in low memory.
842	 * Tracing continues to work after paging is enabled because the
843	 * low memory addresses remain valid until everything is relocated.
844	 * However, tracing through the setidt() that initializes the trace
845	 * trap will crash.
846	 */
847	sgdt	(%esp)
848	movl	2(%esp),%esi		/* base address of bootstrap gdt */
849	movl	$R(_gdt),%edi
850	movl	%edi,2(%esp)		/* prepare to load kernel gdt */
851	movl	$8*18/4,%ecx
852	cld
853	rep				/* copy gdt */
854	movsl
855	movl	$R(_gdt),-8+2(%edi)	/* adjust gdt self-ptr */
856	movb	$0x92,-8+5(%edi)
857	lgdt	(%esp)
858
859	sidt	(%esp)
860	movl	2(%esp),%esi		/* base address of current idt */
861	movl	8+4(%esi),%eax		/* convert dbg descriptor to ... */
862	movw	8(%esi),%ax
863	movl	%eax,R(bdb_dbg_ljmp+1)	/* ... immediate offset ... */
864	movl	8+2(%esi),%eax
865	movw	%ax,R(bdb_dbg_ljmp+5)	/* ... and selector for ljmp */
866	movl	24+4(%esi),%eax		/* same for bpt descriptor */
867	movw	24(%esi),%ax
868	movl	%eax,R(bdb_bpt_ljmp+1)
869	movl	24+2(%esi),%eax
870	movw	%ax,R(bdb_bpt_ljmp+5)
871	movl	$R(_idt),%edi
872	movl	%edi,2(%esp)		/* prepare to load kernel idt */
873	movl	$8*4/4,%ecx
874	cld
875	rep				/* copy idt */
876	movsl
877	lidt	(%esp)
878
879	addl	$6,%esp
880
881bdb_prepare_paging_exit:
882	ret
883
884/* Relocate debugger gdt entries and gdt and idt pointers. */
885bdb_commit_paging:
886	cmpl	$0,_bdb_exists
887	je	bdb_commit_paging_exit
888
889	movl	$_gdt+8*9,%eax		/* adjust slots 9-17 */
890	movl	$9,%ecx
891reloc_gdt:
892	movb	$KERNBASE>>24,7(%eax)	/* top byte of base addresses, was 0, */
893	addl	$8,%eax			/* now KERNBASE>>24 */
894	loop	reloc_gdt
895
896	subl	$6,%esp
897	sgdt	(%esp)
898	addl	$KERNBASE,2(%esp)
899	lgdt	(%esp)
900	sidt	(%esp)
901	addl	$KERNBASE,2(%esp)
902	lidt	(%esp)
903	addl	$6,%esp
904
905	int	$3
906
907bdb_commit_paging_exit:
908	ret
909
910#endif /* BDE_DEBUGGER */
911