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