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