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