locore.s revision 83366
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 83366 2001-09-12 08:38:13Z julian $
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 * NOTE: PTDpde, PTmap, and PTD are being defined as address symbols.
79 * In C you access them directly, and not with a '*'. Storage is not being
80 * allocated. They will magically address the correct locations in KVM
81 * which C will treat as normal variables of the type they are defined in
82 * machine/pmap.h, i.e.  PTDpde = XX ; to set a PDE entry, NOT *PTDpde = XX;
83 */
84	.globl	PTmap,PTD,PTDpde
85	.set	PTmap,(PTDPTDI << PDRSHIFT)
86	.set	PTD,PTmap + (PTDPTDI * PAGE_SIZE)
87	.set	PTDpde,PTD + (PTDPTDI * PDESIZE)
88
89/*
90 * APTmap, APTD is the alternate recursive pagemap.
91 * It's used when modifying another process's page tables.
92 * See the note above. It is true here as well.
93 */
94	.globl	APTmap,APTD,APTDpde
95	.set	APTmap,APTDPTDI << PDRSHIFT
96	.set	APTD,APTmap + (APTDPTDI * PAGE_SIZE)
97	.set	APTDpde,PTD + (APTDPTDI * PDESIZE)
98
99#ifdef SMP
100/*
101 * Define layout of per-cpu address space.
102 * This is "constructed" in locore.s on the BSP and in mp_machdep.c
103 * for each AP.  DO NOT REORDER THESE WITHOUT UPDATING THE REST!
104 */
105	.globl	SMP_prvspace, lapic
106	.set	SMP_prvspace,(MPPTDI << PDRSHIFT)
107	.set	lapic,SMP_prvspace + (NPTEPG-1) * PAGE_SIZE
108#endif /* SMP */
109
110/*
111 * Compiled KERNBASE location
112 */
113	.globl	kernbase
114	.set	kernbase,KERNBASE
115
116/*
117 * Globals
118 */
119	.data
120	ALIGN_DATA		/* just to be sure */
121
122	.globl	HIDENAME(tmpstk)
123	.space	0x2000		/* space for tmpstk - temporary stack */
124HIDENAME(tmpstk):
125
126	.globl	bootinfo
127bootinfo:	.space	BOOTINFO_SIZE		/* bootinfo that we can handle */
128
129KERNend:	.long	0			/* phys addr end of kernel (just after bss) */
130physfree:	.long	0			/* phys addr of next free page */
131
132#ifdef SMP
133		.globl	cpu0prvpage
134cpu0pp:		.long	0			/* phys addr cpu0 private pg */
135cpu0prvpage:	.long	0			/* relocated version */
136
137		.globl	SMPpt
138SMPptpa:	.long	0			/* phys addr SMP page table */
139SMPpt:		.long	0			/* relocated version */
140#endif /* SMP */
141
142	.globl	IdlePTD
143IdlePTD:	.long	0			/* phys addr of kernel PTD */
144
145#ifdef SMP
146	.globl	KPTphys
147#endif
148KPTphys:	.long	0			/* phys addr of kernel page tables */
149
150	.globl	proc0uarea, proc0kstack
151proc0uarea:	.long	0			/* address of proc 0 uarea space */
152proc0kstack:	.long	0			/* address of proc 0 kstack space */
153p0upa:		.long	0			/* phys addr of proc0's UAREA */
154p0kpa:		.long	0			/* phys addr of proc0's STACK */
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	proc0kstack,%eax		/* location of in-kernel stack */
375			/* bootstrap stack end location */
376	leal	(KSTACK_PAGES*PAGE_SIZE-PCB_SIZE)(%eax),%esp
377
378	xorl	%ebp,%ebp			/* mark end of frames */
379
380	movl	IdlePTD,%esi
381	movl	%esi,(KSTACK_PAGES*PAGE_SIZE-PCB_SIZE+PCB_CR3)(%eax)
382
383	testl	$CPUID_PGE, R(cpu_feature)
384	jz	1f
385	movl	%cr4, %eax
386	orl	$CR4_PGE, %eax
387	movl	%eax, %cr4
3881:
389	pushl	physfree			/* value of first for init386(first) */
390	call	init386				/* wire 386 chip for unix operation */
391
392	/*
393	 * Clean up the stack in a way that db_numargs() understands, so
394	 * that backtraces in ddb don't underrun the stack.  Traps for
395	 * inaccessible memory are more fatal than usual this early.
396	 */
397	addl	$4,%esp
398
399	call	mi_startup			/* autoconfiguration, mountroot etc */
400	/* NOTREACHED */
401	addl	$0,%esp				/* for db_numargs() again */
402
403/*
404 * Signal trampoline, copied to top of user stack
405 */
406NON_GPROF_ENTRY(sigcode)
407	call	*SIGF_HANDLER(%esp)		/* call signal handler */
408	lea	SIGF_UC(%esp),%eax		/* get ucontext_t */
409	pushl	%eax
410	testl	$PSL_VM,UC_EFLAGS(%eax)
411	jne	9f
412	movl	UC_GS(%eax),%gs			/* restore %gs */
4139:
414	movl	$SYS_sigreturn,%eax
415	pushl	%eax				/* junk to fake return addr. */
416	int	$0x80				/* enter kernel with args */
4170:	jmp	0b
418
419	ALIGN_TEXT
420osigcode:
421	call	*SIGF_HANDLER(%esp)		/* call signal handler */
422	lea	SIGF_SC(%esp),%eax		/* get sigcontext */
423	pushl	%eax
424	testl	$PSL_VM,SC_PS(%eax)
425	jne	9f
426	movl	SC_GS(%eax),%gs			/* restore %gs */
4279:
428	movl	$0x01d516,SC_TRAPNO(%eax)	/* magic: 0ldSiG */
429	movl	$SYS_sigreturn,%eax
430	pushl	%eax				/* junk to fake return addr. */
431	int	$0x80				/* enter kernel with args */
4320:	jmp	0b
433
434	ALIGN_TEXT
435esigcode:
436
437	.data
438	.globl	szsigcode, szosigcode
439szsigcode:
440	.long	esigcode-sigcode
441szosigcode:
442	.long	esigcode-osigcode
443	.text
444
445/**********************************************************************
446 *
447 * Recover the bootinfo passed to us from the boot program
448 *
449 */
450recover_bootinfo:
451	/*
452	 * This code is called in different ways depending on what loaded
453	 * and started the kernel.  This is used to detect how we get the
454	 * arguments from the other code and what we do with them.
455	 *
456	 * Old disk boot blocks:
457	 *	(*btext)(howto, bootdev, cyloffset, esym);
458	 *	[return address == 0, and can NOT be returned to]
459	 *	[cyloffset was not supported by the FreeBSD boot code
460	 *	 and always passed in as 0]
461	 *	[esym is also known as total in the boot code, and
462	 *	 was never properly supported by the FreeBSD boot code]
463	 *
464	 * Old diskless netboot code:
465	 *	(*btext)(0,0,0,0,&nfsdiskless,0,0,0);
466	 *	[return address != 0, and can NOT be returned to]
467	 *	If we are being booted by this code it will NOT work,
468	 *	so we are just going to halt if we find this case.
469	 *
470	 * New uniform boot code:
471	 *	(*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
472	 *	[return address != 0, and can be returned to]
473	 *
474	 * There may seem to be a lot of wasted arguments in here, but
475	 * that is so the newer boot code can still load very old kernels
476	 * and old boot code can load new kernels.
477	 */
478
479	/*
480	 * The old style disk boot blocks fake a frame on the stack and
481	 * did an lret to get here.  The frame on the stack has a return
482	 * address of 0.
483	 */
484	cmpl	$0,4(%ebp)
485	je	olddiskboot
486
487	/*
488	 * We have some form of return address, so this is either the
489	 * old diskless netboot code, or the new uniform code.  That can
490	 * be detected by looking at the 5th argument, if it is 0
491	 * we are being booted by the new uniform boot code.
492	 */
493	cmpl	$0,24(%ebp)
494	je	newboot
495
496	/*
497	 * Seems we have been loaded by the old diskless boot code, we
498	 * don't stand a chance of running as the diskless structure
499	 * changed considerably between the two, so just halt.
500	 */
501	 hlt
502
503	/*
504	 * We have been loaded by the new uniform boot code.
505	 * Let's check the bootinfo version, and if we do not understand
506	 * it we return to the loader with a status of 1 to indicate this error
507	 */
508newboot:
509	movl	28(%ebp),%ebx		/* &bootinfo.version */
510	movl	BI_VERSION(%ebx),%eax
511	cmpl	$1,%eax			/* We only understand version 1 */
512	je	1f
513	movl	$1,%eax			/* Return status */
514	leave
515	/*
516	 * XXX this returns to our caller's caller (as is required) since
517	 * we didn't set up a frame and our caller did.
518	 */
519	ret
520
5211:
522	/*
523	 * If we have a kernelname copy it in
524	 */
525	movl	BI_KERNELNAME(%ebx),%esi
526	cmpl	$0,%esi
527	je	2f			/* No kernelname */
528	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
529	movl	$R(kernelname),%edi
530	cmpb	$'/',(%esi)		/* Make sure it starts with a slash */
531	je	1f
532	movb	$'/',(%edi)
533	incl	%edi
534	decl	%ecx
5351:
536	cld
537	rep
538	movsb
539
5402:
541	/*
542	 * Determine the size of the boot loader's copy of the bootinfo
543	 * struct.  This is impossible to do properly because old versions
544	 * of the struct don't contain a size field and there are 2 old
545	 * versions with the same version number.
546	 */
547	movl	$BI_ENDCOMMON,%ecx	/* prepare for sizeless version */
548	testl	$RB_BOOTINFO,8(%ebp)	/* bi_size (and bootinfo) valid? */
549	je	got_bi_size		/* no, sizeless version */
550	movl	BI_SIZE(%ebx),%ecx
551got_bi_size:
552
553	/*
554	 * Copy the common part of the bootinfo struct
555	 */
556	movl	%ebx,%esi
557	movl	$R(bootinfo),%edi
558	cmpl	$BOOTINFO_SIZE,%ecx
559	jbe	got_common_bi_size
560	movl	$BOOTINFO_SIZE,%ecx
561got_common_bi_size:
562	cld
563	rep
564	movsb
565
566#ifdef NFS_ROOT
567#ifndef BOOTP_NFSV3
568	/*
569	 * If we have a nfs_diskless structure copy it in
570	 */
571	movl	BI_NFS_DISKLESS(%ebx),%esi
572	cmpl	$0,%esi
573	je	olddiskboot
574	movl	$R(nfs_diskless),%edi
575	movl	$NFSDISKLESS_SIZE,%ecx
576	cld
577	rep
578	movsb
579	movl	$R(nfs_diskless_valid),%edi
580	movl	$1,(%edi)
581#endif
582#endif
583
584	/*
585	 * The old style disk boot.
586	 *	(*btext)(howto, bootdev, cyloffset, esym);
587	 * Note that the newer boot code just falls into here to pick
588	 * up howto and bootdev, cyloffset and esym are no longer used
589	 */
590olddiskboot:
591	movl	8(%ebp),%eax
592	movl	%eax,R(boothowto)
593	movl	12(%ebp),%eax
594	movl	%eax,R(bootdev)
595
596	ret
597
598
599/**********************************************************************
600 *
601 * Identify the CPU and initialize anything special about it
602 *
603 */
604identify_cpu:
605
606	/* Try to toggle alignment check flag; does not exist on 386. */
607	pushfl
608	popl	%eax
609	movl	%eax,%ecx
610	orl	$PSL_AC,%eax
611	pushl	%eax
612	popfl
613	pushfl
614	popl	%eax
615	xorl	%ecx,%eax
616	andl	$PSL_AC,%eax
617	pushl	%ecx
618	popfl
619
620	testl	%eax,%eax
621	jnz	try486
622
623	/* NexGen CPU does not have aligment check flag. */
624	pushfl
625	movl	$0x5555, %eax
626	xorl	%edx, %edx
627	movl	$2, %ecx
628	clc
629	divl	%ecx
630	jz	trynexgen
631	popfl
632	movl	$CPU_386,R(cpu)
633	jmp	3f
634
635trynexgen:
636	popfl
637	movl	$CPU_NX586,R(cpu)
638	movl	$0x4778654e,R(cpu_vendor)	# store vendor string
639	movl	$0x72446e65,R(cpu_vendor+4)
640	movl	$0x6e657669,R(cpu_vendor+8)
641	movl	$0,R(cpu_vendor+12)
642	jmp	3f
643
644try486:	/* Try to toggle identification flag; does not exist on early 486s. */
645	pushfl
646	popl	%eax
647	movl	%eax,%ecx
648	xorl	$PSL_ID,%eax
649	pushl	%eax
650	popfl
651	pushfl
652	popl	%eax
653	xorl	%ecx,%eax
654	andl	$PSL_ID,%eax
655	pushl	%ecx
656	popfl
657
658	testl	%eax,%eax
659	jnz	trycpuid
660	movl	$CPU_486,R(cpu)
661
662	/*
663	 * Check Cyrix CPU
664	 * Cyrix CPUs do not change the undefined flags following
665	 * execution of the divide instruction which divides 5 by 2.
666	 *
667	 * Note: CPUID is enabled on M2, so it passes another way.
668	 */
669	pushfl
670	movl	$0x5555, %eax
671	xorl	%edx, %edx
672	movl	$2, %ecx
673	clc
674	divl	%ecx
675	jnc	trycyrix
676	popfl
677	jmp	3f		/* You may use Intel CPU. */
678
679trycyrix:
680	popfl
681	/*
682	 * IBM Bluelighting CPU also doesn't change the undefined flags.
683	 * Because IBM doesn't disclose the information for Bluelighting
684	 * CPU, we couldn't distinguish it from Cyrix's (including IBM
685	 * brand of Cyrix CPUs).
686	 */
687	movl	$0x69727943,R(cpu_vendor)	# store vendor string
688	movl	$0x736e4978,R(cpu_vendor+4)
689	movl	$0x64616574,R(cpu_vendor+8)
690	jmp	3f
691
692trycpuid:	/* Use the `cpuid' instruction. */
693	xorl	%eax,%eax
694	cpuid					# cpuid 0
695	movl	%eax,R(cpu_high)		# highest capability
696	movl	%ebx,R(cpu_vendor)		# store vendor string
697	movl	%edx,R(cpu_vendor+4)
698	movl	%ecx,R(cpu_vendor+8)
699	movb	$0,R(cpu_vendor+12)
700
701	movl	$1,%eax
702	cpuid					# cpuid 1
703	movl	%eax,R(cpu_id)			# store cpu_id
704	movl	%edx,R(cpu_feature)		# store cpu_feature
705	rorl	$8,%eax				# extract family type
706	andl	$15,%eax
707	cmpl	$5,%eax
708	jae	1f
709
710	/* less than Pentium; must be 486 */
711	movl	$CPU_486,R(cpu)
712	jmp	3f
7131:
714	/* a Pentium? */
715	cmpl	$5,%eax
716	jne	2f
717	movl	$CPU_586,R(cpu)
718	jmp	3f
7192:
720	/* Greater than Pentium...call it a Pentium Pro */
721	movl	$CPU_686,R(cpu)
7223:
723	ret
724
725
726/**********************************************************************
727 *
728 * Create the first page directory and its page tables.
729 *
730 */
731
732create_pagetables:
733
734/* Find end of kernel image (rounded up to a page boundary). */
735	movl	$R(_end),%esi
736
737/* Include symbols, if any. */
738	movl	R(bootinfo+BI_ESYMTAB),%edi
739	testl	%edi,%edi
740	je	over_symalloc
741	movl	%edi,%esi
742	movl	$KERNBASE,%edi
743	addl	%edi,R(bootinfo+BI_SYMTAB)
744	addl	%edi,R(bootinfo+BI_ESYMTAB)
745over_symalloc:
746
747/* If we are told where the end of the kernel space is, believe it. */
748	movl	R(bootinfo+BI_KERNEND),%edi
749	testl	%edi,%edi
750	je	no_kernend
751	movl	%edi,%esi
752no_kernend:
753
754	addl	$PAGE_MASK,%esi
755	andl	$~PAGE_MASK,%esi
756	movl	%esi,R(KERNend)		/* save end of kernel */
757	movl	%esi,R(physfree)	/* next free page is at end of kernel */
758
759/* Allocate Kernel Page Tables */
760	ALLOCPAGES(NKPT)
761	movl	%esi,R(KPTphys)
762
763/* Allocate Page Table Directory */
764	ALLOCPAGES(1)
765	movl	%esi,R(IdlePTD)
766
767/* Allocate UPAGES */
768	ALLOCPAGES(UAREA_PAGES)
769	movl	%esi,R(p0upa)
770	addl	$KERNBASE, %esi
771	movl	%esi, R(proc0uarea)
772
773	ALLOCPAGES(KSTACK_PAGES)
774	movl	%esi,R(p0kpa)
775	addl	$KERNBASE, %esi
776	movl	%esi, R(proc0kstack)
777
778	ALLOCPAGES(1)			/* vm86/bios stack */
779	movl	%esi,R(vm86phystk)
780
781	ALLOCPAGES(3)			/* pgtable + ext + IOPAGES */
782	movl	%esi,R(vm86pa)
783	addl	$KERNBASE, %esi
784	movl	%esi, R(vm86paddr)
785
786#ifdef SMP
787/* Allocate cpu0's private data page */
788	ALLOCPAGES(1)
789	movl	%esi,R(cpu0pp)
790	addl	$KERNBASE, %esi
791	movl	%esi, R(cpu0prvpage)	/* relocated to KVM space */
792
793/* Allocate SMP page table page */
794	ALLOCPAGES(1)
795	movl	%esi,R(SMPptpa)
796	addl	$KERNBASE, %esi
797	movl	%esi, R(SMPpt)		/* relocated to KVM space */
798#endif	/* SMP */
799
800/* Map read-only from zero to the end of the kernel text section */
801	xorl	%eax, %eax
802#ifdef BDE_DEBUGGER
803/* If the debugger is present, actually map everything read-write. */
804	cmpl	$0,R(_bdb_exists)
805	jne	map_read_write
806#endif
807	xorl	%edx,%edx
808
809#if !defined(SMP)
810	testl	$CPUID_PGE, R(cpu_feature)
811	jz	2f
812	orl	$PG_G,%edx
813#endif
814
8152:	movl	$R(etext),%ecx
816	addl	$PAGE_MASK,%ecx
817	shrl	$PAGE_SHIFT,%ecx
818	fillkptphys(%edx)
819
820/* Map read-write, data, bss and symbols */
821	movl	$R(etext),%eax
822	addl	$PAGE_MASK, %eax
823	andl	$~PAGE_MASK, %eax
824map_read_write:
825	movl	$PG_RW,%edx
826#if !defined(SMP)
827	testl	$CPUID_PGE, R(cpu_feature)
828	jz	1f
829	orl	$PG_G,%edx
830#endif
831
8321:	movl	R(KERNend),%ecx
833	subl	%eax,%ecx
834	shrl	$PAGE_SHIFT,%ecx
835	fillkptphys(%edx)
836
837/* Map page directory. */
838	movl	R(IdlePTD), %eax
839	movl	$1, %ecx
840	fillkptphys($PG_RW)
841
842/* Map proc0's UPAGES in the physical way ... */
843	movl	R(p0upa), %eax
844	movl	$(UAREA_PAGES), %ecx
845	fillkptphys($PG_RW)
846
847/* Map proc0's KSTACK in the physical way ... */
848	movl	R(p0kpa), %eax
849	movl	$(KSTACK_PAGES), %ecx
850	fillkptphys($PG_RW)
851
852/* Map ISA hole */
853	movl	$ISA_HOLE_START, %eax
854	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
855	fillkptphys($PG_RW)
856
857/* Map space for the vm86 region */
858	movl	R(vm86phystk), %eax
859	movl	$4, %ecx
860	fillkptphys($PG_RW)
861
862/* Map page 0 into the vm86 page table */
863	movl	$0, %eax
864	movl	$0, %ebx
865	movl	$1, %ecx
866	fillkpt(R(vm86pa), $PG_RW|PG_U)
867
868/* ...likewise for the ISA hole */
869	movl	$ISA_HOLE_START, %eax
870	movl	$ISA_HOLE_START>>PAGE_SHIFT, %ebx
871	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
872	fillkpt(R(vm86pa), $PG_RW|PG_U)
873
874#ifdef SMP
875/* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */
876	movl	R(cpu0pp), %eax
877	movl	$1, %ecx
878	fillkptphys($PG_RW)
879
880/* Map SMP page table page into global kmem FWIW */
881	movl	R(SMPptpa), %eax
882	movl	$1, %ecx
883	fillkptphys($PG_RW)
884
885/* Map the private page into the SMP page table */
886	movl	R(cpu0pp), %eax
887	movl	$0, %ebx		/* pte offset = 0 */
888	movl	$1, %ecx		/* one private page coming right up */
889	fillkpt(R(SMPptpa), $PG_RW)
890
891/* ... and put the page table table in the pde. */
892	movl	R(SMPptpa), %eax
893	movl	$MPPTDI, %ebx
894	movl	$1, %ecx
895	fillkpt(R(IdlePTD), $PG_RW)
896
897/* Fakeup VA for the local apic to allow early traps. */
898	ALLOCPAGES(1)
899	movl	%esi, %eax
900	movl	$(NPTEPG-1), %ebx	/* pte offset = NTEPG-1 */
901	movl	$1, %ecx		/* one private pt coming right up */
902	fillkpt(R(SMPptpa), $PG_RW)
903#endif	/* SMP */
904
905/* install a pde for temporary double map of bottom of VA */
906	movl	R(KPTphys), %eax
907	xorl	%ebx, %ebx
908	movl	$NKPT, %ecx
909	fillkpt(R(IdlePTD), $PG_RW)
910
911/* install pde's for pt's */
912	movl	R(KPTphys), %eax
913	movl	$KPTDI, %ebx
914	movl	$NKPT, %ecx
915	fillkpt(R(IdlePTD), $PG_RW)
916
917/* install a pde recursively mapping page directory as a page table */
918	movl	R(IdlePTD), %eax
919	movl	$PTDPTDI, %ebx
920	movl	$1,%ecx
921	fillkpt(R(IdlePTD), $PG_RW)
922
923	ret
924
925#ifdef BDE_DEBUGGER
926bdb_prepare_paging:
927	cmpl	$0,R(_bdb_exists)
928	je	bdb_prepare_paging_exit
929
930	subl	$6,%esp
931
932	/*
933	 * Copy and convert debugger entries from the bootstrap gdt and idt
934	 * to the kernel gdt and idt.  Everything is still in low memory.
935	 * Tracing continues to work after paging is enabled because the
936	 * low memory addresses remain valid until everything is relocated.
937	 * However, tracing through the setidt() that initializes the trace
938	 * trap will crash.
939	 */
940	sgdt	(%esp)
941	movl	2(%esp),%esi		/* base address of bootstrap gdt */
942	movl	$R(_gdt),%edi
943	movl	%edi,2(%esp)		/* prepare to load kernel gdt */
944	movl	$8*18/4,%ecx
945	cld
946	rep				/* copy gdt */
947	movsl
948	movl	$R(_gdt),-8+2(%edi)	/* adjust gdt self-ptr */
949	movb	$0x92,-8+5(%edi)
950	lgdt	(%esp)
951
952	sidt	(%esp)
953	movl	2(%esp),%esi		/* base address of current idt */
954	movl	8+4(%esi),%eax		/* convert dbg descriptor to ... */
955	movw	8(%esi),%ax
956	movl	%eax,R(bdb_dbg_ljmp+1)	/* ... immediate offset ... */
957	movl	8+2(%esi),%eax
958	movw	%ax,R(bdb_dbg_ljmp+5)	/* ... and selector for ljmp */
959	movl	24+4(%esi),%eax		/* same for bpt descriptor */
960	movw	24(%esi),%ax
961	movl	%eax,R(bdb_bpt_ljmp+1)
962	movl	24+2(%esi),%eax
963	movw	%ax,R(bdb_bpt_ljmp+5)
964	movl	R(_idt),%edi
965	movl	%edi,2(%esp)		/* prepare to load kernel idt */
966	movl	$8*4/4,%ecx
967	cld
968	rep				/* copy idt */
969	movsl
970	lidt	(%esp)
971
972	addl	$6,%esp
973
974bdb_prepare_paging_exit:
975	ret
976
977/* Relocate debugger gdt entries and gdt and idt pointers. */
978bdb_commit_paging:
979	cmpl	$0,_bdb_exists
980	je	bdb_commit_paging_exit
981
982	movl	$gdt+8*9,%eax		/* adjust slots 9-17 */
983	movl	$9,%ecx
984reloc_gdt:
985	movb	$KERNBASE>>24,7(%eax)	/* top byte of base addresses, was 0, */
986	addl	$8,%eax			/* now KERNBASE>>24 */
987	loop	reloc_gdt
988
989	subl	$6,%esp
990	sgdt	(%esp)
991	addl	$KERNBASE,2(%esp)
992	lgdt	(%esp)
993	sidt	(%esp)
994	addl	$KERNBASE,2(%esp)
995	lidt	(%esp)
996	addl	$6,%esp
997
998	int	$3
999
1000bdb_commit_paging_exit:
1001	ret
1002
1003#endif /* BDE_DEBUGGER */
1004