/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/fasttrap_isa.h>
#include <sys/fasttrap_impl.h>
#include <sys/dtrace_impl.h>
#include <sys/privregs.h>
#include <sys/segments.h>
#include <sys/x86_archext.h>
#include <sys/sysmacros.h>
#include <sys/archsystm.h>
/*
* Lossless User-Land Tracing on x86
* ---------------------------------
*
* The execution of most instructions is not dependent on the address; for
* these instructions it is sufficient to copy them into the user process's
* address space and execute them. To effectively single-step an instruction
* in user-land, we copy out the following sequence of instructions to scratch
* space in the user thread's ulwp_t structure.
*
* We then set the program counter (%eip or %rip) to point to this scratch
* space. Once execution resumes, the original instruction is executed and
* then control flow is redirected to what was originally the subsequent
* instruction. If the kernel attemps to deliver a signal while single-
* stepping, the signal is deferred and the program counter is moved into the
* second sequence of instructions. The second sequence ends in a trap into
* the kernel where the deferred signal is then properly handled and delivered.
*
* For instructions whose execute is position dependent, we perform simple
* emulation. These instructions are limited to control transfer
* instructions in 32-bit mode, but in 64-bit mode there's the added wrinkle
* of %rip-relative addressing that means that almost any instruction can be
* position dependent. For all the details on how we emulate generic
* instructions included %rip-relative instructions, see the code in
* fasttrap_pid_probe() below where we handle instructions of type
* FASTTRAP_T_COMMON (under the header: Generic Instruction Tracing).
*/
#define FASTTRAP_REX(w, r, x, b) \
(0x40 | ((w) << 3) | ((r) << 2) | ((x) << 1) | (b))
/*
* Single-byte op-codes.
*/
/*
* Two-byte op-codes (second byte only).
*/
/*
* Instruction prefixes.
*/
/*
* Map between instruction register encodings and the kernel constants which
* correspond to indicies into struct regs.
*/
#ifdef __amd64
};
#else
};
#endif
static uint64_t
{
#ifdef __amd64
/*
* In 64-bit mode, the first six arguments are stored in
* registers.
*/
if (argno < 6)
} else {
#endif
#ifdef __amd64
}
#endif
return (value);
}
/*ARGSUSED*/
int
{
/*
* Read the instruction at the given address out of the process's
* address space. We don't have to worry about a debugger
* changing this instruction before we overwrite it with our trap
* instruction since P_PR_LOCK is set. Since instructions can span
* pages, we potentially read the instruction in two parts. If the
* second part fails, we just zero out that part of the instruction.
*/
return (-1);
}
/*
* If the disassembly fails, then we have a malformed instruction.
*/
return (-1);
/*
* Make sure the disassembler isn't completely broken.
*/
/*
* If the computed size is greater than the number of bytes read,
* then it was a malformed instruction possibly because it fell on a
* page boundary and the subsequent page was missing or because of
* some malicious user.
*/
return (-1);
/*
* Find the start of the instruction's opcode by processing any
* legacy prefixes.
*/
for (;;) {
seg = 0;
case FASTTRAP_PREFIX_SS:
seg++;
/*FALLTHRU*/
case FASTTRAP_PREFIX_GS:
seg++;
/*FALLTHRU*/
case FASTTRAP_PREFIX_FS:
seg++;
/*FALLTHRU*/
case FASTTRAP_PREFIX_ES:
seg++;
/*FALLTHRU*/
case FASTTRAP_PREFIX_DS:
seg++;
/*FALLTHRU*/
case FASTTRAP_PREFIX_CS:
seg++;
/*FALLTHRU*/
case FASTTRAP_PREFIX_OPERAND:
case FASTTRAP_PREFIX_ADDRESS:
case FASTTRAP_PREFIX_LOCK:
case FASTTRAP_PREFIX_REP:
case FASTTRAP_PREFIX_REPNE:
if (seg != 0) {
/*
* It's illegal for an instruction to specify
* two segment prefixes -- give up on this
* illegal instruction.
*/
return (-1);
}
start++;
continue;
}
break;
}
#ifdef __amd64
/*
* Identify the REX prefix on 64-bit processes.
*/
#endif
/*
* Now that we're pretty sure that the instruction is okay, copy the
* valid part to the tracepoint.
*/
case FASTTRAP_0F_JO:
case FASTTRAP_0F_JNO:
case FASTTRAP_0F_JB:
case FASTTRAP_0F_JAE:
case FASTTRAP_0F_JE:
case FASTTRAP_0F_JNE:
case FASTTRAP_0F_JBE:
case FASTTRAP_0F_JA:
case FASTTRAP_0F_JS:
case FASTTRAP_0F_JNS:
case FASTTRAP_0F_JP:
case FASTTRAP_0F_JNP:
case FASTTRAP_0F_JL:
case FASTTRAP_0F_JGE:
case FASTTRAP_0F_JLE:
case FASTTRAP_0F_JG:
/* LINTED - alignment */
break;
}
if (reg == 2)
else
if (mod == 3)
else
/*
* See AMD x86-64 Architecture Programmer's Manual
* Volume 3, Section 1.2.7, Table 1-12, and
* Appendix A.3.1, Table A-15.
*/
i = 3;
} else {
/*
* In 64-bit mode, mod == 0 and r/m == 5
* denotes %rip-relative addressing; in 32-bit
* mode, the base register isn't used. In both
* modes, there is a 32-bit operand.
*/
#ifdef __amd64
if (p->p_model == DATAMODEL_LP64)
else
#endif
sz = 4;
} else {
}
i = 2;
}
if (sz == 1) {
} else if (sz == 4) {
/* LINTED - alignment */
} else {
}
}
} else {
case FASTTRAP_RET:
break;
case FASTTRAP_RET16:
/* LINTED - alignment */
break;
case FASTTRAP_JO:
case FASTTRAP_JNO:
case FASTTRAP_JB:
case FASTTRAP_JAE:
case FASTTRAP_JE:
case FASTTRAP_JNE:
case FASTTRAP_JBE:
case FASTTRAP_JA:
case FASTTRAP_JS:
case FASTTRAP_JNS:
case FASTTRAP_JP:
case FASTTRAP_JNP:
case FASTTRAP_JL:
case FASTTRAP_JGE:
case FASTTRAP_JLE:
case FASTTRAP_JG:
break;
case FASTTRAP_LOOPNZ:
case FASTTRAP_LOOPZ:
case FASTTRAP_LOOP:
break;
case FASTTRAP_JCXZ:
break;
case FASTTRAP_CALL:
/* LINTED - alignment */
break;
case FASTTRAP_JMP32:
/* LINTED - alignment */
break;
case FASTTRAP_JMP8:
break;
case FASTTRAP_PUSHL_EBP:
if (start == 0)
break;
case FASTTRAP_NOP:
#ifdef __amd64
/*
* On amd64 we have to be careful not to confuse a nop
* (actually xchgl %eax, %eax) with an instruction using
* the same opcode, but that does something different
* (e.g. xchgl %r8d, %eax or xcghq %r8, %rax).
*/
if (FASTTRAP_REX_B(rex) == 0)
#endif
break;
case FASTTRAP_INT3:
/*
* The pid provider shares the int3 trap with debugger
* breakpoints so we can't instrument them.
*/
return (-1);
case FASTTRAP_INT:
/*
* Interrupts seem like they could be traced with
* no negative implications, but it's possible that
* a thread could be redirected by the trap handling
* code which would eventually return to the
* instruction after the interrupt. If the interrupt
* were in our scratch space, the subsequent
* instruction might be overwritten before we return.
* Accordingly we refuse to instrument any interrupt.
*/
return (-1);
}
}
#ifdef __amd64
/*
* If the process is 64-bit and the instruction type is still
* FASTTRAP_T_COMMON -- meaning we're going to copy it out an
* execute it -- we need to watch for %rip-relative
* addressing mode. See the portion of fasttrap_pid_probe()
* below where we handle tracepoints with type
* FASTTRAP_T_COMMON for how we emulate instructions that
* employ %rip-relative addressing.
*/
if (rmindex != -1) {
/*
* We need to be sure to avoid other
* registers used by this instruction. While
* the reg field may determine the op code
* rather than denoting a register, assuming
* that it denotes a register is always safe.
* We leave the REX field intact and use
* whatever value's there for simplicity.
*/
if (reg != 0) {
rm = 0;
} else {
rm = 1;
}
}
}
}
#endif
return (0);
}
int
{
return (-1);
return (0);
}
int
{
/*
* Distinguish between read or write failures and a changed
* instruction.
*/
return (0);
if (instr != FASTTRAP_INSTR)
return (0);
return (-1);
return (0);
}
#ifdef __amd64
static uintptr_t
{
return (ret);
return (0);
}
#endif
static uint32_t
{
return (ret);
return (0);
}
static void
{
break;
}
/*
* Don't sweat it if we can't find the tracepoint again; unlike
* when we're in fasttrap_pid_probe(), finding the tracepoint here
* is not essential to the correct execution of the process.
*/
return;
}
/*
* If there's a branch that could act as a return site, we
* need to trace it, and check here if the program counter is
* external to the function.
*/
continue;
}
}
static void
{
mutex_enter(&p->p_lock);
mutex_exit(&p->p_lock);
if (t != NULL)
aston(t);
}
#ifdef __amd64
static void
{
for (i = 0; i < cap; i++) {
x = probe->ftp_argmap[i];
if (x < 6)
else
}
for (; i < argc; i++) {
argv[i] = 0;
}
}
#endif
static void
{
for (i = 0; i < cap; i++) {
x = probe->ftp_argmap[i];
}
for (; i < argc; i++) {
argv[i] = 0;
}
}
static int
{
switch (tp->ftt_segment) {
case FASTTRAP_SEG_CS:
break;
case FASTTRAP_SEG_DS:
break;
case FASTTRAP_SEG_ES:
break;
case FASTTRAP_SEG_FS:
break;
case FASTTRAP_SEG_GS:
break;
case FASTTRAP_SEG_SS:
break;
}
/*
* Make sure the given segment register specifies a user priority
* selector rather than a kernel selector.
*/
return (-1);
/*
* Check the bounds and grab the descriptor out of the specified
* descriptor table.
*/
if (ndx > p->p_ldtlimit)
return (-1);
} else {
return (-1);
}
/*
* The descriptor must have user privilege level and it must be
* present in memory.
*/
return (-1);
/*
* If the S bit in the type field is not set, this descriptor can
* only be used in system context.
*/
return (-1);
/*
*/
return (-1);
return (-1);
} else {
/*
*/
if ((type & 0x8) != 0)
return (-1);
/*
* If the expand-down bit is clear, we just check the limit as
* it would naturally be applied. Otherwise, we need to check
* that the address is the range [limit + 1 .. 0xffff] or
* [limit + 1 ... 0xffffffff] depending on if the default
* operand size bit is set.
*/
if ((type & 0x4) == 0) {
return (-1);
return (-1);
} else {
return (-1);
}
}
return (0);
}
int
{
/*
* It's possible that a user (in a veritable orgy of bad planning)
* could redirect this thread's flow of control before it reached the
* return probe fasttrap. In this case we need to kill the process
* since it's in a unrecoverable state.
*/
if (curthread->t_dtrace_step) {
return (0);
}
/*
* Clear all user tracing flags.
*/
curthread->t_dtrace_ft = 0;
curthread->t_dtrace_pc = 0;
curthread->t_dtrace_npc = 0;
curthread->t_dtrace_scrpc = 0;
curthread->t_dtrace_astpc = 0;
#ifdef __amd64
curthread->t_dtrace_regv = 0;
#endif
/*
* Treat a child created by a call to vfork(2) as if it were its
* parent. We know that there's only one thread of control in such a
* process: this one.
*/
p = p->p_parent;
}
/*
* Lookup the tracepoint that the process just hit.
*/
break;
}
/*
* If we couldn't find a matching tracepoint, either a tracepoint has
* been inserted without using the pid<pid> ioctl interface (see
* fasttrap_ioctl), or somehow we have mislaid this tracepoint.
*/
return (-1);
}
/*
* Set the program counter to the address of the traced instruction
* so that it looks right in ustack() output.
*/
#ifdef __amd64
if (p->p_model == DATAMODEL_LP64) {
/*
* We note that this was an entry
* probe to help ustack() find the
* first caller.
*/
/*
* Note that in this case, we don't
* call dtrace_probe() since it's only
* an artificial probe meant to change
* the flow of control so that it
* encounters the true probe.
*/
is_enabled = 1;
} else {
uintptr_t t[5];
sizeof (t) / sizeof (t[0]), t);
t[2], t[3], t[4]);
}
}
} else {
#endif
/*
* In 32-bit mode, all arguments are passed on the
* stack. If this is a function entry probe, we need
* to skip the first entry on the stack as it
* represents the return address rather than a
* parameter to the function.
*/
/*
* We note that this was an entry
* probe to help ustack() find the
* first caller.
*/
/*
* Note that in this case, we don't
* call dtrace_probe() since it's only
* an artificial probe meant to change
* the flow of control so that it
* encounters the true probe.
*/
is_enabled = 1;
} else {
uint32_t t[5];
sizeof (t) / sizeof (t[0]), t);
t[2], t[3], t[4]);
}
}
#ifdef __amd64
}
#endif
}
/*
* We're about to do a bunch of work so we cache a local copy of
* the tracepoint to emulate the instruction, and then find the
* tracepoint again later if we need to light up any return probes.
*/
/*
* Set the program counter to appear as though the traced instruction
* had completely executed. This ensures that fasttrap_getreg() will
* report the expected value for REG_RIP.
*/
/*
* If there's an is-enabled probe connected to this tracepoint it
* means that there was a 'xorl %eax, %eax' or 'xorq %rax, %rax'
* instruction that was placed there by DTrace when the binary was
* linked. As this probe is, in fact, enabled, we need to stuff 1
* into %eax or %rax. Accordingly, we can bypass all the instruction
* emulation logic since we know the inevitable result. It's possible
* that a user could construct a scenario where the 'is-enabled'
* probe was on some other instruction, but that would be a rather
* exotic way to shoot oneself in the foot.
*/
if (is_enabled) {
goto done;
}
/*
* We emulate certain types of instructions to ensure correctness
* (in the case of position dependent instructions) or optimize
* common cases. The rest we have the thread execute back in user-
* land.
*/
case FASTTRAP_T_RET:
case FASTTRAP_T_RET16:
{
int ret;
/*
* We have to emulate _every_ facet of the behavior of a ret
* instruction including what happens if the load from %esp
* fails; in that case, we send a SIGSEGV.
*/
#ifdef __amd64
if (p->p_model == DATAMODEL_NATIVE) {
#endif
#ifdef __amd64
} else {
}
#endif
if (ret == -1) {
break;
}
break;
}
case FASTTRAP_T_JCC:
{
case FASTTRAP_JO:
break;
case FASTTRAP_JNO:
break;
case FASTTRAP_JB:
break;
case FASTTRAP_JAE:
break;
case FASTTRAP_JE:
break;
case FASTTRAP_JNE:
break;
case FASTTRAP_JBE:
break;
case FASTTRAP_JA:
break;
case FASTTRAP_JS:
break;
case FASTTRAP_JNS:
break;
case FASTTRAP_JP:
break;
case FASTTRAP_JNP:
break;
case FASTTRAP_JL:
break;
case FASTTRAP_JGE:
break;
case FASTTRAP_JLE:
break;
case FASTTRAP_JG:
break;
}
if (taken)
else
break;
}
case FASTTRAP_T_LOOP:
{
#ifdef __amd64
#else
#endif
case FASTTRAP_LOOPNZ:
cx != 0;
break;
case FASTTRAP_LOOPZ:
cx != 0;
break;
case FASTTRAP_LOOP:
break;
}
if (taken)
else
break;
}
case FASTTRAP_T_JCXZ:
{
#ifdef __amd64
#else
#endif
if (cx == 0)
else
break;
}
case FASTTRAP_T_PUSHL_EBP:
{
int ret;
#ifdef __amd64
if (p->p_model == DATAMODEL_NATIVE) {
#endif
#ifdef __amd64
} else {
}
#endif
if (ret == -1) {
break;
}
break;
}
case FASTTRAP_T_NOP:
break;
case FASTTRAP_T_JMP:
case FASTTRAP_T_CALL:
} else {
/*
* If there's a segment prefix for this
* instruction, we'll need to check permissions
* and bounds on the given selector, and adjust
* the address accordingly.
*/
break;
}
#ifdef __amd64
if (p->p_model == DATAMODEL_NATIVE) {
#endif
if (fasttrap_fulword((void *)addr,
&value) == -1) {
addr);
break;
}
#ifdef __amd64
} else {
if (fasttrap_fuword32((void *)addr,
&value32) == -1) {
addr);
break;
}
}
#endif
} else {
}
}
/*
* If this is a call instruction, we need to push the return
* address onto the stack. If this fails, we send the process
* a SIGSEGV and reset the pc to emulate what would happen if
* this instruction weren't traced.
*/
int ret;
#ifdef __amd64
if (p->p_model == DATAMODEL_NATIVE) {
} else {
#endif
#ifdef __amd64
}
#endif
if (ret == -1) {
break;
}
}
break;
case FASTTRAP_T_COMMON:
{
#if defined(__amd64)
#else
#endif
uint_t i = 0;
/*
* Compute the address of the ulwp_t and step over the
* ul_self pointer. The method used to store the user-land
* thread pointer is very different on 32- and 64-bit
* kernels.
*/
#if defined(__amd64)
if (p->p_model == DATAMODEL_LP64) {
addr += sizeof (void *);
} else {
}
#else
addr += sizeof (void *);
#endif
/*
* Generic Instruction Tracing
* ---------------------------
*
* This is the layout of the scratch space in the user-land
* thread structure for our generated instructions.
*
* 32-bit mode bytes
* ------------------------ -----
* a: <original instruction> <= 15
* jmp <pc + tp->ftt_size> 5
* b: <original instrction> <= 15
* int T_DTRACE_RET 2
* -----
* <= 37
*
* 64-bit mode bytes
* ------------------------ -----
* a: <original instruction> <= 15
* jmp 0(%rip) 6
* <pc + tp->ftt_size> 8
* b: <original instruction> <= 15
* int T_DTRACE_RET 2
* -----
* <= 46
*
* The %pc is set to a, and curthread->t_dtrace_astpc is set
* to b. If we encounter a signal on the way out of the
* kernel, trap() will set %pc to curthread->t_dtrace_astpc
* so that we execute the original instruction and re-enter
* the kernel rather than redirecting to the next instruction.
*
* If there are return probes (so we know that we're going to
* need to reenter the kernel after executing the original
* instruction), the scratch space will just contain the
* original instruction followed by an interrupt -- the same
* data as at b.
*
* %rip-relative Addressing
* ------------------------
*
* There's a further complication in 64-bit mode due to %rip-
* relative addressing. While this is clearly a beneficial
* architectural decision for position independent code, it's
* hard not to see it as a personal attack against the pid
* provider since before there was a relatively small set of
* instructions to emulate; with %rip-relative addressing,
* almost every instruction can potentially depend on the
* address at which it's executed. Rather than emulating
* the broad spectrum of instructions that can now be
* position dependent, we emulate jumps and others as in
* 32-bit mode, and take a different tack for instructions
* using %rip-relative addressing.
*
* For every instruction that uses the ModRM byte, the
* in-kernel disassembler reports its location. We use the
* ModRM byte to identify that an instruction uses
* %rip-relative addressing and to see what other registers
* the instruction uses. To emulate those instructions,
* we modify the instruction to be %rax-relative rather than
* %rip-relative (or %rcx-relative if the instruction uses
* %rax; or %r8- or %r9-relative if the REX.B is present so
* we don't have to rewrite the REX prefix). We then load
* the value that %rip would have been into the scratch
* register and generate an instruction to reset the scratch
* register back to its original value. The instruction
* sequence looks like this:
*
* 64-mode %rip-relative bytes
* ------------------------ -----
* a: <modified instruction> <= 15
* movq $<value>, %<scratch> 6
* jmp 0(%rip) 6
* <pc + tp->ftt_size> 8
* b: <modified instruction> <= 15
* int T_DTRACE_RET 2
* -----
* 52
*
* We set curthread->t_dtrace_regv so that upon receiving
* a signal we can reset the value of the scratch register.
*/
#ifdef __amd64
if (tp->ftt_ripmode != 0) {
(FASTTRAP_RIP_1 | FASTTRAP_RIP_2));
/*
* If this was a %rip-relative instruction, we change
* it to be either a %rax- or %rcx-relative
* instruction (depending on whether those registers
* are used as another operand; or %r8- or %r9-
* relative depending on the value of REX.B). We then
* set that register and generate a movq instruction
* to reset the value.
*/
else
scratch[i++] = FASTTRAP_MOV_EAX;
else
scratch[i++] = FASTTRAP_MOV_ECX;
switch (tp->ftt_ripmode) {
case FASTTRAP_RIP_1:
break;
case FASTTRAP_RIP_2:
break;
case FASTTRAP_RIP_1 | FASTTRAP_RIP_X:
break;
case FASTTRAP_RIP_2 | FASTTRAP_RIP_X:
break;
}
/* LINTED - alignment */
i += sizeof (uint64_t);
}
#endif
/*
* Generate the branch instruction to what would have
* normally been the subsequent instruction. In 32-bit mode,
* this is just a relative branch; in 64-bit mode this is a
* %rip-relative branch that loads the 64-bit pc value
* immediately after the jmp instruction.
*/
#ifdef __amd64
if (p->p_model == DATAMODEL_LP64) {
scratch[i++] = FASTTRAP_GROUP5_OP;
/* LINTED - alignment */
i += sizeof (uint32_t);
/* LINTED - alignment */
i += sizeof (uint64_t);
} else {
#endif
/*
* Set up the jmp to the next instruction; note that
* the size of the traced instruction cancels out.
*/
scratch[i++] = FASTTRAP_JMP32;
/* LINTED - alignment */
i += sizeof (uint32_t);
#ifdef __amd64
}
#endif
scratch[i++] = FASTTRAP_INT;
scratch[i++] = T_DTRACE_RET;
break;
}
} else {
}
break;
}
default:
panic("fasttrap: mishandled an instruction");
}
done:
/*
* If there were no return probes when we first found the tracepoint,
* we should feel no obligation to honor any return probes that were
* subsequently enabled -- they'll just have to wait until the next
* time around.
*/
/*
* We need to wait until the results of the instruction are
* apparent before invoking any return probes. If this
* instruction was emulated we can just call
* fasttrap_return_common(); if it needs to be executed, we
* need to wait until the user thread returns to the kernel.
*/
/*
* Set the program counter to the address of the traced
* instruction so that it looks right in ustack()
* output. We had previously set it to the end of the
* instruction to simplify %rip-relative addressing.
*/
} else {
}
}
return (0);
}
int
{
curthread->t_dtrace_pc = 0;
curthread->t_dtrace_npc = 0;
curthread->t_dtrace_scrpc = 0;
curthread->t_dtrace_astpc = 0;
/*
* Treat a child created by a call to vfork(2) as if it were its
* parent. We know that there's only one thread of control in such a
* process: this one.
*/
p = p->p_parent;
}
/*
* We set rp->r_pc to the address of the traced instruction so
* that it appears to dtrace_probe() that we're on the original
* instruction, and so that the user can't easily detect our
* complex web of lies. dtrace_return_probe() (our caller)
* will correctly set %pc after we return.
*/
return (0);
}
/*ARGSUSED*/
int aframes)
{
}
/*ARGSUSED*/
int aframes)
{
}
static ulong_t
{
#ifdef __amd64
switch (reg) {
}
panic("dtrace: illegal register constant");
/*NOTREACHED*/
#else
panic("dtrace: illegal register constant");
#endif
}