Skip to contentSkip to conversation

x86/kprobes: Use switch-case for 0xFF opcodes in prepare_emulation

PATCH Applied Chuang Wang 3 participants 3 messages Nov 29, 2022 – Jan 10, 2023 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/kprobes: Use switch-case for 0xFF opcodes in prepare_emulation

For the FF /digit opcodes in prepare_emulation, use switch-case instead of hand-written code to make the logic easier to understand.

---
 arch/x86/kernel/kprobes/core.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
arch/x86/kernel/kprobes/core.c +11 -9
index 66299682b6b7..66ec32b6cd0e 100644
@@ -655,17 +655,19 @@ static int prepare_emulation(struct kprobe *p, struct insn *insn)
 		 * is determined by the MOD/RM byte.
 		 */
 		opcode = insn->modrm.bytes[0];
-		if ((opcode & 0x30) == 0x10) {
-			if ((opcode & 0x8) == 0x8)
-				return -EOPNOTSUPP;	/* far call */
-			/* call absolute, indirect */
+		switch (X86_MODRM_REG(opcode)) {
+		case 0b010:	/* FF /2, call near, absolute indirect */
 			p->ainsn.emulate_op = kprobe_emulate_call_indirect;
-		} else if ((opcode & 0x30) == 0x20) {
-			if ((opcode & 0x8) == 0x8)
-				return -EOPNOTSUPP;	/* far jmp */
-			/* jmp near absolute indirect */
+			break;
+		case 0b100:	/* FF /4, jmp near, absolute indirect */
 			p->ainsn.emulate_op = kprobe_emulate_jmp_indirect;
-		} else
+			break;
+		case 0b011:	/* FF /3, call far, absolute indirect */
+		case 0b101:	/* FF /5, jmp far, absolute indirect */
+			return -EOPNOTSUPP;
+		}
+
+		if (!p->ainsn.emulate_op)
 			break;
 
 		if (insn->addr_bytes != sizeof(unsigned long))
Signature · 1 line
2.37.2
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · On Tue, 29 Nov 2022 16:39:51 +0800 Chuang Wang <[email protected]> wrote:

For the FF /digit opcodes in prepare_emulation, use switch-case instead of hand-written code to make the logic easier to understand.

This looks good to me.

Thank you!

Quoted 40 lines
---
 arch/x86/kernel/kprobes/core.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
arch/x86/kernel/kprobes/core.c +11 -9
index 66299682b6b7..66ec32b6cd0e 100644
@@ -655,17 +655,19 @@ static int prepare_emulation(struct kprobe *p, struct insn *insn)
 		 * is determined by the MOD/RM byte.
 		 */
 		opcode = insn->modrm.bytes[0];
-		if ((opcode & 0x30) == 0x10) {
-			if ((opcode & 0x8) == 0x8)
-				return -EOPNOTSUPP;	/* far call */
-			/* call absolute, indirect */
+		switch (X86_MODRM_REG(opcode)) {
+		case 0b010:	/* FF /2, call near, absolute indirect */
 			p->ainsn.emulate_op = kprobe_emulate_call_indirect;
-		} else if ((opcode & 0x30) == 0x20) {
-			if ((opcode & 0x8) == 0x8)
-				return -EOPNOTSUPP;	/* far jmp */
-			/* jmp near absolute indirect */
+			break;
+		case 0b100:	/* FF /4, jmp near, absolute indirect */
 			p->ainsn.emulate_op = kprobe_emulate_jmp_indirect;
-		} else
+			break;
+		case 0b011:	/* FF /3, call far, absolute indirect */
+		case 0b101:	/* FF /5, jmp far, absolute indirect */
+			return -EOPNOTSUPP;
+		}
+
+		if (!p->ainsn.emulate_op)
 			break;
 
 		if (insn->addr_bytes != sizeof(unsigned long))
Signature · 1 line
2.37.2
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
merge bot Applied
#2 lore raw
In reply to Chuang Wang
The following commit has been merged into the perf/core branch of tip:

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     9fcad995c6c52cc9791f7ee9f1386a5684055f9c
Gitweb:        https://git.kernel.org/tip/9fcad995c6c52cc9791f7ee9f1386a5684055f9c
Author:        Chuang Wang <[email protected]>
AuthorDate:    Tue, 29 Nov 2022 16:39:51 +08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Tue, 10 Jan 2023 12:37:14 +01:00

x86/kprobes: Use switch-case for 0xFF opcodes in prepare_emulation

For the FF /digit opcodes in prepare_emulation, use switch-case instead of hand-written code to make the logic easier to understand.

---
 arch/x86/kernel/kprobes/core.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
arch/x86/kernel/kprobes/core.c +11 -9
index 33390ed..fd0420a 100644
@@ -655,17 +655,19 @@ static int prepare_emulation(struct kprobe *p, struct insn *insn)
 		 * is determined by the MOD/RM byte.
 		 */
 		opcode = insn->modrm.bytes[0];
-		if ((opcode & 0x30) == 0x10) {
-			if ((opcode & 0x8) == 0x8)
-				return -EOPNOTSUPP;	/* far call */
-			/* call absolute, indirect */
+		switch (X86_MODRM_REG(opcode)) {
+		case 0b010:	/* FF /2, call near, absolute indirect */
 			p->ainsn.emulate_op = kprobe_emulate_call_indirect;
-		} else if ((opcode & 0x30) == 0x20) {
-			if ((opcode & 0x8) == 0x8)
-				return -EOPNOTSUPP;	/* far jmp */
-			/* jmp near absolute indirect */
+			break;
+		case 0b100:	/* FF /4, jmp near, absolute indirect */
 			p->ainsn.emulate_op = kprobe_emulate_jmp_indirect;
-		} else
+			break;
+		case 0b011:	/* FF /3, call far, absolute indirect */
+		case 0b101:	/* FF /5, jmp far, absolute indirect */
+			return -EOPNOTSUPP;
+		}
+
+		if (!p->ainsn.emulate_op)
 			break;
 
 		if (insn->addr_bytes != sizeof(unsigned long))