x86_emulate.c 56 KB

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