Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1703562ybh; Tue, 14 Jul 2020 05:16:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyB/fSi6sUC4PNWv83MtZIKYj5jHO3N1IS6lYvElzM8LI2xbhJHIkQPHD7/Sae59wXlECa3 X-Received: by 2002:aa7:dd10:: with SMTP id i16mr4279445edv.227.1594728987261; Tue, 14 Jul 2020 05:16:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594728987; cv=none; d=google.com; s=arc-20160816; b=euBxL8lyLZv0lbA/aT2Vf/chgS0x2KYomovqaRjvhGiSEFhTCU0recjf379xBNAEd1 NAfSXc3lCS2IMkilzQvVdS6LhvYV6zQfkHfhJoRUbQcOxbEoIKUXJNfbz64qOwYG0SyF CdnxnSYu8lj7EUBXeWuG/BbPq1CRJr88ZhIMmwoeuGCIS29jDKMFBU5buioC9XxHgdOz J23D5N7BCqJAi4eDhkqrnCv1bLt5cwwtVwlXh8n/5J6RD7MNFJ9ESABLBHbaTHO5hBFK lehMjBdUi6TwGdPiB8impbJv7OA0QcdTjLbKRoeJU298Z3/Lpr1XJg3RG4YVHcq/UNau 3WCQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=6r4TvoyHpV+EgUq/lUtKj2DFE2Zf6sCs3K4MGbWGpCQ=; b=igZavq4HOXHmit6cQPjUfTWydFrHCyVjauhmkPHqaomGcTJc7EUvX+uHnhdwb8OQmE FJS99F6GvW9iPJwzySAKMQap6RclAgVFY6mW4z9UmyzJsYCmr60360ig7iitXHFLCol/ RXUdpA/r2jE7QmAJZLKpXpm5HSqxRUkTkmk+JIZVuXh5JSFZAGqS8a9bsVJ5jBmCNOMo Dy9c1RwDHoxAYcrsHE6+DuSKIYHUKq297/IxYY8JQChtho7XycAPhsmXJILviO0pE7Nr 5CE1s3DIzdyqnltjVVhyOxD5RFa3tKWIX4vGkoK/c+U/n+8YiHWMCAu+P4xGSTMVtkLi uaPA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w18si10785975eds.406.2020.07.14.05.16.03; Tue, 14 Jul 2020 05:16:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728881AbgGNMNj (ORCPT + 99 others); Tue, 14 Jul 2020 08:13:39 -0400 Received: from 8bytes.org ([81.169.241.247]:52886 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728444AbgGNMLH (ORCPT ); Tue, 14 Jul 2020 08:11:07 -0400 Received: from cap.home.8bytes.org (p5b006776.dip0.t-ipconnect.de [91.0.103.118]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by theia.8bytes.org (Postfix) with ESMTPSA id 9B050FC0; Tue, 14 Jul 2020 14:11:01 +0200 (CEST) From: Joerg Roedel To: x86@kernel.org Cc: Joerg Roedel , Joerg Roedel , hpa@zytor.com, Andy Lutomirski , Dave Hansen , Peter Zijlstra , Jiri Slaby , Dan Williams , Tom Lendacky , Juergen Gross , Kees Cook , David Rientjes , Cfir Cohen , Erdem Aktas , Masami Hiramatsu , Mike Stunes , Sean Christopherson , Martin Radev , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH v4 52/75] x86/sev-es: Handle MMIO String Instructions Date: Tue, 14 Jul 2020 14:08:54 +0200 Message-Id: <20200714120917.11253-53-joro@8bytes.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200714120917.11253-1-joro@8bytes.org> References: <20200714120917.11253-1-joro@8bytes.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel Add handling for emulation the MOVS instruction on MMIO regions, as done by the memcpy_toio() and memcpy_fromio() functions. Signed-off-by: Joerg Roedel --- arch/x86/kernel/sev-es.c | 77 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/arch/x86/kernel/sev-es.c b/arch/x86/kernel/sev-es.c index 44995c3ad17e..bbd839a9d9a1 100644 --- a/arch/x86/kernel/sev-es.c +++ b/arch/x86/kernel/sev-es.c @@ -601,6 +601,73 @@ static enum es_result vc_handle_mmio_twobyte_ops(struct ghcb *ghcb, return ret; } +/* + * The MOVS instruction has two memory operands, which raises the + * problem that it is not known whether the access to the source or the + * destination caused the #VC exception (and hence whether an MMIO read + * or write operation needs to be emulated). + * + * Instead of playing games with walking page-tables and trying to guess + * whether the source or destination is an MMIO range, split the move + * into two operations, a read and a write with only one memory operand. + * This will cause a nested #VC exception on the MMIO address which can + * then be handled. + * + * This implementation has the benefit that it also supports MOVS where + * source _and_ destination are MMIO regions. + * + * It will slow MOVS on MMIO down a lot, but in SEV-ES guests it is a + * rare operation. If it turns out to be a performance problem the split + * operations can be moved to memcpy_fromio() and memcpy_toio(). + */ +static enum es_result vc_handle_mmio_movs(struct es_em_ctxt *ctxt, + unsigned int bytes) +{ + unsigned long ds_base, es_base; + unsigned char *src, *dst; + unsigned char buffer[8]; + enum es_result ret; + bool rep; + int off; + + ds_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_DS); + es_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_ES); + + if (ds_base == -1L || es_base == -1L) { + ctxt->fi.vector = X86_TRAP_GP; + ctxt->fi.error_code = 0; + return ES_EXCEPTION; + } + + src = ds_base + (unsigned char *)ctxt->regs->si; + dst = es_base + (unsigned char *)ctxt->regs->di; + + ret = vc_read_mem(ctxt, src, buffer, bytes); + if (ret != ES_OK) + return ret; + + ret = vc_write_mem(ctxt, dst, buffer, bytes); + if (ret != ES_OK) + return ret; + + if (ctxt->regs->flags & X86_EFLAGS_DF) + off = -bytes; + else + off = bytes; + + ctxt->regs->si += off; + ctxt->regs->di += off; + + rep = insn_has_rep_prefix(&ctxt->insn); + if (rep) + ctxt->regs->cx -= 1; + + if (!rep || ctxt->regs->cx == 0) + return ES_OK; + else + return ES_RETRY; +} + static enum es_result vc_handle_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt) { @@ -661,6 +728,16 @@ static enum es_result vc_handle_mmio(struct ghcb *ghcb, memcpy(reg_data, ghcb->shared_buffer, bytes); break; + /* MOVS instruction */ + case 0xa4: + bytes = 1; + fallthrough; + case 0xa5: + if (!bytes) + bytes = insn->opnd_bytes; + + ret = vc_handle_mmio_movs(ctxt, bytes); + break; /* Two-Byte Opcodes */ case 0x0f: ret = vc_handle_mmio_twobyte_ops(ghcb, ctxt); -- 2.27.0