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