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 * Copyright (c) 2001 Jake Burkholder.
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department, and William Jolitz.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
41 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
42 * from: FreeBSD: src/sys/i386/i386/vm_machdep.c,v 1.167 2001/07/12
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
50 #include <sys/param.h>
51 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/linker_set.h>
56 #include <sys/malloc.h>
58 #include <sys/mutex.h>
60 #include <sys/sf_buf.h>
61 #include <sys/sched.h>
62 #include <sys/sysctl.h>
63 #include <sys/unistd.h>
64 #include <sys/vmmeter.h>
66 #include <dev/ofw/openfirm.h>
69 #include <vm/vm_extern.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_pageout.h>
75 #include <vm/vm_param.h>
77 #include <vm/uma_int.h>
79 #include <machine/cache.h>
80 #include <machine/cpu.h>
81 #include <machine/fp.h>
82 #include <machine/frame.h>
83 #include <machine/fsr.h>
84 #include <machine/md_var.h>
85 #include <machine/ofw_machdep.h>
86 #include <machine/ofw_mem.h>
87 #include <machine/pcb.h>
88 #include <machine/tlb.h>
89 #include <machine/tstate.h>
92 #define NSFBUFS (512 + maxusers * 16)
95 static void sf_buf_init(void *arg);
96 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL);
99 * Expanded sf_freelist head. Really an SLIST_HEAD() in disguise, with the
100 * sf_freelist head with the sf_lock mutex.
103 SLIST_HEAD(, sf_buf) sf_head;
107 static u_int sf_buf_alloc_want;
109 PMAP_STATS_VAR(uma_nsmall_alloc);
110 PMAP_STATS_VAR(uma_nsmall_alloc_oc);
111 PMAP_STATS_VAR(uma_nsmall_free);
114 cpu_exit(struct thread *td)
119 p->p_md.md_sigtramp = NULL;
120 if (p->p_md.md_utrap != NULL) {
121 utrap_free(p->p_md.md_utrap);
122 p->p_md.md_utrap = NULL;
127 cpu_thread_exit(struct thread *td)
133 cpu_thread_clean(struct thread *td)
139 cpu_thread_alloc(struct thread *td)
143 pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
144 sizeof(struct pcb)) & ~0x3fUL);
146 td->td_frame = (struct trapframe *)pcb - 1;
151 cpu_thread_free(struct thread *td)
157 cpu_thread_swapin(struct thread *td)
163 cpu_thread_swapout(struct thread *td)
169 cpu_set_upcall(struct thread *td, struct thread *td0)
171 struct trapframe *tf;
175 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
179 fr = (struct frame *)tf - 1;
180 fr->fr_local[0] = (u_long)fork_return;
181 fr->fr_local[1] = (u_long)td;
182 fr->fr_local[2] = (u_long)tf;
183 pcb->pcb_pc = (u_long)fork_trampoline - 8;
184 pcb->pcb_sp = (u_long)fr - SPOFF;
186 /* Setup to release the spin count in fork_exit(). */
187 td->td_md.md_spinlock_count = 1;
188 td->td_md.md_saved_pil = 0;
192 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
195 struct trapframe *tf;
201 sp = (uint64_t)stack->ss_sp + stack->ss_size;
202 tf->tf_out[0] = (uint64_t)arg;
203 tf->tf_out[6] = sp - SPOFF - sizeof(struct frame);
204 tf->tf_tpc = (uint64_t)entry;
205 tf->tf_tnpc = tf->tf_tpc + 4;
207 td->td_retval[0] = tf->tf_out[0];
208 td->td_retval[1] = tf->tf_out[1];
212 cpu_set_user_tls(struct thread *td, void *tls_base)
217 td->td_frame->tf_global[7] = (uint64_t) tls_base;
222 * Finish a fork operation, with process p2 nearly set up.
223 * Copy and update the pcb, set up the stack so that the child
224 * ready to run and return to user mode.
227 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
229 struct trapframe *tf;
237 KASSERT(td1 == curthread || td1 == &thread0,
238 ("cpu_fork: p1 not curproc and not proc0"));
240 if ((flags & RFPROC) == 0)
243 p2->p_md.md_sigtramp = td1->td_proc->p_md.md_sigtramp;
244 p2->p_md.md_utrap = utrap_hold(td1->td_proc->p_md.md_utrap);
246 /* The pcb must be aligned on a 64-byte boundary. */
248 pcb2 = (struct pcb *)((td2->td_kstack + td2->td_kstack_pages *
249 PAGE_SIZE - sizeof(struct pcb)) & ~0x3fUL);
253 * Ensure that p1's pcb is up to date.
256 if ((td1->td_frame->tf_fprs & FPRS_FEF) != 0)
257 savefpctx(pcb1->pcb_ufp);
259 /* Make sure the copied windows are spilled. */
261 /* Copy the pcb (this will copy the windows saved in the pcb, too). */
262 bcopy(pcb1, pcb2, sizeof(*pcb1));
265 * If we're creating a new user process and we're sharing the address
266 * space, the parent's top most frame must be saved in the pcb. The
267 * child will pop the frame when it returns to user mode, and may
268 * overwrite it with its own data causing much suffering for the
269 * parent. We check if its already in the pcb, and if not copy it
270 * in. Its unlikely that the copyin will fail, but if so there's not
271 * much we can do. The parent will likely crash soon anyway in that
274 if ((flags & RFMEM) != 0 && td1 != &thread0) {
275 sp = td1->td_frame->tf_sp;
276 for (i = 0; i < pcb1->pcb_nsaved; i++) {
277 if (pcb1->pcb_rwsp[i] == sp)
280 if (i == pcb1->pcb_nsaved) {
281 error = copyin((caddr_t)sp + SPOFF, &pcb1->pcb_rw[i],
282 sizeof(struct rwindow));
284 pcb1->pcb_rwsp[i] = sp;
291 * Create a new fresh stack for the new process.
292 * Copy the trap frame for the return to user mode as if from a
293 * syscall. This copies most of the user mode register values.
295 tf = (struct trapframe *)pcb2 - 1;
296 bcopy(td1->td_frame, tf, sizeof(*tf));
298 tf->tf_out[0] = 0; /* Child returns zero */
300 tf->tf_tstate &= ~TSTATE_XCC_C; /* success */
304 fp = (struct frame *)tf - 1;
305 fp->fr_local[0] = (u_long)fork_return;
306 fp->fr_local[1] = (u_long)td2;
307 fp->fr_local[2] = (u_long)tf;
308 /* Terminate stack traces at this frame. */
309 fp->fr_pc = fp->fr_fp = 0;
310 pcb2->pcb_sp = (u_long)fp - SPOFF;
311 pcb2->pcb_pc = (u_long)fork_trampoline - 8;
313 /* Setup to release the spin count in fork_exit(). */
314 td2->td_md.md_spinlock_count = 1;
315 td2->td_md.md_saved_pil = 0;
318 * Now, cpu_switch() can schedule the new process.
325 static char bspec[64] = "";
339 if ((chosen = OF_finddevice("/chosen")) != 0) {
340 if (OF_getprop(chosen, "bootpath", bspec, sizeof(bspec)) == -1)
342 bspec[sizeof(bspec) - 1] = '\0';
349 * Intercept the return address from a freshly forked process that has NOT
350 * been scheduled yet.
352 * This is needed to make kernel threads stay in kernel mode.
355 cpu_set_fork_handler(struct thread *td, void (*func)(void *), void *arg)
361 fp = (struct frame *)(pcb->pcb_sp + SPOFF);
362 fp->fr_local[0] = (u_long)func;
363 fp->fr_local[1] = (u_long)arg;
367 is_physical_memory(vm_paddr_t addr)
369 struct ofw_mem_region *mr;
371 for (mr = sparc64_memreg; mr < sparc64_memreg + sparc64_nmemreg; mr++)
372 if (addr >= mr->mr_start && addr < mr->mr_start + mr->mr_size)
378 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
381 sf_buf_init(void *arg)
383 struct sf_buf *sf_bufs;
388 TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
390 mtx_init(&sf_freelist.sf_lock, "sf_bufs list lock", NULL, MTX_DEF);
391 SLIST_INIT(&sf_freelist.sf_head);
392 sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE);
393 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
395 for (i = 0; i < nsfbufs; i++) {
396 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
397 SLIST_INSERT_HEAD(&sf_freelist.sf_head, &sf_bufs[i], free_list);
399 sf_buf_alloc_want = 0;
403 * Get an sf_buf from the freelist. Will block if none are available.
406 sf_buf_alloc(struct vm_page *m, int flags)
411 mtx_lock(&sf_freelist.sf_lock);
412 while ((sf = SLIST_FIRST(&sf_freelist.sf_head)) == NULL) {
413 if (flags & SFB_NOWAIT)
416 mbstat.sf_allocwait++;
417 error = msleep(&sf_freelist, &sf_freelist.sf_lock,
418 (flags & SFB_CATCH) ? PCATCH | PVM : PVM, "sfbufa", 0);
422 * If we got a signal, don't risk going back to sleep.
428 SLIST_REMOVE_HEAD(&sf_freelist.sf_head, free_list);
431 nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
432 pmap_qenter(sf->kva, &sf->m, 1);
434 mtx_unlock(&sf_freelist.sf_lock);
439 * Release resources back to the system.
442 sf_buf_free(struct sf_buf *sf)
445 pmap_qremove(sf->kva, 1);
446 mtx_lock(&sf_freelist.sf_lock);
447 SLIST_INSERT_HEAD(&sf_freelist.sf_head, sf, free_list);
449 if (sf_buf_alloc_want > 0)
450 wakeup_one(&sf_freelist);
451 mtx_unlock(&sf_freelist.sf_lock);
459 * Nothing to do here yet - busdma bounce buffers are not yet
465 uma_small_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
467 static vm_pindex_t color;
473 PMAP_STATS_INC(uma_nsmall_alloc);
475 *flags = UMA_SLAB_PRIV;
477 if ((wait & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT)
478 pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED;
480 pflags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED;
483 pflags |= VM_ALLOC_ZERO;
486 m = vm_page_alloc(NULL, color++, pflags | VM_ALLOC_NOOBJ);
496 pa = VM_PAGE_TO_PHYS(m);
497 if (m->md.color != DCACHE_COLOR(pa)) {
498 KASSERT(m->md.colors[0] == 0 && m->md.colors[1] == 0,
499 ("uma_small_alloc: free page still has mappings!"));
500 PMAP_STATS_INC(uma_nsmall_alloc_oc);
501 m->md.color = DCACHE_COLOR(pa);
502 dcache_page_inval(pa);
504 va = (void *)TLB_PHYS_TO_DIRECT(pa);
505 if ((wait & M_ZERO) && (m->flags & PG_ZERO) == 0)
506 bzero(va, PAGE_SIZE);
511 uma_small_free(void *mem, int size, u_int8_t flags)
515 PMAP_STATS_INC(uma_nsmall_free);
516 m = PHYS_TO_VM_PAGE(TLB_DIRECT_TO_PHYS((vm_offset_t)mem));
519 atomic_subtract_int(&cnt.v_wire_count, 1);