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