vm_machdep.c revision 137214
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 *//*-
42 * Copyright (c) 1982, 1986 The Regents of the University of California.
43 * Copyright (c) 1989, 1990 William Jolitz
44 * Copyright (c) 1994 John Dyson
45 * All rights reserved.
46 *
47 * This code is derived from software contributed to Berkeley by
48 * the Systems Programming Group of the University of Utah Computer
49 * Science Department, and William Jolitz.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
53 * are met:
54 * 1. Redistributions of source code must retain the above copyright
55 *    notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 *    notice, this list of conditions and the following disclaimer in the
58 *    documentation and/or other materials provided with the distribution.
59 * 3. All advertising materials mentioning features or use of this software
60 *    must display the following acknowledgement:
61 *	This product includes software developed by the University of
62 *	California, Berkeley and its contributors.
63 * 4. Neither the name of the University nor the names of its contributors
64 *    may be used to endorse or promote products derived from this software
65 *    without specific prior written permission.
66 *
67 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 * SUCH DAMAGE.
78 *
79 *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
80 *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
81 */
82
83#include <sys/cdefs.h>
84__FBSDID("$FreeBSD: head/sys/arm/arm/vm_machdep.c 137214 2004-11-04 18:59:02Z cognet $");
85
86#include <sys/param.h>
87#include <sys/systm.h>
88#include <sys/kernel.h>
89#include <sys/malloc.h>
90#include <sys/mbuf.h>
91#include <sys/proc.h>
92#include <sys/socketvar.h>
93#include <sys/sf_buf.h>
94#include <sys/user.h>
95#include <machine/cpu.h>
96#include <machine/pcb.h>
97#include <vm/vm.h>
98#include <vm/pmap.h>
99#include <sys/lock.h>
100#include <sys/mutex.h>
101
102#include <vm/vm.h>
103#include <vm/vm_extern.h>
104#include <vm/vm_kern.h>
105#include <vm/vm_page.h>
106#include <vm/vm_map.h>
107#include <vm/vm_param.h>
108
109#ifndef NSFBUFS
110#define NSFBUFS		(512 + maxusers * 16)
111#endif
112
113static void     sf_buf_init(void *arg);
114SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL)
115
116LIST_HEAD(sf_head, sf_buf);
117
118
119/*
120 * A hash table of active sendfile(2) buffers
121 */
122static struct sf_head *sf_buf_active;
123static u_long sf_buf_hashmask;
124
125#define SF_BUF_HASH(m)  (((m) - vm_page_array) & sf_buf_hashmask)
126
127static TAILQ_HEAD(, sf_buf) sf_buf_freelist;
128static u_int    sf_buf_alloc_want;
129
130/*
131 * A lock used to synchronize access to the hash table and free list
132 */
133static struct mtx sf_buf_lock;
134
135/*
136 * Finish a fork operation, with process p2 nearly set up.
137 * Copy and update the pcb, set up the stack so that the child
138 * ready to run and return to user mode.
139 */
140void
141cpu_fork(register struct thread *td1, register struct proc *p2,
142    struct thread *td2, int flags)
143{
144	struct pcb *pcb1, *pcb2;
145	struct trapframe *tf;
146	struct switchframe *sf;
147	struct mdproc *mdp2;
148	vm_offset_t uarea = td2->td_kstack + td2->td_kstack_pages * PAGE_SIZE
149	    - USPACE;
150
151	pcb1 = td1->td_pcb;
152	pcb2 = (struct pcb *)(td2->td_kstack + td2->td_kstack_pages * PAGE_SIZE) - 1;
153#ifdef __XSCALE__
154	pmap_use_minicache(td2->td_kstack, td2->td_kstack_pages * PAGE_SIZE);
155#endif
156	td2->td_pcb = pcb2;
157	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
158	mdp2 = &p2->p_md;
159	bcopy(&td1->td_proc->p_md, mdp2, sizeof(*mdp2));
160	pcb2->un_32.pcb32_und_sp = uarea + USPACE_UNDEF_STACK_TOP;
161	pcb2->un_32.pcb32_sp = uarea +
162	    USPACE_SVC_STACK_TOP - sizeof(*pcb2);
163	pmap_activate(td2);
164	td2->td_frame = tf =
165	    (struct trapframe *)pcb2->un_32.pcb32_sp - 1;
166	*tf = *td1->td_frame;
167	sf = (struct switchframe *)tf - 1;
168	sf->sf_r4 = (u_int)fork_return;
169	sf->sf_r5 = (u_int)td2;
170	sf->sf_pc = (u_int)fork_trampoline;
171	tf->tf_spsr &= ~PSR_C_bit;
172	tf->tf_r0 = 0;
173	tf->tf_r1 = 0;
174	pcb2->un_32.pcb32_sp = (u_int)sf;
175}
176
177void
178cpu_thread_swapin(struct thread *td)
179{
180}
181
182void
183cpu_thread_swapout(struct thread *td)
184{
185}
186
187/*
188 * Detatch mapped page and release resources back to the system.
189 */
190void
191sf_buf_free(struct sf_buf *sf)
192{
193	 mtx_lock(&sf_buf_lock);
194	 sf->ref_count--;
195	 if (sf->ref_count == 0) {
196		 TAILQ_INSERT_TAIL(&sf_buf_freelist, sf, free_entry);
197		 nsfbufsused--;
198		 if (sf_buf_alloc_want > 0)
199			 wakeup_one(&sf_buf_freelist);
200	 }
201	 mtx_unlock(&sf_buf_lock);
202}
203
204/*
205 *  * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
206 *   */
207static void
208sf_buf_init(void *arg)
209{
210	struct sf_buf *sf_bufs;
211	vm_offset_t sf_base;
212	int i;
213
214	nsfbufs = NSFBUFS;
215	TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
216
217	sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask);
218	TAILQ_INIT(&sf_buf_freelist);
219	sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE);
220	sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
221	    M_NOWAIT | M_ZERO);
222	for (i = 0; i < nsfbufs; i++) {
223		sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
224		TAILQ_INSERT_TAIL(&sf_buf_freelist, &sf_bufs[i], free_entry);
225	}
226	sf_buf_alloc_want = 0;
227	mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF);
228}
229
230/*
231 * Get an sf_buf from the freelist. Will block if none are available.
232 */
233struct sf_buf *
234sf_buf_alloc(struct vm_page *m, int pri)
235{
236	struct sf_head *hash_list;
237	struct sf_buf *sf;
238	int error;
239
240	hash_list = &sf_buf_active[SF_BUF_HASH(m)];
241	mtx_lock(&sf_buf_lock);
242	LIST_FOREACH(sf, hash_list, list_entry) {
243		if (sf->m == m) {
244			sf->ref_count++;
245			if (sf->ref_count == 1) {
246				TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
247				nsfbufsused++;
248				nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
249			}
250			goto done;
251		}
252	}
253	while ((sf = TAILQ_FIRST(&sf_buf_freelist)) == NULL) {
254		sf_buf_alloc_want++;
255		mbstat.sf_allocwait++;
256		error = msleep(&sf_buf_freelist, &sf_buf_lock, PVM | pri,
257		    "sfbufa", 0);
258		sf_buf_alloc_want--;
259
260
261		/*
262		 * If we got a signal, don't risk going back to sleep.
263		 */
264		if (error)
265			goto done;
266	}
267	TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
268	if (sf->m != NULL)
269		LIST_REMOVE(sf, list_entry);
270	LIST_INSERT_HEAD(hash_list, sf, list_entry);
271	sf->ref_count = 1;
272	sf->m = m;
273	nsfbufsused++;
274	nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
275	pmap_qenter(sf->kva, &sf->m, 1);
276done:
277	mtx_unlock(&sf_buf_lock);
278	return (sf);
279
280}
281
282/*
283 * Initialize machine state (pcb and trap frame) for a new thread about to
284 * upcall. Put enough state in the new thread's PCB to get it to go back
285 * userret(), where we can intercept it again to set the return (upcall)
286 * Address and stack, along with those from upcals that are from other sources
287 * such as those generated in thread_userret() itself.
288 */
289void
290cpu_set_upcall(struct thread *td, struct thread *td0)
291{
292	struct trapframe *tf;
293	struct switchframe *sf;
294
295	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
296	bcopy(td0->td_pcb, td->td_pcb, sizeof(struct pcb));
297	tf = td->td_frame;
298	sf = (struct switchframe *)tf - 1;
299	sf->sf_r4 = (u_int)fork_return;
300	sf->sf_r5 = (u_int)td;
301	sf->sf_pc = (u_int)fork_trampoline;
302	tf->tf_spsr &= ~PSR_C_bit;
303	tf->tf_r0 = 0;
304	td->td_pcb->un_32.pcb32_sp = (u_int)sf;
305	td->td_pcb->un_32.pcb32_und_sp = td->td_kstack + td->td_kstack_pages
306	    * PAGE_SIZE - USPACE + USPACE_UNDEF_STACK_TOP;
307}
308
309/*
310 * Set that machine state for performing an upcall that has to
311 * be done in thread_userret() so that those upcalls generated
312 * in thread_userret() itself can be done as well.
313 */
314void
315cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku)
316{
317	struct trapframe *tf = td->td_frame;
318
319	tf->tf_usr_sp = ((int)ku->ku_stack.ss_sp + ku->ku_stack.ss_size
320	    - sizeof(struct trapframe)) & ~7;
321	tf->tf_pc = (int)ku->ku_func;
322	tf->tf_r0 = (int)ku->ku_mailbox;
323	tf->tf_spsr = PSR_USR32_MODE;
324}
325
326void
327cpu_thread_exit(struct thread *td)
328{
329}
330
331void
332cpu_thread_setup(struct thread *td)
333{
334	td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_pages *
335	    PAGE_SIZE) - 1;
336	td->td_frame = (struct trapframe *)
337	    ((u_int)td->td_kstack + td->td_kstack_pages * PAGE_SIZE - USPACE +
338	     USPACE_SVC_STACK_TOP - sizeof(struct pcb)) - 1;
339#ifdef __XSCALE__
340	pmap_use_minicache(td->td_kstack, td->td_kstack_pages * PAGE_SIZE);
341#endif
342
343}
344void
345cpu_thread_clean(struct thread *td)
346{
347}
348
349/*
350 * Intercept the return address from a freshly forked process that has NOT
351 * been scheduled yet.
352 *
353 * This is needed to make kernel threads stay in kernel mode.
354 */
355void
356cpu_set_fork_handler(struct thread *td, void (*func)(void *), void *arg)
357{
358	struct switchframe *sf;
359	struct trapframe *tf;
360
361	tf = td->td_frame;
362	sf = (struct switchframe *)tf - 1;
363	sf->sf_r4 = (u_int)func;
364	sf->sf_r5 = (u_int)arg;
365	td->td_pcb->un_32.pcb32_sp = (u_int)sf;
366}
367
368/*
369 * Software interrupt handler for queued VM system processing.
370 */
371void
372swi_vm(void *dummy)
373{
374}
375
376void
377cpu_exit(struct thread *td)
378{
379}
380