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