]> git.saurik.com Git - apple/xnu.git/blame - bsd/dev/i386/fasttrap_isa.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / bsd / dev / i386 / fasttrap_isa.c
CommitLineData
2d21ac55
A
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
22/*
b0d623f7 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2d21ac55
A
24 * Use is subject to license terms.
25 */
26
27/*
b0d623f7 28 * #pragma ident "@(#)fasttrap_isa.c 1.27 08/04/09 SMI"
2d21ac55
A
29 */
30
31#ifdef KERNEL
32#ifndef _KERNEL
33#define _KERNEL /* Solaris vs. Darwin */
34#endif
35#endif
36
37#include <sys/fasttrap_isa.h>
38#include <sys/fasttrap_impl.h>
39#include <sys/dtrace.h>
40#include <sys/dtrace_impl.h>
cf7d32b8 41extern dtrace_id_t dtrace_probeid_error;
2d21ac55
A
42
43#include "fasttrap_regset.h"
44
45#include <sys/dtrace_ptss.h>
46#include <kern/debug.h>
47
b0d623f7
A
48/* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
49#define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
2d21ac55
A
50
51/*
52 * Lossless User-Land Tracing on x86
53 * ---------------------------------
54 *
55 * The execution of most instructions is not dependent on the address; for
56 * these instructions it is sufficient to copy them into the user process's
57 * address space and execute them. To effectively single-step an instruction
58 * in user-land, we copy out the following sequence of instructions to scratch
59 * space in the user thread's ulwp_t structure.
60 *
61 * We then set the program counter (%eip or %rip) to point to this scratch
62 * space. Once execution resumes, the original instruction is executed and
63 * then control flow is redirected to what was originally the subsequent
64 * instruction. If the kernel attemps to deliver a signal while single-
65 * stepping, the signal is deferred and the program counter is moved into the
66 * second sequence of instructions. The second sequence ends in a trap into
67 * the kernel where the deferred signal is then properly handled and delivered.
68 *
69 * For instructions whose execute is position dependent, we perform simple
70 * emulation. These instructions are limited to control transfer
71 * instructions in 32-bit mode, but in 64-bit mode there's the added wrinkle
72 * of %rip-relative addressing that means that almost any instruction can be
73 * position dependent. For all the details on how we emulate generic
74 * instructions included %rip-relative instructions, see the code in
75 * fasttrap_pid_probe() below where we handle instructions of type
76 * FASTTRAP_T_COMMON (under the header: Generic Instruction Tracing).
77 */
78
79#define FASTTRAP_MODRM_MOD(modrm) (((modrm) >> 6) & 0x3)
80#define FASTTRAP_MODRM_REG(modrm) (((modrm) >> 3) & 0x7)
81#define FASTTRAP_MODRM_RM(modrm) ((modrm) & 0x7)
82#define FASTTRAP_MODRM(mod, reg, rm) (((mod) << 6) | ((reg) << 3) | (rm))
83
84#define FASTTRAP_SIB_SCALE(sib) (((sib) >> 6) & 0x3)
85#define FASTTRAP_SIB_INDEX(sib) (((sib) >> 3) & 0x7)
86#define FASTTRAP_SIB_BASE(sib) ((sib) & 0x7)
87
88#define FASTTRAP_REX_W(rex) (((rex) >> 3) & 1)
89#define FASTTRAP_REX_R(rex) (((rex) >> 2) & 1)
90#define FASTTRAP_REX_X(rex) (((rex) >> 1) & 1)
91#define FASTTRAP_REX_B(rex) ((rex) & 1)
92#define FASTTRAP_REX(w, r, x, b) \
93 (0x40 | ((w) << 3) | ((r) << 2) | ((x) << 1) | (b))
94
95/*
96 * Single-byte op-codes.
97 */
98#define FASTTRAP_PUSHL_EBP 0x55
99
100#define FASTTRAP_JO 0x70
101#define FASTTRAP_JNO 0x71
102#define FASTTRAP_JB 0x72
103#define FASTTRAP_JAE 0x73
104#define FASTTRAP_JE 0x74
105#define FASTTRAP_JNE 0x75
106#define FASTTRAP_JBE 0x76
107#define FASTTRAP_JA 0x77
108#define FASTTRAP_JS 0x78
109#define FASTTRAP_JNS 0x79
110#define FASTTRAP_JP 0x7a
111#define FASTTRAP_JNP 0x7b
112#define FASTTRAP_JL 0x7c
113#define FASTTRAP_JGE 0x7d
114#define FASTTRAP_JLE 0x7e
115#define FASTTRAP_JG 0x7f
116
117#define FASTTRAP_NOP 0x90
118
119#define FASTTRAP_MOV_EAX 0xb8
120#define FASTTRAP_MOV_ECX 0xb9
121
122#define FASTTRAP_RET16 0xc2
123#define FASTTRAP_RET 0xc3
124
125#define FASTTRAP_LOOPNZ 0xe0
126#define FASTTRAP_LOOPZ 0xe1
127#define FASTTRAP_LOOP 0xe2
128#define FASTTRAP_JCXZ 0xe3
129
130#define FASTTRAP_CALL 0xe8
131#define FASTTRAP_JMP32 0xe9
132#define FASTTRAP_JMP8 0xeb
133
134#define FASTTRAP_INT3 0xcc
135#define FASTTRAP_INT 0xcd
136#define T_DTRACE_RET 0x7f
137
138#define FASTTRAP_2_BYTE_OP 0x0f
139#define FASTTRAP_GROUP5_OP 0xff
140
141/*
142 * Two-byte op-codes (second byte only).
143 */
144#define FASTTRAP_0F_JO 0x80
145#define FASTTRAP_0F_JNO 0x81
146#define FASTTRAP_0F_JB 0x82
147#define FASTTRAP_0F_JAE 0x83
148#define FASTTRAP_0F_JE 0x84
149#define FASTTRAP_0F_JNE 0x85
150#define FASTTRAP_0F_JBE 0x86
151#define FASTTRAP_0F_JA 0x87
152#define FASTTRAP_0F_JS 0x88
153#define FASTTRAP_0F_JNS 0x89
154#define FASTTRAP_0F_JP 0x8a
155#define FASTTRAP_0F_JNP 0x8b
156#define FASTTRAP_0F_JL 0x8c
157#define FASTTRAP_0F_JGE 0x8d
158#define FASTTRAP_0F_JLE 0x8e
159#define FASTTRAP_0F_JG 0x8f
160
161#define FASTTRAP_EFLAGS_OF 0x800
162#define FASTTRAP_EFLAGS_DF 0x400
163#define FASTTRAP_EFLAGS_SF 0x080
164#define FASTTRAP_EFLAGS_ZF 0x040
165#define FASTTRAP_EFLAGS_AF 0x010
166#define FASTTRAP_EFLAGS_PF 0x004
167#define FASTTRAP_EFLAGS_CF 0x001
168
169/*
170 * Instruction prefixes.
171 */
172#define FASTTRAP_PREFIX_OPERAND 0x66
173#define FASTTRAP_PREFIX_ADDRESS 0x67
174#define FASTTRAP_PREFIX_CS 0x2E
175#define FASTTRAP_PREFIX_DS 0x3E
176#define FASTTRAP_PREFIX_ES 0x26
177#define FASTTRAP_PREFIX_FS 0x64
178#define FASTTRAP_PREFIX_GS 0x65
179#define FASTTRAP_PREFIX_SS 0x36
180#define FASTTRAP_PREFIX_LOCK 0xF0
181#define FASTTRAP_PREFIX_REP 0xF3
182#define FASTTRAP_PREFIX_REPNE 0xF2
183
184#define FASTTRAP_NOREG 0xff
185
186/*
187 * Map between instruction register encodings and the kernel constants which
188 * correspond to indicies into struct regs.
189 */
190
191/*
192 * APPLE NOTE: We are cheating here. The regmap is used to decode which register
193 * a given instruction is trying to reference. OS X does not have extended registers
194 * for 32 bit apps, but the *order* is the same. So for 32 bit state, we will return:
195 *
196 * REG_RAX -> EAX
197 * REG_RCX -> ECX
198 * ...
199 * REG_RDI -> EDI
200 *
201 * The fasttrap_getreg function knows how to make the correct transformation.
202 */
203#if __sol64 || defined(__APPLE__)
204static const uint8_t regmap[16] = {
205 REG_RAX, REG_RCX, REG_RDX, REG_RBX, REG_RSP, REG_RBP, REG_RSI, REG_RDI,
206 REG_R8, REG_R9, REG_R10, REG_R11, REG_R12, REG_R13, REG_R14, REG_R15,
207};
208#else
209static const uint8_t regmap[8] = {
210 EAX, ECX, EDX, EBX, UESP, EBP, ESI, EDI
211};
212#endif
213
214static user_addr_t fasttrap_getreg(x86_saved_state_t *, uint_t);
215
216static uint64_t
217fasttrap_anarg(x86_saved_state_t *regs, int function_entry, int argno)
218{
219 uint64_t value;
220 int shift = function_entry ? 1 : 0;
221
222 x86_saved_state64_t *regs64;
223 x86_saved_state32_t *regs32;
224 unsigned int p_model;
225
226 if (is_saved_state64(regs)) {
227 regs64 = saved_state64(regs);
228 regs32 = NULL;
229 p_model = DATAMODEL_LP64;
230 } else {
231 regs64 = NULL;
232 regs32 = saved_state32(regs);
233 p_model = DATAMODEL_ILP32;
234 }
235
236 if (p_model == DATAMODEL_LP64) {
237 user_addr_t stack;
238
239 /*
240 * In 64-bit mode, the first six arguments are stored in
241 * registers.
242 */
243 if (argno < 6)
244 return ((&regs64->rdi)[argno]);
245
246 stack = regs64->isf.rsp + sizeof(uint64_t) * (argno - 6 + shift);
247 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
248 value = dtrace_fuword64(stack);
249 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
250 } else {
b0d623f7 251 uint32_t *stack = (uint32_t *)(uintptr_t)(regs32->uesp);
2d21ac55
A
252 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
253 value = dtrace_fuword32((user_addr_t)(unsigned long)&stack[argno + shift]);
254 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
255 }
256
257 return (value);
258}
259
260/*ARGSUSED*/
261int
262fasttrap_tracepoint_init(proc_t *p, fasttrap_tracepoint_t *tp, user_addr_t pc,
263 fasttrap_probe_type_t type)
264{
265#pragma unused(type)
266 uint8_t instr[FASTTRAP_MAX_INSTR_SIZE + 10];
267 size_t len = FASTTRAP_MAX_INSTR_SIZE;
268 size_t first = MIN(len, PAGE_SIZE - (pc & PAGE_MASK));
269 uint_t start = 0;
270 size_t size;
271 int rmindex;
272 uint8_t seg, rex = 0;
273 unsigned int p_model = (p->p_flag & P_LP64) ? DATAMODEL_LP64 : DATAMODEL_ILP32;
274
275 /*
276 * Read the instruction at the given address out of the process's
277 * address space. We don't have to worry about a debugger
278 * changing this instruction before we overwrite it with our trap
279 * instruction since P_PR_LOCK is set. Since instructions can span
280 * pages, we potentially read the instruction in two parts. If the
281 * second part fails, we just zero out that part of the instruction.
282 */
283 /*
284 * APPLE NOTE: Of course, we do not have a P_PR_LOCK, so this is racey...
285 */
286 if (uread(p, &instr[0], first, pc) != 0)
287 return (-1);
288 if (len > first &&
289 uread(p, &instr[first], len - first, pc + first) != 0) {
290 bzero(&instr[first], len - first);
291 len = first;
292 }
293
294 /*
295 * If the disassembly fails, then we have a malformed instruction.
296 */
297 if ((size = dtrace_instr_size_isa(instr, p_model, &rmindex)) <= 0)
298 return (-1);
299
300 /*
301 * Make sure the disassembler isn't completely broken.
302 */
303 ASSERT(-1 <= rmindex && rmindex < (int)size);
304
305 /*
306 * If the computed size is greater than the number of bytes read,
307 * then it was a malformed instruction possibly because it fell on a
308 * page boundary and the subsequent page was missing or because of
309 * some malicious user.
310 */
311 if (size > len)
312 return (-1);
313
314 tp->ftt_size = (uint8_t)size;
315 tp->ftt_segment = FASTTRAP_SEG_NONE;
316
317 /*
318 * Find the start of the instruction's opcode by processing any
319 * legacy prefixes.
320 */
321 for (;;) {
322 seg = 0;
323 switch (instr[start]) {
324 case FASTTRAP_PREFIX_SS:
325 seg++;
326 /*FALLTHRU*/
327 case FASTTRAP_PREFIX_GS:
328 seg++;
329 /*FALLTHRU*/
330 case FASTTRAP_PREFIX_FS:
331 seg++;
332 /*FALLTHRU*/
333 case FASTTRAP_PREFIX_ES:
334 seg++;
335 /*FALLTHRU*/
336 case FASTTRAP_PREFIX_DS:
337 seg++;
338 /*FALLTHRU*/
339 case FASTTRAP_PREFIX_CS:
340 seg++;
341 /*FALLTHRU*/
342 case FASTTRAP_PREFIX_OPERAND:
343 case FASTTRAP_PREFIX_ADDRESS:
344 case FASTTRAP_PREFIX_LOCK:
345 case FASTTRAP_PREFIX_REP:
346 case FASTTRAP_PREFIX_REPNE:
347 if (seg != 0) {
348 /*
349 * It's illegal for an instruction to specify
350 * two segment prefixes -- give up on this
351 * illegal instruction.
352 */
353 if (tp->ftt_segment != FASTTRAP_SEG_NONE)
354 return (-1);
355
356 tp->ftt_segment = seg;
357 }
358 start++;
359 continue;
360 }
361 break;
362 }
363
364#if __sol64 || defined(__APPLE__)
365 /*
366 * Identify the REX prefix on 64-bit processes.
367 */
368 if (p_model == DATAMODEL_LP64 && (instr[start] & 0xf0) == 0x40)
369 rex = instr[start++];
370#endif
371
372 /*
373 * Now that we're pretty sure that the instruction is okay, copy the
374 * valid part to the tracepoint.
375 */
376 bcopy(instr, tp->ftt_instr, FASTTRAP_MAX_INSTR_SIZE);
377
378 tp->ftt_type = FASTTRAP_T_COMMON;
379 if (instr[start] == FASTTRAP_2_BYTE_OP) {
380 switch (instr[start + 1]) {
381 case FASTTRAP_0F_JO:
382 case FASTTRAP_0F_JNO:
383 case FASTTRAP_0F_JB:
384 case FASTTRAP_0F_JAE:
385 case FASTTRAP_0F_JE:
386 case FASTTRAP_0F_JNE:
387 case FASTTRAP_0F_JBE:
388 case FASTTRAP_0F_JA:
389 case FASTTRAP_0F_JS:
390 case FASTTRAP_0F_JNS:
391 case FASTTRAP_0F_JP:
392 case FASTTRAP_0F_JNP:
393 case FASTTRAP_0F_JL:
394 case FASTTRAP_0F_JGE:
395 case FASTTRAP_0F_JLE:
396 case FASTTRAP_0F_JG:
397 tp->ftt_type = FASTTRAP_T_JCC;
398 tp->ftt_code = (instr[start + 1] & 0x0f) | FASTTRAP_JO;
399 tp->ftt_dest = pc + tp->ftt_size +
b0d623f7 400 /* LINTED - alignment */
2d21ac55
A
401 *(int32_t *)&instr[start + 2];
402 break;
403 }
404 } else if (instr[start] == FASTTRAP_GROUP5_OP) {
405 uint_t mod = FASTTRAP_MODRM_MOD(instr[start + 1]);
406 uint_t reg = FASTTRAP_MODRM_REG(instr[start + 1]);
407 uint_t rm = FASTTRAP_MODRM_RM(instr[start + 1]);
408
409 if (reg == 2 || reg == 4) {
410 uint_t i, sz;
411
412 if (reg == 2)
413 tp->ftt_type = FASTTRAP_T_CALL;
414 else
415 tp->ftt_type = FASTTRAP_T_JMP;
416
417 if (mod == 3)
418 tp->ftt_code = 2;
419 else
420 tp->ftt_code = 1;
421
422 ASSERT(p_model == DATAMODEL_LP64 || rex == 0);
423
424 /*
425 * See AMD x86-64 Architecture Programmer's Manual
426 * Volume 3, Section 1.2.7, Table 1-12, and
427 * Appendix A.3.1, Table A-15.
428 */
429 if (mod != 3 && rm == 4) {
430 uint8_t sib = instr[start + 2];
431 uint_t index = FASTTRAP_SIB_INDEX(sib);
432 uint_t base = FASTTRAP_SIB_BASE(sib);
433
434 tp->ftt_scale = FASTTRAP_SIB_SCALE(sib);
435
436 tp->ftt_index = (index == 4) ?
437 FASTTRAP_NOREG :
438 regmap[index | (FASTTRAP_REX_X(rex) << 3)];
439 tp->ftt_base = (mod == 0 && base == 5) ?
440 FASTTRAP_NOREG :
441 regmap[base | (FASTTRAP_REX_B(rex) << 3)];
442
443 i = 3;
444 sz = mod == 1 ? 1 : 4;
445 } else {
446 /*
447 * In 64-bit mode, mod == 0 and r/m == 5
448 * denotes %rip-relative addressing; in 32-bit
449 * mode, the base register isn't used. In both
450 * modes, there is a 32-bit operand.
451 */
452 if (mod == 0 && rm == 5) {
453#if __sol64 || defined(__APPLE__)
454 if (p_model == DATAMODEL_LP64)
455 tp->ftt_base = REG_RIP;
456 else
457#endif
458 tp->ftt_base = FASTTRAP_NOREG;
459 sz = 4;
460 } else {
461 uint8_t base = rm |
462 (FASTTRAP_REX_B(rex) << 3);
463
464 tp->ftt_base = regmap[base];
465 sz = mod == 1 ? 1 : mod == 2 ? 4 : 0;
466 }
467 tp->ftt_index = FASTTRAP_NOREG;
468 i = 2;
469 }
470
b0d623f7 471 if (sz == 1) {
2d21ac55 472 tp->ftt_dest = *(int8_t *)&instr[start + i];
b0d623f7
A
473 } else if (sz == 4) {
474 /* LINTED - alignment */
2d21ac55 475 tp->ftt_dest = *(int32_t *)&instr[start + i];
b0d623f7 476 } else {
2d21ac55 477 tp->ftt_dest = 0;
b0d623f7 478 }
2d21ac55
A
479 }
480 } else {
481 switch (instr[start]) {
482 case FASTTRAP_RET:
483 tp->ftt_type = FASTTRAP_T_RET;
484 break;
485
486 case FASTTRAP_RET16:
487 tp->ftt_type = FASTTRAP_T_RET16;
b0d623f7 488 /* LINTED - alignment */
2d21ac55
A
489 tp->ftt_dest = *(uint16_t *)&instr[start + 1];
490 break;
491
492 case FASTTRAP_JO:
493 case FASTTRAP_JNO:
494 case FASTTRAP_JB:
495 case FASTTRAP_JAE:
496 case FASTTRAP_JE:
497 case FASTTRAP_JNE:
498 case FASTTRAP_JBE:
499 case FASTTRAP_JA:
500 case FASTTRAP_JS:
501 case FASTTRAP_JNS:
502 case FASTTRAP_JP:
503 case FASTTRAP_JNP:
504 case FASTTRAP_JL:
505 case FASTTRAP_JGE:
506 case FASTTRAP_JLE:
507 case FASTTRAP_JG:
508 tp->ftt_type = FASTTRAP_T_JCC;
509 tp->ftt_code = instr[start];
510 tp->ftt_dest = pc + tp->ftt_size +
511 (int8_t)instr[start + 1];
512 break;
513
514 case FASTTRAP_LOOPNZ:
515 case FASTTRAP_LOOPZ:
516 case FASTTRAP_LOOP:
517 tp->ftt_type = FASTTRAP_T_LOOP;
518 tp->ftt_code = instr[start];
519 tp->ftt_dest = pc + tp->ftt_size +
520 (int8_t)instr[start + 1];
521 break;
522
523 case FASTTRAP_JCXZ:
524 tp->ftt_type = FASTTRAP_T_JCXZ;
525 tp->ftt_dest = pc + tp->ftt_size +
526 (int8_t)instr[start + 1];
527 break;
528
529 case FASTTRAP_CALL:
530 tp->ftt_type = FASTTRAP_T_CALL;
531 tp->ftt_dest = pc + tp->ftt_size +
b0d623f7 532 /* LINTED - alignment */
2d21ac55
A
533 *(int32_t *)&instr[start + 1];
534 tp->ftt_code = 0;
535 break;
536
537 case FASTTRAP_JMP32:
538 tp->ftt_type = FASTTRAP_T_JMP;
539 tp->ftt_dest = pc + tp->ftt_size +
b0d623f7 540 /* LINTED - alignment */
2d21ac55
A
541 *(int32_t *)&instr[start + 1];
542 break;
543 case FASTTRAP_JMP8:
544 tp->ftt_type = FASTTRAP_T_JMP;
545 tp->ftt_dest = pc + tp->ftt_size +
546 (int8_t)instr[start + 1];
547 break;
548
549 case FASTTRAP_PUSHL_EBP:
550 if (start == 0)
551 tp->ftt_type = FASTTRAP_T_PUSHL_EBP;
552 break;
553
554 case FASTTRAP_NOP:
555#if __sol64 || defined(__APPLE__)
556 ASSERT(p_model == DATAMODEL_LP64 || rex == 0);
557
558 /*
559 * On sol64 we have to be careful not to confuse a nop
560 * (actually xchgl %eax, %eax) with an instruction using
561 * the same opcode, but that does something different
562 * (e.g. xchgl %r8d, %eax or xcghq %r8, %rax).
563 */
564 if (FASTTRAP_REX_B(rex) == 0)
565#endif
566 tp->ftt_type = FASTTRAP_T_NOP;
567 break;
568
569 case FASTTRAP_INT3:
570 /*
571 * The pid provider shares the int3 trap with debugger
572 * breakpoints so we can't instrument them.
573 */
574 ASSERT(instr[start] == FASTTRAP_INSTR);
575 return (-1);
576
577 case FASTTRAP_INT:
578 /*
579 * Interrupts seem like they could be traced with
580 * no negative implications, but it's possible that
581 * a thread could be redirected by the trap handling
582 * code which would eventually return to the
583 * instruction after the interrupt. If the interrupt
584 * were in our scratch space, the subsequent
585 * instruction might be overwritten before we return.
586 * Accordingly we refuse to instrument any interrupt.
587 */
588 return (-1);
589 }
590 }
591
592#if __sol64 || defined(__APPLE__)
593 if (p_model == DATAMODEL_LP64 && tp->ftt_type == FASTTRAP_T_COMMON) {
594 /*
595 * If the process is 64-bit and the instruction type is still
596 * FASTTRAP_T_COMMON -- meaning we're going to copy it out an
597 * execute it -- we need to watch for %rip-relative
598 * addressing mode. See the portion of fasttrap_pid_probe()
599 * below where we handle tracepoints with type
600 * FASTTRAP_T_COMMON for how we emulate instructions that
601 * employ %rip-relative addressing.
602 */
603 if (rmindex != -1) {
604 uint_t mod = FASTTRAP_MODRM_MOD(instr[rmindex]);
605 uint_t reg = FASTTRAP_MODRM_REG(instr[rmindex]);
606 uint_t rm = FASTTRAP_MODRM_RM(instr[rmindex]);
607
608 ASSERT(rmindex > (int)start);
609
610 if (mod == 0 && rm == 5) {
611 /*
612 * We need to be sure to avoid other
613 * registers used by this instruction. While
614 * the reg field may determine the op code
615 * rather than denoting a register, assuming
616 * that it denotes a register is always safe.
617 * We leave the REX field intact and use
618 * whatever value's there for simplicity.
619 */
620 if (reg != 0) {
621 tp->ftt_ripmode = FASTTRAP_RIP_1 |
622 (FASTTRAP_RIP_X *
623 FASTTRAP_REX_B(rex));
624 rm = 0;
625 } else {
626 tp->ftt_ripmode = FASTTRAP_RIP_2 |
627 (FASTTRAP_RIP_X *
628 FASTTRAP_REX_B(rex));
629 rm = 1;
630 }
631
632 tp->ftt_modrm = tp->ftt_instr[rmindex];
633 tp->ftt_instr[rmindex] =
634 FASTTRAP_MODRM(2, reg, rm);
635 }
636 }
637 }
638#endif
639
640 return (0);
641}
642
643int
644fasttrap_tracepoint_install(proc_t *p, fasttrap_tracepoint_t *tp)
645{
646 fasttrap_instr_t instr = FASTTRAP_INSTR;
647
648 if (uwrite(p, &instr, 1, tp->ftt_pc) != 0)
649 return (-1);
650
651 return (0);
652}
653
654int
655fasttrap_tracepoint_remove(proc_t *p, fasttrap_tracepoint_t *tp)
656{
657 uint8_t instr;
658
659 /*
660 * Distinguish between read or write failures and a changed
661 * instruction.
662 */
663 if (uread(p, &instr, 1, tp->ftt_pc) != 0)
664 return (0);
665 if (instr != FASTTRAP_INSTR)
666 return (0);
667 if (uwrite(p, &tp->ftt_instr[0], 1, tp->ftt_pc) != 0)
668 return (-1);
669
670 return (0);
671}
672
673static void
674fasttrap_return_common(x86_saved_state_t *regs, user_addr_t pc, pid_t pid,
675 user_addr_t new_pc)
676{
677 x86_saved_state64_t *regs64;
678 x86_saved_state32_t *regs32;
679 unsigned int p_model;
680
b0d623f7
A
681 dtrace_icookie_t cookie;
682
2d21ac55
A
683 if (is_saved_state64(regs)) {
684 regs64 = saved_state64(regs);
685 regs32 = NULL;
686 p_model = DATAMODEL_LP64;
687 } else {
688 regs64 = NULL;
689 regs32 = saved_state32(regs);
690 p_model = DATAMODEL_ILP32;
691 }
692
693 fasttrap_tracepoint_t *tp;
694 fasttrap_bucket_t *bucket;
695 fasttrap_id_t *id;
696 lck_mtx_t *pid_mtx;
697
698 pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
699 lck_mtx_lock(pid_mtx);
700 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
701
702 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
703 if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
b0d623f7 704 tp->ftt_proc->ftpc_acount != 0)
2d21ac55
A
705 break;
706 }
707
708 /*
709 * Don't sweat it if we can't find the tracepoint again; unlike
710 * when we're in fasttrap_pid_probe(), finding the tracepoint here
711 * is not essential to the correct execution of the process.
712 */
713 if (tp == NULL) {
714 lck_mtx_unlock(pid_mtx);
715 return;
716 }
717
718 for (id = tp->ftt_retids; id != NULL; id = id->fti_next) {
719 /*
720 * If there's a branch that could act as a return site, we
721 * need to trace it, and check here if the program counter is
722 * external to the function.
723 */
724 if (tp->ftt_type != FASTTRAP_T_RET &&
725 tp->ftt_type != FASTTRAP_T_RET16 &&
726 new_pc - id->fti_probe->ftp_faddr <
727 id->fti_probe->ftp_fsize)
728 continue;
729
b0d623f7
A
730 /*
731 * Provide a hint to the stack trace functions to add the
732 * following pc to the top of the stack since it's missing
733 * on a return probe yet highly desirable for consistency.
734 */
735 cookie = dtrace_interrupt_disable();
736 cpu_core[CPU->cpu_id].cpuc_missing_tos = pc;
cf7d32b8
A
737 if (ISSET(current_proc()->p_lflag, P_LNOATTACH)) {
738 dtrace_probe(dtrace_probeid_error, 0 /* state */, id->fti_probe->ftp_id,
739 1 /* ndx */, -1 /* offset */, DTRACEFLT_UPRIV);
740 } else if (p_model == DATAMODEL_LP64) {
2d21ac55
A
741 dtrace_probe(id->fti_probe->ftp_id,
742 pc - id->fti_probe->ftp_faddr,
743 regs64->rax, regs64->rdx, 0, 0);
744 } else {
745 dtrace_probe(id->fti_probe->ftp_id,
746 pc - id->fti_probe->ftp_faddr,
747 regs32->eax, regs32->edx, 0, 0);
748 }
b0d623f7
A
749 /* remove the hint */
750 cpu_core[CPU->cpu_id].cpuc_missing_tos = 0;
751 dtrace_interrupt_enable(cookie);
2d21ac55
A
752 }
753
754 lck_mtx_unlock(pid_mtx);
755}
756
757static void
758fasttrap_sigsegv(proc_t *p, uthread_t t, user_addr_t addr)
759{
760 proc_lock(p);
761
762 /* Set fault address and mark signal */
763 t->uu_code = addr;
764 t->uu_siglist |= sigmask(SIGSEGV);
765
766 /*
767 * XXX These two line may be redundant; if not, then we need
768 * XXX to potentially set the data address in the machine
769 * XXX specific thread state structure to indicate the address.
770 */
771 t->uu_exception = KERN_INVALID_ADDRESS; /* SIGSEGV */
772 t->uu_subcode = 0; /* XXX pad */
773
774 proc_unlock(p);
775
776 /* raise signal */
777 signal_setast(t->uu_context.vc_thread);
778}
779
780static void
781fasttrap_usdt_args64(fasttrap_probe_t *probe, x86_saved_state64_t *regs64, int argc,
782 uint64_t *argv)
783{
784 int i, x, cap = MIN(argc, probe->ftp_nargs);
785 user_addr_t stack = (user_addr_t)regs64->isf.rsp;
786
787 for (i = 0; i < cap; i++) {
788 x = probe->ftp_argmap[i];
789
790 if (x < 6) {
791 /* FIXME! This may be broken, needs testing */
792 argv[i] = (&regs64->rdi)[x];
793 } else {
794 fasttrap_fuword64_noerr(stack + (x * sizeof(uint64_t)), &argv[i]);
795 }
796 }
797
798 for (; i < argc; i++) {
799 argv[i] = 0;
800 }
801}
802
803static void
804fasttrap_usdt_args32(fasttrap_probe_t *probe, x86_saved_state32_t *regs32, int argc,
805 uint32_t *argv)
806{
807 int i, x, cap = MIN(argc, probe->ftp_nargs);
b0d623f7 808 uint32_t *stack = (uint32_t *)(uintptr_t)(regs32->uesp);
2d21ac55
A
809
810 for (i = 0; i < cap; i++) {
811 x = probe->ftp_argmap[i];
812
813 fasttrap_fuword32_noerr((user_addr_t)(unsigned long)&stack[x], &argv[i]);
814 }
815
816 for (; i < argc; i++) {
817 argv[i] = 0;
818 }
819}
820
821/*
822 * FIXME!
823 */
824static int
825fasttrap_do_seg(fasttrap_tracepoint_t *tp, x86_saved_state_t *rp, user_addr_t *addr) // 64 bit
826{
827#pragma unused(tp, rp, addr)
828 printf("fasttrap_do_seg() called while unimplemented.\n");
829#if 0
830 proc_t *p = curproc;
831 user_desc_t *desc;
832 uint16_t sel, ndx, type;
833 uintptr_t limit;
834
835 switch (tp->ftt_segment) {
836 case FASTTRAP_SEG_CS:
837 sel = rp->r_cs;
838 break;
839 case FASTTRAP_SEG_DS:
840 sel = rp->r_ds;
841 break;
842 case FASTTRAP_SEG_ES:
843 sel = rp->r_es;
844 break;
845 case FASTTRAP_SEG_FS:
846 sel = rp->r_fs;
847 break;
848 case FASTTRAP_SEG_GS:
849 sel = rp->r_gs;
850 break;
851 case FASTTRAP_SEG_SS:
852 sel = rp->r_ss;
853 break;
854 }
855
856 /*
857 * Make sure the given segment register specifies a user priority
858 * selector rather than a kernel selector.
859 */
860 if (!SELISUPL(sel))
861 return (-1);
862
863 ndx = SELTOIDX(sel);
864
865 /*
866 * Check the bounds and grab the descriptor out of the specified
867 * descriptor table.
868 */
869 if (SELISLDT(sel)) {
870 if (ndx > p->p_ldtlimit)
871 return (-1);
872
873 desc = p->p_ldt + ndx;
874
875 } else {
876 if (ndx >= NGDT)
877 return (-1);
878
879 desc = cpu_get_gdt() + ndx;
880 }
881
882 /*
883 * The descriptor must have user privilege level and it must be
884 * present in memory.
885 */
886 if (desc->usd_dpl != SEL_UPL || desc->usd_p != 1)
887 return (-1);
888
889 type = desc->usd_type;
890
891 /*
892 * If the S bit in the type field is not set, this descriptor can
893 * only be used in system context.
894 */
895 if ((type & 0x10) != 0x10)
896 return (-1);
897
898 limit = USEGD_GETLIMIT(desc) * (desc->usd_gran ? PAGESIZE : 1);
899
900 if (tp->ftt_segment == FASTTRAP_SEG_CS) {
901 /*
902 * The code/data bit and readable bit must both be set.
903 */
904 if ((type & 0xa) != 0xa)
905 return (-1);
906
907 if (*addr > limit)
908 return (-1);
909 } else {
910 /*
911 * The code/data bit must be clear.
912 */
913 if ((type & 0x8) != 0)
914 return (-1);
915
916 /*
917 * If the expand-down bit is clear, we just check the limit as
918 * it would naturally be applied. Otherwise, we need to check
919 * that the address is the range [limit + 1 .. 0xffff] or
920 * [limit + 1 ... 0xffffffff] depending on if the default
921 * operand size bit is set.
922 */
923 if ((type & 0x4) == 0) {
924 if (*addr > limit)
925 return (-1);
926 } else if (desc->usd_def32) {
927 if (*addr < limit + 1 || 0xffff < *addr)
928 return (-1);
929 } else {
930 if (*addr < limit + 1 || 0xffffffff < *addr)
931 return (-1);
932 }
933 }
934
935 *addr += USEGD_GETBASE(desc);
936#endif /* 0 */
937 return (0);
938}
939
940/*
941 * Due to variances between Solaris and xnu, I have split this into a 32 bit and 64 bit
942 * code path. It still takes an x86_saved_state_t* argument, because it must sometimes
943 * call other methods that require a x86_saved_state_t.
944 *
945 * NOTE!!!!
946 *
947 * Any changes made to this method must be echo'd in fasttrap_pid_probe64!
948 *
949 */
950static int
951fasttrap_pid_probe32(x86_saved_state_t *regs)
952{
953 ASSERT(is_saved_state32(regs));
954
955 x86_saved_state32_t *regs32 = saved_state32(regs);
956 user_addr_t pc = regs32->eip - 1;
957 proc_t *p = current_proc();
958 user_addr_t new_pc = 0;
959 fasttrap_bucket_t *bucket;
960 lck_mtx_t *pid_mtx;
961 fasttrap_tracepoint_t *tp, tp_local;
962 pid_t pid;
963 dtrace_icookie_t cookie;
964 uint_t is_enabled = 0;
965
966 uthread_t uthread = (uthread_t)get_bsdthread_info(current_thread());
967
968 /*
969 * It's possible that a user (in a veritable orgy of bad planning)
970 * could redirect this thread's flow of control before it reached the
971 * return probe fasttrap. In this case we need to kill the process
972 * since it's in a unrecoverable state.
973 */
974 if (uthread->t_dtrace_step) {
975 ASSERT(uthread->t_dtrace_on);
976 fasttrap_sigtrap(p, uthread, pc);
977 return (0);
978 }
979
980 /*
981 * Clear all user tracing flags.
982 */
983 uthread->t_dtrace_ft = 0;
984 uthread->t_dtrace_pc = 0;
985 uthread->t_dtrace_npc = 0;
986 uthread->t_dtrace_scrpc = 0;
987 uthread->t_dtrace_astpc = 0;
988
989 /*
990 * Treat a child created by a call to vfork(2) as if it were its
991 * parent. We know that there's only one thread of control in such a
992 * process: this one.
993 */
994 /*
995 * APPLE NOTE: Terry says: "You need to hold the process locks (currently: kernel funnel) for this traversal"
996 * FIXME: How do we assert this?
997 */
998 while (p->p_lflag & P_LINVFORK)
999 p = p->p_pptr;
1000
1001 pid = p->p_pid;
1002 pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
1003 lck_mtx_lock(pid_mtx);
1004 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
1005
1006 /*
1007 * Lookup the tracepoint that the process just hit.
1008 */
1009 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
1010 if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
b0d623f7 1011 tp->ftt_proc->ftpc_acount != 0)
2d21ac55
A
1012 break;
1013 }
1014
1015 /*
1016 * If we couldn't find a matching tracepoint, either a tracepoint has
1017 * been inserted without using the pid<pid> ioctl interface (see
1018 * fasttrap_ioctl), or somehow we have mislaid this tracepoint.
1019 */
1020 if (tp == NULL) {
1021 lck_mtx_unlock(pid_mtx);
1022 return (-1);
1023 }
1024
1025 /*
1026 * Set the program counter to the address of the traced instruction
1027 * so that it looks right in ustack() output.
1028 */
1029 regs32->eip = pc;
1030
1031 if (tp->ftt_ids != NULL) {
1032 fasttrap_id_t *id;
1033
1034 uint32_t s0, s1, s2, s3, s4, s5;
b0d623f7 1035 uint32_t *stack = (uint32_t *)(uintptr_t)(regs32->uesp);
2d21ac55
A
1036
1037 /*
1038 * In 32-bit mode, all arguments are passed on the
1039 * stack. If this is a function entry probe, we need
1040 * to skip the first entry on the stack as it
1041 * represents the return address rather than a
1042 * parameter to the function.
1043 */
1044 fasttrap_fuword32_noerr((user_addr_t)(unsigned long)&stack[0], &s0);
1045 fasttrap_fuword32_noerr((user_addr_t)(unsigned long)&stack[1], &s1);
1046 fasttrap_fuword32_noerr((user_addr_t)(unsigned long)&stack[2], &s2);
1047 fasttrap_fuword32_noerr((user_addr_t)(unsigned long)&stack[3], &s3);
1048 fasttrap_fuword32_noerr((user_addr_t)(unsigned long)&stack[4], &s4);
1049 fasttrap_fuword32_noerr((user_addr_t)(unsigned long)&stack[5], &s5);
1050
1051 for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
1052 fasttrap_probe_t *probe = id->fti_probe;
1053
cf7d32b8
A
1054 if (ISSET(current_proc()->p_lflag, P_LNOATTACH)) {
1055 dtrace_probe(dtrace_probeid_error, 0 /* state */, probe->ftp_id,
1056 1 /* ndx */, -1 /* offset */, DTRACEFLT_UPRIV);
1057 } else if (id->fti_ptype == DTFTP_ENTRY) {
2d21ac55
A
1058 /*
1059 * We note that this was an entry
1060 * probe to help ustack() find the
1061 * first caller.
1062 */
1063 cookie = dtrace_interrupt_disable();
1064 DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
1065 dtrace_probe(probe->ftp_id, s1, s2,
1066 s3, s4, s5);
1067 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
1068 dtrace_interrupt_enable(cookie);
1069 } else if (id->fti_ptype == DTFTP_IS_ENABLED) {
1070 /*
1071 * Note that in this case, we don't
1072 * call dtrace_probe() since it's only
1073 * an artificial probe meant to change
1074 * the flow of control so that it
1075 * encounters the true probe.
1076 */
1077 is_enabled = 1;
1078 } else if (probe->ftp_argmap == NULL) {
1079 dtrace_probe(probe->ftp_id, s0, s1,
1080 s2, s3, s4);
1081 } else {
1082 uint32_t t[5];
1083
1084 fasttrap_usdt_args32(probe, regs32,
1085 sizeof (t) / sizeof (t[0]), t);
1086
1087 dtrace_probe(probe->ftp_id, t[0], t[1],
1088 t[2], t[3], t[4]);
1089 }
1090
1091 /* APPLE NOTE: Oneshot probes get one and only one chance... */
1092 if (probe->ftp_prov->ftp_provider_type == DTFTP_PROVIDER_ONESHOT) {
1093 fasttrap_tracepoint_remove(p, tp);
1094 }
1095 }
1096 }
1097
1098 /*
1099 * We're about to do a bunch of work so we cache a local copy of
1100 * the tracepoint to emulate the instruction, and then find the
1101 * tracepoint again later if we need to light up any return probes.
1102 */
1103 tp_local = *tp;
1104 lck_mtx_unlock(pid_mtx);
1105 tp = &tp_local;
1106
1107 /*
1108 * Set the program counter to appear as though the traced instruction
1109 * had completely executed. This ensures that fasttrap_getreg() will
1110 * report the expected value for REG_RIP.
1111 */
1112 regs32->eip = pc + tp->ftt_size;
1113
1114 /*
1115 * If there's an is-enabled probe connected to this tracepoint it
1116 * means that there was a 'xorl %eax, %eax' or 'xorq %rax, %rax'
1117 * instruction that was placed there by DTrace when the binary was
1118 * linked. As this probe is, in fact, enabled, we need to stuff 1
1119 * into %eax or %rax. Accordingly, we can bypass all the instruction
1120 * emulation logic since we know the inevitable result. It's possible
1121 * that a user could construct a scenario where the 'is-enabled'
1122 * probe was on some other instruction, but that would be a rather
1123 * exotic way to shoot oneself in the foot.
1124 */
1125 if (is_enabled) {
1126 regs32->eax = 1;
1127 new_pc = regs32->eip;
1128 goto done;
1129 }
1130
1131 /*
1132 * We emulate certain types of instructions to ensure correctness
1133 * (in the case of position dependent instructions) or optimize
1134 * common cases. The rest we have the thread execute back in user-
1135 * land.
1136 */
1137 switch (tp->ftt_type) {
1138 case FASTTRAP_T_RET:
1139 case FASTTRAP_T_RET16:
1140 {
1141 user_addr_t dst;
1142 user_addr_t addr;
1143 int ret;
1144
1145 /*
1146 * We have to emulate _every_ facet of the behavior of a ret
1147 * instruction including what happens if the load from %esp
1148 * fails; in that case, we send a SIGSEGV.
1149 */
1150 uint32_t dst32;
1151 ret = fasttrap_fuword32((user_addr_t)regs32->uesp, &dst32);
1152 dst = dst32;
1153 addr = regs32->uesp + sizeof (uint32_t);
1154
1155 if (ret == -1) {
1156 fasttrap_sigsegv(p, uthread, (user_addr_t)regs32->uesp);
1157 new_pc = pc;
1158 break;
1159 }
1160
1161 if (tp->ftt_type == FASTTRAP_T_RET16)
1162 addr += tp->ftt_dest;
1163
1164 regs32->uesp = addr;
1165 new_pc = dst;
1166 break;
1167 }
1168
1169 case FASTTRAP_T_JCC:
1170 {
1171 uint_t taken;
1172
1173 switch (tp->ftt_code) {
1174 case FASTTRAP_JO:
1175 taken = (regs32->efl & FASTTRAP_EFLAGS_OF) != 0;
1176 break;
1177 case FASTTRAP_JNO:
1178 taken = (regs32->efl & FASTTRAP_EFLAGS_OF) == 0;
1179 break;
1180 case FASTTRAP_JB:
1181 taken = (regs32->efl & FASTTRAP_EFLAGS_CF) != 0;
1182 break;
1183 case FASTTRAP_JAE:
1184 taken = (regs32->efl & FASTTRAP_EFLAGS_CF) == 0;
1185 break;
1186 case FASTTRAP_JE:
1187 taken = (regs32->efl & FASTTRAP_EFLAGS_ZF) != 0;
1188 break;
1189 case FASTTRAP_JNE:
1190 taken = (regs32->efl & FASTTRAP_EFLAGS_ZF) == 0;
1191 break;
1192 case FASTTRAP_JBE:
1193 taken = (regs32->efl & FASTTRAP_EFLAGS_CF) != 0 ||
1194 (regs32->efl & FASTTRAP_EFLAGS_ZF) != 0;
1195 break;
1196 case FASTTRAP_JA:
1197 taken = (regs32->efl & FASTTRAP_EFLAGS_CF) == 0 &&
1198 (regs32->efl & FASTTRAP_EFLAGS_ZF) == 0;
1199 break;
1200 case FASTTRAP_JS:
1201 taken = (regs32->efl & FASTTRAP_EFLAGS_SF) != 0;
1202 break;
1203 case FASTTRAP_JNS:
1204 taken = (regs32->efl & FASTTRAP_EFLAGS_SF) == 0;
1205 break;
1206 case FASTTRAP_JP:
1207 taken = (regs32->efl & FASTTRAP_EFLAGS_PF) != 0;
1208 break;
1209 case FASTTRAP_JNP:
1210 taken = (regs32->efl & FASTTRAP_EFLAGS_PF) == 0;
1211 break;
1212 case FASTTRAP_JL:
1213 taken = ((regs32->efl & FASTTRAP_EFLAGS_SF) == 0) !=
1214 ((regs32->efl & FASTTRAP_EFLAGS_OF) == 0);
1215 break;
1216 case FASTTRAP_JGE:
1217 taken = ((regs32->efl & FASTTRAP_EFLAGS_SF) == 0) ==
1218 ((regs32->efl & FASTTRAP_EFLAGS_OF) == 0);
1219 break;
1220 case FASTTRAP_JLE:
1221 taken = (regs32->efl & FASTTRAP_EFLAGS_ZF) != 0 ||
1222 ((regs32->efl & FASTTRAP_EFLAGS_SF) == 0) !=
1223 ((regs32->efl & FASTTRAP_EFLAGS_OF) == 0);
1224 break;
1225 case FASTTRAP_JG:
1226 taken = (regs32->efl & FASTTRAP_EFLAGS_ZF) == 0 &&
1227 ((regs32->efl & FASTTRAP_EFLAGS_SF) == 0) ==
1228 ((regs32->efl & FASTTRAP_EFLAGS_OF) == 0);
1229 break;
1230 default:
1231 taken = FALSE;
1232 }
1233
1234 if (taken)
1235 new_pc = tp->ftt_dest;
1236 else
1237 new_pc = pc + tp->ftt_size;
1238 break;
1239 }
1240
1241 case FASTTRAP_T_LOOP:
1242 {
1243 uint_t taken;
1244 greg_t cx = regs32->ecx--;
1245
1246 switch (tp->ftt_code) {
1247 case FASTTRAP_LOOPNZ:
1248 taken = (regs32->efl & FASTTRAP_EFLAGS_ZF) == 0 &&
1249 cx != 0;
1250 break;
1251 case FASTTRAP_LOOPZ:
1252 taken = (regs32->efl & FASTTRAP_EFLAGS_ZF) != 0 &&
1253 cx != 0;
1254 break;
1255 case FASTTRAP_LOOP:
1256 taken = (cx != 0);
1257 break;
1258 default:
1259 taken = FALSE;
1260 }
1261
1262 if (taken)
1263 new_pc = tp->ftt_dest;
1264 else
1265 new_pc = pc + tp->ftt_size;
1266 break;
1267 }
1268
1269 case FASTTRAP_T_JCXZ:
1270 {
1271 greg_t cx = regs32->ecx;
1272
1273 if (cx == 0)
1274 new_pc = tp->ftt_dest;
1275 else
1276 new_pc = pc + tp->ftt_size;
1277 break;
1278 }
1279
1280 case FASTTRAP_T_PUSHL_EBP:
1281 {
1282 user_addr_t addr = regs32->uesp - sizeof (uint32_t);
1283 int ret = fasttrap_suword32(addr, (uint32_t)regs32->ebp);
1284
1285 if (ret == -1) {
1286 fasttrap_sigsegv(p, uthread, addr);
1287 new_pc = pc;
1288 break;
1289 }
1290
1291 regs32->uesp = addr;
1292 new_pc = pc + tp->ftt_size;
1293 break;
1294 }
1295
1296 case FASTTRAP_T_NOP:
1297 new_pc = pc + tp->ftt_size;
1298 break;
1299
1300 case FASTTRAP_T_JMP:
1301 case FASTTRAP_T_CALL:
1302 if (tp->ftt_code == 0) {
1303 new_pc = tp->ftt_dest;
1304 } else {
1305 user_addr_t /* value ,*/ addr = tp->ftt_dest;
1306
1307 if (tp->ftt_base != FASTTRAP_NOREG)
1308 addr += fasttrap_getreg(regs, tp->ftt_base);
1309 if (tp->ftt_index != FASTTRAP_NOREG)
1310 addr += fasttrap_getreg(regs, tp->ftt_index) <<
1311 tp->ftt_scale;
1312
1313 if (tp->ftt_code == 1) {
1314 /*
1315 * If there's a segment prefix for this
1316 * instruction, we'll need to check permissions
1317 * and bounds on the given selector, and adjust
1318 * the address accordingly.
1319 */
1320 if (tp->ftt_segment != FASTTRAP_SEG_NONE &&
1321 fasttrap_do_seg(tp, regs, &addr) != 0) {
1322 fasttrap_sigsegv(p, uthread, addr);
1323 new_pc = pc;
1324 break;
1325 }
1326
1327 uint32_t value32;
1328 addr = (user_addr_t)(uint32_t)addr;
1329 if (fasttrap_fuword32(addr, &value32) == -1) {
1330 fasttrap_sigsegv(p, uthread, addr);
1331 new_pc = pc;
1332 break;
1333 }
1334 new_pc = value32;
1335 } else {
1336 new_pc = addr;
1337 }
1338 }
1339
1340 /*
1341 * If this is a call instruction, we need to push the return
1342 * address onto the stack. If this fails, we send the process
1343 * a SIGSEGV and reset the pc to emulate what would happen if
1344 * this instruction weren't traced.
1345 */
1346 if (tp->ftt_type == FASTTRAP_T_CALL) {
1347 user_addr_t addr = regs32->uesp - sizeof (uint32_t);
1348 int ret = fasttrap_suword32(addr, (uint32_t)(pc + tp->ftt_size));
1349
1350 if (ret == -1) {
1351 fasttrap_sigsegv(p, uthread, addr);
1352 new_pc = pc;
1353 break;
1354 }
1355
1356 regs32->uesp = addr;
1357 }
1358 break;
1359
1360 case FASTTRAP_T_COMMON:
1361 {
1362 user_addr_t addr;
b0d623f7 1363 uint8_t scratch[2 * FASTTRAP_MAX_INSTR_SIZE + 7];
2d21ac55
A
1364 uint_t i = 0;
1365
1366 /*
1367 * Generic Instruction Tracing
1368 * ---------------------------
1369 *
1370 * This is the layout of the scratch space in the user-land
1371 * thread structure for our generated instructions.
1372 *
1373 * 32-bit mode bytes
1374 * ------------------------ -----
1375 * a: <original instruction> <= 15
1376 * jmp <pc + tp->ftt_size> 5
1377 * b: <original instrction> <= 15
1378 * int T_DTRACE_RET 2
1379 * -----
1380 * <= 37
1381 *
1382 * 64-bit mode bytes
1383 * ------------------------ -----
1384 * a: <original instruction> <= 15
1385 * jmp 0(%rip) 6
1386 * <pc + tp->ftt_size> 8
1387 * b: <original instruction> <= 15
1388 * int T_DTRACE_RET 2
1389 * -----
1390 * <= 46
1391 *
1392 * The %pc is set to a, and curthread->t_dtrace_astpc is set
1393 * to b. If we encounter a signal on the way out of the
1394 * kernel, trap() will set %pc to curthread->t_dtrace_astpc
1395 * so that we execute the original instruction and re-enter
1396 * the kernel rather than redirecting to the next instruction.
1397 *
1398 * If there are return probes (so we know that we're going to
1399 * need to reenter the kernel after executing the original
1400 * instruction), the scratch space will just contain the
1401 * original instruction followed by an interrupt -- the same
1402 * data as at b.
1403 */
1404
1405 addr = uthread->t_dtrace_scratch->addr;
1406
1407 if (addr == 0LL) {
1408 fasttrap_sigtrap(p, uthread, pc); // Should be killing target proc
1409 new_pc = pc;
1410 break;
1411 }
1412
1413 ASSERT(tp->ftt_size < FASTTRAP_MAX_INSTR_SIZE);
1414
1415 uthread->t_dtrace_scrpc = addr;
1416 bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
1417 i += tp->ftt_size;
1418
1419 /*
1420 * Set up the jmp to the next instruction; note that
1421 * the size of the traced instruction cancels out.
1422 */
1423 scratch[i++] = FASTTRAP_JMP32;
b0d623f7 1424 /* LINTED - alignment */
2d21ac55
A
1425 *(uint32_t *)&scratch[i] = pc - addr - 5;
1426 i += sizeof (uint32_t);
1427
1428 uthread->t_dtrace_astpc = addr + i;
1429 bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
1430 i += tp->ftt_size;
1431 scratch[i++] = FASTTRAP_INT;
1432 scratch[i++] = T_DTRACE_RET;
1433
b0d623f7
A
1434 ASSERT(i <= sizeof (scratch));
1435
2d21ac55
A
1436 if (fasttrap_copyout(scratch, addr, i)) {
1437 fasttrap_sigtrap(p, uthread, pc);
1438 new_pc = pc;
1439 break;
1440 }
1441
1442 if (tp->ftt_retids != NULL) {
1443 uthread->t_dtrace_step = 1;
1444 uthread->t_dtrace_ret = 1;
1445 new_pc = uthread->t_dtrace_astpc;
1446 } else {
1447 new_pc = uthread->t_dtrace_scrpc;
1448 }
1449
1450 uthread->t_dtrace_pc = pc;
1451 uthread->t_dtrace_npc = pc + tp->ftt_size;
1452 uthread->t_dtrace_on = 1;
1453 break;
1454 }
1455
1456 default:
1457 panic("fasttrap: mishandled an instruction");
1458 }
1459
1460done:
1461 /*
1462 * APPLE NOTE:
1463 *
1464 * We're setting this earlier than Solaris does, to get a "correct"
1465 * ustack() output. In the Sun code, a() -> b() -> c() -> d() is
1466 * reported at: d, b, a. The new way gives c, b, a, which is closer
1467 * to correct, as the return instruction has already exectued.
1468 */
1469 regs32->eip = new_pc;
1470
1471 /*
1472 * If there were no return probes when we first found the tracepoint,
1473 * we should feel no obligation to honor any return probes that were
1474 * subsequently enabled -- they'll just have to wait until the next
1475 * time around.
1476 */
1477 if (tp->ftt_retids != NULL) {
1478 /*
1479 * We need to wait until the results of the instruction are
1480 * apparent before invoking any return probes. If this
1481 * instruction was emulated we can just call
1482 * fasttrap_return_common(); if it needs to be executed, we
1483 * need to wait until the user thread returns to the kernel.
1484 */
1485 if (tp->ftt_type != FASTTRAP_T_COMMON) {
1486 fasttrap_return_common(regs, pc, pid, new_pc);
1487 } else {
1488 ASSERT(uthread->t_dtrace_ret != 0);
1489 ASSERT(uthread->t_dtrace_pc == pc);
1490 ASSERT(uthread->t_dtrace_scrpc != 0);
1491 ASSERT(new_pc == uthread->t_dtrace_astpc);
1492 }
1493 }
1494
1495 return (0);
1496}
1497
1498/*
1499 * Due to variances between Solaris and xnu, I have split this into a 32 bit and 64 bit
1500 * code path. It still takes an x86_saved_state_t* argument, because it must sometimes
1501 * call other methods that require a x86_saved_state_t.
1502 *
1503 * NOTE!!!!
1504 *
1505 * Any changes made to this method must be echo'd in fasttrap_pid_probe32!
1506 *
1507 */
1508static int
1509fasttrap_pid_probe64(x86_saved_state_t *regs)
1510{
1511 ASSERT(is_saved_state64(regs));
1512
1513 x86_saved_state64_t *regs64 = saved_state64(regs);
1514 user_addr_t pc = regs64->isf.rip - 1;
1515 proc_t *p = current_proc();
1516 user_addr_t new_pc = 0;
1517 fasttrap_bucket_t *bucket;
1518 lck_mtx_t *pid_mtx;
1519 fasttrap_tracepoint_t *tp, tp_local;
1520 pid_t pid;
1521 dtrace_icookie_t cookie;
1522 uint_t is_enabled = 0;
1523
1524 uthread_t uthread = (uthread_t)get_bsdthread_info(current_thread());
1525
1526 /*
1527 * It's possible that a user (in a veritable orgy of bad planning)
1528 * could redirect this thread's flow of control before it reached the
1529 * return probe fasttrap. In this case we need to kill the process
1530 * since it's in a unrecoverable state.
1531 */
1532 if (uthread->t_dtrace_step) {
1533 ASSERT(uthread->t_dtrace_on);
1534 fasttrap_sigtrap(p, uthread, pc);
1535 return (0);
1536 }
1537
1538 /*
1539 * Clear all user tracing flags.
1540 */
1541 uthread->t_dtrace_ft = 0;
1542 uthread->t_dtrace_pc = 0;
1543 uthread->t_dtrace_npc = 0;
1544 uthread->t_dtrace_scrpc = 0;
1545 uthread->t_dtrace_astpc = 0;
1546 uthread->t_dtrace_regv = 0;
1547
1548 /*
1549 * Treat a child created by a call to vfork(2) as if it were its
1550 * parent. We know that there's only one thread of control in such a
1551 * process: this one.
1552 */
1553 /*
1554 * APPLE NOTE: Terry says: "You need to hold the process locks (currently: kernel funnel) for this traversal"
1555 * FIXME: How do we assert this?
1556 */
1557 while (p->p_lflag & P_LINVFORK)
1558 p = p->p_pptr;
1559
1560 pid = p->p_pid;
1561 pid_mtx = &cpu_core[CPU->cpu_id].cpuc_pid_lock;
1562 lck_mtx_lock(pid_mtx);
1563 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
1564
1565 /*
1566 * Lookup the tracepoint that the process just hit.
1567 */
1568 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
1569 if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
b0d623f7 1570 tp->ftt_proc->ftpc_acount != 0)
2d21ac55
A
1571 break;
1572 }
1573
1574 /*
1575 * If we couldn't find a matching tracepoint, either a tracepoint has
1576 * been inserted without using the pid<pid> ioctl interface (see
1577 * fasttrap_ioctl), or somehow we have mislaid this tracepoint.
1578 */
1579 if (tp == NULL) {
1580 lck_mtx_unlock(pid_mtx);
1581 return (-1);
1582 }
1583
1584 /*
1585 * Set the program counter to the address of the traced instruction
1586 * so that it looks right in ustack() output.
1587 */
1588 regs64->isf.rip = pc;
1589
1590 if (tp->ftt_ids != NULL) {
1591 fasttrap_id_t *id;
1592
1593 for (id = tp->ftt_ids; id != NULL; id = id->fti_next) {
1594 fasttrap_probe_t *probe = id->fti_probe;
1595
cf7d32b8
A
1596 if (ISSET(current_proc()->p_lflag, P_LNOATTACH)) {
1597 dtrace_probe(dtrace_probeid_error, 0 /* state */, probe->ftp_id,
1598 1 /* ndx */, -1 /* offset */, DTRACEFLT_UPRIV);
1599 } else if (id->fti_ptype == DTFTP_ENTRY) {
2d21ac55
A
1600 /*
1601 * We note that this was an entry
1602 * probe to help ustack() find the
1603 * first caller.
1604 */
1605 cookie = dtrace_interrupt_disable();
1606 DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY);
1607 dtrace_probe(probe->ftp_id, regs64->rdi,
1608 regs64->rsi, regs64->rdx, regs64->rcx,
1609 regs64->r8);
1610 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY);
1611 dtrace_interrupt_enable(cookie);
1612 } else if (id->fti_ptype == DTFTP_IS_ENABLED) {
1613 /*
1614 * Note that in this case, we don't
1615 * call dtrace_probe() since it's only
1616 * an artificial probe meant to change
1617 * the flow of control so that it
1618 * encounters the true probe.
1619 */
1620 is_enabled = 1;
1621 } else if (probe->ftp_argmap == NULL) {
1622 dtrace_probe(probe->ftp_id, regs64->rdi,
1623 regs64->rsi, regs64->rdx, regs64->rcx,
1624 regs64->r8);
1625 } else {
1626 uint64_t t[5];
1627
1628 fasttrap_usdt_args64(probe, regs64,
1629 sizeof (t) / sizeof (t[0]), t);
1630
1631 dtrace_probe(probe->ftp_id, t[0], t[1],
1632 t[2], t[3], t[4]);
1633 }
1634
1635 /* APPLE NOTE: Oneshot probes get one and only one chance... */
1636 if (probe->ftp_prov->ftp_provider_type == DTFTP_PROVIDER_ONESHOT) {
1637 fasttrap_tracepoint_remove(p, tp);
1638 }
1639 }
1640 }
1641
1642 /*
1643 * We're about to do a bunch of work so we cache a local copy of
1644 * the tracepoint to emulate the instruction, and then find the
1645 * tracepoint again later if we need to light up any return probes.
1646 */
1647 tp_local = *tp;
1648 lck_mtx_unlock(pid_mtx);
1649 tp = &tp_local;
1650
1651 /*
1652 * Set the program counter to appear as though the traced instruction
1653 * had completely executed. This ensures that fasttrap_getreg() will
1654 * report the expected value for REG_RIP.
1655 */
1656 regs64->isf.rip = pc + tp->ftt_size;
1657
1658 /*
1659 * If there's an is-enabled probe connected to this tracepoint it
1660 * means that there was a 'xorl %eax, %eax' or 'xorq %rax, %rax'
1661 * instruction that was placed there by DTrace when the binary was
1662 * linked. As this probe is, in fact, enabled, we need to stuff 1
1663 * into %eax or %rax. Accordingly, we can bypass all the instruction
1664 * emulation logic since we know the inevitable result. It's possible
1665 * that a user could construct a scenario where the 'is-enabled'
1666 * probe was on some other instruction, but that would be a rather
1667 * exotic way to shoot oneself in the foot.
1668 */
1669 if (is_enabled) {
1670 regs64->rax = 1;
1671 new_pc = regs64->isf.rip;
1672 goto done;
1673 }
1674
1675 /*
1676 * We emulate certain types of instructions to ensure correctness
1677 * (in the case of position dependent instructions) or optimize
1678 * common cases. The rest we have the thread execute back in user-
1679 * land.
1680 */
1681 switch (tp->ftt_type) {
1682 case FASTTRAP_T_RET:
1683 case FASTTRAP_T_RET16:
1684 {
1685 user_addr_t dst;
1686 user_addr_t addr;
1687 int ret;
1688
1689 /*
1690 * We have to emulate _every_ facet of the behavior of a ret
1691 * instruction including what happens if the load from %esp
1692 * fails; in that case, we send a SIGSEGV.
1693 */
1694 ret = fasttrap_fuword64((user_addr_t)regs64->isf.rsp, &dst);
1695 addr = regs64->isf.rsp + sizeof (uint64_t);
1696
1697 if (ret == -1) {
1698 fasttrap_sigsegv(p, uthread, (user_addr_t)regs64->isf.rsp);
1699 new_pc = pc;
1700 break;
1701 }
1702
1703 if (tp->ftt_type == FASTTRAP_T_RET16)
1704 addr += tp->ftt_dest;
1705
1706 regs64->isf.rsp = addr;
1707 new_pc = dst;
1708 break;
1709 }
1710
1711 case FASTTRAP_T_JCC:
1712 {
1713 uint_t taken;
1714
1715 switch (tp->ftt_code) {
1716 case FASTTRAP_JO:
1717 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_OF) != 0;
1718 break;
1719 case FASTTRAP_JNO:
1720 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_OF) == 0;
1721 break;
1722 case FASTTRAP_JB:
1723 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_CF) != 0;
1724 break;
1725 case FASTTRAP_JAE:
1726 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_CF) == 0;
1727 break;
1728 case FASTTRAP_JE:
1729 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_ZF) != 0;
1730 break;
1731 case FASTTRAP_JNE:
1732 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_ZF) == 0;
1733 break;
1734 case FASTTRAP_JBE:
1735 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_CF) != 0 ||
1736 (regs64->isf.rflags & FASTTRAP_EFLAGS_ZF) != 0;
1737 break;
1738 case FASTTRAP_JA:
1739 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_CF) == 0 &&
1740 (regs64->isf.rflags & FASTTRAP_EFLAGS_ZF) == 0;
1741 break;
1742 case FASTTRAP_JS:
1743 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_SF) != 0;
1744 break;
1745 case FASTTRAP_JNS:
1746 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_SF) == 0;
1747 break;
1748 case FASTTRAP_JP:
1749 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_PF) != 0;
1750 break;
1751 case FASTTRAP_JNP:
1752 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_PF) == 0;
1753 break;
1754 case FASTTRAP_JL:
1755 taken = ((regs64->isf.rflags & FASTTRAP_EFLAGS_SF) == 0) !=
1756 ((regs64->isf.rflags & FASTTRAP_EFLAGS_OF) == 0);
1757 break;
1758 case FASTTRAP_JGE:
1759 taken = ((regs64->isf.rflags & FASTTRAP_EFLAGS_SF) == 0) ==
1760 ((regs64->isf.rflags & FASTTRAP_EFLAGS_OF) == 0);
1761 break;
1762 case FASTTRAP_JLE:
1763 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_ZF) != 0 ||
1764 ((regs64->isf.rflags & FASTTRAP_EFLAGS_SF) == 0) !=
1765 ((regs64->isf.rflags & FASTTRAP_EFLAGS_OF) == 0);
1766 break;
1767 case FASTTRAP_JG:
1768 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_ZF) == 0 &&
1769 ((regs64->isf.rflags & FASTTRAP_EFLAGS_SF) == 0) ==
1770 ((regs64->isf.rflags & FASTTRAP_EFLAGS_OF) == 0);
1771 break;
1772 default:
1773 taken = FALSE;
1774 }
1775
1776 if (taken)
1777 new_pc = tp->ftt_dest;
1778 else
1779 new_pc = pc + tp->ftt_size;
1780 break;
1781 }
1782
1783 case FASTTRAP_T_LOOP:
1784 {
1785 uint_t taken;
1786 uint64_t cx = regs64->rcx--;
1787
1788 switch (tp->ftt_code) {
1789 case FASTTRAP_LOOPNZ:
1790 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_ZF) == 0 &&
1791 cx != 0;
1792 break;
1793 case FASTTRAP_LOOPZ:
1794 taken = (regs64->isf.rflags & FASTTRAP_EFLAGS_ZF) != 0 &&
1795 cx != 0;
1796 break;
1797 case FASTTRAP_LOOP:
1798 taken = (cx != 0);
1799 break;
1800 default:
1801 taken = FALSE;
1802 }
1803
1804 if (taken)
1805 new_pc = tp->ftt_dest;
1806 else
1807 new_pc = pc + tp->ftt_size;
1808 break;
1809 }
1810
1811 case FASTTRAP_T_JCXZ:
1812 {
1813 uint64_t cx = regs64->rcx;
1814
1815 if (cx == 0)
1816 new_pc = tp->ftt_dest;
1817 else
1818 new_pc = pc + tp->ftt_size;
1819 break;
1820 }
1821
1822 case FASTTRAP_T_PUSHL_EBP:
1823 {
1824 user_addr_t addr = regs64->isf.rsp - sizeof (uint64_t);
1825 int ret = fasttrap_suword64(addr, (uint64_t)regs64->rbp);
1826
1827 if (ret == -1) {
1828 fasttrap_sigsegv(p, uthread, addr);
1829 new_pc = pc;
1830 break;
1831 }
1832
1833 regs64->isf.rsp = addr;
1834 new_pc = pc + tp->ftt_size;
1835 break;
1836 }
1837
1838 case FASTTRAP_T_NOP:
1839 new_pc = pc + tp->ftt_size;
1840 break;
1841
1842 case FASTTRAP_T_JMP:
1843 case FASTTRAP_T_CALL:
1844 if (tp->ftt_code == 0) {
1845 new_pc = tp->ftt_dest;
1846 } else {
1847 user_addr_t value, addr = tp->ftt_dest;
1848
1849 if (tp->ftt_base != FASTTRAP_NOREG)
1850 addr += fasttrap_getreg(regs, tp->ftt_base);
1851 if (tp->ftt_index != FASTTRAP_NOREG)
1852 addr += fasttrap_getreg(regs, tp->ftt_index) <<
1853 tp->ftt_scale;
1854
1855 if (tp->ftt_code == 1) {
1856 /*
1857 * If there's a segment prefix for this
1858 * instruction, we'll need to check permissions
1859 * and bounds on the given selector, and adjust
1860 * the address accordingly.
1861 */
1862 if (tp->ftt_segment != FASTTRAP_SEG_NONE &&
1863 fasttrap_do_seg(tp, regs, &addr) != 0) {
1864 fasttrap_sigsegv(p, uthread, addr);
1865 new_pc = pc;
1866 break;
1867 }
1868
1869 if (fasttrap_fuword64(addr, &value) == -1) {
1870 fasttrap_sigsegv(p, uthread, addr);
1871 new_pc = pc;
1872 break;
1873 }
1874 new_pc = value;
1875 } else {
1876 new_pc = addr;
1877 }
1878 }
1879
1880 /*
1881 * If this is a call instruction, we need to push the return
1882 * address onto the stack. If this fails, we send the process
1883 * a SIGSEGV and reset the pc to emulate what would happen if
1884 * this instruction weren't traced.
1885 */
1886 if (tp->ftt_type == FASTTRAP_T_CALL) {
1887 user_addr_t addr = regs64->isf.rsp - sizeof (uint64_t);
1888 int ret = fasttrap_suword64(addr, pc + tp->ftt_size);
1889
1890 if (ret == -1) {
1891 fasttrap_sigsegv(p, uthread, addr);
1892 new_pc = pc;
1893 break;
1894 }
1895
1896 regs64->isf.rsp = addr;
1897 }
1898 break;
1899
1900 case FASTTRAP_T_COMMON:
1901 {
1902 user_addr_t addr;
b0d623f7 1903 uint8_t scratch[2 * FASTTRAP_MAX_INSTR_SIZE + 22];
2d21ac55
A
1904 uint_t i = 0;
1905
1906 /*
1907 * Generic Instruction Tracing
1908 * ---------------------------
1909 *
1910 * This is the layout of the scratch space in the user-land
1911 * thread structure for our generated instructions.
1912 *
1913 * 32-bit mode bytes
1914 * ------------------------ -----
1915 * a: <original instruction> <= 15
1916 * jmp <pc + tp->ftt_size> 5
1917 * b: <original instrction> <= 15
1918 * int T_DTRACE_RET 2
1919 * -----
1920 * <= 37
1921 *
1922 * 64-bit mode bytes
1923 * ------------------------ -----
1924 * a: <original instruction> <= 15
1925 * jmp 0(%rip) 6
1926 * <pc + tp->ftt_size> 8
1927 * b: <original instruction> <= 15
1928 * int T_DTRACE_RET 2
1929 * -----
1930 * <= 46
1931 *
1932 * The %pc is set to a, and curthread->t_dtrace_astpc is set
1933 * to b. If we encounter a signal on the way out of the
1934 * kernel, trap() will set %pc to curthread->t_dtrace_astpc
1935 * so that we execute the original instruction and re-enter
1936 * the kernel rather than redirecting to the next instruction.
1937 *
1938 * If there are return probes (so we know that we're going to
1939 * need to reenter the kernel after executing the original
1940 * instruction), the scratch space will just contain the
1941 * original instruction followed by an interrupt -- the same
1942 * data as at b.
1943 *
1944 * %rip-relative Addressing
1945 * ------------------------
1946 *
1947 * There's a further complication in 64-bit mode due to %rip-
1948 * relative addressing. While this is clearly a beneficial
1949 * architectural decision for position independent code, it's
1950 * hard not to see it as a personal attack against the pid
1951 * provider since before there was a relatively small set of
1952 * instructions to emulate; with %rip-relative addressing,
1953 * almost every instruction can potentially depend on the
1954 * address at which it's executed. Rather than emulating
1955 * the broad spectrum of instructions that can now be
1956 * position dependent, we emulate jumps and others as in
1957 * 32-bit mode, and take a different tack for instructions
1958 * using %rip-relative addressing.
1959 *
1960 * For every instruction that uses the ModRM byte, the
1961 * in-kernel disassembler reports its location. We use the
1962 * ModRM byte to identify that an instruction uses
1963 * %rip-relative addressing and to see what other registers
1964 * the instruction uses. To emulate those instructions,
1965 * we modify the instruction to be %rax-relative rather than
1966 * %rip-relative (or %rcx-relative if the instruction uses
1967 * %rax; or %r8- or %r9-relative if the REX.B is present so
1968 * we don't have to rewrite the REX prefix). We then load
1969 * the value that %rip would have been into the scratch
1970 * register and generate an instruction to reset the scratch
1971 * register back to its original value. The instruction
1972 * sequence looks like this:
1973 *
1974 * 64-mode %rip-relative bytes
1975 * ------------------------ -----
1976 * a: <modified instruction> <= 15
1977 * movq $<value>, %<scratch> 6
1978 * jmp 0(%rip) 6
1979 * <pc + tp->ftt_size> 8
1980 * b: <modified instruction> <= 15
1981 * int T_DTRACE_RET 2
1982 * -----
1983 * 52
1984 *
1985 * We set curthread->t_dtrace_regv so that upon receiving
1986 * a signal we can reset the value of the scratch register.
1987 */
1988
1989 addr = uthread->t_dtrace_scratch->addr;
1990
1991 if (addr == 0LL) {
1992 fasttrap_sigtrap(p, uthread, pc); // Should be killing target proc
1993 new_pc = pc;
1994 break;
1995 }
1996
1997 ASSERT(tp->ftt_size < FASTTRAP_MAX_INSTR_SIZE);
1998
1999 uthread->t_dtrace_scrpc = addr;
2000 bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
2001 i += tp->ftt_size;
2002
2003 if (tp->ftt_ripmode != 0) {
2004 uint64_t* reg;
2005
2006 ASSERT(tp->ftt_ripmode &
2007 (FASTTRAP_RIP_1 | FASTTRAP_RIP_2));
2008
2009 /*
2010 * If this was a %rip-relative instruction, we change
2011 * it to be either a %rax- or %rcx-relative
2012 * instruction (depending on whether those registers
2013 * are used as another operand; or %r8- or %r9-
2014 * relative depending on the value of REX.B). We then
2015 * set that register and generate a movq instruction
2016 * to reset the value.
2017 */
2018 if (tp->ftt_ripmode & FASTTRAP_RIP_X)
2019 scratch[i++] = FASTTRAP_REX(1, 0, 0, 1);
2020 else
2021 scratch[i++] = FASTTRAP_REX(1, 0, 0, 0);
2022
2023 if (tp->ftt_ripmode & FASTTRAP_RIP_1)
2024 scratch[i++] = FASTTRAP_MOV_EAX;
2025 else
2026 scratch[i++] = FASTTRAP_MOV_ECX;
2027
2028 switch (tp->ftt_ripmode) {
2029 case FASTTRAP_RIP_1:
2030 reg = &regs64->rax;
2031 uthread->t_dtrace_reg = REG_RAX;
2032 break;
2033 case FASTTRAP_RIP_2:
2034 reg = &regs64->rcx;
2035 uthread->t_dtrace_reg = REG_RCX;
2036 break;
2037 case FASTTRAP_RIP_1 | FASTTRAP_RIP_X:
2038 reg = &regs64->r8;
2039 uthread->t_dtrace_reg = REG_R8;
2040 break;
2041 case FASTTRAP_RIP_2 | FASTTRAP_RIP_X:
2042 reg = &regs64->r9;
2043 uthread->t_dtrace_reg = REG_R9;
2044 break;
2045 default:
2046 reg = NULL;
2047 panic("unhandled ripmode in fasttrap_pid_probe64");
2048 }
2049
b0d623f7 2050 /* LINTED - alignment */
2d21ac55
A
2051 *(uint64_t *)&scratch[i] = *reg;
2052 uthread->t_dtrace_regv = *reg;
2053 *reg = pc + tp->ftt_size;
2054 i += sizeof (uint64_t);
2055 }
2056
2057 /*
2058 * Generate the branch instruction to what would have
2059 * normally been the subsequent instruction. In 32-bit mode,
2060 * this is just a relative branch; in 64-bit mode this is a
2061 * %rip-relative branch that loads the 64-bit pc value
2062 * immediately after the jmp instruction.
2063 */
2064 scratch[i++] = FASTTRAP_GROUP5_OP;
2065 scratch[i++] = FASTTRAP_MODRM(0, 4, 5);
b0d623f7 2066 /* LINTED - alignment */
2d21ac55
A
2067 *(uint32_t *)&scratch[i] = 0;
2068 i += sizeof (uint32_t);
b0d623f7 2069 /* LINTED - alignment */
2d21ac55
A
2070 *(uint64_t *)&scratch[i] = pc + tp->ftt_size;
2071 i += sizeof (uint64_t);
2072
2073 uthread->t_dtrace_astpc = addr + i;
2074 bcopy(tp->ftt_instr, &scratch[i], tp->ftt_size);
2075 i += tp->ftt_size;
2076 scratch[i++] = FASTTRAP_INT;
2077 scratch[i++] = T_DTRACE_RET;
2078
b0d623f7
A
2079 ASSERT(i <= sizeof (scratch));
2080
2d21ac55
A
2081 if (fasttrap_copyout(scratch, addr, i)) {
2082 fasttrap_sigtrap(p, uthread, pc);
2083 new_pc = pc;
2084 break;
2085 }
2086
2087 if (tp->ftt_retids != NULL) {
2088 uthread->t_dtrace_step = 1;
2089 uthread->t_dtrace_ret = 1;
2090 new_pc = uthread->t_dtrace_astpc;
2091 } else {
2092 new_pc = uthread->t_dtrace_scrpc;
2093 }
2094
2095 uthread->t_dtrace_pc = pc;
2096 uthread->t_dtrace_npc = pc + tp->ftt_size;
2097 uthread->t_dtrace_on = 1;
2098 break;
2099 }
2100
2101 default:
2102 panic("fasttrap: mishandled an instruction");
2103 }
2104
2105done:
2106 /*
2107 * APPLE NOTE:
2108 *
2109 * We're setting this earlier than Solaris does, to get a "correct"
2110 * ustack() output. In the Sun code, a() -> b() -> c() -> d() is
2111 * reported at: d, b, a. The new way gives c, b, a, which is closer
2112 * to correct, as the return instruction has already exectued.
2113 */
2114 regs64->isf.rip = new_pc;
2115
2116
2117 /*
2118 * If there were no return probes when we first found the tracepoint,
2119 * we should feel no obligation to honor any return probes that were
2120 * subsequently enabled -- they'll just have to wait until the next
2121 * time around.
2122 */
2123 if (tp->ftt_retids != NULL) {
2124 /*
2125 * We need to wait until the results of the instruction are
2126 * apparent before invoking any return probes. If this
2127 * instruction was emulated we can just call
2128 * fasttrap_return_common(); if it needs to be executed, we
2129 * need to wait until the user thread returns to the kernel.
2130 */
2131 if (tp->ftt_type != FASTTRAP_T_COMMON) {
2132 fasttrap_return_common(regs, pc, pid, new_pc);
2133 } else {
2134 ASSERT(uthread->t_dtrace_ret != 0);
2135 ASSERT(uthread->t_dtrace_pc == pc);
2136 ASSERT(uthread->t_dtrace_scrpc != 0);
2137 ASSERT(new_pc == uthread->t_dtrace_astpc);
2138 }
2139 }
2140
2141 return (0);
2142}
2143
2144int
2145fasttrap_pid_probe(x86_saved_state_t *regs)
2146{
2147 if (is_saved_state64(regs))
2148 return fasttrap_pid_probe64(regs);
2149
2150 return fasttrap_pid_probe32(regs);
2151}
2152
2153int
2154fasttrap_return_probe(x86_saved_state_t *regs)
2155{
2156 x86_saved_state64_t *regs64;
2157 x86_saved_state32_t *regs32;
2158 unsigned int p_model;
2159
2160 if (is_saved_state64(regs)) {
2161 regs64 = saved_state64(regs);
2162 regs32 = NULL;
2163 p_model = DATAMODEL_LP64;
2164 } else {
2165 regs64 = NULL;
2166 regs32 = saved_state32(regs);
2167 p_model = DATAMODEL_ILP32;
2168 }
2169
2170 proc_t *p = current_proc();
2171 uthread_t uthread = (uthread_t)get_bsdthread_info(current_thread());
2172 user_addr_t pc = uthread->t_dtrace_pc;
2173 user_addr_t npc = uthread->t_dtrace_npc;
2174
2175 uthread->t_dtrace_pc = 0;
2176 uthread->t_dtrace_npc = 0;
2177 uthread->t_dtrace_scrpc = 0;
2178 uthread->t_dtrace_astpc = 0;
2179
2180 /*
2181 * Treat a child created by a call to vfork(2) as if it were its
2182 * parent. We know that there's only one thread of control in such a
2183 * process: this one.
2184 */
2185 /*
2186 * APPLE NOTE: Terry says: "You need to hold the process locks (currently: kernel funnel) for this traversal"
2187 * How do we assert this?
2188 */
2189 while (p->p_lflag & P_LINVFORK) {
2190 p = p->p_pptr;
2191 }
2192
2193 /*
2194 * We set rp->r_pc to the address of the traced instruction so
2195 * that it appears to dtrace_probe() that we're on the original
2196 * instruction, and so that the user can't easily detect our
2197 * complex web of lies. dtrace_return_probe() (our caller)
2198 * will correctly set %pc after we return.
2199 */
2200 if (p_model == DATAMODEL_LP64)
2201 regs64->isf.rip = pc;
2202 else
2203 regs32->eip = pc;
2204
2205 fasttrap_return_common(regs, pc, p->p_pid, npc);
2206
2207 return (0);
2208}
2209
b0d623f7 2210
2d21ac55
A
2211uint64_t
2212fasttrap_pid_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
2213 int aframes)
2214{
2215#pragma unused(arg, id, parg, aframes)
2216 return (fasttrap_anarg((x86_saved_state_t *)find_user_regs(current_thread()), 1, argno));
2217}
2218
2219uint64_t
2220fasttrap_usdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno,
2221 int aframes)
2222{
2223#pragma unused(arg, id, parg, aframes)
2224 return (fasttrap_anarg((x86_saved_state_t *)find_user_regs(current_thread()), 0, argno));
2225}
2226
2227/*
2228 * APPLE NOTE: See comments by regmap array definition. We are cheating
2229 * when returning 32 bit registers.
2230 */
2231static user_addr_t
2232fasttrap_getreg(x86_saved_state_t *regs, uint_t reg)
2233{
2234 if (is_saved_state64(regs)) {
2235 x86_saved_state64_t *regs64 = saved_state64(regs);
2236
2237 switch (reg) {
2238 case REG_RAX: return regs64->rax;
2239 case REG_RCX: return regs64->rcx;
2240 case REG_RDX: return regs64->rdx;
2241 case REG_RBX: return regs64->rbx;
2242 case REG_RSP: return regs64->isf.rsp;
2243 case REG_RBP: return regs64->rbp;
2244 case REG_RSI: return regs64->rsi;
2245 case REG_RDI: return regs64->rdi;
2246 case REG_R8: return regs64->r8;
2247 case REG_R9: return regs64->r9;
2248 case REG_R10: return regs64->r10;
2249 case REG_R11: return regs64->r11;
2250 case REG_R12: return regs64->r12;
2251 case REG_R13: return regs64->r13;
2252 case REG_R14: return regs64->r14;
2253 case REG_R15: return regs64->r15;
b0d623f7
A
2254 case REG_TRAPNO: return regs64->isf.trapno;
2255 case REG_ERR: return regs64->isf.err;
2256 case REG_RIP: return regs64->isf.rip;
2257 case REG_CS: return regs64->isf.cs;
2258 case REG_RFL: return regs64->isf.rflags;
2259 case REG_SS: return regs64->isf.ss;
2260 case REG_FS: return regs64->fs;
2261 case REG_GS: return regs64->gs;
2262 case REG_ES:
2263 case REG_DS:
2264 case REG_FSBASE:
2265 case REG_GSBASE:
2266 // Important to distinguish these requests (which should be legal) from other values.
2267 panic("dtrace: unimplemented x86_64 getreg()");
2d21ac55
A
2268 }
2269
2270 panic("dtrace: unhandled x86_64 getreg() constant");
2271 } else {
2272 x86_saved_state32_t *regs32 = saved_state32(regs);
2273
2274 switch (reg) {
2275 case REG_RAX: return regs32->eax;
2276 case REG_RCX: return regs32->ecx;
2277 case REG_RDX: return regs32->edx;
2278 case REG_RBX: return regs32->ebx;
2279 case REG_RSP: return regs32->uesp;
2280 case REG_RBP: return regs32->ebp;
2281 case REG_RSI: return regs32->esi;
2282 case REG_RDI: return regs32->edi;
2283 }
2284
2285 panic("dtrace: unhandled i386 getreg() constant");
2286 }
2287
2288 return 0;
2289}