x86_emulate.c 41 KB

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