x86_emulate.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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. #define DPRINTF(x...) do {} while (0)
  29. #endif
  30. #include "x86_emulate.h"
  31. #include <linux/module.h>
  32. /*
  33. * Opcode effective-address decode tables.
  34. * Note that we only emulate instructions that have at least one memory
  35. * operand (excluding implicit stack references). We assume that stack
  36. * references and instruction fetches will never occur in special memory
  37. * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
  38. * not be handled.
  39. */
  40. /* Operand sizes: 8-bit operands or specified/overridden size. */
  41. #define ByteOp (1<<0) /* 8-bit operands. */
  42. /* Destination operand type. */
  43. #define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
  44. #define DstReg (2<<1) /* Register operand. */
  45. #define DstMem (3<<1) /* Memory operand. */
  46. #define DstMask (3<<1)
  47. /* Source operand type. */
  48. #define SrcNone (0<<3) /* No source operand. */
  49. #define SrcImplicit (0<<3) /* Source operand is implicit in the opcode. */
  50. #define SrcReg (1<<3) /* Register operand. */
  51. #define SrcMem (2<<3) /* Memory operand. */
  52. #define SrcMem16 (3<<3) /* Memory operand (16-bit). */
  53. #define SrcMem32 (4<<3) /* Memory operand (32-bit). */
  54. #define SrcImm (5<<3) /* Immediate operand. */
  55. #define SrcImmByte (6<<3) /* 8-bit sign-extended immediate operand. */
  56. #define SrcMask (7<<3)
  57. /* Generic ModRM decode. */
  58. #define ModRM (1<<6)
  59. /* Destination is only written; never read. */
  60. #define Mov (1<<7)
  61. #define BitOp (1<<8)
  62. static u8 opcode_table[256] = {
  63. /* 0x00 - 0x07 */
  64. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  65. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  66. 0, 0, 0, 0,
  67. /* 0x08 - 0x0F */
  68. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  69. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  70. 0, 0, 0, 0,
  71. /* 0x10 - 0x17 */
  72. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  73. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  74. 0, 0, 0, 0,
  75. /* 0x18 - 0x1F */
  76. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  77. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  78. 0, 0, 0, 0,
  79. /* 0x20 - 0x27 */
  80. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  81. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  82. 0, 0, 0, 0,
  83. /* 0x28 - 0x2F */
  84. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  85. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  86. 0, 0, 0, 0,
  87. /* 0x30 - 0x37 */
  88. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  89. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  90. 0, 0, 0, 0,
  91. /* 0x38 - 0x3F */
  92. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  93. ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM,
  94. 0, 0, 0, 0,
  95. /* 0x40 - 0x4F */
  96. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  97. /* 0x50 - 0x57 */
  98. 0, 0, 0, 0, 0, 0, 0, 0,
  99. /* 0x58 - 0x5F */
  100. ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
  101. ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
  102. /* 0x60 - 0x6F */
  103. 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ ,
  104. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  105. /* 0x70 - 0x7F */
  106. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  107. /* 0x80 - 0x87 */
  108. ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM,
  109. ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
  110. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  111. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM,
  112. /* 0x88 - 0x8F */
  113. ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov,
  114. ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  115. 0, 0, 0, DstMem | SrcNone | ModRM | Mov,
  116. /* 0x90 - 0x9F */
  117. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  118. /* 0xA0 - 0xA7 */
  119. ByteOp | DstReg | SrcMem | Mov, DstReg | SrcMem | Mov,
  120. ByteOp | DstMem | SrcReg | Mov, DstMem | SrcReg | Mov,
  121. ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
  122. ByteOp | ImplicitOps, ImplicitOps,
  123. /* 0xA8 - 0xAF */
  124. 0, 0, ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
  125. ByteOp | ImplicitOps | Mov, ImplicitOps | Mov,
  126. ByteOp | ImplicitOps, ImplicitOps,
  127. /* 0xB0 - 0xBF */
  128. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  129. /* 0xC0 - 0xC7 */
  130. ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM,
  131. 0, ImplicitOps, 0, 0,
  132. ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov,
  133. /* 0xC8 - 0xCF */
  134. 0, 0, 0, 0, 0, 0, 0, 0,
  135. /* 0xD0 - 0xD7 */
  136. ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
  137. ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM,
  138. 0, 0, 0, 0,
  139. /* 0xD8 - 0xDF */
  140. 0, 0, 0, 0, 0, 0, 0, 0,
  141. /* 0xE0 - 0xEF */
  142. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  143. /* 0xF0 - 0xF7 */
  144. 0, 0, 0, 0,
  145. ImplicitOps, 0,
  146. ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,
  147. /* 0xF8 - 0xFF */
  148. 0, 0, 0, 0,
  149. 0, 0, ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM
  150. };
  151. static u16 twobyte_table[256] = {
  152. /* 0x00 - 0x0F */
  153. 0, SrcMem | ModRM | DstReg, 0, 0, 0, 0, ImplicitOps, 0,
  154. 0, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0,
  155. /* 0x10 - 0x1F */
  156. 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0,
  157. /* 0x20 - 0x2F */
  158. ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0,
  159. 0, 0, 0, 0, 0, 0, 0, 0,
  160. /* 0x30 - 0x3F */
  161. ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  162. /* 0x40 - 0x47 */
  163. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  164. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  165. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  166. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  167. /* 0x48 - 0x4F */
  168. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  169. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  170. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  171. DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov,
  172. /* 0x50 - 0x5F */
  173. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  174. /* 0x60 - 0x6F */
  175. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  176. /* 0x70 - 0x7F */
  177. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  178. /* 0x80 - 0x8F */
  179. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  180. /* 0x90 - 0x9F */
  181. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  182. /* 0xA0 - 0xA7 */
  183. 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
  184. /* 0xA8 - 0xAF */
  185. 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0,
  186. /* 0xB0 - 0xB7 */
  187. ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0,
  188. DstMem | SrcReg | ModRM | BitOp,
  189. 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
  190. DstReg | SrcMem16 | ModRM | Mov,
  191. /* 0xB8 - 0xBF */
  192. 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp,
  193. 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov,
  194. DstReg | SrcMem16 | ModRM | Mov,
  195. /* 0xC0 - 0xCF */
  196. 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0, 0,
  197. /* 0xD0 - 0xDF */
  198. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  199. /* 0xE0 - 0xEF */
  200. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  201. /* 0xF0 - 0xFF */
  202. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  203. };
  204. /*
  205. * Tell the emulator that of the Group 7 instructions (sgdt, lidt, etc.) we
  206. * are interested only in invlpg and not in any of the rest.
  207. *
  208. * invlpg is a special instruction in that the data it references may not
  209. * be mapped.
  210. */
  211. void kvm_emulator_want_group7_invlpg(void)
  212. {
  213. twobyte_table[1] &= ~SrcMem;
  214. }
  215. EXPORT_SYMBOL_GPL(kvm_emulator_want_group7_invlpg);
  216. /* Type, address-of, and value of an instruction's operand. */
  217. struct operand {
  218. enum { OP_REG, OP_MEM, OP_IMM } type;
  219. unsigned int bytes;
  220. unsigned long val, orig_val, *ptr;
  221. };
  222. /* EFLAGS bit definitions. */
  223. #define EFLG_OF (1<<11)
  224. #define EFLG_DF (1<<10)
  225. #define EFLG_SF (1<<7)
  226. #define EFLG_ZF (1<<6)
  227. #define EFLG_AF (1<<4)
  228. #define EFLG_PF (1<<2)
  229. #define EFLG_CF (1<<0)
  230. /*
  231. * Instruction emulation:
  232. * Most instructions are emulated directly via a fragment of inline assembly
  233. * code. This allows us to save/restore EFLAGS and thus very easily pick up
  234. * any modified flags.
  235. */
  236. #if defined(CONFIG_X86_64)
  237. #define _LO32 "k" /* force 32-bit operand */
  238. #define _STK "%%rsp" /* stack pointer */
  239. #elif defined(__i386__)
  240. #define _LO32 "" /* force 32-bit operand */
  241. #define _STK "%%esp" /* stack pointer */
  242. #endif
  243. /*
  244. * These EFLAGS bits are restored from saved value during emulation, and
  245. * any changes are written back to the saved value after emulation.
  246. */
  247. #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
  248. /* Before executing instruction: restore necessary bits in EFLAGS. */
  249. #define _PRE_EFLAGS(_sav, _msk, _tmp) \
  250. /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); */ \
  251. "push %"_sav"; " \
  252. "movl %"_msk",%"_LO32 _tmp"; " \
  253. "andl %"_LO32 _tmp",("_STK"); " \
  254. "pushf; " \
  255. "notl %"_LO32 _tmp"; " \
  256. "andl %"_LO32 _tmp",("_STK"); " \
  257. "pop %"_tmp"; " \
  258. "orl %"_LO32 _tmp",("_STK"); " \
  259. "popf; " \
  260. /* _sav &= ~msk; */ \
  261. "movl %"_msk",%"_LO32 _tmp"; " \
  262. "notl %"_LO32 _tmp"; " \
  263. "andl %"_LO32 _tmp",%"_sav"; "
  264. /* After executing instruction: write-back necessary bits in EFLAGS. */
  265. #define _POST_EFLAGS(_sav, _msk, _tmp) \
  266. /* _sav |= EFLAGS & _msk; */ \
  267. "pushf; " \
  268. "pop %"_tmp"; " \
  269. "andl %"_msk",%"_LO32 _tmp"; " \
  270. "orl %"_LO32 _tmp",%"_sav"; "
  271. /* Raw emulation: instruction has two explicit operands. */
  272. #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
  273. do { \
  274. unsigned long _tmp; \
  275. \
  276. switch ((_dst).bytes) { \
  277. case 2: \
  278. __asm__ __volatile__ ( \
  279. _PRE_EFLAGS("0","4","2") \
  280. _op"w %"_wx"3,%1; " \
  281. _POST_EFLAGS("0","4","2") \
  282. : "=m" (_eflags), "=m" ((_dst).val), \
  283. "=&r" (_tmp) \
  284. : _wy ((_src).val), "i" (EFLAGS_MASK) ); \
  285. break; \
  286. case 4: \
  287. __asm__ __volatile__ ( \
  288. _PRE_EFLAGS("0","4","2") \
  289. _op"l %"_lx"3,%1; " \
  290. _POST_EFLAGS("0","4","2") \
  291. : "=m" (_eflags), "=m" ((_dst).val), \
  292. "=&r" (_tmp) \
  293. : _ly ((_src).val), "i" (EFLAGS_MASK) ); \
  294. break; \
  295. case 8: \
  296. __emulate_2op_8byte(_op, _src, _dst, \
  297. _eflags, _qx, _qy); \
  298. break; \
  299. } \
  300. } while (0)
  301. #define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
  302. do { \
  303. unsigned long _tmp; \
  304. switch ( (_dst).bytes ) \
  305. { \
  306. case 1: \
  307. __asm__ __volatile__ ( \
  308. _PRE_EFLAGS("0","4","2") \
  309. _op"b %"_bx"3,%1; " \
  310. _POST_EFLAGS("0","4","2") \
  311. : "=m" (_eflags), "=m" ((_dst).val), \
  312. "=&r" (_tmp) \
  313. : _by ((_src).val), "i" (EFLAGS_MASK) ); \
  314. break; \
  315. default: \
  316. __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
  317. _wx, _wy, _lx, _ly, _qx, _qy); \
  318. break; \
  319. } \
  320. } while (0)
  321. /* Source operand is byte-sized and may be restricted to just %cl. */
  322. #define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
  323. __emulate_2op(_op, _src, _dst, _eflags, \
  324. "b", "c", "b", "c", "b", "c", "b", "c")
  325. /* Source operand is byte, word, long or quad sized. */
  326. #define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
  327. __emulate_2op(_op, _src, _dst, _eflags, \
  328. "b", "q", "w", "r", _LO32, "r", "", "r")
  329. /* Source operand is word, long or quad sized. */
  330. #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
  331. __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
  332. "w", "r", _LO32, "r", "", "r")
  333. /* Instruction has only one explicit operand (no source operand). */
  334. #define emulate_1op(_op, _dst, _eflags) \
  335. do { \
  336. unsigned long _tmp; \
  337. \
  338. switch ( (_dst).bytes ) \
  339. { \
  340. case 1: \
  341. __asm__ __volatile__ ( \
  342. _PRE_EFLAGS("0","3","2") \
  343. _op"b %1; " \
  344. _POST_EFLAGS("0","3","2") \
  345. : "=m" (_eflags), "=m" ((_dst).val), \
  346. "=&r" (_tmp) \
  347. : "i" (EFLAGS_MASK) ); \
  348. break; \
  349. case 2: \
  350. __asm__ __volatile__ ( \
  351. _PRE_EFLAGS("0","3","2") \
  352. _op"w %1; " \
  353. _POST_EFLAGS("0","3","2") \
  354. : "=m" (_eflags), "=m" ((_dst).val), \
  355. "=&r" (_tmp) \
  356. : "i" (EFLAGS_MASK) ); \
  357. break; \
  358. case 4: \
  359. __asm__ __volatile__ ( \
  360. _PRE_EFLAGS("0","3","2") \
  361. _op"l %1; " \
  362. _POST_EFLAGS("0","3","2") \
  363. : "=m" (_eflags), "=m" ((_dst).val), \
  364. "=&r" (_tmp) \
  365. : "i" (EFLAGS_MASK) ); \
  366. break; \
  367. case 8: \
  368. __emulate_1op_8byte(_op, _dst, _eflags); \
  369. break; \
  370. } \
  371. } while (0)
  372. /* Emulate an instruction with quadword operands (x86/64 only). */
  373. #if defined(CONFIG_X86_64)
  374. #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \
  375. do { \
  376. __asm__ __volatile__ ( \
  377. _PRE_EFLAGS("0","4","2") \
  378. _op"q %"_qx"3,%1; " \
  379. _POST_EFLAGS("0","4","2") \
  380. : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
  381. : _qy ((_src).val), "i" (EFLAGS_MASK) ); \
  382. } while (0)
  383. #define __emulate_1op_8byte(_op, _dst, _eflags) \
  384. do { \
  385. __asm__ __volatile__ ( \
  386. _PRE_EFLAGS("0","3","2") \
  387. _op"q %1; " \
  388. _POST_EFLAGS("0","3","2") \
  389. : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \
  390. : "i" (EFLAGS_MASK) ); \
  391. } while (0)
  392. #elif defined(__i386__)
  393. #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy)
  394. #define __emulate_1op_8byte(_op, _dst, _eflags)
  395. #endif /* __i386__ */
  396. /* Fetch next part of the instruction being emulated. */
  397. #define insn_fetch(_type, _size, _eip) \
  398. ({ unsigned long _x; \
  399. rc = ops->read_std((unsigned long)(_eip) + ctxt->cs_base, &_x, \
  400. (_size), ctxt->vcpu); \
  401. if ( rc != 0 ) \
  402. goto done; \
  403. (_eip) += (_size); \
  404. (_type)_x; \
  405. })
  406. /* Access/update address held in a register, based on addressing mode. */
  407. #define register_address(base, reg) \
  408. ((base) + ((ad_bytes == sizeof(unsigned long)) ? (reg) : \
  409. ((reg) & ((1UL << (ad_bytes << 3)) - 1))))
  410. #define register_address_increment(reg, inc) \
  411. do { \
  412. /* signed type ensures sign extension to long */ \
  413. int _inc = (inc); \
  414. if ( ad_bytes == sizeof(unsigned long) ) \
  415. (reg) += _inc; \
  416. else \
  417. (reg) = ((reg) & ~((1UL << (ad_bytes << 3)) - 1)) | \
  418. (((reg) + _inc) & ((1UL << (ad_bytes << 3)) - 1)); \
  419. } while (0)
  420. /*
  421. * Given the 'reg' portion of a ModRM byte, and a register block, return a
  422. * pointer into the block that addresses the relevant register.
  423. * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
  424. */
  425. static void *decode_register(u8 modrm_reg, unsigned long *regs,
  426. int highbyte_regs)
  427. {
  428. void *p;
  429. p = &regs[modrm_reg];
  430. if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
  431. p = (unsigned char *)&regs[modrm_reg & 3] + 1;
  432. return p;
  433. }
  434. static int read_descriptor(struct x86_emulate_ctxt *ctxt,
  435. struct x86_emulate_ops *ops,
  436. void *ptr,
  437. u16 *size, unsigned long *address, int op_bytes)
  438. {
  439. int rc;
  440. if (op_bytes == 2)
  441. op_bytes = 3;
  442. *address = 0;
  443. rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
  444. ctxt->vcpu);
  445. if (rc)
  446. return rc;
  447. rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
  448. ctxt->vcpu);
  449. return rc;
  450. }
  451. int
  452. x86_emulate_memop(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
  453. {
  454. unsigned d;
  455. u8 b, sib, twobyte = 0, rex_prefix = 0;
  456. u8 modrm, modrm_mod = 0, modrm_reg = 0, modrm_rm = 0;
  457. unsigned long *override_base = NULL;
  458. unsigned int op_bytes, ad_bytes, lock_prefix = 0, rep_prefix = 0, i;
  459. int rc = 0;
  460. struct operand src, dst;
  461. unsigned long cr2 = ctxt->cr2;
  462. int mode = ctxt->mode;
  463. unsigned long modrm_ea;
  464. int use_modrm_ea, index_reg = 0, base_reg = 0, scale, rip_relative = 0;
  465. int no_wb = 0;
  466. u64 msr_data;
  467. /* Shadow copy of register state. Committed on successful emulation. */
  468. unsigned long _regs[NR_VCPU_REGS];
  469. unsigned long _eip = ctxt->vcpu->rip, _eflags = ctxt->eflags;
  470. unsigned long modrm_val = 0;
  471. memcpy(_regs, ctxt->vcpu->regs, sizeof _regs);
  472. switch (mode) {
  473. case X86EMUL_MODE_REAL:
  474. case X86EMUL_MODE_PROT16:
  475. op_bytes = ad_bytes = 2;
  476. break;
  477. case X86EMUL_MODE_PROT32:
  478. op_bytes = ad_bytes = 4;
  479. break;
  480. #ifdef CONFIG_X86_64
  481. case X86EMUL_MODE_PROT64:
  482. op_bytes = 4;
  483. ad_bytes = 8;
  484. break;
  485. #endif
  486. default:
  487. return -1;
  488. }
  489. /* Legacy prefixes. */
  490. for (i = 0; i < 8; i++) {
  491. switch (b = insn_fetch(u8, 1, _eip)) {
  492. case 0x66: /* operand-size override */
  493. op_bytes ^= 6; /* switch between 2/4 bytes */
  494. break;
  495. case 0x67: /* address-size override */
  496. if (mode == X86EMUL_MODE_PROT64)
  497. ad_bytes ^= 12; /* switch between 4/8 bytes */
  498. else
  499. ad_bytes ^= 6; /* switch between 2/4 bytes */
  500. break;
  501. case 0x2e: /* CS override */
  502. override_base = &ctxt->cs_base;
  503. break;
  504. case 0x3e: /* DS override */
  505. override_base = &ctxt->ds_base;
  506. break;
  507. case 0x26: /* ES override */
  508. override_base = &ctxt->es_base;
  509. break;
  510. case 0x64: /* FS override */
  511. override_base = &ctxt->fs_base;
  512. break;
  513. case 0x65: /* GS override */
  514. override_base = &ctxt->gs_base;
  515. break;
  516. case 0x36: /* SS override */
  517. override_base = &ctxt->ss_base;
  518. break;
  519. case 0xf0: /* LOCK */
  520. lock_prefix = 1;
  521. break;
  522. case 0xf3: /* REP/REPE/REPZ */
  523. rep_prefix = 1;
  524. break;
  525. case 0xf2: /* REPNE/REPNZ */
  526. break;
  527. default:
  528. goto done_prefixes;
  529. }
  530. }
  531. done_prefixes:
  532. /* REX prefix. */
  533. if ((mode == X86EMUL_MODE_PROT64) && ((b & 0xf0) == 0x40)) {
  534. rex_prefix = b;
  535. if (b & 8)
  536. op_bytes = 8; /* REX.W */
  537. modrm_reg = (b & 4) << 1; /* REX.R */
  538. index_reg = (b & 2) << 2; /* REX.X */
  539. modrm_rm = base_reg = (b & 1) << 3; /* REG.B */
  540. b = insn_fetch(u8, 1, _eip);
  541. }
  542. /* Opcode byte(s). */
  543. d = opcode_table[b];
  544. if (d == 0) {
  545. /* Two-byte opcode? */
  546. if (b == 0x0f) {
  547. twobyte = 1;
  548. b = insn_fetch(u8, 1, _eip);
  549. d = twobyte_table[b];
  550. }
  551. /* Unrecognised? */
  552. if (d == 0)
  553. goto cannot_emulate;
  554. }
  555. /* ModRM and SIB bytes. */
  556. if (d & ModRM) {
  557. modrm = insn_fetch(u8, 1, _eip);
  558. modrm_mod |= (modrm & 0xc0) >> 6;
  559. modrm_reg |= (modrm & 0x38) >> 3;
  560. modrm_rm |= (modrm & 0x07);
  561. modrm_ea = 0;
  562. use_modrm_ea = 1;
  563. if (modrm_mod == 3) {
  564. modrm_val = *(unsigned long *)
  565. decode_register(modrm_rm, _regs, d & ByteOp);
  566. goto modrm_done;
  567. }
  568. if (ad_bytes == 2) {
  569. unsigned bx = _regs[VCPU_REGS_RBX];
  570. unsigned bp = _regs[VCPU_REGS_RBP];
  571. unsigned si = _regs[VCPU_REGS_RSI];
  572. unsigned di = _regs[VCPU_REGS_RDI];
  573. /* 16-bit ModR/M decode. */
  574. switch (modrm_mod) {
  575. case 0:
  576. if (modrm_rm == 6)
  577. modrm_ea += insn_fetch(u16, 2, _eip);
  578. break;
  579. case 1:
  580. modrm_ea += insn_fetch(s8, 1, _eip);
  581. break;
  582. case 2:
  583. modrm_ea += insn_fetch(u16, 2, _eip);
  584. break;
  585. }
  586. switch (modrm_rm) {
  587. case 0:
  588. modrm_ea += bx + si;
  589. break;
  590. case 1:
  591. modrm_ea += bx + di;
  592. break;
  593. case 2:
  594. modrm_ea += bp + si;
  595. break;
  596. case 3:
  597. modrm_ea += bp + di;
  598. break;
  599. case 4:
  600. modrm_ea += si;
  601. break;
  602. case 5:
  603. modrm_ea += di;
  604. break;
  605. case 6:
  606. if (modrm_mod != 0)
  607. modrm_ea += bp;
  608. break;
  609. case 7:
  610. modrm_ea += bx;
  611. break;
  612. }
  613. if (modrm_rm == 2 || modrm_rm == 3 ||
  614. (modrm_rm == 6 && modrm_mod != 0))
  615. if (!override_base)
  616. override_base = &ctxt->ss_base;
  617. modrm_ea = (u16)modrm_ea;
  618. } else {
  619. /* 32/64-bit ModR/M decode. */
  620. switch (modrm_rm) {
  621. case 4:
  622. case 12:
  623. sib = insn_fetch(u8, 1, _eip);
  624. index_reg |= (sib >> 3) & 7;
  625. base_reg |= sib & 7;
  626. scale = sib >> 6;
  627. switch (base_reg) {
  628. case 5:
  629. if (modrm_mod != 0)
  630. modrm_ea += _regs[base_reg];
  631. else
  632. modrm_ea += insn_fetch(s32, 4, _eip);
  633. break;
  634. default:
  635. modrm_ea += _regs[base_reg];
  636. }
  637. switch (index_reg) {
  638. case 4:
  639. break;
  640. default:
  641. modrm_ea += _regs[index_reg] << scale;
  642. }
  643. break;
  644. case 5:
  645. if (modrm_mod != 0)
  646. modrm_ea += _regs[modrm_rm];
  647. else if (mode == X86EMUL_MODE_PROT64)
  648. rip_relative = 1;
  649. break;
  650. default:
  651. modrm_ea += _regs[modrm_rm];
  652. break;
  653. }
  654. switch (modrm_mod) {
  655. case 0:
  656. if (modrm_rm == 5)
  657. modrm_ea += insn_fetch(s32, 4, _eip);
  658. break;
  659. case 1:
  660. modrm_ea += insn_fetch(s8, 1, _eip);
  661. break;
  662. case 2:
  663. modrm_ea += insn_fetch(s32, 4, _eip);
  664. break;
  665. }
  666. }
  667. if (!override_base)
  668. override_base = &ctxt->ds_base;
  669. if (mode == X86EMUL_MODE_PROT64 &&
  670. override_base != &ctxt->fs_base &&
  671. override_base != &ctxt->gs_base)
  672. override_base = NULL;
  673. if (override_base)
  674. modrm_ea += *override_base;
  675. if (rip_relative) {
  676. modrm_ea += _eip;
  677. switch (d & SrcMask) {
  678. case SrcImmByte:
  679. modrm_ea += 1;
  680. break;
  681. case SrcImm:
  682. if (d & ByteOp)
  683. modrm_ea += 1;
  684. else
  685. if (op_bytes == 8)
  686. modrm_ea += 4;
  687. else
  688. modrm_ea += op_bytes;
  689. }
  690. }
  691. if (ad_bytes != 8)
  692. modrm_ea = (u32)modrm_ea;
  693. cr2 = modrm_ea;
  694. modrm_done:
  695. ;
  696. }
  697. /*
  698. * Decode and fetch the source operand: register, memory
  699. * or immediate.
  700. */
  701. switch (d & SrcMask) {
  702. case SrcNone:
  703. break;
  704. case SrcReg:
  705. src.type = OP_REG;
  706. if (d & ByteOp) {
  707. src.ptr = decode_register(modrm_reg, _regs,
  708. (rex_prefix == 0));
  709. src.val = src.orig_val = *(u8 *) src.ptr;
  710. src.bytes = 1;
  711. } else {
  712. src.ptr = decode_register(modrm_reg, _regs, 0);
  713. switch ((src.bytes = op_bytes)) {
  714. case 2:
  715. src.val = src.orig_val = *(u16 *) src.ptr;
  716. break;
  717. case 4:
  718. src.val = src.orig_val = *(u32 *) src.ptr;
  719. break;
  720. case 8:
  721. src.val = src.orig_val = *(u64 *) src.ptr;
  722. break;
  723. }
  724. }
  725. break;
  726. case SrcMem16:
  727. src.bytes = 2;
  728. goto srcmem_common;
  729. case SrcMem32:
  730. src.bytes = 4;
  731. goto srcmem_common;
  732. case SrcMem:
  733. src.bytes = (d & ByteOp) ? 1 : op_bytes;
  734. srcmem_common:
  735. src.type = OP_MEM;
  736. src.ptr = (unsigned long *)cr2;
  737. if ((rc = ops->read_emulated((unsigned long)src.ptr,
  738. &src.val, src.bytes, ctxt->vcpu)) != 0)
  739. goto done;
  740. src.orig_val = src.val;
  741. break;
  742. case SrcImm:
  743. src.type = OP_IMM;
  744. src.ptr = (unsigned long *)_eip;
  745. src.bytes = (d & ByteOp) ? 1 : op_bytes;
  746. if (src.bytes == 8)
  747. src.bytes = 4;
  748. /* NB. Immediates are sign-extended as necessary. */
  749. switch (src.bytes) {
  750. case 1:
  751. src.val = insn_fetch(s8, 1, _eip);
  752. break;
  753. case 2:
  754. src.val = insn_fetch(s16, 2, _eip);
  755. break;
  756. case 4:
  757. src.val = insn_fetch(s32, 4, _eip);
  758. break;
  759. }
  760. break;
  761. case SrcImmByte:
  762. src.type = OP_IMM;
  763. src.ptr = (unsigned long *)_eip;
  764. src.bytes = 1;
  765. src.val = insn_fetch(s8, 1, _eip);
  766. break;
  767. }
  768. /* Decode and fetch the destination operand: register or memory. */
  769. switch (d & DstMask) {
  770. case ImplicitOps:
  771. /* Special instructions do their own operand decoding. */
  772. goto special_insn;
  773. case DstReg:
  774. dst.type = OP_REG;
  775. if ((d & ByteOp)
  776. && !(twobyte && (b == 0xb6 || b == 0xb7))) {
  777. dst.ptr = decode_register(modrm_reg, _regs,
  778. (rex_prefix == 0));
  779. dst.val = *(u8 *) dst.ptr;
  780. dst.bytes = 1;
  781. } else {
  782. dst.ptr = decode_register(modrm_reg, _regs, 0);
  783. switch ((dst.bytes = op_bytes)) {
  784. case 2:
  785. dst.val = *(u16 *)dst.ptr;
  786. break;
  787. case 4:
  788. dst.val = *(u32 *)dst.ptr;
  789. break;
  790. case 8:
  791. dst.val = *(u64 *)dst.ptr;
  792. break;
  793. }
  794. }
  795. break;
  796. case DstMem:
  797. dst.type = OP_MEM;
  798. dst.ptr = (unsigned long *)cr2;
  799. dst.bytes = (d & ByteOp) ? 1 : op_bytes;
  800. if (d & BitOp) {
  801. unsigned long mask = ~(dst.bytes * 8 - 1);
  802. dst.ptr = (void *)dst.ptr + (src.val & mask) / 8;
  803. }
  804. if (!(d & Mov) && /* optimisation - avoid slow emulated read */
  805. ((rc = ops->read_emulated((unsigned long)dst.ptr,
  806. &dst.val, dst.bytes, ctxt->vcpu)) != 0))
  807. goto done;
  808. break;
  809. }
  810. dst.orig_val = dst.val;
  811. if (twobyte)
  812. goto twobyte_insn;
  813. switch (b) {
  814. case 0x00 ... 0x05:
  815. add: /* add */
  816. emulate_2op_SrcV("add", src, dst, _eflags);
  817. break;
  818. case 0x08 ... 0x0d:
  819. or: /* or */
  820. emulate_2op_SrcV("or", src, dst, _eflags);
  821. break;
  822. case 0x10 ... 0x15:
  823. adc: /* adc */
  824. emulate_2op_SrcV("adc", src, dst, _eflags);
  825. break;
  826. case 0x18 ... 0x1d:
  827. sbb: /* sbb */
  828. emulate_2op_SrcV("sbb", src, dst, _eflags);
  829. break;
  830. case 0x20 ... 0x25:
  831. and: /* and */
  832. emulate_2op_SrcV("and", src, dst, _eflags);
  833. break;
  834. case 0x28 ... 0x2d:
  835. sub: /* sub */
  836. emulate_2op_SrcV("sub", src, dst, _eflags);
  837. break;
  838. case 0x30 ... 0x35:
  839. xor: /* xor */
  840. emulate_2op_SrcV("xor", src, dst, _eflags);
  841. break;
  842. case 0x38 ... 0x3d:
  843. cmp: /* cmp */
  844. emulate_2op_SrcV("cmp", src, dst, _eflags);
  845. break;
  846. case 0x63: /* movsxd */
  847. if (mode != X86EMUL_MODE_PROT64)
  848. goto cannot_emulate;
  849. dst.val = (s32) src.val;
  850. break;
  851. case 0x80 ... 0x83: /* Grp1 */
  852. switch (modrm_reg) {
  853. case 0:
  854. goto add;
  855. case 1:
  856. goto or;
  857. case 2:
  858. goto adc;
  859. case 3:
  860. goto sbb;
  861. case 4:
  862. goto and;
  863. case 5:
  864. goto sub;
  865. case 6:
  866. goto xor;
  867. case 7:
  868. goto cmp;
  869. }
  870. break;
  871. case 0x84 ... 0x85:
  872. test: /* test */
  873. emulate_2op_SrcV("test", src, dst, _eflags);
  874. break;
  875. case 0x86 ... 0x87: /* xchg */
  876. /* Write back the register source. */
  877. switch (dst.bytes) {
  878. case 1:
  879. *(u8 *) src.ptr = (u8) dst.val;
  880. break;
  881. case 2:
  882. *(u16 *) src.ptr = (u16) dst.val;
  883. break;
  884. case 4:
  885. *src.ptr = (u32) dst.val;
  886. break; /* 64b reg: zero-extend */
  887. case 8:
  888. *src.ptr = dst.val;
  889. break;
  890. }
  891. /*
  892. * Write back the memory destination with implicit LOCK
  893. * prefix.
  894. */
  895. dst.val = src.val;
  896. lock_prefix = 1;
  897. break;
  898. case 0xa0 ... 0xa1: /* mov */
  899. dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
  900. dst.val = src.val;
  901. _eip += ad_bytes; /* skip src displacement */
  902. break;
  903. case 0xa2 ... 0xa3: /* mov */
  904. dst.val = (unsigned long)_regs[VCPU_REGS_RAX];
  905. _eip += ad_bytes; /* skip dst displacement */
  906. break;
  907. case 0x88 ... 0x8b: /* mov */
  908. case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
  909. dst.val = src.val;
  910. break;
  911. case 0x8f: /* pop (sole member of Grp1a) */
  912. /* 64-bit mode: POP always pops a 64-bit operand. */
  913. if (mode == X86EMUL_MODE_PROT64)
  914. dst.bytes = 8;
  915. if ((rc = ops->read_std(register_address(ctxt->ss_base,
  916. _regs[VCPU_REGS_RSP]),
  917. &dst.val, dst.bytes, ctxt->vcpu)) != 0)
  918. goto done;
  919. register_address_increment(_regs[VCPU_REGS_RSP], dst.bytes);
  920. break;
  921. case 0xc0 ... 0xc1:
  922. grp2: /* Grp2 */
  923. switch (modrm_reg) {
  924. case 0: /* rol */
  925. emulate_2op_SrcB("rol", src, dst, _eflags);
  926. break;
  927. case 1: /* ror */
  928. emulate_2op_SrcB("ror", src, dst, _eflags);
  929. break;
  930. case 2: /* rcl */
  931. emulate_2op_SrcB("rcl", src, dst, _eflags);
  932. break;
  933. case 3: /* rcr */
  934. emulate_2op_SrcB("rcr", src, dst, _eflags);
  935. break;
  936. case 4: /* sal/shl */
  937. case 6: /* sal/shl */
  938. emulate_2op_SrcB("sal", src, dst, _eflags);
  939. break;
  940. case 5: /* shr */
  941. emulate_2op_SrcB("shr", src, dst, _eflags);
  942. break;
  943. case 7: /* sar */
  944. emulate_2op_SrcB("sar", src, dst, _eflags);
  945. break;
  946. }
  947. break;
  948. case 0xd0 ... 0xd1: /* Grp2 */
  949. src.val = 1;
  950. goto grp2;
  951. case 0xd2 ... 0xd3: /* Grp2 */
  952. src.val = _regs[VCPU_REGS_RCX];
  953. goto grp2;
  954. case 0xf6 ... 0xf7: /* Grp3 */
  955. switch (modrm_reg) {
  956. case 0 ... 1: /* test */
  957. /*
  958. * Special case in Grp3: test has an immediate
  959. * source operand.
  960. */
  961. src.type = OP_IMM;
  962. src.ptr = (unsigned long *)_eip;
  963. src.bytes = (d & ByteOp) ? 1 : op_bytes;
  964. if (src.bytes == 8)
  965. src.bytes = 4;
  966. switch (src.bytes) {
  967. case 1:
  968. src.val = insn_fetch(s8, 1, _eip);
  969. break;
  970. case 2:
  971. src.val = insn_fetch(s16, 2, _eip);
  972. break;
  973. case 4:
  974. src.val = insn_fetch(s32, 4, _eip);
  975. break;
  976. }
  977. goto test;
  978. case 2: /* not */
  979. dst.val = ~dst.val;
  980. break;
  981. case 3: /* neg */
  982. emulate_1op("neg", dst, _eflags);
  983. break;
  984. default:
  985. goto cannot_emulate;
  986. }
  987. break;
  988. case 0xfe ... 0xff: /* Grp4/Grp5 */
  989. switch (modrm_reg) {
  990. case 0: /* inc */
  991. emulate_1op("inc", dst, _eflags);
  992. break;
  993. case 1: /* dec */
  994. emulate_1op("dec", dst, _eflags);
  995. break;
  996. case 6: /* push */
  997. /* 64-bit mode: PUSH always pushes a 64-bit operand. */
  998. if (mode == X86EMUL_MODE_PROT64) {
  999. dst.bytes = 8;
  1000. if ((rc = ops->read_std((unsigned long)dst.ptr,
  1001. &dst.val, 8,
  1002. ctxt->vcpu)) != 0)
  1003. goto done;
  1004. }
  1005. register_address_increment(_regs[VCPU_REGS_RSP],
  1006. -dst.bytes);
  1007. if ((rc = ops->write_std(
  1008. register_address(ctxt->ss_base,
  1009. _regs[VCPU_REGS_RSP]),
  1010. &dst.val, dst.bytes, ctxt->vcpu)) != 0)
  1011. goto done;
  1012. no_wb = 1;
  1013. break;
  1014. default:
  1015. goto cannot_emulate;
  1016. }
  1017. break;
  1018. }
  1019. writeback:
  1020. if (!no_wb) {
  1021. switch (dst.type) {
  1022. case OP_REG:
  1023. /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
  1024. switch (dst.bytes) {
  1025. case 1:
  1026. *(u8 *)dst.ptr = (u8)dst.val;
  1027. break;
  1028. case 2:
  1029. *(u16 *)dst.ptr = (u16)dst.val;
  1030. break;
  1031. case 4:
  1032. *dst.ptr = (u32)dst.val;
  1033. break; /* 64b: zero-ext */
  1034. case 8:
  1035. *dst.ptr = dst.val;
  1036. break;
  1037. }
  1038. break;
  1039. case OP_MEM:
  1040. if (lock_prefix)
  1041. rc = ops->cmpxchg_emulated((unsigned long)dst.
  1042. ptr, &dst.orig_val,
  1043. &dst.val, dst.bytes,
  1044. ctxt->vcpu);
  1045. else
  1046. rc = ops->write_emulated((unsigned long)dst.ptr,
  1047. &dst.val, dst.bytes,
  1048. ctxt->vcpu);
  1049. if (rc != 0)
  1050. goto done;
  1051. default:
  1052. break;
  1053. }
  1054. }
  1055. /* Commit shadow register state. */
  1056. memcpy(ctxt->vcpu->regs, _regs, sizeof _regs);
  1057. ctxt->eflags = _eflags;
  1058. ctxt->vcpu->rip = _eip;
  1059. done:
  1060. return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
  1061. special_insn:
  1062. if (twobyte)
  1063. goto twobyte_special_insn;
  1064. if (rep_prefix) {
  1065. if (_regs[VCPU_REGS_RCX] == 0) {
  1066. ctxt->vcpu->rip = _eip;
  1067. goto done;
  1068. }
  1069. _regs[VCPU_REGS_RCX]--;
  1070. _eip = ctxt->vcpu->rip;
  1071. }
  1072. switch (b) {
  1073. case 0xa4 ... 0xa5: /* movs */
  1074. dst.type = OP_MEM;
  1075. dst.bytes = (d & ByteOp) ? 1 : op_bytes;
  1076. dst.ptr = (unsigned long *)register_address(ctxt->es_base,
  1077. _regs[VCPU_REGS_RDI]);
  1078. if ((rc = ops->read_emulated(register_address(
  1079. override_base ? *override_base : ctxt->ds_base,
  1080. _regs[VCPU_REGS_RSI]), &dst.val, dst.bytes, ctxt->vcpu)) != 0)
  1081. goto done;
  1082. register_address_increment(_regs[VCPU_REGS_RSI],
  1083. (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
  1084. register_address_increment(_regs[VCPU_REGS_RDI],
  1085. (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
  1086. break;
  1087. case 0xa6 ... 0xa7: /* cmps */
  1088. DPRINTF("Urk! I don't handle CMPS.\n");
  1089. goto cannot_emulate;
  1090. case 0xaa ... 0xab: /* stos */
  1091. dst.type = OP_MEM;
  1092. dst.bytes = (d & ByteOp) ? 1 : op_bytes;
  1093. dst.ptr = (unsigned long *)cr2;
  1094. dst.val = _regs[VCPU_REGS_RAX];
  1095. register_address_increment(_regs[VCPU_REGS_RDI],
  1096. (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
  1097. break;
  1098. case 0xac ... 0xad: /* lods */
  1099. dst.type = OP_REG;
  1100. dst.bytes = (d & ByteOp) ? 1 : op_bytes;
  1101. dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
  1102. if ((rc = ops->read_emulated(cr2, &dst.val, dst.bytes,
  1103. ctxt->vcpu)) != 0)
  1104. goto done;
  1105. register_address_increment(_regs[VCPU_REGS_RSI],
  1106. (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
  1107. break;
  1108. case 0xae ... 0xaf: /* scas */
  1109. DPRINTF("Urk! I don't handle SCAS.\n");
  1110. goto cannot_emulate;
  1111. case 0xf4: /* hlt */
  1112. ctxt->vcpu->halt_request = 1;
  1113. goto done;
  1114. case 0xc3: /* ret */
  1115. dst.ptr = &_eip;
  1116. goto pop_instruction;
  1117. case 0x58 ... 0x5f: /* pop reg */
  1118. dst.ptr = (unsigned long *)&_regs[b & 0x7];
  1119. pop_instruction:
  1120. if ((rc = ops->read_std(register_address(ctxt->ss_base,
  1121. _regs[VCPU_REGS_RSP]), dst.ptr, op_bytes, ctxt->vcpu))
  1122. != 0)
  1123. goto done;
  1124. register_address_increment(_regs[VCPU_REGS_RSP], op_bytes);
  1125. no_wb = 1; /* Disable writeback. */
  1126. break;
  1127. }
  1128. goto writeback;
  1129. twobyte_insn:
  1130. switch (b) {
  1131. case 0x01: /* lgdt, lidt, lmsw */
  1132. /* Disable writeback. */
  1133. no_wb = 1;
  1134. switch (modrm_reg) {
  1135. u16 size;
  1136. unsigned long address;
  1137. case 2: /* lgdt */
  1138. rc = read_descriptor(ctxt, ops, src.ptr,
  1139. &size, &address, op_bytes);
  1140. if (rc)
  1141. goto done;
  1142. realmode_lgdt(ctxt->vcpu, size, address);
  1143. break;
  1144. case 3: /* lidt */
  1145. rc = read_descriptor(ctxt, ops, src.ptr,
  1146. &size, &address, op_bytes);
  1147. if (rc)
  1148. goto done;
  1149. realmode_lidt(ctxt->vcpu, size, address);
  1150. break;
  1151. case 4: /* smsw */
  1152. if (modrm_mod != 3)
  1153. goto cannot_emulate;
  1154. *(u16 *)&_regs[modrm_rm]
  1155. = realmode_get_cr(ctxt->vcpu, 0);
  1156. break;
  1157. case 6: /* lmsw */
  1158. if (modrm_mod != 3)
  1159. goto cannot_emulate;
  1160. realmode_lmsw(ctxt->vcpu, (u16)modrm_val, &_eflags);
  1161. break;
  1162. case 7: /* invlpg*/
  1163. emulate_invlpg(ctxt->vcpu, cr2);
  1164. break;
  1165. default:
  1166. goto cannot_emulate;
  1167. }
  1168. break;
  1169. case 0x21: /* mov from dr to reg */
  1170. no_wb = 1;
  1171. if (modrm_mod != 3)
  1172. goto cannot_emulate;
  1173. rc = emulator_get_dr(ctxt, modrm_reg, &_regs[modrm_rm]);
  1174. break;
  1175. case 0x23: /* mov from reg to dr */
  1176. no_wb = 1;
  1177. if (modrm_mod != 3)
  1178. goto cannot_emulate;
  1179. rc = emulator_set_dr(ctxt, modrm_reg, _regs[modrm_rm]);
  1180. break;
  1181. case 0x40 ... 0x4f: /* cmov */
  1182. dst.val = dst.orig_val = src.val;
  1183. no_wb = 1;
  1184. /*
  1185. * First, assume we're decoding an even cmov opcode
  1186. * (lsb == 0).
  1187. */
  1188. switch ((b & 15) >> 1) {
  1189. case 0: /* cmovo */
  1190. no_wb = (_eflags & EFLG_OF) ? 0 : 1;
  1191. break;
  1192. case 1: /* cmovb/cmovc/cmovnae */
  1193. no_wb = (_eflags & EFLG_CF) ? 0 : 1;
  1194. break;
  1195. case 2: /* cmovz/cmove */
  1196. no_wb = (_eflags & EFLG_ZF) ? 0 : 1;
  1197. break;
  1198. case 3: /* cmovbe/cmovna */
  1199. no_wb = (_eflags & (EFLG_CF | EFLG_ZF)) ? 0 : 1;
  1200. break;
  1201. case 4: /* cmovs */
  1202. no_wb = (_eflags & EFLG_SF) ? 0 : 1;
  1203. break;
  1204. case 5: /* cmovp/cmovpe */
  1205. no_wb = (_eflags & EFLG_PF) ? 0 : 1;
  1206. break;
  1207. case 7: /* cmovle/cmovng */
  1208. no_wb = (_eflags & EFLG_ZF) ? 0 : 1;
  1209. /* fall through */
  1210. case 6: /* cmovl/cmovnge */
  1211. no_wb &= (!(_eflags & EFLG_SF) !=
  1212. !(_eflags & EFLG_OF)) ? 0 : 1;
  1213. break;
  1214. }
  1215. /* Odd cmov opcodes (lsb == 1) have inverted sense. */
  1216. no_wb ^= b & 1;
  1217. break;
  1218. case 0xb0 ... 0xb1: /* cmpxchg */
  1219. /*
  1220. * Save real source value, then compare EAX against
  1221. * destination.
  1222. */
  1223. src.orig_val = src.val;
  1224. src.val = _regs[VCPU_REGS_RAX];
  1225. emulate_2op_SrcV("cmp", src, dst, _eflags);
  1226. if (_eflags & EFLG_ZF) {
  1227. /* Success: write back to memory. */
  1228. dst.val = src.orig_val;
  1229. } else {
  1230. /* Failure: write the value we saw to EAX. */
  1231. dst.type = OP_REG;
  1232. dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
  1233. }
  1234. break;
  1235. case 0xa3:
  1236. bt: /* bt */
  1237. src.val &= (dst.bytes << 3) - 1; /* only subword offset */
  1238. emulate_2op_SrcV_nobyte("bt", src, dst, _eflags);
  1239. break;
  1240. case 0xb3:
  1241. btr: /* btr */
  1242. src.val &= (dst.bytes << 3) - 1; /* only subword offset */
  1243. emulate_2op_SrcV_nobyte("btr", src, dst, _eflags);
  1244. break;
  1245. case 0xab:
  1246. bts: /* bts */
  1247. src.val &= (dst.bytes << 3) - 1; /* only subword offset */
  1248. emulate_2op_SrcV_nobyte("bts", src, dst, _eflags);
  1249. break;
  1250. case 0xb6 ... 0xb7: /* movzx */
  1251. dst.bytes = op_bytes;
  1252. dst.val = (d & ByteOp) ? (u8) src.val : (u16) src.val;
  1253. break;
  1254. case 0xbb:
  1255. btc: /* btc */
  1256. src.val &= (dst.bytes << 3) - 1; /* only subword offset */
  1257. emulate_2op_SrcV_nobyte("btc", src, dst, _eflags);
  1258. break;
  1259. case 0xba: /* Grp8 */
  1260. switch (modrm_reg & 3) {
  1261. case 0:
  1262. goto bt;
  1263. case 1:
  1264. goto bts;
  1265. case 2:
  1266. goto btr;
  1267. case 3:
  1268. goto btc;
  1269. }
  1270. break;
  1271. case 0xbe ... 0xbf: /* movsx */
  1272. dst.bytes = op_bytes;
  1273. dst.val = (d & ByteOp) ? (s8) src.val : (s16) src.val;
  1274. break;
  1275. }
  1276. goto writeback;
  1277. twobyte_special_insn:
  1278. /* Disable writeback. */
  1279. no_wb = 1;
  1280. switch (b) {
  1281. case 0x09: /* wbinvd */
  1282. break;
  1283. case 0x0d: /* GrpP (prefetch) */
  1284. case 0x18: /* Grp16 (prefetch/nop) */
  1285. break;
  1286. case 0x06:
  1287. emulate_clts(ctxt->vcpu);
  1288. break;
  1289. case 0x20: /* mov cr, reg */
  1290. if (modrm_mod != 3)
  1291. goto cannot_emulate;
  1292. _regs[modrm_rm] = realmode_get_cr(ctxt->vcpu, modrm_reg);
  1293. break;
  1294. case 0x22: /* mov reg, cr */
  1295. if (modrm_mod != 3)
  1296. goto cannot_emulate;
  1297. realmode_set_cr(ctxt->vcpu, modrm_reg, modrm_val, &_eflags);
  1298. break;
  1299. case 0x30:
  1300. /* wrmsr */
  1301. msr_data = (u32)_regs[VCPU_REGS_RAX]
  1302. | ((u64)_regs[VCPU_REGS_RDX] << 32);
  1303. rc = kvm_set_msr(ctxt->vcpu, _regs[VCPU_REGS_RCX], msr_data);
  1304. if (rc) {
  1305. kvm_arch_ops->inject_gp(ctxt->vcpu, 0);
  1306. _eip = ctxt->vcpu->rip;
  1307. }
  1308. rc = X86EMUL_CONTINUE;
  1309. break;
  1310. case 0x32:
  1311. /* rdmsr */
  1312. rc = kvm_get_msr(ctxt->vcpu, _regs[VCPU_REGS_RCX], &msr_data);
  1313. if (rc) {
  1314. kvm_arch_ops->inject_gp(ctxt->vcpu, 0);
  1315. _eip = ctxt->vcpu->rip;
  1316. } else {
  1317. _regs[VCPU_REGS_RAX] = (u32)msr_data;
  1318. _regs[VCPU_REGS_RDX] = msr_data >> 32;
  1319. }
  1320. rc = X86EMUL_CONTINUE;
  1321. break;
  1322. case 0xc7: /* Grp9 (cmpxchg8b) */
  1323. {
  1324. u64 old, new;
  1325. if ((rc = ops->read_emulated(cr2, &old, 8, ctxt->vcpu))
  1326. != 0)
  1327. goto done;
  1328. if (((u32) (old >> 0) != (u32) _regs[VCPU_REGS_RAX]) ||
  1329. ((u32) (old >> 32) != (u32) _regs[VCPU_REGS_RDX])) {
  1330. _regs[VCPU_REGS_RAX] = (u32) (old >> 0);
  1331. _regs[VCPU_REGS_RDX] = (u32) (old >> 32);
  1332. _eflags &= ~EFLG_ZF;
  1333. } else {
  1334. new = ((u64)_regs[VCPU_REGS_RCX] << 32)
  1335. | (u32) _regs[VCPU_REGS_RBX];
  1336. if ((rc = ops->cmpxchg_emulated(cr2, &old,
  1337. &new, 8, ctxt->vcpu)) != 0)
  1338. goto done;
  1339. _eflags |= EFLG_ZF;
  1340. }
  1341. break;
  1342. }
  1343. }
  1344. goto writeback;
  1345. cannot_emulate:
  1346. DPRINTF("Cannot emulate %02x\n", b);
  1347. return -1;
  1348. }
  1349. #ifdef __XEN__
  1350. #include <asm/mm.h>
  1351. #include <asm/uaccess.h>
  1352. int
  1353. x86_emulate_read_std(unsigned long addr,
  1354. unsigned long *val,
  1355. unsigned int bytes, struct x86_emulate_ctxt *ctxt)
  1356. {
  1357. unsigned int rc;
  1358. *val = 0;
  1359. if ((rc = copy_from_user((void *)val, (void *)addr, bytes)) != 0) {
  1360. propagate_page_fault(addr + bytes - rc, 0); /* read fault */
  1361. return X86EMUL_PROPAGATE_FAULT;
  1362. }
  1363. return X86EMUL_CONTINUE;
  1364. }
  1365. int
  1366. x86_emulate_write_std(unsigned long addr,
  1367. unsigned long val,
  1368. unsigned int bytes, struct x86_emulate_ctxt *ctxt)
  1369. {
  1370. unsigned int rc;
  1371. if ((rc = copy_to_user((void *)addr, (void *)&val, bytes)) != 0) {
  1372. propagate_page_fault(addr + bytes - rc, PGERR_write_access);
  1373. return X86EMUL_PROPAGATE_FAULT;
  1374. }
  1375. return X86EMUL_CONTINUE;
  1376. }
  1377. #endif