From patchwork Thu Aug 1 10:31:32 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rui Wang X-Patchwork-Id: 2836924 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id F20929F9C8 for ; Thu, 1 Aug 2013 10:37:06 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8FAD6202DB for ; Thu, 1 Aug 2013 10:37:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0BEF1202B8 for ; Thu, 1 Aug 2013 10:37:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754884Ab3HAKg4 (ORCPT ); Thu, 1 Aug 2013 06:36:56 -0400 Received: from mail-pa0-f48.google.com ([209.85.220.48]:40575 "EHLO mail-pa0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752407Ab3HAKgx (ORCPT ); Thu, 1 Aug 2013 06:36:53 -0400 Received: by mail-pa0-f48.google.com with SMTP id kp13so1980110pab.35 for ; Thu, 01 Aug 2013 03:36:52 -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=W1ryyu6sNjyk3kkP/kowolHP9ZZ7RDX3lZWhnZIXMjY=; b=zp2HEOcTw0+TH94NHdEbY+Zakcw0voXmclHbX3+kn0mDmRc+TnGALajfNyGbhrSX7V sm59IfP2HzyFPL6/mwI+/hvmQIMfHex08zksXNN0xcgBdcCf0EFfsU1qqj/uFAqgZnZn NKAGZQc4w7JgsKq7Zbotq1lO23/3Qvdi+PNnWsKu3DwXWxJ0C6KJzTTehCl7uHoVgBTb r4W6dWP3DWiDArWwF/u1IL6poyHipBpuAoYTDDN10hnZ8/oh6+tLrgpCz33k9TuRuCfp Lw2v2FUptcFEenhA6BqZh1+FlmKgvEqVfHTs0f4/f0BNlZK382oedYbpOq0Qp/Td1p2k Qahw== X-Received: by 10.68.251.234 with SMTP id zn10mr952181pbc.188.1375353412642; Thu, 01 Aug 2013 03:36:52 -0700 (PDT) Received: from localhost.localdomain (shzdmzpr01-ext.sh.intel.com. [192.102.204.36]) by mx.google.com with ESMTPSA id r7sm3428559pao.18.2013.08.01.03.36.48 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 01 Aug 2013 03:36:51 -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, linux-acpi@vger.kernel.org, Rui Wang Subject: [RFC v2 2/3] I/O Hook: kernel interface to manage the hook Date: Thu, 1 Aug 2013 18:31:32 +0800 Message-Id: <1375353093-3206-3-git-send-email-rui.y.wang@intel.com> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <1375353093-3206-1-git-send-email-rui.y.wang@intel.com> References: <1375353093-3206-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.3 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=ham 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 Add the following kernel functions used to add/delete/query Register Overrides, and to start/stop the I/O Hook: hook_add_ovrd hook_query_ovrd hook_cleanup_ovrd hook_start_ovrd hook_stop_ovrd hook_get_status Signed-off-by: Rui Wang --- drivers/misc/iohook/iohook.c | 151 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 151 insertions(+), 0 deletions(-) diff --git a/drivers/misc/iohook/iohook.c b/drivers/misc/iohook/iohook.c index e410ece..be671d7 100644 --- a/drivers/misc/iohook/iohook.c +++ b/drivers/misc/iohook/iohook.c @@ -6,6 +6,7 @@ #include "iohook.h" static DEFINE_RAW_SPINLOCK(io_hook_lock); +static DEFINE_RAW_SPINLOCK(engine_lock); LIST_HEAD(ovrd_io_reg_map); LIST_HEAD(ovrd_mem_reg_map); @@ -14,6 +15,150 @@ 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; + +} + +/* 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); + } +} + +/* 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); + +} + +/* to start the hook */ +void hook_start_ovrd(void) +{ + unsigned long lock_flags = 0; + + raw_spin_lock_irqsave(&engine_lock, lock_flags); + if (g_ovrd_on) + goto done; + + static_key_slow_inc(&ovrdhw_enabled); + g_ovrd_on = 1; +done: + raw_spin_unlock_irqrestore(&engine_lock, lock_flags); + +} + +/* to stop the hook */ +void hook_stop_ovrd(void) +{ + unsigned long lock_flags = 0; + + raw_spin_lock_irqsave(&engine_lock, lock_flags); + if (!g_ovrd_on) + goto done; + g_ovrd_on = 0; + static_key_slow_dec(&ovrdhw_enabled); +done: + raw_spin_unlock_irqrestore(&engine_lock, lock_flags); + +} + +int hook_get_status(void) +{ + return g_ovrd_on; +} + /* len should only be 1, 2, 4, 8 */ static int mem_read(u64 address, int len, void *data) { @@ -165,6 +310,9 @@ int read_ovrd_common(int spaceid, u64 address, int len, void *value, void *bus) ret = -EINVAL; + if (!g_ovrd_on) + return ret; + if (spaceid == OVRD_SPACE_MEM) { /* in the case of memory, 'address' is virtual */ vaddr = address; @@ -295,6 +443,9 @@ int write_ovrd_common(int spaceid, u64 address, int len, void *data, void *bus) ret = -EINVAL; + if (!g_ovrd_on) + return ret; + if (spaceid == OVRD_SPACE_MEM) { ovrd_list = &ovrd_mem_reg_map; } else if (spaceid == OVRD_SPACE_IO) {