locore.s revision 27003
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.1 1997/06/27 18:33:41 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#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	movl	$CPU_NX586,R(_cpu)
664	movl	$0x4778654e,R(_cpu_vendor)	# store vendor string
665	movl	$0x72446e65,R(_cpu_vendor+4)
666	movl	$0x6e657669,R(_cpu_vendor+8)
667	movl	$0,R(_cpu_vendor+12)
668	jmp	3f
669
670try486:	/* Try to toggle identification flag; does not exist on early 486s. */
671	pushfl
672	popl	%eax
673	movl	%eax,%ecx
674	xorl	$PSL_ID,%eax
675	pushl	%eax
676	popfl
677	pushfl
678	popl	%eax
679	xorl	%ecx,%eax
680	andl	$PSL_ID,%eax
681	pushl	%ecx
682	popfl
683
684	testl	%eax,%eax
685	jnz	trycpuid
686	movl	$CPU_486,R(_cpu)
687
688	/*
689	 * Check Cyrix CPU
690	 * Cyrix CPUs do not change the undefined flags following
691	 * execution of the divide instruction which divides 5 by 2.
692	 *
693	 * Note: CPUID is enabled on M2, so it passes another way.
694	 */
695	pushfl
696	movl	$0x5555, %eax
697	xorl	%edx, %edx
698	movl	$2, %ecx
699	clc
700	divl	%ecx
701	jnc	trycyrix
702	popfl
703	jmp	3f		/* You may use Intel CPU. */
704
705trycyrix:
706	popfl
707	/*
708	 * IBM Bluelighting CPU also doesn't change the undefined flags.
709	 * Because IBM doesn't disclose the information for Bluelighting
710	 * CPU, we couldn't distinguish it from Cyrix's (including IBM
711	 * brand of Cyrix CPUs).
712	 */
713	movl	$0x69727943,R(_cpu_vendor)	# store vendor string
714	movl	$0x736e4978,R(_cpu_vendor+4)
715	movl	$0x64616574,R(_cpu_vendor+8)
716	jmp	3f
717
718trycpuid:	/* Use the `cpuid' instruction. */
719	xorl	%eax,%eax
720	.byte	0x0f,0xa2			# cpuid 0
721	movl	%eax,R(_cpu_high)		# highest capability
722	movl	%ebx,R(_cpu_vendor)		# store vendor string
723	movl	%edx,R(_cpu_vendor+4)
724	movl	%ecx,R(_cpu_vendor+8)
725	movb	$0,R(_cpu_vendor+12)
726
727	movl	$1,%eax
728	.byte	0x0f,0xa2			# cpuid 1
729	movl	%eax,R(_cpu_id)			# store cpu_id
730	movl	%edx,R(_cpu_feature)		# store cpu_feature
731	rorl	$8,%eax				# extract family type
732	andl	$15,%eax
733	cmpl	$5,%eax
734	jae	1f
735
736	/* less than Pentium; must be 486 */
737	movl	$CPU_486,R(_cpu)
738	jmp	3f
7391:
740	/* a Pentium? */
741	cmpl	$5,%eax
742	jne	2f
743	movl	$CPU_586,R(_cpu)
744	jmp	3f
7452:
746	/* Greater than Pentium...call it a Pentium Pro */
747	movl	$CPU_686,R(_cpu)
7483:
749	ret
750
751
752/**********************************************************************
753 *
754 * Create the first page directory and its page tables.
755 *
756 */
757
758create_pagetables:
759
760	testl	$CPUID_PGE, R(_cpu_feature)
761	jz	1f
762	movl	%cr4, %eax
763	orl	$CR4_PGE, %eax
764	movl	%eax, %cr4
7651:
766
767/* Find end of kernel image (rounded up to a page boundary). */
768	movl	$R(_end),%esi
769
770/* include symbols in "kernel image" if they are loaded and useful */
771#ifdef DDB
772	movl	R(_bootinfo+BI_ESYMTAB),%edi
773	testl	%edi,%edi
774	je	over_symalloc
775	movl	%edi,%esi
776	movl	$KERNBASE,%edi
777	addl	%edi,R(_bootinfo+BI_SYMTAB)
778	addl	%edi,R(_bootinfo+BI_ESYMTAB)
779over_symalloc:
780#endif
781
782	addl	$PAGE_MASK,%esi
783	andl	$~PAGE_MASK,%esi
784	movl	%esi,R(_KERNend)	/* save end of kernel */
785	movl	%esi,R(physfree)	/* next free page is at end of kernel */
786
787/* Allocate Kernel Page Tables */
788	ALLOCPAGES(NKPT)
789	movl	%esi,R(_KPTphys)
790
791/* Allocate Page Table Directory */
792	ALLOCPAGES(1)
793	movl	%esi,R(_IdlePTD)
794
795/* Allocate UPAGES */
796	ALLOCPAGES(UPAGES)
797	movl	%esi,R(p0upa)
798	addl	$KERNBASE, %esi
799	movl	%esi, R(_proc0paddr)
800
801#ifdef SMP
802/* Allocate cpu0's private data page */
803	ALLOCPAGES(1)
804	movl	%esi,R(cpu0pp)
805	addl	$KERNBASE, %esi
806	movl	%esi, R(_cpu0prvpage)	/* relocated to KVM space */
807
808/* Allocate cpu0's private page table for mapping priv page, apic, etc */
809	ALLOCPAGES(1)
810	movl	%esi,R(cpu0pt)
811	addl	$KERNBASE, %esi
812	movl	%esi, R(_cpu0prvpt)	/* relocated to KVM space */
813#endif	/* SMP */
814
815/* Map read-only from zero to the end of the kernel text section */
816	xorl	%eax, %eax
817#ifdef BDE_DEBUGGER
818/* If the debugger is present, actually map everything read-write. */
819	cmpl	$0,R(_bdb_exists)
820	jne	map_read_write
821#endif
822	xorl	%edx,%edx
823	testl	$CPUID_PGE, R(_cpu_feature)
824	jz	2f
825	orl	$PG_G,%edx
826
8272:	movl	$R(_etext),%ecx
828	addl	$PAGE_MASK,%ecx
829	shrl	$PAGE_SHIFT,%ecx
830	fillkptphys(%edx)
831
832/* Map read-write, data, bss and symbols */
833	movl	$R(_etext),%eax
834	addl	$PAGE_MASK, %eax
835	andl	$~PAGE_MASK, %eax
836map_read_write:
837	movl	$PG_RW,%edx
838	testl	$CPUID_PGE, R(_cpu_feature)
839	jz	1f
840	orl	$PG_G,%edx
841
8421:	movl	R(_KERNend),%ecx
843	subl	%eax,%ecx
844	shrl	$PAGE_SHIFT,%ecx
845	fillkptphys(%edx)
846
847/* Map page directory. */
848	movl	R(_IdlePTD), %eax
849	movl	$1, %ecx
850	fillkptphys($PG_RW)
851
852/* Map proc0's UPAGES in the physical way ... */
853	movl	R(p0upa), %eax
854	movl	$UPAGES, %ecx
855	fillkptphys($PG_RW)
856
857/* Map ISA hole */
858	movl	$ISA_HOLE_START, %eax
859	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
860	fillkptphys($PG_RW)
861
862#ifdef SMP
863/* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */
864	movl	R(cpu0pp), %eax
865	movl	$1, %ecx
866	fillkptphys($PG_RW)
867
868/* Map cpu0's private page table into global kmem FWIW */
869	movl	R(cpu0pt), %eax
870	movl	$1, %ecx
871	fillkptphys($PG_RW)
872
873/* Map the private page into the private page table into private space */
874	movl	R(cpu0pp), %eax
875	movl	$0, %ebx		/* pte offset = 0 */
876	movl	$1, %ecx		/* one private page coming right up */
877	fillkpt(R(cpu0pt), $PG_RW)
878
879/* Map the page table page into private space */
880	movl	R(cpu0pt), %eax
881	movl	$1, %ebx		/* pte offset = 1 */
882	movl	$1, %ecx		/* one private pt coming right up */
883	fillkpt(R(cpu0pt), $PG_RW)
884
885/* ... and put the page table table in the pde. */
886	movl	R(cpu0pt), %eax
887	movl	$MPPTDI, %ebx
888	movl	$1, %ecx
889	fillkpt(R(_IdlePTD), $PG_RW)
890#endif	/* SMP */
891
892/* install a pde for temporary double map of bottom of VA */
893	movl	R(_KPTphys), %eax
894	xorl	%ebx, %ebx
895	movl	$1, %ecx
896	fillkpt(R(_IdlePTD), $PG_RW)
897
898/* install pde's for pt's */
899	movl	R(_KPTphys), %eax
900	movl	$KPTDI, %ebx
901	movl	$NKPT, %ecx
902	fillkpt(R(_IdlePTD), $PG_RW)
903
904/* install a pde recursively mapping page directory as a page table */
905	movl	R(_IdlePTD), %eax
906	movl	$PTDPTDI, %ebx
907	movl	$1,%ecx
908	fillkpt(R(_IdlePTD), $PG_RW)
909
910	ret
911
912#ifdef BDE_DEBUGGER
913bdb_prepare_paging:
914	cmpl	$0,R(_bdb_exists)
915	je	bdb_prepare_paging_exit
916
917	subl	$6,%esp
918
919	/*
920	 * Copy and convert debugger entries from the bootstrap gdt and idt
921	 * to the kernel gdt and idt.  Everything is still in low memory.
922	 * Tracing continues to work after paging is enabled because the
923	 * low memory addresses remain valid until everything is relocated.
924	 * However, tracing through the setidt() that initializes the trace
925	 * trap will crash.
926	 */
927	sgdt	(%esp)
928	movl	2(%esp),%esi		/* base address of bootstrap gdt */
929	movl	$R(_gdt),%edi
930	movl	%edi,2(%esp)		/* prepare to load kernel gdt */
931	movl	$8*18/4,%ecx
932	cld
933	rep				/* copy gdt */
934	movsl
935	movl	$R(_gdt),-8+2(%edi)	/* adjust gdt self-ptr */
936	movb	$0x92,-8+5(%edi)
937	lgdt	(%esp)
938
939	sidt	(%esp)
940	movl	2(%esp),%esi		/* base address of current idt */
941	movl	8+4(%esi),%eax		/* convert dbg descriptor to ... */
942	movw	8(%esi),%ax
943	movl	%eax,R(bdb_dbg_ljmp+1)	/* ... immediate offset ... */
944	movl	8+2(%esi),%eax
945	movw	%ax,R(bdb_dbg_ljmp+5)	/* ... and selector for ljmp */
946	movl	24+4(%esi),%eax		/* same for bpt descriptor */
947	movw	24(%esi),%ax
948	movl	%eax,R(bdb_bpt_ljmp+1)
949	movl	24+2(%esi),%eax
950	movw	%ax,R(bdb_bpt_ljmp+5)
951	movl	$R(_idt),%edi
952	movl	%edi,2(%esp)		/* prepare to load kernel idt */
953	movl	$8*4/4,%ecx
954	cld
955	rep				/* copy idt */
956	movsl
957	lidt	(%esp)
958
959	addl	$6,%esp
960
961bdb_prepare_paging_exit:
962	ret
963
964/* Relocate debugger gdt entries and gdt and idt pointers. */
965bdb_commit_paging:
966	cmpl	$0,_bdb_exists
967	je	bdb_commit_paging_exit
968
969	movl	$_gdt+8*9,%eax		/* adjust slots 9-17 */
970	movl	$9,%ecx
971reloc_gdt:
972	movb	$KERNBASE>>24,7(%eax)	/* top byte of base addresses, was 0, */
973	addl	$8,%eax			/* now KERNBASE>>24 */
974	loop	reloc_gdt
975
976	subl	$6,%esp
977	sgdt	(%esp)
978	addl	$KERNBASE,2(%esp)
979	lgdt	(%esp)
980	sidt	(%esp)
981	addl	$KERNBASE,2(%esp)
982	lidt	(%esp)
983	addl	$6,%esp
984
985	int	$3
986
987bdb_commit_paging_exit:
988	ret
989
990#endif /* BDE_DEBUGGER */
991