vm_machdep.c revision 798
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 *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
39 *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
40 *	$Id: vm_machdep.c,v 1.6 1993/10/15 10:34:29 rgrimes Exp $
41 */
42
43#include "npx.h"
44#include "param.h"
45#include "systm.h"
46#include "proc.h"
47#include "malloc.h"
48#include "buf.h"
49#include "user.h"
50
51#include "../include/cpu.h"
52
53#include "vm/vm.h"
54#include "vm/vm_kern.h"
55
56/*
57 * Finish a fork operation, with process p2 nearly set up.
58 * Copy and update the kernel stack and pcb, making the child
59 * ready to run, and marking it so that it can return differently
60 * than the parent.  Returns 1 in the child process, 0 in the parent.
61 * We currently double-map the user area so that the stack is at the same
62 * address in each process; in the future we will probably relocate
63 * the frame pointers on the stack after copying.
64 */
65int
66cpu_fork(p1, p2)
67	register struct proc *p1, *p2;
68{
69	register struct user *up = p2->p_addr;
70	int foo, offset, addr, i;
71	extern char kstack[];
72	extern int mvesp();
73
74	/*
75	 * Copy pcb and stack from proc p1 to p2.
76	 * We do this as cheaply as possible, copying only the active
77	 * part of the stack.  The stack and pcb need to agree;
78	 * this is tricky, as the final pcb is constructed by savectx,
79	 * but its frame isn't yet on the stack when the stack is copied.
80	 * swtch compensates for this when the child eventually runs.
81	 * This should be done differently, with a single call
82	 * that copies and updates the pcb+stack,
83	 * replacing the bcopy and savectx.
84	 */
85	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
86	offset = mvesp() - (int)kstack;
87	bcopy((caddr_t)kstack + offset, (caddr_t)p2->p_addr + offset,
88	    (unsigned) ctob(UPAGES) - offset);
89	p2->p_regs = p1->p_regs;
90
91	/*
92	 * Wire top of address space of child to it's kstack.
93	 * First, fault in a page of pte's to map it.
94	 */
95        addr = trunc_page((u_int)vtopte(kstack));
96	vm_map_pageable(&p2->p_vmspace->vm_map, addr, addr+NBPG, FALSE);
97	for (i=0; i < UPAGES; i++)
98		pmap_enter(&p2->p_vmspace->vm_pmap, kstack+i*NBPG,
99			   pmap_extract(kernel_pmap, ((int)p2->p_addr)+i*NBPG),
100			   /*
101			    * The user area has to be mapped writable because
102			    * it contains the kernel stack (when CR0_WP is on
103			    * on a 486 there is no user-read/kernel-write
104			    * mode).  It is protected from user mode access
105			    * by the segment limits.
106			    */
107			   VM_PROT_READ|VM_PROT_WRITE, TRUE);
108	pmap_activate(&p2->p_vmspace->vm_pmap, &up->u_pcb);
109
110	/*
111	 *
112	 * Arrange for a non-local goto when the new process
113	 * is started, to resume here, returning nonzero from setjmp.
114	 */
115	if (savectx(up, 1)) {
116		/*
117		 * Return 1 in child.
118		 */
119		return (1);
120	}
121	return (0);
122}
123
124#ifdef notyet
125/*
126 * cpu_exit is called as the last action during exit.
127 *
128 * We change to an inactive address space and a "safe" stack,
129 * passing thru an argument to the new stack. Now, safely isolated
130 * from the resources we're shedding, we release the address space
131 * and any remaining machine-dependent resources, including the
132 * memory for the user structure and kernel stack.
133 *
134 * Next, we assign a dummy context to be written over by swtch,
135 * calling it to send this process off to oblivion.
136 * [The nullpcb allows us to minimize cost in swtch() by not having
137 * a special case].
138 */
139struct proc *swtch_to_inactive();
140volatile void
141cpu_exit(p)
142	register struct proc *p;
143{
144	static struct pcb nullpcb;	/* pcb to overwrite on last swtch */
145
146#if NNPX > 0
147	npxexit(p);
148#endif	/* NNPX */
149
150	/* move to inactive space and stack, passing arg accross */
151	p = swtch_to_inactive(p);
152
153	/* drop per-process resources */
154	vmspace_free(p->p_vmspace);
155	kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
156
157	p->p_addr = (struct user *) &nullpcb;
158	splclock();
159	swtch();
160	/* NOTREACHED */
161}
162#else
163void
164cpu_exit(p)
165	register struct proc *p;
166{
167
168#if NNPX > 0
169	npxexit(p);
170#endif	/* NNPX */
171	splclock();
172	swtch();
173	/*
174	 * This is to shutup the compiler, and if swtch() failed I suppose
175	 * this would be a good thing.  This keeps gcc happy because panic
176	 * is a volatile void function as well.
177	 */
178	panic("cpu_exit");
179}
180
181void
182cpu_wait(p)
183	struct proc *p;
184{
185
186	/* drop per-process resources */
187	vmspace_free(p->p_vmspace);
188	kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
189}
190#endif
191
192/*
193 * Set a red zone in the kernel stack after the u. area.
194 */
195void
196setredzone(pte, vaddr)
197	u_short *pte;
198	caddr_t vaddr;
199{
200/* eventually do this by setting up an expand-down stack segment
201   for ss0: selector, allowing stack access down to top of u.
202   this means though that protection violations need to be handled
203   thru a double fault exception that must do an integral task
204   switch to a known good context, within which a dump can be
205   taken. a sensible scheme might be to save the initial context
206   used by sched (that has physical memory mapped 1:1 at bottom)
207   and take the dump while still in mapped mode */
208}
209
210/*
211 * Move pages from one kernel virtual address to another.
212 * Both addresses are assumed to reside in the Sysmap,
213 * and size must be a multiple of CLSIZE.
214 */
215void
216pagemove(from, to, size)
217	register caddr_t from, to;
218	int size;
219{
220	register struct pte *fpte, *tpte;
221
222	if (size % CLBYTES)
223		panic("pagemove");
224	fpte = kvtopte(from);
225	tpte = kvtopte(to);
226	while (size > 0) {
227		*tpte++ = *fpte;
228		*(int *)fpte++ = 0;
229		from += NBPG;
230		to += NBPG;
231		size -= NBPG;
232	}
233	tlbflush();
234}
235
236/*
237 * Convert kernel VA to physical address
238 */
239int
240kvtop(addr)
241	register caddr_t addr;
242{
243	vm_offset_t va;
244
245	va = pmap_extract(kernel_pmap, (vm_offset_t)addr);
246	if (va == 0)
247		panic("kvtop: zero page frame");
248	return((int)va);
249}
250
251#ifdef notdef
252/*
253 * The probe[rw] routines should probably be redone in assembler
254 * for efficiency.
255 */
256prober(addr)
257	register u_int addr;
258{
259	register int page;
260	register struct proc *p;
261
262	if (addr >= USRSTACK)
263		return(0);
264	p = u.u_procp;
265	page = btop(addr);
266	if (page < dptov(p, p->p_dsize) || page > sptov(p, p->p_ssize))
267		return(1);
268	return(0);
269}
270
271probew(addr)
272	register u_int addr;
273{
274	register int page;
275	register struct proc *p;
276
277	if (addr >= USRSTACK)
278		return(0);
279	p = u.u_procp;
280	page = btop(addr);
281	if (page < dptov(p, p->p_dsize) || page > sptov(p, p->p_ssize))
282		return((*(int *)vtopte(p, page) & PG_PROT) == PG_UW);
283	return(0);
284}
285
286/*
287 * NB: assumes a physically contiguous kernel page table
288 *     (makes life a LOT simpler).
289 */
290kernacc(addr, count, rw)
291	register u_int addr;
292	int count, rw;
293{
294	register struct pde *pde;
295	register struct pte *pte;
296	register int ix, cnt;
297	extern long Syssize;
298
299	if (count <= 0)
300		return(0);
301	pde = (struct pde *)((u_int)u.u_procp->p_p0br + u.u_procp->p_szpt * NBPG);
302	ix = (addr & PD_MASK) >> PD_SHIFT;
303	cnt = ((addr + count + (1 << PD_SHIFT) - 1) & PD_MASK) >> PD_SHIFT;
304	cnt -= ix;
305	for (pde += ix; cnt; cnt--, pde++)
306		if (pde->pd_v == 0)
307			return(0);
308	ix = btop(addr-KERNBASE);
309	cnt = btop(addr-KERNBASE+count+NBPG-1);
310	if (cnt > (int)&Syssize)
311		return(0);
312	cnt -= ix;
313	for (pte = &Sysmap[ix]; cnt; cnt--, pte++)
314		if (pte->pg_v == 0 /*|| (rw == B_WRITE && pte->pg_prot == 1)*/)
315			return(0);
316	return(1);
317}
318
319useracc(addr, count, rw)
320	register u_int addr;
321	int count, rw;
322{
323	register int (*func)();
324	register u_int addr2;
325	extern int prober(), probew();
326
327	if (count <= 0)
328		return(0);
329	addr2 = addr;
330	addr += count;
331	func = (rw == B_READ) ? prober : probew;
332	do {
333		if ((*func)(addr2) == 0)
334			return(0);
335		addr2 = (addr2 + NBPG) & ~PGOFSET;
336	} while (addr2 < addr);
337	return(1);
338}
339#endif
340
341extern vm_map_t phys_map;
342
343/*
344 * Map an IO request into kernel virtual address space.  Requests fall into
345 * one of five catagories:
346 *
347 *	B_PHYS|B_UAREA:	User u-area swap.
348 *			Address is relative to start of u-area (p_addr).
349 *	B_PHYS|B_PAGET:	User page table swap.
350 *			Address is a kernel VA in usrpt (Usrptmap).
351 *	B_PHYS|B_DIRTY:	Dirty page push.
352 *			Address is a VA in proc2's address space.
353 *	B_PHYS|B_PGIN:	Kernel pagein of user pages.
354 *			Address is VA in user's address space.
355 *	B_PHYS:		User "raw" IO request.
356 *			Address is VA in user's address space.
357 *
358 * All requests are (re)mapped into kernel VA space via the useriomap
359 * (a name with only slightly more meaning than "kernelmap")
360 */
361void
362vmapbuf(bp)
363	register struct buf *bp;
364{
365	register int npf;
366	register caddr_t addr;
367	register long flags = bp->b_flags;
368	struct proc *p;
369	int off;
370	vm_offset_t kva;
371	register vm_offset_t pa;
372
373	if ((flags & B_PHYS) == 0)
374		panic("vmapbuf");
375	addr = bp->b_saveaddr = bp->b_un.b_addr;
376	off = (int)addr & PGOFSET;
377	p = bp->b_proc;
378	npf = btoc(round_page(bp->b_bcount + off));
379	kva = kmem_alloc_wait(phys_map, ctob(npf));
380	bp->b_un.b_addr = (caddr_t) (kva + off);
381	while (npf--) {
382		pa = pmap_extract(&p->p_vmspace->vm_pmap, (vm_offset_t)addr);
383		if (pa == 0)
384			panic("vmapbuf: null page frame");
385		pmap_enter(vm_map_pmap(phys_map), kva, trunc_page(pa),
386			   VM_PROT_READ|VM_PROT_WRITE, TRUE);
387		addr += PAGE_SIZE;
388		kva += PAGE_SIZE;
389	}
390}
391
392/*
393 * Free the io map PTEs associated with this IO operation.
394 * We also invalidate the TLB entries and restore the original b_addr.
395 */
396void
397vunmapbuf(bp)
398	register struct buf *bp;
399{
400	register int npf;
401	register caddr_t addr = bp->b_un.b_addr;
402	vm_offset_t kva;
403
404	if ((bp->b_flags & B_PHYS) == 0)
405		panic("vunmapbuf");
406	npf = btoc(round_page(bp->b_bcount + ((int)addr & PGOFSET)));
407	kva = (vm_offset_t)((int)addr & ~PGOFSET);
408	kmem_free_wakeup(phys_map, kva, ctob(npf));
409	bp->b_un.b_addr = bp->b_saveaddr;
410	bp->b_saveaddr = NULL;
411}
412
413/*
414 * Force reset the processor by invalidating the entire address space!
415 */
416void				/* XXX should be __dead too */
417cpu_reset() {
418
419	/* force a shutdown by unmapping entire address space ! */
420	bzero((caddr_t) PTD, NBPG);
421
422	/* "good night, sweet prince .... <THUNK!>" */
423	tlbflush();
424	/* NOTREACHED */
425	while(1);		/* to fool compiler... */
426}
427