aboutsummaryrefslogtreecommitdiff
path: root/arch/arm64/kernel/probes-simulate-insn.c
blob: a224c91001d9d3750889f69b6a8da26bee7b7ad2 (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
/*
 * arch/arm64/kernel/probes-simulate-insn.c
 *
 * Copyright (C) 2013 Linaro Limited.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#include <linux/kernel.h>
#include <linux/kprobes.h>
#include <linux/module.h>

#include "probes-simulate-insn.h"

#define sign_extend(x, signbit)		\
	((x) | (0 - ((x) & (1 << (signbit)))))

#define bbl_displacement(insn)		\
	sign_extend(((insn) & 0x3ffffff) << 2, 27)

#define bcond_displacement(insn)	\
	sign_extend(((insn >> 5) & 0x7ffff) << 2, 21)

#define cbz_displacement(insn)	\
	sign_extend(((insn >> 5) & 0x7ffff) << 2, 21)

#define tbz_displacement(insn)	\
	sign_extend(((insn >> 5) & 0x3fff) << 2, 15)

#define ldr_displacement(insn)	\
	sign_extend(((insn >> 5) & 0x7ffff) << 2, 21)


unsigned long __kprobes check_cbz(u32 opcode, struct pt_regs *regs)
{
	int xn = opcode & 0x1f;

	return (opcode & (1 << 31)) ?
	    !(regs->regs[xn]) : !(regs->regs[xn] & 0xffffffff);
}

unsigned long __kprobes check_cbnz(u32 opcode, struct pt_regs *regs)
{
	int xn = opcode & 0x1f;

	return (opcode & (1 << 31)) ?
	    (regs->regs[xn]) : (regs->regs[xn] & 0xffffffff);
}

unsigned long __kprobes check_tbz(u32 opcode, struct pt_regs *regs)
{
	int xn = opcode & 0x1f;
	int bit_pos = ((opcode & (1 << 31)) >> 26) | ((opcode >> 19) & 0x1f);

	return ~((regs->regs[xn] >> bit_pos) & 0x1);
}

unsigned long __kprobes check_tbnz(u32 opcode, struct pt_regs *regs)
{
	int xn = opcode & 0x1f;
	int bit_pos = ((opcode & (1 << 31)) >> 26) | ((opcode >> 19) & 0x1f);

	return (regs->regs[xn] >> bit_pos) & 0x1;
}

/*
 * instruction simulate functions
 */
void __kprobes simulate_none(u32 opcode, long addr, struct pt_regs *regs)
{
}

void __kprobes
simulate_adr_adrp(u32 opcode, long addr, struct pt_regs *regs)
{
	long imm, xn, val;

	xn = opcode & 0x1f;
	imm = ((opcode >> 3) & 0x1ffffc) | ((opcode >> 29) & 0x3);
	imm = sign_extend(imm, 20);
	if (opcode & 0x80000000)
		val = (imm<<12) + (addr & 0xfffffffffffff000);
	else
		val = imm + addr;

	regs->regs[xn] = val;

	instruction_pointer(regs) += 4;
}

void __kprobes
simulate_b_bl(u32 opcode, long addr, struct pt_regs *regs)
{
	int disp = bbl_displacement(opcode);

	/* Link register is x30 */
	if (opcode & (1 << 31))
		regs->regs[30] = addr + 4;

	instruction_pointer(regs) = addr + disp;
}

void __kprobes
simulate_b_cond(u32 opcode, long addr, struct pt_regs *regs)
{
	int disp = bcond_displacement(opcode);

	instruction_pointer(regs) = addr + disp;
}

void __kprobes
simulate_br_blr_ret(u32 opcode, long addr, struct pt_regs *regs)
{
	int xn = (opcode >> 5) & 0x1f;

	/* Link register is x30 */
	if (((opcode >> 21) & 0x3) == 1)
		regs->regs[30] = addr + 4;

	instruction_pointer(regs) = regs->regs[xn];
}

void __kprobes
simulate_cbz_cbnz(u32 opcode, long addr, struct pt_regs *regs)
{
	int disp = cbz_displacement(opcode);

	instruction_pointer(regs) = addr + disp;
}

void __kprobes
simulate_tbz_tbnz(u32 opcode, long addr, struct pt_regs *regs)
{
	int disp = tbz_displacement(opcode);

	instruction_pointer(regs) = addr + disp;
}

void __kprobes
simulate_ldr_literal(u32 opcode, long addr, struct pt_regs *regs)
{
	u64 *load_addr;
	int xn = opcode & 0x1f;
	int disp = ldr_displacement(opcode);

	load_addr = (u64 *) (addr + disp);

	if (opcode & (1 << 30))	/* x0-x31 */
		regs->regs[xn] = *load_addr;
	else			/* w0-w31 */
		*(u32 *) (&regs->regs[xn]) = (*(u32 *) (load_addr));

	instruction_pointer(regs) += 4;
}

void __kprobes
simulate_ldrsw_literal(u32 opcode, long addr, struct pt_regs *regs)
{
	s32 *load_addr;
	int xn = opcode & 0x1f;
	int disp = ldr_displacement(opcode);

	load_addr = (s32 *) (addr + disp);
	regs->regs[xn] = *load_addr;

	instruction_pointer(regs) += 4;
}