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