x86_emulate.c 48 KB

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