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