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