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