x86_emulate.c 43 KB

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