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