x86_emulate.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953
  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_ptr = decode_register(c->modrm_rm,
  635. c->regs, c->d & ByteOp);
  636. c->modrm_val = *(unsigned long *)c->modrm_ptr;
  637. return rc;
  638. }
  639. if (c->ad_bytes == 2) {
  640. unsigned bx = c->regs[VCPU_REGS_RBX];
  641. unsigned bp = c->regs[VCPU_REGS_RBP];
  642. unsigned si = c->regs[VCPU_REGS_RSI];
  643. unsigned di = c->regs[VCPU_REGS_RDI];
  644. /* 16-bit ModR/M decode. */
  645. switch (c->modrm_mod) {
  646. case 0:
  647. if (c->modrm_rm == 6)
  648. c->modrm_ea += insn_fetch(u16, 2, c->eip);
  649. break;
  650. case 1:
  651. c->modrm_ea += insn_fetch(s8, 1, c->eip);
  652. break;
  653. case 2:
  654. c->modrm_ea += insn_fetch(u16, 2, c->eip);
  655. break;
  656. }
  657. switch (c->modrm_rm) {
  658. case 0:
  659. c->modrm_ea += bx + si;
  660. break;
  661. case 1:
  662. c->modrm_ea += bx + di;
  663. break;
  664. case 2:
  665. c->modrm_ea += bp + si;
  666. break;
  667. case 3:
  668. c->modrm_ea += bp + di;
  669. break;
  670. case 4:
  671. c->modrm_ea += si;
  672. break;
  673. case 5:
  674. c->modrm_ea += di;
  675. break;
  676. case 6:
  677. if (c->modrm_mod != 0)
  678. c->modrm_ea += bp;
  679. break;
  680. case 7:
  681. c->modrm_ea += bx;
  682. break;
  683. }
  684. if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
  685. (c->modrm_rm == 6 && c->modrm_mod != 0))
  686. if (!c->override_base)
  687. c->override_base = &ctxt->ss_base;
  688. c->modrm_ea = (u16)c->modrm_ea;
  689. } else {
  690. /* 32/64-bit ModR/M decode. */
  691. switch (c->modrm_rm) {
  692. case 4:
  693. case 12:
  694. sib = insn_fetch(u8, 1, c->eip);
  695. index_reg |= (sib >> 3) & 7;
  696. base_reg |= sib & 7;
  697. scale = sib >> 6;
  698. switch (base_reg) {
  699. case 5:
  700. if (c->modrm_mod != 0)
  701. c->modrm_ea += c->regs[base_reg];
  702. else
  703. c->modrm_ea +=
  704. insn_fetch(s32, 4, c->eip);
  705. break;
  706. default:
  707. c->modrm_ea += c->regs[base_reg];
  708. }
  709. switch (index_reg) {
  710. case 4:
  711. break;
  712. default:
  713. c->modrm_ea += c->regs[index_reg] << scale;
  714. }
  715. break;
  716. case 5:
  717. if (c->modrm_mod != 0)
  718. c->modrm_ea += c->regs[c->modrm_rm];
  719. else if (ctxt->mode == X86EMUL_MODE_PROT64)
  720. rip_relative = 1;
  721. break;
  722. default:
  723. c->modrm_ea += c->regs[c->modrm_rm];
  724. break;
  725. }
  726. switch (c->modrm_mod) {
  727. case 0:
  728. if (c->modrm_rm == 5)
  729. c->modrm_ea += insn_fetch(s32, 4, c->eip);
  730. break;
  731. case 1:
  732. c->modrm_ea += insn_fetch(s8, 1, c->eip);
  733. break;
  734. case 2:
  735. c->modrm_ea += insn_fetch(s32, 4, c->eip);
  736. break;
  737. }
  738. }
  739. if (rip_relative) {
  740. c->modrm_ea += c->eip;
  741. switch (c->d & SrcMask) {
  742. case SrcImmByte:
  743. c->modrm_ea += 1;
  744. break;
  745. case SrcImm:
  746. if (c->d & ByteOp)
  747. c->modrm_ea += 1;
  748. else
  749. if (c->op_bytes == 8)
  750. c->modrm_ea += 4;
  751. else
  752. c->modrm_ea += c->op_bytes;
  753. }
  754. }
  755. done:
  756. return rc;
  757. }
  758. static int decode_abs(struct x86_emulate_ctxt *ctxt,
  759. struct x86_emulate_ops *ops)
  760. {
  761. struct decode_cache *c = &ctxt->decode;
  762. int rc = 0;
  763. switch (c->ad_bytes) {
  764. case 2:
  765. c->modrm_ea = insn_fetch(u16, 2, c->eip);
  766. break;
  767. case 4:
  768. c->modrm_ea = insn_fetch(u32, 4, c->eip);
  769. break;
  770. case 8:
  771. c->modrm_ea = insn_fetch(u64, 8, c->eip);
  772. break;
  773. }
  774. done:
  775. return rc;
  776. }
  777. int
  778. x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
  779. {
  780. struct decode_cache *c = &ctxt->decode;
  781. int rc = 0;
  782. int mode = ctxt->mode;
  783. int def_op_bytes, def_ad_bytes, group;
  784. /* Shadow copy of register state. Committed on successful emulation. */
  785. memset(c, 0, sizeof(struct decode_cache));
  786. c->eip = ctxt->vcpu->arch.rip;
  787. memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
  788. switch (mode) {
  789. case X86EMUL_MODE_REAL:
  790. case X86EMUL_MODE_PROT16:
  791. def_op_bytes = def_ad_bytes = 2;
  792. break;
  793. case X86EMUL_MODE_PROT32:
  794. def_op_bytes = def_ad_bytes = 4;
  795. break;
  796. #ifdef CONFIG_X86_64
  797. case X86EMUL_MODE_PROT64:
  798. def_op_bytes = 4;
  799. def_ad_bytes = 8;
  800. break;
  801. #endif
  802. default:
  803. return -1;
  804. }
  805. c->op_bytes = def_op_bytes;
  806. c->ad_bytes = def_ad_bytes;
  807. /* Legacy prefixes. */
  808. for (;;) {
  809. switch (c->b = insn_fetch(u8, 1, c->eip)) {
  810. case 0x66: /* operand-size override */
  811. /* switch between 2/4 bytes */
  812. c->op_bytes = def_op_bytes ^ 6;
  813. break;
  814. case 0x67: /* address-size override */
  815. if (mode == X86EMUL_MODE_PROT64)
  816. /* switch between 4/8 bytes */
  817. c->ad_bytes = def_ad_bytes ^ 12;
  818. else
  819. /* switch between 2/4 bytes */
  820. c->ad_bytes = def_ad_bytes ^ 6;
  821. break;
  822. case 0x2e: /* CS override */
  823. c->override_base = &ctxt->cs_base;
  824. break;
  825. case 0x3e: /* DS override */
  826. c->override_base = &ctxt->ds_base;
  827. break;
  828. case 0x26: /* ES override */
  829. c->override_base = &ctxt->es_base;
  830. break;
  831. case 0x64: /* FS override */
  832. c->override_base = &ctxt->fs_base;
  833. break;
  834. case 0x65: /* GS override */
  835. c->override_base = &ctxt->gs_base;
  836. break;
  837. case 0x36: /* SS override */
  838. c->override_base = &ctxt->ss_base;
  839. break;
  840. case 0x40 ... 0x4f: /* REX */
  841. if (mode != X86EMUL_MODE_PROT64)
  842. goto done_prefixes;
  843. c->rex_prefix = c->b;
  844. continue;
  845. case 0xf0: /* LOCK */
  846. c->lock_prefix = 1;
  847. break;
  848. case 0xf2: /* REPNE/REPNZ */
  849. c->rep_prefix = REPNE_PREFIX;
  850. break;
  851. case 0xf3: /* REP/REPE/REPZ */
  852. c->rep_prefix = REPE_PREFIX;
  853. break;
  854. default:
  855. goto done_prefixes;
  856. }
  857. /* Any legacy prefix after a REX prefix nullifies its effect. */
  858. c->rex_prefix = 0;
  859. }
  860. done_prefixes:
  861. /* REX prefix. */
  862. if (c->rex_prefix)
  863. if (c->rex_prefix & 8)
  864. c->op_bytes = 8; /* REX.W */
  865. /* Opcode byte(s). */
  866. c->d = opcode_table[c->b];
  867. if (c->d == 0) {
  868. /* Two-byte opcode? */
  869. if (c->b == 0x0f) {
  870. c->twobyte = 1;
  871. c->b = insn_fetch(u8, 1, c->eip);
  872. c->d = twobyte_table[c->b];
  873. }
  874. }
  875. if (c->d & Group) {
  876. group = c->d & GroupMask;
  877. c->modrm = insn_fetch(u8, 1, c->eip);
  878. --c->eip;
  879. group = (group << 3) + ((c->modrm >> 3) & 7);
  880. if ((c->d & GroupDual) && (c->modrm >> 6) == 3)
  881. c->d = group2_table[group];
  882. else
  883. c->d = group_table[group];
  884. }
  885. /* Unrecognised? */
  886. if (c->d == 0) {
  887. DPRINTF("Cannot emulate %02x\n", c->b);
  888. return -1;
  889. }
  890. if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
  891. c->op_bytes = 8;
  892. /* ModRM and SIB bytes. */
  893. if (c->d & ModRM)
  894. rc = decode_modrm(ctxt, ops);
  895. else if (c->d & MemAbs)
  896. rc = decode_abs(ctxt, ops);
  897. if (rc)
  898. goto done;
  899. if (!c->override_base)
  900. c->override_base = &ctxt->ds_base;
  901. if (mode == X86EMUL_MODE_PROT64 &&
  902. c->override_base != &ctxt->fs_base &&
  903. c->override_base != &ctxt->gs_base)
  904. c->override_base = NULL;
  905. if (c->override_base)
  906. c->modrm_ea += *c->override_base;
  907. if (c->ad_bytes != 8)
  908. c->modrm_ea = (u32)c->modrm_ea;
  909. /*
  910. * Decode and fetch the source operand: register, memory
  911. * or immediate.
  912. */
  913. switch (c->d & SrcMask) {
  914. case SrcNone:
  915. break;
  916. case SrcReg:
  917. decode_register_operand(&c->src, c, 0);
  918. break;
  919. case SrcMem16:
  920. c->src.bytes = 2;
  921. goto srcmem_common;
  922. case SrcMem32:
  923. c->src.bytes = 4;
  924. goto srcmem_common;
  925. case SrcMem:
  926. c->src.bytes = (c->d & ByteOp) ? 1 :
  927. c->op_bytes;
  928. /* Don't fetch the address for invlpg: it could be unmapped. */
  929. if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
  930. break;
  931. srcmem_common:
  932. /*
  933. * For instructions with a ModR/M byte, switch to register
  934. * access if Mod = 3.
  935. */
  936. if ((c->d & ModRM) && c->modrm_mod == 3) {
  937. c->src.type = OP_REG;
  938. c->src.val = c->modrm_val;
  939. c->src.ptr = c->modrm_ptr;
  940. break;
  941. }
  942. c->src.type = OP_MEM;
  943. break;
  944. case SrcImm:
  945. c->src.type = OP_IMM;
  946. c->src.ptr = (unsigned long *)c->eip;
  947. c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
  948. if (c->src.bytes == 8)
  949. c->src.bytes = 4;
  950. /* NB. Immediates are sign-extended as necessary. */
  951. switch (c->src.bytes) {
  952. case 1:
  953. c->src.val = insn_fetch(s8, 1, c->eip);
  954. break;
  955. case 2:
  956. c->src.val = insn_fetch(s16, 2, c->eip);
  957. break;
  958. case 4:
  959. c->src.val = insn_fetch(s32, 4, c->eip);
  960. break;
  961. }
  962. break;
  963. case SrcImmByte:
  964. c->src.type = OP_IMM;
  965. c->src.ptr = (unsigned long *)c->eip;
  966. c->src.bytes = 1;
  967. c->src.val = insn_fetch(s8, 1, c->eip);
  968. break;
  969. }
  970. /* Decode and fetch the destination operand: register or memory. */
  971. switch (c->d & DstMask) {
  972. case ImplicitOps:
  973. /* Special instructions do their own operand decoding. */
  974. return 0;
  975. case DstReg:
  976. decode_register_operand(&c->dst, c,
  977. c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
  978. break;
  979. case DstMem:
  980. if ((c->d & ModRM) && c->modrm_mod == 3) {
  981. c->dst.type = OP_REG;
  982. c->dst.val = c->dst.orig_val = c->modrm_val;
  983. c->dst.ptr = c->modrm_ptr;
  984. break;
  985. }
  986. c->dst.type = OP_MEM;
  987. break;
  988. }
  989. done:
  990. return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
  991. }
  992. static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
  993. {
  994. struct decode_cache *c = &ctxt->decode;
  995. c->dst.type = OP_MEM;
  996. c->dst.bytes = c->op_bytes;
  997. c->dst.val = c->src.val;
  998. register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
  999. c->dst.ptr = (void *) register_address(c, ctxt->ss_base,
  1000. c->regs[VCPU_REGS_RSP]);
  1001. }
  1002. static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
  1003. struct x86_emulate_ops *ops)
  1004. {
  1005. struct decode_cache *c = &ctxt->decode;
  1006. int rc;
  1007. rc = ops->read_std(register_address(c, ctxt->ss_base,
  1008. c->regs[VCPU_REGS_RSP]),
  1009. &c->dst.val, c->dst.bytes, ctxt->vcpu);
  1010. if (rc != 0)
  1011. return rc;
  1012. register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->dst.bytes);
  1013. return 0;
  1014. }
  1015. static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
  1016. {
  1017. struct decode_cache *c = &ctxt->decode;
  1018. switch (c->modrm_reg) {
  1019. case 0: /* rol */
  1020. emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
  1021. break;
  1022. case 1: /* ror */
  1023. emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
  1024. break;
  1025. case 2: /* rcl */
  1026. emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
  1027. break;
  1028. case 3: /* rcr */
  1029. emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
  1030. break;
  1031. case 4: /* sal/shl */
  1032. case 6: /* sal/shl */
  1033. emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
  1034. break;
  1035. case 5: /* shr */
  1036. emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
  1037. break;
  1038. case 7: /* sar */
  1039. emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
  1040. break;
  1041. }
  1042. }
  1043. static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
  1044. struct x86_emulate_ops *ops)
  1045. {
  1046. struct decode_cache *c = &ctxt->decode;
  1047. int rc = 0;
  1048. switch (c->modrm_reg) {
  1049. case 0 ... 1: /* test */
  1050. emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
  1051. break;
  1052. case 2: /* not */
  1053. c->dst.val = ~c->dst.val;
  1054. break;
  1055. case 3: /* neg */
  1056. emulate_1op("neg", c->dst, ctxt->eflags);
  1057. break;
  1058. default:
  1059. DPRINTF("Cannot emulate %02x\n", c->b);
  1060. rc = X86EMUL_UNHANDLEABLE;
  1061. break;
  1062. }
  1063. return rc;
  1064. }
  1065. static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
  1066. struct x86_emulate_ops *ops)
  1067. {
  1068. struct decode_cache *c = &ctxt->decode;
  1069. switch (c->modrm_reg) {
  1070. case 0: /* inc */
  1071. emulate_1op("inc", c->dst, ctxt->eflags);
  1072. break;
  1073. case 1: /* dec */
  1074. emulate_1op("dec", c->dst, ctxt->eflags);
  1075. break;
  1076. case 4: /* jmp abs */
  1077. c->eip = c->src.val;
  1078. break;
  1079. case 6: /* push */
  1080. emulate_push(ctxt);
  1081. break;
  1082. }
  1083. return 0;
  1084. }
  1085. static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
  1086. struct x86_emulate_ops *ops,
  1087. unsigned long memop)
  1088. {
  1089. struct decode_cache *c = &ctxt->decode;
  1090. u64 old, new;
  1091. int rc;
  1092. rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu);
  1093. if (rc != 0)
  1094. return rc;
  1095. if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
  1096. ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
  1097. c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
  1098. c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
  1099. ctxt->eflags &= ~EFLG_ZF;
  1100. } else {
  1101. new = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
  1102. (u32) c->regs[VCPU_REGS_RBX];
  1103. rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu);
  1104. if (rc != 0)
  1105. return rc;
  1106. ctxt->eflags |= EFLG_ZF;
  1107. }
  1108. return 0;
  1109. }
  1110. static inline int writeback(struct x86_emulate_ctxt *ctxt,
  1111. struct x86_emulate_ops *ops)
  1112. {
  1113. int rc;
  1114. struct decode_cache *c = &ctxt->decode;
  1115. switch (c->dst.type) {
  1116. case OP_REG:
  1117. /* The 4-byte case *is* correct:
  1118. * in 64-bit mode we zero-extend.
  1119. */
  1120. switch (c->dst.bytes) {
  1121. case 1:
  1122. *(u8 *)c->dst.ptr = (u8)c->dst.val;
  1123. break;
  1124. case 2:
  1125. *(u16 *)c->dst.ptr = (u16)c->dst.val;
  1126. break;
  1127. case 4:
  1128. *c->dst.ptr = (u32)c->dst.val;
  1129. break; /* 64b: zero-ext */
  1130. case 8:
  1131. *c->dst.ptr = c->dst.val;
  1132. break;
  1133. }
  1134. break;
  1135. case OP_MEM:
  1136. if (c->lock_prefix)
  1137. rc = ops->cmpxchg_emulated(
  1138. (unsigned long)c->dst.ptr,
  1139. &c->dst.orig_val,
  1140. &c->dst.val,
  1141. c->dst.bytes,
  1142. ctxt->vcpu);
  1143. else
  1144. rc = ops->write_emulated(
  1145. (unsigned long)c->dst.ptr,
  1146. &c->dst.val,
  1147. c->dst.bytes,
  1148. ctxt->vcpu);
  1149. if (rc != 0)
  1150. return rc;
  1151. break;
  1152. case OP_NONE:
  1153. /* no writeback */
  1154. break;
  1155. default:
  1156. break;
  1157. }
  1158. return 0;
  1159. }
  1160. int
  1161. x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
  1162. {
  1163. unsigned long memop = 0;
  1164. u64 msr_data;
  1165. unsigned long saved_eip = 0;
  1166. struct decode_cache *c = &ctxt->decode;
  1167. int rc = 0;
  1168. /* Shadow copy of register state. Committed on successful emulation.
  1169. * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't
  1170. * modify them.
  1171. */
  1172. memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
  1173. saved_eip = c->eip;
  1174. if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs))
  1175. memop = c->modrm_ea;
  1176. if (c->rep_prefix && (c->d & String)) {
  1177. /* All REP prefixes have the same first termination condition */
  1178. if (c->regs[VCPU_REGS_RCX] == 0) {
  1179. ctxt->vcpu->arch.rip = c->eip;
  1180. goto done;
  1181. }
  1182. /* The second termination condition only applies for REPE
  1183. * and REPNE. Test if the repeat string operation prefix is
  1184. * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
  1185. * corresponding termination condition according to:
  1186. * - if REPE/REPZ and ZF = 0 then done
  1187. * - if REPNE/REPNZ and ZF = 1 then done
  1188. */
  1189. if ((c->b == 0xa6) || (c->b == 0xa7) ||
  1190. (c->b == 0xae) || (c->b == 0xaf)) {
  1191. if ((c->rep_prefix == REPE_PREFIX) &&
  1192. ((ctxt->eflags & EFLG_ZF) == 0)) {
  1193. ctxt->vcpu->arch.rip = c->eip;
  1194. goto done;
  1195. }
  1196. if ((c->rep_prefix == REPNE_PREFIX) &&
  1197. ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
  1198. ctxt->vcpu->arch.rip = c->eip;
  1199. goto done;
  1200. }
  1201. }
  1202. c->regs[VCPU_REGS_RCX]--;
  1203. c->eip = ctxt->vcpu->arch.rip;
  1204. }
  1205. if (c->src.type == OP_MEM) {
  1206. c->src.ptr = (unsigned long *)memop;
  1207. c->src.val = 0;
  1208. rc = ops->read_emulated((unsigned long)c->src.ptr,
  1209. &c->src.val,
  1210. c->src.bytes,
  1211. ctxt->vcpu);
  1212. if (rc != 0)
  1213. goto done;
  1214. c->src.orig_val = c->src.val;
  1215. }
  1216. if ((c->d & DstMask) == ImplicitOps)
  1217. goto special_insn;
  1218. if (c->dst.type == OP_MEM) {
  1219. c->dst.ptr = (unsigned long *)memop;
  1220. c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
  1221. c->dst.val = 0;
  1222. if (c->d & BitOp) {
  1223. unsigned long mask = ~(c->dst.bytes * 8 - 1);
  1224. c->dst.ptr = (void *)c->dst.ptr +
  1225. (c->src.val & mask) / 8;
  1226. }
  1227. if (!(c->d & Mov) &&
  1228. /* optimisation - avoid slow emulated read */
  1229. ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
  1230. &c->dst.val,
  1231. c->dst.bytes, ctxt->vcpu)) != 0))
  1232. goto done;
  1233. }
  1234. c->dst.orig_val = c->dst.val;
  1235. special_insn:
  1236. if (c->twobyte)
  1237. goto twobyte_insn;
  1238. switch (c->b) {
  1239. case 0x00 ... 0x05:
  1240. add: /* add */
  1241. emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
  1242. break;
  1243. case 0x08 ... 0x0d:
  1244. or: /* or */
  1245. emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
  1246. break;
  1247. case 0x10 ... 0x15:
  1248. adc: /* adc */
  1249. emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
  1250. break;
  1251. case 0x18 ... 0x1d:
  1252. sbb: /* sbb */
  1253. emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
  1254. break;
  1255. case 0x20 ... 0x23:
  1256. and: /* and */
  1257. emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
  1258. break;
  1259. case 0x24: /* and al imm8 */
  1260. c->dst.type = OP_REG;
  1261. c->dst.ptr = &c->regs[VCPU_REGS_RAX];
  1262. c->dst.val = *(u8 *)c->dst.ptr;
  1263. c->dst.bytes = 1;
  1264. c->dst.orig_val = c->dst.val;
  1265. goto and;
  1266. case 0x25: /* and ax imm16, or eax imm32 */
  1267. c->dst.type = OP_REG;
  1268. c->dst.bytes = c->op_bytes;
  1269. c->dst.ptr = &c->regs[VCPU_REGS_RAX];
  1270. if (c->op_bytes == 2)
  1271. c->dst.val = *(u16 *)c->dst.ptr;
  1272. else
  1273. c->dst.val = *(u32 *)c->dst.ptr;
  1274. c->dst.orig_val = c->dst.val;
  1275. goto and;
  1276. case 0x28 ... 0x2d:
  1277. sub: /* sub */
  1278. emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
  1279. break;
  1280. case 0x30 ... 0x35:
  1281. xor: /* xor */
  1282. emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
  1283. break;
  1284. case 0x38 ... 0x3d:
  1285. cmp: /* cmp */
  1286. emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
  1287. break;
  1288. case 0x40 ... 0x47: /* inc r16/r32 */
  1289. emulate_1op("inc", c->dst, ctxt->eflags);
  1290. break;
  1291. case 0x48 ... 0x4f: /* dec r16/r32 */
  1292. emulate_1op("dec", c->dst, ctxt->eflags);
  1293. break;
  1294. case 0x50 ... 0x57: /* push reg */
  1295. c->dst.type = OP_MEM;
  1296. c->dst.bytes = c->op_bytes;
  1297. c->dst.val = c->src.val;
  1298. register_address_increment(c, &c->regs[VCPU_REGS_RSP],
  1299. -c->op_bytes);
  1300. c->dst.ptr = (void *) register_address(
  1301. c, ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
  1302. break;
  1303. case 0x58 ... 0x5f: /* pop reg */
  1304. pop_instruction:
  1305. if ((rc = ops->read_std(register_address(c, ctxt->ss_base,
  1306. c->regs[VCPU_REGS_RSP]), c->dst.ptr,
  1307. c->op_bytes, ctxt->vcpu)) != 0)
  1308. goto done;
  1309. register_address_increment(c, &c->regs[VCPU_REGS_RSP],
  1310. c->op_bytes);
  1311. c->dst.type = OP_NONE; /* Disable writeback. */
  1312. break;
  1313. case 0x63: /* movsxd */
  1314. if (ctxt->mode != X86EMUL_MODE_PROT64)
  1315. goto cannot_emulate;
  1316. c->dst.val = (s32) c->src.val;
  1317. break;
  1318. case 0x6a: /* push imm8 */
  1319. c->src.val = 0L;
  1320. c->src.val = insn_fetch(s8, 1, c->eip);
  1321. emulate_push(ctxt);
  1322. break;
  1323. case 0x6c: /* insb */
  1324. case 0x6d: /* insw/insd */
  1325. if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
  1326. 1,
  1327. (c->d & ByteOp) ? 1 : c->op_bytes,
  1328. c->rep_prefix ?
  1329. address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
  1330. (ctxt->eflags & EFLG_DF),
  1331. register_address(c, ctxt->es_base,
  1332. c->regs[VCPU_REGS_RDI]),
  1333. c->rep_prefix,
  1334. c->regs[VCPU_REGS_RDX]) == 0) {
  1335. c->eip = saved_eip;
  1336. return -1;
  1337. }
  1338. return 0;
  1339. case 0x6e: /* outsb */
  1340. case 0x6f: /* outsw/outsd */
  1341. if (kvm_emulate_pio_string(ctxt->vcpu, NULL,
  1342. 0,
  1343. (c->d & ByteOp) ? 1 : c->op_bytes,
  1344. c->rep_prefix ?
  1345. address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
  1346. (ctxt->eflags & EFLG_DF),
  1347. register_address(c, c->override_base ?
  1348. *c->override_base :
  1349. ctxt->ds_base,
  1350. c->regs[VCPU_REGS_RSI]),
  1351. c->rep_prefix,
  1352. c->regs[VCPU_REGS_RDX]) == 0) {
  1353. c->eip = saved_eip;
  1354. return -1;
  1355. }
  1356. return 0;
  1357. case 0x70 ... 0x7f: /* jcc (short) */ {
  1358. int rel = insn_fetch(s8, 1, c->eip);
  1359. if (test_cc(c->b, ctxt->eflags))
  1360. jmp_rel(c, rel);
  1361. break;
  1362. }
  1363. case 0x80 ... 0x83: /* Grp1 */
  1364. switch (c->modrm_reg) {
  1365. case 0:
  1366. goto add;
  1367. case 1:
  1368. goto or;
  1369. case 2:
  1370. goto adc;
  1371. case 3:
  1372. goto sbb;
  1373. case 4:
  1374. goto and;
  1375. case 5:
  1376. goto sub;
  1377. case 6:
  1378. goto xor;
  1379. case 7:
  1380. goto cmp;
  1381. }
  1382. break;
  1383. case 0x84 ... 0x85:
  1384. emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
  1385. break;
  1386. case 0x86 ... 0x87: /* xchg */
  1387. /* Write back the register source. */
  1388. switch (c->dst.bytes) {
  1389. case 1:
  1390. *(u8 *) c->src.ptr = (u8) c->dst.val;
  1391. break;
  1392. case 2:
  1393. *(u16 *) c->src.ptr = (u16) c->dst.val;
  1394. break;
  1395. case 4:
  1396. *c->src.ptr = (u32) c->dst.val;
  1397. break; /* 64b reg: zero-extend */
  1398. case 8:
  1399. *c->src.ptr = c->dst.val;
  1400. break;
  1401. }
  1402. /*
  1403. * Write back the memory destination with implicit LOCK
  1404. * prefix.
  1405. */
  1406. c->dst.val = c->src.val;
  1407. c->lock_prefix = 1;
  1408. break;
  1409. case 0x88 ... 0x8b: /* mov */
  1410. goto mov;
  1411. case 0x8d: /* lea r16/r32, m */
  1412. c->dst.val = c->modrm_ea;
  1413. break;
  1414. case 0x8f: /* pop (sole member of Grp1a) */
  1415. rc = emulate_grp1a(ctxt, ops);
  1416. if (rc != 0)
  1417. goto done;
  1418. break;
  1419. case 0x9c: /* pushf */
  1420. c->src.val = (unsigned long) ctxt->eflags;
  1421. emulate_push(ctxt);
  1422. break;
  1423. case 0x9d: /* popf */
  1424. c->dst.ptr = (unsigned long *) &ctxt->eflags;
  1425. goto pop_instruction;
  1426. case 0xa0 ... 0xa1: /* mov */
  1427. c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
  1428. c->dst.val = c->src.val;
  1429. break;
  1430. case 0xa2 ... 0xa3: /* mov */
  1431. c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX];
  1432. break;
  1433. case 0xa4 ... 0xa5: /* movs */
  1434. c->dst.type = OP_MEM;
  1435. c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
  1436. c->dst.ptr = (unsigned long *)register_address(c,
  1437. ctxt->es_base,
  1438. c->regs[VCPU_REGS_RDI]);
  1439. if ((rc = ops->read_emulated(register_address(c,
  1440. c->override_base ? *c->override_base :
  1441. ctxt->ds_base,
  1442. c->regs[VCPU_REGS_RSI]),
  1443. &c->dst.val,
  1444. c->dst.bytes, ctxt->vcpu)) != 0)
  1445. goto done;
  1446. register_address_increment(c, &c->regs[VCPU_REGS_RSI],
  1447. (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
  1448. : c->dst.bytes);
  1449. register_address_increment(c, &c->regs[VCPU_REGS_RDI],
  1450. (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
  1451. : c->dst.bytes);
  1452. break;
  1453. case 0xa6 ... 0xa7: /* cmps */
  1454. c->src.type = OP_NONE; /* Disable writeback. */
  1455. c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
  1456. c->src.ptr = (unsigned long *)register_address(c,
  1457. c->override_base ? *c->override_base :
  1458. ctxt->ds_base,
  1459. c->regs[VCPU_REGS_RSI]);
  1460. if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
  1461. &c->src.val,
  1462. c->src.bytes,
  1463. ctxt->vcpu)) != 0)
  1464. goto done;
  1465. c->dst.type = OP_NONE; /* Disable writeback. */
  1466. c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
  1467. c->dst.ptr = (unsigned long *)register_address(c,
  1468. ctxt->es_base,
  1469. c->regs[VCPU_REGS_RDI]);
  1470. if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
  1471. &c->dst.val,
  1472. c->dst.bytes,
  1473. ctxt->vcpu)) != 0)
  1474. goto done;
  1475. DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
  1476. emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
  1477. register_address_increment(c, &c->regs[VCPU_REGS_RSI],
  1478. (ctxt->eflags & EFLG_DF) ? -c->src.bytes
  1479. : c->src.bytes);
  1480. register_address_increment(c, &c->regs[VCPU_REGS_RDI],
  1481. (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
  1482. : c->dst.bytes);
  1483. break;
  1484. case 0xaa ... 0xab: /* stos */
  1485. c->dst.type = OP_MEM;
  1486. c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
  1487. c->dst.ptr = (unsigned long *)register_address(c,
  1488. ctxt->es_base,
  1489. c->regs[VCPU_REGS_RDI]);
  1490. c->dst.val = c->regs[VCPU_REGS_RAX];
  1491. register_address_increment(c, &c->regs[VCPU_REGS_RDI],
  1492. (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
  1493. : c->dst.bytes);
  1494. break;
  1495. case 0xac ... 0xad: /* lods */
  1496. c->dst.type = OP_REG;
  1497. c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
  1498. c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
  1499. if ((rc = ops->read_emulated(register_address(c,
  1500. c->override_base ? *c->override_base :
  1501. ctxt->ds_base,
  1502. c->regs[VCPU_REGS_RSI]),
  1503. &c->dst.val,
  1504. c->dst.bytes,
  1505. ctxt->vcpu)) != 0)
  1506. goto done;
  1507. register_address_increment(c, &c->regs[VCPU_REGS_RSI],
  1508. (ctxt->eflags & EFLG_DF) ? -c->dst.bytes
  1509. : c->dst.bytes);
  1510. break;
  1511. case 0xae ... 0xaf: /* scas */
  1512. DPRINTF("Urk! I don't handle SCAS.\n");
  1513. goto cannot_emulate;
  1514. case 0xc0 ... 0xc1:
  1515. emulate_grp2(ctxt);
  1516. break;
  1517. case 0xc3: /* ret */
  1518. c->dst.ptr = &c->eip;
  1519. goto pop_instruction;
  1520. case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
  1521. mov:
  1522. c->dst.val = c->src.val;
  1523. break;
  1524. case 0xd0 ... 0xd1: /* Grp2 */
  1525. c->src.val = 1;
  1526. emulate_grp2(ctxt);
  1527. break;
  1528. case 0xd2 ... 0xd3: /* Grp2 */
  1529. c->src.val = c->regs[VCPU_REGS_RCX];
  1530. emulate_grp2(ctxt);
  1531. break;
  1532. case 0xe8: /* call (near) */ {
  1533. long int rel;
  1534. switch (c->op_bytes) {
  1535. case 2:
  1536. rel = insn_fetch(s16, 2, c->eip);
  1537. break;
  1538. case 4:
  1539. rel = insn_fetch(s32, 4, c->eip);
  1540. break;
  1541. default:
  1542. DPRINTF("Call: Invalid op_bytes\n");
  1543. goto cannot_emulate;
  1544. }
  1545. c->src.val = (unsigned long) c->eip;
  1546. jmp_rel(c, rel);
  1547. c->op_bytes = c->ad_bytes;
  1548. emulate_push(ctxt);
  1549. break;
  1550. }
  1551. case 0xe9: /* jmp rel */
  1552. case 0xeb: /* jmp rel short */
  1553. jmp_rel(c, c->src.val);
  1554. c->dst.type = OP_NONE; /* Disable writeback. */
  1555. break;
  1556. case 0xf4: /* hlt */
  1557. ctxt->vcpu->arch.halt_request = 1;
  1558. goto done;
  1559. case 0xf5: /* cmc */
  1560. /* complement carry flag from eflags reg */
  1561. ctxt->eflags ^= EFLG_CF;
  1562. c->dst.type = OP_NONE; /* Disable writeback. */
  1563. break;
  1564. case 0xf6 ... 0xf7: /* Grp3 */
  1565. rc = emulate_grp3(ctxt, ops);
  1566. if (rc != 0)
  1567. goto done;
  1568. break;
  1569. case 0xf8: /* clc */
  1570. ctxt->eflags &= ~EFLG_CF;
  1571. c->dst.type = OP_NONE; /* Disable writeback. */
  1572. break;
  1573. case 0xfa: /* cli */
  1574. ctxt->eflags &= ~X86_EFLAGS_IF;
  1575. c->dst.type = OP_NONE; /* Disable writeback. */
  1576. break;
  1577. case 0xfb: /* sti */
  1578. ctxt->eflags |= X86_EFLAGS_IF;
  1579. c->dst.type = OP_NONE; /* Disable writeback. */
  1580. break;
  1581. case 0xfe ... 0xff: /* Grp4/Grp5 */
  1582. rc = emulate_grp45(ctxt, ops);
  1583. if (rc != 0)
  1584. goto done;
  1585. break;
  1586. }
  1587. writeback:
  1588. rc = writeback(ctxt, ops);
  1589. if (rc != 0)
  1590. goto done;
  1591. /* Commit shadow register state. */
  1592. memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
  1593. ctxt->vcpu->arch.rip = c->eip;
  1594. done:
  1595. if (rc == X86EMUL_UNHANDLEABLE) {
  1596. c->eip = saved_eip;
  1597. return -1;
  1598. }
  1599. return 0;
  1600. twobyte_insn:
  1601. switch (c->b) {
  1602. case 0x01: /* lgdt, lidt, lmsw */
  1603. switch (c->modrm_reg) {
  1604. u16 size;
  1605. unsigned long address;
  1606. case 0: /* vmcall */
  1607. if (c->modrm_mod != 3 || c->modrm_rm != 1)
  1608. goto cannot_emulate;
  1609. rc = kvm_fix_hypercall(ctxt->vcpu);
  1610. if (rc)
  1611. goto done;
  1612. kvm_emulate_hypercall(ctxt->vcpu);
  1613. /* Disable writeback. */
  1614. c->dst.type = OP_NONE;
  1615. break;
  1616. case 2: /* lgdt */
  1617. rc = read_descriptor(ctxt, ops, c->src.ptr,
  1618. &size, &address, c->op_bytes);
  1619. if (rc)
  1620. goto done;
  1621. realmode_lgdt(ctxt->vcpu, size, address);
  1622. /* Disable writeback. */
  1623. c->dst.type = OP_NONE;
  1624. break;
  1625. case 3: /* lidt/vmmcall */
  1626. if (c->modrm_mod == 3 && c->modrm_rm == 1) {
  1627. rc = kvm_fix_hypercall(ctxt->vcpu);
  1628. if (rc)
  1629. goto done;
  1630. kvm_emulate_hypercall(ctxt->vcpu);
  1631. } else {
  1632. rc = read_descriptor(ctxt, ops, c->src.ptr,
  1633. &size, &address,
  1634. c->op_bytes);
  1635. if (rc)
  1636. goto done;
  1637. realmode_lidt(ctxt->vcpu, size, address);
  1638. }
  1639. /* Disable writeback. */
  1640. c->dst.type = OP_NONE;
  1641. break;
  1642. case 4: /* smsw */
  1643. c->dst.bytes = 2;
  1644. c->dst.val = realmode_get_cr(ctxt->vcpu, 0);
  1645. break;
  1646. case 6: /* lmsw */
  1647. realmode_lmsw(ctxt->vcpu, (u16)c->src.val,
  1648. &ctxt->eflags);
  1649. c->dst.type = OP_NONE;
  1650. break;
  1651. case 7: /* invlpg*/
  1652. emulate_invlpg(ctxt->vcpu, memop);
  1653. /* Disable writeback. */
  1654. c->dst.type = OP_NONE;
  1655. break;
  1656. default:
  1657. goto cannot_emulate;
  1658. }
  1659. break;
  1660. case 0x06:
  1661. emulate_clts(ctxt->vcpu);
  1662. c->dst.type = OP_NONE;
  1663. break;
  1664. case 0x08: /* invd */
  1665. case 0x09: /* wbinvd */
  1666. case 0x0d: /* GrpP (prefetch) */
  1667. case 0x18: /* Grp16 (prefetch/nop) */
  1668. c->dst.type = OP_NONE;
  1669. break;
  1670. case 0x20: /* mov cr, reg */
  1671. if (c->modrm_mod != 3)
  1672. goto cannot_emulate;
  1673. c->regs[c->modrm_rm] =
  1674. realmode_get_cr(ctxt->vcpu, c->modrm_reg);
  1675. c->dst.type = OP_NONE; /* no writeback */
  1676. break;
  1677. case 0x21: /* mov from dr to reg */
  1678. if (c->modrm_mod != 3)
  1679. goto cannot_emulate;
  1680. rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]);
  1681. if (rc)
  1682. goto cannot_emulate;
  1683. c->dst.type = OP_NONE; /* no writeback */
  1684. break;
  1685. case 0x22: /* mov reg, cr */
  1686. if (c->modrm_mod != 3)
  1687. goto cannot_emulate;
  1688. realmode_set_cr(ctxt->vcpu,
  1689. c->modrm_reg, c->modrm_val, &ctxt->eflags);
  1690. c->dst.type = OP_NONE;
  1691. break;
  1692. case 0x23: /* mov from reg to dr */
  1693. if (c->modrm_mod != 3)
  1694. goto cannot_emulate;
  1695. rc = emulator_set_dr(ctxt, c->modrm_reg,
  1696. c->regs[c->modrm_rm]);
  1697. if (rc)
  1698. goto cannot_emulate;
  1699. c->dst.type = OP_NONE; /* no writeback */
  1700. break;
  1701. case 0x30:
  1702. /* wrmsr */
  1703. msr_data = (u32)c->regs[VCPU_REGS_RAX]
  1704. | ((u64)c->regs[VCPU_REGS_RDX] << 32);
  1705. rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
  1706. if (rc) {
  1707. kvm_inject_gp(ctxt->vcpu, 0);
  1708. c->eip = ctxt->vcpu->arch.rip;
  1709. }
  1710. rc = X86EMUL_CONTINUE;
  1711. c->dst.type = OP_NONE;
  1712. break;
  1713. case 0x32:
  1714. /* rdmsr */
  1715. rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
  1716. if (rc) {
  1717. kvm_inject_gp(ctxt->vcpu, 0);
  1718. c->eip = ctxt->vcpu->arch.rip;
  1719. } else {
  1720. c->regs[VCPU_REGS_RAX] = (u32)msr_data;
  1721. c->regs[VCPU_REGS_RDX] = msr_data >> 32;
  1722. }
  1723. rc = X86EMUL_CONTINUE;
  1724. c->dst.type = OP_NONE;
  1725. break;
  1726. case 0x40 ... 0x4f: /* cmov */
  1727. c->dst.val = c->dst.orig_val = c->src.val;
  1728. if (!test_cc(c->b, ctxt->eflags))
  1729. c->dst.type = OP_NONE; /* no writeback */
  1730. break;
  1731. case 0x80 ... 0x8f: /* jnz rel, etc*/ {
  1732. long int rel;
  1733. switch (c->op_bytes) {
  1734. case 2:
  1735. rel = insn_fetch(s16, 2, c->eip);
  1736. break;
  1737. case 4:
  1738. rel = insn_fetch(s32, 4, c->eip);
  1739. break;
  1740. case 8:
  1741. rel = insn_fetch(s64, 8, c->eip);
  1742. break;
  1743. default:
  1744. DPRINTF("jnz: Invalid op_bytes\n");
  1745. goto cannot_emulate;
  1746. }
  1747. if (test_cc(c->b, ctxt->eflags))
  1748. jmp_rel(c, rel);
  1749. c->dst.type = OP_NONE;
  1750. break;
  1751. }
  1752. case 0xa3:
  1753. bt: /* bt */
  1754. c->dst.type = OP_NONE;
  1755. /* only subword offset */
  1756. c->src.val &= (c->dst.bytes << 3) - 1;
  1757. emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
  1758. break;
  1759. case 0xab:
  1760. bts: /* bts */
  1761. /* only subword offset */
  1762. c->src.val &= (c->dst.bytes << 3) - 1;
  1763. emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
  1764. break;
  1765. case 0xb0 ... 0xb1: /* cmpxchg */
  1766. /*
  1767. * Save real source value, then compare EAX against
  1768. * destination.
  1769. */
  1770. c->src.orig_val = c->src.val;
  1771. c->src.val = c->regs[VCPU_REGS_RAX];
  1772. emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
  1773. if (ctxt->eflags & EFLG_ZF) {
  1774. /* Success: write back to memory. */
  1775. c->dst.val = c->src.orig_val;
  1776. } else {
  1777. /* Failure: write the value we saw to EAX. */
  1778. c->dst.type = OP_REG;
  1779. c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
  1780. }
  1781. break;
  1782. case 0xb3:
  1783. btr: /* btr */
  1784. /* only subword offset */
  1785. c->src.val &= (c->dst.bytes << 3) - 1;
  1786. emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
  1787. break;
  1788. case 0xb6 ... 0xb7: /* movzx */
  1789. c->dst.bytes = c->op_bytes;
  1790. c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
  1791. : (u16) c->src.val;
  1792. break;
  1793. case 0xba: /* Grp8 */
  1794. switch (c->modrm_reg & 3) {
  1795. case 0:
  1796. goto bt;
  1797. case 1:
  1798. goto bts;
  1799. case 2:
  1800. goto btr;
  1801. case 3:
  1802. goto btc;
  1803. }
  1804. break;
  1805. case 0xbb:
  1806. btc: /* btc */
  1807. /* only subword offset */
  1808. c->src.val &= (c->dst.bytes << 3) - 1;
  1809. emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
  1810. break;
  1811. case 0xbe ... 0xbf: /* movsx */
  1812. c->dst.bytes = c->op_bytes;
  1813. c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
  1814. (s16) c->src.val;
  1815. break;
  1816. case 0xc3: /* movnti */
  1817. c->dst.bytes = c->op_bytes;
  1818. c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
  1819. (u64) c->src.val;
  1820. break;
  1821. case 0xc7: /* Grp9 (cmpxchg8b) */
  1822. rc = emulate_grp9(ctxt, ops, memop);
  1823. if (rc != 0)
  1824. goto done;
  1825. c->dst.type = OP_NONE;
  1826. break;
  1827. }
  1828. goto writeback;
  1829. cannot_emulate:
  1830. DPRINTF("Cannot emulate %02x\n", c->b);
  1831. c->eip = saved_eip;
  1832. return -1;
  1833. }