x86_emulate.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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. * privieged 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. 0, 0, 0, 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); \
  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. void *decode_register(u8 modrm_reg, unsigned long *regs,
  421. int highbyte_regs)
  422. {
  423. void *p;
  424. p = &regs[modrm_reg];
  425. if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
  426. p = (unsigned char *)&regs[modrm_reg & 3] + 1;
  427. return p;
  428. }
  429. static int read_descriptor(struct x86_emulate_ctxt *ctxt,
  430. struct x86_emulate_ops *ops,
  431. void *ptr,
  432. u16 *size, unsigned long *address, int op_bytes)
  433. {
  434. int rc;
  435. if (op_bytes == 2)
  436. op_bytes = 3;
  437. *address = 0;
  438. rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2, ctxt);
  439. if (rc)
  440. return rc;
  441. rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes, ctxt);
  442. return rc;
  443. }
  444. int
  445. x86_emulate_memop(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
  446. {
  447. unsigned d;
  448. u8 b, sib, twobyte = 0, rex_prefix = 0;
  449. u8 modrm, modrm_mod = 0, modrm_reg = 0, modrm_rm = 0;
  450. unsigned long *override_base = NULL;
  451. unsigned int op_bytes, ad_bytes, lock_prefix = 0, rep_prefix = 0, i;
  452. int rc = 0;
  453. struct operand src, dst;
  454. unsigned long cr2 = ctxt->cr2;
  455. int mode = ctxt->mode;
  456. unsigned long modrm_ea;
  457. int use_modrm_ea, index_reg = 0, base_reg = 0, scale, rip_relative = 0;
  458. int no_wb = 0;
  459. /* Shadow copy of register state. Committed on successful emulation. */
  460. unsigned long _regs[NR_VCPU_REGS];
  461. unsigned long _eip = ctxt->vcpu->rip, _eflags = ctxt->eflags;
  462. unsigned long modrm_val = 0;
  463. memcpy(_regs, ctxt->vcpu->regs, sizeof _regs);
  464. switch (mode) {
  465. case X86EMUL_MODE_REAL:
  466. case X86EMUL_MODE_PROT16:
  467. op_bytes = ad_bytes = 2;
  468. break;
  469. case X86EMUL_MODE_PROT32:
  470. op_bytes = ad_bytes = 4;
  471. break;
  472. #ifdef CONFIG_X86_64
  473. case X86EMUL_MODE_PROT64:
  474. op_bytes = 4;
  475. ad_bytes = 8;
  476. break;
  477. #endif
  478. default:
  479. return -1;
  480. }
  481. /* Legacy prefixes. */
  482. for (i = 0; i < 8; i++) {
  483. switch (b = insn_fetch(u8, 1, _eip)) {
  484. case 0x66: /* operand-size override */
  485. op_bytes ^= 6; /* switch between 2/4 bytes */
  486. break;
  487. case 0x67: /* address-size override */
  488. if (mode == X86EMUL_MODE_PROT64)
  489. ad_bytes ^= 12; /* switch between 4/8 bytes */
  490. else
  491. ad_bytes ^= 6; /* switch between 2/4 bytes */
  492. break;
  493. case 0x2e: /* CS override */
  494. override_base = &ctxt->cs_base;
  495. break;
  496. case 0x3e: /* DS override */
  497. override_base = &ctxt->ds_base;
  498. break;
  499. case 0x26: /* ES override */
  500. override_base = &ctxt->es_base;
  501. break;
  502. case 0x64: /* FS override */
  503. override_base = &ctxt->fs_base;
  504. break;
  505. case 0x65: /* GS override */
  506. override_base = &ctxt->gs_base;
  507. break;
  508. case 0x36: /* SS override */
  509. override_base = &ctxt->ss_base;
  510. break;
  511. case 0xf0: /* LOCK */
  512. lock_prefix = 1;
  513. break;
  514. case 0xf3: /* REP/REPE/REPZ */
  515. rep_prefix = 1;
  516. break;
  517. case 0xf2: /* REPNE/REPNZ */
  518. break;
  519. default:
  520. goto done_prefixes;
  521. }
  522. }
  523. done_prefixes:
  524. /* REX prefix. */
  525. if ((mode == X86EMUL_MODE_PROT64) && ((b & 0xf0) == 0x40)) {
  526. rex_prefix = b;
  527. if (b & 8)
  528. op_bytes = 8; /* REX.W */
  529. modrm_reg = (b & 4) << 1; /* REX.R */
  530. index_reg = (b & 2) << 2; /* REX.X */
  531. modrm_rm = base_reg = (b & 1) << 3; /* REG.B */
  532. b = insn_fetch(u8, 1, _eip);
  533. }
  534. /* Opcode byte(s). */
  535. d = opcode_table[b];
  536. if (d == 0) {
  537. /* Two-byte opcode? */
  538. if (b == 0x0f) {
  539. twobyte = 1;
  540. b = insn_fetch(u8, 1, _eip);
  541. d = twobyte_table[b];
  542. }
  543. /* Unrecognised? */
  544. if (d == 0)
  545. goto cannot_emulate;
  546. }
  547. /* ModRM and SIB bytes. */
  548. if (d & ModRM) {
  549. modrm = insn_fetch(u8, 1, _eip);
  550. modrm_mod |= (modrm & 0xc0) >> 6;
  551. modrm_reg |= (modrm & 0x38) >> 3;
  552. modrm_rm |= (modrm & 0x07);
  553. modrm_ea = 0;
  554. use_modrm_ea = 1;
  555. if (modrm_mod == 3) {
  556. modrm_val = *(unsigned long *)
  557. decode_register(modrm_rm, _regs, d & ByteOp);
  558. goto modrm_done;
  559. }
  560. if (ad_bytes == 2) {
  561. unsigned bx = _regs[VCPU_REGS_RBX];
  562. unsigned bp = _regs[VCPU_REGS_RBP];
  563. unsigned si = _regs[VCPU_REGS_RSI];
  564. unsigned di = _regs[VCPU_REGS_RDI];
  565. /* 16-bit ModR/M decode. */
  566. switch (modrm_mod) {
  567. case 0:
  568. if (modrm_rm == 6)
  569. modrm_ea += insn_fetch(u16, 2, _eip);
  570. break;
  571. case 1:
  572. modrm_ea += insn_fetch(s8, 1, _eip);
  573. break;
  574. case 2:
  575. modrm_ea += insn_fetch(u16, 2, _eip);
  576. break;
  577. }
  578. switch (modrm_rm) {
  579. case 0:
  580. modrm_ea += bx + si;
  581. break;
  582. case 1:
  583. modrm_ea += bx + di;
  584. break;
  585. case 2:
  586. modrm_ea += bp + si;
  587. break;
  588. case 3:
  589. modrm_ea += bp + di;
  590. break;
  591. case 4:
  592. modrm_ea += si;
  593. break;
  594. case 5:
  595. modrm_ea += di;
  596. break;
  597. case 6:
  598. if (modrm_mod != 0)
  599. modrm_ea += bp;
  600. break;
  601. case 7:
  602. modrm_ea += bx;
  603. break;
  604. }
  605. if (modrm_rm == 2 || modrm_rm == 3 ||
  606. (modrm_rm == 6 && modrm_mod != 0))
  607. if (!override_base)
  608. override_base = &ctxt->ss_base;
  609. modrm_ea = (u16)modrm_ea;
  610. } else {
  611. /* 32/64-bit ModR/M decode. */
  612. switch (modrm_rm) {
  613. case 4:
  614. case 12:
  615. sib = insn_fetch(u8, 1, _eip);
  616. index_reg |= (sib >> 3) & 7;
  617. base_reg |= sib & 7;
  618. scale = sib >> 6;
  619. switch (base_reg) {
  620. case 5:
  621. if (modrm_mod != 0)
  622. modrm_ea += _regs[base_reg];
  623. else
  624. modrm_ea += insn_fetch(s32, 4, _eip);
  625. break;
  626. default:
  627. modrm_ea += _regs[base_reg];
  628. }
  629. switch (index_reg) {
  630. case 4:
  631. break;
  632. default:
  633. modrm_ea += _regs[index_reg] << scale;
  634. }
  635. break;
  636. case 5:
  637. if (modrm_mod != 0)
  638. modrm_ea += _regs[modrm_rm];
  639. else if (mode == X86EMUL_MODE_PROT64)
  640. rip_relative = 1;
  641. break;
  642. default:
  643. modrm_ea += _regs[modrm_rm];
  644. break;
  645. }
  646. switch (modrm_mod) {
  647. case 0:
  648. if (modrm_rm == 5)
  649. modrm_ea += insn_fetch(s32, 4, _eip);
  650. break;
  651. case 1:
  652. modrm_ea += insn_fetch(s8, 1, _eip);
  653. break;
  654. case 2:
  655. modrm_ea += insn_fetch(s32, 4, _eip);
  656. break;
  657. }
  658. }
  659. if (!override_base)
  660. override_base = &ctxt->ds_base;
  661. if (mode == X86EMUL_MODE_PROT64 &&
  662. override_base != &ctxt->fs_base &&
  663. override_base != &ctxt->gs_base)
  664. override_base = NULL;
  665. if (override_base)
  666. modrm_ea += *override_base;
  667. if (rip_relative) {
  668. modrm_ea += _eip;
  669. switch (d & SrcMask) {
  670. case SrcImmByte:
  671. modrm_ea += 1;
  672. break;
  673. case SrcImm:
  674. if (d & ByteOp)
  675. modrm_ea += 1;
  676. else
  677. if (op_bytes == 8)
  678. modrm_ea += 4;
  679. else
  680. modrm_ea += op_bytes;
  681. }
  682. }
  683. if (ad_bytes != 8)
  684. modrm_ea = (u32)modrm_ea;
  685. cr2 = modrm_ea;
  686. modrm_done:
  687. ;
  688. }
  689. /*
  690. * Decode and fetch the source operand: register, memory
  691. * or immediate.
  692. */
  693. switch (d & SrcMask) {
  694. case SrcNone:
  695. break;
  696. case SrcReg:
  697. src.type = OP_REG;
  698. if (d & ByteOp) {
  699. src.ptr = decode_register(modrm_reg, _regs,
  700. (rex_prefix == 0));
  701. src.val = src.orig_val = *(u8 *) src.ptr;
  702. src.bytes = 1;
  703. } else {
  704. src.ptr = decode_register(modrm_reg, _regs, 0);
  705. switch ((src.bytes = op_bytes)) {
  706. case 2:
  707. src.val = src.orig_val = *(u16 *) src.ptr;
  708. break;
  709. case 4:
  710. src.val = src.orig_val = *(u32 *) src.ptr;
  711. break;
  712. case 8:
  713. src.val = src.orig_val = *(u64 *) src.ptr;
  714. break;
  715. }
  716. }
  717. break;
  718. case SrcMem16:
  719. src.bytes = 2;
  720. goto srcmem_common;
  721. case SrcMem32:
  722. src.bytes = 4;
  723. goto srcmem_common;
  724. case SrcMem:
  725. src.bytes = (d & ByteOp) ? 1 : op_bytes;
  726. srcmem_common:
  727. src.type = OP_MEM;
  728. src.ptr = (unsigned long *)cr2;
  729. if ((rc = ops->read_emulated((unsigned long)src.ptr,
  730. &src.val, src.bytes, ctxt)) != 0)
  731. goto done;
  732. src.orig_val = src.val;
  733. break;
  734. case SrcImm:
  735. src.type = OP_IMM;
  736. src.ptr = (unsigned long *)_eip;
  737. src.bytes = (d & ByteOp) ? 1 : op_bytes;
  738. if (src.bytes == 8)
  739. src.bytes = 4;
  740. /* NB. Immediates are sign-extended as necessary. */
  741. switch (src.bytes) {
  742. case 1:
  743. src.val = insn_fetch(s8, 1, _eip);
  744. break;
  745. case 2:
  746. src.val = insn_fetch(s16, 2, _eip);
  747. break;
  748. case 4:
  749. src.val = insn_fetch(s32, 4, _eip);
  750. break;
  751. }
  752. break;
  753. case SrcImmByte:
  754. src.type = OP_IMM;
  755. src.ptr = (unsigned long *)_eip;
  756. src.bytes = 1;
  757. src.val = insn_fetch(s8, 1, _eip);
  758. break;
  759. }
  760. /* Decode and fetch the destination operand: register or memory. */
  761. switch (d & DstMask) {
  762. case ImplicitOps:
  763. /* Special instructions do their own operand decoding. */
  764. goto special_insn;
  765. case DstReg:
  766. dst.type = OP_REG;
  767. if ((d & ByteOp)
  768. && !(twobyte_table && (b == 0xb6 || b == 0xb7))) {
  769. dst.ptr = decode_register(modrm_reg, _regs,
  770. (rex_prefix == 0));
  771. dst.val = *(u8 *) dst.ptr;
  772. dst.bytes = 1;
  773. } else {
  774. dst.ptr = decode_register(modrm_reg, _regs, 0);
  775. switch ((dst.bytes = op_bytes)) {
  776. case 2:
  777. dst.val = *(u16 *)dst.ptr;
  778. break;
  779. case 4:
  780. dst.val = *(u32 *)dst.ptr;
  781. break;
  782. case 8:
  783. dst.val = *(u64 *)dst.ptr;
  784. break;
  785. }
  786. }
  787. break;
  788. case DstMem:
  789. dst.type = OP_MEM;
  790. dst.ptr = (unsigned long *)cr2;
  791. dst.bytes = (d & ByteOp) ? 1 : op_bytes;
  792. if (d & BitOp) {
  793. unsigned long mask = ~(dst.bytes * 8 - 1);
  794. dst.ptr = (void *)dst.ptr + (src.val & mask) / 8;
  795. }
  796. if (!(d & Mov) && /* optimisation - avoid slow emulated read */
  797. ((rc = ops->read_emulated((unsigned long)dst.ptr,
  798. &dst.val, dst.bytes, ctxt)) != 0))
  799. goto done;
  800. break;
  801. }
  802. dst.orig_val = dst.val;
  803. if (twobyte)
  804. goto twobyte_insn;
  805. switch (b) {
  806. case 0x00 ... 0x05:
  807. add: /* add */
  808. emulate_2op_SrcV("add", src, dst, _eflags);
  809. break;
  810. case 0x08 ... 0x0d:
  811. or: /* or */
  812. emulate_2op_SrcV("or", src, dst, _eflags);
  813. break;
  814. case 0x10 ... 0x15:
  815. adc: /* adc */
  816. emulate_2op_SrcV("adc", src, dst, _eflags);
  817. break;
  818. case 0x18 ... 0x1d:
  819. sbb: /* sbb */
  820. emulate_2op_SrcV("sbb", src, dst, _eflags);
  821. break;
  822. case 0x20 ... 0x25:
  823. and: /* and */
  824. emulate_2op_SrcV("and", src, dst, _eflags);
  825. break;
  826. case 0x28 ... 0x2d:
  827. sub: /* sub */
  828. emulate_2op_SrcV("sub", src, dst, _eflags);
  829. break;
  830. case 0x30 ... 0x35:
  831. xor: /* xor */
  832. emulate_2op_SrcV("xor", src, dst, _eflags);
  833. break;
  834. case 0x38 ... 0x3d:
  835. cmp: /* cmp */
  836. emulate_2op_SrcV("cmp", src, dst, _eflags);
  837. break;
  838. case 0x63: /* movsxd */
  839. if (mode != X86EMUL_MODE_PROT64)
  840. goto cannot_emulate;
  841. dst.val = (s32) src.val;
  842. break;
  843. case 0x80 ... 0x83: /* Grp1 */
  844. switch (modrm_reg) {
  845. case 0:
  846. goto add;
  847. case 1:
  848. goto or;
  849. case 2:
  850. goto adc;
  851. case 3:
  852. goto sbb;
  853. case 4:
  854. goto and;
  855. case 5:
  856. goto sub;
  857. case 6:
  858. goto xor;
  859. case 7:
  860. goto cmp;
  861. }
  862. break;
  863. case 0x84 ... 0x85:
  864. test: /* test */
  865. emulate_2op_SrcV("test", src, dst, _eflags);
  866. break;
  867. case 0x86 ... 0x87: /* xchg */
  868. /* Write back the register source. */
  869. switch (dst.bytes) {
  870. case 1:
  871. *(u8 *) src.ptr = (u8) dst.val;
  872. break;
  873. case 2:
  874. *(u16 *) src.ptr = (u16) dst.val;
  875. break;
  876. case 4:
  877. *src.ptr = (u32) dst.val;
  878. break; /* 64b reg: zero-extend */
  879. case 8:
  880. *src.ptr = dst.val;
  881. break;
  882. }
  883. /*
  884. * Write back the memory destination with implicit LOCK
  885. * prefix.
  886. */
  887. dst.val = src.val;
  888. lock_prefix = 1;
  889. break;
  890. case 0xa0 ... 0xa1: /* mov */
  891. dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
  892. dst.val = src.val;
  893. _eip += ad_bytes; /* skip src displacement */
  894. break;
  895. case 0xa2 ... 0xa3: /* mov */
  896. dst.val = (unsigned long)_regs[VCPU_REGS_RAX];
  897. _eip += ad_bytes; /* skip dst displacement */
  898. break;
  899. case 0x88 ... 0x8b: /* mov */
  900. case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
  901. dst.val = src.val;
  902. break;
  903. case 0x8f: /* pop (sole member of Grp1a) */
  904. /* 64-bit mode: POP always pops a 64-bit operand. */
  905. if (mode == X86EMUL_MODE_PROT64)
  906. dst.bytes = 8;
  907. if ((rc = ops->read_std(register_address(ctxt->ss_base,
  908. _regs[VCPU_REGS_RSP]),
  909. &dst.val, dst.bytes, ctxt)) != 0)
  910. goto done;
  911. register_address_increment(_regs[VCPU_REGS_RSP], dst.bytes);
  912. break;
  913. case 0xc0 ... 0xc1:
  914. grp2: /* Grp2 */
  915. switch (modrm_reg) {
  916. case 0: /* rol */
  917. emulate_2op_SrcB("rol", src, dst, _eflags);
  918. break;
  919. case 1: /* ror */
  920. emulate_2op_SrcB("ror", src, dst, _eflags);
  921. break;
  922. case 2: /* rcl */
  923. emulate_2op_SrcB("rcl", src, dst, _eflags);
  924. break;
  925. case 3: /* rcr */
  926. emulate_2op_SrcB("rcr", src, dst, _eflags);
  927. break;
  928. case 4: /* sal/shl */
  929. case 6: /* sal/shl */
  930. emulate_2op_SrcB("sal", src, dst, _eflags);
  931. break;
  932. case 5: /* shr */
  933. emulate_2op_SrcB("shr", src, dst, _eflags);
  934. break;
  935. case 7: /* sar */
  936. emulate_2op_SrcB("sar", src, dst, _eflags);
  937. break;
  938. }
  939. break;
  940. case 0xd0 ... 0xd1: /* Grp2 */
  941. src.val = 1;
  942. goto grp2;
  943. case 0xd2 ... 0xd3: /* Grp2 */
  944. src.val = _regs[VCPU_REGS_RCX];
  945. goto grp2;
  946. case 0xf6 ... 0xf7: /* Grp3 */
  947. switch (modrm_reg) {
  948. case 0 ... 1: /* test */
  949. /*
  950. * Special case in Grp3: test has an immediate
  951. * source operand.
  952. */
  953. src.type = OP_IMM;
  954. src.ptr = (unsigned long *)_eip;
  955. src.bytes = (d & ByteOp) ? 1 : op_bytes;
  956. if (src.bytes == 8)
  957. src.bytes = 4;
  958. switch (src.bytes) {
  959. case 1:
  960. src.val = insn_fetch(s8, 1, _eip);
  961. break;
  962. case 2:
  963. src.val = insn_fetch(s16, 2, _eip);
  964. break;
  965. case 4:
  966. src.val = insn_fetch(s32, 4, _eip);
  967. break;
  968. }
  969. goto test;
  970. case 2: /* not */
  971. dst.val = ~dst.val;
  972. break;
  973. case 3: /* neg */
  974. emulate_1op("neg", dst, _eflags);
  975. break;
  976. default:
  977. goto cannot_emulate;
  978. }
  979. break;
  980. case 0xfe ... 0xff: /* Grp4/Grp5 */
  981. switch (modrm_reg) {
  982. case 0: /* inc */
  983. emulate_1op("inc", dst, _eflags);
  984. break;
  985. case 1: /* dec */
  986. emulate_1op("dec", dst, _eflags);
  987. break;
  988. case 6: /* push */
  989. /* 64-bit mode: PUSH always pushes a 64-bit operand. */
  990. if (mode == X86EMUL_MODE_PROT64) {
  991. dst.bytes = 8;
  992. if ((rc = ops->read_std((unsigned long)dst.ptr,
  993. &dst.val, 8,
  994. ctxt)) != 0)
  995. goto done;
  996. }
  997. register_address_increment(_regs[VCPU_REGS_RSP],
  998. -dst.bytes);
  999. if ((rc = ops->write_std(
  1000. register_address(ctxt->ss_base,
  1001. _regs[VCPU_REGS_RSP]),
  1002. &dst.val, dst.bytes, ctxt)) != 0)
  1003. goto done;
  1004. no_wb = 1;
  1005. break;
  1006. default:
  1007. goto cannot_emulate;
  1008. }
  1009. break;
  1010. }
  1011. writeback:
  1012. if (!no_wb) {
  1013. switch (dst.type) {
  1014. case OP_REG:
  1015. /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
  1016. switch (dst.bytes) {
  1017. case 1:
  1018. *(u8 *)dst.ptr = (u8)dst.val;
  1019. break;
  1020. case 2:
  1021. *(u16 *)dst.ptr = (u16)dst.val;
  1022. break;
  1023. case 4:
  1024. *dst.ptr = (u32)dst.val;
  1025. break; /* 64b: zero-ext */
  1026. case 8:
  1027. *dst.ptr = dst.val;
  1028. break;
  1029. }
  1030. break;
  1031. case OP_MEM:
  1032. if (lock_prefix)
  1033. rc = ops->cmpxchg_emulated((unsigned long)dst.
  1034. ptr, &dst.orig_val,
  1035. &dst.val, dst.bytes,
  1036. ctxt);
  1037. else
  1038. rc = ops->write_emulated((unsigned long)dst.ptr,
  1039. &dst.val, dst.bytes,
  1040. ctxt);
  1041. if (rc != 0)
  1042. goto done;
  1043. default:
  1044. break;
  1045. }
  1046. }
  1047. /* Commit shadow register state. */
  1048. memcpy(ctxt->vcpu->regs, _regs, sizeof _regs);
  1049. ctxt->eflags = _eflags;
  1050. ctxt->vcpu->rip = _eip;
  1051. done:
  1052. return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
  1053. special_insn:
  1054. if (twobyte)
  1055. goto twobyte_special_insn;
  1056. if (rep_prefix) {
  1057. if (_regs[VCPU_REGS_RCX] == 0) {
  1058. ctxt->vcpu->rip = _eip;
  1059. goto done;
  1060. }
  1061. _regs[VCPU_REGS_RCX]--;
  1062. _eip = ctxt->vcpu->rip;
  1063. }
  1064. switch (b) {
  1065. case 0xa4 ... 0xa5: /* movs */
  1066. dst.type = OP_MEM;
  1067. dst.bytes = (d & ByteOp) ? 1 : op_bytes;
  1068. dst.ptr = (unsigned long *)register_address(ctxt->es_base,
  1069. _regs[VCPU_REGS_RDI]);
  1070. if ((rc = ops->read_emulated(register_address(
  1071. override_base ? *override_base : ctxt->ds_base,
  1072. _regs[VCPU_REGS_RSI]), &dst.val, dst.bytes, ctxt)) != 0)
  1073. goto done;
  1074. register_address_increment(_regs[VCPU_REGS_RSI],
  1075. (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
  1076. register_address_increment(_regs[VCPU_REGS_RDI],
  1077. (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
  1078. break;
  1079. case 0xa6 ... 0xa7: /* cmps */
  1080. DPRINTF("Urk! I don't handle CMPS.\n");
  1081. goto cannot_emulate;
  1082. case 0xaa ... 0xab: /* stos */
  1083. dst.type = OP_MEM;
  1084. dst.bytes = (d & ByteOp) ? 1 : op_bytes;
  1085. dst.ptr = (unsigned long *)cr2;
  1086. dst.val = _regs[VCPU_REGS_RAX];
  1087. register_address_increment(_regs[VCPU_REGS_RDI],
  1088. (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
  1089. break;
  1090. case 0xac ... 0xad: /* lods */
  1091. dst.type = OP_REG;
  1092. dst.bytes = (d & ByteOp) ? 1 : op_bytes;
  1093. dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
  1094. if ((rc = ops->read_emulated(cr2, &dst.val, dst.bytes, ctxt)) != 0)
  1095. goto done;
  1096. register_address_increment(_regs[VCPU_REGS_RSI],
  1097. (_eflags & EFLG_DF) ? -dst.bytes : dst.bytes);
  1098. break;
  1099. case 0xae ... 0xaf: /* scas */
  1100. DPRINTF("Urk! I don't handle SCAS.\n");
  1101. goto cannot_emulate;
  1102. case 0xf4: /* hlt */
  1103. ctxt->vcpu->halt_request = 1;
  1104. goto done;
  1105. case 0xc3: /* ret */
  1106. dst.ptr = &_eip;
  1107. goto pop_instruction;
  1108. case 0x58 ... 0x5f: /* pop reg */
  1109. dst.ptr = (unsigned long *)&_regs[b & 0x7];
  1110. pop_instruction:
  1111. if ((rc = ops->read_std(register_address(ctxt->ss_base,
  1112. _regs[VCPU_REGS_RSP]), dst.ptr, op_bytes, ctxt)) != 0)
  1113. goto done;
  1114. register_address_increment(_regs[VCPU_REGS_RSP], op_bytes);
  1115. no_wb = 1; /* Disable writeback. */
  1116. break;
  1117. }
  1118. goto writeback;
  1119. twobyte_insn:
  1120. switch (b) {
  1121. case 0x01: /* lgdt, lidt, lmsw */
  1122. switch (modrm_reg) {
  1123. u16 size;
  1124. unsigned long address;
  1125. case 2: /* lgdt */
  1126. rc = read_descriptor(ctxt, ops, src.ptr,
  1127. &size, &address, op_bytes);
  1128. if (rc)
  1129. goto done;
  1130. realmode_lgdt(ctxt->vcpu, size, address);
  1131. break;
  1132. case 3: /* lidt */
  1133. rc = read_descriptor(ctxt, ops, src.ptr,
  1134. &size, &address, op_bytes);
  1135. if (rc)
  1136. goto done;
  1137. realmode_lidt(ctxt->vcpu, size, address);
  1138. break;
  1139. case 4: /* smsw */
  1140. if (modrm_mod != 3)
  1141. goto cannot_emulate;
  1142. *(u16 *)&_regs[modrm_rm]
  1143. = realmode_get_cr(ctxt->vcpu, 0);
  1144. break;
  1145. case 6: /* lmsw */
  1146. if (modrm_mod != 3)
  1147. goto cannot_emulate;
  1148. realmode_lmsw(ctxt->vcpu, (u16)modrm_val, &_eflags);
  1149. break;
  1150. case 7: /* invlpg*/
  1151. emulate_invlpg(ctxt->vcpu, cr2);
  1152. break;
  1153. default:
  1154. goto cannot_emulate;
  1155. }
  1156. break;
  1157. case 0x21: /* mov from dr to reg */
  1158. if (modrm_mod != 3)
  1159. goto cannot_emulate;
  1160. rc = emulator_get_dr(ctxt, modrm_reg, &_regs[modrm_rm]);
  1161. break;
  1162. case 0x23: /* mov from reg to dr */
  1163. if (modrm_mod != 3)
  1164. goto cannot_emulate;
  1165. rc = emulator_set_dr(ctxt, modrm_reg, _regs[modrm_rm]);
  1166. break;
  1167. case 0x40 ... 0x4f: /* cmov */
  1168. dst.val = dst.orig_val = src.val;
  1169. d &= ~Mov; /* default to no move */
  1170. /*
  1171. * First, assume we're decoding an even cmov opcode
  1172. * (lsb == 0).
  1173. */
  1174. switch ((b & 15) >> 1) {
  1175. case 0: /* cmovo */
  1176. d |= (_eflags & EFLG_OF) ? Mov : 0;
  1177. break;
  1178. case 1: /* cmovb/cmovc/cmovnae */
  1179. d |= (_eflags & EFLG_CF) ? Mov : 0;
  1180. break;
  1181. case 2: /* cmovz/cmove */
  1182. d |= (_eflags & EFLG_ZF) ? Mov : 0;
  1183. break;
  1184. case 3: /* cmovbe/cmovna */
  1185. d |= (_eflags & (EFLG_CF | EFLG_ZF)) ? Mov : 0;
  1186. break;
  1187. case 4: /* cmovs */
  1188. d |= (_eflags & EFLG_SF) ? Mov : 0;
  1189. break;
  1190. case 5: /* cmovp/cmovpe */
  1191. d |= (_eflags & EFLG_PF) ? Mov : 0;
  1192. break;
  1193. case 7: /* cmovle/cmovng */
  1194. d |= (_eflags & EFLG_ZF) ? Mov : 0;
  1195. /* fall through */
  1196. case 6: /* cmovl/cmovnge */
  1197. d |= (!(_eflags & EFLG_SF) !=
  1198. !(_eflags & EFLG_OF)) ? Mov : 0;
  1199. break;
  1200. }
  1201. /* Odd cmov opcodes (lsb == 1) have inverted sense. */
  1202. d ^= (b & 1) ? Mov : 0;
  1203. break;
  1204. case 0xb0 ... 0xb1: /* cmpxchg */
  1205. /*
  1206. * Save real source value, then compare EAX against
  1207. * destination.
  1208. */
  1209. src.orig_val = src.val;
  1210. src.val = _regs[VCPU_REGS_RAX];
  1211. emulate_2op_SrcV("cmp", src, dst, _eflags);
  1212. /* Always write back. The question is: where to? */
  1213. d |= Mov;
  1214. if (_eflags & EFLG_ZF) {
  1215. /* Success: write back to memory. */
  1216. dst.val = src.orig_val;
  1217. } else {
  1218. /* Failure: write the value we saw to EAX. */
  1219. dst.type = OP_REG;
  1220. dst.ptr = (unsigned long *)&_regs[VCPU_REGS_RAX];
  1221. }
  1222. break;
  1223. case 0xa3:
  1224. bt: /* bt */
  1225. src.val &= (dst.bytes << 3) - 1; /* only subword offset */
  1226. emulate_2op_SrcV_nobyte("bt", src, dst, _eflags);
  1227. break;
  1228. case 0xb3:
  1229. btr: /* btr */
  1230. src.val &= (dst.bytes << 3) - 1; /* only subword offset */
  1231. emulate_2op_SrcV_nobyte("btr", src, dst, _eflags);
  1232. break;
  1233. case 0xab:
  1234. bts: /* bts */
  1235. src.val &= (dst.bytes << 3) - 1; /* only subword offset */
  1236. emulate_2op_SrcV_nobyte("bts", src, dst, _eflags);
  1237. break;
  1238. case 0xb6 ... 0xb7: /* movzx */
  1239. dst.bytes = op_bytes;
  1240. dst.val = (d & ByteOp) ? (u8) src.val : (u16) src.val;
  1241. break;
  1242. case 0xbb:
  1243. btc: /* btc */
  1244. src.val &= (dst.bytes << 3) - 1; /* only subword offset */
  1245. emulate_2op_SrcV_nobyte("btc", src, dst, _eflags);
  1246. break;
  1247. case 0xba: /* Grp8 */
  1248. switch (modrm_reg & 3) {
  1249. case 0:
  1250. goto bt;
  1251. case 1:
  1252. goto bts;
  1253. case 2:
  1254. goto btr;
  1255. case 3:
  1256. goto btc;
  1257. }
  1258. break;
  1259. case 0xbe ... 0xbf: /* movsx */
  1260. dst.bytes = op_bytes;
  1261. dst.val = (d & ByteOp) ? (s8) src.val : (s16) src.val;
  1262. break;
  1263. }
  1264. goto writeback;
  1265. twobyte_special_insn:
  1266. /* Disable writeback. */
  1267. no_wb = 1;
  1268. switch (b) {
  1269. case 0x09: /* wbinvd */
  1270. break;
  1271. case 0x0d: /* GrpP (prefetch) */
  1272. case 0x18: /* Grp16 (prefetch/nop) */
  1273. break;
  1274. case 0x06:
  1275. emulate_clts(ctxt->vcpu);
  1276. break;
  1277. case 0x20: /* mov cr, reg */
  1278. if (modrm_mod != 3)
  1279. goto cannot_emulate;
  1280. _regs[modrm_rm] = realmode_get_cr(ctxt->vcpu, modrm_reg);
  1281. break;
  1282. case 0x22: /* mov reg, cr */
  1283. if (modrm_mod != 3)
  1284. goto cannot_emulate;
  1285. realmode_set_cr(ctxt->vcpu, modrm_reg, modrm_val, &_eflags);
  1286. break;
  1287. case 0xc7: /* Grp9 (cmpxchg8b) */
  1288. {
  1289. u64 old, new;
  1290. if ((rc = ops->read_emulated(cr2, &old, 8, ctxt)) != 0)
  1291. goto done;
  1292. if (((u32) (old >> 0) != (u32) _regs[VCPU_REGS_RAX]) ||
  1293. ((u32) (old >> 32) != (u32) _regs[VCPU_REGS_RDX])) {
  1294. _regs[VCPU_REGS_RAX] = (u32) (old >> 0);
  1295. _regs[VCPU_REGS_RDX] = (u32) (old >> 32);
  1296. _eflags &= ~EFLG_ZF;
  1297. } else {
  1298. new = ((u64)_regs[VCPU_REGS_RCX] << 32)
  1299. | (u32) _regs[VCPU_REGS_RBX];
  1300. if ((rc = ops->cmpxchg_emulated(cr2, &old,
  1301. &new, 8, ctxt)) != 0)
  1302. goto done;
  1303. _eflags |= EFLG_ZF;
  1304. }
  1305. break;
  1306. }
  1307. }
  1308. goto writeback;
  1309. cannot_emulate:
  1310. DPRINTF("Cannot emulate %02x\n", b);
  1311. return -1;
  1312. }
  1313. #ifdef __XEN__
  1314. #include <asm/mm.h>
  1315. #include <asm/uaccess.h>
  1316. int
  1317. x86_emulate_read_std(unsigned long addr,
  1318. unsigned long *val,
  1319. unsigned int bytes, struct x86_emulate_ctxt *ctxt)
  1320. {
  1321. unsigned int rc;
  1322. *val = 0;
  1323. if ((rc = copy_from_user((void *)val, (void *)addr, bytes)) != 0) {
  1324. propagate_page_fault(addr + bytes - rc, 0); /* read fault */
  1325. return X86EMUL_PROPAGATE_FAULT;
  1326. }
  1327. return X86EMUL_CONTINUE;
  1328. }
  1329. int
  1330. x86_emulate_write_std(unsigned long addr,
  1331. unsigned long val,
  1332. unsigned int bytes, struct x86_emulate_ctxt *ctxt)
  1333. {
  1334. unsigned int rc;
  1335. if ((rc = copy_to_user((void *)addr, (void *)&val, bytes)) != 0) {
  1336. propagate_page_fault(addr + bytes - rc, PGERR_write_access);
  1337. return X86EMUL_PROPAGATE_FAULT;
  1338. }
  1339. return X86EMUL_CONTINUE;
  1340. }
  1341. #endif