Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 1 | /* Support for MMIO probes. |
| 2 | * Benfit many code from kprobes |
| 3 | * (C) 2002 Louis Zhuang <louis.zhuang@intel.com>. |
| 4 | * 2007 Alexander Eichner |
| 5 | * 2008 Pekka Paalanen <pq@iki.fi> |
| 6 | */ |
| 7 | |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 8 | #include <linux/list.h> |
Ingo Molnar | 668a6c3 | 2008-05-19 13:35:24 +0200 | [diff] [blame] | 9 | #include <linux/rculist.h> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 10 | #include <linux/spinlock.h> |
| 11 | #include <linux/hash.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 15 | #include <linux/uaccess.h> |
| 16 | #include <linux/ptrace.h> |
| 17 | #include <linux/preempt.h> |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 18 | #include <linux/percpu.h> |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 19 | #include <linux/kdebug.h> |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 20 | #include <linux/mutex.h> |
Pekka Paalanen | 970e6fa | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 21 | #include <linux/io.h> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 22 | #include <asm/cacheflush.h> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 23 | #include <asm/tlbflush.h> |
Pekka Paalanen | 970e6fa | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 24 | #include <linux/errno.h> |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 25 | #include <asm/debugreg.h> |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 26 | #include <linux/mmiotrace.h> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 27 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 28 | #define KMMIO_PAGE_HASH_BITS 4 |
| 29 | #define KMMIO_PAGE_TABLE_SIZE (1 << KMMIO_PAGE_HASH_BITS) |
| 30 | |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 31 | struct kmmio_fault_page { |
| 32 | struct list_head list; |
| 33 | struct kmmio_fault_page *release_next; |
| 34 | unsigned long page; /* location of the fault page */ |
| 35 | |
| 36 | /* |
| 37 | * Number of times this page has been registered as a part |
| 38 | * of a probe. If zero, page is disarmed and this may be freed. |
| 39 | * Used only by writers (RCU). |
| 40 | */ |
| 41 | int count; |
| 42 | }; |
| 43 | |
| 44 | struct kmmio_delayed_release { |
| 45 | struct rcu_head rcu; |
| 46 | struct kmmio_fault_page *release_list; |
| 47 | }; |
| 48 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 49 | struct kmmio_context { |
| 50 | struct kmmio_fault_page *fpage; |
| 51 | struct kmmio_probe *probe; |
| 52 | unsigned long saved_flags; |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 53 | unsigned long addr; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 54 | int active; |
| 55 | }; |
| 56 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 57 | static DEFINE_SPINLOCK(kmmio_lock); |
| 58 | |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 59 | /* Protected by kmmio_lock */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 60 | unsigned int kmmio_count; |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 61 | |
| 62 | /* Read-protected by RCU, write-protected by kmmio_lock. */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 63 | static struct list_head kmmio_page_table[KMMIO_PAGE_TABLE_SIZE]; |
| 64 | static LIST_HEAD(kmmio_probes); |
| 65 | |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 66 | static struct list_head *kmmio_page_list(unsigned long page) |
| 67 | { |
| 68 | return &kmmio_page_table[hash_long(page, KMMIO_PAGE_HASH_BITS)]; |
| 69 | } |
| 70 | |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 71 | /* Accessed per-cpu */ |
| 72 | static DEFINE_PER_CPU(struct kmmio_context, kmmio_ctx); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 73 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 74 | /* |
| 75 | * this is basically a dynamic stabbing problem: |
| 76 | * Could use the existing prio tree code or |
| 77 | * Possible better implementations: |
| 78 | * The Interval Skip List: A Data Structure for Finding All Intervals That |
| 79 | * Overlap a Point (might be simple) |
| 80 | * Space Efficient Dynamic Stabbing with Fast Queries - Mikkel Thorup |
| 81 | */ |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 82 | /* Get the kmmio at this addr (if any). You must be holding RCU read lock. */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 83 | static struct kmmio_probe *get_kmmio_probe(unsigned long addr) |
| 84 | { |
| 85 | struct kmmio_probe *p; |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 86 | list_for_each_entry_rcu(p, &kmmio_probes, list) { |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 87 | if (addr >= p->addr && addr <= (p->addr + p->len)) |
| 88 | return p; |
| 89 | } |
| 90 | return NULL; |
| 91 | } |
| 92 | |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 93 | /* You must be holding RCU read lock. */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 94 | static struct kmmio_fault_page *get_kmmio_fault_page(unsigned long page) |
| 95 | { |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 96 | struct list_head *head; |
| 97 | struct kmmio_fault_page *p; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 98 | |
| 99 | page &= PAGE_MASK; |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 100 | head = kmmio_page_list(page); |
| 101 | list_for_each_entry_rcu(p, head, list) { |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 102 | if (p->page == page) |
| 103 | return p; |
| 104 | } |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 105 | return NULL; |
| 106 | } |
| 107 | |
Stuart Bennett | e9d54ca | 2009-01-30 17:38:59 +0000 | [diff] [blame^] | 108 | static int set_page_present(unsigned long addr, bool present, |
Pekka Paalanen | 790e2a2 | 2008-05-12 21:21:14 +0200 | [diff] [blame] | 109 | unsigned int *pglevel) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 110 | { |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 111 | pteval_t pteval; |
| 112 | pmdval_t pmdval; |
Pekka Paalanen | 790e2a2 | 2008-05-12 21:21:14 +0200 | [diff] [blame] | 113 | unsigned int level; |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 114 | pmd_t *pmd; |
| 115 | pte_t *pte = lookup_address(addr, &level); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 116 | |
Pekka Paalanen | 75bb883 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 117 | if (!pte) { |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 118 | pr_err("kmmio: no pte for page 0x%08lx\n", addr); |
Stuart Bennett | e9d54ca | 2009-01-30 17:38:59 +0000 | [diff] [blame^] | 119 | return -1; |
Pekka Paalanen | 75bb883 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 122 | if (pglevel) |
| 123 | *pglevel = level; |
| 124 | |
| 125 | switch (level) { |
| 126 | case PG_LEVEL_2M: |
| 127 | pmd = (pmd_t *)pte; |
| 128 | pmdval = pmd_val(*pmd) & ~_PAGE_PRESENT; |
| 129 | if (present) |
| 130 | pmdval |= _PAGE_PRESENT; |
| 131 | set_pmd(pmd, __pmd(pmdval)); |
| 132 | break; |
| 133 | |
| 134 | case PG_LEVEL_4K: |
| 135 | pteval = pte_val(*pte) & ~_PAGE_PRESENT; |
| 136 | if (present) |
| 137 | pteval |= _PAGE_PRESENT; |
| 138 | set_pte_atomic(pte, __pte(pteval)); |
| 139 | break; |
| 140 | |
| 141 | default: |
| 142 | pr_err("kmmio: unexpected page level 0x%x.\n", level); |
Stuart Bennett | e9d54ca | 2009-01-30 17:38:59 +0000 | [diff] [blame^] | 143 | return -1; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 146 | __flush_tlb_one(addr); |
Stuart Bennett | e9d54ca | 2009-01-30 17:38:59 +0000 | [diff] [blame^] | 147 | |
| 148 | return 0; |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 149 | } |
Pekka Paalanen | 75bb883 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 150 | |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 151 | /** Mark the given page as not present. Access to it will trigger a fault. */ |
Stuart Bennett | e9d54ca | 2009-01-30 17:38:59 +0000 | [diff] [blame^] | 152 | static int arm_kmmio_fault_page(unsigned long page, unsigned int *pglevel) |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 153 | { |
Stuart Bennett | e9d54ca | 2009-01-30 17:38:59 +0000 | [diff] [blame^] | 154 | int ret = set_page_present(page & PAGE_MASK, false, pglevel); |
| 155 | WARN_ONCE(ret < 0, KERN_ERR "kmmio arming 0x%08lx failed.\n", page); |
| 156 | return ret; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 159 | /** Mark the given page as present. */ |
Pekka Paalanen | 790e2a2 | 2008-05-12 21:21:14 +0200 | [diff] [blame] | 160 | static void disarm_kmmio_fault_page(unsigned long page, unsigned int *pglevel) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 161 | { |
Stuart Bennett | e9d54ca | 2009-01-30 17:38:59 +0000 | [diff] [blame^] | 162 | int ret = set_page_present(page & PAGE_MASK, true, pglevel); |
| 163 | WARN_ONCE(ret < 0, KERN_ERR "kmmio disarming 0x%08lx failed.\n", page); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 167 | * This is being called from do_page_fault(). |
| 168 | * |
| 169 | * We may be in an interrupt or a critical section. Also prefecthing may |
| 170 | * trigger a page fault. We may be in the middle of process switch. |
| 171 | * We cannot take any locks, because we could be executing especially |
| 172 | * within a kmmio critical section. |
| 173 | * |
| 174 | * Local interrupts are disabled, so preemption cannot happen. |
| 175 | * Do not enable interrupts, do not sleep, and watch out for other CPUs. |
| 176 | */ |
| 177 | /* |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 178 | * Interrupts are disabled on entry as trap3 is an interrupt gate |
| 179 | * and they remain disabled thorough out this function. |
| 180 | */ |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 181 | int kmmio_handler(struct pt_regs *regs, unsigned long addr) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 182 | { |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 183 | struct kmmio_context *ctx; |
| 184 | struct kmmio_fault_page *faultpage; |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 185 | int ret = 0; /* default to fault not handled */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * Preemption is now disabled to prevent process switch during |
| 189 | * single stepping. We can only handle one active kmmio trace |
| 190 | * per cpu, so ensure that we finish it before something else |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 191 | * gets to run. We also hold the RCU read lock over single |
| 192 | * stepping to avoid looking up the probe and kmmio_fault_page |
| 193 | * again. |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 194 | */ |
| 195 | preempt_disable(); |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 196 | rcu_read_lock(); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 197 | |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 198 | faultpage = get_kmmio_fault_page(addr); |
| 199 | if (!faultpage) { |
| 200 | /* |
| 201 | * Either this page fault is not caused by kmmio, or |
| 202 | * another CPU just pulled the kmmio probe from under |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 203 | * our feet. The latter case should not be possible. |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 204 | */ |
| 205 | goto no_kmmio; |
| 206 | } |
| 207 | |
| 208 | ctx = &get_cpu_var(kmmio_ctx); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 209 | if (ctx->active) { |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 210 | disarm_kmmio_fault_page(faultpage->page, NULL); |
| 211 | if (addr == ctx->addr) { |
| 212 | /* |
| 213 | * On SMP we sometimes get recursive probe hits on the |
| 214 | * same address. Context is already saved, fall out. |
| 215 | */ |
| 216 | pr_debug("kmmio: duplicate probe hit on CPU %d, for " |
| 217 | "address 0x%08lx.\n", |
| 218 | smp_processor_id(), addr); |
| 219 | ret = 1; |
| 220 | goto no_kmmio_ctx; |
| 221 | } |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 222 | /* |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 223 | * Prevent overwriting already in-flight context. |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 224 | * This should not happen, let's hope disarming at least |
| 225 | * prevents a panic. |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 226 | */ |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 227 | pr_emerg("kmmio: recursive probe hit on CPU %d, " |
| 228 | "for address 0x%08lx. Ignoring.\n", |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 229 | smp_processor_id(), addr); |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 230 | pr_emerg("kmmio: previous hit was at 0x%08lx.\n", |
| 231 | ctx->addr); |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 232 | goto no_kmmio_ctx; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 233 | } |
| 234 | ctx->active++; |
| 235 | |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 236 | ctx->fpage = faultpage; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 237 | ctx->probe = get_kmmio_probe(addr); |
Ingo Molnar | 4902316 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 238 | ctx->saved_flags = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF)); |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 239 | ctx->addr = addr; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 240 | |
| 241 | if (ctx->probe && ctx->probe->pre_handler) |
| 242 | ctx->probe->pre_handler(ctx->probe, regs, addr); |
| 243 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 244 | /* |
| 245 | * Enable single-stepping and disable interrupts for the faulting |
| 246 | * context. Local interrupts must not get enabled during stepping. |
| 247 | */ |
Ingo Molnar | 4902316 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 248 | regs->flags |= X86_EFLAGS_TF; |
| 249 | regs->flags &= ~X86_EFLAGS_IF; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 250 | |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 251 | /* Now we set present bit in PTE and single step. */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 252 | disarm_kmmio_fault_page(ctx->fpage->page, NULL); |
| 253 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 254 | /* |
| 255 | * If another cpu accesses the same page while we are stepping, |
| 256 | * the access will not be caught. It will simply succeed and the |
| 257 | * only downside is we lose the event. If this becomes a problem, |
| 258 | * the user should drop to single cpu before tracing. |
| 259 | */ |
| 260 | |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 261 | put_cpu_var(kmmio_ctx); |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 262 | return 1; /* fault handled */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 263 | |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 264 | no_kmmio_ctx: |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 265 | put_cpu_var(kmmio_ctx); |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 266 | no_kmmio: |
| 267 | rcu_read_unlock(); |
| 268 | preempt_enable_no_resched(); |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 269 | return ret; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Interrupts are disabled on entry as trap1 is an interrupt gate |
| 274 | * and they remain disabled thorough out this function. |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 275 | * This must always get called as the pair to kmmio_handler(). |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 276 | */ |
| 277 | static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs) |
| 278 | { |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 279 | int ret = 0; |
| 280 | struct kmmio_context *ctx = &get_cpu_var(kmmio_ctx); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 281 | |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 282 | if (!ctx->active) { |
| 283 | pr_debug("kmmio: spurious debug trap on CPU %d.\n", |
| 284 | smp_processor_id()); |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 285 | goto out; |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 286 | } |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 287 | |
| 288 | if (ctx->probe && ctx->probe->post_handler) |
| 289 | ctx->probe->post_handler(ctx->probe, condition, regs); |
| 290 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 291 | arm_kmmio_fault_page(ctx->fpage->page, NULL); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 292 | |
Ingo Molnar | 4902316 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 293 | regs->flags &= ~X86_EFLAGS_TF; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 294 | regs->flags |= ctx->saved_flags; |
| 295 | |
| 296 | /* These were acquired in kmmio_handler(). */ |
| 297 | ctx->active--; |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 298 | BUG_ON(ctx->active); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 299 | rcu_read_unlock(); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 300 | preempt_enable_no_resched(); |
| 301 | |
| 302 | /* |
| 303 | * if somebody else is singlestepping across a probe point, flags |
| 304 | * will have TF set, in which case, continue the remaining processing |
| 305 | * of do_debug, as if this is not a probe hit. |
| 306 | */ |
Ingo Molnar | 4902316 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 307 | if (!(regs->flags & X86_EFLAGS_TF)) |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 308 | ret = 1; |
Pekka Paalanen | f513638 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 309 | out: |
| 310 | put_cpu_var(kmmio_ctx); |
| 311 | return ret; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 314 | /* You must be holding kmmio_lock. */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 315 | static int add_kmmio_fault_page(unsigned long page) |
| 316 | { |
| 317 | struct kmmio_fault_page *f; |
| 318 | |
| 319 | page &= PAGE_MASK; |
| 320 | f = get_kmmio_fault_page(page); |
| 321 | if (f) { |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 322 | if (!f->count) |
| 323 | arm_kmmio_fault_page(f->page, NULL); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 324 | f->count++; |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | f = kmalloc(sizeof(*f), GFP_ATOMIC); |
| 329 | if (!f) |
| 330 | return -1; |
| 331 | |
| 332 | f->count = 1; |
| 333 | f->page = page; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 334 | |
Stuart Bennett | e9d54ca | 2009-01-30 17:38:59 +0000 | [diff] [blame^] | 335 | if (arm_kmmio_fault_page(f->page, NULL)) { |
| 336 | kfree(f); |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | list_add_rcu(&f->list, kmmio_page_list(f->page)); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 345 | /* You must be holding kmmio_lock. */ |
| 346 | static void release_kmmio_fault_page(unsigned long page, |
| 347 | struct kmmio_fault_page **release_list) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 348 | { |
| 349 | struct kmmio_fault_page *f; |
| 350 | |
| 351 | page &= PAGE_MASK; |
| 352 | f = get_kmmio_fault_page(page); |
| 353 | if (!f) |
| 354 | return; |
| 355 | |
| 356 | f->count--; |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 357 | BUG_ON(f->count < 0); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 358 | if (!f->count) { |
| 359 | disarm_kmmio_fault_page(f->page, NULL); |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 360 | f->release_next = *release_list; |
| 361 | *release_list = f; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Pekka Paalanen | 87e547f | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 365 | /* |
| 366 | * With page-unaligned ioremaps, one or two armed pages may contain |
| 367 | * addresses from outside the intended mapping. Events for these addresses |
| 368 | * are currently silently dropped. The events may result only from programming |
| 369 | * mistakes by accessing addresses before the beginning or past the end of a |
| 370 | * mapping. |
| 371 | */ |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 372 | int register_kmmio_probe(struct kmmio_probe *p) |
| 373 | { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 374 | unsigned long flags; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 375 | int ret = 0; |
| 376 | unsigned long size = 0; |
Pekka Paalanen | 87e547f | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 377 | const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 378 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 379 | spin_lock_irqsave(&kmmio_lock, flags); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 380 | if (get_kmmio_probe(p->addr)) { |
| 381 | ret = -EEXIST; |
| 382 | goto out; |
| 383 | } |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 384 | kmmio_count++; |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 385 | list_add_rcu(&p->list, &kmmio_probes); |
Pekka Paalanen | 87e547f | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 386 | while (size < size_lim) { |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 387 | if (add_kmmio_fault_page(p->addr + size)) |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 388 | pr_err("kmmio: Unable to set page fault.\n"); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 389 | size += PAGE_SIZE; |
| 390 | } |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 391 | out: |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 392 | spin_unlock_irqrestore(&kmmio_lock, flags); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 393 | /* |
| 394 | * XXX: What should I do here? |
| 395 | * Here was a call to global_flush_tlb(), but it does not exist |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 396 | * anymore. It seems it's not needed after all. |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 397 | */ |
| 398 | return ret; |
| 399 | } |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 400 | EXPORT_SYMBOL(register_kmmio_probe); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 401 | |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 402 | static void rcu_free_kmmio_fault_pages(struct rcu_head *head) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 403 | { |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 404 | struct kmmio_delayed_release *dr = container_of( |
| 405 | head, |
| 406 | struct kmmio_delayed_release, |
| 407 | rcu); |
| 408 | struct kmmio_fault_page *p = dr->release_list; |
| 409 | while (p) { |
| 410 | struct kmmio_fault_page *next = p->release_next; |
| 411 | BUG_ON(p->count); |
| 412 | kfree(p); |
| 413 | p = next; |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 414 | } |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 415 | kfree(dr); |
| 416 | } |
| 417 | |
| 418 | static void remove_kmmio_fault_pages(struct rcu_head *head) |
| 419 | { |
| 420 | struct kmmio_delayed_release *dr = container_of( |
| 421 | head, |
| 422 | struct kmmio_delayed_release, |
| 423 | rcu); |
| 424 | struct kmmio_fault_page *p = dr->release_list; |
| 425 | struct kmmio_fault_page **prevp = &dr->release_list; |
| 426 | unsigned long flags; |
| 427 | spin_lock_irqsave(&kmmio_lock, flags); |
| 428 | while (p) { |
| 429 | if (!p->count) |
| 430 | list_del_rcu(&p->list); |
| 431 | else |
| 432 | *prevp = p->release_next; |
| 433 | prevp = &p->release_next; |
| 434 | p = p->release_next; |
| 435 | } |
| 436 | spin_unlock_irqrestore(&kmmio_lock, flags); |
| 437 | /* This is the real RCU destroy call. */ |
| 438 | call_rcu(&dr->rcu, rcu_free_kmmio_fault_pages); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /* |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 442 | * Remove a kmmio probe. You have to synchronize_rcu() before you can be |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 443 | * sure that the callbacks will not be called anymore. Only after that |
| 444 | * you may actually release your struct kmmio_probe. |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 445 | * |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 446 | * Unregistering a kmmio fault page has three steps: |
| 447 | * 1. release_kmmio_fault_page() |
| 448 | * Disarm the page, wait a grace period to let all faults finish. |
| 449 | * 2. remove_kmmio_fault_pages() |
| 450 | * Remove the pages from kmmio_page_table. |
| 451 | * 3. rcu_free_kmmio_fault_pages() |
| 452 | * Actally free the kmmio_fault_page structs as with RCU. |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 453 | */ |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 454 | void unregister_kmmio_probe(struct kmmio_probe *p) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 455 | { |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 456 | unsigned long flags; |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 457 | unsigned long size = 0; |
Pekka Paalanen | 87e547f | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 458 | const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK); |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 459 | struct kmmio_fault_page *release_list = NULL; |
| 460 | struct kmmio_delayed_release *drelease; |
| 461 | |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 462 | spin_lock_irqsave(&kmmio_lock, flags); |
Pekka Paalanen | 87e547f | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 463 | while (size < size_lim) { |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 464 | release_kmmio_fault_page(p->addr + size, &release_list); |
| 465 | size += PAGE_SIZE; |
| 466 | } |
| 467 | list_del_rcu(&p->list); |
| 468 | kmmio_count--; |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 469 | spin_unlock_irqrestore(&kmmio_lock, flags); |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 470 | |
| 471 | drelease = kmalloc(sizeof(*drelease), GFP_ATOMIC); |
| 472 | if (!drelease) { |
| 473 | pr_crit("kmmio: leaking kmmio_fault_page objects.\n"); |
| 474 | return; |
| 475 | } |
| 476 | drelease->release_list = release_list; |
| 477 | |
| 478 | /* |
| 479 | * This is not really RCU here. We have just disarmed a set of |
| 480 | * pages so that they cannot trigger page faults anymore. However, |
| 481 | * we cannot remove the pages from kmmio_page_table, |
| 482 | * because a probe hit might be in flight on another CPU. The |
| 483 | * pages are collected into a list, and they will be removed from |
| 484 | * kmmio_page_table when it is certain that no probe hit related to |
| 485 | * these pages can be in flight. RCU grace period sounds like a |
| 486 | * good choice. |
| 487 | * |
| 488 | * If we removed the pages too early, kmmio page fault handler might |
| 489 | * not find the respective kmmio_fault_page and determine it's not |
| 490 | * a kmmio fault, when it actually is. This would lead to madness. |
| 491 | */ |
| 492 | call_rcu(&drelease->rcu, remove_kmmio_fault_pages); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 493 | } |
Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 494 | EXPORT_SYMBOL(unregister_kmmio_probe); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 495 | |
| 496 | static int kmmio_die_notifier(struct notifier_block *nb, unsigned long val, |
| 497 | void *args) |
| 498 | { |
| 499 | struct die_args *arg = args; |
| 500 | |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 501 | if (val == DIE_DEBUG && (arg->err & DR_STEP)) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 502 | if (post_kmmio_handler(arg->err, arg->regs) == 1) |
| 503 | return NOTIFY_STOP; |
| 504 | |
| 505 | return NOTIFY_DONE; |
| 506 | } |
Pekka Paalanen | 1382953 | 2008-05-12 21:20:58 +0200 | [diff] [blame] | 507 | |
| 508 | static struct notifier_block nb_die = { |
| 509 | .notifier_call = kmmio_die_notifier |
| 510 | }; |
| 511 | |
| 512 | static int __init init_kmmio(void) |
| 513 | { |
| 514 | int i; |
| 515 | for (i = 0; i < KMMIO_PAGE_TABLE_SIZE; i++) |
| 516 | INIT_LIST_HEAD(&kmmio_page_table[i]); |
| 517 | return register_die_notifier(&nb_die); |
| 518 | } |
| 519 | fs_initcall(init_kmmio); /* should be before device_initcall() */ |