vm_machdep.c revision 255273
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 255273 2013-09-05 23:00:24Z nwhitehorn $
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/ktr.h>
77#include <sys/lock.h>
78#include <sys/mutex.h>
79#include <sys/vnode.h>
80#include <sys/vmmeter.h>
81#include <sys/kernel.h>
82#include <sys/mbuf.h>
83#include <sys/sf_buf.h>
84#include <sys/sysctl.h>
85#include <sys/sysent.h>
86#include <sys/unistd.h>
87
88#include <machine/cpu.h>
89#include <machine/fpu.h>
90#include <machine/frame.h>
91#include <machine/md_var.h>
92#include <machine/pcb.h>
93
94#include <dev/ofw/openfirm.h>
95
96#include <vm/vm.h>
97#include <vm/vm_param.h>
98#include <vm/vm_kern.h>
99#include <vm/vm_page.h>
100#include <vm/vm_map.h>
101#include <vm/vm_extern.h>
102
103/*
104 * On systems without a direct mapped region (e.g. PPC64),
105 * we use the same code as the Book E implementation. Since
106 * we need to have runtime detection of this, define some machinery
107 * for sf_bufs in this case, and ignore it on systems with direct maps.
108 */
109
110#ifndef NSFBUFS
111#define NSFBUFS         (512 + maxusers * 16)
112#endif
113
114static void sf_buf_init(void *arg);
115SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL);
116
117LIST_HEAD(sf_head, sf_buf);
118
119/* A hash table of active sendfile(2) buffers */
120static struct sf_head *sf_buf_active;
121static u_long sf_buf_hashmask;
122
123#define SF_BUF_HASH(m)  (((m) - vm_page_array) & sf_buf_hashmask)
124
125static TAILQ_HEAD(, sf_buf) sf_buf_freelist;
126static u_int sf_buf_alloc_want;
127
128/*
129 * A lock used to synchronize access to the hash table and free list
130 */
131static struct mtx sf_buf_lock;
132
133#ifdef __powerpc64__
134extern uintptr_t tocbase;
135#endif
136
137
138/*
139 * Finish a fork operation, with process p2 nearly set up.
140 * Copy and update the pcb, set up the stack so that the child
141 * ready to run and return to user mode.
142 */
143void
144cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
145{
146	struct	trapframe *tf;
147	struct	callframe *cf;
148	struct	pcb *pcb;
149
150	KASSERT(td1 == curthread || td1 == &thread0,
151	    ("cpu_fork: p1 not curproc and not proc0"));
152	CTR3(KTR_PROC, "cpu_fork: called td1=%p p2=%p flags=%x",
153	    td1, p2, flags);
154
155	if ((flags & RFPROC) == 0)
156		return;
157
158	pcb = (struct pcb *)((td2->td_kstack +
159	    td2->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb)) & ~0x2fUL);
160	td2->td_pcb = pcb;
161
162	/* Copy the pcb */
163	bcopy(td1->td_pcb, pcb, sizeof(struct pcb));
164
165	/*
166	 * Create a fresh stack for the new process.
167	 * Copy the trap frame for the return to user mode as if from a
168	 * syscall.  This copies most of the user mode register values.
169	 */
170	tf = (struct trapframe *)pcb - 1;
171	bcopy(td1->td_frame, tf, sizeof(*tf));
172
173	/* Set up trap frame. */
174	tf->fixreg[FIRSTARG] = 0;
175	tf->fixreg[FIRSTARG + 1] = 0;
176	tf->cr &= ~0x10000000;
177
178	td2->td_frame = tf;
179
180	cf = (struct callframe *)tf - 1;
181	memset(cf, 0, sizeof(struct callframe));
182	#ifdef __powerpc64__
183	cf->cf_toc = tocbase;
184	#endif
185	cf->cf_func = (register_t)fork_return;
186	cf->cf_arg0 = (register_t)td2;
187	cf->cf_arg1 = (register_t)tf;
188
189	pcb->pcb_sp = (register_t)cf;
190	KASSERT(pcb->pcb_sp % 16 == 0, ("stack misaligned"));
191	#ifdef __powerpc64__
192	pcb->pcb_lr = ((register_t *)fork_trampoline)[0];
193	pcb->pcb_toc = ((register_t *)fork_trampoline)[1];
194	#else
195	pcb->pcb_lr = (register_t)fork_trampoline;
196	#endif
197	pcb->pcb_cpu.aim.usr_vsid = 0;
198
199	/* Setup to release spin count in fork_exit(). */
200	td2->td_md.md_spinlock_count = 1;
201	td2->td_md.md_saved_msr = PSL_KERNSET;
202
203	/*
204 	 * Now cpu_switch() can schedule the new process.
205	 */
206}
207
208/*
209 * Intercept the return address from a freshly forked process that has NOT
210 * been scheduled yet.
211 *
212 * This is needed to make kernel threads stay in kernel mode.
213 */
214void
215cpu_set_fork_handler(td, func, arg)
216	struct thread *td;
217	void (*func)(void *);
218	void *arg;
219{
220	struct	callframe *cf;
221
222	CTR4(KTR_PROC, "%s called with td=%p func=%p arg=%p",
223	    __func__, td, func, arg);
224
225	cf = (struct callframe *)td->td_pcb->pcb_sp;
226
227	cf->cf_func = (register_t)func;
228	cf->cf_arg0 = (register_t)arg;
229}
230
231void
232cpu_exit(td)
233	register struct thread *td;
234{
235}
236
237/*
238 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
239 */
240static void
241sf_buf_init(void *arg)
242{
243        struct sf_buf *sf_bufs;
244        vm_offset_t sf_base;
245        int i;
246
247	/* Don't bother on systems with a direct map */
248
249	if (hw_direct_map)
250		return;
251
252        nsfbufs = NSFBUFS;
253        TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
254
255        sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask);
256        TAILQ_INIT(&sf_buf_freelist);
257        sf_base = kva_alloc(nsfbufs * PAGE_SIZE);
258        sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, M_NOWAIT | M_ZERO);
259
260        for (i = 0; i < nsfbufs; i++) {
261                sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
262                TAILQ_INSERT_TAIL(&sf_buf_freelist, &sf_bufs[i], free_entry);
263        }
264        sf_buf_alloc_want = 0;
265        mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF);
266}
267
268/*
269 * Get an sf_buf from the freelist. Will block if none are available.
270 */
271struct sf_buf *
272sf_buf_alloc(struct vm_page *m, int flags)
273{
274        struct sf_head *hash_list;
275        struct sf_buf *sf;
276        int error;
277
278	if (hw_direct_map) {
279		/* Shortcut the direct mapped case */
280
281		return ((struct sf_buf *)m);
282	}
283
284        hash_list = &sf_buf_active[SF_BUF_HASH(m)];
285        mtx_lock(&sf_buf_lock);
286        LIST_FOREACH(sf, hash_list, list_entry) {
287                if (sf->m == m) {
288                        sf->ref_count++;
289                        if (sf->ref_count == 1) {
290                                TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
291                                nsfbufsused++;
292                                nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
293                        }
294                        goto done;
295                }
296        }
297
298        while ((sf = TAILQ_FIRST(&sf_buf_freelist)) == NULL) {
299                if (flags & SFB_NOWAIT)
300                        goto done;
301
302                sf_buf_alloc_want++;
303                SFSTAT_INC(sf_allocwait);
304                error = msleep(&sf_buf_freelist, &sf_buf_lock,
305                    (flags & SFB_CATCH) ? PCATCH | PVM : PVM, "sfbufa", 0);
306                sf_buf_alloc_want--;
307
308                /*
309                 * If we got a signal, don't risk going back to sleep.
310                 */
311                if (error)
312                        goto done;
313        }
314
315        TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
316        if (sf->m != NULL)
317                LIST_REMOVE(sf, list_entry);
318
319        LIST_INSERT_HEAD(hash_list, sf, list_entry);
320        sf->ref_count = 1;
321        sf->m = m;
322        nsfbufsused++;
323        nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
324        pmap_qenter(sf->kva, &sf->m, 1);
325done:
326        mtx_unlock(&sf_buf_lock);
327        return (sf);
328}
329
330/*
331 * Detatch mapped page and release resources back to the system.
332 *
333 * Remove a reference from the given sf_buf, adding it to the free
334 * list when its reference count reaches zero. A freed sf_buf still,
335 * however, retains its virtual-to-physical mapping until it is
336 * recycled or reactivated by sf_buf_alloc(9).
337 */
338void
339sf_buf_free(struct sf_buf *sf)
340{
341	if (hw_direct_map)
342		return;
343
344        mtx_lock(&sf_buf_lock);
345        sf->ref_count--;
346        if (sf->ref_count == 0) {
347                TAILQ_INSERT_TAIL(&sf_buf_freelist, sf, free_entry);
348                nsfbufsused--;
349
350                if (sf_buf_alloc_want > 0)
351                        wakeup(&sf_buf_freelist);
352        }
353        mtx_unlock(&sf_buf_lock);
354}
355
356/*
357 * Software interrupt handler for queued VM system processing.
358 */
359void
360swi_vm(void *dummy)
361{
362
363	if (busdma_swi_pending != 0)
364		busdma_swi();
365}
366
367/*
368 * Tell whether this address is in some physical memory region.
369 * Currently used by the kernel coredump code in order to avoid
370 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
371 * or other unpredictable behaviour.
372 */
373
374
375int
376is_physical_memory(addr)
377	vm_offset_t addr;
378{
379	/*
380	 * stuff other tests for known memory-mapped devices (PCI?)
381	 * here
382	 */
383
384	return 1;
385}
386
387/*
388 * CPU threading functions related to the VM layer. These could be used
389 * to map the SLB bits required for the kernel stack instead of forcing a
390 * fixed-size KVA.
391 */
392
393void
394cpu_thread_swapin(struct thread *td)
395{
396}
397
398void
399cpu_thread_swapout(struct thread *td)
400{
401}
402
403