2 * SPDX-License-Identifier: BSD-3-Clause
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
9 * This code is derived from software contributed to Berkeley by
10 * the Systems Programming Group of the University of Utah Computer
11 * Science Department, and William Jolitz.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
38 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
39 * from: src/sys/i386/i386/vm_machdep.c,v 1.132.2.2 2000/08/26 04:19:26 yokota
40 * JNPR: vm_machdep.c,v 1.8.2.2 2007/08/16 15:59:17 girish
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include "opt_compat.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
53 #include <sys/syscall.h>
54 #include <sys/sysent.h>
56 #include <sys/vnode.h>
57 #include <sys/vmmeter.h>
58 #include <sys/kernel.h>
59 #include <sys/sysctl.h>
60 #include <sys/unistd.h>
62 #include <machine/cache.h>
63 #include <machine/clock.h>
64 #include <machine/cpu.h>
65 #include <machine/cpufunc.h>
66 #include <machine/cpuinfo.h>
67 #include <machine/md_var.h>
68 #include <machine/pcb.h>
69 #include <machine/tls.h>
72 #include <vm/vm_extern.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pageout.h>
78 #include <vm/vm_param.h>
80 #include <vm/uma_int.h>
85 /* Duplicated from asm.h */
86 #if defined(__mips_o32)
91 #if defined(__mips_o32) || defined(__mips_o64)
92 #define CALLFRAME_SIZ (SZREG * (4 + 2))
93 #elif defined(__mips_n32) || defined(__mips_n64)
94 #define CALLFRAME_SIZ (SZREG * 4)
98 * Finish a fork operation, with process p2 nearly set up.
99 * Copy and update the pcb, set up the stack so that the child
100 * ready to run and return to user mode.
103 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2,int flags)
109 if ((flags & RFPROC) == 0)
111 /* It is assumed that the vm_thread_alloc called
112 * cpu_thread_alloc() before cpu_fork is called.
115 /* Point the pcb to the top of the stack */
118 /* Copy p1's pcb, note that in this case
119 * our pcb also includes the td_frame being copied
120 * too. The older mips2 code did an additional copy
121 * of the td_frame, for us that's not needed any
122 * longer (this copy does them both)
124 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
126 /* Point mdproc and then copy over td1's contents
127 * md_proc is empty for MIPS
129 td2->td_md.md_flags = td1->td_md.md_flags & MDTD_FPUSED;
132 * Set up return-value registers as fork() libc stub expects.
134 td2->td_frame->v0 = 0;
135 td2->td_frame->v1 = 1;
136 td2->td_frame->a3 = 0;
138 if (td1 == PCPU_GET(fpcurthread))
139 MipsSaveCurFPState(td1);
141 pcb2->pcb_context[PCB_REG_RA] = (register_t)(intptr_t)fork_trampoline;
142 /* Make sp 64-bit aligned */
143 pcb2->pcb_context[PCB_REG_SP] = (register_t)(((vm_offset_t)td2->td_pcb &
144 ~(sizeof(__int64_t) - 1)) - CALLFRAME_SIZ);
145 pcb2->pcb_context[PCB_REG_S0] = (register_t)(intptr_t)fork_return;
146 pcb2->pcb_context[PCB_REG_S1] = (register_t)(intptr_t)td2;
147 pcb2->pcb_context[PCB_REG_S2] = (register_t)(intptr_t)td2->td_frame;
148 pcb2->pcb_context[PCB_REG_SR] = mips_rd_status() &
149 (MIPS_SR_KX | MIPS_SR_UX | MIPS_SR_INT_MASK);
151 * FREEBSD_DEVELOPERS_FIXME:
152 * Setup any other CPU-Specific registers (Not MIPS Standard)
153 * and/or bits in other standard MIPS registers (if CPU-Specific)
157 td2->td_md.md_tls = td1->td_md.md_tls;
158 td2->td_md.md_tls_tcb_offset = td1->td_md.md_tls_tcb_offset;
159 td2->td_md.md_saved_intr = MIPS_SR_INT_IE;
160 td2->td_md.md_spinlock_count = 1;
162 if (td1->td_md.md_flags & MDTD_COP2USED) {
163 if (td1->td_md.md_cop2owner == COP2_OWNER_USERLAND) {
164 if (td1->td_md.md_ucop2)
165 octeon_cop2_save(td1->td_md.md_ucop2);
167 panic("cpu_fork: ucop2 is NULL but COP2 is enabled");
170 if (td1->td_md.md_cop2)
171 octeon_cop2_save(td1->td_md.md_cop2);
173 panic("cpu_fork: cop2 is NULL but COP2 is enabled");
177 if (td1->td_md.md_cop2) {
178 td2->td_md.md_cop2 = octeon_cop2_alloc_ctx();
179 memcpy(td2->td_md.md_cop2, td1->td_md.md_cop2,
180 sizeof(*td1->td_md.md_cop2));
182 if (td1->td_md.md_ucop2) {
183 td2->td_md.md_ucop2 = octeon_cop2_alloc_ctx();
184 memcpy(td2->td_md.md_ucop2, td1->td_md.md_ucop2,
185 sizeof(*td1->td_md.md_ucop2));
187 td2->td_md.md_cop2owner = td1->td_md.md_cop2owner;
188 pcb2->pcb_context[PCB_REG_SR] |= MIPS_SR_PX | MIPS_SR_UX | MIPS_SR_KX | MIPS_SR_SX;
189 /* Clear COP2 bits for userland & kernel */
190 td2->td_frame->sr &= ~MIPS_SR_COP_2_BIT;
191 pcb2->pcb_context[PCB_REG_SR] &= ~MIPS_SR_COP_2_BIT;
196 * Intercept the return address from a freshly forked process that has NOT
197 * been scheduled yet.
199 * This is needed to make kernel threads stay in kernel mode.
202 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
205 * Note that the trap frame follows the args, so the function
206 * is really called like this: func(arg, frame);
208 td->td_pcb->pcb_context[PCB_REG_S0] = (register_t)(intptr_t)func;
209 td->td_pcb->pcb_context[PCB_REG_S1] = (register_t)(intptr_t)arg;
213 cpu_exit(struct thread *td)
218 cpu_thread_exit(struct thread *td)
221 if (PCPU_GET(fpcurthread) == td)
222 PCPU_GET(fpcurthread) = (struct thread *)0;
224 if (td->td_md.md_cop2)
225 memset(td->td_md.md_cop2, 0,
226 sizeof(*td->td_md.md_cop2));
227 if (td->td_md.md_ucop2)
228 memset(td->td_md.md_ucop2, 0,
229 sizeof(*td->td_md.md_ucop2));
234 cpu_thread_free(struct thread *td)
237 if (td->td_md.md_cop2)
238 octeon_cop2_free_ctx(td->td_md.md_cop2);
239 if (td->td_md.md_ucop2)
240 octeon_cop2_free_ctx(td->td_md.md_ucop2);
241 td->td_md.md_cop2 = NULL;
242 td->td_md.md_ucop2 = NULL;
247 cpu_thread_clean(struct thread *td)
252 cpu_thread_swapin(struct thread *td)
258 * The kstack may be at a different physical address now.
259 * Cache the PTEs for the Kernel stack in the machine dependent
260 * part of the thread struct so cpu_switch() can quickly map in
261 * the pcb struct and kernel stack.
263 for (i = 0; i < KSTACK_PAGES; i++) {
264 pte = pmap_pte(kernel_pmap, td->td_kstack + i * PAGE_SIZE);
265 td->td_md.md_upte[i] = *pte & ~TLBLO_SWBITS_MASK;
270 cpu_thread_swapout(struct thread *td)
275 cpu_thread_alloc(struct thread *td)
280 KASSERT((td->td_kstack & (1 << PAGE_SHIFT)) == 0, ("kernel stack must be aligned."));
281 td->td_pcb = (struct pcb *)(td->td_kstack +
282 td->td_kstack_pages * PAGE_SIZE) - 1;
283 td->td_frame = &td->td_pcb->pcb_regs;
285 for (i = 0; i < KSTACK_PAGES; i++) {
286 pte = pmap_pte(kernel_pmap, td->td_kstack + i * PAGE_SIZE);
287 td->td_md.md_upte[i] = *pte & ~TLBLO_SWBITS_MASK;
292 cpu_set_syscall_retval(struct thread *td, int error)
294 struct trapframe *locr0 = td->td_frame;
300 #if defined(__mips_n32) || defined(__mips_n64)
301 #ifdef COMPAT_FREEBSD32
302 if (code == SYS___syscall && SV_PROC_FLAG(td->td_proc, SV_ILP32))
306 if (code == SYS___syscall)
310 if (code == SYS_syscall)
312 else if (code == SYS___syscall) {
314 code = _QUAD_LOWWORD ? locr0->a1 : locr0->a0;
321 if (quad_syscall && code != SYS_lseek) {
323 * System call invoked through the
324 * SYS___syscall interface but the
325 * return value is really just 32
328 locr0->v0 = td->td_retval[0];
330 locr0->v1 = td->td_retval[0];
333 locr0->v0 = td->td_retval[0];
334 locr0->v1 = td->td_retval[1];
340 locr0->pc = td->td_pcb->pcb_tpc;
344 break; /* nothing to do */
347 if (quad_syscall && code != SYS_lseek) {
360 * Initialize machine state, mostly pcb and trap frame for a new
361 * thread, about to return to userspace. Put enough state in the new
362 * thread's PCB to get it to go back to the fork_return(), which
363 * finalizes the thread state and handles peculiarities of the first
364 * return to userspace for the new thread.
367 cpu_copy_thread(struct thread *td, struct thread *td0)
371 /* Point the pcb to the top of the stack. */
375 * Copy the upcall pcb. This loads kernel regs.
376 * Those not loaded individually below get their default
379 * XXXKSE It might be a good idea to simply skip this as
380 * the values of the other registers may be unimportant.
381 * This would remove any requirement for knowing the KSE
382 * at this time (see the matching comment below for
383 * more analysis) (need a good safe default).
384 * In MIPS, the trapframe is the first element of the PCB
385 * and gets copied when we copy the PCB. No separate copy
388 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
391 * Set registers for trampoline to user mode.
394 pcb2->pcb_context[PCB_REG_RA] = (register_t)(intptr_t)fork_trampoline;
395 /* Make sp 64-bit aligned */
396 pcb2->pcb_context[PCB_REG_SP] = (register_t)(((vm_offset_t)td->td_pcb &
397 ~(sizeof(__int64_t) - 1)) - CALLFRAME_SIZ);
398 pcb2->pcb_context[PCB_REG_S0] = (register_t)(intptr_t)fork_return;
399 pcb2->pcb_context[PCB_REG_S1] = (register_t)(intptr_t)td;
400 pcb2->pcb_context[PCB_REG_S2] = (register_t)(intptr_t)td->td_frame;
401 /* Dont set IE bit in SR. sched lock release will take care of it */
402 pcb2->pcb_context[PCB_REG_SR] = mips_rd_status() &
403 (MIPS_SR_PX | MIPS_SR_KX | MIPS_SR_UX | MIPS_SR_INT_MASK);
406 * FREEBSD_DEVELOPERS_FIXME:
407 * Setup any other CPU-Specific registers (Not MIPS Standard)
411 /* Setup to release spin count in in fork_exit(). */
412 td->td_md.md_spinlock_count = 1;
413 td->td_md.md_saved_intr = MIPS_SR_INT_IE;
415 /* Maybe we need to fix this? */
416 td->td_md.md_saved_sr = ( (MIPS_SR_COP_2_BIT | MIPS_SR_COP_0_BIT) |
417 (MIPS_SR_PX | MIPS_SR_UX | MIPS_SR_KX | MIPS_SR_SX) |
418 (MIPS_SR_INT_IE | MIPS_HARD_INT_MASK));
423 * Set that machine state for performing an upcall that starts
424 * the entry function with the given argument.
427 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
430 struct trapframe *tf;
434 * At the point where a function is called, sp must be 8
435 * byte aligned[for compatibility with 64-bit CPUs]
436 * in ``See MIPS Run'' by D. Sweetman, p. 269
439 sp = (((intptr_t)stack->ss_sp + stack->ss_size) & ~0x7) -
443 * Set the trap frame to point at the beginning of the uts
447 bzero(tf, sizeof(struct trapframe));
449 tf->pc = (register_t)(intptr_t)entry;
451 * MIPS ABI requires T9 to be the same as PC
452 * in subroutine entry point
454 tf->t9 = (register_t)(intptr_t)entry;
455 tf->a0 = (register_t)(intptr_t)arg;
458 * Keep interrupt mask
460 td->td_frame->sr = MIPS_SR_KSU_USER | MIPS_SR_EXL | MIPS_SR_INT_IE |
461 (mips_rd_status() & MIPS_SR_INT_MASK);
462 #if defined(__mips_n32)
463 td->td_frame->sr |= MIPS_SR_PX;
464 #elif defined(__mips_n64)
465 td->td_frame->sr |= MIPS_SR_PX | MIPS_SR_UX | MIPS_SR_KX;
467 /* tf->sr |= (ALL_INT_MASK & idle_mask) | SR_INT_ENAB; */
468 /**XXX the above may now be wrong -- mips2 implements this as panic */
470 * FREEBSD_DEVELOPERS_FIXME:
471 * Setup any other CPU-Specific registers (Not MIPS Standard)
477 * Implement the pre-zeroed page mechanism.
478 * This routine is called from the idle loop.
481 #define ZIDLE_LO(v) ((v) * 2 / 3)
482 #define ZIDLE_HI(v) ((v) * 4 / 5)
485 * Software interrupt handler for queued VM system processing.
491 if (busdma_swi_pending)
496 cpu_set_user_tls(struct thread *td, void *tls_base)
499 #if defined(__mips_n64) && defined(COMPAT_FREEBSD32)
500 if (td->td_proc && SV_PROC_FLAG(td->td_proc, SV_ILP32))
501 td->td_md.md_tls_tcb_offset = TLS_TP_OFFSET + TLS_TCB_SIZE32;
504 td->td_md.md_tls_tcb_offset = TLS_TP_OFFSET + TLS_TCB_SIZE;
505 td->td_md.md_tls = (char*)tls_base;
506 if (td == curthread && cpuinfo.userlocal_reg == true) {
507 mips_wr_userlocal((unsigned long)tls_base +
508 td->td_md.md_tls_tcb_offset);
517 #define DB_PRINT_REG(ptr, regname) \
518 db_printf(" %-12s %p\n", #regname, (void *)(intptr_t)((ptr)->regname))
520 #define DB_PRINT_REG_ARRAY(ptr, arrname, regname) \
521 db_printf(" %-12s %p\n", #regname, (void *)(intptr_t)((ptr)->arrname[regname]))
524 dump_trapframe(struct trapframe *trapframe)
527 db_printf("Trapframe at %p\n", trapframe);
529 DB_PRINT_REG(trapframe, zero);
530 DB_PRINT_REG(trapframe, ast);
531 DB_PRINT_REG(trapframe, v0);
532 DB_PRINT_REG(trapframe, v1);
533 DB_PRINT_REG(trapframe, a0);
534 DB_PRINT_REG(trapframe, a1);
535 DB_PRINT_REG(trapframe, a2);
536 DB_PRINT_REG(trapframe, a3);
537 #if defined(__mips_n32) || defined(__mips_n64)
538 DB_PRINT_REG(trapframe, a4);
539 DB_PRINT_REG(trapframe, a5);
540 DB_PRINT_REG(trapframe, a6);
541 DB_PRINT_REG(trapframe, a7);
542 DB_PRINT_REG(trapframe, t0);
543 DB_PRINT_REG(trapframe, t1);
544 DB_PRINT_REG(trapframe, t2);
545 DB_PRINT_REG(trapframe, t3);
547 DB_PRINT_REG(trapframe, t0);
548 DB_PRINT_REG(trapframe, t1);
549 DB_PRINT_REG(trapframe, t2);
550 DB_PRINT_REG(trapframe, t3);
551 DB_PRINT_REG(trapframe, t4);
552 DB_PRINT_REG(trapframe, t5);
553 DB_PRINT_REG(trapframe, t6);
554 DB_PRINT_REG(trapframe, t7);
556 DB_PRINT_REG(trapframe, s0);
557 DB_PRINT_REG(trapframe, s1);
558 DB_PRINT_REG(trapframe, s2);
559 DB_PRINT_REG(trapframe, s3);
560 DB_PRINT_REG(trapframe, s4);
561 DB_PRINT_REG(trapframe, s5);
562 DB_PRINT_REG(trapframe, s6);
563 DB_PRINT_REG(trapframe, s7);
564 DB_PRINT_REG(trapframe, t8);
565 DB_PRINT_REG(trapframe, t9);
566 DB_PRINT_REG(trapframe, k0);
567 DB_PRINT_REG(trapframe, k1);
568 DB_PRINT_REG(trapframe, gp);
569 DB_PRINT_REG(trapframe, sp);
570 DB_PRINT_REG(trapframe, s8);
571 DB_PRINT_REG(trapframe, ra);
572 DB_PRINT_REG(trapframe, sr);
573 DB_PRINT_REG(trapframe, mullo);
574 DB_PRINT_REG(trapframe, mulhi);
575 DB_PRINT_REG(trapframe, badvaddr);
576 DB_PRINT_REG(trapframe, cause);
577 DB_PRINT_REG(trapframe, pc);
580 DB_SHOW_COMMAND(pcb, ddb_dump_pcb)
584 struct trapframe *trapframe;
586 /* Determine which thread to examine. */
588 td = db_lookup_thread(addr, true);
594 db_printf("Thread %d at %p\n", td->td_tid, td);
596 db_printf("PCB at %p\n", pcb);
598 trapframe = &pcb->pcb_regs;
599 dump_trapframe(trapframe);
601 db_printf("PCB Context:\n");
602 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_S0);
603 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_S1);
604 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_S2);
605 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_S3);
606 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_S4);
607 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_S5);
608 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_S6);
609 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_S7);
610 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_SP);
611 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_S8);
612 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_RA);
613 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_SR);
614 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_GP);
615 DB_PRINT_REG_ARRAY(pcb, pcb_context, PCB_REG_PC);
617 db_printf("PCB onfault = %p\n", pcb->pcb_onfault);
618 db_printf("md_saved_intr = 0x%0lx\n", (long)td->td_md.md_saved_intr);
619 db_printf("md_spinlock_count = %d\n", td->td_md.md_spinlock_count);
621 if (td->td_frame != trapframe) {
622 db_printf("td->td_frame %p is not the same as pcb_regs %p\n",
623 td->td_frame, trapframe);
628 * Dump the trapframe beginning at address specified by first argument.
630 DB_SHOW_COMMAND(trapframe, ddb_dump_trapframe)
636 dump_trapframe((struct trapframe *)addr);