locore.s revision 1058
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.14 1994/01/31 04:39:37 davidg Exp $
38 */
39
40/*
41 * locore.s:	FreeBSD machine support for the Intel 386
42 *		originally from: locore.s, by William F. Jolitz
43 *
44 *		Substantially rewritten by David Greenman, Rod Grimes,
45 *			Bruce Evans, Wolfgang Solfrank, and many others.
46 */
47
48#include "npx.h"				/* for NNPX */
49#include "assym.s"				/* system definitions */
50#include "machine/psl.h"			/* processor status longword defs */
51#include "machine/pte.h"			/* page table entry definitions */
52#include "errno.h"				/* error return codes */
53#include "machine/specialreg.h"			/* x86 special registers */
54#include "i386/isa/debug.h"			/* BDE debugging macros */
55#include "machine/cputypes.h"			/* x86 cpu type definitions */
56#include "syscall.h"				/* system call numbers */
57#include "machine/asmacros.h"			/* miscellaneous asm macros */
58
59/*
60 *	XXX
61 *
62 * Note: This version greatly munged to avoid various assembler errors
63 * that may be fixed in newer versions of gas. Perhaps newer versions
64 * will have more pleasant appearance.
65 */
66
67/*
68 * PTmap is recursive pagemap at top of virtual address space.
69 * Within PTmap, the page directory can be found (third indirection).
70 */
71	.globl	_PTmap,_PTD,_PTDpde,_Sysmap
72	.set	_PTmap,PTDPTDI << PDRSHIFT
73	.set	_PTD,_PTmap + (PTDPTDI * NBPG)
74	.set	_PTDpde,_PTD + (PTDPTDI * PDESIZE)
75
76/* Sysmap is the base address of the kernel page tables */
77	.set	_Sysmap,_PTmap + (KPTDI * NBPG)
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 * NBPG)
86	.set	_APTDpde,_PTD + (APTDPTDI * PDESIZE)
87
88/*
89 * Access to each processes kernel stack is via a region of
90 * per-process address space (at the beginning), immediatly above
91 * the user process stack.
92 */
93	.set	_kstack,USRSTACK
94	.globl	_kstack
95
96/*
97 * Globals
98 */
99	.data
100	.globl	_esym
101_esym:	.long	0				/* ptr to end of syms */
102
103	.globl	_boothowto,_bootdev,_curpcb
104
105	.globl	_cpu,_cold,_atdevbase
106_cpu:	.long	0				/* are we 386, 386sx, or 486 */
107_cold:	.long	1				/* cold till we are not */
108_atdevbase:	.long	0			/* location of start of iomem in virtual */
109_atdevphys:	.long	0			/* location of device mapping ptes (phys) */
110
111	.globl	_KERNend
112_KERNend:	.long	0			/* phys addr end of kernel (just after bss) */
113
114	.globl	_IdlePTD,_KPTphys
115_IdlePTD:	.long	0			/* phys addr of kernel PTD */
116_KPTphys:	.long	0			/* phys addr of kernel page tables */
117
118	.globl	_cyloffset
119_cyloffset:	.long	0			/* cylinder offset from boot blocks */
120
121	.globl	_proc0paddr
122_proc0paddr:	.long	0			/* address of proc 0 address space */
123
124#ifdef BDE_DEBUGGER
125	.globl	_bdb_exists			/* flag to indicate BDE debugger is available */
126_bde_exists:	.long	0
127#endif
128
129	.globl	tmpstk
130	.space	0x1000
131tmpstk:
132
133
134/*
135 * System Initialization
136 */
137	.text
138
139/*
140 * btext: beginning of text section.
141 * Also the entry point (jumped to directly from the boot blocks).
142 */
143ENTRY(btext)
144	movw	$0x1234,0x472			/* warm boot */
145	jmp	1f
146	.space	0x500				/* skip over warm boot shit */
147
148	/*
149	 * pass parameters on stack (howto, bootdev, unit, cyloffset, esym)
150	 * note: (%esp) is return address of boot
151	 * ( if we want to hold onto /boot, it's physical %esp up to _end)
152	 */
153
154 1:	movl	4(%esp),%eax
155	movl	%eax,_boothowto-KERNBASE
156	movl	8(%esp),%eax
157	movl	%eax,_bootdev-KERNBASE
158	movl	12(%esp),%eax
159	movl	%eax,_cyloffset-KERNBASE
160	movl	16(%esp),%eax
161	addl	$KERNBASE,%eax
162	movl	%eax,_esym-KERNBASE
163#ifdef DISKLESS					/* Copy diskless structure */
164	movl	_nfs_diskless_size-KERNBASE,%ecx
165	movl	20(%esp),%esi
166	movl	$(_nfs_diskless-KERNBASE),%edi
167	rep
168	movsb
169#endif
170
171	/* find out our CPU type. */
172        pushfl
173        popl    %eax
174        movl    %eax,%ecx
175        xorl    $0x40000,%eax
176        pushl   %eax
177        popfl
178        pushfl
179        popl    %eax
180        xorl    %ecx,%eax
181        shrl    $18,%eax
182        andl    $1,%eax
183        push    %ecx
184        popfl
185
186        cmpl    $0,%eax
187        jne     1f
188        movl    $CPU_386,_cpu-KERNBASE
189	jmp	2f
1901:      movl    $CPU_486,_cpu-KERNBASE
1912:
192
193	/*
194	 * Finished with old stack; load new %esp now instead of later so
195	 * we can trace this code without having to worry about the trace
196	 * trap clobbering the memory test or the zeroing of the bss+bootstrap
197	 * page tables.
198	 *
199	 * XXX - wdboot clears the bss after testing that this is safe.
200	 * This is too wasteful - memory below 640K is scarce.  The boot
201	 * program should check:
202	 *	text+data <= &stack_variable - more_space_for_stack
203	 *	text+data+bss+pad+space_for_page_tables <= end_of_memory
204	 * Oops, the gdt is in the carcass of the boot program so clearing
205	 * the rest of memory is still not possible.
206	 */
207	movl	$tmpstk-KERNBASE,%esp		/* bootstrap stack end location */
208
209/*
210 * Virtual address space of kernel:
211 *
212 *	text | data | bss | [syms] | page dir | proc0 kernel stack | usr stk map | Sysmap
213 *      pages:                          1         UPAGES (2)             1         NKPT (7)
214 */
215
216/* find end of kernel image */
217	movl	$_end-KERNBASE,%ecx
218	addl	$NBPG-1,%ecx			/* page align up */
219	andl	$~(NBPG-1),%ecx
220	movl	%ecx,%esi			/* esi=start of tables */
221	movl	%ecx,_KERNend-KERNBASE		/* save end of kernel */
222
223/* clear bss */
224	movl	$_edata-KERNBASE,%edi
225	subl	%edi,%ecx			/* get amount to clear */
226	xorl	%eax,%eax			/* specify zero fill */
227	cld
228	rep
229	stosb
230
231/*
232 * The value in esi is both the end of the kernel bss and a pointer to
233 * the kernel page directory, and is used by the rest of locore to build
234 * the tables.
235 * esi + 1(page dir) + 2(UPAGES) + 1(p0stack) + NKPT(number of kernel
236 * page table pages) is then passed on the stack to init386(first) as
237 * the value first. esi should ALWAYS be page aligned!!
238 */
239	movl	%esi,%ecx			/* Get current first availiable address */
240
241/* clear pagetables, page directory, stack, etc... */
242	movl	%esi,%edi			/* base (page directory) */
243	movl	$((1+UPAGES+1+NKPT)*NBPG),%ecx	/* amount to clear */
244	xorl	%eax,%eax			/* specify zero fill */
245	cld
246	rep
247	stosb
248
249/* physical address of Idle proc/kernel page directory */
250	movl	%esi,_IdlePTD-KERNBASE
251
252/*
253 * fillkpt
254 *	eax = (page frame address | control | status) == pte
255 *	ebx = address of page table
256 *	ecx = how many pages to map
257 */
258#define	fillkpt		\
2591:	movl	%eax,(%ebx)	; \
260	addl	$NBPG,%eax	; /* increment physical address */ \
261	addl	$4,%ebx		; /* next pte */ \
262	loop	1b		;
263
264/*
265 * Map Kernel
266 *
267 * First step - build page tables
268 */
269#if defined (KGDB) || defined (BDE_DEBUGGER)
270	movl	_KERNend-KERNBASE,%ecx		/* this much memory, */
271	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
272#ifdef BDE_DEBUGGER
273	cmpl	$0xa0,%ecx			/* XXX - cover debugger pages */
274	jae	1f
275	movl	$0xa0,%ecx
2761:
277#endif /* BDE_DEBUGGER */
278	movl	$PG_V|PG_KW,%eax		/* having these bits set, */
279	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
280	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
281	fillkpt
282
283#else /* !KGDB && !BDE_DEBUGGER */
284	/* write protect kernel text (doesn't do a thing for 386's - only 486's) */
285	movl	$_etext-KERNBASE,%ecx		/* get size of text */
286	shrl	$PGSHIFT,%ecx			/* for this many PTEs */
287	movl	$PG_V|PG_KR,%eax		/* specify read only */
288	lea	((1+UPAGES+1)*NBPG)(%esi),%ebx	/* phys addr of kernel PT base */
289	movl	%ebx,_KPTphys-KERNBASE		/* save in global */
290	fillkpt
291
292	/* data and bss are r/w */
293	andl	$PG_FRAME,%eax			/* strip to just addr of bss */
294	movl	_KERNend-KERNBASE,%ecx		/* calculate size */
295	subl	%eax,%ecx
296	shrl	$PGSHIFT,%ecx
297	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
298	fillkpt
299#endif
300
301/* now initialize the page dir, upages, p0stack PT, and page tables */
302
303	movl	$(1+UPAGES+1+NKPT),%ecx	/* number of PTEs */
304	movl	%esi,%eax			/* phys address of PTD */
305	andl	$PG_FRAME,%eax			/* convert to PFN, should be a NOP */
306	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
307	movl	%esi,%ebx			/* calculate pte offset to ptd */
308	shrl	$PGSHIFT-2,%ebx
309	addl	%esi,%ebx			/* address of page directory */
310	addl	$((1+UPAGES+1)*NBPG),%ebx	/* offset to kernel page tables */
311	fillkpt
312
313/* map I/O memory map */
314
315	movl    _KPTphys-KERNBASE,%ebx		/* base of kernel page tables */
316	lea     (0xa0 * PTESIZE)(%ebx),%ebx	/* hardwire ISA hole at KERNBASE + 0xa0000 */
317	movl	$0x100-0xa0,%ecx		/* for this many pte s, */
318	movl	$(0xa0000|PG_V|PG_KW|PG_N),%eax	/* valid, kernel read/write, non-cacheable */
319	movl	%ebx,_atdevphys-KERNBASE	/* save phys addr of ptes */
320	fillkpt
321
322 /* map proc 0's kernel stack into user page table page */
323
324	movl	$UPAGES,%ecx			/* for this many pte s, */
325	lea	(1*NBPG)(%esi),%eax		/* physical address in proc 0 */
326	lea	(KERNBASE)(%eax),%edx		/* change into virtual addr */
327	movl	%edx,_proc0paddr-KERNBASE	/* save VA for proc 0 init */
328	orl	$PG_V|PG_KW,%eax		/* valid, kernel read/write */
329	lea	((1+UPAGES)*NBPG)(%esi),%ebx	/* addr of stack page table in proc 0 */
330	addl	$(KSTKPTEOFF * PTESIZE),%ebx	/* offset to kernel stack PTE */
331	fillkpt
332
333/*
334 * Initialize kernel page table directory
335 */
336	/* install a pde for temporary double map of bottom of VA */
337	movl	_KPTphys-KERNBASE,%eax
338	orl     $PG_V|PG_KW,%eax		/* valid, kernel read/write */
339	movl	%eax,(%esi)			/* which is where temp maps! */
340
341	/* initialize kernel pde's */
342	movl	$(NKPT),%ecx			/* for this many PDEs */
343	lea	(KPTDI*PDESIZE)(%esi),%ebx	/* offset of pde for kernel */
344	fillkpt
345
346	/* install a pde recursively mapping page directory as a page table! */
347	movl	%esi,%eax			/* phys address of ptd in proc 0 */
348	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
349	movl	%eax,PTDPTDI*PDESIZE(%esi)	/* which is where PTmap maps! */
350
351	/* install a pde to map kernel stack for proc 0 */
352	lea	((1+UPAGES)*NBPG)(%esi),%eax	/* physical address of pt in proc 0 */
353	orl	$PG_V|PG_KW,%eax		/* pde entry is valid */
354	movl	%eax,KSTKPTDI*PDESIZE(%esi)	/* which is where kernel stack maps! */
355
356#ifdef BDE_DEBUGGER
357	/* copy and convert stuff from old gdt and idt for debugger */
358
359	cmpl	$0x0375c339,0x96104		/* XXX - debugger signature */
360	jne	1f
361	movb	$1,_bdb_exists-KERNBASE
3621:
363	pushal
364	subl	$2*6,%esp
365
366	sgdt	(%esp)
367	movl	2(%esp),%esi			/* base address of current gdt */
368	movl	$_gdt-KERNBASE,%edi
369	movl	%edi,2(%esp)
370	movl	$8*18/4,%ecx
371	rep					/* copy gdt */
372	movsl
373	movl	$_gdt-KERNBASE,-8+2(%edi)	/* adjust gdt self-ptr */
374	movb	$0x92,-8+5(%edi)
375
376	sidt	6(%esp)
377	movl	6+2(%esp),%esi			/* base address of current idt */
378	movl	8+4(%esi),%eax			/* convert dbg descriptor to ... */
379	movw	8(%esi),%ax
380	movl	%eax,bdb_dbg_ljmp+1-KERNBASE	/* ... immediate offset ... */
381	movl	8+2(%esi),%eax
382	movw	%ax,bdb_dbg_ljmp+5-KERNBASE	/* ... and selector for ljmp */
383	movl	24+4(%esi),%eax			/* same for bpt descriptor */
384	movw	24(%esi),%ax
385	movl	%eax,bdb_bpt_ljmp+1-KERNBASE
386	movl	24+2(%esi),%eax
387	movw	%ax,bdb_bpt_ljmp+5-KERNBASE
388
389	movl	$_idt-KERNBASE,%edi
390	movl	%edi,6+2(%esp)
391	movl	$8*4/4,%ecx
392	rep					/* copy idt */
393	movsl
394
395	lgdt	(%esp)
396	lidt	6(%esp)
397
398	addl	$2*6,%esp
399	popal
400#endif
401
402	/* load base of page directory and enable mapping */
403	movl	%esi,%eax			/* phys address of ptd in proc 0 */
404	orl	$I386_CR3PAT,%eax
405	movl	%eax,%cr3			/* load ptd addr into mmu */
406	movl	%cr0,%eax			/* get control word */
407	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
408	movl	%eax,%cr0			/* and let's page NOW! */
409
410	pushl	$begin				/* jump to high mem */
411	ret
412
413begin: /* now running relocated at KERNBASE where the system is linked to run */
414
415	.globl _Crtat				/* XXX - locore should not know about */
416	movl	_Crtat,%eax			/* variables of device drivers (pccons)! */
417	subl	$(KERNBASE+0xA0000),%eax
418	movl	_atdevphys,%edx			/* get pte PA */
419	subl	_KPTphys,%edx			/* remove base of ptes, now have phys offset */
420	shll	$PGSHIFT-2,%edx			/* corresponding to virt offset */
421	addl	$KERNBASE,%edx			/* add virtual base */
422	movl	%edx,_atdevbase
423	addl	%eax,%edx
424	movl	%edx,_Crtat
425
426	/* set up bootstrap stack - 48 bytes */
427	movl	$_kstack+UPAGES*NBPG-4*12,%esp	/* bootstrap stack end location */
428	xorl	%eax,%eax			/* mark end of frames */
429	movl	%eax,%ebp
430	movl	_proc0paddr,%eax
431	movl	%esi,PCB_CR3(%eax)
432
433#ifdef BDE_DEBUGGER
434	/* relocate debugger gdt entries */
435
436	movl	$_gdt+8*9,%eax			/* adjust slots 9-17 */
437	movl	$9,%ecx
438reloc_gdt:
439	movb	$0xfe,7(%eax)			/* top byte of base addresses, was 0, */
440	addl	$8,%eax				/* now KERNBASE>>24 */
441	loop	reloc_gdt
442
443	cmpl	$0,_bdb_exists
444	je	1f
445	int	$3
4461:
447#endif
448
449	/*
450	 * Skip over the page tables and the kernel stack
451	 */
452	lea	((1+UPAGES+1+NKPT)*NBPG)(%esi),%esi
453
454	pushl	%esi				/* value of first for init386(first) */
455	call	_init386			/* wire 386 chip for unix operation */
456
457	movl	$0,_PTD
458	call	_main				/* autoconfiguration, mountroot etc */
459	popl	%esi
460
461	/*
462	 * now we've run main() and determined what cpu-type we are, we can
463	 * enable WP mode on i486 cpus and above.
464	 */
465#if defined(I486_CPU) || defined(I586_CPU)
466	cmpl    $CPUCLASS_386,_cpu_class
467	je	1f
468	movl	%cr0,%eax			/* get control word */
469	orl	$CR0_WP,%eax			/* enable write protect for all modes */
470	movl	%eax,%cr0			/* and do it */
471#endif
472	/*
473	 * on return from main(), we are process 1
474	 * set up address space and stack so that we can 'return' to user mode
475	 */
4761:
477	.globl	__ucodesel,__udatasel
478	movl	__ucodesel,%eax
479	movl	__udatasel,%ecx
480	/* build outer stack frame */
481	pushl	%ecx				/* user ss */
482	pushl	$USRSTACK			/* user esp */
483	pushl	%eax				/* user cs */
484	pushl	$0				/* user ip */
485	movl	%cx,%ds
486	movl	%cx,%es
487	movl	%ax,%fs				/* double map cs to fs */
488	movl	%cx,%gs				/* and ds to gs */
489	lret					/* goto user! */
490
491	pushl	$lretmsg1			/* "should never get here!" */
492	call	_panic
493lretmsg1:
494	.asciz	"lret: toinit\n"
495
496
497#define	LCALL(x,y)	.byte 0x9a ; .long y; .word x
498/*
499 * Icode is copied out to process 1 and executed in user mode:
500 *	execve("/sbin/init", argv, envp); exit(0);
501 * If the execve fails, process 1 exits and the system panics.
502 */
503NON_GPROF_ENTRY(icode)
504	pushl	$0				/* envp for execve() */
505
506#	pushl	$argv-_icode			/* can't do this 'cos gas 1.38 is broken */
507	movl	$argv,%eax
508	subl	$_icode,%eax
509	pushl	%eax				/* argp for execve() */
510
511#	pushl	$init-_icode
512	movl	$init,%eax
513	subl	$_icode,%eax
514	pushl	%eax				/* fname for execve() */
515
516	pushl	%eax				/* dummy return address */
517
518	movl	$SYS_execve,%eax
519	LCALL(0x7,0x0)
520
521	/* exit if something botches up in the above execve() */
522	pushl	%eax				/* execve failed, the errno will do for an */
523						/* exit code because errnos are < 128 */
524	pushl	%eax				/* dummy return address */
525	movl	$SYS_exit,%eax
526	LCALL(0x7,0x0)
527
528init:
529	.asciz	"/sbin/init"
530	ALIGN_DATA
531argv:
532	.long	init+6-_icode			/* argv[0] = "init" ("/sbin/init" + 6) */
533	.long	eicode-_icode			/* argv[1] follows icode after copyout */
534	.long	0
535eicode:
536
537	.globl	_szicode
538_szicode:
539	.long	_szicode-_icode
540
541NON_GPROF_ENTRY(sigcode)
542	call	SIGF_HANDLER(%esp)
543	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
544						/* copy at 8(%esp)) */
545	pushl	%eax
546	pushl	%eax				/* junk to fake return address */
547	movl	$103,%eax			/* XXX sigreturn() */
548	LCALL(0x7,0)				/* enter kernel with args on stack */
549	hlt					/* never gets here */
550
551	.globl	_szsigcode
552_szsigcode:
553	.long	_szsigcode-_sigcode
554
555