Path: blob/main/sys/cddl/dev/dtrace/riscv/dtrace_subr.c
48375 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*21* Portions Copyright 2016-2018 Ruslan Bukin <[email protected]>22*23*/24/*25* Copyright 2005 Sun Microsystems, Inc. All rights reserved.26* Use is subject to license terms.27*/2829#include <sys/param.h>30#include <sys/systm.h>31#include <sys/kernel.h>32#include <sys/malloc.h>33#include <sys/kmem.h>34#include <sys/proc.h>35#include <sys/smp.h>36#include <sys/dtrace_impl.h>37#include <sys/dtrace_bsd.h>38#include <cddl/dev/dtrace/dtrace_cddl.h>39#include <machine/vmparam.h>40#include <machine/encoding.h>41#include <machine/riscvreg.h>42#include <machine/clock.h>43#include <machine/frame.h>44#include <machine/trap.h>45#include <vm/pmap.h>4647extern dtrace_id_t dtrace_probeid_error;48extern int (*dtrace_invop_jump_addr)(struct trapframe *);49extern void dtrace_getnanotime(struct timespec *tsp);50extern void dtrace_getnanouptime(struct timespec *tsp);5152int dtrace_invop(uintptr_t, struct trapframe *);53void dtrace_invop_init(void);54void dtrace_invop_uninit(void);5556typedef struct dtrace_invop_hdlr {57int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);58struct dtrace_invop_hdlr *dtih_next;59} dtrace_invop_hdlr_t;6061dtrace_invop_hdlr_t *dtrace_invop_hdlr;6263int64dtrace_invop(uintptr_t addr, struct trapframe *frame)65{66struct thread *td;67dtrace_invop_hdlr_t *hdlr;68int rval;6970rval = 0;71td = curthread;72td->t_dtrace_trapframe = frame;73for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)74if ((rval = hdlr->dtih_func(addr, frame, 0)) != 0)75break;76td->t_dtrace_trapframe = NULL;77return (rval);78}7980void81dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))82{83dtrace_invop_hdlr_t *hdlr;8485hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);86hdlr->dtih_func = func;87hdlr->dtih_next = dtrace_invop_hdlr;88dtrace_invop_hdlr = hdlr;89}9091void92dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))93{94dtrace_invop_hdlr_t *hdlr, *prev;9596hdlr = dtrace_invop_hdlr;97prev = NULL;9899for (;;) {100if (hdlr == NULL)101panic("attempt to remove non-existent invop handler");102103if (hdlr->dtih_func == func)104break;105106prev = hdlr;107hdlr = hdlr->dtih_next;108}109110if (prev == NULL) {111ASSERT(dtrace_invop_hdlr == hdlr);112dtrace_invop_hdlr = hdlr->dtih_next;113} else {114ASSERT(dtrace_invop_hdlr != hdlr);115prev->dtih_next = hdlr->dtih_next;116}117118kmem_free(hdlr, 0);119}120121/*ARGSUSED*/122void123dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))124{125126(*func)(0, (uintptr_t)VM_MIN_KERNEL_ADDRESS);127}128129/*130* DTrace needs a high resolution time function which can131* be called from a probe context and guaranteed not to have132* instrumented with probes itself.133*134* Returns nanoseconds since boot.135*/136uint64_t137dtrace_gethrtime(void)138{139struct timespec curtime;140141dtrace_getnanouptime(&curtime);142143return (curtime.tv_sec * 1000000000UL + curtime.tv_nsec);144145}146147uint64_t148dtrace_gethrestime(void)149{150struct timespec current_time;151152dtrace_getnanotime(¤t_time);153154return (current_time.tv_sec * 1000000000UL + current_time.tv_nsec);155}156157/* Function to handle DTrace traps during probes. See riscv/riscv/trap.c */158int159dtrace_trap(struct trapframe *frame, u_int type)160{161/*162* A trap can occur while DTrace executes a probe. Before163* executing the probe, DTrace blocks re-scheduling and sets164* a flag in its per-cpu flags to indicate that it doesn't165* want to fault. On returning from the probe, the no-fault166* flag is cleared and finally re-scheduling is enabled.167*168* Check if DTrace has enabled 'no-fault' mode:169*/170if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {171/*172* There are only a couple of trap types that are expected.173* All the rest will be handled in the usual way.174*/175switch (type) {176case SCAUSE_LOAD_ACCESS_FAULT:177case SCAUSE_STORE_ACCESS_FAULT:178case SCAUSE_INST_ACCESS_FAULT:179case SCAUSE_INST_PAGE_FAULT:180case SCAUSE_LOAD_PAGE_FAULT:181case SCAUSE_STORE_PAGE_FAULT:182/* Flag a bad address. */183cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;184cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_stval;185186/*187* Offset the instruction pointer to the instruction188* following the one causing the fault.189*/190frame->tf_sepc +=191dtrace_instr_size((uint8_t *)frame->tf_sepc);192193return (1);194default:195/* Handle all other traps in the usual way. */196break;197}198}199200/* Handle the trap in the usual way. */201return (0);202}203204void205dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,206int fault, int fltoffs, uintptr_t illval)207{208209dtrace_probe(dtrace_probeid_error, (uint64_t)(uintptr_t)state,210(uintptr_t)epid,211(uintptr_t)which, (uintptr_t)fault, (uintptr_t)fltoffs);212}213214static int215dtrace_invop_start(struct trapframe *frame)216{217register_t *sp;218uint32_t uimm;219uint32_t imm;220int invop;221222invop = dtrace_invop(frame->tf_sepc, frame);223if (invop == 0)224return (-1);225226if (dtrace_match_opcode(invop, (MATCH_SD | RS2_RA | RS1_SP),227(MASK_SD | RS2_MASK | RS1_MASK))) {228/* Non-compressed store of ra to sp */229imm = (invop >> 7) & 0x1f;230imm |= ((invop >> 25) & 0x7f) << 5;231sp = (register_t *)((uint8_t *)frame->tf_sp + imm);232*sp = frame->tf_ra;233frame->tf_sepc += INSN_SIZE;234return (0);235}236237if (dtrace_match_opcode(invop, (MATCH_JALR | (X_RA << RS1_SHIFT)),238(MASK_JALR | RD_MASK | RS1_MASK | IMM_MASK))) {239/* Non-compressed ret */240frame->tf_sepc = frame->tf_ra;241return (0);242}243244if (dtrace_match_opcode(invop, (MATCH_C_SDSP | RS2_C_RA),245(MASK_C_SDSP | RS2_C_MASK))) {246/* 'C'-compressed store of ra to sp */247uimm = ((invop >> 10) & 0x7) << 3;248uimm |= ((invop >> 7) & 0x7) << 6;249sp = (register_t *)((uint8_t *)frame->tf_sp + uimm);250*sp = frame->tf_ra;251frame->tf_sepc += INSN_C_SIZE;252return (0);253}254255if (dtrace_match_opcode(invop, (MATCH_C_JR | (X_RA << RD_SHIFT)),256(MASK_C_JR | RD_MASK))) {257/* 'C'-compressed ret */258frame->tf_sepc = frame->tf_ra;259return (0);260}261262if (dtrace_match_opcode(invop, MATCH_C_NOP, MASK_C_NOP))263return (0);264265#ifdef INVARIANTS266panic("Instruction %x doesn't match any opcode.", invop);267#endif268269return (-1);270}271272void273dtrace_invop_init(void)274{275276dtrace_invop_jump_addr = dtrace_invop_start;277}278279void280dtrace_invop_uninit(void)281{282283dtrace_invop_jump_addr = 0;284}285286287