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