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