Module Name: src Committed By: simonb Date: Mon Mar 29 05:17:09 UTC 2021
Added Files: src/external/cddl/osnet/dev/dtrace/mips: dtrace_asm.S dtrace_isa.c dtrace_subr.c regset.h src/external/cddl/osnet/dev/fbt/mips: fbt_isa.c fbt_isa.h Log Message: Work in progress dtrace for MIPS. MIPS support mostly copied from FreeBSD, with NetBSD-specific changes largely based on aarch64 dtrace support. Working well enough for system call tracing. To generate a diff of this commit: cvs rdiff -u -r0 -r1.1 src/external/cddl/osnet/dev/dtrace/mips/dtrace_asm.S \ src/external/cddl/osnet/dev/dtrace/mips/dtrace_isa.c \ src/external/cddl/osnet/dev/dtrace/mips/dtrace_subr.c \ src/external/cddl/osnet/dev/dtrace/mips/regset.h cvs rdiff -u -r0 -r1.1 src/external/cddl/osnet/dev/fbt/mips/fbt_isa.c \ src/external/cddl/osnet/dev/fbt/mips/fbt_isa.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Added files: Index: src/external/cddl/osnet/dev/dtrace/mips/dtrace_asm.S diff -u /dev/null src/external/cddl/osnet/dev/dtrace/mips/dtrace_asm.S:1.1 --- /dev/null Mon Mar 29 05:17:09 2021 +++ src/external/cddl/osnet/dev/dtrace/mips/dtrace_asm.S Mon Mar 29 05:17:09 2021 @@ -0,0 +1,233 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License, Version 1.0 only + * (the "License"). You may not use this file except in compliance + * with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + * + * $FreeBSD$ + */ +/* + * Copyright 2004 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#define _ASM +#define _LOCORE + +#include <sys/cpuvar_defs.h> +#include <sys/dtrace.h> + +#include <machine/asm.h> +#include <mips/cpuregs.h> +#include <machine/regnum.h> + + .set noreorder # Noreorder is default style! + +/* + * Primitives + */ + + .text + +/* +void dtrace_membar_producer(void) +*/ +LEAF(dtrace_membar_producer) + j ra + nop +END(dtrace_membar_producer) + +/* +void dtrace_membar_consumer(void) +*/ +LEAF(dtrace_membar_consumer) + j ra + nop +END(dtrace_membar_consumer) + +/* +dtrace_icookie_t dtrace_interrupt_disable(void) +*/ +LEAF(dtrace_interrupt_disable) + mfc0 t0, MIPS_COP_0_STATUS + move v0, t0 + and v0, v0, MIPS_SR_INT_IE + and t0, t0, ~MIPS_SR_INT_IE + mtc0 t0, MIPS_COP_0_STATUS + j ra + nop +END(dtrace_interrupt_disable) + +/* +void dtrace_interrupt_enable(dtrace_icookie_t cookie) +*/ +LEAF(dtrace_interrupt_enable) + mfc0 t0, MIPS_COP_0_STATUS + beqz a0, not_enabled + or t0, t0, MIPS_SR_INT_IE + mtc0 t0, MIPS_COP_0_STATUS +not_enabled: + j ra + nop +END(dtrace_interrupt_enable) + +/* +uint32_t dtrace_cas32(uint32_t *target, uint32_t cmp, uint32_t new) +*/ +LEAF(dtrace_cas32) +1: + move t1, a2 + ll t0, 0(a0) + bne t0, a1, 2f + nop + sc t1, 0(a0) + beqz t1, 1b + nop +2: move v0, t0 + j ra + nop +END(dtrace_cas32) + +/* +void * +dtrace_casptr(void *target, void *cmp, void *new) +*/ +LEAF(dtrace_casptr) +1: + move t1, a2 + PTR_LL t0, 0(a0) + bne t0, a1, 2f + nop + PTR_SC t1, 0(a0) + beqz t1, 1b + nop +2: move v0, t0 + j ra + nop +END(dtrace_casptr) + + +/* +uintptr_t +dtrace_fulword(void *addr) +*/ +LEAF(dtrace_fulword) +END(dtrace_fulword) + +/* +uint8_t +dtrace_fuword8_nocheck(void *addr) +*/ +LEAF(dtrace_fuword8_nocheck) + lbu v0, 0(a0) + j ra + nop +END(dtrace_fuword8_nocheck) + +/* +uint16_t +dtrace_fuword16_nocheck(void *addr) +*/ +LEAF(dtrace_fuword16_nocheck) + lhu v0, 0(a0) + j ra + nop +END(dtrace_fuword16_nocheck) + +/* +uint32_t +dtrace_fuword32_nocheck(void *addr) +*/ +LEAF(dtrace_fuword32_nocheck) + lw v0, 0(a0) + j ra + nop +END(dtrace_fuword32_nocheck) + +/* +uint64_t +dtrace_fuword64_nocheck(void *addr) +*/ +LEAF(dtrace_fuword64_nocheck) +#if defined(__mips_n64) || defined(__mips_n32) + ld v0, 0(a0) +#else + lw v1,4(a0) + lw v0,0(a0) +#endif + j ra + nop +END(dtrace_fuword64_nocheck) + +/* +XXX: unoptimized +void +dtrace_copy(uintptr_t src, uintptr_t dest, size_t size) +*/ +LEAF(dtrace_copy) +1: + beqz a2, 2f + nop + lbu t0, 0(a0) + sb t0, 0(a1) + PTR_ADDU a0, a0, 1 + PTR_ADDU a1, a1, 1 + INT_SUBU a2, a2, 1 + j 1b + nop +2: + j ra + nop +END(dtrace_copy) + +/* +XXX: Unoptimized. Check for flags on page boundaries only(?) +void +dtrace_copystr(uintptr_t uaddr, uintptr_t kaddr, size_t size, + volatile uint16_t *flags) +*/ +LEAF(dtrace_copystr) +1: + lbu t0, 0(a0) + sb t0, 0(a1) + PTR_ADDU a0, a0, 1 + PTR_ADDU a1, a1, 1 + INT_SUBU a2, a2, 1 + beqz t0, 2f + nop + lhu t1, (a3) + and t1, t1, CPU_DTRACE_BADADDR + bnez t1, 2f + nop + + bnez a2, 1b + nop +2: + j ra + nop +END(dtrace_copystr) + +/* +uintptr_t +dtrace_caller(int aframes) +*/ +LEAF(dtrace_caller) + li v0, -1 + j ra + nop +END(dtrace_caller) Index: src/external/cddl/osnet/dev/dtrace/mips/dtrace_isa.c diff -u /dev/null src/external/cddl/osnet/dev/dtrace/mips/dtrace_isa.c:1.1 --- /dev/null Mon Mar 29 05:17:09 2021 +++ src/external/cddl/osnet/dev/dtrace/mips/dtrace_isa.c Mon Mar 29 05:17:09 2021 @@ -0,0 +1,739 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License, Version 1.0 only + * (the "License"). You may not use this file except in compliance + * with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + * + * $FreeBSD$ + */ +/* + * Copyright 2005 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ +#include <sys/cdefs.h> + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/kernel.h> + +#include <mips/cpuregs.h> +#include <mips/frame.h> +#include <mips/locore.h> +#include <mips/reg.h> + +#include <machine/db_machdep.h> +#include <machine/mips_opcode.h> +#include <ddb/db_sym.h> +#include <ddb/ddb.h> + +#include "regset.h" + +#ifdef __mips_n64 +#define MIPS_IS_VALID_KERNELADDR(reg) ((((reg) & 3) == 0) && \ + ((vm_offset_t)(reg) >= MIPS_XKPHYS_START)) +#else +#define MIPS_IS_VALID_KERNELADDR(reg) ((((reg) & 3) == 0) && \ + ((vm_offset_t)(reg) >= MIPS_KSEG0_START)) +#endif + +#ifdef __FreeBSD__ +#define CURRENT_CPU curcpu +#define CURRENT_TRAPFRAME curthread->td_frame +#endif +#ifdef __NetBSD__ +#define CURRENT_CPU cpu_index(curcpu()) +#define CURRENT_TRAPFRAME curlwp->l_md.md_utf +#endif + +#ifdef __FreeBSD__ +#define KDBPEEK(va) kdbpeek((int *)(va)) +#define KDBPEEKD(va) kdbpeekd((int *)(va)) +#endif +#ifdef __NetBSD__ +#define KDBPEEK(va) kdbrpeek((va), sizeof(int32_t)) +#define KDBPEEKD(va) kdbrpeek((va), sizeof(int64_t)) +#endif + +#ifndef OP_BCOND +#define OP_BCOND OP_REGIMM +#endif + +/* + * We need some reasonable default to prevent backtrace code + * from wandering too far + */ +#define MAX_FUNCTION_SIZE 0x10000 +#define MAX_PROLOGUE_SIZE 0x100 + +uint8_t dtrace_fuword8_nocheck(void *); +uint16_t dtrace_fuword16_nocheck(void *); +uint32_t dtrace_fuword32_nocheck(void *); +uint64_t dtrace_fuword64_nocheck(void *); + +static int dtrace_next_frame(register_t *pc, register_t *sp, register_t *args, int *valid_args); +static int dtrace_next_uframe(register_t *pc, register_t *sp, register_t *ra); + +void +dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes, + uint32_t *intrpc) +{ + int depth = 0; + vm_offset_t callpc; + pc_t caller = (pc_t) solaris_cpu[CURRENT_CPU].cpu_dtrace_caller; + register_t sp, ra, pc; + + if (intrpc != 0) + pcstack[depth++] = (pc_t) intrpc; + + aframes++; + + sp = (register_t)(intptr_t)__builtin_frame_address(0); + ra = (register_t)(intptr_t)__builtin_return_address(0); + + __asm __volatile( + "jal 99f\n" + "nop\n" + "99:\n" + "move %0, $31\n" /* get ra */ + "move $31, %1\n" /* restore ra */ + : "=r" (pc) + : "r" (ra)); + + while (depth < pcstack_limit) { + + callpc = pc; + + if (aframes > 0) { + aframes--; + if ((aframes == 0) && (caller != 0)) { + pcstack[depth++] = caller; + } + } + else { + pcstack[depth++] = callpc; + } + + if (dtrace_next_frame(&pc, &sp, NULL, NULL) < 0) + break; + } + + for (; depth < pcstack_limit; depth++) { + pcstack[depth] = 0; + } +} + +void +dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit) +{ + proc_t *p = curproc; + struct trapframe *tf; + register_t sp, ra, pc; + volatile uint16_t *flags = + (volatile uint16_t *)&cpu_core[CURRENT_CPU].cpuc_dtrace_flags; + + if (*flags & CPU_DTRACE_FAULT) + return; + + if (pcstack_limit <= 0) + return; + + /* + * If there's no user context we still need to zero the stack. + */ + if (p == NULL || (tf = CURRENT_TRAPFRAME) == NULL) + goto zero; + + *pcstack++ = (uint64_t)p->p_pid; + pcstack_limit--; + + if (pcstack_limit <= 0) + return; + + pc = tf->tf_regs[_R_PC]; + sp = tf->tf_regs[_R_SP]; + ra = tf->tf_regs[_R_RA]; + *pcstack++ = (uint64_t)pc; + + /* + * Unwind, and unwind, and unwind + */ + while (1) { + if (dtrace_next_uframe(&pc, &sp, &ra) < 0) + break; + + *pcstack++ = pc; + pcstack_limit--; + + if (pcstack_limit <= 0) + break; + } + +zero: + while (pcstack_limit-- > 0) + *pcstack++ = 0; +} + +int +dtrace_getustackdepth(void) +{ + int n = 0; + proc_t *p = curproc; + struct trapframe *tf; + register_t sp, ra, pc; + volatile uint16_t *flags = + (volatile uint16_t *)&cpu_core[CURRENT_CPU].cpuc_dtrace_flags; + + if (*flags & CPU_DTRACE_FAULT) + return (0); + + if (p == NULL || (tf = CURRENT_TRAPFRAME) == NULL) + return (0); + + pc = tf->tf_regs[_R_PC]; + sp = tf->tf_regs[_R_SP]; + ra = tf->tf_regs[_R_RA]; + n++; + + /* + * Unwind, and unwind, and unwind + */ + while (1) { + if (dtrace_next_uframe(&pc, &sp, &ra) < 0) + break; + n++; + } + + return (n); +} + +void +dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit) +{ + printf("IMPLEMENT ME: %s\n", __func__); +} + +/*ARGSUSED*/ +uint64_t +dtrace_getarg(int arg, int aframes) +{ + int i; + register_t sp, ra, pc; + /* XXX: Fix this ugly code */ + register_t args[8]; + int valid[8]; + + sp = (register_t)(intptr_t)__builtin_frame_address(0); + ra = (register_t)(intptr_t)__builtin_return_address(0); + + __asm __volatile( + "jal 99f\n" + "nop\n" + "99:\n" + "move %0, $31\n" /* get ra */ + "move $31, %1\n" /* restore ra */ + : "=r" (pc) + : "r" (ra)); + + for (i = 0; i <= aframes + 1; i++) { + if (dtrace_next_frame(&pc, &sp, args, valid) < 0) { + printf("%s: stack ends at frame #%d\n", __func__, i); + return (0); + } + } + + if (arg < 8) { + if (valid[arg]) + return (args[arg]); + else + printf("%s: request arg%d is not valid\n", __func__, arg); + } + + return (0); +} + +int +dtrace_getstackdepth(int aframes) +{ + register_t sp, ra, pc; + int depth = 0; + + sp = (register_t)(intptr_t)__builtin_frame_address(0); + ra = (register_t)(intptr_t)__builtin_return_address(0); + + __asm __volatile( + "jal 99f\n" + "nop\n" + "99:\n" + "move %0, $31\n" /* get ra */ + "move $31, %1\n" /* restore ra */ + : "=r" (pc) + : "r" (ra)); + + for (;;) { + if (dtrace_next_frame(&pc, &sp, NULL, NULL) < 0) + break; + depth++; + } + + if (depth < aframes) + return 0; + else + return depth - aframes; +} + +ulong_t +dtrace_getreg(struct trapframe *rp, uint_t reg) +{ + + return (0); +} + +static int +dtrace_next_frame(register_t *pc, register_t *sp, + register_t *args, int *valid_args) +{ + InstFmt i; + /* + * Arrays for a0..a3 registers and flags if content + * of these registers is valid, e.g. obtained from the stack + */ + uintptr_t va; + unsigned instr, mask; + unsigned int frames = 0; + int more, stksize; + register_t ra = 0; + int arg, r; + vm_offset_t addr; + + /* + * Invalidate arguments values + */ + if (valid_args) { + for (r = 0; r < 8; r++) + valid_args[r] = 0; + } + + /* Jump here after a nonstandard (interrupt handler) frame */ + stksize = 0; + if (frames++ > 100) { + /* return breaks stackframe-size heuristics with gcc -O2 */ + goto error; /* XXX */ + } + + /* check for bad SP: could foul up next frame */ + if (!MIPS_IS_VALID_KERNELADDR(*sp)) { + goto error; + } + + /* check for bad PC */ + if (!MIPS_IS_VALID_KERNELADDR(*pc)) { + goto error; + } + + /* + * Find the beginning of the current subroutine by scanning + * backwards from the current PC for the end of the previous + * subroutine. + */ + va = *pc - sizeof(int); + while (1) { + instr = KDBPEEK(va); + + /* [d]addiu sp,sp,-X */ + if (((instr & 0xffff8000) == 0x27bd8000) + || ((instr & 0xffff8000) == 0x67bd8000)) + break; + + /* jr ra */ + if (instr == 0x03e00008) { + /* skip over branch-delay slot instruction */ + va += 2 * sizeof(int); + break; + } + + va -= sizeof(int); + } + + /* skip over nulls which might separate .o files */ + while ((instr = KDBPEEK(va)) == 0) + va += sizeof(int); + + /* scan forwards to find stack size and any saved registers */ + stksize = 0; + more = 3; + mask = 0; + for (; more; va += sizeof(int), + more = (more == 3) ? 3 : more - 1) { + /* stop if hit our current position */ + if (va >= *pc) + break; + instr = KDBPEEK(va); + i.word = instr; + switch (i.JType.op) { + case OP_SPECIAL: + switch (i.RType.func) { + case OP_JR: + case OP_JALR: + more = 2; /* stop after next instruction */ + break; + + case OP_SYSCALL: + case OP_BREAK: + more = 1; /* stop now */ + }; + break; + + case OP_BCOND: + case OP_J: + case OP_JAL: + case OP_BEQ: + case OP_BNE: + case OP_BLEZ: + case OP_BGTZ: + more = 2; /* stop after next instruction */ + break; + + case OP_COP0: + case OP_COP1: + case OP_COP2: + case OP_COP3: + switch (i.RType.rs) { + case OP_BCx: + case OP_BCy: + more = 2; /* stop after next instruction */ + }; + break; + + case OP_SW: + /* look for saved registers on the stack */ + if (i.IType.rs != 29) + break; + /* only restore the first one */ + if (mask & (1 << i.IType.rt)) + break; + mask |= (1 << i.IType.rt); + addr = (vm_offset_t)(*sp + (short)i.IType.imm); + switch (i.IType.rt) { + case 4:/* a0 */ + case 5:/* a1 */ + case 6:/* a2 */ + case 7:/* a3 */ +#if defined(__mips_n64) || defined(__mips_n32) + case 8:/* a4 */ + case 9:/* a5 */ + case 10:/* a6 */ + case 11:/* a7 */ +#endif + arg = i.IType.rt - 4; + if (args) + args[arg] = KDBPEEK(addr); + if (valid_args) + valid_args[arg] = 1; + break; + case 31: /* ra */ + ra = KDBPEEK(addr); + } + break; + + case OP_SD: + /* look for saved registers on the stack */ + if (i.IType.rs != 29) + break; + /* only restore the first one */ + if (mask & (1 << i.IType.rt)) + break; + mask |= (1 << i.IType.rt); + addr = (vm_offset_t)(*sp + (short)i.IType.imm); + switch (i.IType.rt) { + case 4:/* a0 */ + case 5:/* a1 */ + case 6:/* a2 */ + case 7:/* a3 */ +#if defined(__mips_n64) || defined(__mips_n32) + case 8:/* a4 */ + case 9:/* a5 */ + case 10:/* a6 */ + case 11:/* a7 */ +#endif + arg = i.IType.rt - 4; + if (args) + args[arg] = KDBPEEKD(addr); + if (valid_args) + valid_args[arg] = 1; + break; + + case 31: /* ra */ + ra = KDBPEEKD(addr); + } + break; + + case OP_ADDI: + case OP_ADDIU: + case OP_DADDI: + case OP_DADDIU: + /* look for stack pointer adjustment */ + if (i.IType.rs != 29 || i.IType.rt != 29) + break; + stksize = -((short)i.IType.imm); + } + } + + if (!MIPS_IS_VALID_KERNELADDR(ra)) + return (-1); + + *pc = ra; + *sp += stksize; + +#if defined(__mips_o32) + /* + * For MIPS32 fill out arguments 5..8 from the stack + */ + for (arg = 4; arg < 8; arg++) { + addr = (vm_offset_t)(*sp + arg*sizeof(register_t)); + if (args) + args[arg] = KDBPEEKD(addr); + if (valid_args) + valid_args[arg] = 1; + } +#endif + + return (0); +error: + return (-1); +} + +static int +dtrace_next_uframe(register_t *pc, register_t *sp, register_t *ra) +{ + int offset, registers_on_stack; + uint32_t opcode, mask; + register_t function_start; + int stksize; + InstFmt i; + + volatile uint16_t *flags = + (volatile uint16_t *)&cpu_core[CURRENT_CPU].cpuc_dtrace_flags; + + registers_on_stack = 0; + mask = 0; + function_start = 0; + offset = 0; + stksize = 0; + + while (offset < MAX_FUNCTION_SIZE) { + opcode = dtrace_fuword32((void *)(vm_offset_t)(*pc - offset)); + + if (*flags & CPU_DTRACE_FAULT) + goto fault; + + /* [d]addiu sp, sp, -X*/ + if (((opcode & 0xffff8000) == 0x27bd8000) + || ((opcode & 0xffff8000) == 0x67bd8000)) { + function_start = *pc - offset; + registers_on_stack = 1; + break; + } + + /* lui gp, X */ + if ((opcode & 0xffff8000) == 0x3c1c0000) { + /* + * Function might start with this instruction + * Keep an eye on "jr ra" and sp correction + * with positive value further on + */ + function_start = *pc - offset; + } + + if (function_start) { + /* + * Stop looking further. Possible end of + * function instruction: it means there is no + * stack modifications, sp is unchanged + */ + + /* [d]addiu sp,sp,X */ + if (((opcode & 0xffff8000) == 0x27bd0000) + || ((opcode & 0xffff8000) == 0x67bd0000)) + break; + + if (opcode == 0x03e00008) + break; + } + + offset += sizeof(int); + } + + if (!function_start) + return (-1); + + if (registers_on_stack) { + offset = 0; + while ((offset < MAX_PROLOGUE_SIZE) + && ((function_start + offset) < *pc)) { + i.word = + dtrace_fuword32((void *)(vm_offset_t)(function_start + offset)); + switch (i.JType.op) { + case OP_SW: + /* look for saved registers on the stack */ + if (i.IType.rs != 29) + break; + /* only restore the first one */ + if (mask & (1 << i.IType.rt)) + break; + mask |= (1 << i.IType.rt); + if (i.IType.rt == 31) + *ra = dtrace_fuword32((void *)(vm_offset_t)(*sp + (short)i.IType.imm)); + break; + + case OP_SD: + /* look for saved registers on the stack */ + if (i.IType.rs != 29) + break; + /* only restore the first one */ + if (mask & (1 << i.IType.rt)) + break; + mask |= (1 << i.IType.rt); + /* ra */ + if (i.IType.rt == 31) + *ra = dtrace_fuword64((void *)(vm_offset_t)(*sp + (short)i.IType.imm)); + break; + + case OP_ADDI: + case OP_ADDIU: + case OP_DADDI: + case OP_DADDIU: + /* look for stack pointer adjustment */ + if (i.IType.rs != 29 || i.IType.rt != 29) + break; + stksize = -((short)i.IType.imm); + } + + offset += sizeof(int); + + if (*flags & CPU_DTRACE_FAULT) + goto fault; + } + } + + /* + * We reached the end of backtrace + */ + if (*pc == *ra) + return (-1); + + *pc = *ra; + *sp += stksize; + + return (0); +fault: + /* + * We just got lost in backtrace, no big deal + */ + *flags &= ~CPU_DTRACE_FAULT; + return (-1); +} + +static int +dtrace_copycheck(uintptr_t uaddr, uintptr_t kaddr, size_t size) +{ + + if (uaddr + size > VM_MAXUSER_ADDRESS || uaddr + size < uaddr) { + DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); + cpu_core[CURRENT_CPU].cpuc_dtrace_illval = uaddr; + return (0); + } + + return (1); +} + +void +dtrace_copyin(uintptr_t uaddr, uintptr_t kaddr, size_t size, + volatile uint16_t *flags) +{ + if (dtrace_copycheck(uaddr, kaddr, size)) + dtrace_copy(uaddr, kaddr, size); +} + +void +dtrace_copyout(uintptr_t kaddr, uintptr_t uaddr, size_t size, + volatile uint16_t *flags) +{ + if (dtrace_copycheck(uaddr, kaddr, size)) + dtrace_copy(kaddr, uaddr, size); +} + +void +dtrace_copyinstr(uintptr_t uaddr, uintptr_t kaddr, size_t size, + volatile uint16_t *flags) +{ + if (dtrace_copycheck(uaddr, kaddr, size)) + dtrace_copystr(uaddr, kaddr, size, flags); +} + +void +dtrace_copyoutstr(uintptr_t kaddr, uintptr_t uaddr, size_t size, + volatile uint16_t *flags) +{ + if (dtrace_copycheck(uaddr, kaddr, size)) + dtrace_copystr(kaddr, uaddr, size, flags); +} + +uint8_t +dtrace_fuword8(void *uaddr) +{ + if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) { + DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); + cpu_core[CURRENT_CPU].cpuc_dtrace_illval = (uintptr_t)uaddr; + return (0); + } + return (dtrace_fuword8_nocheck(uaddr)); +} + +uint16_t +dtrace_fuword16(void *uaddr) +{ + if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) { + DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); + cpu_core[CURRENT_CPU].cpuc_dtrace_illval = (uintptr_t)uaddr; + return (0); + } + return (dtrace_fuword16_nocheck(uaddr)); +} + +uint32_t +dtrace_fuword32(void *uaddr) +{ + if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) { + DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); + cpu_core[CURRENT_CPU].cpuc_dtrace_illval = (uintptr_t)uaddr; + return (0); + } + return (dtrace_fuword32_nocheck(uaddr)); +} + +uint64_t +dtrace_fuword64(void *uaddr) +{ + if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) { + DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); + cpu_core[CURRENT_CPU].cpuc_dtrace_illval = (uintptr_t)uaddr; + return (0); + } + return (dtrace_fuword64_nocheck(uaddr)); +} Index: src/external/cddl/osnet/dev/dtrace/mips/dtrace_subr.c diff -u /dev/null src/external/cddl/osnet/dev/dtrace/mips/dtrace_subr.c:1.1 --- /dev/null Mon Mar 29 05:17:09 2021 +++ src/external/cddl/osnet/dev/dtrace/mips/dtrace_subr.c Mon Mar 29 05:17:09 2021 @@ -0,0 +1,314 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License, Version 1.0 only + * (the "License"). You may not use this file except in compliance + * with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + * + * $FreeBSD$ + * + */ +/* + * Copyright 2005 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/types.h> +#include <sys/kernel.h> +#include <sys/malloc.h> +#include <sys/kmem.h> +#include <sys/xcall.h> +#include <sys/cpu.h> +#include <sys/dtrace_impl.h> +#include <sys/dtrace_bsd.h> +#include <machine/regnum.h> +#include <machine/locore.h> +#include <machine/trap.h> + +#define DELAYBRANCH(x) ((int)(x) < 0) + +#ifdef __FreeBSD__ +#define CURRENT_CPU curcpu +#endif +#ifdef __NetBSD__ +#define CURRENT_CPU cpu_index(curcpu()) +#endif + +extern dtrace_id_t dtrace_probeid_error; +extern int (*dtrace_invop_jump_addr)(struct trapframe *); +extern void dtrace_getnanotime(struct timespec *tsp); + +int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t); +void dtrace_invop_init(void); +void dtrace_invop_uninit(void); + +void dtrace_gethrtime_init(void); + +typedef struct dtrace_invop_hdlr { + int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t); + struct dtrace_invop_hdlr *dtih_next; +} dtrace_invop_hdlr_t; + +dtrace_invop_hdlr_t *dtrace_invop_hdlr; + +int +dtrace_invop(uintptr_t addr, struct trapframe *stack, uintptr_t eax) +{ + dtrace_invop_hdlr_t *hdlr; + int rval; + + for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) + if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0) + return (rval); + + return (0); +} + +void +dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) +{ + dtrace_invop_hdlr_t *hdlr; + + hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP); + hdlr->dtih_func = func; + hdlr->dtih_next = dtrace_invop_hdlr; + dtrace_invop_hdlr = hdlr; +} + +void +dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t)) +{ + dtrace_invop_hdlr_t *hdlr, *prev; + + hdlr = dtrace_invop_hdlr; + prev = NULL; + + for (;;) { + if (hdlr == NULL) + panic("attempt to remove non-existent invop handler"); + + if (hdlr->dtih_func == func) + break; + + prev = hdlr; + hdlr = hdlr->dtih_next; + } + + if (prev == NULL) { + ASSERT(dtrace_invop_hdlr == hdlr); + dtrace_invop_hdlr = hdlr->dtih_next; + } else { + ASSERT(dtrace_invop_hdlr != hdlr); + prev->dtih_next = hdlr->dtih_next; + } + + kmem_free(hdlr, 0); +} + +/*ARGSUSED*/ +void +dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) +{ + /* XXXXXXsimonb what is a "toxic range"? */ +} + +static void +xcall_func(void *arg0, void *arg1) +{ + dtrace_xcall_t func = arg0; + + (*func)(arg1); +} + +void +dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) +{ + uint64_t where; + + if (cpu == DTRACE_CPUALL) { + where = xc_broadcast(0, xcall_func, func, arg); + } else { + struct cpu_info *ci = cpu_lookup(cpu); + + KASSERT(ci != NULL); + where = xc_unicast(0, xcall_func, func, arg, ci); + } + xc_wait(where); +} + +static void +dtrace_sync_func(void) +{ + +} + +void +dtrace_sync(void) +{ + + dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); +} + +/* + * DTrace needs a high resolution time function which can + * be called from a probe context and guaranteed not to have + * instrumented with probes itself. + * + * Returns nanoseconds since boot. + */ +uint64_t +dtrace_gethrtime() +{ + struct timespec curtime; + + nanouptime(&curtime); + + return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec); + +} + +void +dtrace_gethrtime_init(void) +{ +} + +uint64_t +dtrace_gethrestime(void) +{ + struct timespec current_time; + + dtrace_getnanotime(¤t_time); + + return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec); +} + +/* Function to handle DTrace traps during probes. See amd64/amd64/trap.c */ +int +dtrace_trap(struct trapframe *frame, u_int type) +{ + /* + * A trap can occur while DTrace executes a probe. Before + * executing the probe, DTrace blocks re-scheduling and sets + * a flag in its per-cpu flags to indicate that it doesn't + * want to fault. On returning from the probe, the no-fault + * flag is cleared and finally re-scheduling is enabled. + * + * Check if DTrace has enabled 'no-fault' mode: + */ + + if ((cpu_core[CURRENT_CPU].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) { + /* + * There are only a couple of trap types that are expected. + * All the rest will be handled in the usual way. + */ + switch (type) { + /* Page fault. */ + case T_TLB_ST_MISS: + case T_ADDR_ERR_ST: + case T_TLB_LD_MISS: + case T_ADDR_ERR_LD: + case T_BUS_ERR_IFETCH: + /* Flag a bad address. */ + cpu_core[CURRENT_CPU].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR; + cpu_core[CURRENT_CPU].cpuc_dtrace_illval = frame->tf_regs[_R_BADVADDR]; + + /* + * Offset the instruction pointer to the instruction + * following the one causing the fault. + */ + if (DELAYBRANCH(frame->tf_regs[_R_CAUSE])) /* Check BD bit */ + { + /* XXX: check MipsEmulateBranch on MIPS64 + frame->tf_regs[_R_PC] = MipsEmulateBranch(frame, + frame->tf_regs[_R_PC], 0, 0); + */ + panic("%s: delay slot at %jx, badvaddr = %jx\n", + __func__, (intmax_t)frame->tf_regs[_R_PC], + (intmax_t)frame->tf_regs[_R_BADVADDR]); + } + else + frame->tf_regs[_R_PC] += sizeof(int); + return (1); + default: + /* Handle all other traps in the usual way. */ + break; + } + } + + /* Handle the trap in the usual way. */ + return (0); +} + +void +dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which, + int fault, int fltoffs, uintptr_t illval) +{ + + dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state, + (uintptr_t)epid, + (uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs); +} + +static int +dtrace_invop_start(struct trapframe *frame) +{ + register_t *sp; + int16_t offs; + int invop; + + invop = dtrace_invop(frame->tf_regs[_R_PC], frame, frame->tf_regs[_R_PC]); + if (invop == 0) + return (-1); + + offs = (invop & LDSD_DATA_MASK); + sp = (register_t *)(intptr_t)(frame->tf_regs[_R_SP] + offs); + + switch (invop & LDSD_RA_SP_MASK) { + case LD_RA_SP: + frame->tf_regs[_R_RA] = *sp; + frame->tf_regs[_R_PC] += INSN_SIZE; + break; + case SD_RA_SP: + *(sp) = frame->tf_regs[_R_RA]; + frame->tf_regs[_R_PC] += INSN_SIZE; + break; + default: + printf("%s: 0x%x undefined\n", __func__, invop); + return (-1); + }; + + return (0); +} + +void +dtrace_invop_init(void) +{ + + dtrace_invop_jump_addr = dtrace_invop_start; +} + +void +dtrace_invop_uninit(void) +{ + + dtrace_invop_jump_addr = 0; +} Index: src/external/cddl/osnet/dev/dtrace/mips/regset.h diff -u /dev/null src/external/cddl/osnet/dev/dtrace/mips/regset.h:1.1 --- /dev/null Mon Mar 29 05:17:09 2021 +++ src/external/cddl/osnet/dev/dtrace/mips/regset.h Mon Mar 29 05:17:09 2021 @@ -0,0 +1,62 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License, Version 1.0 only + * (the "License"). You may not use this file except in compliance + * with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + * + * $FreeBSD$ + */ +/* + * Copyright 2004 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +/* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ + +/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ +/* All Rights Reserved */ + +#ifndef _REGSET_H +#define _REGSET_H + +/* + * #pragma ident "@(#)regset.h 1.11 05/06/08 SMI" + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * XXXDTRACE: define registers properly + */ + +#if 0 +#define REG_PC PC +#define REG_FP EBP +#define REG_SP SP +#define REG_PS EFL +#define REG_R0 EAX +#define REG_R1 EDX +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* _REGSET_H */ Index: src/external/cddl/osnet/dev/fbt/mips/fbt_isa.c diff -u /dev/null src/external/cddl/osnet/dev/fbt/mips/fbt_isa.c:1.1 --- /dev/null Mon Mar 29 05:17:09 2021 +++ src/external/cddl/osnet/dev/fbt/mips/fbt_isa.c Mon Mar 29 05:17:09 2021 @@ -0,0 +1,231 @@ +/* $NetBSD: fbt_isa.c,v 1.1 2021/03/29 05:17:09 simonb Exp $ */ + +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + * + * Portions Copyright 2006-2008 John Birrell j...@freebsd.org + * Portions Copyright 2013 Justin Hibbits jhibb...@freebsd.org + * Portions Copyright 2013 Howard Su howar...@freebsd.org + * Portions Copyright 2015-2016 Ruslan Bukin <b...@bsdpad.com> + * + * $FreeBSD$ + */ + +/* + * Copyright 2006 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#include <sys/cdefs.h> +#include <sys/param.h> +#include <sys/cpu.h> +#include <sys/kmem.h> +#include <sys/module.h> + +#include <sys/dtrace.h> + +#include <machine/regnum.h> +#include <machine/locore.h> + +#include <mips/cache.h> + +#include "fbt.h" + +#ifdef __FreeBSD__ +#define CURRENT_CPU curcpu +#endif +#ifdef __NetBSD__ +#define CURRENT_CPU cpu_index(curcpu()) +#endif + +#define FBT_PATCHVAL (MIPS_BREAK_INSTR) +#define FBT_ENTRY "entry" +#define FBT_RETURN "return" + +int +fbt_invop(uintptr_t addr, struct trapframe *frame, uintptr_t rval) +{ + solaris_cpu_t *cpu; + fbt_probe_t *fbt; + + cpu = &solaris_cpu[CURRENT_CPU]; + fbt = fbt_probetab[FBT_ADDR2NDX(addr)]; + + for (; fbt != NULL; fbt = fbt->fbtp_hashnext) { + if ((uintptr_t)fbt->fbtp_patchpoint == addr) { + cpu->cpu_dtrace_caller = addr; + + dtrace_probe(fbt->fbtp_id, frame->tf_regs[_R_A0], + frame->tf_regs[_R_A1], frame->tf_regs[_R_A2], + frame->tf_regs[_R_A3], frame->tf_regs[_R_A4]); + + cpu->cpu_dtrace_caller = 0; + return (fbt->fbtp_savedval); + } + } + + return (0); +} + +void +fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val) +{ + + *fbt->fbtp_patchpoint = val; + mips_icache_sync_range((vm_offset_t)fbt->fbtp_patchpoint, 4); +} + +#if defined(__FreeBSD__) +int +fbt_provide_module_function(linker_file_t lf, int symindx, + linker_symval_t *symval, void *opaque) +#elif defined(__NetBSD__) +int +fbt_provide_module_cb(const char *name, int symindx, void *value, + uint32_t symsize, int type, void *opaque) +#else +#error unsupported platform +#endif +{ + fbt_probe_t *fbt, *retfbt; + uint32_t *instr, *limit; + +#ifdef __FreeBSD__ + const char *name; + char *modname; + + modname = opaque; + name = symval->name; + + instr = (uint32_t *)(symval->value); + limit = (uint32_t *)(symval->value + symval->size); +#endif +#ifdef __NetBSD__ + struct fbt_ksyms_arg *fka = opaque; + modctl_t *mod = fka->fka_mod; + const char *modname = module_name(mod); + + /* got a function? */ + if (ELF_ST_TYPE(type) != STT_FUNC) + return 0; + + instr = (uint32_t *)(value); + limit = (uint32_t *)((uintptr_t)value + symsize); +#endif + + /* Check if function is excluded from instrumentation */ + if (fbt_excluded(name)) + return (0); + + /* Look for store double to ra register */ + for (; instr < limit; instr++) { + if ((*instr & LDSD_RA_SP_MASK) == SD_RA_SP) + break; + } + + if (instr >= limit) + return (0); + +#ifdef __FreeBSD__ + fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); +#endif +#ifdef __NetBSD__ + fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP); +#endif + fbt->fbtp_name = name; + fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, + name, FBT_ENTRY, 3, fbt); + fbt->fbtp_patchpoint = instr; +#ifdef __FreeBSD__ + fbt->fbtp_ctl = lf; + fbt->fbtp_loadcnt = lf->loadcnt; +#endif +#ifdef __NetBSD__ + fbt->fbtp_ctl = mod; +#endif + fbt->fbtp_savedval = *instr; + fbt->fbtp_patchval = FBT_PATCHVAL; + fbt->fbtp_rval = DTRACE_INVOP_SD; + fbt->fbtp_symindx = symindx; + + fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; + fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; + +#ifdef __FreeBSD__ + lf->fbt_nentries++; +#endif + + retfbt = NULL; +again: + for (; instr < limit; instr++) { + if ((*instr & LDSD_RA_SP_MASK) == LD_RA_SP) { + break; + } + } + + if (instr >= limit) + return (0); + + /* + * We have a winner! + */ +#ifdef __FreeBSD__ + fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); +#endif +#ifdef __NetBSD__ + fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP); +#endif + fbt->fbtp_name = name; + if (retfbt == NULL) { + fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, + name, FBT_RETURN, 3, fbt); + } else { +#ifdef __FreeBSD__ + retfbt->fbtp_probenext = fbt; +#endif +#ifdef __NetBSD__ + fbt->fbtp_ctl = mod; +#endif + fbt->fbtp_id = retfbt->fbtp_id; + } + retfbt = fbt; + + fbt->fbtp_patchpoint = instr; +#ifdef __FreeBSD__ + fbt->fbtp_ctl = lf; + fbt->fbtp_loadcnt = lf->loadcnt; +#endif +#ifdef __NetBSD__ + fbt->fbtp_ctl = mod; +#endif + fbt->fbtp_symindx = symindx; + fbt->fbtp_rval = DTRACE_INVOP_LD; + fbt->fbtp_savedval = *instr; + fbt->fbtp_patchval = FBT_PATCHVAL; + fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; + fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; + +#ifdef __FreeBSD__ + lf->fbt_nentries++; +#endif + + instr++; + goto again; +} Index: src/external/cddl/osnet/dev/fbt/mips/fbt_isa.h diff -u /dev/null src/external/cddl/osnet/dev/fbt/mips/fbt_isa.h:1.1 --- /dev/null Mon Mar 29 05:17:09 2021 +++ src/external/cddl/osnet/dev/fbt/mips/fbt_isa.h Mon Mar 29 05:17:09 2021 @@ -0,0 +1,32 @@ +/* $NetBSD: fbt_isa.h,v 1.1 2021/03/29 05:17:09 simonb Exp $ */ + +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + * + * $FreeBSD$ + * + */ + +#ifndef _FBT_ISA_H_ +#define _FBT_ISA_H_ + +typedef uint32_t fbt_patchval_t; + +#endif