I/O Hook: Trace h/w access and emulate h/w events
Subsystems (6)
v3 cover I/O Hook: Trace h/w access and emulate h/w events
Hi All,
This is the 3rd version of I/O Hook, a patch set aimed at intercepting h/w access by the OS. Some examples of how it can be used:
1) To emulate h/w events (e.g. hotplug)
2) To inject h/w errors to the kernel
3) To trace h/w access by the OS for performance tuning or debugging.
Details of the examples can be found in Documentation/PCI/iohook.txt.
A set of user space tools using I/O Hook for various use cases will be hosted on https://github.com/iohook. It initially contains inject-aer which takes the trace event output of /sys/kernel/debug/tracing/events/ras/aer_event/ and regenerate the same PCIE AER event so that PCIE AER can be easily tested.
Changes since v2:
- Added the hook for MSRs (can specify a cpu number)
- Can trigger IPI by a vector (so that exceptions can be emulated)
- Defined iohook_event as the trace event to trace h/w access. A new attribute 'tc' can be defined for any Register Override for tracing purpose.
Rui Wang (5):
I/O Hook: core functions and Register Override
I/O Hook: Help functions to manage the hook
I/O Hook: sysfs interface to emulate h/w events
I/O Hook: Override MSRs while triggering MCEs
IO Hook: Tracing hw access
Documentation/PCI/iohook.txt | 353 ++++++++++ arch/Kconfig | 10 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/include/asm/io.h | 57 ++- arch/x86/include/asm/msr.h | 100 +++- arch/x86/lib/msr-smp.c | 31 + arch/x86/vdso/Makefile | 2 + drivers/misc/Makefile | 1 + drivers/misc/iohook/Makefile | 1 + drivers/misc/iohook/iohook.c | 1367 +++++++++++++++++++++++++++++++++++++ drivers/misc/iohook/iohook.h | 6 + drivers/pci/access.c | 66 ++ include/linux/reg_ovrd.h | 55 ++ include/trace/events/iohook.h | 58 ++ 14 files changed, 2101 insertions(+), 7 deletions(-) create mode 100644 Documentation/PCI/iohook.txt create mode 100644 drivers/misc/iohook/Makefile create mode 100644 drivers/misc/iohook/iohook.c create mode 100644 drivers/misc/iohook/iohook.h create mode 100644 include/linux/reg_ovrd.h create mode 100644 include/trace/events/iohook.h
Signature · 1 line
1.7.5.4
PATCH v3 1/5 I/O Hook: core functions and Register Override
This is the 3rd version of I/O Hook, a patch set aimed at intercepting h/w access by the OS. Some examples of how it can be used:
1) To emulate h/w events (e.g. hotplug)
2) To inject h/w errors to the kernel
3) To trace h/w access by the OS for performance tuning or debugging
Details of the examples can be found in Documentation/PCI/iohook.txt.
A set of user space tools using I/O Hook for various use cases will be hosted on https://github.com/iohook. It initially contains inject-aer which takes the trace event output of /sys/kernel/debug/tracing/events/ras/aer_event/ and regenerate the same PCIE AER event so that PCIE AER can be easily tested.
This patch provides a hook in the core h/w access functions in the kernel. It also introduces Register Override, which is a set of bits defined in RAM to override the real value of a h/w register. With the hook in place, access to h/w registers can be redirected to Register Overrides with user-defined values, so that h/w states can be emulated easily.
A Register Override can be defined in whatever bit-width, identified by its address, bitmask, initial value and attributs like read-only, read-write, write-clear, etc., similar to how a hardware register behaves when accessed.
Jump Label is used, so when the hook is disabled (by default), this adds only a NOP to the core functions, with zero performance penalty.
This patch is the first step towards the goal of emulating h/w events.
--- arch/Kconfig | 10 + arch/x86/boot/compressed/Makefile | 1 + arch/x86/include/asm/io.h | 57 +++++- arch/x86/vdso/Makefile | 2 + drivers/misc/Makefile | 1 + drivers/misc/iohook/Makefile | 1 + drivers/misc/iohook/iohook.c | 392 +++++++++++++++++++++++++++++++++++++ drivers/misc/iohook/iohook.h | 6 + drivers/pci/access.c | 66 ++++++ include/linux/reg_ovrd.h | 54 +++++ 10 files changed, 588 insertions(+), 2 deletions(-) create mode 100644 drivers/misc/iohook/Makefile create mode 100644 drivers/misc/iohook/iohook.c create mode 100644 drivers/misc/iohook/iohook.h create mode 100644 include/linux/reg_ovrd.h
arch/Kconfig +10 -0
@@ -46,6 +46,16 @@ config KPROBES for kernel debugging, non-intrusive instrumentation and testing. If in doubt, say "N". +config IO_HOOK + bool "Method for emulating hardware events" + default n + depends on PCI + help + I/O Hook is a mechanism to intercept i/o register access functions + in the kernel. By overriding h/w register bits with user-defined + bits in RAM called Register Override, it is possible to emulate + h/w states without modifying the driver specific to that hardware. + config JUMP_LABEL bool "Optimize very unlikely/likely branches" depends on HAVE_ARCH_JUMP_LABEL
arch/x86/boot/compressed/Makefile +1 -0
@@ -10,6 +10,7 @@ targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma \ KBUILD_CFLAGS := -m$(BITS) -D__KERNEL__ $(LINUX_INCLUDE) -O2 KBUILD_CFLAGS += -fno-strict-aliasing -fPIC KBUILD_CFLAGS += -DDISABLE_BRANCH_PROFILING +KBUILD_CFLAGS += -DNO_IO_HOOK cflags-$(CONFIG_X86_32) := -march=i386 cflags-$(CONFIG_X86_64) := -mcmodel=small KBUILD_CFLAGS += $(cflags-y)
arch/x86/include/asm/io.h +55 -2
84 lines not on this page · raw message
arch/x86/vdso/Makefile +2 -0
@@ -4,6 +4,8 @@ KBUILD_CFLAGS += $(DISABLE_LTO) +KBUILD_CFLAGS += -DNO_IO_HOOK + VDSO64-$(CONFIG_X86_64) := y VDSOX32-$(CONFIG_X86_X32_ABI) := y VDSO32-$(CONFIG_X86_32) := y
drivers/misc/Makefile +1 -0
@@ -49,6 +49,7 @@ obj-y += carma/ obj-$(CONFIG_USB_SWITCH_FSA9480) += fsa9480.o obj-$(CONFIG_ALTERA_STAPL) +=altera-stapl/ obj-$(CONFIG_INTEL_MEI) += mei/ +obj-$(CONFIG_IO_HOOK) += iohook/ obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/ obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o obj-$(CONFIG_SRAM) += sram.o
drivers/misc/iohook/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_IO_HOOK) += iohook.o
drivers/misc/iohook/iohook.c new file +392 -0
392 lines not on this page · raw message
drivers/misc/iohook/iohook.h new file +6 -0
@@ -0,0 +1,6 @@ +#ifndef _IOHOOK_H +#define _IOHOOK_H + +extern raw_spinlock_t pci_lock; + +#endif
drivers/pci/access.c +66 -0
96 lines not on this page · raw message
include/linux/reg_ovrd.h new file +54 -0
54 lines not on this page · raw message
Signature · 1 line
1.7.5.4
PATCH v3 2/5 I/O Hook: Help functions to manage the hook
In reply to Rui WangAdd the following kernel helper functions used to add/delete/query Register Overrides, and to start/stop the I/O Hook:
iohook_add_ovrd
iohook_query_ovrd
iohook_cleanup_ovrd
iohook_start_ovrd
iohook_stop_ovrd
iohook_get_status
--- drivers/misc/iohook/iohook.c | 151 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 151 insertions(+), 0 deletions(-)
drivers/misc/iohook/iohook.c +151 -0
@@ -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 *iohook_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 ("iohook_query_ovrd() returns %p, idx=%d\n" , + regentry, idx); +return regentry; + +} + +/* delete all Register Overrides from one space (IO/mem/pciconf) */ +void iohook_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 iohook_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 ("iohook_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 ("iohook_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 iohook_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 iohook_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 iohook_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) {
@@ -170,6 +315,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;
@@ -300,6 +448,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 ) {/* in the case of memory, 'address' is virtual */ vaddr = address;
Signature · 1 line
1.7.5.4
PATCH v3 3/5 I/O Hook: sysfs interface to emulate h/w events
In reply to Rui WangAdd sysfs interface used to emulate h/w events. Here's how it works:
The sysfs interface can be used to add/delete Register Overrides with user- defined values. The user can also specify which IRQ to be triggered via Inter-Processor Interrupt (IPI) while the h/w registers are being overridden. When the irq handler is triggered by the IPI it looks for the registers specific to some h/w events. As long as the Register Overrides are setup correctly, the irq handler will believe that the h/w is in a state corresponding to a predefined interrupt, thus process the event.
This can be typically used to generate ACPI events, PCI interrupts, PCIe AER injection etc., and can thus be used to help test RAS features like the hotplug of CPU/MEM/IOH on machines that are not capable of generating the events.
See Documentation/PCI/iohook.txt for usage details.
--- Documentation/PCI/iohook.txt | 282 ++++++++++++++++++++++++ drivers/misc/iohook/iohook.c | 490 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 772 insertions(+), 0 deletions(-) create mode 100644 Documentation/PCI/iohook.txt
Documentation/PCI/iohook.txt new file +282 -0
@@ -0,0 +1,282 @@ +Emulating h/w events via iohook +================================= + +1. Introduction +2. How to use it +3. Use cases + +1. Introduction +--------------------- +I/O Hook is a mechanism to intercept i/o register access functions in the +kernel. By overriding h/w register bits with user-defined bits in RAM called +Register Overrides, it is possible to emulate h/w states without modifying +the driver specific to that hardware. + +iohook is a driver that exports sysfs interface used to talk to the I/O Hook +in the kernel in order to emulate h/w events. Here's how it works: + +The sysfs interface can be used to add/delete Register Overrides with user- +defined values. The user can also specify which IRQ to be triggered via +Inter-Processor Interrupt (IPI) while the h/w registers are being over- +ridden. When the irq handler is triggered by the IPI it looks for the +registers specific to some h/w events. As long as the Register Overrides are +setup correctly, the irq handler will believe that the h/w is in a state +corresponding to a predefined interrupt, thus process the event. + +A Register Override can be defined in whatever bit-width, identified by its +address, bitmask, initial value and attributes like read-only, read-write, +write-clear, etc., similar to how a hardware register behaves when accessed. + +A Register Override may not use every bit in a byte. Its bitmask identifies +which bits are used (overridden). The unused bits are accessed on the h/w +and combined with the overridden bits to form the final result. The reason +to support this combination is that many h/w events are controlled by only +a few bits. For example the ACPI GPEx_STS and GPEx_EN are encoded such that +each bit represents a different General Purpose Event. The user is supposed +to fully understand the side-effect, if any, of reading adjacent bits when +he or she adds a Register Override not in the entirety of a byte, a word, a +dword, or a qword. + +iohook can be typically used to generate ACPI events, PCI interrupts, PCIe +AER injection etc., and can thus be used to help test RAS features like +the hotplug of CPU/MEM/IOH on machines that are not capable of generating +these events. + +2. How to use it +------------------- + +2.1 Kernel compilation and sysfs interface + +First compile the kernel with CONFIG_IO_HOOK. After the new kernel is loaded +a directory subtree is created under /sys/kernel/debug/iohook + +bash# cd /sys/kernel/debug/iohook +bash# ls +io irq mem pciconf trigger + +Each file is used to manage a type of resource. +'io' is used to add/show Register Overrides in IO port space. +'mem' is used to add/show Register Overrides in memory space. +'pciconf' is used to add/show Register Overrides in pci config space. +'irq' is used to set the desired IRQ to be triggered via IPI. +'trigger' is used to turn on/off the I/O Hook. + +2.2 Add Register Overrides + +A Register Override can be specified on the command line with the following +syntax (all numbers are in hex without space between each element) + +for a Register Override in IO port or memory space, it's specified as: + + address-length[value/mask]attribute + +for a Register Override in PCI config space, it's specified as: + + domain|bus:dev.func+offset-length[value/mask]attribute + +where + address - the 64bit address of the h/w register to be overridden + + length - the number of bytes affected. Affected here means that + at least one bit in that byte is overridden. For 'length' + less than 8, the overridden bits are determined by the + corresponding bits set in 'mask'. Other bits are unaffected + and accessed on the h/w. For 'length' >= 8 then 'mask' is + ignored and the entire range of bytes are overridden to be + a single value specified by the first byte of 'value'. This + can be used, for example, to set the entire PCI Config + space of a device to 0xff. + + value - the user-defined value to replace the content of the + corresponding h/w register. for 'length' < 8, only the bits + masked by 'mask' are used. + + mask - is the bit-mask specifying the bits to be overridden when + 'length' < 8. + + attribute - used to specify the attribute of the overridden bits. + It can be ro, rw, wc, rc to mean read-only, read-write, + write-clear, and read-clear respectively. + + domain - pci domain number + bus - pci bus number + dev - pci device number + func - pci function number + offset - used to specify the offset of the affected bytes in the + PCI config space. + +Multiple registers can be specified on one line with each separated by at +least one space. For example, to override two registers in IO space at port +0x420 and port 0x428, with the former in write-clear mode and the latter in +read-only mode: + +bash# cd /sys/kernel/debug/iohook +bash# echo "420-1[04/04]wc 428-1[04/04]ro" > io +The syntax is "address-length[value/mask]attribute". +Since only one bit is overridden (mask is 0x04), the affected byte is 1. +So 'length' is 1. + +As another example, to add two Register Overrides in the PCI config space of +device 00:05.0 at offsets 0x130 and 0x134 respectively: + +bash# echo "0000|00:05.0+130-1[01/01]wc 0000|00:05.0+134-2[0500/ffff]ro">pciconf +The syntax is "domain|bus:dev.func+offset-length[value/mask]attribute" +The first register overrides only bit0 and the second register overrides the +first 2 bytes (mask == 0xffff), with an initial value of 0x0500. + +Register Overrides are disabled when added. They can be enabled by using the +'trigger' file. See below. + +2.3 Add IRQ and enable the Register Overrides + +To specify an IRQ to be triggered via IPI, just echo the IRQ number in decimal +to the 'irq' file. For example: + +bash# cd /sys/kernel/debug/iohook +bash# echo 9 > irq +This specifies that IRQ9 be triggered after the Register Overrides are enabled. + +To enable the Register Overrides in the kernel: + +bash# echo 1 > trigger + +This immediately enables all the Register Overrides and if an IRQ number was +specified, generate the IPI. + +To disable the Register Overrides in the kernel: + +bash echo 0 > trigger +This immediately disables all Register Overrides. The kernel starts to see +real h/w registers again. This does not delete the Register Overrides. They +can be re-enabled again by echo 1 > trigger. + +3. Use cases +----------------- + +3.1 Generate ACPI Events + +A typical use case is to generate ACPI events. Suppose we want to test IOH +hotplug on a machine whose BIOS doesn't support it. We can override its DSDT +and add a GPE to notify the OS the hot-add/removal of the IOH device. We can +then use iohook to trigger the imaginary GPE and the OS will have to process +the hotplug event. (For detailed instructions on how to override DSDT see +Documentation/acpi/initrd_table_override.txt.) The following is an example: + +We first extract the DSDT +bash # cat /sys/firmware/acpi/tables/DSDT > DSDT +bash # iasl -d DSDT +Now we have disassembled the DSDT into DSDT.dsl. We vi DSDT.dsl and notice +that there's a IOH device named \_SB.IOH1. Since GPEs are named _Lxx with xx +being the GPE numbers, we notice that there's no _L02 in DSDT.dsl, so we can +use this spare GPE to notify the OS the hotplug of \_SB.IOH1. We add a new +method in DSDT.dsl: + + Method (_L02, 0, NotSerialized) // _Lxx: Level-Triggered GPE + { + Notify (\_SB.IOH1, 0x0) // 0x0: hot-add event + } + +ACPI uses a single interrupt (SCI) to dispatch all GPEs. The SCI IRQ number +can be found from: + +bash # grep acpi /proc/interrupts | awk '{ print $1; }' +9: + +which means SCI is IRQ9. What we need to do is to generate IRQ9 via IPI and +cause the SCI interrupt handler to call _L02. How can we do that? Each _Lxx +has a status bit in GPEx_STS to reflect if it is asserted and a controlling +bit in GPEx_EN to reflect if it is enabled. The SCI interrupt handler reads +GPEx_STS and GPEx_EN to decide whether to call a _Lxx. A _Lxx is called if +it is both enabled and asserted. We can use Register Overrides to override +the bits controlling _L02 in GPEx_STS and GPEx_EN so that the SCI handler +will believe that _L02 is asserted and enabled, thus it will call the ACPI +method that we added. + +_L02's controlling bits are in GPE0_STS and GPE0_EN, whose addresses can be +found from FACP as follows. + +bash # cat /sys/firmware/acpi/tables/FACP > FACP +bash # iasl -d FACP +bash # grep GPE FACP.dsl +[050h 080 4] GPE0 Block Address : 00000420 +[054h 084 4] GPE1 Block Address : 00000000 +[05Ch 092 1] GPE0 Block Length : 10 + +So according to FACP, GPE0 block is at port 0x420; GPE0 block length is 0x10. +GPE0_STS/GPE0_EN each occupies half the block length, with GPE0_STS at 0x420 +and GPE0_EN at 0x428. _L02 is controlled by bit2 of each of them. We need to +override bit2 of both IO port 0x420 and IO port 0x428. + +Before adding the Register Override we need to replace the DSDT provided by +BIOS with our modified DSDT.dsl, by following initrd_table_override.txt. +Once the new DSDT is injected into initrd we reboot the system and add the +Register Override as follows. + +bash # cd /sys/kernel/debug/iohook/ +bash # echo "420-1[4/4]wc 428-1[4/4]ro" > io +bash # echo 9 > irq +bash # echo 1 > trigger + +The last command immediately triggers the _L02 method that we provided and +Linux sees a hot-add ACPI event for the IOH device. + + +3.2 Generate PCIe Native Hotplug + +The pciehp driver allocates an irq to handle each native PCIe hotplug slot. +The driver prints the status of each hotplug slot when debugging is enabled +so we can see which irq it's using. + +bash # modprobe pciehp pciehp_debug=1 + +dmesg shows: + +pciehp 0000:00:1c.0:pcie04: Hotplug Controller: +pciehp 0000:00:1c.0:pcie04: Seg/Bus/Dev/Func/IRQ : 0000:00:1c.0 IRQ 70 + +So we pick this hotplug slot at 00:1c.0 which uses IRQ70. Its PCIe Slot +Status Register is at offset 0x5a of its pci config space. We can easily +inject a Hot-Add (Presence Detect) event into the system by adding a +Register Override to override the register at offset 0x5a in the PCI config +space of 00:1c.0 + +bash # cd /sys/kernel/debug/iohook/ +bash # echo "00|00:1c.0+5a-1[48/ff]wc" > pciconf +bash # echo 70 > irq +bash # echo 1 > trigger + +dmesg shows: + +pciehp 0000:00:1c.0:pcie04: Card present on Slot(1) +pciehp 0000:00:1c.0:pcie04: Device 0000:0f:00.0 already exists at 0000:0f:00, +cannot hot-add +pciehp 0000:00:1c.0:pcie04: Cannot add device at 0000:0f:00 + +We can also inject an Attention Button Pressed event: + +bash # echo "00|00:1c.0+5a-1[41/ff]wc" > pciconf +bash # echo 70 > irq +bash # echo 1 > trigger + +dmesg shows: + +pciehp 0000:00:1c.0:pcie04: Button pressed on Slot(1) +pciehp 0000:00:1c.0:pcie04: PCI slot #1 - powering off due to button press + +3.3 PCIe AER error injection + +The aerdrv driver allocates a few irqs to handle AER. Each of them handles a +PCIe root port device. In an example aerdrv uses irq66 to handle AER on root +port 00:05.0. As seen in lspci, the AER capability is at offset 0x100 of its +pci config space. So the Root Error Status Register is at offset 0x130, and +the Error Source Identification Register is at offset 0x134. We can inject a +Correctable Error with a source id to identify its child at 05:00.0. + +bash # cd /sys/kernel/debug/iohook/ +bash # echo "00|00:05.0+130-1[01/01]wc 00|00:05.0+134-2[0500/ffff]ro" > pciconf +bash # echo 66 > irq +bash # echo 1 > trigger + +dmesg shows: +pcieport 0000:00:05.0: AER: Corrected error received: id=0500 +
drivers/misc/iohook/iohook.c +490 -0
503 lines not on this page · raw message
Signature · 1 line
1.7.5.4
PATCH v3 4/5 I/O Hook: Override MSRs while triggering MCEs
In reply to Rui WangMSRs can be overriden by specifying a cpu id and an MSR number.
bash# echo "0:17a-8[4/4]ro" > msr
would override MSR 0x17a on cpu0. Use 'all' as the cpu id to override the MSR on all CPUs. e.g. "all:17a-8[4/4]ro"
When specifying the interrupt to be triggered, a positive number denotes an IRQ and a negative number denotes a vector, thus
bash# echo -18 > irq
specifies that a machine check exception (int 18) will be triggered.
--- Documentation/PCI/iohook.txt | 31 ++++++-- arch/x86/include/asm/msr.h | 100 ++++++++++++++++++++- arch/x86/lib/msr-smp.c | 31 +++++++ drivers/misc/iohook/iohook.c | 195 +++++++++++++++++++++++++++++++++++++++--- include/linux/reg_ovrd.h | 1 + 5 files changed, 335 insertions(+), 23 deletions(-)
Documentation/PCI/iohook.txt +25 -6
@@ -52,11 +52,12 @@ a directory subtree is created under /sys/kernel/debug/iohook bash# cd /sys/kernel/debug/iohook bash# ls -io irq mem pciconf trigger +io irq mem msr pciconf trigger Each file is used to manage a type of resource. 'io' is used to add/show Register Overrides in IO port space. 'mem' is used to add/show Register Overrides in memory space. +'msr' is used to add/show Register Overrides in MSR space. 'pciconf' is used to add/show Register Overrides in pci config space. 'irq' is used to set the desired IRQ to be triggered via IPI. 'trigger' is used to turn on/off the I/O Hook.
@@ -74,8 +75,15 @@ for a Register Override in PCI config space, it's specified as: domain|bus:dev.func+offset-length[value/mask]attribute +for a Register Override in MSR space, it's specified as: + + cpuid:regnum-length[value/mask]attribute + where address - the 64bit address of the h/w register to be overridden + cpuid - the cpu on which the MSR specified by regnum is to be + overriden. Use 'all' as the cpuid to specify all cpus. + regnum - the MSR to be overriden. length - the number of bytes affected. Affected here means that at least one bit in that byte is overridden. For 'length'
@@ -124,24 +132,35 @@ The syntax is "domain|bus:dev.func+offset-length[value/mask]attribute" The first register overrides only bit0 and the second register overrides the first 2 bytes (mask == 0xffff), with an initial value of 0x0500. +As another example, MSRs can be overriden by specifying a cpu id and an MSR +number: + +bash# echo "0:17a-8[4/4]ro" > msr +The above specifies that bit2 of MSR 0x17a on cpu0 is overriden as 1 (bit set). + Register Overrides are disabled when added. They can be enabled by using the 'trigger' file. See below. 2.3 Add IRQ and enable the Register Overrides -To specify an IRQ to be triggered via IPI, just echo the IRQ number in decimal -to the 'irq' file. For example: +To specify an interrupt to be triggered via IPI, just echo the IRQ or vector +number in decimal to the 'irq' file. A positive number denotes an IRQ and a +negative number denotes a vector. For example: bash# cd /sys/kernel/debug/iohook bash# echo 9 > irq This specifies that IRQ9 be triggered after the Register Overrides are enabled. -To enable the Register Overrides in the kernel: +bash# echo -18 > irq +specifies that a machine check exception(vector 18) will be triggered. + +Register Overrides are disabled after added. Use the 'trigger' file to enabled +them all: bash# echo 1 > trigger -This immediately enables all the Register Overrides and if an IRQ number was -specified, generate the IPI. +This immediately enables all the Register Overrides and if an IRQ (or vector) +number was specified, generate the IPI. To disable the Register Overrides in the kernel:
arch/x86/include/asm/msr.h +95 -5
129 lines not on this page · raw message
arch/x86/lib/msr-smp.c +31 -0
@@ -128,6 +128,33 @@ void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs) }EXPORT_SYMBOL (rdmsr_on_cpus); +#if !defined(NO_IO_HOOK ) && defined(CONFIG_IO_HOOK ) + +#define rdmsr_safe_regs_ovrd (rv) \ +{ \ + u64 val;\ + if (arch_static_branch(&ovrdhw_enabled)) { \ + if (!read_ovrd_common(OVRD_SPACE_MSR, 0, \ + sizeof(u64), &val, rv)) \ + return; \ + } \ +} + +#define wrmsr_safe_regs_ovrd (rv) \ +{ \ + u64 val;\ + if (arch_static_branch(&ovrdhw_enabled)) { \ + if (!write_ovrd_common(OVRD_SPACE_MSR, 0, \ + sizeof(u64), &val, rv)) \ + return; \ + } \ +} + +#else +#define rdmsr_safe_regs_ovrd (rv) +#define wrmsr_safe_regs_ovrd (rv) +#endif +/* * wrmsr on a bunch of CPUs *
@@ -229,6 +256,8 @@ static void __rdmsr_safe_regs_on_cpu(void *info) {struct msr_regs_info *rv = info; +rdmsr_safe_regs_ovrd (rv); + rv->err =rdmsr_safe_regs (rv->regs); }
@@ -236,6 +265,8 @@ static void __wrmsr_safe_regs_on_cpu(void *info) {struct msr_regs_info *rv = info; +wrmsr_safe_regs_ovrd (rv); + rv->err =wrmsr_safe_regs (rv->regs); }
drivers/misc/iohook/iohook.c +183 -12
347 lines not on this page · raw message
include/linux/reg_ovrd.h +1 -0
@@ -37,6 +37,7 @@ struct reg_ovrd {#define OVRD_SPACE_IO 0#define OVRD_SPACE_MEM 1#define OVRD_SPACE_PCICONF 2 +#define OVRD_SPACE_MSR 3#define PCI_ENCODE_ADDR (domain, bus, devfn, pos) \ (((u64)(domain))<<32|(bus)<<20|(devfn)<<12|(pos))
Signature · 1 line
1.7.5.4
PATCH v3 5/5 IO Hook: Tracing hw access
In reply to Rui WangA new attribute "tc" can be defined for any Register Override so that IO Hook can report each read from (or write to) the specified area in mem, io, msr, and pciconf spaces. A trace event named 'iohook_event' is defined to report the accesses. For example
echo "0x383ffff00000-4000[0/0]tc" > /sys/kernel/debug/tracing/iohook/mem
or
trace_event=iohook_event iohook.mem="383ffff00000-4000[0/0]tc" as bootparameters in grub can be used to trace any access to physical address region starting from 0x383ffff00000 with a length of 0x4000. A typical trace event output looks like the following:
bash $ sudo cat /sys/kernel/debug/tracing/trace
...
swapper/0-1 [018] .... 11.594157: iohook_event: pci_read_config(0000:00:14.00, 0x6, 0x2) <== 0x290
swapper/0-1 [018] .... 11.599914: iohook_event: pci_read_config(0000:00:14.00, 0x34, 0x1) <== 0x70
modprobe-613 [013] d... 12.447408: iohook_event: pci_write_config(0000:00:14.00, 0x4, 0x2) ==> 0x6
modprobe-613 [013] .... 12.452565: iohook_event: pci_read_config(0000:00:14.00, 0xd, 0x1) <== 0x0
modprobe-613 [013] d... 12.458302: iohook_event: pci_write_config(0000:00:14.00, 0xd, 0x1) ==> 0x40
modprobe-613 [013] .... 12.477935: iohook_event: read(0x383ffff00000, 0x4) <== 0x1000080
modprobe-613 [013] .... 12.477936: iohook_event: read(0x383ffff00018, 0x4) <== 0x2000
modprobe-613 [013] d... 12.478001: iohook_event: write(0x383ffff02030, 0x4) ==> 0x37822000
modprobe-613 [013] d... 12.478001: iohook_event: write(0x383ffff02034, 0x4) ==> 0x0
...
--- Documentation/PCI/iohook.txt | 54 +++++++- drivers/misc/iohook/iohook.c | 319 +++++++++++++++++++++++++++++++---------- include/trace/events/iohook.h | 58 ++++++++ 3 files changed, 352 insertions(+), 79 deletions(-) create mode 100644 include/trace/events/iohook.h
Documentation/PCI/iohook.txt +53 -1
@@ -104,7 +104,10 @@ where attribute - used to specify the attribute of the overridden bits. It can be ro, rw, wc, rc to mean read-only, read-write, - write-clear, and read-clear respectively. + write-clear, and read-clear respectively. A special attr- + ibute tc is defined to trace hw access through the trace + event named iohook_event. When tc is used as the attribute + value/mask are ignored. domain - pci domain number bus - pci bus number
@@ -299,3 +302,52 @@ bash # echo 1 > trigger dmesg shows: pcieport 0000:00:05.0: AER: Corrected error received: id=0500 +3.4 Tracing hardware access by the OS + +I/O Hook can be used to trace access to hw registers by the OS. When compared +to mmio trace, this IO Hook trace has both benefits and shortcomings: + a) I/O Hook is SMP safe while mmio trace isn't. + b) mmio trace can trace arbitrary assembly instruction accessing + arbitrary address, while IO Hook can only trace standard + functions like readl(), inb(), pci_read_config_byte(), etc. + C) I/O Hook can trace IO ports and PCI Config space, while mmio trace + cannot. + +A trace event named 'iohook_event' is defined to report the accesses. A +special attribute tc can be used to specify the register to be traced. +For example in order to trace access to physical address region starting +from 0x383ffff00000 with a length of 0x4000, the following cmd sequence is used: + +bash # echo "0x383ffff00000-4000[0/0]tc" > /sys/kernel/debug/tracing/iohook/mem +bash # echo 1 > /sys/kernel/debug/iohook/trigger + +To trace hw access during boot, the following boot parameters can be used in +grub: + iohook.mem, iohook.io, iohook.pciconf, iohook.msr +For example + trace_event=iohook_event iohook.mem="383ffff00000-4000[0/0]tc" as boot +parameters in grub can be used to trace the same physical address region +starting from 0x383ffff00000. A typical trace event output looks like the +following: + +bash $ sudo cat /sys/kernel/debug/tracing/trace +... +swapper/0-1 [018] .... 11.594157: iohook_event: pci_read_config(0000:00:14.00, 0x6, 0x2) <== 0x290 + +swapper/0-1 [018] .... 11.599914: iohook_event: pci_read_config(0000:00:14.00, 0x34, 0x1) <== 0x70 + +modprobe-613 [013] d... 12.447408: iohook_event: pci_write_config(0000:00:14.00, 0x4, 0x2) ==> 0x6 + +modprobe-613 [013] .... 12.452565: iohook_event: pci_read_config(0000:00:14.00, 0xd, 0x1) <== 0x0 + +modprobe-613 [013] d... 12.458302: iohook_event: pci_write_config(0000:00:14.00, 0xd, 0x1) ==> 0x40 + +modprobe-613 [013] .... 12.477935: iohook_event: read(0x383ffff00000, 0x4) <== 0x1000080 + +modprobe-613 [013] .... 12.477936: iohook_event: read(0x383ffff00018, 0x4) <== 0x2000 + +modprobe-613 [013] d... 12.478001: iohook_event: write(0x383ffff02030, 0x4) ==> 0x37822000 + +modprobe-613 [013] d... 12.478001: iohook_event: write(0x383ffff02034, 0x4) ==> 0x0 +... +
drivers/misc/iohook/iohook.c +241 -78
450 lines not on this page · raw message
include/trace/events/iohook.h new file +58 -0
58 lines not on this page · raw message
Signature · 1 line
1.7.5.4