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