summaryrefslogtreecommitdiffstats
path: root/tools/objtool/arch/loongarch/decode.c
blob: aee479d2191c21a893a7ea04f01e93d68290978f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// SPDX-License-Identifier: GPL-2.0-or-later
#include <string.h>
#include <objtool/check.h>
#include <objtool/warn.h>
#include <asm/inst.h>
#include <asm/orc_types.h>
#include <linux/objtool_types.h>

#ifndef EM_LOONGARCH
#define EM_LOONGARCH	258
#endif

int arch_ftrace_match(char *name)
{
	return !strcmp(name, "_mcount");
}

unsigned long arch_jump_destination(struct instruction *insn)
{
	return insn->offset + (insn->immediate << 2);
}

unsigned long arch_dest_reloc_offset(int addend)
{
	return addend;
}

bool arch_pc_relative_reloc(struct reloc *reloc)
{
	return false;
}

bool arch_callee_saved_reg(unsigned char reg)
{
	switch (reg) {
	case CFI_RA:
	case CFI_FP:
	case CFI_S0 ... CFI_S8:
		return true;
	default:
		return false;
	}
}

int arch_decode_hint_reg(u8 sp_reg, int *base)
{
	switch (sp_reg) {
	case ORC_REG_UNDEFINED:
		*base = CFI_UNDEFINED;
		break;
	case ORC_REG_SP:
		*base = CFI_SP;
		break;
	case ORC_REG_FP:
		*base = CFI_FP;
		break;
	default:
		return -1;
	}

	return 0;
}

static bool is_loongarch(const struct elf *elf)
{
	if (elf->ehdr.e_machine == EM_LOONGARCH)
		return true;

	WARN("unexpected ELF machine type %d", elf->ehdr.e_machine);
	return false;
}

#define ADD_OP(op) \
	if (!(op = calloc(1, sizeof(*op)))) \
		return -1; \
	else for (*ops_list = op, ops_list = &op->next; op; op = NULL)

static bool decode_insn_reg0i26_fomat(union loongarch_instruction inst,
				      struct instruction *insn)
{
	switch (inst.reg0i26_format.opcode) {
	case b_op:
		insn->type = INSN_JUMP_UNCONDITIONAL;
		insn->immediate = sign_extend64(inst.reg0i26_format.immediate_h << 16 |
						inst.reg0i26_format.immediate_l, 25);
		break;
	case bl_op:
		insn->type = INSN_CALL;
		insn->immediate = sign_extend64(inst.reg0i26_format.immediate_h << 16 |
						inst.reg0i26_format.immediate_l, 25);
		break;
	default:
		return false;
	}

	return true;
}

static bool decode_insn_reg1i21_fomat(union loongarch_instruction inst,
				      struct instruction *insn)
{
	switch (inst.reg1i21_format.opcode) {
	case beqz_op:
	case bnez_op:
	case bceqz_op:
		insn->type = INSN_JUMP_CONDITIONAL;
		insn->immediate = sign_extend64(inst.reg1i21_format.immediate_h << 16 |
						inst.reg1i21_format.immediate_l, 20);
		break;
	default:
		return false;
	}

	return true;
}

static bool decode_insn_reg2i12_fomat(union loongarch_instruction inst,
				      struct instruction *insn,
				      struct stack_op **ops_list,
				      struct stack_op *op)
{
	switch (inst.reg2i12_format.opcode) {
	case addid_op:
		if ((inst.reg2i12_format.rd == CFI_SP) || (inst.reg2i12_format.rj == CFI_SP)) {
			/* addi.d sp,sp,si12 or addi.d fp,sp,si12 */
			insn->immediate = sign_extend64(inst.reg2i12_format.immediate, 11);
			ADD_OP(op) {
				op->src.type = OP_SRC_ADD;
				op->src.reg = inst.reg2i12_format.rj;
				op->src.offset = insn->immediate;
				op->dest.type = OP_DEST_REG;
				op->dest.reg = inst.reg2i12_format.rd;
			}
		}
		break;
	case ldd_op:
		if (inst.reg2i12_format.rj == CFI_SP) {
			/* ld.d rd,sp,si12 */
			insn->immediate = sign_extend64(inst.reg2i12_format.immediate, 11);
			ADD_OP(op) {
				op->src.type = OP_SRC_REG_INDIRECT;
				op->src.reg = CFI_SP;
				op->src.offset = insn->immediate;
				op->dest.type = OP_DEST_REG;
				op->dest.reg = inst.reg2i12_format.rd;
			}
		}
		break;
	case std_op:
		if (inst.reg2i12_format.rj == CFI_SP) {
			/* st.d rd,sp,si12 */
			insn->immediate = sign_extend64(inst.reg2i12_format.immediate, 11);
			ADD_OP(op) {
				op->src.type = OP_SRC_REG;
				op->src.reg = inst.reg2i12_format.rd;
				op->dest.type = OP_DEST_REG_INDIRECT;
				op->dest.reg = CFI_SP;
				op->dest.offset = insn->immediate;
			}
		}
		break;
	case andi_op:
		if (inst.reg2i12_format.rd == 0 &&
		    inst.reg2i12_format.rj == 0 &&
		    inst.reg2i12_format.immediate == 0)
			/* andi r0,r0,0 */
			insn->type = INSN_NOP;
		break;
	default:
		return false;
	}

	return true;
}

static bool decode_insn_reg2i14_fomat(union loongarch_instruction inst,
				      struct instruction *insn,
				      struct stack_op **ops_list,
				      struct stack_op *op)
{
	switch (inst.reg2i14_format.opcode) {
	case ldptrd_op:
		if (inst.reg2i14_format.rj == CFI_SP) {
			/* ldptr.d rd,sp,si14 */
			insn->immediate = sign_extend64(inst.reg2i14_format.immediate, 13);
			ADD_OP(op) {
				op->src.type = OP_SRC_REG_INDIRECT;
				op->src.reg = CFI_SP;
				op->src.offset = insn->immediate;
				op->dest.type = OP_DEST_REG;
				op->dest.reg = inst.reg2i14_format.rd;
			}
		}
		break;
	case stptrd_op:
		if (inst.reg2i14_format.rj == CFI_SP) {
			/* stptr.d ra,sp,0 */
			if (inst.reg2i14_format.rd == LOONGARCH_GPR_RA &&
			    inst.reg2i14_format.immediate == 0)
				break;

			/* stptr.d rd,sp,si14 */
			insn->immediate = sign_extend64(inst.reg2i14_format.immediate, 13);
			ADD_OP(op) {
				op->src.type = OP_SRC_REG;
				op->src.reg = inst.reg2i14_format.rd;
				op->dest.type = OP_DEST_REG_INDIRECT;
				op->dest.reg = CFI_SP;
				op->dest.offset = insn->immediate;
			}
		}
		break;
	default:
		return false;
	}

	return true;
}

static bool decode_insn_reg2i16_fomat(union loongarch_instruction inst,
				      struct instruction *insn)
{
	switch (inst.reg2i16_format.opcode) {
	case jirl_op:
		if (inst.reg2i16_format.rd == 0 &&
		    inst.reg2i16_format.rj == CFI_RA &&
		    inst.reg2i16_format.immediate == 0) {
			/* jirl r0,ra,0 */
			insn->type = INSN_RETURN;
		} else if (inst.reg2i16_format.rd == CFI_RA) {
			/* jirl ra,rj,offs16 */
			insn->type = INSN_CALL_DYNAMIC;
		} else if (inst.reg2i16_format.rd == CFI_A0 &&
			   inst.reg2i16_format.immediate == 0) {
			/*
			 * jirl a0,t0,0
			 * this is a special case in loongarch_suspend_enter,
			 * just treat it as a call instruction.
			 */
			insn->type = INSN_CALL_DYNAMIC;
		} else if (inst.reg2i16_format.rd == 0 &&
			   inst.reg2i16_format.immediate == 0) {
			/* jirl r0,rj,0 */
			insn->type = INSN_JUMP_DYNAMIC;
		} else if (inst.reg2i16_format.rd == 0 &&
			   inst.reg2i16_format.immediate != 0) {
			/*
			 * jirl r0,t0,12
			 * this is a rare case in JUMP_VIRT_ADDR,
			 * just ignore it due to it is harmless for tracing.
			 */
			break;
		} else {
			/* jirl rd,rj,offs16 */
			insn->type = INSN_JUMP_UNCONDITIONAL;
			insn->immediate = sign_extend64(inst.reg2i16_format.immediate, 15);
		}
		break;
	case beq_op:
	case bne_op:
	case blt_op:
	case bge_op:
	case bltu_op:
	case bgeu_op:
		insn->type = INSN_JUMP_CONDITIONAL;
		insn->immediate = sign_extend64(inst.reg2i16_format.immediate, 15);
		break;
	default:
		return false;
	}

	return true;
}

int arch_decode_instruction(struct objtool_file *file, const struct section *sec,
			    unsigned long offset, unsigned int maxlen,
			    struct instruction *insn)
{
	struct stack_op **ops_list = &insn->stack_ops;
	const struct elf *elf = file->elf;
	struct stack_op *op = NULL;
	union loongarch_instruction inst;

	if (!is_loongarch(elf))
		return -1;

	if (maxlen < LOONGARCH_INSN_SIZE)
		return 0;

	insn->len = LOONGARCH_INSN_SIZE;
	insn->type = INSN_OTHER;
	insn->immediate = 0;

	inst = *(union loongarch_instruction *)(sec->data->d_buf + offset);

	if (decode_insn_reg0i26_fomat(inst, insn))
		return 0;
	if (decode_insn_reg1i21_fomat(inst, insn))
		return 0;
	if (decode_insn_reg2i12_fomat(inst, insn, ops_list, op))
		return 0;
	if (decode_insn_reg2i14_fomat(inst, insn, ops_list, op))
		return 0;
	if (decode_insn_reg2i16_fomat(inst, insn))
		return 0;

	if (inst.word == 0)
		insn->type = INSN_NOP;
	else if (inst.reg0i15_format.opcode == break_op) {
		/* break */
		insn->type = INSN_BUG;
	} else if (inst.reg2_format.opcode == ertn_op) {
		/* ertn */
		insn->type = INSN_RETURN;
	}

	return 0;
}

const char *arch_nop_insn(int len)
{
	static u32 nop;

	if (len != LOONGARCH_INSN_SIZE)
		WARN("invalid NOP size: %d\n", len);

	nop = LOONGARCH_INSN_NOP;

	return (const char *)&nop;
}

const char *arch_ret_insn(int len)
{
	static u32 ret;

	if (len != LOONGARCH_INSN_SIZE)
		WARN("invalid RET size: %d\n", len);

	emit_jirl((union loongarch_instruction *)&ret, LOONGARCH_GPR_RA, LOONGARCH_GPR_ZERO, 0);

	return (const char *)&ret;
}

void arch_initial_func_cfi_state(struct cfi_init_state *state)
{
	int i;

	for (i = 0; i < CFI_NUM_REGS; i++) {
		state->regs[i].base = CFI_UNDEFINED;
		state->regs[i].offset = 0;
	}

	/* initial CFA (call frame address) */
	state->cfa.base = CFI_SP;
	state->cfa.offset = 0;
}