Received: by 10.213.65.68 with SMTP id h4csp110628imn; Tue, 27 Mar 2018 17:54:02 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/Q7o7Vn6czGw1EsScexeLy+an4PytXOYVzUfu7GkQ7Tg1KCfNKSGWi1oFxPvLu9WyVX/fb X-Received: by 2002:a17:902:8c88:: with SMTP id t8-v6mr1459459plo.329.1522198442622; Tue, 27 Mar 2018 17:54:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522198442; cv=none; d=google.com; s=arc-20160816; b=fx3d70OrhjkbQQEBQqS6Wy1neE3Lh1Bu2pCQSiEgwrDu6k6CdyKns3ykd2mvJAr13n 24LAeyJhiajIWTpU1pTdeBPkwAGNLnfcLYvPSKHFuJpNCBj12m9EjRSj7PkSVbo222Mr cllFpQyVKvbjKczsL+nfiiRv+L1/GuuYzoMToctuMmphupcIh4v+IWqhkRLEn0oGtYzY +1bO/wgHBfrVKewFwGPm3uC8fouSM5kApG8zq7FeHGTCIeWgqOGoPZ8urS9KgGtxH+b6 pX8P4hZ9rjBbFA3ej3pxYwt8Ye8EHVuC8ZMiguj/QbffPd3hCltAlvPwLBTrlMlFiT5Z 91Sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=q2Wpve5zDYbu1LjE3LxPDpUZik1txDfAG3MARKP2UiY=; b=s1wZm/HVUuCeo7a4vJZk9s1Z6hlg9PQf/7O6egVLREpjbkGIJuV7eHdMMn1m/rQn61 ETUs+xUhCsb4JPCh20r5dnXwDm+7T9BtZqGTxS4fl0ULSXUh56i+T+JkcMxTGIkidICp 2+ix8X/hApKMMNP+Q/KtqNagVbnDZZbhh1KOlXZALSfKWvASKRr9xT5Nr2w55aMOLok5 KggxVA1J8cY61RHfWRk3e1lm0jpHFh0vDi+zSpsLkasVMBa7uTmqq/wtFut1b1y8bLtn O/q3UvYyh0vHqq1v3pUSPjwR6Ds0EFpAmdBNjZ81/a8CvXJywpEKiLhuAwrOgkHi0I2p av3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=NYpzgRo4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l6si1626890pgq.550.2018.03.27.17.53.47; Tue, 27 Mar 2018 17:54:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=NYpzgRo4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752860AbeC1AvS (ORCPT + 99 others); Tue, 27 Mar 2018 20:51:18 -0400 Received: from mail-pl0-f65.google.com ([209.85.160.65]:35148 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752129AbeC1Atq (ORCPT ); Tue, 27 Mar 2018 20:49:46 -0400 Received: by mail-pl0-f65.google.com with SMTP id p9-v6so532995pls.2; Tue, 27 Mar 2018 17:49:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=q2Wpve5zDYbu1LjE3LxPDpUZik1txDfAG3MARKP2UiY=; b=NYpzgRo4ch+8aY2ppUBn+CNFe2SVIT4eXfsrc/EPlG438I6Pv06dN6VyhXhh1W+oza W5H67sAojycKVPj77Mj4NME4xRAODfLYZaX02u99gCJZWet1GFYPUm/dnp6uI+BSqbAd DTxybq4HYjkIiXw6PZySoRxJdAHSGnY7rtH4MOw8OlMtX6iOU6T/k/nVgYdXRqGdtC5q +5/yuo5xdXVnPgzdxi6X9RfTiiDDP9HbsQ5d9n4gI79HWSWcGk4porintcAkxZqEqVXY n5hzBpNGp1RqF3T4v3MTOuMboKSADkA0RL41P6B5DnEdL6sR4Jhq3AqHsjrKmk9LkL6y Qiig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=q2Wpve5zDYbu1LjE3LxPDpUZik1txDfAG3MARKP2UiY=; b=oQAY/em6x4BBr4/OIpXN1SK6whHuFOIsohTkAo0c6PUfDH6d/YzOW7qnUxSAk1hruM LDSad+lBJ9yyDYG4iXPvUNBwXpXl8T50uHyJPh6/XwMn921n8USEAQ65Nq5ncD3OaGLe quFVz8KVJc2s2fKyzTMN71zs1P71DwJnb/jSa06Q2lbxF8KIIDOhpcZg1xr1VN3Zms3/ GIQTrwtbqYLHmyecBn351TaL8sMSaJ8Jtx5dHZXXLdqv+HpGH+BbFDLYbu9T2P6+dT4k JhV+cxQZVHgjVm87Zv79VGHX47eGTcCMO+9zdsvrpTpEXXOuDJ2B5MP+cfx1apdg79qe v7LA== X-Gm-Message-State: AElRT7FrK2EA30A1SRugkY7SoosJpajesH7LK9iDrZCLV6mg7gl6p1tW 1tgj0qNZlcOR4gN219hj3Y1ZFw== X-Received: by 2002:a17:902:be12:: with SMTP id r18-v6mr1537638pls.8.1522198185755; Tue, 27 Mar 2018 17:49:45 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id j65sm4367083pge.58.2018.03.27.17.49.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 27 Mar 2018 17:49:45 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Andrew Cooper , Konrad Rzeszutek Wilk , Liran Alon Subject: [PATCH v3 0/2] KVM: X86: Add Force Emulation Prefix for "emulate the next instruction" Date: Tue, 27 Mar 2018 17:48:52 -0700 Message-Id: <1522198134-2709-1-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There is no easy way to force KVM to run an instruction through the emulator (by design as that will expose the x86 emulator as a significant attack-surface). However, we do wish to expose the x86 emulator in case we are testing it (e.g. via kvm-unit-tests). Therefore, this patch adds a "force emulation prefix" that is designed to raise #UD which KVM will trap and it's #UD exit-handler will match "force emulation prefix" to run instruction after prefix by the x86 emulator. To not expose the x86 emulator by default, we add a module parameter that should be off by default. A simple testcase here: #include #include #define HYPERVISOR_INFO 0x40000000 #define CPUID(idx, eax, ebx, ecx, edx) \ asm volatile ( \ "ud2a; .ascii \"kvm\"; cpuid" \ :"=b" (*ebx), "=a" (*eax), "=c" (*ecx), "=d" (*edx) \ :"0"(idx) ); void main() { unsigned int eax, ebx, ecx, edx; char string[13]; CPUID(HYPERVISOR_INFO, &eax, &ebx, &ecx, &edx); *(unsigned int *)(string + 0) = ebx; *(unsigned int *)(string + 4) = ecx; *(unsigned int *)(string + 8) = edx; string[12] = 0; if (strncmp(string, "KVMKVMKVM\0\0\0", 12) == 0) printf("kvm guest\n"); else printf("bare hardware\n"); } v2 -> v3: * fix compile warning v1 -> v2: * update patch descriptions * move handle_ud to x86.c, shared by vmx and svm * the parameter is in kvm module * rename parameter to force_emulation_prefix Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Andrew Cooper Cc: Konrad Rzeszutek Wilk Cc: Liran Alon Wanpeng Li (2): KVM: X86: Introduce handle_ud() KVM: X86: Add Force Emulation Prefix for "emulate the next instruction" arch/x86/kvm/svm.c | 9 +-------- arch/x86/kvm/vmx.c | 10 ++-------- arch/x86/kvm/x86.c | 29 +++++++++++++++++++++++++++++ arch/x86/kvm/x86.h | 2 ++ 4 files changed, 34 insertions(+), 16 deletions(-) -- 2.7.4