x86_emulate.c 55 KB

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