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