From patchwork Sun Jul 25 19:20:18 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mohammed Gamal X-Patchwork-Id: 114150 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.4/8.14.3) with ESMTP id o6PJKbOs002375 for ; Sun, 25 Jul 2010 19:20:37 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752171Ab0GYTU2 (ORCPT ); Sun, 25 Jul 2010 15:20:28 -0400 Received: from mail-ww0-f44.google.com ([74.125.82.44]:54559 "EHLO mail-ww0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752144Ab0GYTU0 (ORCPT ); Sun, 25 Jul 2010 15:20:26 -0400 Received: by wwj40 with SMTP id 40so6201413wwj.1 for ; Sun, 25 Jul 2010 12:20:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:cc:subject:date :message-id:x-mailer; bh=TvInijSzASq8SCH7j+2Kvd5Mrs73Df5/1eH4MocRlL8=; b=W9m5P5IQ3T+RO78U0Gr68t/yqfW0oGnueBY793TMmUQ7WbusZWNLEk1GwG8LeH9VAO ItkoPR56uvv03E6DgmBT+GU55xyV8APhgzxJ9Fv3Vi/QGhvMTOq+/ByUfhafJhiP3iJS 3IHXJa9d6Gw2XjbiUI+Hhzhc9GFQAu7JFPwjY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=YbZ6n8ei6WBv+KmN+E4hLKpGUw9n9WuejDyRM+cQwWh5Mj+Acjotl7518n4RbZcwWm mWzPyElcr2AUsjn9rGlMGsUMJDQHlWK86T5krijZFZkK1syd5QFEqnF/gKxcj7UTToa+ pg/RmEkO3E2TBNtgwFfGo8VNm9rUbId1GjVzc= Received: by 10.216.132.166 with SMTP id o38mr6401112wei.16.1280085625553; Sun, 25 Jul 2010 12:20:25 -0700 (PDT) Received: from localhost ([41.238.114.84]) by mx.google.com with ESMTPS id k7sm1454442wej.26.2010.07.25.12.20.23 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sun, 25 Jul 2010 12:20:25 -0700 (PDT) From: Mohammed Gamal To: avi@redhat.com Cc: mtosatti@redhat.com, kvm@vger.kernel.org, Mohammed Gamal Subject: [PATCH] x86 emulator: Add IRET instruction Date: Sun, 25 Jul 2010 22:20:18 +0300 Message-Id: <1280085618-27368-1-git-send-email-m.gamal005@gmail.com> X-Mailer: git-send-email 1.7.0.4 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Sun, 25 Jul 2010 19:20:37 +0000 (UTC) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index b38bd8b..4f18b9d 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -1778,6 +1778,61 @@ static int emulate_popa(struct x86_emulate_ctxt *ctxt, return rc; } +static int emulate_iret_real(struct x86_emulate_ctxt *ctxt, + struct x86_emulate_ops *ops) +{ + struct decode_cache *c = &ctxt->decode; + int rc = X86EMUL_CONTINUE; + unsigned long temp_eip = 0; + unsigned long temp_eflags = 0; + /* TODO: Add stack limit check */ + + rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes); + + if (rc != X86EMUL_CONTINUE) + return rc; + + if (temp_eip & ~0xffff) { + emulate_gp(ctxt, 0); + return X86EMUL_PROPAGATE_FAULT; + } + + c->eip = temp_eip; + + rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_CS); + + if (rc != X86EMUL_CONTINUE) + return rc; + + rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes); + + if (rc != X86EMUL_CONTINUE) + return rc; + + if (c->op_bytes == 4) + temp_eflags = ((temp_eflags & 0x257fd5) | (ctxt->eflags & 0x1a0000)); + + ctxt->eflags = temp_eflags; + + return rc; +} + +static inline int emulate_iret(struct x86_emulate_ctxt *ctxt, + struct x86_emulate_ops* ops) +{ + switch(ctxt->mode) { + case X86EMUL_MODE_REAL: + return emulate_iret_real(ctxt, ops); + case X86EMUL_MODE_VM86: + case X86EMUL_MODE_PROT16: + case X86EMUL_MODE_PROT32: + case X86EMUL_MODE_PROT64: + default: + /* iret from protected mode unimplemented yet */ + return X86EMUL_UNHANDLEABLE; + } +} + static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) { @@ -2910,6 +2965,12 @@ special_insn: if (rc != X86EMUL_CONTINUE) goto done; break; + case 0xcf: /* iret */ + rc = emulate_iret(ctxt, ops); + + if (rc != X86EMUL_CONTINUE) + goto done; + break; case 0xd0 ... 0xd1: /* Grp2 */ c->src.val = 1; emulate_grp2(ctxt);