162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * arch/arm/probes/kprobes/checkers-thumb.c
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2014 Huawei Inc.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/kernel.h>
962306a36Sopenharmony_ci#include "../decode.h"
1062306a36Sopenharmony_ci#include "../decode-thumb.h"
1162306a36Sopenharmony_ci#include "checkers.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_cistatic enum probes_insn __kprobes t32_check_stack(probes_opcode_t insn,
1462306a36Sopenharmony_ci		struct arch_probes_insn *asi,
1562306a36Sopenharmony_ci		const struct decode_header *h)
1662306a36Sopenharmony_ci{
1762306a36Sopenharmony_ci	/*
1862306a36Sopenharmony_ci	 * PROBES_T32_LDMSTM, PROBES_T32_LDRDSTRD and PROBES_T32_LDRSTR
1962306a36Sopenharmony_ci	 * may get here. Simply mark all normal insns as STACK_USE_NONE.
2062306a36Sopenharmony_ci	 */
2162306a36Sopenharmony_ci	static const union decode_item table[] = {
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci		/*
2462306a36Sopenharmony_ci		 * First, filter out all ldr insns to make our life easier.
2562306a36Sopenharmony_ci		 * Following load insns may come here:
2662306a36Sopenharmony_ci		 * LDM, LDRD, LDR.
2762306a36Sopenharmony_ci		 * In T32 encoding, bit 20 is enough for distinguishing
2862306a36Sopenharmony_ci		 * load and store. All load insns have this bit set, when
2962306a36Sopenharmony_ci		 * all store insns have this bit clear.
3062306a36Sopenharmony_ci		 */
3162306a36Sopenharmony_ci		DECODE_CUSTOM	(0x00100000, 0x00100000, STACK_USE_NONE),
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci		/*
3462306a36Sopenharmony_ci		 * Mark all 'STR{,B,H}, Rt, [Rn, Rm]' as STACK_USE_UNKNOWN
3562306a36Sopenharmony_ci		 * if Rn or Rm is SP. T32 doesn't encode STRD.
3662306a36Sopenharmony_ci		 */
3762306a36Sopenharmony_ci		/*                                 xx | Rn | Rt |         | Rm |*/
3862306a36Sopenharmony_ci		/* STR (register)	1111 1000 0100 xxxx xxxx 0000 00xx xxxx */
3962306a36Sopenharmony_ci		/* STRB (register)	1111 1000 0000 xxxx xxxx 0000 00xx xxxx */
4062306a36Sopenharmony_ci		/* STRH (register)	1111 1000 0010 xxxx xxxx 0000 00xx xxxx */
4162306a36Sopenharmony_ci		/* INVALID INSN		1111 1000 0110 xxxx xxxx 0000 00xx xxxx */
4262306a36Sopenharmony_ci		/* By Introducing INVALID INSN, bit 21 and 22 can be ignored. */
4362306a36Sopenharmony_ci		DECODE_OR	(0xff9f0fc0, 0xf80d0000),
4462306a36Sopenharmony_ci		DECODE_CUSTOM	(0xff900fcf, 0xf800000d, STACK_USE_UNKNOWN),
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci		/*                                 xx | Rn | Rt | PUW|   imm8  |*/
4862306a36Sopenharmony_ci		/* STR (imm 8)		1111 1000 0100 1101 xxxx 110x xxxx xxxx */
4962306a36Sopenharmony_ci		/* STRB (imm 8)		1111 1000 0000 1101 xxxx 110x xxxx xxxx */
5062306a36Sopenharmony_ci		/* STRH (imm 8)		1111 1000 0010 1101 xxxx 110x xxxx xxxx */
5162306a36Sopenharmony_ci		/* INVALID INSN		1111 1000 0110 1101 xxxx 110x xxxx xxxx */
5262306a36Sopenharmony_ci		/* Only consider U == 0 and P == 1: strx rx, [sp, #-<imm>] */
5362306a36Sopenharmony_ci		DECODE_CUSTOM	(0xff9f0e00, 0xf80d0c00, STACK_USE_FIXED_0XX),
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci		/* For STR{,B,H} (imm 12), offset is always positive, so ignore them. */
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci		/*                              P U W | Rn | Rt | Rt2|   imm8  |*/
5862306a36Sopenharmony_ci		/* STRD (immediate)	1110 1001 01x0 1101 xxxx xxxx xxxx xxxx */
5962306a36Sopenharmony_ci		/*
6062306a36Sopenharmony_ci		 * Only consider U == 0 and P == 1.
6162306a36Sopenharmony_ci		 * Also note that STRD in T32 encoding is special:
6262306a36Sopenharmony_ci		 * imm = ZeroExtend(imm8:'00', 32)
6362306a36Sopenharmony_ci		 */
6462306a36Sopenharmony_ci		DECODE_CUSTOM	(0xffdf0000, 0xe94d0000, STACK_USE_T32STRD),
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci		/*                                    | Rn | */
6762306a36Sopenharmony_ci		/* STMDB		1110 1001 00x0 1101 xxxx xxxx xxxx xxxx */
6862306a36Sopenharmony_ci		DECODE_CUSTOM	(0xffdf0000, 0xe90d0000, STACK_USE_STMDX),
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci		/* fall through */
7162306a36Sopenharmony_ci		DECODE_CUSTOM	(0, 0, STACK_USE_NONE),
7262306a36Sopenharmony_ci		DECODE_END
7362306a36Sopenharmony_ci	};
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	return probes_decode_insn(insn, asi, table, false, false, stack_check_actions, NULL);
7662306a36Sopenharmony_ci}
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ciconst struct decode_checker t32_stack_checker[NUM_PROBES_T32_ACTIONS] = {
7962306a36Sopenharmony_ci	[PROBES_T32_LDMSTM] = {.checker = t32_check_stack},
8062306a36Sopenharmony_ci	[PROBES_T32_LDRDSTRD] = {.checker = t32_check_stack},
8162306a36Sopenharmony_ci	[PROBES_T32_LDRSTR] = {.checker = t32_check_stack},
8262306a36Sopenharmony_ci};
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci/*
8562306a36Sopenharmony_ci * See following comments. This insn must be 'push'.
8662306a36Sopenharmony_ci */
8762306a36Sopenharmony_cistatic enum probes_insn __kprobes t16_check_stack(probes_opcode_t insn,
8862306a36Sopenharmony_ci		struct arch_probes_insn *asi,
8962306a36Sopenharmony_ci		const struct decode_header *h)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	unsigned int reglist = insn & 0x1ff;
9262306a36Sopenharmony_ci	asi->stack_space = hweight32(reglist) * 4;
9362306a36Sopenharmony_ci	return INSN_GOOD;
9462306a36Sopenharmony_ci}
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci/*
9762306a36Sopenharmony_ci * T16 encoding is simple: only the 'push' insn can need extra stack space.
9862306a36Sopenharmony_ci * Other insns, like str, can only use r0-r7 as Rn.
9962306a36Sopenharmony_ci */
10062306a36Sopenharmony_ciconst struct decode_checker t16_stack_checker[NUM_PROBES_T16_ACTIONS] = {
10162306a36Sopenharmony_ci	[PROBES_T16_PUSH] = {.checker = t16_check_stack},
10262306a36Sopenharmony_ci};
103