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.)
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.
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>
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>
24 #include <asm/cacheflush.h>
25 #include <asm/kmemcheck.h>
26 #include <asm/pgtable.h>
27 #include <asm/tlbflush.h>
34 #ifdef CONFIG_KMEMCHECK_DISABLED_BY_DEFAULT
35 # define KMEMCHECK_ENABLED 0
38 #ifdef CONFIG_KMEMCHECK_ENABLED_BY_DEFAULT
39 # define KMEMCHECK_ENABLED 1
42 #ifdef CONFIG_KMEMCHECK_ONESHOT_BY_DEFAULT
43 # define KMEMCHECK_ENABLED 2
46 int kmemcheck_enabled = KMEMCHECK_ENABLED;
48 int __init kmemcheck_init(void)
50 printk(KERN_INFO "kmemcheck: \"Bugs, beware!\"\n");
54 * Limit SMP to use a single CPU. We rely on the fact that this code
55 * runs before SMP is set up.
57 if (setup_max_cpus > 1) {
59 "kmemcheck: Limiting number of CPUs to 1.\n");
67 early_initcall(kmemcheck_init);
69 #ifdef CONFIG_KMEMCHECK_DISABLED_BY_DEFAULT
70 int kmemcheck_enabled = 0;
73 #ifdef CONFIG_KMEMCHECK_ENABLED_BY_DEFAULT
74 int kmemcheck_enabled = 1;
77 #ifdef CONFIG_KMEMCHECK_ONESHOT_BY_DEFAULT
78 int kmemcheck_enabled = 2;
82 * We need to parse the kmemcheck= option before any memory is allocated.
84 static int __init param_kmemcheck(char *str)
89 sscanf(str, "%d", &kmemcheck_enabled);
93 early_param("kmemcheck", param_kmemcheck);
95 int kmemcheck_show_addr(unsigned long address)
99 pte = kmemcheck_pte_lookup(address);
103 set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
104 __flush_tlb_one(address);
108 int kmemcheck_hide_addr(unsigned long address)
112 pte = kmemcheck_pte_lookup(address);
116 set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
117 __flush_tlb_one(address);
121 struct kmemcheck_context {
126 * There can be at most two memory operands to an instruction, but
127 * each address can cross a page boundary -- so we may need up to
128 * four addresses that must be hidden/revealed for each fault.
130 unsigned long addr[4];
131 unsigned long n_addrs;
134 /* Data size of the instruction that caused a fault. */
138 static DEFINE_PER_CPU(struct kmemcheck_context, kmemcheck_context);
140 bool kmemcheck_active(struct pt_regs *regs)
142 struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
144 return data->balance > 0;
147 /* Save an address that needs to be shown/hidden */
148 static void kmemcheck_save_addr(unsigned long addr)
150 struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
152 BUG_ON(data->n_addrs >= ARRAY_SIZE(data->addr));
153 data->addr[data->n_addrs++] = addr;
156 static unsigned int kmemcheck_show_all(void)
158 struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
163 for (i = 0; i < data->n_addrs; ++i)
164 n += kmemcheck_show_addr(data->addr[i]);
169 static unsigned int kmemcheck_hide_all(void)
171 struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
176 for (i = 0; i < data->n_addrs; ++i)
177 n += kmemcheck_hide_addr(data->addr[i]);
183 * Called from the #PF handler.
185 void kmemcheck_show(struct pt_regs *regs)
187 struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
189 BUG_ON(!irqs_disabled());
191 if (unlikely(data->balance != 0)) {
192 kmemcheck_show_all();
193 kmemcheck_error_save_bug(regs);
199 * None of the addresses actually belonged to kmemcheck. Note that
200 * this is not an error.
202 if (kmemcheck_show_all() == 0)
208 * The IF needs to be cleared as well, so that the faulting
209 * instruction can run "uninterrupted". Otherwise, we might take
210 * an interrupt and start executing that before we've had a chance
211 * to hide the page again.
213 * NOTE: In the rare case of multiple faults, we must not override
214 * the original flags:
216 if (!(regs->flags & X86_EFLAGS_TF))
217 data->flags = regs->flags;
219 regs->flags |= X86_EFLAGS_TF;
220 regs->flags &= ~X86_EFLAGS_IF;
224 * Called from the #DB handler.
226 void kmemcheck_hide(struct pt_regs *regs)
228 struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
231 BUG_ON(!irqs_disabled());
233 if (data->balance == 0)
236 if (unlikely(data->balance != 1)) {
237 kmemcheck_show_all();
238 kmemcheck_error_save_bug(regs);
242 if (!(data->flags & X86_EFLAGS_TF))
243 regs->flags &= ~X86_EFLAGS_TF;
244 if (data->flags & X86_EFLAGS_IF)
245 regs->flags |= X86_EFLAGS_IF;
249 if (kmemcheck_enabled)
250 n = kmemcheck_hide_all();
252 n = kmemcheck_show_all();
261 if (!(data->flags & X86_EFLAGS_TF))
262 regs->flags &= ~X86_EFLAGS_TF;
263 if (data->flags & X86_EFLAGS_IF)
264 regs->flags |= X86_EFLAGS_IF;
267 void kmemcheck_show_pages(struct page *p, unsigned int n)
271 for (i = 0; i < n; ++i) {
272 unsigned long address;
276 address = (unsigned long) page_address(&p[i]);
277 pte = lookup_address(address, &level);
279 BUG_ON(level != PG_LEVEL_4K);
281 set_pte(pte, __pte(pte_val(*pte) | _PAGE_PRESENT));
282 set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_HIDDEN));
283 __flush_tlb_one(address);
287 bool kmemcheck_page_is_tracked(struct page *p)
289 /* This will also check the "hidden" flag of the PTE. */
290 return kmemcheck_pte_lookup((unsigned long) page_address(p));
293 void kmemcheck_hide_pages(struct page *p, unsigned int n)
297 for (i = 0; i < n; ++i) {
298 unsigned long address;
302 address = (unsigned long) page_address(&p[i]);
303 pte = lookup_address(address, &level);
305 BUG_ON(level != PG_LEVEL_4K);
307 set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_PRESENT));
308 set_pte(pte, __pte(pte_val(*pte) | _PAGE_HIDDEN));
309 __flush_tlb_one(address);
313 /* Access may NOT cross page boundary */
314 static void kmemcheck_read_strict(struct pt_regs *regs,
315 unsigned long addr, unsigned int size)
318 enum kmemcheck_shadow status;
320 shadow = kmemcheck_shadow_lookup(addr);
324 kmemcheck_save_addr(addr);
325 status = kmemcheck_shadow_test(shadow, size);
326 if (status == KMEMCHECK_SHADOW_INITIALIZED)
329 if (kmemcheck_enabled)
330 kmemcheck_error_save(status, addr, size, regs);
332 if (kmemcheck_enabled == 2)
333 kmemcheck_enabled = 0;
335 /* Don't warn about it again. */
336 kmemcheck_shadow_set(shadow, size);
339 /* Access may cross page boundary */
340 static void kmemcheck_read(struct pt_regs *regs,
341 unsigned long addr, unsigned int size)
343 unsigned long page = addr & PAGE_MASK;
344 unsigned long next_addr = addr + size - 1;
345 unsigned long next_page = next_addr & PAGE_MASK;
347 if (likely(page == next_page)) {
348 kmemcheck_read_strict(regs, addr, size);
353 * What we do is basically to split the access across the
354 * two pages and handle each part separately. Yes, this means
355 * that we may now see reads that are 3 + 5 bytes, for
356 * example (and if both are uninitialized, there will be two
357 * reports), but it makes the code a lot simpler.
359 kmemcheck_read_strict(regs, addr, next_page - addr);
360 kmemcheck_read_strict(regs, next_page, next_addr - next_page);
363 static void kmemcheck_write_strict(struct pt_regs *regs,
364 unsigned long addr, unsigned int size)
368 shadow = kmemcheck_shadow_lookup(addr);
372 kmemcheck_save_addr(addr);
373 kmemcheck_shadow_set(shadow, size);
376 static void kmemcheck_write(struct pt_regs *regs,
377 unsigned long addr, unsigned int size)
379 unsigned long page = addr & PAGE_MASK;
380 unsigned long next_addr = addr + size - 1;
381 unsigned long next_page = next_addr & PAGE_MASK;
383 if (likely(page == next_page)) {
384 kmemcheck_write_strict(regs, addr, size);
388 /* See comment in kmemcheck_read(). */
389 kmemcheck_write_strict(regs, addr, next_page - addr);
390 kmemcheck_write_strict(regs, next_page, next_addr - next_page);
394 * Copying is hard. We have two addresses, each of which may be split across
395 * a page (and each page will have different shadow addresses).
397 static void kmemcheck_copy(struct pt_regs *regs,
398 unsigned long src_addr, unsigned long dst_addr, unsigned int size)
401 enum kmemcheck_shadow status;
404 unsigned long next_addr;
405 unsigned long next_page;
411 BUG_ON(size > sizeof(shadow));
413 page = src_addr & PAGE_MASK;
414 next_addr = src_addr + size - 1;
415 next_page = next_addr & PAGE_MASK;
417 if (likely(page == next_page)) {
419 x = kmemcheck_shadow_lookup(src_addr);
421 kmemcheck_save_addr(src_addr);
422 for (i = 0; i < size; ++i)
425 for (i = 0; i < size; ++i)
426 shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
429 n = next_page - src_addr;
430 BUG_ON(n > sizeof(shadow));
433 x = kmemcheck_shadow_lookup(src_addr);
435 kmemcheck_save_addr(src_addr);
436 for (i = 0; i < n; ++i)
440 for (i = 0; i < n; ++i)
441 shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
445 x = kmemcheck_shadow_lookup(next_page);
447 kmemcheck_save_addr(next_page);
448 for (i = n; i < size; ++i)
449 shadow[i] = x[i - n];
452 for (i = n; i < size; ++i)
453 shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
457 page = dst_addr & PAGE_MASK;
458 next_addr = dst_addr + size - 1;
459 next_page = next_addr & PAGE_MASK;
461 if (likely(page == next_page)) {
463 x = kmemcheck_shadow_lookup(dst_addr);
465 kmemcheck_save_addr(dst_addr);
466 for (i = 0; i < size; ++i) {
468 shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
472 n = next_page - dst_addr;
473 BUG_ON(n > sizeof(shadow));
476 x = kmemcheck_shadow_lookup(dst_addr);
478 kmemcheck_save_addr(dst_addr);
479 for (i = 0; i < n; ++i) {
481 shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
486 x = kmemcheck_shadow_lookup(next_page);
488 kmemcheck_save_addr(next_page);
489 for (i = n; i < size; ++i) {
490 x[i - n] = shadow[i];
491 shadow[i] = KMEMCHECK_SHADOW_INITIALIZED;
496 status = kmemcheck_shadow_test(shadow, size);
497 if (status == KMEMCHECK_SHADOW_INITIALIZED)
500 if (kmemcheck_enabled)
501 kmemcheck_error_save(status, src_addr, size, regs);
503 if (kmemcheck_enabled == 2)
504 kmemcheck_enabled = 0;
507 enum kmemcheck_method {
512 static void kmemcheck_access(struct pt_regs *regs,
513 unsigned long fallback_address, enum kmemcheck_method fallback_method)
516 const uint8_t *insn_primary;
519 struct kmemcheck_context *data = &__get_cpu_var(kmemcheck_context);
521 /* Recursive fault -- ouch. */
523 kmemcheck_show_addr(fallback_address);
524 kmemcheck_error_save_bug(regs);
530 insn = (const uint8_t *) regs->ip;
531 insn_primary = kmemcheck_opcode_get_primary(insn);
533 kmemcheck_opcode_decode(insn, &size);
535 switch (insn_primary[0]) {
536 #ifdef CONFIG_KMEMCHECK_BITOPS_OK
539 * Unfortunately, these instructions have to be excluded from
540 * our regular checking since they access only some (and not
541 * all) bits. This clears out "bogus" bitfield-access warnings.
547 switch ((insn_primary[1] >> 3) & 7) {
554 kmemcheck_write(regs, fallback_address, size);
572 /* MOVS, MOVSB, MOVSW, MOVSD */
576 * These instructions are special because they take two
577 * addresses, but we only get one page fault.
579 kmemcheck_copy(regs, regs->si, regs->di, size);
582 /* CMPS, CMPSB, CMPSW, CMPSD */
585 kmemcheck_read(regs, regs->si, size);
586 kmemcheck_read(regs, regs->di, size);
591 * If the opcode isn't special in any way, we use the data from the
592 * page fault handler to determine the address and type of memory
595 switch (fallback_method) {
597 kmemcheck_read(regs, fallback_address, size);
599 case KMEMCHECK_WRITE:
600 kmemcheck_write(regs, fallback_address, size);
608 bool kmemcheck_fault(struct pt_regs *regs, unsigned long address,
609 unsigned long error_code)
615 * XXX: Is it safe to assume that memory accesses from virtual 86
616 * mode or non-kernel code segments will _never_ access kernel
617 * memory (e.g. tracked pages)? For now, we need this to avoid
618 * invoking kmemcheck for PnP BIOS calls.
620 if (regs->flags & X86_VM_MASK)
622 if (regs->cs != __KERNEL_CS)
625 pte = lookup_address(address, &level);
628 if (level != PG_LEVEL_4K)
630 if (!pte_hidden(*pte))
634 kmemcheck_access(regs, address, KMEMCHECK_WRITE);
636 kmemcheck_access(regs, address, KMEMCHECK_READ);
638 kmemcheck_show(regs);
642 bool kmemcheck_trap(struct pt_regs *regs)
644 if (!kmemcheck_active(regs))
648 kmemcheck_hide(regs);