locore.s revision 29655
1113094Sphk/*-
2135482Sphk * Copyright (c) 1990 The Regents of the University of California.
3113094Sphk * All rights reserved.
4113094Sphk *
5113094Sphk * This code is derived from software contributed to Berkeley by
6113094Sphk * William Jolitz.
7113094Sphk *
8113094Sphk * Redistribution and use in source and binary forms, with or without
9113094Sphk * modification, are permitted provided that the following conditions
10113094Sphk * are met:
11113094Sphk * 1. Redistributions of source code must retain the above copyright
12113094Sphk *    notice, this list of conditions and the following disclaimer.
13113094Sphk * 2. Redistributions in binary form must reproduce the above copyright
14113094Sphk *    notice, this list of conditions and the following disclaimer in the
15113094Sphk *    documentation and/or other materials provided with the distribution.
16113094Sphk * 3. All advertising materials mentioning features or use of this software
17113094Sphk *    must display the following acknowledgement:
18113094Sphk *	This product includes software developed by the University of
19113094Sphk *	California, Berkeley and its contributors.
20113094Sphk * 4. Neither the name of the University nor the names of its contributors
21113094Sphk *    may be used to endorse or promote products derived from this software
22113094Sphk *    without specific prior written permission.
23113094Sphk *
24113094Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25113094Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26113094Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27113094Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28135482Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29135482Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30135482Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31135482Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32135482Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33135482Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34135482Sphk * SUCH DAMAGE.
35135482Sphk *
36135482Sphk *	from: @(#)locore.s	7.3 (Berkeley) 5/13/91
37135482Sphk *	$Id: locore.s,v 1.97 1997/09/07 22:03:46 fsmp Exp $
38135482Sphk *
39135482Sphk *		originally from: locore.s, by William F. Jolitz
40135482Sphk *
41135482Sphk *		Substantially rewritten by David Greenman, Rod Grimes,
42113094Sphk *			Bruce Evans, Wolfgang Solfrank, Poul-Henning Kamp
43113094Sphk *			and many others.
44135482Sphk */
45119418Sobrien
46119418Sobrien#include "apm.h"
47119418Sobrien#include "opt_cpu.h"
48113094Sphk#include "opt_ddb.h"
49113094Sphk#include "opt_userconfig.h"
50113094Sphk
51113094Sphk#include <sys/syscall.h>
52130026Sphk#include <sys/reboot.h>
53113270Sphk
54113094Sphk#include <machine/asmacros.h>
55113094Sphk#include <machine/cputypes.h>
56113094Sphk#include <machine/psl.h>
57113094Sphk#include <machine/pmap.h>
58113094Sphk#include <machine/specialreg.h>
59119274Simp
60119274Simp#include "assym.s"
61113094Sphk
62113094Sphk/*
63113094Sphk *	XXX
64113094Sphk *
65113270Sphk * Note: This version greatly munged to avoid various assembler errors
66113270Sphk * that may be fixed in newer versions of gas. Perhaps newer versions
67113270Sphk * will have more pleasant appearance.
68113270Sphk */
69135482Sphk
70135482Sphk/*
71135482Sphk * PTmap is recursive pagemap at top of virtual address space.
72135482Sphk * Within PTmap, the page directory can be found (third indirection).
73135482Sphk */
74135482Sphk	.globl	_PTmap,_PTD,_PTDpde
75113270Sphk	.set	_PTmap,(PTDPTDI << PDRSHIFT)
76135482Sphk	.set	_PTD,_PTmap + (PTDPTDI * PAGE_SIZE)
77135482Sphk	.set	_PTDpde,_PTD + (PTDPTDI * PDESIZE)
78135482Sphk
79135482Sphk/*
80135482Sphk * APTmap, APTD is the alternate recursive pagemap.
81135482Sphk * It's used when modifying another process's page tables.
82135482Sphk */
83135482Sphk	.globl	_APTmap,_APTD,_APTDpde
84143844Sphk	.set	_APTmap,APTDPTDI << PDRSHIFT
85135482Sphk	.set	_APTD,_APTmap + (APTDPTDI * PAGE_SIZE)
86135482Sphk	.set	_APTDpde,_PTD + (APTDPTDI * PDESIZE)
87135482Sphk
88135482Sphk#ifdef SMP
89113270Sphk	.globl	_SMP_prvstart
90113270Sphk	.set	_SMP_prvstart,(MPPTDI << PDRSHIFT)
91143844Sphk
92113270Sphk	.globl	_SMP_prvpage,_SMP_prvpt,_lapic,_SMP_ioapic
93113270Sphk	.globl	_prv_CPAGE1,_prv_CPAGE2,_prv_CPAGE3
94113270Sphk	.globl	_idlestack,_idlestack_top
95135482Sphk	.set	_SMP_prvpage,_SMP_prvstart
96143844Sphk	.set	_SMP_prvpt,_SMP_prvstart + PAGE_SIZE
97135482Sphk	.set	_lapic,_SMP_prvstart + (2 * PAGE_SIZE)
98135482Sphk	.set	_idlestack,_SMP_prvstart + (3 * PAGE_SIZE)
99135482Sphk	.set	_idlestack_top,_SMP_prvstart + (5 * PAGE_SIZE)
100113094Sphk	.set	_prv_CPAGE1,_SMP_prvstart + (5 * PAGE_SIZE)
101113094Sphk	.set	_prv_CPAGE2,_SMP_prvstart + (6 * PAGE_SIZE)
102113094Sphk	.set	_prv_CPAGE3,_SMP_prvstart + (7 * PAGE_SIZE)
103113094Sphk	.set	_SMP_ioapic,_SMP_prvstart + (16 * PAGE_SIZE)
104113094Sphk
105150526Sphk	.globl	_cpuid,_curproc,_curpcb,_npxproc,_runtime,_cpu_lockid
106135482Sphk	.globl	_common_tss,_other_cpus,_my_idlePTD,_ss_tpr
107113270Sphk	.globl	_prv_CMAP1,_prv_CMAP2,_prv_CMAP3
108135482Sphk	.globl	_inside_intr
109135482Sphk	.set	_cpuid,_SMP_prvpage+0		/* [0] */
110135482Sphk	.set	_curproc,_SMP_prvpage+4		/* [1] */
111135482Sphk	.set	_curpcb,_SMP_prvpage+8		/* [2] */
112135482Sphk	.set	_npxproc,_SMP_prvpage+12	/* [3] */
113143844Sphk	.set	_runtime,_SMP_prvpage+16	/* [4,5] */
114135482Sphk	.set	_cpu_lockid,_SMP_prvpage+24	/* [6] */
115113094Sphk	.set	_other_cpus,_SMP_prvpage+28	/* [7] bitmap of available CPUs,
116135482Sphk						    excluding ourself */
117135482Sphk	.set	_my_idlePTD,_SMP_prvpage+32	/* [8] */
118166901Spiso	.set	_ss_tpr,_SMP_prvpage+36		/* [9] */
119113270Sphk	.set	_prv_CMAP1,_SMP_prvpage+40	/* [10] */
120135482Sphk	.set	_prv_CMAP2,_SMP_prvpage+44	/* [11] */
121135482Sphk	.set	_prv_CMAP3,_SMP_prvpage+48	/* [12] */
122171717Skib	.set	_inside_intr,_SMP_prvpage+52	/* [13] */
123135482Sphk	.set	_common_tss,_SMP_prvpage+56	/* 102 (ie: 104) bytes long */
124135482Sphk
125135482Sphk/* Fetch the .set's for the local apic */
126135482Sphk#include "i386/i386/mp_apicdefs.s"
127113094Sphk
128166901Spiso#endif
129135482Sphk
130135482Sphk/*
131135482Sphk * Globals
132135482Sphk */
133135482Sphk	.data
134113270Sphk	ALIGN_DATA		/* just to be sure */
135135482Sphk
136150526Sphk	.globl	HIDENAME(tmpstk)
137135482Sphk	.space	0x2000		/* space for tmpstk - temporary stack */
138166901SpisoHIDENAME(tmpstk):
139150526Sphk
140113270Sphk	.globl	_boothowto,_bootdev
141143844Sphk
142135482Sphk	.globl	_cpu,_cpu_vendor,_cpu_id,_bootinfo
143143844Sphk	.globl	_cpu_high, _cpu_feature
144113270Sphk
145150526Sphk_cpu:	.long	0				/* are we 386, 386sx, or 486 */
146135482Sphk_cpu_id:	.long	0			/* stepping ID */
147135482Sphk_cpu_high:	.long	0			/* highest arg to CPUID */
148113094Sphk_cpu_feature:	.long	0			/* features */
149135482Sphk_cpu_vendor:	.space	20			/* CPU origin code */
150135482Sphk_bootinfo:	.space	BOOTINFO_SIZE		/* bootinfo that we can handle */
151166901Spiso
152113270Sphk_KERNend:	.long	0			/* phys addr end of kernel (just after bss) */
153113270Sphkphysfree:	.long	0			/* phys addr of next free page */
154135482Sphk
155135482Sphk#ifdef SMP
156143844Sphkcpu0pp:		.long	0			/* phys addr cpu0 private pg */
157150526Sphkcpu0pt:		.long	0			/* phys addr cpu0 private pt */
158150526Sphk
159113270Sphk		.globl	_cpu0prvpage,_cpu0prvpt
160166901Spiso_cpu0prvpage:	.long	0			/* relocated version */
161113270Sphk_cpu0prvpt:	.long	0			/* relocated version */
162113270Sphk#endif /* SMP */
163113094Sphk
164201223Srnoland	.globl	_IdlePTD
165201223Srnoland_IdlePTD:	.long	0			/* phys addr of kernel PTD */
166113270Sphk
167135482Sphk#ifdef SMP
168135482Sphk	.globl	_KPTphys
169135482Sphk#endif
170135482Sphk_KPTphys:	.long	0			/* phys addr of kernel page tables */
171113270Sphk
172135482Sphk	.globl	_proc0paddr
173135482Sphk_proc0paddr:	.long	0			/* address of proc 0 address space */
174135482Sphkp0upa:		.long	0			/* phys addr of proc0's UPAGES */
175135482Sphk
176135482Sphk#ifdef BDE_DEBUGGER
177135482Sphk	.globl	_bdb_exists			/* flag to indicate BDE debugger is present */
178113270Sphk_bdb_exists:	.long	0
179135482Sphk#endif
180135482Sphk
181135482Sphk
182135482Sphk/**********************************************************************
183135482Sphk *
184135482Sphk * Some handy macros
185113270Sphk *
186135482Sphk */
187135482Sphk
188113270Sphk#define R(foo) ((foo)-KERNBASE)
189135482Sphk
190113270Sphk#define ALLOCPAGES(foo) \
191113270Sphk	movl	R(physfree), %esi ; \
192113270Sphk	movl	$((foo)*PAGE_SIZE), %eax ; \
193135482Sphk	addl	%esi, %eax ; \
194113094Sphk	movl	%eax, R(physfree) ; \
195113094Sphk	movl	%esi, %edi ; \
196113270Sphk	movl	$((foo)*PAGE_SIZE),%ecx ; \
197135482Sphk	xorl	%eax,%eax ; \
198135482Sphk	cld ; \
199143844Sphk	rep ; \
200135482Sphk	stosb
201113094Sphk
202135482Sphk/*
203135482Sphk * fillkpt
204135482Sphk *	eax = page frame address
205135482Sphk *	ebx = index into page table
206135482Sphk *	ecx = how many pages to map
207135482Sphk * 	base = base address of page dir/table
208135482Sphk *	prot = protection bits
209135482Sphk */
210135482Sphk#define	fillkpt(base, prot)		  \
211135482Sphk	shll	$2,%ebx			; \
212135482Sphk	addl	base,%ebx		; \
213135482Sphk	orl	$PG_V,%eax		; \
214135482Sphk	orl	prot,%eax		; \
215135482Sphk1:	movl	%eax,(%ebx)		; \
216135482Sphk	addl	$PAGE_SIZE,%eax		; /* increment physical address */ \
217135482Sphk	addl	$4,%ebx			; /* next pte */ \
218135482Sphk	loop	1b
219135482Sphk
220135482Sphk/*
221135482Sphk * fillkptphys(prot)
222135482Sphk *	eax = physical address
223135482Sphk *	ecx = how many pages to map
224135482Sphk *	prot = protection bits
225135482Sphk */
226135482Sphk#define	fillkptphys(prot)		  \
227135482Sphk	movl	%eax, %ebx		; \
228135482Sphk	shrl	$PAGE_SHIFT, %ebx	; \
229135482Sphk	fillkpt(R(_KPTphys), prot)
230135482Sphk
231135482Sphk	.text
232135482Sphk/**********************************************************************
233135482Sphk *
234135482Sphk * This is where the bootblocks start us, set the ball rolling...
235135482Sphk *
236135482Sphk */
237135482SphkNON_GPROF_ENTRY(btext)
238135482Sphk
239135482Sphk#ifdef PC98
240135482Sphk	jmp	1f
241113270Sphk	.globl	_pc98_system_parameter
242135482Sphk	.org	0x400
243143844Sphk_pc98_system_parameter:
244135482Sphk	.space	0x240		/* BIOS parameter block */
245135482Sphk1:
246135482Sphk	/* save SYSTEM PARAMETER for resume (NS/T or other) */
247143844Sphk	movl	$0xa1000,%esi
248143844Sphk	movl	$0x100000,%edi
249143844Sphk	movl	$0x0630,%ecx
250135482Sphk	cld
251135482Sphk	rep
252135482Sphk	movsb
253135482Sphk#else	/* IBM-PC */
254143844Sphk#ifdef BDE_DEBUGGER
255143844Sphk#ifdef BIOS_STEALS_3K
256143844Sphk	cmpl	$0x0375c339,0x95504
257135482Sphk#else
258135482Sphk	cmpl	$0x0375c339,0x96104	/* XXX - debugger signature */
259135482Sphk#endif
260135482Sphk	jne	1f
261135482Sphk	movb	$1,R(_bdb_exists)
262135482Sphk1:
263135482Sphk#endif
264135482Sphk
265135482Sphk/* Tell the bios to warmboot next time */
266135482Sphk	movw	$0x1234,0x472
267135482Sphk#endif	/* PC98 */
268135482Sphk
269135482Sphk/* Set up a real frame in case the double return in newboot is executed. */
270113094Sphk	pushl	%ebp
271135482Sphk	movl	%esp, %ebp
272135482Sphk
273135482Sphk/* Don't trust what the BIOS gives for eflags. */
274143844Sphk	pushl	$PSL_KERNEL
275135482Sphk	popfl
276135482Sphk
277113270Sphk/*
278135482Sphk * Don't trust what the BIOS gives for %fs and %gs.  Trust the bootstrap
279150526Sphk * to set %cs, %ds, %es and %ss.
280113270Sphk */
281135482Sphk	mov	%ds, %ax
282150526Sphk	mov	%ax, %fs
283113270Sphk	mov	%ax, %gs
284135482Sphk
285150526Sphk	call	recover_bootinfo
286113270Sphk
287135482Sphk/* Get onto a stack that we can trust. */
288150526Sphk/*
289113270Sphk * XXX this step is delayed in case recover_bootinfo needs to return via
290135482Sphk * the old stack, but it need not be, since recover_bootinfo actually
291150526Sphk * returns via the old frame.
292113270Sphk */
293135482Sphk	movl	$R(HIDENAME(tmpstk)),%esp
294150526Sphk
295113270Sphk#ifdef PC98
296135482Sphk	testb	$0x02,0x100620		/* pc98_machine_type & M_EPSON_PC98 */
297150526Sphk	jz	3f
298113270Sphk	cmpb	$0x0b,0x100624		/* epson_machine_id <= 0x0b */
299135482Sphk	ja	3f
300150526Sphk
301113270Sphk	/* count up memory */
302135482Sphk	movl	$0x100000,%eax		/* next, talley remaining memory */
303135482Sphk	movl	$0xFFF-0x100,%ecx
304113094Sphk1:	movl	0(%eax),%ebx		/* save location to check */
305135482Sphk	movl	$0xa55a5aa5,0(%eax)	/* write test pattern */
306135482Sphk	cmpl	$0xa55a5aa5,0(%eax)	/* does not check yet for rollover */
307143844Sphk	jne	2f
308150526Sphk	movl	%ebx,0(%eax)		/* restore memory */
309150526Sphk	addl	$PAGE_SIZE,%eax
310150526Sphk	loop	1b
311150526Sphk2:	subl	$0x100000,%eax
312113094Sphk	shrl	$17,%eax
313135482Sphk	movb	%al,0x100401
314150526Sphk3:
315113094Sphk#endif
316135482Sphk
317135482Sphk	call	identify_cpu
318135482Sphk
319135482Sphk/* clear bss */
320135482Sphk/*
321143844Sphk * XXX this should be done a little earlier.
322135482Sphk *
323135482Sphk * XXX we don't check that there is memory for our bss and page tables
324135482Sphk * before using it.
325135482Sphk *
326135482Sphk * XXX the boot program somewhat bogusly clears the bss.  We still have
327135482Sphk * to do it in case we were unzipped by kzipboot.  Then the boot program
328135482Sphk * only clears kzipboot's bss.
329135482Sphk *
330135482Sphk * XXX the gdt and idt are still somewhere in the boot program.  We
331135482Sphk * depend on the convention that the boot program is below 1MB and we
332135482Sphk * are above 1MB to keep the gdt and idt  away from the bss and page
333143844Sphk * tables.  The idt is only used if BDE_DEBUGGER is enabled.
334135482Sphk */
335135482Sphk	movl	$R(_end),%ecx
336135482Sphk	movl	$R(_edata),%edi
337135482Sphk	subl	%edi,%ecx
338135482Sphk	xorl	%eax,%eax
339135482Sphk	cld
340135482Sphk	rep
341113270Sphk	stosb
342135482Sphk
343113270Sphk#if NAPM > 0
344113270Sphk/*
345113094Sphk * XXX it's not clear that APM can live in the current environonment.
346113094Sphk * Only pc-relative addressing works.
347113094Sphk */
348113094Sphk	call	_apm_setup
349113094Sphk#endif
350113094Sphk
351113094Sphk	call	create_pagetables
352113094Sphk
353113094Sphk#ifdef VM86
354143168Simp/*
355113094Sphk * If the CPU has support for VME, turn it on.
356113094Sphk */
357150526Sphk	testl	$CPUID_VME, R(_cpu_feature)
358150526Sphk	jz	1f
359150526Sphk	movl	%cr4, %eax
360150526Sphk	orl	$CR4_VME, %eax
361150526Sphk	movl	%eax, %cr4
362150526Sphk1:
363150526Sphk#endif /* VM86 */
364113094Sphk
365113094Sphk#ifdef BDE_DEBUGGER
366113094Sphk/*
367113094Sphk * Adjust as much as possible for paging before enabling paging so that the
368150526Sphk * adjustments can be traced.
369113094Sphk */
370113094Sphk	call	bdb_prepare_paging
371113094Sphk#endif
372113094Sphk
373113094Sphk/* Now enable paging */
374150526Sphk	movl	R(_IdlePTD), %eax
375150526Sphk	movl	%eax,%cr3			/* load ptd addr into mmu */
376150526Sphk	movl	%cr0,%eax			/* get control word */
377113094Sphk	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
378217068Sjhb	movl	%eax,%cr0			/* and let's page NOW! */
379166901Spiso
380113270Sphk#ifdef BDE_DEBUGGER
381113270Sphk/*
382150526Sphk * Complete the adjustments for paging so that we can keep tracing through
383135482Sphk * initi386() after the low (physical) addresses for the gdt and idt become
384166901Spiso * invalid.
385113270Sphk */
386113094Sphk	call	bdb_commit_paging
387150526Sphk#endif
388150526Sphk
389113094Sphk	pushl	$begin				/* jump to high virtualized address */
390150526Sphk	ret
391113094Sphk
392135482Sphk/* now running relocated at KERNBASE where the system is linked to run */
393135482Sphkbegin:
394135482Sphk	/* set up bootstrap stack */
395135482Sphk	movl	_proc0paddr,%esp	/* location of in-kernel pages */
396113094Sphk	addl	$UPAGES*PAGE_SIZE,%esp	/* bootstrap stack end location */
397113094Sphk	xorl	%eax,%eax			/* mark end of frames */
398113094Sphk	movl	%eax,%ebp
399113094Sphk	movl	_proc0paddr,%eax
400113094Sphk	movl	_IdlePTD, %esi
401113094Sphk	movl	%esi,PCB_CR3(%eax)
402113094Sphk
403113094Sphk	movl	physfree, %esi
404113094Sphk	pushl	%esi				/* value of first for init386(first) */
405113094Sphk	call	_init386			/* wire 386 chip for unix operation */
406113094Sphk	popl	%esi
407113094Sphk
408113094Sphk	.globl	__ucodesel,__udatasel
409113094Sphk
410246128Ssbz	pushl	$0				/* unused */
411246128Ssbz	pushl	__udatasel			/* ss */
412113094Sphk	pushl	$0				/* esp - filled in by execve() */
413113094Sphk	pushl	$PSL_USER			/* eflags (IOPL 0, int enab) */
414113094Sphk	pushl	__ucodesel			/* cs */
415113094Sphk	pushl	$0				/* eip - filled in by execve() */
416113094Sphk	subl	$(12*4),%esp			/* space for rest of registers */
417113094Sphk
418113094Sphk	pushl	%esp				/* call main with frame pointer */
419113094Sphk	call	_main				/* autoconfiguration, mountroot etc */
420113094Sphk
421135482Sphk	hlt		/* never returns to here */
422113270Sphk
423/*
424 * When starting init, call this to configure the process for user
425 * mode.  This will be inherited by other processes.
426 */
427NON_GPROF_ENTRY(prepare_usermode)
428	/*
429	 * Now we've run main() and determined what cpu-type we are, we can
430	 * enable write protection and alignment checking on i486 cpus and
431	 * above.
432	 */
433#if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
434	cmpl    $CPUCLASS_386,_cpu_class
435	je	1f
436	movl	%cr0,%eax			/* get control word */
437	orl	$CR0_WP|CR0_AM,%eax		/* enable i486 features */
438	movl	%eax,%cr0			/* and do it */
4391:
440#endif
441	/*
442	 * on return from main(), we are process 1
443	 * set up address space and stack so that we can 'return' to user mode
444	 */
445	movl	__ucodesel,%eax
446	movl	__udatasel,%ecx
447
448#if 0
449	movl	%cx,%ds
450#endif
451	movl	%cx,%es
452	movl	%ax,%fs				/* double map cs to fs */
453	movl	%cx,%gs				/* and ds to gs */
454	ret					/* goto user! */
455
456
457#define LCALL(x,y)	.byte 0x9a ; .long y ; .word x
458
459/*
460 * Signal trampoline, copied to top of user stack
461 */
462NON_GPROF_ENTRY(sigcode)
463	call	SIGF_HANDLER(%esp)
464	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
465						/* copy at 8(%esp)) */
466	pushl	%eax
467	pushl	%eax				/* junk to fake return address */
468	movl	$SYS_sigreturn,%eax		/* sigreturn() */
469	LCALL(0x7,0)				/* enter kernel with args on stack */
470	hlt					/* never gets here */
471	ALIGN_TEXT
472_esigcode:
473
474	.data
475	.globl	_szsigcode
476_szsigcode:
477	.long	_esigcode-_sigcode
478	.text
479
480/**********************************************************************
481 *
482 * Recover the bootinfo passed to us from the boot program
483 *
484 */
485recover_bootinfo:
486	/*
487	 * This code is called in different ways depending on what loaded
488	 * and started the kernel.  This is used to detect how we get the
489	 * arguments from the other code and what we do with them.
490	 *
491	 * Old disk boot blocks:
492	 *	(*btext)(howto, bootdev, cyloffset, esym);
493	 *	[return address == 0, and can NOT be returned to]
494	 *	[cyloffset was not supported by the FreeBSD boot code
495	 *	 and always passed in as 0]
496	 *	[esym is also known as total in the boot code, and
497	 *	 was never properly supported by the FreeBSD boot code]
498	 *
499	 * Old diskless netboot code:
500	 *	(*btext)(0,0,0,0,&nfsdiskless,0,0,0);
501	 *	[return address != 0, and can NOT be returned to]
502	 *	If we are being booted by this code it will NOT work,
503	 *	so we are just going to halt if we find this case.
504	 *
505	 * New uniform boot code:
506	 *	(*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
507	 *	[return address != 0, and can be returned to]
508	 *
509	 * There may seem to be a lot of wasted arguments in here, but
510	 * that is so the newer boot code can still load very old kernels
511	 * and old boot code can load new kernels.
512	 */
513
514	/*
515	 * The old style disk boot blocks fake a frame on the stack and
516	 * did an lret to get here.  The frame on the stack has a return
517	 * address of 0.
518	 */
519	cmpl	$0,4(%ebp)
520	je	olddiskboot
521
522	/*
523	 * We have some form of return address, so this is either the
524	 * old diskless netboot code, or the new uniform code.  That can
525	 * be detected by looking at the 5th argument, if it is 0
526	 * we are being booted by the new uniform boot code.
527	 */
528	cmpl	$0,24(%ebp)
529	je	newboot
530
531	/*
532	 * Seems we have been loaded by the old diskless boot code, we
533	 * don't stand a chance of running as the diskless structure
534	 * changed considerably between the two, so just halt.
535	 */
536	 hlt
537
538	/*
539	 * We have been loaded by the new uniform boot code.
540	 * Let's check the bootinfo version, and if we do not understand
541	 * it we return to the loader with a status of 1 to indicate this error
542	 */
543newboot:
544	movl	28(%ebp),%ebx		/* &bootinfo.version */
545	movl	BI_VERSION(%ebx),%eax
546	cmpl	$1,%eax			/* We only understand version 1 */
547	je	1f
548	movl	$1,%eax			/* Return status */
549	leave
550	/*
551	 * XXX this returns to our caller's caller (as is required) since
552	 * we didn't set up a frame and our caller did.
553	 */
554	ret
555
5561:
557	/*
558	 * If we have a kernelname copy it in
559	 */
560	movl	BI_KERNELNAME(%ebx),%esi
561	cmpl	$0,%esi
562	je	2f			/* No kernelname */
563	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
564	movl	$R(_kernelname),%edi
565	cmpb	$'/',(%esi)		/* Make sure it starts with a slash */
566	je	1f
567	movb	$'/',(%edi)
568	incl	%edi
569	decl	%ecx
5701:
571	cld
572	rep
573	movsb
574
5752:
576	/*
577	 * Determine the size of the boot loader's copy of the bootinfo
578	 * struct.  This is impossible to do properly because old versions
579	 * of the struct don't contain a size field and there are 2 old
580	 * versions with the same version number.
581	 */
582	movl	$BI_ENDCOMMON,%ecx	/* prepare for sizeless version */
583	testl	$RB_BOOTINFO,8(%ebp)	/* bi_size (and bootinfo) valid? */
584	je	got_bi_size		/* no, sizeless version */
585	movl	BI_SIZE(%ebx),%ecx
586got_bi_size:
587
588	/*
589	 * Copy the common part of the bootinfo struct
590	 */
591	movl	%ebx,%esi
592	movl	$R(_bootinfo),%edi
593	cmpl	$BOOTINFO_SIZE,%ecx
594	jbe	got_common_bi_size
595	movl	$BOOTINFO_SIZE,%ecx
596got_common_bi_size:
597	cld
598	rep
599	movsb
600
601#ifdef NFS
602#ifndef BOOTP_NFSV3
603	/*
604	 * If we have a nfs_diskless structure copy it in
605	 */
606	movl	BI_NFS_DISKLESS(%ebx),%esi
607	cmpl	$0,%esi
608	je	olddiskboot
609	movl	$R(_nfs_diskless),%edi
610	movl	$NFSDISKLESS_SIZE,%ecx
611	cld
612	rep
613	movsb
614	movl	$R(_nfs_diskless_valid),%edi
615	movl	$1,(%edi)
616#endif
617#endif
618
619	/*
620	 * The old style disk boot.
621	 *	(*btext)(howto, bootdev, cyloffset, esym);
622	 * Note that the newer boot code just falls into here to pick
623	 * up howto and bootdev, cyloffset and esym are no longer used
624	 */
625olddiskboot:
626	movl	8(%ebp),%eax
627	movl	%eax,R(_boothowto)
628	movl	12(%ebp),%eax
629	movl	%eax,R(_bootdev)
630
631#if defined(USERCONFIG_BOOT) && defined(USERCONFIG)
632#ifdef PC98
633	movl	$0x90200, %esi
634#else
635	movl	$0x10200, %esi
636#endif
637	movl	$R(_userconfig_from_boot),%edi
638	movl	$512,%ecx
639	cld
640	rep
641	movsb
642#endif /* USERCONFIG_BOOT */
643
644	ret
645
646
647/**********************************************************************
648 *
649 * Identify the CPU and initialize anything special about it
650 *
651 */
652identify_cpu:
653
654	/* Try to toggle alignment check flag; does not exist on 386. */
655	pushfl
656	popl	%eax
657	movl	%eax,%ecx
658	orl	$PSL_AC,%eax
659	pushl	%eax
660	popfl
661	pushfl
662	popl	%eax
663	xorl	%ecx,%eax
664	andl	$PSL_AC,%eax
665	pushl	%ecx
666	popfl
667
668	testl	%eax,%eax
669	jnz	try486
670
671	/* NexGen CPU does not have aligment check flag. */
672	pushfl
673	movl	$0x5555, %eax
674	xorl	%edx, %edx
675	movl	$2, %ecx
676	clc
677	divl	%ecx
678	jz	trynexgen
679	popfl
680	movl	$CPU_386,R(_cpu)
681	jmp	3f
682
683trynexgen:
684	popfl
685	movl	$CPU_NX586,R(_cpu)
686	movl	$0x4778654e,R(_cpu_vendor)	# store vendor string
687	movl	$0x72446e65,R(_cpu_vendor+4)
688	movl	$0x6e657669,R(_cpu_vendor+8)
689	movl	$0,R(_cpu_vendor+12)
690	jmp	3f
691
692try486:	/* Try to toggle identification flag; does not exist on early 486s. */
693	pushfl
694	popl	%eax
695	movl	%eax,%ecx
696	xorl	$PSL_ID,%eax
697	pushl	%eax
698	popfl
699	pushfl
700	popl	%eax
701	xorl	%ecx,%eax
702	andl	$PSL_ID,%eax
703	pushl	%ecx
704	popfl
705
706	testl	%eax,%eax
707	jnz	trycpuid
708	movl	$CPU_486,R(_cpu)
709
710	/*
711	 * Check Cyrix CPU
712	 * Cyrix CPUs do not change the undefined flags following
713	 * execution of the divide instruction which divides 5 by 2.
714	 *
715	 * Note: CPUID is enabled on M2, so it passes another way.
716	 */
717	pushfl
718	movl	$0x5555, %eax
719	xorl	%edx, %edx
720	movl	$2, %ecx
721	clc
722	divl	%ecx
723	jnc	trycyrix
724	popfl
725	jmp	3f		/* You may use Intel CPU. */
726
727trycyrix:
728	popfl
729	/*
730	 * IBM Bluelighting CPU also doesn't change the undefined flags.
731	 * Because IBM doesn't disclose the information for Bluelighting
732	 * CPU, we couldn't distinguish it from Cyrix's (including IBM
733	 * brand of Cyrix CPUs).
734	 */
735	movl	$0x69727943,R(_cpu_vendor)	# store vendor string
736	movl	$0x736e4978,R(_cpu_vendor+4)
737	movl	$0x64616574,R(_cpu_vendor+8)
738	jmp	3f
739
740trycpuid:	/* Use the `cpuid' instruction. */
741	xorl	%eax,%eax
742	.byte	0x0f,0xa2			# cpuid 0
743	movl	%eax,R(_cpu_high)		# highest capability
744	movl	%ebx,R(_cpu_vendor)		# store vendor string
745	movl	%edx,R(_cpu_vendor+4)
746	movl	%ecx,R(_cpu_vendor+8)
747	movb	$0,R(_cpu_vendor+12)
748
749	movl	$1,%eax
750	.byte	0x0f,0xa2			# cpuid 1
751	movl	%eax,R(_cpu_id)			# store cpu_id
752	movl	%edx,R(_cpu_feature)		# store cpu_feature
753	rorl	$8,%eax				# extract family type
754	andl	$15,%eax
755	cmpl	$5,%eax
756	jae	1f
757
758	/* less than Pentium; must be 486 */
759	movl	$CPU_486,R(_cpu)
760	jmp	3f
7611:
762	/* a Pentium? */
763	cmpl	$5,%eax
764	jne	2f
765	movl	$CPU_586,R(_cpu)
766	jmp	3f
7672:
768	/* Greater than Pentium...call it a Pentium Pro */
769	movl	$CPU_686,R(_cpu)
7703:
771	ret
772
773
774/**********************************************************************
775 *
776 * Create the first page directory and its page tables.
777 *
778 */
779
780create_pagetables:
781
782	testl	$CPUID_PGE, R(_cpu_feature)
783	jz	1f
784	movl	%cr4, %eax
785	orl	$CR4_PGE, %eax
786	movl	%eax, %cr4
7871:
788
789/* Find end of kernel image (rounded up to a page boundary). */
790	movl	$R(_end),%esi
791
792/* include symbols in "kernel image" if they are loaded and useful */
793#ifdef DDB
794	movl	R(_bootinfo+BI_ESYMTAB),%edi
795	testl	%edi,%edi
796	je	over_symalloc
797	movl	%edi,%esi
798	movl	$KERNBASE,%edi
799	addl	%edi,R(_bootinfo+BI_SYMTAB)
800	addl	%edi,R(_bootinfo+BI_ESYMTAB)
801over_symalloc:
802#endif
803
804	addl	$PAGE_MASK,%esi
805	andl	$~PAGE_MASK,%esi
806	movl	%esi,R(_KERNend)	/* save end of kernel */
807	movl	%esi,R(physfree)	/* next free page is at end of kernel */
808
809/* Allocate Kernel Page Tables */
810	ALLOCPAGES(NKPT)
811	movl	%esi,R(_KPTphys)
812
813/* Allocate Page Table Directory */
814	ALLOCPAGES(1)
815	movl	%esi,R(_IdlePTD)
816
817/* Allocate UPAGES */
818	ALLOCPAGES(UPAGES)
819	movl	%esi,R(p0upa)
820	addl	$KERNBASE, %esi
821	movl	%esi, R(_proc0paddr)
822
823#ifdef SMP
824/* Allocate cpu0's private data page */
825	ALLOCPAGES(1)
826	movl	%esi,R(cpu0pp)
827	addl	$KERNBASE, %esi
828	movl	%esi, R(_cpu0prvpage)	/* relocated to KVM space */
829
830/* Allocate cpu0's private page table for mapping priv page, apic, etc */
831	ALLOCPAGES(1)
832	movl	%esi,R(cpu0pt)
833	addl	$KERNBASE, %esi
834	movl	%esi, R(_cpu0prvpt)	/* relocated to KVM space */
835#endif	/* SMP */
836
837/* Map read-only from zero to the end of the kernel text section */
838	xorl	%eax, %eax
839#ifdef BDE_DEBUGGER
840/* If the debugger is present, actually map everything read-write. */
841	cmpl	$0,R(_bdb_exists)
842	jne	map_read_write
843#endif
844	xorl	%edx,%edx
845
846#if !defined(SMP)
847	testl	$CPUID_PGE, R(_cpu_feature)
848	jz	2f
849	orl	$PG_G,%edx
850#endif
851
8522:	movl	$R(_etext),%ecx
853	addl	$PAGE_MASK,%ecx
854	shrl	$PAGE_SHIFT,%ecx
855	fillkptphys(%edx)
856
857/* Map read-write, data, bss and symbols */
858	movl	$R(_etext),%eax
859	addl	$PAGE_MASK, %eax
860	andl	$~PAGE_MASK, %eax
861map_read_write:
862	movl	$PG_RW,%edx
863#if !defined(SMP)
864	testl	$CPUID_PGE, R(_cpu_feature)
865	jz	1f
866	orl	$PG_G,%edx
867#endif
868
8691:	movl	R(_KERNend),%ecx
870	subl	%eax,%ecx
871	shrl	$PAGE_SHIFT,%ecx
872	fillkptphys(%edx)
873
874/* Map page directory. */
875	movl	R(_IdlePTD), %eax
876	movl	$1, %ecx
877	fillkptphys($PG_RW)
878
879/* Map proc0's UPAGES in the physical way ... */
880	movl	R(p0upa), %eax
881	movl	$UPAGES, %ecx
882	fillkptphys($PG_RW)
883
884/* Map ISA hole */
885	movl	$ISA_HOLE_START, %eax
886	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
887	fillkptphys($PG_RW)
888
889#ifdef SMP
890/* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */
891	movl	R(cpu0pp), %eax
892	movl	$1, %ecx
893	fillkptphys($PG_RW)
894
895/* Map cpu0's private page table into global kmem FWIW */
896	movl	R(cpu0pt), %eax
897	movl	$1, %ecx
898	fillkptphys($PG_RW)
899
900/* Map the private page into the private page table into private space */
901	movl	R(cpu0pp), %eax
902	movl	$0, %ebx		/* pte offset = 0 */
903	movl	$1, %ecx		/* one private page coming right up */
904	fillkpt(R(cpu0pt), $PG_RW)
905
906/* Map the page table page into private space */
907	movl	R(cpu0pt), %eax
908	movl	$1, %ebx		/* pte offset = 1 */
909	movl	$1, %ecx		/* one private pt coming right up */
910	fillkpt(R(cpu0pt), $PG_RW)
911
912/* ... and put the page table table in the pde. */
913	movl	R(cpu0pt), %eax
914	movl	$MPPTDI, %ebx
915	movl	$1, %ecx
916	fillkpt(R(_IdlePTD), $PG_RW)
917#endif	/* SMP */
918
919/* install a pde for temporary double map of bottom of VA */
920	movl	R(_KPTphys), %eax
921	xorl	%ebx, %ebx
922	movl	$1, %ecx
923	fillkpt(R(_IdlePTD), $PG_RW)
924
925/* install pde's for pt's */
926	movl	R(_KPTphys), %eax
927	movl	$KPTDI, %ebx
928	movl	$NKPT, %ecx
929	fillkpt(R(_IdlePTD), $PG_RW)
930
931/* install a pde recursively mapping page directory as a page table */
932	movl	R(_IdlePTD), %eax
933	movl	$PTDPTDI, %ebx
934	movl	$1,%ecx
935	fillkpt(R(_IdlePTD), $PG_RW)
936
937	ret
938
939#ifdef BDE_DEBUGGER
940bdb_prepare_paging:
941	cmpl	$0,R(_bdb_exists)
942	je	bdb_prepare_paging_exit
943
944	subl	$6,%esp
945
946	/*
947	 * Copy and convert debugger entries from the bootstrap gdt and idt
948	 * to the kernel gdt and idt.  Everything is still in low memory.
949	 * Tracing continues to work after paging is enabled because the
950	 * low memory addresses remain valid until everything is relocated.
951	 * However, tracing through the setidt() that initializes the trace
952	 * trap will crash.
953	 */
954	sgdt	(%esp)
955	movl	2(%esp),%esi		/* base address of bootstrap gdt */
956	movl	$R(_gdt),%edi
957	movl	%edi,2(%esp)		/* prepare to load kernel gdt */
958	movl	$8*18/4,%ecx
959	cld
960	rep				/* copy gdt */
961	movsl
962	movl	$R(_gdt),-8+2(%edi)	/* adjust gdt self-ptr */
963	movb	$0x92,-8+5(%edi)
964	lgdt	(%esp)
965
966	sidt	(%esp)
967	movl	2(%esp),%esi		/* base address of current idt */
968	movl	8+4(%esi),%eax		/* convert dbg descriptor to ... */
969	movw	8(%esi),%ax
970	movl	%eax,R(bdb_dbg_ljmp+1)	/* ... immediate offset ... */
971	movl	8+2(%esi),%eax
972	movw	%ax,R(bdb_dbg_ljmp+5)	/* ... and selector for ljmp */
973	movl	24+4(%esi),%eax		/* same for bpt descriptor */
974	movw	24(%esi),%ax
975	movl	%eax,R(bdb_bpt_ljmp+1)
976	movl	24+2(%esi),%eax
977	movw	%ax,R(bdb_bpt_ljmp+5)
978	movl	$R(_idt),%edi
979	movl	%edi,2(%esp)		/* prepare to load kernel idt */
980	movl	$8*4/4,%ecx
981	cld
982	rep				/* copy idt */
983	movsl
984	lidt	(%esp)
985
986	addl	$6,%esp
987
988bdb_prepare_paging_exit:
989	ret
990
991/* Relocate debugger gdt entries and gdt and idt pointers. */
992bdb_commit_paging:
993	cmpl	$0,_bdb_exists
994	je	bdb_commit_paging_exit
995
996	movl	$_gdt+8*9,%eax		/* adjust slots 9-17 */
997	movl	$9,%ecx
998reloc_gdt:
999	movb	$KERNBASE>>24,7(%eax)	/* top byte of base addresses, was 0, */
1000	addl	$8,%eax			/* now KERNBASE>>24 */
1001	loop	reloc_gdt
1002
1003	subl	$6,%esp
1004	sgdt	(%esp)
1005	addl	$KERNBASE,2(%esp)
1006	lgdt	(%esp)
1007	sidt	(%esp)
1008	addl	$KERNBASE,2(%esp)
1009	lidt	(%esp)
1010	addl	$6,%esp
1011
1012	int	$3
1013
1014bdb_commit_paging_exit:
1015	ret
1016
1017#endif /* BDE_DEBUGGER */
1018