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