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