License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-2.6-block.git] / arch / x86 / mm / kmemcheck / opcode.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
dfec072e
VN
2#include <linux/types.h>
3
4#include "opcode.h"
5
6static bool opcode_is_prefix(uint8_t b)
7{
8 return
9 /* Group 1 */
10 b == 0xf0 || b == 0xf2 || b == 0xf3
11 /* Group 2 */
12 || b == 0x2e || b == 0x36 || b == 0x3e || b == 0x26
9fbaf49c 13 || b == 0x64 || b == 0x65
dfec072e
VN
14 /* Group 3 */
15 || b == 0x66
16 /* Group 4 */
17 || b == 0x67;
18}
19
6d9609c1 20#ifdef CONFIG_X86_64
dfec072e
VN
21static bool opcode_is_rex_prefix(uint8_t b)
22{
23 return (b & 0xf0) == 0x40;
24}
6d9609c1
PE
25#else
26static bool opcode_is_rex_prefix(uint8_t b)
27{
28 return false;
29}
30#endif
dfec072e
VN
31
32#define REX_W (1 << 3)
33
34/*
35 * This is a VERY crude opcode decoder. We only need to find the size of the
36 * load/store that caused our #PF and this should work for all the opcodes
37 * that we care about. Moreover, the ones who invented this instruction set
38 * should be shot.
39 */
40void kmemcheck_opcode_decode(const uint8_t *op, unsigned int *size)
41{
42 /* Default operand size */
43 int operand_size_override = 4;
44
45 /* prefixes */
46 for (; opcode_is_prefix(*op); ++op) {
47 if (*op == 0x66)
48 operand_size_override = 2;
49 }
50
dfec072e
VN
51 /* REX prefix */
52 if (opcode_is_rex_prefix(*op)) {
53 uint8_t rex = *op;
54
55 ++op;
56 if (rex & REX_W) {
57 switch (*op) {
58 case 0x63:
59 *size = 4;
60 return;
61 case 0x0f:
62 ++op;
63
64 switch (*op) {
65 case 0xb6:
66 case 0xbe:
67 *size = 1;
68 return;
69 case 0xb7:
70 case 0xbf:
71 *size = 2;
72 return;
73 }
74
75 break;
76 }
77
78 *size = 8;
79 return;
80 }
81 }
dfec072e
VN
82
83 /* escape opcode */
84 if (*op == 0x0f) {
85 ++op;
86
87 /*
88 * This is move with zero-extend and sign-extend, respectively;
89 * we don't have to think about 0xb6/0xbe, because this is
90 * already handled in the conditional below.
91 */
92 if (*op == 0xb7 || *op == 0xbf)
93 operand_size_override = 2;
94 }
95
96 *size = (*op & 1) ? operand_size_override : 1;
97}
98
99const uint8_t *kmemcheck_opcode_get_primary(const uint8_t *op)
100{
101 /* skip prefixes */
102 while (opcode_is_prefix(*op))
103 ++op;
104 if (opcode_is_rex_prefix(*op))
105 ++op;
106 return op;
107}