]> err.no Git - linux-2.6/blob - arch/x86/kernel/mmiotrace/mmio-mod.c
x86 mmiotrace: fix relay-buffer-full flag for SMP
[linux-2.6] / arch / x86 / kernel / mmiotrace / mmio-mod.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  * Copyright (C) IBM Corporation, 2005
17  *               Jeff Muizelaar, 2006, 2007
18  *               Pekka Paalanen, 2008 <pq@iki.fi>
19  *
20  * Derived from the read-mod example from relay-examples by Tom Zanussi.
21  */
22 #include <linux/module.h>
23 #include <linux/relay.h>
24 #include <linux/debugfs.h>
25 #include <linux/proc_fs.h>
26 #include <asm/io.h>
27 #include <linux/version.h>
28 #include <linux/kallsyms.h>
29 #include <asm/pgtable.h>
30 #include <linux/mmiotrace.h>
31 #include <asm/e820.h> /* for ISA_START_ADDRESS */
32 #include <asm/atomic.h>
33
34 #include "kmmio.h"
35 #include "pf_in.h"
36
37 /* This app's relay channel files will appear in /debug/mmio-trace */
38 #define APP_DIR         "mmio-trace"
39 /* the marker injection file in /proc */
40 #define MARKER_FILE     "mmio-marker"
41
42 #define MODULE_NAME     "mmiotrace"
43
44 struct trap_reason {
45         unsigned long addr;
46         unsigned long ip;
47         enum reason_type type;
48         int active_traces;
49 };
50
51 /* Accessed per-cpu. */
52 static struct trap_reason pf_reason[NR_CPUS];
53 static struct mm_io_header_rw cpu_trace[NR_CPUS];
54
55 /* Access to this is not per-cpu. */
56 static atomic_t dropped[NR_CPUS];
57
58 static struct file_operations mmio_fops = {
59         .owner = THIS_MODULE,
60 };
61
62 static const size_t subbuf_size = 256*1024;
63 static struct rchan *chan;
64 static struct dentry *dir;
65 static struct proc_dir_entry *proc_marker_file;
66
67 /* module parameters */
68 static unsigned int      n_subbufs = 32*4;
69 static unsigned long filter_offset;
70 static int                 nommiotrace;
71 static int               ISA_trace;
72 static int                trace_pc;
73
74 module_param(n_subbufs, uint, 0);
75 module_param(filter_offset, ulong, 0);
76 module_param(nommiotrace, bool, 0);
77 module_param(ISA_trace, bool, 0);
78 module_param(trace_pc, bool, 0);
79
80 MODULE_PARM_DESC(n_subbufs, "Number of 256kB buffers, default 128.");
81 MODULE_PARM_DESC(filter_offset, "Start address of traced mappings.");
82 MODULE_PARM_DESC(nommiotrace, "Disable actual MMIO tracing.");
83 MODULE_PARM_DESC(ISA_trace, "Do not exclude the low ISA range.");
84 MODULE_PARM_DESC(trace_pc, "Record address of faulting instructions.");
85
86 static void record_timestamp(struct mm_io_header *header)
87 {
88         struct timespec now;
89
90         getnstimeofday(&now);
91         header->sec = now.tv_sec;
92         header->nsec = now.tv_nsec;
93 }
94
95 /*
96  * Write callback for the /proc entry:
97  * Read a marker and write it to the mmio trace log
98  */
99 static int write_marker(struct file *file, const char __user *buffer,
100                                         unsigned long count, void *data)
101 {
102         char *event = NULL;
103         struct mm_io_header *headp;
104         int len = (count > 65535) ? 65535 : count;
105
106         event = kzalloc(sizeof(*headp) + len, GFP_KERNEL);
107         if (!event)
108                 return -ENOMEM;
109
110         headp = (struct mm_io_header *)event;
111         headp->type = MMIO_MAGIC | (MMIO_MARKER << MMIO_OPCODE_SHIFT);
112         headp->data_len = len;
113         record_timestamp(headp);
114
115         if (copy_from_user(event + sizeof(*headp), buffer, len)) {
116                 kfree(event);
117                 return -EFAULT;
118         }
119
120         relay_write(chan, event, sizeof(*headp) + len);
121         kfree(event);
122         return len;
123 }
124
125 static void print_pte(unsigned long address)
126 {
127         int level;
128         pte_t *pte = lookup_address(address, &level);
129
130         if (!pte) {
131                 printk(KERN_ERR "Error in %s: no pte for page 0x%08lx\n",
132                                                 __FUNCTION__, address);
133                 return;
134         }
135
136         if (level == PG_LEVEL_2M) {
137                 printk(KERN_EMERG MODULE_NAME ": 4MB pages are not "
138                                                 "currently supported: %lx\n",
139                                                 address);
140                 BUG();
141         }
142         printk(KERN_DEBUG MODULE_NAME ": pte for 0x%lx: 0x%lx 0x%lx\n",
143                                         address, pte_val(*pte),
144                                         pte_val(*pte) & _PAGE_PRESENT);
145 }
146
147 /*
148  * For some reason the pre/post pairs have been called in an
149  * unmatched order. Report and die.
150  */
151 static void die_kmmio_nesting_error(struct pt_regs *regs, unsigned long addr)
152 {
153         const unsigned long cpu = smp_processor_id();
154         printk(KERN_EMERG MODULE_NAME ": unexpected fault for address: %lx, "
155                                         "last fault for address: %lx\n",
156                                         addr, pf_reason[cpu].addr);
157         print_pte(addr);
158 #ifdef __i386__
159         print_symbol(KERN_EMERG "faulting EIP is at %s\n", regs->ip);
160         print_symbol(KERN_EMERG "last faulting EIP was at %s\n",
161                                                         pf_reason[cpu].ip);
162         printk(KERN_EMERG
163                         "eax: %08lx   ebx: %08lx   ecx: %08lx   edx: %08lx\n",
164                         regs->ax, regs->bx, regs->cx, regs->dx);
165         printk(KERN_EMERG
166                         "esi: %08lx   edi: %08lx   ebp: %08lx   esp: %08lx\n",
167                         regs->si, regs->di, regs->bp, regs->sp);
168 #else
169         print_symbol(KERN_EMERG "faulting RIP is at %s\n", regs->ip);
170         print_symbol(KERN_EMERG "last faulting RIP was at %s\n",
171                                                         pf_reason[cpu].ip);
172         printk(KERN_EMERG "rax: %016lx   rcx: %016lx   rdx: %016lx\n",
173                                         regs->ax, regs->cx, regs->dx);
174         printk(KERN_EMERG "rsi: %016lx   rdi: %016lx   "
175                                 "rbp: %016lx   rsp: %016lx\n",
176                                 regs->si, regs->di, regs->bp, regs->sp);
177 #endif
178         BUG();
179 }
180
181 static void pre(struct kmmio_probe *p, struct pt_regs *regs,
182                                                 unsigned long addr)
183 {
184         const unsigned long cpu = smp_processor_id();
185         const unsigned long instptr = instruction_pointer(regs);
186         const enum reason_type type = get_ins_type(instptr);
187
188         /* it doesn't make sense to have more than one active trace per cpu */
189         if (pf_reason[cpu].active_traces)
190                 die_kmmio_nesting_error(regs, addr);
191         else
192                 pf_reason[cpu].active_traces++;
193
194         pf_reason[cpu].type = type;
195         pf_reason[cpu].addr = addr;
196         pf_reason[cpu].ip = instptr;
197
198         cpu_trace[cpu].header.type = MMIO_MAGIC;
199         cpu_trace[cpu].header.pid = 0;
200         cpu_trace[cpu].header.data_len = sizeof(struct mm_io_rw);
201         cpu_trace[cpu].rw.address = addr;
202
203         /*
204          * Only record the program counter when requested.
205          * It may taint clean-room reverse engineering.
206          */
207         if (trace_pc)
208                 cpu_trace[cpu].rw.pc = instptr;
209         else
210                 cpu_trace[cpu].rw.pc = 0;
211
212         record_timestamp(&cpu_trace[cpu].header);
213
214         switch (type) {
215         case REG_READ:
216                 cpu_trace[cpu].header.type |=
217                         (MMIO_READ << MMIO_OPCODE_SHIFT) |
218                         (get_ins_mem_width(instptr) << MMIO_WIDTH_SHIFT);
219                 break;
220         case REG_WRITE:
221                 cpu_trace[cpu].header.type |=
222                         (MMIO_WRITE << MMIO_OPCODE_SHIFT) |
223                         (get_ins_mem_width(instptr) << MMIO_WIDTH_SHIFT);
224                 cpu_trace[cpu].rw.value = get_ins_reg_val(instptr, regs);
225                 break;
226         case IMM_WRITE:
227                 cpu_trace[cpu].header.type |=
228                         (MMIO_WRITE << MMIO_OPCODE_SHIFT) |
229                         (get_ins_mem_width(instptr) << MMIO_WIDTH_SHIFT);
230                 cpu_trace[cpu].rw.value = get_ins_imm_val(instptr);
231                 break;
232         default:
233                 {
234                         unsigned char *ip = (unsigned char *)instptr;
235                         cpu_trace[cpu].header.type |=
236                                         (MMIO_UNKNOWN_OP << MMIO_OPCODE_SHIFT);
237                         cpu_trace[cpu].rw.value = (*ip) << 16 |
238                                                         *(ip + 1) << 8 |
239                                                         *(ip + 2);
240                 }
241         }
242 }
243
244 static void post(struct kmmio_probe *p, unsigned long condition,
245                                                         struct pt_regs *regs)
246 {
247         const unsigned long cpu = smp_processor_id();
248
249         /* this should always return the active_trace count to 0 */
250         pf_reason[cpu].active_traces--;
251         if (pf_reason[cpu].active_traces) {
252                 printk(KERN_EMERG MODULE_NAME ": unexpected post handler");
253                 BUG();
254         }
255
256         switch (pf_reason[cpu].type) {
257         case REG_READ:
258                 cpu_trace[cpu].rw.value = get_ins_reg_val(pf_reason[cpu].ip,
259                                                                         regs);
260                 break;
261         default:
262                 break;
263         }
264         relay_write(chan, &cpu_trace[cpu], sizeof(struct mm_io_header_rw));
265 }
266
267 /*
268  * subbuf_start() relay callback.
269  *
270  * Defined so that we know when events are dropped due to the buffer-full
271  * condition.
272  */
273 static int subbuf_start_handler(struct rchan_buf *buf, void *subbuf,
274                                         void *prev_subbuf, size_t prev_padding)
275 {
276         unsigned int cpu = buf->cpu;
277         atomic_t *drop = &dropped[cpu];
278         int count;
279         if (relay_buf_full(buf)) {
280                 if (atomic_inc_return(drop) == 1) {
281                         printk(KERN_ERR MODULE_NAME ": cpu %d buffer full!\n",
282                                                                         cpu);
283                 }
284                 return 0;
285         } else if ((count = atomic_read(drop))) {
286                 printk(KERN_ERR MODULE_NAME
287                                         ": cpu %d buffer no longer full, "
288                                         "missed %d events.\n",
289                                         cpu, count);
290                 atomic_sub(count, drop);
291         }
292
293         return 1;
294 }
295
296 /* file_create() callback.  Creates relay file in debugfs. */
297 static struct dentry *create_buf_file_handler(const char *filename,
298                                                 struct dentry *parent,
299                                                 int mode,
300                                                 struct rchan_buf *buf,
301                                                 int *is_global)
302 {
303         struct dentry *buf_file;
304
305         mmio_fops.read = relay_file_operations.read;
306         mmio_fops.open = relay_file_operations.open;
307         mmio_fops.poll = relay_file_operations.poll;
308         mmio_fops.mmap = relay_file_operations.mmap;
309         mmio_fops.release = relay_file_operations.release;
310         mmio_fops.splice_read = relay_file_operations.splice_read;
311
312         buf_file = debugfs_create_file(filename, mode, parent, buf,
313                                                                 &mmio_fops);
314
315         return buf_file;
316 }
317
318 /* file_remove() default callback.  Removes relay file in debugfs. */
319 static int remove_buf_file_handler(struct dentry *dentry)
320 {
321         debugfs_remove(dentry);
322         return 0;
323 }
324
325 static struct rchan_callbacks relay_callbacks = {
326         .subbuf_start = subbuf_start_handler,
327         .create_buf_file = create_buf_file_handler,
328         .remove_buf_file = remove_buf_file_handler,
329 };
330
331 /*
332  * create_channel - creates channel /debug/APP_DIR/cpuXXX
333  * Returns channel on success, NULL otherwise
334  */
335 static struct rchan *create_channel(unsigned size, unsigned n)
336 {
337         return relay_open("cpu", dir, size, n, &relay_callbacks, NULL);
338 }
339
340 /* destroy_channel - destroys channel /debug/APP_DIR/cpuXXX */
341 static void destroy_channel(void)
342 {
343         if (chan) {
344                 relay_close(chan);
345                 chan = NULL;
346         }
347 }
348
349 struct remap_trace {
350         struct list_head list;
351         struct kmmio_probe probe;
352 };
353 static LIST_HEAD(trace_list);
354 static DEFINE_SPINLOCK(trace_list_lock);
355
356 static void do_ioremap_trace_core(unsigned long offset, unsigned long size,
357                                                         void __iomem *addr)
358 {
359         struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL);
360         struct mm_io_header_map event = {
361                 .header = {
362                         .type = MMIO_MAGIC |
363                                         (MMIO_PROBE << MMIO_OPCODE_SHIFT),
364                         .sec = 0,
365                         .nsec = 0,
366                         .pid = 0,
367                         .data_len = sizeof(struct mm_io_map)
368                 },
369                 .map = {
370                         .phys = offset,
371                         .addr = (unsigned long)addr,
372                         .len  = size,
373                         .pc   = 0
374                 }
375         };
376         record_timestamp(&event.header);
377
378         *trace = (struct remap_trace) {
379                 .probe = {
380                         .addr = (unsigned long)addr,
381                         .len = size,
382                         .pre_handler = pre,
383                         .post_handler = post,
384                 }
385         };
386
387         relay_write(chan, &event, sizeof(event));
388         spin_lock(&trace_list_lock);
389         list_add_tail(&trace->list, &trace_list);
390         spin_unlock(&trace_list_lock);
391         if (!nommiotrace)
392                 register_kmmio_probe(&trace->probe);
393 }
394
395 static void ioremap_trace_core(unsigned long offset, unsigned long size,
396                                                         void __iomem *addr)
397 {
398         if ((filter_offset) && (offset != filter_offset))
399                 return;
400
401         /* Don't trace the low PCI/ISA area, it's always mapped.. */
402         if (!ISA_trace && (offset < ISA_END_ADDRESS) &&
403                                         (offset + size > ISA_START_ADDRESS)) {
404                 printk(KERN_NOTICE MODULE_NAME ": Ignoring map of low "
405                                                 "PCI/ISA area (0x%lx-0x%lx)\n",
406                                                 offset, offset + size);
407                 return;
408         }
409         do_ioremap_trace_core(offset, size, addr);
410 }
411
412 void __iomem *ioremap_cache_trace(unsigned long offset, unsigned long size)
413 {
414         void __iomem *p = ioremap_cache(offset, size);
415         printk(KERN_DEBUG MODULE_NAME ": ioremap_cache(0x%lx, 0x%lx) = %p\n",
416                                                         offset, size, p);
417         ioremap_trace_core(offset, size, p);
418         return p;
419 }
420 EXPORT_SYMBOL(ioremap_cache_trace);
421
422 void __iomem *ioremap_nocache_trace(unsigned long offset, unsigned long size)
423 {
424         void __iomem *p = ioremap_nocache(offset, size);
425         printk(KERN_DEBUG MODULE_NAME ": ioremap_nocache(0x%lx, 0x%lx) = %p\n",
426                                                         offset, size, p);
427         ioremap_trace_core(offset, size, p);
428         return p;
429 }
430 EXPORT_SYMBOL(ioremap_nocache_trace);
431
432 void iounmap_trace(volatile void __iomem *addr)
433 {
434         struct mm_io_header_map event = {
435                 .header = {
436                         .type = MMIO_MAGIC |
437                                 (MMIO_UNPROBE << MMIO_OPCODE_SHIFT),
438                         .sec = 0,
439                         .nsec = 0,
440                         .pid = 0,
441                         .data_len = sizeof(struct mm_io_map)
442                 },
443                 .map = {
444                         .phys = 0,
445                         .addr = (unsigned long)addr,
446                         .len  = 0,
447                         .pc   = 0
448                 }
449         };
450         struct remap_trace *trace;
451         struct remap_trace *tmp;
452         printk(KERN_DEBUG MODULE_NAME ": Unmapping %p.\n", addr);
453         record_timestamp(&event.header);
454
455         spin_lock(&trace_list_lock);
456         list_for_each_entry_safe(trace, tmp, &trace_list, list) {
457                 if ((unsigned long)addr == trace->probe.addr) {
458                         if (!nommiotrace)
459                                 unregister_kmmio_probe(&trace->probe);
460                         list_del(&trace->list);
461                         kfree(trace);
462                         break;
463                 }
464         }
465         spin_unlock(&trace_list_lock);
466         relay_write(chan, &event, sizeof(event));
467         iounmap(addr);
468 }
469 EXPORT_SYMBOL(iounmap_trace);
470
471 static void clear_trace_list(void)
472 {
473         struct remap_trace *trace;
474         struct remap_trace *tmp;
475
476         spin_lock(&trace_list_lock);
477         list_for_each_entry_safe(trace, tmp, &trace_list, list) {
478                 printk(KERN_WARNING MODULE_NAME ": purging non-iounmapped "
479                                         "trace @0x%08lx, size 0x%lx.\n",
480                                         trace->probe.addr, trace->probe.len);
481                 if (!nommiotrace)
482                         unregister_kmmio_probe(&trace->probe);
483                 list_del(&trace->list);
484                 kfree(trace);
485                 break;
486         }
487         spin_unlock(&trace_list_lock);
488 }
489
490 static int __init init(void)
491 {
492         if (n_subbufs < 2)
493                 return -EINVAL;
494
495         dir = debugfs_create_dir(APP_DIR, NULL);
496         if (!dir) {
497                 printk(KERN_ERR MODULE_NAME
498                                 ": Couldn't create relay app directory.\n");
499                 return -ENOMEM;
500         }
501
502         chan = create_channel(subbuf_size, n_subbufs);
503         if (!chan) {
504                 debugfs_remove(dir);
505                 printk(KERN_ERR MODULE_NAME
506                                 ": relay app channel creation failed\n");
507                 return -ENOMEM;
508         }
509
510         init_kmmio();
511
512         proc_marker_file = create_proc_entry(MARKER_FILE, 0, NULL);
513         if (proc_marker_file)
514                 proc_marker_file->write_proc = write_marker;
515
516         printk(KERN_DEBUG MODULE_NAME ": loaded.\n");
517         if (nommiotrace)
518                 printk(KERN_DEBUG MODULE_NAME ": MMIO tracing disabled.\n");
519         if (ISA_trace)
520                 printk(KERN_WARNING MODULE_NAME
521                                 ": Warning! low ISA range will be traced.\n");
522         return 0;
523 }
524
525 static void __exit cleanup(void)
526 {
527         printk(KERN_DEBUG MODULE_NAME ": unload...\n");
528         clear_trace_list();
529         cleanup_kmmio();
530         remove_proc_entry(MARKER_FILE, NULL);
531         destroy_channel();
532         if (dir)
533                 debugfs_remove(dir);
534 }
535
536 module_init(init);
537 module_exit(cleanup);
538 MODULE_LICENSE("GPL");