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