x86_emulate.c 54 KB

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