x86_emulate.c 52 KB

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