xref: /xnu-12377.1.9/bsd/dev/arm64/disassembler.c (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1*f6217f89SApple OSS Distributions /*
2*f6217f89SApple OSS Distributions  * Copyright (c) 2017-2018 Apple Inc. All rights reserved.
3*f6217f89SApple OSS Distributions  *
4*f6217f89SApple OSS Distributions  * Disassemblers for ARM64 (AArch64)
5*f6217f89SApple OSS Distributions  */
6*f6217f89SApple OSS Distributions 
7*f6217f89SApple OSS Distributions #include  <sys/fasttrap_isa.h>
8*f6217f89SApple OSS Distributions 
9*f6217f89SApple OSS Distributions uint8_t dtrace_decode_arm64(uint32_t instr);
10*f6217f89SApple OSS Distributions 
11*f6217f89SApple OSS Distributions struct arm64_decode_entry {
12*f6217f89SApple OSS Distributions 	uint32_t mask;
13*f6217f89SApple OSS Distributions 	uint32_t value;
14*f6217f89SApple OSS Distributions 	uint8_t type;
15*f6217f89SApple OSS Distributions };
16*f6217f89SApple OSS Distributions 
17*f6217f89SApple OSS Distributions struct arm64_decode_entry arm64_decode_table[] = {
18*f6217f89SApple OSS Distributions 	{ .mask = 0xFFFFFFFF, .value = FASTTRAP_ARM64_OP_VALUE_FUNC_ENTRY, .type = FASTTRAP_T_ARM64_STANDARD_FUNCTION_ENTRY },
19*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_LDR_S_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_LDR_S_PC_REL, .type = FASTTRAP_T_ARM64_LDR_S_PC_REL },
20*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_LDR_W_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_LDR_W_PC_REL, .type = FASTTRAP_T_ARM64_LDR_W_PC_REL },
21*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_LDR_D_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_LDR_D_PC_REL, .type = FASTTRAP_T_ARM64_LDR_D_PC_REL },
22*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_LDR_X_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_LDR_X_PC_REL, .type = FASTTRAP_T_ARM64_LDR_X_PC_REL },
23*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_LDR_Q_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_LDR_Q_PC_REL, .type = FASTTRAP_T_ARM64_LDR_Q_PC_REL },
24*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_LRDSW_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_LRDSW_PC_REL, .type = FASTTRAP_T_ARM64_LDRSW_PC_REL },
25*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_B_COND_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_B_COND_PC_REL, .type = FASTTRAP_T_ARM64_B_COND },
26*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_CBNZ_W_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_CBNZ_W_PC_REL, .type = FASTTRAP_T_ARM64_CBNZ_W },
27*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_CBNZ_X_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_CBNZ_X_PC_REL, .type = FASTTRAP_T_ARM64_CBNZ_X },
28*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_CBZ_W_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_CBZ_W_PC_REL, .type = FASTTRAP_T_ARM64_CBZ_W },
29*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_CBZ_X_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_CBZ_X_PC_REL, .type = FASTTRAP_T_ARM64_CBZ_X },
30*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_TBNZ_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_TBNZ_PC_REL, .type = FASTTRAP_T_ARM64_TBNZ },
31*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_TBZ_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_TBZ_PC_REL, .type = FASTTRAP_T_ARM64_TBZ },
32*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_B_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_B_PC_REL, .type = FASTTRAP_T_ARM64_B },
33*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_BL_PC_REL, .value = FASTTRAP_ARM64_OP_VALUE_BL_PC_REL, .type = FASTTRAP_T_ARM64_BL },
34*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_BLR, .value = FASTTRAP_ARM64_OP_VALUE_BLR, .type = FASTTRAP_T_ARM64_BLR },
35*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_BR, .value = FASTTRAP_ARM64_OP_VALUE_BR, .type = FASTTRAP_T_ARM64_BR },
36*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_RET, .value = FASTTRAP_ARM64_OP_VALUE_RET, .type = FASTTRAP_T_ARM64_RET },
37*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_ADRP, .value = FASTTRAP_ARM64_OP_VALUE_ADRP, .type = FASTTRAP_T_ARM64_ADRP },
38*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_ADR, .value = FASTTRAP_ARM64_OP_VALUE_ADR, .type = FASTTRAP_T_ARM64_ADR },
39*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_PRFM, .value = FASTTRAP_ARM64_OP_VALUE_PRFM, .type = FASTTRAP_T_ARM64_PRFM },
40*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_EXCL_MEM, .value = FASTTRAP_ARM64_OP_VALUE_EXCL_MEM, .type = FASTTRAP_T_ARM64_EXCLUSIVE_MEM },
41*f6217f89SApple OSS Distributions 	{ .mask = FASTTRAP_ARM64_OP_MASK_RETAB, .value = FASTTRAP_ARM64_OP_VALUE_RETAB, .type = FASTTRAP_T_ARM64_RETAB }
42*f6217f89SApple OSS Distributions };
43*f6217f89SApple OSS Distributions 
44*f6217f89SApple OSS Distributions #define NUM_DECODE_ENTRIES (sizeof(arm64_decode_table) / sizeof(struct arm64_decode_entry))
45*f6217f89SApple OSS Distributions 
46*f6217f89SApple OSS Distributions uint8_t
dtrace_decode_arm64(uint32_t instr)47*f6217f89SApple OSS Distributions dtrace_decode_arm64(uint32_t instr)
48*f6217f89SApple OSS Distributions {
49*f6217f89SApple OSS Distributions 	unsigned i;
50*f6217f89SApple OSS Distributions 
51*f6217f89SApple OSS Distributions 	for (i = 0; i < NUM_DECODE_ENTRIES; i++) {
52*f6217f89SApple OSS Distributions 		if ((instr & arm64_decode_table[i].mask) == arm64_decode_table[i].value) {
53*f6217f89SApple OSS Distributions 			return arm64_decode_table[i].type;
54*f6217f89SApple OSS Distributions 		}
55*f6217f89SApple OSS Distributions 	}
56*f6217f89SApple OSS Distributions 
57*f6217f89SApple OSS Distributions 	return FASTTRAP_T_COMMON;
58*f6217f89SApple OSS Distributions }
59