locore.s revision 28808
1295009Sjkim/*-
2280297Sjkim * Copyright (c) 1990 The Regents of the University of California.
3280297Sjkim * All rights reserved.
4280297Sjkim *
5109998Smarkm * This code is derived from software contributed to Berkeley by
6109998Smarkm * William Jolitz.
7109998Smarkm *
8109998Smarkm * Redistribution and use in source and binary forms, with or without
9109998Smarkm * modification, are permitted provided that the following conditions
10109998Smarkm * are met:
11109998Smarkm * 1. Redistributions of source code must retain the above copyright
12109998Smarkm *    notice, this list of conditions and the following disclaimer.
13109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
14280297Sjkim *    notice, this list of conditions and the following disclaimer in the
15109998Smarkm *    documentation and/or other materials provided with the distribution.
16109998Smarkm * 3. All advertising materials mentioning features or use of this software
17109998Smarkm *    must display the following acknowledgement:
18109998Smarkm *	This product includes software developed by the University of
19109998Smarkm *	California, Berkeley and its contributors.
20109998Smarkm * 4. Neither the name of the University nor the names of its contributors
21109998Smarkm *    may be used to endorse or promote products derived from this software
22109998Smarkm *    without specific prior written permission.
23109998Smarkm *
24109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25109998Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27109998Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28109998Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29109998Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30109998Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32109998Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33109998Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34109998Smarkm * SUCH DAMAGE.
35109998Smarkm *
36109998Smarkm *	from: @(#)locore.s	7.3 (Berkeley) 5/13/91
37109998Smarkm *	$Id: locore.s,v 1.95 1997/08/24 00:05:33 fsmp Exp $
38109998Smarkm *
39109998Smarkm *		originally from: locore.s, by William F. Jolitz
40109998Smarkm *
41109998Smarkm *		Substantially rewritten by David Greenman, Rod Grimes,
42109998Smarkm *			Bruce Evans, Wolfgang Solfrank, Poul-Henning Kamp
43109998Smarkm *			and many others.
44109998Smarkm */
45109998Smarkm
46109998Smarkm#include "apm.h"
47109998Smarkm#include "opt_cpu.h"
48109998Smarkm#include "opt_ddb.h"
49109998Smarkm#include "opt_userconfig.h"
50109998Smarkm
51109998Smarkm#include <sys/syscall.h>
52109998Smarkm#include <sys/reboot.h>
53109998Smarkm
54109998Smarkm#include <machine/asmacros.h>
55109998Smarkm#include <machine/cputypes.h>
56109998Smarkm#include <machine/psl.h>
57109998Smarkm#include <machine/pmap.h>
58109998Smarkm#include <machine/specialreg.h>
59109998Smarkm
60109998Smarkm#include "assym.s"
61280297Sjkim
62109998Smarkm/*
63280297Sjkim *	XXX
64280297Sjkim *
65109998Smarkm * Note: This version greatly munged to avoid various assembler errors
66109998Smarkm * that may be fixed in newer versions of gas. Perhaps newer versions
67109998Smarkm * will have more pleasant appearance.
68109998Smarkm */
69109998Smarkm
70280297Sjkim/*
71280297Sjkim * PTmap is recursive pagemap at top of virtual address space.
72280297Sjkim * Within PTmap, the page directory can be found (third indirection).
73280297Sjkim */
74109998Smarkm	.globl	_PTmap,_PTD,_PTDpde
75280297Sjkim	.set	_PTmap,(PTDPTDI << PDRSHIFT)
76280297Sjkim	.set	_PTD,_PTmap + (PTDPTDI * PAGE_SIZE)
77280297Sjkim	.set	_PTDpde,_PTD + (PTDPTDI * PDESIZE)
78280297Sjkim
79109998Smarkm/*
80280297Sjkim * APTmap, APTD is the alternate recursive pagemap.
81280297Sjkim * It's used when modifying another process's page tables.
82280297Sjkim */
83280297Sjkim	.globl	_APTmap,_APTD,_APTDpde
84109998Smarkm	.set	_APTmap,APTDPTDI << PDRSHIFT
85109998Smarkm	.set	_APTD,_APTmap + (APTDPTDI * PAGE_SIZE)
86109998Smarkm	.set	_APTDpde,_PTD + (APTDPTDI * PDESIZE)
87109998Smarkm
88109998Smarkm#ifdef SMP
89	.globl	_SMP_prvstart
90	.set	_SMP_prvstart,(MPPTDI << PDRSHIFT)
91
92	.globl	_SMP_prvpage,_SMP_prvpt,_lapic,_SMP_ioapic
93	.globl	_prv_CPAGE1,_prv_CPAGE2,_prv_CPAGE3
94	.globl	_idlestack,_idlestack_top
95	.set	_SMP_prvpage,_SMP_prvstart
96	.set	_SMP_prvpt,_SMP_prvstart + PAGE_SIZE
97	.set	_lapic,_SMP_prvstart + (2 * PAGE_SIZE)
98	.set	_idlestack,_SMP_prvstart + (3 * PAGE_SIZE)
99	.set	_idlestack_top,_SMP_prvstart + (4 * PAGE_SIZE)
100	.set	_prv_CPAGE1,_SMP_prvstart + (4 * PAGE_SIZE)
101	.set	_prv_CPAGE2,_SMP_prvstart + (5 * PAGE_SIZE)
102	.set	_prv_CPAGE3,_SMP_prvstart + (6 * PAGE_SIZE)
103	.set	_SMP_ioapic,_SMP_prvstart + (16 * PAGE_SIZE)
104
105	.globl	_cpuid,_curproc,_curpcb,_npxproc,_runtime,_cpu_lockid
106	.globl	_common_tss,_other_cpus,_my_idlePTD,_ss_tpr
107	.globl	_prv_CMAP1,_prv_CMAP2,_prv_CMAP3
108	.set	_cpuid,_SMP_prvpage+0		/* [0] */
109	.set	_curproc,_SMP_prvpage+4		/* [1] */
110	.set	_curpcb,_SMP_prvpage+8		/* [2] */
111	.set	_npxproc,_SMP_prvpage+12	/* [3] */
112	.set	_runtime,_SMP_prvpage+16	/* [4,5] */
113	.set	_cpu_lockid,_SMP_prvpage+24	/* [6] */
114	.set	_other_cpus,_SMP_prvpage+28	/* [7] bitmap of available CPUs,
115						    excluding ourself */
116	.set	_my_idlePTD,_SMP_prvpage+32	/* [8] */
117	.set	_ss_tpr,_SMP_prvpage+36		/* [9] */
118	.set	_prv_CMAP1,_SMP_prvpage+40	/* [10] */
119	.set	_prv_CMAP2,_SMP_prvpage+44	/* [11] */
120	.set	_prv_CMAP3,_SMP_prvpage+48	/* [12] */
121	.set	_common_tss,_SMP_prvpage+52	/* 102 (ie: 104) bytes long */
122
123/* Fetch the .set's for the local apic */
124#include "i386/i386/mp_apicdefs.s"
125
126#endif
127
128/*
129 * Globals
130 */
131	.data
132	ALIGN_DATA		/* just to be sure */
133
134	.globl	HIDENAME(tmpstk)
135	.space	0x2000		/* space for tmpstk - temporary stack */
136HIDENAME(tmpstk):
137
138	.globl	_boothowto,_bootdev
139
140	.globl	_cpu,_cpu_vendor,_cpu_id,_bootinfo
141	.globl	_cpu_high, _cpu_feature
142
143_cpu:	.long	0				/* are we 386, 386sx, or 486 */
144_cpu_id:	.long	0			/* stepping ID */
145_cpu_high:	.long	0			/* highest arg to CPUID */
146_cpu_feature:	.long	0			/* features */
147_cpu_vendor:	.space	20			/* CPU origin code */
148_bootinfo:	.space	BOOTINFO_SIZE		/* bootinfo that we can handle */
149
150_KERNend:	.long	0			/* phys addr end of kernel (just after bss) */
151physfree:	.long	0			/* phys addr of next free page */
152
153#ifdef SMP
154cpu0pp:		.long	0			/* phys addr cpu0 private pg */
155cpu0pt:		.long	0			/* phys addr cpu0 private pt */
156
157		.globl	_cpu0prvpage,_cpu0prvpt
158_cpu0prvpage:	.long	0			/* relocated version */
159_cpu0prvpt:	.long	0			/* relocated version */
160#endif /* SMP */
161
162	.globl	_IdlePTD
163_IdlePTD:	.long	0			/* phys addr of kernel PTD */
164
165#ifdef SMP
166	.globl	_KPTphys
167#endif
168_KPTphys:	.long	0			/* phys addr of kernel page tables */
169
170	.globl	_proc0paddr
171_proc0paddr:	.long	0			/* address of proc 0 address space */
172p0upa:		.long	0			/* phys addr of proc0's UPAGES */
173
174#ifdef BDE_DEBUGGER
175	.globl	_bdb_exists			/* flag to indicate BDE debugger is present */
176_bdb_exists:	.long	0
177#endif
178
179
180/**********************************************************************
181 *
182 * Some handy macros
183 *
184 */
185
186#define R(foo) ((foo)-KERNBASE)
187
188#define ALLOCPAGES(foo) \
189	movl	R(physfree), %esi ; \
190	movl	$((foo)*PAGE_SIZE), %eax ; \
191	addl	%esi, %eax ; \
192	movl	%eax, R(physfree) ; \
193	movl	%esi, %edi ; \
194	movl	$((foo)*PAGE_SIZE),%ecx ; \
195	xorl	%eax,%eax ; \
196	cld ; \
197	rep ; \
198	stosb
199
200/*
201 * fillkpt
202 *	eax = page frame address
203 *	ebx = index into page table
204 *	ecx = how many pages to map
205 * 	base = base address of page dir/table
206 *	prot = protection bits
207 */
208#define	fillkpt(base, prot)		  \
209	shll	$2,%ebx			; \
210	addl	base,%ebx		; \
211	orl	$PG_V,%eax		; \
212	orl	prot,%eax		; \
2131:	movl	%eax,(%ebx)		; \
214	addl	$PAGE_SIZE,%eax		; /* increment physical address */ \
215	addl	$4,%ebx			; /* next pte */ \
216	loop	1b
217
218/*
219 * fillkptphys(prot)
220 *	eax = physical address
221 *	ecx = how many pages to map
222 *	prot = protection bits
223 */
224#define	fillkptphys(prot)		  \
225	movl	%eax, %ebx		; \
226	shrl	$PAGE_SHIFT, %ebx	; \
227	fillkpt(R(_KPTphys), prot)
228
229	.text
230/**********************************************************************
231 *
232 * This is where the bootblocks start us, set the ball rolling...
233 *
234 */
235NON_GPROF_ENTRY(btext)
236
237#ifdef PC98
238	jmp	1f
239	.globl	_pc98_system_parameter
240	.org	0x400
241_pc98_system_parameter:
242	.space	0x240		/* BIOS parameter block */
2431:
244	/* save SYSTEM PARAMETER for resume (NS/T or other) */
245	movl	$0xa1000,%esi
246	movl	$0x100000,%edi
247	movl	$0x0630,%ecx
248	cld
249	rep
250	movsb
251#else	/* IBM-PC */
252#ifdef BDE_DEBUGGER
253#ifdef BIOS_STEALS_3K
254	cmpl	$0x0375c339,0x95504
255#else
256	cmpl	$0x0375c339,0x96104	/* XXX - debugger signature */
257#endif
258	jne	1f
259	movb	$1,R(_bdb_exists)
2601:
261#endif
262
263/* Tell the bios to warmboot next time */
264	movw	$0x1234,0x472
265#endif	/* PC98 */
266
267/* Set up a real frame in case the double return in newboot is executed. */
268	pushl	%ebp
269	movl	%esp, %ebp
270
271/* Don't trust what the BIOS gives for eflags. */
272	pushl	$PSL_KERNEL
273	popfl
274
275/*
276 * Don't trust what the BIOS gives for %fs and %gs.  Trust the bootstrap
277 * to set %cs, %ds, %es and %ss.
278 */
279	mov	%ds, %ax
280	mov	%ax, %fs
281	mov	%ax, %gs
282
283	call	recover_bootinfo
284
285/* Get onto a stack that we can trust. */
286/*
287 * XXX this step is delayed in case recover_bootinfo needs to return via
288 * the old stack, but it need not be, since recover_bootinfo actually
289 * returns via the old frame.
290 */
291	movl	$R(HIDENAME(tmpstk)),%esp
292
293#ifdef PC98
294	testb	$0x02,0x100620		/* pc98_machine_type & M_EPSON_PC98 */
295	jz	3f
296	cmpb	$0x0b,0x100624		/* epson_machine_id <= 0x0b */
297	ja	3f
298
299	/* count up memory */
300	movl	$0x100000,%eax		/* next, talley remaining memory */
301	movl	$0xFFF-0x100,%ecx
3021:	movl	0(%eax),%ebx		/* save location to check */
303	movl	$0xa55a5aa5,0(%eax)	/* write test pattern */
304	cmpl	$0xa55a5aa5,0(%eax)	/* does not check yet for rollover */
305	jne	2f
306	movl	%ebx,0(%eax)		/* restore memory */
307	addl	$PAGE_SIZE,%eax
308	loop	1b
3092:	subl	$0x100000,%eax
310	shrl	$17,%eax
311	movb	%al,0x100401
3123:
313#endif
314
315	call	identify_cpu
316
317/* clear bss */
318/*
319 * XXX this should be done a little earlier.
320 *
321 * XXX we don't check that there is memory for our bss and page tables
322 * before using it.
323 *
324 * XXX the boot program somewhat bogusly clears the bss.  We still have
325 * to do it in case we were unzipped by kzipboot.  Then the boot program
326 * only clears kzipboot's bss.
327 *
328 * XXX the gdt and idt are still somewhere in the boot program.  We
329 * depend on the convention that the boot program is below 1MB and we
330 * are above 1MB to keep the gdt and idt  away from the bss and page
331 * tables.  The idt is only used if BDE_DEBUGGER is enabled.
332 */
333	movl	$R(_end),%ecx
334	movl	$R(_edata),%edi
335	subl	%edi,%ecx
336	xorl	%eax,%eax
337	cld
338	rep
339	stosb
340
341#if NAPM > 0
342/*
343 * XXX it's not clear that APM can live in the current environonment.
344 * Only pc-relative addressing works.
345 */
346	call	_apm_setup
347#endif
348
349	call	create_pagetables
350
351#ifdef VM86
352/*
353 * If the CPU has support for VME, turn it on.
354 */
355	testl	$CPUID_VME, R(_cpu_feature)
356	jz	1f
357	movl	%cr4, %eax
358	orl	$CR4_VME, %eax
359	movl	%eax, %cr4
3601:
361#endif /* VM86 */
362
363#ifdef BDE_DEBUGGER
364/*
365 * Adjust as much as possible for paging before enabling paging so that the
366 * adjustments can be traced.
367 */
368	call	bdb_prepare_paging
369#endif
370
371/* Now enable paging */
372	movl	R(_IdlePTD), %eax
373	movl	%eax,%cr3			/* load ptd addr into mmu */
374	movl	%cr0,%eax			/* get control word */
375	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
376	movl	%eax,%cr0			/* and let's page NOW! */
377
378#ifdef BDE_DEBUGGER
379/*
380 * Complete the adjustments for paging so that we can keep tracing through
381 * initi386() after the low (physical) addresses for the gdt and idt become
382 * invalid.
383 */
384	call	bdb_commit_paging
385#endif
386
387	pushl	$begin				/* jump to high virtualized address */
388	ret
389
390/* now running relocated at KERNBASE where the system is linked to run */
391begin:
392	/* set up bootstrap stack */
393	movl	_proc0paddr,%esp	/* location of in-kernel pages */
394	addl	$UPAGES*PAGE_SIZE,%esp	/* bootstrap stack end location */
395	xorl	%eax,%eax			/* mark end of frames */
396	movl	%eax,%ebp
397	movl	_proc0paddr,%eax
398	movl	_IdlePTD, %esi
399	movl	%esi,PCB_CR3(%eax)
400
401	movl	physfree, %esi
402	pushl	%esi				/* value of first for init386(first) */
403	call	_init386			/* wire 386 chip for unix operation */
404	popl	%esi
405
406	.globl	__ucodesel,__udatasel
407
408	pushl	$0				/* unused */
409	pushl	__udatasel			/* ss */
410	pushl	$0				/* esp - filled in by execve() */
411	pushl	$PSL_USER			/* eflags (IOPL 0, int enab) */
412	pushl	__ucodesel			/* cs */
413	pushl	$0				/* eip - filled in by execve() */
414	subl	$(12*4),%esp			/* space for rest of registers */
415
416	pushl	%esp				/* call main with frame pointer */
417	call	_main				/* autoconfiguration, mountroot etc */
418
419	hlt		/* never returns to here */
420
421/*
422 * When starting init, call this to configure the process for user
423 * mode.  This will be inherited by other processes.
424 */
425NON_GPROF_ENTRY(prepare_usermode)
426	/*
427	 * Now we've run main() and determined what cpu-type we are, we can
428	 * enable write protection and alignment checking on i486 cpus and
429	 * above.
430	 */
431#if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
432	cmpl    $CPUCLASS_386,_cpu_class
433	je	1f
434	movl	%cr0,%eax			/* get control word */
435	orl	$CR0_WP|CR0_AM,%eax		/* enable i486 features */
436	movl	%eax,%cr0			/* and do it */
4371:
438#endif
439	/*
440	 * on return from main(), we are process 1
441	 * set up address space and stack so that we can 'return' to user mode
442	 */
443	movl	__ucodesel,%eax
444	movl	__udatasel,%ecx
445
446#if 0
447	movl	%cx,%ds
448#endif
449	movl	%cx,%es
450	movl	%ax,%fs				/* double map cs to fs */
451	movl	%cx,%gs				/* and ds to gs */
452	ret					/* goto user! */
453
454
455#define LCALL(x,y)	.byte 0x9a ; .long y ; .word x
456
457/*
458 * Signal trampoline, copied to top of user stack
459 */
460NON_GPROF_ENTRY(sigcode)
461	call	SIGF_HANDLER(%esp)
462	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
463						/* copy at 8(%esp)) */
464	pushl	%eax
465	pushl	%eax				/* junk to fake return address */
466	movl	$SYS_sigreturn,%eax		/* sigreturn() */
467	LCALL(0x7,0)				/* enter kernel with args on stack */
468	hlt					/* never gets here */
469	ALIGN_TEXT
470_esigcode:
471
472	.data
473	.globl	_szsigcode
474_szsigcode:
475	.long	_esigcode-_sigcode
476	.text
477
478/**********************************************************************
479 *
480 * Recover the bootinfo passed to us from the boot program
481 *
482 */
483recover_bootinfo:
484	/*
485	 * This code is called in different ways depending on what loaded
486	 * and started the kernel.  This is used to detect how we get the
487	 * arguments from the other code and what we do with them.
488	 *
489	 * Old disk boot blocks:
490	 *	(*btext)(howto, bootdev, cyloffset, esym);
491	 *	[return address == 0, and can NOT be returned to]
492	 *	[cyloffset was not supported by the FreeBSD boot code
493	 *	 and always passed in as 0]
494	 *	[esym is also known as total in the boot code, and
495	 *	 was never properly supported by the FreeBSD boot code]
496	 *
497	 * Old diskless netboot code:
498	 *	(*btext)(0,0,0,0,&nfsdiskless,0,0,0);
499	 *	[return address != 0, and can NOT be returned to]
500	 *	If we are being booted by this code it will NOT work,
501	 *	so we are just going to halt if we find this case.
502	 *
503	 * New uniform boot code:
504	 *	(*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
505	 *	[return address != 0, and can be returned to]
506	 *
507	 * There may seem to be a lot of wasted arguments in here, but
508	 * that is so the newer boot code can still load very old kernels
509	 * and old boot code can load new kernels.
510	 */
511
512	/*
513	 * The old style disk boot blocks fake a frame on the stack and
514	 * did an lret to get here.  The frame on the stack has a return
515	 * address of 0.
516	 */
517	cmpl	$0,4(%ebp)
518	je	olddiskboot
519
520	/*
521	 * We have some form of return address, so this is either the
522	 * old diskless netboot code, or the new uniform code.  That can
523	 * be detected by looking at the 5th argument, if it is 0
524	 * we are being booted by the new uniform boot code.
525	 */
526	cmpl	$0,24(%ebp)
527	je	newboot
528
529	/*
530	 * Seems we have been loaded by the old diskless boot code, we
531	 * don't stand a chance of running as the diskless structure
532	 * changed considerably between the two, so just halt.
533	 */
534	 hlt
535
536	/*
537	 * We have been loaded by the new uniform boot code.
538	 * Let's check the bootinfo version, and if we do not understand
539	 * it we return to the loader with a status of 1 to indicate this error
540	 */
541newboot:
542	movl	28(%ebp),%ebx		/* &bootinfo.version */
543	movl	BI_VERSION(%ebx),%eax
544	cmpl	$1,%eax			/* We only understand version 1 */
545	je	1f
546	movl	$1,%eax			/* Return status */
547	leave
548	/*
549	 * XXX this returns to our caller's caller (as is required) since
550	 * we didn't set up a frame and our caller did.
551	 */
552	ret
553
5541:
555	/*
556	 * If we have a kernelname copy it in
557	 */
558	movl	BI_KERNELNAME(%ebx),%esi
559	cmpl	$0,%esi
560	je	2f			/* No kernelname */
561	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
562	movl	$R(_kernelname),%edi
563	cmpb	$'/',(%esi)		/* Make sure it starts with a slash */
564	je	1f
565	movb	$'/',(%edi)
566	incl	%edi
567	decl	%ecx
5681:
569	cld
570	rep
571	movsb
572
5732:
574	/*
575	 * Determine the size of the boot loader's copy of the bootinfo
576	 * struct.  This is impossible to do properly because old versions
577	 * of the struct don't contain a size field and there are 2 old
578	 * versions with the same version number.
579	 */
580	movl	$BI_ENDCOMMON,%ecx	/* prepare for sizeless version */
581	testl	$RB_BOOTINFO,8(%ebp)	/* bi_size (and bootinfo) valid? */
582	je	got_bi_size		/* no, sizeless version */
583	movl	BI_SIZE(%ebx),%ecx
584got_bi_size:
585
586	/*
587	 * Copy the common part of the bootinfo struct
588	 */
589	movl	%ebx,%esi
590	movl	$R(_bootinfo),%edi
591	cmpl	$BOOTINFO_SIZE,%ecx
592	jbe	got_common_bi_size
593	movl	$BOOTINFO_SIZE,%ecx
594got_common_bi_size:
595	cld
596	rep
597	movsb
598
599#ifdef NFS
600#ifndef BOOTP_NFSV3
601	/*
602	 * If we have a nfs_diskless structure copy it in
603	 */
604	movl	BI_NFS_DISKLESS(%ebx),%esi
605	cmpl	$0,%esi
606	je	olddiskboot
607	movl	$R(_nfs_diskless),%edi
608	movl	$NFSDISKLESS_SIZE,%ecx
609	cld
610	rep
611	movsb
612	movl	$R(_nfs_diskless_valid),%edi
613	movl	$1,(%edi)
614#endif
615#endif
616
617	/*
618	 * The old style disk boot.
619	 *	(*btext)(howto, bootdev, cyloffset, esym);
620	 * Note that the newer boot code just falls into here to pick
621	 * up howto and bootdev, cyloffset and esym are no longer used
622	 */
623olddiskboot:
624	movl	8(%ebp),%eax
625	movl	%eax,R(_boothowto)
626	movl	12(%ebp),%eax
627	movl	%eax,R(_bootdev)
628
629#if defined(USERCONFIG_BOOT) && defined(USERCONFIG)
630#ifdef PC98
631	movl	$0x90200, %esi
632#else
633	movl	$0x10200, %esi
634#endif
635	movl	$R(_userconfig_from_boot),%edi
636	movl	$512,%ecx
637	cld
638	rep
639	movsb
640#endif /* USERCONFIG_BOOT */
641
642	ret
643
644
645/**********************************************************************
646 *
647 * Identify the CPU and initialize anything special about it
648 *
649 */
650identify_cpu:
651
652	/* Try to toggle alignment check flag; does not exist on 386. */
653	pushfl
654	popl	%eax
655	movl	%eax,%ecx
656	orl	$PSL_AC,%eax
657	pushl	%eax
658	popfl
659	pushfl
660	popl	%eax
661	xorl	%ecx,%eax
662	andl	$PSL_AC,%eax
663	pushl	%ecx
664	popfl
665
666	testl	%eax,%eax
667	jnz	try486
668
669	/* NexGen CPU does not have aligment check flag. */
670	pushfl
671	movl	$0x5555, %eax
672	xorl	%edx, %edx
673	movl	$2, %ecx
674	clc
675	divl	%ecx
676	jz	trynexgen
677	popfl
678	movl	$CPU_386,R(_cpu)
679	jmp	3f
680
681trynexgen:
682	popfl
683	movl	$CPU_NX586,R(_cpu)
684	movl	$0x4778654e,R(_cpu_vendor)	# store vendor string
685	movl	$0x72446e65,R(_cpu_vendor+4)
686	movl	$0x6e657669,R(_cpu_vendor+8)
687	movl	$0,R(_cpu_vendor+12)
688	jmp	3f
689
690try486:	/* Try to toggle identification flag; does not exist on early 486s. */
691	pushfl
692	popl	%eax
693	movl	%eax,%ecx
694	xorl	$PSL_ID,%eax
695	pushl	%eax
696	popfl
697	pushfl
698	popl	%eax
699	xorl	%ecx,%eax
700	andl	$PSL_ID,%eax
701	pushl	%ecx
702	popfl
703
704	testl	%eax,%eax
705	jnz	trycpuid
706	movl	$CPU_486,R(_cpu)
707
708	/*
709	 * Check Cyrix CPU
710	 * Cyrix CPUs do not change the undefined flags following
711	 * execution of the divide instruction which divides 5 by 2.
712	 *
713	 * Note: CPUID is enabled on M2, so it passes another way.
714	 */
715	pushfl
716	movl	$0x5555, %eax
717	xorl	%edx, %edx
718	movl	$2, %ecx
719	clc
720	divl	%ecx
721	jnc	trycyrix
722	popfl
723	jmp	3f		/* You may use Intel CPU. */
724
725trycyrix:
726	popfl
727	/*
728	 * IBM Bluelighting CPU also doesn't change the undefined flags.
729	 * Because IBM doesn't disclose the information for Bluelighting
730	 * CPU, we couldn't distinguish it from Cyrix's (including IBM
731	 * brand of Cyrix CPUs).
732	 */
733	movl	$0x69727943,R(_cpu_vendor)	# store vendor string
734	movl	$0x736e4978,R(_cpu_vendor+4)
735	movl	$0x64616574,R(_cpu_vendor+8)
736	jmp	3f
737
738trycpuid:	/* Use the `cpuid' instruction. */
739	xorl	%eax,%eax
740	.byte	0x0f,0xa2			# cpuid 0
741	movl	%eax,R(_cpu_high)		# highest capability
742	movl	%ebx,R(_cpu_vendor)		# store vendor string
743	movl	%edx,R(_cpu_vendor+4)
744	movl	%ecx,R(_cpu_vendor+8)
745	movb	$0,R(_cpu_vendor+12)
746
747	movl	$1,%eax
748	.byte	0x0f,0xa2			# cpuid 1
749	movl	%eax,R(_cpu_id)			# store cpu_id
750	movl	%edx,R(_cpu_feature)		# store cpu_feature
751	rorl	$8,%eax				# extract family type
752	andl	$15,%eax
753	cmpl	$5,%eax
754	jae	1f
755
756	/* less than Pentium; must be 486 */
757	movl	$CPU_486,R(_cpu)
758	jmp	3f
7591:
760	/* a Pentium? */
761	cmpl	$5,%eax
762	jne	2f
763	movl	$CPU_586,R(_cpu)
764	jmp	3f
7652:
766	/* Greater than Pentium...call it a Pentium Pro */
767	movl	$CPU_686,R(_cpu)
7683:
769	ret
770
771
772/**********************************************************************
773 *
774 * Create the first page directory and its page tables.
775 *
776 */
777
778create_pagetables:
779
780	testl	$CPUID_PGE, R(_cpu_feature)
781	jz	1f
782	movl	%cr4, %eax
783	orl	$CR4_PGE, %eax
784	movl	%eax, %cr4
7851:
786
787/* Find end of kernel image (rounded up to a page boundary). */
788	movl	$R(_end),%esi
789
790/* include symbols in "kernel image" if they are loaded and useful */
791#ifdef DDB
792	movl	R(_bootinfo+BI_ESYMTAB),%edi
793	testl	%edi,%edi
794	je	over_symalloc
795	movl	%edi,%esi
796	movl	$KERNBASE,%edi
797	addl	%edi,R(_bootinfo+BI_SYMTAB)
798	addl	%edi,R(_bootinfo+BI_ESYMTAB)
799over_symalloc:
800#endif
801
802	addl	$PAGE_MASK,%esi
803	andl	$~PAGE_MASK,%esi
804	movl	%esi,R(_KERNend)	/* save end of kernel */
805	movl	%esi,R(physfree)	/* next free page is at end of kernel */
806
807/* Allocate Kernel Page Tables */
808	ALLOCPAGES(NKPT)
809	movl	%esi,R(_KPTphys)
810
811/* Allocate Page Table Directory */
812	ALLOCPAGES(1)
813	movl	%esi,R(_IdlePTD)
814
815/* Allocate UPAGES */
816	ALLOCPAGES(UPAGES)
817	movl	%esi,R(p0upa)
818	addl	$KERNBASE, %esi
819	movl	%esi, R(_proc0paddr)
820
821#ifdef SMP
822/* Allocate cpu0's private data page */
823	ALLOCPAGES(1)
824	movl	%esi,R(cpu0pp)
825	addl	$KERNBASE, %esi
826	movl	%esi, R(_cpu0prvpage)	/* relocated to KVM space */
827
828/* Allocate cpu0's private page table for mapping priv page, apic, etc */
829	ALLOCPAGES(1)
830	movl	%esi,R(cpu0pt)
831	addl	$KERNBASE, %esi
832	movl	%esi, R(_cpu0prvpt)	/* relocated to KVM space */
833#endif	/* SMP */
834
835/* Map read-only from zero to the end of the kernel text section */
836	xorl	%eax, %eax
837#ifdef BDE_DEBUGGER
838/* If the debugger is present, actually map everything read-write. */
839	cmpl	$0,R(_bdb_exists)
840	jne	map_read_write
841#endif
842	xorl	%edx,%edx
843
844#if !defined(SMP)
845	testl	$CPUID_PGE, R(_cpu_feature)
846	jz	2f
847	orl	$PG_G,%edx
848#endif
849
8502:	movl	$R(_etext),%ecx
851	addl	$PAGE_MASK,%ecx
852	shrl	$PAGE_SHIFT,%ecx
853	fillkptphys(%edx)
854
855/* Map read-write, data, bss and symbols */
856	movl	$R(_etext),%eax
857	addl	$PAGE_MASK, %eax
858	andl	$~PAGE_MASK, %eax
859map_read_write:
860	movl	$PG_RW,%edx
861#if !defined(SMP)
862	testl	$CPUID_PGE, R(_cpu_feature)
863	jz	1f
864	orl	$PG_G,%edx
865#endif
866
8671:	movl	R(_KERNend),%ecx
868	subl	%eax,%ecx
869	shrl	$PAGE_SHIFT,%ecx
870	fillkptphys(%edx)
871
872/* Map page directory. */
873	movl	R(_IdlePTD), %eax
874	movl	$1, %ecx
875	fillkptphys($PG_RW)
876
877/* Map proc0's UPAGES in the physical way ... */
878	movl	R(p0upa), %eax
879	movl	$UPAGES, %ecx
880	fillkptphys($PG_RW)
881
882/* Map ISA hole */
883	movl	$ISA_HOLE_START, %eax
884	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
885	fillkptphys($PG_RW)
886
887#ifdef SMP
888/* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */
889	movl	R(cpu0pp), %eax
890	movl	$1, %ecx
891	fillkptphys($PG_RW)
892
893/* Map cpu0's private page table into global kmem FWIW */
894	movl	R(cpu0pt), %eax
895	movl	$1, %ecx
896	fillkptphys($PG_RW)
897
898/* Map the private page into the private page table into private space */
899	movl	R(cpu0pp), %eax
900	movl	$0, %ebx		/* pte offset = 0 */
901	movl	$1, %ecx		/* one private page coming right up */
902	fillkpt(R(cpu0pt), $PG_RW)
903
904/* Map the page table page into private space */
905	movl	R(cpu0pt), %eax
906	movl	$1, %ebx		/* pte offset = 1 */
907	movl	$1, %ecx		/* one private pt coming right up */
908	fillkpt(R(cpu0pt), $PG_RW)
909
910/* ... and put the page table table in the pde. */
911	movl	R(cpu0pt), %eax
912	movl	$MPPTDI, %ebx
913	movl	$1, %ecx
914	fillkpt(R(_IdlePTD), $PG_RW)
915#endif	/* SMP */
916
917/* install a pde for temporary double map of bottom of VA */
918	movl	R(_KPTphys), %eax
919	xorl	%ebx, %ebx
920	movl	$1, %ecx
921	fillkpt(R(_IdlePTD), $PG_RW)
922
923/* install pde's for pt's */
924	movl	R(_KPTphys), %eax
925	movl	$KPTDI, %ebx
926	movl	$NKPT, %ecx
927	fillkpt(R(_IdlePTD), $PG_RW)
928
929/* install a pde recursively mapping page directory as a page table */
930	movl	R(_IdlePTD), %eax
931	movl	$PTDPTDI, %ebx
932	movl	$1,%ecx
933	fillkpt(R(_IdlePTD), $PG_RW)
934
935	ret
936
937#ifdef BDE_DEBUGGER
938bdb_prepare_paging:
939	cmpl	$0,R(_bdb_exists)
940	je	bdb_prepare_paging_exit
941
942	subl	$6,%esp
943
944	/*
945	 * Copy and convert debugger entries from the bootstrap gdt and idt
946	 * to the kernel gdt and idt.  Everything is still in low memory.
947	 * Tracing continues to work after paging is enabled because the
948	 * low memory addresses remain valid until everything is relocated.
949	 * However, tracing through the setidt() that initializes the trace
950	 * trap will crash.
951	 */
952	sgdt	(%esp)
953	movl	2(%esp),%esi		/* base address of bootstrap gdt */
954	movl	$R(_gdt),%edi
955	movl	%edi,2(%esp)		/* prepare to load kernel gdt */
956	movl	$8*18/4,%ecx
957	cld
958	rep				/* copy gdt */
959	movsl
960	movl	$R(_gdt),-8+2(%edi)	/* adjust gdt self-ptr */
961	movb	$0x92,-8+5(%edi)
962	lgdt	(%esp)
963
964	sidt	(%esp)
965	movl	2(%esp),%esi		/* base address of current idt */
966	movl	8+4(%esi),%eax		/* convert dbg descriptor to ... */
967	movw	8(%esi),%ax
968	movl	%eax,R(bdb_dbg_ljmp+1)	/* ... immediate offset ... */
969	movl	8+2(%esi),%eax
970	movw	%ax,R(bdb_dbg_ljmp+5)	/* ... and selector for ljmp */
971	movl	24+4(%esi),%eax		/* same for bpt descriptor */
972	movw	24(%esi),%ax
973	movl	%eax,R(bdb_bpt_ljmp+1)
974	movl	24+2(%esi),%eax
975	movw	%ax,R(bdb_bpt_ljmp+5)
976	movl	$R(_idt),%edi
977	movl	%edi,2(%esp)		/* prepare to load kernel idt */
978	movl	$8*4/4,%ecx
979	cld
980	rep				/* copy idt */
981	movsl
982	lidt	(%esp)
983
984	addl	$6,%esp
985
986bdb_prepare_paging_exit:
987	ret
988
989/* Relocate debugger gdt entries and gdt and idt pointers. */
990bdb_commit_paging:
991	cmpl	$0,_bdb_exists
992	je	bdb_commit_paging_exit
993
994	movl	$_gdt+8*9,%eax		/* adjust slots 9-17 */
995	movl	$9,%ecx
996reloc_gdt:
997	movb	$KERNBASE>>24,7(%eax)	/* top byte of base addresses, was 0, */
998	addl	$8,%eax			/* now KERNBASE>>24 */
999	loop	reloc_gdt
1000
1001	subl	$6,%esp
1002	sgdt	(%esp)
1003	addl	$KERNBASE,2(%esp)
1004	lgdt	(%esp)
1005	sidt	(%esp)
1006	addl	$KERNBASE,2(%esp)
1007	lidt	(%esp)
1008	addl	$6,%esp
1009
1010	int	$3
1011
1012bdb_commit_paging_exit:
1013	ret
1014
1015#endif /* BDE_DEBUGGER */
1016