x86_emulate.c 52 KB

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