lkml.org 
[lkml]   [2012]   [Jan]   [25]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Date
SubjectRe: [PATCH v9 3.2 1/9] uprobes: Install and remove breakpoints.
On Tue, Jan 10, 2012 at 12:48 PM, Srikar Dronamraju
<srikar@linux.vnet.ibm.com> wrote:
> +/*
> + * If uprobe->insn doesn't use rip-relative addressing, return
> + * immediately.  Otherwise, rewrite the instruction so that it accesses
> + * its memory operand indirectly through a scratch register.  Set
> + * uprobe->arch_info.fixups and uprobe->arch_info.rip_rela_target_address
> + * accordingly.  (The contents of the scratch register will be saved
> + * before we single-step the modified instruction, and restored
> + * afterward.)
> + *
> + * We do this because a rip-relative instruction can access only a
> + * relatively small area (+/- 2 GB from the instruction), and the XOL
> + * area typically lies beyond that area.  At least for instructions
> + * that store to memory, we can't execute the original instruction
> + * and "fix things up" later, because the misdirected store could be
> + * disastrous.
> + *
> + * Some useful facts about rip-relative instructions:
> + * - There's always a modrm byte.
> + * - There's never a SIB byte.
> + * - The displacement is always 4 bytes.
> + */
> +static void handle_riprel_insn(struct mm_struct *mm, struct uprobe *uprobe,
> +                                                       struct insn *insn)
> +{
> +       u8 *cursor;
> +       u8 reg;
> +
> +       if (mm->context.ia32_compat)
> +               return;
> +
> +       uprobe->arch_info.rip_rela_target_address = 0x0;
> +       if (!insn_rip_relative(insn))
> +               return;
> +
> +       /*
> +        * Point cursor at the modrm byte.  The next 4 bytes are the
> +        * displacement.  Beyond the displacement, for some instructions,
> +        * is the immediate operand.
> +        */
> +       cursor = uprobe->insn + insn->prefixes.nbytes
> +                       + insn->rex_prefix.nbytes + insn->opcode.nbytes;
> +       insn_get_length(insn);
> +
> +       /*
> +        * Convert from rip-relative addressing to indirect addressing
> +        * via a scratch register.  Change the r/m field from 0x5 (%rip)
> +        * to 0x0 (%rax) or 0x1 (%rcx), and squeeze out the offset field.
> +        */
> +       reg = MODRM_REG(insn);
> +       if (reg == 0) {
> +               /*
> +                * The register operand (if any) is either the A register
> +                * (%rax, %eax, etc.) or (if the 0x4 bit is set in the
> +                * REX prefix) %r8.  In any case, we know the C register
> +                * is NOT the register operand, so we use %rcx (register
> +                * #1) for the scratch register.
> +                */
> +               uprobe->arch_info.fixups = UPROBES_FIX_RIP_CX;
> +               /* Change modrm from 00 000 101 to 00 000 001. */
> +               *cursor = 0x1;
> +       } else {
> +               /* Use %rax (register #0) for the scratch register. */
> +               uprobe->arch_info.fixups = UPROBES_FIX_RIP_AX;
> +               /* Change modrm from 00 xxx 101 to 00 xxx 000 */
> +               *cursor = (reg << 3);
> +       }
> +
> +       /* Target address = address of next instruction + (signed) offset */
> +       uprobe->arch_info.rip_rela_target_address = (long)insn->length
> +                                       + insn->displacement.value;
> +       /* Displacement field is gone; slide immediate field (if any) over. */
> +       if (insn->immediate.nbytes) {
> +               cursor++;
> +               memmove(cursor, cursor + insn->displacement.nbytes,
> +                                               insn->immediate.nbytes);
> +       }
> +       return;
> +}

It seems to be possible to store RIP value *without displacement*
into AX/CX and convert rip-relative instruction into AX/CX *relative* one.
Example:
c7 05 78 56 34 12 2a 00 00 00 movl $0x2a,0x12345678(%rip)
converts to:
c7 81 78 56 34 12 2a 00 00 00 movl $0x2a,0x12345678(%rcx)

This way instruction size stays the same and you don't need
to memmove immediate value.

--
vda
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2012-01-25 16:17    [W:0.225 / U:0.312 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site