]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/sun4v/sun4v/vm_machdep.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / sun4v / sun4v / 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/sysent.h>
61 #include <sys/unistd.h>
62 #include <sys/vmmeter.h>
63
64 #include <dev/ofw/openfirm.h>
65
66 #include <vm/vm.h>
67 #include <vm/vm_extern.h>
68 #include <vm/pmap.h>
69 #include <vm/vm_kern.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_pageout.h>
73 #include <vm/vm_param.h>
74 #include <vm/uma.h>
75 #include <vm/uma_int.h>
76
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 #include <machine/wstate.h>
89 #include <machine/asm.h>
90
91 void
92 cpu_exit(struct thread *td)
93 {
94         struct proc *p;
95
96         p = td->td_proc;
97         p->p_md.md_sigtramp = NULL;
98         if (p->p_md.md_utrap != NULL) {
99                 utrap_free(p->p_md.md_utrap);
100                 p->p_md.md_utrap = NULL;
101         }
102 }
103
104 void
105 cpu_thread_exit(struct thread *td)
106 {
107 }
108
109 void
110 cpu_thread_clean(struct thread *td)
111 {
112 }
113
114 void
115 cpu_thread_alloc(struct thread *td)
116 {
117         struct pcb *pcb;
118
119         pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
120             sizeof(struct pcb)) & ~0x3fUL);
121         pcb->pcb_kstack = (uint64_t)(((char *)pcb) - (CCFSZ + SPOFF));
122         pcb->pcb_nsaved = 0;
123         td->td_frame = (struct trapframe *)pcb - 1;
124         pcb = (struct pcb *)TLB_PHYS_TO_DIRECT(vtophys((vm_offset_t)pcb));
125         KASSERT(pcb > (struct pcb *)VM_MIN_DIRECT_ADDRESS,("pcb is NULL"));
126         td->td_pcb = pcb;
127         
128 }
129
130 void
131 cpu_thread_free(struct thread *td)
132 {
133 }
134
135 void
136 cpu_thread_swapin(struct thread *td)
137 {
138 }
139
140 void
141 cpu_thread_swapout(struct thread *td)
142 {
143 }
144
145 void
146 cpu_set_syscall_retval(struct thread *td, int error)
147 {
148
149         switch (error) {
150         case 0:
151                 td->td_frame->tf_out[0] = td->td_retval[0];
152                 td->td_frame->tf_out[1] = td->td_retval[1];
153                 td->td_frame->tf_tstate &= ~TSTATE_XCC_C;
154                 break;
155
156         case ERESTART:
157                 /*
158                  * Undo the tpc advancement we have done on syscall
159                  * enter, we want to reexecute the system call.
160                  */
161                 td->td_frame->tf_tpc = td->td_pcb->pcb_tpc;
162                 td->td_frame->tf_tnpc -= 4;
163                 break;
164
165         case EJUSTRETURN:
166                 break;
167
168         default:
169                 if (td->td_proc->p_sysent->sv_errsize) {
170                         if (error >= td->td_proc->p_sysent->sv_errsize)
171                                 error = -1;     /* XXX */
172                         else
173                                 error = td->td_proc->p_sysent->sv_errtbl[error];
174                 }
175                 td->td_frame->tf_out[0] = error;
176                 td->td_frame->tf_tstate |= TSTATE_XCC_C;
177                 break;
178         }
179 }
180
181 void
182 cpu_set_upcall(struct thread *td, struct thread *td0)
183 {
184         struct trapframe *tf;
185         struct frame *fr;
186         struct pcb *pcb;
187
188         bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
189
190         pcb = td->td_pcb;
191
192         tf = td->td_frame;
193         fr = (struct frame *)tf - 1;
194         fr->fr_local[0] = (u_long)fork_return;
195         fr->fr_local[1] = (u_long)td;
196         fr->fr_local[2] = (u_long)tf;
197         pcb->pcb_pc = (u_long)fork_trampoline - 8;
198         pcb->pcb_sp = (u_long)fr - SPOFF;
199
200         /* Setup to release spin count in fork_exit(). */
201         td->td_md.md_spinlock_count = 1;
202         td->td_md.md_saved_pil = 0;
203 }
204
205 void
206 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
207         stack_t *stack)
208 {
209         struct trapframe *tf;
210         uint64_t sp;
211
212         if (td == curthread)
213                 flushw();
214         tf = td->td_frame;
215         sp = (uint64_t)stack->ss_sp + stack->ss_size;
216         tf->tf_out[0] = (uint64_t)arg;
217         tf->tf_out[6] = sp - SPOFF - sizeof(struct frame);
218         tf->tf_tpc = (uint64_t)entry;
219         tf->tf_tnpc = tf->tf_tpc + 4;
220
221         td->td_retval[0] = tf->tf_out[0];
222         td->td_retval[1] = tf->tf_out[1];
223 }
224
225 int
226 cpu_set_user_tls(struct thread *td, void *tls_base)
227 {
228
229         if (td == curthread)
230                 flushw();
231         td->td_frame->tf_global[7] = (uint64_t) tls_base;
232         return (0);
233 }
234
235 /*
236  * Finish a fork operation, with process p2 nearly set up.
237  * Copy and update the pcb, set up the stack so that the child
238  * ready to run and return to user mode.
239  */
240 void
241 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
242 {
243         struct trapframe *tf;
244         struct frame *fp;
245         struct pcb *pcb1;
246         struct pcb *pcb2, *pcb2orig;
247         vm_offset_t sp;
248         int error;
249         int i;
250
251         KASSERT(td1 == curthread || td1 == &thread0,
252             ("cpu_fork: p1 not curproc and not proc0"));
253
254         if ((flags & RFPROC) == 0)
255                 return;
256
257         p2->p_md.md_sigtramp = td1->td_proc->p_md.md_sigtramp;
258         p2->p_md.md_utrap = utrap_hold(td1->td_proc->p_md.md_utrap);
259
260         /* The pcb must be aligned on a 64-byte boundary. */
261         pcb1 = td1->td_pcb;
262
263         pcb2orig = (struct pcb *)((td2->td_kstack + td2->td_kstack_pages *
264             PAGE_SIZE - sizeof(struct pcb)) & ~0x3fUL);
265         pcb2 = (struct pcb *)TLB_PHYS_TO_DIRECT(vtophys((vm_offset_t)pcb2orig));
266
267         td2->td_pcb = pcb2;
268
269         /*
270          * Ensure that p1's pcb is up to date.
271          */
272         critical_enter();
273         if ((td1->td_frame->tf_fprs & FPRS_FEF) != 0)
274                 savefpctx(pcb1->pcb_ufp);
275         critical_exit();
276         /* Make sure the copied windows are spilled. */
277         flushw();
278         /* Copy the pcb (this will copy the windows saved in the pcb, too). */
279         bcopy(pcb1, pcb2, sizeof(*pcb1));
280
281         /*
282          * If we're creating a new user process and we're sharing the address
283          * space, the parent's top most frame must be saved in the pcb.  The
284          * child will pop the frame when it returns to user mode, and may
285          * overwrite it with its own data causing much suffering for the
286          * parent.  We check if its already in the pcb, and if not copy it
287          * in.  Its unlikely that the copyin will fail, but if so there's not
288          * much we can do.  The parent will likely crash soon anyway in that
289          * case.
290          */
291         if ((flags & RFMEM) != 0 && td1 != &thread0) {
292                 sp = td1->td_frame->tf_sp;
293                 for (i = 0; i < pcb1->pcb_nsaved; i++) {
294                         if (pcb1->pcb_rwsp[i] == sp)
295                                 break;
296                 }
297                 if (i == pcb1->pcb_nsaved) {
298                         error = copyin((caddr_t)sp + SPOFF, &pcb1->pcb_rw[i],
299                             sizeof(struct rwindow));
300                         if (error == 0) {
301                                 pcb1->pcb_rwsp[i] = sp;
302                                 pcb1->pcb_nsaved++;
303                         }
304                 }
305         }
306
307         /*
308          * Create a new fresh stack for the new process.
309          * Copy the trap frame for the return to user mode as if from a
310          * syscall.  This copies most of the user mode register values.
311          */
312         tf = (struct trapframe *)pcb2orig - 1;
313         bcopy(td1->td_frame, tf, sizeof(*tf));
314
315         tf->tf_out[0] = 0;                      /* Child returns zero */
316         tf->tf_out[1] = 0;
317         tf->tf_tstate &= ~TSTATE_XCC_C;         /* success */
318         tf->tf_fprs = 0;
319         tf->tf_wstate = WSTATE_U64;
320
321
322         td2->td_frame = tf;
323         fp = (struct frame *)tf - 1;
324         fp->fr_local[0] = (u_long)fork_return;
325         fp->fr_local[1] = (u_long)td2;
326         fp->fr_local[2] = (u_long)tf;
327         /* Terminate stack traces at this frame. */
328         fp->fr_pc = fp->fr_fp = 0;
329         pcb2->pcb_sp = (u_long)fp - SPOFF;
330         pcb2->pcb_pc = (u_long)fork_trampoline - 8;
331         pcb2->pcb_kstack = (uint64_t)(((char *)pcb2orig) - (CCFSZ + SPOFF));
332
333         /* Setup to release spin count in fork_exit(). */
334         td2->td_md.md_spinlock_count = 1;
335         td2->td_md.md_saved_pil = 0;
336
337         /*
338          * Now, cpu_switch() can schedule the new process.
339          */
340 }
341
342 void
343 cpu_reset(void)
344 {
345         static char bspec[64] = "";
346         phandle_t chosen;
347 #ifdef notyet
348         static struct {
349                 cell_t  name;
350                 cell_t  nargs;
351                 cell_t  nreturns;
352                 cell_t  bootspec;
353         } args = {
354                 (cell_t)"boot",
355                 1,
356                 0,
357                 (cell_t)bspec
358         };
359 #endif
360         if ((chosen = OF_finddevice("/chosen")) != 0) {
361                 if (OF_getprop(chosen, "bootpath", bspec, sizeof(bspec)) == -1)
362                         bspec[0] = '\0';
363                 bspec[sizeof(bspec) - 1] = '\0';
364         }
365         hv_mach_sir();
366 }
367
368 /*
369  * Intercept the return address from a freshly forked process that has NOT
370  * been scheduled yet.
371  *
372  * This is needed to make kernel threads stay in kernel mode.
373  */
374 void
375 cpu_set_fork_handler(struct thread *td, void (*func)(void *), void *arg)
376 {
377         struct frame *fp;
378         struct pcb *pcb;
379
380         pcb = td->td_pcb;
381         fp = (struct frame *)(pcb->pcb_sp + SPOFF);
382         fp->fr_local[0] = (u_long)func;
383         fp->fr_local[1] = (u_long)arg;
384 }
385
386 int
387 is_physical_memory(vm_paddr_t addr)
388 {
389         struct ofw_mem_region *mr;
390
391         for (mr = sparc64_memreg; mr < sparc64_memreg + sparc64_nmemreg; mr++)
392                 if (addr >= mr->mr_start && addr < mr->mr_start + mr->mr_size)
393                         return (1);
394         return (0);
395 }
396
397 /*
398  * Get an sf_buf from the freelist. Will block if none are available.
399  */
400 struct sf_buf *
401 sf_buf_alloc(struct vm_page *m, int flags)
402 {
403         return ((struct sf_buf *)m);
404 }
405
406 /*
407  * Release resources back to the system.
408  */
409 void
410 sf_buf_free(struct sf_buf *sf)
411 {
412 }
413
414 void
415 swi_vm(void *v)
416 {
417
418         /*
419          * Nothing to do here yet - busdma bounce buffers are not yet
420          * implemented.
421          */
422 }
423
424 void *
425 uma_small_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
426 {
427         static vm_pindex_t color;
428         vm_paddr_t pa;
429         vm_page_t m;
430         int pflags;
431         void *va;
432
433
434         *flags = UMA_SLAB_PRIV;
435
436         if ((wait & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT)
437                 pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED;
438         else
439                 pflags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED;
440
441         if (wait & M_ZERO)
442                 pflags |= VM_ALLOC_ZERO;
443
444         for (;;) {
445                 m = vm_page_alloc(NULL, color++, pflags | VM_ALLOC_NOOBJ);
446                 if (m == NULL) {
447                         if (wait & M_NOWAIT)
448                                 return (NULL);
449                         else
450                                 VM_WAIT;
451                 } else
452                         break;
453         }
454
455         pa = VM_PAGE_TO_PHYS(m);
456         va = (void *)TLB_PHYS_TO_DIRECT(pa);
457         if ((wait & M_ZERO) && ((m->flags & PG_ZERO) == 0))
458                 hwblkclr((void *)TLB_PHYS_TO_DIRECT(pa), PAGE_SIZE);
459         return (va);
460 }
461
462 void
463 uma_small_free(void *mem, int size, u_int8_t flags)
464 {
465         vm_page_t m;
466
467         m = PHYS_TO_VM_PAGE(TLB_DIRECT_TO_PHYS((vm_offset_t)mem));
468         m->wire_count--;
469         vm_page_free(m);
470         atomic_subtract_int(&cnt.v_wire_count, 1);
471 }