]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/sparc64/vm_machdep.c
MFV r337027:
[FreeBSD/FreeBSD.git] / sys / sparc64 / sparc64 / vm_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986 The Regents of the University of California.
5  * Copyright (c) 1989, 1990 William Jolitz
6  * Copyright (c) 1994 John Dyson
7  * Copyright (c) 2001 Jake Burkholder.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * the Systems Programming Group of the University of Utah Computer
12  * Science Department, and William Jolitz.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      from: @(#)vm_machdep.c  7.3 (Berkeley) 5/13/91
39  *      Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
40  *      from: FreeBSD: src/sys/i386/i386/vm_machdep.c,v 1.167 2001/07/12
41  */
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45
46 #include "opt_pmap.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/bio.h>
51 #include <sys/buf.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mbuf.h>
55 #include <sys/mutex.h>
56 #include <sys/proc.h>
57 #include <sys/sysent.h>
58 #include <sys/sched.h>
59 #include <sys/sf_buf.h>
60 #include <sys/sysctl.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/cache.h>
78 #include <machine/cpu.h>
79 #include <machine/fp.h>
80 #include <machine/frame.h>
81 #include <machine/fsr.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 PMAP_STATS_VAR(uma_nsmall_alloc);
90 PMAP_STATS_VAR(uma_nsmall_alloc_oc);
91 PMAP_STATS_VAR(uma_nsmall_free);
92
93 void
94 cpu_exit(struct thread *td)
95 {
96         struct proc *p;
97
98         p = td->td_proc;
99         p->p_md.md_sigtramp = NULL;
100         if (p->p_md.md_utrap != NULL) {
101                 utrap_free(p->p_md.md_utrap);
102                 p->p_md.md_utrap = NULL;
103         }
104 }
105
106 void
107 cpu_thread_exit(struct thread *td)
108 {
109
110 }
111
112 void
113 cpu_thread_clean(struct thread *td)
114 {
115
116 }
117
118 void
119 cpu_thread_alloc(struct thread *td)
120 {
121         struct pcb *pcb;
122
123         pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
124             sizeof(struct pcb)) & ~0x3fUL);
125         pcb->pcb_nsaved = 0;
126         td->td_frame = (struct trapframe *)pcb - 1;
127         td->td_pcb = pcb;
128 }
129
130 void
131 cpu_thread_free(struct thread *td)
132 {
133
134 }
135
136 void
137 cpu_thread_swapin(struct thread *td)
138 {
139
140 }
141
142 void
143 cpu_thread_swapout(struct thread *td)
144 {
145
146 }
147
148 void
149 cpu_set_syscall_retval(struct thread *td, int error)
150 {
151
152         switch (error) {
153         case 0:
154                 td->td_frame->tf_out[0] = td->td_retval[0];
155                 td->td_frame->tf_out[1] = td->td_retval[1];
156                 td->td_frame->tf_tstate &= ~TSTATE_XCC_C;
157                 break;
158
159         case ERESTART:
160                 /*
161                  * Undo the tpc advancement we have done on syscall
162                  * enter, we want to reexecute the system call.
163                  */
164                 td->td_frame->tf_tpc = td->td_pcb->pcb_tpc;
165                 td->td_frame->tf_tnpc -= 4;
166                 break;
167
168         case EJUSTRETURN:
169                 break;
170
171         default:
172                 td->td_frame->tf_out[0] = SV_ABI_ERRNO(td->td_proc, error);
173                 td->td_frame->tf_tstate |= TSTATE_XCC_C;
174                 break;
175         }
176 }
177
178 void
179 cpu_copy_thread(struct thread *td, struct thread *td0)
180 {
181         struct trapframe *tf;
182         struct frame *fr;
183         struct pcb *pcb;
184
185         bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
186
187         pcb = td->td_pcb;
188         tf = td->td_frame;
189         fr = (struct frame *)tf - 1;
190         fr->fr_local[0] = (u_long)fork_return;
191         fr->fr_local[1] = (u_long)td;
192         fr->fr_local[2] = (u_long)tf;
193         pcb->pcb_pc = (u_long)fork_trampoline - 8;
194         pcb->pcb_sp = (u_long)fr - SPOFF;
195
196         /* Setup to release the spin count in fork_exit(). */
197         td->td_md.md_spinlock_count = 1;
198         td->td_md.md_saved_pil = 0;
199 }
200
201 void
202 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
203     stack_t *stack)
204 {
205         struct trapframe *tf;
206         uint64_t sp;
207
208         if (td == curthread)
209                 flushw();
210         tf = td->td_frame;
211         sp = (uint64_t)stack->ss_sp + stack->ss_size;
212         tf->tf_out[0] = (uint64_t)arg;
213         tf->tf_out[6] = sp - SPOFF - sizeof(struct frame);
214         tf->tf_tpc = (uint64_t)entry;
215         tf->tf_tnpc = tf->tf_tpc + 4;
216
217         td->td_retval[0] = tf->tf_out[0];
218         td->td_retval[1] = tf->tf_out[1];
219 }
220
221 int
222 cpu_set_user_tls(struct thread *td, void *tls_base)
223 {
224
225         if (td == curthread)
226                 flushw();
227         td->td_frame->tf_global[7] = (uint64_t)tls_base;
228         return (0);
229 }
230
231 /*
232  * Finish a fork operation, with process p2 nearly set up.
233  * Copy and update the pcb, set up the stack so that the child
234  * ready to run and return to user mode.
235  */
236 void
237 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
238 {
239         struct trapframe *tf;
240         struct frame *fp;
241         struct pcb *pcb1;
242         struct pcb *pcb2;
243         vm_offset_t sp;
244         int error;
245         int i;
246
247         KASSERT(td1 == curthread || td1 == &thread0,
248             ("cpu_fork: p1 not curproc and not proc0"));
249
250         if ((flags & RFPROC) == 0)
251                 return;
252
253         p2->p_md.md_sigtramp = td1->td_proc->p_md.md_sigtramp;
254         p2->p_md.md_utrap = utrap_hold(td1->td_proc->p_md.md_utrap);
255
256         /* The pcb must be aligned on a 64-byte boundary. */
257         pcb1 = td1->td_pcb;
258         pcb2 = (struct pcb *)((td2->td_kstack + td2->td_kstack_pages *
259             PAGE_SIZE - sizeof(struct pcb)) & ~0x3fUL);
260         td2->td_pcb = pcb2;
261
262         /*
263          * Ensure that p1's pcb is up to date.
264          */
265         critical_enter();
266         if ((td1->td_frame->tf_fprs & FPRS_FEF) != 0)
267                 savefpctx(pcb1->pcb_ufp);
268         critical_exit();
269         /* Make sure the copied windows are spilled. */
270         flushw();
271         /* Copy the pcb (this will copy the windows saved in the pcb, too). */
272         bcopy(pcb1, pcb2, sizeof(*pcb1));
273
274         /*
275          * If we're creating a new user process and we're sharing the address
276          * space, the parent's top most frame must be saved in the pcb.  The
277          * child will pop the frame when it returns to user mode, and may
278          * overwrite it with its own data causing much suffering for the
279          * parent.  We check if its already in the pcb, and if not copy it
280          * in.  Its unlikely that the copyin will fail, but if so there's not
281          * much we can do.  The parent will likely crash soon anyway in that
282          * case.
283          */
284         if ((flags & RFMEM) != 0 && td1 != &thread0) {
285                 sp = td1->td_frame->tf_sp;
286                 for (i = 0; i < pcb1->pcb_nsaved; i++) {
287                         if (pcb1->pcb_rwsp[i] == sp)
288                                 break;
289                 }
290                 if (i == pcb1->pcb_nsaved) {
291                         error = copyin((caddr_t)sp + SPOFF, &pcb1->pcb_rw[i],
292                             sizeof(struct rwindow));
293                         if (error == 0) {
294                                 pcb1->pcb_rwsp[i] = sp;
295                                 pcb1->pcb_nsaved++;
296                         }
297                 }
298         }
299
300         /*
301          * Create a new fresh stack for the new process.
302          * Copy the trap frame for the return to user mode as if from a
303          * syscall.  This copies most of the user mode register values.
304          */
305         tf = (struct trapframe *)pcb2 - 1;
306         bcopy(td1->td_frame, tf, sizeof(*tf));
307
308         tf->tf_out[0] = 0;                      /* Child returns zero */
309         tf->tf_out[1] = 0;
310         tf->tf_tstate &= ~TSTATE_XCC_C;         /* success */
311         tf->tf_fprs = 0;
312
313         td2->td_frame = tf;
314         fp = (struct frame *)tf - 1;
315         fp->fr_local[0] = (u_long)fork_return;
316         fp->fr_local[1] = (u_long)td2;
317         fp->fr_local[2] = (u_long)tf;
318         /* Terminate stack traces at this frame. */
319         fp->fr_pc = fp->fr_fp = 0;
320         pcb2->pcb_sp = (u_long)fp - SPOFF;
321         pcb2->pcb_pc = (u_long)fork_trampoline - 8;
322
323         /* Setup to release the spin count in fork_exit(). */
324         td2->td_md.md_spinlock_count = 1;
325         td2->td_md.md_saved_pil = 0;
326
327         /*
328          * Now, cpu_switch() can schedule the new process.
329          */
330 }
331
332 void
333 cpu_reset(void)
334 {
335         static char bspec[64] = "";
336         phandle_t chosen;
337         static struct {
338                 cell_t  name;
339                 cell_t  nargs;
340                 cell_t  nreturns;
341                 cell_t  bootspec;
342         } args = {
343                 (cell_t)"boot",
344                 1,
345                 0,
346                 (cell_t)bspec
347         };
348
349         if ((chosen = OF_finddevice("/chosen")) != -1) {
350                 if (OF_getprop(chosen, "bootpath", bspec, sizeof(bspec)) == -1)
351                         bspec[0] = '\0';
352                 bspec[sizeof(bspec) - 1] = '\0';
353         }
354
355         cpu_shutdown(&args);
356 }
357
358 /*
359  * Intercept the return address from a freshly forked process that has NOT
360  * been scheduled yet.
361  *
362  * This is needed to make kernel threads stay in kernel mode.
363  */
364 void
365 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
366 {
367         struct frame *fp;
368         struct pcb *pcb;
369
370         pcb = td->td_pcb;
371         fp = (struct frame *)(pcb->pcb_sp + SPOFF);
372         fp->fr_local[0] = (u_long)func;
373         fp->fr_local[1] = (u_long)arg;
374 }
375
376 int
377 is_physical_memory(vm_paddr_t addr)
378 {
379         struct ofw_mem_region *mr;
380
381         for (mr = sparc64_memreg; mr < sparc64_memreg + sparc64_nmemreg; mr++)
382                 if (addr >= mr->mr_start && addr < mr->mr_start + mr->mr_size)
383                         return (1);
384         return (0);
385 }
386
387 void
388 swi_vm(void *v)
389 {
390
391         /* Nothing to do here - busdma bounce buffers are not implemented. */
392 }
393
394 void *
395 uma_small_alloc(uma_zone_t zone, vm_size_t bytes, int domain, u_int8_t *flags,
396     int wait)
397 {
398         vm_paddr_t pa;
399         vm_page_t m;
400         void *va;
401
402         PMAP_STATS_INC(uma_nsmall_alloc);
403
404         *flags = UMA_SLAB_PRIV;
405
406         m = vm_page_alloc_domain(NULL, 0, domain,
407             malloc2vm_flags(wait) | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ);
408         if (m == NULL)
409                 return (NULL);
410
411         pa = VM_PAGE_TO_PHYS(m);
412         if (dcache_color_ignore == 0 && m->md.color != DCACHE_COLOR(pa)) {
413                 KASSERT(m->md.colors[0] == 0 && m->md.colors[1] == 0,
414                     ("uma_small_alloc: free page %p still has mappings!", m));
415                 PMAP_STATS_INC(uma_nsmall_alloc_oc);
416                 m->md.color = DCACHE_COLOR(pa);
417                 dcache_page_inval(pa);
418         }
419         va = (void *)TLB_PHYS_TO_DIRECT(pa);
420         if ((wait & M_ZERO) && (m->flags & PG_ZERO) == 0)
421                 cpu_block_zero(va, PAGE_SIZE);
422         return (va);
423 }
424
425 void
426 uma_small_free(void *mem, vm_size_t size, u_int8_t flags)
427 {
428         vm_page_t m;
429
430         PMAP_STATS_INC(uma_nsmall_free);
431         m = PHYS_TO_VM_PAGE(TLB_DIRECT_TO_PHYS((vm_offset_t)mem));
432         vm_page_unwire_noq(m);
433         vm_page_free(m);
434 }
435
436 void
437 sf_buf_map(struct sf_buf *sf, int flags)
438 {
439
440         pmap_qenter(sf->kva, &sf->m, 1);
441 }
442
443 int
444 sf_buf_unmap(struct sf_buf *sf)
445 {
446
447         pmap_qremove(sf->kva, 1);
448         return (1);
449 }