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