xref: /linux/arch/arm64/kernel/probes/decode-insn.c (revision 3e593f66754def77fa3433c595f941f1defe4af1)
12dd0e8d2SSandeepa Prabhu /*
22dd0e8d2SSandeepa Prabhu  * arch/arm64/kernel/probes/decode-insn.c
32dd0e8d2SSandeepa Prabhu  *
42dd0e8d2SSandeepa Prabhu  * Copyright (C) 2013 Linaro Limited.
52dd0e8d2SSandeepa Prabhu  *
62dd0e8d2SSandeepa Prabhu  * This program is free software; you can redistribute it and/or modify
72dd0e8d2SSandeepa Prabhu  * it under the terms of the GNU General Public License version 2 as
82dd0e8d2SSandeepa Prabhu  * published by the Free Software Foundation.
92dd0e8d2SSandeepa Prabhu  *
102dd0e8d2SSandeepa Prabhu  * This program is distributed in the hope that it will be useful,
112dd0e8d2SSandeepa Prabhu  * but WITHOUT ANY WARRANTY; without even the implied warranty of
122dd0e8d2SSandeepa Prabhu  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
132dd0e8d2SSandeepa Prabhu  * General Public License for more details.
142dd0e8d2SSandeepa Prabhu  */
152dd0e8d2SSandeepa Prabhu 
162dd0e8d2SSandeepa Prabhu #include <linux/kernel.h>
172dd0e8d2SSandeepa Prabhu #include <linux/kprobes.h>
182dd0e8d2SSandeepa Prabhu #include <linux/module.h>
19*3e593f66SDavid A. Long #include <linux/kallsyms.h>
202dd0e8d2SSandeepa Prabhu #include <asm/kprobes.h>
212dd0e8d2SSandeepa Prabhu #include <asm/insn.h>
222dd0e8d2SSandeepa Prabhu #include <asm/sections.h>
232dd0e8d2SSandeepa Prabhu 
242dd0e8d2SSandeepa Prabhu #include "decode-insn.h"
2539a67d49SSandeepa Prabhu #include "simulate-insn.h"
262dd0e8d2SSandeepa Prabhu 
272dd0e8d2SSandeepa Prabhu static bool __kprobes aarch64_insn_is_steppable(u32 insn)
282dd0e8d2SSandeepa Prabhu {
292dd0e8d2SSandeepa Prabhu 	/*
302dd0e8d2SSandeepa Prabhu 	 * Branch instructions will write a new value into the PC which is
312dd0e8d2SSandeepa Prabhu 	 * likely to be relative to the XOL address and therefore invalid.
322dd0e8d2SSandeepa Prabhu 	 * Deliberate generation of an exception during stepping is also not
332dd0e8d2SSandeepa Prabhu 	 * currently safe. Lastly, MSR instructions can do any number of nasty
342dd0e8d2SSandeepa Prabhu 	 * things we can't handle during single-stepping.
352dd0e8d2SSandeepa Prabhu 	 */
362dd0e8d2SSandeepa Prabhu 	if (aarch64_get_insn_class(insn) == AARCH64_INSN_CLS_BR_SYS) {
372dd0e8d2SSandeepa Prabhu 		if (aarch64_insn_is_branch(insn) ||
382dd0e8d2SSandeepa Prabhu 		    aarch64_insn_is_msr_imm(insn) ||
392dd0e8d2SSandeepa Prabhu 		    aarch64_insn_is_msr_reg(insn) ||
402dd0e8d2SSandeepa Prabhu 		    aarch64_insn_is_exception(insn) ||
412dd0e8d2SSandeepa Prabhu 		    aarch64_insn_is_eret(insn))
422dd0e8d2SSandeepa Prabhu 			return false;
432dd0e8d2SSandeepa Prabhu 
442dd0e8d2SSandeepa Prabhu 		/*
452dd0e8d2SSandeepa Prabhu 		 * The MRS instruction may not return a correct value when
462dd0e8d2SSandeepa Prabhu 		 * executing in the single-stepping environment. We do make one
472dd0e8d2SSandeepa Prabhu 		 * exception, for reading the DAIF bits.
482dd0e8d2SSandeepa Prabhu 		 */
492dd0e8d2SSandeepa Prabhu 		if (aarch64_insn_is_mrs(insn))
502dd0e8d2SSandeepa Prabhu 			return aarch64_insn_extract_system_reg(insn)
512dd0e8d2SSandeepa Prabhu 			     != AARCH64_INSN_SPCLREG_DAIF;
522dd0e8d2SSandeepa Prabhu 
532dd0e8d2SSandeepa Prabhu 		/*
542dd0e8d2SSandeepa Prabhu 		 * The HINT instruction is is problematic when single-stepping,
552dd0e8d2SSandeepa Prabhu 		 * except for the NOP case.
562dd0e8d2SSandeepa Prabhu 		 */
572dd0e8d2SSandeepa Prabhu 		if (aarch64_insn_is_hint(insn))
582dd0e8d2SSandeepa Prabhu 			return aarch64_insn_is_nop(insn);
592dd0e8d2SSandeepa Prabhu 
602dd0e8d2SSandeepa Prabhu 		return true;
612dd0e8d2SSandeepa Prabhu 	}
622dd0e8d2SSandeepa Prabhu 
632dd0e8d2SSandeepa Prabhu 	/*
642dd0e8d2SSandeepa Prabhu 	 * Instructions which load PC relative literals are not going to work
652dd0e8d2SSandeepa Prabhu 	 * when executed from an XOL slot. Instructions doing an exclusive
662dd0e8d2SSandeepa Prabhu 	 * load/store are not going to complete successfully when single-step
672dd0e8d2SSandeepa Prabhu 	 * exception handling happens in the middle of the sequence.
682dd0e8d2SSandeepa Prabhu 	 */
692dd0e8d2SSandeepa Prabhu 	if (aarch64_insn_uses_literal(insn) ||
702dd0e8d2SSandeepa Prabhu 	    aarch64_insn_is_exclusive(insn))
712dd0e8d2SSandeepa Prabhu 		return false;
722dd0e8d2SSandeepa Prabhu 
732dd0e8d2SSandeepa Prabhu 	return true;
742dd0e8d2SSandeepa Prabhu }
752dd0e8d2SSandeepa Prabhu 
762dd0e8d2SSandeepa Prabhu /* Return:
772dd0e8d2SSandeepa Prabhu  *   INSN_REJECTED     If instruction is one not allowed to kprobe,
782dd0e8d2SSandeepa Prabhu  *   INSN_GOOD         If instruction is supported and uses instruction slot,
7939a67d49SSandeepa Prabhu  *   INSN_GOOD_NO_SLOT If instruction is supported but doesn't use its slot.
802dd0e8d2SSandeepa Prabhu  */
812dd0e8d2SSandeepa Prabhu static enum kprobe_insn __kprobes
822dd0e8d2SSandeepa Prabhu arm_probe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
832dd0e8d2SSandeepa Prabhu {
842dd0e8d2SSandeepa Prabhu 	/*
852dd0e8d2SSandeepa Prabhu 	 * Instructions reading or modifying the PC won't work from the XOL
862dd0e8d2SSandeepa Prabhu 	 * slot.
872dd0e8d2SSandeepa Prabhu 	 */
882dd0e8d2SSandeepa Prabhu 	if (aarch64_insn_is_steppable(insn))
892dd0e8d2SSandeepa Prabhu 		return INSN_GOOD;
9039a67d49SSandeepa Prabhu 
9139a67d49SSandeepa Prabhu 	if (aarch64_insn_is_bcond(insn)) {
9239a67d49SSandeepa Prabhu 		asi->handler = simulate_b_cond;
9339a67d49SSandeepa Prabhu 	} else if (aarch64_insn_is_cbz(insn) ||
9439a67d49SSandeepa Prabhu 	    aarch64_insn_is_cbnz(insn)) {
9539a67d49SSandeepa Prabhu 		asi->handler = simulate_cbz_cbnz;
9639a67d49SSandeepa Prabhu 	} else if (aarch64_insn_is_tbz(insn) ||
9739a67d49SSandeepa Prabhu 	    aarch64_insn_is_tbnz(insn)) {
9839a67d49SSandeepa Prabhu 		asi->handler = simulate_tbz_tbnz;
9939a67d49SSandeepa Prabhu 	} else if (aarch64_insn_is_adr_adrp(insn)) {
10039a67d49SSandeepa Prabhu 		asi->handler = simulate_adr_adrp;
10139a67d49SSandeepa Prabhu 	} else if (aarch64_insn_is_b(insn) ||
10239a67d49SSandeepa Prabhu 	    aarch64_insn_is_bl(insn)) {
10339a67d49SSandeepa Prabhu 		asi->handler = simulate_b_bl;
10439a67d49SSandeepa Prabhu 	} else if (aarch64_insn_is_br(insn) ||
10539a67d49SSandeepa Prabhu 	    aarch64_insn_is_blr(insn) ||
10639a67d49SSandeepa Prabhu 	    aarch64_insn_is_ret(insn)) {
10739a67d49SSandeepa Prabhu 		asi->handler = simulate_br_blr_ret;
10839a67d49SSandeepa Prabhu 	} else if (aarch64_insn_is_ldr_lit(insn)) {
10939a67d49SSandeepa Prabhu 		asi->handler = simulate_ldr_literal;
11039a67d49SSandeepa Prabhu 	} else if (aarch64_insn_is_ldrsw_lit(insn)) {
11139a67d49SSandeepa Prabhu 		asi->handler = simulate_ldrsw_literal;
11239a67d49SSandeepa Prabhu 	} else {
11339a67d49SSandeepa Prabhu 		/*
11439a67d49SSandeepa Prabhu 		 * Instruction cannot be stepped out-of-line and we don't
11539a67d49SSandeepa Prabhu 		 * (yet) simulate it.
11639a67d49SSandeepa Prabhu 		 */
1172dd0e8d2SSandeepa Prabhu 		return INSN_REJECTED;
1182dd0e8d2SSandeepa Prabhu 	}
1192dd0e8d2SSandeepa Prabhu 
12039a67d49SSandeepa Prabhu 	return INSN_GOOD_NO_SLOT;
12139a67d49SSandeepa Prabhu }
12239a67d49SSandeepa Prabhu 
1232dd0e8d2SSandeepa Prabhu static bool __kprobes
1242dd0e8d2SSandeepa Prabhu is_probed_address_atomic(kprobe_opcode_t *scan_start, kprobe_opcode_t *scan_end)
1252dd0e8d2SSandeepa Prabhu {
126*3e593f66SDavid A. Long 	while (scan_start >= scan_end) {
1272dd0e8d2SSandeepa Prabhu 		/*
1282dd0e8d2SSandeepa Prabhu 		 * atomic region starts from exclusive load and ends with
1292dd0e8d2SSandeepa Prabhu 		 * exclusive store.
1302dd0e8d2SSandeepa Prabhu 		 */
1312dd0e8d2SSandeepa Prabhu 		if (aarch64_insn_is_store_ex(le32_to_cpu(*scan_start)))
1322dd0e8d2SSandeepa Prabhu 			return false;
1332dd0e8d2SSandeepa Prabhu 		else if (aarch64_insn_is_load_ex(le32_to_cpu(*scan_start)))
1342dd0e8d2SSandeepa Prabhu 			return true;
1352dd0e8d2SSandeepa Prabhu 		scan_start--;
1362dd0e8d2SSandeepa Prabhu 	}
1372dd0e8d2SSandeepa Prabhu 
1382dd0e8d2SSandeepa Prabhu 	return false;
1392dd0e8d2SSandeepa Prabhu }
1402dd0e8d2SSandeepa Prabhu 
1412dd0e8d2SSandeepa Prabhu enum kprobe_insn __kprobes
1422dd0e8d2SSandeepa Prabhu arm_kprobe_decode_insn(kprobe_opcode_t *addr, struct arch_specific_insn *asi)
1432dd0e8d2SSandeepa Prabhu {
1442dd0e8d2SSandeepa Prabhu 	enum kprobe_insn decoded;
1452dd0e8d2SSandeepa Prabhu 	kprobe_opcode_t insn = le32_to_cpu(*addr);
146*3e593f66SDavid A. Long 	kprobe_opcode_t *scan_end = NULL;
147*3e593f66SDavid A. Long 	unsigned long size = 0, offset = 0;
1482dd0e8d2SSandeepa Prabhu 
149*3e593f66SDavid A. Long 	/*
150*3e593f66SDavid A. Long 	 * If there's a symbol defined in front of and near enough to
151*3e593f66SDavid A. Long 	 * the probe address assume it is the entry point to this
152*3e593f66SDavid A. Long 	 * code and use it to further limit how far back we search
153*3e593f66SDavid A. Long 	 * when determining if we're in an atomic sequence. If we could
154*3e593f66SDavid A. Long 	 * not find any symbol skip the atomic test altogether as we
155*3e593f66SDavid A. Long 	 * could otherwise end up searching irrelevant text/literals.
156*3e593f66SDavid A. Long 	 * KPROBES depends on KALLSYMS so this last case should never
157*3e593f66SDavid A. Long 	 * happen.
158*3e593f66SDavid A. Long 	 */
159*3e593f66SDavid A. Long 	if (kallsyms_lookup_size_offset((unsigned long) addr, &size, &offset)) {
160*3e593f66SDavid A. Long 		if (offset < (MAX_ATOMIC_CONTEXT_SIZE*sizeof(kprobe_opcode_t)))
161*3e593f66SDavid A. Long 			scan_end = addr - (offset / sizeof(kprobe_opcode_t));
162*3e593f66SDavid A. Long 		else
163*3e593f66SDavid A. Long 			scan_end = addr - MAX_ATOMIC_CONTEXT_SIZE;
1642dd0e8d2SSandeepa Prabhu 	}
1652dd0e8d2SSandeepa Prabhu 	decoded = arm_probe_decode_insn(insn, asi);
1662dd0e8d2SSandeepa Prabhu 
167*3e593f66SDavid A. Long 	if (decoded != INSN_REJECTED && scan_end)
168*3e593f66SDavid A. Long 		if (is_probed_address_atomic(addr - 1, scan_end))
1692dd0e8d2SSandeepa Prabhu 			return INSN_REJECTED;
1702dd0e8d2SSandeepa Prabhu 
1712dd0e8d2SSandeepa Prabhu 	return decoded;
1722dd0e8d2SSandeepa Prabhu }
173