x86_emulate.c 38 KB

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