From patchwork Fri Jul 26 06:51:14 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rui Wang X-Patchwork-Id: 2833803 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 944A3C0319 for ; Fri, 26 Jul 2013 06:57:10 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9DD4520149 for ; Fri, 26 Jul 2013 06:57:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 82F7220142 for ; Fri, 26 Jul 2013 06:57:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756987Ab3GZG4w (ORCPT ); Fri, 26 Jul 2013 02:56:52 -0400 Received: from mail-pa0-f45.google.com ([209.85.220.45]:57526 "EHLO mail-pa0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753583Ab3GZG4Y (ORCPT ); Fri, 26 Jul 2013 02:56:24 -0400 Received: by mail-pa0-f45.google.com with SMTP id bg4so2931821pad.32 for ; Thu, 25 Jul 2013 23:56:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=KnLIx1q7Rf78+cS0FMFuHcgHvY90kBB6zsd+rUo7MCo=; b=BOeQr5g46I+sqE7Vw2G3owJq2N/xnmj8wnl+XwjRMmqa5GsGYAdY+Hljr4Tu+SjjrX 4IHQIT4IzY7HtiLR4CRHhBEzGfn8V7I/rYcRb83uXS3ZwlpreiqwObiFqWeq0ZHgyBgu cmZx7AvbjyySO7PWKKuOr8yM2LLzJoS1NqUZWFoVioTfqt1jx962hWq/AHEmQBQRU+Eq BslUf5M2F6P8FYmBJ/Q8JnkU7A0LuSROlW9Iz5ptcT50qgmTh/GXP2WyineYeSUK4HJh zZ4eCL6dD1XyeglbcN+WeZJQYvwZEpkyF44g0twWYnNlfQNEucvF7S0lxLWbKzPXWjau jfng== X-Received: by 10.66.141.104 with SMTP id rn8mr53305103pab.52.1374821784155; Thu, 25 Jul 2013 23:56:24 -0700 (PDT) Received: from localhost.localdomain (shzdmzpr02-ext.sh.intel.com. [192.102.204.37]) by mx.google.com with ESMTPSA id ry2sm58264328pbc.41.2013.07.25.23.56.19 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 25 Jul 2013 23:56:23 -0700 (PDT) From: Rui Wang To: bhelgaas@google.com Cc: tony.luck@intel.com, chaohong.guo@intel.com, gong.chen@linux.intel.com, rafael.j.wysocki@intel.com, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, Rui Wang Subject: [RFC 2/3] IO Hook: kernel interface to manage the hook Date: Fri, 26 Jul 2013 14:51:14 +0800 Message-Id: <1374821475-3196-3-git-send-email-rui.y.wang@intel.com> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <1374821475-3196-1-git-send-email-rui.y.wang@intel.com> References: <1374821475-3196-1-git-send-email-rui.y.wang@intel.com> Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The following kernel functions are exported: hook_add_ovrd hook_query_ovrd hook_cleanup_ovrd hook_start_ovrd hook_stop_ovrd hook_get_status Kernel modules can use them to add/delete/query Register Overrides, and to start/stop the hook engine. Signed-off-by: Rui Wang --- drivers/pci/access.c | 150 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/reg_ovrd.h | 9 +++ 2 files changed, 159 insertions(+), 0 deletions(-) diff --git a/drivers/pci/access.c b/drivers/pci/access.c index fe7b282..81428655 100644 --- a/drivers/pci/access.c +++ b/drivers/pci/access.c @@ -27,6 +27,156 @@ LIST_HEAD(ovrd_pci_conf_reg_map); struct static_key ovrdhw_enabled = STATIC_KEY_INIT_FALSE; EXPORT_SYMBOL(ovrdhw_enabled); +int g_ovrd_on; +/* query a Register Override given spaceid and index */ +struct reg_ovrd *hook_query_ovrd(int spaceid, int idx) +{ + struct list_head *reg_ovrd; + struct reg_ovrd *ovrdreg, *regentry; + unsigned long lock_flags = 0; + int n = 0; + + if (spaceid == OVRD_SPACE_MEM) + reg_ovrd = &ovrd_mem_reg_map; + else if (spaceid == OVRD_SPACE_IO) + reg_ovrd = &ovrd_io_reg_map; + else + reg_ovrd = &ovrd_pci_conf_reg_map; + + regentry = NULL; + raw_spin_lock_irqsave(&io_hook_lock, lock_flags); + list_for_each_entry(ovrdreg, reg_ovrd, node) { + if (n++ >= idx) { + regentry = ovrdreg; + break; + } + } + raw_spin_unlock_irqrestore(&io_hook_lock, lock_flags); + + pr_info("hook_query_ovrd() returns %p, idx=%d\n", + regentry, idx); + return regentry; + +} +EXPORT_SYMBOL(hook_query_ovrd); + +/* delete all Register Overrides from one space (IO/mem/pciconf) */ +void hook_cleanup_ovrd(int spaceid) +{ + struct list_head *tmp, *next, *ovrd_list; + + if (spaceid == OVRD_SPACE_MEM) + ovrd_list = &ovrd_mem_reg_map; + else if (spaceid == OVRD_SPACE_IO) + ovrd_list = &ovrd_io_reg_map; + else + ovrd_list = &ovrd_pci_conf_reg_map; + + list_for_each_safe(tmp, next, ovrd_list) { + struct reg_ovrd *ovrdreg = + list_entry(tmp, struct reg_ovrd, node); + list_del(tmp); + kfree(ovrdreg); + } +} +EXPORT_SYMBOL(hook_cleanup_ovrd); + +/* for pci config space, address is encoded per PCI_ENCODE_ADDR() */ +void hook_add_ovrd(int spaceid, u64 address, u64 value, u64 mask, + u32 length, u8 attrib) +{ + struct list_head *reg_ovrd; + struct reg_ovrd *ovrdreg; + unsigned long lock_flags = 0; + + if (spaceid == OVRD_SPACE_MEM) + reg_ovrd = &ovrd_mem_reg_map; + else if (spaceid == OVRD_SPACE_IO) + reg_ovrd = &ovrd_io_reg_map; + else + reg_ovrd = &ovrd_pci_conf_reg_map; + + raw_spin_lock_irqsave(&io_hook_lock, lock_flags); + list_for_each_entry(ovrdreg, reg_ovrd, node) { + if (ovrdreg->address == address && + ovrdreg->attrib == attrib && + ovrdreg->length == length) { + /* if already added the address, just change the bits */ + ovrdreg->bit_mask |= mask; + ovrdreg->val |= value; + pr_info("hook_add_ovrd(): 0x%llx already added, changed to 0x%llx, mask:0x%llx, attrib:0x%x\n", + address, ovrdreg->val, ovrdreg->bit_mask, + attrib); + goto out; + } else if (address >= ovrdreg->address && + address < ovrdreg->address + ovrdreg->length) { + pr_info("hook_add_ovrd(): conflicting reg at 0x%llx, length:%llx, mask:0x%llx, attrib:0x%x\n", + address, ovrdreg->val, ovrdreg->bit_mask, + attrib); + goto out; + } + } + + raw_spin_unlock_irqrestore(&io_hook_lock, lock_flags); + + ovrdreg = kmalloc(sizeof(struct reg_ovrd), GFP_ATOMIC); + if (!ovrdreg) { + pr_info("failed to alloc Reg Override!\n"); + return; + } + + ovrdreg->address = address; + ovrdreg->val = value; + ovrdreg->length = length; + ovrdreg->bit_mask = mask; + ovrdreg->attrib = attrib; + raw_spin_lock_irqsave(&io_hook_lock, lock_flags); + list_add_tail(&ovrdreg->node, reg_ovrd); +out: + raw_spin_unlock_irqrestore(&io_hook_lock, lock_flags); + +} +EXPORT_SYMBOL(hook_add_ovrd); + +/* to start the hook */ +void hook_start_ovrd(void) +{ + unsigned long lock_flags = 0; + + raw_spin_lock_irqsave(&io_hook_lock, lock_flags); + if (g_ovrd_on) + goto done; + + static_key_slow_inc(&ovrdhw_enabled); + g_ovrd_on = 1; +done: + raw_spin_unlock_irqrestore(&io_hook_lock, lock_flags); + +} +EXPORT_SYMBOL(hook_start_ovrd); + +/* to stop the hook */ +void hook_stop_ovrd(void) +{ + unsigned long lock_flags = 0; + + raw_spin_lock_irqsave(&io_hook_lock, lock_flags); + if (!g_ovrd_on) + goto done; + g_ovrd_on = 0; + static_key_slow_dec(&ovrdhw_enabled); +done: + raw_spin_unlock_irqrestore(&io_hook_lock, lock_flags); + +} +EXPORT_SYMBOL(hook_stop_ovrd); + +int hook_get_status() +{ + return g_ovrd_on; +} +EXPORT_SYMBOL(hook_get_status); + /* len should only be 1, 2, 4, 8 */ static int mem_read(u64 address, int len, void *data) { diff --git a/include/linux/reg_ovrd.h b/include/linux/reg_ovrd.h index 5f851fe..7a81b58 100644 --- a/include/linux/reg_ovrd.h +++ b/include/linux/reg_ovrd.h @@ -42,5 +42,14 @@ struct reg_ovrd { #define PCI_DECODE_BUSN(x) ((u8)(((x) >> 20) & 0xff)) #define PCI_DECODE_DOMAIN(x) ((u32)((x) >> 32)) +#ifdef CONFIG_IO_HOOK +void hook_add_ovrd(int spaceid, u64 address, u64 value, u64 mask, + u32 length, u8 attrib); +struct reg_ovrd *hook_query_ovrd(int spaceid, int idx); +void hook_cleanup_ovrd(int spaceid); +void hook_start_ovrd(void); +void hook_stop_ovrd(void); +int hook_get_status(void); +#endif /* CONFIG_IO_HOOK */ #endif /* __REG_OVRD_H__ */