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