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 * Copyright (c) 2001 Jake Burkholder.
6 * All rights reserved.
7 *
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.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
27 *
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
38 * SUCH DAMAGE.
39 *
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
43 * $FreeBSD: releng/5.2/sys/sparc64/sparc64/vm_machdep.c 122821 2003-11-16 23:40:06Z alc $
44 */
45
46 #include "opt_kstack_pages.h"
47 #include "opt_pmap.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/proc.h>
53 #include <sys/bio.h>
54 #include <sys/buf.h>
55 #include <sys/kernel.h>
56 #include <sys/linker_set.h>
57 #include <sys/mbuf.h>
58 #include <sys/mutex.h>
59 #include <sys/sf_buf.h>
60 #include <sys/sysctl.h>
61 #include <sys/unistd.h>
62 #include <sys/user.h>
63 #include <sys/vmmeter.h>
64
65 #include <dev/ofw/openfirm.h>
66
67 #include <vm/vm.h>
68 #include <vm/vm_extern.h>
69 #include <vm/pmap.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_pageout.h>
74 #include <vm/vm_param.h>
75 #include <vm/uma.h>
76 #include <vm/uma_int.h>
77
78 #include <machine/cache.h>
79 #include <machine/bus.h>
80 #include <machine/cpu.h>
81 #include <machine/fp.h>
82 #include <machine/fsr.h>
83 #include <machine/frame.h>
84 #include <machine/md_var.h>
85 #include <machine/ofw_machdep.h>
86 #include <machine/ofw_mem.h>
87 #include <machine/tlb.h>
88 #include <machine/tstate.h>
89
90 static void sf_buf_init(void *arg);
91 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL)
92
93 /*
94 * Expanded sf_freelist head. Really an SLIST_HEAD() in disguise, with the
95 * sf_freelist head with the sf_lock mutex.
96 */
97 static struct {
98 SLIST_HEAD(, sf_buf) sf_head;
99 struct mtx sf_lock;
100 } sf_freelist;
101
102 static u_int sf_buf_alloc_want;
103
104 PMAP_STATS_VAR(uma_nsmall_alloc);
105 PMAP_STATS_VAR(uma_nsmall_alloc_oc);
106 PMAP_STATS_VAR(uma_nsmall_free);
107
108 void
109 cpu_exit(struct thread *td)
110 {
111 struct md_utrap *ut;
112 struct proc *p;
113
114 p = td->td_proc;
115 p->p_md.md_sigtramp = NULL;
116 if ((ut = p->p_md.md_utrap) != NULL) {
117 ut->ut_refcnt--;
118 if (ut->ut_refcnt == 0)
119 free(ut, M_SUBPROC);
120 p->p_md.md_utrap = NULL;
121 }
122 }
123
124 void
125 cpu_sched_exit(struct thread *td)
126 {
127 struct vmspace *vm;
128 struct pcpu *pc;
129 struct proc *p;
130
131 mtx_assert(&sched_lock, MA_OWNED);
132
133 p = td->td_proc;
134 vm = p->p_vmspace;
135 if (vm->vm_refcnt > 1)
136 return;
137 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
138 if (pc->pc_vmspace == vm) {
139 vm->vm_pmap.pm_active &= ~pc->pc_cpumask;
140 vm->vm_pmap.pm_context[pc->pc_cpuid] = -1;
141 pc->pc_vmspace = NULL;
142 }
143 }
144 }
145
146 void
147 cpu_thread_exit(struct thread *td)
148 {
149 }
150
151 void
152 cpu_thread_clean(struct thread *td)
153 {
154 }
155
156 void
157 cpu_thread_setup(struct thread *td)
158 {
159 struct pcb *pcb;
160
161 pcb = (struct pcb *)((td->td_kstack + KSTACK_PAGES * PAGE_SIZE -
162 sizeof(struct pcb)) & ~0x3fUL);
163 td->td_frame = (struct trapframe *)pcb - 1;
164 td->td_pcb = pcb;
165 }
166
167 void
168 cpu_thread_swapin(struct thread *td)
169 {
170 }
171
172 void
173 cpu_thread_swapout(struct thread *td)
174 {
175 }
176
177 void
178 cpu_set_upcall(struct thread *td, struct thread *td0)
179 {
180 struct trapframe *tf;
181 struct frame *fr;
182 struct pcb *pcb;
183
184 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
185
186 pcb = td->td_pcb;
187 tf = td->td_frame;
188 fr = (struct frame *)tf - 1;
189 fr->fr_local[0] = (u_long)fork_return;
190 fr->fr_local[1] = (u_long)td;
191 fr->fr_local[2] = (u_long)tf;
192 pcb->pcb_pc = (u_long)fork_trampoline - 8;
193 pcb->pcb_sp = (u_long)fr - SPOFF;
194 }
195
196 void
197 cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku)
198 {
199 struct trapframe *tf;
200 uint64_t sp;
201
202 tf = td->td_frame;
203 sp = (uint64_t)ku->ku_stack.ss_sp + ku->ku_stack.ss_size;
204 tf->tf_out[0] = (uint64_t)ku->ku_mailbox;
205 tf->tf_out[6] = sp - SPOFF - sizeof(struct frame);
206 tf->tf_tpc = (uint64_t)ku->ku_func;
207 tf->tf_tnpc = tf->tf_tpc + 4;
208
209 td->td_retval[0] = tf->tf_out[0];
210 td->td_retval[1] = tf->tf_out[1];
211 }
212
213 /*
214 * Finish a fork operation, with process p2 nearly set up.
215 * Copy and update the pcb, set up the stack so that the child
216 * ready to run and return to user mode.
217 */
218 void
219 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
220 {
221 struct md_utrap *ut;
222 struct trapframe *tf;
223 struct frame *fp;
224 struct pcb *pcb1;
225 struct pcb *pcb2;
226 vm_offset_t sp;
227 int error;
228 int i;
229
230 KASSERT(td1 == curthread || td1 == &thread0,
231 ("cpu_fork: p1 not curproc and not proc0"));
232
233 if ((flags & RFPROC) == 0)
234 return;
235
236 p2->p_md.md_sigtramp = td1->td_proc->p_md.md_sigtramp;
237 if ((ut = td1->td_proc->p_md.md_utrap) != NULL)
238 ut->ut_refcnt++;
239 p2->p_md.md_utrap = ut;
240
241 /* The pcb must be aligned on a 64-byte boundary. */
242 pcb1 = td1->td_pcb;
243 pcb2 = (struct pcb *)((td2->td_kstack + KSTACK_PAGES * PAGE_SIZE -
244 sizeof(struct pcb)) & ~0x3fUL);
245 td2->td_pcb = pcb2;
246
247 /*
248 * Ensure that p1's pcb is up to date.
249 */
250 critical_enter();
251 if ((td1->td_frame->tf_fprs & FPRS_FEF) != 0)
252 savefpctx(pcb1->pcb_ufp);
253 critical_exit();
254 /* Make sure the copied windows are spilled. */
255 flushw();
256 /* Copy the pcb (this will copy the windows saved in the pcb, too). */
257 bcopy(pcb1, pcb2, sizeof(*pcb1));
258
259 /*
260 * If we're creating a new user process and we're sharing the address
261 * space, the parent's top most frame must be saved in the pcb. The
262 * child will pop the frame when it returns to user mode, and may
263 * overwrite it with its own data causing much suffering for the
264 * parent. We check if its already in the pcb, and if not copy it
265 * in. Its unlikely that the copyin will fail, but if so there's not
266 * much we can do. The parent will likely crash soon anyway in that
267 * case.
268 */
269 if ((flags & RFMEM) != 0 && td1 != &thread0) {
270 sp = td1->td_frame->tf_sp;
271 for (i = 0; i < pcb1->pcb_nsaved; i++) {
272 if (pcb1->pcb_rwsp[i] == sp)
273 break;
274 }
275 if (i == pcb1->pcb_nsaved) {
276 error = copyin((caddr_t)sp + SPOFF, &pcb1->pcb_rw[i],
277 sizeof(struct rwindow));
278 if (error == 0) {
279 pcb1->pcb_rwsp[i] = sp;
280 pcb1->pcb_nsaved++;
281 }
282 }
283 }
284
285 /*
286 * Create a new fresh stack for the new process.
287 * Copy the trap frame for the return to user mode as if from a
288 * syscall. This copies most of the user mode register values.
289 */
290 tf = (struct trapframe *)pcb2 - 1;
291 bcopy(td1->td_frame, tf, sizeof(*tf));
292
293 tf->tf_out[0] = 0; /* Child returns zero */
294 tf->tf_out[1] = 0;
295 tf->tf_tstate &= ~TSTATE_XCC_C; /* success */
296 tf->tf_fprs = 0;
297
298 td2->td_frame = tf;
299 fp = (struct frame *)tf - 1;
300 fp->fr_local[0] = (u_long)fork_return;
301 fp->fr_local[1] = (u_long)td2;
302 fp->fr_local[2] = (u_long)tf;
303 pcb2->pcb_sp = (u_long)fp - SPOFF;
304 pcb2->pcb_pc = (u_long)fork_trampoline - 8;
305
306 /*
307 * Now, cpu_switch() can schedule the new process.
308 */
309 }
310
311 void
312 cpu_reset(void)
313 {
314 static char bspec[64] = "";
315 phandle_t chosen;
316 static struct {
317 cell_t name;
318 cell_t nargs;
319 cell_t nreturns;
320 cell_t bootspec;
321 } args = {
322 (cell_t)"boot",
323 1,
324 0,
325 (cell_t)bspec
326 };
327 if ((chosen = OF_finddevice("/chosen")) != 0) {
328 if (OF_getprop(chosen, "bootpath", bspec, sizeof(bspec)) == -1)
329 bspec[0] = '\0';
330 bspec[sizeof(bspec) - 1] = '\0';
331 }
332
333 openfirmware_exit(&args);
334 }
335
336 /*
337 * Intercept the return address from a freshly forked process that has NOT
338 * been scheduled yet.
339 *
340 * This is needed to make kernel threads stay in kernel mode.
341 */
342 void
343 cpu_set_fork_handler(struct thread *td, void (*func)(void *), void *arg)
344 {
345 struct frame *fp;
346 struct pcb *pcb;
347
348 pcb = td->td_pcb;
349 fp = (struct frame *)(pcb->pcb_sp + SPOFF);
350 fp->fr_local[0] = (u_long)func;
351 fp->fr_local[1] = (u_long)arg;
352 }
353
354 int
355 is_physical_memory(vm_paddr_t addr)
356 {
357 struct ofw_mem_region *mr;
358
359 for (mr = sparc64_memreg; mr < sparc64_memreg + sparc64_nmemreg; mr++)
360 if (addr >= mr->mr_start && addr < mr->mr_start + mr->mr_size)
361 return (1);
362 return (0);
363 }
364
365 /*
366 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
367 */
368 static void
369 sf_buf_init(void *arg)
370 {
371 struct sf_buf *sf_bufs;
372 vm_offset_t sf_base;
373 int i;
374
375 mtx_init(&sf_freelist.sf_lock, "sf_bufs list lock", NULL, MTX_DEF);
376 SLIST_INIT(&sf_freelist.sf_head);
377 sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE);
378 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
379 M_NOWAIT | M_ZERO);
380 for (i = 0; i < nsfbufs; i++) {
381 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
382 SLIST_INSERT_HEAD(&sf_freelist.sf_head, &sf_bufs[i], free_list);
383 }
384 sf_buf_alloc_want = 0;
385 }
386
387 /*
388 * Get an sf_buf from the freelist. Will block if none are available.
389 */
390 struct sf_buf *
391 sf_buf_alloc(struct vm_page *m)
392 {
393 struct sf_buf *sf;
394 int error;
395
396 mtx_lock(&sf_freelist.sf_lock);
397 while ((sf = SLIST_FIRST(&sf_freelist.sf_head)) == NULL) {
398 sf_buf_alloc_want++;
399 error = msleep(&sf_freelist, &sf_freelist.sf_lock, PVM|PCATCH,
400 "sfbufa", 0);
401 sf_buf_alloc_want--;
402
403 /*
404 * If we got a signal, don't risk going back to sleep.
405 */
406 if (error)
407 break;
408 }
409 if (sf != NULL) {
410 SLIST_REMOVE_HEAD(&sf_freelist.sf_head, free_list);
411 sf->m = m;
412 pmap_qenter(sf->kva, &sf->m, 1);
413 }
414 mtx_unlock(&sf_freelist.sf_lock);
415 return (sf);
416 }
417
418 /*
419 * Detatch mapped page and release resources back to the system.
420 */
421 void
422 sf_buf_free(void *addr, void *args)
423 {
424 struct sf_buf *sf;
425 struct vm_page *m;
426
427 sf = args;
428 pmap_qremove((vm_offset_t)addr, 1);
429 m = sf->m;
430 vm_page_lock_queues();
431 vm_page_unwire(m, 0);
432 /*
433 * Check for the object going away on us. This can
434 * happen since we don't hold a reference to it.
435 * If so, we're responsible for freeing the page.
436 */
437 if (m->wire_count == 0 && m->object == NULL)
438 vm_page_free(m);
439 vm_page_unlock_queues();
440 sf->m = NULL;
441 mtx_lock(&sf_freelist.sf_lock);
442 SLIST_INSERT_HEAD(&sf_freelist.sf_head, sf, free_list);
443 if (sf_buf_alloc_want > 0)
444 wakeup_one(&sf_freelist);
445 mtx_unlock(&sf_freelist.sf_lock);
446 }
447
448 void
449 swi_vm(void *v)
450 {
451
452 /*
453 * Nothing to do here yet - busdma bounce buffers are not yet
454 * implemented.
455 */
456 }
457
458 void *
459 uma_small_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
460 {
461 static vm_pindex_t color;
462 vm_paddr_t pa;
463 vm_page_t m;
464 int pflags;
465 void *va;
466
467 PMAP_STATS_INC(uma_nsmall_alloc);
468
469 *flags = UMA_SLAB_PRIV;
470
471 if ((wait & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT)
472 pflags = VM_ALLOC_INTERRUPT;
473 else
474 pflags = VM_ALLOC_SYSTEM;
475
476 if (wait & M_ZERO)
477 pflags |= VM_ALLOC_ZERO;
478
479 for (;;) {
480 m = vm_page_alloc(NULL, color++, pflags | VM_ALLOC_NOOBJ);
481 if (m == NULL) {
482 if (wait & M_NOWAIT)
483 return (NULL);
484 else
485 VM_WAIT;
486 } else
487 break;
488 }
489
490 pa = VM_PAGE_TO_PHYS(m);
491 if (m->md.color != DCACHE_COLOR(pa)) {
492 KASSERT(m->md.colors[0] == 0 && m->md.colors[1] == 0,
493 ("uma_small_alloc: free page still has mappings!"));
494 PMAP_STATS_INC(uma_nsmall_alloc_oc);
495 m->md.color = DCACHE_COLOR(pa);
496 dcache_page_inval(pa);
497 }
498 va = (void *)TLB_PHYS_TO_DIRECT(pa);
499 if ((wait & M_ZERO) && (m->flags & PG_ZERO) == 0)
500 bzero(va, PAGE_SIZE);
501 return (va);
502 }
503
504 void
505 uma_small_free(void *mem, int size, u_int8_t flags)
506 {
507 vm_page_t m;
508
509 PMAP_STATS_INC(uma_nsmall_free);
510 m = PHYS_TO_VM_PAGE(TLB_DIRECT_TO_PHYS((vm_offset_t)mem));
511 vm_page_lock_queues();
512 vm_page_free(m);
513 vm_page_unlock_queues();
514 }
Cache object: 98c91abbf0acef37f188fd1bd95aa791
|