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