x86_emulate.c 53 KB

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