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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 * 21 * Portions Copyright 2010 The FreeBSD Foundation 22 * 23 * $FreeBSD$ 24 */ 25 26 /* 27 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 28 * Use is subject to license terms. 29 */ 30 31 #ifdef illumos 32 #pragma ident "%Z%%M% %I% %E% SMI" 33 #endif 34 35 #include <sys/fasttrap_isa.h> 36 #include <sys/fasttrap_impl.h> 37 #include <sys/dtrace.h> 38 #include <sys/dtrace_impl.h> 39 #include <sys/cmn_err.h> 40 #ifdef illumos 41 #include <sys/regset.h> 42 #include <sys/privregs.h> 43 #include <sys/segments.h> 44 #include <sys/x86_archext.h> 45 #else 46 #include <sys/types.h> 47 #include <sys/dtrace_bsd.h> 48 #include <sys/proc.h> 49 #include <sys/rmlock.h> 50 #include <cddl/dev/dtrace/dtrace_cddl.h> 51 #include <cddl/dev/dtrace/x86/regset.h> 52 #include <machine/segments.h> 53 #include <machine/reg.h> 54 #include <machine/pcb.h> 55 #endif 56 #include <sys/sysmacros.h> 57 #ifdef illumos 58 #include <sys/trap.h> 59 #include <sys/archsystm.h> 60 #else 61 #include <sys/ptrace.h> 62 #endif /* illumos */ 63 64 #ifdef __i386__ 65 #define r_rax r_eax 66 #define r_rbx r_ebx 67 #define r_rip r_eip 68 #define r_rflags r_eflags 69 #define r_rsp r_esp 70 #define r_rbp r_ebp 71 #endif 72 73 /* 74 * Lossless User-Land Tracing on x86 75 * --------------------------------- 76 * 77 * The execution of most instructions is not dependent on the address; for 78 * these instructions it is sufficient to copy them into the user process's 79 * address space and execute them. To effectively single-step an instruction 80 * in user-land, we copy out the following sequence of instructions to scratch 81 * space in the user thread's ulwp_t structure. 82 * 83 * We then set the program counter (%eip or %rip) to point to this scratch 84 * space. Once execution resumes, the original instruction is executed and 85 * then control flow is redirected to what was originally the subsequent 86 * instruction. If the kernel attemps to deliver a signal while single- 87 * stepping, the signal is deferred and the program counter is moved into the 88 * second sequence of instructions. The second sequence ends in a trap into 89 * the kernel where the deferred signal is then properly handled and delivered. 90 * 91 * For instructions whose execute is position dependent, we perform simple 92 * emulation. These instructions are limited to control transfer 93 * instructions in 32-bit mode, but in 64-bit mode there's the added wrinkle 94 * of %rip-relative addressing that means that almost any instruction can be 95 * position dependent. For all the details on how we emulate generic 96 * instructions included %rip-relative instructions, see the code in 97 * fasttrap_pid_probe() below where we handle instructions of type 98 * FASTTRAP_T_COMMON (under the header: Generic Instruction Tracing). 99 */ 100 101 #define FASTTRAP_MODRM_MOD(modrm) (((modrm) >> 6) & 0x3) 102 #define FASTTRAP_MODRM_REG(modrm) (((modrm) >> 3) & 0x7) 103 #define FASTTRAP_MODRM_RM(modrm) ((modrm) & 0x7) 104 #define FASTTRAP_MODRM(mod, reg, rm) (((mod) << 6) | ((reg) << 3) | (rm)) 105 106 #define FASTTRAP_SIB_SCALE(sib) (((sib) >> 6) & 0x3) 107 #define FASTTRAP_SIB_INDEX(sib) (((sib) >> 3) & 0x7) 108 #define FASTTRAP_SIB_BASE(sib) ((sib) & 0x7) 109 110 #define FASTTRAP_REX_W(rex) (((rex) >> 3) & 1) 111 #define FASTTRAP_REX_R(rex) (((rex) >> 2) & 1) 112 #define FASTTRAP_REX_X(rex) (((rex) >> 1) & 1) 113 #define FASTTRAP_REX_B(rex) ((rex) & 1) 114 #define FASTTRAP_REX(w, r, x, b) \ 115 (0x40 | ((w) << 3) | ((r) << 2) | ((x) << 1) | (b)) 116 117 /* 118 * Single-byte op-codes. 119 */ 120 #define FASTTRAP_PUSHL_EBP 0x55 121 122 #define FASTTRAP_JO 0x70 123 #define FASTTRAP_JNO 0x71 124 #define FASTTRAP_JB 0x72 125 #define FASTTRAP_JAE 0x73 126 #define FASTTRAP_JE 0x74 127 #define FASTTRAP_JNE 0x75 128 #define FASTTRAP_JBE 0x76 129 #define FASTTRAP_JA 0x77 130 #define FASTTRAP_JS 0x78 131 #define FASTTRAP_JNS 0x79 132 #define FASTTRAP_JP 0x7a 133 #define FASTTRAP_JNP 0x7b 134 #define FASTTRAP_JL 0x7c 135 #define FASTTRAP_JGE 0x7d 136 #define FASTTRAP_JLE 0x7e 137 #define FASTTRAP_JG 0x7f 138 139 #define FASTTRAP_NOP 0x90 140 141 #define FASTTRAP_MOV_EAX 0xb8 142 #define FASTTRAP_MOV_ECX 0xb9 143 144 #define FASTTRAP_RET16 0xc2 145 #define FASTTRAP_RET 0xc3 146 147 #define FASTTRAP_LOOPNZ 0xe0 148 #define FASTTRAP_LOOPZ 0xe1 149 #define FASTTRAP_LOOP 0xe2 150 #define FASTTRAP_JCXZ 0xe3 151 152 #define FASTTRAP_CALL 0xe8 153 #define FASTTRAP_JMP32 0xe9 154 #define FASTTRAP_JMP8 0xeb 155 156 #define FASTTRAP_INT3 0xcc 157 #define FASTTRAP_INT 0xcd 158 159 #define FASTTRAP_2_BYTE_OP 0x0f 160 #define FASTTRAP_GROUP5_OP 0xff 161 162 /* 163 * Two-byte op-codes (second byte only). 164 */ 165 #define FASTTRAP_0F_JO 0x80 166 #define FASTTRAP_0F_JNO 0x81 167 #define FASTTRAP_0F_JB 0x82 168 #define FASTTRAP_0F_JAE 0x83 169 #define FASTTRAP_0F_JE 0x84 170 #define FASTTRAP_0F_JNE 0x85 171 #define FASTTRAP_0F_JBE 0x86 172 #define FASTTRAP_0F_JA 0x87 173 #define FASTTRAP_0F_JS 0x88 174 #define FASTTRAP_0F_JNS 0x89 175 #define FASTTRAP_0F_JP 0x8a 176 #define FASTTRAP_0F_JNP 0x8b 177 #define FASTTRAP_0F_JL 0x8c 178 #define FASTTRAP_0F_JGE 0x8d 179 #define FASTTRAP_0F_JLE 0x8e 180 #define FASTTRAP_0F_JG 0x8f 181 182 #define FASTTRAP_EFLAGS_OF 0x800 183 #define FASTTRAP_EFLAGS_DF 0x400 184 #define FASTTRAP_EFLAGS_SF 0x080 185 #define FASTTRAP_EFLAGS_ZF 0x040 186 #define FASTTRAP_EFLAGS_AF 0x010 187 #define FASTTRAP_EFLAGS_PF 0x004 188 #define FASTTRAP_EFLAGS_CF 0x001 189 190 /* 191 * Instruction prefixes. 192 */ 193 #define FASTTRAP_PREFIX_OPERAND 0x66 194 #define FASTTRAP_PREFIX_ADDRESS 0x67 195 #define FASTTRAP_PREFIX_CS 0x2E 196 #define FASTTRAP_PREFIX_DS 0x3E 197 #define FASTTRAP_PREFIX_ES 0x26 198 #define FASTTRAP_PREFIX_FS 0x64 199 #define FASTTRAP_PREFIX_GS 0x65 200 #define FASTTRAP_PREFIX_SS 0x36 201 #define FASTTRAP_PREFIX_LOCK 0xF0 202 #define FASTTRAP_PREFIX_REP 0xF3 203 #define FASTTRAP_PREFIX_REPNE 0xF2 204 205 #define FASTTRAP_NOREG 0xff 206 207 /* 208 * Map between instruction register encodings and the kernel constants which 209 * correspond to indicies into struct regs. 210 */ 211 #ifdef __amd64 212 static const uint8_t regmap[16] = { 213 REG_RAX, REG_RCX, REG_RDX, REG_RBX, REG_RSP, REG_RBP, REG_RSI, REG_RDI, 214 REG_R8, REG_R9, REG_R10, REG_R11, REG_R12, REG_R13, REG_R14, REG_R15, 215 }; 216 #else 217 static const uint8_t regmap[8] = { 218 EAX, ECX, EDX, EBX, UESP, EBP, ESI, EDI 219 }; 220 #endif 221 222 static ulong_t fasttrap_getreg(struct reg *, uint_t); 223 224 static uint64_t 225 fasttrap_anarg(struct reg *rp, int function_entry, int argno) 226 { 227 uint64_t value = 0; 228 int shift = function_entry ? 1 : 0; 229 230 #ifdef __amd64 231 if (curproc->p_model == DATAMODEL_LP64) { 232 uintptr_t *stack; 233 234 /* 235 * In 64-bit mode, the first six arguments are stored in 236 * registers. 237 */ 238 if (argno < 6) 239 switch (argno) { 240 case 0: 241 return (rp->r_rdi); 242 case 1: 243 return (rp->r_rsi); 244 case 2: 245 return (rp->r_rdx); 246 case 3: 247 return (rp->r_rcx); 248 case 4: 249 return (rp->r_r8); 250 case 5: 251 return (rp->r_r9); 252 } 253 254 stack = (uintptr_t *)rp->r_rsp; 255 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 256 value = dtrace_fulword(&stack[argno - 6 + shift]); 257 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR); 258 } else { 259 #endif 260 uint32_t *stack = (uint32_t *)rp->r_rsp; 261 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 262 value = dtrace_fuword32(&stack[argno + shift]); 263 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR); 264 #ifdef __amd64 265 } 266 #endif 267 268 return (value); 269 } 270 271 /*ARGSUSED*/ 272 int 273 fasttrap_tracepoint_init(proc_t *p, fasttrap_tracepoint_t *tp, uintptr_t pc, 274 fasttrap_probe_type_t type) 275 { 276 uint8_t instr[FASTTRAP_MAX_INSTR_SIZE + 10]; 277 size_t len = FASTTRAP_MAX_INSTR_SIZE; 278 size_t first = MIN(len, PAGESIZE - (pc & PAGEOFFSET)); 279 uint_t start = 0; 280 int rmindex, size; 281 uint8_t seg, rex = 0; 282 283 /* 284 * Read the instruction at the given address out of the process's 285 * address space. We don't have to worry about a debugger 286 * changing this instruction before we overwrite it with our trap 287 * instruction since P_PR_LOCK is set. Since instructions can span 288 * pages, we potentially read the instruction in two parts. If the 289 * second part fails, we just zero out that part of the instruction. 290 */ 291 if (uread(p, &instr[0], first, pc) != 0) 292 return (-1); 293 if (len > first && 294 uread(p, &instr[first], len - first, pc + first) != 0) { 295 bzero(&instr[first], len - first); 296 len = first; 297 } 298 299 /* 300 * If the disassembly fails, then we have a malformed instruction. 301 */ 302 if ((size = dtrace_instr_size_isa(instr, p->p_model, &rmindex)) <= 0) 303 return (-1); 304 305 /* 306 * Make sure the disassembler isn't completely broken. 307 */ 308 ASSERT(-1 <= rmindex && rmindex < size); 309 310 /* 311 * If the computed size is greater than the number of bytes read, 312 * then it was a malformed instruction possibly because it fell on a 313 * page boundary and the subsequent page was missing or because of 314 * some malicious user. 315 */ 316 if (size > len) 317 return (-1); 318 319 tp->ftt_size = (uint8_t)size; 320 tp->ftt_segment = FASTTRAP_SEG_NONE; 321 322 /* 323 * Find the start of the instruction's opcode by processing any 324 * legacy prefixes. 325 */ 326 for (;;) { 327 seg = 0; 328 switch (instr[start]) { 329 case FASTTRAP_PREFIX_SS: 330 seg++; 331 /*FALLTHRU*/ 332 case FASTTRAP_PREFIX_GS: 333 seg++; 334 /*FALLTHRU*/ 335 case FASTTRAP_PREFIX_FS: 336 seg++; 337 /*FALLTHRU*/ 338 case FASTTRAP_PREFIX_ES: 339 seg++; 340 /*FALLTHRU*/ 341 case FASTTRAP_PREFIX_DS: 342 seg++; 343 /*FALLTHRU*/ 344 case FASTTRAP_PREFIX_CS: 345 seg++; 346 /*FALLTHRU*/ 347 case FASTTRAP_PREFIX_OPERAND: 348 case FASTTRAP_PREFIX_ADDRESS: 349 case FASTTRAP_PREFIX_LOCK: 350 case FASTTRAP_PREFIX_REP: 351 case FASTTRAP_PREFIX_REPNE: 352 if (seg != 0) { 353 /* 354 * It's illegal for an instruction to specify 355 * two segment prefixes -- give up on this 356 * illegal instruction. 357 */ 358 if (tp->ftt_segment != FASTTRAP_SEG_NONE) 359 return (-1); 360 361 tp->ftt_segment = seg; 362 } 363 start++; 364 continue; 365 } 366 break; 367 } 368 369 #ifdef __amd64 370 /* 371 * Identify the REX prefix on 64-bit processes. 372 */ 373 if (p->p_model == DATAMODEL_LP64 && (instr[start] & 0xf0) == 0x40) 374 rex = instr[start++]; 375 #endif 376 377 /* 378 * Now that we're pretty sure that the instruction is okay, copy the 379 * valid part to the tracepoint. 380 */ 381 bcopy(instr, tp->ftt_instr, FASTTRAP_MAX_INSTR_SIZE); 382 383 tp->ftt_type = FASTTRAP_T_COMMON; 384 if (instr[start] == FASTTRAP_2_BYTE_OP) { 385 switch (instr[start + 1]) { 386 case FASTTRAP_0F_JO: 387 case FASTTRAP_0F_JNO: 388 case FASTTRAP_0F_JB: 389 case FASTTRAP_0F_JAE: 390 case FASTTRAP_0F_JE: 391 case FASTTRAP_0F_JNE: 392 case FASTTRAP_0F_JBE: 393 case FASTTRAP_0F_JA: 394 case FASTTRAP_0F_JS: 395 case FASTTRAP_0F_JNS: 396 case FASTTRAP_0F_JP: 397 case FASTTRAP_0F_JNP: 398 case FASTTRAP_0F_JL: 399 case FASTTRAP_0F_JGE: 400 case FASTTRAP_0F_JLE: 401 case FASTTRAP_0F_JG: 402 tp->ftt_type = FASTTRAP_T_JCC; 403 tp->ftt_code = (instr[start + 1] & 0x0f) | FASTTRAP_JO; 404 tp->ftt_dest = pc + tp->ftt_size + 405 /* LINTED - alignment */ 406 *(int32_t *)&instr[start + 2]; 407 break; 408 } 409 } else if (instr[start] == FASTTRAP_GROUP5_OP) { 410 uint_t mod = FASTTRAP_MODRM_MOD(instr[start + 1]); 411 uint_t reg = FASTTRAP_MODRM_REG(instr[start + 1]); 412 uint_t rm = FASTTRAP_MODRM_RM(instr[start + 1]); 413 414 if (reg == 2 || reg == 4) { 415 uint_t i, sz; 416 417 if (reg == 2) 418 tp->ftt_type = FASTTRAP_T_CALL; 419 else 420 tp->ftt_type = FASTTRAP_T_JMP; 421 422 if (mod == 3) 423 tp->ftt_code = 2; 424 else 425 tp->ftt_code = 1; 426 427 ASSERT(p->p_model == DATAMODEL_LP64 || rex == 0); 428 429 /* 430 * See AMD x86-64 Architecture Programmer's Manual 431 * Volume 3, Section 1.2.7, Table 1-12, and 432 * Appendix A.3.1, Table A-15. 433 */ 434 if (mod != 3 && rm == 4) { 435 uint8_t sib = instr[start + 2]; 436 uint_t index = FASTTRAP_SIB_INDEX(sib); 437 uint_t base = FASTTRAP_SIB_BASE(sib); 438 439 tp->ftt_scale = FASTTRAP_SIB_SCALE(sib); 440 441 tp->ftt_index = (index == 4) ? 442 FASTTRAP_NOREG : 443 regmap[index | (FASTTRAP_REX_X(rex) << 3)]; 444 tp->ftt_base = (mod == 0 && base == 5) ? 445 FASTTRAP_NOREG : 446 regmap[base | (FASTTRAP_REX_B(rex) << 3)]; 447 448 i = 3; 449 sz = mod == 1 ? 1 : 4; 450 } else { 451 /* 452 * In 64-bit mode, mod == 0 and r/m == 5 453 * denotes %rip-relative addressing; in 32-bit 454 * mode, the base register isn't used. In both 455 * modes, there is a 32-bit operand. 456 */ 457 if (mod == 0 && rm == 5) { 458 #ifdef __amd64 459 if (p->p_model == DATAMODEL_LP64) 460 tp->ftt_base = REG_RIP; 461 else 462 #endif 463 tp->ftt_base = FASTTRAP_NOREG; 464 sz = 4; 465 } else { 466 uint8_t base = rm | 467 (FASTTRAP_REX_B(rex) << 3); 468 469 tp->ftt_base = regmap[base]; 470 sz = mod == 1 ? 1 : mod == 2 ? 4 : 0; 471 } 472 tp->ftt_index = FASTTRAP_NOREG; 473 i = 2; 474 } 475 476 if (sz == 1) { 477 tp->ftt_dest = *(int8_t *)&instr[start + i]; 478 } else if (sz == 4) { 479 /* LINTED - alignment */ 480 tp->ftt_dest = *(int32_t *)&instr[start + i]; 481 } else { 482 tp->ftt_dest = 0; 483 } 484 } 485 } else { 486 switch (instr[start]) { 487 case FASTTRAP_RET: 488 tp->ftt_type = FASTTRAP_T_RET; 489 break; 490 491 case FASTTRAP_RET16: 492 tp->ftt_type = FASTTRAP_T_RET16; 493 /* LINTED - alignment */ 494 tp->ftt_dest = *(uint16_t *)&instr[start + 1]; 495 break; 496 497 case FASTTRAP_JO: 498 case FASTTRAP_JNO: 499 case FASTTRAP_JB: 500 case FASTTRAP_JAE: 501 case FASTTRAP_JE: 502 case FASTTRAP_JNE: 503 case FASTTRAP_JBE: 504 case FASTTRAP_JA: 505 case FASTTRAP_JS: 506 case FASTTRAP_JNS: 507 case FASTTRAP_JP: 508 case FASTTRAP_JNP: 509 case FASTTRAP_JL: 510 case FASTTRAP_JGE: 511 case FASTTRAP_JLE: 512 case FASTTRAP_JG: 513 tp->ftt_type = FASTTRAP_T_JCC; 514 tp->ftt_code = instr[start]; 515 tp->ftt_dest = pc + tp->ftt_size + 516 (int8_t)instr[start + 1]; 517 break; 518 519 case FASTTRAP_LOOPNZ: 520 case FASTTRAP_LOOPZ: 521 case FASTTRAP_LOOP: 522 tp->ftt_type = FASTTRAP_T_LOOP; 523 tp->ftt_code = instr[start]; 524 tp->ftt_dest = pc + tp->ftt_size + 525 (int8_t)instr[start + 1]; 526 break; 527 528 case FASTTRAP_JCXZ: 529 tp->ftt_type = FASTTRAP_T_JCXZ; 530 tp->ftt_dest = pc + tp->ftt_size + 531 (int8_t)instr[start + 1]; 532 break; 533 534 case FASTTRAP_CALL: 535 tp->ftt_type = FASTTRAP_T_CALL; 536 tp->ftt_dest = pc + tp->ftt_size + 537 /* LINTED - alignment */ 538 *(int32_t *)&instr[start + 1]; 539 tp->ftt_code = 0; 540 break; 541 542 case FASTTRAP_JMP32: 543 tp->ftt_type = FASTTRAP_T_JMP; 544 tp->ftt_dest = pc + tp->ftt_size + 545 /* LINTED - alignment */ 546 *(int32_t *)&instr[start + 1]; 547 break; 548 case FASTTRAP_JMP8: 549 tp->ftt_type = FASTTRAP_T_JMP; 550 tp->ftt_dest = pc + tp->ftt_size + 551 (int8_t)instr[start + 1]; 552 break; 553 554 case FASTTRAP_PUSHL_EBP: 555 if (start == 0) 556 tp->ftt_type = FASTTRAP_T_PUSHL_EBP; 557 break; 558 559 case FASTTRAP_NOP: 560 #ifdef __amd64 561 ASSERT(p->p_model == DATAMODEL_LP64 || rex == 0); 562 563 /* 564 * On amd64 we have to be careful not to confuse a nop 565 * (actually xchgl %eax, %eax) with an instruction using 566 * the same opcode, but that does something different 567 * (e.g. xchgl %r8d, %eax or xcghq %r8, %rax). 568 */ 569 if (FASTTRAP_REX_B(rex) == 0) 570 #endif 571 tp->ftt_type = FASTTRAP_T_NOP; 572 break; 573 574 case FASTTRAP_INT3: 575 /* 576 * The pid provider shares the int3 trap with debugger 577 * breakpoints so we can't instrument them. 578 */ 579 ASSERT(instr[start] == FASTTRAP_INSTR); 580 return (-1); 581 582 case FASTTRAP_INT: 583 /* 584 * Interrupts seem like they could be traced with 585 * no negative implications, but it's possible that 586 * a thread could be redirected by the trap handling 587 * code which would eventually return to the 588 * instruction after the interrupt. If the interrupt 589 * were in our scratch space, the subsequent 590 * instruction might be overwritten before we return. 591 * Accordingly we refuse to instrument any interrupt. 592 */ 593 return (-1); 594 } 595 } 596 597 #ifdef __amd64 598 if (p->p_model == DATAMODEL_LP64 && tp->ftt_type == FASTTRAP_T_COMMON) { 599 /* 600 * If the process is 64-bit and the instruction type is still 601 * FASTTRAP_T_COMMON -- meaning we're going to copy it out an 602 * execute it -- we need to watch for %rip-relative 603 * addressing mode. See the portion of fasttrap_pid_probe() 604 * below where we handle tracepoints with type 605 * FASTTRAP_T_COMMON for how we emulate instructions that 606 * employ %rip-relative addressing. 607 */ 608 if (rmindex != -1) { 609 uint_t mod = FASTTRAP_MODRM_MOD(instr[rmindex]); 610 uint_t reg = FASTTRAP_MODRM_REG(instr[rmindex]); 611 uint_t rm = FASTTRAP_MODRM_RM(instr[rmindex]); 612 613 ASSERT(rmindex > start); 614 615 if (mod == 0 && rm == 5) { 616 /* 617 * We need to be sure to avoid other 618 * registers used by this instruction. While 619 * the reg field may determine the op code 620 * rather than denoting a register, assuming 621 * that it denotes a register is always safe. 622 * We leave the REX field intact and use 623 * whatever value's there for simplicity. 624 */ 625 if (reg != 0) { 626 tp->ftt_ripmode = FASTTRAP_RIP_1 | 627 (FASTTRAP_RIP_X * 628 FASTTRAP_REX_B(rex)); 629 rm = 0; 630 } else { 631 tp->ftt_ripmode = FASTTRAP_RIP_2 | 632 (FASTTRAP_RIP_X * 633 FASTTRAP_REX_B(rex)); 634 rm = 1; 635 } 636 637 tp->ftt_modrm = tp->ftt_instr[rmindex]; 638 tp->ftt_instr[rmindex] = 639 FASTTRAP_MODRM(2, reg, rm); 640 } 641 } 642 } 643 #endif 644 645 return (0); 646 } 647 648 int 649 fasttrap_tracepoint_install(proc_t *p, fasttrap_tracepoint_t *tp) 650 { 651 fasttrap_instr_t instr = FASTTRAP_INSTR; 652 653 if (uwrite(p, &instr, 1, tp->ftt_pc) != 0) 654 return (-1); 655 656 return (0); 657 } 658 659 int 660 fasttrap_tracepoint_remove(proc_t *p, fasttrap_tracepoint_t *tp) 661 { 662 uint8_t instr; 663 664 /* 665 * Distinguish between read or write failures and a changed 666 * instruction. 667 */ 668 if (uread(p, &instr, 1, tp->ftt_pc) != 0) 669 return (0); 670 if (instr != FASTTRAP_INSTR) 671 return (0); 672 if (uwrite(p, &tp->ftt_instr[0], 1, tp->ftt_pc) != 0) 673 return (-1); 674 675 return (0); 676 } 677 678 #ifdef __amd64 679 static uintptr_t 680 fasttrap_fulword_noerr(const void *uaddr) 681 { 682 uintptr_t ret; 683 684 if ((ret = fasttrap_fulword(uaddr)) != -1) 685 return (ret); 686 687 return (0); 688 } 689 #endif 690 691 static uint32_t 692 fasttrap_fuword32_noerr(const void *uaddr) 693 { 694 uint32_t ret; 695 696 if ((ret = fasttrap_fuword32(uaddr)) != -1) 697 return (ret); 698 699 return (0); 700 } 701 702 static void 703 fasttrap_return_common(struct reg *rp, uintptr_t pc, pid_t pid, 704 uintptr_t new_pc) 705 { 706 fasttrap_tracepoint_t *tp; 707 fasttrap_bucket_t *bucket; 708 fasttrap_id_t *id; 709 #ifdef illumos 710 kmutex_t *pid_mtx; 711 712 pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock; 713 mutex_enter(pid_mtx); 714 #else 715 struct rm_priotracker tracker; 716 717 rm_rlock(&fasttrap_tp_lock, &tracker); 718 #endif 719 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 720 721 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 722 if (pid == tp->ftt_pid && pc == tp->ftt_pc && 723 tp->ftt_proc->ftpc_acount != 0) 724 break; 725 } 726 727 /* 728 * Don't sweat it if we can't find the tracepoint again; unlike 729 * when we're in fasttrap_pid_probe(), finding the tracepoint here 730 * is not essential to the correct execution of the process. 731 */ 732 if (tp == NULL) { 733 #ifdef illumos 734 mutex_exit(pid_mtx); 735 #else 736 rm_runlock(&fasttrap_tp_lock, &tracker); 737 #endif 738 return; 739 } 740 741 for (id = tp->ftt_retids; id != NULL; id = id->fti_next) { 742 /* 743 * If there's a branch that could act as a return site, we 744 * need to trace it, and check here if the program counter is 745 * external to the function. 746 */ 747 if (tp->ftt_type != FASTTRAP_T_RET && 748 tp->ftt_type != FASTTRAP_T_RET16 && 749 new_pc - id->fti_probe->ftp_faddr < 750 id->fti_probe->ftp_fsize) 751 continue; 752 753 dtrace_probe(id->fti_probe->ftp_id, 754 pc - id->fti_probe->ftp_faddr, 755 rp->r_rax, rp->r_rbx, 0, 0); 756 } 757 758 #ifdef illumos 759 mutex_exit(pid_mtx); 760 #else 761 rm_runlock(&fasttrap_tp_lock, &tracker); 762 #endif 763 } 764 765 static void 766 fasttrap_sigsegv(proc_t *p, kthread_t *t, uintptr_t addr) 767 { 768 #ifdef illumos 769 sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP); 770 771 sqp->sq_info.si_signo = SIGSEGV; 772 sqp->sq_info.si_code = SEGV_MAPERR; 773 sqp->sq_info.si_addr = (caddr_t)addr; 774 775 mutex_enter(&p->p_lock); 776 sigaddqa(p, t, sqp); 777 mutex_exit(&p->p_lock); 778 779 if (t != NULL) 780 aston(t); 781 #else 782 ksiginfo_t *ksi = kmem_zalloc(sizeof (ksiginfo_t), KM_SLEEP); 783 784 ksiginfo_init(ksi); 785 ksi->ksi_signo = SIGSEGV; 786 ksi->ksi_code = SEGV_MAPERR; 787 ksi->ksi_addr = (caddr_t)addr; 788 (void) tdksignal(t, SIGSEGV, ksi); 789 #endif 790 } 791 792 #ifdef __amd64 793 static void 794 fasttrap_usdt_args64(fasttrap_probe_t *probe, struct reg *rp, int argc, 795 uintptr_t *argv) 796 { 797 int i, x, cap = MIN(argc, probe->ftp_nargs); 798 uintptr_t *stack = (uintptr_t *)rp->r_rsp; 799 800 for (i = 0; i < cap; i++) { 801 x = probe->ftp_argmap[i]; 802 803 if (x < 6) 804 argv[i] = (&rp->r_rdi)[x]; 805 else 806 argv[i] = fasttrap_fulword_noerr(&stack[x]); 807 } 808 809 for (; i < argc; i++) { 810 argv[i] = 0; 811 } 812 } 813 #endif 814 815 static void 816 fasttrap_usdt_args32(fasttrap_probe_t *probe, struct reg *rp, int argc, 817 uint32_t *argv) 818 { 819 int i, x, cap = MIN(argc, probe->ftp_nargs); 820 uint32_t *stack = (uint32_t *)rp->r_rsp; 821 822 for (i = 0; i < cap; i++) { 823 x = probe->ftp_argmap[i]; 824 825 argv[i] = fasttrap_fuword32_noerr(&stack[x]); 826 } 827 828 for (; i < argc; i++) { 829 argv[i] = 0; 830 } 831 } 832 833 static int 834 fasttrap_do_seg(fasttrap_tracepoint_t *tp, struct reg *rp, uintptr_t *addr) 835 { 836 proc_t *p = curproc; 837 #ifdef __i386__ 838 struct segment_descriptor *desc; 839 #else 840 struct user_segment_descriptor *desc; 841 #endif 842 uint16_t sel = 0, ndx, type; 843 uintptr_t limit; 844 845 switch (tp->ftt_segment) { 846 case FASTTRAP_SEG_CS: 847 sel = rp->r_cs; 848 break; 849 case FASTTRAP_SEG_DS: 850 sel = rp->r_ds; 851 break; 852 case FASTTRAP_SEG_ES: 853 sel = rp->r_es; 854 break; 855 case FASTTRAP_SEG_FS: 856 sel = rp->r_fs; 857 break; 858 case FASTTRAP_SEG_GS: 859 sel = rp->r_gs; 860 break; 861 case FASTTRAP_SEG_SS: 862 sel = rp->r_ss; 863 break; 864 } 865 866 /* 867 * Make sure the given segment register specifies a user priority 868 * selector rather than a kernel selector. 869 */ 870 if (ISPL(sel) != SEL_UPL) 871 return (-1); 872 873 ndx = IDXSEL(sel); 874 875 /* 876 * Check the bounds and grab the descriptor out of the specified 877 * descriptor table. 878 */ 879 if (ISLDT(sel)) { 880 #ifdef __i386__ 881 if (ndx > p->p_md.md_ldt->ldt_len) 882 return (-1); 883 884 desc = (struct segment_descriptor *) 885 p->p_md.md_ldt[ndx].ldt_base; 886 #else 887 if (ndx > max_ldt_segment) 888 return (-1); 889 890 desc = (struct user_segment_descriptor *) 891 p->p_md.md_ldt[ndx].ldt_base; 892 #endif 893 894 } else { 895 if (ndx >= NGDT) 896 return (-1); 897 898 #ifdef __i386__ 899 desc = &gdt[ndx].sd; 900 #else 901 desc = &gdt[ndx]; 902 #endif 903 } 904 905 /* 906 * The descriptor must have user privilege level and it must be 907 * present in memory. 908 */ 909 if (desc->sd_dpl != SEL_UPL || desc->sd_p != 1) 910 return (-1); 911 912 type = desc->sd_type; 913 914 /* 915 * If the S bit in the type field is not set, this descriptor can 916 * only be used in system context. 917 */ 918 if ((type & 0x10) != 0x10) 919 return (-1); 920 921 limit = USD_GETLIMIT(desc) * (desc->sd_gran ? PAGESIZE : 1); 922 923 if (tp->ftt_segment == FASTTRAP_SEG_CS) { 924 /* 925 * The code/data bit and readable bit must both be set. 926 */ 927 if ((type & 0xa) != 0xa) 928 return (-1); 929 930 if (*addr > limit) 931 return (-1); 932 } else { 933 /* 934 * The code/data bit must be clear. 935 */ 936 if ((type & 0x8) != 0) 937 return (-1); 938 939 /* 940 * If the expand-down bit is clear, we just check the limit as 941 * it would naturally be applied. Otherwise, we need to check 942 * that the address is the range [limit + 1 .. 0xffff] or 943 * [limit + 1 ... 0xffffffff] depending on if the default 944 * operand size bit is set. 945 */ 946 if ((type & 0x4) == 0) { 947 if (*addr > limit) 948 return (-1); 949 } else if (desc->sd_def32) { 950 if (*addr < limit + 1 || 0xffff < *addr) 951 return (-1); 952 } else { 953 if (*addr < limit + 1 || 0xffffffff < *addr) 954 return (-1); 955 } 956 } 957 958 *addr += USD_GETBASE(desc); 959 960 return (0); 961 } 962 963 int 964 fasttrap_pid_probe(struct trapframe *tf) 965 { 966 struct reg reg, *rp; 967 proc_t *p = curproc, *pp; 968 struct rm_priotracker tracker; 969 uintptr_t pc; 970 uintptr_t new_pc = 0; 971 fasttrap_bucket_t *bucket; 972 #ifdef illumos 973 kmutex_t *pid_mtx; 974 #endif 975 fasttrap_tracepoint_t *tp, tp_local; 976 pid_t pid; 977 dtrace_icookie_t cookie; 978 uint_t is_enabled = 0; 979 980 fill_frame_regs(tf, ®); 981 rp = ® 982 983 pc = rp->r_rip - 1; 984 985 /* 986 * It's possible that a user (in a veritable orgy of bad planning) 987 * could redirect this thread's flow of control before it reached the 988 * return probe fasttrap. In this case we need to kill the process 989 * since it's in a unrecoverable state. 990 */ 991 if (curthread->t_dtrace_step) { 992 ASSERT(curthread->t_dtrace_on); 993 fasttrap_sigtrap(p, curthread, pc); 994 return (0); 995 } 996 997 /* 998 * Clear all user tracing flags. 999 */ 1000 curthread->t_dtrace_ft = 0; 1001 curthread->t_dtrace_pc = 0; 1002 curthread->t_dtrace_npc = 0; 1003 curthread->t_dtrace_scrpc = 0; 1004 curthread->t_dtrace_astpc = 0; 1005 #ifdef __amd64 1006 curthread->t_dtrace_regv = 0; 1007 #endif 1008 1009 /* 1010 * Treat a child created by a call to vfork(2) as if it were its 1011 * parent. We know that there's only one thread of control in such a 1012 * process: this one. 1013 */ 1014 #ifdef illumos 1015 while (p->p_flag & SVFORK) { 1016 p = p->p_parent; 1017 } 1018 1019 pid = p->p_pid; 1020 pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock; 1021 mutex_enter(pid_mtx); 1022 #else 1023 pp = p; 1024 sx_slock(&proctree_lock); 1025 while (pp->p_vmspace == pp->p_pptr->p_vmspace) 1026 pp = pp->p_pptr; 1027 pid = pp->p_pid; 1028 sx_sunlock(&proctree_lock); 1029 pp = NULL; 1030 1031 rm_rlock(&fasttrap_tp_lock, &tracker); 1032 #endif 1033 1034 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 1035 1036 /* 1037 * Lookup the tracepoint that the process just hit. 1038 */ 1039 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 1040 if (pid == tp->ftt_pid && pc == tp->ftt_pc && 1041 tp->ftt_proc->ftpc_acount != 0) 1042 break; 1043 } 1044 1045 /* 1046 * If we couldn't find a matching tracepoint, either a tracepoint has 1047 * been inserted without using the pid<pid> ioctl interface (see 1048 * fasttrap_ioctl), or somehow we have mislaid this tracepoint. 1049 */ 1050 if (tp == NULL) { 1051 #ifdef illumos 1052 mutex_exit(pid_mtx); 1053 #else 1054 rm_runlock(&fasttrap_tp_lock, &tracker); 1055 #endif 1056 return (-1); 1057 } 1058 1059 /* 1060 * Set the program counter to the address of the traced instruction 1061 * so that it looks right in ustack() output. 1062 */ 1063 rp->r_rip = pc; 1064 1065 if (tp->ftt_ids != NULL) { 1066 fasttrap_id_t *id; 1067 1068 #ifdef __amd64 1069 if (p->p_model == DATAMODEL_LP64) { 1070 for (id = tp->ftt_ids; id != NULL; id = id->fti_next) { 1071 fasttrap_probe_t *probe = id->fti_probe; 1072 1073 if (id->fti_ptype == DTFTP_ENTRY) { 1074 /* 1075 * We note that this was an entry 1076 * probe to help ustack() find the 1077 * first caller. 1078 */ 1079 cookie = dtrace_interrupt_disable(); 1080 DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY); 1081 dtrace_probe(probe->ftp_id, rp->r_rdi, 1082 rp->r_rsi, rp->r_rdx, rp->r_rcx, 1083 rp->r_r8); 1084 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY); 1085 dtrace_interrupt_enable(cookie); 1086 } else if (id->fti_ptype == DTFTP_IS_ENABLED) { 1087 /* 1088 * Note that in this case, we don't 1089 * call dtrace_probe() since it's only 1090 * an artificial probe meant to change 1091 * the flow of control so that it 1092 * encounters the true probe. 1093 */ 1094 is_enabled = 1; 1095 } else if (probe->ftp_argmap == NULL) { 1096 dtrace_probe(probe->ftp_id, rp->r_rdi, 1097 rp->r_rsi, rp->r_rdx, rp->r_rcx, 1098 rp->r_r8); 1099 } else { 1100 uintptr_t t[5]; 1101 1102 fasttrap_usdt_args64(probe, rp, 1103 sizeof (t) / sizeof (t[0]), t); 1104 1105 dtrace_probe(probe->ftp_id, t[0], t[1], 1106 t[2], t[3], t[4]); 1107 } 1108 } 1109 } else { 1110 #endif 1111 uintptr_t s0, s1, s2, s3, s4, s5; 1112 uint32_t *stack = (uint32_t *)rp->r_rsp; 1113 1114 /* 1115 * In 32-bit mode, all arguments are passed on the 1116 * stack. If this is a function entry probe, we need 1117 * to skip the first entry on the stack as it 1118 * represents the return address rather than a 1119 * parameter to the function. 1120 */ 1121 s0 = fasttrap_fuword32_noerr(&stack[0]); 1122 s1 = fasttrap_fuword32_noerr(&stack[1]); 1123 s2 = fasttrap_fuword32_noerr(&stack[2]); 1124 s3 = fasttrap_fuword32_noerr(&stack[3]); 1125 s4 = fasttrap_fuword32_noerr(&stack[4]); 1126 s5 = fasttrap_fuword32_noerr(&stack[5]); 1127 1128 for (id = tp->ftt_ids; id != NULL; id = id->fti_next) { 1129 fasttrap_probe_t *probe = id->fti_probe; 1130 1131 if (id->fti_ptype == DTFTP_ENTRY) { 1132 /* 1133 * We note that this was an entry 1134 * probe to help ustack() find the 1135 * first caller. 1136 */ 1137 cookie = dtrace_interrupt_disable(); 1138 DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY); 1139 dtrace_probe(probe->ftp_id, s1, s2, 1140 s3, s4, s5); 1141 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY); 1142 dtrace_interrupt_enable(cookie); 1143 } else if (id->fti_ptype == DTFTP_IS_ENABLED) { 1144 /* 1145 * Note that in this case, we don't 1146 * call dtrace_probe() since it's only 1147 * an artificial probe meant to change 1148 * the flow of control so that it 1149 * encounters the true probe. 1150 */ 1151 is_enabled = 1; 1152 } else if (probe->ftp_argmap == NULL) { 1153 dtrace_probe(probe->ftp_id, s0, s1, 1154 s2, s3, s4); 1155 } else { 1156 uint32_t t[5]; 1157 1158 fasttrap_usdt_args32(probe, rp, 1159 sizeof (t) / sizeof (t[0]), t); 1160 1161 dtrace_probe(probe->ftp_id, t[0], t[1], 1162 t[2], t[3], t[4]); 1163 } 1164 } 1165 #ifdef __amd64 1166 } 1167 #endif 1168 } 1169 1170 /* 1171 * We're about to do a bunch of work so we cache a local copy of 1172 * the tracepoint to emulate the instruction, and then find the 1173 * tracepoint again later if we need to light up any return probes. 1174 */ 1175 tp_local = *tp; 1176 #ifdef illumos 1177 mutex_exit(pid_mtx); 1178 #else 1179 rm_runlock(&fasttrap_tp_lock, &tracker); 1180 #endif 1181 tp = &tp_local; 1182 1183 /* 1184 * Set the program counter to appear as though the traced instruction 1185 * had completely executed. This ensures that fasttrap_getreg() will 1186 * report the expected value for REG_RIP. 1187 */ 1188 rp->r_rip = pc + tp->ftt_size; 1189 1190 /* 1191 * If there's an is-enabled probe connected to this tracepoint it 1192 * means that there was a 'xorl %eax, %eax' or 'xorq %rax, %rax' 1193 * instruction that was placed there by DTrace when the binary was 1194 * linked. As this probe is, in fact, enabled, we need to stuff 1 1195 * into %eax or %rax. Accordingly, we can bypass all the instruction 1196 * emulation logic since we know the inevitable result. It's possible 1197 * that a user could construct a scenario where the 'is-enabled' 1198 * probe was on some other instruction, but that would be a rather 1199 * exotic way to shoot oneself in the foot. 1200 */ 1201 if (is_enabled) { 1202 rp->r_rax = 1; 1203 new_pc = rp->r_rip; 1204 goto done; 1205 } 1206 1207 /* 1208 * We emulate certain types of instructions to ensure correctness 1209 * (in the case of position dependent instructions) or optimize 1210 * common cases. The rest we have the thread execute back in user- 1211 * land. 1212 */ 1213 switch (tp->ftt_type) { 1214 case FASTTRAP_T_RET: 1215 case FASTTRAP_T_RET16: 1216 { 1217 uintptr_t dst = 0; 1218 uintptr_t addr = 0; 1219 int ret = 0; 1220 1221 /* 1222 * We have to emulate _every_ facet of the behavior of a ret 1223 * instruction including what happens if the load from %esp 1224 * fails; in that case, we send a SIGSEGV. 1225 */ 1226 #ifdef __amd64 1227 if (p->p_model == DATAMODEL_NATIVE) { 1228 ret = dst = fasttrap_fulword((void *)rp->r_rsp); 1229 addr = rp->r_rsp + sizeof (uintptr_t); 1230 } else { 1231 #endif 1232 uint32_t dst32; 1233 ret = dst32 = fasttrap_fuword32((void *)rp->r_rsp); 1234 dst = dst32; 1235 addr = rp->r_rsp + sizeof (uint32_t); 1236 #ifdef __amd64 1237 } 1238 #endif 1239 1240 if (ret == -1) { 1241 fasttrap_sigsegv(p, curthread, rp->r_rsp); 1242 new_pc = pc; 1243 break; 1244 } 1245 1246 if (tp->ftt_type == FASTTRAP_T_RET16) 1247 addr += tp->ftt_dest; 1248 1249 rp->r_rsp = addr; 1250 new_pc = dst; 1251 break; 1252 } 1253 1254 case FASTTRAP_T_JCC: 1255 { 1256 uint_t taken = 0; 1257 1258 switch (tp->ftt_code) { 1259 case FASTTRAP_JO: 1260 taken = (rp->r_rflags & FASTTRAP_EFLAGS_OF) != 0; 1261 break; 1262 case FASTTRAP_JNO: 1263 taken = (rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0; 1264 break; 1265 case FASTTRAP_JB: 1266 taken = (rp->r_rflags & FASTTRAP_EFLAGS_CF) != 0; 1267 break; 1268 case FASTTRAP_JAE: 1269 taken = (rp->r_rflags & FASTTRAP_EFLAGS_CF) == 0; 1270 break; 1271 case FASTTRAP_JE: 1272 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) != 0; 1273 break; 1274 case FASTTRAP_JNE: 1275 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) == 0; 1276 break; 1277 case FASTTRAP_JBE: 1278 taken = (rp->r_rflags & FASTTRAP_EFLAGS_CF) != 0 || 1279 (rp->r_rflags & FASTTRAP_EFLAGS_ZF) != 0; 1280 break; 1281 case FASTTRAP_JA: 1282 taken = (rp->r_rflags & FASTTRAP_EFLAGS_CF) == 0 && 1283 (rp->r_rflags & FASTTRAP_EFLAGS_ZF) == 0; 1284 break; 1285 case FASTTRAP_JS: 1286 taken = (rp->r_rflags & FASTTRAP_EFLAGS_SF) != 0; 1287 break; 1288 case FASTTRAP_JNS: 1289 taken = (rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0; 1290 break; 1291 case FASTTRAP_JP: 1292 taken = (rp->r_rflags & FASTTRAP_EFLAGS_PF) != 0; 1293 break; 1294 case FASTTRAP_JNP: 1295 taken = (rp->r_rflags & FASTTRAP_EFLAGS_PF) == 0; 1296 break; 1297 case FASTTRAP_JL: 1298 taken = ((rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0) != 1299 ((rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0); 1300 break; 1301 case FASTTRAP_JGE: 1302 taken = ((rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0) == 1303 ((rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0); 1304 break; 1305 case FASTTRAP_JLE: 1306 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) != 0 || 1307 ((rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0) != 1308 ((rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0); 1309 break; 1310 case FASTTRAP_JG: 1311 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) == 0 && 1312 ((rp->r_rflags & FASTTRAP_EFLAGS_SF) == 0) == 1313 ((rp->r_rflags & FASTTRAP_EFLAGS_OF) == 0); 1314 break; 1315 1316 } 1317 1318 if (taken) 1319 new_pc = tp->ftt_dest; 1320 else 1321 new_pc = pc + tp->ftt_size; 1322 break; 1323 } 1324 1325 case FASTTRAP_T_LOOP: 1326 { 1327 uint_t taken = 0; 1328 #ifdef __amd64 1329 greg_t cx = rp->r_rcx--; 1330 #else 1331 greg_t cx = rp->r_ecx--; 1332 #endif 1333 1334 switch (tp->ftt_code) { 1335 case FASTTRAP_LOOPNZ: 1336 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) == 0 && 1337 cx != 0; 1338 break; 1339 case FASTTRAP_LOOPZ: 1340 taken = (rp->r_rflags & FASTTRAP_EFLAGS_ZF) != 0 && 1341 cx != 0; 1342 break; 1343 case FASTTRAP_LOOP: 1344 taken = (cx != 0); 1345 break; 1346 } 1347 1348 if (taken) 1349 new_pc = tp->ftt_dest; 1350 else 1351 new_pc = pc + tp->ftt_size; 1352 break; 1353 } 1354 1355 case FASTTRAP_T_JCXZ: 1356 { 1357 #ifdef __amd64 1358 greg_t cx = rp->r_rcx; 1359 #else 1360 greg_t cx = rp->r_ecx; 1361 #endif 1362 1363 if (cx == 0) 1364 new_pc = tp->ftt_dest; 1365 else 1366 new_pc = pc + tp->ftt_size; 1367 break; 1368 } 1369 1370 case FASTTRAP_T_PUSHL_EBP: 1371 { 1372 int ret = 0; 1373 1374 #ifdef __amd64 1375 if (p->p_model == DATAMODEL_NATIVE) { 1376 rp->r_rsp -= sizeof (uintptr_t); 1377 ret = fasttrap_sulword((void *)rp->r_rsp, rp->r_rbp); 1378 } else { 1379 #endif 1380 rp->r_rsp -= sizeof (uint32_t); 1381 ret = fasttrap_suword32((void *)rp->r_rsp, rp->r_rbp); 1382 #ifdef __amd64 1383 } 1384 #endif 1385 1386 if (ret == -1) { 1387 fasttrap_sigsegv(p, curthread, rp->r_rsp); 1388 new_pc = pc; 1389 break; 1390 } 1391 1392 new_pc = pc + tp->ftt_size; 1393 break; 1394 } 1395 1396 case FASTTRAP_T_NOP: 1397 new_pc = pc + tp->ftt_size; 1398 break; 1399 1400 case FASTTRAP_T_JMP: 1401 case FASTTRAP_T_CALL: 1402 if (tp->ftt_code == 0) { 1403 new_pc = tp->ftt_dest; 1404 } else { 1405 uintptr_t value, addr = tp->ftt_dest; 1406 1407 if (tp->ftt_base != FASTTRAP_NOREG) 1408 addr += fasttrap_getreg(rp, tp->ftt_base); 1409 if (tp->ftt_index != FASTTRAP_NOREG) 1410 addr += fasttrap_getreg(rp, tp->ftt_index) << 1411 tp->ftt_scale; 1412 1413 if (tp->ftt_code == 1) { 1414 /* 1415 * If there's a segment prefix for this 1416 * instruction, we'll need to check permissions 1417 * and bounds on the given selector, and adjust 1418 * the address accordingly. 1419 */ 1420 if (tp->ftt_segment != FASTTRAP_SEG_NONE && 1421 fasttrap_do_seg(tp, rp, &addr) != 0) { 1422 fasttrap_sigsegv(p, curthread, addr); 1423 new_pc = pc; 1424 break; 1425 } 1426 1427 #ifdef __amd64 1428 if (p->p_model == DATAMODEL_NATIVE) { 1429 #endif 1430 if ((value = fasttrap_fulword((void *)addr)) 1431 == -1) { 1432 fasttrap_sigsegv(p, curthread, 1433 addr); 1434 new_pc = pc; 1435 break; 1436 } 1437 new_pc = value; 1438 #ifdef __amd64 1439 } else { 1440 uint32_t value32; 1441 addr = (uintptr_t)(uint32_t)addr; 1442 if ((value32 = fasttrap_fuword32((void *)addr)) 1443 == -1) { 1444 fasttrap_sigsegv(p, curthread, 1445 addr); 1446 new_pc = pc; 1447 break; 1448 } 1449 new_pc = value32; 1450 } 1451 #endif 1452 } else { 1453 new_pc = addr; 1454 } 1455 } 1456 1457 /* 1458 * If this is a call instruction, we need to push the return 1459 * address onto the stack. If this fails, we send the process 1460 * a SIGSEGV and reset the pc to emulate what would happen if 1461 * this instruction weren't traced. 1462 */ 1463 if (tp->ftt_type == FASTTRAP_T_CALL) { 1464 int ret = 0; 1465 uintptr_t addr = 0, pcps; 1466 #ifdef __amd64 1467 if (p->p_model == DATAMODEL_NATIVE) { 1468 addr = rp->r_rsp - sizeof (uintptr_t); 1469 pcps = pc + tp->ftt_size; 1470 ret = fasttrap_sulword((void *)addr, pcps); 1471 } else { 1472 #endif 1473 addr = rp->r_rsp - sizeof (uint32_t); 1474 pcps = (uint32_t)(pc + tp->ftt_size); 1475 ret = fasttrap_suword32((void *)addr, pcps); 1476 #ifdef __amd64 1477 } 1478 #endif 1479 1480 if (ret == -1) { 1481 fasttrap_sigsegv(p, curthread, addr); 1482 new_pc = pc; 1483 break; 1484 } 1485 1486 rp->r_rsp = addr; 1487 } 1488 1489 break; 1490 1491 case FASTTRAP_T_COMMON: 1492 { 1493 uintptr_t addr; 1494 #if defined(__amd64) 1495 uint8_t scratch[2 * FASTTRAP_MAX_INSTR_SIZE + 22]; 1496 #else 1497 uint8_t scratch[2 * FASTTRAP_MAX_INSTR_SIZE + 7]; 1498 #endif 1499 uint_t i = 0; 1500 #ifdef illumos 1501 klwp_t *lwp = ttolwp(curthread); 1502 1503 /* 1504 * Compute the address of the ulwp_t and step over the 1505 * ul_self pointer. The method used to store the user-land 1506 * thread pointer is very different on 32- and 64-bit 1507 * kernels. 1508 */ 1509 #if defined(__amd64) 1510 if (p->p_model == DATAMODEL_LP64) { 1511 addr = lwp->lwp_pcb.pcb_fsbase; 1512 addr += sizeof (void *); 1513 } else { 1514 addr = lwp->lwp_pcb.pcb_gsbase; 1515 addr += sizeof (caddr32_t); 1516 } 1517 #else 1518 addr = USD_GETBASE(&lwp->lwp_pcb.pcb_gsdesc); 1519 addr += sizeof (void *); 1520 #endif 1521 #else /* !illumos */ 1522 fasttrap_scrspace_t *scrspace; 1523 scrspace = fasttrap_scraddr(curthread, tp->ftt_proc); 1524 if (scrspace == NULL) { 1525 /* 1526 * We failed to allocate scratch space for this thread. 1527 * Try to write the original instruction back out and 1528 * reset the pc. 1529 */ 1530 if (fasttrap_copyout(tp->ftt_instr, (void *)pc, 1531 tp->ftt_size)) 1532 fasttrap_sigtrap(p, curthread, pc); 1533 new_pc = pc; 1534 break; 1535 } 1536 addr = scrspace->ftss_addr; 1537 #endif /* illumos */ 1538 1539 /* 1540 * Generic Instruction Tracing 1541 * --------------------------- 1542 * 1543 * This is the layout of the scratch space in the user-land 1544 * thread structure for our generated instructions. 1545 * 1546 * 32-bit mode bytes 1547 * ------------------------ ----- 1548 * a: <original instruction> <= 15 1549 * jmp <pc + tp->ftt_size> 5 1550 * b: <original instruction> <= 15 1551 * int T_DTRACE_RET 2 1552 * ----- 1553 * <= 37 1554 * 1555 * 64-bit mode bytes 1556 * ------------------------ ----- 1557 * a: <original instruction> <= 15 1558 * jmp 0(%rip) 6 1559 * <pc + tp->ftt_size> 8 1560 * b: <original instruction> <= 15 1561 * int T_DTRACE_RET 2 1562 * ----- 1563 * <= 46 1564 * 1565 * The %pc is set to a, and curthread->t_dtrace_astpc is set 1566 * to b. If we encounter a signal on the way out of the 1567 * kernel, trap() will set %pc to curthread->t_dtrace_astpc 1568 * so that we execute the original instruction and re-enter 1569 * the kernel rather than redirecting to the next instruction. 1570 * 1571 * If there are return probes (so we know that we're going to 1572 * need to reenter the kernel after executing the original 1573 * instruction), the scratch space will just contain the 1574 * original instruction followed by an interrupt -- the same 1575 * data as at b. 1576 * 1577 * %rip-relative Addressing 1578 * ------------------------ 1579 * 1580 * There's a further complication in 64-bit mode due to %rip- 1581 * relative addressing. While this is clearly a beneficial 1582 * architectural decision for position independent code, it's 1583 * hard not to see it as a personal attack against the pid 1584 * provider since before there was a relatively small set of 1585 * instructions to emulate; with %rip-relative addressing, 1586 * almost every instruction can potentially depend on the 1587 * address at which it's executed. Rather than emulating 1588 * the broad spectrum of instructions that can now be 1589 * position dependent, we emulate jumps and others as in 1590 * 32-bit mode, and take a different tack for instructions 1591 * using %rip-relative addressing. 1592 * 1593 * For every instruction that uses the ModRM byte, the 1594 * in-kernel disassembler reports its location. We use the 1595 * ModRM byte to identify that an instruction uses 1596 * %rip-relative addressing and to see what other registers 1597 * the instruction uses. To emulate those instructions, 1598 * we modify the instruction to be %rax-relative rather than 1599 * %rip-relative (or %rcx-relative if the instruction uses 1600 * %rax; or %r8- or %r9-relative if the REX.B is present so 1601 * we don't have to rewrite the REX prefix). We then load 1602 * the value that %rip would have been into the scratch 1603 * register and generate an instruction to reset the scratch 1604 * register back to its original value. The instruction 1605 * sequence looks like this: 1606 * 1607 * 64-mode %rip-relative bytes 1608 * ------------------------ ----- 1609 * a: <modified instruction> <= 15 1610 * movq $<value>, %<scratch> 6 1611 * jmp 0(%rip) 6 1612 * <pc + tp->ftt_size> 8 1613 * b: <modified instruction> <= 15 1614 * int T_DTRACE_RET 2 1615 * ----- 1616 * 52 1617 * 1618 * We set curthread->t_dtrace_regv so that upon receiving 1619 * a signal we can reset the value of the scratch register. 1620 */ 1621 1622 ASSERT(tp->ftt_size <= FASTTRAP_MAX_INSTR_SIZE); 1623 1624 curthread->t_dtrace_scrpc = addr; 1625 bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size); 1626 i += tp->ftt_size; 1627 1628 #ifdef __amd64 1629 if (tp->ftt_ripmode != 0) { 1630 greg_t *reg = NULL; 1631 1632 ASSERT(p->p_model == DATAMODEL_LP64); 1633 ASSERT(tp->ftt_ripmode & 1634 (FASTTRAP_RIP_1 | FASTTRAP_RIP_2)); 1635 1636 /* 1637 * If this was a %rip-relative instruction, we change 1638 * it to be either a %rax- or %rcx-relative 1639 * instruction (depending on whether those registers 1640 * are used as another operand; or %r8- or %r9- 1641 * relative depending on the value of REX.B). We then 1642 * set that register and generate a movq instruction 1643 * to reset the value. 1644 */ 1645 if (tp->ftt_ripmode & FASTTRAP_RIP_X) 1646 scratch[i++] = FASTTRAP_REX(1, 0, 0, 1); 1647 else 1648 scratch[i++] = FASTTRAP_REX(1, 0, 0, 0); 1649 1650 if (tp->ftt_ripmode & FASTTRAP_RIP_1) 1651 scratch[i++] = FASTTRAP_MOV_EAX; 1652 else 1653 scratch[i++] = FASTTRAP_MOV_ECX; 1654 1655 switch (tp->ftt_ripmode) { 1656 case FASTTRAP_RIP_1: 1657 reg = &rp->r_rax; 1658 curthread->t_dtrace_reg = REG_RAX; 1659 break; 1660 case FASTTRAP_RIP_2: 1661 reg = &rp->r_rcx; 1662 curthread->t_dtrace_reg = REG_RCX; 1663 break; 1664 case FASTTRAP_RIP_1 | FASTTRAP_RIP_X: 1665 reg = &rp->r_r8; 1666 curthread->t_dtrace_reg = REG_R8; 1667 break; 1668 case FASTTRAP_RIP_2 | FASTTRAP_RIP_X: 1669 reg = &rp->r_r9; 1670 curthread->t_dtrace_reg = REG_R9; 1671 break; 1672 } 1673 1674 /* LINTED - alignment */ 1675 *(uint64_t *)&scratch[i] = *reg; 1676 curthread->t_dtrace_regv = *reg; 1677 *reg = pc + tp->ftt_size; 1678 i += sizeof (uint64_t); 1679 } 1680 #endif 1681 1682 /* 1683 * Generate the branch instruction to what would have 1684 * normally been the subsequent instruction. In 32-bit mode, 1685 * this is just a relative branch; in 64-bit mode this is a 1686 * %rip-relative branch that loads the 64-bit pc value 1687 * immediately after the jmp instruction. 1688 */ 1689 #ifdef __amd64 1690 if (p->p_model == DATAMODEL_LP64) { 1691 scratch[i++] = FASTTRAP_GROUP5_OP; 1692 scratch[i++] = FASTTRAP_MODRM(0, 4, 5); 1693 /* LINTED - alignment */ 1694 *(uint32_t *)&scratch[i] = 0; 1695 i += sizeof (uint32_t); 1696 /* LINTED - alignment */ 1697 *(uint64_t *)&scratch[i] = pc + tp->ftt_size; 1698 i += sizeof (uint64_t); 1699 } else { 1700 #endif 1701 /* 1702 * Set up the jmp to the next instruction; note that 1703 * the size of the traced instruction cancels out. 1704 */ 1705 scratch[i++] = FASTTRAP_JMP32; 1706 /* LINTED - alignment */ 1707 *(uint32_t *)&scratch[i] = pc - addr - 5; 1708 i += sizeof (uint32_t); 1709 #ifdef __amd64 1710 } 1711 #endif 1712 1713 curthread->t_dtrace_astpc = addr + i; 1714 bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size); 1715 i += tp->ftt_size; 1716 scratch[i++] = FASTTRAP_INT; 1717 scratch[i++] = T_DTRACE_RET; 1718 1719 ASSERT(i <= sizeof (scratch)); 1720 1721 if (fasttrap_copyout(scratch, (char *)addr, i)) { 1722 fasttrap_sigtrap(p, curthread, pc); 1723 new_pc = pc; 1724 break; 1725 } 1726 if (tp->ftt_retids != NULL) { 1727 curthread->t_dtrace_step = 1; 1728 curthread->t_dtrace_ret = 1; 1729 new_pc = curthread->t_dtrace_astpc; 1730 } else { 1731 new_pc = curthread->t_dtrace_scrpc; 1732 } 1733 1734 curthread->t_dtrace_pc = pc; 1735 curthread->t_dtrace_npc = pc + tp->ftt_size; 1736 curthread->t_dtrace_on = 1; 1737 break; 1738 } 1739 1740 default: 1741 panic("fasttrap: mishandled an instruction"); 1742 } 1743 1744 done: 1745 /* 1746 * If there were no return probes when we first found the tracepoint, 1747 * we should feel no obligation to honor any return probes that were 1748 * subsequently enabled -- they'll just have to wait until the next 1749 * time around. 1750 */ 1751 if (tp->ftt_retids != NULL) { 1752 /* 1753 * We need to wait until the results of the instruction are 1754 * apparent before invoking any return probes. If this 1755 * instruction was emulated we can just call 1756 * fasttrap_return_common(); if it needs to be executed, we 1757 * need to wait until the user thread returns to the kernel. 1758 */ 1759 if (tp->ftt_type != FASTTRAP_T_COMMON) { 1760 /* 1761 * Set the program counter to the address of the traced 1762 * instruction so that it looks right in ustack() 1763 * output. We had previously set it to the end of the 1764 * instruction to simplify %rip-relative addressing. 1765 */ 1766 rp->r_rip = pc; 1767 1768 fasttrap_return_common(rp, pc, pid, new_pc); 1769 } else { 1770 ASSERT(curthread->t_dtrace_ret != 0); 1771 ASSERT(curthread->t_dtrace_pc == pc); 1772 ASSERT(curthread->t_dtrace_scrpc != 0); 1773 ASSERT(new_pc == curthread->t_dtrace_astpc); 1774 } 1775 } 1776 1777 rp->r_rip = new_pc; 1778 1779 #ifndef illumos 1780 PROC_LOCK(p); 1781 proc_write_regs(curthread, rp); 1782 PROC_UNLOCK(p); 1783 #endif 1784 1785 return (0); 1786 } 1787 1788 int 1789 fasttrap_return_probe(struct trapframe *tf) 1790 { 1791 struct reg reg, *rp; 1792 proc_t *p = curproc; 1793 uintptr_t pc = curthread->t_dtrace_pc; 1794 uintptr_t npc = curthread->t_dtrace_npc; 1795 1796 fill_frame_regs(tf, ®); 1797 rp = ® 1798 1799 curthread->t_dtrace_pc = 0; 1800 curthread->t_dtrace_npc = 0; 1801 curthread->t_dtrace_scrpc = 0; 1802 curthread->t_dtrace_astpc = 0; 1803 1804 #ifdef illumos 1805 /* 1806 * Treat a child created by a call to vfork(2) as if it were its 1807 * parent. We know that there's only one thread of control in such a 1808 * process: this one. 1809 */ 1810 while (p->p_flag & SVFORK) { 1811 p = p->p_parent; 1812 } 1813 #endif 1814 1815 /* 1816 * We set rp->r_rip to the address of the traced instruction so 1817 * that it appears to dtrace_probe() that we're on the original 1818 * instruction. 1819 */ 1820 rp->r_rip = pc; 1821 1822 fasttrap_return_common(rp, pc, p->p_pid, npc); 1823 1824 return (0); 1825 } 1826 1827 /*ARGSUSED*/ 1828 uint64_t 1829 fasttrap_pid_getarg(void *arg, dtrace_id_t id, void *parg, int argno, 1830 int aframes) 1831 { 1832 struct reg r; 1833 1834 fill_regs(curthread, &r); 1835 1836 return (fasttrap_anarg(&r, 1, argno)); 1837 } 1838 1839 /*ARGSUSED*/ 1840 uint64_t 1841 fasttrap_usdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno, 1842 int aframes) 1843 { 1844 struct reg r; 1845 1846 fill_regs(curthread, &r); 1847 1848 return (fasttrap_anarg(&r, 0, argno)); 1849 } 1850 1851 static ulong_t 1852 fasttrap_getreg(struct reg *rp, uint_t reg) 1853 { 1854 #ifdef __amd64 1855 switch (reg) { 1856 case REG_R15: return (rp->r_r15); 1857 case REG_R14: return (rp->r_r14); 1858 case REG_R13: return (rp->r_r13); 1859 case REG_R12: return (rp->r_r12); 1860 case REG_R11: return (rp->r_r11); 1861 case REG_R10: return (rp->r_r10); 1862 case REG_R9: return (rp->r_r9); 1863 case REG_R8: return (rp->r_r8); 1864 case REG_RDI: return (rp->r_rdi); 1865 case REG_RSI: return (rp->r_rsi); 1866 case REG_RBP: return (rp->r_rbp); 1867 case REG_RBX: return (rp->r_rbx); 1868 case REG_RDX: return (rp->r_rdx); 1869 case REG_RCX: return (rp->r_rcx); 1870 case REG_RAX: return (rp->r_rax); 1871 case REG_TRAPNO: return (rp->r_trapno); 1872 case REG_ERR: return (rp->r_err); 1873 case REG_RIP: return (rp->r_rip); 1874 case REG_CS: return (rp->r_cs); 1875 case REG_RFL: return (rp->r_rflags); 1876 case REG_RSP: return (rp->r_rsp); 1877 case REG_SS: return (rp->r_ss); 1878 case REG_FS: return (rp->r_fs); 1879 case REG_GS: return (rp->r_gs); 1880 case REG_DS: return (rp->r_ds); 1881 case REG_ES: return (rp->r_es); 1882 case REG_FSBASE: return (rdmsr(MSR_FSBASE)); 1883 case REG_GSBASE: return (rdmsr(MSR_GSBASE)); 1884 } 1885 1886 panic("dtrace: illegal register constant"); 1887 /*NOTREACHED*/ 1888 #else 1889 #define _NGREG 19 1890 if (reg >= _NGREG) 1891 panic("dtrace: illegal register constant"); 1892 1893 return (((greg_t *)&rp->r_gs)[reg]); 1894 #endif 1895 } 1896