vm_machdep.c revision 140
1/*-
2 * Copyright (c) 1982, 1986 The Regents of the University of California.
3 * Copyright (c) 1989, 1990 William Jolitz
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
8 * Science Department, and William Jolitz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	@(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
39 *
40 * PATCHES MAGIC                LEVEL   PATCH THAT GOT US HERE
41 * --------------------         -----   ----------------------
42 * CURRENT PATCH LEVEL:         1       00154
43 * --------------------         -----   ----------------------
44 *
45 * 20 Apr 93	Bruce Evans		New npx-0.5 code
46 *
47 */
48
49/*
50 *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
51 */
52static char rcsid[] = "$Header: /a/cvs/386BSD/src/sys.386bsd/i386/i386/vm_machdep.c,v 1.1.1.1 1993/06/12 14:58:05 rgrimes Exp $";
53
54#include "param.h"
55#include "systm.h"
56#include "proc.h"
57#include "malloc.h"
58#include "buf.h"
59#include "user.h"
60
61#include "../include/cpu.h"
62
63#include "vm/vm.h"
64#include "vm/vm_kern.h"
65
66/*
67 * Finish a fork operation, with process p2 nearly set up.
68 * Copy and update the kernel stack and pcb, making the child
69 * ready to run, and marking it so that it can return differently
70 * than the parent.  Returns 1 in the child process, 0 in the parent.
71 * We currently double-map the user area so that the stack is at the same
72 * address in each process; in the future we will probably relocate
73 * the frame pointers on the stack after copying.
74 */
75cpu_fork(p1, p2)
76	register struct proc *p1, *p2;
77{
78	register struct user *up = p2->p_addr;
79	int foo, offset, addr, i;
80	extern char kstack[];
81	extern int mvesp();
82
83	/*
84	 * Copy pcb and stack from proc p1 to p2.
85	 * We do this as cheaply as possible, copying only the active
86	 * part of the stack.  The stack and pcb need to agree;
87	 * this is tricky, as the final pcb is constructed by savectx,
88	 * but its frame isn't yet on the stack when the stack is copied.
89	 * swtch compensates for this when the child eventually runs.
90	 * This should be done differently, with a single call
91	 * that copies and updates the pcb+stack,
92	 * replacing the bcopy and savectx.
93	 */
94	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
95	offset = mvesp() - (int)kstack;
96	bcopy((caddr_t)kstack + offset, (caddr_t)p2->p_addr + offset,
97	    (unsigned) ctob(UPAGES) - offset);
98	p2->p_regs = p1->p_regs;
99
100	/*
101	 * Wire top of address space of child to it's kstack.
102	 * First, fault in a page of pte's to map it.
103	 */
104        addr = trunc_page((u_int)vtopte(kstack));
105	vm_map_pageable(&p2->p_vmspace->vm_map, addr, addr+NBPG, FALSE);
106	for (i=0; i < UPAGES; i++)
107		pmap_enter(&p2->p_vmspace->vm_pmap, kstack+i*NBPG,
108			pmap_extract(kernel_pmap, ((int)p2->p_addr)+i*NBPG), VM_PROT_READ, 1);
109
110	pmap_activate(&p2->p_vmspace->vm_pmap, &up->u_pcb);
111
112	/*
113	 *
114	 * Arrange for a non-local goto when the new process
115	 * is started, to resume here, returning nonzero from setjmp.
116	 */
117	if (savectx(up, 1)) {
118		/*
119		 * Return 1 in child.
120		 */
121		return (1);
122	}
123	return (0);
124}
125
126#ifdef notyet
127/*
128 * cpu_exit is called as the last action during exit.
129 *
130 * We change to an inactive address space and a "safe" stack,
131 * passing thru an argument to the new stack. Now, safely isolated
132 * from the resources we're shedding, we release the address space
133 * and any remaining machine-dependent resources, including the
134 * memory for the user structure and kernel stack.
135 *
136 * Next, we assign a dummy context to be written over by swtch,
137 * calling it to send this process off to oblivion.
138 * [The nullpcb allows us to minimize cost in swtch() by not having
139 * a special case].
140 */
141struct proc *swtch_to_inactive();
142volatile void
143cpu_exit(p)
144	register struct proc *p;
145{
146	static struct pcb nullpcb;	/* pcb to overwrite on last swtch */
147
148#ifdef NPX
149	npxexit(p);
150#endif
151
152	/* move to inactive space and stack, passing arg accross */
153	p = swtch_to_inactive(p);
154
155	/* drop per-process resources */
156	vmspace_free(p->p_vmspace);
157	kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
158
159	p->p_addr = (struct user *) &nullpcb;
160	splclock();
161	swtch();
162	/* NOTREACHED */
163}
164#else
165volatile void
166cpu_exit(p)
167	register struct proc *p;
168{
169
170#ifdef NPX
171	npxexit(p);
172#endif
173	splclock();
174	swtch();
175}
176
177cpu_wait(p) struct proc *p; {
178
179	/* drop per-process resources */
180	vmspace_free(p->p_vmspace);
181	kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
182}
183#endif
184
185/*
186 * Set a red zone in the kernel stack after the u. area.
187 */
188setredzone(pte, vaddr)
189	u_short *pte;
190	caddr_t vaddr;
191{
192/* eventually do this by setting up an expand-down stack segment
193   for ss0: selector, allowing stack access down to top of u.
194   this means though that protection violations need to be handled
195   thru a double fault exception that must do an integral task
196   switch to a known good context, within which a dump can be
197   taken. a sensible scheme might be to save the initial context
198   used by sched (that has physical memory mapped 1:1 at bottom)
199   and take the dump while still in mapped mode */
200}
201
202/*
203 * Move pages from one kernel virtual address to another.
204 * Both addresses are assumed to reside in the Sysmap,
205 * and size must be a multiple of CLSIZE.
206 */
207pagemove(from, to, size)
208	register caddr_t from, to;
209	int size;
210{
211	register struct pte *fpte, *tpte;
212
213	if (size % CLBYTES)
214		panic("pagemove");
215	fpte = kvtopte(from);
216	tpte = kvtopte(to);
217	while (size > 0) {
218		*tpte++ = *fpte;
219		*(int *)fpte++ = 0;
220		from += NBPG;
221		to += NBPG;
222		size -= NBPG;
223	}
224	tlbflush();
225}
226
227/*
228 * Convert kernel VA to physical address
229 */
230kvtop(addr)
231	register caddr_t addr;
232{
233	vm_offset_t va;
234
235	va = pmap_extract(kernel_pmap, (vm_offset_t)addr);
236	if (va == 0)
237		panic("kvtop: zero page frame");
238	return((int)va);
239}
240
241#ifdef notdef
242/*
243 * The probe[rw] routines should probably be redone in assembler
244 * for efficiency.
245 */
246prober(addr)
247	register u_int addr;
248{
249	register int page;
250	register struct proc *p;
251
252	if (addr >= USRSTACK)
253		return(0);
254	p = u.u_procp;
255	page = btop(addr);
256	if (page < dptov(p, p->p_dsize) || page > sptov(p, p->p_ssize))
257		return(1);
258	return(0);
259}
260
261probew(addr)
262	register u_int addr;
263{
264	register int page;
265	register struct proc *p;
266
267	if (addr >= USRSTACK)
268		return(0);
269	p = u.u_procp;
270	page = btop(addr);
271	if (page < dptov(p, p->p_dsize) || page > sptov(p, p->p_ssize))
272		return((*(int *)vtopte(p, page) & PG_PROT) == PG_UW);
273	return(0);
274}
275
276/*
277 * NB: assumes a physically contiguous kernel page table
278 *     (makes life a LOT simpler).
279 */
280kernacc(addr, count, rw)
281	register u_int addr;
282	int count, rw;
283{
284	register struct pde *pde;
285	register struct pte *pte;
286	register int ix, cnt;
287	extern long Syssize;
288
289	if (count <= 0)
290		return(0);
291	pde = (struct pde *)((u_int)u.u_procp->p_p0br + u.u_procp->p_szpt * NBPG);
292	ix = (addr & PD_MASK) >> PD_SHIFT;
293	cnt = ((addr + count + (1 << PD_SHIFT) - 1) & PD_MASK) >> PD_SHIFT;
294	cnt -= ix;
295	for (pde += ix; cnt; cnt--, pde++)
296		if (pde->pd_v == 0)
297			return(0);
298	ix = btop(addr-0xfe000000);
299	cnt = btop(addr-0xfe000000+count+NBPG-1);
300	if (cnt > (int)&Syssize)
301		return(0);
302	cnt -= ix;
303	for (pte = &Sysmap[ix]; cnt; cnt--, pte++)
304		if (pte->pg_v == 0 /*|| (rw == B_WRITE && pte->pg_prot == 1)*/)
305			return(0);
306	return(1);
307}
308
309useracc(addr, count, rw)
310	register u_int addr;
311	int count, rw;
312{
313	register int (*func)();
314	register u_int addr2;
315	extern int prober(), probew();
316
317	if (count <= 0)
318		return(0);
319	addr2 = addr;
320	addr += count;
321	func = (rw == B_READ) ? prober : probew;
322	do {
323		if ((*func)(addr2) == 0)
324			return(0);
325		addr2 = (addr2 + NBPG) & ~PGOFSET;
326	} while (addr2 < addr);
327	return(1);
328}
329#endif
330
331extern vm_map_t phys_map;
332
333/*
334 * Map an IO request into kernel virtual address space.  Requests fall into
335 * one of five catagories:
336 *
337 *	B_PHYS|B_UAREA:	User u-area swap.
338 *			Address is relative to start of u-area (p_addr).
339 *	B_PHYS|B_PAGET:	User page table swap.
340 *			Address is a kernel VA in usrpt (Usrptmap).
341 *	B_PHYS|B_DIRTY:	Dirty page push.
342 *			Address is a VA in proc2's address space.
343 *	B_PHYS|B_PGIN:	Kernel pagein of user pages.
344 *			Address is VA in user's address space.
345 *	B_PHYS:		User "raw" IO request.
346 *			Address is VA in user's address space.
347 *
348 * All requests are (re)mapped into kernel VA space via the useriomap
349 * (a name with only slightly more meaning than "kernelmap")
350 */
351vmapbuf(bp)
352	register struct buf *bp;
353{
354	register int npf;
355	register caddr_t addr;
356	register long flags = bp->b_flags;
357	struct proc *p;
358	int off;
359	vm_offset_t kva;
360	register vm_offset_t pa;
361
362	if ((flags & B_PHYS) == 0)
363		panic("vmapbuf");
364	addr = bp->b_saveaddr = bp->b_un.b_addr;
365	off = (int)addr & PGOFSET;
366	p = bp->b_proc;
367	npf = btoc(round_page(bp->b_bcount + off));
368	kva = kmem_alloc_wait(phys_map, ctob(npf));
369	bp->b_un.b_addr = (caddr_t) (kva + off);
370	while (npf--) {
371		pa = pmap_extract(&p->p_vmspace->vm_pmap, (vm_offset_t)addr);
372		if (pa == 0)
373			panic("vmapbuf: null page frame");
374		pmap_enter(vm_map_pmap(phys_map), kva, trunc_page(pa),
375			   VM_PROT_READ|VM_PROT_WRITE, TRUE);
376		addr += PAGE_SIZE;
377		kva += PAGE_SIZE;
378	}
379}
380
381/*
382 * Free the io map PTEs associated with this IO operation.
383 * We also invalidate the TLB entries and restore the original b_addr.
384 */
385vunmapbuf(bp)
386	register struct buf *bp;
387{
388	register int npf;
389	register caddr_t addr = bp->b_un.b_addr;
390	vm_offset_t kva;
391
392	if ((bp->b_flags & B_PHYS) == 0)
393		panic("vunmapbuf");
394	npf = btoc(round_page(bp->b_bcount + ((int)addr & PGOFSET)));
395	kva = (vm_offset_t)((int)addr & ~PGOFSET);
396	kmem_free_wakeup(phys_map, kva, ctob(npf));
397	bp->b_un.b_addr = bp->b_saveaddr;
398	bp->b_saveaddr = NULL;
399}
400
401/*
402 * Force reset the processor by invalidating the entire address space!
403 */
404cpu_reset() {
405
406	/* force a shutdown by unmapping entire address space ! */
407	bzero((caddr_t) PTD, NBPG);
408
409	/* "good night, sweet prince .... <THUNK!>" */
410	tlbflush();
411	/* NOTREACHED */
412}
413