x86_emulate.c 56 KB

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