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