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