vm_machdep.c revision 127788
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 127788 2004-04-03 09:16:27Z alc $
4236865Sdfr */
4336865Sdfr/*
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>
8554207Speter#include <sys/unistd.h>
8636865Sdfr
8736865Sdfr#include <machine/clock.h>
8836865Sdfr#include <machine/cpu.h>
8941499Sdfr#include <machine/fpu.h>
9097397Sbenno#include <machine/frame.h>
9136865Sdfr#include <machine/md_var.h>
9236865Sdfr
9378342Sbenno#include <dev/ofw/openfirm.h>
9478342Sbenno
9536865Sdfr#include <vm/vm.h>
9636865Sdfr#include <vm/vm_param.h>
9736865Sdfr#include <vm/vm_kern.h>
9836865Sdfr#include <vm/vm_page.h>
9936865Sdfr#include <vm/vm_map.h>
10036865Sdfr#include <vm/vm_extern.h>
10136865Sdfr
10236865Sdfr#include <sys/user.h>
10336865Sdfr
104119563Salcstatic void	sf_buf_init(void *arg);
105119563SalcSYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL)
106119563Salc
10736865Sdfr/*
108119563Salc * Expanded sf_freelist head. Really an SLIST_HEAD() in disguise, with the
109119563Salc * sf_freelist head with the sf_lock mutex.
110119563Salc */
111119563Salcstatic struct {
112119563Salc	SLIST_HEAD(, sf_buf) sf_head;
113119563Salc	struct mtx sf_lock;
114119563Salc} sf_freelist;
115119563Salc
116119563Salcstatic u_int	sf_buf_alloc_want;
117119563Salc
118119563Salc/*
11936865Sdfr * Finish a fork operation, with process p2 nearly set up.
12036865Sdfr * Copy and update the pcb, set up the stack so that the child
12136865Sdfr * ready to run and return to user mode.
12236865Sdfr */
12336865Sdfrvoid
12490361Sjuliancpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
12536865Sdfr{
12684977Sbenno	struct	proc *p1;
12784977Sbenno	struct	trapframe *tf;
12884977Sbenno	struct	callframe *cf;
12990643Sbenno	struct	pcb *pcb;
13084977Sbenno
13191467Sbenno	KASSERT(td1 == curthread || td1 == &thread0,
13290643Sbenno	    ("cpu_fork: p1 not curproc and not proc0"));
13384977Sbenno	CTR3(KTR_PROC, "cpu_fork: called td1=%08x p2=%08x flags=%x", (u_int)td1, (u_int)p2, flags);
13484977Sbenno
13584977Sbenno	if ((flags & RFPROC) == 0)
13684977Sbenno		return;
13784977Sbenno
13884977Sbenno	p1 = td1->td_proc;
13984977Sbenno
140127582Speter	pcb = (struct pcb *)((td2->td_kstack +
141127582Speter	    td2->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb)) & ~0x2fU);
14290643Sbenno	td2->td_pcb = pcb;
14384977Sbenno
14490643Sbenno	/* Copy the pcb */
14590643Sbenno	bcopy(td1->td_pcb, pcb, sizeof(struct pcb));
14684977Sbenno
14784977Sbenno	/*
14890643Sbenno	 * Create a fresh stack for the new process.
14984977Sbenno	 * Copy the trap frame for the return to user mode as if from a
15084977Sbenno	 * syscall.  This copies most of the user mode register values.
15184977Sbenno	 */
15290643Sbenno	tf = (struct trapframe *)pcb - 1;
15390643Sbenno	bcopy(td1->td_frame, tf, sizeof(*tf));
15484977Sbenno
15597397Sbenno	/* Set up trap frame. */
15697397Sbenno	tf->fixreg[FIRSTARG] = 0;
15797397Sbenno	tf->fixreg[FIRSTARG + 1] = 0;
15897397Sbenno	tf->cr &= ~0x10000000;
15990643Sbenno
16090643Sbenno	td2->td_frame = tf;
16190643Sbenno
16290643Sbenno	cf = (struct callframe *)tf - 1;
16391467Sbenno	cf->cf_func = (register_t)fork_return;
16491467Sbenno	cf->cf_arg0 = (register_t)td2;
16591467Sbenno	cf->cf_arg1 = (register_t)tf;
16684977Sbenno
16791467Sbenno	pcb->pcb_sp = (register_t)cf;
16891467Sbenno	pcb->pcb_lr = (register_t)fork_trampoline;
169105611Sgrehan	pcb->pcb_usr = kernel_pmap->pm_sr[USER_SR];
17084977Sbenno
17190643Sbenno	/*
17290643Sbenno 	 * Now cpu_switch() can schedule the new process.
17390643Sbenno	 */
17436865Sdfr}
17536865Sdfr
17636865Sdfr/*
17736865Sdfr * Intercept the return address from a freshly forked process that has NOT
17836865Sdfr * been scheduled yet.
17936865Sdfr *
18036865Sdfr * This is needed to make kernel threads stay in kernel mode.
18136865Sdfr */
18236865Sdfrvoid
18383682Smpcpu_set_fork_handler(td, func, arg)
18483682Smp	struct thread *td;
18592842Salfred	void (*func)(void *);
18648391Speter	void *arg;
18736865Sdfr{
18884977Sbenno	struct	callframe *cf;
18984977Sbenno
19084977Sbenno	CTR3(KTR_PROC, "cpu_set_fork_handler: called with td=%08x func=%08x arg=%08x",
19184977Sbenno	    (u_int)td, (u_int)func, (u_int)arg);
19284977Sbenno
19391467Sbenno	cf = (struct callframe *)td->td_pcb->pcb_sp;
19484977Sbenno
19591467Sbenno	cf->cf_func = (register_t)func;
19691467Sbenno	cf->cf_arg0 = (register_t)arg;
19736865Sdfr}
19836865Sdfr
19936865Sdfr/*
20036865Sdfr * cpu_exit is called as the last action during exit.
20136865Sdfr * We release the address space of the process, block interrupts,
20236865Sdfr * and call switch_exit.  switch_exit switches to proc0's PCB and stack,
20336865Sdfr * then jumps into the middle of cpu_switch, as if it were switching
20436865Sdfr * from proc0.
20536865Sdfr */
20636865Sdfrvoid
20783682Smpcpu_exit(td)
20883682Smp	register struct thread *td;
20936865Sdfr{
21036865Sdfr}
21136865Sdfr
21236865Sdfrvoid
21398765Sjakecpu_sched_exit(td)
21498765Sjake	register struct thread *td;
21598765Sjake{
21698765Sjake}
21798765Sjake
21883276Speter/* Temporary helper */
21983276Spetervoid
220118893Sgrehancpu_throw(struct thread *old, struct thread *new)
22183276Speter{
22283276Speter
223118893Sgrehan	cpu_switch(old, new);
22483276Speter	panic("cpu_throw() didn't");
22536865Sdfr}
22636865Sdfr
22736865Sdfr/*
22865557Sjasone * Reset back to firmware.
22936865Sdfr */
23036865Sdfrvoid
23136865Sdfrcpu_reset()
23236865Sdfr{
23378342Sbenno	OF_exit();
23436865Sdfr}
23536865Sdfr
23636865Sdfr/*
237119563Salc * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
238119563Salc */
239119563Salcstatic void
240119563Salcsf_buf_init(void *arg)
241119563Salc{
242119563Salc	struct sf_buf *sf_bufs;
243119563Salc	vm_offset_t sf_base;
244119563Salc	int i;
245119563Salc
246119563Salc	mtx_init(&sf_freelist.sf_lock, "sf_bufs list lock", NULL, MTX_DEF);
247119563Salc	SLIST_INIT(&sf_freelist.sf_head);
248119563Salc	sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE);
249119563Salc	sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
250119563Salc	    M_NOWAIT | M_ZERO);
251119563Salc	for (i = 0; i < nsfbufs; i++) {
252119563Salc		sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
253119563Salc		SLIST_INSERT_HEAD(&sf_freelist.sf_head, &sf_bufs[i], free_list);
254119563Salc	}
255119563Salc	sf_buf_alloc_want = 0;
256119563Salc}
257119563Salc
258119563Salc/*
259119563Salc * Get an sf_buf from the freelist. Will block if none are available.
260119563Salc */
261119563Salcstruct sf_buf *
262127788Salcsf_buf_alloc(struct vm_page *m, int pri)
263119563Salc{
264119563Salc	struct sf_buf *sf;
265119563Salc	int error;
266119563Salc
267119563Salc	mtx_lock(&sf_freelist.sf_lock);
268119563Salc	while ((sf = SLIST_FIRST(&sf_freelist.sf_head)) == NULL) {
269119563Salc		sf_buf_alloc_want++;
270123929Ssilby		mbstat.sf_allocwait++;
271127788Salc		error = msleep(&sf_freelist, &sf_freelist.sf_lock, PVM | pri,
272119563Salc		    "sfbufa", 0);
273119563Salc		sf_buf_alloc_want--;
274119563Salc
275119563Salc		/*
276126474Sgrehan		 * If we got a signal, don't risk going back to sleep.
277119563Salc		 */
278119563Salc		if (error)
279119563Salc			break;
280119563Salc	}
281119563Salc	if (sf != NULL) {
282119563Salc		SLIST_REMOVE_HEAD(&sf_freelist.sf_head, free_list);
283119563Salc		sf->m = m;
284123884Ssilby		nsfbufsused++;
285123920Ssilby		nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
286119563Salc		pmap_qenter(sf->kva, &sf->m, 1);
287119563Salc	}
288119563Salc	mtx_unlock(&sf_freelist.sf_lock);
289119563Salc	return (sf);
290119563Salc}
291119563Salc
292119563Salc/*
293127086Salc * Release resources back to the system.
294119563Salc */
295119563Salcvoid
296127086Salcsf_buf_free(struct sf_buf *sf)
297119563Salc{
298119563Salc
299127086Salc	pmap_qremove(sf->kva, 1);
300119563Salc	mtx_lock(&sf_freelist.sf_lock);
301119563Salc	SLIST_INSERT_HEAD(&sf_freelist.sf_head, sf, free_list);
302123884Ssilby	nsfbufsused--;
303119563Salc	if (sf_buf_alloc_want > 0)
304119563Salc		wakeup_one(&sf_freelist);
305119563Salc	mtx_unlock(&sf_freelist.sf_lock);
306119563Salc}
307119563Salc
308119563Salc/*
30936865Sdfr * Software interrupt handler for queued VM system processing.
310126474Sgrehan */
311126474Sgrehanvoid
312126474Sgrehanswi_vm(void *dummy)
313126474Sgrehan{
31478342Sbenno#if 0 /* XXX: Don't have busdma stuff yet */
31536865Sdfr	if (busdma_swi_pending != 0)
31636865Sdfr		busdma_swi();
31778342Sbenno#endif
31836865Sdfr}
31936865Sdfr
32036865Sdfr/*
32136865Sdfr * Tell whether this address is in some physical memory region.
32236865Sdfr * Currently used by the kernel coredump code in order to avoid
32336865Sdfr * dumping the ``ISA memory hole'' which could cause indefinite hangs,
32436865Sdfr * or other unpredictable behaviour.
32536865Sdfr */
32636865Sdfr
32736865Sdfr
32836865Sdfrint
32936865Sdfris_physical_memory(addr)
33036865Sdfr	vm_offset_t addr;
33136865Sdfr{
33236865Sdfr	/*
33336865Sdfr	 * stuff other tests for known memory-mapped devices (PCI?)
33436865Sdfr	 * here
33536865Sdfr	 */
33636865Sdfr
33736865Sdfr	return 1;
33836865Sdfr}
33999659Sbenno
34099659Sbenno/*
34199659Sbenno * KSE functions
34299659Sbenno */
34399659Sbennovoid
344126474Sgrehancpu_thread_exit(struct thread *td)
34599659Sbenno{
34699659Sbenno}
34799659Sbenno
34899659Sbennovoid
349126474Sgrehancpu_thread_clean(struct thread *td)
350107180Smux{
351107180Smux}
352107180Smux
353107180Smuxvoid
35499659Sbennocpu_thread_setup(struct thread *td)
35599659Sbenno{
356126474Sgrehan	struct pcb *pcb;
357126474Sgrehan
358127618Sbenno	pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
359126474Sgrehan	    sizeof(struct pcb)) & ~0x2fU);
360126474Sgrehan	td->td_pcb = pcb;
361126474Sgrehan	td->td_frame = (struct trapframe *)pcb - 1;
362119004Smarcel}
36399659Sbenno
364119004Smarcelvoid
365119004Smarcelcpu_thread_swapin(struct thread *td)
366119004Smarcel{
36799659Sbenno}
36899659Sbenno
36999659Sbennovoid
370119004Smarcelcpu_thread_swapout(struct thread *td)
371119004Smarcel{
372119004Smarcel}
373119004Smarcel
374119004Smarcelvoid
375115858Smarcelcpu_set_upcall(struct thread *td, struct thread *td0)
37699659Sbenno{
377126474Sgrehan	struct pcb *pcb2;
378126474Sgrehan	struct trapframe *tf;
379126474Sgrehan	struct callframe *cf;
380126474Sgrehan
381126474Sgrehan	pcb2 = td->td_pcb;
382126474Sgrehan
383126474Sgrehan	/* Copy the upcall pcb */
384126474Sgrehan	bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
385126474Sgrehan
386126474Sgrehan	/* Create a stack for the new thread */
387126474Sgrehan	tf = td->td_frame;
388126474Sgrehan	bcopy(td0->td_frame, tf, sizeof(struct trapframe));
389126474Sgrehan	tf->fixreg[FIRSTARG] = 0;
390126474Sgrehan	tf->fixreg[FIRSTARG + 1] = 0;
391126474Sgrehan	tf->cr &= ~0x10000000;
392126474Sgrehan
393126474Sgrehan	/* Set registers for trampoline to user mode. */
394126474Sgrehan	cf = (struct callframe *)tf - 1;
395126474Sgrehan	cf->cf_func = (register_t)fork_return;
396126474Sgrehan	cf->cf_arg0 = (register_t)td;
397126474Sgrehan	cf->cf_arg1 = (register_t)tf;
398126474Sgrehan
399126474Sgrehan	pcb2->pcb_sp = (register_t)cf;
400126474Sgrehan	pcb2->pcb_lr = (register_t)fork_trampoline;
401126474Sgrehan	pcb2->pcb_usr = kernel_pmap->pm_sr[USER_SR];
40299659Sbenno}
40399659Sbenno
40499659Sbennovoid
405111028Sjeffcpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku)
40699659Sbenno{
407126474Sgrehan        struct trapframe *tf;
408126474Sgrehan        uint32_t sp;
409126474Sgrehan
410126474Sgrehan	tf = td->td_frame;
411126474Sgrehan	/* align stack and alloc space for frame ptr and saved LR */
412126474Sgrehan        sp = ((uint32_t)ku->ku_stack.ss_sp + ku->ku_stack.ss_size
413126474Sgrehan		- 2*sizeof(u_int32_t)) & ~0x1f;
414126474Sgrehan	bzero(tf, sizeof(struct trapframe));
415126474Sgrehan
416126474Sgrehan	tf->fixreg[1] = (register_t)sp;
417126474Sgrehan        tf->fixreg[3] = (register_t)ku->ku_mailbox;
418126474Sgrehan        tf->srr0 = (register_t)ku->ku_func;
419126474Sgrehan        tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
420126474Sgrehan        td->td_pcb->pcb_flags = 0;
421126474Sgrehan
422126474Sgrehan        td->td_retval[0] = (register_t)ku->ku_func;
423126474Sgrehan        td->td_retval[1] = 0;
42499659Sbenno}
425