vm_machdep.c revision 68900
1/*-
2 * Copyright (c) 1982, 1986 The Regents of the University of California.
3 * Copyright (c) 1989, 1990 William Jolitz
4 * Copyright (c) 1994 John Dyson
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department, and William Jolitz.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the University of
22 *	California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
40 *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
41 * $FreeBSD: head/sys/powerpc/aim/vm_machdep.c 68900 2000-11-19 12:28:42Z dfr $
42 */
43/*
44 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
45 * All rights reserved.
46 *
47 * Author: Chris G. Demetriou
48 *
49 * Permission to use, copy, modify and distribute this software and
50 * its documentation is hereby granted, provided that both the copyright
51 * notice and this permission notice appear in all copies of the
52 * software, derivative works or modified versions, and any portions
53 * thereof, and that both notices appear in supporting documentation.
54 *
55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58 *
59 * Carnegie Mellon requests users of this software to return to
60 *
61 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62 *  School of Computer Science
63 *  Carnegie Mellon University
64 *  Pittsburgh PA 15213-3890
65 *
66 * any improvements or extensions that they make and grant Carnegie the
67 * rights to redistribute these changes.
68 */
69
70#include <sys/param.h>
71#include <sys/systm.h>
72#include <sys/proc.h>
73#include <sys/malloc.h>
74#include <sys/bio.h>
75#include <sys/buf.h>
76#include <sys/mutex.h>
77#include <sys/vnode.h>
78#include <sys/vmmeter.h>
79#include <sys/kernel.h>
80#include <sys/sysctl.h>
81#include <sys/unistd.h>
82
83#include <machine/clock.h>
84#include <machine/cpu.h>
85#include <machine/fpu.h>
86#include <machine/md_var.h>
87#include <machine/prom.h>
88
89#include <vm/vm.h>
90#include <vm/vm_param.h>
91#include <sys/lock.h>
92#include <vm/vm_kern.h>
93#include <vm/vm_page.h>
94#include <vm/vm_map.h>
95#include <vm/vm_extern.h>
96
97#include <sys/user.h>
98
99/*
100 * quick version of vm_fault
101 */
102int
103vm_fault_quick(v, prot)
104	caddr_t v;
105	int prot;
106{
107	int r;
108	if (prot & VM_PROT_WRITE)
109		r = subyte(v, fubyte(v));
110	else
111		r = fubyte(v);
112	return(r);
113}
114
115/*
116 * Finish a fork operation, with process p2 nearly set up.
117 * Copy and update the pcb, set up the stack so that the child
118 * ready to run and return to user mode.
119 */
120void
121cpu_fork(p1, p2, flags)
122	register struct proc *p1, *p2;
123	int flags;
124{
125	if ((flags & RFPROC) == 0)
126		return;
127
128	p2->p_md.md_tf = p1->p_md.md_tf;
129	p2->p_md.md_flags = p1->p_md.md_flags & (MDP_FPUSED | MDP_UAC_MASK);
130
131	/*
132	 * Cache the physical address of the pcb, so we can
133	 * swap to it easily.
134	 */
135	p2->p_md.md_pcbpaddr = (void*)vtophys((vm_offset_t)&p2->p_addr->u_pcb);
136
137	/*
138	 * Copy floating point state from the FP chip to the PCB
139	 * if this process has state stored there.
140	 */
141	alpha_fpstate_save(p1, 0);
142
143	/*
144	 * Copy pcb and stack from proc p1 to p2.  We do this as
145	 * cheaply as possible, copying only the active part of the
146	 * stack.  The stack and pcb need to agree. Make sure that the
147	 * new process has FEN disabled.
148	 */
149	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
150	p2->p_addr->u_pcb.pcb_hw.apcb_usp = alpha_pal_rdusp();
151	p2->p_addr->u_pcb.pcb_hw.apcb_flags &= ~ALPHA_PCB_FLAGS_FEN;
152
153	/*
154	 * Set the floating point state.
155	 */
156	if ((p2->p_addr->u_pcb.pcb_fp_control & IEEE_INHERIT) == 0) {
157		p2->p_addr->u_pcb.pcb_fp_control = 0;
158		p2->p_addr->u_pcb.pcb_fp.fpr_cr = (FPCR_DYN_NORMAL
159						   | FPCR_INVD | FPCR_DZED
160						   | FPCR_OVFD | FPCR_INED
161						   | FPCR_UNFD);
162	}
163
164	/*
165	 * Arrange for a non-local goto when the new process
166	 * is started, to resume here, returning nonzero from setjmp.
167	 */
168#ifdef DIAGNOSTIC
169	if (p1 != curproc)
170		panic("cpu_fork: curproc");
171	alpha_fpstate_check(p1);
172#endif
173
174	/*
175	 * create the child's kernel stack, from scratch.
176	 */
177	{
178		struct user *up = p2->p_addr;
179		struct trapframe *p2tf;
180
181		/*
182		 * Pick a stack pointer, leaving room for a trapframe;
183		 * copy trapframe from parent so return to user mode
184		 * will be to right address, with correct registers.
185		 */
186		p2tf = p2->p_md.md_tf = (struct trapframe *)
187		    ((char *)p2->p_addr + USPACE - sizeof(struct trapframe));
188		bcopy(p1->p_md.md_tf, p2->p_md.md_tf,
189		    sizeof(struct trapframe));
190
191		/*
192		 * Set up return-value registers as fork() libc stub expects.
193		 */
194		p2tf->tf_regs[FRAME_V0] = 0; 	/* child's pid (linux) 	*/
195		p2tf->tf_regs[FRAME_A3] = 0;	/* no error 		*/
196		p2tf->tf_regs[FRAME_A4] = 1;	/* is child (FreeBSD) 	*/
197
198		/*
199		 * Arrange for continuation at child_return(), which
200		 * will return to exception_return().  Note that the child
201		 * process doesn't stay in the kernel for long!
202		 *
203		 * This is an inlined version of cpu_set_kpc.
204		 */
205		up->u_pcb.pcb_hw.apcb_ksp = (u_int64_t)p2tf;
206		up->u_pcb.pcb_context[0] =
207		    (u_int64_t)child_return;		/* s0: pc */
208		up->u_pcb.pcb_context[1] =
209		    (u_int64_t)exception_return;	/* s1: ra */
210		up->u_pcb.pcb_context[2] = (u_long) p2;	/* s2: a0 */
211		up->u_pcb.pcb_context[7] =
212		    (u_int64_t)switch_trampoline;	/* ra: assembly magic */
213
214		/*
215		 * Clear the saved recursion count for sched_lock
216		 * since the child needs only one count which is
217		 * released in switch_trampoline.
218		 */
219		up->u_pcb.pcb_schednest = 0;
220	}
221}
222
223/*
224 * Intercept the return address from a freshly forked process that has NOT
225 * been scheduled yet.
226 *
227 * This is needed to make kernel threads stay in kernel mode.
228 */
229void
230cpu_set_fork_handler(p, func, arg)
231	struct proc *p;
232	void (*func) __P((void *));
233	void *arg;
234{
235	/*
236	 * Note that the trap frame follows the args, so the function
237	 * is really called like this:  func(arg, frame);
238	 */
239	p->p_addr->u_pcb.pcb_context[0] = (u_long) func;
240	p->p_addr->u_pcb.pcb_context[2] = (u_long) arg;
241}
242
243/*
244 * cpu_exit is called as the last action during exit.
245 * We release the address space of the process, block interrupts,
246 * and call switch_exit.  switch_exit switches to proc0's PCB and stack,
247 * then jumps into the middle of cpu_switch, as if it were switching
248 * from proc0.
249 */
250void
251cpu_exit(p)
252	register struct proc *p;
253{
254	alpha_fpstate_drop(p);
255
256	mtx_enter(&sched_lock, MTX_SPIN);
257	mtx_exit(&Giant, MTX_DEF | MTX_NOSWITCH);
258	mtx_assert(&Giant, MA_NOTOWNED);
259	cnt.v_swtch++;
260	cpu_switch();
261	panic("cpu_exit");
262}
263
264void
265cpu_wait(p)
266	struct proc *p;
267{
268	/* drop per-process resources */
269	pmap_dispose_proc(p);
270
271	/* and clean-out the vmspace */
272	vmspace_free(p->p_vmspace);
273}
274
275/*
276 * Dump the machine specific header information at the start of a core dump.
277 */
278int
279cpu_coredump(p, vp, cred)
280	struct proc *p;
281	struct vnode *vp;
282	struct ucred *cred;
283{
284
285	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
286	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
287	    p));
288}
289
290#ifdef notyet
291static void
292setredzone(pte, vaddr)
293	u_short *pte;
294	caddr_t vaddr;
295{
296/* eventually do this by setting up an expand-down stack segment
297   for ss0: selector, allowing stack access down to top of u.
298   this means though that protection violations need to be handled
299   thru a double fault exception that must do an integral task
300   switch to a known good context, within which a dump can be
301   taken. a sensible scheme might be to save the initial context
302   used by sched (that has physical memory mapped 1:1 at bottom)
303   and take the dump while still in mapped mode */
304}
305#endif
306
307/*
308 * Map an IO request into kernel virtual address space.
309 *
310 * All requests are (re)mapped into kernel VA space.
311 * Notice that we use b_bufsize for the size of the buffer
312 * to be mapped.  b_bcount might be modified by the driver.
313 */
314void
315vmapbuf(bp)
316	register struct buf *bp;
317{
318	register caddr_t addr, v, kva;
319	vm_offset_t pa;
320
321	if ((bp->b_flags & B_PHYS) == 0)
322		panic("vmapbuf");
323
324	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data);
325	    addr < bp->b_data + bp->b_bufsize;
326	    addr += PAGE_SIZE, v += PAGE_SIZE) {
327		/*
328		 * Do the vm_fault if needed; do the copy-on-write thing
329		 * when reading stuff off device into memory.
330		 */
331		vm_fault_quick(addr,
332			(bp->b_iocmd == BIO_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
333		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
334		if (pa == 0)
335			panic("vmapbuf: page not present");
336		vm_page_hold(PHYS_TO_VM_PAGE(pa));
337		pmap_kenter((vm_offset_t) v, pa);
338	}
339
340	kva = bp->b_saveaddr;
341	bp->b_saveaddr = bp->b_data;
342	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
343}
344
345/*
346 * Free the io map PTEs associated with this IO operation.
347 * We also invalidate the TLB entries and restore the original b_addr.
348 */
349void
350vunmapbuf(bp)
351	register struct buf *bp;
352{
353	register caddr_t addr;
354	vm_offset_t pa;
355
356	if ((bp->b_flags & B_PHYS) == 0)
357		panic("vunmapbuf");
358
359	for (addr = (caddr_t)trunc_page(bp->b_data);
360	    addr < bp->b_data + bp->b_bufsize;
361	    addr += PAGE_SIZE) {
362		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
363		pmap_kremove((vm_offset_t) addr);
364		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
365	}
366
367	bp->b_data = bp->b_saveaddr;
368}
369
370/*
371 * Reset back to firmware.
372 */
373void
374cpu_reset()
375{
376	prom_halt(0);
377}
378
379int
380grow_stack(p, sp)
381	struct proc *p;
382	size_t sp;
383{
384	int rv;
385
386	rv = vm_map_growstack (p, sp);
387	if (rv != KERN_SUCCESS)
388		return (0);
389
390	return (1);
391}
392
393
394static int cnt_prezero;
395
396SYSCTL_INT(_machdep, OID_AUTO, cnt_prezero, CTLFLAG_RD, &cnt_prezero, 0, "");
397
398/*
399 * Implement the pre-zeroed page mechanism.
400 * This routine is called from the idle loop.
401 */
402
403#define ZIDLE_LO(v)    ((v) * 2 / 3)
404#define ZIDLE_HI(v)    ((v) * 4 / 5)
405
406int
407vm_page_zero_idle()
408{
409	static int free_rover;
410	static int zero_state;
411	vm_page_t m;
412	int s;
413
414	/*
415         * Attempt to maintain approximately 1/2 of our free pages in a
416         * PG_ZERO'd state.   Add some hysteresis to (attempt to) avoid
417         * generally zeroing a page when the system is near steady-state.
418         * Otherwise we might get 'flutter' during disk I/O / IPC or
419         * fast sleeps.  We also do not want to be continuously zeroing
420         * pages because doing so may flush our L1 and L2 caches too much.
421	 */
422
423	if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count))
424		return(0);
425	if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
426		return(0);
427
428#ifdef SMP
429	if (KLOCK_ENTER(M_TRY)) {
430#endif
431		s = splvm();
432		m = vm_page_list_find(PQ_FREE, free_rover, FALSE);
433		zero_state = 0;
434		if (m != NULL && (m->flags & PG_ZERO) == 0) {
435			vm_page_queues[m->queue].lcnt--;
436			TAILQ_REMOVE(&vm_page_queues[m->queue].pl, m, pageq);
437			m->queue = PQ_NONE;
438			splx(s);
439#if 0
440			rel_mplock();
441#endif
442			pmap_zero_page(VM_PAGE_TO_PHYS(m));
443#if 0
444			get_mplock();
445#endif
446			(void)splvm();
447			vm_page_flag_set(m, PG_ZERO);
448			m->queue = PQ_FREE + m->pc;
449			vm_page_queues[m->queue].lcnt++;
450			TAILQ_INSERT_TAIL(&vm_page_queues[m->queue].pl, m,
451			    pageq);
452			++vm_page_zero_count;
453			++cnt_prezero;
454			if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
455				zero_state = 1;
456		}
457		free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK;
458		splx(s);
459#ifdef SMP
460		KLOCK_EXIT;
461#endif
462		return (1);
463#ifdef SMP
464	}
465#endif
466	return (0);
467}
468
469/*
470 * Software interrupt handler for queued VM system processing.
471 */
472void
473swi_vm(void *dummy)
474{
475	if (busdma_swi_pending != 0)
476		busdma_swi();
477}
478
479/*
480 * Tell whether this address is in some physical memory region.
481 * Currently used by the kernel coredump code in order to avoid
482 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
483 * or other unpredictable behaviour.
484 */
485
486
487int
488is_physical_memory(addr)
489	vm_offset_t addr;
490{
491	/*
492	 * stuff other tests for known memory-mapped devices (PCI?)
493	 * here
494	 */
495
496	return 1;
497}
498