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