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