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