vm_machdep.c revision 209975
136865Sdfr/*-
236865Sdfr * Copyright (c) 1982, 1986 The Regents of the University of California.
336865Sdfr * Copyright (c) 1989, 1990 William Jolitz
436865Sdfr * Copyright (c) 1994 John Dyson
536865Sdfr * All rights reserved.
636865Sdfr *
736865Sdfr * This code is derived from software contributed to Berkeley by
836865Sdfr * the Systems Programming Group of the University of Utah Computer
936865Sdfr * Science Department, and William Jolitz.
1036865Sdfr *
1136865Sdfr * Redistribution and use in source and binary forms, with or without
1236865Sdfr * modification, are permitted provided that the following conditions
1336865Sdfr * are met:
1436865Sdfr * 1. Redistributions of source code must retain the above copyright
1536865Sdfr *    notice, this list of conditions and the following disclaimer.
1636865Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1736865Sdfr *    notice, this list of conditions and the following disclaimer in the
1836865Sdfr *    documentation and/or other materials provided with the distribution.
1936865Sdfr * 3. All advertising materials mentioning features or use of this software
2036865Sdfr *    must display the following acknowledgement:
2136865Sdfr *	This product includes software developed by the University of
2236865Sdfr *	California, Berkeley and its contributors.
2336865Sdfr * 4. Neither the name of the University nor the names of its contributors
2436865Sdfr *    may be used to endorse or promote products derived from this software
2536865Sdfr *    without specific prior written permission.
2636865Sdfr *
2736865Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2836865Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2936865Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3036865Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3136865Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3236865Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3336865Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3436865Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3536865Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3636865Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3736865Sdfr * SUCH DAMAGE.
3836865Sdfr *
3936865Sdfr *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
4036865Sdfr *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
4150477Speter * $FreeBSD: head/sys/powerpc/aim/vm_machdep.c 209975 2010-07-13 05:32:19Z nwhitehorn $
4236865Sdfr */
43139825Simp/*-
4436865Sdfr * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
4536865Sdfr * All rights reserved.
4636865Sdfr *
4736865Sdfr * Author: Chris G. Demetriou
48126474Sgrehan *
4936865Sdfr * Permission to use, copy, modify and distribute this software and
5036865Sdfr * its documentation is hereby granted, provided that both the copyright
5136865Sdfr * notice and this permission notice appear in all copies of the
5236865Sdfr * software, derivative works or modified versions, and any portions
5336865Sdfr * thereof, and that both notices appear in supporting documentation.
54126474Sgrehan *
55126474Sgrehan * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56126474Sgrehan * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
5736865Sdfr * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58126474Sgrehan *
5936865Sdfr * Carnegie Mellon requests users of this software to return to
6036865Sdfr *
6136865Sdfr *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
6236865Sdfr *  School of Computer Science
6336865Sdfr *  Carnegie Mellon University
6436865Sdfr *  Pittsburgh PA 15213-3890
6536865Sdfr *
6636865Sdfr * any improvements or extensions that they make and grant Carnegie the
6736865Sdfr * rights to redistribute these changes.
6836865Sdfr */
6936865Sdfr
7036865Sdfr#include <sys/param.h>
7136865Sdfr#include <sys/systm.h>
7236865Sdfr#include <sys/proc.h>
7336865Sdfr#include <sys/malloc.h>
7460041Sphk#include <sys/bio.h>
7536865Sdfr#include <sys/buf.h>
7685201Smp#include <sys/ktr.h>
7785201Smp#include <sys/lock.h>
7867365Sjhb#include <sys/mutex.h>
7936865Sdfr#include <sys/vnode.h>
8036865Sdfr#include <sys/vmmeter.h>
8136865Sdfr#include <sys/kernel.h>
82119563Salc#include <sys/mbuf.h>
83122780Salc#include <sys/sf_buf.h>
8436865Sdfr#include <sys/sysctl.h>
85199135Skib#include <sys/sysent.h>
8654207Speter#include <sys/unistd.h>
8736865Sdfr
8836865Sdfr#include <machine/cpu.h>
8941499Sdfr#include <machine/fpu.h>
9097397Sbenno#include <machine/frame.h>
9136865Sdfr#include <machine/md_var.h>
92138129Sdas#include <machine/pcb.h>
9336865Sdfr
9478342Sbenno#include <dev/ofw/openfirm.h>
9578342Sbenno
9636865Sdfr#include <vm/vm.h>
9736865Sdfr#include <vm/vm_param.h>
9836865Sdfr#include <vm/vm_kern.h>
9936865Sdfr#include <vm/vm_page.h>
10036865Sdfr#include <vm/vm_map.h>
10136865Sdfr#include <vm/vm_extern.h>
10236865Sdfr
10336865Sdfr/*
104190681Snwhitehorn * On systems without a direct mapped region (e.g. PPC64),
105190681Snwhitehorn * we use the same code as the Book E implementation. Since
106190681Snwhitehorn * we need to have runtime detection of this, define some machinery
107190681Snwhitehorn * for sf_bufs in this case, and ignore it on systems with direct maps.
108190681Snwhitehorn */
109190681Snwhitehorn
110190681Snwhitehorn#ifndef NSFBUFS
111190681Snwhitehorn#define NSFBUFS         (512 + maxusers * 16)
112190681Snwhitehorn#endif
113190681Snwhitehorn
114190681Snwhitehornstatic void sf_buf_init(void *arg);
115190681SnwhitehornSYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL);
116190681Snwhitehorn
117190681SnwhitehornLIST_HEAD(sf_head, sf_buf);
118190681Snwhitehorn
119190681Snwhitehorn/* A hash table of active sendfile(2) buffers */
120190681Snwhitehornstatic struct sf_head *sf_buf_active;
121190681Snwhitehornstatic u_long sf_buf_hashmask;
122190681Snwhitehorn
123190681Snwhitehorn#define SF_BUF_HASH(m)  (((m) - vm_page_array) & sf_buf_hashmask)
124190681Snwhitehorn
125190681Snwhitehornstatic TAILQ_HEAD(, sf_buf) sf_buf_freelist;
126190681Snwhitehornstatic u_int sf_buf_alloc_want;
127190681Snwhitehorn
128190681Snwhitehorn/*
129190681Snwhitehorn * A lock used to synchronize access to the hash table and free list
130190681Snwhitehorn */
131190681Snwhitehornstatic struct mtx sf_buf_lock;
132190681Snwhitehorn
133209975Snwhitehorn#ifdef __powerpc64__
134209975Snwhitehornextern uintptr_t tocbase;
135209975Snwhitehorn#endif
136190681Snwhitehorn
137209975Snwhitehorn
138190681Snwhitehorn/*
13936865Sdfr * Finish a fork operation, with process p2 nearly set up.
14036865Sdfr * Copy and update the pcb, set up the stack so that the child
14136865Sdfr * ready to run and return to user mode.
14236865Sdfr */
14336865Sdfrvoid
14490361Sjuliancpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
14536865Sdfr{
14684977Sbenno	struct	proc *p1;
14784977Sbenno	struct	trapframe *tf;
14884977Sbenno	struct	callframe *cf;
14990643Sbenno	struct	pcb *pcb;
15084977Sbenno
15191467Sbenno	KASSERT(td1 == curthread || td1 == &thread0,
15290643Sbenno	    ("cpu_fork: p1 not curproc and not proc0"));
153209975Snwhitehorn	CTR3(KTR_PROC, "cpu_fork: called td1=%p p2=%p flags=%x",
154209975Snwhitehorn	    td1, p2, flags);
15584977Sbenno
15684977Sbenno	if ((flags & RFPROC) == 0)
15784977Sbenno		return;
15884977Sbenno
15984977Sbenno	p1 = td1->td_proc;
16084977Sbenno
161127582Speter	pcb = (struct pcb *)((td2->td_kstack +
162209975Snwhitehorn	    td2->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb)) & ~0x2fUL);
16390643Sbenno	td2->td_pcb = pcb;
16484977Sbenno
16590643Sbenno	/* Copy the pcb */
16690643Sbenno	bcopy(td1->td_pcb, pcb, sizeof(struct pcb));
16784977Sbenno
16884977Sbenno	/*
16990643Sbenno	 * Create a fresh stack for the new process.
17084977Sbenno	 * Copy the trap frame for the return to user mode as if from a
17184977Sbenno	 * syscall.  This copies most of the user mode register values.
17284977Sbenno	 */
17390643Sbenno	tf = (struct trapframe *)pcb - 1;
17490643Sbenno	bcopy(td1->td_frame, tf, sizeof(*tf));
17584977Sbenno
17697397Sbenno	/* Set up trap frame. */
17797397Sbenno	tf->fixreg[FIRSTARG] = 0;
17897397Sbenno	tf->fixreg[FIRSTARG + 1] = 0;
17997397Sbenno	tf->cr &= ~0x10000000;
18090643Sbenno
18190643Sbenno	td2->td_frame = tf;
18290643Sbenno
18390643Sbenno	cf = (struct callframe *)tf - 1;
184132520Sgrehan	memset(cf, 0, sizeof(struct callframe));
185209975Snwhitehorn	#ifdef __powerpc64__
186209975Snwhitehorn	cf->cf_toc = tocbase;
187209975Snwhitehorn	#endif
18891467Sbenno	cf->cf_func = (register_t)fork_return;
18991467Sbenno	cf->cf_arg0 = (register_t)td2;
19091467Sbenno	cf->cf_arg1 = (register_t)tf;
19184977Sbenno
19291467Sbenno	pcb->pcb_sp = (register_t)cf;
193209975Snwhitehorn	#ifdef __powerpc64__
194209975Snwhitehorn	pcb->pcb_lr = ((register_t *)fork_trampoline)[0];
195209975Snwhitehorn	pcb->pcb_toc = ((register_t *)fork_trampoline)[1];
196209975Snwhitehorn	#else
19791467Sbenno	pcb->pcb_lr = (register_t)fork_trampoline;
198209975Snwhitehorn	#endif
199209975Snwhitehorn	pcb->pcb_cpu.aim.usr_vsid = 0;
200209975Snwhitehorn	pcb->pcb_cpu.aim.usr_esid = 0;
20184977Sbenno
202170305Sjeff	/* Setup to release spin count in fork_exit(). */
203144637Sjhb	td2->td_md.md_spinlock_count = 1;
204144637Sjhb	td2->td_md.md_saved_msr = PSL_KERNSET;
205144637Sjhb
20690643Sbenno	/*
20790643Sbenno 	 * Now cpu_switch() can schedule the new process.
20890643Sbenno	 */
20936865Sdfr}
21036865Sdfr
21136865Sdfr/*
21236865Sdfr * Intercept the return address from a freshly forked process that has NOT
21336865Sdfr * been scheduled yet.
21436865Sdfr *
21536865Sdfr * This is needed to make kernel threads stay in kernel mode.
21636865Sdfr */
21736865Sdfrvoid
21883682Smpcpu_set_fork_handler(td, func, arg)
21983682Smp	struct thread *td;
22092842Salfred	void (*func)(void *);
22148391Speter	void *arg;
22236865Sdfr{
22384977Sbenno	struct	callframe *cf;
22484977Sbenno
225209975Snwhitehorn	CTR4(KTR_PROC, "%s called with td=%p func=%p arg=%p",
226209975Snwhitehorn	    __func__, td, func, arg);
22784977Sbenno
22891467Sbenno	cf = (struct callframe *)td->td_pcb->pcb_sp;
22984977Sbenno
23091467Sbenno	cf->cf_func = (register_t)func;
23191467Sbenno	cf->cf_arg0 = (register_t)arg;
23236865Sdfr}
23336865Sdfr
23436865Sdfrvoid
23583682Smpcpu_exit(td)
23683682Smp	register struct thread *td;
23736865Sdfr{
23836865Sdfr}
23936865Sdfr
24036865Sdfr/*
24165557Sjasone * Reset back to firmware.
24236865Sdfr */
24336865Sdfrvoid
24436865Sdfrcpu_reset()
24536865Sdfr{
246165608Smarcel	OF_reboot();
24736865Sdfr}
24836865Sdfr
24936865Sdfr/*
250190681Snwhitehorn * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
251119563Salc */
252190681Snwhitehornstatic void
253190681Snwhitehornsf_buf_init(void *arg)
254190681Snwhitehorn{
255190681Snwhitehorn        struct sf_buf *sf_bufs;
256190681Snwhitehorn        vm_offset_t sf_base;
257190681Snwhitehorn        int i;
258190681Snwhitehorn
259190681Snwhitehorn	/* Don't bother on systems with a direct map */
260190681Snwhitehorn
261190681Snwhitehorn	if (hw_direct_map)
262190681Snwhitehorn		return;
263190681Snwhitehorn
264190681Snwhitehorn        nsfbufs = NSFBUFS;
265190681Snwhitehorn        TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
266190681Snwhitehorn
267190681Snwhitehorn        sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask);
268190681Snwhitehorn        TAILQ_INIT(&sf_buf_freelist);
269190681Snwhitehorn        sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE);
270190681Snwhitehorn        sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, M_NOWAIT | M_ZERO);
271190681Snwhitehorn
272190681Snwhitehorn        for (i = 0; i < nsfbufs; i++) {
273190681Snwhitehorn                sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
274190681Snwhitehorn                TAILQ_INSERT_TAIL(&sf_buf_freelist, &sf_bufs[i], free_entry);
275190681Snwhitehorn        }
276190681Snwhitehorn        sf_buf_alloc_want = 0;
277190681Snwhitehorn        mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF);
278190681Snwhitehorn}
279190681Snwhitehorn
280190681Snwhitehorn/*
281190681Snwhitehorn * Get an sf_buf from the freelist. Will block if none are available.
282190681Snwhitehorn */
283119563Salcstruct sf_buf *
284190681Snwhitehornsf_buf_alloc(struct vm_page *m, int flags)
285119563Salc{
286190681Snwhitehorn        struct sf_head *hash_list;
287190681Snwhitehorn        struct sf_buf *sf;
288190681Snwhitehorn        int error;
289119563Salc
290190681Snwhitehorn	if (hw_direct_map) {
291190681Snwhitehorn		/* Shortcut the direct mapped case */
292190681Snwhitehorn
293190681Snwhitehorn		return ((struct sf_buf *)m);
294190681Snwhitehorn	}
295190681Snwhitehorn
296190681Snwhitehorn        hash_list = &sf_buf_active[SF_BUF_HASH(m)];
297190681Snwhitehorn        mtx_lock(&sf_buf_lock);
298190681Snwhitehorn        LIST_FOREACH(sf, hash_list, list_entry) {
299190681Snwhitehorn                if (sf->m == m) {
300190681Snwhitehorn                        sf->ref_count++;
301190681Snwhitehorn                        if (sf->ref_count == 1) {
302190681Snwhitehorn                                TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
303190681Snwhitehorn                                nsfbufsused++;
304190681Snwhitehorn                                nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
305190681Snwhitehorn                        }
306190681Snwhitehorn                        goto done;
307190681Snwhitehorn                }
308190681Snwhitehorn        }
309190681Snwhitehorn
310190681Snwhitehorn        while ((sf = TAILQ_FIRST(&sf_buf_freelist)) == NULL) {
311190681Snwhitehorn                if (flags & SFB_NOWAIT)
312190681Snwhitehorn                        goto done;
313190681Snwhitehorn
314190681Snwhitehorn                sf_buf_alloc_want++;
315190681Snwhitehorn                mbstat.sf_allocwait++;
316190681Snwhitehorn                error = msleep(&sf_buf_freelist, &sf_buf_lock,
317190681Snwhitehorn                    (flags & SFB_CATCH) ? PCATCH | PVM : PVM, "sfbufa", 0);
318190681Snwhitehorn                sf_buf_alloc_want--;
319190681Snwhitehorn
320190681Snwhitehorn                /*
321190681Snwhitehorn                 * If we got a signal, don't risk going back to sleep.
322190681Snwhitehorn                 */
323190681Snwhitehorn                if (error)
324190681Snwhitehorn                        goto done;
325190681Snwhitehorn        }
326190681Snwhitehorn
327190681Snwhitehorn        TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
328190681Snwhitehorn        if (sf->m != NULL)
329190681Snwhitehorn                LIST_REMOVE(sf, list_entry);
330190681Snwhitehorn
331190681Snwhitehorn        LIST_INSERT_HEAD(hash_list, sf, list_entry);
332190681Snwhitehorn        sf->ref_count = 1;
333190681Snwhitehorn        sf->m = m;
334190681Snwhitehorn        nsfbufsused++;
335190681Snwhitehorn        nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
336190681Snwhitehorn        pmap_qenter(sf->kva, &sf->m, 1);
337190681Snwhitehorndone:
338190681Snwhitehorn        mtx_unlock(&sf_buf_lock);
339190681Snwhitehorn        return (sf);
340119563Salc}
341119563Salc
342119563Salc/*
343190681Snwhitehorn * Detatch mapped page and release resources back to the system.
344190681Snwhitehorn *
345190681Snwhitehorn * Remove a reference from the given sf_buf, adding it to the free
346190681Snwhitehorn * list when its reference count reaches zero. A freed sf_buf still,
347190681Snwhitehorn * however, retains its virtual-to-physical mapping until it is
348190681Snwhitehorn * recycled or reactivated by sf_buf_alloc(9).
349119563Salc */
350119563Salcvoid
351127086Salcsf_buf_free(struct sf_buf *sf)
352119563Salc{
353190681Snwhitehorn	if (hw_direct_map)
354190681Snwhitehorn		return;
355190681Snwhitehorn
356190681Snwhitehorn        mtx_lock(&sf_buf_lock);
357190681Snwhitehorn        sf->ref_count--;
358190681Snwhitehorn        if (sf->ref_count == 0) {
359190681Snwhitehorn                TAILQ_INSERT_TAIL(&sf_buf_freelist, sf, free_entry);
360190681Snwhitehorn                nsfbufsused--;
361190681Snwhitehorn
362190681Snwhitehorn                if (sf_buf_alloc_want > 0)
363190681Snwhitehorn                        wakeup_one(&sf_buf_freelist);
364190681Snwhitehorn        }
365190681Snwhitehorn        mtx_unlock(&sf_buf_lock);
366119563Salc}
367119563Salc
368119563Salc/*
36936865Sdfr * Software interrupt handler for queued VM system processing.
370126474Sgrehan */
371126474Sgrehanvoid
372126474Sgrehanswi_vm(void *dummy)
373126474Sgrehan{
374209950Snwhitehorn
37536865Sdfr	if (busdma_swi_pending != 0)
37636865Sdfr		busdma_swi();
37736865Sdfr}
37836865Sdfr
37936865Sdfr/*
38036865Sdfr * Tell whether this address is in some physical memory region.
38136865Sdfr * Currently used by the kernel coredump code in order to avoid
38236865Sdfr * dumping the ``ISA memory hole'' which could cause indefinite hangs,
38336865Sdfr * or other unpredictable behaviour.
38436865Sdfr */
38536865Sdfr
38636865Sdfr
38736865Sdfrint
38836865Sdfris_physical_memory(addr)
38936865Sdfr	vm_offset_t addr;
39036865Sdfr{
39136865Sdfr	/*
39236865Sdfr	 * stuff other tests for known memory-mapped devices (PCI?)
39336865Sdfr	 * here
39436865Sdfr	 */
39536865Sdfr
39636865Sdfr	return 1;
39736865Sdfr}
39899659Sbenno
39999659Sbenno/*
400209975Snwhitehorn * CPU threading functions related to the VM layer. These could be used
401209975Snwhitehorn * to map the SLB bits required for the kernel stack instead of forcing a
402209975Snwhitehorn * fixed-size KVA.
40399659Sbenno */
40499659Sbenno
40599659Sbennovoid
406119004Smarcelcpu_thread_swapin(struct thread *td)
407119004Smarcel{
40899659Sbenno}
40999659Sbenno
41099659Sbennovoid
411119004Smarcelcpu_thread_swapout(struct thread *td)
412119004Smarcel{
413119004Smarcel}
414119004Smarcel
415