x86_emulate.c 53 KB

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