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