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 | |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 35 | #include "mmu.h" /* for is_long_mode() */ |
| 36 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 37 | /* |
| 38 | * Opcode effective-address decode tables. |
| 39 | * Note that we only emulate instructions that have at least one memory |
| 40 | * operand (excluding implicit stack references). We assume that stack |
| 41 | * references and instruction fetches will never occur in special memory |
| 42 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need |
| 43 | * not be handled. |
| 44 | */ |
| 45 | |
| 46 | /* Operand sizes: 8-bit operands or specified/overridden size. */ |
| 47 | #define ByteOp (1<<0) /* 8-bit operands. */ |
| 48 | /* Destination operand type. */ |
| 49 | #define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */ |
| 50 | #define DstReg (2<<1) /* Register operand. */ |
| 51 | #define DstMem (3<<1) /* Memory operand. */ |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 52 | #define DstAcc (4<<1) /* Destination Accumulator */ |
| 53 | #define DstMask (7<<1) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 54 | /* Source operand type. */ |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 55 | #define SrcNone (0<<4) /* No source operand. */ |
| 56 | #define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */ |
| 57 | #define SrcReg (1<<4) /* Register operand. */ |
| 58 | #define SrcMem (2<<4) /* Memory operand. */ |
| 59 | #define SrcMem16 (3<<4) /* Memory operand (16-bit). */ |
| 60 | #define SrcMem32 (4<<4) /* Memory operand (32-bit). */ |
| 61 | #define SrcImm (5<<4) /* Immediate operand. */ |
| 62 | #define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */ |
Guillaume Thouvenin | bfcadf8 | 2008-12-04 14:27:38 +0100 | [diff] [blame] | 63 | #define SrcOne (7<<4) /* Implied '1' */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 64 | #define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */ |
Avi Kivity | c9eaf20 | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 65 | #define SrcImmU (9<<4) /* Immediate operand, unsigned */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 66 | #define SrcMask (0xf<<4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 67 | /* Generic ModRM decode. */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 68 | #define ModRM (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 69 | /* Destination is only written; never read. */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 70 | #define Mov (1<<9) |
| 71 | #define BitOp (1<<10) |
| 72 | #define MemAbs (1<<11) /* Memory operand is absolute displacement */ |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 73 | #define String (1<<12) /* String instruction (rep capable) */ |
| 74 | #define Stack (1<<13) /* Stack instruction (push/pop) */ |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 75 | #define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */ |
| 76 | #define GroupDual (1<<15) /* Alternate decoding of mod == 3 */ |
| 77 | #define GroupMask 0xff /* Group number stored in bits 0:7 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 78 | /* Misc flags */ |
| 79 | #define No64 (1<<28) |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 80 | /* Source 2 operand type */ |
| 81 | #define Src2None (0<<29) |
| 82 | #define Src2CL (1<<29) |
| 83 | #define Src2ImmByte (2<<29) |
| 84 | #define Src2One (3<<29) |
Gleb Natapov | a5f868b | 2009-04-12 13:36:14 +0300 | [diff] [blame] | 85 | #define Src2Imm16 (4<<29) |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 86 | #define Src2Mask (7<<29) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 87 | |
Avi Kivity | 43bb19c | 2008-01-18 12:46:50 +0200 | [diff] [blame] | 88 | enum { |
Avi Kivity | 1d6ad20 | 2008-01-23 22:26:09 +0200 | [diff] [blame] | 89 | Group1_80, Group1_81, Group1_82, Group1_83, |
Avi Kivity | d95058a | 2008-01-18 13:36:50 +0200 | [diff] [blame] | 90 | Group1A, Group3_Byte, Group3, Group4, Group5, Group7, |
Avi Kivity | 43bb19c | 2008-01-18 12:46:50 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
Guillaume Thouvenin | 45ed60b | 2008-12-04 14:25:38 +0100 | [diff] [blame] | 93 | static u32 opcode_table[256] = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 94 | /* 0x00 - 0x07 */ |
| 95 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 96 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 97 | ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 98 | ImplicitOps | Stack | No64, ImplicitOps | Stack | No64, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 99 | /* 0x08 - 0x0F */ |
| 100 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 101 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 102 | 0, 0, ImplicitOps | Stack | No64, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 103 | /* 0x10 - 0x17 */ |
| 104 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 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 | /* 0x18 - 0x1F */ |
| 109 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 110 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 111 | ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 112 | ImplicitOps | Stack | No64, ImplicitOps | Stack | No64, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 113 | /* 0x20 - 0x27 */ |
| 114 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 115 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
Guillaume Thouvenin | aa3a816 | 2008-09-12 13:52:18 +0200 | [diff] [blame] | 116 | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 117 | /* 0x28 - 0x2F */ |
| 118 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 119 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
| 120 | 0, 0, 0, 0, |
| 121 | /* 0x30 - 0x37 */ |
| 122 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 123 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
| 124 | 0, 0, 0, 0, |
| 125 | /* 0x38 - 0x3F */ |
| 126 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 127 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
Guillaume Thouvenin | 8a9fee6 | 2008-09-12 13:51:15 +0200 | [diff] [blame] | 128 | ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, |
| 129 | 0, 0, |
Nitin A Kamble | d77a250 | 2007-10-12 17:40:33 -0700 | [diff] [blame] | 130 | /* 0x40 - 0x47 */ |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 131 | DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, |
Nitin A Kamble | d77a250 | 2007-10-12 17:40:33 -0700 | [diff] [blame] | 132 | /* 0x48 - 0x4F */ |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 133 | DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, |
Nitin A Kamble | 7f0aaee | 2007-06-19 11:16:04 +0300 | [diff] [blame] | 134 | /* 0x50 - 0x57 */ |
Avi Kivity | 6e3d5df | 2007-12-06 18:14:14 +0200 | [diff] [blame] | 135 | SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, |
| 136 | SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, |
Nitin A Kamble | 7f0aaee | 2007-06-19 11:16:04 +0300 | [diff] [blame] | 137 | /* 0x58 - 0x5F */ |
Avi Kivity | 6e3d5df | 2007-12-06 18:14:14 +0200 | [diff] [blame] | 138 | DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack, |
| 139 | DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack, |
Nitin A Kamble | 7d31691 | 2007-08-28 17:58:52 -0700 | [diff] [blame] | 140 | /* 0x60 - 0x67 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 141 | 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ , |
Nitin A Kamble | 7d31691 | 2007-08-28 17:58:52 -0700 | [diff] [blame] | 142 | 0, 0, 0, 0, |
| 143 | /* 0x68 - 0x6F */ |
Avi Kivity | 91ed7a0 | 2008-05-29 14:38:38 +0300 | [diff] [blame] | 144 | SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0, |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 145 | SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */ |
| 146 | SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */ |
Nitin A Kamble | 55bebde | 2007-09-15 10:25:41 +0300 | [diff] [blame] | 147 | /* 0x70 - 0x77 */ |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 148 | SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte, |
| 149 | SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte, |
Nitin A Kamble | 55bebde | 2007-09-15 10:25:41 +0300 | [diff] [blame] | 150 | /* 0x78 - 0x7F */ |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 151 | SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte, |
| 152 | SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 153 | /* 0x80 - 0x87 */ |
Avi Kivity | 1d6ad20 | 2008-01-23 22:26:09 +0200 | [diff] [blame] | 154 | Group | Group1_80, Group | Group1_81, |
| 155 | Group | Group1_82, Group | Group1_83, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 156 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 157 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 158 | /* 0x88 - 0x8F */ |
| 159 | ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov, |
| 160 | ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 161 | DstMem | SrcReg | ModRM | Mov, ModRM | DstReg, |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 162 | DstReg | SrcMem | ModRM | Mov, Group | Group1A, |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 163 | /* 0x90 - 0x97 */ |
| 164 | DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, |
| 165 | /* 0x98 - 0x9F */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 166 | 0, 0, SrcImm | Src2Imm16 | No64, 0, |
Gleb Natapov | 0654169 | 2009-04-12 13:36:20 +0300 | [diff] [blame] | 167 | ImplicitOps | Stack, ImplicitOps | Stack, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 168 | /* 0xA0 - 0xA7 */ |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 169 | ByteOp | DstReg | SrcMem | Mov | MemAbs, DstReg | SrcMem | Mov | MemAbs, |
| 170 | ByteOp | DstMem | SrcReg | Mov | MemAbs, DstMem | SrcReg | Mov | MemAbs, |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 171 | ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String, |
| 172 | ByteOp | ImplicitOps | String, ImplicitOps | String, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 173 | /* 0xA8 - 0xAF */ |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 174 | 0, 0, ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String, |
| 175 | ByteOp | ImplicitOps | Mov | String, ImplicitOps | Mov | String, |
| 176 | ByteOp | ImplicitOps | String, ImplicitOps | String, |
Mohammed Gamal | a5e2e82 | 2008-08-27 05:02:56 +0300 | [diff] [blame] | 177 | /* 0xB0 - 0xB7 */ |
| 178 | ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov, |
| 179 | ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov, |
| 180 | ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov, |
| 181 | ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov, |
| 182 | /* 0xB8 - 0xBF */ |
| 183 | DstReg | SrcImm | Mov, DstReg | SrcImm | Mov, |
| 184 | DstReg | SrcImm | Mov, DstReg | SrcImm | Mov, |
| 185 | DstReg | SrcImm | Mov, DstReg | SrcImm | Mov, |
| 186 | DstReg | SrcImm | Mov, DstReg | SrcImm | Mov, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 187 | /* 0xC0 - 0xC7 */ |
Nitin A Kamble | d9413cd | 2007-06-19 11:21:15 +0300 | [diff] [blame] | 188 | ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM, |
Avi Kivity | 6e3d5df | 2007-12-06 18:14:14 +0200 | [diff] [blame] | 189 | 0, ImplicitOps | Stack, 0, 0, |
Nitin A Kamble | d9413cd | 2007-06-19 11:21:15 +0300 | [diff] [blame] | 190 | ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 191 | /* 0xC8 - 0xCF */ |
Gleb Natapov | e637b82 | 2009-04-12 13:36:52 +0300 | [diff] [blame] | 192 | 0, 0, 0, ImplicitOps | Stack, |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 193 | ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 194 | /* 0xD0 - 0xD7 */ |
| 195 | ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM, |
| 196 | ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM, |
| 197 | 0, 0, 0, 0, |
| 198 | /* 0xD8 - 0xDF */ |
| 199 | 0, 0, 0, 0, 0, 0, 0, 0, |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 200 | /* 0xE0 - 0xE7 */ |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 201 | 0, 0, 0, 0, |
Gleb Natapov | 84ce66a | 2009-04-12 13:36:46 +0300 | [diff] [blame] | 202 | ByteOp | SrcImmUByte, SrcImmUByte, |
| 203 | ByteOp | SrcImmUByte, SrcImmUByte, |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 204 | /* 0xE8 - 0xEF */ |
Gleb Natapov | d53c477 | 2009-04-12 13:36:36 +0300 | [diff] [blame] | 205 | SrcImm | Stack, SrcImm | ImplicitOps, |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 206 | SrcImmU | Src2Imm16 | No64, SrcImmByte | ImplicitOps, |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 207 | SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, |
| 208 | SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 209 | /* 0xF0 - 0xF7 */ |
| 210 | 0, 0, 0, 0, |
Avi Kivity | 7d858a1 | 2008-01-18 12:58:04 +0200 | [diff] [blame] | 211 | ImplicitOps, ImplicitOps, Group | Group3_Byte, Group | Group3, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 212 | /* 0xF8 - 0xFF */ |
Nitin A Kamble | b284be5 | 2007-10-16 18:23:27 -0700 | [diff] [blame] | 213 | ImplicitOps, 0, ImplicitOps, ImplicitOps, |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 214 | ImplicitOps, ImplicitOps, Group | Group4, Group | Group5, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
Guillaume Thouvenin | 45ed60b | 2008-12-04 14:25:38 +0100 | [diff] [blame] | 217 | static u32 twobyte_table[256] = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 218 | /* 0x00 - 0x0F */ |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 219 | 0, Group | GroupDual | Group7, 0, 0, 0, ImplicitOps, ImplicitOps, 0, |
Avi Kivity | 651a3e2 | 2007-10-28 16:09:18 +0200 | [diff] [blame] | 220 | ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 221 | /* 0x10 - 0x1F */ |
| 222 | 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0, |
| 223 | /* 0x20 - 0x2F */ |
| 224 | ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0, |
| 225 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 226 | /* 0x30 - 0x3F */ |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 227 | ImplicitOps, 0, ImplicitOps, 0, |
| 228 | ImplicitOps, ImplicitOps, 0, 0, |
| 229 | 0, 0, 0, 0, 0, 0, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 230 | /* 0x40 - 0x47 */ |
| 231 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 232 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 233 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 234 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 235 | /* 0x48 - 0x4F */ |
| 236 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 237 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 238 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 239 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 240 | /* 0x50 - 0x5F */ |
| 241 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 242 | /* 0x60 - 0x6F */ |
| 243 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 244 | /* 0x70 - 0x7F */ |
| 245 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 246 | /* 0x80 - 0x8F */ |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 247 | SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, |
| 248 | SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 249 | /* 0x90 - 0x9F */ |
| 250 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 251 | /* 0xA0 - 0xA7 */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 252 | ImplicitOps | Stack, ImplicitOps | Stack, |
| 253 | 0, DstMem | SrcReg | ModRM | BitOp, |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 254 | DstMem | SrcReg | Src2ImmByte | ModRM, |
| 255 | DstMem | SrcReg | Src2CL | ModRM, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 256 | /* 0xA8 - 0xAF */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 257 | ImplicitOps | Stack, ImplicitOps | Stack, |
| 258 | 0, DstMem | SrcReg | ModRM | BitOp, |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 259 | DstMem | SrcReg | Src2ImmByte | ModRM, |
| 260 | DstMem | SrcReg | Src2CL | ModRM, |
| 261 | ModRM, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 262 | /* 0xB0 - 0xB7 */ |
| 263 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0, |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 264 | DstMem | SrcReg | ModRM | BitOp, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 265 | 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov, |
| 266 | DstReg | SrcMem16 | ModRM | Mov, |
| 267 | /* 0xB8 - 0xBF */ |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 268 | 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 269 | 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov, |
| 270 | DstReg | SrcMem16 | ModRM | Mov, |
| 271 | /* 0xC0 - 0xCF */ |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 272 | 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM, |
| 273 | 0, 0, 0, 0, 0, 0, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 274 | /* 0xD0 - 0xDF */ |
| 275 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 276 | /* 0xE0 - 0xEF */ |
| 277 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 278 | /* 0xF0 - 0xFF */ |
| 279 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 280 | }; |
| 281 | |
Guillaume Thouvenin | 45ed60b | 2008-12-04 14:25:38 +0100 | [diff] [blame] | 282 | static u32 group_table[] = { |
Avi Kivity | 1d6ad20 | 2008-01-23 22:26:09 +0200 | [diff] [blame] | 283 | [Group1_80*8] = |
| 284 | ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM, |
| 285 | ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM, |
| 286 | ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM, |
| 287 | ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM, |
| 288 | [Group1_81*8] = |
| 289 | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM, |
| 290 | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM, |
| 291 | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM, |
| 292 | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM, |
| 293 | [Group1_82*8] = |
| 294 | ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM, |
| 295 | ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM, |
| 296 | ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM, |
| 297 | ByteOp | DstMem | SrcImm | ModRM, ByteOp | DstMem | SrcImm | ModRM, |
| 298 | [Group1_83*8] = |
| 299 | DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM, |
| 300 | DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM, |
| 301 | DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM, |
| 302 | DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM, |
Avi Kivity | 43bb19c | 2008-01-18 12:46:50 +0200 | [diff] [blame] | 303 | [Group1A*8] = |
| 304 | DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0, |
Avi Kivity | 7d858a1 | 2008-01-18 12:58:04 +0200 | [diff] [blame] | 305 | [Group3_Byte*8] = |
| 306 | ByteOp | SrcImm | DstMem | ModRM, 0, |
| 307 | ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM, |
| 308 | 0, 0, 0, 0, |
| 309 | [Group3*8] = |
roel kluin | 41afa02 | 2008-08-18 21:25:01 -0400 | [diff] [blame] | 310 | DstMem | SrcImm | ModRM, 0, |
Avi Kivity | 6eb06cb | 2008-08-21 17:41:39 +0300 | [diff] [blame] | 311 | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM, |
Avi Kivity | 7d858a1 | 2008-01-18 12:58:04 +0200 | [diff] [blame] | 312 | 0, 0, 0, 0, |
Avi Kivity | fd60754 | 2008-01-18 13:12:26 +0200 | [diff] [blame] | 313 | [Group4*8] = |
| 314 | ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM, |
| 315 | 0, 0, 0, 0, 0, 0, |
| 316 | [Group5*8] = |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 317 | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM, |
| 318 | SrcMem | ModRM | Stack, 0, |
Avi Kivity | ef46f18 | 2008-09-11 19:47:13 +0300 | [diff] [blame] | 319 | SrcMem | ModRM | Stack, 0, SrcMem | ModRM | Stack, 0, |
Avi Kivity | d95058a | 2008-01-18 13:36:50 +0200 | [diff] [blame] | 320 | [Group7*8] = |
| 321 | 0, 0, ModRM | SrcMem, ModRM | SrcMem, |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 322 | SrcNone | ModRM | DstMem | Mov, 0, |
| 323 | SrcMem16 | ModRM | Mov, SrcMem | ModRM | ByteOp, |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 324 | }; |
| 325 | |
Guillaume Thouvenin | 45ed60b | 2008-12-04 14:25:38 +0100 | [diff] [blame] | 326 | static u32 group2_table[] = { |
Avi Kivity | d95058a | 2008-01-18 13:36:50 +0200 | [diff] [blame] | 327 | [Group7*8] = |
Amit Shah | fbce554 | 2008-12-04 11:11:40 +0000 | [diff] [blame] | 328 | SrcNone | ModRM, 0, 0, SrcNone | ModRM, |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 329 | SrcNone | ModRM | DstMem | Mov, 0, |
| 330 | SrcMem16 | ModRM | Mov, 0, |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 331 | }; |
| 332 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 333 | /* EFLAGS bit definitions. */ |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 334 | #define EFLG_VM (1<<17) |
| 335 | #define EFLG_RF (1<<16) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 336 | #define EFLG_OF (1<<11) |
| 337 | #define EFLG_DF (1<<10) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 338 | #define EFLG_IF (1<<9) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 339 | #define EFLG_SF (1<<7) |
| 340 | #define EFLG_ZF (1<<6) |
| 341 | #define EFLG_AF (1<<4) |
| 342 | #define EFLG_PF (1<<2) |
| 343 | #define EFLG_CF (1<<0) |
| 344 | |
| 345 | /* |
| 346 | * Instruction emulation: |
| 347 | * Most instructions are emulated directly via a fragment of inline assembly |
| 348 | * code. This allows us to save/restore EFLAGS and thus very easily pick up |
| 349 | * any modified flags. |
| 350 | */ |
| 351 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 352 | #if defined(CONFIG_X86_64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 353 | #define _LO32 "k" /* force 32-bit operand */ |
| 354 | #define _STK "%%rsp" /* stack pointer */ |
| 355 | #elif defined(__i386__) |
| 356 | #define _LO32 "" /* force 32-bit operand */ |
| 357 | #define _STK "%%esp" /* stack pointer */ |
| 358 | #endif |
| 359 | |
| 360 | /* |
| 361 | * These EFLAGS bits are restored from saved value during emulation, and |
| 362 | * any changes are written back to the saved value after emulation. |
| 363 | */ |
| 364 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 365 | |
| 366 | /* Before executing instruction: restore necessary bits in EFLAGS. */ |
Avi Kivity | e934c9c | 2007-12-06 16:15:02 +0200 | [diff] [blame] | 367 | #define _PRE_EFLAGS(_sav, _msk, _tmp) \ |
| 368 | /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \ |
| 369 | "movl %"_sav",%"_LO32 _tmp"; " \ |
| 370 | "push %"_tmp"; " \ |
| 371 | "push %"_tmp"; " \ |
| 372 | "movl %"_msk",%"_LO32 _tmp"; " \ |
| 373 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 374 | "pushf; " \ |
| 375 | "notl %"_LO32 _tmp"; " \ |
| 376 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 377 | "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \ |
| 378 | "pop %"_tmp"; " \ |
| 379 | "orl %"_LO32 _tmp",("_STK"); " \ |
| 380 | "popf; " \ |
| 381 | "pop %"_sav"; " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 382 | |
| 383 | /* After executing instruction: write-back necessary bits in EFLAGS. */ |
| 384 | #define _POST_EFLAGS(_sav, _msk, _tmp) \ |
| 385 | /* _sav |= EFLAGS & _msk; */ \ |
| 386 | "pushf; " \ |
| 387 | "pop %"_tmp"; " \ |
| 388 | "andl %"_msk",%"_LO32 _tmp"; " \ |
| 389 | "orl %"_LO32 _tmp",%"_sav"; " |
| 390 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 391 | #ifdef CONFIG_X86_64 |
| 392 | #define ON64(x) x |
| 393 | #else |
| 394 | #define ON64(x) |
| 395 | #endif |
| 396 | |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 397 | #define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \ |
| 398 | do { \ |
| 399 | __asm__ __volatile__ ( \ |
| 400 | _PRE_EFLAGS("0", "4", "2") \ |
| 401 | _op _suffix " %"_x"3,%1; " \ |
| 402 | _POST_EFLAGS("0", "4", "2") \ |
| 403 | : "=m" (_eflags), "=m" ((_dst).val), \ |
| 404 | "=&r" (_tmp) \ |
| 405 | : _y ((_src).val), "i" (EFLAGS_MASK)); \ |
Avi Kivity | f3fd92f | 2008-11-29 20:38:12 +0200 | [diff] [blame] | 406 | } while (0) |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 407 | |
| 408 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 409 | /* Raw emulation: instruction has two explicit operands. */ |
| 410 | #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] | 411 | do { \ |
| 412 | unsigned long _tmp; \ |
| 413 | \ |
| 414 | switch ((_dst).bytes) { \ |
| 415 | case 2: \ |
| 416 | ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \ |
| 417 | break; \ |
| 418 | case 4: \ |
| 419 | ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \ |
| 420 | break; \ |
| 421 | case 8: \ |
| 422 | ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \ |
| 423 | break; \ |
| 424 | } \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 425 | } while (0) |
| 426 | |
| 427 | #define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \ |
| 428 | do { \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 429 | unsigned long _tmp; \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 430 | switch ((_dst).bytes) { \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 431 | case 1: \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 432 | ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 433 | break; \ |
| 434 | default: \ |
| 435 | __emulate_2op_nobyte(_op, _src, _dst, _eflags, \ |
| 436 | _wx, _wy, _lx, _ly, _qx, _qy); \ |
| 437 | break; \ |
| 438 | } \ |
| 439 | } while (0) |
| 440 | |
| 441 | /* Source operand is byte-sized and may be restricted to just %cl. */ |
| 442 | #define emulate_2op_SrcB(_op, _src, _dst, _eflags) \ |
| 443 | __emulate_2op(_op, _src, _dst, _eflags, \ |
| 444 | "b", "c", "b", "c", "b", "c", "b", "c") |
| 445 | |
| 446 | /* Source operand is byte, word, long or quad sized. */ |
| 447 | #define emulate_2op_SrcV(_op, _src, _dst, _eflags) \ |
| 448 | __emulate_2op(_op, _src, _dst, _eflags, \ |
| 449 | "b", "q", "w", "r", _LO32, "r", "", "r") |
| 450 | |
| 451 | /* Source operand is word, long or quad sized. */ |
| 452 | #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \ |
| 453 | __emulate_2op_nobyte(_op, _src, _dst, _eflags, \ |
| 454 | "w", "r", _LO32, "r", "", "r") |
| 455 | |
Guillaume Thouvenin | d175226 | 2008-12-04 14:29:00 +0100 | [diff] [blame] | 456 | /* Instruction has three operands and one operand is stored in ECX register */ |
| 457 | #define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \ |
| 458 | do { \ |
| 459 | unsigned long _tmp; \ |
| 460 | _type _clv = (_cl).val; \ |
| 461 | _type _srcv = (_src).val; \ |
| 462 | _type _dstv = (_dst).val; \ |
| 463 | \ |
| 464 | __asm__ __volatile__ ( \ |
| 465 | _PRE_EFLAGS("0", "5", "2") \ |
| 466 | _op _suffix " %4,%1 \n" \ |
| 467 | _POST_EFLAGS("0", "5", "2") \ |
| 468 | : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \ |
| 469 | : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \ |
| 470 | ); \ |
| 471 | \ |
| 472 | (_cl).val = (unsigned long) _clv; \ |
| 473 | (_src).val = (unsigned long) _srcv; \ |
| 474 | (_dst).val = (unsigned long) _dstv; \ |
| 475 | } while (0) |
| 476 | |
| 477 | #define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \ |
| 478 | do { \ |
| 479 | switch ((_dst).bytes) { \ |
| 480 | case 2: \ |
| 481 | __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \ |
| 482 | "w", unsigned short); \ |
| 483 | break; \ |
| 484 | case 4: \ |
| 485 | __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \ |
| 486 | "l", unsigned int); \ |
| 487 | break; \ |
| 488 | case 8: \ |
| 489 | ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \ |
| 490 | "q", unsigned long)); \ |
| 491 | break; \ |
| 492 | } \ |
| 493 | } while (0) |
| 494 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 495 | #define __emulate_1op(_op, _dst, _eflags, _suffix) \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 496 | do { \ |
| 497 | unsigned long _tmp; \ |
| 498 | \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 499 | __asm__ __volatile__ ( \ |
| 500 | _PRE_EFLAGS("0", "3", "2") \ |
| 501 | _op _suffix " %1; " \ |
| 502 | _POST_EFLAGS("0", "3", "2") \ |
| 503 | : "=m" (_eflags), "+m" ((_dst).val), \ |
| 504 | "=&r" (_tmp) \ |
| 505 | : "i" (EFLAGS_MASK)); \ |
| 506 | } while (0) |
| 507 | |
| 508 | /* Instruction has only one explicit operand (no source operand). */ |
| 509 | #define emulate_1op(_op, _dst, _eflags) \ |
| 510 | do { \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 511 | switch ((_dst).bytes) { \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 512 | case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \ |
| 513 | case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \ |
| 514 | case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \ |
| 515 | case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 516 | } \ |
| 517 | } while (0) |
| 518 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 519 | /* Fetch next part of the instruction being emulated. */ |
| 520 | #define insn_fetch(_type, _size, _eip) \ |
| 521 | ({ unsigned long _x; \ |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 522 | rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 523 | if (rc != 0) \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 524 | goto done; \ |
| 525 | (_eip) += (_size); \ |
| 526 | (_type)_x; \ |
| 527 | }) |
| 528 | |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 529 | static inline unsigned long ad_mask(struct decode_cache *c) |
| 530 | { |
| 531 | return (1UL << (c->ad_bytes << 3)) - 1; |
| 532 | } |
| 533 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 534 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 535 | static inline unsigned long |
| 536 | address_mask(struct decode_cache *c, unsigned long reg) |
| 537 | { |
| 538 | if (c->ad_bytes == sizeof(unsigned long)) |
| 539 | return reg; |
| 540 | else |
| 541 | return reg & ad_mask(c); |
| 542 | } |
| 543 | |
| 544 | static inline unsigned long |
| 545 | register_address(struct decode_cache *c, unsigned long base, unsigned long reg) |
| 546 | { |
| 547 | return base + address_mask(c, reg); |
| 548 | } |
| 549 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 550 | static inline void |
| 551 | register_address_increment(struct decode_cache *c, unsigned long *reg, int inc) |
| 552 | { |
| 553 | if (c->ad_bytes == sizeof(unsigned long)) |
| 554 | *reg += inc; |
| 555 | else |
| 556 | *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c)); |
| 557 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 558 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 559 | static inline void jmp_rel(struct decode_cache *c, int rel) |
| 560 | { |
| 561 | register_address_increment(c, &c->eip, rel); |
| 562 | } |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 563 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 564 | static void set_seg_override(struct decode_cache *c, int seg) |
| 565 | { |
| 566 | c->has_seg_override = true; |
| 567 | c->seg_override = seg; |
| 568 | } |
| 569 | |
| 570 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
| 571 | { |
| 572 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 573 | return 0; |
| 574 | |
| 575 | return kvm_x86_ops->get_segment_base(ctxt->vcpu, seg); |
| 576 | } |
| 577 | |
| 578 | static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt, |
| 579 | struct decode_cache *c) |
| 580 | { |
| 581 | if (!c->has_seg_override) |
| 582 | return 0; |
| 583 | |
| 584 | return seg_base(ctxt, c->seg_override); |
| 585 | } |
| 586 | |
| 587 | static unsigned long es_base(struct x86_emulate_ctxt *ctxt) |
| 588 | { |
| 589 | return seg_base(ctxt, VCPU_SREG_ES); |
| 590 | } |
| 591 | |
| 592 | static unsigned long ss_base(struct x86_emulate_ctxt *ctxt) |
| 593 | { |
| 594 | return seg_base(ctxt, VCPU_SREG_SS); |
| 595 | } |
| 596 | |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 597 | static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt, |
| 598 | struct x86_emulate_ops *ops, |
| 599 | unsigned long linear, u8 *dest) |
| 600 | { |
| 601 | struct fetch_cache *fc = &ctxt->decode.fetch; |
| 602 | int rc; |
| 603 | int size; |
| 604 | |
| 605 | if (linear < fc->start || linear >= fc->end) { |
| 606 | size = min(15UL, PAGE_SIZE - offset_in_page(linear)); |
| 607 | rc = ops->read_std(linear, fc->data, size, ctxt->vcpu); |
| 608 | if (rc) |
| 609 | return rc; |
| 610 | fc->start = linear; |
| 611 | fc->end = linear + size; |
| 612 | } |
| 613 | *dest = fc->data[linear - fc->start]; |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, |
| 618 | struct x86_emulate_ops *ops, |
| 619 | unsigned long eip, void *dest, unsigned size) |
| 620 | { |
| 621 | int rc = 0; |
| 622 | |
| 623 | eip += ctxt->cs_base; |
| 624 | while (size--) { |
| 625 | rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++); |
| 626 | if (rc) |
| 627 | return rc; |
| 628 | } |
| 629 | return 0; |
| 630 | } |
| 631 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 632 | /* |
| 633 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 634 | * pointer into the block that addresses the relevant register. |
| 635 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 636 | */ |
| 637 | static void *decode_register(u8 modrm_reg, unsigned long *regs, |
| 638 | int highbyte_regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 639 | { |
| 640 | void *p; |
| 641 | |
| 642 | p = ®s[modrm_reg]; |
| 643 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
| 644 | p = (unsigned char *)®s[modrm_reg & 3] + 1; |
| 645 | return p; |
| 646 | } |
| 647 | |
| 648 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
| 649 | struct x86_emulate_ops *ops, |
| 650 | void *ptr, |
| 651 | u16 *size, unsigned long *address, int op_bytes) |
| 652 | { |
| 653 | int rc; |
| 654 | |
| 655 | if (op_bytes == 2) |
| 656 | op_bytes = 3; |
| 657 | *address = 0; |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 658 | rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2, |
| 659 | ctxt->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 660 | if (rc) |
| 661 | return rc; |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 662 | rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes, |
| 663 | ctxt->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 664 | return rc; |
| 665 | } |
| 666 | |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 667 | static int test_cc(unsigned int condition, unsigned int flags) |
| 668 | { |
| 669 | int rc = 0; |
| 670 | |
| 671 | switch ((condition & 15) >> 1) { |
| 672 | case 0: /* o */ |
| 673 | rc |= (flags & EFLG_OF); |
| 674 | break; |
| 675 | case 1: /* b/c/nae */ |
| 676 | rc |= (flags & EFLG_CF); |
| 677 | break; |
| 678 | case 2: /* z/e */ |
| 679 | rc |= (flags & EFLG_ZF); |
| 680 | break; |
| 681 | case 3: /* be/na */ |
| 682 | rc |= (flags & (EFLG_CF|EFLG_ZF)); |
| 683 | break; |
| 684 | case 4: /* s */ |
| 685 | rc |= (flags & EFLG_SF); |
| 686 | break; |
| 687 | case 5: /* p/pe */ |
| 688 | rc |= (flags & EFLG_PF); |
| 689 | break; |
| 690 | case 7: /* le/ng */ |
| 691 | rc |= (flags & EFLG_ZF); |
| 692 | /* fall through */ |
| 693 | case 6: /* l/nge */ |
| 694 | rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF)); |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | /* Odd condition identifiers (lsb == 1) have inverted sense. */ |
| 699 | return (!!rc ^ (condition & 1)); |
| 700 | } |
| 701 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 702 | static void decode_register_operand(struct operand *op, |
| 703 | struct decode_cache *c, |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 704 | int inhibit_bytereg) |
| 705 | { |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 706 | unsigned reg = c->modrm_reg; |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 707 | int highbyte_regs = c->rex_prefix == 0; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 708 | |
| 709 | if (!(c->d & ModRM)) |
| 710 | reg = (c->b & 7) | ((c->rex_prefix & 1) << 3); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 711 | op->type = OP_REG; |
| 712 | if ((c->d & ByteOp) && !inhibit_bytereg) { |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 713 | op->ptr = decode_register(reg, c->regs, highbyte_regs); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 714 | op->val = *(u8 *)op->ptr; |
| 715 | op->bytes = 1; |
| 716 | } else { |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 717 | op->ptr = decode_register(reg, c->regs, 0); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 718 | op->bytes = c->op_bytes; |
| 719 | switch (op->bytes) { |
| 720 | case 2: |
| 721 | op->val = *(u16 *)op->ptr; |
| 722 | break; |
| 723 | case 4: |
| 724 | op->val = *(u32 *)op->ptr; |
| 725 | break; |
| 726 | case 8: |
| 727 | op->val = *(u64 *) op->ptr; |
| 728 | break; |
| 729 | } |
| 730 | } |
| 731 | op->orig_val = op->val; |
| 732 | } |
| 733 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 734 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
| 735 | struct x86_emulate_ops *ops) |
| 736 | { |
| 737 | struct decode_cache *c = &ctxt->decode; |
| 738 | u8 sib; |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 739 | int index_reg = 0, base_reg = 0, scale; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 740 | int rc = 0; |
| 741 | |
| 742 | if (c->rex_prefix) { |
| 743 | c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */ |
| 744 | index_reg = (c->rex_prefix & 2) << 2; /* REX.X */ |
| 745 | c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */ |
| 746 | } |
| 747 | |
| 748 | c->modrm = insn_fetch(u8, 1, c->eip); |
| 749 | c->modrm_mod |= (c->modrm & 0xc0) >> 6; |
| 750 | c->modrm_reg |= (c->modrm & 0x38) >> 3; |
| 751 | c->modrm_rm |= (c->modrm & 0x07); |
| 752 | c->modrm_ea = 0; |
| 753 | c->use_modrm_ea = 1; |
| 754 | |
| 755 | if (c->modrm_mod == 3) { |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 756 | c->modrm_ptr = decode_register(c->modrm_rm, |
| 757 | c->regs, c->d & ByteOp); |
| 758 | c->modrm_val = *(unsigned long *)c->modrm_ptr; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 759 | return rc; |
| 760 | } |
| 761 | |
| 762 | if (c->ad_bytes == 2) { |
| 763 | unsigned bx = c->regs[VCPU_REGS_RBX]; |
| 764 | unsigned bp = c->regs[VCPU_REGS_RBP]; |
| 765 | unsigned si = c->regs[VCPU_REGS_RSI]; |
| 766 | unsigned di = c->regs[VCPU_REGS_RDI]; |
| 767 | |
| 768 | /* 16-bit ModR/M decode. */ |
| 769 | switch (c->modrm_mod) { |
| 770 | case 0: |
| 771 | if (c->modrm_rm == 6) |
| 772 | c->modrm_ea += insn_fetch(u16, 2, c->eip); |
| 773 | break; |
| 774 | case 1: |
| 775 | c->modrm_ea += insn_fetch(s8, 1, c->eip); |
| 776 | break; |
| 777 | case 2: |
| 778 | c->modrm_ea += insn_fetch(u16, 2, c->eip); |
| 779 | break; |
| 780 | } |
| 781 | switch (c->modrm_rm) { |
| 782 | case 0: |
| 783 | c->modrm_ea += bx + si; |
| 784 | break; |
| 785 | case 1: |
| 786 | c->modrm_ea += bx + di; |
| 787 | break; |
| 788 | case 2: |
| 789 | c->modrm_ea += bp + si; |
| 790 | break; |
| 791 | case 3: |
| 792 | c->modrm_ea += bp + di; |
| 793 | break; |
| 794 | case 4: |
| 795 | c->modrm_ea += si; |
| 796 | break; |
| 797 | case 5: |
| 798 | c->modrm_ea += di; |
| 799 | break; |
| 800 | case 6: |
| 801 | if (c->modrm_mod != 0) |
| 802 | c->modrm_ea += bp; |
| 803 | break; |
| 804 | case 7: |
| 805 | c->modrm_ea += bx; |
| 806 | break; |
| 807 | } |
| 808 | if (c->modrm_rm == 2 || c->modrm_rm == 3 || |
| 809 | (c->modrm_rm == 6 && c->modrm_mod != 0)) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 810 | if (!c->has_seg_override) |
| 811 | set_seg_override(c, VCPU_SREG_SS); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 812 | c->modrm_ea = (u16)c->modrm_ea; |
| 813 | } else { |
| 814 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 815 | if ((c->modrm_rm & 7) == 4) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 816 | sib = insn_fetch(u8, 1, c->eip); |
| 817 | index_reg |= (sib >> 3) & 7; |
| 818 | base_reg |= sib & 7; |
| 819 | scale = sib >> 6; |
| 820 | |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 821 | if ((base_reg & 7) == 5 && c->modrm_mod == 0) |
| 822 | c->modrm_ea += insn_fetch(s32, 4, c->eip); |
| 823 | else |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 824 | c->modrm_ea += c->regs[base_reg]; |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 825 | if (index_reg != 4) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 826 | c->modrm_ea += c->regs[index_reg] << scale; |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 827 | } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) { |
| 828 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 829 | c->rip_relative = 1; |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 830 | } else |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 831 | c->modrm_ea += c->regs[c->modrm_rm]; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 832 | switch (c->modrm_mod) { |
| 833 | case 0: |
| 834 | if (c->modrm_rm == 5) |
| 835 | c->modrm_ea += insn_fetch(s32, 4, c->eip); |
| 836 | break; |
| 837 | case 1: |
| 838 | c->modrm_ea += insn_fetch(s8, 1, c->eip); |
| 839 | break; |
| 840 | case 2: |
| 841 | c->modrm_ea += insn_fetch(s32, 4, c->eip); |
| 842 | break; |
| 843 | } |
| 844 | } |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 845 | done: |
| 846 | return rc; |
| 847 | } |
| 848 | |
| 849 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
| 850 | struct x86_emulate_ops *ops) |
| 851 | { |
| 852 | struct decode_cache *c = &ctxt->decode; |
| 853 | int rc = 0; |
| 854 | |
| 855 | switch (c->ad_bytes) { |
| 856 | case 2: |
| 857 | c->modrm_ea = insn_fetch(u16, 2, c->eip); |
| 858 | break; |
| 859 | case 4: |
| 860 | c->modrm_ea = insn_fetch(u32, 4, c->eip); |
| 861 | break; |
| 862 | case 8: |
| 863 | c->modrm_ea = insn_fetch(u64, 8, c->eip); |
| 864 | break; |
| 865 | } |
| 866 | done: |
| 867 | return rc; |
| 868 | } |
| 869 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 870 | int |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 871 | 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] | 872 | { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 873 | struct decode_cache *c = &ctxt->decode; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 874 | int rc = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 875 | int mode = ctxt->mode; |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 876 | int def_op_bytes, def_ad_bytes, group; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 877 | |
| 878 | /* Shadow copy of register state. Committed on successful emulation. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 879 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 880 | memset(c, 0, sizeof(struct decode_cache)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 881 | c->eip = kvm_rip_read(ctxt->vcpu); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 882 | ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 883 | memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 884 | |
| 885 | switch (mode) { |
| 886 | case X86EMUL_MODE_REAL: |
| 887 | case X86EMUL_MODE_PROT16: |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 888 | def_op_bytes = def_ad_bytes = 2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 889 | break; |
| 890 | case X86EMUL_MODE_PROT32: |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 891 | def_op_bytes = def_ad_bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 892 | break; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 893 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 894 | case X86EMUL_MODE_PROT64: |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 895 | def_op_bytes = 4; |
| 896 | def_ad_bytes = 8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 897 | break; |
| 898 | #endif |
| 899 | default: |
| 900 | return -1; |
| 901 | } |
| 902 | |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 903 | c->op_bytes = def_op_bytes; |
| 904 | c->ad_bytes = def_ad_bytes; |
| 905 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 906 | /* Legacy prefixes. */ |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 907 | for (;;) { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 908 | switch (c->b = insn_fetch(u8, 1, c->eip)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 909 | case 0x66: /* operand-size override */ |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 910 | /* switch between 2/4 bytes */ |
| 911 | c->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 912 | break; |
| 913 | case 0x67: /* address-size override */ |
| 914 | if (mode == X86EMUL_MODE_PROT64) |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 915 | /* switch between 4/8 bytes */ |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 916 | c->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 917 | else |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 918 | /* switch between 2/4 bytes */ |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 919 | c->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 920 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 921 | case 0x26: /* ES override */ |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 922 | case 0x2e: /* CS override */ |
| 923 | case 0x36: /* SS override */ |
| 924 | case 0x3e: /* DS override */ |
| 925 | set_seg_override(c, (c->b >> 3) & 3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 926 | break; |
| 927 | case 0x64: /* FS override */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 928 | case 0x65: /* GS override */ |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 929 | set_seg_override(c, c->b & 7); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 930 | break; |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 931 | case 0x40 ... 0x4f: /* REX */ |
| 932 | if (mode != X86EMUL_MODE_PROT64) |
| 933 | goto done_prefixes; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 934 | c->rex_prefix = c->b; |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 935 | continue; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 936 | case 0xf0: /* LOCK */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 937 | c->lock_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 938 | break; |
Laurent Vivier | ae6200b | 2007-09-20 11:17:24 +0200 | [diff] [blame] | 939 | case 0xf2: /* REPNE/REPNZ */ |
Guillaume Thouvenin | 90e0a28 | 2007-11-22 11:32:09 +0100 | [diff] [blame] | 940 | c->rep_prefix = REPNE_PREFIX; |
| 941 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 942 | case 0xf3: /* REP/REPE/REPZ */ |
Guillaume Thouvenin | 90e0a28 | 2007-11-22 11:32:09 +0100 | [diff] [blame] | 943 | c->rep_prefix = REPE_PREFIX; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 944 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 945 | default: |
| 946 | goto done_prefixes; |
| 947 | } |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 948 | |
| 949 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 950 | |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 951 | c->rex_prefix = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | done_prefixes: |
| 955 | |
| 956 | /* REX prefix. */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 957 | if (c->rex_prefix) |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 958 | if (c->rex_prefix & 8) |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 959 | c->op_bytes = 8; /* REX.W */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 960 | |
| 961 | /* Opcode byte(s). */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 962 | c->d = opcode_table[c->b]; |
| 963 | if (c->d == 0) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 964 | /* Two-byte opcode? */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 965 | if (c->b == 0x0f) { |
| 966 | c->twobyte = 1; |
| 967 | c->b = insn_fetch(u8, 1, c->eip); |
| 968 | c->d = twobyte_table[c->b]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 969 | } |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 970 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 971 | |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 972 | if (mode == X86EMUL_MODE_PROT64 && (c->d & No64)) { |
| 973 | kvm_report_emulation_failure(ctxt->vcpu, "invalid x86/64 instruction");; |
| 974 | return -1; |
| 975 | } |
| 976 | |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 977 | if (c->d & Group) { |
| 978 | group = c->d & GroupMask; |
| 979 | c->modrm = insn_fetch(u8, 1, c->eip); |
| 980 | --c->eip; |
| 981 | |
| 982 | group = (group << 3) + ((c->modrm >> 3) & 7); |
| 983 | if ((c->d & GroupDual) && (c->modrm >> 6) == 3) |
| 984 | c->d = group2_table[group]; |
| 985 | else |
| 986 | c->d = group_table[group]; |
| 987 | } |
| 988 | |
| 989 | /* Unrecognised? */ |
| 990 | if (c->d == 0) { |
| 991 | DPRINTF("Cannot emulate %02x\n", c->b); |
| 992 | return -1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 993 | } |
| 994 | |
Avi Kivity | 6e3d5df | 2007-12-06 18:14:14 +0200 | [diff] [blame] | 995 | if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack)) |
| 996 | c->op_bytes = 8; |
| 997 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 998 | /* ModRM and SIB bytes. */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 999 | if (c->d & ModRM) |
| 1000 | rc = decode_modrm(ctxt, ops); |
| 1001 | else if (c->d & MemAbs) |
| 1002 | rc = decode_abs(ctxt, ops); |
| 1003 | if (rc) |
| 1004 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1005 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1006 | if (!c->has_seg_override) |
| 1007 | set_seg_override(c, VCPU_SREG_DS); |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 1008 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1009 | if (!(!c->twobyte && c->b == 0x8d)) |
| 1010 | c->modrm_ea += seg_override_base(ctxt, c); |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 1011 | |
| 1012 | if (c->ad_bytes != 8) |
| 1013 | c->modrm_ea = (u32)c->modrm_ea; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1014 | /* |
| 1015 | * Decode and fetch the source operand: register, memory |
| 1016 | * or immediate. |
| 1017 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1018 | switch (c->d & SrcMask) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1019 | case SrcNone: |
| 1020 | break; |
| 1021 | case SrcReg: |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 1022 | decode_register_operand(&c->src, c, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1023 | break; |
| 1024 | case SrcMem16: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1025 | c->src.bytes = 2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1026 | goto srcmem_common; |
| 1027 | case SrcMem32: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1028 | c->src.bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1029 | goto srcmem_common; |
| 1030 | case SrcMem: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1031 | c->src.bytes = (c->d & ByteOp) ? 1 : |
| 1032 | c->op_bytes; |
Rusty Russell | b85b9ee9 | 2007-09-09 14:12:54 +0300 | [diff] [blame] | 1033 | /* Don't fetch the address for invlpg: it could be unmapped. */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1034 | if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7) |
Rusty Russell | b85b9ee9 | 2007-09-09 14:12:54 +0300 | [diff] [blame] | 1035 | break; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1036 | srcmem_common: |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1037 | /* |
| 1038 | * For instructions with a ModR/M byte, switch to register |
| 1039 | * access if Mod = 3. |
| 1040 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1041 | if ((c->d & ModRM) && c->modrm_mod == 3) { |
| 1042 | c->src.type = OP_REG; |
Avi Kivity | 66b8550 | 2008-04-14 23:27:07 +0300 | [diff] [blame] | 1043 | c->src.val = c->modrm_val; |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 1044 | c->src.ptr = c->modrm_ptr; |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1045 | break; |
| 1046 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1047 | c->src.type = OP_MEM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1048 | break; |
| 1049 | case SrcImm: |
Avi Kivity | c9eaf20 | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 1050 | case SrcImmU: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1051 | c->src.type = OP_IMM; |
| 1052 | c->src.ptr = (unsigned long *)c->eip; |
| 1053 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1054 | if (c->src.bytes == 8) |
| 1055 | c->src.bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1056 | /* NB. Immediates are sign-extended as necessary. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1057 | switch (c->src.bytes) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1058 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1059 | c->src.val = insn_fetch(s8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1060 | break; |
| 1061 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1062 | c->src.val = insn_fetch(s16, 2, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1063 | break; |
| 1064 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1065 | c->src.val = insn_fetch(s32, 4, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1066 | break; |
| 1067 | } |
Avi Kivity | c9eaf20 | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 1068 | if ((c->d & SrcMask) == SrcImmU) { |
| 1069 | switch (c->src.bytes) { |
| 1070 | case 1: |
| 1071 | c->src.val &= 0xff; |
| 1072 | break; |
| 1073 | case 2: |
| 1074 | c->src.val &= 0xffff; |
| 1075 | break; |
| 1076 | case 4: |
| 1077 | c->src.val &= 0xffffffff; |
| 1078 | break; |
| 1079 | } |
| 1080 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1081 | break; |
| 1082 | case SrcImmByte: |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 1083 | case SrcImmUByte: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1084 | c->src.type = OP_IMM; |
| 1085 | c->src.ptr = (unsigned long *)c->eip; |
| 1086 | c->src.bytes = 1; |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 1087 | if ((c->d & SrcMask) == SrcImmByte) |
| 1088 | c->src.val = insn_fetch(s8, 1, c->eip); |
| 1089 | else |
| 1090 | c->src.val = insn_fetch(u8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1091 | break; |
Guillaume Thouvenin | bfcadf8 | 2008-12-04 14:27:38 +0100 | [diff] [blame] | 1092 | case SrcOne: |
| 1093 | c->src.bytes = 1; |
| 1094 | c->src.val = 1; |
| 1095 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1096 | } |
| 1097 | |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 1098 | /* |
| 1099 | * Decode and fetch the second source operand: register, memory |
| 1100 | * or immediate. |
| 1101 | */ |
| 1102 | switch (c->d & Src2Mask) { |
| 1103 | case Src2None: |
| 1104 | break; |
| 1105 | case Src2CL: |
| 1106 | c->src2.bytes = 1; |
| 1107 | c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8; |
| 1108 | break; |
| 1109 | case Src2ImmByte: |
| 1110 | c->src2.type = OP_IMM; |
| 1111 | c->src2.ptr = (unsigned long *)c->eip; |
| 1112 | c->src2.bytes = 1; |
| 1113 | c->src2.val = insn_fetch(u8, 1, c->eip); |
| 1114 | break; |
Gleb Natapov | a5f868b | 2009-04-12 13:36:14 +0300 | [diff] [blame] | 1115 | case Src2Imm16: |
| 1116 | c->src2.type = OP_IMM; |
| 1117 | c->src2.ptr = (unsigned long *)c->eip; |
| 1118 | c->src2.bytes = 2; |
| 1119 | c->src2.val = insn_fetch(u16, 2, c->eip); |
| 1120 | break; |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 1121 | case Src2One: |
| 1122 | c->src2.bytes = 1; |
| 1123 | c->src2.val = 1; |
| 1124 | break; |
| 1125 | } |
| 1126 | |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1127 | /* Decode and fetch the destination operand: register or memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1128 | switch (c->d & DstMask) { |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1129 | case ImplicitOps: |
| 1130 | /* Special instructions do their own operand decoding. */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1131 | return 0; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1132 | case DstReg: |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 1133 | decode_register_operand(&c->dst, c, |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1134 | c->twobyte && (c->b == 0xb6 || c->b == 0xb7)); |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1135 | break; |
| 1136 | case DstMem: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1137 | if ((c->d & ModRM) && c->modrm_mod == 3) { |
Guillaume Thouvenin | 89c6963 | 2008-05-27 10:22:20 +0200 | [diff] [blame] | 1138 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1139 | c->dst.type = OP_REG; |
Avi Kivity | 66b8550 | 2008-04-14 23:27:07 +0300 | [diff] [blame] | 1140 | c->dst.val = c->dst.orig_val = c->modrm_val; |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 1141 | c->dst.ptr = c->modrm_ptr; |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1142 | break; |
| 1143 | } |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1144 | c->dst.type = OP_MEM; |
| 1145 | break; |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1146 | case DstAcc: |
| 1147 | c->dst.type = OP_REG; |
| 1148 | c->dst.bytes = c->op_bytes; |
| 1149 | c->dst.ptr = &c->regs[VCPU_REGS_RAX]; |
| 1150 | switch (c->op_bytes) { |
| 1151 | case 1: |
| 1152 | c->dst.val = *(u8 *)c->dst.ptr; |
| 1153 | break; |
| 1154 | case 2: |
| 1155 | c->dst.val = *(u16 *)c->dst.ptr; |
| 1156 | break; |
| 1157 | case 4: |
| 1158 | c->dst.val = *(u32 *)c->dst.ptr; |
| 1159 | break; |
| 1160 | } |
| 1161 | c->dst.orig_val = c->dst.val; |
| 1162 | break; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1163 | } |
| 1164 | |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 1165 | if (c->rip_relative) |
| 1166 | c->modrm_ea += c->eip; |
| 1167 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1168 | done: |
| 1169 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
| 1170 | } |
| 1171 | |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1172 | static inline void emulate_push(struct x86_emulate_ctxt *ctxt) |
| 1173 | { |
| 1174 | struct decode_cache *c = &ctxt->decode; |
| 1175 | |
| 1176 | c->dst.type = OP_MEM; |
| 1177 | c->dst.bytes = c->op_bytes; |
| 1178 | c->dst.val = c->src.val; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 1179 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1180 | c->dst.ptr = (void *) register_address(c, ss_base(ctxt), |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1181 | c->regs[VCPU_REGS_RSP]); |
| 1182 | } |
| 1183 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1184 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1185 | struct x86_emulate_ops *ops, |
| 1186 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1187 | { |
| 1188 | struct decode_cache *c = &ctxt->decode; |
| 1189 | int rc; |
| 1190 | |
Avi Kivity | 781d0ed | 2008-11-27 18:00:28 +0200 | [diff] [blame] | 1191 | rc = ops->read_emulated(register_address(c, ss_base(ctxt), |
| 1192 | c->regs[VCPU_REGS_RSP]), |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1193 | dest, len, ctxt->vcpu); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1194 | if (rc != 0) |
| 1195 | return rc; |
| 1196 | |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1197 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1198 | return rc; |
| 1199 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1200 | |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1201 | static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg) |
| 1202 | { |
| 1203 | struct decode_cache *c = &ctxt->decode; |
| 1204 | struct kvm_segment segment; |
| 1205 | |
| 1206 | kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg); |
| 1207 | |
| 1208 | c->src.val = segment.selector; |
| 1209 | emulate_push(ctxt); |
| 1210 | } |
| 1211 | |
| 1212 | static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, |
| 1213 | struct x86_emulate_ops *ops, int seg) |
| 1214 | { |
| 1215 | struct decode_cache *c = &ctxt->decode; |
| 1216 | unsigned long selector; |
| 1217 | int rc; |
| 1218 | |
| 1219 | rc = emulate_pop(ctxt, ops, &selector, c->op_bytes); |
| 1220 | if (rc != 0) |
| 1221 | return rc; |
| 1222 | |
| 1223 | rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)selector, 1, seg); |
| 1224 | return rc; |
| 1225 | } |
| 1226 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1227 | static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt, |
| 1228 | struct x86_emulate_ops *ops) |
| 1229 | { |
| 1230 | struct decode_cache *c = &ctxt->decode; |
| 1231 | int rc; |
| 1232 | |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1233 | rc = emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1234 | if (rc != 0) |
| 1235 | return rc; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1236 | return 0; |
| 1237 | } |
| 1238 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1239 | static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1240 | { |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1241 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1242 | switch (c->modrm_reg) { |
| 1243 | case 0: /* rol */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1244 | emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1245 | break; |
| 1246 | case 1: /* ror */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1247 | emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1248 | break; |
| 1249 | case 2: /* rcl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1250 | emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1251 | break; |
| 1252 | case 3: /* rcr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1253 | emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1254 | break; |
| 1255 | case 4: /* sal/shl */ |
| 1256 | case 6: /* sal/shl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1257 | emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1258 | break; |
| 1259 | case 5: /* shr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1260 | emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1261 | break; |
| 1262 | case 7: /* sar */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1263 | emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1264 | break; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1269 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1270 | { |
| 1271 | struct decode_cache *c = &ctxt->decode; |
| 1272 | int rc = 0; |
| 1273 | |
| 1274 | switch (c->modrm_reg) { |
| 1275 | case 0 ... 1: /* test */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1276 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1277 | break; |
| 1278 | case 2: /* not */ |
| 1279 | c->dst.val = ~c->dst.val; |
| 1280 | break; |
| 1281 | case 3: /* neg */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1282 | emulate_1op("neg", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1283 | break; |
| 1284 | default: |
| 1285 | DPRINTF("Cannot emulate %02x\n", c->b); |
| 1286 | rc = X86EMUL_UNHANDLEABLE; |
| 1287 | break; |
| 1288 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1289 | return rc; |
| 1290 | } |
| 1291 | |
| 1292 | static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 1293 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1294 | { |
| 1295 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1296 | |
| 1297 | switch (c->modrm_reg) { |
| 1298 | case 0: /* inc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1299 | emulate_1op("inc", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1300 | break; |
| 1301 | case 1: /* dec */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1302 | emulate_1op("dec", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1303 | break; |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1304 | case 2: /* call near abs */ { |
| 1305 | long int old_eip; |
| 1306 | old_eip = c->eip; |
| 1307 | c->eip = c->src.val; |
| 1308 | c->src.val = old_eip; |
| 1309 | emulate_push(ctxt); |
| 1310 | break; |
| 1311 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1312 | case 4: /* jmp abs */ |
Avi Kivity | fd60754 | 2008-01-18 13:12:26 +0200 | [diff] [blame] | 1313 | c->eip = c->src.val; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1314 | break; |
| 1315 | case 6: /* push */ |
Avi Kivity | fd60754 | 2008-01-18 13:12:26 +0200 | [diff] [blame] | 1316 | emulate_push(ctxt); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1317 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1318 | } |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
| 1322 | static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt, |
| 1323 | struct x86_emulate_ops *ops, |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1324 | unsigned long memop) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1325 | { |
| 1326 | struct decode_cache *c = &ctxt->decode; |
| 1327 | u64 old, new; |
| 1328 | int rc; |
| 1329 | |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1330 | rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1331 | if (rc != 0) |
| 1332 | return rc; |
| 1333 | |
| 1334 | if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) || |
| 1335 | ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) { |
| 1336 | |
| 1337 | c->regs[VCPU_REGS_RAX] = (u32) (old >> 0); |
| 1338 | c->regs[VCPU_REGS_RDX] = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1339 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1340 | |
| 1341 | } else { |
| 1342 | new = ((u64)c->regs[VCPU_REGS_RCX] << 32) | |
| 1343 | (u32) c->regs[VCPU_REGS_RBX]; |
| 1344 | |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1345 | rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1346 | if (rc != 0) |
| 1347 | return rc; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1348 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1349 | } |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1353 | static int emulate_ret_far(struct x86_emulate_ctxt *ctxt, |
| 1354 | struct x86_emulate_ops *ops) |
| 1355 | { |
| 1356 | struct decode_cache *c = &ctxt->decode; |
| 1357 | int rc; |
| 1358 | unsigned long cs; |
| 1359 | |
| 1360 | rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes); |
| 1361 | if (rc) |
| 1362 | return rc; |
| 1363 | if (c->op_bytes == 4) |
| 1364 | c->eip = (u32)c->eip; |
| 1365 | rc = emulate_pop(ctxt, ops, &cs, c->op_bytes); |
| 1366 | if (rc) |
| 1367 | return rc; |
| 1368 | rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, 1, VCPU_SREG_CS); |
| 1369 | return rc; |
| 1370 | } |
| 1371 | |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1372 | static inline int writeback(struct x86_emulate_ctxt *ctxt, |
| 1373 | struct x86_emulate_ops *ops) |
| 1374 | { |
| 1375 | int rc; |
| 1376 | struct decode_cache *c = &ctxt->decode; |
| 1377 | |
| 1378 | switch (c->dst.type) { |
| 1379 | case OP_REG: |
| 1380 | /* The 4-byte case *is* correct: |
| 1381 | * in 64-bit mode we zero-extend. |
| 1382 | */ |
| 1383 | switch (c->dst.bytes) { |
| 1384 | case 1: |
| 1385 | *(u8 *)c->dst.ptr = (u8)c->dst.val; |
| 1386 | break; |
| 1387 | case 2: |
| 1388 | *(u16 *)c->dst.ptr = (u16)c->dst.val; |
| 1389 | break; |
| 1390 | case 4: |
| 1391 | *c->dst.ptr = (u32)c->dst.val; |
| 1392 | break; /* 64b: zero-ext */ |
| 1393 | case 8: |
| 1394 | *c->dst.ptr = c->dst.val; |
| 1395 | break; |
| 1396 | } |
| 1397 | break; |
| 1398 | case OP_MEM: |
| 1399 | if (c->lock_prefix) |
| 1400 | rc = ops->cmpxchg_emulated( |
| 1401 | (unsigned long)c->dst.ptr, |
| 1402 | &c->dst.orig_val, |
| 1403 | &c->dst.val, |
| 1404 | c->dst.bytes, |
| 1405 | ctxt->vcpu); |
| 1406 | else |
| 1407 | rc = ops->write_emulated( |
| 1408 | (unsigned long)c->dst.ptr, |
| 1409 | &c->dst.val, |
| 1410 | c->dst.bytes, |
| 1411 | ctxt->vcpu); |
| 1412 | if (rc != 0) |
| 1413 | return rc; |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 1414 | break; |
| 1415 | case OP_NONE: |
| 1416 | /* no writeback */ |
| 1417 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1418 | default: |
| 1419 | break; |
| 1420 | } |
| 1421 | return 0; |
| 1422 | } |
| 1423 | |
Jaswinder Singh Rajput | a3f9d39 | 2009-06-18 16:53:25 +0530 | [diff] [blame] | 1424 | static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask) |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 1425 | { |
| 1426 | u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask); |
| 1427 | /* |
| 1428 | * an sti; sti; sequence only disable interrupts for the first |
| 1429 | * instruction. So, if the last instruction, be it emulated or |
| 1430 | * not, left the system with the INT_STI flag enabled, it |
| 1431 | * means that the last instruction is an sti. We should not |
| 1432 | * leave the flag on in this case. The same goes for mov ss |
| 1433 | */ |
| 1434 | if (!(int_shadow & mask)) |
| 1435 | ctxt->interruptibility = mask; |
| 1436 | } |
| 1437 | |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1438 | static inline void |
| 1439 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
| 1440 | struct kvm_segment *cs, struct kvm_segment *ss) |
| 1441 | { |
| 1442 | memset(cs, 0, sizeof(struct kvm_segment)); |
| 1443 | kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS); |
| 1444 | memset(ss, 0, sizeof(struct kvm_segment)); |
| 1445 | |
| 1446 | cs->l = 0; /* will be adjusted later */ |
| 1447 | cs->base = 0; /* flat segment */ |
| 1448 | cs->g = 1; /* 4kb granularity */ |
| 1449 | cs->limit = 0xffffffff; /* 4GB limit */ |
| 1450 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 1451 | cs->s = 1; |
| 1452 | cs->dpl = 0; /* will be adjusted later */ |
| 1453 | cs->present = 1; |
| 1454 | cs->db = 1; |
| 1455 | |
| 1456 | ss->unusable = 0; |
| 1457 | ss->base = 0; /* flat segment */ |
| 1458 | ss->limit = 0xffffffff; /* 4GB limit */ |
| 1459 | ss->g = 1; /* 4kb granularity */ |
| 1460 | ss->s = 1; |
| 1461 | ss->type = 0x03; /* Read/Write, Accessed */ |
| 1462 | ss->db = 1; /* 32bit stack segment */ |
| 1463 | ss->dpl = 0; |
| 1464 | ss->present = 1; |
| 1465 | } |
| 1466 | |
| 1467 | static int |
| 1468 | emulate_syscall(struct x86_emulate_ctxt *ctxt) |
| 1469 | { |
| 1470 | struct decode_cache *c = &ctxt->decode; |
| 1471 | struct kvm_segment cs, ss; |
| 1472 | u64 msr_data; |
| 1473 | |
| 1474 | /* syscall is not available in real mode */ |
| 1475 | if (c->lock_prefix || ctxt->mode == X86EMUL_MODE_REAL |
| 1476 | || !(ctxt->vcpu->arch.cr0 & X86_CR0_PE)) |
| 1477 | return -1; |
| 1478 | |
| 1479 | setup_syscalls_segments(ctxt, &cs, &ss); |
| 1480 | |
| 1481 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
| 1482 | msr_data >>= 32; |
| 1483 | cs.selector = (u16)(msr_data & 0xfffc); |
| 1484 | ss.selector = (u16)(msr_data + 8); |
| 1485 | |
| 1486 | if (is_long_mode(ctxt->vcpu)) { |
| 1487 | cs.db = 0; |
| 1488 | cs.l = 1; |
| 1489 | } |
| 1490 | kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS); |
| 1491 | kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS); |
| 1492 | |
| 1493 | c->regs[VCPU_REGS_RCX] = c->eip; |
| 1494 | if (is_long_mode(ctxt->vcpu)) { |
| 1495 | #ifdef CONFIG_X86_64 |
| 1496 | c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF; |
| 1497 | |
| 1498 | kvm_x86_ops->get_msr(ctxt->vcpu, |
| 1499 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 1500 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
| 1501 | c->eip = msr_data; |
| 1502 | |
| 1503 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data); |
| 1504 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
| 1505 | #endif |
| 1506 | } else { |
| 1507 | /* legacy mode */ |
| 1508 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
| 1509 | c->eip = (u32)msr_data; |
| 1510 | |
| 1511 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 1512 | } |
| 1513 | |
| 1514 | return 0; |
| 1515 | } |
| 1516 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1517 | static int |
| 1518 | emulate_sysenter(struct x86_emulate_ctxt *ctxt) |
| 1519 | { |
| 1520 | struct decode_cache *c = &ctxt->decode; |
| 1521 | struct kvm_segment cs, ss; |
| 1522 | u64 msr_data; |
| 1523 | |
| 1524 | /* inject #UD if LOCK prefix is used */ |
| 1525 | if (c->lock_prefix) |
| 1526 | return -1; |
| 1527 | |
| 1528 | /* inject #GP if in real mode or paging is disabled */ |
| 1529 | if (ctxt->mode == X86EMUL_MODE_REAL || |
| 1530 | !(ctxt->vcpu->arch.cr0 & X86_CR0_PE)) { |
| 1531 | kvm_inject_gp(ctxt->vcpu, 0); |
| 1532 | return -1; |
| 1533 | } |
| 1534 | |
| 1535 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 1536 | * Therefore, we inject an #UD. |
| 1537 | */ |
| 1538 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 1539 | return -1; |
| 1540 | |
| 1541 | setup_syscalls_segments(ctxt, &cs, &ss); |
| 1542 | |
| 1543 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
| 1544 | switch (ctxt->mode) { |
| 1545 | case X86EMUL_MODE_PROT32: |
| 1546 | if ((msr_data & 0xfffc) == 0x0) { |
| 1547 | kvm_inject_gp(ctxt->vcpu, 0); |
| 1548 | return -1; |
| 1549 | } |
| 1550 | break; |
| 1551 | case X86EMUL_MODE_PROT64: |
| 1552 | if (msr_data == 0x0) { |
| 1553 | kvm_inject_gp(ctxt->vcpu, 0); |
| 1554 | return -1; |
| 1555 | } |
| 1556 | break; |
| 1557 | } |
| 1558 | |
| 1559 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 1560 | cs.selector = (u16)msr_data; |
| 1561 | cs.selector &= ~SELECTOR_RPL_MASK; |
| 1562 | ss.selector = cs.selector + 8; |
| 1563 | ss.selector &= ~SELECTOR_RPL_MASK; |
| 1564 | if (ctxt->mode == X86EMUL_MODE_PROT64 |
| 1565 | || is_long_mode(ctxt->vcpu)) { |
| 1566 | cs.db = 0; |
| 1567 | cs.l = 1; |
| 1568 | } |
| 1569 | |
| 1570 | kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS); |
| 1571 | kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS); |
| 1572 | |
| 1573 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data); |
| 1574 | c->eip = msr_data; |
| 1575 | |
| 1576 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data); |
| 1577 | c->regs[VCPU_REGS_RSP] = msr_data; |
| 1578 | |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1582 | static int |
| 1583 | emulate_sysexit(struct x86_emulate_ctxt *ctxt) |
| 1584 | { |
| 1585 | struct decode_cache *c = &ctxt->decode; |
| 1586 | struct kvm_segment cs, ss; |
| 1587 | u64 msr_data; |
| 1588 | int usermode; |
| 1589 | |
| 1590 | /* inject #UD if LOCK prefix is used */ |
| 1591 | if (c->lock_prefix) |
| 1592 | return -1; |
| 1593 | |
| 1594 | /* inject #GP if in real mode or paging is disabled */ |
| 1595 | if (ctxt->mode == X86EMUL_MODE_REAL |
| 1596 | || !(ctxt->vcpu->arch.cr0 & X86_CR0_PE)) { |
| 1597 | kvm_inject_gp(ctxt->vcpu, 0); |
| 1598 | return -1; |
| 1599 | } |
| 1600 | |
| 1601 | /* sysexit must be called from CPL 0 */ |
| 1602 | if (kvm_x86_ops->get_cpl(ctxt->vcpu) != 0) { |
| 1603 | kvm_inject_gp(ctxt->vcpu, 0); |
| 1604 | return -1; |
| 1605 | } |
| 1606 | |
| 1607 | setup_syscalls_segments(ctxt, &cs, &ss); |
| 1608 | |
| 1609 | if ((c->rex_prefix & 0x8) != 0x0) |
| 1610 | usermode = X86EMUL_MODE_PROT64; |
| 1611 | else |
| 1612 | usermode = X86EMUL_MODE_PROT32; |
| 1613 | |
| 1614 | cs.dpl = 3; |
| 1615 | ss.dpl = 3; |
| 1616 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
| 1617 | switch (usermode) { |
| 1618 | case X86EMUL_MODE_PROT32: |
| 1619 | cs.selector = (u16)(msr_data + 16); |
| 1620 | if ((msr_data & 0xfffc) == 0x0) { |
| 1621 | kvm_inject_gp(ctxt->vcpu, 0); |
| 1622 | return -1; |
| 1623 | } |
| 1624 | ss.selector = (u16)(msr_data + 24); |
| 1625 | break; |
| 1626 | case X86EMUL_MODE_PROT64: |
| 1627 | cs.selector = (u16)(msr_data + 32); |
| 1628 | if (msr_data == 0x0) { |
| 1629 | kvm_inject_gp(ctxt->vcpu, 0); |
| 1630 | return -1; |
| 1631 | } |
| 1632 | ss.selector = cs.selector + 8; |
| 1633 | cs.db = 0; |
| 1634 | cs.l = 1; |
| 1635 | break; |
| 1636 | } |
| 1637 | cs.selector |= SELECTOR_RPL_MASK; |
| 1638 | ss.selector |= SELECTOR_RPL_MASK; |
| 1639 | |
| 1640 | kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS); |
| 1641 | kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS); |
| 1642 | |
| 1643 | c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX]; |
| 1644 | c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX]; |
| 1645 | |
| 1646 | return 0; |
| 1647 | } |
| 1648 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1649 | int |
Laurent Vivier | 1be3aa4 | 2007-09-18 11:27:27 +0200 | [diff] [blame] | 1650 | 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] | 1651 | { |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1652 | unsigned long memop = 0; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1653 | u64 msr_data; |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1654 | unsigned long saved_eip = 0; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1655 | struct decode_cache *c = &ctxt->decode; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 1656 | unsigned int port; |
| 1657 | int io_dir_in; |
Laurent Vivier | 1be3aa4 | 2007-09-18 11:27:27 +0200 | [diff] [blame] | 1658 | int rc = 0; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1659 | |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 1660 | ctxt->interruptibility = 0; |
| 1661 | |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1662 | /* Shadow copy of register state. Committed on successful emulation. |
| 1663 | * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't |
| 1664 | * modify them. |
| 1665 | */ |
| 1666 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1667 | memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs); |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1668 | saved_eip = c->eip; |
| 1669 | |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 1670 | if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs)) |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1671 | memop = c->modrm_ea; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1672 | |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 1673 | if (c->rep_prefix && (c->d & String)) { |
| 1674 | /* All REP prefixes have the same first termination condition */ |
| 1675 | if (c->regs[VCPU_REGS_RCX] == 0) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1676 | kvm_rip_write(ctxt->vcpu, c->eip); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 1677 | goto done; |
| 1678 | } |
| 1679 | /* The second termination condition only applies for REPE |
| 1680 | * and REPNE. Test if the repeat string operation prefix is |
| 1681 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 1682 | * corresponding termination condition according to: |
| 1683 | * - if REPE/REPZ and ZF = 0 then done |
| 1684 | * - if REPNE/REPNZ and ZF = 1 then done |
| 1685 | */ |
| 1686 | if ((c->b == 0xa6) || (c->b == 0xa7) || |
| 1687 | (c->b == 0xae) || (c->b == 0xaf)) { |
| 1688 | if ((c->rep_prefix == REPE_PREFIX) && |
| 1689 | ((ctxt->eflags & EFLG_ZF) == 0)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1690 | kvm_rip_write(ctxt->vcpu, c->eip); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 1691 | goto done; |
| 1692 | } |
| 1693 | if ((c->rep_prefix == REPNE_PREFIX) && |
| 1694 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1695 | kvm_rip_write(ctxt->vcpu, c->eip); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 1696 | goto done; |
| 1697 | } |
| 1698 | } |
| 1699 | c->regs[VCPU_REGS_RCX]--; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1700 | c->eip = kvm_rip_read(ctxt->vcpu); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 1701 | } |
| 1702 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1703 | if (c->src.type == OP_MEM) { |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1704 | c->src.ptr = (unsigned long *)memop; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1705 | c->src.val = 0; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1706 | rc = ops->read_emulated((unsigned long)c->src.ptr, |
| 1707 | &c->src.val, |
| 1708 | c->src.bytes, |
| 1709 | ctxt->vcpu); |
| 1710 | if (rc != 0) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1711 | goto done; |
| 1712 | c->src.orig_val = c->src.val; |
| 1713 | } |
| 1714 | |
| 1715 | if ((c->d & DstMask) == ImplicitOps) |
| 1716 | goto special_insn; |
| 1717 | |
| 1718 | |
| 1719 | if (c->dst.type == OP_MEM) { |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1720 | c->dst.ptr = (unsigned long *)memop; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1721 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1722 | c->dst.val = 0; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1723 | if (c->d & BitOp) { |
| 1724 | unsigned long mask = ~(c->dst.bytes * 8 - 1); |
Avi Kivity | df513e2 | 2007-03-28 20:04:16 +0200 | [diff] [blame] | 1725 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1726 | c->dst.ptr = (void *)c->dst.ptr + |
| 1727 | (c->src.val & mask) / 8; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1728 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1729 | if (!(c->d & Mov) && |
| 1730 | /* optimisation - avoid slow emulated read */ |
| 1731 | ((rc = ops->read_emulated((unsigned long)c->dst.ptr, |
| 1732 | &c->dst.val, |
| 1733 | c->dst.bytes, ctxt->vcpu)) != 0)) |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1734 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1735 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1736 | c->dst.orig_val = c->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1737 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 1738 | special_insn: |
| 1739 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1740 | if (c->twobyte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1741 | goto twobyte_insn; |
| 1742 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1743 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1744 | case 0x00 ... 0x05: |
| 1745 | add: /* add */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1746 | emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1747 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1748 | case 0x06: /* push es */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1749 | emulate_push_sreg(ctxt, VCPU_SREG_ES); |
| 1750 | break; |
| 1751 | case 0x07: /* pop es */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1752 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES); |
| 1753 | if (rc != 0) |
| 1754 | goto done; |
| 1755 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1756 | case 0x08 ... 0x0d: |
| 1757 | or: /* or */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1758 | emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1759 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1760 | case 0x0e: /* push cs */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1761 | emulate_push_sreg(ctxt, VCPU_SREG_CS); |
| 1762 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1763 | case 0x10 ... 0x15: |
| 1764 | adc: /* adc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1765 | emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1766 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1767 | case 0x16: /* push ss */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1768 | emulate_push_sreg(ctxt, VCPU_SREG_SS); |
| 1769 | break; |
| 1770 | case 0x17: /* pop ss */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1771 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS); |
| 1772 | if (rc != 0) |
| 1773 | goto done; |
| 1774 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1775 | case 0x18 ... 0x1d: |
| 1776 | sbb: /* sbb */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1777 | emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1778 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1779 | case 0x1e: /* push ds */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1780 | emulate_push_sreg(ctxt, VCPU_SREG_DS); |
| 1781 | break; |
| 1782 | case 0x1f: /* pop ds */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1783 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS); |
| 1784 | if (rc != 0) |
| 1785 | goto done; |
| 1786 | break; |
Guillaume Thouvenin | aa3a816 | 2008-09-12 13:52:18 +0200 | [diff] [blame] | 1787 | case 0x20 ... 0x25: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1788 | and: /* and */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1789 | emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1790 | break; |
| 1791 | case 0x28 ... 0x2d: |
| 1792 | sub: /* sub */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1793 | emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1794 | break; |
| 1795 | case 0x30 ... 0x35: |
| 1796 | xor: /* xor */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1797 | emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1798 | break; |
| 1799 | case 0x38 ... 0x3d: |
| 1800 | cmp: /* cmp */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1801 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1802 | break; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1803 | case 0x40 ... 0x47: /* inc r16/r32 */ |
| 1804 | emulate_1op("inc", c->dst, ctxt->eflags); |
| 1805 | break; |
| 1806 | case 0x48 ... 0x4f: /* dec r16/r32 */ |
| 1807 | emulate_1op("dec", c->dst, ctxt->eflags); |
| 1808 | break; |
| 1809 | case 0x50 ... 0x57: /* push reg */ |
Guillaume Thouvenin | 2786b01 | 2008-09-22 16:08:06 +0200 | [diff] [blame] | 1810 | emulate_push(ctxt); |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1811 | break; |
| 1812 | case 0x58 ... 0x5f: /* pop reg */ |
| 1813 | pop_instruction: |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1814 | rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes); |
Avi Kivity | 8a09b68 | 2008-11-27 18:06:33 +0200 | [diff] [blame] | 1815 | if (rc != 0) |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1816 | goto done; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1817 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1818 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1819 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1820 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1821 | c->dst.val = (s32) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1822 | break; |
Avi Kivity | 91ed7a0 | 2008-05-29 14:38:38 +0300 | [diff] [blame] | 1823 | case 0x68: /* push imm */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 1824 | case 0x6a: /* push imm8 */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 1825 | emulate_push(ctxt); |
| 1826 | break; |
| 1827 | case 0x6c: /* insb */ |
| 1828 | case 0x6d: /* insw/insd */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame^] | 1829 | if (kvm_emulate_pio_string(ctxt->vcpu, |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 1830 | 1, |
| 1831 | (c->d & ByteOp) ? 1 : c->op_bytes, |
| 1832 | c->rep_prefix ? |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 1833 | address_mask(c, c->regs[VCPU_REGS_RCX]) : 1, |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 1834 | (ctxt->eflags & EFLG_DF), |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1835 | register_address(c, es_base(ctxt), |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 1836 | c->regs[VCPU_REGS_RDI]), |
| 1837 | c->rep_prefix, |
| 1838 | c->regs[VCPU_REGS_RDX]) == 0) { |
| 1839 | c->eip = saved_eip; |
| 1840 | return -1; |
| 1841 | } |
| 1842 | return 0; |
| 1843 | case 0x6e: /* outsb */ |
| 1844 | case 0x6f: /* outsw/outsd */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame^] | 1845 | if (kvm_emulate_pio_string(ctxt->vcpu, |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 1846 | 0, |
| 1847 | (c->d & ByteOp) ? 1 : c->op_bytes, |
| 1848 | c->rep_prefix ? |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 1849 | address_mask(c, c->regs[VCPU_REGS_RCX]) : 1, |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 1850 | (ctxt->eflags & EFLG_DF), |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1851 | register_address(c, |
| 1852 | seg_override_base(ctxt, c), |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 1853 | c->regs[VCPU_REGS_RSI]), |
| 1854 | c->rep_prefix, |
| 1855 | c->regs[VCPU_REGS_RDX]) == 0) { |
| 1856 | c->eip = saved_eip; |
| 1857 | return -1; |
| 1858 | } |
| 1859 | return 0; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 1860 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 1861 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 1862 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 1863 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1864 | case 0x80 ... 0x83: /* Grp1 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1865 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1866 | case 0: |
| 1867 | goto add; |
| 1868 | case 1: |
| 1869 | goto or; |
| 1870 | case 2: |
| 1871 | goto adc; |
| 1872 | case 3: |
| 1873 | goto sbb; |
| 1874 | case 4: |
| 1875 | goto and; |
| 1876 | case 5: |
| 1877 | goto sub; |
| 1878 | case 6: |
| 1879 | goto xor; |
| 1880 | case 7: |
| 1881 | goto cmp; |
| 1882 | } |
| 1883 | break; |
| 1884 | case 0x84 ... 0x85: |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1885 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1886 | break; |
| 1887 | case 0x86 ... 0x87: /* xchg */ |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 1888 | xchg: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1889 | /* Write back the register source. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1890 | switch (c->dst.bytes) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1891 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1892 | *(u8 *) c->src.ptr = (u8) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1893 | break; |
| 1894 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1895 | *(u16 *) c->src.ptr = (u16) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1896 | break; |
| 1897 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1898 | *c->src.ptr = (u32) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1899 | break; /* 64b reg: zero-extend */ |
| 1900 | case 8: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1901 | *c->src.ptr = c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1902 | break; |
| 1903 | } |
| 1904 | /* |
| 1905 | * Write back the memory destination with implicit LOCK |
| 1906 | * prefix. |
| 1907 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1908 | c->dst.val = c->src.val; |
| 1909 | c->lock_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1910 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1911 | case 0x88 ... 0x8b: /* mov */ |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1912 | goto mov; |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 1913 | case 0x8c: { /* mov r/m, sreg */ |
| 1914 | struct kvm_segment segreg; |
| 1915 | |
| 1916 | if (c->modrm_reg <= 5) |
| 1917 | kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg); |
| 1918 | else { |
| 1919 | printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n", |
| 1920 | c->modrm); |
| 1921 | goto cannot_emulate; |
| 1922 | } |
| 1923 | c->dst.val = segreg.selector; |
| 1924 | break; |
| 1925 | } |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 1926 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | f9b7aab | 2008-04-14 23:46:37 +0300 | [diff] [blame] | 1927 | c->dst.val = c->modrm_ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 1928 | break; |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 1929 | case 0x8e: { /* mov seg, r/m16 */ |
| 1930 | uint16_t sel; |
| 1931 | int type_bits; |
| 1932 | int err; |
| 1933 | |
| 1934 | sel = c->src.val; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 1935 | if (c->modrm_reg == VCPU_SREG_SS) |
| 1936 | toggle_interruptibility(ctxt, X86_SHADOW_INT_MOV_SS); |
| 1937 | |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 1938 | if (c->modrm_reg <= 5) { |
| 1939 | type_bits = (c->modrm_reg == 1) ? 9 : 1; |
| 1940 | err = kvm_load_segment_descriptor(ctxt->vcpu, sel, |
| 1941 | type_bits, c->modrm_reg); |
| 1942 | } else { |
| 1943 | printk(KERN_INFO "Invalid segreg in modrm byte 0x%02x\n", |
| 1944 | c->modrm); |
| 1945 | goto cannot_emulate; |
| 1946 | } |
| 1947 | |
| 1948 | if (err < 0) |
| 1949 | goto cannot_emulate; |
| 1950 | |
| 1951 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 1952 | break; |
| 1953 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1954 | case 0x8f: /* pop (sole member of Grp1a) */ |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1955 | rc = emulate_grp1a(ctxt, ops); |
| 1956 | if (rc != 0) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1957 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1958 | break; |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 1959 | case 0x90: /* nop / xchg r8,rax */ |
| 1960 | if (!(c->rex_prefix & 1)) { /* nop */ |
| 1961 | c->dst.type = OP_NONE; |
| 1962 | break; |
| 1963 | } |
| 1964 | case 0x91 ... 0x97: /* xchg reg,rax */ |
| 1965 | c->src.type = c->dst.type = OP_REG; |
| 1966 | c->src.bytes = c->dst.bytes = c->op_bytes; |
| 1967 | c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX]; |
| 1968 | c->src.val = *(c->src.ptr); |
| 1969 | goto xchg; |
Nitin A Kamble | fd2a760 | 2007-08-28 18:22:47 -0700 | [diff] [blame] | 1970 | case 0x9c: /* pushf */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1971 | c->src.val = (unsigned long) ctxt->eflags; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1972 | emulate_push(ctxt); |
| 1973 | break; |
Nitin A Kamble | 535eabc | 2007-09-15 10:45:05 +0300 | [diff] [blame] | 1974 | case 0x9d: /* popf */ |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 1975 | c->dst.type = OP_REG; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1976 | c->dst.ptr = (unsigned long *) &ctxt->eflags; |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 1977 | c->dst.bytes = c->op_bytes; |
Nitin A Kamble | 535eabc | 2007-09-15 10:45:05 +0300 | [diff] [blame] | 1978 | goto pop_instruction; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 1979 | case 0xa0 ... 0xa1: /* mov */ |
| 1980 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
| 1981 | c->dst.val = c->src.val; |
| 1982 | break; |
| 1983 | case 0xa2 ... 0xa3: /* mov */ |
| 1984 | c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX]; |
| 1985 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1986 | case 0xa4 ... 0xa5: /* movs */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1987 | c->dst.type = OP_MEM; |
| 1988 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 1989 | c->dst.ptr = (unsigned long *)register_address(c, |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1990 | es_base(ctxt), |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1991 | c->regs[VCPU_REGS_RDI]); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 1992 | if ((rc = ops->read_emulated(register_address(c, |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1993 | seg_override_base(ctxt, c), |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1994 | c->regs[VCPU_REGS_RSI]), |
| 1995 | &c->dst.val, |
| 1996 | c->dst.bytes, ctxt->vcpu)) != 0) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1997 | goto done; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 1998 | register_address_increment(c, &c->regs[VCPU_REGS_RSI], |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1999 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2000 | : c->dst.bytes); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2001 | register_address_increment(c, &c->regs[VCPU_REGS_RDI], |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2002 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2003 | : c->dst.bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2004 | break; |
| 2005 | case 0xa6 ... 0xa7: /* cmps */ |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2006 | c->src.type = OP_NONE; /* Disable writeback. */ |
| 2007 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 2008 | c->src.ptr = (unsigned long *)register_address(c, |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 2009 | seg_override_base(ctxt, c), |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2010 | c->regs[VCPU_REGS_RSI]); |
| 2011 | if ((rc = ops->read_emulated((unsigned long)c->src.ptr, |
| 2012 | &c->src.val, |
| 2013 | c->src.bytes, |
| 2014 | ctxt->vcpu)) != 0) |
| 2015 | goto done; |
| 2016 | |
| 2017 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2018 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 2019 | c->dst.ptr = (unsigned long *)register_address(c, |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 2020 | es_base(ctxt), |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2021 | c->regs[VCPU_REGS_RDI]); |
| 2022 | if ((rc = ops->read_emulated((unsigned long)c->dst.ptr, |
| 2023 | &c->dst.val, |
| 2024 | c->dst.bytes, |
| 2025 | ctxt->vcpu)) != 0) |
| 2026 | goto done; |
| 2027 | |
| 2028 | DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr); |
| 2029 | |
| 2030 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
| 2031 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2032 | register_address_increment(c, &c->regs[VCPU_REGS_RSI], |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2033 | (ctxt->eflags & EFLG_DF) ? -c->src.bytes |
| 2034 | : c->src.bytes); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2035 | register_address_increment(c, &c->regs[VCPU_REGS_RDI], |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2036 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
| 2037 | : c->dst.bytes); |
| 2038 | |
| 2039 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2040 | case 0xaa ... 0xab: /* stos */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2041 | c->dst.type = OP_MEM; |
| 2042 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 2043 | c->dst.ptr = (unsigned long *)register_address(c, |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 2044 | es_base(ctxt), |
Sheng Yang | a7e6c88 | 2007-11-15 14:52:28 +0800 | [diff] [blame] | 2045 | c->regs[VCPU_REGS_RDI]); |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2046 | c->dst.val = c->regs[VCPU_REGS_RAX]; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2047 | register_address_increment(c, &c->regs[VCPU_REGS_RDI], |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2048 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2049 | : c->dst.bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2050 | break; |
| 2051 | case 0xac ... 0xad: /* lods */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2052 | c->dst.type = OP_REG; |
| 2053 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 2054 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 2055 | if ((rc = ops->read_emulated(register_address(c, |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 2056 | seg_override_base(ctxt, c), |
Sheng Yang | a7e6c88 | 2007-11-15 14:52:28 +0800 | [diff] [blame] | 2057 | c->regs[VCPU_REGS_RSI]), |
| 2058 | &c->dst.val, |
| 2059 | c->dst.bytes, |
| 2060 | ctxt->vcpu)) != 0) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2061 | goto done; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2062 | register_address_increment(c, &c->regs[VCPU_REGS_RSI], |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2063 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2064 | : c->dst.bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2065 | break; |
| 2066 | case 0xae ... 0xaf: /* scas */ |
| 2067 | DPRINTF("Urk! I don't handle SCAS.\n"); |
| 2068 | goto cannot_emulate; |
Mohammed Gamal | a5e2e82 | 2008-08-27 05:02:56 +0300 | [diff] [blame] | 2069 | case 0xb0 ... 0xbf: /* mov r, imm */ |
Guillaume Thouvenin | 615ac12 | 2008-05-27 10:19:16 +0200 | [diff] [blame] | 2070 | goto mov; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2071 | case 0xc0 ... 0xc1: |
| 2072 | emulate_grp2(ctxt); |
| 2073 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2074 | case 0xc3: /* ret */ |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 2075 | c->dst.type = OP_REG; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2076 | c->dst.ptr = &c->eip; |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 2077 | c->dst.bytes = c->op_bytes; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2078 | goto pop_instruction; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2079 | case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */ |
| 2080 | mov: |
| 2081 | c->dst.val = c->src.val; |
| 2082 | break; |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2083 | case 0xcb: /* ret far */ |
| 2084 | rc = emulate_ret_far(ctxt, ops); |
| 2085 | if (rc) |
| 2086 | goto done; |
| 2087 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2088 | case 0xd0 ... 0xd1: /* Grp2 */ |
| 2089 | c->src.val = 1; |
| 2090 | emulate_grp2(ctxt); |
| 2091 | break; |
| 2092 | case 0xd2 ... 0xd3: /* Grp2 */ |
| 2093 | c->src.val = c->regs[VCPU_REGS_RCX]; |
| 2094 | emulate_grp2(ctxt); |
| 2095 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2096 | case 0xe4: /* inb */ |
| 2097 | case 0xe5: /* in */ |
Gleb Natapov | 84ce66a | 2009-04-12 13:36:46 +0300 | [diff] [blame] | 2098 | port = c->src.val; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2099 | io_dir_in = 1; |
| 2100 | goto do_io; |
| 2101 | case 0xe6: /* outb */ |
| 2102 | case 0xe7: /* out */ |
Gleb Natapov | 84ce66a | 2009-04-12 13:36:46 +0300 | [diff] [blame] | 2103 | port = c->src.val; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2104 | io_dir_in = 0; |
| 2105 | goto do_io; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 2106 | case 0xe8: /* call (near) */ { |
Gleb Natapov | d53c477 | 2009-04-12 13:36:36 +0300 | [diff] [blame] | 2107 | long int rel = c->src.val; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2108 | c->src.val = (unsigned long) c->eip; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2109 | jmp_rel(c, rel); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2110 | emulate_push(ctxt); |
| 2111 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 2112 | } |
| 2113 | case 0xe9: /* jmp rel */ |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 2114 | goto jmp; |
Gleb Natapov | 782b877 | 2009-04-12 13:36:25 +0300 | [diff] [blame] | 2115 | case 0xea: /* jmp far */ |
| 2116 | if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val, 9, |
| 2117 | VCPU_SREG_CS) < 0) { |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 2118 | DPRINTF("jmp far: Failed to load CS descriptor\n"); |
| 2119 | goto cannot_emulate; |
| 2120 | } |
| 2121 | |
Gleb Natapov | 782b877 | 2009-04-12 13:36:25 +0300 | [diff] [blame] | 2122 | c->eip = c->src.val; |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 2123 | break; |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 2124 | case 0xeb: |
| 2125 | jmp: /* jmp rel short */ |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2126 | jmp_rel(c, c->src.val); |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 2127 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 2128 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2129 | case 0xec: /* in al,dx */ |
| 2130 | case 0xed: /* in (e/r)ax,dx */ |
| 2131 | port = c->regs[VCPU_REGS_RDX]; |
| 2132 | io_dir_in = 1; |
| 2133 | goto do_io; |
| 2134 | case 0xee: /* out al,dx */ |
| 2135 | case 0xef: /* out (e/r)ax,dx */ |
| 2136 | port = c->regs[VCPU_REGS_RDX]; |
| 2137 | io_dir_in = 0; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame^] | 2138 | do_io: if (kvm_emulate_pio(ctxt->vcpu, io_dir_in, |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2139 | (c->d & ByteOp) ? 1 : c->op_bytes, |
| 2140 | port) != 0) { |
| 2141 | c->eip = saved_eip; |
| 2142 | goto cannot_emulate; |
| 2143 | } |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 2144 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2145 | case 0xf4: /* hlt */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2146 | ctxt->vcpu->arch.halt_request = 1; |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 2147 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2148 | case 0xf5: /* cmc */ |
| 2149 | /* complement carry flag from eflags reg */ |
| 2150 | ctxt->eflags ^= EFLG_CF; |
| 2151 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2152 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2153 | case 0xf6 ... 0xf7: /* Grp3 */ |
| 2154 | rc = emulate_grp3(ctxt, ops); |
| 2155 | if (rc != 0) |
| 2156 | goto done; |
| 2157 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2158 | case 0xf8: /* clc */ |
| 2159 | ctxt->eflags &= ~EFLG_CF; |
| 2160 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2161 | break; |
| 2162 | case 0xfa: /* cli */ |
| 2163 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 2164 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2165 | break; |
| 2166 | case 0xfb: /* sti */ |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 2167 | toggle_interruptibility(ctxt, X86_SHADOW_INT_STI); |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2168 | ctxt->eflags |= X86_EFLAGS_IF; |
| 2169 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2170 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 2171 | case 0xfc: /* cld */ |
| 2172 | ctxt->eflags &= ~EFLG_DF; |
| 2173 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2174 | break; |
| 2175 | case 0xfd: /* std */ |
| 2176 | ctxt->eflags |= EFLG_DF; |
| 2177 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2178 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2179 | case 0xfe ... 0xff: /* Grp4/Grp5 */ |
| 2180 | rc = emulate_grp45(ctxt, ops); |
| 2181 | if (rc != 0) |
| 2182 | goto done; |
| 2183 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2184 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2185 | |
| 2186 | writeback: |
| 2187 | rc = writeback(ctxt, ops); |
| 2188 | if (rc != 0) |
| 2189 | goto done; |
| 2190 | |
| 2191 | /* Commit shadow register state. */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2192 | memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2193 | kvm_rip_write(ctxt->vcpu, c->eip); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2194 | |
| 2195 | done: |
| 2196 | if (rc == X86EMUL_UNHANDLEABLE) { |
| 2197 | c->eip = saved_eip; |
| 2198 | return -1; |
| 2199 | } |
| 2200 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2201 | |
| 2202 | twobyte_insn: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2203 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2204 | case 0x01: /* lgdt, lidt, lmsw */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2205 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2206 | u16 size; |
| 2207 | unsigned long address; |
| 2208 | |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2209 | case 0: /* vmcall */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2210 | if (c->modrm_mod != 3 || c->modrm_rm != 1) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2211 | goto cannot_emulate; |
| 2212 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2213 | rc = kvm_fix_hypercall(ctxt->vcpu); |
| 2214 | if (rc) |
| 2215 | goto done; |
| 2216 | |
Avi Kivity | 33e3885 | 2008-05-21 15:34:25 +0300 | [diff] [blame] | 2217 | /* Let the processor re-execute the fixed hypercall */ |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2218 | c->eip = kvm_rip_read(ctxt->vcpu); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2219 | /* Disable writeback. */ |
| 2220 | c->dst.type = OP_NONE; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2221 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2222 | case 2: /* lgdt */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2223 | rc = read_descriptor(ctxt, ops, c->src.ptr, |
| 2224 | &size, &address, c->op_bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2225 | if (rc) |
| 2226 | goto done; |
| 2227 | realmode_lgdt(ctxt->vcpu, size, address); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2228 | /* Disable writeback. */ |
| 2229 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2230 | break; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2231 | case 3: /* lidt/vmmcall */ |
Avi Kivity | 2b3d2a2 | 2008-12-23 19:46:01 +0200 | [diff] [blame] | 2232 | if (c->modrm_mod == 3) { |
| 2233 | switch (c->modrm_rm) { |
| 2234 | case 1: |
| 2235 | rc = kvm_fix_hypercall(ctxt->vcpu); |
| 2236 | if (rc) |
| 2237 | goto done; |
| 2238 | break; |
| 2239 | default: |
| 2240 | goto cannot_emulate; |
| 2241 | } |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2242 | } else { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2243 | rc = read_descriptor(ctxt, ops, c->src.ptr, |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2244 | &size, &address, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2245 | c->op_bytes); |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2246 | if (rc) |
| 2247 | goto done; |
| 2248 | realmode_lidt(ctxt->vcpu, size, address); |
| 2249 | } |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2250 | /* Disable writeback. */ |
| 2251 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2252 | break; |
| 2253 | case 4: /* smsw */ |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2254 | c->dst.bytes = 2; |
| 2255 | c->dst.val = realmode_get_cr(ctxt->vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2256 | break; |
| 2257 | case 6: /* lmsw */ |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2258 | realmode_lmsw(ctxt->vcpu, (u16)c->src.val, |
| 2259 | &ctxt->eflags); |
Avi Kivity | dc7457e | 2008-04-30 16:13:36 +0300 | [diff] [blame] | 2260 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2261 | break; |
| 2262 | case 7: /* invlpg*/ |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 2263 | emulate_invlpg(ctxt->vcpu, memop); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2264 | /* Disable writeback. */ |
| 2265 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2266 | break; |
| 2267 | default: |
| 2268 | goto cannot_emulate; |
| 2269 | } |
| 2270 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 2271 | case 0x05: /* syscall */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2272 | if (emulate_syscall(ctxt) == -1) |
| 2273 | goto cannot_emulate; |
| 2274 | else |
| 2275 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 2276 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2277 | case 0x06: |
| 2278 | emulate_clts(ctxt->vcpu); |
| 2279 | c->dst.type = OP_NONE; |
| 2280 | break; |
| 2281 | case 0x08: /* invd */ |
| 2282 | case 0x09: /* wbinvd */ |
| 2283 | case 0x0d: /* GrpP (prefetch) */ |
| 2284 | case 0x18: /* Grp16 (prefetch/nop) */ |
| 2285 | c->dst.type = OP_NONE; |
| 2286 | break; |
| 2287 | case 0x20: /* mov cr, reg */ |
| 2288 | if (c->modrm_mod != 3) |
| 2289 | goto cannot_emulate; |
| 2290 | c->regs[c->modrm_rm] = |
| 2291 | realmode_get_cr(ctxt->vcpu, c->modrm_reg); |
| 2292 | c->dst.type = OP_NONE; /* no writeback */ |
| 2293 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2294 | case 0x21: /* mov from dr to reg */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2295 | if (c->modrm_mod != 3) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2296 | goto cannot_emulate; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2297 | rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]); |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 2298 | if (rc) |
| 2299 | goto cannot_emulate; |
| 2300 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2301 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2302 | case 0x22: /* mov reg, cr */ |
| 2303 | if (c->modrm_mod != 3) |
| 2304 | goto cannot_emulate; |
| 2305 | realmode_set_cr(ctxt->vcpu, |
| 2306 | c->modrm_reg, c->modrm_val, &ctxt->eflags); |
| 2307 | c->dst.type = OP_NONE; |
| 2308 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2309 | case 0x23: /* mov from reg to dr */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2310 | if (c->modrm_mod != 3) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2311 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2312 | rc = emulator_set_dr(ctxt, c->modrm_reg, |
| 2313 | c->regs[c->modrm_rm]); |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 2314 | if (rc) |
| 2315 | goto cannot_emulate; |
| 2316 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2317 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2318 | case 0x30: |
| 2319 | /* wrmsr */ |
| 2320 | msr_data = (u32)c->regs[VCPU_REGS_RAX] |
| 2321 | | ((u64)c->regs[VCPU_REGS_RDX] << 32); |
| 2322 | rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data); |
| 2323 | if (rc) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2324 | kvm_inject_gp(ctxt->vcpu, 0); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2325 | c->eip = kvm_rip_read(ctxt->vcpu); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2326 | } |
| 2327 | rc = X86EMUL_CONTINUE; |
| 2328 | c->dst.type = OP_NONE; |
| 2329 | break; |
| 2330 | case 0x32: |
| 2331 | /* rdmsr */ |
| 2332 | rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data); |
| 2333 | if (rc) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2334 | kvm_inject_gp(ctxt->vcpu, 0); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2335 | c->eip = kvm_rip_read(ctxt->vcpu); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2336 | } else { |
| 2337 | c->regs[VCPU_REGS_RAX] = (u32)msr_data; |
| 2338 | c->regs[VCPU_REGS_RDX] = msr_data >> 32; |
| 2339 | } |
| 2340 | rc = X86EMUL_CONTINUE; |
| 2341 | c->dst.type = OP_NONE; |
| 2342 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 2343 | case 0x34: /* sysenter */ |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2344 | if (emulate_sysenter(ctxt) == -1) |
| 2345 | goto cannot_emulate; |
| 2346 | else |
| 2347 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 2348 | break; |
| 2349 | case 0x35: /* sysexit */ |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2350 | if (emulate_sysexit(ctxt) == -1) |
| 2351 | goto cannot_emulate; |
| 2352 | else |
| 2353 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 2354 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2355 | case 0x40 ... 0x4f: /* cmov */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2356 | c->dst.val = c->dst.orig_val = c->src.val; |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 2357 | if (!test_cc(c->b, ctxt->eflags)) |
| 2358 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2359 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 2360 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2361 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 2362 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2363 | c->dst.type = OP_NONE; |
| 2364 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2365 | case 0xa0: /* push fs */ |
| 2366 | emulate_push_sreg(ctxt, VCPU_SREG_FS); |
| 2367 | break; |
| 2368 | case 0xa1: /* pop fs */ |
| 2369 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS); |
| 2370 | if (rc != 0) |
| 2371 | goto done; |
| 2372 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2373 | case 0xa3: |
| 2374 | bt: /* bt */ |
Qing He | e4f8e03 | 2007-09-24 17:22:13 +0800 | [diff] [blame] | 2375 | c->dst.type = OP_NONE; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2376 | /* only subword offset */ |
| 2377 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2378 | emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2379 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 2380 | case 0xa4: /* shld imm8, r, r/m */ |
| 2381 | case 0xa5: /* shld cl, r, r/m */ |
| 2382 | emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags); |
| 2383 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2384 | case 0xa8: /* push gs */ |
| 2385 | emulate_push_sreg(ctxt, VCPU_SREG_GS); |
| 2386 | break; |
| 2387 | case 0xa9: /* pop gs */ |
| 2388 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS); |
| 2389 | if (rc != 0) |
| 2390 | goto done; |
| 2391 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2392 | case 0xab: |
| 2393 | bts: /* bts */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2394 | /* only subword offset */ |
| 2395 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2396 | emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2397 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 2398 | case 0xac: /* shrd imm8, r, r/m */ |
| 2399 | case 0xad: /* shrd cl, r, r/m */ |
| 2400 | emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags); |
| 2401 | break; |
Glauber Costa | 2a7c5b8 | 2008-07-10 17:08:15 -0300 | [diff] [blame] | 2402 | case 0xae: /* clflush */ |
| 2403 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2404 | case 0xb0 ... 0xb1: /* cmpxchg */ |
| 2405 | /* |
| 2406 | * Save real source value, then compare EAX against |
| 2407 | * destination. |
| 2408 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2409 | c->src.orig_val = c->src.val; |
| 2410 | c->src.val = c->regs[VCPU_REGS_RAX]; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2411 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
| 2412 | if (ctxt->eflags & EFLG_ZF) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2413 | /* Success: write back to memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2414 | c->dst.val = c->src.orig_val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2415 | } else { |
| 2416 | /* Failure: write the value we saw to EAX. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2417 | c->dst.type = OP_REG; |
| 2418 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2419 | } |
| 2420 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2421 | case 0xb3: |
| 2422 | btr: /* btr */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2423 | /* only subword offset */ |
| 2424 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2425 | emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2426 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2427 | case 0xb6 ... 0xb7: /* movzx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2428 | c->dst.bytes = c->op_bytes; |
| 2429 | c->dst.val = (c->d & ByteOp) ? (u8) c->src.val |
| 2430 | : (u16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2431 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2432 | case 0xba: /* Grp8 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2433 | switch (c->modrm_reg & 3) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2434 | case 0: |
| 2435 | goto bt; |
| 2436 | case 1: |
| 2437 | goto bts; |
| 2438 | case 2: |
| 2439 | goto btr; |
| 2440 | case 3: |
| 2441 | goto btc; |
| 2442 | } |
| 2443 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2444 | case 0xbb: |
| 2445 | btc: /* btc */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2446 | /* only subword offset */ |
| 2447 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2448 | emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2449 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2450 | case 0xbe ... 0xbf: /* movsx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2451 | c->dst.bytes = c->op_bytes; |
| 2452 | c->dst.val = (c->d & ByteOp) ? (s8) c->src.val : |
| 2453 | (s16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2454 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 2455 | case 0xc3: /* movnti */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2456 | c->dst.bytes = c->op_bytes; |
| 2457 | c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val : |
| 2458 | (u64) c->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 2459 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2460 | case 0xc7: /* Grp9 (cmpxchg8b) */ |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 2461 | rc = emulate_grp9(ctxt, ops, memop); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2462 | if (rc != 0) |
| 2463 | goto done; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2464 | c->dst.type = OP_NONE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2465 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2466 | } |
| 2467 | goto writeback; |
| 2468 | |
| 2469 | cannot_emulate: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2470 | DPRINTF("Cannot emulate %02x\n", c->b); |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 2471 | c->eip = saved_eip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2472 | return -1; |
| 2473 | } |