x86_emulate.c 51 KB

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