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