18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * arch/arm/probes/kprobes/checkers-thumb.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2014 Huawei Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include "../decode.h"
108c2ecf20Sopenharmony_ci#include "../decode-thumb.h"
118c2ecf20Sopenharmony_ci#include "checkers.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic enum probes_insn __kprobes t32_check_stack(probes_opcode_t insn,
148c2ecf20Sopenharmony_ci		struct arch_probes_insn *asi,
158c2ecf20Sopenharmony_ci		const struct decode_header *h)
168c2ecf20Sopenharmony_ci{
178c2ecf20Sopenharmony_ci	/*
188c2ecf20Sopenharmony_ci	 * PROBES_T32_LDMSTM, PROBES_T32_LDRDSTRD and PROBES_T32_LDRSTR
198c2ecf20Sopenharmony_ci	 * may get here. Simply mark all normal insns as STACK_USE_NONE.
208c2ecf20Sopenharmony_ci	 */
218c2ecf20Sopenharmony_ci	static const union decode_item table[] = {
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci		/*
248c2ecf20Sopenharmony_ci		 * First, filter out all ldr insns to make our life easier.
258c2ecf20Sopenharmony_ci		 * Following load insns may come here:
268c2ecf20Sopenharmony_ci		 * LDM, LDRD, LDR.
278c2ecf20Sopenharmony_ci		 * In T32 encoding, bit 20 is enough for distinguishing
288c2ecf20Sopenharmony_ci		 * load and store. All load insns have this bit set, when
298c2ecf20Sopenharmony_ci		 * all store insns have this bit clear.
308c2ecf20Sopenharmony_ci		 */
318c2ecf20Sopenharmony_ci		DECODE_CUSTOM	(0x00100000, 0x00100000, STACK_USE_NONE),
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci		/*
348c2ecf20Sopenharmony_ci		 * Mark all 'STR{,B,H}, Rt, [Rn, Rm]' as STACK_USE_UNKNOWN
358c2ecf20Sopenharmony_ci		 * if Rn or Rm is SP. T32 doesn't encode STRD.
368c2ecf20Sopenharmony_ci		 */
378c2ecf20Sopenharmony_ci		/*                                 xx | Rn | Rt |         | Rm |*/
388c2ecf20Sopenharmony_ci		/* STR (register)	1111 1000 0100 xxxx xxxx 0000 00xx xxxx */
398c2ecf20Sopenharmony_ci		/* STRB (register)	1111 1000 0000 xxxx xxxx 0000 00xx xxxx */
408c2ecf20Sopenharmony_ci		/* STRH (register)	1111 1000 0010 xxxx xxxx 0000 00xx xxxx */
418c2ecf20Sopenharmony_ci		/* INVALID INSN		1111 1000 0110 xxxx xxxx 0000 00xx xxxx */
428c2ecf20Sopenharmony_ci		/* By Introducing INVALID INSN, bit 21 and 22 can be ignored. */
438c2ecf20Sopenharmony_ci		DECODE_OR	(0xff9f0fc0, 0xf80d0000),
448c2ecf20Sopenharmony_ci		DECODE_CUSTOM	(0xff900fcf, 0xf800000d, STACK_USE_UNKNOWN),
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci		/*                                 xx | Rn | Rt | PUW|   imm8  |*/
488c2ecf20Sopenharmony_ci		/* STR (imm 8)		1111 1000 0100 1101 xxxx 110x xxxx xxxx */
498c2ecf20Sopenharmony_ci		/* STRB (imm 8)		1111 1000 0000 1101 xxxx 110x xxxx xxxx */
508c2ecf20Sopenharmony_ci		/* STRH (imm 8)		1111 1000 0010 1101 xxxx 110x xxxx xxxx */
518c2ecf20Sopenharmony_ci		/* INVALID INSN		1111 1000 0110 1101 xxxx 110x xxxx xxxx */
528c2ecf20Sopenharmony_ci		/* Only consider U == 0 and P == 1: strx rx, [sp, #-<imm>] */
538c2ecf20Sopenharmony_ci		DECODE_CUSTOM	(0xff9f0e00, 0xf80d0c00, STACK_USE_FIXED_0XX),
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci		/* For STR{,B,H} (imm 12), offset is always positive, so ignore them. */
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci		/*                              P U W | Rn | Rt | Rt2|   imm8  |*/
588c2ecf20Sopenharmony_ci		/* STRD (immediate)	1110 1001 01x0 1101 xxxx xxxx xxxx xxxx */
598c2ecf20Sopenharmony_ci		/*
608c2ecf20Sopenharmony_ci		 * Only consider U == 0 and P == 1.
618c2ecf20Sopenharmony_ci		 * Also note that STRD in T32 encoding is special:
628c2ecf20Sopenharmony_ci		 * imm = ZeroExtend(imm8:'00', 32)
638c2ecf20Sopenharmony_ci		 */
648c2ecf20Sopenharmony_ci		DECODE_CUSTOM	(0xffdf0000, 0xe94d0000, STACK_USE_T32STRD),
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci		/*                                    | Rn | */
678c2ecf20Sopenharmony_ci		/* STMDB		1110 1001 00x0 1101 xxxx xxxx xxxx xxxx */
688c2ecf20Sopenharmony_ci		DECODE_CUSTOM	(0xffdf0000, 0xe90d0000, STACK_USE_STMDX),
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci		/* fall through */
718c2ecf20Sopenharmony_ci		DECODE_CUSTOM	(0, 0, STACK_USE_NONE),
728c2ecf20Sopenharmony_ci		DECODE_END
738c2ecf20Sopenharmony_ci	};
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	return probes_decode_insn(insn, asi, table, false, false, stack_check_actions, NULL);
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ciconst struct decode_checker t32_stack_checker[NUM_PROBES_T32_ACTIONS] = {
798c2ecf20Sopenharmony_ci	[PROBES_T32_LDMSTM] = {.checker = t32_check_stack},
808c2ecf20Sopenharmony_ci	[PROBES_T32_LDRDSTRD] = {.checker = t32_check_stack},
818c2ecf20Sopenharmony_ci	[PROBES_T32_LDRSTR] = {.checker = t32_check_stack},
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/*
858c2ecf20Sopenharmony_ci * See following comments. This insn must be 'push'.
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_cistatic enum probes_insn __kprobes t16_check_stack(probes_opcode_t insn,
888c2ecf20Sopenharmony_ci		struct arch_probes_insn *asi,
898c2ecf20Sopenharmony_ci		const struct decode_header *h)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	unsigned int reglist = insn & 0x1ff;
928c2ecf20Sopenharmony_ci	asi->stack_space = hweight32(reglist) * 4;
938c2ecf20Sopenharmony_ci	return INSN_GOOD;
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/*
978c2ecf20Sopenharmony_ci * T16 encoding is simple: only the 'push' insn can need extra stack space.
988c2ecf20Sopenharmony_ci * Other insns, like str, can only use r0-r7 as Rn.
998c2ecf20Sopenharmony_ci */
1008c2ecf20Sopenharmony_ciconst struct decode_checker t16_stack_checker[NUM_PROBES_T16_ACTIONS] = {
1018c2ecf20Sopenharmony_ci	[PROBES_T16_PUSH] = {.checker = t16_check_stack},
1028c2ecf20Sopenharmony_ci};
103