x86_emulate.c 51 KB

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