xref: /linux/arch/arm64/kernel/probes/decode-insn.c (revision acc450aa07099d071b18174c22a1119c57da8227)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22dd0e8d2SSandeepa Prabhu /*
32dd0e8d2SSandeepa Prabhu  * arch/arm64/kernel/probes/decode-insn.c
42dd0e8d2SSandeepa Prabhu  *
52dd0e8d2SSandeepa Prabhu  * Copyright (C) 2013 Linaro Limited.
62dd0e8d2SSandeepa Prabhu  */
72dd0e8d2SSandeepa Prabhu 
82dd0e8d2SSandeepa Prabhu #include <linux/kernel.h>
92dd0e8d2SSandeepa Prabhu #include <linux/kprobes.h>
102dd0e8d2SSandeepa Prabhu #include <linux/module.h>
113e593f66SDavid A. Long #include <linux/kallsyms.h>
122dd0e8d2SSandeepa Prabhu #include <asm/insn.h>
132dd0e8d2SSandeepa Prabhu #include <asm/sections.h>
142dd0e8d2SSandeepa Prabhu 
152dd0e8d2SSandeepa Prabhu #include "decode-insn.h"
1639a67d49SSandeepa Prabhu #include "simulate-insn.h"
172dd0e8d2SSandeepa Prabhu 
182dd0e8d2SSandeepa Prabhu static bool __kprobes aarch64_insn_is_steppable(u32 insn)
192dd0e8d2SSandeepa Prabhu {
202dd0e8d2SSandeepa Prabhu 	/*
212dd0e8d2SSandeepa Prabhu 	 * Branch instructions will write a new value into the PC which is
222dd0e8d2SSandeepa Prabhu 	 * likely to be relative to the XOL address and therefore invalid.
232dd0e8d2SSandeepa Prabhu 	 * Deliberate generation of an exception during stepping is also not
242dd0e8d2SSandeepa Prabhu 	 * currently safe. Lastly, MSR instructions can do any number of nasty
252dd0e8d2SSandeepa Prabhu 	 * things we can't handle during single-stepping.
262dd0e8d2SSandeepa Prabhu 	 */
274488f90cSMark Rutland 	if (aarch64_insn_is_class_branch_sys(insn)) {
282dd0e8d2SSandeepa Prabhu 		if (aarch64_insn_is_branch(insn) ||
292dd0e8d2SSandeepa Prabhu 		    aarch64_insn_is_msr_imm(insn) ||
302dd0e8d2SSandeepa Prabhu 		    aarch64_insn_is_msr_reg(insn) ||
312dd0e8d2SSandeepa Prabhu 		    aarch64_insn_is_exception(insn) ||
3293396936SAmit Daniel Kachhap 		    aarch64_insn_is_eret(insn) ||
3393396936SAmit Daniel Kachhap 		    aarch64_insn_is_eret_auth(insn))
342dd0e8d2SSandeepa Prabhu 			return false;
352dd0e8d2SSandeepa Prabhu 
362dd0e8d2SSandeepa Prabhu 		/*
372dd0e8d2SSandeepa Prabhu 		 * The MRS instruction may not return a correct value when
382dd0e8d2SSandeepa Prabhu 		 * executing in the single-stepping environment. We do make one
392dd0e8d2SSandeepa Prabhu 		 * exception, for reading the DAIF bits.
402dd0e8d2SSandeepa Prabhu 		 */
412dd0e8d2SSandeepa Prabhu 		if (aarch64_insn_is_mrs(insn))
422dd0e8d2SSandeepa Prabhu 			return aarch64_insn_extract_system_reg(insn)
432dd0e8d2SSandeepa Prabhu 			     != AARCH64_INSN_SPCLREG_DAIF;
442dd0e8d2SSandeepa Prabhu 
452dd0e8d2SSandeepa Prabhu 		/*
4603c9c8faSAmit Daniel Kachhap 		 * The HINT instruction is steppable only if it is in whitelist
4703c9c8faSAmit Daniel Kachhap 		 * and the rest of other such instructions are blocked for
4803c9c8faSAmit Daniel Kachhap 		 * single stepping as they may cause exception or other
4903c9c8faSAmit Daniel Kachhap 		 * unintended behaviour.
502dd0e8d2SSandeepa Prabhu 		 */
512dd0e8d2SSandeepa Prabhu 		if (aarch64_insn_is_hint(insn))
5207dcd967SMark Brown 			return aarch64_insn_is_steppable_hint(insn);
532dd0e8d2SSandeepa Prabhu 
542dd0e8d2SSandeepa Prabhu 		return true;
552dd0e8d2SSandeepa Prabhu 	}
562dd0e8d2SSandeepa Prabhu 
572dd0e8d2SSandeepa Prabhu 	/*
582dd0e8d2SSandeepa Prabhu 	 * Instructions which load PC relative literals are not going to work
592dd0e8d2SSandeepa Prabhu 	 * when executed from an XOL slot. Instructions doing an exclusive
602dd0e8d2SSandeepa Prabhu 	 * load/store are not going to complete successfully when single-step
612dd0e8d2SSandeepa Prabhu 	 * exception handling happens in the middle of the sequence.
622dd0e8d2SSandeepa Prabhu 	 */
632dd0e8d2SSandeepa Prabhu 	if (aarch64_insn_uses_literal(insn) ||
642dd0e8d2SSandeepa Prabhu 	    aarch64_insn_is_exclusive(insn))
652dd0e8d2SSandeepa Prabhu 		return false;
662dd0e8d2SSandeepa Prabhu 
672dd0e8d2SSandeepa Prabhu 	return true;
682dd0e8d2SSandeepa Prabhu }
692dd0e8d2SSandeepa Prabhu 
702dd0e8d2SSandeepa Prabhu /* Return:
712dd0e8d2SSandeepa Prabhu  *   INSN_REJECTED     If instruction is one not allowed to kprobe,
722dd0e8d2SSandeepa Prabhu  *   INSN_GOOD         If instruction is supported and uses instruction slot,
7339a67d49SSandeepa Prabhu  *   INSN_GOOD_NO_SLOT If instruction is supported but doesn't use its slot.
742dd0e8d2SSandeepa Prabhu  */
75c2249707SPratyush Anand enum probe_insn __kprobes
76c2249707SPratyush Anand arm_probe_decode_insn(probe_opcode_t insn, struct arch_probe_insn *api)
772dd0e8d2SSandeepa Prabhu {
782dd0e8d2SSandeepa Prabhu 	/*
792dd0e8d2SSandeepa Prabhu 	 * Instructions reading or modifying the PC won't work from the XOL
802dd0e8d2SSandeepa Prabhu 	 * slot.
812dd0e8d2SSandeepa Prabhu 	 */
822dd0e8d2SSandeepa Prabhu 	if (aarch64_insn_is_steppable(insn))
832dd0e8d2SSandeepa Prabhu 		return INSN_GOOD;
8439a67d49SSandeepa Prabhu 
8539a67d49SSandeepa Prabhu 	if (aarch64_insn_is_bcond(insn)) {
86c2249707SPratyush Anand 		api->handler = simulate_b_cond;
8739a67d49SSandeepa Prabhu 	} else if (aarch64_insn_is_cbz(insn) ||
8839a67d49SSandeepa Prabhu 	    aarch64_insn_is_cbnz(insn)) {
89c2249707SPratyush Anand 		api->handler = simulate_cbz_cbnz;
9039a67d49SSandeepa Prabhu 	} else if (aarch64_insn_is_tbz(insn) ||
9139a67d49SSandeepa Prabhu 	    aarch64_insn_is_tbnz(insn)) {
92c2249707SPratyush Anand 		api->handler = simulate_tbz_tbnz;
9339a67d49SSandeepa Prabhu 	} else if (aarch64_insn_is_adr_adrp(insn)) {
94c2249707SPratyush Anand 		api->handler = simulate_adr_adrp;
9539a67d49SSandeepa Prabhu 	} else if (aarch64_insn_is_b(insn) ||
9639a67d49SSandeepa Prabhu 	    aarch64_insn_is_bl(insn)) {
97c2249707SPratyush Anand 		api->handler = simulate_b_bl;
9839a67d49SSandeepa Prabhu 	} else if (aarch64_insn_is_br(insn) ||
9939a67d49SSandeepa Prabhu 	    aarch64_insn_is_blr(insn) ||
10039a67d49SSandeepa Prabhu 	    aarch64_insn_is_ret(insn)) {
101c2249707SPratyush Anand 		api->handler = simulate_br_blr_ret;
10239a67d49SSandeepa Prabhu 	} else {
10339a67d49SSandeepa Prabhu 		/*
10439a67d49SSandeepa Prabhu 		 * Instruction cannot be stepped out-of-line and we don't
10539a67d49SSandeepa Prabhu 		 * (yet) simulate it.
10639a67d49SSandeepa Prabhu 		 */
1072dd0e8d2SSandeepa Prabhu 		return INSN_REJECTED;
1082dd0e8d2SSandeepa Prabhu 	}
1092dd0e8d2SSandeepa Prabhu 
11039a67d49SSandeepa Prabhu 	return INSN_GOOD_NO_SLOT;
11139a67d49SSandeepa Prabhu }
11239a67d49SSandeepa Prabhu 
113c2249707SPratyush Anand #ifdef CONFIG_KPROBES
1142dd0e8d2SSandeepa Prabhu static bool __kprobes
1152dd0e8d2SSandeepa Prabhu is_probed_address_atomic(kprobe_opcode_t *scan_start, kprobe_opcode_t *scan_end)
1162dd0e8d2SSandeepa Prabhu {
1173e593f66SDavid A. Long 	while (scan_start >= scan_end) {
1182dd0e8d2SSandeepa Prabhu 		/*
1192dd0e8d2SSandeepa Prabhu 		 * atomic region starts from exclusive load and ends with
1202dd0e8d2SSandeepa Prabhu 		 * exclusive store.
1212dd0e8d2SSandeepa Prabhu 		 */
1222dd0e8d2SSandeepa Prabhu 		if (aarch64_insn_is_store_ex(le32_to_cpu(*scan_start)))
1232dd0e8d2SSandeepa Prabhu 			return false;
1242dd0e8d2SSandeepa Prabhu 		else if (aarch64_insn_is_load_ex(le32_to_cpu(*scan_start)))
1252dd0e8d2SSandeepa Prabhu 			return true;
1262dd0e8d2SSandeepa Prabhu 		scan_start--;
1272dd0e8d2SSandeepa Prabhu 	}
1282dd0e8d2SSandeepa Prabhu 
1292dd0e8d2SSandeepa Prabhu 	return false;
1302dd0e8d2SSandeepa Prabhu }
1312dd0e8d2SSandeepa Prabhu 
132c2249707SPratyush Anand enum probe_insn __kprobes
1332dd0e8d2SSandeepa Prabhu arm_kprobe_decode_insn(kprobe_opcode_t *addr, struct arch_specific_insn *asi)
1342dd0e8d2SSandeepa Prabhu {
135c2249707SPratyush Anand 	enum probe_insn decoded;
136c2249707SPratyush Anand 	probe_opcode_t insn = le32_to_cpu(*addr);
137c2249707SPratyush Anand 	probe_opcode_t *scan_end = NULL;
1383e593f66SDavid A. Long 	unsigned long size = 0, offset = 0;
139*acc450aaSMark Rutland 	struct arch_probe_insn *api = &asi->api;
140*acc450aaSMark Rutland 
141*acc450aaSMark Rutland 	if (aarch64_insn_is_ldr_lit(insn)) {
142*acc450aaSMark Rutland 		api->handler = simulate_ldr_literal;
143*acc450aaSMark Rutland 		decoded = INSN_GOOD_NO_SLOT;
144*acc450aaSMark Rutland 	} else if (aarch64_insn_is_ldrsw_lit(insn)) {
145*acc450aaSMark Rutland 		api->handler = simulate_ldrsw_literal;
146*acc450aaSMark Rutland 		decoded = INSN_GOOD_NO_SLOT;
147*acc450aaSMark Rutland 	} else {
148*acc450aaSMark Rutland 		decoded = arm_probe_decode_insn(insn, &asi->api);
149*acc450aaSMark Rutland 	}
1502dd0e8d2SSandeepa Prabhu 
1513e593f66SDavid A. Long 	/*
1523e593f66SDavid A. Long 	 * If there's a symbol defined in front of and near enough to
1533e593f66SDavid A. Long 	 * the probe address assume it is the entry point to this
1543e593f66SDavid A. Long 	 * code and use it to further limit how far back we search
1553e593f66SDavid A. Long 	 * when determining if we're in an atomic sequence. If we could
1563e593f66SDavid A. Long 	 * not find any symbol skip the atomic test altogether as we
1573e593f66SDavid A. Long 	 * could otherwise end up searching irrelevant text/literals.
1583e593f66SDavid A. Long 	 * KPROBES depends on KALLSYMS so this last case should never
1593e593f66SDavid A. Long 	 * happen.
1603e593f66SDavid A. Long 	 */
1613e593f66SDavid A. Long 	if (kallsyms_lookup_size_offset((unsigned long) addr, &size, &offset)) {
1623e593f66SDavid A. Long 		if (offset < (MAX_ATOMIC_CONTEXT_SIZE*sizeof(kprobe_opcode_t)))
1633e593f66SDavid A. Long 			scan_end = addr - (offset / sizeof(kprobe_opcode_t));
1643e593f66SDavid A. Long 		else
1653e593f66SDavid A. Long 			scan_end = addr - MAX_ATOMIC_CONTEXT_SIZE;
1662dd0e8d2SSandeepa Prabhu 	}
1672dd0e8d2SSandeepa Prabhu 
1683e593f66SDavid A. Long 	if (decoded != INSN_REJECTED && scan_end)
1693e593f66SDavid A. Long 		if (is_probed_address_atomic(addr - 1, scan_end))
1702dd0e8d2SSandeepa Prabhu 			return INSN_REJECTED;
1712dd0e8d2SSandeepa Prabhu 
1722dd0e8d2SSandeepa Prabhu 	return decoded;
1732dd0e8d2SSandeepa Prabhu }
174c2249707SPratyush Anand #endif
175