x86_emulate.c 54 KB

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