From patchwork Fri Sep 16 17:43:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamas Lengyel X-Patchwork-Id: 9336293 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 D81916077F for ; Fri, 16 Sep 2016 17:46:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C99922A052 for ; Fri, 16 Sep 2016 17:46:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BDE872A058; Fri, 16 Sep 2016 17:46:43 +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=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RCVD_IN_SORBS_SPAM,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 73E172A052 for ; Fri, 16 Sep 2016 17:46:41 +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 1bkxB3-0000p2-Tg; Fri, 16 Sep 2016 17:43:53 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bkxB2-0000ow-Jd for xen-devel@lists.xenproject.org; Fri, 16 Sep 2016 17:43:52 +0000 Received: from [85.158.137.68] by server-1.bemta-3.messagelabs.com id 1A/FC-17152-75F2CD75; Fri, 16 Sep 2016 17:43:51 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprCIsWRWlGSWpSXmKPExsVyMfSOo26Y/p1 wg7tPLS2+b5nM5MDocfjDFZYAxijWzLyk/IoE1ozGJZvYClqzKxqfTWZuYJwT1MXIxSEkMJ1R Ys/S7awgDovAGxaJs2+fsoE4EgLvWCS2XOln6mLkBHJiJNb3n2XuYuQAsqslfs/SBAkLCWhKH NxwhhnC7meS2H2qBMRmEzCSuHq1hw3EFhFQkri3ajITyExmgXWMEnM+z2QFSQgLmElc2bcAbD 6LgKrEz54+sDivgJ3ElVPvGCH2yklc2vaFeQIj3wJGhlWMGsWpRWWpRbqGFnpJRZnpGSW5iZk 5uoYGxnq5qcXFiempOYlJxXrJ+bmbGIGhUs/AwLiD8fdpz0OMkhxMSqK8xYW3w4X4kvJTKjMS izPii0pzUosPMcpwcChJ8K7WuxMuJFiUmp5akZaZAwxamLQEB4+SCO8ckDRvcUFibnFmOkTqF KMlx5bf19YycRybewNIbpt6by2TEEtefl6qlDhvGkiDAEhDRmke3DhYZF1ilJUS5mVkYGAQ4i lILcrNLEGVf8UozsGoJMyrDjKFJzOvBG7rK6CDmIAOWj3rNshBJYkIKakGxuVMQULMzGyad5h 7Qj+lPlrRxnZKu/Xmg5mp85KkOC9I6MtfPHNi4vKLX3R/PZUXbU9VsOa4/zRZPtJ4R7c9Q7Pw y7nfHdlcz52/VeKaLHFq46RE70eH1FiSVj19dHLerrvm39mfnPSbk3fF9cWS8z/rrVJzos6Yx 7fIuRcfPtij7FoV+neuixJLcUaioRZzUXEiAAW2fTinAgAA X-Env-Sender: tamas.lengyel@zentific.com X-Msg-Ref: server-3.tower-31.messagelabs.com!1474047829!61167874!1 X-Originating-IP: [209.85.220.65] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 8.84; banners=-,-,- X-VirusChecked: Checked Received: (qmail 20119 invoked from network); 16 Sep 2016 17:43:50 -0000 Received: from mail-pa0-f65.google.com (HELO mail-pa0-f65.google.com) (209.85.220.65) by server-3.tower-31.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 16 Sep 2016 17:43:50 -0000 Received: by mail-pa0-f65.google.com with SMTP id my20so728215pab.3 for ; Fri, 16 Sep 2016 10:43:50 -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=xTkOsh1Yb5Y0jskOLGtRDO0vG3pnblRzR0dlEtEtO/o=; b=1NkyvS4OkpbII5Se1lx4EWVUhhOUrtJ1QrC2jBy3KGfAbcvMyT/RHe+FnwAksEHqQv JIL6zIdsbKaRS3yX+CzFp1vtECnWGBltqfm63jVyXDeSkYkd93PB2x6lHgpqYoyT06DU tUjSMvSsEKQpu42WIEGvsgmezIfPHv8aKBqddUw7YGpCIQJ+4lI2G3JZFfwx7xiAzkV5 tI7EY344wkvHjooKcfJtPrgKArqBJR6N46IyPRrbuIEyUfw0pN1m8pJOljmQzTSI1hw8 AKJbeMdCZAqn4NO1rise6VXhrFcKx/srWbnFxcselnXmi8gv8+Hu8JEi+hMMViWT3Yxj wwZw== 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=xTkOsh1Yb5Y0jskOLGtRDO0vG3pnblRzR0dlEtEtO/o=; b=TEUhqnTsTbIX3ZrKuUwowtKcKG134oeooz/qwCBZdRRJa0bT1i4CBtF3p0QoxoxCca eU/0LcCYjKHRxnuPQGyOIy6yx0qIXwKFpwvHFwfCtDvDcNHKmB/3qDio1IdsAjxwdLdy NWSN06FdMFoBH3d6ubPk31s2V6bnqiT6l3JjRN0hNAbx9nNRyW8Uj5bZH51Dye34ECwv 1SX6hieMhM2Qkpp42Ra1LEgqU7dzNMSb9m2Gfw7cIHpOE/NBruCZ9ZbVdBdkYTKOf1I8 vkDMOkkcE3AmPGukblifCZJMnRMy/rvorIrZhAbWbLmLAlbN/ygJfpekgfciIUOL488K io7g== X-Gm-Message-State: AE9vXwMnJ4Z+njAJAhsAK6+uGHkG2QxHI/UYVgvvnNyO/MbISHM/ckJfFFSzV0IIoe7VMw== X-Received: by 10.66.144.227 with SMTP id sp3mr24686050pab.131.1474047828905; Fri, 16 Sep 2016 10:43:48 -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 7sm53695975pfk.6.2016.09.16.10.43.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Sep 2016 10:43:48 -0700 (PDT) From: Tamas K Lengyel To: xen-devel@lists.xenproject.org Date: Fri, 16 Sep 2016 11:43:27 -0600 Message-Id: <20160916174327.1405-1-tamas.lengyel@zentific.com> X-Mailer: git-send-email 2.9.3 Cc: Tamas K Lengyel , Julien Grall , Stefano Stabellini , Ian Jackson Subject: [Xen-devel] [PATCH v4] 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: Stefano Stabellini Cc: Julien Grall v4: Style fixes 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 | 80 +++++++++++++++++++++++++++++++++++++ xen/arch/arm/traps.c | 16 +++++++- 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, 160 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..cadeae1 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=%016"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..1e8bdfc --- /dev/null +++ b/xen/arch/arm/monitor.c @@ -0,0 +1,80 @@ +/* + * arch/arm/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..3555a28 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,17 @@ 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 +2614,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 +2647,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..b251f68 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. + * These kinds of events will be 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