1 /*
2  * Copyright (C) 2013 Huawei Ltd.
3  * Author: Jiang Liu <liuj97@gmail.com>
4  *
5  * Copyright (C) 2014 Zi Shen Lim <zlim.lnx@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef __ARCH_ARM_ARM64_INSN
20 #define __ARCH_ARM_ARM64_INSN
21 
22 #include <xen/types.h>
23 #include <xen/stdbool.h>
24 
25 enum aarch64_insn_hint_op {
26 	AARCH64_INSN_HINT_NOP	= 0x0 << 5,
27 	AARCH64_INSN_HINT_YIELD	= 0x1 << 5,
28 	AARCH64_INSN_HINT_WFE	= 0x2 << 5,
29 	AARCH64_INSN_HINT_WFI	= 0x3 << 5,
30 	AARCH64_INSN_HINT_SEV	= 0x4 << 5,
31 	AARCH64_INSN_HINT_SEVL	= 0x5 << 5,
32 };
33 
34 enum aarch64_insn_imm_type {
35 	AARCH64_INSN_IMM_ADR,
36 	AARCH64_INSN_IMM_26,
37 	AARCH64_INSN_IMM_19,
38 	AARCH64_INSN_IMM_16,
39 	AARCH64_INSN_IMM_14,
40 	AARCH64_INSN_IMM_12,
41 	AARCH64_INSN_IMM_9,
42 	AARCH64_INSN_IMM_7,
43 	AARCH64_INSN_IMM_6,
44 	AARCH64_INSN_IMM_S,
45 	AARCH64_INSN_IMM_R,
46 	AARCH64_INSN_IMM_MAX
47 };
48 
49 enum aarch64_insn_branch_type {
50 	AARCH64_INSN_BRANCH_NOLINK,
51 	AARCH64_INSN_BRANCH_LINK,
52 	AARCH64_INSN_BRANCH_RETURN,
53 	AARCH64_INSN_BRANCH_COMP_ZERO,
54 	AARCH64_INSN_BRANCH_COMP_NONZERO,
55 };
56 
57 #define	__AARCH64_INSN_FUNCS(abbr, mask, val)	\
58 static always_inline bool aarch64_insn_is_##abbr(u32 code) \
59 { return (code & (mask)) == (val); } \
60 static always_inline u32 aarch64_insn_get_##abbr##_value(void) \
61 { return (val); }
62 
63 __AARCH64_INSN_FUNCS(b,		0xFC000000, 0x14000000)
64 __AARCH64_INSN_FUNCS(bl,	0xFC000000, 0x94000000)
65 __AARCH64_INSN_FUNCS(cbz,	0x7F000000, 0x34000000)
66 __AARCH64_INSN_FUNCS(cbnz,	0x7F000000, 0x35000000)
67 __AARCH64_INSN_FUNCS(tbz,	0x7F000000, 0x36000000)
68 __AARCH64_INSN_FUNCS(tbnz,	0x7F000000, 0x37000000)
69 __AARCH64_INSN_FUNCS(bcond,	0xFF000010, 0x54000000)
70 __AARCH64_INSN_FUNCS(hint,	0xFFFFF01F, 0xD503201F)
71 
72 bool aarch64_insn_is_branch_imm(u32 insn);
73 
74 u64 aarch64_insn_decode_immediate(enum aarch64_insn_imm_type type, u32 insn);
75 u32 aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type,
76 				  u32 insn, u64 imm);
77 
78 s32 aarch64_get_branch_offset(u32 insn);
79 u32 aarch64_set_branch_offset(u32 insn, s32 offset);
80 
81 u32 aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr,
82 				enum aarch64_insn_branch_type type);
83 u32 aarch64_insn_gen_hint(enum aarch64_insn_hint_op op);
84 u32 aarch64_insn_gen_nop(void);
85 
86 /* Wrapper for common code */
insn_is_branch_imm(u32 insn)87 static inline bool insn_is_branch_imm(u32 insn)
88 {
89     return aarch64_insn_is_branch_imm(insn);
90 }
91 
insn_get_branch_offset(u32 insn)92 static inline s32 insn_get_branch_offset(u32 insn)
93 {
94     return aarch64_get_branch_offset(insn);
95 }
96 
insn_set_branch_offset(u32 insn,s32 offset)97 static inline u32 insn_set_branch_offset(u32 insn, s32 offset)
98 {
99     return aarch64_set_branch_offset(insn, offset);
100 }
101 
102 #endif /* !__ARCH_ARM_ARM64_INSN */
103 /*
104  * Local variables:
105  * mode: C
106  * c-file-style: "BSD"
107  * c-basic-offset: 8
108  * indent-tabs-mode: t
109  * End:
110  */
111