Add option to hook device reset through ftrace

pull/1/head
Adam Madsen 4 years ago
parent afbc509011
commit 74ea6dd018

@ -1,2 +1,2 @@
vendor-reset-y += src/vendor-reset.o
vendor-reset-y += src/vendor-reset.o src/ftrace.o src/hooks.o
ccflags-y += -I$(src)/src

@ -18,63 +18,68 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "amd/amd.h"
static struct vendor_reset_cfg vendor_reset_devices[] =
{
/* AMD Polaris 10 */
{PCI_VENDOR_ID_ATI, 0x67C0, &amd_polaris10_ops},
{PCI_VENDOR_ID_ATI, 0x67C1, &amd_polaris10_ops},
{PCI_VENDOR_ID_ATI, 0x67C2, &amd_polaris10_ops},
{PCI_VENDOR_ID_ATI, 0x67C4, &amd_polaris10_ops},
{PCI_VENDOR_ID_ATI, 0x67C7, &amd_polaris10_ops},
{PCI_VENDOR_ID_ATI, 0x67D0, &amd_polaris10_ops},
{PCI_VENDOR_ID_ATI, 0x67DF, &amd_polaris10_ops},
{PCI_VENDOR_ID_ATI, 0x67C8, &amd_polaris10_ops},
{PCI_VENDOR_ID_ATI, 0x67C9, &amd_polaris10_ops},
{PCI_VENDOR_ID_ATI, 0x67CA, &amd_polaris10_ops},
{PCI_VENDOR_ID_ATI, 0x67CC, &amd_polaris10_ops},
{PCI_VENDOR_ID_ATI, 0x67CF, &amd_polaris10_ops},
{PCI_VENDOR_ID_ATI, 0x6FDF, &amd_polaris10_ops},
#define AMD_POLARIS10(op) \
{PCI_VENDOR_ID_ATI, 0x67C0, op}, \
{PCI_VENDOR_ID_ATI, 0x67C1, op}, \
{PCI_VENDOR_ID_ATI, 0x67C2, op}, \
{PCI_VENDOR_ID_ATI, 0x67C4, op}, \
{PCI_VENDOR_ID_ATI, 0x67C7, op}, \
{PCI_VENDOR_ID_ATI, 0x67D0, op}, \
{PCI_VENDOR_ID_ATI, 0x67DF, op}, \
{PCI_VENDOR_ID_ATI, 0x67C8, op}, \
{PCI_VENDOR_ID_ATI, 0x67C9, op}, \
{PCI_VENDOR_ID_ATI, 0x67CA, op}, \
{PCI_VENDOR_ID_ATI, 0x67CC, op}, \
{PCI_VENDOR_ID_ATI, 0x67CF, op}, \
{PCI_VENDOR_ID_ATI, 0x6FDF, op}
/* AMD Vega 10 */
{PCI_VENDOR_ID_ATI, 0x6860, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x6861, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x6862, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x6863, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x6864, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x6867, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x6868, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x6869, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x686a, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x686b, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x686c, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x686d, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x686e, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x686f, &amd_vega10_ops},
{PCI_VENDOR_ID_ATI, 0x687f, &amd_vega10_ops},
#define AMD_VEGA10(op) \
{PCI_VENDOR_ID_ATI, 0x6860, op}, \
{PCI_VENDOR_ID_ATI, 0x6861, op}, \
{PCI_VENDOR_ID_ATI, 0x6862, op}, \
{PCI_VENDOR_ID_ATI, 0x6863, op}, \
{PCI_VENDOR_ID_ATI, 0x6864, op}, \
{PCI_VENDOR_ID_ATI, 0x6867, op}, \
{PCI_VENDOR_ID_ATI, 0x6868, op}, \
{PCI_VENDOR_ID_ATI, 0x6869, op}, \
{PCI_VENDOR_ID_ATI, 0x686a, op}, \
{PCI_VENDOR_ID_ATI, 0x686b, op}, \
{PCI_VENDOR_ID_ATI, 0x686c, op}, \
{PCI_VENDOR_ID_ATI, 0x686d, op}, \
{PCI_VENDOR_ID_ATI, 0x686e, op}, \
{PCI_VENDOR_ID_ATI, 0x686f, op}, \
{PCI_VENDOR_ID_ATI, 0x687f, op}
/* AMD Vega 20 */
{PCI_VENDOR_ID_ATI, 0x66a0, &amd_vega20_ops},
{PCI_VENDOR_ID_ATI, 0x66a1, &amd_vega20_ops},
{PCI_VENDOR_ID_ATI, 0x66a2, &amd_vega20_ops},
{PCI_VENDOR_ID_ATI, 0x66a3, &amd_vega20_ops},
{PCI_VENDOR_ID_ATI, 0x66a4, &amd_vega20_ops},
{PCI_VENDOR_ID_ATI, 0x66a7, &amd_vega20_ops},
{PCI_VENDOR_ID_ATI, 0x66af, &amd_vega20_ops},
#define AMD_VEGA20(op) \
{PCI_VENDOR_ID_ATI, 0x66a0, op}, \
{PCI_VENDOR_ID_ATI, 0x66a1, op}, \
{PCI_VENDOR_ID_ATI, 0x66a2, op}, \
{PCI_VENDOR_ID_ATI, 0x66a3, op}, \
{PCI_VENDOR_ID_ATI, 0x66a4, op}, \
{PCI_VENDOR_ID_ATI, 0x66a7, op}, \
{PCI_VENDOR_ID_ATI, 0x66af, op}
/* AMD Navi 10 */
{PCI_VENDOR_ID_ATI, 0x7310, &amd_navi10_ops},
{PCI_VENDOR_ID_ATI, 0x7312, &amd_navi10_ops},
{PCI_VENDOR_ID_ATI, 0x7318, &amd_navi10_ops},
{PCI_VENDOR_ID_ATI, 0x7319, &amd_navi10_ops},
{PCI_VENDOR_ID_ATI, 0x731a, &amd_navi10_ops},
{PCI_VENDOR_ID_ATI, 0x731b, &amd_navi10_ops},
{PCI_VENDOR_ID_ATI, 0x731e, &amd_navi10_ops},
{PCI_VENDOR_ID_ATI, 0x731f, &amd_navi10_ops},
#define AMD_NAVI10(op) \
{PCI_VENDOR_ID_ATI, 0x7310, op}, \
{PCI_VENDOR_ID_ATI, 0x7312, op}, \
{PCI_VENDOR_ID_ATI, 0x7318, op}, \
{PCI_VENDOR_ID_ATI, 0x7319, op}, \
{PCI_VENDOR_ID_ATI, 0x731a, op}, \
{PCI_VENDOR_ID_ATI, 0x731b, op}, \
{PCI_VENDOR_ID_ATI, 0x731e, op}, \
{PCI_VENDOR_ID_ATI, 0x731f, op}
/* AMD Navi 14 */
{PCI_VENDOR_ID_ATI, 0x7340, &amd_navi10_ops},
{PCI_VENDOR_ID_ATI, 0x7341, &amd_navi10_ops},
{PCI_VENDOR_ID_ATI, 0x7347, &amd_navi10_ops},
{PCI_VENDOR_ID_ATI, 0x734F, &amd_navi10_ops},
#define AMD_NAVI14(op) \
{PCI_VENDOR_ID_ATI, 0x7340, op}, \
{PCI_VENDOR_ID_ATI, 0x7341, op}, \
{PCI_VENDOR_ID_ATI, 0x7347, op}, \
{PCI_VENDOR_ID_ATI, 0x734F, op}
{0}};
static struct vendor_reset_cfg vendor_reset_devices[] = {
AMD_POLARIS10(&amd_polaris10_ops),
AMD_VEGA10(&amd_vega10_ops),
AMD_VEGA20(&amd_vega20_ops),
AMD_NAVI10(&amd_navi10_ops),
AMD_NAVI14(&amd_navi10_ops),
{0},
};

@ -0,0 +1,123 @@
/*
Vendor Reset - Vendor Specific Reset
Copyright (C) 2020 Geoffrey McRae <geoff@hostfission.com>
Copyright (C) 2020 Adam Madsen <adam@ajmadsen.com>
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
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, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/kallsyms.h>
#include <linux/kernel.h>
#include <linux/ftrace.h>
#include <linux/kprobes.h>
#include <linux/pci.h>
#include "ftrace.h"
/*
* Honestly the whole removal of kallsyms_lookup_name is hella fishy, Google.
* Thank you, Filip Pynckels
* http://users.telenet.be/pynckels/2020-2-Linux-kernel-unexported-kallsyms-functions.pdf
*/
static int resolve_hook_address(struct ftrace_hook *hook)
{
struct kprobe kp;
kp.symbol_name = hook->name;
if (register_kprobe(&kp))
{
pr_warn("unresolved symbol %s\n", hook->name);
return -ENOENT;
}
hook->address = (unsigned long)kp.addr;
unregister_kprobe(&kp);
*((unsigned long *)hook->original) = hook->address;
return 0;
}
static void notrace fh_trace_thunk(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *ops, struct pt_regs *regs)
{
struct ftrace_hook *hook = to_ftrace_hook(ops);
if (!within_module(parent_ip, THIS_MODULE))
regs->ip = (unsigned long)hook->function;
}
int fh_install_hook(struct ftrace_hook *hook)
{
int err;
err = resolve_hook_address(hook);
if (err)
return err;
hook->ops.func = fh_trace_thunk;
hook->ops.flags = FTRACE_OPS_FL_SAVE_REGS |
FTRACE_OPS_FL_IPMODIFY;
err = ftrace_set_filter_ip(&hook->ops, hook->address, 0, 0);
if (err)
{
pr_warn("ftrace_set_filter_ip() failed: %d\n", err);
return err;
}
err = register_ftrace_function(&hook->ops);
if (err)
{
pr_warn("register_ftrace_function() failed in fh_install_hook: %d\n", err);
ftrace_set_filter_ip(&hook->ops, hook->address, 1, 0);
return err;
}
return 0;
}
void fh_remove_hook(struct ftrace_hook *hook)
{
int err;
err = unregister_ftrace_function(&hook->ops);
if (err)
{
pr_warn("unregister_ftrace_function() failed: %d\n", err);
}
err = ftrace_set_filter_ip(&hook->ops, hook->address, 1, 0);
if (err)
{
pr_warn("ftrace_set_filter_ip() failed in fh_remove_hook: %d\n", err);
}
}
int fh_install_hooks(struct ftrace_hook *hooks)
{
int ret;
struct ftrace_hook *hook;
for (hook = hooks; hook->name; ++hook)
if ((ret = fh_install_hook(hook)))
return ret;
return 0;
}
void fh_remove_hooks(struct ftrace_hook *hooks)
{
struct ftrace_hook *hook;
for (hook = hooks; hook->name; ++hook)
fh_remove_hook(hook);
}

@ -0,0 +1,48 @@
/*
Vendor Reset - Vendor Specific Reset
Copyright (C) 2020 Geoffrey McRae <geoff@hostfission.com>
Copyright (C) 2020 Adam Madsen <adam@ajmadsen.com>
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
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, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __VENDOR_RESET_FTRACE_H__
#define __VENDOR_RESET_FTRACE_H__
#include <linux/ftrace.h>
struct ftrace_hook
{
const char *name;
void *function;
void *original;
unsigned long address;
struct ftrace_ops ops;
};
#define to_ftrace_hook(ops) container_of(ops, struct ftrace_hook, ops)
#define HOOK(_name, orig, replacement) \
{ \
.name = (_name), \
.original = (orig), \
.function = (replacement), \
}
int fh_install_hooks(struct ftrace_hook *hooks);
void fh_remove_hooks(struct ftrace_hook *hooks);
#endif

@ -0,0 +1,66 @@
/*
Vendor Reset - Vendor Specific Reset
Copyright (C) 2020 Geoffrey McRae <geoff@hostfission.com>
Copyright (C) 2020 Adam Madsen <adam@ajmadsen.com>
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
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, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/kernel.h>
#include <linux/pci.h>
#include "vendor-reset-dev.h"
#include "device-db.h"
#include "ftrace.h"
#include "hooks.h"
static int (*orig_pci_dev_specific_reset)(struct pci_dev *dev, int probe);
static int hooked_pci_dev_specific_reset(struct pci_dev *dev, int probe)
{
int ret;
struct vendor_reset_cfg *cfg;
struct vendor_reset_dev vdev;
ret = orig_pci_dev_specific_reset(dev, probe);
if (!ret || ret != -ENOTTY)
return ret;
for (cfg = vendor_reset_devices; cfg->vendor; ++cfg)
if ((cfg->vendor == dev->vendor || cfg->vendor == PCI_ANY_ID) && (cfg->device == dev->device || cfg->device == PCI_ANY_ID))
break;
if (cfg->vendor)
{
vdev.pdev = dev;
if (cfg->ops->pre_reset && (ret = cfg->ops->pre_reset(&vdev)) && ret)
return ret;
ret = vdev.reset_ret = cfg->ops->reset(&vdev);
if (ret)
pr_warn("failed to reset device: %d\n", ret);
if (cfg->ops->post_reset)
ret = cfg->ops->post_reset(&vdev);
return ret;
}
return -ENOTTY;
}
struct ftrace_hook fh_hooks[] = {
HOOK("pci_dev_specific_reset", &orig_pci_dev_specific_reset, hooked_pci_dev_specific_reset),
{0},
};

@ -0,0 +1,27 @@
/*
Vendor Reset - Vendor Specific Reset
Copyright (C) 2020 Geoffrey McRae <geoff@hostfission.com>
Copyright (C) 2020 Adam Madsen <adam@ajmadsen.com>
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
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, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __VENDOR_RESET_HOOKS_H__
#define __VENDOR_RESET_HOOKS_H__
#include <linux/types.h>
extern struct ftrace_hook fh_hooks[];
#endif

@ -28,8 +28,14 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "device-db.h"
#include "ftrace.h"
#include "hooks.h"
#define VENDOR_RESET_DEVNAME "vendor_reset"
static bool install_hook = false;
module_param(install_hook, bool, 0);
static long vendor_reset_ioctl_reset(struct file * filp, unsigned long arg)
{
struct vendor_reset_ioctl dev;
@ -145,12 +151,24 @@ static struct miscdevice vendor_reset_misc =
static int __init vendor_reset_init(void)
{
return misc_register(&vendor_reset_misc);
int ret;
ret = misc_register(&vendor_reset_misc);
if (ret)
return ret;
if (install_hook)
ret = fh_install_hooks(fh_hooks);
return ret;
}
static void __exit vendor_reset_exit(void)
{
misc_deregister(&vendor_reset_misc);
if (install_hook)
fh_remove_hooks(fh_hooks);
}
module_init(vendor_reset_init);

Loading…
Cancel
Save