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