locore.s revision 108911
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 * $FreeBSD: head/sys/i386/i386/locore.s 108911 2003-01-08 01:20:05Z jhb $
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 "opt_bootp.h"
47#include "opt_compat.h"
48#include "opt_nfsroot.h"
49
50#include <sys/syscall.h>
51#include <sys/reboot.h>
52
53#include <machine/asmacros.h>
54#include <machine/cputypes.h>
55#include <machine/psl.h>
56#include <machine/pmap.h>
57#include <machine/specialreg.h>
58
59#include "assym.s"
60
61/*
62 *	XXX
63 *
64 * Note: This version greatly munged to avoid various assembler errors
65 * that may be fixed in newer versions of gas. Perhaps newer versions
66 * will have more pleasant appearance.
67 */
68
69/*
70 * PTmap is recursive pagemap at top of virtual address space.
71 * Within PTmap, the page directory can be found (third indirection).
72 *
73 * NOTE: PTDpde, PTmap, and PTD are being defined as address symbols.
74 * In C you access them directly, and not with a '*'. Storage is not being
75 * allocated. They will magically address the correct locations in KVM
76 * which C will treat as normal variables of the type they are defined in
77 * machine/pmap.h, i.e.  PTDpde = XX ; to set a PDE entry, NOT *PTDpde = XX;
78 */
79	.globl	PTmap,PTD,PTDpde
80	.set	PTmap,(PTDPTDI << PDRSHIFT)
81	.set	PTD,PTmap + (PTDPTDI * PAGE_SIZE)
82	.set	PTDpde,PTD + (PTDPTDI * PDESIZE)
83
84/*
85 * APTmap, APTD is the alternate recursive pagemap.
86 * It's used when modifying another process's page tables.
87 * See the note above. It is true here as well.
88 */
89	.globl	APTmap,APTD,APTDpde
90	.set	APTmap,APTDPTDI << PDRSHIFT
91	.set	APTD,APTmap + (APTDPTDI * PAGE_SIZE)
92	.set	APTDpde,PTD + (APTDPTDI * PDESIZE)
93
94#ifdef SMP
95/*
96 * Define layout of per-cpu address space.
97 * This is "constructed" in locore.s on the BSP and in mp_machdep.c
98 * for each AP.  DO NOT REORDER THESE WITHOUT UPDATING THE REST!
99 */
100	.globl	SMP_prvspace, lapic
101	.set	SMP_prvspace,(MPPTDI << PDRSHIFT)
102	.set	lapic,SMP_prvspace + (NPTEPG-1) * PAGE_SIZE
103#endif /* SMP */
104
105/*
106 * Compiled KERNBASE location
107 */
108	.globl	kernbase
109	.set	kernbase,KERNBASE
110
111/*
112 * Globals
113 */
114	.data
115	ALIGN_DATA			/* just to be sure */
116
117	.globl	HIDENAME(tmpstk)
118	.space	0x2000			/* space for tmpstk - temporary stack */
119HIDENAME(tmpstk):
120
121	.globl	bootinfo
122bootinfo:	.space	BOOTINFO_SIZE	/* bootinfo that we can handle */
123
124		.globl KERNend
125KERNend:	.long	0		/* phys addr end of kernel (just after bss) */
126physfree:	.long	0		/* phys addr of next free page */
127
128#ifdef SMP
129		.globl	cpu0prvpage
130cpu0pp:		.long	0		/* phys addr cpu0 private pg */
131cpu0prvpage:	.long	0		/* relocated version */
132
133		.globl	SMPpt
134SMPptpa:	.long	0		/* phys addr SMP page table */
135SMPpt:		.long	0		/* relocated version */
136#endif /* SMP */
137
138	.globl	IdlePTD
139IdlePTD:	.long	0		/* phys addr of kernel PTD */
140
141#ifdef SMP
142	.globl	KPTphys
143#endif
144KPTphys:	.long	0		/* phys addr of kernel page tables */
145
146	.globl	proc0uarea, proc0kstack
147proc0uarea:	.long	0		/* address of proc 0 uarea space */
148proc0kstack:	.long	0		/* address of proc 0 kstack space */
149p0upa:		.long	0		/* phys addr of proc0's UAREA */
150p0kpa:		.long	0		/* phys addr of proc0's STACK */
151
152vm86phystk:	.long	0		/* PA of vm86/bios stack */
153
154	.globl	vm86paddr, vm86pa
155vm86paddr:	.long	0		/* address of vm86 region */
156vm86pa:		.long	0		/* phys addr of vm86 region */
157
158#ifdef BDE_DEBUGGER
159	.globl	_bdb_exists		/* flag to indicate BDE debugger is present */
160_bdb_exists:	.long	0
161#endif
162
163#ifdef PC98
164	.globl	pc98_system_parameter
165pc98_system_parameter:
166	.space	0x240
167#endif
168
169/**********************************************************************
170 *
171 * Some handy macros
172 *
173 */
174
175#define R(foo) ((foo)-KERNBASE)
176
177#define ALLOCPAGES(foo) \
178	movl	R(physfree), %esi ; \
179	movl	$((foo)*PAGE_SIZE), %eax ; \
180	addl	%esi, %eax ; \
181	movl	%eax, R(physfree) ; \
182	movl	%esi, %edi ; \
183	movl	$((foo)*PAGE_SIZE),%ecx ; \
184	xorl	%eax,%eax ; \
185	cld ; \
186	rep ; \
187	stosb
188
189/*
190 * fillkpt
191 *	eax = page frame address
192 *	ebx = index into page table
193 *	ecx = how many pages to map
194 * 	base = base address of page dir/table
195 *	prot = protection bits
196 */
197#define	fillkpt(base, prot)		  \
198	shll	$2,%ebx			; \
199	addl	base,%ebx		; \
200	orl	$PG_V,%eax		; \
201	orl	prot,%eax		; \
2021:	movl	%eax,(%ebx)		; \
203	addl	$PAGE_SIZE,%eax		; /* increment physical address */ \
204	addl	$4,%ebx			; /* next pte */ \
205	loop	1b
206
207/*
208 * fillkptphys(prot)
209 *	eax = physical address
210 *	ecx = how many pages to map
211 *	prot = protection bits
212 */
213#define	fillkptphys(prot)		  \
214	movl	%eax, %ebx		; \
215	shrl	$PAGE_SHIFT, %ebx	; \
216	fillkpt(R(KPTphys), prot)
217
218	.text
219/**********************************************************************
220 *
221 * This is where the bootblocks start us, set the ball rolling...
222 *
223 */
224NON_GPROF_ENTRY(btext)
225
226#ifdef PC98
227	/* save SYSTEM PARAMETER for resume (NS/T or other) */
228	movl	$0xa1400,%esi
229	movl	$R(pc98_system_parameter),%edi
230	movl	$0x0240,%ecx
231	cld
232	rep
233	movsb
234#else	/* IBM-PC */
235#ifdef BDE_DEBUGGER
236#ifdef BIOS_STEALS_3K
237	cmpl	$0x0375c339,0x95504
238#else
239	cmpl	$0x0375c339,0x96104	/* XXX - debugger signature */
240#endif
241	jne	1f
242	movb	$1,R(_bdb_exists)
2431:
244#endif
245/* Tell the bios to warmboot next time */
246	movw	$0x1234,0x472
247#endif	/* PC98 */
248
249/* Set up a real frame in case the double return in newboot is executed. */
250	pushl	%ebp
251	movl	%esp, %ebp
252
253/* Don't trust what the BIOS gives for eflags. */
254	pushl	$PSL_KERNEL
255	popfl
256
257/*
258 * Don't trust what the BIOS gives for %fs and %gs.  Trust the bootstrap
259 * to set %cs, %ds, %es and %ss.
260 */
261	mov	%ds, %ax
262	mov	%ax, %fs
263	mov	%ax, %gs
264
265	call	recover_bootinfo
266
267/* Get onto a stack that we can trust. */
268/*
269 * XXX this step is delayed in case recover_bootinfo needs to return via
270 * the old stack, but it need not be, since recover_bootinfo actually
271 * returns via the old frame.
272 */
273	movl	$R(HIDENAME(tmpstk)),%esp
274
275#ifdef PC98
276	/* pc98_machine_type & M_EPSON_PC98 */
277	testb	$0x02,R(pc98_system_parameter)+220
278	jz	3f
279	/* epson_machine_id <= 0x0b */
280	cmpb	$0x0b,R(pc98_system_parameter)+224
281	ja	3f
282
283	/* count up memory */
284	movl	$0x100000,%eax		/* next, talley remaining memory */
285	movl	$0xFFF-0x100,%ecx
2861:	movl	0(%eax),%ebx		/* save location to check */
287	movl	$0xa55a5aa5,0(%eax)	/* write test pattern */
288	cmpl	$0xa55a5aa5,0(%eax)	/* does not check yet for rollover */
289	jne	2f
290	movl	%ebx,0(%eax)		/* restore memory */
291	addl	$PAGE_SIZE,%eax
292	loop	1b
2932:	subl	$0x100000,%eax
294	shrl	$17,%eax
295	movb	%al,R(pc98_system_parameter)+1
2963:
297
298	movw	R(pc98_system_parameter+0x86),%ax
299	movw	%ax,R(cpu_id)
300#endif
301
302	call	identify_cpu
303
304/* clear bss */
305/*
306 * XXX this should be done a little earlier.
307 *
308 * XXX we don't check that there is memory for our bss and page tables
309 * before using it.
310 *
311 * XXX the boot program somewhat bogusly clears the bss.  We still have
312 * to do it in case we were unzipped by kzipboot.  Then the boot program
313 * only clears kzipboot's bss.
314 *
315 * XXX the gdt and idt are still somewhere in the boot program.  We
316 * depend on the convention that the boot program is below 1MB and we
317 * are above 1MB to keep the gdt and idt  away from the bss and page
318 * tables.  The idt is only used if BDE_DEBUGGER is enabled.
319 */
320	movl	$R(end),%ecx
321	movl	$R(edata),%edi
322	subl	%edi,%ecx
323	xorl	%eax,%eax
324	cld
325	rep
326	stosb
327
328	call	create_pagetables
329
330/*
331 * If the CPU has support for VME, turn it on.
332 */
333	testl	$CPUID_VME, R(cpu_feature)
334	jz	1f
335	movl	%cr4, %eax
336	orl	$CR4_VME, %eax
337	movl	%eax, %cr4
3381:
339
340#ifdef BDE_DEBUGGER
341/*
342 * Adjust as much as possible for paging before enabling paging so that the
343 * adjustments can be traced.
344 */
345	call	bdb_prepare_paging
346#endif
347
348/* Now enable paging */
349	movl	R(IdlePTD), %eax
350	movl	%eax,%cr3		/* load ptd addr into mmu */
351	movl	%cr0,%eax		/* get control word */
352	orl	$CR0_PE|CR0_PG,%eax	/* enable paging */
353	movl	%eax,%cr0		/* and let's page NOW! */
354
355#ifdef BDE_DEBUGGER
356/*
357 * Complete the adjustments for paging so that we can keep tracing through
358 * initi386() after the low (physical) addresses for the gdt and idt become
359 * invalid.
360 */
361	call	bdb_commit_paging
362#endif
363
364	pushl	$begin			/* jump to high virtualized address */
365	ret
366
367/* now running relocated at KERNBASE where the system is linked to run */
368begin:
369	/* set up bootstrap stack */
370	movl	proc0kstack,%eax	/* location of in-kernel stack */
371			/* bootstrap stack end location */
372	leal	(KSTACK_PAGES*PAGE_SIZE-PCB_SIZE)(%eax),%esp
373
374	xorl	%ebp,%ebp		/* mark end of frames */
375
376	movl	IdlePTD,%esi
377	movl	%esi,(KSTACK_PAGES*PAGE_SIZE-PCB_SIZE+PCB_CR3)(%eax)
378
379	pushl	physfree		/* value of first for init386(first) */
380	call	init386			/* wire 386 chip for unix operation */
381
382	/*
383	 * Clean up the stack in a way that db_numargs() understands, so
384	 * that backtraces in ddb don't underrun the stack.  Traps for
385	 * inaccessible memory are more fatal than usual this early.
386	 */
387	addl	$4,%esp
388
389	call	mi_startup		/* autoconfiguration, mountroot etc */
390	/* NOTREACHED */
391	addl	$0,%esp			/* for db_numargs() again */
392
393/*
394 * Signal trampoline, copied to top of user stack
395 */
396NON_GPROF_ENTRY(sigcode)
397	calll	*SIGF_HANDLER(%esp)
398	leal	SIGF_UC(%esp),%eax	/* get ucontext */
399	pushl	%eax
400	testl	$PSL_VM,UC_EFLAGS(%eax)
401	jne	1f
402	movl	UC_GS(%eax),%gs		/* restore %gs */
4031:
404	movl	$SYS_sigreturn,%eax
405	pushl	%eax			/* junk to fake return addr. */
406	int	$0x80			/* enter kernel with args */
407					/* on stack */
4081:
409	jmp	1b
410
411#ifdef COMPAT_FREEBSD4
412	ALIGN_TEXT
413freebsd4_sigcode:
414	calll	*SIGF_HANDLER(%esp)
415	leal	SIGF_UC4(%esp),%eax	/* get ucontext */
416	pushl	%eax
417	testl	$PSL_VM,UC4_EFLAGS(%eax)
418	jne	1f
419	movl	UC4_GS(%eax),%gs	/* restore %gs */
4201:
421	movl	$344,%eax		/* 4.x SYS_sigreturn */
422	pushl	%eax			/* junk to fake return addr. */
423	int	$0x80			/* enter kernel with args */
424					/* on stack */
4251:
426	jmp	1b
427#endif
428
429#ifdef COMPAT_43
430	ALIGN_TEXT
431osigcode:
432	call	*SIGF_HANDLER(%esp)	/* call signal handler */
433	lea	SIGF_SC(%esp),%eax	/* get sigcontext */
434	pushl	%eax
435	testl	$PSL_VM,SC_PS(%eax)
436	jne	9f
437	movl	SC_GS(%eax),%gs		/* restore %gs */
4389:
439	movl	$103,%eax		/* 3.x SYS_sigreturn */
440	pushl	%eax			/* junk to fake return addr. */
441	int	$0x80			/* enter kernel with args */
4420:	jmp	0b
443#endif /* COMPAT_43 */
444
445	ALIGN_TEXT
446esigcode:
447
448	.data
449	.globl	szsigcode
450szsigcode:
451	.long	esigcode-sigcode
452#ifdef COMPAT_FREEBSD4
453	.globl	szfreebsd4_sigcode
454szfreebsd4_sigcode:
455	.long	esigcode-freebsd4_sigcode
456#endif
457#ifdef COMPAT_43
458	.globl	szosigcode
459szosigcode:
460	.long	esigcode-osigcode
461#endif
462	.text
463
464/**********************************************************************
465 *
466 * Recover the bootinfo passed to us from the boot program
467 *
468 */
469recover_bootinfo:
470	/*
471	 * This code is called in different ways depending on what loaded
472	 * and started the kernel.  This is used to detect how we get the
473	 * arguments from the other code and what we do with them.
474	 *
475	 * Old disk boot blocks:
476	 *	(*btext)(howto, bootdev, cyloffset, esym);
477	 *	[return address == 0, and can NOT be returned to]
478	 *	[cyloffset was not supported by the FreeBSD boot code
479	 *	 and always passed in as 0]
480	 *	[esym is also known as total in the boot code, and
481	 *	 was never properly supported by the FreeBSD boot code]
482	 *
483	 * Old diskless netboot code:
484	 *	(*btext)(0,0,0,0,&nfsdiskless,0,0,0);
485	 *	[return address != 0, and can NOT be returned to]
486	 *	If we are being booted by this code it will NOT work,
487	 *	so we are just going to halt if we find this case.
488	 *
489	 * New uniform boot code:
490	 *	(*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
491	 *	[return address != 0, and can be returned to]
492	 *
493	 * There may seem to be a lot of wasted arguments in here, but
494	 * that is so the newer boot code can still load very old kernels
495	 * and old boot code can load new kernels.
496	 */
497
498	/*
499	 * The old style disk boot blocks fake a frame on the stack and
500	 * did an lret to get here.  The frame on the stack has a return
501	 * address of 0.
502	 */
503	cmpl	$0,4(%ebp)
504	je	olddiskboot
505
506	/*
507	 * We have some form of return address, so this is either the
508	 * old diskless netboot code, or the new uniform code.  That can
509	 * be detected by looking at the 5th argument, if it is 0
510	 * we are being booted by the new uniform boot code.
511	 */
512	cmpl	$0,24(%ebp)
513	je	newboot
514
515	/*
516	 * Seems we have been loaded by the old diskless boot code, we
517	 * don't stand a chance of running as the diskless structure
518	 * changed considerably between the two, so just halt.
519	 */
520	 hlt
521
522	/*
523	 * We have been loaded by the new uniform boot code.
524	 * Let's check the bootinfo version, and if we do not understand
525	 * it we return to the loader with a status of 1 to indicate this error
526	 */
527newboot:
528	movl	28(%ebp),%ebx		/* &bootinfo.version */
529	movl	BI_VERSION(%ebx),%eax
530	cmpl	$1,%eax			/* We only understand version 1 */
531	je	1f
532	movl	$1,%eax			/* Return status */
533	leave
534	/*
535	 * XXX this returns to our caller's caller (as is required) since
536	 * we didn't set up a frame and our caller did.
537	 */
538	ret
539
5401:
541	/*
542	 * If we have a kernelname copy it in
543	 */
544	movl	BI_KERNELNAME(%ebx),%esi
545	cmpl	$0,%esi
546	je	2f			/* No kernelname */
547	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
548	movl	$R(kernelname),%edi
549	cmpb	$'/',(%esi)		/* Make sure it starts with a slash */
550	je	1f
551	movb	$'/',(%edi)
552	incl	%edi
553	decl	%ecx
5541:
555	cld
556	rep
557	movsb
558
5592:
560	/*
561	 * Determine the size of the boot loader's copy of the bootinfo
562	 * struct.  This is impossible to do properly because old versions
563	 * of the struct don't contain a size field and there are 2 old
564	 * versions with the same version number.
565	 */
566	movl	$BI_ENDCOMMON,%ecx	/* prepare for sizeless version */
567	testl	$RB_BOOTINFO,8(%ebp)	/* bi_size (and bootinfo) valid? */
568	je	got_bi_size		/* no, sizeless version */
569	movl	BI_SIZE(%ebx),%ecx
570got_bi_size:
571
572	/*
573	 * Copy the common part of the bootinfo struct
574	 */
575	movl	%ebx,%esi
576	movl	$R(bootinfo),%edi
577	cmpl	$BOOTINFO_SIZE,%ecx
578	jbe	got_common_bi_size
579	movl	$BOOTINFO_SIZE,%ecx
580got_common_bi_size:
581	cld
582	rep
583	movsb
584
585#ifdef NFS_ROOT
586#ifndef BOOTP_NFSV3
587	/*
588	 * If we have a nfs_diskless structure copy it in
589	 */
590	movl	BI_NFS_DISKLESS(%ebx),%esi
591	cmpl	$0,%esi
592	je	olddiskboot
593	movl	$R(nfs_diskless),%edi
594	movl	$NFSDISKLESS_SIZE,%ecx
595	cld
596	rep
597	movsb
598	movl	$R(nfs_diskless_valid),%edi
599	movl	$1,(%edi)
600#endif
601#endif
602
603	/*
604	 * The old style disk boot.
605	 *	(*btext)(howto, bootdev, cyloffset, esym);
606	 * Note that the newer boot code just falls into here to pick
607	 * up howto and bootdev, cyloffset and esym are no longer used
608	 */
609olddiskboot:
610	movl	8(%ebp),%eax
611	movl	%eax,R(boothowto)
612	movl	12(%ebp),%eax
613	movl	%eax,R(bootdev)
614
615	ret
616
617
618/**********************************************************************
619 *
620 * Identify the CPU and initialize anything special about it
621 *
622 */
623identify_cpu:
624
625	/* Try to toggle alignment check flag; does not exist on 386. */
626	pushfl
627	popl	%eax
628	movl	%eax,%ecx
629	orl	$PSL_AC,%eax
630	pushl	%eax
631	popfl
632	pushfl
633	popl	%eax
634	xorl	%ecx,%eax
635	andl	$PSL_AC,%eax
636	pushl	%ecx
637	popfl
638
639	testl	%eax,%eax
640	jnz	try486
641
642	/* NexGen CPU does not have aligment check flag. */
643	pushfl
644	movl	$0x5555, %eax
645	xorl	%edx, %edx
646	movl	$2, %ecx
647	clc
648	divl	%ecx
649	jz	trynexgen
650	popfl
651	movl	$CPU_386,R(cpu)
652	jmp	3f
653
654trynexgen:
655	popfl
656	movl	$CPU_NX586,R(cpu)
657	movl	$0x4778654e,R(cpu_vendor)	# store vendor string
658	movl	$0x72446e65,R(cpu_vendor+4)
659	movl	$0x6e657669,R(cpu_vendor+8)
660	movl	$0,R(cpu_vendor+12)
661	jmp	3f
662
663try486:	/* Try to toggle identification flag; does not exist on early 486s. */
664	pushfl
665	popl	%eax
666	movl	%eax,%ecx
667	xorl	$PSL_ID,%eax
668	pushl	%eax
669	popfl
670	pushfl
671	popl	%eax
672	xorl	%ecx,%eax
673	andl	$PSL_ID,%eax
674	pushl	%ecx
675	popfl
676
677	testl	%eax,%eax
678	jnz	trycpuid
679	movl	$CPU_486,R(cpu)
680
681	/*
682	 * Check Cyrix CPU
683	 * Cyrix CPUs do not change the undefined flags following
684	 * execution of the divide instruction which divides 5 by 2.
685	 *
686	 * Note: CPUID is enabled on M2, so it passes another way.
687	 */
688	pushfl
689	movl	$0x5555, %eax
690	xorl	%edx, %edx
691	movl	$2, %ecx
692	clc
693	divl	%ecx
694	jnc	trycyrix
695	popfl
696	jmp	3f		/* You may use Intel CPU. */
697
698trycyrix:
699	popfl
700	/*
701	 * IBM Bluelighting CPU also doesn't change the undefined flags.
702	 * Because IBM doesn't disclose the information for Bluelighting
703	 * CPU, we couldn't distinguish it from Cyrix's (including IBM
704	 * brand of Cyrix CPUs).
705	 */
706	movl	$0x69727943,R(cpu_vendor)	# store vendor string
707	movl	$0x736e4978,R(cpu_vendor+4)
708	movl	$0x64616574,R(cpu_vendor+8)
709	jmp	3f
710
711trycpuid:	/* Use the `cpuid' instruction. */
712	xorl	%eax,%eax
713	cpuid					# cpuid 0
714	movl	%eax,R(cpu_high)		# highest capability
715	movl	%ebx,R(cpu_vendor)		# store vendor string
716	movl	%edx,R(cpu_vendor+4)
717	movl	%ecx,R(cpu_vendor+8)
718	movb	$0,R(cpu_vendor+12)
719
720	movl	$1,%eax
721	cpuid					# cpuid 1
722	movl	%eax,R(cpu_id)			# store cpu_id
723	movl	%ebx,R(cpuid_cpuinfo)		# store cpuid_cpuinfo
724	movl	%edx,R(cpu_feature)		# store cpu_feature
725	rorl	$8,%eax				# extract family type
726	andl	$15,%eax
727	cmpl	$5,%eax
728	jae	1f
729
730	/* less than Pentium; must be 486 */
731	movl	$CPU_486,R(cpu)
732	jmp	3f
7331:
734	/* a Pentium? */
735	cmpl	$5,%eax
736	jne	2f
737	movl	$CPU_586,R(cpu)
738	jmp	3f
7392:
740	/* Greater than Pentium...call it a Pentium Pro */
741	movl	$CPU_686,R(cpu)
7423:
743	ret
744
745
746/**********************************************************************
747 *
748 * Create the first page directory and its page tables.
749 *
750 */
751
752create_pagetables:
753
754/* Find end of kernel image (rounded up to a page boundary). */
755	movl	$R(_end),%esi
756
757/* Include symbols, if any. */
758	movl	R(bootinfo+BI_ESYMTAB),%edi
759	testl	%edi,%edi
760	je	over_symalloc
761	movl	%edi,%esi
762	movl	$KERNBASE,%edi
763	addl	%edi,R(bootinfo+BI_SYMTAB)
764	addl	%edi,R(bootinfo+BI_ESYMTAB)
765over_symalloc:
766
767/* If we are told where the end of the kernel space is, believe it. */
768	movl	R(bootinfo+BI_KERNEND),%edi
769	testl	%edi,%edi
770	je	no_kernend
771	movl	%edi,%esi
772no_kernend:
773
774	addl	$PAGE_MASK,%esi
775	andl	$~PAGE_MASK,%esi
776	movl	%esi,R(KERNend)		/* save end of kernel */
777	movl	%esi,R(physfree)	/* next free page is at end of kernel */
778
779/* Allocate Kernel Page Tables */
780	ALLOCPAGES(NKPT)
781	movl	%esi,R(KPTphys)
782
783/* Allocate Page Table Directory */
784	ALLOCPAGES(1)
785	movl	%esi,R(IdlePTD)
786
787/* Allocate UPAGES */
788	ALLOCPAGES(UAREA_PAGES)
789	movl	%esi,R(p0upa)
790	addl	$KERNBASE, %esi
791	movl	%esi, R(proc0uarea)
792
793	ALLOCPAGES(KSTACK_PAGES)
794	movl	%esi,R(p0kpa)
795	addl	$KERNBASE, %esi
796	movl	%esi, R(proc0kstack)
797
798	ALLOCPAGES(1)			/* vm86/bios stack */
799	movl	%esi,R(vm86phystk)
800
801	ALLOCPAGES(3)			/* pgtable + ext + IOPAGES */
802	movl	%esi,R(vm86pa)
803	addl	$KERNBASE, %esi
804	movl	%esi, R(vm86paddr)
805
806#ifdef SMP
807/* Allocate cpu0's private data page */
808	ALLOCPAGES(1)
809	movl	%esi,R(cpu0pp)
810	addl	$KERNBASE, %esi
811	movl	%esi, R(cpu0prvpage)	/* relocated to KVM space */
812
813/* Allocate SMP page table page */
814	ALLOCPAGES(1)
815	movl	%esi,R(SMPptpa)
816	addl	$KERNBASE, %esi
817	movl	%esi, R(SMPpt)		/* relocated to KVM space */
818#endif	/* SMP */
819
820/* Map read-only from zero to the end of the kernel text section */
821	xorl	%eax, %eax
822#ifdef BDE_DEBUGGER
823/* If the debugger is present, actually map everything read-write. */
824	cmpl	$0,R(_bdb_exists)
825	jne	map_read_write
826#endif
827	xorl	%edx,%edx
828	movl	$R(etext),%ecx
829	addl	$PAGE_MASK,%ecx
830	shrl	$PAGE_SHIFT,%ecx
831	fillkptphys(%edx)
832
833/* Map read-write, data, bss and symbols */
834	movl	$R(etext),%eax
835	addl	$PAGE_MASK, %eax
836	andl	$~PAGE_MASK, %eax
837map_read_write:
838	movl	$PG_RW,%edx
839	movl	R(KERNend),%ecx
840	subl	%eax,%ecx
841	shrl	$PAGE_SHIFT,%ecx
842	fillkptphys(%edx)
843
844/* Map page directory. */
845	movl	R(IdlePTD), %eax
846	movl	$1, %ecx
847	fillkptphys($PG_RW)
848
849/* Map proc0's UPAGES in the physical way ... */
850	movl	R(p0upa), %eax
851	movl	$(UAREA_PAGES), %ecx
852	fillkptphys($PG_RW)
853
854/* Map proc0's KSTACK in the physical way ... */
855	movl	R(p0kpa), %eax
856	movl	$(KSTACK_PAGES), %ecx
857	fillkptphys($PG_RW)
858
859/* Map ISA hole */
860	movl	$ISA_HOLE_START, %eax
861	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
862	fillkptphys($PG_RW)
863
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
881#ifdef SMP
882/* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */
883	movl	R(cpu0pp), %eax
884	movl	$1, %ecx
885	fillkptphys($PG_RW)
886
887/* Map SMP page table page into global kmem FWIW */
888	movl	R(SMPptpa), %eax
889	movl	$1, %ecx
890	fillkptphys($PG_RW)
891
892/* Map the private page into the SMP page table */
893	movl	R(cpu0pp), %eax
894	movl	$0, %ebx		/* pte offset = 0 */
895	movl	$1, %ecx		/* one private page coming right up */
896	fillkpt(R(SMPptpa), $PG_RW)
897
898/* ... and put the page table table in the pde. */
899	movl	R(SMPptpa), %eax
900	movl	$MPPTDI, %ebx
901	movl	$1, %ecx
902	fillkpt(R(IdlePTD), $PG_RW)
903
904/* Fakeup VA for the local apic to allow early traps. */
905	ALLOCPAGES(1)
906	movl	%esi, %eax
907	movl	$(NPTEPG-1), %ebx	/* pte offset = NTEPG-1 */
908	movl	$1, %ecx		/* one private pt coming right up */
909	fillkpt(R(SMPptpa), $PG_RW)
910#endif	/* SMP */
911
912/* install a pde for temporary double map of bottom of VA */
913	movl	R(KPTphys), %eax
914	xorl	%ebx, %ebx
915	movl	$NKPT, %ecx
916	fillkpt(R(IdlePTD), $PG_RW)
917
918/* install pde's for pt's */
919	movl	R(KPTphys), %eax
920	movl	$KPTDI, %ebx
921	movl	$NKPT, %ecx
922	fillkpt(R(IdlePTD), $PG_RW)
923
924/* install a pde recursively mapping page directory as a page table */
925	movl	R(IdlePTD), %eax
926	movl	$PTDPTDI, %ebx
927	movl	$1,%ecx
928	fillkpt(R(IdlePTD), $PG_RW)
929
930	ret
931
932#ifdef BDE_DEBUGGER
933bdb_prepare_paging:
934	cmpl	$0,R(_bdb_exists)
935	je	bdb_prepare_paging_exit
936
937	subl	$6,%esp
938
939	/*
940	 * Copy and convert debugger entries from the bootstrap gdt and idt
941	 * to the kernel gdt and idt.  Everything is still in low memory.
942	 * Tracing continues to work after paging is enabled because the
943	 * low memory addresses remain valid until everything is relocated.
944	 * However, tracing through the setidt() that initializes the trace
945	 * trap will crash.
946	 */
947	sgdt	(%esp)
948	movl	2(%esp),%esi		/* base address of bootstrap gdt */
949	movl	$R(_gdt),%edi
950	movl	%edi,2(%esp)		/* prepare to load kernel gdt */
951	movl	$8*18/4,%ecx
952	cld
953	rep				/* copy gdt */
954	movsl
955	movl	$R(_gdt),-8+2(%edi)	/* adjust gdt self-ptr */
956	movb	$0x92,-8+5(%edi)
957	lgdt	(%esp)
958
959	sidt	(%esp)
960	movl	2(%esp),%esi		/* base address of current idt */
961	movl	8+4(%esi),%eax		/* convert dbg descriptor to ... */
962	movw	8(%esi),%ax
963	movl	%eax,R(bdb_dbg_ljmp+1)	/* ... immediate offset ... */
964	movl	8+2(%esi),%eax
965	movw	%ax,R(bdb_dbg_ljmp+5)	/* ... and selector for ljmp */
966	movl	24+4(%esi),%eax		/* same for bpt descriptor */
967	movw	24(%esi),%ax
968	movl	%eax,R(bdb_bpt_ljmp+1)
969	movl	24+2(%esi),%eax
970	movw	%ax,R(bdb_bpt_ljmp+5)
971	movl	R(_idt),%edi
972	movl	%edi,2(%esp)		/* prepare to load kernel idt */
973	movl	$8*4/4,%ecx
974	cld
975	rep				/* copy idt */
976	movsl
977	lidt	(%esp)
978
979	addl	$6,%esp
980
981bdb_prepare_paging_exit:
982	ret
983
984/* Relocate debugger gdt entries and gdt and idt pointers. */
985bdb_commit_paging:
986	cmpl	$0,_bdb_exists
987	je	bdb_commit_paging_exit
988
989	movl	$gdt+8*9,%eax		/* adjust slots 9-17 */
990	movl	$9,%ecx
991reloc_gdt:
992	movb	$KERNBASE>>24,7(%eax)	/* top byte of base addresses, was 0, */
993	addl	$8,%eax			/* now KERNBASE>>24 */
994	loop	reloc_gdt
995
996	subl	$6,%esp
997	sgdt	(%esp)
998	addl	$KERNBASE,2(%esp)
999	lgdt	(%esp)
1000	sidt	(%esp)
1001	addl	$KERNBASE,2(%esp)
1002	lidt	(%esp)
1003	addl	$6,%esp
1004
1005	int	$3
1006
1007bdb_commit_paging_exit:
1008	ret
1009
1010#endif /* BDE_DEBUGGER */
1011