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