blob: 6a902b155f5d6a09e6fd12883d0bda3abfed451b [file] [log] [blame]
Ricardo Neri32542ee2017-10-27 13:25:36 -07001/*
2 * Utility functions for x86 operand and address decoding
3 *
4 * Copyright (C) Intel Corporation 2017
5 */
6#include <linux/kernel.h>
7#include <linux/string.h>
Ricardo Neried594e42017-10-27 13:25:37 -07008#include <linux/ratelimit.h>
Ricardo Neri32542ee2017-10-27 13:25:36 -07009#include <asm/inat.h>
10#include <asm/insn.h>
11#include <asm/insn-eval.h>
Ricardo Neri32d0b952017-10-27 13:25:40 -070012#include <asm/vm86.h>
Ricardo Neri32542ee2017-10-27 13:25:36 -070013
Ricardo Neried594e42017-10-27 13:25:37 -070014#undef pr_fmt
15#define pr_fmt(fmt) "insn: " fmt
16
Ricardo Neri32542ee2017-10-27 13:25:36 -070017enum reg_type {
18 REG_TYPE_RM = 0,
19 REG_TYPE_INDEX,
20 REG_TYPE_BASE,
21};
22
Ricardo Neri536b8152017-10-27 13:25:39 -070023/**
24 * is_string_insn() - Determine if instruction is a string instruction
25 * @insn: Instruction containing the opcode to inspect
26 *
27 * Returns:
28 *
29 * true if the instruction, determined by the opcode, is any of the
30 * string instructions as defined in the Intel Software Development manual.
31 * False otherwise.
32 */
33static bool is_string_insn(struct insn *insn)
34{
35 insn_get_opcode(insn);
36
37 /* All string instructions have a 1-byte opcode. */
38 if (insn->opcode.nbytes != 1)
39 return false;
40
41 switch (insn->opcode.bytes[0]) {
42 case 0x6c ... 0x6f: /* INS, OUTS */
43 case 0xa4 ... 0xa7: /* MOVS, CMPS */
44 case 0xaa ... 0xaf: /* STOS, LODS, SCAS */
45 return true;
46 default:
47 return false;
48 }
49}
50
Ricardo Neri32d0b952017-10-27 13:25:40 -070051/**
52 * get_seg_reg_override_idx() - obtain segment register override index
53 * @insn: Valid instruction with segment override prefixes
54 *
55 * Inspect the instruction prefixes in @insn and find segment overrides, if any.
56 *
57 * Returns:
58 *
59 * A constant identifying the segment register to use, among CS, SS, DS,
60 * ES, FS, or GS. INAT_SEG_REG_DEFAULT is returned if no segment override
61 * prefixes were found.
62 *
63 * -EINVAL in case of error.
64 */
65static int get_seg_reg_override_idx(struct insn *insn)
66{
67 int idx = INAT_SEG_REG_DEFAULT;
68 int num_overrides = 0, i;
69
70 insn_get_prefixes(insn);
71
72 /* Look for any segment override prefixes. */
73 for (i = 0; i < insn->prefixes.nbytes; i++) {
74 insn_attr_t attr;
75
76 attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
77 switch (attr) {
78 case INAT_MAKE_PREFIX(INAT_PFX_CS):
79 idx = INAT_SEG_REG_CS;
80 num_overrides++;
81 break;
82 case INAT_MAKE_PREFIX(INAT_PFX_SS):
83 idx = INAT_SEG_REG_SS;
84 num_overrides++;
85 break;
86 case INAT_MAKE_PREFIX(INAT_PFX_DS):
87 idx = INAT_SEG_REG_DS;
88 num_overrides++;
89 break;
90 case INAT_MAKE_PREFIX(INAT_PFX_ES):
91 idx = INAT_SEG_REG_ES;
92 num_overrides++;
93 break;
94 case INAT_MAKE_PREFIX(INAT_PFX_FS):
95 idx = INAT_SEG_REG_FS;
96 num_overrides++;
97 break;
98 case INAT_MAKE_PREFIX(INAT_PFX_GS):
99 idx = INAT_SEG_REG_GS;
100 num_overrides++;
101 break;
102 /* No default action needed. */
103 }
104 }
105
106 /* More than one segment override prefix leads to undefined behavior. */
107 if (num_overrides > 1)
108 return -EINVAL;
109
110 return idx;
111}
112
113/**
114 * check_seg_overrides() - check if segment override prefixes are allowed
115 * @insn: Valid instruction with segment override prefixes
116 * @regoff: Operand offset, in pt_regs, for which the check is performed
117 *
118 * For a particular register used in register-indirect addressing, determine if
119 * segment override prefixes can be used. Specifically, no overrides are allowed
120 * for rDI if used with a string instruction.
121 *
122 * Returns:
123 *
124 * True if segment override prefixes can be used with the register indicated
125 * in @regoff. False if otherwise.
126 */
127static bool check_seg_overrides(struct insn *insn, int regoff)
128{
129 if (regoff == offsetof(struct pt_regs, di) && is_string_insn(insn))
130 return false;
131
132 return true;
133}
134
135/**
136 * resolve_default_seg() - resolve default segment register index for an operand
137 * @insn: Instruction with opcode and address size. Must be valid.
138 * @regs: Register values as seen when entering kernel mode
139 * @off: Operand offset, in pt_regs, for which resolution is needed
140 *
141 * Resolve the default segment register index associated with the instruction
142 * operand register indicated by @off. Such index is resolved based on defaults
143 * described in the Intel Software Development Manual.
144 *
145 * Returns:
146 *
147 * If in protected mode, a constant identifying the segment register to use,
148 * among CS, SS, ES or DS. If in long mode, INAT_SEG_REG_IGNORE.
149 *
150 * -EINVAL in case of error.
151 */
152static int resolve_default_seg(struct insn *insn, struct pt_regs *regs, int off)
153{
154 if (user_64bit_mode(regs))
155 return INAT_SEG_REG_IGNORE;
156 /*
157 * Resolve the default segment register as described in Section 3.7.4
158 * of the Intel Software Development Manual Vol. 1:
159 *
160 * + DS for all references involving r[ABCD]X, and rSI.
161 * + If used in a string instruction, ES for rDI. Otherwise, DS.
162 * + AX, CX and DX are not valid register operands in 16-bit address
163 * encodings but are valid for 32-bit and 64-bit encodings.
164 * + -EDOM is reserved to identify for cases in which no register
165 * is used (i.e., displacement-only addressing). Use DS.
166 * + SS for rSP or rBP.
167 * + CS for rIP.
168 */
169
170 switch (off) {
171 case offsetof(struct pt_regs, ax):
172 case offsetof(struct pt_regs, cx):
173 case offsetof(struct pt_regs, dx):
174 /* Need insn to verify address size. */
175 if (insn->addr_bytes == 2)
176 return -EINVAL;
177
178 case -EDOM:
179 case offsetof(struct pt_regs, bx):
180 case offsetof(struct pt_regs, si):
181 return INAT_SEG_REG_DS;
182
183 case offsetof(struct pt_regs, di):
184 if (is_string_insn(insn))
185 return INAT_SEG_REG_ES;
186 return INAT_SEG_REG_DS;
187
188 case offsetof(struct pt_regs, bp):
189 case offsetof(struct pt_regs, sp):
190 return INAT_SEG_REG_SS;
191
192 case offsetof(struct pt_regs, ip):
193 return INAT_SEG_REG_CS;
194
195 default:
196 return -EINVAL;
197 }
198}
199
200/**
201 * resolve_seg_reg() - obtain segment register index
202 * @insn: Instruction with operands
203 * @regs: Register values as seen when entering kernel mode
204 * @regoff: Operand offset, in pt_regs, used to deterimine segment register
205 *
206 * Determine the segment register associated with the operands and, if
207 * applicable, prefixes and the instruction pointed by @insn.
208 *
209 * The segment register associated to an operand used in register-indirect
210 * addressing depends on:
211 *
212 * a) Whether running in long mode (in such a case segments are ignored, except
213 * if FS or GS are used).
214 *
215 * b) Whether segment override prefixes can be used. Certain instructions and
216 * registers do not allow override prefixes.
217 *
218 * c) Whether segment overrides prefixes are found in the instruction prefixes.
219 *
220 * d) If there are not segment override prefixes or they cannot be used, the
221 * default segment register associated with the operand register is used.
222 *
223 * The function checks first if segment override prefixes can be used with the
224 * operand indicated by @regoff. If allowed, obtain such overridden segment
225 * register index. Lastly, if not prefixes were found or cannot be used, resolve
226 * the segment register index to use based on the defaults described in the
227 * Intel documentation. In long mode, all segment register indexes will be
228 * ignored, except if overrides were found for FS or GS. All these operations
229 * are done using helper functions.
230 *
231 * The operand register, @regoff, is represented as the offset from the base of
232 * pt_regs.
233 *
234 * As stated, the main use of this function is to determine the segment register
235 * index based on the instruction, its operands and prefixes. Hence, @insn
236 * must be valid. However, if @regoff indicates rIP, we don't need to inspect
237 * @insn at all as in this case CS is used in all cases. This case is checked
238 * before proceeding further.
239 *
240 * Please note that this function does not return the value in the segment
241 * register (i.e., the segment selector) but our defined index. The segment
242 * selector needs to be obtained using get_segment_selector() and passing the
243 * segment register index resolved by this function.
244 *
245 * Returns:
246 *
247 * An index identifying the segment register to use, among CS, SS, DS,
248 * ES, FS, or GS. INAT_SEG_REG_IGNORE is returned if running in long mode.
249 *
250 * -EINVAL in case of error.
251 */
252static int resolve_seg_reg(struct insn *insn, struct pt_regs *regs, int regoff)
253{
254 int idx;
255
256 /*
257 * In the unlikely event of having to resolve the segment register
258 * index for rIP, do it first. Segment override prefixes should not
259 * be used. Hence, it is not necessary to inspect the instruction,
260 * which may be invalid at this point.
261 */
262 if (regoff == offsetof(struct pt_regs, ip)) {
263 if (user_64bit_mode(regs))
264 return INAT_SEG_REG_IGNORE;
265 else
266 return INAT_SEG_REG_CS;
267 }
268
269 if (!insn)
270 return -EINVAL;
271
272 if (!check_seg_overrides(insn, regoff))
273 return resolve_default_seg(insn, regs, regoff);
274
275 idx = get_seg_reg_override_idx(insn);
276 if (idx < 0)
277 return idx;
278
279 if (idx == INAT_SEG_REG_DEFAULT)
280 return resolve_default_seg(insn, regs, regoff);
281
282 /*
283 * In long mode, segment override prefixes are ignored, except for
284 * overrides for FS and GS.
285 */
286 if (user_64bit_mode(regs)) {
287 if (idx != INAT_SEG_REG_FS &&
288 idx != INAT_SEG_REG_GS)
289 idx = INAT_SEG_REG_IGNORE;
290 }
291
292 return idx;
293}
294
295/**
296 * get_segment_selector() - obtain segment selector
297 * @regs: Register values as seen when entering kernel mode
298 * @seg_reg_idx: Segment register index to use
299 *
300 * Obtain the segment selector from any of the CS, SS, DS, ES, FS, GS segment
301 * registers. In CONFIG_X86_32, the segment is obtained from either pt_regs or
302 * kernel_vm86_regs as applicable. In CONFIG_X86_64, CS and SS are obtained
303 * from pt_regs. DS, ES, FS and GS are obtained by reading the actual CPU
304 * registers. This done for only for completeness as in CONFIG_X86_64 segment
305 * registers are ignored.
306 *
307 * Returns:
308 *
309 * Value of the segment selector, including null when running in
310 * long mode.
311 *
312 * -EINVAL on error.
313 */
314static short get_segment_selector(struct pt_regs *regs, int seg_reg_idx)
315{
316#ifdef CONFIG_X86_64
317 unsigned short sel;
318
319 switch (seg_reg_idx) {
320 case INAT_SEG_REG_IGNORE:
321 return 0;
322 case INAT_SEG_REG_CS:
323 return (unsigned short)(regs->cs & 0xffff);
324 case INAT_SEG_REG_SS:
325 return (unsigned short)(regs->ss & 0xffff);
326 case INAT_SEG_REG_DS:
327 savesegment(ds, sel);
328 return sel;
329 case INAT_SEG_REG_ES:
330 savesegment(es, sel);
331 return sel;
332 case INAT_SEG_REG_FS:
333 savesegment(fs, sel);
334 return sel;
335 case INAT_SEG_REG_GS:
336 savesegment(gs, sel);
337 return sel;
338 default:
339 return -EINVAL;
340 }
341#else /* CONFIG_X86_32 */
342 struct kernel_vm86_regs *vm86regs = (struct kernel_vm86_regs *)regs;
343
344 if (v8086_mode(regs)) {
345 switch (seg_reg_idx) {
346 case INAT_SEG_REG_CS:
347 return (unsigned short)(regs->cs & 0xffff);
348 case INAT_SEG_REG_SS:
349 return (unsigned short)(regs->ss & 0xffff);
350 case INAT_SEG_REG_DS:
351 return vm86regs->ds;
352 case INAT_SEG_REG_ES:
353 return vm86regs->es;
354 case INAT_SEG_REG_FS:
355 return vm86regs->fs;
356 case INAT_SEG_REG_GS:
357 return vm86regs->gs;
358 case INAT_SEG_REG_IGNORE:
359 /* fall through */
360 default:
361 return -EINVAL;
362 }
363 }
364
365 switch (seg_reg_idx) {
366 case INAT_SEG_REG_CS:
367 return (unsigned short)(regs->cs & 0xffff);
368 case INAT_SEG_REG_SS:
369 return (unsigned short)(regs->ss & 0xffff);
370 case INAT_SEG_REG_DS:
371 return (unsigned short)(regs->ds & 0xffff);
372 case INAT_SEG_REG_ES:
373 return (unsigned short)(regs->es & 0xffff);
374 case INAT_SEG_REG_FS:
375 return (unsigned short)(regs->fs & 0xffff);
376 case INAT_SEG_REG_GS:
377 /*
378 * GS may or may not be in regs as per CONFIG_X86_32_LAZY_GS.
379 * The macro below takes care of both cases.
380 */
381 return get_user_gs(regs);
382 case INAT_SEG_REG_IGNORE:
383 /* fall through */
384 default:
385 return -EINVAL;
386 }
387#endif /* CONFIG_X86_64 */
388}
389
Ricardo Neri32542ee2017-10-27 13:25:36 -0700390static int get_reg_offset(struct insn *insn, struct pt_regs *regs,
391 enum reg_type type)
392{
393 int regno = 0;
394
395 static const int regoff[] = {
396 offsetof(struct pt_regs, ax),
397 offsetof(struct pt_regs, cx),
398 offsetof(struct pt_regs, dx),
399 offsetof(struct pt_regs, bx),
400 offsetof(struct pt_regs, sp),
401 offsetof(struct pt_regs, bp),
402 offsetof(struct pt_regs, si),
403 offsetof(struct pt_regs, di),
404#ifdef CONFIG_X86_64
405 offsetof(struct pt_regs, r8),
406 offsetof(struct pt_regs, r9),
407 offsetof(struct pt_regs, r10),
408 offsetof(struct pt_regs, r11),
409 offsetof(struct pt_regs, r12),
410 offsetof(struct pt_regs, r13),
411 offsetof(struct pt_regs, r14),
412 offsetof(struct pt_regs, r15),
413#endif
414 };
415 int nr_registers = ARRAY_SIZE(regoff);
416 /*
417 * Don't possibly decode a 32-bit instructions as
418 * reading a 64-bit-only register.
419 */
420 if (IS_ENABLED(CONFIG_X86_64) && !insn->x86_64)
421 nr_registers -= 8;
422
423 switch (type) {
424 case REG_TYPE_RM:
425 regno = X86_MODRM_RM(insn->modrm.value);
426 if (X86_REX_B(insn->rex_prefix.value))
427 regno += 8;
428 break;
429
430 case REG_TYPE_INDEX:
431 regno = X86_SIB_INDEX(insn->sib.value);
432 if (X86_REX_X(insn->rex_prefix.value))
433 regno += 8;
434
435 /*
436 * If ModRM.mod != 3 and SIB.index = 4 the scale*index
437 * portion of the address computation is null. This is
438 * true only if REX.X is 0. In such a case, the SIB index
439 * is used in the address computation.
440 */
441 if (X86_MODRM_MOD(insn->modrm.value) != 3 && regno == 4)
442 return -EDOM;
443 break;
444
445 case REG_TYPE_BASE:
446 regno = X86_SIB_BASE(insn->sib.value);
447 /*
448 * If ModRM.mod is 0 and SIB.base == 5, the base of the
449 * register-indirect addressing is 0. In this case, a
450 * 32-bit displacement follows the SIB byte.
451 */
452 if (!X86_MODRM_MOD(insn->modrm.value) && regno == 5)
453 return -EDOM;
454
455 if (X86_REX_B(insn->rex_prefix.value))
456 regno += 8;
457 break;
458
459 default:
Ricardo Neried594e42017-10-27 13:25:37 -0700460 pr_err_ratelimited("invalid register type: %d\n", type);
461 return -EINVAL;
Ricardo Neri32542ee2017-10-27 13:25:36 -0700462 }
463
464 if (regno >= nr_registers) {
465 WARN_ONCE(1, "decoded an instruction with an invalid register");
466 return -EINVAL;
467 }
468 return regoff[regno];
469}
470
Ricardo Nerie5e45f12017-10-27 13:25:38 -0700471/**
472 * insn_get_modrm_rm_off() - Obtain register in r/m part of the ModRM byte
473 * @insn: Instruction containing the ModRM byte
474 * @regs: Register values as seen when entering kernel mode
475 *
476 * Returns:
477 *
478 * The register indicated by the r/m part of the ModRM byte. The
479 * register is obtained as an offset from the base of pt_regs. In specific
480 * cases, the returned value can be -EDOM to indicate that the particular value
481 * of ModRM does not refer to a register and shall be ignored.
482 */
483int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs)
484{
485 return get_reg_offset(insn, regs, REG_TYPE_RM);
486}
487
Ricardo Neri32542ee2017-10-27 13:25:36 -0700488/*
489 * return the address being referenced be instruction
490 * for rm=3 returning the content of the rm reg
491 * for rm!=3 calculates the address using SIB and Disp
492 */
493void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs)
494{
495 int addr_offset, base_offset, indx_offset;
496 unsigned long linear_addr = -1L;
497 long eff_addr, base, indx;
498 insn_byte_t sib;
499
500 insn_get_modrm(insn);
501 insn_get_sib(insn);
502 sib = insn->sib.value;
503
504 if (X86_MODRM_MOD(insn->modrm.value) == 3) {
505 addr_offset = get_reg_offset(insn, regs, REG_TYPE_RM);
506 if (addr_offset < 0)
507 goto out;
508
509 eff_addr = regs_get_register(regs, addr_offset);
510 } else {
511 if (insn->sib.nbytes) {
512 /*
513 * Negative values in the base and index offset means
514 * an error when decoding the SIB byte. Except -EDOM,
515 * which means that the registers should not be used
516 * in the address computation.
517 */
518 base_offset = get_reg_offset(insn, regs, REG_TYPE_BASE);
519 if (base_offset == -EDOM)
520 base = 0;
521 else if (base_offset < 0)
522 goto out;
523 else
524 base = regs_get_register(regs, base_offset);
525
526 indx_offset = get_reg_offset(insn, regs, REG_TYPE_INDEX);
527
528 if (indx_offset == -EDOM)
529 indx = 0;
530 else if (indx_offset < 0)
531 goto out;
532 else
533 indx = regs_get_register(regs, indx_offset);
534
535 eff_addr = base + indx * (1 << X86_SIB_SCALE(sib));
536 } else {
537 addr_offset = get_reg_offset(insn, regs, REG_TYPE_RM);
538 if (addr_offset < 0)
539 goto out;
540
541 eff_addr = regs_get_register(regs, addr_offset);
542 }
543
544 eff_addr += insn->displacement.value;
545 }
546
547 linear_addr = (unsigned long)eff_addr;
548
549out:
550 return (void __user *)linear_addr;
551}