x86_emulate.c 52 KB

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