1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/dtrace.h> 30 #include <sys/fasttrap.h> 31 #include <sys/x_call.h> 32 #include <sys/atomic.h> 33 #include <sys/machsystm.h> 34 35 static void 36 dtrace_xcall_func(uint64_t arg1, uint64_t arg2) 37 { 38 (*(dtrace_xcall_t)arg1)((void *)(arg2)); 39 } 40 41 void 42 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg) 43 { 44 if (cpu == DTRACE_CPUALL) { 45 xc_all(dtrace_xcall_func, (uint64_t)func, (uint64_t)arg); 46 } else { 47 xc_one(cpu, dtrace_xcall_func, (uint64_t)func, (uint64_t)arg); 48 } 49 } 50 51 /*ARGSUSED*/ 52 static void 53 dtrace_sync_func(uint64_t arg1, uint64_t arg2) 54 { 55 membar_consumer(); 56 } 57 58 void 59 dtrace_sync(void) 60 { 61 membar_producer(); 62 xc_all(dtrace_sync_func, 0, 0); 63 } 64 65 void 66 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit)) 67 { 68 (*func)(PIOMAPBASE, PIOMAPBASE + PIOMAPSIZE); 69 (*func)(OFW_START_ADDR, OFW_END_ADDR); 70 71 if (hole_end > hole_start) 72 (*func)((uintptr_t)hole_start, (uintptr_t)hole_end); 73 } 74 75 int (*dtrace_fasttrap_probe_ptr)(struct regs *); 76 77 void 78 dtrace_fasttrap_probe(struct regs *rp) 79 { 80 krwlock_t *rwp = &CPU->cpu_ft_lock; 81 82 rw_enter(rwp, RW_READER); 83 if (dtrace_fasttrap_probe_ptr == NULL) { 84 rw_exit(rwp); 85 rp->r_pc = rp->r_npc; 86 rp->r_npc = rp->r_pc + 4; 87 } else { 88 (void) (*dtrace_fasttrap_probe_ptr)(rp); 89 rw_exit(rwp); 90 } 91 } 92 93 int (*dtrace_pid_probe_ptr)(struct regs *); 94 95 void 96 dtrace_pid_probe(struct regs *rp) 97 { 98 krwlock_t *rwp = &CPU->cpu_ft_lock; 99 uint32_t instr; 100 101 /* 102 * This trap should only be invoked if there's a corresponding 103 * enabled dtrace probe. If there isn't, send SIGILL as though 104 * the process had executed an invalid trap instruction. 105 */ 106 rw_enter(rwp, RW_READER); 107 if (dtrace_pid_probe_ptr != NULL && (*dtrace_pid_probe_ptr)(rp) == 0) { 108 rw_exit(rwp); 109 return; 110 } 111 rw_exit(rwp); 112 113 /* 114 * It is possible that we were preempted after entering the kernel, 115 * and the tracepoint was removed. If it appears that the process hit 116 * our reserved trap instruction, we call send SIGILL just as though 117 * the user had executed an unused trap instruction. 118 */ 119 if (fuword32((void *)rp->r_pc, &instr) != 0 || 120 instr == FASTTRAP_INSTR) { 121 sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP); 122 proc_t *p = curproc; 123 124 sqp->sq_info.si_signo = SIGILL; 125 sqp->sq_info.si_code = ILL_ILLTRP; 126 sqp->sq_info.si_addr = (caddr_t)rp->r_pc; 127 sqp->sq_info.si_trapno = 0x38; 128 129 mutex_enter(&p->p_lock); 130 sigaddqa(p, curthread, sqp); 131 mutex_exit(&p->p_lock); 132 aston(curthread); 133 } 134 } 135 136 int (*dtrace_return_probe_ptr)(struct regs *); 137 138 void 139 dtrace_return_probe(struct regs *rp) 140 { 141 krwlock_t *rwp; 142 uintptr_t npc = curthread->t_dtrace_npc; 143 uint8_t on = curthread->t_dtrace_on; 144 uint8_t step = curthread->t_dtrace_step; 145 uint8_t ret = curthread->t_dtrace_ret; 146 147 if (curthread->t_dtrace_ast) { 148 aston(curthread); 149 curthread->t_sig_check = 1; 150 } 151 152 /* 153 * Clear all user tracing flags. 154 */ 155 curthread->t_dtrace_ft = 0; 156 157 /* 158 * If we weren't expecting to take a return probe trap, kill the 159 * process as though it had just executed an unassigned trap 160 * instruction. 161 */ 162 if (step == 0) { 163 ASSERT(on != 0); 164 tsignal(curthread, SIGILL); 165 return; 166 } 167 168 ASSERT(rp->r_npc == rp->r_pc + 4); 169 170 /* 171 * If we hit this trap unrelated to a return probe, we're just here 172 * to reset the AST flag since we deferred a signal until after we 173 * logically single-stepped the instruction we copied out. 174 */ 175 if (ret == 0) { 176 rp->r_pc = npc; 177 rp->r_npc = npc + 4; 178 return; 179 } 180 181 /* 182 * We need to wait until after we've called the dtrace_return_probe_ptr 183 * function pointer to set %pc and %npc. 184 */ 185 rwp = &CPU->cpu_ft_lock; 186 rw_enter(rwp, RW_READER); 187 if (dtrace_return_probe_ptr != NULL) 188 (void) (*dtrace_return_probe_ptr)(rp); 189 rw_exit(rwp); 190 rp->r_pc = npc; 191 rp->r_npc = npc + 4; 192 } 193 194 void 195 dtrace_safe_synchronous_signal(void) 196 { 197 kthread_t *t = curthread; 198 struct regs *rp = lwptoregs(ttolwp(t)); 199 200 ASSERT(t->t_dtrace_on); 201 202 /* 203 * If we're not actively tracing an instruction, turn off tracing 204 * flags. If the instruction we copied out caused a synchronous 205 * trap, reset the pc and npc back to their original values and turn 206 * off the flags. 207 */ 208 if (rp->r_pc != t->t_dtrace_scrpc && rp->r_pc != t->t_dtrace_astpc && 209 rp->r_npc != t->t_dtrace_astpc) { 210 t->t_dtrace_ft = 0; 211 } else if (rp->r_pc == t->t_dtrace_scrpc) { 212 rp->r_pc = t->t_dtrace_pc; 213 rp->r_npc = t->t_dtrace_npc; 214 t->t_dtrace_ft = 0; 215 } 216 } 217 218 int 219 dtrace_safe_defer_signal(void) 220 { 221 kthread_t *t = curthread; 222 struct regs *rp = lwptoregs(ttolwp(t)); 223 224 ASSERT(t->t_dtrace_on); 225 226 /* 227 * If we're not actively tracing an instruction, turn off tracing 228 * flags. 229 */ 230 if (rp->r_pc != t->t_dtrace_scrpc && rp->r_pc != t->t_dtrace_astpc && 231 rp->r_npc != t->t_dtrace_astpc) { 232 t->t_dtrace_ft = 0; 233 return (0); 234 } 235 236 /* 237 * Otherwise, make sure we'll return to the kernel after executing 238 * the instruction we copied out. 239 */ 240 if (!t->t_dtrace_step) { 241 ASSERT(rp->r_pc == t->t_dtrace_scrpc); 242 rp->r_npc = t->t_dtrace_astpc; 243 t->t_dtrace_step = 1; 244 } 245 246 t->t_dtrace_ast = 1; 247 248 return (1); 249 } 250