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 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
37 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
38 * from: FreeBSD: src/sys/i386/i386/vm_machdep.c,v 1.167 2001/07/12
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
53 #include <sys/mutex.h>
55 #include <sys/sysent.h>
56 #include <sys/sched.h>
57 #include <sys/sf_buf.h>
58 #include <sys/sysctl.h>
59 #include <sys/unistd.h>
60 #include <sys/vmmeter.h>
62 #include <dev/ofw/openfirm.h>
65 #include <vm/vm_extern.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_pageout.h>
71 #include <vm/vm_param.h>
73 #include <vm/uma_int.h>
75 #include <machine/cache.h>
76 #include <machine/cpu.h>
77 #include <machine/fp.h>
78 #include <machine/frame.h>
79 #include <machine/fsr.h>
80 #include <machine/md_var.h>
81 #include <machine/ofw_machdep.h>
82 #include <machine/ofw_mem.h>
83 #include <machine/pcb.h>
84 #include <machine/tlb.h>
85 #include <machine/tstate.h>
87 PMAP_STATS_VAR(uma_nsmall_alloc);
88 PMAP_STATS_VAR(uma_nsmall_alloc_oc);
89 PMAP_STATS_VAR(uma_nsmall_free);
92 cpu_exit(struct thread *td)
97 p->p_md.md_sigtramp = NULL;
98 if (p->p_md.md_utrap != NULL) {
99 utrap_free(p->p_md.md_utrap);
100 p->p_md.md_utrap = NULL;
105 cpu_thread_exit(struct thread *td)
111 cpu_thread_clean(struct thread *td)
117 cpu_thread_alloc(struct thread *td)
121 pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
122 sizeof(struct pcb)) & ~0x3fUL);
124 td->td_frame = (struct trapframe *)pcb - 1;
129 cpu_thread_free(struct thread *td)
135 cpu_thread_swapin(struct thread *td)
141 cpu_thread_swapout(struct thread *td)
147 cpu_set_syscall_retval(struct thread *td, int error)
152 td->td_frame->tf_out[0] = td->td_retval[0];
153 td->td_frame->tf_out[1] = td->td_retval[1];
154 td->td_frame->tf_tstate &= ~TSTATE_XCC_C;
159 * Undo the tpc advancement we have done on syscall
160 * enter, we want to reexecute the system call.
162 td->td_frame->tf_tpc = td->td_pcb->pcb_tpc;
163 td->td_frame->tf_tnpc -= 4;
170 if (td->td_proc->p_sysent->sv_errsize) {
171 if (error >= td->td_proc->p_sysent->sv_errsize)
172 error = -1; /* XXX */
174 error = td->td_proc->p_sysent->sv_errtbl[error];
176 td->td_frame->tf_out[0] = error;
177 td->td_frame->tf_tstate |= TSTATE_XCC_C;
183 cpu_set_upcall(struct thread *td, struct thread *td0)
185 struct trapframe *tf;
189 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
193 fr = (struct frame *)tf - 1;
194 fr->fr_local[0] = (u_long)fork_return;
195 fr->fr_local[1] = (u_long)td;
196 fr->fr_local[2] = (u_long)tf;
197 pcb->pcb_pc = (u_long)fork_trampoline - 8;
198 pcb->pcb_sp = (u_long)fr - SPOFF;
200 /* Setup to release the spin count in fork_exit(). */
201 td->td_md.md_spinlock_count = 1;
202 td->td_md.md_saved_pil = 0;
206 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
209 struct trapframe *tf;
215 sp = (uint64_t)stack->ss_sp + stack->ss_size;
216 tf->tf_out[0] = (uint64_t)arg;
217 tf->tf_out[6] = sp - SPOFF - sizeof(struct frame);
218 tf->tf_tpc = (uint64_t)entry;
219 tf->tf_tnpc = tf->tf_tpc + 4;
221 td->td_retval[0] = tf->tf_out[0];
222 td->td_retval[1] = tf->tf_out[1];
226 cpu_set_user_tls(struct thread *td, void *tls_base)
231 td->td_frame->tf_global[7] = (uint64_t)tls_base;
236 * Finish a fork operation, with process p2 nearly set up.
237 * Copy and update the pcb, set up the stack so that the child
238 * ready to run and return to user mode.
241 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
243 struct trapframe *tf;
251 KASSERT(td1 == curthread || td1 == &thread0,
252 ("cpu_fork: p1 not curproc and not proc0"));
254 if ((flags & RFPROC) == 0)
257 p2->p_md.md_sigtramp = td1->td_proc->p_md.md_sigtramp;
258 p2->p_md.md_utrap = utrap_hold(td1->td_proc->p_md.md_utrap);
260 /* The pcb must be aligned on a 64-byte boundary. */
262 pcb2 = (struct pcb *)((td2->td_kstack + td2->td_kstack_pages *
263 PAGE_SIZE - sizeof(struct pcb)) & ~0x3fUL);
267 * Ensure that p1's pcb is up to date.
270 if ((td1->td_frame->tf_fprs & FPRS_FEF) != 0)
271 savefpctx(pcb1->pcb_ufp);
273 /* Make sure the copied windows are spilled. */
275 /* Copy the pcb (this will copy the windows saved in the pcb, too). */
276 bcopy(pcb1, pcb2, sizeof(*pcb1));
279 * If we're creating a new user process and we're sharing the address
280 * space, the parent's top most frame must be saved in the pcb. The
281 * child will pop the frame when it returns to user mode, and may
282 * overwrite it with its own data causing much suffering for the
283 * parent. We check if its already in the pcb, and if not copy it
284 * in. Its unlikely that the copyin will fail, but if so there's not
285 * much we can do. The parent will likely crash soon anyway in that
288 if ((flags & RFMEM) != 0 && td1 != &thread0) {
289 sp = td1->td_frame->tf_sp;
290 for (i = 0; i < pcb1->pcb_nsaved; i++) {
291 if (pcb1->pcb_rwsp[i] == sp)
294 if (i == pcb1->pcb_nsaved) {
295 error = copyin((caddr_t)sp + SPOFF, &pcb1->pcb_rw[i],
296 sizeof(struct rwindow));
298 pcb1->pcb_rwsp[i] = sp;
305 * Create a new fresh stack for the new process.
306 * Copy the trap frame for the return to user mode as if from a
307 * syscall. This copies most of the user mode register values.
309 tf = (struct trapframe *)pcb2 - 1;
310 bcopy(td1->td_frame, tf, sizeof(*tf));
312 tf->tf_out[0] = 0; /* Child returns zero */
314 tf->tf_tstate &= ~TSTATE_XCC_C; /* success */
318 fp = (struct frame *)tf - 1;
319 fp->fr_local[0] = (u_long)fork_return;
320 fp->fr_local[1] = (u_long)td2;
321 fp->fr_local[2] = (u_long)tf;
322 /* Terminate stack traces at this frame. */
323 fp->fr_pc = fp->fr_fp = 0;
324 pcb2->pcb_sp = (u_long)fp - SPOFF;
325 pcb2->pcb_pc = (u_long)fork_trampoline - 8;
327 /* Setup to release the spin count in fork_exit(). */
328 td2->td_md.md_spinlock_count = 1;
329 td2->td_md.md_saved_pil = 0;
332 * Now, cpu_switch() can schedule the new process.
339 static char bspec[64] = "";
353 if ((chosen = OF_finddevice("/chosen")) != -1) {
354 if (OF_getprop(chosen, "bootpath", bspec, sizeof(bspec)) == -1)
356 bspec[sizeof(bspec) - 1] = '\0';
363 * Intercept the return address from a freshly forked process that has NOT
364 * been scheduled yet.
366 * This is needed to make kernel threads stay in kernel mode.
369 cpu_set_fork_handler(struct thread *td, void (*func)(void *), void *arg)
375 fp = (struct frame *)(pcb->pcb_sp + SPOFF);
376 fp->fr_local[0] = (u_long)func;
377 fp->fr_local[1] = (u_long)arg;
381 is_physical_memory(vm_paddr_t addr)
383 struct ofw_mem_region *mr;
385 for (mr = sparc64_memreg; mr < sparc64_memreg + sparc64_nmemreg; mr++)
386 if (addr >= mr->mr_start && addr < mr->mr_start + mr->mr_size)
395 /* Nothing to do here - busdma bounce buffers are not implemented. */
399 uma_small_alloc(uma_zone_t zone, vm_size_t bytes, u_int8_t *flags, int wait)
406 PMAP_STATS_INC(uma_nsmall_alloc);
408 *flags = UMA_SLAB_PRIV;
409 pflags = malloc2vm_flags(wait) | VM_ALLOC_WIRED;
412 m = vm_page_alloc(NULL, 0, pflags | VM_ALLOC_NOOBJ);
422 pa = VM_PAGE_TO_PHYS(m);
423 if (dcache_color_ignore == 0 && m->md.color != DCACHE_COLOR(pa)) {
424 KASSERT(m->md.colors[0] == 0 && m->md.colors[1] == 0,
425 ("uma_small_alloc: free page %p still has mappings!", m));
426 PMAP_STATS_INC(uma_nsmall_alloc_oc);
427 m->md.color = DCACHE_COLOR(pa);
428 dcache_page_inval(pa);
430 va = (void *)TLB_PHYS_TO_DIRECT(pa);
431 if ((wait & M_ZERO) && (m->flags & PG_ZERO) == 0)
432 cpu_block_zero(va, PAGE_SIZE);
437 uma_small_free(void *mem, vm_size_t size, u_int8_t flags)
441 PMAP_STATS_INC(uma_nsmall_free);
442 m = PHYS_TO_VM_PAGE(TLB_DIRECT_TO_PHYS((vm_offset_t)mem));
445 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
449 sf_buf_map(struct sf_buf *sf, int flags)
452 pmap_qenter(sf->kva, &sf->m, 1);
456 sf_buf_unmap(struct sf_buf *sf)
459 pmap_qremove(sf->kva, 1);