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