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