x86_emulate.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. /******************************************************************************
  2. * x86_emulate.c
  3. *
  4. * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
  5. *
  6. * Copyright (c) 2005 Keir Fraser
  7. *
  8. * Linux coding style, mod r/m decoder, segment base fixes, real-mode
  9. * privileged instructions:
  10. *
  11. * Copyright (C) 2006 Qumranet
  12. *
  13. * Avi Kivity <avi@qumranet.com>
  14. * Yaniv Kamay <yaniv@qumranet.com>
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2. See
  17. * the COPYING file in the top-level directory.
  18. *
  19. * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
  20. */
  21. #ifndef __KERNEL__
  22. #include <stdio.h>
  23. #include <stdint.h>
  24. #include <public/xen.h>
  25. #define DPRINTF(_f, _a ...) printf( _f , ## _a )
  26. #else
  27. #include "kvm.h"
  28. #define DPRINTF(x...) do {} while (0)
  29. #endif
  30. #include "x86_emulate.h"
  31. #include <linux/module.h>
  32. /*
  33. * Opcode effective-address decode tables.
  34. * Note that we only emulate instructions that have at least one memory
  35. * operand (excluding implicit stack references). We assume that stack
  36. * references and instruction fetches will never occur in special memory
  37. * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
  38. * not be handled.
  39. */
  40. /* Operand sizes: 8-bit operands or specified/overridden size. */
  41. #define ByteOp (1<<0) /* 8-bit operands. */
  42. /* Destination operand type. */
  43. #define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
  44. #define DstReg (2<<1) /* Register operand. */
  45. #define DstMem (3<<1) /* Memory operand. */
  46. #define DstMask (3<<1)
  47. /* Source operand type. */
  48. #define SrcNone (0<<3) /* No source operand. */
  49. #define SrcImplicit (0<<3) /* Source operand is implicit in the opcode. */
  50. #define SrcReg (1<<3) /* Register operand. */
  51. #define SrcMem (2<<3) /* Memory operand. */
  52. #define SrcMem16 (3<<3) /* Memory operand (16-bit). */
  53. #define SrcMem32 (4<<3) /* Memory operand (32-bit). */
  54. #define SrcImm (5<<3) /* Immediate operand. */
  55. #define SrcImmByte (6<<3) /* 8-bit sign-extended immediate operand. */
  56. #define SrcMask (7<<3)
  57. /* Generic ModRM decode. */
  58. #define ModRM (1<<6)
  59. /* Destination is only written; never read. */
  60. #define Mov (1<<7)
  61. #define BitOp (1<<8)
  62. static u8 opcode_table[256] = {
  63. /* 0x00 - 0x07 */
  64. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  65. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  66. 0, 0, 0, 0,
  67. /* 0x08 - 0x0F */
  68. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  69. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  70. 0, 0, 0, 0,
  71. /* 0x10 - 0x17 */
  72. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  73. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  74. 0, 0, 0, 0,
  75. /* 0x18 - 0x1F */
  76. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  77. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  78. 0, 0, 0, 0,
  79. /* 0x20 - 0x27 */
  80. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  81. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  82. SrcImmByte, SrcImm, 0, 0,
  83. /* 0x28 - 0x2F */
  84. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  85. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  86. 0, 0, 0, 0,
  87. /* 0x30 - 0x37 */
  88. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  89. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  90. 0, 0, 0, 0,
  91. /* 0x38 - 0x3F */
  92. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  93. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  94. 0, 0, 0, 0,
  95. /* 0x40 - 0x4F */
  96. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  97. /* 0x50 - 0x57 */
  98. ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
  99. ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
  100. /* 0x58 - 0x5F */
  101. ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
  102. ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
  103. /* 0x60 - 0x6B */
  104. 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
  105. 0, 0, 0, 0, 0, 0, 0, 0,
  106. /* 0x6C - 0x6F */
  107. SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */
  108. SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */
  109. /* 0x70 - 0x7F */
  110. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  111. /* 0x80 - 0x87 */
  112. ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
  113. ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
  114. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  115. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  116. /* 0x88 - 0x8F */
  117. ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
  118. ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  119. 0, 0, 0, DstMem | SrcNone | ModRM | Mov,
  120. /* 0x90 - 0x9F */
  121. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  122. /* 0xA0 - 0xA7 */
  123. ByteOp | DstReg | SrcMem | Mov, DstReg | SrcMem | Mov,
  124. ByteOp | DstMem | SrcReg | Mov, DstMem | SrcReg | Mov,
  125. ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
  126. ByteOp | ImplicitOps, ImplicitOps,
  127. /* 0xA8 - 0xAF */
  128. 0, 0, ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
  129. ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
  130. ByteOp | ImplicitOps, ImplicitOps,
  131. /* 0xB0 - 0xBF */
  132. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  133. /* 0xC0 - 0xC7 */
  134. ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
  135. 0, ImplicitOps, 0, 0,
  136. ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
  137. /* 0xC8 - 0xCF */
  138. 0, 0, 0, 0, 0, 0, 0, 0,
  139. /* 0xD0 - 0xD7 */
  140. ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
  141. ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
  142. 0, 0, 0, 0,
  143. /* 0xD8 - 0xDF */
  144. 0, 0, 0, 0, 0, 0, 0, 0,
  145. /* 0xE0 - 0xE7 */
  146. 0, 0, 0, 0, 0, 0, 0, 0,
  147. /* 0xE8 - 0xEF */
  148. 0, SrcImm|ImplicitOps, 0, SrcImmByte|ImplicitOps, 0, 0, 0, 0,
  149. /* 0xF0 - 0xF7 */
  150. 0, 0, 0, 0,
  151. ImplicitOps, 0,
  152. ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
  153. /* 0xF8 - 0xFF */
  154. 0, 0, 0, 0,
  155. 0, 0, ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM
  156. };
  157. static u16 twobyte_table[256] = {
  158. /* 0x00 - 0x0F */
  159. 0, SrcMem | ModRM | DstReg, 0, 0, 0, 0, ImplicitOps, 0,
  160. 0, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
  161. /* 0x10 - 0x1F */
  162. 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
  163. /* 0x20 - 0x2F */
  164. ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
  165. 0, 0, 0, 0, 0, 0, 0, 0,
  166. /* 0x30 - 0x3F */
  167. ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  168. /* 0x40 - 0x47 */
  169. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  170. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  171. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  172. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  173. /* 0x48 - 0x4F */
  174. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  175. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  176. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  177. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  178. /* 0x50 - 0x5F */
  179. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  180. /* 0x60 - 0x6F */
  181. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  182. /* 0x70 - 0x7F */
  183. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  184. /* 0x80 - 0x8F */
  185. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  186. /* 0x90 - 0x9F */
  187. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  188. /* 0xA0 - 0xA7 */
  189. 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
  190. /* 0xA8 - 0xAF */
  191. 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
  192. /* 0xB0 - 0xB7 */
  193. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
  194. DstMem | SrcReg | ModRM | BitOp,
  195. 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
  196. DstReg | SrcMem16 | ModRM | Mov,
  197. /* 0xB8 - 0xBF */
  198. 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
  199. 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
  200. DstReg | SrcMem16 | ModRM | Mov,
  201. /* 0xC0 - 0xCF */
  202. 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0, 0,
  203. /* 0xD0 - 0xDF */
  204. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  205. /* 0xE0 - 0xEF */
  206. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  207. /* 0xF0 - 0xFF */
  208. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  209. };
  210. /*
  211. * Tell the emulator that of the Group 7 instructions (sgdt, lidt, etc.) we
  212. * are interested only in invlpg and not in any of the rest.
  213. *
  214. * invlpg is a special instruction in that the data it references may not
  215. * be mapped.
  216. */
  217. void kvm_emulator_want_group7_invlpg(void)
  218. {
  219. twobyte_table[1] &= ~SrcMem;
  220. }
  221. EXPORT_SYMBOL_GPL(kvm_emulator_want_group7_invlpg);
  222. /* Type, address-of, and value of an instruction's operand. */
  223. struct operand {
  224. enum { OP_REG, OP_MEM, OP_IMM } type;
  225. unsigned int bytes;
  226. unsigned long val, orig_val, *ptr;
  227. };
  228. /* EFLAGS bit definitions. */
  229. #define EFLG_OF (1<<11)
  230. #define EFLG_DF (1<<10)
  231. #define EFLG_SF (1<<7)
  232. #define EFLG_ZF (1<<6)
  233. #define EFLG_AF (1<<4)
  234. #define EFLG_PF (1<<2)
  235. #define EFLG_CF (1<<0)
  236. /*
  237. * Instruction emulation:
  238. * Most instructions are emulated directly via a fragment of inline assembly
  239. * code. This allows us to save/restore EFLAGS and thus very easily pick up
  240. * any modified flags.
  241. */
  242. #if defined(CONFIG_X86_64)
  243. #define _LO32 "k" /* force 32-bit operand */
  244. #define _STK "%%rsp" /* stack pointer */
  245. #elif defined(__i386__)
  246. #define _LO32 "" /* force 32-bit operand */
  247. #define _STK "%%esp" /* stack pointer */
  248. #endif
  249. /*
  250. * These EFLAGS bits are restored from saved value during emulation, and
  251. * any changes are written back to the saved value after emulation.
  252. */
  253. #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
  254. /* Before executing instruction: restore necessary bits in EFLAGS. */
  255. #define _PRE_EFLAGS(_sav, _msk, _tmp) \
  256. /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); */ \
  257. "push %"_sav"; " \
  258. "movl %"_msk",%"_LO32 _tmp"; " \
  259. "andl %"_LO32 _tmp",("_STK"); " \
  260. "pushf; " \
  261. "notl %"_LO32 _tmp"; " \
  262. "andl %"_LO32 _tmp",("_STK"); " \
  263. "pop %"_tmp"; " \
  264. "orl %"_LO32 _tmp",("_STK"); " \
  265. "popf; " \
  266. /* _sav &= ~msk; */ \
  267. "movl %"_msk",%"_LO32 _tmp"; " \
  268. "notl %"_LO32 _tmp"; " \
  269. "andl %"_LO32 _tmp",%"_sav"; "
  270. /* After executing instruction: write-back necessary bits in EFLAGS. */
  271. #define _POST_EFLAGS(_sav, _msk, _tmp) \
  272. /* _sav |= EFLAGS & _msk; */ \
  273. "pushf; " \
  274. "pop %"_tmp"; " \
  275. "andl %"_msk",%"_LO32 _tmp"; " \
  276. "orl %"_LO32 _tmp",%"_sav"; "
  277. /* Raw emulation: instruction has two explicit operands. */
  278. #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
  279. do { \
  280. unsigned long _tmp; \
  281. \
  282. switch ((_dst).bytes) { \
  283. case 2: \
  284. __asm__ __volatile__ ( \
  285. _PRE_EFLAGS("0","4","2") \
  286. _op"w %"_wx"3,%1; " \
  287. _POST_EFLAGS("0","4","2") \
  288. : "=m" (_eflags), "=m" ((_dst).val), \
  289. "=&r" (_tmp) \
  290. : _wy ((_src).val), "i" (EFLAGS_MASK) ); \
  291. break; \
  292. case 4: \
  293. __asm__ __volatile__ ( \
  294. _PRE_EFLAGS("0","4","2") \
  295. _op"l %"_lx"3,%1; " \
  296. _POST_EFLAGS("0","4","2") \
  297. : "=m" (_eflags), "=m" ((_dst).val), \
  298. "=&r" (_tmp) \
  299. : _ly ((_src).val), "i" (EFLAGS_MASK) ); \
  300. break; \
  301. case 8: \
  302. __emulate_2op_8byte(_op, _src, _dst, \
  303. _eflags, _qx, _qy); \
  304. break; \
  305. } \
  306. } while (0)
  307. #define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
  308. do { \
  309. unsigned long _tmp; \
  310. switch ( (_dst).bytes ) \
  311. { \
  312. case 1: \
  313. __asm__ __volatile__ ( \
  314. _PRE_EFLAGS("0","4","2") \
  315. _op"b %"_bx"3,%1; " \
  316. _POST_EFLAGS("0","4","2") \
  317. : "=m" (_eflags), "=m" ((_dst).val), \
  318. "=&r" (_tmp) \
  319. : _by ((_src).val), "i" (EFLAGS_MASK) ); \
  320. break; \
  321. default: \
  322. __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
  323. _wx, _wy, _lx, _ly, _qx, _qy); \
  324. break; \
  325. } \
  326. } while (0)
  327. /* Source operand is byte-sized and may be restricted to just %cl. */
  328. #define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
  329. __emulate_2op(_op, _src, _dst, _eflags, \
  330. "b", "c", "b", "c", "b", "c", "b", "c")
  331. /* Source operand is byte, word, long or quad sized. */
  332. #define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
  333. __emulate_2op(_op, _src, _dst, _eflags, \
  334. "b", "q", "w", "r", _LO32, "r", "", "r")
  335. /* Source operand is word, long or quad sized. */
  336. #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
  337. __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
  338. "w", "r", _LO32, "r", "", "r")
  339. /* Instruction has only one explicit operand (no source operand). */
  340. #define emulate_1op(_op, _dst, _eflags) \
  341. do { \
  342. unsigned long _tmp; \
  343. \
  344. switch ( (_dst).bytes ) \
  345. { \
  346. case 1: \
  347. __asm__ __volatile__ ( \
  348. _PRE_EFLAGS("0","3","2") \
  349. _op"b %1; " \
  350. _POST_EFLAGS("0","3","2") \
  351. : "=m" (_eflags), "=m" ((_dst).val), \
  352. "=&r" (_tmp) \
  353. : "i" (EFLAGS_MASK) ); \
  354. break; \
  355. case 2: \
  356. __asm__ __volatile__ ( \
  357. _PRE_EFLAGS("0","3","2") \
  358. _op"w %1; " \
  359. _POST_EFLAGS("0","3","2") \
  360. : "=m" (_eflags), "=m" ((_dst).val), \
  361. "=&r" (_tmp) \
  362. : "i" (EFLAGS_MASK) ); \
  363. break; \
  364. case 4: \
  365. __asm__ __volatile__ ( \
  366. _PRE_EFLAGS("0","3","2") \
  367. _op"l %1; " \
  368. _POST_EFLAGS("0","3","2") \
  369. : "=m" (_eflags), "=m" ((_dst).val), \
  370. "=&r" (_tmp) \
  371. : "i" (EFLAGS_MASK) ); \
  372. break; \
  373. case 8: \
  374. __emulate_1op_8byte(_op, _dst, _eflags); \
  375. break; \
  376. } \
  377. } while (0)
  378. /* Emulate an instruction with quadword operands (x86/64 only). */
  379. #if defined(CONFIG_X86_64)
  380. #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
  381. do { \
  382. __asm__ __volatile__ ( \
  383. _PRE_EFLAGS("0","4","2") \
  384. _op"q %"_qx"3,%1; " \
  385. _POST_EFLAGS("0","4","2") \
  386. : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
  387. : _qy ((_src).val), "i" (EFLAGS_MASK) ); \
  388. } while (0)
  389. #define __emulate_1op_8byte(_op, _dst, _eflags) \
  390. do { \
  391. __asm__ __volatile__ ( \
  392. _PRE_EFLAGS("0","3","2") \
  393. _op"q %1; " \
  394. _POST_EFLAGS("0","3","2") \
  395. : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
  396. : "i" (EFLAGS_MASK) ); \
  397. } while (0)
  398. #elif defined(__i386__)
  399. #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
  400. #define __emulate_1op_8byte(_op, _dst, _eflags)
  401. #endif /* __i386__ */
  402. /* Fetch next part of the instruction being emulated. */
  403. #define insn_fetch(_type, _size, _eip) \
  404. ({ unsigned long _x; \
  405. rc = ops->read_std((unsigned long)(_eip) + ctxt->cs_base, &_x, \
  406. (_size), ctxt->vcpu); \
  407. if ( rc != 0 ) \
  408. goto done; \
  409. (_eip) += (_size); \
  410. (_type)_x; \
  411. })
  412. /* Access/update address held in a register, based on addressing mode. */
  413. #define address_mask(reg) \
  414. ((ad_bytes == sizeof(unsigned long)) ? \
  415. (reg) : ((reg) & ((1UL << (ad_bytes << 3)) - 1)))
  416. #define register_address(base, reg) \
  417. ((base) + address_mask(reg))
  418. #define register_address_increment(reg, inc) \
  419. do { \
  420. /* signed type ensures sign extension to long */ \
  421. int _inc = (inc); \
  422. if ( ad_bytes == sizeof(unsigned long) ) \
  423. (reg) += _inc; \
  424. else \
  425. (reg) = ((reg) & ~((1UL << (ad_bytes << 3)) - 1)) | \
  426. (((reg) + _inc) & ((1UL << (ad_bytes << 3)) - 1)); \
  427. } while (0)
  428. #define JMP_REL(rel) \
  429. do { \
  430. _eip += (int)(rel); \
  431. _eip = ((op_bytes == 2) ? (uint16_t)_eip : (uint32_t)_eip); \
  432. } while (0)
  433. /*
  434. * Given the 'reg' portion of a ModRM byte, and a register block, return a
  435. * pointer into the block that addresses the relevant register.
  436. * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
  437. */
  438. static void *decode_register(u8 modrm_reg, unsigned long *regs,
  439. int highbyte_regs)
  440. {
  441. void *p;
  442. p = &regs[modrm_reg];
  443. if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
  444. p = (unsigned char *)&regs[modrm_reg & 3] + 1;
  445. return p;
  446. }
  447. static int read_descriptor(struct x86_emulate_ctxt *ctxt,
  448. struct x86_emulate_ops *ops,
  449. void *ptr,
  450. u16 *size, unsigned long *address, int op_bytes)
  451. {
  452. int rc;
  453. if (op_bytes == 2)
  454. op_bytes = 3;
  455. *address = 0;
  456. rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
  457. ctxt->vcpu);
  458. if (rc)
  459. return rc;
  460. rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
  461. ctxt->vcpu);
  462. return rc;
  463. }
  464. int
  465. x86_emulate_memop(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
  466. {
  467. unsigned d;
  468. u8 b, sib, twobyte = 0, rex_prefix = 0;
  469. u8 modrm, modrm_mod = 0, modrm_reg = 0, modrm_rm = 0;
  470. unsigned long *override_base = NULL;
  471. unsigned int op_bytes, ad_bytes, lock_prefix = 0, rep_prefix = 0, i;
  472. int rc = 0;
  473. struct operand src, dst;
  474. unsigned long cr2 = ctxt->cr2;
  475. int mode = ctxt->mode;
  476. unsigned long modrm_ea;
  477. int use_modrm_ea, index_reg = 0, base_reg = 0, scale, rip_relative = 0;
  478. int no_wb = 0;
  479. u64 msr_data;
  480. /* Shadow copy of register state. Committed on successful emulation. */
  481. unsigned long _regs[NR_VCPU_REGS];
  482. unsigned long _eip = ctxt->vcpu->rip, _eflags = ctxt->eflags;
  483. unsigned long modrm_val = 0;
  484. memcpy(_regs, ctxt->vcpu->regs, sizeof _regs);
  485. switch (mode) {
  486. case X86EMUL_MODE_REAL:
  487. case X86EMUL_MODE_PROT16:
  488. op_bytes = ad_bytes = 2;
  489. break;
  490. case X86EMUL_MODE_PROT32:
  491. op_bytes = ad_bytes = 4;
  492. break;
  493. #ifdef CONFIG_X86_64
  494. case X86EMUL_MODE_PROT64:
  495. op_bytes = 4;
  496. ad_bytes = 8;
  497. break;
  498. #endif
  499. default:
  500. return -1;
  501. }
  502. /* Legacy prefixes. */
  503. for (i = 0; i < 8; i++) {
  504. switch (b = insn_fetch(u8, 1, _eip)) {
  505. case 0x66: /* operand-size override */
  506. op_bytes ^= 6; /* switch between 2/4 bytes */
  507. break;
  508. case 0x67: /* address-size override */
  509. if (mode == X86EMUL_MODE_PROT64)
  510. ad_bytes ^= 12; /* switch between 4/8 bytes */
  511. else
  512. ad_bytes ^= 6; /* switch between 2/4 bytes */
  513. break;
  514. case 0x2e: /* CS override */
  515. override_base = &ctxt->cs_base;
  516. break;
  517. case 0x3e: /* DS override */
  518. override_base = &ctxt->ds_base;
  519. break;
  520. case 0x26: /* ES override */
  521. override_base = &ctxt->es_base;
  522. break;
  523. case 0x64: /* FS override */
  524. override_base = &ctxt->fs_base;
  525. break;
  526. case 0x65: /* GS override */
  527. override_base = &ctxt->gs_base;
  528. break;
  529. case 0x36: /* SS override */
  530. override_base = &ctxt->ss_base;
  531. break;
  532. case 0xf0: /* LOCK */
  533. lock_prefix = 1;
  534. break;
  535. case 0xf3: /* REP/REPE/REPZ */
  536. rep_prefix = 1;
  537. break;
  538. case 0xf2: /* REPNE/REPNZ */
  539. break;
  540. default:
  541. goto done_prefixes;
  542. }
  543. }
  544. done_prefixes:
  545. /* REX prefix. */
  546. if ((mode == X86EMUL_MODE_PROT64) && ((b & 0xf0) == 0x40)) {
  547. rex_prefix = b;
  548. if (b & 8)
  549. op_bytes = 8; /* REX.W */
  550. modrm_reg = (b & 4) << 1; /* REX.R */
  551. index_reg = (b & 2) << 2; /* REX.X */
  552. modrm_rm = base_reg = (b & 1) << 3; /* REG.B */
  553. b = insn_fetch(u8, 1, _eip);
  554. }
  555. /* Opcode byte(s). */
  556. d = opcode_table[b];
  557. if (d == 0) {
  558. /* Two-byte opcode? */
  559. if (b == 0x0f) {
  560. twobyte = 1;
  561. b = insn_fetch(u8, 1, _eip);
  562. d = twobyte_table[b];
  563. }
  564. /* Unrecognised? */
  565. if (d == 0)
  566. goto cannot_emulate;
  567. }
  568. /* ModRM and SIB bytes. */
  569. if (d & ModRM) {
  570. modrm = insn_fetch(u8, 1, _eip);
  571. modrm_mod |= (modrm & 0xc0) >> 6;
  572. modrm_reg |= (modrm & 0x38) >> 3;
  573. modrm_rm |= (modrm & 0x07);
  574. modrm_ea = 0;
  575. use_modrm_ea = 1;
  576. if (modrm_mod == 3) {
  577. modrm_val = *(unsigned long *)
  578. decode_register(modrm_rm, _regs, d & ByteOp);
  579. goto modrm_done;
  580. }
  581. if (ad_bytes == 2) {
  582. unsigned bx = _regs[VCPU_REGS_RBX];
  583. unsigned bp = _regs[VCPU_REGS_RBP];
  584. unsigned si = _regs[VCPU_REGS_RSI];
  585. unsigned di = _regs[VCPU_REGS_RDI];
  586. /* 16-bit ModR/M decode. */
  587. switch (modrm_mod) {
  588. case 0:
  589. if (modrm_rm == 6)
  590. modrm_ea += insn_fetch(u16, 2, _eip);
  591. break;
  592. case 1:
  593. modrm_ea += insn_fetch(s8, 1, _eip);
  594. break;
  595. case 2:
  596. modrm_ea += insn_fetch(u16, 2, _eip);
  597. break;
  598. }
  599. switch (modrm_rm) {
  600. case 0:
  601. modrm_ea += bx + si;
  602. break;
  603. case 1:
  604. modrm_ea += bx + di;
  605. break;
  606. case 2:
  607. modrm_ea += bp + si;
  608. break;
  609. case 3:
  610. modrm_ea += bp + di;
  611. break;
  612. case 4:
  613. modrm_ea += si;
  614. break;
  615. case 5:
  616. modrm_ea += di;
  617. break;
  618. case 6:
  619. if (modrm_mod != 0)
  620. modrm_ea += bp;
  621. break;
  622. case 7:
  623. modrm_ea += bx;
  624. break;
  625. }
  626. if (modrm_rm == 2 || modrm_rm == 3 ||
  627. (modrm_rm == 6 && modrm_mod != 0))
  628. if (!override_base)
  629. override_base = &ctxt->ss_base;
  630. modrm_ea = (u16)modrm_ea;
  631. } else {
  632. /* 32/64-bit ModR/M decode. */
  633. switch (modrm_rm) {
  634. case 4:
  635. case 12:
  636. sib = insn_fetch(u8, 1, _eip);
  637. index_reg |= (sib >> 3) & 7;
  638. base_reg |= sib & 7;
  639. scale = sib >> 6;
  640. switch (base_reg) {
  641. case 5:
  642. if (modrm_mod != 0)
  643. modrm_ea += _regs[base_reg];
  644. else
  645. modrm_ea += insn_fetch(s32, 4, _eip);
  646. break;
  647. default:
  648. modrm_ea += _regs[base_reg];
  649. }
  650. switch (index_reg) {
  651. case 4:
  652. break;
  653. default:
  654. modrm_ea += _regs[index_reg] << scale;
  655. }
  656. break;
  657. case 5:
  658. if (modrm_mod != 0)
  659. modrm_ea += _regs[modrm_rm];
  660. else if (mode == X86EMUL_MODE_PROT64)
  661. rip_relative = 1;
  662. break;
  663. default:
  664. modrm_ea += _regs[modrm_rm];
  665. break;
  666. }
  667. switch (modrm_mod) {
  668. case 0:
  669. if (modrm_rm == 5)
  670. modrm_ea += insn_fetch(s32, 4, _eip);
  671. break;
  672. case 1:
  673. modrm_ea += insn_fetch(s8, 1, _eip);
  674. break;
  675. case 2:
  676. modrm_ea += insn_fetch(s32, 4, _eip);
  677. break;
  678. }
  679. }
  680. if (!override_base)
  681. override_base = &ctxt->ds_base;
  682. if (mode == X86EMUL_MODE_PROT64 &&
  683. override_base != &ctxt->fs_base &&
  684. override_base != &ctxt->gs_base)
  685. override_base = NULL;
  686. if (override_base)
  687. modrm_ea += *override_base;
  688. if (rip_relative) {
  689. modrm_ea += _eip;
  690. switch (d & SrcMask) {
  691. case SrcImmByte:
  692. modrm_ea += 1;
  693. break;
  694. case SrcImm:
  695. if (d & ByteOp)
  696. modrm_ea += 1;
  697. else
  698. if (op_bytes == 8)
  699. modrm_ea += 4;
  700. else
  701. modrm_ea += op_bytes;
  702. }
  703. }
  704. if (ad_bytes != 8)
  705. modrm_ea = (u32)modrm_ea;
  706. cr2 = modrm_ea;
  707. modrm_done:
  708. ;
  709. }
  710. /*
  711. * Decode and fetch the source operand: register, memory
  712. * or immediate.
  713. */
  714. switch (d & SrcMask) {
  715. case SrcNone:
  716. break;
  717. case SrcReg:
  718. src.type = OP_REG;
  719. if (d & ByteOp) {
  720. src.ptr = decode_register(modrm_reg, _regs,
  721. (rex_prefix == 0));
  722. src.val = src.orig_val = *(u8 *) src.ptr;
  723. src.bytes = 1;
  724. } else {
  725. src.ptr = decode_register(modrm_reg, _regs, 0);
  726. switch ((src.bytes = op_bytes)) {
  727. case 2:
  728. src.val = src.orig_val = *(u16 *) src.ptr;
  729. break;
  730. case 4:
  731. src.val = src.orig_val = *(u32 *) src.ptr;
  732. break;
  733. case 8:
  734. src.val = src.orig_val = *(u64 *) src.ptr;
  735. break;
  736. }
  737. }
  738. break;
  739. case SrcMem16:
  740. src.bytes = 2;
  741. goto srcmem_common;
  742. case SrcMem32:
  743. src.bytes = 4;
  744. goto srcmem_common;
  745. case SrcMem:
  746. src.bytes = (d & ByteOp) ? 1 : op_bytes;
  747. srcmem_common:
  748. src.type = OP_MEM;
  749. src.ptr = (unsigned long *)cr2;
  750. if ((rc = ops->read_emulated((unsigned long)src.ptr,
  751. &src.val, src.bytes, ctxt->vcpu)) != 0)
  752. goto done;
  753. src.orig_val = src.val;
  754. break;
  755. case SrcImm:
  756. src.type = OP_IMM;
  757. src.ptr = (unsigned long *)_eip;
  758. src.bytes = (d & ByteOp) ? 1 : op_bytes;
  759. if (src.bytes == 8)
  760. src.bytes = 4;
  761. /* NB. Immediates are sign-extended as necessary. */
  762. switch (src.bytes) {
  763. case 1:
  764. src.val = insn_fetch(s8, 1, _eip);
  765. break;
  766. case 2:
  767. src.val = insn_fetch(s16, 2, _eip);
  768. break;
  769. case 4:
  770. src.val = insn_fetch(s32, 4, _eip);
  771. break;
  772. }
  773. break;
  774. case SrcImmByte:
  775. src.type = OP_IMM;
  776. src.ptr = (unsigned long *)_eip;
  777. src.bytes = 1;
  778. src.val = insn_fetch(s8, 1, _eip);
  779. break;
  780. }
  781. /* Decode and fetch the destination operand: register or memory. */
  782. switch (d & DstMask) {
  783. case ImplicitOps:
  784. /* Special instructions do their own operand decoding. */
  785. goto special_insn;
  786. case DstReg:
  787. dst.type = OP_REG;
  788. if ((d & ByteOp)
  789. && !(twobyte && (b == 0xb6 || b == 0xb7))) {
  790. dst.ptr = decode_register(modrm_reg, _regs,
  791. (rex_prefix == 0));
  792. dst.val = *(u8 *) dst.ptr;
  793. dst.bytes = 1;
  794. } else {
  795. dst.ptr = decode_register(modrm_reg, _regs, 0);
  796. switch ((dst.bytes = op_bytes)) {
  797. case 2:
  798. dst.val = *(u16 *)dst.ptr;
  799. break;
  800. case 4:
  801. dst.val = *(u32 *)dst.ptr;
  802. break;
  803. case 8:
  804. dst.val = *(u64 *)dst.ptr;
  805. break;
  806. }
  807. }
  808. break;
  809. case DstMem:
  810. dst.type = OP_MEM;
  811. dst.ptr = (unsigned long *)cr2;
  812. dst.bytes = (d & ByteOp) ? 1 : op_bytes;
  813. if (d & BitOp) {
  814. unsigned long mask = ~(dst.bytes * 8 - 1);
  815. dst.ptr = (void *)dst.ptr + (src.val & mask) / 8;
  816. }
  817. if (!(d & Mov) && /* optimisation - avoid slow emulated read */
  818. ((rc = ops->read_emulated((unsigned long)dst.ptr,
  819. &dst.val, dst.bytes, ctxt->vcpu)) != 0))
  820. goto done;
  821. break;
  822. }
  823. dst.orig_val = dst.val;
  824. if (twobyte)
  825. goto twobyte_insn;
  826. switch (b) {
  827. case 0x00 ... 0x05:
  828. add: /* add */
  829. emulate_2op_SrcV("add", src, dst, _eflags);
  830. break;
  831. case 0x08 ... 0x0d:
  832. or: /* or */
  833. emulate_2op_SrcV("or", src, dst, _eflags);
  834. break;
  835. case 0x10 ... 0x15:
  836. adc: /* adc */
  837. emulate_2op_SrcV("adc", src, dst, _eflags);
  838. break;
  839. case 0x18 ... 0x1d:
  840. sbb: /* sbb */
  841. emulate_2op_SrcV("sbb", src, dst, _eflags);
  842. break;
  843. case 0x20 ... 0x23:
  844. and: /* and */
  845. emulate_2op_SrcV("and", src, dst, _eflags);
  846. break;
  847. case 0x24: /* and al imm8 */
  848. dst.type = OP_REG;
  849. dst.ptr = &_regs[VCPU_REGS_RAX];
  850. dst.val = *(u8 *)dst.ptr;
  851. dst.bytes = 1;
  852. dst.orig_val = dst.val;
  853. goto and;
  854. case 0x25: /* and ax imm16, or eax imm32 */
  855. dst.type = OP_REG;
  856. dst.bytes = op_bytes;
  857. dst.ptr = &_regs[VCPU_REGS_RAX];
  858. if (op_bytes == 2)
  859. dst.val = *(u16 *)dst.ptr;
  860. else
  861. dst.val = *(u32 *)dst.ptr;
  862. dst.orig_val = dst.val;
  863. goto and;
  864. case 0x28 ... 0x2d:
  865. sub: /* sub */
  866. emulate_2op_SrcV("sub", src, dst, _eflags);
  867. break;
  868. case 0x30 ... 0x35:
  869. xor: /* xor */
  870. emulate_2op_SrcV("xor", src, dst, _eflags);
  871. break;
  872. case 0x38 ... 0x3d:
  873. cmp: /* cmp */
  874. emulate_2op_SrcV("cmp", src, dst, _eflags);
  875. break;
  876. case 0x63: /* movsxd */
  877. if (mode != X86EMUL_MODE_PROT64)
  878. goto cannot_emulate;
  879. dst.val = (s32) src.val;
  880. break;
  881. case 0x80 ... 0x83: /* Grp1 */
  882. switch (modrm_reg) {
  883. case 0:
  884. goto add;
  885. case 1:
  886. goto or;
  887. case 2:
  888. goto adc;
  889. case 3:
  890. goto sbb;
  891. case 4:
  892. goto and;
  893. case 5:
  894. goto sub;
  895. case 6:
  896. goto xor;
  897. case 7:
  898. goto cmp;
  899. }
  900. break;
  901. case 0x84 ... 0x85:
  902. test: /* test */
  903. emulate_2op_SrcV("test", src, dst, _eflags);
  904. break;
  905. case 0x86 ... 0x87: /* xchg */
  906. /* Write back the register source. */
  907. switch (dst.bytes) {
  908. case 1:
  909. *(u8 *) src.ptr = (u8) dst.val;
  910. break;
  911. case 2:
  912. *(u16 *) src.ptr = (u16) dst.val;
  913. break;
  914. case 4:
  915. *src.ptr = (u32) dst.val;
  916. break; /* 64b reg: zero-extend */
  917. case 8:
  918. *src.ptr = dst.val;
  919. break;
  920. }
  921. /*
  922. * Write back the memory destination with implicit LOCK
  923. * prefix.
  924. */
  925. dst.val = src.val;
  926. lock_prefix = 1;
  927. break;
  928. case 0xa0 ... 0xa1: /* mov */
  929. dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
  930. dst.val = src.val;
  931. _eip += ad_bytes; /* skip src displacement */
  932. break;
  933. case 0xa2 ... 0xa3: /* mov */
  934. dst.val = (unsigned long)_regs[VCPU_REGS_RAX];
  935. _eip += ad_bytes; /* skip dst displacement */
  936. break;
  937. case 0x88 ... 0x8b: /* mov */
  938. case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
  939. dst.val = src.val;
  940. break;
  941. case 0x8f: /* pop (sole member of Grp1a) */
  942. /* 64-bit mode: POP always pops a 64-bit operand. */
  943. if (mode == X86EMUL_MODE_PROT64)
  944. dst.bytes = 8;
  945. if ((rc = ops->read_std(register_address(ctxt->ss_base,
  946. _regs[VCPU_REGS_RSP]),
  947. &dst.val, dst.bytes, ctxt->vcpu)) != 0)
  948. goto done;
  949. register_address_increment(_regs[VCPU_REGS_RSP], dst.bytes);
  950. break;
  951. case 0xc0 ... 0xc1:
  952. grp2: /* Grp2 */
  953. switch (modrm_reg) {
  954. case 0: /* rol */
  955. emulate_2op_SrcB("rol", src, dst, _eflags);
  956. break;
  957. case 1: /* ror */
  958. emulate_2op_SrcB("ror", src, dst, _eflags);
  959. break;
  960. case 2: /* rcl */
  961. emulate_2op_SrcB("rcl", src, dst, _eflags);
  962. break;
  963. case 3: /* rcr */
  964. emulate_2op_SrcB("rcr", src, dst, _eflags);
  965. break;
  966. case 4: /* sal/shl */
  967. case 6: /* sal/shl */
  968. emulate_2op_SrcB("sal", src, dst, _eflags);
  969. break;
  970. case 5: /* shr */
  971. emulate_2op_SrcB("shr", src, dst, _eflags);
  972. break;
  973. case 7: /* sar */
  974. emulate_2op_SrcB("sar", src, dst, _eflags);
  975. break;
  976. }
  977. break;
  978. case 0xd0 ... 0xd1: /* Grp2 */
  979. src.val = 1;
  980. goto grp2;
  981. case 0xd2 ... 0xd3: /* Grp2 */
  982. src.val = _regs[VCPU_REGS_RCX];
  983. goto grp2;
  984. case 0xe9: /* jmp rel */
  985. case 0xeb: /* jmp rel short */
  986. JMP_REL(src.val);
  987. no_wb = 1; /* Disable writeback. */
  988. break;
  989. case 0xf6 ... 0xf7: /* Grp3 */
  990. switch (modrm_reg) {
  991. case 0 ... 1: /* test */
  992. /*
  993. * Special case in Grp3: test has an immediate
  994. * source operand.
  995. */
  996. src.type = OP_IMM;
  997. src.ptr = (unsigned long *)_eip;
  998. src.bytes = (d & ByteOp) ? 1 : op_bytes;
  999. if (src.bytes == 8)
  1000. src.bytes = 4;
  1001. switch (src.bytes) {
  1002. case 1:
  1003. src.val = insn_fetch(s8, 1, _eip);
  1004. break;
  1005. case 2:
  1006. src.val = insn_fetch(s16, 2, _eip);
  1007. break;
  1008. case 4:
  1009. src.val = insn_fetch(s32, 4, _eip);
  1010. break;
  1011. }
  1012. goto test;
  1013. case 2: /* not */
  1014. dst.val = ~dst.val;
  1015. break;
  1016. case 3: /* neg */
  1017. emulate_1op("neg", dst, _eflags);
  1018. break;
  1019. default:
  1020. goto cannot_emulate;
  1021. }
  1022. break;
  1023. case 0xfe ... 0xff: /* Grp4/Grp5 */
  1024. switch (modrm_reg) {
  1025. case 0: /* inc */
  1026. emulate_1op("inc", dst, _eflags);
  1027. break;
  1028. case 1: /* dec */
  1029. emulate_1op("dec", dst, _eflags);
  1030. break;
  1031. case 6: /* push */
  1032. /* 64-bit mode: PUSH always pushes a 64-bit operand. */
  1033. if (mode == X86EMUL_MODE_PROT64) {
  1034. dst.bytes = 8;
  1035. if ((rc = ops->read_std((unsigned long)dst.ptr,
  1036. &dst.val, 8,
  1037. ctxt->vcpu)) != 0)
  1038. goto done;
  1039. }
  1040. register_address_increment(_regs[VCPU_REGS_RSP],
  1041. -dst.bytes);
  1042. if ((rc = ops->write_std(
  1043. register_address(ctxt->ss_base,
  1044. _regs[VCPU_REGS_RSP]),
  1045. &dst.val, dst.bytes, ctxt->vcpu)) != 0)
  1046. goto done;
  1047. no_wb = 1;
  1048. break;
  1049. default:
  1050. goto cannot_emulate;
  1051. }
  1052. break;
  1053. }
  1054. writeback:
  1055. if (!no_wb) {
  1056. switch (dst.type) {
  1057. case OP_REG:
  1058. /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
  1059. switch (dst.bytes) {
  1060. case 1:
  1061. *(u8 *)dst.ptr = (u8)dst.val;
  1062. break;
  1063. case 2:
  1064. *(u16 *)dst.ptr = (u16)dst.val;
  1065. break;
  1066. case 4:
  1067. *dst.ptr = (u32)dst.val;
  1068. break; /* 64b: zero-ext */
  1069. case 8:
  1070. *dst.ptr = dst.val;
  1071. break;
  1072. }
  1073. break;
  1074. case OP_MEM:
  1075. if (lock_prefix)
  1076. rc = ops->cmpxchg_emulated((unsigned long)dst.
  1077. ptr, &dst.orig_val,
  1078. &dst.val, dst.bytes,
  1079. ctxt->vcpu);
  1080. else
  1081. rc = ops->write_emulated((unsigned long)dst.ptr,
  1082. &dst.val, dst.bytes,
  1083. ctxt->vcpu);
  1084. if (rc != 0)
  1085. goto done;
  1086. default:
  1087. break;
  1088. }
  1089. }
  1090. /* Commit shadow register state. */
  1091. memcpy(ctxt->vcpu->regs, _regs, sizeof _regs);
  1092. ctxt->eflags = _eflags;
  1093. ctxt->vcpu->rip = _eip;
  1094. done:
  1095. return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
  1096. special_insn:
  1097. if (twobyte)
  1098. goto twobyte_special_insn;
  1099. switch(b) {
  1100. case 0x50 ... 0x57: /* push reg */
  1101. if (op_bytes == 2)
  1102. src.val = (u16) _regs[b & 0x7];
  1103. else
  1104. src.val = (u32) _regs[b & 0x7];
  1105. dst.type = OP_MEM;
  1106. dst.bytes = op_bytes;
  1107. dst.val = src.val;
  1108. register_address_increment(_regs[VCPU_REGS_RSP], -op_bytes);
  1109. dst.ptr = (void *) register_address(
  1110. ctxt->ss_base, _regs[VCPU_REGS_RSP]);
  1111. break;
  1112. case 0x6c: /* insb */
  1113. case 0x6d: /* insw/insd */
  1114. if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
  1115. 1, /* in */
  1116. (d & ByteOp) ? 1 : op_bytes, /* size */
  1117. rep_prefix ?
  1118. address_mask(_regs[VCPU_REGS_RCX]) : 1, /* count */
  1119. (_eflags & EFLG_DF), /* down */
  1120. register_address(ctxt->es_base,
  1121. _regs[VCPU_REGS_RDI]), /* address */
  1122. rep_prefix,
  1123. _regs[VCPU_REGS_RDX] /* port */
  1124. ) == 0)
  1125. return -1;
  1126. return 0;
  1127. case 0x6e: /* outsb */
  1128. case 0x6f: /* outsw/outsd */
  1129. if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
  1130. 0, /* in */
  1131. (d & ByteOp) ? 1 : op_bytes, /* size */
  1132. rep_prefix ?
  1133. address_mask(_regs[VCPU_REGS_RCX]) : 1, /* count */
  1134. (_eflags & EFLG_DF), /* down */
  1135. register_address(override_base ?
  1136. *override_base : ctxt->ds_base,
  1137. _regs[VCPU_REGS_RSI]), /* address */
  1138. rep_prefix,
  1139. _regs[VCPU_REGS_RDX] /* port */
  1140. ) == 0)
  1141. return -1;
  1142. return 0;
  1143. }
  1144. if (rep_prefix) {
  1145. if (_regs[VCPU_REGS_RCX] == 0) {
  1146. ctxt->vcpu->rip = _eip;
  1147. goto done;
  1148. }
  1149. _regs[VCPU_REGS_RCX]--;
  1150. _eip = ctxt->vcpu->rip;
  1151. }
  1152. switch (b) {
  1153. case 0xa4 ... 0xa5: /* movs */
  1154. dst.type = OP_MEM;
  1155. dst.bytes = (d & ByteOp) ? 1 : op_bytes;
  1156. dst.ptr = (unsigned long *)register_address(ctxt->es_base,
  1157. _regs[VCPU_REGS_RDI]);
  1158. if ((rc = ops->read_emulated(register_address(
  1159. override_base ? *override_base : ctxt->ds_base,
  1160. _regs[VCPU_REGS_RSI]), &dst.val, dst.bytes, ctxt->vcpu)) != 0)
  1161. goto done;
  1162. register_address_increment(_regs[VCPU_REGS_RSI],
  1163. (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
  1164. register_address_increment(_regs[VCPU_REGS_RDI],
  1165. (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
  1166. break;
  1167. case 0xa6 ... 0xa7: /* cmps */
  1168. DPRINTF("Urk! I don't handle CMPS.\n");
  1169. goto cannot_emulate;
  1170. case 0xaa ... 0xab: /* stos */
  1171. dst.type = OP_MEM;
  1172. dst.bytes = (d & ByteOp) ? 1 : op_bytes;
  1173. dst.ptr = (unsigned long *)cr2;
  1174. dst.val = _regs[VCPU_REGS_RAX];
  1175. register_address_increment(_regs[VCPU_REGS_RDI],
  1176. (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
  1177. break;
  1178. case 0xac ... 0xad: /* lods */
  1179. dst.type = OP_REG;
  1180. dst.bytes = (d & ByteOp) ? 1 : op_bytes;
  1181. dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
  1182. if ((rc = ops->read_emulated(cr2, &dst.val, dst.bytes,
  1183. ctxt->vcpu)) != 0)
  1184. goto done;
  1185. register_address_increment(_regs[VCPU_REGS_RSI],
  1186. (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
  1187. break;
  1188. case 0xae ... 0xaf: /* scas */
  1189. DPRINTF("Urk! I don't handle SCAS.\n");
  1190. goto cannot_emulate;
  1191. case 0xf4: /* hlt */
  1192. ctxt->vcpu->halt_request = 1;
  1193. goto done;
  1194. case 0xc3: /* ret */
  1195. dst.ptr = &_eip;
  1196. goto pop_instruction;
  1197. case 0x58 ... 0x5f: /* pop reg */
  1198. dst.ptr = (unsigned long *)&_regs[b & 0x7];
  1199. pop_instruction:
  1200. if ((rc = ops->read_std(register_address(ctxt->ss_base,
  1201. _regs[VCPU_REGS_RSP]), dst.ptr, op_bytes, ctxt->vcpu))
  1202. != 0)
  1203. goto done;
  1204. register_address_increment(_regs[VCPU_REGS_RSP], op_bytes);
  1205. no_wb = 1; /* Disable writeback. */
  1206. break;
  1207. }
  1208. goto writeback;
  1209. twobyte_insn:
  1210. switch (b) {
  1211. case 0x01: /* lgdt, lidt, lmsw */
  1212. /* Disable writeback. */
  1213. no_wb = 1;
  1214. switch (modrm_reg) {
  1215. u16 size;
  1216. unsigned long address;
  1217. case 2: /* lgdt */
  1218. rc = read_descriptor(ctxt, ops, src.ptr,
  1219. &size, &address, op_bytes);
  1220. if (rc)
  1221. goto done;
  1222. realmode_lgdt(ctxt->vcpu, size, address);
  1223. break;
  1224. case 3: /* lidt */
  1225. rc = read_descriptor(ctxt, ops, src.ptr,
  1226. &size, &address, op_bytes);
  1227. if (rc)
  1228. goto done;
  1229. realmode_lidt(ctxt->vcpu, size, address);
  1230. break;
  1231. case 4: /* smsw */
  1232. if (modrm_mod != 3)
  1233. goto cannot_emulate;
  1234. *(u16 *)&_regs[modrm_rm]
  1235. = realmode_get_cr(ctxt->vcpu, 0);
  1236. break;
  1237. case 6: /* lmsw */
  1238. if (modrm_mod != 3)
  1239. goto cannot_emulate;
  1240. realmode_lmsw(ctxt->vcpu, (u16)modrm_val, &_eflags);
  1241. break;
  1242. case 7: /* invlpg*/
  1243. emulate_invlpg(ctxt->vcpu, cr2);
  1244. break;
  1245. default:
  1246. goto cannot_emulate;
  1247. }
  1248. break;
  1249. case 0x21: /* mov from dr to reg */
  1250. no_wb = 1;
  1251. if (modrm_mod != 3)
  1252. goto cannot_emulate;
  1253. rc = emulator_get_dr(ctxt, modrm_reg, &_regs[modrm_rm]);
  1254. break;
  1255. case 0x23: /* mov from reg to dr */
  1256. no_wb = 1;
  1257. if (modrm_mod != 3)
  1258. goto cannot_emulate;
  1259. rc = emulator_set_dr(ctxt, modrm_reg, _regs[modrm_rm]);
  1260. break;
  1261. case 0x40 ... 0x4f: /* cmov */
  1262. dst.val = dst.orig_val = src.val;
  1263. no_wb = 1;
  1264. /*
  1265. * First, assume we're decoding an even cmov opcode
  1266. * (lsb == 0).
  1267. */
  1268. switch ((b & 15) >> 1) {
  1269. case 0: /* cmovo */
  1270. no_wb = (_eflags & EFLG_OF) ? 0 : 1;
  1271. break;
  1272. case 1: /* cmovb/cmovc/cmovnae */
  1273. no_wb = (_eflags & EFLG_CF) ? 0 : 1;
  1274. break;
  1275. case 2: /* cmovz/cmove */
  1276. no_wb = (_eflags & EFLG_ZF) ? 0 : 1;
  1277. break;
  1278. case 3: /* cmovbe/cmovna */
  1279. no_wb = (_eflags & (EFLG_CF | EFLG_ZF)) ? 0 : 1;
  1280. break;
  1281. case 4: /* cmovs */
  1282. no_wb = (_eflags & EFLG_SF) ? 0 : 1;
  1283. break;
  1284. case 5: /* cmovp/cmovpe */
  1285. no_wb = (_eflags & EFLG_PF) ? 0 : 1;
  1286. break;
  1287. case 7: /* cmovle/cmovng */
  1288. no_wb = (_eflags & EFLG_ZF) ? 0 : 1;
  1289. /* fall through */
  1290. case 6: /* cmovl/cmovnge */
  1291. no_wb &= (!(_eflags & EFLG_SF) !=
  1292. !(_eflags & EFLG_OF)) ? 0 : 1;
  1293. break;
  1294. }
  1295. /* Odd cmov opcodes (lsb == 1) have inverted sense. */
  1296. no_wb ^= b & 1;
  1297. break;
  1298. case 0xb0 ... 0xb1: /* cmpxchg */
  1299. /*
  1300. * Save real source value, then compare EAX against
  1301. * destination.
  1302. */
  1303. src.orig_val = src.val;
  1304. src.val = _regs[VCPU_REGS_RAX];
  1305. emulate_2op_SrcV("cmp", src, dst, _eflags);
  1306. if (_eflags & EFLG_ZF) {
  1307. /* Success: write back to memory. */
  1308. dst.val = src.orig_val;
  1309. } else {
  1310. /* Failure: write the value we saw to EAX. */
  1311. dst.type = OP_REG;
  1312. dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
  1313. }
  1314. break;
  1315. case 0xa3:
  1316. bt: /* bt */
  1317. src.val &= (dst.bytes << 3) - 1; /* only subword offset */
  1318. emulate_2op_SrcV_nobyte("bt", src, dst, _eflags);
  1319. break;
  1320. case 0xb3:
  1321. btr: /* btr */
  1322. src.val &= (dst.bytes << 3) - 1; /* only subword offset */
  1323. emulate_2op_SrcV_nobyte("btr", src, dst, _eflags);
  1324. break;
  1325. case 0xab:
  1326. bts: /* bts */
  1327. src.val &= (dst.bytes << 3) - 1; /* only subword offset */
  1328. emulate_2op_SrcV_nobyte("bts", src, dst, _eflags);
  1329. break;
  1330. case 0xb6 ... 0xb7: /* movzx */
  1331. dst.bytes = op_bytes;
  1332. dst.val = (d & ByteOp) ? (u8) src.val : (u16) src.val;
  1333. break;
  1334. case 0xbb:
  1335. btc: /* btc */
  1336. src.val &= (dst.bytes << 3) - 1; /* only subword offset */
  1337. emulate_2op_SrcV_nobyte("btc", src, dst, _eflags);
  1338. break;
  1339. case 0xba: /* Grp8 */
  1340. switch (modrm_reg & 3) {
  1341. case 0:
  1342. goto bt;
  1343. case 1:
  1344. goto bts;
  1345. case 2:
  1346. goto btr;
  1347. case 3:
  1348. goto btc;
  1349. }
  1350. break;
  1351. case 0xbe ... 0xbf: /* movsx */
  1352. dst.bytes = op_bytes;
  1353. dst.val = (d & ByteOp) ? (s8) src.val : (s16) src.val;
  1354. break;
  1355. }
  1356. goto writeback;
  1357. twobyte_special_insn:
  1358. /* Disable writeback. */
  1359. no_wb = 1;
  1360. switch (b) {
  1361. case 0x09: /* wbinvd */
  1362. break;
  1363. case 0x0d: /* GrpP (prefetch) */
  1364. case 0x18: /* Grp16 (prefetch/nop) */
  1365. break;
  1366. case 0x06:
  1367. emulate_clts(ctxt->vcpu);
  1368. break;
  1369. case 0x20: /* mov cr, reg */
  1370. if (modrm_mod != 3)
  1371. goto cannot_emulate;
  1372. _regs[modrm_rm] = realmode_get_cr(ctxt->vcpu, modrm_reg);
  1373. break;
  1374. case 0x22: /* mov reg, cr */
  1375. if (modrm_mod != 3)
  1376. goto cannot_emulate;
  1377. realmode_set_cr(ctxt->vcpu, modrm_reg, modrm_val, &_eflags);
  1378. break;
  1379. case 0x30:
  1380. /* wrmsr */
  1381. msr_data = (u32)_regs[VCPU_REGS_RAX]
  1382. | ((u64)_regs[VCPU_REGS_RDX] << 32);
  1383. rc = kvm_set_msr(ctxt->vcpu, _regs[VCPU_REGS_RCX], msr_data);
  1384. if (rc) {
  1385. kvm_arch_ops->inject_gp(ctxt->vcpu, 0);
  1386. _eip = ctxt->vcpu->rip;
  1387. }
  1388. rc = X86EMUL_CONTINUE;
  1389. break;
  1390. case 0x32:
  1391. /* rdmsr */
  1392. rc = kvm_get_msr(ctxt->vcpu, _regs[VCPU_REGS_RCX], &msr_data);
  1393. if (rc) {
  1394. kvm_arch_ops->inject_gp(ctxt->vcpu, 0);
  1395. _eip = ctxt->vcpu->rip;
  1396. } else {
  1397. _regs[VCPU_REGS_RAX] = (u32)msr_data;
  1398. _regs[VCPU_REGS_RDX] = msr_data >> 32;
  1399. }
  1400. rc = X86EMUL_CONTINUE;
  1401. break;
  1402. case 0xc7: /* Grp9 (cmpxchg8b) */
  1403. {
  1404. u64 old, new;
  1405. if ((rc = ops->read_emulated(cr2, &old, 8, ctxt->vcpu))
  1406. != 0)
  1407. goto done;
  1408. if (((u32) (old >> 0) != (u32) _regs[VCPU_REGS_RAX]) ||
  1409. ((u32) (old >> 32) != (u32) _regs[VCPU_REGS_RDX])) {
  1410. _regs[VCPU_REGS_RAX] = (u32) (old >> 0);
  1411. _regs[VCPU_REGS_RDX] = (u32) (old >> 32);
  1412. _eflags &= ~EFLG_ZF;
  1413. } else {
  1414. new = ((u64)_regs[VCPU_REGS_RCX] << 32)
  1415. | (u32) _regs[VCPU_REGS_RBX];
  1416. if ((rc = ops->cmpxchg_emulated(cr2, &old,
  1417. &new, 8, ctxt->vcpu)) != 0)
  1418. goto done;
  1419. _eflags |= EFLG_ZF;
  1420. }
  1421. break;
  1422. }
  1423. }
  1424. goto writeback;
  1425. cannot_emulate:
  1426. DPRINTF("Cannot emulate %02x\n", b);
  1427. return -1;
  1428. }
  1429. #ifdef __XEN__
  1430. #include <asm/mm.h>
  1431. #include <asm/uaccess.h>
  1432. int
  1433. x86_emulate_read_std(unsigned long addr,
  1434. unsigned long *val,
  1435. unsigned int bytes, struct x86_emulate_ctxt *ctxt)
  1436. {
  1437. unsigned int rc;
  1438. *val = 0;
  1439. if ((rc = copy_from_user((void *)val, (void *)addr, bytes)) != 0) {
  1440. propagate_page_fault(addr + bytes - rc, 0); /* read fault */
  1441. return X86EMUL_PROPAGATE_FAULT;
  1442. }
  1443. return X86EMUL_CONTINUE;
  1444. }
  1445. int
  1446. x86_emulate_write_std(unsigned long addr,
  1447. unsigned long val,
  1448. unsigned int bytes, struct x86_emulate_ctxt *ctxt)
  1449. {
  1450. unsigned int rc;
  1451. if ((rc = copy_to_user((void *)addr, (void *)&val, bytes)) != 0) {
  1452. propagate_page_fault(addr + bytes - rc, PGERR_write_access);
  1453. return X86EMUL_PROPAGATE_FAULT;
  1454. }
  1455. return X86EMUL_CONTINUE;
  1456. }
  1457. #endif