Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 2 | * emulate.c |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 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 |
Rusty Russell | dcc0766 | 2007-07-17 23:16:56 +1000 | [diff] [blame] | 9 | * privileged instructions: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 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 | |
| 22 | #ifndef __KERNEL__ |
| 23 | #include <stdio.h> |
| 24 | #include <stdint.h> |
| 25 | #include <public/xen.h> |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 26 | #define DPRINTF(_f, _a ...) printf(_f , ## _a) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 27 | #else |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 28 | #include <linux/kvm_host.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 29 | #include "kvm_cache_regs.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 30 | #define DPRINTF(x...) do {} while (0) |
| 31 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 32 | #include <linux/module.h> |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 33 | #include <asm/kvm_emulate.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 34 | |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 35 | #include "x86.h" |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 36 | #include "tss.h" |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 37 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 38 | /* |
| 39 | * Opcode effective-address decode tables. |
| 40 | * Note that we only emulate instructions that have at least one memory |
| 41 | * operand (excluding implicit stack references). We assume that stack |
| 42 | * references and instruction fetches will never occur in special memory |
| 43 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need |
| 44 | * not be handled. |
| 45 | */ |
| 46 | |
| 47 | /* Operand sizes: 8-bit operands or specified/overridden size. */ |
| 48 | #define ByteOp (1<<0) /* 8-bit operands. */ |
| 49 | /* Destination operand type. */ |
| 50 | #define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */ |
| 51 | #define DstReg (2<<1) /* Register operand. */ |
| 52 | #define DstMem (3<<1) /* Memory operand. */ |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 53 | #define DstAcc (4<<1) /* Destination Accumulator */ |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame^] | 54 | #define DstDI (5<<1) /* Destination is in ES:(E)DI */ |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 55 | #define DstMask (7<<1) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 56 | /* Source operand type. */ |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 57 | #define SrcNone (0<<4) /* No source operand. */ |
| 58 | #define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */ |
| 59 | #define SrcReg (1<<4) /* Register operand. */ |
| 60 | #define SrcMem (2<<4) /* Memory operand. */ |
| 61 | #define SrcMem16 (3<<4) /* Memory operand (16-bit). */ |
| 62 | #define SrcMem32 (4<<4) /* Memory operand (32-bit). */ |
| 63 | #define SrcImm (5<<4) /* Immediate operand. */ |
| 64 | #define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */ |
Guillaume Thouvenin | bfcadf8 | 2008-12-04 14:27:38 +0100 | [diff] [blame] | 65 | #define SrcOne (7<<4) /* Implied '1' */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 66 | #define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */ |
Avi Kivity | c9eaf20 | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 67 | #define SrcImmU (9<<4) /* Immediate operand, unsigned */ |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame^] | 68 | #define SrcSI (0xa<<4) /* Source is in the DS:RSI */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 69 | #define SrcMask (0xf<<4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 70 | /* Generic ModRM decode. */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 71 | #define ModRM (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 72 | /* Destination is only written; never read. */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 73 | #define Mov (1<<9) |
| 74 | #define BitOp (1<<10) |
| 75 | #define MemAbs (1<<11) /* Memory operand is absolute displacement */ |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 76 | #define String (1<<12) /* String instruction (rep capable) */ |
| 77 | #define Stack (1<<13) /* Stack instruction (push/pop) */ |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 78 | #define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */ |
| 79 | #define GroupDual (1<<15) /* Alternate decoding of mod == 3 */ |
| 80 | #define GroupMask 0xff /* Group number stored in bits 0:7 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 81 | /* Misc flags */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 82 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 83 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 84 | #define No64 (1<<28) |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 85 | /* Source 2 operand type */ |
| 86 | #define Src2None (0<<29) |
| 87 | #define Src2CL (1<<29) |
| 88 | #define Src2ImmByte (2<<29) |
| 89 | #define Src2One (3<<29) |
Gleb Natapov | a5f868b | 2009-04-12 13:36:14 +0300 | [diff] [blame] | 90 | #define Src2Imm16 (4<<29) |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 91 | #define Src2Mem16 (5<<29) /* Used for Ep encoding. First argument has to be |
| 92 | in memory and second argument is located |
| 93 | immediately after the first one in memory. */ |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 94 | #define Src2Mask (7<<29) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 95 | |
Avi Kivity | 43bb19c | 2008-01-18 12:46:50 +0200 | [diff] [blame] | 96 | enum { |
Avi Kivity | 1d6ad20 | 2008-01-23 22:26:09 +0200 | [diff] [blame] | 97 | Group1_80, Group1_81, Group1_82, Group1_83, |
Avi Kivity | d95058a | 2008-01-18 13:36:50 +0200 | [diff] [blame] | 98 | Group1A, Group3_Byte, Group3, Group4, Group5, Group7, |
Gleb Natapov | 60a29d4 | 2010-02-10 14:21:30 +0200 | [diff] [blame] | 99 | Group8, Group9, |
Avi Kivity | 43bb19c | 2008-01-18 12:46:50 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
Guillaume Thouvenin | 45ed60b | 2008-12-04 14:25:38 +0100 | [diff] [blame] | 102 | static u32 opcode_table[256] = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 103 | /* 0x00 - 0x07 */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 104 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 105 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 106 | ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 107 | ImplicitOps | Stack | No64, ImplicitOps | Stack | No64, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 108 | /* 0x08 - 0x0F */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 109 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 110 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
Mohammed Gamal | 94677e6 | 2009-08-28 16:41:44 +0200 | [diff] [blame] | 111 | ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, |
| 112 | ImplicitOps | Stack | No64, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 113 | /* 0x10 - 0x17 */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 114 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 115 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 116 | ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 117 | ImplicitOps | Stack | No64, ImplicitOps | Stack | No64, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 118 | /* 0x18 - 0x1F */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 119 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 120 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 121 | ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 122 | ImplicitOps | Stack | No64, ImplicitOps | Stack | No64, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 123 | /* 0x20 - 0x27 */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 124 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 125 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
Guillaume Thouvenin | aa3a816 | 2008-09-12 13:52:18 +0200 | [diff] [blame] | 126 | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 127 | /* 0x28 - 0x2F */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 128 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 129 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
| 130 | 0, 0, 0, 0, |
| 131 | /* 0x30 - 0x37 */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 132 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 133 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
| 134 | 0, 0, 0, 0, |
| 135 | /* 0x38 - 0x3F */ |
| 136 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 137 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
Guillaume Thouvenin | 8a9fee6 | 2008-09-12 13:51:15 +0200 | [diff] [blame] | 138 | ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, |
| 139 | 0, 0, |
Nitin A Kamble | d77a250 | 2007-10-12 17:40:33 -0700 | [diff] [blame] | 140 | /* 0x40 - 0x47 */ |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 141 | DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, |
Nitin A Kamble | d77a250 | 2007-10-12 17:40:33 -0700 | [diff] [blame] | 142 | /* 0x48 - 0x4F */ |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 143 | DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, |
Nitin A Kamble | 7f0aaee | 2007-06-19 11:16:04 +0300 | [diff] [blame] | 144 | /* 0x50 - 0x57 */ |
Avi Kivity | 6e3d5df | 2007-12-06 18:14:14 +0200 | [diff] [blame] | 145 | SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, |
| 146 | SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, |
Nitin A Kamble | 7f0aaee | 2007-06-19 11:16:04 +0300 | [diff] [blame] | 147 | /* 0x58 - 0x5F */ |
Avi Kivity | 6e3d5df | 2007-12-06 18:14:14 +0200 | [diff] [blame] | 148 | DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack, |
| 149 | DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack, |
Nitin A Kamble | 7d31691 | 2007-08-28 17:58:52 -0700 | [diff] [blame] | 150 | /* 0x60 - 0x67 */ |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 151 | ImplicitOps | Stack | No64, ImplicitOps | Stack | No64, |
| 152 | 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ , |
Nitin A Kamble | 7d31691 | 2007-08-28 17:58:52 -0700 | [diff] [blame] | 153 | 0, 0, 0, 0, |
| 154 | /* 0x68 - 0x6F */ |
Avi Kivity | 91ed7a0 | 2008-05-29 14:38:38 +0300 | [diff] [blame] | 155 | SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0, |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 156 | SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */ |
| 157 | SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */ |
Nitin A Kamble | 55bebde | 2007-09-15 10:25:41 +0300 | [diff] [blame] | 158 | /* 0x70 - 0x77 */ |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 159 | SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte, |
| 160 | SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte, |
Nitin A Kamble | 55bebde | 2007-09-15 10:25:41 +0300 | [diff] [blame] | 161 | /* 0x78 - 0x7F */ |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 162 | SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte, |
| 163 | SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 164 | /* 0x80 - 0x87 */ |
Avi Kivity | 1d6ad20 | 2008-01-23 22:26:09 +0200 | [diff] [blame] | 165 | Group | Group1_80, Group | Group1_81, |
| 166 | Group | Group1_82, Group | Group1_83, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 167 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 168 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 169 | /* 0x88 - 0x8F */ |
| 170 | ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov, |
| 171 | ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 172 | DstMem | SrcReg | ModRM | Mov, ModRM | DstReg, |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 173 | DstReg | SrcMem | ModRM | Mov, Group | Group1A, |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 174 | /* 0x90 - 0x97 */ |
| 175 | DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, |
| 176 | /* 0x98 - 0x9F */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 177 | 0, 0, SrcImm | Src2Imm16 | No64, 0, |
Gleb Natapov | 0654169 | 2009-04-12 13:36:20 +0300 | [diff] [blame] | 178 | ImplicitOps | Stack, ImplicitOps | Stack, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 179 | /* 0xA0 - 0xA7 */ |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 180 | ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs, |
| 181 | ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs, |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame^] | 182 | ByteOp | SrcSI | DstDI | Mov | String, SrcSI | DstDI | Mov | String, |
| 183 | ByteOp | SrcSI | DstDI | String, SrcSI | DstDI | String, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 184 | /* 0xA8 - 0xAF */ |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame^] | 185 | 0, 0, ByteOp | DstDI | Mov | String, DstDI | Mov | String, |
| 186 | ByteOp | SrcSI | DstAcc | Mov | String, SrcSI | DstAcc | Mov | String, |
| 187 | ByteOp | DstDI | String, DstDI | String, |
Mohammed Gamal | a5e2e82 | 2008-08-27 05:02:56 +0300 | [diff] [blame] | 188 | /* 0xB0 - 0xB7 */ |
| 189 | ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov, |
| 190 | ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov, |
| 191 | ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov, |
| 192 | ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov, |
| 193 | /* 0xB8 - 0xBF */ |
| 194 | DstReg | SrcImm | Mov, DstReg | SrcImm | Mov, |
| 195 | DstReg | SrcImm | Mov, DstReg | SrcImm | Mov, |
| 196 | DstReg | SrcImm | Mov, DstReg | SrcImm | Mov, |
| 197 | DstReg | SrcImm | Mov, DstReg | SrcImm | Mov, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 198 | /* 0xC0 - 0xC7 */ |
Nitin A Kamble | d9413cd | 2007-06-19 11:21:15 +0300 | [diff] [blame] | 199 | ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM, |
Avi Kivity | 6e3d5df | 2007-12-06 18:14:14 +0200 | [diff] [blame] | 200 | 0, ImplicitOps | Stack, 0, 0, |
Nitin A Kamble | d9413cd | 2007-06-19 11:21:15 +0300 | [diff] [blame] | 201 | ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 202 | /* 0xC8 - 0xCF */ |
Gleb Natapov | e637b82 | 2009-04-12 13:36:52 +0300 | [diff] [blame] | 203 | 0, 0, 0, ImplicitOps | Stack, |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 204 | ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 205 | /* 0xD0 - 0xD7 */ |
| 206 | ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM, |
| 207 | ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM, |
| 208 | 0, 0, 0, 0, |
| 209 | /* 0xD8 - 0xDF */ |
| 210 | 0, 0, 0, 0, 0, 0, 0, 0, |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 211 | /* 0xE0 - 0xE7 */ |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 212 | 0, 0, 0, 0, |
Gleb Natapov | 84ce66a | 2009-04-12 13:36:46 +0300 | [diff] [blame] | 213 | ByteOp | SrcImmUByte, SrcImmUByte, |
| 214 | ByteOp | SrcImmUByte, SrcImmUByte, |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 215 | /* 0xE8 - 0xEF */ |
Gleb Natapov | d53c477 | 2009-04-12 13:36:36 +0300 | [diff] [blame] | 216 | SrcImm | Stack, SrcImm | ImplicitOps, |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 217 | SrcImmU | Src2Imm16 | No64, SrcImmByte | ImplicitOps, |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 218 | SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, |
| 219 | SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 220 | /* 0xF0 - 0xF7 */ |
| 221 | 0, 0, 0, 0, |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 222 | ImplicitOps | Priv, ImplicitOps, Group | Group3_Byte, Group | Group3, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 223 | /* 0xF8 - 0xFF */ |
Nitin A Kamble | b284be5 | 2007-10-16 18:23:27 -0700 | [diff] [blame] | 224 | ImplicitOps, 0, ImplicitOps, ImplicitOps, |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 225 | ImplicitOps, ImplicitOps, Group | Group4, Group | Group5, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 226 | }; |
| 227 | |
Guillaume Thouvenin | 45ed60b | 2008-12-04 14:25:38 +0100 | [diff] [blame] | 228 | static u32 twobyte_table[256] = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 229 | /* 0x00 - 0x0F */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 230 | 0, Group | GroupDual | Group7, 0, 0, |
| 231 | 0, ImplicitOps, ImplicitOps | Priv, 0, |
| 232 | ImplicitOps | Priv, ImplicitOps | Priv, 0, 0, |
| 233 | 0, ImplicitOps | ModRM, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 234 | /* 0x10 - 0x1F */ |
| 235 | 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0, |
| 236 | /* 0x20 - 0x2F */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 237 | ModRM | ImplicitOps | Priv, ModRM | Priv, |
| 238 | ModRM | ImplicitOps | Priv, ModRM | Priv, |
| 239 | 0, 0, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 240 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 241 | /* 0x30 - 0x3F */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 242 | ImplicitOps | Priv, 0, ImplicitOps | Priv, 0, |
| 243 | ImplicitOps, ImplicitOps | Priv, 0, 0, |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 244 | 0, 0, 0, 0, 0, 0, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 245 | /* 0x40 - 0x47 */ |
| 246 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 247 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 248 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 249 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 250 | /* 0x48 - 0x4F */ |
| 251 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 252 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 253 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 254 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 255 | /* 0x50 - 0x5F */ |
| 256 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 257 | /* 0x60 - 0x6F */ |
| 258 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 259 | /* 0x70 - 0x7F */ |
| 260 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 261 | /* 0x80 - 0x8F */ |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 262 | SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, |
| 263 | SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 264 | /* 0x90 - 0x9F */ |
| 265 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 266 | /* 0xA0 - 0xA7 */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 267 | ImplicitOps | Stack, ImplicitOps | Stack, |
| 268 | 0, DstMem | SrcReg | ModRM | BitOp, |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 269 | DstMem | SrcReg | Src2ImmByte | ModRM, |
| 270 | DstMem | SrcReg | Src2CL | ModRM, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 271 | /* 0xA8 - 0xAF */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 272 | ImplicitOps | Stack, ImplicitOps | Stack, |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 273 | 0, DstMem | SrcReg | ModRM | BitOp | Lock, |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 274 | DstMem | SrcReg | Src2ImmByte | ModRM, |
| 275 | DstMem | SrcReg | Src2CL | ModRM, |
| 276 | ModRM, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 277 | /* 0xB0 - 0xB7 */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 278 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, |
| 279 | 0, DstMem | SrcReg | ModRM | BitOp | Lock, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 280 | 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov, |
| 281 | DstReg | SrcMem16 | ModRM | Mov, |
| 282 | /* 0xB8 - 0xBF */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 283 | 0, 0, |
| 284 | Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 285 | 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov, |
| 286 | DstReg | SrcMem16 | ModRM | Mov, |
| 287 | /* 0xC0 - 0xCF */ |
Gleb Natapov | 60a29d4 | 2010-02-10 14:21:30 +0200 | [diff] [blame] | 288 | 0, 0, 0, DstMem | SrcReg | ModRM | Mov, |
| 289 | 0, 0, 0, Group | GroupDual | Group9, |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 290 | 0, 0, 0, 0, 0, 0, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 291 | /* 0xD0 - 0xDF */ |
| 292 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 293 | /* 0xE0 - 0xEF */ |
| 294 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 295 | /* 0xF0 - 0xFF */ |
| 296 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 297 | }; |
| 298 | |
Guillaume Thouvenin | 45ed60b | 2008-12-04 14:25:38 +0100 | [diff] [blame] | 299 | static u32 group_table[] = { |
Avi Kivity | 1d6ad20 | 2008-01-23 22:26:09 +0200 | [diff] [blame] | 300 | [Group1_80*8] = |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 301 | ByteOp | DstMem | SrcImm | ModRM | Lock, |
| 302 | ByteOp | DstMem | SrcImm | ModRM | Lock, |
| 303 | ByteOp | DstMem | SrcImm | ModRM | Lock, |
| 304 | ByteOp | DstMem | SrcImm | ModRM | Lock, |
| 305 | ByteOp | DstMem | SrcImm | ModRM | Lock, |
| 306 | ByteOp | DstMem | SrcImm | ModRM | Lock, |
| 307 | ByteOp | DstMem | SrcImm | ModRM | Lock, |
| 308 | ByteOp | DstMem | SrcImm | ModRM, |
Avi Kivity | 1d6ad20 | 2008-01-23 22:26:09 +0200 | [diff] [blame] | 309 | [Group1_81*8] = |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 310 | DstMem | SrcImm | ModRM | Lock, |
| 311 | DstMem | SrcImm | ModRM | Lock, |
| 312 | DstMem | SrcImm | ModRM | Lock, |
| 313 | DstMem | SrcImm | ModRM | Lock, |
| 314 | DstMem | SrcImm | ModRM | Lock, |
| 315 | DstMem | SrcImm | ModRM | Lock, |
| 316 | DstMem | SrcImm | ModRM | Lock, |
| 317 | DstMem | SrcImm | ModRM, |
Avi Kivity | 1d6ad20 | 2008-01-23 22:26:09 +0200 | [diff] [blame] | 318 | [Group1_82*8] = |
Gleb Natapov | e424e19 | 2010-02-11 12:41:10 +0200 | [diff] [blame] | 319 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, |
| 320 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, |
| 321 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, |
| 322 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, |
| 323 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, |
| 324 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, |
| 325 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, |
| 326 | ByteOp | DstMem | SrcImm | ModRM | No64, |
Avi Kivity | 1d6ad20 | 2008-01-23 22:26:09 +0200 | [diff] [blame] | 327 | [Group1_83*8] = |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 328 | DstMem | SrcImmByte | ModRM | Lock, |
| 329 | DstMem | SrcImmByte | ModRM | Lock, |
| 330 | DstMem | SrcImmByte | ModRM | Lock, |
| 331 | DstMem | SrcImmByte | ModRM | Lock, |
| 332 | DstMem | SrcImmByte | ModRM | Lock, |
| 333 | DstMem | SrcImmByte | ModRM | Lock, |
| 334 | DstMem | SrcImmByte | ModRM | Lock, |
| 335 | DstMem | SrcImmByte | ModRM, |
Avi Kivity | 43bb19c | 2008-01-18 12:46:50 +0200 | [diff] [blame] | 336 | [Group1A*8] = |
| 337 | DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0, |
Avi Kivity | 7d858a1 | 2008-01-18 12:58:04 +0200 | [diff] [blame] | 338 | [Group3_Byte*8] = |
| 339 | ByteOp | SrcImm | DstMem | ModRM, 0, |
| 340 | ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM, |
| 341 | 0, 0, 0, 0, |
| 342 | [Group3*8] = |
roel kluin | 41afa02 | 2008-08-18 21:25:01 -0400 | [diff] [blame] | 343 | DstMem | SrcImm | ModRM, 0, |
Avi Kivity | 6eb06cb | 2008-08-21 17:41:39 +0300 | [diff] [blame] | 344 | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM, |
Avi Kivity | 7d858a1 | 2008-01-18 12:58:04 +0200 | [diff] [blame] | 345 | 0, 0, 0, 0, |
Avi Kivity | fd60754 | 2008-01-18 13:12:26 +0200 | [diff] [blame] | 346 | [Group4*8] = |
| 347 | ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM, |
| 348 | 0, 0, 0, 0, 0, 0, |
| 349 | [Group5*8] = |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 350 | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM, |
| 351 | SrcMem | ModRM | Stack, 0, |
Gleb Natapov | ea79849 | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 352 | SrcMem | ModRM | Stack, SrcMem | ModRM | Src2Mem16 | ImplicitOps, |
| 353 | SrcMem | ModRM | Stack, 0, |
Avi Kivity | d95058a | 2008-01-18 13:36:50 +0200 | [diff] [blame] | 354 | [Group7*8] = |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 355 | 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv, |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 356 | SrcNone | ModRM | DstMem | Mov, 0, |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 357 | SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv, |
Gleb Natapov | 2db2c2e | 2010-02-10 14:21:29 +0200 | [diff] [blame] | 358 | [Group8*8] = |
| 359 | 0, 0, 0, 0, |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 360 | DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock, |
| 361 | DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock, |
Gleb Natapov | 60a29d4 | 2010-02-10 14:21:30 +0200 | [diff] [blame] | 362 | [Group9*8] = |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 363 | 0, ImplicitOps | ModRM | Lock, 0, 0, 0, 0, 0, 0, |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 364 | }; |
| 365 | |
Guillaume Thouvenin | 45ed60b | 2008-12-04 14:25:38 +0100 | [diff] [blame] | 366 | static u32 group2_table[] = { |
Avi Kivity | d95058a | 2008-01-18 13:36:50 +0200 | [diff] [blame] | 367 | [Group7*8] = |
Gleb Natapov | 835e6b8 | 2010-03-03 17:53:05 +0200 | [diff] [blame] | 368 | SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM | Priv, |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 369 | SrcNone | ModRM | DstMem | Mov, 0, |
Gleb Natapov | 835e6b8 | 2010-03-03 17:53:05 +0200 | [diff] [blame] | 370 | SrcMem16 | ModRM | Mov | Priv, 0, |
Gleb Natapov | 60a29d4 | 2010-02-10 14:21:30 +0200 | [diff] [blame] | 371 | [Group9*8] = |
| 372 | 0, 0, 0, 0, 0, 0, 0, 0, |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 373 | }; |
| 374 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 375 | /* EFLAGS bit definitions. */ |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 376 | #define EFLG_ID (1<<21) |
| 377 | #define EFLG_VIP (1<<20) |
| 378 | #define EFLG_VIF (1<<19) |
| 379 | #define EFLG_AC (1<<18) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 380 | #define EFLG_VM (1<<17) |
| 381 | #define EFLG_RF (1<<16) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 382 | #define EFLG_IOPL (3<<12) |
| 383 | #define EFLG_NT (1<<14) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 384 | #define EFLG_OF (1<<11) |
| 385 | #define EFLG_DF (1<<10) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 386 | #define EFLG_IF (1<<9) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 387 | #define EFLG_TF (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 388 | #define EFLG_SF (1<<7) |
| 389 | #define EFLG_ZF (1<<6) |
| 390 | #define EFLG_AF (1<<4) |
| 391 | #define EFLG_PF (1<<2) |
| 392 | #define EFLG_CF (1<<0) |
| 393 | |
| 394 | /* |
| 395 | * Instruction emulation: |
| 396 | * Most instructions are emulated directly via a fragment of inline assembly |
| 397 | * code. This allows us to save/restore EFLAGS and thus very easily pick up |
| 398 | * any modified flags. |
| 399 | */ |
| 400 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 401 | #if defined(CONFIG_X86_64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 402 | #define _LO32 "k" /* force 32-bit operand */ |
| 403 | #define _STK "%%rsp" /* stack pointer */ |
| 404 | #elif defined(__i386__) |
| 405 | #define _LO32 "" /* force 32-bit operand */ |
| 406 | #define _STK "%%esp" /* stack pointer */ |
| 407 | #endif |
| 408 | |
| 409 | /* |
| 410 | * These EFLAGS bits are restored from saved value during emulation, and |
| 411 | * any changes are written back to the saved value after emulation. |
| 412 | */ |
| 413 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 414 | |
| 415 | /* Before executing instruction: restore necessary bits in EFLAGS. */ |
Avi Kivity | e934c9c | 2007-12-06 16:15:02 +0200 | [diff] [blame] | 416 | #define _PRE_EFLAGS(_sav, _msk, _tmp) \ |
| 417 | /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \ |
| 418 | "movl %"_sav",%"_LO32 _tmp"; " \ |
| 419 | "push %"_tmp"; " \ |
| 420 | "push %"_tmp"; " \ |
| 421 | "movl %"_msk",%"_LO32 _tmp"; " \ |
| 422 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 423 | "pushf; " \ |
| 424 | "notl %"_LO32 _tmp"; " \ |
| 425 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 426 | "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \ |
| 427 | "pop %"_tmp"; " \ |
| 428 | "orl %"_LO32 _tmp",("_STK"); " \ |
| 429 | "popf; " \ |
| 430 | "pop %"_sav"; " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 431 | |
| 432 | /* After executing instruction: write-back necessary bits in EFLAGS. */ |
| 433 | #define _POST_EFLAGS(_sav, _msk, _tmp) \ |
| 434 | /* _sav |= EFLAGS & _msk; */ \ |
| 435 | "pushf; " \ |
| 436 | "pop %"_tmp"; " \ |
| 437 | "andl %"_msk",%"_LO32 _tmp"; " \ |
| 438 | "orl %"_LO32 _tmp",%"_sav"; " |
| 439 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 440 | #ifdef CONFIG_X86_64 |
| 441 | #define ON64(x) x |
| 442 | #else |
| 443 | #define ON64(x) |
| 444 | #endif |
| 445 | |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 446 | #define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \ |
| 447 | do { \ |
| 448 | __asm__ __volatile__ ( \ |
| 449 | _PRE_EFLAGS("0", "4", "2") \ |
| 450 | _op _suffix " %"_x"3,%1; " \ |
| 451 | _POST_EFLAGS("0", "4", "2") \ |
| 452 | : "=m" (_eflags), "=m" ((_dst).val), \ |
| 453 | "=&r" (_tmp) \ |
| 454 | : _y ((_src).val), "i" (EFLAGS_MASK)); \ |
Avi Kivity | f3fd92f | 2008-11-29 20:38:12 +0200 | [diff] [blame] | 455 | } while (0) |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 456 | |
| 457 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 458 | /* Raw emulation: instruction has two explicit operands. */ |
| 459 | #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 460 | do { \ |
| 461 | unsigned long _tmp; \ |
| 462 | \ |
| 463 | switch ((_dst).bytes) { \ |
| 464 | case 2: \ |
| 465 | ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \ |
| 466 | break; \ |
| 467 | case 4: \ |
| 468 | ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \ |
| 469 | break; \ |
| 470 | case 8: \ |
| 471 | ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \ |
| 472 | break; \ |
| 473 | } \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 474 | } while (0) |
| 475 | |
| 476 | #define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \ |
| 477 | do { \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 478 | unsigned long _tmp; \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 479 | switch ((_dst).bytes) { \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 480 | case 1: \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 481 | ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 482 | break; \ |
| 483 | default: \ |
| 484 | __emulate_2op_nobyte(_op, _src, _dst, _eflags, \ |
| 485 | _wx, _wy, _lx, _ly, _qx, _qy); \ |
| 486 | break; \ |
| 487 | } \ |
| 488 | } while (0) |
| 489 | |
| 490 | /* Source operand is byte-sized and may be restricted to just %cl. */ |
| 491 | #define emulate_2op_SrcB(_op, _src, _dst, _eflags) \ |
| 492 | __emulate_2op(_op, _src, _dst, _eflags, \ |
| 493 | "b", "c", "b", "c", "b", "c", "b", "c") |
| 494 | |
| 495 | /* Source operand is byte, word, long or quad sized. */ |
| 496 | #define emulate_2op_SrcV(_op, _src, _dst, _eflags) \ |
| 497 | __emulate_2op(_op, _src, _dst, _eflags, \ |
| 498 | "b", "q", "w", "r", _LO32, "r", "", "r") |
| 499 | |
| 500 | /* Source operand is word, long or quad sized. */ |
| 501 | #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \ |
| 502 | __emulate_2op_nobyte(_op, _src, _dst, _eflags, \ |
| 503 | "w", "r", _LO32, "r", "", "r") |
| 504 | |
Guillaume Thouvenin | d175226 | 2008-12-04 14:29:00 +0100 | [diff] [blame] | 505 | /* Instruction has three operands and one operand is stored in ECX register */ |
| 506 | #define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \ |
| 507 | do { \ |
| 508 | unsigned long _tmp; \ |
| 509 | _type _clv = (_cl).val; \ |
| 510 | _type _srcv = (_src).val; \ |
| 511 | _type _dstv = (_dst).val; \ |
| 512 | \ |
| 513 | __asm__ __volatile__ ( \ |
| 514 | _PRE_EFLAGS("0", "5", "2") \ |
| 515 | _op _suffix " %4,%1 \n" \ |
| 516 | _POST_EFLAGS("0", "5", "2") \ |
| 517 | : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \ |
| 518 | : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \ |
| 519 | ); \ |
| 520 | \ |
| 521 | (_cl).val = (unsigned long) _clv; \ |
| 522 | (_src).val = (unsigned long) _srcv; \ |
| 523 | (_dst).val = (unsigned long) _dstv; \ |
| 524 | } while (0) |
| 525 | |
| 526 | #define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \ |
| 527 | do { \ |
| 528 | switch ((_dst).bytes) { \ |
| 529 | case 2: \ |
| 530 | __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \ |
| 531 | "w", unsigned short); \ |
| 532 | break; \ |
| 533 | case 4: \ |
| 534 | __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \ |
| 535 | "l", unsigned int); \ |
| 536 | break; \ |
| 537 | case 8: \ |
| 538 | ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \ |
| 539 | "q", unsigned long)); \ |
| 540 | break; \ |
| 541 | } \ |
| 542 | } while (0) |
| 543 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 544 | #define __emulate_1op(_op, _dst, _eflags, _suffix) \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 545 | do { \ |
| 546 | unsigned long _tmp; \ |
| 547 | \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 548 | __asm__ __volatile__ ( \ |
| 549 | _PRE_EFLAGS("0", "3", "2") \ |
| 550 | _op _suffix " %1; " \ |
| 551 | _POST_EFLAGS("0", "3", "2") \ |
| 552 | : "=m" (_eflags), "+m" ((_dst).val), \ |
| 553 | "=&r" (_tmp) \ |
| 554 | : "i" (EFLAGS_MASK)); \ |
| 555 | } while (0) |
| 556 | |
| 557 | /* Instruction has only one explicit operand (no source operand). */ |
| 558 | #define emulate_1op(_op, _dst, _eflags) \ |
| 559 | do { \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 560 | switch ((_dst).bytes) { \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 561 | case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \ |
| 562 | case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \ |
| 563 | case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \ |
| 564 | case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 565 | } \ |
| 566 | } while (0) |
| 567 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 568 | /* Fetch next part of the instruction being emulated. */ |
| 569 | #define insn_fetch(_type, _size, _eip) \ |
| 570 | ({ unsigned long _x; \ |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 571 | rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \ |
Gleb Natapov | af5b4f7 | 2010-03-15 16:38:30 +0200 | [diff] [blame] | 572 | if (rc != X86EMUL_CONTINUE) \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 573 | goto done; \ |
| 574 | (_eip) += (_size); \ |
| 575 | (_type)_x; \ |
| 576 | }) |
| 577 | |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 578 | static inline unsigned long ad_mask(struct decode_cache *c) |
| 579 | { |
| 580 | return (1UL << (c->ad_bytes << 3)) - 1; |
| 581 | } |
| 582 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 583 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 584 | static inline unsigned long |
| 585 | address_mask(struct decode_cache *c, unsigned long reg) |
| 586 | { |
| 587 | if (c->ad_bytes == sizeof(unsigned long)) |
| 588 | return reg; |
| 589 | else |
| 590 | return reg & ad_mask(c); |
| 591 | } |
| 592 | |
| 593 | static inline unsigned long |
| 594 | register_address(struct decode_cache *c, unsigned long base, unsigned long reg) |
| 595 | { |
| 596 | return base + address_mask(c, reg); |
| 597 | } |
| 598 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 599 | static inline void |
| 600 | register_address_increment(struct decode_cache *c, unsigned long *reg, int inc) |
| 601 | { |
| 602 | if (c->ad_bytes == sizeof(unsigned long)) |
| 603 | *reg += inc; |
| 604 | else |
| 605 | *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c)); |
| 606 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 607 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 608 | static inline void jmp_rel(struct decode_cache *c, int rel) |
| 609 | { |
| 610 | register_address_increment(c, &c->eip, rel); |
| 611 | } |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 612 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 613 | static void set_seg_override(struct decode_cache *c, int seg) |
| 614 | { |
| 615 | c->has_seg_override = true; |
| 616 | c->seg_override = seg; |
| 617 | } |
| 618 | |
| 619 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
| 620 | { |
| 621 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 622 | return 0; |
| 623 | |
| 624 | return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg); |
| 625 | } |
| 626 | |
| 627 | static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt, |
| 628 | struct decode_cache *c) |
| 629 | { |
| 630 | if (!c->has_seg_override) |
| 631 | return 0; |
| 632 | |
| 633 | return seg_base(ctxt, c->seg_override); |
| 634 | } |
| 635 | |
| 636 | static unsigned long es_base(struct x86_emulate_ctxt *ctxt) |
| 637 | { |
| 638 | return seg_base(ctxt, VCPU_SREG_ES); |
| 639 | } |
| 640 | |
| 641 | static unsigned long ss_base(struct x86_emulate_ctxt *ctxt) |
| 642 | { |
| 643 | return seg_base(ctxt, VCPU_SREG_SS); |
| 644 | } |
| 645 | |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 646 | static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt, |
| 647 | struct x86_emulate_ops *ops, |
| 648 | unsigned long linear, u8 *dest) |
| 649 | { |
| 650 | struct fetch_cache *fc = &ctxt->decode.fetch; |
| 651 | int rc; |
| 652 | int size; |
| 653 | |
| 654 | if (linear < fc->start || linear >= fc->end) { |
| 655 | size = min(15UL, PAGE_SIZE - offset_in_page(linear)); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 656 | rc = ops->fetch(linear, fc->data, size, ctxt->vcpu, NULL); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 657 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 658 | return rc; |
| 659 | fc->start = linear; |
| 660 | fc->end = linear + size; |
| 661 | } |
| 662 | *dest = fc->data[linear - fc->start]; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 663 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, |
| 667 | struct x86_emulate_ops *ops, |
| 668 | unsigned long eip, void *dest, unsigned size) |
| 669 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 670 | int rc; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 671 | |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 672 | /* x86 instructions are limited to 15 bytes. */ |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 673 | if (eip + size - ctxt->eip > 15) |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 674 | return X86EMUL_UNHANDLEABLE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 675 | eip += ctxt->cs_base; |
| 676 | while (size--) { |
| 677 | rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 678 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 679 | return rc; |
| 680 | } |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 681 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 682 | } |
| 683 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 684 | /* |
| 685 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 686 | * pointer into the block that addresses the relevant register. |
| 687 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 688 | */ |
| 689 | static void *decode_register(u8 modrm_reg, unsigned long *regs, |
| 690 | int highbyte_regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 691 | { |
| 692 | void *p; |
| 693 | |
| 694 | p = ®s[modrm_reg]; |
| 695 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
| 696 | p = (unsigned char *)®s[modrm_reg & 3] + 1; |
| 697 | return p; |
| 698 | } |
| 699 | |
| 700 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
| 701 | struct x86_emulate_ops *ops, |
| 702 | void *ptr, |
| 703 | u16 *size, unsigned long *address, int op_bytes) |
| 704 | { |
| 705 | int rc; |
| 706 | |
| 707 | if (op_bytes == 2) |
| 708 | op_bytes = 3; |
| 709 | *address = 0; |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 710 | rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2, |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 711 | ctxt->vcpu, NULL); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 712 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 713 | return rc; |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 714 | rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes, |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 715 | ctxt->vcpu, NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 716 | return rc; |
| 717 | } |
| 718 | |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 719 | static int test_cc(unsigned int condition, unsigned int flags) |
| 720 | { |
| 721 | int rc = 0; |
| 722 | |
| 723 | switch ((condition & 15) >> 1) { |
| 724 | case 0: /* o */ |
| 725 | rc |= (flags & EFLG_OF); |
| 726 | break; |
| 727 | case 1: /* b/c/nae */ |
| 728 | rc |= (flags & EFLG_CF); |
| 729 | break; |
| 730 | case 2: /* z/e */ |
| 731 | rc |= (flags & EFLG_ZF); |
| 732 | break; |
| 733 | case 3: /* be/na */ |
| 734 | rc |= (flags & (EFLG_CF|EFLG_ZF)); |
| 735 | break; |
| 736 | case 4: /* s */ |
| 737 | rc |= (flags & EFLG_SF); |
| 738 | break; |
| 739 | case 5: /* p/pe */ |
| 740 | rc |= (flags & EFLG_PF); |
| 741 | break; |
| 742 | case 7: /* le/ng */ |
| 743 | rc |= (flags & EFLG_ZF); |
| 744 | /* fall through */ |
| 745 | case 6: /* l/nge */ |
| 746 | rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF)); |
| 747 | break; |
| 748 | } |
| 749 | |
| 750 | /* Odd condition identifiers (lsb == 1) have inverted sense. */ |
| 751 | return (!!rc ^ (condition & 1)); |
| 752 | } |
| 753 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 754 | static void decode_register_operand(struct operand *op, |
| 755 | struct decode_cache *c, |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 756 | int inhibit_bytereg) |
| 757 | { |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 758 | unsigned reg = c->modrm_reg; |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 759 | int highbyte_regs = c->rex_prefix == 0; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 760 | |
| 761 | if (!(c->d & ModRM)) |
| 762 | reg = (c->b & 7) | ((c->rex_prefix & 1) << 3); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 763 | op->type = OP_REG; |
| 764 | if ((c->d & ByteOp) && !inhibit_bytereg) { |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 765 | op->ptr = decode_register(reg, c->regs, highbyte_regs); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 766 | op->val = *(u8 *)op->ptr; |
| 767 | op->bytes = 1; |
| 768 | } else { |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 769 | op->ptr = decode_register(reg, c->regs, 0); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 770 | op->bytes = c->op_bytes; |
| 771 | switch (op->bytes) { |
| 772 | case 2: |
| 773 | op->val = *(u16 *)op->ptr; |
| 774 | break; |
| 775 | case 4: |
| 776 | op->val = *(u32 *)op->ptr; |
| 777 | break; |
| 778 | case 8: |
| 779 | op->val = *(u64 *) op->ptr; |
| 780 | break; |
| 781 | } |
| 782 | } |
| 783 | op->orig_val = op->val; |
| 784 | } |
| 785 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 786 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
| 787 | struct x86_emulate_ops *ops) |
| 788 | { |
| 789 | struct decode_cache *c = &ctxt->decode; |
| 790 | u8 sib; |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 791 | int index_reg = 0, base_reg = 0, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 792 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 793 | |
| 794 | if (c->rex_prefix) { |
| 795 | c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */ |
| 796 | index_reg = (c->rex_prefix & 2) << 2; /* REX.X */ |
| 797 | c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */ |
| 798 | } |
| 799 | |
| 800 | c->modrm = insn_fetch(u8, 1, c->eip); |
| 801 | c->modrm_mod |= (c->modrm & 0xc0) >> 6; |
| 802 | c->modrm_reg |= (c->modrm & 0x38) >> 3; |
| 803 | c->modrm_rm |= (c->modrm & 0x07); |
| 804 | c->modrm_ea = 0; |
| 805 | c->use_modrm_ea = 1; |
| 806 | |
| 807 | if (c->modrm_mod == 3) { |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 808 | c->modrm_ptr = decode_register(c->modrm_rm, |
| 809 | c->regs, c->d & ByteOp); |
| 810 | c->modrm_val = *(unsigned long *)c->modrm_ptr; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 811 | return rc; |
| 812 | } |
| 813 | |
| 814 | if (c->ad_bytes == 2) { |
| 815 | unsigned bx = c->regs[VCPU_REGS_RBX]; |
| 816 | unsigned bp = c->regs[VCPU_REGS_RBP]; |
| 817 | unsigned si = c->regs[VCPU_REGS_RSI]; |
| 818 | unsigned di = c->regs[VCPU_REGS_RDI]; |
| 819 | |
| 820 | /* 16-bit ModR/M decode. */ |
| 821 | switch (c->modrm_mod) { |
| 822 | case 0: |
| 823 | if (c->modrm_rm == 6) |
| 824 | c->modrm_ea += insn_fetch(u16, 2, c->eip); |
| 825 | break; |
| 826 | case 1: |
| 827 | c->modrm_ea += insn_fetch(s8, 1, c->eip); |
| 828 | break; |
| 829 | case 2: |
| 830 | c->modrm_ea += insn_fetch(u16, 2, c->eip); |
| 831 | break; |
| 832 | } |
| 833 | switch (c->modrm_rm) { |
| 834 | case 0: |
| 835 | c->modrm_ea += bx + si; |
| 836 | break; |
| 837 | case 1: |
| 838 | c->modrm_ea += bx + di; |
| 839 | break; |
| 840 | case 2: |
| 841 | c->modrm_ea += bp + si; |
| 842 | break; |
| 843 | case 3: |
| 844 | c->modrm_ea += bp + di; |
| 845 | break; |
| 846 | case 4: |
| 847 | c->modrm_ea += si; |
| 848 | break; |
| 849 | case 5: |
| 850 | c->modrm_ea += di; |
| 851 | break; |
| 852 | case 6: |
| 853 | if (c->modrm_mod != 0) |
| 854 | c->modrm_ea += bp; |
| 855 | break; |
| 856 | case 7: |
| 857 | c->modrm_ea += bx; |
| 858 | break; |
| 859 | } |
| 860 | if (c->modrm_rm == 2 || c->modrm_rm == 3 || |
| 861 | (c->modrm_rm == 6 && c->modrm_mod != 0)) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 862 | if (!c->has_seg_override) |
| 863 | set_seg_override(c, VCPU_SREG_SS); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 864 | c->modrm_ea = (u16)c->modrm_ea; |
| 865 | } else { |
| 866 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 867 | if ((c->modrm_rm & 7) == 4) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 868 | sib = insn_fetch(u8, 1, c->eip); |
| 869 | index_reg |= (sib >> 3) & 7; |
| 870 | base_reg |= sib & 7; |
| 871 | scale = sib >> 6; |
| 872 | |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 873 | if ((base_reg & 7) == 5 && c->modrm_mod == 0) |
| 874 | c->modrm_ea += insn_fetch(s32, 4, c->eip); |
| 875 | else |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 876 | c->modrm_ea += c->regs[base_reg]; |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 877 | if (index_reg != 4) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 878 | c->modrm_ea += c->regs[index_reg] << scale; |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 879 | } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) { |
| 880 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 881 | c->rip_relative = 1; |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 882 | } else |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 883 | c->modrm_ea += c->regs[c->modrm_rm]; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 884 | switch (c->modrm_mod) { |
| 885 | case 0: |
| 886 | if (c->modrm_rm == 5) |
| 887 | c->modrm_ea += insn_fetch(s32, 4, c->eip); |
| 888 | break; |
| 889 | case 1: |
| 890 | c->modrm_ea += insn_fetch(s8, 1, c->eip); |
| 891 | break; |
| 892 | case 2: |
| 893 | c->modrm_ea += insn_fetch(s32, 4, c->eip); |
| 894 | break; |
| 895 | } |
| 896 | } |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 897 | done: |
| 898 | return rc; |
| 899 | } |
| 900 | |
| 901 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
| 902 | struct x86_emulate_ops *ops) |
| 903 | { |
| 904 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 905 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 906 | |
| 907 | switch (c->ad_bytes) { |
| 908 | case 2: |
| 909 | c->modrm_ea = insn_fetch(u16, 2, c->eip); |
| 910 | break; |
| 911 | case 4: |
| 912 | c->modrm_ea = insn_fetch(u32, 4, c->eip); |
| 913 | break; |
| 914 | case 8: |
| 915 | c->modrm_ea = insn_fetch(u64, 8, c->eip); |
| 916 | break; |
| 917 | } |
| 918 | done: |
| 919 | return rc; |
| 920 | } |
| 921 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 922 | int |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 923 | x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 924 | { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 925 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 926 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 927 | int mode = ctxt->mode; |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 928 | int def_op_bytes, def_ad_bytes, group; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 929 | |
| 930 | /* Shadow copy of register state. Committed on successful emulation. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 931 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 932 | memset(c, 0, sizeof(struct decode_cache)); |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 933 | c->eip = ctxt->eip; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 934 | ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 935 | memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 936 | |
| 937 | switch (mode) { |
| 938 | case X86EMUL_MODE_REAL: |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 939 | case X86EMUL_MODE_VM86: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 940 | case X86EMUL_MODE_PROT16: |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 941 | def_op_bytes = def_ad_bytes = 2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 942 | break; |
| 943 | case X86EMUL_MODE_PROT32: |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 944 | def_op_bytes = def_ad_bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 945 | break; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 946 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 947 | case X86EMUL_MODE_PROT64: |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 948 | def_op_bytes = 4; |
| 949 | def_ad_bytes = 8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 950 | break; |
| 951 | #endif |
| 952 | default: |
| 953 | return -1; |
| 954 | } |
| 955 | |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 956 | c->op_bytes = def_op_bytes; |
| 957 | c->ad_bytes = def_ad_bytes; |
| 958 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 959 | /* Legacy prefixes. */ |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 960 | for (;;) { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 961 | switch (c->b = insn_fetch(u8, 1, c->eip)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 962 | case 0x66: /* operand-size override */ |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 963 | /* switch between 2/4 bytes */ |
| 964 | c->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 965 | break; |
| 966 | case 0x67: /* address-size override */ |
| 967 | if (mode == X86EMUL_MODE_PROT64) |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 968 | /* switch between 4/8 bytes */ |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 969 | c->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 970 | else |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 971 | /* switch between 2/4 bytes */ |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 972 | c->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 973 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 974 | case 0x26: /* ES override */ |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 975 | case 0x2e: /* CS override */ |
| 976 | case 0x36: /* SS override */ |
| 977 | case 0x3e: /* DS override */ |
| 978 | set_seg_override(c, (c->b >> 3) & 3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 979 | break; |
| 980 | case 0x64: /* FS override */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 981 | case 0x65: /* GS override */ |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 982 | set_seg_override(c, c->b & 7); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 983 | break; |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 984 | case 0x40 ... 0x4f: /* REX */ |
| 985 | if (mode != X86EMUL_MODE_PROT64) |
| 986 | goto done_prefixes; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 987 | c->rex_prefix = c->b; |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 988 | continue; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 989 | case 0xf0: /* LOCK */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 990 | c->lock_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 991 | break; |
Laurent Vivier | ae6200b | 2007-09-20 11:17:24 +0200 | [diff] [blame] | 992 | case 0xf2: /* REPNE/REPNZ */ |
Guillaume Thouvenin | 90e0a28 | 2007-11-22 11:32:09 +0100 | [diff] [blame] | 993 | c->rep_prefix = REPNE_PREFIX; |
| 994 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 995 | case 0xf3: /* REP/REPE/REPZ */ |
Guillaume Thouvenin | 90e0a28 | 2007-11-22 11:32:09 +0100 | [diff] [blame] | 996 | c->rep_prefix = REPE_PREFIX; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 997 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 998 | default: |
| 999 | goto done_prefixes; |
| 1000 | } |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 1001 | |
| 1002 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 1003 | |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1004 | c->rex_prefix = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | done_prefixes: |
| 1008 | |
| 1009 | /* REX prefix. */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1010 | if (c->rex_prefix) |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1011 | if (c->rex_prefix & 8) |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1012 | c->op_bytes = 8; /* REX.W */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1013 | |
| 1014 | /* Opcode byte(s). */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1015 | c->d = opcode_table[c->b]; |
| 1016 | if (c->d == 0) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1017 | /* Two-byte opcode? */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1018 | if (c->b == 0x0f) { |
| 1019 | c->twobyte = 1; |
| 1020 | c->b = insn_fetch(u8, 1, c->eip); |
| 1021 | c->d = twobyte_table[c->b]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1022 | } |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1023 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1024 | |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1025 | if (c->d & Group) { |
| 1026 | group = c->d & GroupMask; |
| 1027 | c->modrm = insn_fetch(u8, 1, c->eip); |
| 1028 | --c->eip; |
| 1029 | |
| 1030 | group = (group << 3) + ((c->modrm >> 3) & 7); |
| 1031 | if ((c->d & GroupDual) && (c->modrm >> 6) == 3) |
| 1032 | c->d = group2_table[group]; |
| 1033 | else |
| 1034 | c->d = group_table[group]; |
| 1035 | } |
| 1036 | |
| 1037 | /* Unrecognised? */ |
| 1038 | if (c->d == 0) { |
| 1039 | DPRINTF("Cannot emulate %02x\n", c->b); |
| 1040 | return -1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1041 | } |
| 1042 | |
Avi Kivity | 6e3d5df | 2007-12-06 18:14:14 +0200 | [diff] [blame] | 1043 | if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack)) |
| 1044 | c->op_bytes = 8; |
| 1045 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1046 | /* ModRM and SIB bytes. */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1047 | if (c->d & ModRM) |
| 1048 | rc = decode_modrm(ctxt, ops); |
| 1049 | else if (c->d & MemAbs) |
| 1050 | rc = decode_abs(ctxt, ops); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1051 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1052 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1053 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1054 | if (!c->has_seg_override) |
| 1055 | set_seg_override(c, VCPU_SREG_DS); |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 1056 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1057 | if (!(!c->twobyte && c->b == 0x8d)) |
| 1058 | c->modrm_ea += seg_override_base(ctxt, c); |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 1059 | |
| 1060 | if (c->ad_bytes != 8) |
| 1061 | c->modrm_ea = (u32)c->modrm_ea; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1062 | |
| 1063 | if (c->rip_relative) |
| 1064 | c->modrm_ea += c->eip; |
| 1065 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1066 | /* |
| 1067 | * Decode and fetch the source operand: register, memory |
| 1068 | * or immediate. |
| 1069 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1070 | switch (c->d & SrcMask) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1071 | case SrcNone: |
| 1072 | break; |
| 1073 | case SrcReg: |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 1074 | decode_register_operand(&c->src, c, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1075 | break; |
| 1076 | case SrcMem16: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1077 | c->src.bytes = 2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1078 | goto srcmem_common; |
| 1079 | case SrcMem32: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1080 | c->src.bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1081 | goto srcmem_common; |
| 1082 | case SrcMem: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1083 | c->src.bytes = (c->d & ByteOp) ? 1 : |
| 1084 | c->op_bytes; |
Rusty Russell | b85b9ee9 | 2007-09-09 14:12:54 +0300 | [diff] [blame] | 1085 | /* Don't fetch the address for invlpg: it could be unmapped. */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1086 | if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7) |
Rusty Russell | b85b9ee9 | 2007-09-09 14:12:54 +0300 | [diff] [blame] | 1087 | break; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1088 | srcmem_common: |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1089 | /* |
| 1090 | * For instructions with a ModR/M byte, switch to register |
| 1091 | * access if Mod = 3. |
| 1092 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1093 | if ((c->d & ModRM) && c->modrm_mod == 3) { |
| 1094 | c->src.type = OP_REG; |
Avi Kivity | 66b8550 | 2008-04-14 23:27:07 +0300 | [diff] [blame] | 1095 | c->src.val = c->modrm_val; |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 1096 | c->src.ptr = c->modrm_ptr; |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1097 | break; |
| 1098 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1099 | c->src.type = OP_MEM; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1100 | c->src.ptr = (unsigned long *)c->modrm_ea; |
| 1101 | c->src.val = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1102 | break; |
| 1103 | case SrcImm: |
Avi Kivity | c9eaf20 | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 1104 | case SrcImmU: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1105 | c->src.type = OP_IMM; |
| 1106 | c->src.ptr = (unsigned long *)c->eip; |
| 1107 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1108 | if (c->src.bytes == 8) |
| 1109 | c->src.bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1110 | /* NB. Immediates are sign-extended as necessary. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1111 | switch (c->src.bytes) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1112 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1113 | c->src.val = insn_fetch(s8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1114 | break; |
| 1115 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1116 | c->src.val = insn_fetch(s16, 2, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1117 | break; |
| 1118 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1119 | c->src.val = insn_fetch(s32, 4, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1120 | break; |
| 1121 | } |
Avi Kivity | c9eaf20 | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 1122 | if ((c->d & SrcMask) == SrcImmU) { |
| 1123 | switch (c->src.bytes) { |
| 1124 | case 1: |
| 1125 | c->src.val &= 0xff; |
| 1126 | break; |
| 1127 | case 2: |
| 1128 | c->src.val &= 0xffff; |
| 1129 | break; |
| 1130 | case 4: |
| 1131 | c->src.val &= 0xffffffff; |
| 1132 | break; |
| 1133 | } |
| 1134 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1135 | break; |
| 1136 | case SrcImmByte: |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 1137 | case SrcImmUByte: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1138 | c->src.type = OP_IMM; |
| 1139 | c->src.ptr = (unsigned long *)c->eip; |
| 1140 | c->src.bytes = 1; |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 1141 | if ((c->d & SrcMask) == SrcImmByte) |
| 1142 | c->src.val = insn_fetch(s8, 1, c->eip); |
| 1143 | else |
| 1144 | c->src.val = insn_fetch(u8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1145 | break; |
Guillaume Thouvenin | bfcadf8 | 2008-12-04 14:27:38 +0100 | [diff] [blame] | 1146 | case SrcOne: |
| 1147 | c->src.bytes = 1; |
| 1148 | c->src.val = 1; |
| 1149 | break; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame^] | 1150 | case SrcSI: |
| 1151 | c->src.type = OP_MEM; |
| 1152 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1153 | c->src.ptr = (unsigned long *) |
| 1154 | register_address(c, seg_override_base(ctxt, c), |
| 1155 | c->regs[VCPU_REGS_RSI]); |
| 1156 | c->src.val = 0; |
| 1157 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 1160 | /* |
| 1161 | * Decode and fetch the second source operand: register, memory |
| 1162 | * or immediate. |
| 1163 | */ |
| 1164 | switch (c->d & Src2Mask) { |
| 1165 | case Src2None: |
| 1166 | break; |
| 1167 | case Src2CL: |
| 1168 | c->src2.bytes = 1; |
| 1169 | c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8; |
| 1170 | break; |
| 1171 | case Src2ImmByte: |
| 1172 | c->src2.type = OP_IMM; |
| 1173 | c->src2.ptr = (unsigned long *)c->eip; |
| 1174 | c->src2.bytes = 1; |
| 1175 | c->src2.val = insn_fetch(u8, 1, c->eip); |
| 1176 | break; |
Gleb Natapov | a5f868b | 2009-04-12 13:36:14 +0300 | [diff] [blame] | 1177 | case Src2Imm16: |
| 1178 | c->src2.type = OP_IMM; |
| 1179 | c->src2.ptr = (unsigned long *)c->eip; |
| 1180 | c->src2.bytes = 2; |
| 1181 | c->src2.val = insn_fetch(u16, 2, c->eip); |
| 1182 | break; |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 1183 | case Src2One: |
| 1184 | c->src2.bytes = 1; |
| 1185 | c->src2.val = 1; |
| 1186 | break; |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 1187 | case Src2Mem16: |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 1188 | c->src2.type = OP_MEM; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1189 | c->src2.bytes = 2; |
| 1190 | c->src2.ptr = (unsigned long *)(c->modrm_ea + c->src.bytes); |
| 1191 | c->src2.val = 0; |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 1192 | break; |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 1193 | } |
| 1194 | |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1195 | /* Decode and fetch the destination operand: register or memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1196 | switch (c->d & DstMask) { |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1197 | case ImplicitOps: |
| 1198 | /* Special instructions do their own operand decoding. */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1199 | return 0; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1200 | case DstReg: |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 1201 | decode_register_operand(&c->dst, c, |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1202 | c->twobyte && (c->b == 0xb6 || c->b == 0xb7)); |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1203 | break; |
| 1204 | case DstMem: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1205 | if ((c->d & ModRM) && c->modrm_mod == 3) { |
Guillaume Thouvenin | 89c6963 | 2008-05-27 10:22:20 +0200 | [diff] [blame] | 1206 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1207 | c->dst.type = OP_REG; |
Avi Kivity | 66b8550 | 2008-04-14 23:27:07 +0300 | [diff] [blame] | 1208 | c->dst.val = c->dst.orig_val = c->modrm_val; |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 1209 | c->dst.ptr = c->modrm_ptr; |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1210 | break; |
| 1211 | } |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1212 | c->dst.type = OP_MEM; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1213 | c->dst.ptr = (unsigned long *)c->modrm_ea; |
| 1214 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1215 | c->dst.val = 0; |
| 1216 | if (c->d & BitOp) { |
| 1217 | unsigned long mask = ~(c->dst.bytes * 8 - 1); |
| 1218 | |
| 1219 | c->dst.ptr = (void *)c->dst.ptr + |
| 1220 | (c->src.val & mask) / 8; |
| 1221 | } |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1222 | break; |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1223 | case DstAcc: |
| 1224 | c->dst.type = OP_REG; |
Gleb Natapov | d6d367d | 2010-03-15 16:38:28 +0200 | [diff] [blame] | 1225 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1226 | c->dst.ptr = &c->regs[VCPU_REGS_RAX]; |
Gleb Natapov | d6d367d | 2010-03-15 16:38:28 +0200 | [diff] [blame] | 1227 | switch (c->dst.bytes) { |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1228 | case 1: |
| 1229 | c->dst.val = *(u8 *)c->dst.ptr; |
| 1230 | break; |
| 1231 | case 2: |
| 1232 | c->dst.val = *(u16 *)c->dst.ptr; |
| 1233 | break; |
| 1234 | case 4: |
| 1235 | c->dst.val = *(u32 *)c->dst.ptr; |
| 1236 | break; |
Gleb Natapov | d6d367d | 2010-03-15 16:38:28 +0200 | [diff] [blame] | 1237 | case 8: |
| 1238 | c->dst.val = *(u64 *)c->dst.ptr; |
| 1239 | break; |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1240 | } |
| 1241 | c->dst.orig_val = c->dst.val; |
| 1242 | break; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame^] | 1243 | case DstDI: |
| 1244 | c->dst.type = OP_MEM; |
| 1245 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1246 | c->dst.ptr = (unsigned long *) |
| 1247 | register_address(c, es_base(ctxt), |
| 1248 | c->regs[VCPU_REGS_RDI]); |
| 1249 | c->dst.val = 0; |
| 1250 | break; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | done: |
| 1254 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
| 1255 | } |
| 1256 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1257 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 1258 | { |
| 1259 | u32 limit = get_desc_limit(desc); |
| 1260 | |
| 1261 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 1262 | } |
| 1263 | |
| 1264 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
| 1265 | struct x86_emulate_ops *ops, |
| 1266 | u16 selector, struct desc_ptr *dt) |
| 1267 | { |
| 1268 | if (selector & 1 << 2) { |
| 1269 | struct desc_struct desc; |
| 1270 | memset (dt, 0, sizeof *dt); |
| 1271 | if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu)) |
| 1272 | return; |
| 1273 | |
| 1274 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
| 1275 | dt->address = get_desc_base(&desc); |
| 1276 | } else |
| 1277 | ops->get_gdt(dt, ctxt->vcpu); |
| 1278 | } |
| 1279 | |
| 1280 | /* allowed just for 8 bytes segments */ |
| 1281 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1282 | struct x86_emulate_ops *ops, |
| 1283 | u16 selector, struct desc_struct *desc) |
| 1284 | { |
| 1285 | struct desc_ptr dt; |
| 1286 | u16 index = selector >> 3; |
| 1287 | int ret; |
| 1288 | u32 err; |
| 1289 | ulong addr; |
| 1290 | |
| 1291 | get_descriptor_table_ptr(ctxt, ops, selector, &dt); |
| 1292 | |
| 1293 | if (dt.size < index * 8 + 7) { |
| 1294 | kvm_inject_gp(ctxt->vcpu, selector & 0xfffc); |
| 1295 | return X86EMUL_PROPAGATE_FAULT; |
| 1296 | } |
| 1297 | addr = dt.address + index * 8; |
| 1298 | ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err); |
| 1299 | if (ret == X86EMUL_PROPAGATE_FAULT) |
| 1300 | kvm_inject_page_fault(ctxt->vcpu, addr, err); |
| 1301 | |
| 1302 | return ret; |
| 1303 | } |
| 1304 | |
| 1305 | /* allowed just for 8 bytes segments */ |
| 1306 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1307 | struct x86_emulate_ops *ops, |
| 1308 | u16 selector, struct desc_struct *desc) |
| 1309 | { |
| 1310 | struct desc_ptr dt; |
| 1311 | u16 index = selector >> 3; |
| 1312 | u32 err; |
| 1313 | ulong addr; |
| 1314 | int ret; |
| 1315 | |
| 1316 | get_descriptor_table_ptr(ctxt, ops, selector, &dt); |
| 1317 | |
| 1318 | if (dt.size < index * 8 + 7) { |
| 1319 | kvm_inject_gp(ctxt->vcpu, selector & 0xfffc); |
| 1320 | return X86EMUL_PROPAGATE_FAULT; |
| 1321 | } |
| 1322 | |
| 1323 | addr = dt.address + index * 8; |
| 1324 | ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err); |
| 1325 | if (ret == X86EMUL_PROPAGATE_FAULT) |
| 1326 | kvm_inject_page_fault(ctxt->vcpu, addr, err); |
| 1327 | |
| 1328 | return ret; |
| 1329 | } |
| 1330 | |
| 1331 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1332 | struct x86_emulate_ops *ops, |
| 1333 | u16 selector, int seg) |
| 1334 | { |
| 1335 | struct desc_struct seg_desc; |
| 1336 | u8 dpl, rpl, cpl; |
| 1337 | unsigned err_vec = GP_VECTOR; |
| 1338 | u32 err_code = 0; |
| 1339 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
| 1340 | int ret; |
| 1341 | |
| 1342 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1343 | |
| 1344 | if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) |
| 1345 | || ctxt->mode == X86EMUL_MODE_REAL) { |
| 1346 | /* set real mode segment descriptor */ |
| 1347 | set_desc_base(&seg_desc, selector << 4); |
| 1348 | set_desc_limit(&seg_desc, 0xffff); |
| 1349 | seg_desc.type = 3; |
| 1350 | seg_desc.p = 1; |
| 1351 | seg_desc.s = 1; |
| 1352 | goto load; |
| 1353 | } |
| 1354 | |
| 1355 | /* NULL selector is not valid for TR, CS and SS */ |
| 1356 | if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR) |
| 1357 | && null_selector) |
| 1358 | goto exception; |
| 1359 | |
| 1360 | /* TR should be in GDT only */ |
| 1361 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1362 | goto exception; |
| 1363 | |
| 1364 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1365 | goto load; |
| 1366 | |
| 1367 | ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc); |
| 1368 | if (ret != X86EMUL_CONTINUE) |
| 1369 | return ret; |
| 1370 | |
| 1371 | err_code = selector & 0xfffc; |
| 1372 | err_vec = GP_VECTOR; |
| 1373 | |
| 1374 | /* can't load system descriptor into segment selecor */ |
| 1375 | if (seg <= VCPU_SREG_GS && !seg_desc.s) |
| 1376 | goto exception; |
| 1377 | |
| 1378 | if (!seg_desc.p) { |
| 1379 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1380 | goto exception; |
| 1381 | } |
| 1382 | |
| 1383 | rpl = selector & 3; |
| 1384 | dpl = seg_desc.dpl; |
| 1385 | cpl = ops->cpl(ctxt->vcpu); |
| 1386 | |
| 1387 | switch (seg) { |
| 1388 | case VCPU_SREG_SS: |
| 1389 | /* |
| 1390 | * segment is not a writable data segment or segment |
| 1391 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1392 | */ |
| 1393 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1394 | goto exception; |
| 1395 | break; |
| 1396 | case VCPU_SREG_CS: |
| 1397 | if (!(seg_desc.type & 8)) |
| 1398 | goto exception; |
| 1399 | |
| 1400 | if (seg_desc.type & 4) { |
| 1401 | /* conforming */ |
| 1402 | if (dpl > cpl) |
| 1403 | goto exception; |
| 1404 | } else { |
| 1405 | /* nonconforming */ |
| 1406 | if (rpl > cpl || dpl != cpl) |
| 1407 | goto exception; |
| 1408 | } |
| 1409 | /* CS(RPL) <- CPL */ |
| 1410 | selector = (selector & 0xfffc) | cpl; |
| 1411 | break; |
| 1412 | case VCPU_SREG_TR: |
| 1413 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1414 | goto exception; |
| 1415 | break; |
| 1416 | case VCPU_SREG_LDTR: |
| 1417 | if (seg_desc.s || seg_desc.type != 2) |
| 1418 | goto exception; |
| 1419 | break; |
| 1420 | default: /* DS, ES, FS, or GS */ |
| 1421 | /* |
| 1422 | * segment is not a data or readable code segment or |
| 1423 | * ((segment is a data or nonconforming code segment) |
| 1424 | * and (both RPL and CPL > DPL)) |
| 1425 | */ |
| 1426 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1427 | (((seg_desc.type & 0xc) != 0xc) && |
| 1428 | (rpl > dpl && cpl > dpl))) |
| 1429 | goto exception; |
| 1430 | break; |
| 1431 | } |
| 1432 | |
| 1433 | if (seg_desc.s) { |
| 1434 | /* mark segment as accessed */ |
| 1435 | seg_desc.type |= 1; |
| 1436 | ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc); |
| 1437 | if (ret != X86EMUL_CONTINUE) |
| 1438 | return ret; |
| 1439 | } |
| 1440 | load: |
| 1441 | ops->set_segment_selector(selector, seg, ctxt->vcpu); |
| 1442 | ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu); |
| 1443 | return X86EMUL_CONTINUE; |
| 1444 | exception: |
| 1445 | kvm_queue_exception_e(ctxt->vcpu, err_vec, err_code); |
| 1446 | return X86EMUL_PROPAGATE_FAULT; |
| 1447 | } |
| 1448 | |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1449 | static inline void emulate_push(struct x86_emulate_ctxt *ctxt) |
| 1450 | { |
| 1451 | struct decode_cache *c = &ctxt->decode; |
| 1452 | |
| 1453 | c->dst.type = OP_MEM; |
| 1454 | c->dst.bytes = c->op_bytes; |
| 1455 | c->dst.val = c->src.val; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 1456 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1457 | c->dst.ptr = (void *) register_address(c, ss_base(ctxt), |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1458 | c->regs[VCPU_REGS_RSP]); |
| 1459 | } |
| 1460 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1461 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1462 | struct x86_emulate_ops *ops, |
| 1463 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1464 | { |
| 1465 | struct decode_cache *c = &ctxt->decode; |
| 1466 | int rc; |
| 1467 | |
Avi Kivity | 781d0ed | 2008-11-27 18:00:28 +0200 | [diff] [blame] | 1468 | rc = ops->read_emulated(register_address(c, ss_base(ctxt), |
| 1469 | c->regs[VCPU_REGS_RSP]), |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1470 | dest, len, ctxt->vcpu); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1471 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1472 | return rc; |
| 1473 | |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1474 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1475 | return rc; |
| 1476 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1477 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1478 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
| 1479 | struct x86_emulate_ops *ops, |
| 1480 | void *dest, int len) |
| 1481 | { |
| 1482 | int rc; |
| 1483 | unsigned long val, change_mask; |
| 1484 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 1485 | int cpl = ops->cpl(ctxt->vcpu); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1486 | |
| 1487 | rc = emulate_pop(ctxt, ops, &val, len); |
| 1488 | if (rc != X86EMUL_CONTINUE) |
| 1489 | return rc; |
| 1490 | |
| 1491 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
| 1492 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID; |
| 1493 | |
| 1494 | switch(ctxt->mode) { |
| 1495 | case X86EMUL_MODE_PROT64: |
| 1496 | case X86EMUL_MODE_PROT32: |
| 1497 | case X86EMUL_MODE_PROT16: |
| 1498 | if (cpl == 0) |
| 1499 | change_mask |= EFLG_IOPL; |
| 1500 | if (cpl <= iopl) |
| 1501 | change_mask |= EFLG_IF; |
| 1502 | break; |
| 1503 | case X86EMUL_MODE_VM86: |
| 1504 | if (iopl < 3) { |
| 1505 | kvm_inject_gp(ctxt->vcpu, 0); |
| 1506 | return X86EMUL_PROPAGATE_FAULT; |
| 1507 | } |
| 1508 | change_mask |= EFLG_IF; |
| 1509 | break; |
| 1510 | default: /* real mode */ |
| 1511 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1512 | break; |
| 1513 | } |
| 1514 | |
| 1515 | *(unsigned long *)dest = |
| 1516 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1517 | |
| 1518 | return rc; |
| 1519 | } |
| 1520 | |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1521 | static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg) |
| 1522 | { |
| 1523 | struct decode_cache *c = &ctxt->decode; |
| 1524 | struct kvm_segment segment; |
| 1525 | |
| 1526 | kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg); |
| 1527 | |
| 1528 | c->src.val = segment.selector; |
| 1529 | emulate_push(ctxt); |
| 1530 | } |
| 1531 | |
| 1532 | static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, |
| 1533 | struct x86_emulate_ops *ops, int seg) |
| 1534 | { |
| 1535 | struct decode_cache *c = &ctxt->decode; |
| 1536 | unsigned long selector; |
| 1537 | int rc; |
| 1538 | |
| 1539 | rc = emulate_pop(ctxt, ops, &selector, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1540 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1541 | return rc; |
| 1542 | |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 1543 | rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1544 | return rc; |
| 1545 | } |
| 1546 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1547 | static void emulate_pusha(struct x86_emulate_ctxt *ctxt) |
| 1548 | { |
| 1549 | struct decode_cache *c = &ctxt->decode; |
| 1550 | unsigned long old_esp = c->regs[VCPU_REGS_RSP]; |
| 1551 | int reg = VCPU_REGS_RAX; |
| 1552 | |
| 1553 | while (reg <= VCPU_REGS_RDI) { |
| 1554 | (reg == VCPU_REGS_RSP) ? |
| 1555 | (c->src.val = old_esp) : (c->src.val = c->regs[reg]); |
| 1556 | |
| 1557 | emulate_push(ctxt); |
| 1558 | ++reg; |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | static int emulate_popa(struct x86_emulate_ctxt *ctxt, |
| 1563 | struct x86_emulate_ops *ops) |
| 1564 | { |
| 1565 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1566 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1567 | int reg = VCPU_REGS_RDI; |
| 1568 | |
| 1569 | while (reg >= VCPU_REGS_RAX) { |
| 1570 | if (reg == VCPU_REGS_RSP) { |
| 1571 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], |
| 1572 | c->op_bytes); |
| 1573 | --reg; |
| 1574 | } |
| 1575 | |
| 1576 | rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1577 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1578 | break; |
| 1579 | --reg; |
| 1580 | } |
| 1581 | return rc; |
| 1582 | } |
| 1583 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1584 | static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt, |
| 1585 | struct x86_emulate_ops *ops) |
| 1586 | { |
| 1587 | struct decode_cache *c = &ctxt->decode; |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1588 | |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1589 | return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1590 | } |
| 1591 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1592 | static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1593 | { |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1594 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1595 | switch (c->modrm_reg) { |
| 1596 | case 0: /* rol */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1597 | emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1598 | break; |
| 1599 | case 1: /* ror */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1600 | emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1601 | break; |
| 1602 | case 2: /* rcl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1603 | emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1604 | break; |
| 1605 | case 3: /* rcr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1606 | emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1607 | break; |
| 1608 | case 4: /* sal/shl */ |
| 1609 | case 6: /* sal/shl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1610 | emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1611 | break; |
| 1612 | case 5: /* shr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1613 | emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1614 | break; |
| 1615 | case 7: /* sar */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1616 | emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1617 | break; |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1622 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1623 | { |
| 1624 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1625 | |
| 1626 | switch (c->modrm_reg) { |
| 1627 | case 0 ... 1: /* test */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1628 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1629 | break; |
| 1630 | case 2: /* not */ |
| 1631 | c->dst.val = ~c->dst.val; |
| 1632 | break; |
| 1633 | case 3: /* neg */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1634 | emulate_1op("neg", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1635 | break; |
| 1636 | default: |
Gleb Natapov | aca06a8 | 2010-03-18 15:20:15 +0200 | [diff] [blame] | 1637 | return 0; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1638 | } |
Gleb Natapov | aca06a8 | 2010-03-18 15:20:15 +0200 | [diff] [blame] | 1639 | return 1; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 1643 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1644 | { |
| 1645 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1646 | |
| 1647 | switch (c->modrm_reg) { |
| 1648 | case 0: /* inc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1649 | emulate_1op("inc", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1650 | break; |
| 1651 | case 1: /* dec */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1652 | emulate_1op("dec", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1653 | break; |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1654 | case 2: /* call near abs */ { |
| 1655 | long int old_eip; |
| 1656 | old_eip = c->eip; |
| 1657 | c->eip = c->src.val; |
| 1658 | c->src.val = old_eip; |
| 1659 | emulate_push(ctxt); |
| 1660 | break; |
| 1661 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1662 | case 4: /* jmp abs */ |
Avi Kivity | fd60754 | 2008-01-18 13:12:26 +0200 | [diff] [blame] | 1663 | c->eip = c->src.val; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1664 | break; |
| 1665 | case 6: /* push */ |
Avi Kivity | fd60754 | 2008-01-18 13:12:26 +0200 | [diff] [blame] | 1666 | emulate_push(ctxt); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1667 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1668 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1669 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1673 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1674 | { |
| 1675 | struct decode_cache *c = &ctxt->decode; |
| 1676 | u64 old, new; |
| 1677 | int rc; |
| 1678 | |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1679 | rc = ops->read_emulated(c->modrm_ea, &old, 8, ctxt->vcpu); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1680 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1681 | return rc; |
| 1682 | |
| 1683 | if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) || |
| 1684 | ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) { |
| 1685 | |
| 1686 | c->regs[VCPU_REGS_RAX] = (u32) (old >> 0); |
| 1687 | c->regs[VCPU_REGS_RDX] = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1688 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1689 | |
| 1690 | } else { |
| 1691 | new = ((u64)c->regs[VCPU_REGS_RCX] << 32) | |
| 1692 | (u32) c->regs[VCPU_REGS_RBX]; |
| 1693 | |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1694 | rc = ops->cmpxchg_emulated(c->modrm_ea, &old, &new, 8, ctxt->vcpu); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1695 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1696 | return rc; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1697 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1698 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1699 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1700 | } |
| 1701 | |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1702 | static int emulate_ret_far(struct x86_emulate_ctxt *ctxt, |
| 1703 | struct x86_emulate_ops *ops) |
| 1704 | { |
| 1705 | struct decode_cache *c = &ctxt->decode; |
| 1706 | int rc; |
| 1707 | unsigned long cs; |
| 1708 | |
| 1709 | rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1710 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1711 | return rc; |
| 1712 | if (c->op_bytes == 4) |
| 1713 | c->eip = (u32)c->eip; |
| 1714 | rc = emulate_pop(ctxt, ops, &cs, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1715 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1716 | return rc; |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 1717 | rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1718 | return rc; |
| 1719 | } |
| 1720 | |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1721 | static inline int writeback(struct x86_emulate_ctxt *ctxt, |
| 1722 | struct x86_emulate_ops *ops) |
| 1723 | { |
| 1724 | int rc; |
| 1725 | struct decode_cache *c = &ctxt->decode; |
| 1726 | |
| 1727 | switch (c->dst.type) { |
| 1728 | case OP_REG: |
| 1729 | /* The 4-byte case *is* correct: |
| 1730 | * in 64-bit mode we zero-extend. |
| 1731 | */ |
| 1732 | switch (c->dst.bytes) { |
| 1733 | case 1: |
| 1734 | *(u8 *)c->dst.ptr = (u8)c->dst.val; |
| 1735 | break; |
| 1736 | case 2: |
| 1737 | *(u16 *)c->dst.ptr = (u16)c->dst.val; |
| 1738 | break; |
| 1739 | case 4: |
| 1740 | *c->dst.ptr = (u32)c->dst.val; |
| 1741 | break; /* 64b: zero-ext */ |
| 1742 | case 8: |
| 1743 | *c->dst.ptr = c->dst.val; |
| 1744 | break; |
| 1745 | } |
| 1746 | break; |
| 1747 | case OP_MEM: |
| 1748 | if (c->lock_prefix) |
| 1749 | rc = ops->cmpxchg_emulated( |
| 1750 | (unsigned long)c->dst.ptr, |
| 1751 | &c->dst.orig_val, |
| 1752 | &c->dst.val, |
| 1753 | c->dst.bytes, |
| 1754 | ctxt->vcpu); |
| 1755 | else |
| 1756 | rc = ops->write_emulated( |
| 1757 | (unsigned long)c->dst.ptr, |
| 1758 | &c->dst.val, |
| 1759 | c->dst.bytes, |
| 1760 | ctxt->vcpu); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1761 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1762 | return rc; |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 1763 | break; |
| 1764 | case OP_NONE: |
| 1765 | /* no writeback */ |
| 1766 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1767 | default: |
| 1768 | break; |
| 1769 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1770 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1771 | } |
| 1772 | |
Jaswinder Singh Rajput | a3f9d39 | 2009-06-18 16:53:25 +0530 | [diff] [blame] | 1773 | static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask) |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 1774 | { |
| 1775 | u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask); |
| 1776 | /* |
| 1777 | * an sti; sti; sequence only disable interrupts for the first |
| 1778 | * instruction. So, if the last instruction, be it emulated or |
| 1779 | * not, left the system with the INT_STI flag enabled, it |
| 1780 | * means that the last instruction is an sti. We should not |
| 1781 | * leave the flag on in this case. The same goes for mov ss |
| 1782 | */ |
| 1783 | if (!(int_shadow & mask)) |
| 1784 | ctxt->interruptibility = mask; |
| 1785 | } |
| 1786 | |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1787 | static inline void |
| 1788 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
| 1789 | struct kvm_segment *cs, struct kvm_segment *ss) |
| 1790 | { |
| 1791 | memset(cs, 0, sizeof(struct kvm_segment)); |
| 1792 | kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS); |
| 1793 | memset(ss, 0, sizeof(struct kvm_segment)); |
| 1794 | |
| 1795 | cs->l = 0; /* will be adjusted later */ |
| 1796 | cs->base = 0; /* flat segment */ |
| 1797 | cs->g = 1; /* 4kb granularity */ |
| 1798 | cs->limit = 0xffffffff; /* 4GB limit */ |
| 1799 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 1800 | cs->s = 1; |
| 1801 | cs->dpl = 0; /* will be adjusted later */ |
| 1802 | cs->present = 1; |
| 1803 | cs->db = 1; |
| 1804 | |
| 1805 | ss->unusable = 0; |
| 1806 | ss->base = 0; /* flat segment */ |
| 1807 | ss->limit = 0xffffffff; /* 4GB limit */ |
| 1808 | ss->g = 1; /* 4kb granularity */ |
| 1809 | ss->s = 1; |
| 1810 | ss->type = 0x03; /* Read/Write, Accessed */ |
| 1811 | ss->db = 1; /* 32bit stack segment */ |
| 1812 | ss->dpl = 0; |
| 1813 | ss->present = 1; |
| 1814 | } |
| 1815 | |
| 1816 | static int |
| 1817 | emulate_syscall(struct x86_emulate_ctxt *ctxt) |
| 1818 | { |
| 1819 | struct decode_cache *c = &ctxt->decode; |
| 1820 | struct kvm_segment cs, ss; |
| 1821 | u64 msr_data; |
| 1822 | |
| 1823 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 1824 | if (ctxt->mode == X86EMUL_MODE_REAL || |
| 1825 | ctxt->mode == X86EMUL_MODE_VM86) { |
| 1826 | kvm_queue_exception(ctxt->vcpu, UD_VECTOR); |
| 1827 | return X86EMUL_PROPAGATE_FAULT; |
| 1828 | } |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1829 | |
| 1830 | setup_syscalls_segments(ctxt, &cs, &ss); |
| 1831 | |
| 1832 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
| 1833 | msr_data >>= 32; |
| 1834 | cs.selector = (u16)(msr_data & 0xfffc); |
| 1835 | ss.selector = (u16)(msr_data + 8); |
| 1836 | |
| 1837 | if (is_long_mode(ctxt->vcpu)) { |
| 1838 | cs.db = 0; |
| 1839 | cs.l = 1; |
| 1840 | } |
| 1841 | kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS); |
| 1842 | kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS); |
| 1843 | |
| 1844 | c->regs[VCPU_REGS_RCX] = c->eip; |
| 1845 | if (is_long_mode(ctxt->vcpu)) { |
| 1846 | #ifdef CONFIG_X86_64 |
| 1847 | c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF; |
| 1848 | |
| 1849 | kvm_x86_ops->get_msr(ctxt->vcpu, |
| 1850 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 1851 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
| 1852 | c->eip = msr_data; |
| 1853 | |
| 1854 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data); |
| 1855 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
| 1856 | #endif |
| 1857 | } else { |
| 1858 | /* legacy mode */ |
| 1859 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
| 1860 | c->eip = (u32)msr_data; |
| 1861 | |
| 1862 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 1863 | } |
| 1864 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1865 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1866 | } |
| 1867 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1868 | static int |
| 1869 | emulate_sysenter(struct x86_emulate_ctxt *ctxt) |
| 1870 | { |
| 1871 | struct decode_cache *c = &ctxt->decode; |
| 1872 | struct kvm_segment cs, ss; |
| 1873 | u64 msr_data; |
| 1874 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 1875 | /* inject #GP if in real mode */ |
| 1876 | if (ctxt->mode == X86EMUL_MODE_REAL) { |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1877 | kvm_inject_gp(ctxt->vcpu, 0); |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 1878 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 1882 | * Therefore, we inject an #UD. |
| 1883 | */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 1884 | if (ctxt->mode == X86EMUL_MODE_PROT64) { |
| 1885 | kvm_queue_exception(ctxt->vcpu, UD_VECTOR); |
| 1886 | return X86EMUL_PROPAGATE_FAULT; |
| 1887 | } |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1888 | |
| 1889 | setup_syscalls_segments(ctxt, &cs, &ss); |
| 1890 | |
| 1891 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
| 1892 | switch (ctxt->mode) { |
| 1893 | case X86EMUL_MODE_PROT32: |
| 1894 | if ((msr_data & 0xfffc) == 0x0) { |
| 1895 | kvm_inject_gp(ctxt->vcpu, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1896 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1897 | } |
| 1898 | break; |
| 1899 | case X86EMUL_MODE_PROT64: |
| 1900 | if (msr_data == 0x0) { |
| 1901 | kvm_inject_gp(ctxt->vcpu, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1902 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1903 | } |
| 1904 | break; |
| 1905 | } |
| 1906 | |
| 1907 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 1908 | cs.selector = (u16)msr_data; |
| 1909 | cs.selector &= ~SELECTOR_RPL_MASK; |
| 1910 | ss.selector = cs.selector + 8; |
| 1911 | ss.selector &= ~SELECTOR_RPL_MASK; |
| 1912 | if (ctxt->mode == X86EMUL_MODE_PROT64 |
| 1913 | || is_long_mode(ctxt->vcpu)) { |
| 1914 | cs.db = 0; |
| 1915 | cs.l = 1; |
| 1916 | } |
| 1917 | |
| 1918 | kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS); |
| 1919 | kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS); |
| 1920 | |
| 1921 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data); |
| 1922 | c->eip = msr_data; |
| 1923 | |
| 1924 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data); |
| 1925 | c->regs[VCPU_REGS_RSP] = msr_data; |
| 1926 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1927 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1928 | } |
| 1929 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1930 | static int |
| 1931 | emulate_sysexit(struct x86_emulate_ctxt *ctxt) |
| 1932 | { |
| 1933 | struct decode_cache *c = &ctxt->decode; |
| 1934 | struct kvm_segment cs, ss; |
| 1935 | u64 msr_data; |
| 1936 | int usermode; |
| 1937 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 1938 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 1939 | if (ctxt->mode == X86EMUL_MODE_REAL || |
| 1940 | ctxt->mode == X86EMUL_MODE_VM86) { |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1941 | kvm_inject_gp(ctxt->vcpu, 0); |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 1942 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1943 | } |
| 1944 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1945 | setup_syscalls_segments(ctxt, &cs, &ss); |
| 1946 | |
| 1947 | if ((c->rex_prefix & 0x8) != 0x0) |
| 1948 | usermode = X86EMUL_MODE_PROT64; |
| 1949 | else |
| 1950 | usermode = X86EMUL_MODE_PROT32; |
| 1951 | |
| 1952 | cs.dpl = 3; |
| 1953 | ss.dpl = 3; |
| 1954 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
| 1955 | switch (usermode) { |
| 1956 | case X86EMUL_MODE_PROT32: |
| 1957 | cs.selector = (u16)(msr_data + 16); |
| 1958 | if ((msr_data & 0xfffc) == 0x0) { |
| 1959 | kvm_inject_gp(ctxt->vcpu, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1960 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1961 | } |
| 1962 | ss.selector = (u16)(msr_data + 24); |
| 1963 | break; |
| 1964 | case X86EMUL_MODE_PROT64: |
| 1965 | cs.selector = (u16)(msr_data + 32); |
| 1966 | if (msr_data == 0x0) { |
| 1967 | kvm_inject_gp(ctxt->vcpu, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1968 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1969 | } |
| 1970 | ss.selector = cs.selector + 8; |
| 1971 | cs.db = 0; |
| 1972 | cs.l = 1; |
| 1973 | break; |
| 1974 | } |
| 1975 | cs.selector |= SELECTOR_RPL_MASK; |
| 1976 | ss.selector |= SELECTOR_RPL_MASK; |
| 1977 | |
| 1978 | kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS); |
| 1979 | kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS); |
| 1980 | |
| 1981 | c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX]; |
| 1982 | c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX]; |
| 1983 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1984 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1985 | } |
| 1986 | |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 1987 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt, |
| 1988 | struct x86_emulate_ops *ops) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1989 | { |
| 1990 | int iopl; |
| 1991 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 1992 | return false; |
| 1993 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 1994 | return true; |
| 1995 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 1996 | return ops->cpl(ctxt->vcpu) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
| 2000 | struct x86_emulate_ops *ops, |
| 2001 | u16 port, u16 len) |
| 2002 | { |
| 2003 | struct kvm_segment tr_seg; |
| 2004 | int r; |
| 2005 | u16 io_bitmap_ptr; |
| 2006 | u8 perm, bit_idx = port & 0x7; |
| 2007 | unsigned mask = (1 << len) - 1; |
| 2008 | |
| 2009 | kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR); |
| 2010 | if (tr_seg.unusable) |
| 2011 | return false; |
| 2012 | if (tr_seg.limit < 103) |
| 2013 | return false; |
| 2014 | r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu, |
| 2015 | NULL); |
| 2016 | if (r != X86EMUL_CONTINUE) |
| 2017 | return false; |
| 2018 | if (io_bitmap_ptr + port/8 > tr_seg.limit) |
| 2019 | return false; |
| 2020 | r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1, |
| 2021 | ctxt->vcpu, NULL); |
| 2022 | if (r != X86EMUL_CONTINUE) |
| 2023 | return false; |
| 2024 | if ((perm >> bit_idx) & mask) |
| 2025 | return false; |
| 2026 | return true; |
| 2027 | } |
| 2028 | |
| 2029 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
| 2030 | struct x86_emulate_ops *ops, |
| 2031 | u16 port, u16 len) |
| 2032 | { |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 2033 | if (emulator_bad_iopl(ctxt, ops)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2034 | if (!emulator_io_port_access_allowed(ctxt, ops, port, len)) |
| 2035 | return false; |
| 2036 | return true; |
| 2037 | } |
| 2038 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2039 | static u32 get_cached_descriptor_base(struct x86_emulate_ctxt *ctxt, |
| 2040 | struct x86_emulate_ops *ops, |
| 2041 | int seg) |
| 2042 | { |
| 2043 | struct desc_struct desc; |
| 2044 | if (ops->get_cached_descriptor(&desc, seg, ctxt->vcpu)) |
| 2045 | return get_desc_base(&desc); |
| 2046 | else |
| 2047 | return ~0; |
| 2048 | } |
| 2049 | |
| 2050 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
| 2051 | struct x86_emulate_ops *ops, |
| 2052 | struct tss_segment_16 *tss) |
| 2053 | { |
| 2054 | struct decode_cache *c = &ctxt->decode; |
| 2055 | |
| 2056 | tss->ip = c->eip; |
| 2057 | tss->flag = ctxt->eflags; |
| 2058 | tss->ax = c->regs[VCPU_REGS_RAX]; |
| 2059 | tss->cx = c->regs[VCPU_REGS_RCX]; |
| 2060 | tss->dx = c->regs[VCPU_REGS_RDX]; |
| 2061 | tss->bx = c->regs[VCPU_REGS_RBX]; |
| 2062 | tss->sp = c->regs[VCPU_REGS_RSP]; |
| 2063 | tss->bp = c->regs[VCPU_REGS_RBP]; |
| 2064 | tss->si = c->regs[VCPU_REGS_RSI]; |
| 2065 | tss->di = c->regs[VCPU_REGS_RDI]; |
| 2066 | |
| 2067 | tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu); |
| 2068 | tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 2069 | tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu); |
| 2070 | tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu); |
| 2071 | tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu); |
| 2072 | } |
| 2073 | |
| 2074 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
| 2075 | struct x86_emulate_ops *ops, |
| 2076 | struct tss_segment_16 *tss) |
| 2077 | { |
| 2078 | struct decode_cache *c = &ctxt->decode; |
| 2079 | int ret; |
| 2080 | |
| 2081 | c->eip = tss->ip; |
| 2082 | ctxt->eflags = tss->flag | 2; |
| 2083 | c->regs[VCPU_REGS_RAX] = tss->ax; |
| 2084 | c->regs[VCPU_REGS_RCX] = tss->cx; |
| 2085 | c->regs[VCPU_REGS_RDX] = tss->dx; |
| 2086 | c->regs[VCPU_REGS_RBX] = tss->bx; |
| 2087 | c->regs[VCPU_REGS_RSP] = tss->sp; |
| 2088 | c->regs[VCPU_REGS_RBP] = tss->bp; |
| 2089 | c->regs[VCPU_REGS_RSI] = tss->si; |
| 2090 | c->regs[VCPU_REGS_RDI] = tss->di; |
| 2091 | |
| 2092 | /* |
| 2093 | * SDM says that segment selectors are loaded before segment |
| 2094 | * descriptors |
| 2095 | */ |
| 2096 | ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu); |
| 2097 | ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu); |
| 2098 | ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu); |
| 2099 | ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu); |
| 2100 | ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu); |
| 2101 | |
| 2102 | /* |
| 2103 | * Now load segment descriptors. If fault happenes at this stage |
| 2104 | * it is handled in a context of new task |
| 2105 | */ |
| 2106 | ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR); |
| 2107 | if (ret != X86EMUL_CONTINUE) |
| 2108 | return ret; |
| 2109 | ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES); |
| 2110 | if (ret != X86EMUL_CONTINUE) |
| 2111 | return ret; |
| 2112 | ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS); |
| 2113 | if (ret != X86EMUL_CONTINUE) |
| 2114 | return ret; |
| 2115 | ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS); |
| 2116 | if (ret != X86EMUL_CONTINUE) |
| 2117 | return ret; |
| 2118 | ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS); |
| 2119 | if (ret != X86EMUL_CONTINUE) |
| 2120 | return ret; |
| 2121 | |
| 2122 | return X86EMUL_CONTINUE; |
| 2123 | } |
| 2124 | |
| 2125 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
| 2126 | struct x86_emulate_ops *ops, |
| 2127 | u16 tss_selector, u16 old_tss_sel, |
| 2128 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2129 | { |
| 2130 | struct tss_segment_16 tss_seg; |
| 2131 | int ret; |
| 2132 | u32 err, new_tss_base = get_desc_base(new_desc); |
| 2133 | |
| 2134 | ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2135 | &err); |
| 2136 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2137 | /* FIXME: need to provide precise fault address */ |
| 2138 | kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err); |
| 2139 | return ret; |
| 2140 | } |
| 2141 | |
| 2142 | save_state_to_tss16(ctxt, ops, &tss_seg); |
| 2143 | |
| 2144 | ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2145 | &err); |
| 2146 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2147 | /* FIXME: need to provide precise fault address */ |
| 2148 | kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err); |
| 2149 | return ret; |
| 2150 | } |
| 2151 | |
| 2152 | ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2153 | &err); |
| 2154 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2155 | /* FIXME: need to provide precise fault address */ |
| 2156 | kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err); |
| 2157 | return ret; |
| 2158 | } |
| 2159 | |
| 2160 | if (old_tss_sel != 0xffff) { |
| 2161 | tss_seg.prev_task_link = old_tss_sel; |
| 2162 | |
| 2163 | ret = ops->write_std(new_tss_base, |
| 2164 | &tss_seg.prev_task_link, |
| 2165 | sizeof tss_seg.prev_task_link, |
| 2166 | ctxt->vcpu, &err); |
| 2167 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2168 | /* FIXME: need to provide precise fault address */ |
| 2169 | kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err); |
| 2170 | return ret; |
| 2171 | } |
| 2172 | } |
| 2173 | |
| 2174 | return load_state_from_tss16(ctxt, ops, &tss_seg); |
| 2175 | } |
| 2176 | |
| 2177 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
| 2178 | struct x86_emulate_ops *ops, |
| 2179 | struct tss_segment_32 *tss) |
| 2180 | { |
| 2181 | struct decode_cache *c = &ctxt->decode; |
| 2182 | |
| 2183 | tss->cr3 = ops->get_cr(3, ctxt->vcpu); |
| 2184 | tss->eip = c->eip; |
| 2185 | tss->eflags = ctxt->eflags; |
| 2186 | tss->eax = c->regs[VCPU_REGS_RAX]; |
| 2187 | tss->ecx = c->regs[VCPU_REGS_RCX]; |
| 2188 | tss->edx = c->regs[VCPU_REGS_RDX]; |
| 2189 | tss->ebx = c->regs[VCPU_REGS_RBX]; |
| 2190 | tss->esp = c->regs[VCPU_REGS_RSP]; |
| 2191 | tss->ebp = c->regs[VCPU_REGS_RBP]; |
| 2192 | tss->esi = c->regs[VCPU_REGS_RSI]; |
| 2193 | tss->edi = c->regs[VCPU_REGS_RDI]; |
| 2194 | |
| 2195 | tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu); |
| 2196 | tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 2197 | tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu); |
| 2198 | tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu); |
| 2199 | tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu); |
| 2200 | tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu); |
| 2201 | tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu); |
| 2202 | } |
| 2203 | |
| 2204 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
| 2205 | struct x86_emulate_ops *ops, |
| 2206 | struct tss_segment_32 *tss) |
| 2207 | { |
| 2208 | struct decode_cache *c = &ctxt->decode; |
| 2209 | int ret; |
| 2210 | |
| 2211 | ops->set_cr(3, tss->cr3, ctxt->vcpu); |
| 2212 | c->eip = tss->eip; |
| 2213 | ctxt->eflags = tss->eflags | 2; |
| 2214 | c->regs[VCPU_REGS_RAX] = tss->eax; |
| 2215 | c->regs[VCPU_REGS_RCX] = tss->ecx; |
| 2216 | c->regs[VCPU_REGS_RDX] = tss->edx; |
| 2217 | c->regs[VCPU_REGS_RBX] = tss->ebx; |
| 2218 | c->regs[VCPU_REGS_RSP] = tss->esp; |
| 2219 | c->regs[VCPU_REGS_RBP] = tss->ebp; |
| 2220 | c->regs[VCPU_REGS_RSI] = tss->esi; |
| 2221 | c->regs[VCPU_REGS_RDI] = tss->edi; |
| 2222 | |
| 2223 | /* |
| 2224 | * SDM says that segment selectors are loaded before segment |
| 2225 | * descriptors |
| 2226 | */ |
| 2227 | ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu); |
| 2228 | ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu); |
| 2229 | ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu); |
| 2230 | ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu); |
| 2231 | ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu); |
| 2232 | ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu); |
| 2233 | ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu); |
| 2234 | |
| 2235 | /* |
| 2236 | * Now load segment descriptors. If fault happenes at this stage |
| 2237 | * it is handled in a context of new task |
| 2238 | */ |
| 2239 | ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2240 | if (ret != X86EMUL_CONTINUE) |
| 2241 | return ret; |
| 2242 | ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES); |
| 2243 | if (ret != X86EMUL_CONTINUE) |
| 2244 | return ret; |
| 2245 | ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS); |
| 2246 | if (ret != X86EMUL_CONTINUE) |
| 2247 | return ret; |
| 2248 | ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS); |
| 2249 | if (ret != X86EMUL_CONTINUE) |
| 2250 | return ret; |
| 2251 | ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS); |
| 2252 | if (ret != X86EMUL_CONTINUE) |
| 2253 | return ret; |
| 2254 | ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS); |
| 2255 | if (ret != X86EMUL_CONTINUE) |
| 2256 | return ret; |
| 2257 | ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS); |
| 2258 | if (ret != X86EMUL_CONTINUE) |
| 2259 | return ret; |
| 2260 | |
| 2261 | return X86EMUL_CONTINUE; |
| 2262 | } |
| 2263 | |
| 2264 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
| 2265 | struct x86_emulate_ops *ops, |
| 2266 | u16 tss_selector, u16 old_tss_sel, |
| 2267 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2268 | { |
| 2269 | struct tss_segment_32 tss_seg; |
| 2270 | int ret; |
| 2271 | u32 err, new_tss_base = get_desc_base(new_desc); |
| 2272 | |
| 2273 | ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2274 | &err); |
| 2275 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2276 | /* FIXME: need to provide precise fault address */ |
| 2277 | kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err); |
| 2278 | return ret; |
| 2279 | } |
| 2280 | |
| 2281 | save_state_to_tss32(ctxt, ops, &tss_seg); |
| 2282 | |
| 2283 | ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2284 | &err); |
| 2285 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2286 | /* FIXME: need to provide precise fault address */ |
| 2287 | kvm_inject_page_fault(ctxt->vcpu, old_tss_base, err); |
| 2288 | return ret; |
| 2289 | } |
| 2290 | |
| 2291 | ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2292 | &err); |
| 2293 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2294 | /* FIXME: need to provide precise fault address */ |
| 2295 | kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err); |
| 2296 | return ret; |
| 2297 | } |
| 2298 | |
| 2299 | if (old_tss_sel != 0xffff) { |
| 2300 | tss_seg.prev_task_link = old_tss_sel; |
| 2301 | |
| 2302 | ret = ops->write_std(new_tss_base, |
| 2303 | &tss_seg.prev_task_link, |
| 2304 | sizeof tss_seg.prev_task_link, |
| 2305 | ctxt->vcpu, &err); |
| 2306 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2307 | /* FIXME: need to provide precise fault address */ |
| 2308 | kvm_inject_page_fault(ctxt->vcpu, new_tss_base, err); |
| 2309 | return ret; |
| 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | return load_state_from_tss32(ctxt, ops, &tss_seg); |
| 2314 | } |
| 2315 | |
| 2316 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
| 2317 | struct x86_emulate_ops *ops, |
| 2318 | u16 tss_selector, int reason) |
| 2319 | { |
| 2320 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2321 | int ret; |
| 2322 | u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu); |
| 2323 | ulong old_tss_base = |
| 2324 | get_cached_descriptor_base(ctxt, ops, VCPU_SREG_TR); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2325 | u32 desc_limit; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2326 | |
| 2327 | /* FIXME: old_tss_base == ~0 ? */ |
| 2328 | |
| 2329 | ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc); |
| 2330 | if (ret != X86EMUL_CONTINUE) |
| 2331 | return ret; |
| 2332 | ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc); |
| 2333 | if (ret != X86EMUL_CONTINUE) |
| 2334 | return ret; |
| 2335 | |
| 2336 | /* FIXME: check that next_tss_desc is tss */ |
| 2337 | |
| 2338 | if (reason != TASK_SWITCH_IRET) { |
| 2339 | if ((tss_selector & 3) > next_tss_desc.dpl || |
| 2340 | ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) { |
| 2341 | kvm_inject_gp(ctxt->vcpu, 0); |
| 2342 | return X86EMUL_PROPAGATE_FAULT; |
| 2343 | } |
| 2344 | } |
| 2345 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2346 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2347 | if (!next_tss_desc.p || |
| 2348 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2349 | desc_limit < 0x2b)) { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2350 | kvm_queue_exception_e(ctxt->vcpu, TS_VECTOR, |
| 2351 | tss_selector & 0xfffc); |
| 2352 | return X86EMUL_PROPAGATE_FAULT; |
| 2353 | } |
| 2354 | |
| 2355 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2356 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
| 2357 | write_segment_descriptor(ctxt, ops, old_tss_sel, |
| 2358 | &curr_tss_desc); |
| 2359 | } |
| 2360 | |
| 2361 | if (reason == TASK_SWITCH_IRET) |
| 2362 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2363 | |
| 2364 | /* set back link to prev task only if NT bit is set in eflags |
| 2365 | note that old_tss_sel is not used afetr this point */ |
| 2366 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2367 | old_tss_sel = 0xffff; |
| 2368 | |
| 2369 | if (next_tss_desc.type & 8) |
| 2370 | ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel, |
| 2371 | old_tss_base, &next_tss_desc); |
| 2372 | else |
| 2373 | ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel, |
| 2374 | old_tss_base, &next_tss_desc); |
| 2375 | |
| 2376 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2377 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2378 | |
| 2379 | if (reason != TASK_SWITCH_IRET) { |
| 2380 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
| 2381 | write_segment_descriptor(ctxt, ops, tss_selector, |
| 2382 | &next_tss_desc); |
| 2383 | } |
| 2384 | |
| 2385 | ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu); |
| 2386 | ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu); |
| 2387 | ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu); |
| 2388 | |
| 2389 | return ret; |
| 2390 | } |
| 2391 | |
| 2392 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
| 2393 | struct x86_emulate_ops *ops, |
| 2394 | u16 tss_selector, int reason) |
| 2395 | { |
| 2396 | struct decode_cache *c = &ctxt->decode; |
| 2397 | int rc; |
| 2398 | |
| 2399 | memset(c, 0, sizeof(struct decode_cache)); |
| 2400 | c->eip = ctxt->eip; |
| 2401 | memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs); |
| 2402 | |
| 2403 | rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason); |
| 2404 | |
| 2405 | if (rc == X86EMUL_CONTINUE) { |
| 2406 | memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs); |
| 2407 | kvm_rip_write(ctxt->vcpu, c->eip); |
| 2408 | } |
| 2409 | |
| 2410 | return rc; |
| 2411 | } |
| 2412 | |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame^] | 2413 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base, |
| 2414 | int reg, unsigned long **ptr) |
| 2415 | { |
| 2416 | struct decode_cache *c = &ctxt->decode; |
| 2417 | int df = (ctxt->eflags & EFLG_DF) ? -1 : 1; |
| 2418 | |
| 2419 | register_address_increment(c, &c->regs[reg], df * c->src.bytes); |
| 2420 | *ptr = (unsigned long *)register_address(c, base, c->regs[reg]); |
| 2421 | } |
| 2422 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2423 | int |
Laurent Vivier | 1be3aa4 | 2007-09-18 11:27:27 +0200 | [diff] [blame] | 2424 | x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2425 | { |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2426 | u64 msr_data; |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 2427 | unsigned long saved_eip = 0; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2428 | struct decode_cache *c = &ctxt->decode; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2429 | unsigned int port; |
| 2430 | int io_dir_in; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2431 | int rc = X86EMUL_CONTINUE; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2432 | |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 2433 | ctxt->interruptibility = 0; |
| 2434 | |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 2435 | /* Shadow copy of register state. Committed on successful emulation. |
| 2436 | * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't |
| 2437 | * modify them. |
| 2438 | */ |
| 2439 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2440 | memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs); |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 2441 | saved_eip = c->eip; |
| 2442 | |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 2443 | if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) { |
| 2444 | kvm_queue_exception(ctxt->vcpu, UD_VECTOR); |
| 2445 | goto done; |
| 2446 | } |
| 2447 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 2448 | /* LOCK prefix is allowed only with some instructions */ |
Gleb Natapov | a41ffb75 | 2010-03-18 15:20:14 +0200 | [diff] [blame] | 2449 | if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) { |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 2450 | kvm_queue_exception(ctxt->vcpu, UD_VECTOR); |
| 2451 | goto done; |
| 2452 | } |
| 2453 | |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 2454 | /* Privileged instruction can be executed only in CPL=0 */ |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 2455 | if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) { |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 2456 | kvm_inject_gp(ctxt->vcpu, 0); |
| 2457 | goto done; |
| 2458 | } |
| 2459 | |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2460 | if (c->rep_prefix && (c->d & String)) { |
| 2461 | /* All REP prefixes have the same first termination condition */ |
Gleb Natapov | c73e197 | 2010-03-15 16:38:29 +0200 | [diff] [blame] | 2462 | if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2463 | kvm_rip_write(ctxt->vcpu, c->eip); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2464 | goto done; |
| 2465 | } |
| 2466 | /* The second termination condition only applies for REPE |
| 2467 | * and REPNE. Test if the repeat string operation prefix is |
| 2468 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 2469 | * corresponding termination condition according to: |
| 2470 | * - if REPE/REPZ and ZF = 0 then done |
| 2471 | * - if REPNE/REPNZ and ZF = 1 then done |
| 2472 | */ |
| 2473 | if ((c->b == 0xa6) || (c->b == 0xa7) || |
| 2474 | (c->b == 0xae) || (c->b == 0xaf)) { |
| 2475 | if ((c->rep_prefix == REPE_PREFIX) && |
| 2476 | ((ctxt->eflags & EFLG_ZF) == 0)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2477 | kvm_rip_write(ctxt->vcpu, c->eip); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2478 | goto done; |
| 2479 | } |
| 2480 | if ((c->rep_prefix == REPNE_PREFIX) && |
| 2481 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2482 | kvm_rip_write(ctxt->vcpu, c->eip); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2483 | goto done; |
| 2484 | } |
| 2485 | } |
Gleb Natapov | c73e197 | 2010-03-15 16:38:29 +0200 | [diff] [blame] | 2486 | register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1); |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 2487 | c->eip = ctxt->eip; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2488 | } |
| 2489 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2490 | if (c->src.type == OP_MEM) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2491 | rc = ops->read_emulated((unsigned long)c->src.ptr, |
| 2492 | &c->src.val, |
| 2493 | c->src.bytes, |
| 2494 | ctxt->vcpu); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 2495 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2496 | goto done; |
| 2497 | c->src.orig_val = c->src.val; |
| 2498 | } |
| 2499 | |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 2500 | if (c->src2.type == OP_MEM) { |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 2501 | rc = ops->read_emulated((unsigned long)c->src2.ptr, |
| 2502 | &c->src2.val, |
| 2503 | c->src2.bytes, |
| 2504 | ctxt->vcpu); |
| 2505 | if (rc != X86EMUL_CONTINUE) |
| 2506 | goto done; |
| 2507 | } |
| 2508 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2509 | if ((c->d & DstMask) == ImplicitOps) |
| 2510 | goto special_insn; |
| 2511 | |
| 2512 | |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 2513 | if ((c->dst.type == OP_MEM) && !(c->d & Mov)) { |
| 2514 | /* optimisation - avoid slow emulated read if Mov */ |
| 2515 | rc = ops->read_emulated((unsigned long)c->dst.ptr, &c->dst.val, |
| 2516 | c->dst.bytes, ctxt->vcpu); |
| 2517 | if (rc != X86EMUL_CONTINUE) |
| 2518 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 2519 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2520 | c->dst.orig_val = c->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 2521 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2522 | special_insn: |
| 2523 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2524 | if (c->twobyte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2525 | goto twobyte_insn; |
| 2526 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2527 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2528 | case 0x00 ... 0x05: |
| 2529 | add: /* add */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2530 | emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2531 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2532 | case 0x06: /* push es */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2533 | emulate_push_sreg(ctxt, VCPU_SREG_ES); |
| 2534 | break; |
| 2535 | case 0x07: /* pop es */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2536 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2537 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2538 | goto done; |
| 2539 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2540 | case 0x08 ... 0x0d: |
| 2541 | or: /* or */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2542 | emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2543 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2544 | case 0x0e: /* push cs */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2545 | emulate_push_sreg(ctxt, VCPU_SREG_CS); |
| 2546 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2547 | case 0x10 ... 0x15: |
| 2548 | adc: /* adc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2549 | emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2550 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2551 | case 0x16: /* push ss */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2552 | emulate_push_sreg(ctxt, VCPU_SREG_SS); |
| 2553 | break; |
| 2554 | case 0x17: /* pop ss */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2555 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2556 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2557 | goto done; |
| 2558 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2559 | case 0x18 ... 0x1d: |
| 2560 | sbb: /* sbb */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2561 | emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2562 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2563 | case 0x1e: /* push ds */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2564 | emulate_push_sreg(ctxt, VCPU_SREG_DS); |
| 2565 | break; |
| 2566 | case 0x1f: /* pop ds */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2567 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2568 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2569 | goto done; |
| 2570 | break; |
Guillaume Thouvenin | aa3a816 | 2008-09-12 13:52:18 +0200 | [diff] [blame] | 2571 | case 0x20 ... 0x25: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2572 | and: /* and */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2573 | emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2574 | break; |
| 2575 | case 0x28 ... 0x2d: |
| 2576 | sub: /* sub */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2577 | emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2578 | break; |
| 2579 | case 0x30 ... 0x35: |
| 2580 | xor: /* xor */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2581 | emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2582 | break; |
| 2583 | case 0x38 ... 0x3d: |
| 2584 | cmp: /* cmp */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2585 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2586 | break; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 2587 | case 0x40 ... 0x47: /* inc r16/r32 */ |
| 2588 | emulate_1op("inc", c->dst, ctxt->eflags); |
| 2589 | break; |
| 2590 | case 0x48 ... 0x4f: /* dec r16/r32 */ |
| 2591 | emulate_1op("dec", c->dst, ctxt->eflags); |
| 2592 | break; |
| 2593 | case 0x50 ... 0x57: /* push reg */ |
Guillaume Thouvenin | 2786b01 | 2008-09-22 16:08:06 +0200 | [diff] [blame] | 2594 | emulate_push(ctxt); |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 2595 | break; |
| 2596 | case 0x58 ... 0x5f: /* pop reg */ |
| 2597 | pop_instruction: |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 2598 | rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2599 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 2600 | goto done; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 2601 | break; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2602 | case 0x60: /* pusha */ |
| 2603 | emulate_pusha(ctxt); |
| 2604 | break; |
| 2605 | case 0x61: /* popa */ |
| 2606 | rc = emulate_popa(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2607 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2608 | goto done; |
| 2609 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2610 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2611 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2612 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2613 | c->dst.val = (s32) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2614 | break; |
Avi Kivity | 91ed7a0 | 2008-05-29 14:38:38 +0300 | [diff] [blame] | 2615 | case 0x68: /* push imm */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2616 | case 0x6a: /* push imm8 */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2617 | emulate_push(ctxt); |
| 2618 | break; |
| 2619 | case 0x6c: /* insb */ |
| 2620 | case 0x6d: /* insw/insd */ |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2621 | if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX], |
| 2622 | (c->d & ByteOp) ? 1 : c->op_bytes)) { |
| 2623 | kvm_inject_gp(ctxt->vcpu, 0); |
| 2624 | goto done; |
| 2625 | } |
| 2626 | if (kvm_emulate_pio_string(ctxt->vcpu, |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2627 | 1, |
| 2628 | (c->d & ByteOp) ? 1 : c->op_bytes, |
| 2629 | c->rep_prefix ? |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 2630 | address_mask(c, c->regs[VCPU_REGS_RCX]) : 1, |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2631 | (ctxt->eflags & EFLG_DF), |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 2632 | register_address(c, es_base(ctxt), |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2633 | c->regs[VCPU_REGS_RDI]), |
| 2634 | c->rep_prefix, |
| 2635 | c->regs[VCPU_REGS_RDX]) == 0) { |
| 2636 | c->eip = saved_eip; |
| 2637 | return -1; |
| 2638 | } |
| 2639 | return 0; |
| 2640 | case 0x6e: /* outsb */ |
| 2641 | case 0x6f: /* outsw/outsd */ |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2642 | if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX], |
| 2643 | (c->d & ByteOp) ? 1 : c->op_bytes)) { |
| 2644 | kvm_inject_gp(ctxt->vcpu, 0); |
| 2645 | goto done; |
| 2646 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2647 | if (kvm_emulate_pio_string(ctxt->vcpu, |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2648 | 0, |
| 2649 | (c->d & ByteOp) ? 1 : c->op_bytes, |
| 2650 | c->rep_prefix ? |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 2651 | address_mask(c, c->regs[VCPU_REGS_RCX]) : 1, |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2652 | (ctxt->eflags & EFLG_DF), |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 2653 | register_address(c, |
| 2654 | seg_override_base(ctxt, c), |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2655 | c->regs[VCPU_REGS_RSI]), |
| 2656 | c->rep_prefix, |
| 2657 | c->regs[VCPU_REGS_RDX]) == 0) { |
| 2658 | c->eip = saved_eip; |
| 2659 | return -1; |
| 2660 | } |
| 2661 | return 0; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 2662 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2663 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 2664 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2665 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2666 | case 0x80 ... 0x83: /* Grp1 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2667 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2668 | case 0: |
| 2669 | goto add; |
| 2670 | case 1: |
| 2671 | goto or; |
| 2672 | case 2: |
| 2673 | goto adc; |
| 2674 | case 3: |
| 2675 | goto sbb; |
| 2676 | case 4: |
| 2677 | goto and; |
| 2678 | case 5: |
| 2679 | goto sub; |
| 2680 | case 6: |
| 2681 | goto xor; |
| 2682 | case 7: |
| 2683 | goto cmp; |
| 2684 | } |
| 2685 | break; |
| 2686 | case 0x84 ... 0x85: |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2687 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2688 | break; |
| 2689 | case 0x86 ... 0x87: /* xchg */ |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 2690 | xchg: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2691 | /* Write back the register source. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2692 | switch (c->dst.bytes) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2693 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2694 | *(u8 *) c->src.ptr = (u8) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2695 | break; |
| 2696 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2697 | *(u16 *) c->src.ptr = (u16) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2698 | break; |
| 2699 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2700 | *c->src.ptr = (u32) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2701 | break; /* 64b reg: zero-extend */ |
| 2702 | case 8: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2703 | *c->src.ptr = c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2704 | break; |
| 2705 | } |
| 2706 | /* |
| 2707 | * Write back the memory destination with implicit LOCK |
| 2708 | * prefix. |
| 2709 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2710 | c->dst.val = c->src.val; |
| 2711 | c->lock_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2712 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2713 | case 0x88 ... 0x8b: /* mov */ |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2714 | goto mov; |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 2715 | case 0x8c: { /* mov r/m, sreg */ |
| 2716 | struct kvm_segment segreg; |
| 2717 | |
Gleb Natapov | 5e3ae6c | 2010-03-18 15:20:07 +0200 | [diff] [blame] | 2718 | if (c->modrm_reg <= VCPU_SREG_GS) |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 2719 | kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg); |
| 2720 | else { |
Gleb Natapov | 5e3ae6c | 2010-03-18 15:20:07 +0200 | [diff] [blame] | 2721 | kvm_queue_exception(ctxt->vcpu, UD_VECTOR); |
| 2722 | goto done; |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 2723 | } |
| 2724 | c->dst.val = segreg.selector; |
| 2725 | break; |
| 2726 | } |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 2727 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | f9b7aab | 2008-04-14 23:46:37 +0300 | [diff] [blame] | 2728 | c->dst.val = c->modrm_ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 2729 | break; |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 2730 | case 0x8e: { /* mov seg, r/m16 */ |
| 2731 | uint16_t sel; |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 2732 | |
| 2733 | sel = c->src.val; |
Gleb Natapov | 8b9f441 | 2010-02-18 12:14:59 +0200 | [diff] [blame] | 2734 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 2735 | if (c->modrm_reg == VCPU_SREG_CS || |
| 2736 | c->modrm_reg > VCPU_SREG_GS) { |
Gleb Natapov | 8b9f441 | 2010-02-18 12:14:59 +0200 | [diff] [blame] | 2737 | kvm_queue_exception(ctxt->vcpu, UD_VECTOR); |
| 2738 | goto done; |
| 2739 | } |
| 2740 | |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 2741 | if (c->modrm_reg == VCPU_SREG_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2742 | toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_MOV_SS); |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 2743 | |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 2744 | rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg); |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 2745 | |
| 2746 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2747 | break; |
| 2748 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2749 | case 0x8f: /* pop (sole member of Grp1a) */ |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2750 | rc = emulate_grp1a(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2751 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2752 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2753 | break; |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 2754 | case 0x90: /* nop / xchg r8,rax */ |
| 2755 | if (!(c->rex_prefix & 1)) { /* nop */ |
| 2756 | c->dst.type = OP_NONE; |
| 2757 | break; |
| 2758 | } |
| 2759 | case 0x91 ... 0x97: /* xchg reg,rax */ |
| 2760 | c->src.type = c->dst.type = OP_REG; |
| 2761 | c->src.bytes = c->dst.bytes = c->op_bytes; |
| 2762 | c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX]; |
| 2763 | c->src.val = *(c->src.ptr); |
| 2764 | goto xchg; |
Nitin A Kamble | fd2a760 | 2007-08-28 18:22:47 -0700 | [diff] [blame] | 2765 | case 0x9c: /* pushf */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2766 | c->src.val = (unsigned long) ctxt->eflags; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2767 | emulate_push(ctxt); |
| 2768 | break; |
Nitin A Kamble | 535eabc | 2007-09-15 10:45:05 +0300 | [diff] [blame] | 2769 | case 0x9d: /* popf */ |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 2770 | c->dst.type = OP_REG; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2771 | c->dst.ptr = (unsigned long *) &ctxt->eflags; |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 2772 | c->dst.bytes = c->op_bytes; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 2773 | rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes); |
| 2774 | if (rc != X86EMUL_CONTINUE) |
| 2775 | goto done; |
| 2776 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2777 | case 0xa0 ... 0xa1: /* mov */ |
| 2778 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
| 2779 | c->dst.val = c->src.val; |
| 2780 | break; |
| 2781 | case 0xa2 ... 0xa3: /* mov */ |
| 2782 | c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX]; |
| 2783 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2784 | case 0xa4 ... 0xa5: /* movs */ |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame^] | 2785 | goto mov; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2786 | case 0xa6 ... 0xa7: /* cmps */ |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2787 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2788 | DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame^] | 2789 | goto cmp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2790 | case 0xaa ... 0xab: /* stos */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2791 | c->dst.val = c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2792 | break; |
| 2793 | case 0xac ... 0xad: /* lods */ |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame^] | 2794 | goto mov; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2795 | case 0xae ... 0xaf: /* scas */ |
| 2796 | DPRINTF("Urk! I don't handle SCAS.\n"); |
| 2797 | goto cannot_emulate; |
Mohammed Gamal | a5e2e82 | 2008-08-27 05:02:56 +0300 | [diff] [blame] | 2798 | case 0xb0 ... 0xbf: /* mov r, imm */ |
Guillaume Thouvenin | 615ac12 | 2008-05-27 10:19:16 +0200 | [diff] [blame] | 2799 | goto mov; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2800 | case 0xc0 ... 0xc1: |
| 2801 | emulate_grp2(ctxt); |
| 2802 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2803 | case 0xc3: /* ret */ |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 2804 | c->dst.type = OP_REG; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2805 | c->dst.ptr = &c->eip; |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 2806 | c->dst.bytes = c->op_bytes; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2807 | goto pop_instruction; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2808 | case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */ |
| 2809 | mov: |
| 2810 | c->dst.val = c->src.val; |
| 2811 | break; |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2812 | case 0xcb: /* ret far */ |
| 2813 | rc = emulate_ret_far(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2814 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2815 | goto done; |
| 2816 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2817 | case 0xd0 ... 0xd1: /* Grp2 */ |
| 2818 | c->src.val = 1; |
| 2819 | emulate_grp2(ctxt); |
| 2820 | break; |
| 2821 | case 0xd2 ... 0xd3: /* Grp2 */ |
| 2822 | c->src.val = c->regs[VCPU_REGS_RCX]; |
| 2823 | emulate_grp2(ctxt); |
| 2824 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2825 | case 0xe4: /* inb */ |
| 2826 | case 0xe5: /* in */ |
Gleb Natapov | 84ce66a | 2009-04-12 13:36:46 +0300 | [diff] [blame] | 2827 | port = c->src.val; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2828 | io_dir_in = 1; |
| 2829 | goto do_io; |
| 2830 | case 0xe6: /* outb */ |
| 2831 | case 0xe7: /* out */ |
Gleb Natapov | 84ce66a | 2009-04-12 13:36:46 +0300 | [diff] [blame] | 2832 | port = c->src.val; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2833 | io_dir_in = 0; |
| 2834 | goto do_io; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 2835 | case 0xe8: /* call (near) */ { |
Gleb Natapov | d53c477 | 2009-04-12 13:36:36 +0300 | [diff] [blame] | 2836 | long int rel = c->src.val; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2837 | c->src.val = (unsigned long) c->eip; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2838 | jmp_rel(c, rel); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2839 | emulate_push(ctxt); |
| 2840 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 2841 | } |
| 2842 | case 0xe9: /* jmp rel */ |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 2843 | goto jmp; |
Gleb Natapov | 782b877 | 2009-04-12 13:36:25 +0300 | [diff] [blame] | 2844 | case 0xea: /* jmp far */ |
Gleb Natapov | ea79849 | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 2845 | jump_far: |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 2846 | if (load_segment_descriptor(ctxt, ops, c->src2.val, |
| 2847 | VCPU_SREG_CS)) |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 2848 | goto done; |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 2849 | |
Gleb Natapov | 782b877 | 2009-04-12 13:36:25 +0300 | [diff] [blame] | 2850 | c->eip = c->src.val; |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 2851 | break; |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 2852 | case 0xeb: |
| 2853 | jmp: /* jmp rel short */ |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2854 | jmp_rel(c, c->src.val); |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 2855 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 2856 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2857 | case 0xec: /* in al,dx */ |
| 2858 | case 0xed: /* in (e/r)ax,dx */ |
| 2859 | port = c->regs[VCPU_REGS_RDX]; |
| 2860 | io_dir_in = 1; |
| 2861 | goto do_io; |
| 2862 | case 0xee: /* out al,dx */ |
| 2863 | case 0xef: /* out (e/r)ax,dx */ |
| 2864 | port = c->regs[VCPU_REGS_RDX]; |
| 2865 | io_dir_in = 0; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2866 | do_io: |
| 2867 | if (!emulator_io_permited(ctxt, ops, port, |
| 2868 | (c->d & ByteOp) ? 1 : c->op_bytes)) { |
| 2869 | kvm_inject_gp(ctxt->vcpu, 0); |
| 2870 | goto done; |
| 2871 | } |
| 2872 | if (kvm_emulate_pio(ctxt->vcpu, io_dir_in, |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2873 | (c->d & ByteOp) ? 1 : c->op_bytes, |
| 2874 | port) != 0) { |
| 2875 | c->eip = saved_eip; |
| 2876 | goto cannot_emulate; |
| 2877 | } |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 2878 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2879 | case 0xf4: /* hlt */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2880 | ctxt->vcpu->arch.halt_request = 1; |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 2881 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2882 | case 0xf5: /* cmc */ |
| 2883 | /* complement carry flag from eflags reg */ |
| 2884 | ctxt->eflags ^= EFLG_CF; |
| 2885 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2886 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2887 | case 0xf6 ... 0xf7: /* Grp3 */ |
Gleb Natapov | aca06a8 | 2010-03-18 15:20:15 +0200 | [diff] [blame] | 2888 | if (!emulate_grp3(ctxt, ops)) |
| 2889 | goto cannot_emulate; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2890 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2891 | case 0xf8: /* clc */ |
| 2892 | ctxt->eflags &= ~EFLG_CF; |
| 2893 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2894 | break; |
| 2895 | case 0xfa: /* cli */ |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 2896 | if (emulator_bad_iopl(ctxt, ops)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2897 | kvm_inject_gp(ctxt->vcpu, 0); |
| 2898 | else { |
| 2899 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 2900 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2901 | } |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2902 | break; |
| 2903 | case 0xfb: /* sti */ |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 2904 | if (emulator_bad_iopl(ctxt, ops)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2905 | kvm_inject_gp(ctxt->vcpu, 0); |
| 2906 | else { |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2907 | toggle_interruptibility(ctxt, KVM_X86_SHADOW_INT_STI); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2908 | ctxt->eflags |= X86_EFLAGS_IF; |
| 2909 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2910 | } |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2911 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 2912 | case 0xfc: /* cld */ |
| 2913 | ctxt->eflags &= ~EFLG_DF; |
| 2914 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2915 | break; |
| 2916 | case 0xfd: /* std */ |
| 2917 | ctxt->eflags |= EFLG_DF; |
| 2918 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2919 | break; |
Gleb Natapov | ea79849 | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 2920 | case 0xfe: /* Grp4 */ |
| 2921 | grp45: |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2922 | rc = emulate_grp45(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2923 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2924 | goto done; |
| 2925 | break; |
Gleb Natapov | ea79849 | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 2926 | case 0xff: /* Grp5 */ |
| 2927 | if (c->modrm_reg == 5) |
| 2928 | goto jump_far; |
| 2929 | goto grp45; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2930 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2931 | |
| 2932 | writeback: |
| 2933 | rc = writeback(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2934 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2935 | goto done; |
| 2936 | |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame^] | 2937 | if ((c->d & SrcMask) == SrcSI) |
| 2938 | string_addr_inc(ctxt, seg_override_base(ctxt, c), VCPU_REGS_RSI, |
| 2939 | &c->src.ptr); |
| 2940 | |
| 2941 | if ((c->d & DstMask) == DstDI) |
| 2942 | string_addr_inc(ctxt, es_base(ctxt), VCPU_REGS_RDI, |
| 2943 | &c->dst.ptr); |
| 2944 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2945 | /* Commit shadow register state. */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2946 | memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2947 | kvm_rip_write(ctxt->vcpu, c->eip); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2948 | |
| 2949 | done: |
| 2950 | if (rc == X86EMUL_UNHANDLEABLE) { |
| 2951 | c->eip = saved_eip; |
| 2952 | return -1; |
| 2953 | } |
| 2954 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2955 | |
| 2956 | twobyte_insn: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2957 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2958 | case 0x01: /* lgdt, lidt, lmsw */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2959 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2960 | u16 size; |
| 2961 | unsigned long address; |
| 2962 | |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2963 | case 0: /* vmcall */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2964 | if (c->modrm_mod != 3 || c->modrm_rm != 1) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2965 | goto cannot_emulate; |
| 2966 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2967 | rc = kvm_fix_hypercall(ctxt->vcpu); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2968 | if (rc != X86EMUL_CONTINUE) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2969 | goto done; |
| 2970 | |
Avi Kivity | 33e3885 | 2008-05-21 15:34:25 +0300 | [diff] [blame] | 2971 | /* Let the processor re-execute the fixed hypercall */ |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 2972 | c->eip = ctxt->eip; |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2973 | /* Disable writeback. */ |
| 2974 | c->dst.type = OP_NONE; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2975 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2976 | case 2: /* lgdt */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2977 | rc = read_descriptor(ctxt, ops, c->src.ptr, |
| 2978 | &size, &address, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2979 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2980 | goto done; |
| 2981 | realmode_lgdt(ctxt->vcpu, size, address); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2982 | /* Disable writeback. */ |
| 2983 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2984 | break; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2985 | case 3: /* lidt/vmmcall */ |
Avi Kivity | 2b3d2a2 | 2008-12-23 19:46:01 +0200 | [diff] [blame] | 2986 | if (c->modrm_mod == 3) { |
| 2987 | switch (c->modrm_rm) { |
| 2988 | case 1: |
| 2989 | rc = kvm_fix_hypercall(ctxt->vcpu); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2990 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 2b3d2a2 | 2008-12-23 19:46:01 +0200 | [diff] [blame] | 2991 | goto done; |
| 2992 | break; |
| 2993 | default: |
| 2994 | goto cannot_emulate; |
| 2995 | } |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2996 | } else { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2997 | rc = read_descriptor(ctxt, ops, c->src.ptr, |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2998 | &size, &address, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2999 | c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3000 | if (rc != X86EMUL_CONTINUE) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3001 | goto done; |
| 3002 | realmode_lidt(ctxt->vcpu, size, address); |
| 3003 | } |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3004 | /* Disable writeback. */ |
| 3005 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3006 | break; |
| 3007 | case 4: /* smsw */ |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3008 | c->dst.bytes = 2; |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3009 | c->dst.val = ops->get_cr(0, ctxt->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3010 | break; |
| 3011 | case 6: /* lmsw */ |
Gleb Natapov | 93a152b | 2010-03-18 15:20:04 +0200 | [diff] [blame] | 3012 | ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) | |
| 3013 | (c->src.val & 0x0f), ctxt->vcpu); |
Avi Kivity | dc7457e | 2008-04-30 16:13:36 +0300 | [diff] [blame] | 3014 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3015 | break; |
Gleb Natapov | 6e1e5ff | 2010-03-18 15:20:08 +0200 | [diff] [blame] | 3016 | case 5: /* not defined */ |
| 3017 | kvm_queue_exception(ctxt->vcpu, UD_VECTOR); |
| 3018 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3019 | case 7: /* invlpg*/ |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 3020 | emulate_invlpg(ctxt->vcpu, c->modrm_ea); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3021 | /* Disable writeback. */ |
| 3022 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3023 | break; |
| 3024 | default: |
| 3025 | goto cannot_emulate; |
| 3026 | } |
| 3027 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3028 | case 0x05: /* syscall */ |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 3029 | rc = emulate_syscall(ctxt); |
| 3030 | if (rc != X86EMUL_CONTINUE) |
| 3031 | goto done; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 3032 | else |
| 3033 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3034 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3035 | case 0x06: |
| 3036 | emulate_clts(ctxt->vcpu); |
| 3037 | c->dst.type = OP_NONE; |
| 3038 | break; |
| 3039 | case 0x08: /* invd */ |
| 3040 | case 0x09: /* wbinvd */ |
| 3041 | case 0x0d: /* GrpP (prefetch) */ |
| 3042 | case 0x18: /* Grp16 (prefetch/nop) */ |
| 3043 | c->dst.type = OP_NONE; |
| 3044 | break; |
| 3045 | case 0x20: /* mov cr, reg */ |
Gleb Natapov | 6aebfa6 | 2010-03-18 15:20:10 +0200 | [diff] [blame] | 3046 | switch (c->modrm_reg) { |
| 3047 | case 1: |
| 3048 | case 5 ... 7: |
| 3049 | case 9 ... 15: |
| 3050 | kvm_queue_exception(ctxt->vcpu, UD_VECTOR); |
| 3051 | goto done; |
| 3052 | } |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3053 | c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3054 | c->dst.type = OP_NONE; /* no writeback */ |
| 3055 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3056 | case 0x21: /* mov from dr to reg */ |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3057 | if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) && |
| 3058 | (c->modrm_reg == 4 || c->modrm_reg == 5)) { |
| 3059 | kvm_queue_exception(ctxt->vcpu, UD_VECTOR); |
| 3060 | goto done; |
| 3061 | } |
| 3062 | emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]); |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3063 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3064 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3065 | case 0x22: /* mov reg, cr */ |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3066 | ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3067 | c->dst.type = OP_NONE; |
| 3068 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3069 | case 0x23: /* mov from reg to dr */ |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3070 | if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) && |
| 3071 | (c->modrm_reg == 4 || c->modrm_reg == 5)) { |
| 3072 | kvm_queue_exception(ctxt->vcpu, UD_VECTOR); |
| 3073 | goto done; |
| 3074 | } |
| 3075 | emulator_set_dr(ctxt, c->modrm_reg, c->regs[c->modrm_rm]); |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3076 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3077 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3078 | case 0x30: |
| 3079 | /* wrmsr */ |
| 3080 | msr_data = (u32)c->regs[VCPU_REGS_RAX] |
| 3081 | | ((u64)c->regs[VCPU_REGS_RDX] << 32); |
Takuya Yoshikawa | 0e4176a | 2010-02-12 16:00:55 +0900 | [diff] [blame] | 3082 | if (kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 3083 | kvm_inject_gp(ctxt->vcpu, 0); |
Gleb Natapov | fd52536 | 2010-03-18 15:20:13 +0200 | [diff] [blame] | 3084 | goto done; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3085 | } |
| 3086 | rc = X86EMUL_CONTINUE; |
| 3087 | c->dst.type = OP_NONE; |
| 3088 | break; |
| 3089 | case 0x32: |
| 3090 | /* rdmsr */ |
Takuya Yoshikawa | 0e4176a | 2010-02-12 16:00:55 +0900 | [diff] [blame] | 3091 | if (kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 3092 | kvm_inject_gp(ctxt->vcpu, 0); |
Gleb Natapov | fd52536 | 2010-03-18 15:20:13 +0200 | [diff] [blame] | 3093 | goto done; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3094 | } else { |
| 3095 | c->regs[VCPU_REGS_RAX] = (u32)msr_data; |
| 3096 | c->regs[VCPU_REGS_RDX] = msr_data >> 32; |
| 3097 | } |
| 3098 | rc = X86EMUL_CONTINUE; |
| 3099 | c->dst.type = OP_NONE; |
| 3100 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3101 | case 0x34: /* sysenter */ |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 3102 | rc = emulate_sysenter(ctxt); |
| 3103 | if (rc != X86EMUL_CONTINUE) |
| 3104 | goto done; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 3105 | else |
| 3106 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3107 | break; |
| 3108 | case 0x35: /* sysexit */ |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 3109 | rc = emulate_sysexit(ctxt); |
| 3110 | if (rc != X86EMUL_CONTINUE) |
| 3111 | goto done; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 3112 | else |
| 3113 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3114 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3115 | case 0x40 ... 0x4f: /* cmov */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3116 | c->dst.val = c->dst.orig_val = c->src.val; |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3117 | if (!test_cc(c->b, ctxt->eflags)) |
| 3118 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3119 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3120 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3121 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3122 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3123 | c->dst.type = OP_NONE; |
| 3124 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3125 | case 0xa0: /* push fs */ |
| 3126 | emulate_push_sreg(ctxt, VCPU_SREG_FS); |
| 3127 | break; |
| 3128 | case 0xa1: /* pop fs */ |
| 3129 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3130 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3131 | goto done; |
| 3132 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3133 | case 0xa3: |
| 3134 | bt: /* bt */ |
Qing He | e4f8e03 | 2007-09-24 17:22:13 +0800 | [diff] [blame] | 3135 | c->dst.type = OP_NONE; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3136 | /* only subword offset */ |
| 3137 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3138 | emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3139 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 3140 | case 0xa4: /* shld imm8, r, r/m */ |
| 3141 | case 0xa5: /* shld cl, r, r/m */ |
| 3142 | emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags); |
| 3143 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3144 | case 0xa8: /* push gs */ |
| 3145 | emulate_push_sreg(ctxt, VCPU_SREG_GS); |
| 3146 | break; |
| 3147 | case 0xa9: /* pop gs */ |
| 3148 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3149 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3150 | goto done; |
| 3151 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3152 | case 0xab: |
| 3153 | bts: /* bts */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3154 | /* only subword offset */ |
| 3155 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3156 | emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3157 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 3158 | case 0xac: /* shrd imm8, r, r/m */ |
| 3159 | case 0xad: /* shrd cl, r, r/m */ |
| 3160 | emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags); |
| 3161 | break; |
Glauber Costa | 2a7c5b8 | 2008-07-10 17:08:15 -0300 | [diff] [blame] | 3162 | case 0xae: /* clflush */ |
| 3163 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3164 | case 0xb0 ... 0xb1: /* cmpxchg */ |
| 3165 | /* |
| 3166 | * Save real source value, then compare EAX against |
| 3167 | * destination. |
| 3168 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3169 | c->src.orig_val = c->src.val; |
| 3170 | c->src.val = c->regs[VCPU_REGS_RAX]; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3171 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
| 3172 | if (ctxt->eflags & EFLG_ZF) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3173 | /* Success: write back to memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3174 | c->dst.val = c->src.orig_val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3175 | } else { |
| 3176 | /* Failure: write the value we saw to EAX. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3177 | c->dst.type = OP_REG; |
| 3178 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3179 | } |
| 3180 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3181 | case 0xb3: |
| 3182 | btr: /* btr */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3183 | /* only subword offset */ |
| 3184 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3185 | emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3186 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3187 | case 0xb6 ... 0xb7: /* movzx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3188 | c->dst.bytes = c->op_bytes; |
| 3189 | c->dst.val = (c->d & ByteOp) ? (u8) c->src.val |
| 3190 | : (u16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3191 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3192 | case 0xba: /* Grp8 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3193 | switch (c->modrm_reg & 3) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3194 | case 0: |
| 3195 | goto bt; |
| 3196 | case 1: |
| 3197 | goto bts; |
| 3198 | case 2: |
| 3199 | goto btr; |
| 3200 | case 3: |
| 3201 | goto btc; |
| 3202 | } |
| 3203 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3204 | case 0xbb: |
| 3205 | btc: /* btc */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3206 | /* only subword offset */ |
| 3207 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3208 | emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3209 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3210 | case 0xbe ... 0xbf: /* movsx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3211 | c->dst.bytes = c->op_bytes; |
| 3212 | c->dst.val = (c->d & ByteOp) ? (s8) c->src.val : |
| 3213 | (s16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3214 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 3215 | case 0xc3: /* movnti */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3216 | c->dst.bytes = c->op_bytes; |
| 3217 | c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val : |
| 3218 | (u64) c->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 3219 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3220 | case 0xc7: /* Grp9 (cmpxchg8b) */ |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 3221 | rc = emulate_grp9(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3222 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3223 | goto done; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3224 | c->dst.type = OP_NONE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3225 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3226 | } |
| 3227 | goto writeback; |
| 3228 | |
| 3229 | cannot_emulate: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3230 | DPRINTF("Cannot emulate %02x\n", c->b); |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 3231 | c->eip = saved_eip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3232 | return -1; |
| 3233 | } |