5b99004fb4becbead27268039a3e37757759dc2c
[pandora-kernel.git] / arch / x86 / mm / kmemcheck / kmemcheck.c
1 /**
2  * kmemcheck - a heavyweight memory checker for the linux kernel
3  * Copyright (C) 2007, 2008  Vegard Nossum <vegardno@ifi.uio.no>
4  * (With a lot of help from Ingo Molnar and Pekka Enberg.)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License (version 2) as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/kallsyms.h>
14 #include <linux/kernel.h>
15 #include <linux/kmemcheck.h>
16 #include <linux/mm.h>
17 #include <linux/module.h>
18 #include <linux/page-flags.h>
19 #include <linux/percpu.h>
20 #include <linux/ptrace.h>
21 #include <linux/string.h>
22 #include <linux/types.h>
23
24 #include <asm/cacheflush.h>
25 #include <asm/kmemcheck.h>
26 #include <asm/pgtable.h>
27 #include <asm/tlbflush.h>
28
29 #include "error.h"
30 #include "opcode.h"
31 #include "pte.h"
32 #include "selftest.h"
33 #include "shadow.h"
34
35
36 #ifdef CONFIG_KMEMCHECK_DISABLED_BY_DEFAULT
37 #  define KMEMCHECK_ENABLED 0
38 #endif
39
40 #ifdef CONFIG_KMEMCHECK_ENABLED_BY_DEFAULT
41 #  define KMEMCHECK_ENABLED 1
42 #endif
43
44 #ifdef CONFIG_KMEMCHECK_ONESHOT_BY_DEFAULT
45 #  define KMEMCHECK_ENABLED 2
46 #endif
47
48 int kmemcheck_enabled = KMEMCHECK_ENABLED;
49
50 int __init kmemcheck_init(void)
51 {
52 #ifdef CONFIG_SMP
53         /*
54          * Limit SMP to use a single CPU. We rely on the fact that this code
55          * runs before SMP is set up.
56          */
57         if (setup_max_cpus > 1) {
58                 printk(KERN_INFO
59                         "kmemcheck: Limiting number of CPUs to 1.\n");
60                 setup_max_cpus = 1;
61         }
62 #endif
63
64         if (!kmemcheck_selftest()) {
65                 printk(KERN_INFO "kmemcheck: self-tests failed; disabling\n");
66                 kmemcheck_enabled = 0;
67                 return -EINVAL;
68         }
69
70         printk(KERN_INFO "kmemcheck: Initialized\n");
71         return 0;
72 }
73
74 early_initcall(kmemcheck_init);
75
76 /*
77  * We need to parse the kmemcheck= option before any memory is allocated.
78  */
79 static int __init param_kmemcheck(char *str)
80 {
81         if (!str)
82                 return -EINVAL;
83
84         sscanf(str, "%d", &kmemcheck_enabled);
85         return 0;
86 }
87
88 early_param("kmemcheck", param_kmemcheck);
89
90 int kmemcheck_show_addr(unsigned long address)
91 {
92         pte_t *pte;
93
94         pte = kmemcheck_pte_lookup(address);
95         if (!pte)
96                 return 0;
97
98         set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
99         __flush_tlb_one(address);
100         return 1;
101 }
102
103 int kmemcheck_hide_addr(unsigned long address)
104 {
105         pte_t *pte;
106
107         pte = kmemcheck_pte_lookup(address);
108         if (!pte)
109                 return 0;
110
111         set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
112         __flush_tlb_one(address);
113         return 1;
114 }
115
116 struct kmemcheck_context {
117         bool busy;
118         int balance;
119
120         /*
121          * There can be at most two memory operands to an instruction, but
122          * each address can cross a page boundary -- so we may need up to
123          * four addresses that must be hidden/revealed for each fault.
124          */
125         unsigned long addr[4];
126         unsigned long n_addrs;
127         unsigned long flags;
128
129         /* Data size of the instruction that caused a fault. */
130         unsigned int size;
131 };
132
133 static DEFINE_PER_CPU(struct kmemcheck_context, kmemcheck_context);
134
135 bool kmemcheck_active(struct pt_regs *regs)
136 {
137         struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
138
139         return data->balance > 0;
140 }
141
142 /* Save an address that needs to be shown/hidden */
143 static void kmemcheck_save_addr(unsigned long addr)
144 {
145         struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
146
147         BUG_ON(data->n_addrs >= ARRAY_SIZE(data->addr));
148         data->addr[data->n_addrs++] = addr;
149 }
150
151 static unsigned int kmemcheck_show_all(void)
152 {
153         struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
154         unsigned int i;
155         unsigned int n;
156
157         n = 0;
158         for (i = 0; i < data->n_addrs; ++i)
159                 n += kmemcheck_show_addr(data->addr[i]);
160
161         return n;
162 }
163
164 static unsigned int kmemcheck_hide_all(void)
165 {
166         struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
167         unsigned int i;
168         unsigned int n;
169
170         n = 0;
171         for (i = 0; i < data->n_addrs; ++i)
172                 n += kmemcheck_hide_addr(data->addr[i]);
173
174         return n;
175 }
176
177 /*
178  * Called from the #PF handler.
179  */
180 void kmemcheck_show(struct pt_regs *regs)
181 {
182         struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
183
184         BUG_ON(!irqs_disabled());
185
186         if (unlikely(data->balance != 0)) {
187                 kmemcheck_show_all();
188                 kmemcheck_error_save_bug(regs);
189                 data->balance = 0;
190                 return;
191         }
192
193         /*
194          * None of the addresses actually belonged to kmemcheck. Note that
195          * this is not an error.
196          */
197         if (kmemcheck_show_all() == 0)
198                 return;
199
200         ++data->balance;
201
202         /*
203          * The IF needs to be cleared as well, so that the faulting
204          * instruction can run "uninterrupted". Otherwise, we might take
205          * an interrupt and start executing that before we've had a chance
206          * to hide the page again.
207          *
208          * NOTE: In the rare case of multiple faults, we must not override
209          * the original flags:
210          */
211         if (!(regs->flags & X86_EFLAGS_TF))
212                 data->flags = regs->flags;
213
214         regs->flags |= X86_EFLAGS_TF;
215         regs->flags &= ~X86_EFLAGS_IF;
216 }
217
218 /*
219  * Called from the #DB handler.
220  */
221 void kmemcheck_hide(struct pt_regs *regs)
222 {
223         struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
224         int n;
225
226         BUG_ON(!irqs_disabled());
227
228         if (unlikely(data->balance != 1)) {
229                 kmemcheck_show_all();
230                 kmemcheck_error_save_bug(regs);
231                 data->n_addrs = 0;
232                 data->balance = 0;
233
234                 if (!(data->flags & X86_EFLAGS_TF))
235                         regs->flags &= ~X86_EFLAGS_TF;
236                 if (data->flags & X86_EFLAGS_IF)
237                         regs->flags |= X86_EFLAGS_IF;
238                 return;
239         }
240
241         if (kmemcheck_enabled)
242                 n = kmemcheck_hide_all();
243         else
244                 n = kmemcheck_show_all();
245
246         if (n == 0)
247                 return;
248
249         --data->balance;
250
251         data->n_addrs = 0;
252
253         if (!(data->flags & X86_EFLAGS_TF))
254                 regs->flags &= ~X86_EFLAGS_TF;
255         if (data->flags & X86_EFLAGS_IF)
256                 regs->flags |= X86_EFLAGS_IF;
257 }
258
259 void kmemcheck_show_pages(struct page *p, unsigned int n)
260 {
261         unsigned int i;
262
263         for (i = 0; i < n; ++i) {
264                 unsigned long address;
265                 pte_t *pte;
266                 unsigned int level;
267
268                 address = (unsigned long) page_address(&p[i]);
269                 pte = lookup_address(address, &level);
270                 BUG_ON(!pte);
271                 BUG_ON(level != PG_LEVEL_4K);
272
273                 set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
274                 set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_HIDDEN));
275                 __flush_tlb_one(address);
276         }
277 }
278
279 bool kmemcheck_page_is_tracked(struct page *p)
280 {
281         /* This will also check the "hidden" flag of the PTE. */
282         return kmemcheck_pte_lookup((unsigned long) page_address(p));
283 }
284
285 void kmemcheck_hide_pages(struct page *p, unsigned int n)
286 {
287         unsigned int i;
288
289         for (i = 0; i < n; ++i) {
290                 unsigned long address;
291                 pte_t *pte;
292                 unsigned int level;
293
294                 address = (unsigned long) page_address(&p[i]);
295                 pte = lookup_address(address, &level);
296                 BUG_ON(!pte);
297                 BUG_ON(level != PG_LEVEL_4K);
298
299                 set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
300                 set_pte(pte, __pte(pte_val(*pte) | _PAGE_HIDDEN));
301                 __flush_tlb_one(address);
302         }
303 }
304
305 /* Access may NOT cross page boundary */
306 static void kmemcheck_read_strict(struct pt_regs *regs,
307         unsigned long addr, unsigned int size)
308 {
309         void *shadow;
310         enum kmemcheck_shadow status;
311
312         shadow = kmemcheck_shadow_lookup(addr);
313         if (!shadow)
314                 return;
315
316         kmemcheck_save_addr(addr);
317         status = kmemcheck_shadow_test(shadow, size);
318         if (status == KMEMCHECK_SHADOW_INITIALIZED)
319                 return;
320
321         if (kmemcheck_enabled)
322                 kmemcheck_error_save(status, addr, size, regs);
323
324         if (kmemcheck_enabled == 2)
325                 kmemcheck_enabled = 0;
326
327         /* Don't warn about it again. */
328         kmemcheck_shadow_set(shadow, size);
329 }
330
331 /* Access may cross page boundary */
332 static void kmemcheck_read(struct pt_regs *regs,
333         unsigned long addr, unsigned int size)
334 {
335         unsigned long page = addr & PAGE_MASK;
336         unsigned long next_addr = addr + size - 1;
337         unsigned long next_page = next_addr & PAGE_MASK;
338
339         if (likely(page == next_page)) {
340                 kmemcheck_read_strict(regs, addr, size);
341                 return;
342         }
343
344         /*
345          * What we do is basically to split the access across the
346          * two pages and handle each part separately. Yes, this means
347          * that we may now see reads that are 3 + 5 bytes, for
348          * example (and if both are uninitialized, there will be two
349          * reports), but it makes the code a lot simpler.
350          */
351         kmemcheck_read_strict(regs, addr, next_page - addr);
352         kmemcheck_read_strict(regs, next_page, next_addr - next_page);
353 }
354
355 static void kmemcheck_write_strict(struct pt_regs *regs,
356         unsigned long addr, unsigned int size)
357 {
358         void *shadow;
359
360         shadow = kmemcheck_shadow_lookup(addr);
361         if (!shadow)
362                 return;
363
364         kmemcheck_save_addr(addr);
365         kmemcheck_shadow_set(shadow, size);
366 }
367
368 static void kmemcheck_write(struct pt_regs *regs,
369         unsigned long addr, unsigned int size)
370 {
371         unsigned long page = addr & PAGE_MASK;
372         unsigned long next_addr = addr + size - 1;
373         unsigned long next_page = next_addr & PAGE_MASK;
374
375         if (likely(page == next_page)) {
376                 kmemcheck_write_strict(regs, addr, size);
377                 return;
378         }
379
380         /* See comment in kmemcheck_read(). */
381         kmemcheck_write_strict(regs, addr, next_page - addr);
382         kmemcheck_write_strict(regs, next_page, next_addr - next_page);
383 }
384
385 /*
386  * Copying is hard. We have two addresses, each of which may be split across
387  * a page (and each page will have different shadow addresses).
388  */
389 static void kmemcheck_copy(struct pt_regs *regs,
390         unsigned long src_addr, unsigned long dst_addr, unsigned int size)
391 {
392         uint8_t shadow[8];
393         enum kmemcheck_shadow status;
394
395         unsigned long page;
396         unsigned long next_addr;
397         unsigned long next_page;
398
399         uint8_t *x;
400         unsigned int i;
401         unsigned int n;
402
403         BUG_ON(size > sizeof(shadow));
404
405         page = src_addr & PAGE_MASK;
406         next_addr = src_addr + size - 1;
407         next_page = next_addr & PAGE_MASK;
408
409         if (likely(page == next_page)) {
410                 /* Same page */
411                 x = kmemcheck_shadow_lookup(src_addr);
412                 if (x) {
413                         kmemcheck_save_addr(src_addr);
414                         for (i = 0; i < size; ++i)
415                                 shadow[i] = x[i];
416                 } else {
417                         for (i = 0; i < size; ++i)
418                                 shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
419                 }
420         } else {
421                 n = next_page - src_addr;
422                 BUG_ON(n > sizeof(shadow));
423
424                 /* First page */
425                 x = kmemcheck_shadow_lookup(src_addr);
426                 if (x) {
427                         kmemcheck_save_addr(src_addr);
428                         for (i = 0; i < n; ++i)
429                                 shadow[i] = x[i];
430                 } else {
431                         /* Not tracked */
432                         for (i = 0; i < n; ++i)
433                                 shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
434                 }
435
436                 /* Second page */
437                 x = kmemcheck_shadow_lookup(next_page);
438                 if (x) {
439                         kmemcheck_save_addr(next_page);
440                         for (i = n; i < size; ++i)
441                                 shadow[i] = x[i - n];
442                 } else {
443                         /* Not tracked */
444                         for (i = n; i < size; ++i)
445                                 shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
446                 }
447         }
448
449         page = dst_addr & PAGE_MASK;
450         next_addr = dst_addr + size - 1;
451         next_page = next_addr & PAGE_MASK;
452
453         if (likely(page == next_page)) {
454                 /* Same page */
455                 x = kmemcheck_shadow_lookup(dst_addr);
456                 if (x) {
457                         kmemcheck_save_addr(dst_addr);
458                         for (i = 0; i < size; ++i) {
459                                 x[i] = shadow[i];
460                                 shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
461                         }
462                 }
463         } else {
464                 n = next_page - dst_addr;
465                 BUG_ON(n > sizeof(shadow));
466
467                 /* First page */
468                 x = kmemcheck_shadow_lookup(dst_addr);
469                 if (x) {
470                         kmemcheck_save_addr(dst_addr);
471                         for (i = 0; i < n; ++i) {
472                                 x[i] = shadow[i];
473                                 shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
474                         }
475                 }
476
477                 /* Second page */
478                 x = kmemcheck_shadow_lookup(next_page);
479                 if (x) {
480                         kmemcheck_save_addr(next_page);
481                         for (i = n; i < size; ++i) {
482                                 x[i - n] = shadow[i];
483                                 shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
484                         }
485                 }
486         }
487
488         status = kmemcheck_shadow_test(shadow, size);
489         if (status == KMEMCHECK_SHADOW_INITIALIZED)
490                 return;
491
492         if (kmemcheck_enabled)
493                 kmemcheck_error_save(status, src_addr, size, regs);
494
495         if (kmemcheck_enabled == 2)
496                 kmemcheck_enabled = 0;
497 }
498
499 enum kmemcheck_method {
500         KMEMCHECK_READ,
501         KMEMCHECK_WRITE,
502 };
503
504 static void kmemcheck_access(struct pt_regs *regs,
505         unsigned long fallback_address, enum kmemcheck_method fallback_method)
506 {
507         const uint8_t *insn;
508         const uint8_t *insn_primary;
509         unsigned int size;
510
511         struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
512
513         /* Recursive fault -- ouch. */
514         if (data->busy) {
515                 kmemcheck_show_addr(fallback_address);
516                 kmemcheck_error_save_bug(regs);
517                 return;
518         }
519
520         data->busy = true;
521
522         insn = (const uint8_t *) regs->ip;
523         insn_primary = kmemcheck_opcode_get_primary(insn);
524
525         kmemcheck_opcode_decode(insn, &size);
526
527         switch (insn_primary[0]) {
528 #ifdef CONFIG_KMEMCHECK_BITOPS_OK
529                 /* AND, OR, XOR */
530                 /*
531                  * Unfortunately, these instructions have to be excluded from
532                  * our regular checking since they access only some (and not
533                  * all) bits. This clears out "bogus" bitfield-access warnings.
534                  */
535         case 0x80:
536         case 0x81:
537         case 0x82:
538         case 0x83:
539                 switch ((insn_primary[1] >> 3) & 7) {
540                         /* OR */
541                 case 1:
542                         /* AND */
543                 case 4:
544                         /* XOR */
545                 case 6:
546                         kmemcheck_write(regs, fallback_address, size);
547                         goto out;
548
549                         /* ADD */
550                 case 0:
551                         /* ADC */
552                 case 2:
553                         /* SBB */
554                 case 3:
555                         /* SUB */
556                 case 5:
557                         /* CMP */
558                 case 7:
559                         break;
560                 }
561                 break;
562 #endif
563
564                 /* MOVS, MOVSB, MOVSW, MOVSD */
565         case 0xa4:
566         case 0xa5:
567                 /*
568                  * These instructions are special because they take two
569                  * addresses, but we only get one page fault.
570                  */
571                 kmemcheck_copy(regs, regs->si, regs->di, size);
572                 goto out;
573
574                 /* CMPS, CMPSB, CMPSW, CMPSD */
575         case 0xa6:
576         case 0xa7:
577                 kmemcheck_read(regs, regs->si, size);
578                 kmemcheck_read(regs, regs->di, size);
579                 goto out;
580         }
581
582         /*
583          * If the opcode isn't special in any way, we use the data from the
584          * page fault handler to determine the address and type of memory
585          * access.
586          */
587         switch (fallback_method) {
588         case KMEMCHECK_READ:
589                 kmemcheck_read(regs, fallback_address, size);
590                 goto out;
591         case KMEMCHECK_WRITE:
592                 kmemcheck_write(regs, fallback_address, size);
593                 goto out;
594         }
595
596 out:
597         data->busy = false;
598 }
599
600 bool kmemcheck_fault(struct pt_regs *regs, unsigned long address,
601         unsigned long error_code)
602 {
603         pte_t *pte;
604
605         /*
606          * XXX: Is it safe to assume that memory accesses from virtual 86
607          * mode or non-kernel code segments will _never_ access kernel
608          * memory (e.g. tracked pages)? For now, we need this to avoid
609          * invoking kmemcheck for PnP BIOS calls.
610          */
611         if (regs->flags & X86_VM_MASK)
612                 return false;
613         if (regs->cs != __KERNEL_CS)
614                 return false;
615
616         pte = kmemcheck_pte_lookup(address);
617         if (!pte)
618                 return false;
619
620         if (error_code & 2)
621                 kmemcheck_access(regs, address, KMEMCHECK_WRITE);
622         else
623                 kmemcheck_access(regs, address, KMEMCHECK_READ);
624
625         kmemcheck_show(regs);
626         return true;
627 }
628
629 bool kmemcheck_trap(struct pt_regs *regs)
630 {
631         if (!kmemcheck_active(regs))
632                 return false;
633
634         /* We're done. */
635         kmemcheck_hide(regs);
636         return true;
637 }