1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Utility functions for x86 operand and address decoding
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) Intel Corporation 2017
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun #include <linux/kernel.h>
7*4882a593Smuzhiyun #include <linux/string.h>
8*4882a593Smuzhiyun #include <linux/ratelimit.h>
9*4882a593Smuzhiyun #include <linux/mmu_context.h>
10*4882a593Smuzhiyun #include <asm/desc_defs.h>
11*4882a593Smuzhiyun #include <asm/desc.h>
12*4882a593Smuzhiyun #include <asm/inat.h>
13*4882a593Smuzhiyun #include <asm/insn.h>
14*4882a593Smuzhiyun #include <asm/insn-eval.h>
15*4882a593Smuzhiyun #include <asm/ldt.h>
16*4882a593Smuzhiyun #include <asm/vm86.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #undef pr_fmt
19*4882a593Smuzhiyun #define pr_fmt(fmt) "insn: " fmt
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun enum reg_type {
22*4882a593Smuzhiyun REG_TYPE_RM = 0,
23*4882a593Smuzhiyun REG_TYPE_REG,
24*4882a593Smuzhiyun REG_TYPE_INDEX,
25*4882a593Smuzhiyun REG_TYPE_BASE,
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /**
29*4882a593Smuzhiyun * is_string_insn() - Determine if instruction is a string instruction
30*4882a593Smuzhiyun * @insn: Instruction containing the opcode to inspect
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * Returns:
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * true if the instruction, determined by the opcode, is any of the
35*4882a593Smuzhiyun * string instructions as defined in the Intel Software Development manual.
36*4882a593Smuzhiyun * False otherwise.
37*4882a593Smuzhiyun */
is_string_insn(struct insn * insn)38*4882a593Smuzhiyun static bool is_string_insn(struct insn *insn)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun insn_get_opcode(insn);
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* All string instructions have a 1-byte opcode. */
43*4882a593Smuzhiyun if (insn->opcode.nbytes != 1)
44*4882a593Smuzhiyun return false;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun switch (insn->opcode.bytes[0]) {
47*4882a593Smuzhiyun case 0x6c ... 0x6f: /* INS, OUTS */
48*4882a593Smuzhiyun case 0xa4 ... 0xa7: /* MOVS, CMPS */
49*4882a593Smuzhiyun case 0xaa ... 0xaf: /* STOS, LODS, SCAS */
50*4882a593Smuzhiyun return true;
51*4882a593Smuzhiyun default:
52*4882a593Smuzhiyun return false;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /**
57*4882a593Smuzhiyun * insn_has_rep_prefix() - Determine if instruction has a REP prefix
58*4882a593Smuzhiyun * @insn: Instruction containing the prefix to inspect
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * Returns:
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * true if the instruction has a REP prefix, false if not.
63*4882a593Smuzhiyun */
insn_has_rep_prefix(struct insn * insn)64*4882a593Smuzhiyun bool insn_has_rep_prefix(struct insn *insn)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun insn_byte_t p;
67*4882a593Smuzhiyun int i;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun insn_get_prefixes(insn);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun for_each_insn_prefix(insn, i, p) {
72*4882a593Smuzhiyun if (p == 0xf2 || p == 0xf3)
73*4882a593Smuzhiyun return true;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun return false;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /**
80*4882a593Smuzhiyun * get_seg_reg_override_idx() - obtain segment register override index
81*4882a593Smuzhiyun * @insn: Valid instruction with segment override prefixes
82*4882a593Smuzhiyun *
83*4882a593Smuzhiyun * Inspect the instruction prefixes in @insn and find segment overrides, if any.
84*4882a593Smuzhiyun *
85*4882a593Smuzhiyun * Returns:
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * A constant identifying the segment register to use, among CS, SS, DS,
88*4882a593Smuzhiyun * ES, FS, or GS. INAT_SEG_REG_DEFAULT is returned if no segment override
89*4882a593Smuzhiyun * prefixes were found.
90*4882a593Smuzhiyun *
91*4882a593Smuzhiyun * -EINVAL in case of error.
92*4882a593Smuzhiyun */
get_seg_reg_override_idx(struct insn * insn)93*4882a593Smuzhiyun static int get_seg_reg_override_idx(struct insn *insn)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun int idx = INAT_SEG_REG_DEFAULT;
96*4882a593Smuzhiyun int num_overrides = 0, i;
97*4882a593Smuzhiyun insn_byte_t p;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun insn_get_prefixes(insn);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /* Look for any segment override prefixes. */
102*4882a593Smuzhiyun for_each_insn_prefix(insn, i, p) {
103*4882a593Smuzhiyun insn_attr_t attr;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun attr = inat_get_opcode_attribute(p);
106*4882a593Smuzhiyun switch (attr) {
107*4882a593Smuzhiyun case INAT_MAKE_PREFIX(INAT_PFX_CS):
108*4882a593Smuzhiyun idx = INAT_SEG_REG_CS;
109*4882a593Smuzhiyun num_overrides++;
110*4882a593Smuzhiyun break;
111*4882a593Smuzhiyun case INAT_MAKE_PREFIX(INAT_PFX_SS):
112*4882a593Smuzhiyun idx = INAT_SEG_REG_SS;
113*4882a593Smuzhiyun num_overrides++;
114*4882a593Smuzhiyun break;
115*4882a593Smuzhiyun case INAT_MAKE_PREFIX(INAT_PFX_DS):
116*4882a593Smuzhiyun idx = INAT_SEG_REG_DS;
117*4882a593Smuzhiyun num_overrides++;
118*4882a593Smuzhiyun break;
119*4882a593Smuzhiyun case INAT_MAKE_PREFIX(INAT_PFX_ES):
120*4882a593Smuzhiyun idx = INAT_SEG_REG_ES;
121*4882a593Smuzhiyun num_overrides++;
122*4882a593Smuzhiyun break;
123*4882a593Smuzhiyun case INAT_MAKE_PREFIX(INAT_PFX_FS):
124*4882a593Smuzhiyun idx = INAT_SEG_REG_FS;
125*4882a593Smuzhiyun num_overrides++;
126*4882a593Smuzhiyun break;
127*4882a593Smuzhiyun case INAT_MAKE_PREFIX(INAT_PFX_GS):
128*4882a593Smuzhiyun idx = INAT_SEG_REG_GS;
129*4882a593Smuzhiyun num_overrides++;
130*4882a593Smuzhiyun break;
131*4882a593Smuzhiyun /* No default action needed. */
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /* More than one segment override prefix leads to undefined behavior. */
136*4882a593Smuzhiyun if (num_overrides > 1)
137*4882a593Smuzhiyun return -EINVAL;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun return idx;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /**
143*4882a593Smuzhiyun * check_seg_overrides() - check if segment override prefixes are allowed
144*4882a593Smuzhiyun * @insn: Valid instruction with segment override prefixes
145*4882a593Smuzhiyun * @regoff: Operand offset, in pt_regs, for which the check is performed
146*4882a593Smuzhiyun *
147*4882a593Smuzhiyun * For a particular register used in register-indirect addressing, determine if
148*4882a593Smuzhiyun * segment override prefixes can be used. Specifically, no overrides are allowed
149*4882a593Smuzhiyun * for rDI if used with a string instruction.
150*4882a593Smuzhiyun *
151*4882a593Smuzhiyun * Returns:
152*4882a593Smuzhiyun *
153*4882a593Smuzhiyun * True if segment override prefixes can be used with the register indicated
154*4882a593Smuzhiyun * in @regoff. False if otherwise.
155*4882a593Smuzhiyun */
check_seg_overrides(struct insn * insn,int regoff)156*4882a593Smuzhiyun static bool check_seg_overrides(struct insn *insn, int regoff)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun if (regoff == offsetof(struct pt_regs, di) && is_string_insn(insn))
159*4882a593Smuzhiyun return false;
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun return true;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /**
165*4882a593Smuzhiyun * resolve_default_seg() - resolve default segment register index for an operand
166*4882a593Smuzhiyun * @insn: Instruction with opcode and address size. Must be valid.
167*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
168*4882a593Smuzhiyun * @off: Operand offset, in pt_regs, for which resolution is needed
169*4882a593Smuzhiyun *
170*4882a593Smuzhiyun * Resolve the default segment register index associated with the instruction
171*4882a593Smuzhiyun * operand register indicated by @off. Such index is resolved based on defaults
172*4882a593Smuzhiyun * described in the Intel Software Development Manual.
173*4882a593Smuzhiyun *
174*4882a593Smuzhiyun * Returns:
175*4882a593Smuzhiyun *
176*4882a593Smuzhiyun * If in protected mode, a constant identifying the segment register to use,
177*4882a593Smuzhiyun * among CS, SS, ES or DS. If in long mode, INAT_SEG_REG_IGNORE.
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * -EINVAL in case of error.
180*4882a593Smuzhiyun */
resolve_default_seg(struct insn * insn,struct pt_regs * regs,int off)181*4882a593Smuzhiyun static int resolve_default_seg(struct insn *insn, struct pt_regs *regs, int off)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun if (any_64bit_mode(regs))
184*4882a593Smuzhiyun return INAT_SEG_REG_IGNORE;
185*4882a593Smuzhiyun /*
186*4882a593Smuzhiyun * Resolve the default segment register as described in Section 3.7.4
187*4882a593Smuzhiyun * of the Intel Software Development Manual Vol. 1:
188*4882a593Smuzhiyun *
189*4882a593Smuzhiyun * + DS for all references involving r[ABCD]X, and rSI.
190*4882a593Smuzhiyun * + If used in a string instruction, ES for rDI. Otherwise, DS.
191*4882a593Smuzhiyun * + AX, CX and DX are not valid register operands in 16-bit address
192*4882a593Smuzhiyun * encodings but are valid for 32-bit and 64-bit encodings.
193*4882a593Smuzhiyun * + -EDOM is reserved to identify for cases in which no register
194*4882a593Smuzhiyun * is used (i.e., displacement-only addressing). Use DS.
195*4882a593Smuzhiyun * + SS for rSP or rBP.
196*4882a593Smuzhiyun * + CS for rIP.
197*4882a593Smuzhiyun */
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun switch (off) {
200*4882a593Smuzhiyun case offsetof(struct pt_regs, ax):
201*4882a593Smuzhiyun case offsetof(struct pt_regs, cx):
202*4882a593Smuzhiyun case offsetof(struct pt_regs, dx):
203*4882a593Smuzhiyun /* Need insn to verify address size. */
204*4882a593Smuzhiyun if (insn->addr_bytes == 2)
205*4882a593Smuzhiyun return -EINVAL;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun fallthrough;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun case -EDOM:
210*4882a593Smuzhiyun case offsetof(struct pt_regs, bx):
211*4882a593Smuzhiyun case offsetof(struct pt_regs, si):
212*4882a593Smuzhiyun return INAT_SEG_REG_DS;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun case offsetof(struct pt_regs, di):
215*4882a593Smuzhiyun if (is_string_insn(insn))
216*4882a593Smuzhiyun return INAT_SEG_REG_ES;
217*4882a593Smuzhiyun return INAT_SEG_REG_DS;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun case offsetof(struct pt_regs, bp):
220*4882a593Smuzhiyun case offsetof(struct pt_regs, sp):
221*4882a593Smuzhiyun return INAT_SEG_REG_SS;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun case offsetof(struct pt_regs, ip):
224*4882a593Smuzhiyun return INAT_SEG_REG_CS;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun default:
227*4882a593Smuzhiyun return -EINVAL;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun * resolve_seg_reg() - obtain segment register index
233*4882a593Smuzhiyun * @insn: Instruction with operands
234*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
235*4882a593Smuzhiyun * @regoff: Operand offset, in pt_regs, used to deterimine segment register
236*4882a593Smuzhiyun *
237*4882a593Smuzhiyun * Determine the segment register associated with the operands and, if
238*4882a593Smuzhiyun * applicable, prefixes and the instruction pointed by @insn.
239*4882a593Smuzhiyun *
240*4882a593Smuzhiyun * The segment register associated to an operand used in register-indirect
241*4882a593Smuzhiyun * addressing depends on:
242*4882a593Smuzhiyun *
243*4882a593Smuzhiyun * a) Whether running in long mode (in such a case segments are ignored, except
244*4882a593Smuzhiyun * if FS or GS are used).
245*4882a593Smuzhiyun *
246*4882a593Smuzhiyun * b) Whether segment override prefixes can be used. Certain instructions and
247*4882a593Smuzhiyun * registers do not allow override prefixes.
248*4882a593Smuzhiyun *
249*4882a593Smuzhiyun * c) Whether segment overrides prefixes are found in the instruction prefixes.
250*4882a593Smuzhiyun *
251*4882a593Smuzhiyun * d) If there are not segment override prefixes or they cannot be used, the
252*4882a593Smuzhiyun * default segment register associated with the operand register is used.
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * The function checks first if segment override prefixes can be used with the
255*4882a593Smuzhiyun * operand indicated by @regoff. If allowed, obtain such overridden segment
256*4882a593Smuzhiyun * register index. Lastly, if not prefixes were found or cannot be used, resolve
257*4882a593Smuzhiyun * the segment register index to use based on the defaults described in the
258*4882a593Smuzhiyun * Intel documentation. In long mode, all segment register indexes will be
259*4882a593Smuzhiyun * ignored, except if overrides were found for FS or GS. All these operations
260*4882a593Smuzhiyun * are done using helper functions.
261*4882a593Smuzhiyun *
262*4882a593Smuzhiyun * The operand register, @regoff, is represented as the offset from the base of
263*4882a593Smuzhiyun * pt_regs.
264*4882a593Smuzhiyun *
265*4882a593Smuzhiyun * As stated, the main use of this function is to determine the segment register
266*4882a593Smuzhiyun * index based on the instruction, its operands and prefixes. Hence, @insn
267*4882a593Smuzhiyun * must be valid. However, if @regoff indicates rIP, we don't need to inspect
268*4882a593Smuzhiyun * @insn at all as in this case CS is used in all cases. This case is checked
269*4882a593Smuzhiyun * before proceeding further.
270*4882a593Smuzhiyun *
271*4882a593Smuzhiyun * Please note that this function does not return the value in the segment
272*4882a593Smuzhiyun * register (i.e., the segment selector) but our defined index. The segment
273*4882a593Smuzhiyun * selector needs to be obtained using get_segment_selector() and passing the
274*4882a593Smuzhiyun * segment register index resolved by this function.
275*4882a593Smuzhiyun *
276*4882a593Smuzhiyun * Returns:
277*4882a593Smuzhiyun *
278*4882a593Smuzhiyun * An index identifying the segment register to use, among CS, SS, DS,
279*4882a593Smuzhiyun * ES, FS, or GS. INAT_SEG_REG_IGNORE is returned if running in long mode.
280*4882a593Smuzhiyun *
281*4882a593Smuzhiyun * -EINVAL in case of error.
282*4882a593Smuzhiyun */
resolve_seg_reg(struct insn * insn,struct pt_regs * regs,int regoff)283*4882a593Smuzhiyun static int resolve_seg_reg(struct insn *insn, struct pt_regs *regs, int regoff)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun int idx;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /*
288*4882a593Smuzhiyun * In the unlikely event of having to resolve the segment register
289*4882a593Smuzhiyun * index for rIP, do it first. Segment override prefixes should not
290*4882a593Smuzhiyun * be used. Hence, it is not necessary to inspect the instruction,
291*4882a593Smuzhiyun * which may be invalid at this point.
292*4882a593Smuzhiyun */
293*4882a593Smuzhiyun if (regoff == offsetof(struct pt_regs, ip)) {
294*4882a593Smuzhiyun if (any_64bit_mode(regs))
295*4882a593Smuzhiyun return INAT_SEG_REG_IGNORE;
296*4882a593Smuzhiyun else
297*4882a593Smuzhiyun return INAT_SEG_REG_CS;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun if (!insn)
301*4882a593Smuzhiyun return -EINVAL;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun if (!check_seg_overrides(insn, regoff))
304*4882a593Smuzhiyun return resolve_default_seg(insn, regs, regoff);
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun idx = get_seg_reg_override_idx(insn);
307*4882a593Smuzhiyun if (idx < 0)
308*4882a593Smuzhiyun return idx;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun if (idx == INAT_SEG_REG_DEFAULT)
311*4882a593Smuzhiyun return resolve_default_seg(insn, regs, regoff);
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun /*
314*4882a593Smuzhiyun * In long mode, segment override prefixes are ignored, except for
315*4882a593Smuzhiyun * overrides for FS and GS.
316*4882a593Smuzhiyun */
317*4882a593Smuzhiyun if (any_64bit_mode(regs)) {
318*4882a593Smuzhiyun if (idx != INAT_SEG_REG_FS &&
319*4882a593Smuzhiyun idx != INAT_SEG_REG_GS)
320*4882a593Smuzhiyun idx = INAT_SEG_REG_IGNORE;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun return idx;
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun /**
327*4882a593Smuzhiyun * get_segment_selector() - obtain segment selector
328*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
329*4882a593Smuzhiyun * @seg_reg_idx: Segment register index to use
330*4882a593Smuzhiyun *
331*4882a593Smuzhiyun * Obtain the segment selector from any of the CS, SS, DS, ES, FS, GS segment
332*4882a593Smuzhiyun * registers. In CONFIG_X86_32, the segment is obtained from either pt_regs or
333*4882a593Smuzhiyun * kernel_vm86_regs as applicable. In CONFIG_X86_64, CS and SS are obtained
334*4882a593Smuzhiyun * from pt_regs. DS, ES, FS and GS are obtained by reading the actual CPU
335*4882a593Smuzhiyun * registers. This done for only for completeness as in CONFIG_X86_64 segment
336*4882a593Smuzhiyun * registers are ignored.
337*4882a593Smuzhiyun *
338*4882a593Smuzhiyun * Returns:
339*4882a593Smuzhiyun *
340*4882a593Smuzhiyun * Value of the segment selector, including null when running in
341*4882a593Smuzhiyun * long mode.
342*4882a593Smuzhiyun *
343*4882a593Smuzhiyun * -EINVAL on error.
344*4882a593Smuzhiyun */
get_segment_selector(struct pt_regs * regs,int seg_reg_idx)345*4882a593Smuzhiyun static short get_segment_selector(struct pt_regs *regs, int seg_reg_idx)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun #ifdef CONFIG_X86_64
348*4882a593Smuzhiyun unsigned short sel;
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun switch (seg_reg_idx) {
351*4882a593Smuzhiyun case INAT_SEG_REG_IGNORE:
352*4882a593Smuzhiyun return 0;
353*4882a593Smuzhiyun case INAT_SEG_REG_CS:
354*4882a593Smuzhiyun return (unsigned short)(regs->cs & 0xffff);
355*4882a593Smuzhiyun case INAT_SEG_REG_SS:
356*4882a593Smuzhiyun return (unsigned short)(regs->ss & 0xffff);
357*4882a593Smuzhiyun case INAT_SEG_REG_DS:
358*4882a593Smuzhiyun savesegment(ds, sel);
359*4882a593Smuzhiyun return sel;
360*4882a593Smuzhiyun case INAT_SEG_REG_ES:
361*4882a593Smuzhiyun savesegment(es, sel);
362*4882a593Smuzhiyun return sel;
363*4882a593Smuzhiyun case INAT_SEG_REG_FS:
364*4882a593Smuzhiyun savesegment(fs, sel);
365*4882a593Smuzhiyun return sel;
366*4882a593Smuzhiyun case INAT_SEG_REG_GS:
367*4882a593Smuzhiyun savesegment(gs, sel);
368*4882a593Smuzhiyun return sel;
369*4882a593Smuzhiyun default:
370*4882a593Smuzhiyun return -EINVAL;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun #else /* CONFIG_X86_32 */
373*4882a593Smuzhiyun struct kernel_vm86_regs *vm86regs = (struct kernel_vm86_regs *)regs;
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun if (v8086_mode(regs)) {
376*4882a593Smuzhiyun switch (seg_reg_idx) {
377*4882a593Smuzhiyun case INAT_SEG_REG_CS:
378*4882a593Smuzhiyun return (unsigned short)(regs->cs & 0xffff);
379*4882a593Smuzhiyun case INAT_SEG_REG_SS:
380*4882a593Smuzhiyun return (unsigned short)(regs->ss & 0xffff);
381*4882a593Smuzhiyun case INAT_SEG_REG_DS:
382*4882a593Smuzhiyun return vm86regs->ds;
383*4882a593Smuzhiyun case INAT_SEG_REG_ES:
384*4882a593Smuzhiyun return vm86regs->es;
385*4882a593Smuzhiyun case INAT_SEG_REG_FS:
386*4882a593Smuzhiyun return vm86regs->fs;
387*4882a593Smuzhiyun case INAT_SEG_REG_GS:
388*4882a593Smuzhiyun return vm86regs->gs;
389*4882a593Smuzhiyun case INAT_SEG_REG_IGNORE:
390*4882a593Smuzhiyun default:
391*4882a593Smuzhiyun return -EINVAL;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun switch (seg_reg_idx) {
396*4882a593Smuzhiyun case INAT_SEG_REG_CS:
397*4882a593Smuzhiyun return (unsigned short)(regs->cs & 0xffff);
398*4882a593Smuzhiyun case INAT_SEG_REG_SS:
399*4882a593Smuzhiyun return (unsigned short)(regs->ss & 0xffff);
400*4882a593Smuzhiyun case INAT_SEG_REG_DS:
401*4882a593Smuzhiyun return (unsigned short)(regs->ds & 0xffff);
402*4882a593Smuzhiyun case INAT_SEG_REG_ES:
403*4882a593Smuzhiyun return (unsigned short)(regs->es & 0xffff);
404*4882a593Smuzhiyun case INAT_SEG_REG_FS:
405*4882a593Smuzhiyun return (unsigned short)(regs->fs & 0xffff);
406*4882a593Smuzhiyun case INAT_SEG_REG_GS:
407*4882a593Smuzhiyun /*
408*4882a593Smuzhiyun * GS may or may not be in regs as per CONFIG_X86_32_LAZY_GS.
409*4882a593Smuzhiyun * The macro below takes care of both cases.
410*4882a593Smuzhiyun */
411*4882a593Smuzhiyun return get_user_gs(regs);
412*4882a593Smuzhiyun case INAT_SEG_REG_IGNORE:
413*4882a593Smuzhiyun default:
414*4882a593Smuzhiyun return -EINVAL;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun #endif /* CONFIG_X86_64 */
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
get_reg_offset(struct insn * insn,struct pt_regs * regs,enum reg_type type)419*4882a593Smuzhiyun static int get_reg_offset(struct insn *insn, struct pt_regs *regs,
420*4882a593Smuzhiyun enum reg_type type)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun int regno = 0;
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun static const int regoff[] = {
425*4882a593Smuzhiyun offsetof(struct pt_regs, ax),
426*4882a593Smuzhiyun offsetof(struct pt_regs, cx),
427*4882a593Smuzhiyun offsetof(struct pt_regs, dx),
428*4882a593Smuzhiyun offsetof(struct pt_regs, bx),
429*4882a593Smuzhiyun offsetof(struct pt_regs, sp),
430*4882a593Smuzhiyun offsetof(struct pt_regs, bp),
431*4882a593Smuzhiyun offsetof(struct pt_regs, si),
432*4882a593Smuzhiyun offsetof(struct pt_regs, di),
433*4882a593Smuzhiyun #ifdef CONFIG_X86_64
434*4882a593Smuzhiyun offsetof(struct pt_regs, r8),
435*4882a593Smuzhiyun offsetof(struct pt_regs, r9),
436*4882a593Smuzhiyun offsetof(struct pt_regs, r10),
437*4882a593Smuzhiyun offsetof(struct pt_regs, r11),
438*4882a593Smuzhiyun offsetof(struct pt_regs, r12),
439*4882a593Smuzhiyun offsetof(struct pt_regs, r13),
440*4882a593Smuzhiyun offsetof(struct pt_regs, r14),
441*4882a593Smuzhiyun offsetof(struct pt_regs, r15),
442*4882a593Smuzhiyun #endif
443*4882a593Smuzhiyun };
444*4882a593Smuzhiyun int nr_registers = ARRAY_SIZE(regoff);
445*4882a593Smuzhiyun /*
446*4882a593Smuzhiyun * Don't possibly decode a 32-bit instructions as
447*4882a593Smuzhiyun * reading a 64-bit-only register.
448*4882a593Smuzhiyun */
449*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_X86_64) && !insn->x86_64)
450*4882a593Smuzhiyun nr_registers -= 8;
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun switch (type) {
453*4882a593Smuzhiyun case REG_TYPE_RM:
454*4882a593Smuzhiyun regno = X86_MODRM_RM(insn->modrm.value);
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun /*
457*4882a593Smuzhiyun * ModRM.mod == 0 and ModRM.rm == 5 means a 32-bit displacement
458*4882a593Smuzhiyun * follows the ModRM byte.
459*4882a593Smuzhiyun */
460*4882a593Smuzhiyun if (!X86_MODRM_MOD(insn->modrm.value) && regno == 5)
461*4882a593Smuzhiyun return -EDOM;
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun if (X86_REX_B(insn->rex_prefix.value))
464*4882a593Smuzhiyun regno += 8;
465*4882a593Smuzhiyun break;
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun case REG_TYPE_REG:
468*4882a593Smuzhiyun regno = X86_MODRM_REG(insn->modrm.value);
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun if (X86_REX_R(insn->rex_prefix.value))
471*4882a593Smuzhiyun regno += 8;
472*4882a593Smuzhiyun break;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun case REG_TYPE_INDEX:
475*4882a593Smuzhiyun regno = X86_SIB_INDEX(insn->sib.value);
476*4882a593Smuzhiyun if (X86_REX_X(insn->rex_prefix.value))
477*4882a593Smuzhiyun regno += 8;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun /*
480*4882a593Smuzhiyun * If ModRM.mod != 3 and SIB.index = 4 the scale*index
481*4882a593Smuzhiyun * portion of the address computation is null. This is
482*4882a593Smuzhiyun * true only if REX.X is 0. In such a case, the SIB index
483*4882a593Smuzhiyun * is used in the address computation.
484*4882a593Smuzhiyun */
485*4882a593Smuzhiyun if (X86_MODRM_MOD(insn->modrm.value) != 3 && regno == 4)
486*4882a593Smuzhiyun return -EDOM;
487*4882a593Smuzhiyun break;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun case REG_TYPE_BASE:
490*4882a593Smuzhiyun regno = X86_SIB_BASE(insn->sib.value);
491*4882a593Smuzhiyun /*
492*4882a593Smuzhiyun * If ModRM.mod is 0 and SIB.base == 5, the base of the
493*4882a593Smuzhiyun * register-indirect addressing is 0. In this case, a
494*4882a593Smuzhiyun * 32-bit displacement follows the SIB byte.
495*4882a593Smuzhiyun */
496*4882a593Smuzhiyun if (!X86_MODRM_MOD(insn->modrm.value) && regno == 5)
497*4882a593Smuzhiyun return -EDOM;
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun if (X86_REX_B(insn->rex_prefix.value))
500*4882a593Smuzhiyun regno += 8;
501*4882a593Smuzhiyun break;
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun default:
504*4882a593Smuzhiyun pr_err_ratelimited("invalid register type: %d\n", type);
505*4882a593Smuzhiyun return -EINVAL;
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun if (regno >= nr_registers) {
509*4882a593Smuzhiyun WARN_ONCE(1, "decoded an instruction with an invalid register");
510*4882a593Smuzhiyun return -EINVAL;
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun return regoff[regno];
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun /**
516*4882a593Smuzhiyun * get_reg_offset_16() - Obtain offset of register indicated by instruction
517*4882a593Smuzhiyun * @insn: Instruction containing ModRM byte
518*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
519*4882a593Smuzhiyun * @offs1: Offset of the first operand register
520*4882a593Smuzhiyun * @offs2: Offset of the second opeand register, if applicable
521*4882a593Smuzhiyun *
522*4882a593Smuzhiyun * Obtain the offset, in pt_regs, of the registers indicated by the ModRM byte
523*4882a593Smuzhiyun * in @insn. This function is to be used with 16-bit address encodings. The
524*4882a593Smuzhiyun * @offs1 and @offs2 will be written with the offset of the two registers
525*4882a593Smuzhiyun * indicated by the instruction. In cases where any of the registers is not
526*4882a593Smuzhiyun * referenced by the instruction, the value will be set to -EDOM.
527*4882a593Smuzhiyun *
528*4882a593Smuzhiyun * Returns:
529*4882a593Smuzhiyun *
530*4882a593Smuzhiyun * 0 on success, -EINVAL on error.
531*4882a593Smuzhiyun */
get_reg_offset_16(struct insn * insn,struct pt_regs * regs,int * offs1,int * offs2)532*4882a593Smuzhiyun static int get_reg_offset_16(struct insn *insn, struct pt_regs *regs,
533*4882a593Smuzhiyun int *offs1, int *offs2)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun /*
536*4882a593Smuzhiyun * 16-bit addressing can use one or two registers. Specifics of
537*4882a593Smuzhiyun * encodings are given in Table 2-1. "16-Bit Addressing Forms with the
538*4882a593Smuzhiyun * ModR/M Byte" of the Intel Software Development Manual.
539*4882a593Smuzhiyun */
540*4882a593Smuzhiyun static const int regoff1[] = {
541*4882a593Smuzhiyun offsetof(struct pt_regs, bx),
542*4882a593Smuzhiyun offsetof(struct pt_regs, bx),
543*4882a593Smuzhiyun offsetof(struct pt_regs, bp),
544*4882a593Smuzhiyun offsetof(struct pt_regs, bp),
545*4882a593Smuzhiyun offsetof(struct pt_regs, si),
546*4882a593Smuzhiyun offsetof(struct pt_regs, di),
547*4882a593Smuzhiyun offsetof(struct pt_regs, bp),
548*4882a593Smuzhiyun offsetof(struct pt_regs, bx),
549*4882a593Smuzhiyun };
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun static const int regoff2[] = {
552*4882a593Smuzhiyun offsetof(struct pt_regs, si),
553*4882a593Smuzhiyun offsetof(struct pt_regs, di),
554*4882a593Smuzhiyun offsetof(struct pt_regs, si),
555*4882a593Smuzhiyun offsetof(struct pt_regs, di),
556*4882a593Smuzhiyun -EDOM,
557*4882a593Smuzhiyun -EDOM,
558*4882a593Smuzhiyun -EDOM,
559*4882a593Smuzhiyun -EDOM,
560*4882a593Smuzhiyun };
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun if (!offs1 || !offs2)
563*4882a593Smuzhiyun return -EINVAL;
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun /* Operand is a register, use the generic function. */
566*4882a593Smuzhiyun if (X86_MODRM_MOD(insn->modrm.value) == 3) {
567*4882a593Smuzhiyun *offs1 = insn_get_modrm_rm_off(insn, regs);
568*4882a593Smuzhiyun *offs2 = -EDOM;
569*4882a593Smuzhiyun return 0;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun *offs1 = regoff1[X86_MODRM_RM(insn->modrm.value)];
573*4882a593Smuzhiyun *offs2 = regoff2[X86_MODRM_RM(insn->modrm.value)];
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun /*
576*4882a593Smuzhiyun * If ModRM.mod is 0 and ModRM.rm is 110b, then we use displacement-
577*4882a593Smuzhiyun * only addressing. This means that no registers are involved in
578*4882a593Smuzhiyun * computing the effective address. Thus, ensure that the first
579*4882a593Smuzhiyun * register offset is invalild. The second register offset is already
580*4882a593Smuzhiyun * invalid under the aforementioned conditions.
581*4882a593Smuzhiyun */
582*4882a593Smuzhiyun if ((X86_MODRM_MOD(insn->modrm.value) == 0) &&
583*4882a593Smuzhiyun (X86_MODRM_RM(insn->modrm.value) == 6))
584*4882a593Smuzhiyun *offs1 = -EDOM;
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun return 0;
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun /**
590*4882a593Smuzhiyun * get_desc() - Obtain contents of a segment descriptor
591*4882a593Smuzhiyun * @out: Segment descriptor contents on success
592*4882a593Smuzhiyun * @sel: Segment selector
593*4882a593Smuzhiyun *
594*4882a593Smuzhiyun * Given a segment selector, obtain a pointer to the segment descriptor.
595*4882a593Smuzhiyun * Both global and local descriptor tables are supported.
596*4882a593Smuzhiyun *
597*4882a593Smuzhiyun * Returns:
598*4882a593Smuzhiyun *
599*4882a593Smuzhiyun * True on success, false on failure.
600*4882a593Smuzhiyun *
601*4882a593Smuzhiyun * NULL on error.
602*4882a593Smuzhiyun */
get_desc(struct desc_struct * out,unsigned short sel)603*4882a593Smuzhiyun static bool get_desc(struct desc_struct *out, unsigned short sel)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun struct desc_ptr gdt_desc = {0, 0};
606*4882a593Smuzhiyun unsigned long desc_base;
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun #ifdef CONFIG_MODIFY_LDT_SYSCALL
609*4882a593Smuzhiyun if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT) {
610*4882a593Smuzhiyun bool success = false;
611*4882a593Smuzhiyun struct ldt_struct *ldt;
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun /* Bits [15:3] contain the index of the desired entry. */
614*4882a593Smuzhiyun sel >>= 3;
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun mutex_lock(¤t->active_mm->context.lock);
617*4882a593Smuzhiyun ldt = current->active_mm->context.ldt;
618*4882a593Smuzhiyun if (ldt && sel < ldt->nr_entries) {
619*4882a593Smuzhiyun *out = ldt->entries[sel];
620*4882a593Smuzhiyun success = true;
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun mutex_unlock(¤t->active_mm->context.lock);
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun return success;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun #endif
628*4882a593Smuzhiyun native_store_gdt(&gdt_desc);
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun /*
631*4882a593Smuzhiyun * Segment descriptors have a size of 8 bytes. Thus, the index is
632*4882a593Smuzhiyun * multiplied by 8 to obtain the memory offset of the desired descriptor
633*4882a593Smuzhiyun * from the base of the GDT. As bits [15:3] of the segment selector
634*4882a593Smuzhiyun * contain the index, it can be regarded as multiplied by 8 already.
635*4882a593Smuzhiyun * All that remains is to clear bits [2:0].
636*4882a593Smuzhiyun */
637*4882a593Smuzhiyun desc_base = sel & ~(SEGMENT_RPL_MASK | SEGMENT_TI_MASK);
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun if (desc_base > gdt_desc.size)
640*4882a593Smuzhiyun return false;
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun *out = *(struct desc_struct *)(gdt_desc.address + desc_base);
643*4882a593Smuzhiyun return true;
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun /**
647*4882a593Smuzhiyun * insn_get_seg_base() - Obtain base address of segment descriptor.
648*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
649*4882a593Smuzhiyun * @seg_reg_idx: Index of the segment register pointing to seg descriptor
650*4882a593Smuzhiyun *
651*4882a593Smuzhiyun * Obtain the base address of the segment as indicated by the segment descriptor
652*4882a593Smuzhiyun * pointed by the segment selector. The segment selector is obtained from the
653*4882a593Smuzhiyun * input segment register index @seg_reg_idx.
654*4882a593Smuzhiyun *
655*4882a593Smuzhiyun * Returns:
656*4882a593Smuzhiyun *
657*4882a593Smuzhiyun * In protected mode, base address of the segment. Zero in long mode,
658*4882a593Smuzhiyun * except when FS or GS are used. In virtual-8086 mode, the segment
659*4882a593Smuzhiyun * selector shifted 4 bits to the right.
660*4882a593Smuzhiyun *
661*4882a593Smuzhiyun * -1L in case of error.
662*4882a593Smuzhiyun */
insn_get_seg_base(struct pt_regs * regs,int seg_reg_idx)663*4882a593Smuzhiyun unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun struct desc_struct desc;
666*4882a593Smuzhiyun short sel;
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun sel = get_segment_selector(regs, seg_reg_idx);
669*4882a593Smuzhiyun if (sel < 0)
670*4882a593Smuzhiyun return -1L;
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun if (v8086_mode(regs))
673*4882a593Smuzhiyun /*
674*4882a593Smuzhiyun * Base is simply the segment selector shifted 4
675*4882a593Smuzhiyun * bits to the right.
676*4882a593Smuzhiyun */
677*4882a593Smuzhiyun return (unsigned long)(sel << 4);
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun if (any_64bit_mode(regs)) {
680*4882a593Smuzhiyun /*
681*4882a593Smuzhiyun * Only FS or GS will have a base address, the rest of
682*4882a593Smuzhiyun * the segments' bases are forced to 0.
683*4882a593Smuzhiyun */
684*4882a593Smuzhiyun unsigned long base;
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun if (seg_reg_idx == INAT_SEG_REG_FS) {
687*4882a593Smuzhiyun rdmsrl(MSR_FS_BASE, base);
688*4882a593Smuzhiyun } else if (seg_reg_idx == INAT_SEG_REG_GS) {
689*4882a593Smuzhiyun /*
690*4882a593Smuzhiyun * swapgs was called at the kernel entry point. Thus,
691*4882a593Smuzhiyun * MSR_KERNEL_GS_BASE will have the user-space GS base.
692*4882a593Smuzhiyun */
693*4882a593Smuzhiyun if (user_mode(regs))
694*4882a593Smuzhiyun rdmsrl(MSR_KERNEL_GS_BASE, base);
695*4882a593Smuzhiyun else
696*4882a593Smuzhiyun rdmsrl(MSR_GS_BASE, base);
697*4882a593Smuzhiyun } else {
698*4882a593Smuzhiyun base = 0;
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun return base;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun /* In protected mode the segment selector cannot be null. */
704*4882a593Smuzhiyun if (!sel)
705*4882a593Smuzhiyun return -1L;
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun if (!get_desc(&desc, sel))
708*4882a593Smuzhiyun return -1L;
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun return get_desc_base(&desc);
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun /**
714*4882a593Smuzhiyun * get_seg_limit() - Obtain the limit of a segment descriptor
715*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
716*4882a593Smuzhiyun * @seg_reg_idx: Index of the segment register pointing to seg descriptor
717*4882a593Smuzhiyun *
718*4882a593Smuzhiyun * Obtain the limit of the segment as indicated by the segment descriptor
719*4882a593Smuzhiyun * pointed by the segment selector. The segment selector is obtained from the
720*4882a593Smuzhiyun * input segment register index @seg_reg_idx.
721*4882a593Smuzhiyun *
722*4882a593Smuzhiyun * Returns:
723*4882a593Smuzhiyun *
724*4882a593Smuzhiyun * In protected mode, the limit of the segment descriptor in bytes.
725*4882a593Smuzhiyun * In long mode and virtual-8086 mode, segment limits are not enforced. Thus,
726*4882a593Smuzhiyun * limit is returned as -1L to imply a limit-less segment.
727*4882a593Smuzhiyun *
728*4882a593Smuzhiyun * Zero is returned on error.
729*4882a593Smuzhiyun */
get_seg_limit(struct pt_regs * regs,int seg_reg_idx)730*4882a593Smuzhiyun static unsigned long get_seg_limit(struct pt_regs *regs, int seg_reg_idx)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun struct desc_struct desc;
733*4882a593Smuzhiyun unsigned long limit;
734*4882a593Smuzhiyun short sel;
735*4882a593Smuzhiyun
736*4882a593Smuzhiyun sel = get_segment_selector(regs, seg_reg_idx);
737*4882a593Smuzhiyun if (sel < 0)
738*4882a593Smuzhiyun return 0;
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun if (any_64bit_mode(regs) || v8086_mode(regs))
741*4882a593Smuzhiyun return -1L;
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun if (!sel)
744*4882a593Smuzhiyun return 0;
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun if (!get_desc(&desc, sel))
747*4882a593Smuzhiyun return 0;
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun /*
750*4882a593Smuzhiyun * If the granularity bit is set, the limit is given in multiples
751*4882a593Smuzhiyun * of 4096. This also means that the 12 least significant bits are
752*4882a593Smuzhiyun * not tested when checking the segment limits. In practice,
753*4882a593Smuzhiyun * this means that the segment ends in (limit << 12) + 0xfff.
754*4882a593Smuzhiyun */
755*4882a593Smuzhiyun limit = get_desc_limit(&desc);
756*4882a593Smuzhiyun if (desc.g)
757*4882a593Smuzhiyun limit = (limit << 12) + 0xfff;
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun return limit;
760*4882a593Smuzhiyun }
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun /**
763*4882a593Smuzhiyun * insn_get_code_seg_params() - Obtain code segment parameters
764*4882a593Smuzhiyun * @regs: Structure with register values as seen when entering kernel mode
765*4882a593Smuzhiyun *
766*4882a593Smuzhiyun * Obtain address and operand sizes of the code segment. It is obtained from the
767*4882a593Smuzhiyun * selector contained in the CS register in regs. In protected mode, the default
768*4882a593Smuzhiyun * address is determined by inspecting the L and D bits of the segment
769*4882a593Smuzhiyun * descriptor. In virtual-8086 mode, the default is always two bytes for both
770*4882a593Smuzhiyun * address and operand sizes.
771*4882a593Smuzhiyun *
772*4882a593Smuzhiyun * Returns:
773*4882a593Smuzhiyun *
774*4882a593Smuzhiyun * An int containing ORed-in default parameters on success.
775*4882a593Smuzhiyun *
776*4882a593Smuzhiyun * -EINVAL on error.
777*4882a593Smuzhiyun */
insn_get_code_seg_params(struct pt_regs * regs)778*4882a593Smuzhiyun int insn_get_code_seg_params(struct pt_regs *regs)
779*4882a593Smuzhiyun {
780*4882a593Smuzhiyun struct desc_struct desc;
781*4882a593Smuzhiyun short sel;
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun if (v8086_mode(regs))
784*4882a593Smuzhiyun /* Address and operand size are both 16-bit. */
785*4882a593Smuzhiyun return INSN_CODE_SEG_PARAMS(2, 2);
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun sel = get_segment_selector(regs, INAT_SEG_REG_CS);
788*4882a593Smuzhiyun if (sel < 0)
789*4882a593Smuzhiyun return sel;
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun if (!get_desc(&desc, sel))
792*4882a593Smuzhiyun return -EINVAL;
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun /*
795*4882a593Smuzhiyun * The most significant byte of the Type field of the segment descriptor
796*4882a593Smuzhiyun * determines whether a segment contains data or code. If this is a data
797*4882a593Smuzhiyun * segment, return error.
798*4882a593Smuzhiyun */
799*4882a593Smuzhiyun if (!(desc.type & BIT(3)))
800*4882a593Smuzhiyun return -EINVAL;
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun switch ((desc.l << 1) | desc.d) {
803*4882a593Smuzhiyun case 0: /*
804*4882a593Smuzhiyun * Legacy mode. CS.L=0, CS.D=0. Address and operand size are
805*4882a593Smuzhiyun * both 16-bit.
806*4882a593Smuzhiyun */
807*4882a593Smuzhiyun return INSN_CODE_SEG_PARAMS(2, 2);
808*4882a593Smuzhiyun case 1: /*
809*4882a593Smuzhiyun * Legacy mode. CS.L=0, CS.D=1. Address and operand size are
810*4882a593Smuzhiyun * both 32-bit.
811*4882a593Smuzhiyun */
812*4882a593Smuzhiyun return INSN_CODE_SEG_PARAMS(4, 4);
813*4882a593Smuzhiyun case 2: /*
814*4882a593Smuzhiyun * IA-32e 64-bit mode. CS.L=1, CS.D=0. Address size is 64-bit;
815*4882a593Smuzhiyun * operand size is 32-bit.
816*4882a593Smuzhiyun */
817*4882a593Smuzhiyun return INSN_CODE_SEG_PARAMS(4, 8);
818*4882a593Smuzhiyun case 3: /* Invalid setting. CS.L=1, CS.D=1 */
819*4882a593Smuzhiyun fallthrough;
820*4882a593Smuzhiyun default:
821*4882a593Smuzhiyun return -EINVAL;
822*4882a593Smuzhiyun }
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun /**
826*4882a593Smuzhiyun * insn_get_modrm_rm_off() - Obtain register in r/m part of the ModRM byte
827*4882a593Smuzhiyun * @insn: Instruction containing the ModRM byte
828*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
829*4882a593Smuzhiyun *
830*4882a593Smuzhiyun * Returns:
831*4882a593Smuzhiyun *
832*4882a593Smuzhiyun * The register indicated by the r/m part of the ModRM byte. The
833*4882a593Smuzhiyun * register is obtained as an offset from the base of pt_regs. In specific
834*4882a593Smuzhiyun * cases, the returned value can be -EDOM to indicate that the particular value
835*4882a593Smuzhiyun * of ModRM does not refer to a register and shall be ignored.
836*4882a593Smuzhiyun */
insn_get_modrm_rm_off(struct insn * insn,struct pt_regs * regs)837*4882a593Smuzhiyun int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs)
838*4882a593Smuzhiyun {
839*4882a593Smuzhiyun return get_reg_offset(insn, regs, REG_TYPE_RM);
840*4882a593Smuzhiyun }
841*4882a593Smuzhiyun
842*4882a593Smuzhiyun /**
843*4882a593Smuzhiyun * insn_get_modrm_reg_off() - Obtain register in reg part of the ModRM byte
844*4882a593Smuzhiyun * @insn: Instruction containing the ModRM byte
845*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
846*4882a593Smuzhiyun *
847*4882a593Smuzhiyun * Returns:
848*4882a593Smuzhiyun *
849*4882a593Smuzhiyun * The register indicated by the reg part of the ModRM byte. The
850*4882a593Smuzhiyun * register is obtained as an offset from the base of pt_regs.
851*4882a593Smuzhiyun */
insn_get_modrm_reg_off(struct insn * insn,struct pt_regs * regs)852*4882a593Smuzhiyun int insn_get_modrm_reg_off(struct insn *insn, struct pt_regs *regs)
853*4882a593Smuzhiyun {
854*4882a593Smuzhiyun return get_reg_offset(insn, regs, REG_TYPE_REG);
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun /**
858*4882a593Smuzhiyun * get_seg_base_limit() - obtain base address and limit of a segment
859*4882a593Smuzhiyun * @insn: Instruction. Must be valid.
860*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
861*4882a593Smuzhiyun * @regoff: Operand offset, in pt_regs, used to resolve segment descriptor
862*4882a593Smuzhiyun * @base: Obtained segment base
863*4882a593Smuzhiyun * @limit: Obtained segment limit
864*4882a593Smuzhiyun *
865*4882a593Smuzhiyun * Obtain the base address and limit of the segment associated with the operand
866*4882a593Smuzhiyun * @regoff and, if any or allowed, override prefixes in @insn. This function is
867*4882a593Smuzhiyun * different from insn_get_seg_base() as the latter does not resolve the segment
868*4882a593Smuzhiyun * associated with the instruction operand. If a limit is not needed (e.g.,
869*4882a593Smuzhiyun * when running in long mode), @limit can be NULL.
870*4882a593Smuzhiyun *
871*4882a593Smuzhiyun * Returns:
872*4882a593Smuzhiyun *
873*4882a593Smuzhiyun * 0 on success. @base and @limit will contain the base address and of the
874*4882a593Smuzhiyun * resolved segment, respectively.
875*4882a593Smuzhiyun *
876*4882a593Smuzhiyun * -EINVAL on error.
877*4882a593Smuzhiyun */
get_seg_base_limit(struct insn * insn,struct pt_regs * regs,int regoff,unsigned long * base,unsigned long * limit)878*4882a593Smuzhiyun static int get_seg_base_limit(struct insn *insn, struct pt_regs *regs,
879*4882a593Smuzhiyun int regoff, unsigned long *base,
880*4882a593Smuzhiyun unsigned long *limit)
881*4882a593Smuzhiyun {
882*4882a593Smuzhiyun int seg_reg_idx;
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun if (!base)
885*4882a593Smuzhiyun return -EINVAL;
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun seg_reg_idx = resolve_seg_reg(insn, regs, regoff);
888*4882a593Smuzhiyun if (seg_reg_idx < 0)
889*4882a593Smuzhiyun return seg_reg_idx;
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun *base = insn_get_seg_base(regs, seg_reg_idx);
892*4882a593Smuzhiyun if (*base == -1L)
893*4882a593Smuzhiyun return -EINVAL;
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun if (!limit)
896*4882a593Smuzhiyun return 0;
897*4882a593Smuzhiyun
898*4882a593Smuzhiyun *limit = get_seg_limit(regs, seg_reg_idx);
899*4882a593Smuzhiyun if (!(*limit))
900*4882a593Smuzhiyun return -EINVAL;
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun return 0;
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun /**
906*4882a593Smuzhiyun * get_eff_addr_reg() - Obtain effective address from register operand
907*4882a593Smuzhiyun * @insn: Instruction. Must be valid.
908*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
909*4882a593Smuzhiyun * @regoff: Obtained operand offset, in pt_regs, with the effective address
910*4882a593Smuzhiyun * @eff_addr: Obtained effective address
911*4882a593Smuzhiyun *
912*4882a593Smuzhiyun * Obtain the effective address stored in the register operand as indicated by
913*4882a593Smuzhiyun * the ModRM byte. This function is to be used only with register addressing
914*4882a593Smuzhiyun * (i.e., ModRM.mod is 3). The effective address is saved in @eff_addr. The
915*4882a593Smuzhiyun * register operand, as an offset from the base of pt_regs, is saved in @regoff;
916*4882a593Smuzhiyun * such offset can then be used to resolve the segment associated with the
917*4882a593Smuzhiyun * operand. This function can be used with any of the supported address sizes
918*4882a593Smuzhiyun * in x86.
919*4882a593Smuzhiyun *
920*4882a593Smuzhiyun * Returns:
921*4882a593Smuzhiyun *
922*4882a593Smuzhiyun * 0 on success. @eff_addr will have the effective address stored in the
923*4882a593Smuzhiyun * operand indicated by ModRM. @regoff will have such operand as an offset from
924*4882a593Smuzhiyun * the base of pt_regs.
925*4882a593Smuzhiyun *
926*4882a593Smuzhiyun * -EINVAL on error.
927*4882a593Smuzhiyun */
get_eff_addr_reg(struct insn * insn,struct pt_regs * regs,int * regoff,long * eff_addr)928*4882a593Smuzhiyun static int get_eff_addr_reg(struct insn *insn, struct pt_regs *regs,
929*4882a593Smuzhiyun int *regoff, long *eff_addr)
930*4882a593Smuzhiyun {
931*4882a593Smuzhiyun int ret;
932*4882a593Smuzhiyun
933*4882a593Smuzhiyun ret = insn_get_modrm(insn);
934*4882a593Smuzhiyun if (ret)
935*4882a593Smuzhiyun return ret;
936*4882a593Smuzhiyun
937*4882a593Smuzhiyun if (X86_MODRM_MOD(insn->modrm.value) != 3)
938*4882a593Smuzhiyun return -EINVAL;
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun *regoff = get_reg_offset(insn, regs, REG_TYPE_RM);
941*4882a593Smuzhiyun if (*regoff < 0)
942*4882a593Smuzhiyun return -EINVAL;
943*4882a593Smuzhiyun
944*4882a593Smuzhiyun /* Ignore bytes that are outside the address size. */
945*4882a593Smuzhiyun if (insn->addr_bytes == 2)
946*4882a593Smuzhiyun *eff_addr = regs_get_register(regs, *regoff) & 0xffff;
947*4882a593Smuzhiyun else if (insn->addr_bytes == 4)
948*4882a593Smuzhiyun *eff_addr = regs_get_register(regs, *regoff) & 0xffffffff;
949*4882a593Smuzhiyun else /* 64-bit address */
950*4882a593Smuzhiyun *eff_addr = regs_get_register(regs, *regoff);
951*4882a593Smuzhiyun
952*4882a593Smuzhiyun return 0;
953*4882a593Smuzhiyun }
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun /**
956*4882a593Smuzhiyun * get_eff_addr_modrm() - Obtain referenced effective address via ModRM
957*4882a593Smuzhiyun * @insn: Instruction. Must be valid.
958*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
959*4882a593Smuzhiyun * @regoff: Obtained operand offset, in pt_regs, associated with segment
960*4882a593Smuzhiyun * @eff_addr: Obtained effective address
961*4882a593Smuzhiyun *
962*4882a593Smuzhiyun * Obtain the effective address referenced by the ModRM byte of @insn. After
963*4882a593Smuzhiyun * identifying the registers involved in the register-indirect memory reference,
964*4882a593Smuzhiyun * its value is obtained from the operands in @regs. The computed address is
965*4882a593Smuzhiyun * stored @eff_addr. Also, the register operand that indicates the associated
966*4882a593Smuzhiyun * segment is stored in @regoff, this parameter can later be used to determine
967*4882a593Smuzhiyun * such segment.
968*4882a593Smuzhiyun *
969*4882a593Smuzhiyun * Returns:
970*4882a593Smuzhiyun *
971*4882a593Smuzhiyun * 0 on success. @eff_addr will have the referenced effective address. @regoff
972*4882a593Smuzhiyun * will have a register, as an offset from the base of pt_regs, that can be used
973*4882a593Smuzhiyun * to resolve the associated segment.
974*4882a593Smuzhiyun *
975*4882a593Smuzhiyun * -EINVAL on error.
976*4882a593Smuzhiyun */
get_eff_addr_modrm(struct insn * insn,struct pt_regs * regs,int * regoff,long * eff_addr)977*4882a593Smuzhiyun static int get_eff_addr_modrm(struct insn *insn, struct pt_regs *regs,
978*4882a593Smuzhiyun int *regoff, long *eff_addr)
979*4882a593Smuzhiyun {
980*4882a593Smuzhiyun long tmp;
981*4882a593Smuzhiyun int ret;
982*4882a593Smuzhiyun
983*4882a593Smuzhiyun if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
984*4882a593Smuzhiyun return -EINVAL;
985*4882a593Smuzhiyun
986*4882a593Smuzhiyun ret = insn_get_modrm(insn);
987*4882a593Smuzhiyun if (ret)
988*4882a593Smuzhiyun return ret;
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun if (X86_MODRM_MOD(insn->modrm.value) > 2)
991*4882a593Smuzhiyun return -EINVAL;
992*4882a593Smuzhiyun
993*4882a593Smuzhiyun *regoff = get_reg_offset(insn, regs, REG_TYPE_RM);
994*4882a593Smuzhiyun
995*4882a593Smuzhiyun /*
996*4882a593Smuzhiyun * -EDOM means that we must ignore the address_offset. In such a case,
997*4882a593Smuzhiyun * in 64-bit mode the effective address relative to the rIP of the
998*4882a593Smuzhiyun * following instruction.
999*4882a593Smuzhiyun */
1000*4882a593Smuzhiyun if (*regoff == -EDOM) {
1001*4882a593Smuzhiyun if (any_64bit_mode(regs))
1002*4882a593Smuzhiyun tmp = regs->ip + insn->length;
1003*4882a593Smuzhiyun else
1004*4882a593Smuzhiyun tmp = 0;
1005*4882a593Smuzhiyun } else if (*regoff < 0) {
1006*4882a593Smuzhiyun return -EINVAL;
1007*4882a593Smuzhiyun } else {
1008*4882a593Smuzhiyun tmp = regs_get_register(regs, *regoff);
1009*4882a593Smuzhiyun }
1010*4882a593Smuzhiyun
1011*4882a593Smuzhiyun if (insn->addr_bytes == 4) {
1012*4882a593Smuzhiyun int addr32 = (int)(tmp & 0xffffffff) + insn->displacement.value;
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun *eff_addr = addr32 & 0xffffffff;
1015*4882a593Smuzhiyun } else {
1016*4882a593Smuzhiyun *eff_addr = tmp + insn->displacement.value;
1017*4882a593Smuzhiyun }
1018*4882a593Smuzhiyun
1019*4882a593Smuzhiyun return 0;
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun /**
1023*4882a593Smuzhiyun * get_eff_addr_modrm_16() - Obtain referenced effective address via ModRM
1024*4882a593Smuzhiyun * @insn: Instruction. Must be valid.
1025*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
1026*4882a593Smuzhiyun * @regoff: Obtained operand offset, in pt_regs, associated with segment
1027*4882a593Smuzhiyun * @eff_addr: Obtained effective address
1028*4882a593Smuzhiyun *
1029*4882a593Smuzhiyun * Obtain the 16-bit effective address referenced by the ModRM byte of @insn.
1030*4882a593Smuzhiyun * After identifying the registers involved in the register-indirect memory
1031*4882a593Smuzhiyun * reference, its value is obtained from the operands in @regs. The computed
1032*4882a593Smuzhiyun * address is stored @eff_addr. Also, the register operand that indicates
1033*4882a593Smuzhiyun * the associated segment is stored in @regoff, this parameter can later be used
1034*4882a593Smuzhiyun * to determine such segment.
1035*4882a593Smuzhiyun *
1036*4882a593Smuzhiyun * Returns:
1037*4882a593Smuzhiyun *
1038*4882a593Smuzhiyun * 0 on success. @eff_addr will have the referenced effective address. @regoff
1039*4882a593Smuzhiyun * will have a register, as an offset from the base of pt_regs, that can be used
1040*4882a593Smuzhiyun * to resolve the associated segment.
1041*4882a593Smuzhiyun *
1042*4882a593Smuzhiyun * -EINVAL on error.
1043*4882a593Smuzhiyun */
get_eff_addr_modrm_16(struct insn * insn,struct pt_regs * regs,int * regoff,short * eff_addr)1044*4882a593Smuzhiyun static int get_eff_addr_modrm_16(struct insn *insn, struct pt_regs *regs,
1045*4882a593Smuzhiyun int *regoff, short *eff_addr)
1046*4882a593Smuzhiyun {
1047*4882a593Smuzhiyun int addr_offset1, addr_offset2, ret;
1048*4882a593Smuzhiyun short addr1 = 0, addr2 = 0, displacement;
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun if (insn->addr_bytes != 2)
1051*4882a593Smuzhiyun return -EINVAL;
1052*4882a593Smuzhiyun
1053*4882a593Smuzhiyun insn_get_modrm(insn);
1054*4882a593Smuzhiyun
1055*4882a593Smuzhiyun if (!insn->modrm.nbytes)
1056*4882a593Smuzhiyun return -EINVAL;
1057*4882a593Smuzhiyun
1058*4882a593Smuzhiyun if (X86_MODRM_MOD(insn->modrm.value) > 2)
1059*4882a593Smuzhiyun return -EINVAL;
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun ret = get_reg_offset_16(insn, regs, &addr_offset1, &addr_offset2);
1062*4882a593Smuzhiyun if (ret < 0)
1063*4882a593Smuzhiyun return -EINVAL;
1064*4882a593Smuzhiyun
1065*4882a593Smuzhiyun /*
1066*4882a593Smuzhiyun * Don't fail on invalid offset values. They might be invalid because
1067*4882a593Smuzhiyun * they cannot be used for this particular value of ModRM. Instead, use
1068*4882a593Smuzhiyun * them in the computation only if they contain a valid value.
1069*4882a593Smuzhiyun */
1070*4882a593Smuzhiyun if (addr_offset1 != -EDOM)
1071*4882a593Smuzhiyun addr1 = regs_get_register(regs, addr_offset1) & 0xffff;
1072*4882a593Smuzhiyun
1073*4882a593Smuzhiyun if (addr_offset2 != -EDOM)
1074*4882a593Smuzhiyun addr2 = regs_get_register(regs, addr_offset2) & 0xffff;
1075*4882a593Smuzhiyun
1076*4882a593Smuzhiyun displacement = insn->displacement.value & 0xffff;
1077*4882a593Smuzhiyun *eff_addr = addr1 + addr2 + displacement;
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun /*
1080*4882a593Smuzhiyun * The first operand register could indicate to use of either SS or DS
1081*4882a593Smuzhiyun * registers to obtain the segment selector. The second operand
1082*4882a593Smuzhiyun * register can only indicate the use of DS. Thus, the first operand
1083*4882a593Smuzhiyun * will be used to obtain the segment selector.
1084*4882a593Smuzhiyun */
1085*4882a593Smuzhiyun *regoff = addr_offset1;
1086*4882a593Smuzhiyun
1087*4882a593Smuzhiyun return 0;
1088*4882a593Smuzhiyun }
1089*4882a593Smuzhiyun
1090*4882a593Smuzhiyun /**
1091*4882a593Smuzhiyun * get_eff_addr_sib() - Obtain referenced effective address via SIB
1092*4882a593Smuzhiyun * @insn: Instruction. Must be valid.
1093*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
1094*4882a593Smuzhiyun * @regoff: Obtained operand offset, in pt_regs, associated with segment
1095*4882a593Smuzhiyun * @eff_addr: Obtained effective address
1096*4882a593Smuzhiyun *
1097*4882a593Smuzhiyun * Obtain the effective address referenced by the SIB byte of @insn. After
1098*4882a593Smuzhiyun * identifying the registers involved in the indexed, register-indirect memory
1099*4882a593Smuzhiyun * reference, its value is obtained from the operands in @regs. The computed
1100*4882a593Smuzhiyun * address is stored @eff_addr. Also, the register operand that indicates the
1101*4882a593Smuzhiyun * associated segment is stored in @regoff, this parameter can later be used to
1102*4882a593Smuzhiyun * determine such segment.
1103*4882a593Smuzhiyun *
1104*4882a593Smuzhiyun * Returns:
1105*4882a593Smuzhiyun *
1106*4882a593Smuzhiyun * 0 on success. @eff_addr will have the referenced effective address.
1107*4882a593Smuzhiyun * @base_offset will have a register, as an offset from the base of pt_regs,
1108*4882a593Smuzhiyun * that can be used to resolve the associated segment.
1109*4882a593Smuzhiyun *
1110*4882a593Smuzhiyun * Negative value on error.
1111*4882a593Smuzhiyun */
get_eff_addr_sib(struct insn * insn,struct pt_regs * regs,int * base_offset,long * eff_addr)1112*4882a593Smuzhiyun static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs,
1113*4882a593Smuzhiyun int *base_offset, long *eff_addr)
1114*4882a593Smuzhiyun {
1115*4882a593Smuzhiyun long base, indx;
1116*4882a593Smuzhiyun int indx_offset;
1117*4882a593Smuzhiyun int ret;
1118*4882a593Smuzhiyun
1119*4882a593Smuzhiyun if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
1120*4882a593Smuzhiyun return -EINVAL;
1121*4882a593Smuzhiyun
1122*4882a593Smuzhiyun ret = insn_get_modrm(insn);
1123*4882a593Smuzhiyun if (ret)
1124*4882a593Smuzhiyun return ret;
1125*4882a593Smuzhiyun
1126*4882a593Smuzhiyun if (!insn->modrm.nbytes)
1127*4882a593Smuzhiyun return -EINVAL;
1128*4882a593Smuzhiyun
1129*4882a593Smuzhiyun if (X86_MODRM_MOD(insn->modrm.value) > 2)
1130*4882a593Smuzhiyun return -EINVAL;
1131*4882a593Smuzhiyun
1132*4882a593Smuzhiyun ret = insn_get_sib(insn);
1133*4882a593Smuzhiyun if (ret)
1134*4882a593Smuzhiyun return ret;
1135*4882a593Smuzhiyun
1136*4882a593Smuzhiyun if (!insn->sib.nbytes)
1137*4882a593Smuzhiyun return -EINVAL;
1138*4882a593Smuzhiyun
1139*4882a593Smuzhiyun *base_offset = get_reg_offset(insn, regs, REG_TYPE_BASE);
1140*4882a593Smuzhiyun indx_offset = get_reg_offset(insn, regs, REG_TYPE_INDEX);
1141*4882a593Smuzhiyun
1142*4882a593Smuzhiyun /*
1143*4882a593Smuzhiyun * Negative values in the base and index offset means an error when
1144*4882a593Smuzhiyun * decoding the SIB byte. Except -EDOM, which means that the registers
1145*4882a593Smuzhiyun * should not be used in the address computation.
1146*4882a593Smuzhiyun */
1147*4882a593Smuzhiyun if (*base_offset == -EDOM)
1148*4882a593Smuzhiyun base = 0;
1149*4882a593Smuzhiyun else if (*base_offset < 0)
1150*4882a593Smuzhiyun return -EINVAL;
1151*4882a593Smuzhiyun else
1152*4882a593Smuzhiyun base = regs_get_register(regs, *base_offset);
1153*4882a593Smuzhiyun
1154*4882a593Smuzhiyun if (indx_offset == -EDOM)
1155*4882a593Smuzhiyun indx = 0;
1156*4882a593Smuzhiyun else if (indx_offset < 0)
1157*4882a593Smuzhiyun return -EINVAL;
1158*4882a593Smuzhiyun else
1159*4882a593Smuzhiyun indx = regs_get_register(regs, indx_offset);
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun if (insn->addr_bytes == 4) {
1162*4882a593Smuzhiyun int addr32, base32, idx32;
1163*4882a593Smuzhiyun
1164*4882a593Smuzhiyun base32 = base & 0xffffffff;
1165*4882a593Smuzhiyun idx32 = indx & 0xffffffff;
1166*4882a593Smuzhiyun
1167*4882a593Smuzhiyun addr32 = base32 + idx32 * (1 << X86_SIB_SCALE(insn->sib.value));
1168*4882a593Smuzhiyun addr32 += insn->displacement.value;
1169*4882a593Smuzhiyun
1170*4882a593Smuzhiyun *eff_addr = addr32 & 0xffffffff;
1171*4882a593Smuzhiyun } else {
1172*4882a593Smuzhiyun *eff_addr = base + indx * (1 << X86_SIB_SCALE(insn->sib.value));
1173*4882a593Smuzhiyun *eff_addr += insn->displacement.value;
1174*4882a593Smuzhiyun }
1175*4882a593Smuzhiyun
1176*4882a593Smuzhiyun return 0;
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun
1179*4882a593Smuzhiyun /**
1180*4882a593Smuzhiyun * get_addr_ref_16() - Obtain the 16-bit address referred by instruction
1181*4882a593Smuzhiyun * @insn: Instruction containing ModRM byte and displacement
1182*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
1183*4882a593Smuzhiyun *
1184*4882a593Smuzhiyun * This function is to be used with 16-bit address encodings. Obtain the memory
1185*4882a593Smuzhiyun * address referred by the instruction's ModRM and displacement bytes. Also, the
1186*4882a593Smuzhiyun * segment used as base is determined by either any segment override prefixes in
1187*4882a593Smuzhiyun * @insn or the default segment of the registers involved in the address
1188*4882a593Smuzhiyun * computation. In protected mode, segment limits are enforced.
1189*4882a593Smuzhiyun *
1190*4882a593Smuzhiyun * Returns:
1191*4882a593Smuzhiyun *
1192*4882a593Smuzhiyun * Linear address referenced by the instruction operands on success.
1193*4882a593Smuzhiyun *
1194*4882a593Smuzhiyun * -1L on error.
1195*4882a593Smuzhiyun */
get_addr_ref_16(struct insn * insn,struct pt_regs * regs)1196*4882a593Smuzhiyun static void __user *get_addr_ref_16(struct insn *insn, struct pt_regs *regs)
1197*4882a593Smuzhiyun {
1198*4882a593Smuzhiyun unsigned long linear_addr = -1L, seg_base, seg_limit;
1199*4882a593Smuzhiyun int ret, regoff;
1200*4882a593Smuzhiyun short eff_addr;
1201*4882a593Smuzhiyun long tmp;
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun if (insn_get_displacement(insn))
1204*4882a593Smuzhiyun goto out;
1205*4882a593Smuzhiyun
1206*4882a593Smuzhiyun if (insn->addr_bytes != 2)
1207*4882a593Smuzhiyun goto out;
1208*4882a593Smuzhiyun
1209*4882a593Smuzhiyun if (X86_MODRM_MOD(insn->modrm.value) == 3) {
1210*4882a593Smuzhiyun ret = get_eff_addr_reg(insn, regs, ®off, &tmp);
1211*4882a593Smuzhiyun if (ret)
1212*4882a593Smuzhiyun goto out;
1213*4882a593Smuzhiyun
1214*4882a593Smuzhiyun eff_addr = tmp;
1215*4882a593Smuzhiyun } else {
1216*4882a593Smuzhiyun ret = get_eff_addr_modrm_16(insn, regs, ®off, &eff_addr);
1217*4882a593Smuzhiyun if (ret)
1218*4882a593Smuzhiyun goto out;
1219*4882a593Smuzhiyun }
1220*4882a593Smuzhiyun
1221*4882a593Smuzhiyun ret = get_seg_base_limit(insn, regs, regoff, &seg_base, &seg_limit);
1222*4882a593Smuzhiyun if (ret)
1223*4882a593Smuzhiyun goto out;
1224*4882a593Smuzhiyun
1225*4882a593Smuzhiyun /*
1226*4882a593Smuzhiyun * Before computing the linear address, make sure the effective address
1227*4882a593Smuzhiyun * is within the limits of the segment. In virtual-8086 mode, segment
1228*4882a593Smuzhiyun * limits are not enforced. In such a case, the segment limit is -1L to
1229*4882a593Smuzhiyun * reflect this fact.
1230*4882a593Smuzhiyun */
1231*4882a593Smuzhiyun if ((unsigned long)(eff_addr & 0xffff) > seg_limit)
1232*4882a593Smuzhiyun goto out;
1233*4882a593Smuzhiyun
1234*4882a593Smuzhiyun linear_addr = (unsigned long)(eff_addr & 0xffff) + seg_base;
1235*4882a593Smuzhiyun
1236*4882a593Smuzhiyun /* Limit linear address to 20 bits */
1237*4882a593Smuzhiyun if (v8086_mode(regs))
1238*4882a593Smuzhiyun linear_addr &= 0xfffff;
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun out:
1241*4882a593Smuzhiyun return (void __user *)linear_addr;
1242*4882a593Smuzhiyun }
1243*4882a593Smuzhiyun
1244*4882a593Smuzhiyun /**
1245*4882a593Smuzhiyun * get_addr_ref_32() - Obtain a 32-bit linear address
1246*4882a593Smuzhiyun * @insn: Instruction with ModRM, SIB bytes and displacement
1247*4882a593Smuzhiyun * @regs: Register values as seen when entering kernel mode
1248*4882a593Smuzhiyun *
1249*4882a593Smuzhiyun * This function is to be used with 32-bit address encodings to obtain the
1250*4882a593Smuzhiyun * linear memory address referred by the instruction's ModRM, SIB,
1251*4882a593Smuzhiyun * displacement bytes and segment base address, as applicable. If in protected
1252*4882a593Smuzhiyun * mode, segment limits are enforced.
1253*4882a593Smuzhiyun *
1254*4882a593Smuzhiyun * Returns:
1255*4882a593Smuzhiyun *
1256*4882a593Smuzhiyun * Linear address referenced by instruction and registers on success.
1257*4882a593Smuzhiyun *
1258*4882a593Smuzhiyun * -1L on error.
1259*4882a593Smuzhiyun */
get_addr_ref_32(struct insn * insn,struct pt_regs * regs)1260*4882a593Smuzhiyun static void __user *get_addr_ref_32(struct insn *insn, struct pt_regs *regs)
1261*4882a593Smuzhiyun {
1262*4882a593Smuzhiyun unsigned long linear_addr = -1L, seg_base, seg_limit;
1263*4882a593Smuzhiyun int eff_addr, regoff;
1264*4882a593Smuzhiyun long tmp;
1265*4882a593Smuzhiyun int ret;
1266*4882a593Smuzhiyun
1267*4882a593Smuzhiyun if (insn->addr_bytes != 4)
1268*4882a593Smuzhiyun goto out;
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun if (X86_MODRM_MOD(insn->modrm.value) == 3) {
1271*4882a593Smuzhiyun ret = get_eff_addr_reg(insn, regs, ®off, &tmp);
1272*4882a593Smuzhiyun if (ret)
1273*4882a593Smuzhiyun goto out;
1274*4882a593Smuzhiyun
1275*4882a593Smuzhiyun eff_addr = tmp;
1276*4882a593Smuzhiyun
1277*4882a593Smuzhiyun } else {
1278*4882a593Smuzhiyun if (insn->sib.nbytes) {
1279*4882a593Smuzhiyun ret = get_eff_addr_sib(insn, regs, ®off, &tmp);
1280*4882a593Smuzhiyun if (ret)
1281*4882a593Smuzhiyun goto out;
1282*4882a593Smuzhiyun
1283*4882a593Smuzhiyun eff_addr = tmp;
1284*4882a593Smuzhiyun } else {
1285*4882a593Smuzhiyun ret = get_eff_addr_modrm(insn, regs, ®off, &tmp);
1286*4882a593Smuzhiyun if (ret)
1287*4882a593Smuzhiyun goto out;
1288*4882a593Smuzhiyun
1289*4882a593Smuzhiyun eff_addr = tmp;
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun }
1292*4882a593Smuzhiyun
1293*4882a593Smuzhiyun ret = get_seg_base_limit(insn, regs, regoff, &seg_base, &seg_limit);
1294*4882a593Smuzhiyun if (ret)
1295*4882a593Smuzhiyun goto out;
1296*4882a593Smuzhiyun
1297*4882a593Smuzhiyun /*
1298*4882a593Smuzhiyun * In protected mode, before computing the linear address, make sure
1299*4882a593Smuzhiyun * the effective address is within the limits of the segment.
1300*4882a593Smuzhiyun * 32-bit addresses can be used in long and virtual-8086 modes if an
1301*4882a593Smuzhiyun * address override prefix is used. In such cases, segment limits are
1302*4882a593Smuzhiyun * not enforced. When in virtual-8086 mode, the segment limit is -1L
1303*4882a593Smuzhiyun * to reflect this situation.
1304*4882a593Smuzhiyun *
1305*4882a593Smuzhiyun * After computed, the effective address is treated as an unsigned
1306*4882a593Smuzhiyun * quantity.
1307*4882a593Smuzhiyun */
1308*4882a593Smuzhiyun if (!any_64bit_mode(regs) && ((unsigned int)eff_addr > seg_limit))
1309*4882a593Smuzhiyun goto out;
1310*4882a593Smuzhiyun
1311*4882a593Smuzhiyun /*
1312*4882a593Smuzhiyun * Even though 32-bit address encodings are allowed in virtual-8086
1313*4882a593Smuzhiyun * mode, the address range is still limited to [0x-0xffff].
1314*4882a593Smuzhiyun */
1315*4882a593Smuzhiyun if (v8086_mode(regs) && (eff_addr & ~0xffff))
1316*4882a593Smuzhiyun goto out;
1317*4882a593Smuzhiyun
1318*4882a593Smuzhiyun /*
1319*4882a593Smuzhiyun * Data type long could be 64 bits in size. Ensure that our 32-bit
1320*4882a593Smuzhiyun * effective address is not sign-extended when computing the linear
1321*4882a593Smuzhiyun * address.
1322*4882a593Smuzhiyun */
1323*4882a593Smuzhiyun linear_addr = (unsigned long)(eff_addr & 0xffffffff) + seg_base;
1324*4882a593Smuzhiyun
1325*4882a593Smuzhiyun /* Limit linear address to 20 bits */
1326*4882a593Smuzhiyun if (v8086_mode(regs))
1327*4882a593Smuzhiyun linear_addr &= 0xfffff;
1328*4882a593Smuzhiyun
1329*4882a593Smuzhiyun out:
1330*4882a593Smuzhiyun return (void __user *)linear_addr;
1331*4882a593Smuzhiyun }
1332*4882a593Smuzhiyun
1333*4882a593Smuzhiyun /**
1334*4882a593Smuzhiyun * get_addr_ref_64() - Obtain a 64-bit linear address
1335*4882a593Smuzhiyun * @insn: Instruction struct with ModRM and SIB bytes and displacement
1336*4882a593Smuzhiyun * @regs: Structure with register values as seen when entering kernel mode
1337*4882a593Smuzhiyun *
1338*4882a593Smuzhiyun * This function is to be used with 64-bit address encodings to obtain the
1339*4882a593Smuzhiyun * linear memory address referred by the instruction's ModRM, SIB,
1340*4882a593Smuzhiyun * displacement bytes and segment base address, as applicable.
1341*4882a593Smuzhiyun *
1342*4882a593Smuzhiyun * Returns:
1343*4882a593Smuzhiyun *
1344*4882a593Smuzhiyun * Linear address referenced by instruction and registers on success.
1345*4882a593Smuzhiyun *
1346*4882a593Smuzhiyun * -1L on error.
1347*4882a593Smuzhiyun */
1348*4882a593Smuzhiyun #ifndef CONFIG_X86_64
get_addr_ref_64(struct insn * insn,struct pt_regs * regs)1349*4882a593Smuzhiyun static void __user *get_addr_ref_64(struct insn *insn, struct pt_regs *regs)
1350*4882a593Smuzhiyun {
1351*4882a593Smuzhiyun return (void __user *)-1L;
1352*4882a593Smuzhiyun }
1353*4882a593Smuzhiyun #else
get_addr_ref_64(struct insn * insn,struct pt_regs * regs)1354*4882a593Smuzhiyun static void __user *get_addr_ref_64(struct insn *insn, struct pt_regs *regs)
1355*4882a593Smuzhiyun {
1356*4882a593Smuzhiyun unsigned long linear_addr = -1L, seg_base;
1357*4882a593Smuzhiyun int regoff, ret;
1358*4882a593Smuzhiyun long eff_addr;
1359*4882a593Smuzhiyun
1360*4882a593Smuzhiyun if (insn->addr_bytes != 8)
1361*4882a593Smuzhiyun goto out;
1362*4882a593Smuzhiyun
1363*4882a593Smuzhiyun if (X86_MODRM_MOD(insn->modrm.value) == 3) {
1364*4882a593Smuzhiyun ret = get_eff_addr_reg(insn, regs, ®off, &eff_addr);
1365*4882a593Smuzhiyun if (ret)
1366*4882a593Smuzhiyun goto out;
1367*4882a593Smuzhiyun
1368*4882a593Smuzhiyun } else {
1369*4882a593Smuzhiyun if (insn->sib.nbytes) {
1370*4882a593Smuzhiyun ret = get_eff_addr_sib(insn, regs, ®off, &eff_addr);
1371*4882a593Smuzhiyun if (ret)
1372*4882a593Smuzhiyun goto out;
1373*4882a593Smuzhiyun } else {
1374*4882a593Smuzhiyun ret = get_eff_addr_modrm(insn, regs, ®off, &eff_addr);
1375*4882a593Smuzhiyun if (ret)
1376*4882a593Smuzhiyun goto out;
1377*4882a593Smuzhiyun }
1378*4882a593Smuzhiyun
1379*4882a593Smuzhiyun }
1380*4882a593Smuzhiyun
1381*4882a593Smuzhiyun ret = get_seg_base_limit(insn, regs, regoff, &seg_base, NULL);
1382*4882a593Smuzhiyun if (ret)
1383*4882a593Smuzhiyun goto out;
1384*4882a593Smuzhiyun
1385*4882a593Smuzhiyun linear_addr = (unsigned long)eff_addr + seg_base;
1386*4882a593Smuzhiyun
1387*4882a593Smuzhiyun out:
1388*4882a593Smuzhiyun return (void __user *)linear_addr;
1389*4882a593Smuzhiyun }
1390*4882a593Smuzhiyun #endif /* CONFIG_X86_64 */
1391*4882a593Smuzhiyun
1392*4882a593Smuzhiyun /**
1393*4882a593Smuzhiyun * insn_get_addr_ref() - Obtain the linear address referred by instruction
1394*4882a593Smuzhiyun * @insn: Instruction structure containing ModRM byte and displacement
1395*4882a593Smuzhiyun * @regs: Structure with register values as seen when entering kernel mode
1396*4882a593Smuzhiyun *
1397*4882a593Smuzhiyun * Obtain the linear address referred by the instruction's ModRM, SIB and
1398*4882a593Smuzhiyun * displacement bytes, and segment base, as applicable. In protected mode,
1399*4882a593Smuzhiyun * segment limits are enforced.
1400*4882a593Smuzhiyun *
1401*4882a593Smuzhiyun * Returns:
1402*4882a593Smuzhiyun *
1403*4882a593Smuzhiyun * Linear address referenced by instruction and registers on success.
1404*4882a593Smuzhiyun *
1405*4882a593Smuzhiyun * -1L on error.
1406*4882a593Smuzhiyun */
insn_get_addr_ref(struct insn * insn,struct pt_regs * regs)1407*4882a593Smuzhiyun void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs)
1408*4882a593Smuzhiyun {
1409*4882a593Smuzhiyun if (!insn || !regs)
1410*4882a593Smuzhiyun return (void __user *)-1L;
1411*4882a593Smuzhiyun
1412*4882a593Smuzhiyun switch (insn->addr_bytes) {
1413*4882a593Smuzhiyun case 2:
1414*4882a593Smuzhiyun return get_addr_ref_16(insn, regs);
1415*4882a593Smuzhiyun case 4:
1416*4882a593Smuzhiyun return get_addr_ref_32(insn, regs);
1417*4882a593Smuzhiyun case 8:
1418*4882a593Smuzhiyun return get_addr_ref_64(insn, regs);
1419*4882a593Smuzhiyun default:
1420*4882a593Smuzhiyun return (void __user *)-1L;
1421*4882a593Smuzhiyun }
1422*4882a593Smuzhiyun }
1423*4882a593Smuzhiyun
insn_get_effective_ip(struct pt_regs * regs)1424*4882a593Smuzhiyun unsigned long insn_get_effective_ip(struct pt_regs *regs)
1425*4882a593Smuzhiyun {
1426*4882a593Smuzhiyun unsigned long seg_base = 0;
1427*4882a593Smuzhiyun
1428*4882a593Smuzhiyun /*
1429*4882a593Smuzhiyun * If not in user-space long mode, a custom code segment could be in
1430*4882a593Smuzhiyun * use. This is true in protected mode (if the process defined a local
1431*4882a593Smuzhiyun * descriptor table), or virtual-8086 mode. In most of the cases
1432*4882a593Smuzhiyun * seg_base will be zero as in USER_CS.
1433*4882a593Smuzhiyun */
1434*4882a593Smuzhiyun if (!user_64bit_mode(regs)) {
1435*4882a593Smuzhiyun seg_base = insn_get_seg_base(regs, INAT_SEG_REG_CS);
1436*4882a593Smuzhiyun if (seg_base == -1L)
1437*4882a593Smuzhiyun return 0;
1438*4882a593Smuzhiyun }
1439*4882a593Smuzhiyun
1440*4882a593Smuzhiyun return seg_base + regs->ip;
1441*4882a593Smuzhiyun }
1442*4882a593Smuzhiyun
1443*4882a593Smuzhiyun /**
1444*4882a593Smuzhiyun * insn_fetch_from_user() - Copy instruction bytes from user-space memory
1445*4882a593Smuzhiyun * @regs: Structure with register values as seen when entering kernel mode
1446*4882a593Smuzhiyun * @buf: Array to store the fetched instruction
1447*4882a593Smuzhiyun *
1448*4882a593Smuzhiyun * Gets the linear address of the instruction and copies the instruction bytes
1449*4882a593Smuzhiyun * to the buf.
1450*4882a593Smuzhiyun *
1451*4882a593Smuzhiyun * Returns:
1452*4882a593Smuzhiyun *
1453*4882a593Smuzhiyun * Number of instruction bytes copied.
1454*4882a593Smuzhiyun *
1455*4882a593Smuzhiyun * 0 if nothing was copied.
1456*4882a593Smuzhiyun */
insn_fetch_from_user(struct pt_regs * regs,unsigned char buf[MAX_INSN_SIZE])1457*4882a593Smuzhiyun int insn_fetch_from_user(struct pt_regs *regs, unsigned char buf[MAX_INSN_SIZE])
1458*4882a593Smuzhiyun {
1459*4882a593Smuzhiyun unsigned long ip;
1460*4882a593Smuzhiyun int not_copied;
1461*4882a593Smuzhiyun
1462*4882a593Smuzhiyun ip = insn_get_effective_ip(regs);
1463*4882a593Smuzhiyun if (!ip)
1464*4882a593Smuzhiyun return 0;
1465*4882a593Smuzhiyun
1466*4882a593Smuzhiyun not_copied = copy_from_user(buf, (void __user *)ip, MAX_INSN_SIZE);
1467*4882a593Smuzhiyun
1468*4882a593Smuzhiyun return MAX_INSN_SIZE - not_copied;
1469*4882a593Smuzhiyun }
1470*4882a593Smuzhiyun
1471*4882a593Smuzhiyun /**
1472*4882a593Smuzhiyun * insn_fetch_from_user_inatomic() - Copy instruction bytes from user-space memory
1473*4882a593Smuzhiyun * while in atomic code
1474*4882a593Smuzhiyun * @regs: Structure with register values as seen when entering kernel mode
1475*4882a593Smuzhiyun * @buf: Array to store the fetched instruction
1476*4882a593Smuzhiyun *
1477*4882a593Smuzhiyun * Gets the linear address of the instruction and copies the instruction bytes
1478*4882a593Smuzhiyun * to the buf. This function must be used in atomic context.
1479*4882a593Smuzhiyun *
1480*4882a593Smuzhiyun * Returns:
1481*4882a593Smuzhiyun *
1482*4882a593Smuzhiyun * Number of instruction bytes copied.
1483*4882a593Smuzhiyun *
1484*4882a593Smuzhiyun * 0 if nothing was copied.
1485*4882a593Smuzhiyun */
insn_fetch_from_user_inatomic(struct pt_regs * regs,unsigned char buf[MAX_INSN_SIZE])1486*4882a593Smuzhiyun int insn_fetch_from_user_inatomic(struct pt_regs *regs, unsigned char buf[MAX_INSN_SIZE])
1487*4882a593Smuzhiyun {
1488*4882a593Smuzhiyun unsigned long ip;
1489*4882a593Smuzhiyun int not_copied;
1490*4882a593Smuzhiyun
1491*4882a593Smuzhiyun ip = insn_get_effective_ip(regs);
1492*4882a593Smuzhiyun if (!ip)
1493*4882a593Smuzhiyun return 0;
1494*4882a593Smuzhiyun
1495*4882a593Smuzhiyun not_copied = __copy_from_user_inatomic(buf, (void __user *)ip, MAX_INSN_SIZE);
1496*4882a593Smuzhiyun
1497*4882a593Smuzhiyun return MAX_INSN_SIZE - not_copied;
1498*4882a593Smuzhiyun }
1499*4882a593Smuzhiyun
1500*4882a593Smuzhiyun /**
1501*4882a593Smuzhiyun * insn_decode_from_regs() - Decode an instruction
1502*4882a593Smuzhiyun * @insn: Structure to store decoded instruction
1503*4882a593Smuzhiyun * @regs: Structure with register values as seen when entering kernel mode
1504*4882a593Smuzhiyun * @buf: Buffer containing the instruction bytes
1505*4882a593Smuzhiyun * @buf_size: Number of instruction bytes available in buf
1506*4882a593Smuzhiyun *
1507*4882a593Smuzhiyun * Decodes the instruction provided in buf and stores the decoding results in
1508*4882a593Smuzhiyun * insn. Also determines the correct address and operand sizes.
1509*4882a593Smuzhiyun *
1510*4882a593Smuzhiyun * Returns:
1511*4882a593Smuzhiyun *
1512*4882a593Smuzhiyun * True if instruction was decoded, False otherwise.
1513*4882a593Smuzhiyun */
insn_decode_from_regs(struct insn * insn,struct pt_regs * regs,unsigned char buf[MAX_INSN_SIZE],int buf_size)1514*4882a593Smuzhiyun bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs,
1515*4882a593Smuzhiyun unsigned char buf[MAX_INSN_SIZE], int buf_size)
1516*4882a593Smuzhiyun {
1517*4882a593Smuzhiyun int seg_defs;
1518*4882a593Smuzhiyun
1519*4882a593Smuzhiyun insn_init(insn, buf, buf_size, user_64bit_mode(regs));
1520*4882a593Smuzhiyun
1521*4882a593Smuzhiyun /*
1522*4882a593Smuzhiyun * Override the default operand and address sizes with what is specified
1523*4882a593Smuzhiyun * in the code segment descriptor. The instruction decoder only sets
1524*4882a593Smuzhiyun * the address size it to either 4 or 8 address bytes and does nothing
1525*4882a593Smuzhiyun * for the operand bytes. This OK for most of the cases, but we could
1526*4882a593Smuzhiyun * have special cases where, for instance, a 16-bit code segment
1527*4882a593Smuzhiyun * descriptor is used.
1528*4882a593Smuzhiyun * If there is an address override prefix, the instruction decoder
1529*4882a593Smuzhiyun * correctly updates these values, even for 16-bit defaults.
1530*4882a593Smuzhiyun */
1531*4882a593Smuzhiyun seg_defs = insn_get_code_seg_params(regs);
1532*4882a593Smuzhiyun if (seg_defs == -EINVAL)
1533*4882a593Smuzhiyun return false;
1534*4882a593Smuzhiyun
1535*4882a593Smuzhiyun insn->addr_bytes = INSN_CODE_SEG_ADDR_SZ(seg_defs);
1536*4882a593Smuzhiyun insn->opnd_bytes = INSN_CODE_SEG_OPND_SZ(seg_defs);
1537*4882a593Smuzhiyun
1538*4882a593Smuzhiyun if (insn_get_length(insn))
1539*4882a593Smuzhiyun return false;
1540*4882a593Smuzhiyun
1541*4882a593Smuzhiyun if (buf_size < insn->length)
1542*4882a593Smuzhiyun return false;
1543*4882a593Smuzhiyun
1544*4882a593Smuzhiyun return true;
1545*4882a593Smuzhiyun }
1546