locore.s revision 18842
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 *	$Id: locore.s,v 1.74 1996/10/05 10:43:58 jkh 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 * Access to each processes kernel stack is via a region of
91 * per-process address space (at the beginning), immediately above
92 * the user process stack.
93 */
94	.set	_kstack,USRSTACK
95	.globl	_kstack
96
97/*
98 * Globals
99 */
100	.data
101	ALIGN_DATA		/* just to be sure */
102
103	.globl	tmpstk
104	.space	0x2000		/* space for tmpstk - temporary stack */
105tmpstk:
106
107	.globl	_boothowto,_bootdev
108
109	.globl	_cpu,_cpu_vendor,_cpu_id,_bootinfo
110	.globl	_cpu_high, _cpu_feature
111
112_cpu:	.long	0				/* are we 386, 386sx, or 486 */
113_cpu_id:	.long	0			/* stepping ID */
114_cpu_high:	.long	0			/* highest arg to CPUID */
115_cpu_feature:	.long	0			/* features */
116_cpu_vendor:	.space	20			/* CPU origin code */
117_bootinfo:	.space	BOOTINFO_SIZE		/* bootinfo that we can handle */
118
119_KERNend:	.long	0			/* phys addr end of kernel (just after bss) */
120physfree:	.long	0			/* phys addr of next free page */
121p0upa:	.long	0				/* phys addr of proc0's UPAGES */
122p0upt:	.long	0				/* phys addr of proc0's UPAGES page table */
123
124	.globl	_IdlePTD
125_IdlePTD:	.long	0			/* phys addr of kernel PTD */
126
127_KPTphys:	.long	0			/* phys addr of kernel page tables */
128
129	.globl	_proc0paddr
130_proc0paddr:	.long	0			/* address of proc 0 address space */
131
132#ifdef BDE_DEBUGGER
133	.globl	_bdb_exists			/* flag to indicate BDE debugger is present */
134_bdb_exists:	.long	0
135#endif
136
137
138/**********************************************************************
139 *
140 * Some handy macros
141 *
142 */
143
144#define R(foo) ((foo)-KERNBASE)
145
146#define ALLOCPAGES(foo) \
147	movl	R(physfree), %esi ; \
148	movl	$((foo)*PAGE_SIZE), %eax ; \
149	addl	%esi, %eax ; \
150	movl	%eax, R(physfree) ; \
151	movl	%esi, %edi ; \
152	movl	$((foo)*PAGE_SIZE),%ecx ; \
153	xorl	%eax,%eax ; \
154	cld ; \
155	rep ; \
156	stosb
157
158/*
159 * fillkpt
160 *	eax = page frame address
161 *	ebx = index into page table
162 *	ecx = how many pages to map
163 * 	base = base address of page dir/table
164 *	prot = protection bits
165 */
166#define	fillkpt(base, prot)		  \
167	shll	$2, %ebx		; \
168	addl	base, %ebx		; \
169	orl	$PG_V+prot, %eax	; \
1701:	movl	%eax,(%ebx)		; \
171	addl	$PAGE_SIZE,%eax		; /* increment physical address */ \
172	addl	$4,%ebx			; /* next pte */ \
173	loop	1b
174
175/*
176 * fillkptphys(prot)
177 *	eax = physical address
178 *	ecx = how many pages to map
179 *	prot = protection bits
180 */
181#define	fillkptphys(prot)		  \
182	movl	%eax, %ebx		; \
183	shrl	$PAGE_SHIFT, %ebx	; \
184	fillkpt(R(_KPTphys), prot)
185
186	.text
187/**********************************************************************
188 *
189 * This is where the bootblocks start us, set the ball rolling...
190 *
191 */
192NON_GPROF_ENTRY(btext)
193
194#ifdef BDE_DEBUGGER
195#ifdef BIOS_STEALS_3K
196	cmpl	$0x0375c339,0x95504
197#else
198	cmpl	$0x0375c339,0x96104	/* XXX - debugger signature */
199#endif
200	jne	1f
201	movb	$1,R(_bdb_exists)
2021:
203#endif
204
205/* Tell the bios to warmboot next time */
206	movw	$0x1234,0x472
207
208/* Set up a real frame in case the double return in newboot is executed. */
209	pushl	%ebp
210	movl	%esp, %ebp
211
212/* Don't trust what the BIOS gives for eflags. */
213	pushl	$PSL_KERNEL
214	popfl
215
216/*
217 * Don't trust what the BIOS gives for %fs and %gs.  Trust the bootstrap
218 * to set %cs, %ds, %es and %ss.
219 */
220	mov	%ds, %ax
221	mov	%ax, %fs
222	mov	%ax, %gs
223
224	call	recover_bootinfo
225
226/* Get onto a stack that we can trust. */
227/*
228 * XXX this step is delayed in case recover_bootinfo needs to return via
229 * the old stack, but it need not be, since recover_bootinfo actually
230 * returns via the old frame.
231 */
232	movl	$R(tmpstk),%esp
233
234	call	identify_cpu
235
236/* clear bss */
237/*
238 * XXX this should be done a little earlier.
239 *
240 * XXX we don't check that there is memory for our bss and page tables
241 * before using it.
242 *
243 * XXX the boot program somewhat bogusly clears the bss.  We still have
244 * to do it in case we were unzipped by kzipboot.  Then the boot program
245 * only clears kzipboot's bss.
246 *
247 * XXX the gdt and idt are still somewhere in the boot program.  We
248 * depend on the convention that the boot program is below 1MB and we
249 * are above 1MB to keep the gdt and idt  away from the bss and page
250 * tables.  The idt is only used if BDE_DEBUGGER is enabled.
251 */
252	movl	$R(_end),%ecx
253	movl	$R(_edata),%edi
254	subl	%edi,%ecx
255	xorl	%eax,%eax
256	cld
257	rep
258	stosb
259
260#if NAPM > 0
261/*
262 * XXX it's not clear that APM can live in the current environonment.
263 * Only pc-relative addressing works.
264 */
265	call	_apm_setup
266#endif
267
268	call	create_pagetables
269
270#ifdef BDE_DEBUGGER
271/*
272 * Adjust as much as possible for paging before enabling paging so that the
273 * adjustments can be traced.
274 */
275	call	bdb_prepare_paging
276#endif
277
278/* Now enable paging */
279	movl	R(_IdlePTD), %eax
280	movl	%eax,%cr3			/* load ptd addr into mmu */
281	movl	%cr0,%eax			/* get control word */
282	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
283	movl	%eax,%cr0			/* and let's page NOW! */
284
285#ifdef BDE_DEBUGGER
286/*
287 * Complete the adjustments for paging so that we can keep tracing through
288 * initi386() after the low (physical) addresses for the gdt and idt become
289 * invalid.
290 */
291	call	bdb_commit_paging
292#endif
293
294	pushl	$begin				/* jump to high virtualized address */
295	ret
296
297/* now running relocated at KERNBASE where the system is linked to run */
298begin:
299	/* set up bootstrap stack */
300	movl	$_kstack+UPAGES*PAGE_SIZE,%esp	/* bootstrap stack end location */
301	xorl	%eax,%eax			/* mark end of frames */
302	movl	%eax,%ebp
303	movl	_proc0paddr,%eax
304	movl	_IdlePTD, %esi
305	movl	%esi,PCB_CR3(%eax)
306
307	movl	physfree, %esi
308	pushl	%esi				/* value of first for init386(first) */
309	call	_init386			/* wire 386 chip for unix operation */
310	popl	%esi
311
312	.globl	__ucodesel,__udatasel
313
314	pushl	$0				/* unused */
315	pushl	__udatasel			/* ss */
316	pushl	$0				/* esp - filled in by execve() */
317	pushl	$PSL_USER			/* eflags (IOPL 0, int enab) */
318	pushl	__ucodesel			/* cs */
319	pushl	$0				/* eip - filled in by execve() */
320	subl	$(12*4),%esp			/* space for rest of registers */
321
322	pushl	%esp				/* call main with frame pointer */
323	call	_main				/* autoconfiguration, mountroot etc */
324
325	addl	$(13*4),%esp			/* back to a frame we can return with */
326
327	/*
328	 * now we've run main() and determined what cpu-type we are, we can
329	 * enable write protection and alignment checking on i486 cpus and
330	 * above.
331	 */
332#if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
333	cmpl    $CPUCLASS_386,_cpu_class
334	je	1f
335	movl	%cr0,%eax			/* get control word */
336	orl	$CR0_WP|CR0_AM,%eax		/* enable i486 features */
337	movl	%eax,%cr0			/* and do it */
3381:
339#endif
340	/*
341	 * on return from main(), we are process 1
342	 * set up address space and stack so that we can 'return' to user mode
343	 */
344	movl	__ucodesel,%eax
345	movl	__udatasel,%ecx
346
347	movl	%cx,%ds
348	movl	%cx,%es
349	movl	%ax,%fs				/* double map cs to fs */
350	movl	%cx,%gs				/* and ds to gs */
351	iret					/* goto user! */
352
353#define LCALL(x,y)	.byte 0x9a ; .long y ; .word x
354
355/*
356 * Signal trampoline, copied to top of user stack
357 */
358NON_GPROF_ENTRY(sigcode)
359	call	SIGF_HANDLER(%esp)
360	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
361						/* copy at 8(%esp)) */
362	pushl	%eax
363	pushl	%eax				/* junk to fake return address */
364	movl	$SYS_sigreturn,%eax		/* sigreturn() */
365	LCALL(0x7,0)				/* enter kernel with args on stack */
366	hlt					/* never gets here */
367	.align	2,0x90				/* long word text-align */
368_esigcode:
369
370	.data
371	.globl	_szsigcode
372_szsigcode:
373	.long	_esigcode-_sigcode
374	.text
375
376/**********************************************************************
377 *
378 * Recover the bootinfo passed to us from the boot program
379 *
380 */
381recover_bootinfo:
382	/*
383	 * This code is called in different ways depending on what loaded
384	 * and started the kernel.  This is used to detect how we get the
385	 * arguments from the other code and what we do with them.
386	 *
387	 * Old disk boot blocks:
388	 *	(*btext)(howto, bootdev, cyloffset, esym);
389	 *	[return address == 0, and can NOT be returned to]
390	 *	[cyloffset was not supported by the FreeBSD boot code
391	 *	 and always passed in as 0]
392	 *	[esym is also known as total in the boot code, and
393	 *	 was never properly supported by the FreeBSD boot code]
394	 *
395	 * Old diskless netboot code:
396	 *	(*btext)(0,0,0,0,&nfsdiskless,0,0,0);
397	 *	[return address != 0, and can NOT be returned to]
398	 *	If we are being booted by this code it will NOT work,
399	 *	so we are just going to halt if we find this case.
400	 *
401	 * New uniform boot code:
402	 *	(*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
403	 *	[return address != 0, and can be returned to]
404	 *
405	 * There may seem to be a lot of wasted arguments in here, but
406	 * that is so the newer boot code can still load very old kernels
407	 * and old boot code can load new kernels.
408	 */
409
410	/*
411	 * The old style disk boot blocks fake a frame on the stack and
412	 * did an lret to get here.  The frame on the stack has a return
413	 * address of 0.
414	 */
415	cmpl	$0,4(%ebp)
416	je	olddiskboot
417
418	/*
419	 * We have some form of return address, so this is either the
420	 * old diskless netboot code, or the new uniform code.  That can
421	 * be detected by looking at the 5th argument, if it is 0
422	 * we are being booted by the new uniform boot code.
423	 */
424	cmpl	$0,24(%ebp)
425	je	newboot
426
427	/*
428	 * Seems we have been loaded by the old diskless boot code, we
429	 * don't stand a chance of running as the diskless structure
430	 * changed considerably between the two, so just halt.
431	 */
432	 hlt
433
434	/*
435	 * We have been loaded by the new uniform boot code.
436	 * Let's check the bootinfo version, and if we do not understand
437	 * it we return to the loader with a status of 1 to indicate this error
438	 */
439newboot:
440	movl	28(%ebp),%ebx		/* &bootinfo.version */
441	movl	BI_VERSION(%ebx),%eax
442	cmpl	$1,%eax			/* We only understand version 1 */
443	je	1f
444	movl	$1,%eax			/* Return status */
445	leave
446	/*
447	 * XXX this returns to our caller's caller (as is required) since
448	 * we didn't set up a frame and our caller did.
449	 */
450	ret
451
4521:
453	/*
454	 * If we have a kernelname copy it in
455	 */
456	movl	BI_KERNELNAME(%ebx),%esi
457	cmpl	$0,%esi
458	je	2f			/* No kernelname */
459	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
460	movl	$R(_kernelname),%edi
461	cmpb	$'/',(%esi)		/* Make sure it starts with a slash */
462	je	1f
463	movb	$'/',(%edi)
464	incl	%edi
465	decl	%ecx
4661:
467	cld
468	rep
469	movsb
470
4712:
472	/*
473	 * Determine the size of the boot loader's copy of the bootinfo
474	 * struct.  This is impossible to do properly because old versions
475	 * of the struct don't contain a size field and there are 2 old
476	 * versions with the same version number.
477	 */
478	movl	$BI_ENDCOMMON,%ecx	/* prepare for sizeless version */
479	testl	$RB_BOOTINFO,8(%ebp)	/* bi_size (and bootinfo) valid? */
480	je	got_bi_size		/* no, sizeless version */
481	movl	BI_SIZE(%ebx),%ecx
482got_bi_size:
483
484	/*
485	 * Copy the common part of the bootinfo struct
486	 */
487	movl	%ebx,%esi
488	movl	$R(_bootinfo),%edi
489	cmpl	$BOOTINFO_SIZE,%ecx
490	jbe	got_common_bi_size
491	movl	$BOOTINFO_SIZE,%ecx
492got_common_bi_size:
493	cld
494	rep
495	movsb
496
497#ifdef NFS
498	/*
499	 * If we have a nfs_diskless structure copy it in
500	 */
501	movl	BI_NFS_DISKLESS(%ebx),%esi
502	cmpl	$0,%esi
503	je	olddiskboot
504	movl	$R(_nfs_diskless),%edi
505	movl	$NFSDISKLESS_SIZE,%ecx
506	cld
507	rep
508	movsb
509	movl	$R(_nfs_diskless_valid),%edi
510	movl	$1,(%edi)
511#endif
512
513	/*
514	 * The old style disk boot.
515	 *	(*btext)(howto, bootdev, cyloffset, esym);
516	 * Note that the newer boot code just falls into here to pick
517	 * up howto and bootdev, cyloffset and esym are no longer used
518	 */
519olddiskboot:
520	movl	8(%ebp),%eax
521	movl	%eax,R(_boothowto)
522	movl	12(%ebp),%eax
523	movl	%eax,R(_bootdev)
524
525#if defined(USERCONFIG_BOOT) && defined(USERCONFIG)
526	movl	$0x10200, %esi
527	movl	$R(_userconfig_from_boot),%edi
528	movl	$512,%ecx
529	cld
530	rep
531	movsb
532#endif /* USERCONFIG_BOOT */
533
534	ret
535
536
537/**********************************************************************
538 *
539 * Identify the CPU and initialize anything special about it
540 *
541 */
542identify_cpu:
543
544	/* Try to toggle alignment check flag; does not exist on 386. */
545	pushfl
546	popl	%eax
547	movl	%eax,%ecx
548	orl	$PSL_AC,%eax
549	pushl	%eax
550	popfl
551	pushfl
552	popl	%eax
553	xorl	%ecx,%eax
554	andl	$PSL_AC,%eax
555	pushl	%ecx
556	popfl
557
558	testl	%eax,%eax
559	jnz	1f
560	movl	$CPU_386,R(_cpu)
561	jmp	3f
562
5631:	/* Try to toggle identification flag; does not exist on early 486s. */
564	pushfl
565	popl	%eax
566	movl	%eax,%ecx
567	xorl	$PSL_ID,%eax
568	pushl	%eax
569	popfl
570	pushfl
571	popl	%eax
572	xorl	%ecx,%eax
573	andl	$PSL_ID,%eax
574	pushl	%ecx
575	popfl
576
577	testl	%eax,%eax
578	jnz	1f
579	movl	$CPU_486,R(_cpu)
580
581	/* check for Cyrix 486DLC -- based on check routine  */
582	/* documented in "Cx486SLC/e SMM Programmer's Guide" */
583	xorw	%dx,%dx
584	cmpw	%dx,%dx			# set flags to known state
585	pushfw
586	popw	%cx			# store flags in ecx
587	movw	$0xffff,%ax
588	movw	$0x0004,%bx
589	divw	%bx
590	pushfw
591	popw	%ax
592	andw	$0x08d5,%ax		# mask off important bits
593	andw	$0x08d5,%cx
594	cmpw	%ax,%cx
595
596	jnz	3f			# if flags changed, Intel chip
597
598	movl	$CPU_486DLC,R(_cpu) # set CPU value for Cyrix
599	movl	$0x69727943,R(_cpu_vendor)	# store vendor string
600	movw	$0x0078,R(_cpu_vendor+4)
601
602#ifndef CYRIX_CACHE_WORKS
603	/* Disable caching of the ISA hole only. */
604	invd
605	movb	$CCR0,%al		# Configuration Register index (CCR0)
606	outb	%al,$0x22
607	inb	$0x23,%al
608	orb	$(CCR0_NC1|CCR0_BARB),%al
609	movb	%al,%ah
610	movb	$CCR0,%al
611	outb	%al,$0x22
612	movb	%ah,%al
613	outb	%al,$0x23
614	invd
615#else /* CYRIX_CACHE_WORKS */
616	/* Set cache parameters */
617	invd				# Start with guaranteed clean cache
618	movb	$CCR0,%al		# Configuration Register index (CCR0)
619	outb	%al,$0x22
620	inb	$0x23,%al
621	andb	$~CCR0_NC0,%al
622#ifndef CYRIX_CACHE_REALLY_WORKS
623	orb	$(CCR0_NC1|CCR0_BARB),%al
624#else /* CYRIX_CACHE_REALLY_WORKS */
625	orb	$CCR0_NC1,%al
626#endif /* !CYRIX_CACHE_REALLY_WORKS */
627	movb	%al,%ah
628	movb	$CCR0,%al
629	outb	%al,$0x22
630	movb	%ah,%al
631	outb	%al,$0x23
632	/* clear non-cacheable region 1	*/
633	movb	$(NCR1+2),%al
634	outb	%al,$0x22
635	movb	$NCR_SIZE_0K,%al
636	outb	%al,$0x23
637	/* clear non-cacheable region 2	*/
638	movb	$(NCR2+2),%al
639	outb	%al,$0x22
640	movb	$NCR_SIZE_0K,%al
641	outb	%al,$0x23
642	/* clear non-cacheable region 3	*/
643	movb	$(NCR3+2),%al
644	outb	%al,$0x22
645	movb	$NCR_SIZE_0K,%al
646	outb	%al,$0x23
647	/* clear non-cacheable region 4	*/
648	movb	$(NCR4+2),%al
649	outb	%al,$0x22
650	movb	$NCR_SIZE_0K,%al
651	outb	%al,$0x23
652	/* enable caching in CR0 */
653	movl	%cr0,%eax
654	andl	$~(CR0_CD|CR0_NW),%eax
655	movl	%eax,%cr0
656	invd
657#endif /* !CYRIX_CACHE_WORKS */
658	jmp	3f
659
6601:	/* Use the `cpuid' instruction. */
661	xorl	%eax,%eax
662	.byte	0x0f,0xa2			# cpuid 0
663	movl	%eax,R(_cpu_high)		# highest capability
664	movl	%ebx,R(_cpu_vendor)		# store vendor string
665	movl	%edx,R(_cpu_vendor+4)
666	movl	%ecx,R(_cpu_vendor+8)
667	movb	$0,R(_cpu_vendor+12)
668
669	movl	$1,%eax
670	.byte	0x0f,0xa2			# cpuid 1
671	movl	%eax,R(_cpu_id)			# store cpu_id
672	movl	%edx,R(_cpu_feature)		# store cpu_feature
673	rorl	$8,%eax				# extract family type
674	andl	$15,%eax
675	cmpl	$5,%eax
676	jae	1f
677
678	/* less than Pentium; must be 486 */
679	movl	$CPU_486,R(_cpu)
680	jmp	3f
6811:
682	/* a Pentium? */
683	cmpl	$5,%eax
684	jne	2f
685	movl	$CPU_586,R(_cpu)
686	jmp	3f
6872:
688	/* Greater than Pentium...call it a Pentium Pro */
689	movl	$CPU_686,R(_cpu)
6903:
691	ret
692
693
694/**********************************************************************
695 *
696 * Create the first page directory and its page tables.
697 *
698 */
699
700create_pagetables:
701
702/* Find end of kernel image (rounded up to a page boundary). */
703	movl	$R(_end),%esi
704
705/* include symbols in "kernel image" if they are loaded and useful */
706#ifdef DDB
707	movl	R(_bootinfo+BI_ESYMTAB),%edi
708	testl	%edi,%edi
709	je	over_symalloc
710	movl	%edi,%esi
711	movl	$KERNBASE,%edi
712	addl	%edi,R(_bootinfo+BI_SYMTAB)
713	addl	%edi,R(_bootinfo+BI_ESYMTAB)
714over_symalloc:
715#endif
716
717	addl	$PAGE_MASK,%esi
718	andl	$~PAGE_MASK,%esi
719	movl	%esi,R(_KERNend)	/* save end of kernel */
720	movl	%esi,R(physfree)	/* next free page is at end of kernel */
721
722/* Allocate Kernel Page Tables */
723	ALLOCPAGES(NKPT)
724	movl	%esi,R(_KPTphys)
725
726/* Allocate Page Table Directory */
727	ALLOCPAGES(1)
728	movl	%esi,R(_IdlePTD)
729
730/* Allocate UPAGES */
731	ALLOCPAGES(UPAGES)
732	movl	%esi,R(p0upa)
733	addl	$KERNBASE, %esi
734	movl	%esi, R(_proc0paddr)
735
736/* Allocate proc0's page table for the UPAGES. */
737	ALLOCPAGES(1)
738	movl	%esi,R(p0upt)
739
740/* Map read-only from zero to the end of the kernel text section */
741	xorl	%eax, %eax
742#ifdef BDE_DEBUGGER
743/* If the debugger is present, actually map everything read-write. */
744	cmpl	$0,R(_bdb_exists)
745	jne	map_read_write
746#endif
747	movl	$R(_etext),%ecx
748	addl	$PAGE_MASK,%ecx
749	shrl	$PAGE_SHIFT,%ecx
750	fillkptphys(0)
751
752/* Map read-write, data, bss and symbols */
753	movl	$R(_etext),%eax
754	addl	$PAGE_MASK, %eax
755	andl	$~PAGE_MASK, %eax
756map_read_write:
757	movl	R(_KERNend),%ecx
758	subl	%eax,%ecx
759	shrl	$PAGE_SHIFT,%ecx
760	fillkptphys(PG_RW)
761
762/* Map page directory. */
763	movl	R(_IdlePTD), %eax
764	movl	$1, %ecx
765	fillkptphys(PG_RW)
766
767/* Map proc0's page table for the UPAGES. */
768	movl	R(p0upt), %eax
769	movl	$1, %ecx
770	fillkptphys(PG_RW)
771
772/* Map proc0's UPAGES in the physical way ... */
773	movl	R(p0upa), %eax
774	movl	$UPAGES, %ecx
775	fillkptphys(PG_RW)
776
777/* Map ISA hole */
778	movl	$ISA_HOLE_START, %eax
779	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
780	fillkptphys(PG_RW|PG_N)
781
782/* Map proc0s UPAGES in the special page table for this purpose ... */
783	movl	R(p0upa), %eax
784	movl	$KSTKPTEOFF, %ebx
785	movl	$UPAGES, %ecx
786	fillkpt(R(p0upt), PG_RW)
787
788/* ... and put the page table in the pde. */
789	movl	R(p0upt), %eax
790	movl	$KSTKPTDI, %ebx
791	movl	$1, %ecx
792	fillkpt(R(_IdlePTD), PG_RW)
793
794/* install a pde for temporary double map of bottom of VA */
795	movl	R(_KPTphys), %eax
796	xorl	%ebx, %ebx
797	movl	$1, %ecx
798	fillkpt(R(_IdlePTD), PG_RW)
799
800/* install pde's for pt's */
801	movl	R(_KPTphys), %eax
802	movl	$KPTDI, %ebx
803	movl	$NKPT, %ecx
804	fillkpt(R(_IdlePTD), PG_RW)
805
806/* install a pde recursively mapping page directory as a page table */
807	movl	R(_IdlePTD), %eax
808	movl	$PTDPTDI, %ebx
809	movl	$1,%ecx
810	fillkpt(R(_IdlePTD), PG_RW)
811
812	ret
813
814#ifdef BDE_DEBUGGER
815bdb_prepare_paging:
816	cmpl	$0,R(_bdb_exists)
817	je	bdb_prepare_paging_exit
818
819	subl	$6,%esp
820
821	/*
822	 * Copy and convert debugger entries from the bootstrap gdt and idt
823	 * to the kernel gdt and idt.  Everything is still in low memory.
824	 * Tracing continues to work after paging is enabled because the
825	 * low memory addresses remain valid until everything is relocated.
826	 * However, tracing through the setidt() that initializes the trace
827	 * trap will crash.
828	 */
829	sgdt	(%esp)
830	movl	2(%esp),%esi		/* base address of bootstrap gdt */
831	movl	$R(_gdt),%edi
832	movl	%edi,2(%esp)		/* prepare to load kernel gdt */
833	movl	$8*18/4,%ecx
834	cld
835	rep				/* copy gdt */
836	movsl
837	movl	$R(_gdt),-8+2(%edi)	/* adjust gdt self-ptr */
838	movb	$0x92,-8+5(%edi)
839	lgdt	(%esp)
840
841	sidt	(%esp)
842	movl	2(%esp),%esi		/* base address of current idt */
843	movl	8+4(%esi),%eax		/* convert dbg descriptor to ... */
844	movw	8(%esi),%ax
845	movl	%eax,R(bdb_dbg_ljmp+1)	/* ... immediate offset ... */
846	movl	8+2(%esi),%eax
847	movw	%ax,R(bdb_dbg_ljmp+5)	/* ... and selector for ljmp */
848	movl	24+4(%esi),%eax		/* same for bpt descriptor */
849	movw	24(%esi),%ax
850	movl	%eax,R(bdb_bpt_ljmp+1)
851	movl	24+2(%esi),%eax
852	movw	%ax,R(bdb_bpt_ljmp+5)
853	movl	$R(_idt),%edi
854	movl	%edi,2(%esp)		/* prepare to load kernel idt */
855	movl	$8*4/4,%ecx
856	cld
857	rep				/* copy idt */
858	movsl
859	lidt	(%esp)
860
861	addl	$6,%esp
862
863bdb_prepare_paging_exit:
864	ret
865
866/* Relocate debugger gdt entries and gdt and idt pointers. */
867bdb_commit_paging:
868	cmpl	$0,_bdb_exists
869	je	bdb_commit_paging_exit
870
871	movl	$_gdt+8*9,%eax		/* adjust slots 9-17 */
872	movl	$9,%ecx
873reloc_gdt:
874	movb	$KERNBASE>>24,7(%eax)	/* top byte of base addresses, was 0, */
875	addl	$8,%eax			/* now KERNBASE>>24 */
876	loop	reloc_gdt
877
878	subl	$6,%esp
879	sgdt	(%esp)
880	addl	$KERNBASE,2(%esp)
881	lgdt	(%esp)
882	sidt	(%esp)
883	addl	$KERNBASE,2(%esp)
884	lidt	(%esp)
885	addl	$6,%esp
886
887	int	$3
888
889bdb_commit_paging_exit:
890	ret
891
892#endif /* BDE_DEBUGGER */
893