x86_emulate.c 52 KB

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