Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * arch/arm/probes/kprobes/checkers-arm.c
0004  *
0005  * Copyright (C) 2014 Huawei Inc.
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include "../decode.h"
0010 #include "../decode-arm.h"
0011 #include "checkers.h"
0012 
0013 static enum probes_insn __kprobes arm_check_stack(probes_opcode_t insn,
0014         struct arch_probes_insn *asi,
0015         const struct decode_header *h)
0016 {
0017     /*
0018      * PROBES_LDRSTRD, PROBES_LDMSTM, PROBES_STORE,
0019      * PROBES_STORE_EXTRA may get here. Simply mark all normal
0020      * insns as STACK_USE_NONE.
0021      */
0022     static const union decode_item table[] = {
0023         /*
0024          * 'STR{,D,B,H}, Rt, [Rn, Rm]' should be marked as UNKNOWN
0025          * if Rn or Rm is SP.
0026          *                                 x
0027          * STR (register)   cccc 011x x0x0 xxxx xxxx xxxx xxxx xxxx
0028          * STRB (register)  cccc 011x x1x0 xxxx xxxx xxxx xxxx xxxx
0029          */
0030         DECODE_OR   (0x0e10000f, 0x0600000d),
0031         DECODE_OR   (0x0e1f0000, 0x060d0000),
0032 
0033         /*
0034          *                                                     x
0035          * STRD (register)  cccc 000x x0x0 xxxx xxxx xxxx 1111 xxxx
0036          * STRH (register)  cccc 000x x0x0 xxxx xxxx xxxx 1011 xxxx
0037          */
0038         DECODE_OR   (0x0e5000bf, 0x000000bd),
0039         DECODE_CUSTOM   (0x0e5f00b0, 0x000d00b0, STACK_USE_UNKNOWN),
0040 
0041         /*
0042          * For PROBES_LDMSTM, only stmdx sp, [...] need to examine
0043          *
0044          * Bit B/A (bit 24) encodes arithmetic operation order. 1 means
0045          * before, 0 means after.
0046          * Bit I/D (bit 23) encodes arithmetic operation. 1 means
0047          * increment, 0 means decrement.
0048          *
0049          * So:
0050          *                              B I
0051          *                              / /
0052          *                              A D   | Rn |
0053          * STMDX SP, [...]  cccc 100x 00x0 xxxx xxxx xxxx xxxx xxxx
0054          */
0055         DECODE_CUSTOM   (0x0edf0000, 0x080d0000, STACK_USE_STMDX),
0056 
0057         /*                              P U W | Rn | Rt |     imm12    |*/
0058         /* STR (immediate)  cccc 010x x0x0 1101 xxxx xxxx xxxx xxxx */
0059         /* STRB (immediate) cccc 010x x1x0 1101 xxxx xxxx xxxx xxxx */
0060         /*                              P U W | Rn | Rt |imm4|    |imm4|*/
0061         /* STRD (immediate) cccc 000x x1x0 1101 xxxx xxxx 1111 xxxx */
0062         /* STRH (immediate) cccc 000x x1x0 1101 xxxx xxxx 1011 xxxx */
0063         /*
0064          * index = (P == '1'); add = (U == '1').
0065          * Above insns with:
0066          *    index == 0 (str{,d,h} rx, [sp], #+/-imm) or
0067          *    add == 1 (str{,d,h} rx, [sp, #+<imm>])
0068          * should be STACK_USE_NONE.
0069          * Only str{,b,d,h} rx,[sp,#-n] (P == 1 and U == 0) are
0070          * required to be examined.
0071          */
0072         /* STR{,B} Rt,[SP,#-n]  cccc 0101 0xx0 1101 xxxx xxxx xxxx xxxx */
0073         DECODE_CUSTOM   (0x0f9f0000, 0x050d0000, STACK_USE_FIXED_XXX),
0074 
0075         /* STR{D,H} Rt,[SP,#-n] cccc 0001 01x0 1101 xxxx xxxx 1x11 xxxx */
0076         DECODE_CUSTOM   (0x0fdf00b0, 0x014d00b0, STACK_USE_FIXED_X0X),
0077 
0078         /* fall through */
0079         DECODE_CUSTOM   (0, 0, STACK_USE_NONE),
0080         DECODE_END
0081     };
0082 
0083     return probes_decode_insn(insn, asi, table, false, false, stack_check_actions, NULL);
0084 }
0085 
0086 const struct decode_checker arm_stack_checker[NUM_PROBES_ARM_ACTIONS] = {
0087     [PROBES_LDRSTRD] = {.checker = arm_check_stack},
0088     [PROBES_STORE_EXTRA] = {.checker = arm_check_stack},
0089     [PROBES_STORE] = {.checker = arm_check_stack},
0090     [PROBES_LDMSTM] = {.checker = arm_check_stack},
0091 };
0092 
0093 static enum probes_insn __kprobes arm_check_regs_nouse(probes_opcode_t insn,
0094         struct arch_probes_insn *asi,
0095         const struct decode_header *h)
0096 {
0097     asi->register_usage_flags = 0;
0098     return INSN_GOOD;
0099 }
0100 
0101 static enum probes_insn arm_check_regs_normal(probes_opcode_t insn,
0102         struct arch_probes_insn *asi,
0103         const struct decode_header *h)
0104 {
0105     u32 regs = h->type_regs.bits >> DECODE_TYPE_BITS;
0106     int i;
0107 
0108     asi->register_usage_flags = 0;
0109     for (i = 0; i < 5; regs >>= 4, insn >>= 4, i++)
0110         if (regs & 0xf)
0111             asi->register_usage_flags |= 1 << (insn & 0xf);
0112 
0113     return INSN_GOOD;
0114 }
0115 
0116 
0117 static enum probes_insn arm_check_regs_ldmstm(probes_opcode_t insn,
0118         struct arch_probes_insn *asi,
0119         const struct decode_header *h)
0120 {
0121     unsigned int reglist = insn & 0xffff;
0122     unsigned int rn = (insn >> 16) & 0xf;
0123     asi->register_usage_flags = reglist | (1 << rn);
0124     return INSN_GOOD;
0125 }
0126 
0127 static enum probes_insn arm_check_regs_mov_ip_sp(probes_opcode_t insn,
0128         struct arch_probes_insn *asi,
0129         const struct decode_header *h)
0130 {
0131     /* Instruction is 'mov ip, sp' i.e. 'mov r12, r13' */
0132     asi->register_usage_flags = (1 << 12) | (1<< 13);
0133     return INSN_GOOD;
0134 }
0135 
0136 /*
0137  *                                    | Rn |Rt/d|         | Rm |
0138  * LDRD (register)      cccc 000x x0x0 xxxx xxxx xxxx 1101 xxxx
0139  * STRD (register)      cccc 000x x0x0 xxxx xxxx xxxx 1111 xxxx
0140  *                                    | Rn |Rt/d|         |imm4L|
0141  * LDRD (immediate)     cccc 000x x1x0 xxxx xxxx xxxx 1101 xxxx
0142  * STRD (immediate)     cccc 000x x1x0 xxxx xxxx xxxx 1111 xxxx
0143  *
0144  * Such instructions access Rt/d and its next register, so different
0145  * from others, a specific checker is required to handle this extra
0146  * implicit register usage.
0147  */
0148 static enum probes_insn arm_check_regs_ldrdstrd(probes_opcode_t insn,
0149         struct arch_probes_insn *asi,
0150         const struct decode_header *h)
0151 {
0152     int rdt = (insn >> 12) & 0xf;
0153     arm_check_regs_normal(insn, asi, h);
0154     asi->register_usage_flags |= 1 << (rdt + 1);
0155     return INSN_GOOD;
0156 }
0157 
0158 
0159 const struct decode_checker arm_regs_checker[NUM_PROBES_ARM_ACTIONS] = {
0160     [PROBES_MRS] = {.checker = arm_check_regs_normal},
0161     [PROBES_SATURATING_ARITHMETIC] = {.checker = arm_check_regs_normal},
0162     [PROBES_MUL1] = {.checker = arm_check_regs_normal},
0163     [PROBES_MUL2] = {.checker = arm_check_regs_normal},
0164     [PROBES_MUL_ADD_LONG] = {.checker = arm_check_regs_normal},
0165     [PROBES_MUL_ADD] = {.checker = arm_check_regs_normal},
0166     [PROBES_LOAD] = {.checker = arm_check_regs_normal},
0167     [PROBES_LOAD_EXTRA] = {.checker = arm_check_regs_normal},
0168     [PROBES_STORE] = {.checker = arm_check_regs_normal},
0169     [PROBES_STORE_EXTRA] = {.checker = arm_check_regs_normal},
0170     [PROBES_DATA_PROCESSING_REG] = {.checker = arm_check_regs_normal},
0171     [PROBES_DATA_PROCESSING_IMM] = {.checker = arm_check_regs_normal},
0172     [PROBES_SEV] = {.checker = arm_check_regs_nouse},
0173     [PROBES_WFE] = {.checker = arm_check_regs_nouse},
0174     [PROBES_SATURATE] = {.checker = arm_check_regs_normal},
0175     [PROBES_REV] = {.checker = arm_check_regs_normal},
0176     [PROBES_MMI] = {.checker = arm_check_regs_normal},
0177     [PROBES_PACK] = {.checker = arm_check_regs_normal},
0178     [PROBES_EXTEND] = {.checker = arm_check_regs_normal},
0179     [PROBES_EXTEND_ADD] = {.checker = arm_check_regs_normal},
0180     [PROBES_BITFIELD] = {.checker = arm_check_regs_normal},
0181     [PROBES_LDMSTM] = {.checker = arm_check_regs_ldmstm},
0182     [PROBES_MOV_IP_SP] = {.checker = arm_check_regs_mov_ip_sp},
0183     [PROBES_LDRSTRD] = {.checker = arm_check_regs_ldrdstrd},
0184 };