blob: 62e7b8bcd2dc8461c153edfab2f64c364c1e462d [file] [log] [blame]
Jiang Liuf8dadc92014-01-07 22:17:08 +08001/*
2 * Copyright (C) 2013 Huawei Ltd.
3 * Author: Jiang Liu <liuj97@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17#ifndef __ASM_INSN_H
18#define __ASM_INSN_H
Mark Brownd10fbc12014-05-29 16:02:15 +010019
Jiang Liuf8dadc92014-01-07 22:17:08 +080020#include <linux/types.h>
21
Jiang Liu9f6c8442014-01-07 22:17:09 +080022/* A64 instructions are always 32 bits. */
23#define AARCH64_INSN_SIZE 4
24
Mark Brownd10fbc12014-05-29 16:02:15 +010025#ifndef __ASSEMBLY__
26
Jiang Liuf8dadc92014-01-07 22:17:08 +080027/*
28 * ARM Architecture Reference Manual for ARMv8 Profile-A, Issue A.a
29 * Section C3.1 "A64 instruction index by encoding":
30 * AArch64 main encoding table
31 * Bit position
32 * 28 27 26 25 Encoding Group
33 * 0 0 - - Unallocated
34 * 1 0 0 - Data processing, immediate
35 * 1 0 1 - Branch, exception generation and system instructions
36 * - 1 - 0 Loads and stores
37 * - 1 0 1 Data processing - register
38 * 0 1 1 1 Data processing - SIMD and floating point
39 * 1 1 1 1 Data processing - SIMD and floating point
40 * "-" means "don't care"
41 */
42enum aarch64_insn_encoding_class {
43 AARCH64_INSN_CLS_UNKNOWN, /* UNALLOCATED */
44 AARCH64_INSN_CLS_DP_IMM, /* Data processing - immediate */
45 AARCH64_INSN_CLS_DP_REG, /* Data processing - register */
46 AARCH64_INSN_CLS_DP_FPSIMD, /* Data processing - SIMD and FP */
47 AARCH64_INSN_CLS_LDST, /* Loads and stores */
48 AARCH64_INSN_CLS_BR_SYS, /* Branch, exception generation and
49 * system instructions */
50};
51
52enum aarch64_insn_hint_op {
53 AARCH64_INSN_HINT_NOP = 0x0 << 5,
54 AARCH64_INSN_HINT_YIELD = 0x1 << 5,
55 AARCH64_INSN_HINT_WFE = 0x2 << 5,
56 AARCH64_INSN_HINT_WFI = 0x3 << 5,
57 AARCH64_INSN_HINT_SEV = 0x4 << 5,
58 AARCH64_INSN_HINT_SEVL = 0x5 << 5,
59};
60
Jiang Liu0eebc282014-01-07 22:17:10 +080061enum aarch64_insn_imm_type {
62 AARCH64_INSN_IMM_ADR,
63 AARCH64_INSN_IMM_26,
64 AARCH64_INSN_IMM_19,
65 AARCH64_INSN_IMM_16,
66 AARCH64_INSN_IMM_14,
67 AARCH64_INSN_IMM_12,
68 AARCH64_INSN_IMM_9,
69 AARCH64_INSN_IMM_MAX
70};
71
Jiang Liu228d2492014-01-07 22:17:11 +080072enum aarch64_insn_branch_type {
73 AARCH64_INSN_BRANCH_NOLINK,
74 AARCH64_INSN_BRANCH_LINK,
75};
76
Jiang Liuf8dadc92014-01-07 22:17:08 +080077#define __AARCH64_INSN_FUNCS(abbr, mask, val) \
78static __always_inline bool aarch64_insn_is_##abbr(u32 code) \
79{ return (code & (mask)) == (val); } \
80static __always_inline u32 aarch64_insn_get_##abbr##_value(void) \
81{ return (val); }
82
83__AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000)
84__AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000)
85__AARCH64_INSN_FUNCS(svc, 0xFFE0001F, 0xD4000001)
86__AARCH64_INSN_FUNCS(hvc, 0xFFE0001F, 0xD4000002)
87__AARCH64_INSN_FUNCS(smc, 0xFFE0001F, 0xD4000003)
88__AARCH64_INSN_FUNCS(brk, 0xFFE0001F, 0xD4200000)
89__AARCH64_INSN_FUNCS(hint, 0xFFFFF01F, 0xD503201F)
90
91#undef __AARCH64_INSN_FUNCS
92
93bool aarch64_insn_is_nop(u32 insn);
94
Jiang Liu9f6c8442014-01-07 22:17:09 +080095int aarch64_insn_read(void *addr, u32 *insnp);
96int aarch64_insn_write(void *addr, u32 insn);
Jiang Liuf8dadc92014-01-07 22:17:08 +080097enum aarch64_insn_encoding_class aarch64_get_insn_class(u32 insn);
Jiang Liu0eebc282014-01-07 22:17:10 +080098u32 aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type,
99 u32 insn, u64 imm);
Jiang Liu228d2492014-01-07 22:17:11 +0800100u32 aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr,
101 enum aarch64_insn_branch_type type);
102u32 aarch64_insn_gen_hint(enum aarch64_insn_hint_op op);
103u32 aarch64_insn_gen_nop(void);
104
Jiang Liuf8dadc92014-01-07 22:17:08 +0800105bool aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn);
106
Jiang Liu9f6c8442014-01-07 22:17:09 +0800107int aarch64_insn_patch_text_nosync(void *addr, u32 insn);
108int aarch64_insn_patch_text_sync(void *addrs[], u32 insns[], int cnt);
109int aarch64_insn_patch_text(void *addrs[], u32 insns[], int cnt);
110
Mark Brownd10fbc12014-05-29 16:02:15 +0100111#endif /* __ASSEMBLY__ */
112
Jiang Liuf8dadc92014-01-07 22:17:08 +0800113#endif /* __ASM_INSN_H */