From patchwork Thu Sep 15 18:24:41 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamas Lengyel X-Patchwork-Id: 9334547 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A2AAA60839 for ; Thu, 15 Sep 2016 18:28:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 97B3E296C1 for ; Thu, 15 Sep 2016 18:28:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8C1AC29B14; Thu, 15 Sep 2016 18:28:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5D24E296C1 for ; Thu, 15 Sep 2016 18:28:07 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bkbLA-0004PN-33; Thu, 15 Sep 2016 18:24:52 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bkbL8-0004PF-7J for xen-devel@lists.xenproject.org; Thu, 15 Sep 2016 18:24:50 +0000 Received: from [85.158.137.68] by server-13.bemta-3.messagelabs.com id D1/2D-06162-177EAD75; Thu, 15 Sep 2016 18:24:49 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprMIsWRWlGSWpSXmKPExsVyMfSOs27B81v hBk8Wy1h83zKZyYHR4/CHKywBjFGsmXlJ+RUJrBkr9zayFLRmVzx78IKxgXFWUBcjF4eQwDRG iYc3X7F3MXJysAi8YZF4PzkbxJYQeMcisWd5IIQdI/Hn0zM2CLta4vuZ3SwgtpCApsTBDWeYI QZNYpLoWL8FLMEmYCRx9WoPWIOIgJLEvVWTmUCKmAVamCR2bf3MCJIQFjCTmH5oGTPEZlWJr/ f6gZo5OHgF7CU2fZaAWCYncWnbF+YJjHwLGBlWMWoUpxaVpRbpGhnoJRVlpmeU5CZm5ugaGhj r5aYWFyemp+YkJhXrJefnbmIEBko9AwPjDsbmE36HGCU5mJREeXl23QoX4kvKT6nMSCzOiC8q zUktPsQow8GhJMEr+gwoJ1iUmp5akZaZAwxZmLQEB4+SCG8ISJq3uCAxtzgzHSJ1itGSY8vva 2uZOI7NvQEkt029t5ZJiCUvPy9VSpz3/lOgBgGQhozSPLhxsLi6xCgrJczLyMDAIMRTkFqUm1 mCKv+KUZyDUUkYYgpPZl4J3NZXQAcxAR20Zc11kINKEhFSUg2MFact70056xlebrgkbVpJ5y3 /lolqV2Zv+poYc1RryrkMv67slUe6tA9ETV+9bt0FzsOljwNeJTNMm5Be/PVAndLdZ78+JQb8 DLhxxuh21ZTVyfWZhrVvhd9GvhSsML5wU/e+VKpG9sbVKs9Xr/uez3xMPMfh7grJv298C2OV5 zLfVeC2CDrersRSnJFoqMVcVJwIADKsZ2emAgAA X-Env-Sender: tamas.lengyel@zentific.com X-Msg-Ref: server-8.tower-31.messagelabs.com!1473963886!61046090!1 X-Originating-IP: [209.85.220.67] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 8.84; banners=-,-,- X-VirusChecked: Checked Received: (qmail 24867 invoked from network); 15 Sep 2016 18:24:48 -0000 Received: from mail-pa0-f67.google.com (HELO mail-pa0-f67.google.com) (209.85.220.67) by server-8.tower-31.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 15 Sep 2016 18:24:48 -0000 Received: by mail-pa0-f67.google.com with SMTP id p2so2400839pap.3 for ; Thu, 15 Sep 2016 11:24:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zentific-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=iCmenS1s5jgeXatvNp1M8Pvt/CzBXPrbExpk6971o94=; b=Gss7x79aAH7dHZOFu3Vtkh1y5L9A+p1+mlQ2WezYHbyqYGPstKVkcUvdzCHxFtFROj uA8OX/S40eIi/xdA7I88IUxtbixOW06gZj/GPEH75GkAyY67g2/x+4dRflRNet2ovRIZ Q+Sfka2f+dGCGC+vedc7Fo7MAUBsr1W0+zJtqQbL18WlZEDAHbLOYu2S1QdWHVcTOmAm c8W3yPV4nzC7cis9dbfZlH/Nlsme36sNhczQQPTOST+qfGy+7J+WGKEy2c7xW6omhjWq yYVfBqbQ+2OYwNfFXXeMBH2En6G34ziYO1N7eKipFQ0PWnrtA2ek4OfRPmyu+l/prE3n skTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=iCmenS1s5jgeXatvNp1M8Pvt/CzBXPrbExpk6971o94=; b=VIkT9WTjvXnxFddozIHUmTbFcWazr9eI2+BBPRjpcZru+WpTqH4Uw83aD2ufOpZHTi n5Ta5GJQiOTDcvHdq+c8PhvQj8mwUR6EJF20sRiavDjGL6OZMDsmQl4KuYqYFevpo0Pi MEi37Sm+/46dJB2UgY8G8SH3gYTUsuJgYQ/W9UfLmvh8Suu+jOs7tThdH/MiMP9NHDWs 9DvzcBk815j2chJEWz/4DR8Ac0PghP5HsZ0VlWKdfYAtyVT2bnNbhpvydgvxzI/+bSoV 5jdKKHysZsBuTBuRWnl+Ad1wNG2zFSHFg/7TrUTCIO713+VwDD1ZqW/ude/V2PcPIWZ3 Gp4Q== X-Gm-Message-State: AE9vXwN6H+j+aTX/WFx77HRJyMQ6hhUv4oms3Zp/ABNbH97G6hguPtu0O36Nxkx7MXmxEA== X-Received: by 10.66.222.202 with SMTP id qo10mr16671318pac.76.1473963886145; Thu, 15 Sep 2016 11:24:46 -0700 (PDT) Received: from l1.lan (c-73-14-35-59.hsd1.co.comcast.net. [73.14.35.59]) by smtp.gmail.com with ESMTPSA id x66sm46715557pfb.86.2016.09.15.11.24.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Sep 2016 11:24:45 -0700 (PDT) From: Tamas K Lengyel To: xen-devel@lists.xenproject.org Date: Thu, 15 Sep 2016 12:24:41 -0600 Message-Id: <20160915182441.21356-1-tamas.lengyel@zentific.com> X-Mailer: git-send-email 2.9.3 Cc: Tamas K Lengyel , Razvan Cojocaru , Tamas K Lengyel , Ian Jackson , Julien Grall , Stefano Stabellini Subject: [Xen-devel] [PATCH v3] vm_event: Implement ARM SMC events X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP The ARM SMC instructions are already configured to trap to Xen by default. In this patch we allow a user-space process in a privileged domain to receive notification of when such event happens through the vm_event subsystem by introducing the PRIVILEGED_CALL type. The intended use-case for this feature is for a monitor application to be able insert tap-points into the domU kernel-code. For this task only unconditional SMC instruction should be used. Signed-off-by: Tamas K Lengyel Acked-by: Wei Liu Acked-by: Razvan Cojocaru --- Cc: Ian Jackson Cc: Razvan Cojocaru Cc: Stefano Stabellini Cc: Julien Grall v3: Rebase on latest master Note: previous discussion around this patch proposed filtering SMCs with failed condition checks. As that patch is yet-to-be implemented and the 4.8 code-freeze rapidly approaching I would like this patch to get included. In this patch a proper warning is placed in the public header for potential users not to rely on SMCs with failed condition checks being trapped. As the intended use-case for this feature doesn't use conditional SMCs this warning should be sufficient. Hardware that does issue events for SMCs with failed condition checks doesn't pose a problem for a monitor application in any way. The xen-access test tool illustrates how SMCs issued by the guest can be safely handled for all cases. --- tools/libxc/include/xenctrl.h | 2 + tools/libxc/xc_monitor.c | 14 +++++++ tools/tests/xen-access/xen-access.c | 32 ++++++++++++++- xen/arch/arm/Makefile | 1 + xen/arch/arm/monitor.c | 79 +++++++++++++++++++++++++++++++++++++ xen/arch/arm/traps.c | 15 ++++++- xen/include/asm-arm/domain.h | 5 +++ xen/include/asm-arm/monitor.h | 18 +++------ xen/include/public/domctl.h | 1 + xen/include/public/vm_event.h | 7 ++++ 10 files changed, 158 insertions(+), 16 deletions(-) create mode 100644 xen/arch/arm/monitor.c diff --git a/tools/libxc/include/xenctrl.h b/tools/libxc/include/xenctrl.h index 560ce7b..eb53172 100644 --- a/tools/libxc/include/xenctrl.h +++ b/tools/libxc/include/xenctrl.h @@ -2168,6 +2168,8 @@ int xc_monitor_guest_request(xc_interface *xch, domid_t domain_id, int xc_monitor_debug_exceptions(xc_interface *xch, domid_t domain_id, bool enable, bool sync); int xc_monitor_cpuid(xc_interface *xch, domid_t domain_id, bool enable); +int xc_monitor_privileged_call(xc_interface *xch, domid_t domain_id, + bool enable); /** * This function enables / disables emulation for each REP for a * REP-compatible instruction. diff --git a/tools/libxc/xc_monitor.c b/tools/libxc/xc_monitor.c index 4298813..15a7c32 100644 --- a/tools/libxc/xc_monitor.c +++ b/tools/libxc/xc_monitor.c @@ -185,6 +185,20 @@ int xc_monitor_cpuid(xc_interface *xch, domid_t domain_id, bool enable) return do_domctl(xch, &domctl); } +int xc_monitor_privileged_call(xc_interface *xch, domid_t domain_id, + bool enable) +{ + DECLARE_DOMCTL; + + domctl.cmd = XEN_DOMCTL_monitor_op; + domctl.domain = domain_id; + domctl.u.monitor_op.op = enable ? XEN_DOMCTL_MONITOR_OP_ENABLE + : XEN_DOMCTL_MONITOR_OP_DISABLE; + domctl.u.monitor_op.event = XEN_DOMCTL_MONITOR_EVENT_PRIVILEGED_CALL; + + return do_domctl(xch, &domctl); +} + /* * Local variables: * mode: C diff --git a/tools/tests/xen-access/xen-access.c b/tools/tests/xen-access/xen-access.c index ed18c71..6eefe0c 100644 --- a/tools/tests/xen-access/xen-access.c +++ b/tools/tests/xen-access/xen-access.c @@ -338,6 +338,8 @@ void usage(char* progname) fprintf(stderr, "Usage: %s [-m] write|exec", progname); #if defined(__i386__) || defined(__x86_64__) fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec|debug|cpuid"); +#elif defined(__arm__) || defined(__aarch64__) + fprintf(stderr, "|privcall"); #endif fprintf(stderr, "\n" @@ -362,6 +364,7 @@ int main(int argc, char *argv[]) int required = 0; int breakpoint = 0; int shutting_down = 0; + int privcall = 0; int altp2m = 0; int debug = 0; int cpuid = 0; @@ -431,6 +434,11 @@ int main(int argc, char *argv[]) { cpuid = 1; } +#elif defined(__arm__) || defined(__aarch64__) + else if ( !strcmp(argv[0], "privcall") ) + { + privcall = 1; + } #endif else { @@ -563,6 +571,16 @@ int main(int argc, char *argv[]) } } + if ( privcall ) + { + rc = xc_monitor_privileged_call(xch, domain_id, 1); + if ( rc < 0 ) + { + ERROR("Error %d setting privileged call trapping with vm_event\n", rc); + goto exit; + } + } + /* Wait for access */ for (;;) { @@ -578,6 +596,9 @@ int main(int argc, char *argv[]) if ( cpuid ) rc = xc_monitor_cpuid(xch, domain_id, 0); + if ( privcall ) + rc = xc_monitor_privileged_call(xch, domain_id, 0); + if ( altp2m ) { rc = xc_altp2m_switch_to_view( xch, domain_id, 0 ); @@ -678,7 +699,7 @@ int main(int argc, char *argv[]) rsp.u.mem_access = req.u.mem_access; break; case VM_EVENT_REASON_SOFTWARE_BREAKPOINT: - printf("Breakpoint: rip=%016"PRIx64", gfn=%"PRIx64" (vcpu %d)\n", + printf("Breakpoint: rip=%"PRIx64" gfn=%"PRIx64" (vcpu %d)\n", req.data.regs.x86.rip, req.u.software_breakpoint.gfn, req.vcpu_id); @@ -695,6 +716,15 @@ int main(int argc, char *argv[]) continue; } break; + case VM_EVENT_REASON_PRIVILEGED_CALL: + printf("Privileged call: pc=%"PRIx64" (vcpu %d)\n", + req.data.regs.arm.pc, + req.vcpu_id); + + rsp.data.regs.arm = req.data.regs.arm; + rsp.data.regs.arm.pc += 4; + rsp.flags |= VM_EVENT_FLAG_SET_REGISTERS; + break; case VM_EVENT_REASON_SINGLESTEP: printf("Singlestep: rip=%016"PRIx64", vcpu %d, altp2m %u\n", req.data.regs.x86.rip, diff --git a/xen/arch/arm/Makefile b/xen/arch/arm/Makefile index 64fdf41..b140d7e 100644 --- a/xen/arch/arm/Makefile +++ b/xen/arch/arm/Makefile @@ -25,6 +25,7 @@ obj-y += irq.o obj-y += kernel.o obj-$(CONFIG_LIVEPATCH) += livepatch.o obj-y += mm.o +obj-y += monitor.o obj-y += p2m.o obj-y += percpu.o obj-y += platform.o diff --git a/xen/arch/arm/monitor.c b/xen/arch/arm/monitor.c new file mode 100644 index 0000000..db05453 --- /dev/null +++ b/xen/arch/arm/monitor.c @@ -0,0 +1,79 @@ +/* + * arch/x86/monitor.c + * + * Arch-specific monitor_op domctl handler. + * + * Copyright (c) 2016 Tamas K Lengyel (tamas.lengyel@zentific.com) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License v2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; If not, see . + */ + +#include +#include +#include +#include +#include + +int arch_monitor_domctl_event(struct domain *d, + struct xen_domctl_monitor_op *mop) +{ + struct arch_domain *ad = &d->arch; + bool_t requested_status = (XEN_DOMCTL_MONITOR_OP_ENABLE == mop->op); + + switch ( mop->event ) + { + case XEN_DOMCTL_MONITOR_EVENT_PRIVILEGED_CALL: + { + bool_t old_status = ad->monitor.privileged_call_enabled; + + if ( unlikely(old_status == requested_status) ) + return -EEXIST; + + domain_pause(d); + ad->monitor.privileged_call_enabled = requested_status; + domain_unpause(d); + break; + } + + default: + /* + * Should not be reached unless arch_monitor_get_capabilities() is + * not properly implemented. + */ + ASSERT_UNREACHABLE(); + return -EOPNOTSUPP; + } + + return 0; +} + +int monitor_smc(void) { + struct vcpu *curr = current; + vm_event_request_t req = { + .reason = VM_EVENT_REASON_PRIVILEGED_CALL + }; + + if ( !curr->domain->arch.monitor.privileged_call_enabled ) + return 0; + + return monitor_traps(curr, 1, &req); +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/arm/traps.c b/xen/arch/arm/traps.c index 39a05fd..cf58fd5 100644 --- a/xen/arch/arm/traps.c +++ b/xen/arch/arm/traps.c @@ -41,6 +41,7 @@ #include #include #include +#include #include "decode.h" #include "vtimer.h" @@ -2527,6 +2528,16 @@ bad_data_abort: inject_dabt_exception(regs, info.gva, hsr.len); } +static void do_trap_smc(struct cpu_user_regs *regs, const union hsr hsr) +{ + int rc = 0; + if ( current->domain->arch.monitor.privileged_call_enabled ) + rc = monitor_smc(); + + if ( rc != 1 ) + inject_undef_exception(regs, hsr); +} + static void enter_hypervisor_head(struct cpu_user_regs *regs) { if ( guest_mode(regs) ) @@ -2602,7 +2613,7 @@ asmlinkage void do_trap_hypervisor(struct cpu_user_regs *regs) */ GUEST_BUG_ON(!psr_mode_is_32bit(regs->cpsr)); perfc_incr(trap_smc32); - inject_undef32_exception(regs); + do_trap_smc(regs, hsr); break; case HSR_EC_HVC32: GUEST_BUG_ON(!psr_mode_is_32bit(regs->cpsr)); @@ -2635,7 +2646,7 @@ asmlinkage void do_trap_hypervisor(struct cpu_user_regs *regs) */ GUEST_BUG_ON(psr_mode_is_32bit(regs->cpsr)); perfc_incr(trap_smc64); - inject_undef64_exception(regs, hsr.len); + do_trap_smc(regs, hsr); break; case HSR_EC_SYSREG: GUEST_BUG_ON(psr_mode_is_32bit(regs->cpsr)); diff --git a/xen/include/asm-arm/domain.h b/xen/include/asm-arm/domain.h index 9452fcd..2d6fbb1 100644 --- a/xen/include/asm-arm/domain.h +++ b/xen/include/asm-arm/domain.h @@ -126,6 +126,11 @@ struct arch_domain paddr_t efi_acpi_gpa; paddr_t efi_acpi_len; #endif + + /* Monitor options */ + struct { + uint8_t privileged_call_enabled : 1; + } monitor; } __cacheline_aligned; struct arch_vcpu diff --git a/xen/include/asm-arm/monitor.h b/xen/include/asm-arm/monitor.h index 4af707a..1c4fea3 100644 --- a/xen/include/asm-arm/monitor.h +++ b/xen/include/asm-arm/monitor.h @@ -32,19 +32,8 @@ int arch_monitor_domctl_op(struct domain *d, struct xen_domctl_monitor_op *mop) return -EOPNOTSUPP; } -static inline int arch_monitor_domctl_event(struct domain *d, - struct xen_domctl_monitor_op *mop) -{ - /* - * No arch-specific monitor vm-events on ARM. - * - * Should not be reached unless arch_monitor_get_capabilities() is not - * properly implemented. - */ - ASSERT_UNREACHABLE(); - return -EOPNOTSUPP; -} + struct xen_domctl_monitor_op *mop); static inline int arch_monitor_init_domain(struct domain *d) @@ -63,9 +52,12 @@ static inline uint32_t arch_monitor_get_capabilities(struct domain *d) { uint32_t capabilities = 0; - capabilities = (1U << XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST); + capabilities = (1U << XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST | + 1U << XEN_DOMCTL_MONITOR_EVENT_PRIVILEGED_CALL); return capabilities; } +int monitor_smc(void); + #endif /* __ASM_ARM_MONITOR_H__ */ diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h index ddd3de4..177319d 100644 --- a/xen/include/public/domctl.h +++ b/xen/include/public/domctl.h @@ -1085,6 +1085,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_psr_cmt_op_t); #define XEN_DOMCTL_MONITOR_EVENT_GUEST_REQUEST 4 #define XEN_DOMCTL_MONITOR_EVENT_DEBUG_EXCEPTION 5 #define XEN_DOMCTL_MONITOR_EVENT_CPUID 6 +#define XEN_DOMCTL_MONITOR_EVENT_PRIVILEGED_CALL 7 struct xen_domctl_monitor_op { uint32_t op; /* XEN_DOMCTL_MONITOR_OP_* */ diff --git a/xen/include/public/vm_event.h b/xen/include/public/vm_event.h index f756126..dc48e4c 100644 --- a/xen/include/public/vm_event.h +++ b/xen/include/public/vm_event.h @@ -124,6 +124,13 @@ #define VM_EVENT_REASON_DEBUG_EXCEPTION 9 /* CPUID executed */ #define VM_EVENT_REASON_CPUID 10 +/* + * Privileged call executed (e.g. SMC). + * Note: event may be generated even if SMC condition check fails on some CPUs. + * As this behavior is CPU-specific users are advised to not rely on it. + * Such events are planned to be forcibly filtered out in future versions. + */ +#define VM_EVENT_REASON_PRIVILEGED_CALL 11 /* Supported values for the vm_event_write_ctrlreg index. */ #define VM_EVENT_X86_CR0 0