locore.s revision 43434
1331722Seadler/*-
21573Srgrimes * Copyright (c) 1990 The Regents of the University of California.
31573Srgrimes * All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * William Jolitz.
71573Srgrimes *
8227753Stheraven * Redistribution and use in source and binary forms, with or without
9227753Stheraven * modification, are permitted provided that the following conditions
10227753Stheraven * are met:
11227753Stheraven * 1. Redistributions of source code must retain the above copyright
12227753Stheraven *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 3. All advertising materials mentioning features or use of this software
171573Srgrimes *    must display the following acknowledgement:
181573Srgrimes *	This product includes software developed by the University of
191573Srgrimes *	California, Berkeley and its contributors.
201573Srgrimes * 4. Neither the name of the University nor the names of its contributors
211573Srgrimes *    may be used to endorse or promote products derived from this software
221573Srgrimes *    without specific prior written permission.
231573Srgrimes *
241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341573Srgrimes * SUCH DAMAGE.
351573Srgrimes *
361573Srgrimes *	from: @(#)locore.s	7.3 (Berkeley) 5/13/91
371573Srgrimes *	$Id: locore.s,v 1.118 1998/12/08 10:22:31 kato Exp $
381573Srgrimes *
391573Srgrimes *		originally from: locore.s, by William F. Jolitz
401573Srgrimes *
4190045Sobrien *		Substantially rewritten by David Greenman, Rod Grimes,
4290045Sobrien *			Bruce Evans, Wolfgang Solfrank, Poul-Henning Kamp
431573Srgrimes *			and many others.
441573Srgrimes */
451573Srgrimes
461573Srgrimes#include "apm.h"
471573Srgrimes#include "opt_bootp.h"
481573Srgrimes#include "opt_ddb.h"
49132812Stjr#include "opt_nfsroot.h"
50132812Stjr#include "opt_userconfig.h"
51132812Stjr#include "opt_vm86.h"
52132812Stjr
53132812Stjr#include <sys/syscall.h>
54132812Stjr#include <sys/reboot.h>
55132812Stjr
56132812Stjr#include <machine/asmacros.h>
57132812Stjr#include <machine/cputypes.h>
58132812Stjr#include <machine/psl.h>
59132812Stjr#include <machine/pmap.h>
601573Srgrimes#include <machine/specialreg.h>
61132812Stjr
621573Srgrimes#include "assym.s"
63132812Stjr
64132812Stjr/*
651573Srgrimes *	XXX
6619276Sache *
6719276Sache * Note: This version greatly munged to avoid various assembler errors
681573Srgrimes * that may be fixed in newer versions of gas. Perhaps newer versions
691573Srgrimes * will have more pleasant appearance.
7026484Sache */
7126484Sache
7226484Sache/*
731573Srgrimes * PTmap is recursive pagemap at top of virtual address space.
74132812Stjr * Within PTmap, the page directory can be found (third indirection).
75206711Sjilles */
76206711Sjilles	.globl	_PTmap,_PTD,_PTDpde
7726484Sache	.set	_PTmap,(PTDPTDI << PDRSHIFT)
781573Srgrimes	.set	_PTD,_PTmap + (PTDPTDI * PAGE_SIZE)
79288029Srodrigc	.set	_PTDpde,_PTD + (PTDPTDI * PDESIZE)
801573Srgrimes
81132812Stjr/*
82132812Stjr * APTmap, APTD is the alternate recursive pagemap.
83206711Sjilles * It's used when modifying another process's page tables.
84132812Stjr */
85132812Stjr	.globl	_APTmap,_APTD,_APTDpde
86132812Stjr	.set	_APTmap,APTDPTDI << PDRSHIFT
87288029Srodrigc	.set	_APTD,_APTmap + (APTDPTDI * PAGE_SIZE)
88288029Srodrigc	.set	_APTDpde,_PTD + (APTDPTDI * PDESIZE)
89132812Stjr
90288309Sjilles/*
91288309Sjilles * Globals
9226484Sache */
93132812Stjr	.data
94132812Stjr	ALIGN_DATA		/* just to be sure */
95132812Stjr
961573Srgrimes	.globl	HIDENAME(tmpstk)
97288309Sjilles	.space	0x2000		/* space for tmpstk - temporary stack */
98206711SjillesHIDENAME(tmpstk):
99132812Stjr
100132812Stjr	.globl	_boothowto,_bootdev
101132812Stjr
102132812Stjr	.globl	_cpu,_cpu_vendor,_cpu_id,_bootinfo
103132812Stjr	.globl	_cpu_high, _cpu_feature
104132812Stjr
105132812Stjr_cpu:	.long	0				/* are we 386, 386sx, or 486 */
106132812Stjr_cpu_id:	.long	0			/* stepping ID */
107132812Stjr_cpu_high:	.long	0			/* highest arg to CPUID */
108132812Stjr_cpu_feature:	.long	0			/* features */
109132812Stjr_cpu_vendor:	.space	20			/* CPU origin code */
1101573Srgrimes_bootinfo:	.space	BOOTINFO_SIZE		/* bootinfo that we can handle */
111132812Stjr
11219132Sache_KERNend:	.long	0			/* phys addr end of kernel (just after bss) */
113288309Sjillesphysfree:	.long	0			/* phys addr of next free page */
114288309Sjilles
115288309Sjilles#ifdef SMP
1161573Srgrimescpu0pp:		.long	0			/* phys addr cpu0 private pg */
117132812Stjrcpu0pt:		.long	0			/* phys addr cpu0 private pt */
1181573Srgrimes
119132812Stjr		.globl	_cpu0prvpage,_cpu0prvpt
120288309Sjilles_cpu0prvpage:	.long	0			/* relocated version */
121132812Stjr_cpu0prvpt:	.long	0			/* relocated version */
1221573Srgrimes#endif /* SMP */
1231573Srgrimes
124288309Sjilles	.globl	_IdlePTD
125132812Stjr_IdlePTD:	.long	0			/* phys addr of kernel PTD */
1261573Srgrimes
1271573Srgrimes#ifdef SMP
1281573Srgrimes	.globl	_KPTphys
1291573Srgrimes#endif
1301573Srgrimes_KPTphys:	.long	0			/* phys addr of kernel page tables */
1311573Srgrimes
1321573Srgrimes	.globl	_proc0paddr
133132812Stjr_proc0paddr:	.long	0			/* address of proc 0 address space */
1341573Srgrimesp0upa:		.long	0			/* phys addr of proc0's UPAGES */
1351573Srgrimes
136288309Sjilles#ifdef VM86
1371573Srgrimesvm86phystk:	.long	0			/* PA of vm86/bios stack */
1381573Srgrimes
1391573Srgrimes	.globl	_vm86paddr, _vm86pa
1401573Srgrimes_vm86paddr:	.long	0			/* address of vm86 region */
14125269Sjdp_vm86pa:	.long	0			/* phys addr of vm86 region */
14225269Sjdp#endif
1431573Srgrimes
1441573Srgrimes#ifdef BDE_DEBUGGER
1451573Srgrimes	.globl	_bdb_exists			/* flag to indicate BDE debugger is present */
1461573Srgrimes_bdb_exists:	.long	0
1471573Srgrimes#endif
1481573Srgrimes
1491573Srgrimes#ifdef PC98
1501573Srgrimes	.globl	_pc98_system_parameter
1511573Srgrimes_pc98_system_parameter:
152288309Sjilles	.space	0x240
153288309Sjilles#endif
154288309Sjilles
155288309Sjilles/**********************************************************************
156288309Sjilles *
157288309Sjilles * Some handy macros
158288309Sjilles *
159288309Sjilles */
160288309Sjilles
1611573Srgrimes#define R(foo) ((foo)-KERNBASE)
162132812Stjr
1631573Srgrimes#define ALLOCPAGES(foo) \
164132812Stjr	movl	R(physfree), %esi ; \
165288309Sjilles	movl	$((foo)*PAGE_SIZE), %eax ; \
166132812Stjr	addl	%esi, %eax ; \
16726486Sache	movl	%eax, R(physfree) ; \
16826486Sache	movl	%esi, %edi ; \
169288309Sjilles	movl	$((foo)*PAGE_SIZE),%ecx ; \
17026486Sache	xorl	%eax,%eax ; \
171132812Stjr	cld ; \
172132812Stjr	rep ; \
17326484Sache	stosb
17426484Sache
17526484Sache/*
17626484Sache * fillkpt
17726484Sache *	eax = page frame address
17826484Sache *	ebx = index into page table
179288309Sjilles *	ecx = how many pages to map
18026484Sache * 	base = base address of page dir/table
181132812Stjr *	prot = protection bits
1821573Srgrimes */
1831573Srgrimes#define	fillkpt(base, prot)		  \
1841573Srgrimes	shll	$2,%ebx			; \
185132812Stjr	addl	base,%ebx		; \
186132812Stjr	orl	$PG_V,%eax		; \
187322524Spfg	orl	prot,%eax		; \
188322524Spfg1:	movl	%eax,(%ebx)		; \
189132812Stjr	addl	$PAGE_SIZE,%eax		; /* increment physical address */ \
190132812Stjr	addl	$4,%ebx			; /* next pte */ \
1911573Srgrimes	loop	1b
1921573Srgrimes
1931573Srgrimes/*
19426484Sache * fillkptphys(prot)
195288309Sjilles *	eax = physical address
196132812Stjr *	ecx = how many pages to map
19719059Swosch *	prot = protection bits
19819132Sache */
199132812Stjr#define	fillkptphys(prot)		  \
20019059Swosch	movl	%eax, %ebx		; \
201288309Sjilles	shrl	$PAGE_SHIFT, %ebx	; \
202288309Sjilles	fillkpt(R(_KPTphys), prot)
203288309Sjilles
204288309Sjilles	.text
205288309Sjilles/**********************************************************************
206288309Sjilles *
207288309Sjilles * This is where the bootblocks start us, set the ball rolling...
208288309Sjilles *
209288309Sjilles */
210288309SjillesNON_GPROF_ENTRY(btext)
211288309Sjilles
212288309Sjilles#ifdef PC98
213288309Sjilles	/* save SYSTEM PARAMETER for resume (NS/T or other) */
214288309Sjilles	movl	$0xa1400,%esi
215288309Sjilles	movl	$R(_pc98_system_parameter),%edi
216288309Sjilles	movl	$0x0240,%ecx
217288309Sjilles	cld
218288309Sjilles	rep
219288309Sjilles	movsb
220288309Sjilles#else	/* IBM-PC */
221288309Sjilles#ifdef BDE_DEBUGGER
222288309Sjilles#ifdef BIOS_STEALS_3K
223288309Sjilles	cmpl	$0x0375c339,0x95504
224288309Sjilles#else
225288309Sjilles	cmpl	$0x0375c339,0x96104	/* XXX - debugger signature */
226288309Sjilles#endif
227288309Sjilles	jne	1f
2281573Srgrimes	movb	$1,R(_bdb_exists)
2291573Srgrimes1:
230132812Stjr#endif
2311573Srgrimes#endif	/* PC98 */
2321573Srgrimes
2331573Srgrimes/* Tell the bios to warmboot next time */
23426484Sache	movw	$0x1234,0x472
235288029Srodrigc
236288029Srodrigc/* Set up a real frame in case the double return in newboot is executed. */
2371573Srgrimes	pushl	%ebp
23826492Sache	movl	%esp, %ebp
239132812Stjr
240132812Stjr/* Don't trust what the BIOS gives for eflags. */
241132812Stjr	pushl	$PSL_KERNEL
242227753Stheraven	popfl
243227753Stheraven
2441573Srgrimes/*
2451573Srgrimes * Don't trust what the BIOS gives for %fs and %gs.  Trust the bootstrap
2461573Srgrimes * to set %cs, %ds, %es and %ss.
2471573Srgrimes */
2481573Srgrimes	mov	%ds, %ax
2491573Srgrimes	mov	%ax, %fs
2501573Srgrimes	mov	%ax, %gs
2511573Srgrimes
252323548Spfg	call	recover_bootinfo
2531573Srgrimes
2548870Srgrimes/* Get onto a stack that we can trust. */
25519132Sache/*
256132812Stjr * XXX this step is delayed in case recover_bootinfo needs to return via
25719059Swosch * the old stack, but it need not be, since recover_bootinfo actually
25826484Sache * returns via the old frame.
25926484Sache */
26026484Sache	movl	$R(HIDENAME(tmpstk)),%esp
26126484Sache
26226484Sache#ifdef PC98
26326492Sache	testb	$0x02,0x100620		/* pc98_machine_type & M_EPSON_PC98 */
264132812Stjr	jz	3f
265132812Stjr	cmpb	$0x0b,0x100624		/* epson_machine_id <= 0x0b */
266132812Stjr	ja	3f
267132812Stjr
268132812Stjr	/* count up memory */
269132812Stjr	movl	$0x100000,%eax		/* next, talley remaining memory */
27026484Sache	movl	$0xFFF-0x100,%ecx
271132812Stjr1:	movl	0(%eax),%ebx		/* save location to check */
27226486Sache	movl	$0xa55a5aa5,0(%eax)	/* write test pattern */
273132812Stjr	cmpl	$0xa55a5aa5,0(%eax)	/* does not check yet for rollover */
274132812Stjr	jne	2f
275132812Stjr	movl	%ebx,0(%eax)		/* restore memory */
276132812Stjr	addl	$PAGE_SIZE,%eax
277132812Stjr	loop	1b
278132812Stjr2:	subl	$0x100000,%eax
27926486Sache	shrl	$17,%eax
28019132Sache	movb	%al,0x100401
281132812Stjr3:
28219059Swosch#endif
283132812Stjr
284132812Stjr	call	identify_cpu
285132812Stjr
286132812Stjr/* clear bss */
287132812Stjr/*
288132812Stjr * XXX this should be done a little earlier.
289132812Stjr *
290132812Stjr * XXX we don't check that there is memory for our bss and page tables
291132812Stjr * before using it.
2921573Srgrimes *
29326484Sache * XXX the boot program somewhat bogusly clears the bss.  We still have
29419059Swosch * to do it in case we were unzipped by kzipboot.  Then the boot program
29519132Sache * only clears kzipboot's bss.
296132812Stjr *
29719059Swosch * XXX the gdt and idt are still somewhere in the boot program.  We
298227753Stheraven * depend on the convention that the boot program is below 1MB and we
29924632Sache * are above 1MB to keep the gdt and idt  away from the bss and page
300304275Sache * tables.  The idt is only used if BDE_DEBUGGER is enabled.
301304275Sache */
30217533Sache	movl	$R(_end),%ecx
3031573Srgrimes	movl	$R(_edata),%edi
3041573Srgrimes	subl	%edi,%ecx
3051573Srgrimes	xorl	%eax,%eax
306132812Stjr	cld
30726492Sache	rep
30826484Sache	stosb
30926484Sache
3101573Srgrimes#if NAPM > 0
311#ifndef VM86
312/*
313 * XXX it's not clear that APM can live in the current environonment.
314 * Only pc-relative addressing works.
315 */
316	call	_apm_setup
317#endif
318#endif
319
320	call	create_pagetables
321
322#ifdef VM86
323/*
324 * If the CPU has support for VME, turn it on.
325 */
326	testl	$CPUID_VME, R(_cpu_feature)
327	jz	1f
328	movl	%cr4, %eax
329	orl	$CR4_VME, %eax
330	movl	%eax, %cr4
3311:
332#endif /* VM86 */
333
334#ifdef BDE_DEBUGGER
335/*
336 * Adjust as much as possible for paging before enabling paging so that the
337 * adjustments can be traced.
338 */
339	call	bdb_prepare_paging
340#endif
341
342/* Now enable paging */
343	movl	R(_IdlePTD), %eax
344	movl	%eax,%cr3			/* load ptd addr into mmu */
345	movl	%cr0,%eax			/* get control word */
346	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
347	movl	%eax,%cr0			/* and let's page NOW! */
348
349#ifdef BDE_DEBUGGER
350/*
351 * Complete the adjustments for paging so that we can keep tracing through
352 * initi386() after the low (physical) addresses for the gdt and idt become
353 * invalid.
354 */
355	call	bdb_commit_paging
356#endif
357
358	pushl	$begin				/* jump to high virtualized address */
359	ret
360
361/* now running relocated at KERNBASE where the system is linked to run */
362begin:
363	/* set up bootstrap stack */
364	movl	_proc0paddr,%esp	/* location of in-kernel pages */
365	addl	$UPAGES*PAGE_SIZE,%esp	/* bootstrap stack end location */
366	xorl	%eax,%eax			/* mark end of frames */
367	movl	%eax,%ebp
368	movl	_proc0paddr,%eax
369	movl	_IdlePTD, %esi
370	movl	%esi,PCB_CR3(%eax)
371	movl	$_proc0,_curproc
372
373	movl	physfree, %esi
374	pushl	%esi				/* value of first for init386(first) */
375	call	_init386			/* wire 386 chip for unix operation */
376	popl	%esi
377
378	.globl	__ucodesel,__udatasel
379
380	pushl	$0				/* unused */
381	pushl	__udatasel			/* ss */
382	pushl	$0				/* esp - filled in by execve() */
383	pushl	$PSL_USER			/* eflags (IOPL 0, int enab) */
384	pushl	__ucodesel			/* cs */
385	pushl	$0				/* eip - filled in by execve() */
386	subl	$(12*4),%esp			/* space for rest of registers */
387
388	pushl	%esp				/* call main with frame pointer */
389	call	_main				/* autoconfiguration, mountroot etc */
390
391	hlt		/* never returns to here */
392
393/*
394 * When starting init, call this to configure the process for user
395 * mode.  This will be inherited by other processes.
396 */
397NON_GPROF_ENTRY(prepare_usermode)
398	/*
399	 * Now we've run main() and determined what cpu-type we are, we can
400	 * enable write protection and alignment checking on i486 cpus and
401	 * above.
402	 */
403#if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
404	cmpl    $CPUCLASS_386,_cpu_class
405	je	1f
406	movl	%cr0,%eax			/* get control word */
407	orl	$CR0_WP|CR0_AM,%eax		/* enable i486 features */
408	movl	%eax,%cr0			/* and do it */
4091:
410#endif
411	/*
412	 * on return from main(), we are process 1
413	 * set up address space and stack so that we can 'return' to user mode
414	 */
415	movl	__ucodesel,%eax
416	movl	__udatasel,%ecx
417
418#if 0
419	movl	%cx,%ds
420#endif
421	movl	%cx,%es
422	movl	%ax,%fs				/* double map cs to fs */
423	movl	%cx,%gs				/* and ds to gs */
424	ret					/* goto user! */
425
426
427#define LCALL(x,y)	.byte 0x9a ; .long y ; .word x
428
429/*
430 * Signal trampoline, copied to top of user stack
431 */
432NON_GPROF_ENTRY(sigcode)
433	call	SIGF_HANDLER(%esp)
434	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
435						/* copy at 8(%esp)) */
436	pushl	%eax
437	pushl	%eax				/* junk to fake return address */
438	movl	$SYS_sigreturn,%eax		/* sigreturn() */
439	LCALL(0x7,0)				/* enter kernel with args on stack */
440	hlt					/* never gets here */
441	ALIGN_TEXT
442_esigcode:
443
444	.data
445	.globl	_szsigcode
446_szsigcode:
447	.long	_esigcode-_sigcode
448	.text
449
450/**********************************************************************
451 *
452 * Recover the bootinfo passed to us from the boot program
453 *
454 */
455recover_bootinfo:
456	/*
457	 * This code is called in different ways depending on what loaded
458	 * and started the kernel.  This is used to detect how we get the
459	 * arguments from the other code and what we do with them.
460	 *
461	 * Old disk boot blocks:
462	 *	(*btext)(howto, bootdev, cyloffset, esym);
463	 *	[return address == 0, and can NOT be returned to]
464	 *	[cyloffset was not supported by the FreeBSD boot code
465	 *	 and always passed in as 0]
466	 *	[esym is also known as total in the boot code, and
467	 *	 was never properly supported by the FreeBSD boot code]
468	 *
469	 * Old diskless netboot code:
470	 *	(*btext)(0,0,0,0,&nfsdiskless,0,0,0);
471	 *	[return address != 0, and can NOT be returned to]
472	 *	If we are being booted by this code it will NOT work,
473	 *	so we are just going to halt if we find this case.
474	 *
475	 * New uniform boot code:
476	 *	(*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
477	 *	[return address != 0, and can be returned to]
478	 *
479	 * There may seem to be a lot of wasted arguments in here, but
480	 * that is so the newer boot code can still load very old kernels
481	 * and old boot code can load new kernels.
482	 */
483
484	/*
485	 * The old style disk boot blocks fake a frame on the stack and
486	 * did an lret to get here.  The frame on the stack has a return
487	 * address of 0.
488	 */
489	cmpl	$0,4(%ebp)
490	je	olddiskboot
491
492	/*
493	 * We have some form of return address, so this is either the
494	 * old diskless netboot code, or the new uniform code.  That can
495	 * be detected by looking at the 5th argument, if it is 0
496	 * we are being booted by the new uniform boot code.
497	 */
498	cmpl	$0,24(%ebp)
499	je	newboot
500
501	/*
502	 * Seems we have been loaded by the old diskless boot code, we
503	 * don't stand a chance of running as the diskless structure
504	 * changed considerably between the two, so just halt.
505	 */
506	 hlt
507
508	/*
509	 * We have been loaded by the new uniform boot code.
510	 * Let's check the bootinfo version, and if we do not understand
511	 * it we return to the loader with a status of 1 to indicate this error
512	 */
513newboot:
514	movl	28(%ebp),%ebx		/* &bootinfo.version */
515	movl	BI_VERSION(%ebx),%eax
516	cmpl	$1,%eax			/* We only understand version 1 */
517	je	1f
518	movl	$1,%eax			/* Return status */
519	leave
520	/*
521	 * XXX this returns to our caller's caller (as is required) since
522	 * we didn't set up a frame and our caller did.
523	 */
524	ret
525
5261:
527	/*
528	 * If we have a kernelname copy it in
529	 */
530	movl	BI_KERNELNAME(%ebx),%esi
531	cmpl	$0,%esi
532	je	2f			/* No kernelname */
533	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
534	movl	$R(_kernelname),%edi
535	cmpb	$'/',(%esi)		/* Make sure it starts with a slash */
536	je	1f
537	movb	$'/',(%edi)
538	incl	%edi
539	decl	%ecx
5401:
541	cld
542	rep
543	movsb
544
5452:
546	/*
547	 * Determine the size of the boot loader's copy of the bootinfo
548	 * struct.  This is impossible to do properly because old versions
549	 * of the struct don't contain a size field and there are 2 old
550	 * versions with the same version number.
551	 */
552	movl	$BI_ENDCOMMON,%ecx	/* prepare for sizeless version */
553	testl	$RB_BOOTINFO,8(%ebp)	/* bi_size (and bootinfo) valid? */
554	je	got_bi_size		/* no, sizeless version */
555	movl	BI_SIZE(%ebx),%ecx
556got_bi_size:
557
558	/*
559	 * Copy the common part of the bootinfo struct
560	 */
561	movl	%ebx,%esi
562	movl	$R(_bootinfo),%edi
563	cmpl	$BOOTINFO_SIZE,%ecx
564	jbe	got_common_bi_size
565	movl	$BOOTINFO_SIZE,%ecx
566got_common_bi_size:
567	cld
568	rep
569	movsb
570
571#ifdef NFS_ROOT
572#ifndef BOOTP_NFSV3
573	/*
574	 * If we have a nfs_diskless structure copy it in
575	 */
576	movl	BI_NFS_DISKLESS(%ebx),%esi
577	cmpl	$0,%esi
578	je	olddiskboot
579	movl	$R(_nfs_diskless),%edi
580	movl	$NFSDISKLESS_SIZE,%ecx
581	cld
582	rep
583	movsb
584	movl	$R(_nfs_diskless_valid),%edi
585	movl	$1,(%edi)
586#endif
587#endif
588
589	/*
590	 * The old style disk boot.
591	 *	(*btext)(howto, bootdev, cyloffset, esym);
592	 * Note that the newer boot code just falls into here to pick
593	 * up howto and bootdev, cyloffset and esym are no longer used
594	 */
595olddiskboot:
596	movl	8(%ebp),%eax
597	movl	%eax,R(_boothowto)
598	movl	12(%ebp),%eax
599	movl	%eax,R(_bootdev)
600
601	ret
602
603
604/**********************************************************************
605 *
606 * Identify the CPU and initialize anything special about it
607 *
608 */
609identify_cpu:
610
611	/* Try to toggle alignment check flag; does not exist on 386. */
612	pushfl
613	popl	%eax
614	movl	%eax,%ecx
615	orl	$PSL_AC,%eax
616	pushl	%eax
617	popfl
618	pushfl
619	popl	%eax
620	xorl	%ecx,%eax
621	andl	$PSL_AC,%eax
622	pushl	%ecx
623	popfl
624
625	testl	%eax,%eax
626	jnz	try486
627
628	/* NexGen CPU does not have aligment check flag. */
629	pushfl
630	movl	$0x5555, %eax
631	xorl	%edx, %edx
632	movl	$2, %ecx
633	clc
634	divl	%ecx
635	jz	trynexgen
636	popfl
637	movl	$CPU_386,R(_cpu)
638	jmp	3f
639
640trynexgen:
641	popfl
642	movl	$CPU_NX586,R(_cpu)
643	movl	$0x4778654e,R(_cpu_vendor)	# store vendor string
644	movl	$0x72446e65,R(_cpu_vendor+4)
645	movl	$0x6e657669,R(_cpu_vendor+8)
646	movl	$0,R(_cpu_vendor+12)
647	jmp	3f
648
649try486:	/* Try to toggle identification flag; does not exist on early 486s. */
650	pushfl
651	popl	%eax
652	movl	%eax,%ecx
653	xorl	$PSL_ID,%eax
654	pushl	%eax
655	popfl
656	pushfl
657	popl	%eax
658	xorl	%ecx,%eax
659	andl	$PSL_ID,%eax
660	pushl	%ecx
661	popfl
662
663	testl	%eax,%eax
664	jnz	trycpuid
665	movl	$CPU_486,R(_cpu)
666
667	/*
668	 * Check Cyrix CPU
669	 * Cyrix CPUs do not change the undefined flags following
670	 * execution of the divide instruction which divides 5 by 2.
671	 *
672	 * Note: CPUID is enabled on M2, so it passes another way.
673	 */
674	pushfl
675	movl	$0x5555, %eax
676	xorl	%edx, %edx
677	movl	$2, %ecx
678	clc
679	divl	%ecx
680	jnc	trycyrix
681	popfl
682	jmp	3f		/* You may use Intel CPU. */
683
684trycyrix:
685	popfl
686	/*
687	 * IBM Bluelighting CPU also doesn't change the undefined flags.
688	 * Because IBM doesn't disclose the information for Bluelighting
689	 * CPU, we couldn't distinguish it from Cyrix's (including IBM
690	 * brand of Cyrix CPUs).
691	 */
692	movl	$0x69727943,R(_cpu_vendor)	# store vendor string
693	movl	$0x736e4978,R(_cpu_vendor+4)
694	movl	$0x64616574,R(_cpu_vendor+8)
695	jmp	3f
696
697trycpuid:	/* Use the `cpuid' instruction. */
698	xorl	%eax,%eax
699	.byte	0x0f,0xa2			# cpuid 0
700	movl	%eax,R(_cpu_high)		# highest capability
701	movl	%ebx,R(_cpu_vendor)		# store vendor string
702	movl	%edx,R(_cpu_vendor+4)
703	movl	%ecx,R(_cpu_vendor+8)
704	movb	$0,R(_cpu_vendor+12)
705
706	movl	$1,%eax
707	.byte	0x0f,0xa2			# cpuid 1
708	movl	%eax,R(_cpu_id)			# store cpu_id
709	movl	%edx,R(_cpu_feature)		# store cpu_feature
710	rorl	$8,%eax				# extract family type
711	andl	$15,%eax
712	cmpl	$5,%eax
713	jae	1f
714
715	/* less than Pentium; must be 486 */
716	movl	$CPU_486,R(_cpu)
717	jmp	3f
7181:
719	/* a Pentium? */
720	cmpl	$5,%eax
721	jne	2f
722	movl	$CPU_586,R(_cpu)
723	jmp	3f
7242:
725	/* Greater than Pentium...call it a Pentium Pro */
726	movl	$CPU_686,R(_cpu)
7273:
728	ret
729
730
731/**********************************************************************
732 *
733 * Create the first page directory and its page tables.
734 *
735 */
736
737create_pagetables:
738
739	testl	$CPUID_PGE, R(_cpu_feature)
740	jz	1f
741	movl	%cr4, %eax
742	orl	$CR4_PGE, %eax
743	movl	%eax, %cr4
7441:
745
746/* Find end of kernel image (rounded up to a page boundary). */
747	movl	$R(_end),%esi
748
749/* include symbols if loaded and useful */
750#ifdef DDB
751	movl	R(_bootinfo+BI_ESYMTAB),%edi
752	testl	%edi,%edi
753	je	over_symalloc
754	movl	%edi,%esi
755	movl	$KERNBASE,%edi
756	addl	%edi,R(_bootinfo+BI_SYMTAB)
757	addl	%edi,R(_bootinfo+BI_ESYMTAB)
758over_symalloc:
759#endif
760
761/* If we are told where the end of the kernel space is, believe it. */
762	movl	R(_bootinfo+BI_KERNEND),%edi
763	testl	%edi,%edi
764	je	no_kernend
765	movl	%edi,%esi
766no_kernend:
767
768	addl	$PAGE_MASK,%esi
769	andl	$~PAGE_MASK,%esi
770	movl	%esi,R(_KERNend)	/* save end of kernel */
771	movl	%esi,R(physfree)	/* next free page is at end of kernel */
772
773/* Allocate Kernel Page Tables */
774	ALLOCPAGES(NKPT)
775	movl	%esi,R(_KPTphys)
776
777/* Allocate Page Table Directory */
778	ALLOCPAGES(1)
779	movl	%esi,R(_IdlePTD)
780
781/* Allocate UPAGES */
782	ALLOCPAGES(UPAGES)
783	movl	%esi,R(p0upa)
784	addl	$KERNBASE, %esi
785	movl	%esi, R(_proc0paddr)
786
787#ifdef VM86
788	ALLOCPAGES(1)			/* vm86/bios stack */
789	movl	%esi,R(vm86phystk)
790
791	ALLOCPAGES(3)			/* pgtable + ext + IOPAGES */
792	movl	%esi,R(_vm86pa)
793	addl	$KERNBASE, %esi
794	movl	%esi, R(_vm86paddr)
795#endif /* VM86 */
796
797#ifdef SMP
798/* Allocate cpu0's private data page */
799	ALLOCPAGES(1)
800	movl	%esi,R(cpu0pp)
801	addl	$KERNBASE, %esi
802	movl	%esi, R(_cpu0prvpage)	/* relocated to KVM space */
803
804/* Allocate cpu0's private page table for mapping priv page, apic, etc */
805	ALLOCPAGES(1)
806	movl	%esi,R(cpu0pt)
807	addl	$KERNBASE, %esi
808	movl	%esi, R(_cpu0prvpt)	/* relocated to KVM space */
809#endif	/* SMP */
810
811/* Map read-only from zero to the end of the kernel text section */
812	xorl	%eax, %eax
813#ifdef BDE_DEBUGGER
814/* If the debugger is present, actually map everything read-write. */
815	cmpl	$0,R(_bdb_exists)
816	jne	map_read_write
817#endif
818	xorl	%edx,%edx
819
820#if !defined(SMP)
821	testl	$CPUID_PGE, R(_cpu_feature)
822	jz	2f
823	orl	$PG_G,%edx
824#endif
825
8262:	movl	$R(_etext),%ecx
827	addl	$PAGE_MASK,%ecx
828	shrl	$PAGE_SHIFT,%ecx
829	fillkptphys(%edx)
830
831/* Map read-write, data, bss and symbols */
832	movl	$R(_etext),%eax
833	addl	$PAGE_MASK, %eax
834	andl	$~PAGE_MASK, %eax
835map_read_write:
836	movl	$PG_RW,%edx
837#if !defined(SMP)
838	testl	$CPUID_PGE, R(_cpu_feature)
839	jz	1f
840	orl	$PG_G,%edx
841#endif
842
8431:	movl	R(_KERNend),%ecx
844	subl	%eax,%ecx
845	shrl	$PAGE_SHIFT,%ecx
846	fillkptphys(%edx)
847
848/* Map page directory. */
849	movl	R(_IdlePTD), %eax
850	movl	$1, %ecx
851	fillkptphys($PG_RW)
852
853/* Map proc0's UPAGES in the physical way ... */
854	movl	R(p0upa), %eax
855	movl	$UPAGES, %ecx
856	fillkptphys($PG_RW)
857
858/* Map ISA hole */
859	movl	$ISA_HOLE_START, %eax
860	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
861	fillkptphys($PG_RW)
862
863#ifdef VM86
864/* Map space for the vm86 region */
865	movl	R(vm86phystk), %eax
866	movl	$4, %ecx
867	fillkptphys($PG_RW)
868
869/* Map page 0 into the vm86 page table */
870	movl	$0, %eax
871	movl	$0, %ebx
872	movl	$1, %ecx
873	fillkpt(R(_vm86pa), $PG_RW|PG_U)
874
875/* ...likewise for the ISA hole */
876	movl	$ISA_HOLE_START, %eax
877	movl	$ISA_HOLE_START>>PAGE_SHIFT, %ebx
878	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
879	fillkpt(R(_vm86pa), $PG_RW|PG_U)
880#endif /* VM86 */
881
882#ifdef SMP
883/* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */
884	movl	R(cpu0pp), %eax
885	movl	$1, %ecx
886	fillkptphys($PG_RW)
887
888/* Map cpu0's private page table into global kmem FWIW */
889	movl	R(cpu0pt), %eax
890	movl	$1, %ecx
891	fillkptphys($PG_RW)
892
893/* Map the private page into the private page table into private space */
894	movl	R(cpu0pp), %eax
895	movl	$0, %ebx		/* pte offset = 0 */
896	movl	$1, %ecx		/* one private page coming right up */
897	fillkpt(R(cpu0pt), $PG_RW)
898
899/* Map the page table page into private space */
900	movl	R(cpu0pt), %eax
901	movl	$1, %ebx		/* pte offset = 1 */
902	movl	$1, %ecx		/* one private pt coming right up */
903	fillkpt(R(cpu0pt), $PG_RW)
904
905/* ... and put the page table table in the pde. */
906	movl	R(cpu0pt), %eax
907	movl	$MPPTDI, %ebx
908	movl	$1, %ecx
909	fillkpt(R(_IdlePTD), $PG_RW)
910
911/* Fakeup VA for the local apic to allow early traps. */
912	ALLOCPAGES(1)
913	movl	%esi, %eax
914	movl	$2, %ebx		/* pte offset = 2 */
915	movl	$1, %ecx		/* one private pt coming right up */
916	fillkpt(R(cpu0pt), $PG_RW)
917
918/* Initialize mp lock to allow early traps */
919	movl	$1, R(_mp_lock)
920
921/* Initialize my_idlePTD to IdlePTD */
922	movl	R(cpu0pp), %eax
923	movl	R(_IdlePTD), %ecx
924	movl	%ecx,GD_MY_IDLEPTD(%eax)
925/* Initialize IdlePTDS[0] */
926	addl	$KERNBASE, %ecx
927	movl	%ecx, R(CNAME(IdlePTDS))
928
929#endif	/* SMP */
930
931/* install a pde for temporary double map of bottom of VA */
932	movl	R(_KPTphys), %eax
933	xorl	%ebx, %ebx
934	movl	$1, %ecx
935	fillkpt(R(_IdlePTD), $PG_RW)
936
937/* install pde's for pt's */
938	movl	R(_KPTphys), %eax
939	movl	$KPTDI, %ebx
940	movl	$NKPT, %ecx
941	fillkpt(R(_IdlePTD), $PG_RW)
942
943/* install a pde recursively mapping page directory as a page table */
944	movl	R(_IdlePTD), %eax
945	movl	$PTDPTDI, %ebx
946	movl	$1,%ecx
947	fillkpt(R(_IdlePTD), $PG_RW)
948
949	ret
950
951#ifdef BDE_DEBUGGER
952bdb_prepare_paging:
953	cmpl	$0,R(_bdb_exists)
954	je	bdb_prepare_paging_exit
955
956	subl	$6,%esp
957
958	/*
959	 * Copy and convert debugger entries from the bootstrap gdt and idt
960	 * to the kernel gdt and idt.  Everything is still in low memory.
961	 * Tracing continues to work after paging is enabled because the
962	 * low memory addresses remain valid until everything is relocated.
963	 * However, tracing through the setidt() that initializes the trace
964	 * trap will crash.
965	 */
966	sgdt	(%esp)
967	movl	2(%esp),%esi		/* base address of bootstrap gdt */
968	movl	$R(_gdt),%edi
969	movl	%edi,2(%esp)		/* prepare to load kernel gdt */
970	movl	$8*18/4,%ecx
971	cld
972	rep				/* copy gdt */
973	movsl
974	movl	$R(_gdt),-8+2(%edi)	/* adjust gdt self-ptr */
975	movb	$0x92,-8+5(%edi)
976	lgdt	(%esp)
977
978	sidt	(%esp)
979	movl	2(%esp),%esi		/* base address of current idt */
980	movl	8+4(%esi),%eax		/* convert dbg descriptor to ... */
981	movw	8(%esi),%ax
982	movl	%eax,R(bdb_dbg_ljmp+1)	/* ... immediate offset ... */
983	movl	8+2(%esi),%eax
984	movw	%ax,R(bdb_dbg_ljmp+5)	/* ... and selector for ljmp */
985	movl	24+4(%esi),%eax		/* same for bpt descriptor */
986	movw	24(%esi),%ax
987	movl	%eax,R(bdb_bpt_ljmp+1)
988	movl	24+2(%esi),%eax
989	movw	%ax,R(bdb_bpt_ljmp+5)
990	movl	$R(_idt),%edi
991	movl	%edi,2(%esp)		/* prepare to load kernel idt */
992	movl	$8*4/4,%ecx
993	cld
994	rep				/* copy idt */
995	movsl
996	lidt	(%esp)
997
998	addl	$6,%esp
999
1000bdb_prepare_paging_exit:
1001	ret
1002
1003/* Relocate debugger gdt entries and gdt and idt pointers. */
1004bdb_commit_paging:
1005	cmpl	$0,_bdb_exists
1006	je	bdb_commit_paging_exit
1007
1008	movl	$_gdt+8*9,%eax		/* adjust slots 9-17 */
1009	movl	$9,%ecx
1010reloc_gdt:
1011	movb	$KERNBASE>>24,7(%eax)	/* top byte of base addresses, was 0, */
1012	addl	$8,%eax			/* now KERNBASE>>24 */
1013	loop	reloc_gdt
1014
1015	subl	$6,%esp
1016	sgdt	(%esp)
1017	addl	$KERNBASE,2(%esp)
1018	lgdt	(%esp)
1019	sidt	(%esp)
1020	addl	$KERNBASE,2(%esp)
1021	lidt	(%esp)
1022	addl	$6,%esp
1023
1024	int	$3
1025
1026bdb_commit_paging_exit:
1027	ret
1028
1029#endif /* BDE_DEBUGGER */
1030