x86_emulate.c 47 KB

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