x86_emulate.c 51 KB

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