]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/sparc64/sparc64/vm_machdep.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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  * 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$
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_alloc(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_free(struct thread *td)
148 {
149 }
150  
151 void
152 cpu_thread_swapin(struct thread *td)
153 {
154 }
155
156 void
157 cpu_thread_swapout(struct thread *td)
158 {
159 }
160
161 void
162 cpu_set_upcall(struct thread *td, struct thread *td0)
163 {
164         struct trapframe *tf;
165         struct frame *fr;
166         struct pcb *pcb;
167
168         bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
169
170         pcb = td->td_pcb;
171         tf = td->td_frame;
172         fr = (struct frame *)tf - 1;
173         fr->fr_local[0] = (u_long)fork_return;
174         fr->fr_local[1] = (u_long)td;
175         fr->fr_local[2] = (u_long)tf;
176         pcb->pcb_pc = (u_long)fork_trampoline - 8;
177         pcb->pcb_sp = (u_long)fr - SPOFF;
178
179         /* Setup to release the spin count in fork_exit(). */
180         td->td_md.md_spinlock_count = 1;
181         td->td_md.md_saved_pil = 0;
182 }
183
184 void
185 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
186         stack_t *stack)
187 {
188         struct trapframe *tf;
189         uint64_t sp;
190
191         if (td == curthread)
192                 flushw();
193         tf = td->td_frame;
194         sp = (uint64_t)stack->ss_sp + stack->ss_size;
195         tf->tf_out[0] = (uint64_t)arg;
196         tf->tf_out[6] = sp - SPOFF - sizeof(struct frame);
197         tf->tf_tpc = (uint64_t)entry;
198         tf->tf_tnpc = tf->tf_tpc + 4;
199
200         td->td_retval[0] = tf->tf_out[0];
201         td->td_retval[1] = tf->tf_out[1];
202 }
203
204 int
205 cpu_set_user_tls(struct thread *td, void *tls_base)
206 {
207
208         if (td == curthread)
209                 flushw();
210         td->td_frame->tf_global[7] = (uint64_t) tls_base;
211         return (0);
212 }
213
214 /*
215  * Finish a fork operation, with process p2 nearly set up.
216  * Copy and update the pcb, set up the stack so that the child
217  * ready to run and return to user mode.
218  */
219 void
220 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
221 {
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         p2->p_md.md_utrap = utrap_hold(td1->td_proc->p_md.md_utrap);
238
239         /* The pcb must be aligned on a 64-byte boundary. */
240         pcb1 = td1->td_pcb;
241         pcb2 = (struct pcb *)((td2->td_kstack + td2->td_kstack_pages *
242             PAGE_SIZE - sizeof(struct pcb)) & ~0x3fUL);
243         td2->td_pcb = pcb2;
244
245         /*
246          * Ensure that p1's pcb is up to date.
247          */
248         critical_enter();
249         if ((td1->td_frame->tf_fprs & FPRS_FEF) != 0)
250                 savefpctx(pcb1->pcb_ufp);
251         critical_exit();
252         /* Make sure the copied windows are spilled. */
253         flushw();
254         /* Copy the pcb (this will copy the windows saved in the pcb, too). */
255         bcopy(pcb1, pcb2, sizeof(*pcb1));
256
257         /*
258          * If we're creating a new user process and we're sharing the address
259          * space, the parent's top most frame must be saved in the pcb.  The
260          * child will pop the frame when it returns to user mode, and may
261          * overwrite it with its own data causing much suffering for the
262          * parent.  We check if its already in the pcb, and if not copy it
263          * in.  Its unlikely that the copyin will fail, but if so there's not
264          * much we can do.  The parent will likely crash soon anyway in that
265          * case.
266          */
267         if ((flags & RFMEM) != 0 && td1 != &thread0) {
268                 sp = td1->td_frame->tf_sp;
269                 for (i = 0; i < pcb1->pcb_nsaved; i++) {
270                         if (pcb1->pcb_rwsp[i] == sp)
271                                 break;
272                 }
273                 if (i == pcb1->pcb_nsaved) {
274                         error = copyin((caddr_t)sp + SPOFF, &pcb1->pcb_rw[i],
275                             sizeof(struct rwindow));
276                         if (error == 0) {
277                                 pcb1->pcb_rwsp[i] = sp;
278                                 pcb1->pcb_nsaved++;
279                         }
280                 }
281         }
282
283         /*
284          * Create a new fresh stack for the new process.
285          * Copy the trap frame for the return to user mode as if from a
286          * syscall.  This copies most of the user mode register values.
287          */
288         tf = (struct trapframe *)pcb2 - 1;
289         bcopy(td1->td_frame, tf, sizeof(*tf));
290
291         tf->tf_out[0] = 0;                      /* Child returns zero */
292         tf->tf_out[1] = 0;
293         tf->tf_tstate &= ~TSTATE_XCC_C;         /* success */
294         tf->tf_fprs = 0;
295
296         td2->td_frame = tf;
297         fp = (struct frame *)tf - 1;
298         fp->fr_local[0] = (u_long)fork_return;
299         fp->fr_local[1] = (u_long)td2;
300         fp->fr_local[2] = (u_long)tf;
301         /* Terminate stack traces at this frame. */
302         fp->fr_pc = fp->fr_fp = 0;
303         pcb2->pcb_sp = (u_long)fp - SPOFF;
304         pcb2->pcb_pc = (u_long)fork_trampoline - 8;
305
306         /* Setup to release the spin count in fork_exit(). */
307         td2->td_md.md_spinlock_count = 1;
308         td2->td_md.md_saved_pil = 0;
309
310         /*
311          * Now, cpu_switch() can schedule the new process.
312          */
313 }
314
315 void
316 cpu_reset(void)
317 {
318         static char bspec[64] = "";
319         phandle_t chosen;
320         static struct {
321                 cell_t  name;
322                 cell_t  nargs;
323                 cell_t  nreturns;
324                 cell_t  bootspec;
325         } args = {
326                 (cell_t)"boot",
327                 1,
328                 0,
329                 (cell_t)bspec
330         };
331         if ((chosen = OF_finddevice("/chosen")) != 0) {
332                 if (OF_getprop(chosen, "bootpath", bspec, sizeof(bspec)) == -1)
333                         bspec[0] = '\0';
334                 bspec[sizeof(bspec) - 1] = '\0';
335         }
336
337         openfirmware_exit(&args);
338 }
339
340 /*
341  * Intercept the return address from a freshly forked process that has NOT
342  * been scheduled yet.
343  *
344  * This is needed to make kernel threads stay in kernel mode.
345  */
346 void
347 cpu_set_fork_handler(struct thread *td, void (*func)(void *), void *arg)
348 {
349         struct frame *fp;
350         struct pcb *pcb;
351
352         pcb = td->td_pcb;
353         fp = (struct frame *)(pcb->pcb_sp + SPOFF);
354         fp->fr_local[0] = (u_long)func;
355         fp->fr_local[1] = (u_long)arg;
356 }
357
358 int
359 is_physical_memory(vm_paddr_t addr)
360 {
361         struct ofw_mem_region *mr;
362
363         for (mr = sparc64_memreg; mr < sparc64_memreg + sparc64_nmemreg; mr++)
364                 if (addr >= mr->mr_start && addr < mr->mr_start + mr->mr_size)
365                         return (1);
366         return (0);
367 }
368
369 /*
370  * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
371  */
372 static void
373 sf_buf_init(void *arg)
374 {
375         struct sf_buf *sf_bufs;
376         vm_offset_t sf_base;
377         int i;
378
379         nsfbufs = NSFBUFS;
380         TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
381
382         mtx_init(&sf_freelist.sf_lock, "sf_bufs list lock", NULL, MTX_DEF);
383         SLIST_INIT(&sf_freelist.sf_head);
384         sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE);
385         sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
386             M_NOWAIT | M_ZERO);
387         for (i = 0; i < nsfbufs; i++) {
388                 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
389                 SLIST_INSERT_HEAD(&sf_freelist.sf_head, &sf_bufs[i], free_list);
390         }
391         sf_buf_alloc_want = 0;
392 }
393
394 /*
395  * Get an sf_buf from the freelist. Will block if none are available.
396  */
397 struct sf_buf *
398 sf_buf_alloc(struct vm_page *m, int flags)
399 {
400         struct sf_buf *sf;
401         int error;
402
403         mtx_lock(&sf_freelist.sf_lock);
404         while ((sf = SLIST_FIRST(&sf_freelist.sf_head)) == NULL) {
405                 if (flags & SFB_NOWAIT)
406                         break;
407                 sf_buf_alloc_want++;
408                 mbstat.sf_allocwait++;
409                 error = msleep(&sf_freelist, &sf_freelist.sf_lock,
410                     (flags & SFB_CATCH) ? PCATCH | PVM : PVM, "sfbufa", 0);
411                 sf_buf_alloc_want--;
412
413                 /*
414                  * If we got a signal, don't risk going back to sleep. 
415                  */
416                 if (error)
417                         break;
418         }
419         if (sf != NULL) {
420                 SLIST_REMOVE_HEAD(&sf_freelist.sf_head, free_list);
421                 sf->m = m;
422                 nsfbufsused++;
423                 nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
424                 pmap_qenter(sf->kva, &sf->m, 1);
425         }
426         mtx_unlock(&sf_freelist.sf_lock);
427         return (sf);
428 }
429
430 /*
431  * Release resources back to the system.
432  */
433 void
434 sf_buf_free(struct sf_buf *sf)
435 {
436
437         pmap_qremove(sf->kva, 1);
438         mtx_lock(&sf_freelist.sf_lock);
439         SLIST_INSERT_HEAD(&sf_freelist.sf_head, sf, free_list);
440         nsfbufsused--;
441         if (sf_buf_alloc_want > 0)
442                 wakeup_one(&sf_freelist);
443         mtx_unlock(&sf_freelist.sf_lock);
444 }
445
446 void
447 swi_vm(void *v)
448 {
449
450         /*
451          * Nothing to do here yet - busdma bounce buffers are not yet
452          * implemented.
453          */
454 }
455
456 void *
457 uma_small_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
458 {
459         static vm_pindex_t color;
460         vm_paddr_t pa;
461         vm_page_t m;
462         int pflags;
463         void *va;
464
465         PMAP_STATS_INC(uma_nsmall_alloc);
466
467         *flags = UMA_SLAB_PRIV;
468
469         if ((wait & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT)
470                 pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED;
471         else
472                 pflags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED;
473
474         if (wait & M_ZERO)
475                 pflags |= VM_ALLOC_ZERO;
476
477         for (;;) {
478                 m = vm_page_alloc(NULL, color++, pflags | VM_ALLOC_NOOBJ);
479                 if (m == NULL) {
480                         if (wait & M_NOWAIT)
481                                 return (NULL);
482                         else
483                                 VM_WAIT;
484                 } else
485                         break;
486         }
487
488         pa = VM_PAGE_TO_PHYS(m);
489         if (m->md.color != DCACHE_COLOR(pa)) {
490                 KASSERT(m->md.colors[0] == 0 && m->md.colors[1] == 0,
491                     ("uma_small_alloc: free page still has mappings!"));
492                 PMAP_STATS_INC(uma_nsmall_alloc_oc);
493                 m->md.color = DCACHE_COLOR(pa);
494                 dcache_page_inval(pa);
495         }
496         va = (void *)TLB_PHYS_TO_DIRECT(pa);
497         if ((wait & M_ZERO) && (m->flags & PG_ZERO) == 0)
498                 bzero(va, PAGE_SIZE);
499         return (va);
500 }
501
502 void
503 uma_small_free(void *mem, int size, u_int8_t flags)
504 {
505         vm_page_t m;
506
507         PMAP_STATS_INC(uma_nsmall_free);
508         m = PHYS_TO_VM_PAGE(TLB_DIRECT_TO_PHYS((vm_offset_t)mem));
509         m->wire_count--;
510         vm_page_free(m);
511         atomic_subtract_int(&cnt.v_wire_count, 1);
512 }