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
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
39 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
40 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
44 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
45 * All rights reserved.
47 * Author: Chris G. Demetriou
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.
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.
59 * Carnegie Mellon requests users of this software to return to
61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
62 * School of Computer Science
63 * Carnegie Mellon University
64 * Pittsburgh PA 15213-3890
66 * any improvements or extensions that they make and grant Carnegie the
67 * rights to redistribute these changes.
70 #include <sys/param.h>
71 #include <sys/systm.h>
73 #include <sys/malloc.h>
78 #include <sys/mutex.h>
79 #include <sys/vnode.h>
80 #include <sys/vmmeter.h>
81 #include <sys/kernel.h>
83 #include <sys/sf_buf.h>
84 #include <sys/sysctl.h>
85 #include <sys/unistd.h>
87 #include <machine/clock.h>
88 #include <machine/cpu.h>
89 #include <machine/fpu.h>
90 #include <machine/frame.h>
91 #include <machine/md_var.h>
93 #include <dev/ofw/openfirm.h>
96 #include <vm/vm_param.h>
97 #include <vm/vm_kern.h>
98 #include <vm/vm_page.h>
99 #include <vm/vm_map.h>
100 #include <vm/vm_extern.h>
102 #include <sys/user.h>
104 static void sf_buf_init(void *arg);
105 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL)
108 * Expanded sf_freelist head. Really an SLIST_HEAD() in disguise, with the
109 * sf_freelist head with the sf_lock mutex.
112 SLIST_HEAD(, sf_buf) sf_head;
116 static u_int sf_buf_alloc_want;
119 * Finish a fork operation, with process p2 nearly set up.
120 * Copy and update the pcb, set up the stack so that the child
121 * ready to run and return to user mode.
124 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
127 struct trapframe *tf;
128 struct callframe *cf;
131 KASSERT(td1 == curthread || td1 == &thread0,
132 ("cpu_fork: p1 not curproc and not proc0"));
133 CTR3(KTR_PROC, "cpu_fork: called td1=%08x p2=%08x flags=%x", (u_int)td1, (u_int)p2, flags);
135 if ((flags & RFPROC) == 0)
140 pcb = (struct pcb *)((td2->td_kstack +
141 td2->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb)) & ~0x2fU);
145 bcopy(td1->td_pcb, pcb, sizeof(struct pcb));
148 * Create a fresh stack for the new process.
149 * Copy the trap frame for the return to user mode as if from a
150 * syscall. This copies most of the user mode register values.
152 tf = (struct trapframe *)pcb - 1;
153 bcopy(td1->td_frame, tf, sizeof(*tf));
155 /* Set up trap frame. */
156 tf->fixreg[FIRSTARG] = 0;
157 tf->fixreg[FIRSTARG + 1] = 0;
158 tf->cr &= ~0x10000000;
162 cf = (struct callframe *)tf - 1;
163 cf->cf_func = (register_t)fork_return;
164 cf->cf_arg0 = (register_t)td2;
165 cf->cf_arg1 = (register_t)tf;
167 pcb->pcb_sp = (register_t)cf;
168 pcb->pcb_lr = (register_t)fork_trampoline;
169 pcb->pcb_usr = kernel_pmap->pm_sr[USER_SR];
172 * Now cpu_switch() can schedule the new process.
177 * Intercept the return address from a freshly forked process that has NOT
178 * been scheduled yet.
180 * This is needed to make kernel threads stay in kernel mode.
183 cpu_set_fork_handler(td, func, arg)
185 void (*func)(void *);
188 struct callframe *cf;
190 CTR3(KTR_PROC, "cpu_set_fork_handler: called with td=%08x func=%08x arg=%08x",
191 (u_int)td, (u_int)func, (u_int)arg);
193 cf = (struct callframe *)td->td_pcb->pcb_sp;
195 cf->cf_func = (register_t)func;
196 cf->cf_arg0 = (register_t)arg;
200 * cpu_exit is called as the last action during exit.
201 * We release the address space of the process, block interrupts,
202 * and call switch_exit. switch_exit switches to proc0's PCB and stack,
203 * then jumps into the middle of cpu_switch, as if it were switching
208 register struct thread *td;
214 register struct thread *td;
218 /* Temporary helper */
220 cpu_throw(struct thread *old, struct thread *new)
223 cpu_switch(old, new);
224 panic("cpu_throw() didn't");
228 * Reset back to firmware.
237 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
240 sf_buf_init(void *arg)
242 struct sf_buf *sf_bufs;
246 mtx_init(&sf_freelist.sf_lock, "sf_bufs list lock", NULL, MTX_DEF);
247 SLIST_INIT(&sf_freelist.sf_head);
248 sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE);
249 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
251 for (i = 0; i < nsfbufs; i++) {
252 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
253 SLIST_INSERT_HEAD(&sf_freelist.sf_head, &sf_bufs[i], free_list);
255 sf_buf_alloc_want = 0;
259 * Get an sf_buf from the freelist. Will block if none are available.
262 sf_buf_alloc(struct vm_page *m)
267 mtx_lock(&sf_freelist.sf_lock);
268 while ((sf = SLIST_FIRST(&sf_freelist.sf_head)) == NULL) {
270 mbstat.sf_allocwait++;
271 error = msleep(&sf_freelist, &sf_freelist.sf_lock, PVM|PCATCH,
276 * If we got a signal, don't risk going back to sleep.
282 SLIST_REMOVE_HEAD(&sf_freelist.sf_head, free_list);
285 nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
286 pmap_qenter(sf->kva, &sf->m, 1);
288 mtx_unlock(&sf_freelist.sf_lock);
293 * Release resources back to the system.
296 sf_buf_free(struct sf_buf *sf)
299 pmap_qremove(sf->kva, 1);
300 mtx_lock(&sf_freelist.sf_lock);
301 SLIST_INSERT_HEAD(&sf_freelist.sf_head, sf, free_list);
303 if (sf_buf_alloc_want > 0)
304 wakeup_one(&sf_freelist);
305 mtx_unlock(&sf_freelist.sf_lock);
309 * Software interrupt handler for queued VM system processing.
314 #if 0 /* XXX: Don't have busdma stuff yet */
315 if (busdma_swi_pending != 0)
321 * Tell whether this address is in some physical memory region.
322 * Currently used by the kernel coredump code in order to avoid
323 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
324 * or other unpredictable behaviour.
329 is_physical_memory(addr)
333 * stuff other tests for known memory-mapped devices (PCI?)
344 cpu_thread_exit(struct thread *td)
349 cpu_thread_clean(struct thread *td)
354 cpu_thread_setup(struct thread *td)
358 pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
359 sizeof(struct pcb)) & ~0x2fU);
361 td->td_frame = (struct trapframe *)pcb - 1;
365 cpu_thread_swapin(struct thread *td)
370 cpu_thread_swapout(struct thread *td)
375 cpu_set_upcall(struct thread *td, struct thread *td0)
378 struct trapframe *tf;
379 struct callframe *cf;
383 /* Copy the upcall pcb */
384 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
386 /* Create a stack for the new thread */
388 bcopy(td0->td_frame, tf, sizeof(struct trapframe));
389 tf->fixreg[FIRSTARG] = 0;
390 tf->fixreg[FIRSTARG + 1] = 0;
391 tf->cr &= ~0x10000000;
393 /* Set registers for trampoline to user mode. */
394 cf = (struct callframe *)tf - 1;
395 cf->cf_func = (register_t)fork_return;
396 cf->cf_arg0 = (register_t)td;
397 cf->cf_arg1 = (register_t)tf;
399 pcb2->pcb_sp = (register_t)cf;
400 pcb2->pcb_lr = (register_t)fork_trampoline;
401 pcb2->pcb_usr = kernel_pmap->pm_sr[USER_SR];
405 cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku)
407 struct trapframe *tf;
411 /* align stack and alloc space for frame ptr and saved LR */
412 sp = ((uint32_t)ku->ku_stack.ss_sp + ku->ku_stack.ss_size
413 - 2*sizeof(u_int32_t)) & ~0x1f;
414 bzero(tf, sizeof(struct trapframe));
416 tf->fixreg[1] = (register_t)sp;
417 tf->fixreg[3] = (register_t)ku->ku_mailbox;
418 tf->srr0 = (register_t)ku->ku_func;
419 tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
420 td->td_pcb->pcb_flags = 0;
422 td->td_retval[0] = (register_t)ku->ku_func;
423 td->td_retval[1] = 0;