check.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <linux/module.h>
  2. #include <linux/sched.h>
  3. #include <linux/kthread.h>
  4. #include <linux/workqueue.h>
  5. #include <linux/memblock.h>
  6. #include <asm/proto.h>
  7. /*
  8. * Some BIOSes seem to corrupt the low 64k of memory during events
  9. * like suspend/resume and unplugging an HDMI cable. Reserve all
  10. * remaining free memory in that area and fill it with a distinct
  11. * pattern.
  12. */
  13. #define MAX_SCAN_AREAS 8
  14. static int __read_mostly memory_corruption_check = -1;
  15. static unsigned __read_mostly corruption_check_size = 64*1024;
  16. static unsigned __read_mostly corruption_check_period = 60; /* seconds */
  17. static struct scan_area {
  18. u64 addr;
  19. u64 size;
  20. } scan_areas[MAX_SCAN_AREAS];
  21. static int num_scan_areas;
  22. static __init int set_corruption_check(char *arg)
  23. {
  24. char *end;
  25. memory_corruption_check = simple_strtol(arg, &end, 10);
  26. return (*end == 0) ? 0 : -EINVAL;
  27. }
  28. early_param("memory_corruption_check", set_corruption_check);
  29. static __init int set_corruption_check_period(char *arg)
  30. {
  31. char *end;
  32. corruption_check_period = simple_strtoul(arg, &end, 10);
  33. return (*end == 0) ? 0 : -EINVAL;
  34. }
  35. early_param("memory_corruption_check_period", set_corruption_check_period);
  36. static __init int set_corruption_check_size(char *arg)
  37. {
  38. char *end;
  39. unsigned size;
  40. size = memparse(arg, &end);
  41. if (*end == '\0')
  42. corruption_check_size = size;
  43. return (size == corruption_check_size) ? 0 : -EINVAL;
  44. }
  45. early_param("memory_corruption_check_size", set_corruption_check_size);
  46. void __init setup_bios_corruption_check(void)
  47. {
  48. u64 addr = PAGE_SIZE; /* assume first page is reserved anyway */
  49. if (memory_corruption_check == -1) {
  50. memory_corruption_check =
  51. #ifdef CONFIG_X86_BOOTPARAM_MEMORY_CORRUPTION_CHECK
  52. 1
  53. #else
  54. 0
  55. #endif
  56. ;
  57. }
  58. if (corruption_check_size == 0)
  59. memory_corruption_check = 0;
  60. if (!memory_corruption_check)
  61. return;
  62. corruption_check_size = round_up(corruption_check_size, PAGE_SIZE);
  63. while (addr < corruption_check_size && num_scan_areas < MAX_SCAN_AREAS) {
  64. u64 size;
  65. addr = memblock_x86_find_in_range_size(addr, &size, PAGE_SIZE);
  66. if (addr == MEMBLOCK_ERROR)
  67. break;
  68. if (addr >= corruption_check_size)
  69. break;
  70. if ((addr + size) > corruption_check_size)
  71. size = corruption_check_size - addr;
  72. memblock_x86_reserve_range(addr, addr + size, "SCAN RAM");
  73. scan_areas[num_scan_areas].addr = addr;
  74. scan_areas[num_scan_areas].size = size;
  75. num_scan_areas++;
  76. /* Assume we've already mapped this early memory */
  77. memset(__va(addr), 0, size);
  78. addr += size;
  79. }
  80. if (num_scan_areas)
  81. printk(KERN_INFO "Scanning %d areas for low memory corruption\n", num_scan_areas);
  82. }
  83. void check_for_bios_corruption(void)
  84. {
  85. int i;
  86. int corruption = 0;
  87. if (!memory_corruption_check)
  88. return;
  89. for (i = 0; i < num_scan_areas; i++) {
  90. unsigned long *addr = __va(scan_areas[i].addr);
  91. unsigned long size = scan_areas[i].size;
  92. for (; size; addr++, size -= sizeof(unsigned long)) {
  93. if (!*addr)
  94. continue;
  95. printk(KERN_ERR "Corrupted low memory at %p (%lx phys) = %08lx\n",
  96. addr, __pa(addr), *addr);
  97. corruption = 1;
  98. *addr = 0;
  99. }
  100. }
  101. WARN_ONCE(corruption, KERN_ERR "Memory corruption detected in low memory\n");
  102. }
  103. static void check_corruption(struct work_struct *dummy);
  104. static DECLARE_DELAYED_WORK(bios_check_work, check_corruption);
  105. static void check_corruption(struct work_struct *dummy)
  106. {
  107. check_for_bios_corruption();
  108. schedule_delayed_work(&bios_check_work,
  109. round_jiffies_relative(corruption_check_period*HZ));
  110. }
  111. static int start_periodic_check_for_corruption(void)
  112. {
  113. if (!num_scan_areas || !memory_corruption_check || corruption_check_period == 0)
  114. return 0;
  115. printk(KERN_INFO "Scanning for low memory corruption every %d seconds\n",
  116. corruption_check_period);
  117. /* First time we run the checks right away */
  118. schedule_delayed_work(&bios_check_work, 0);
  119. return 0;
  120. }
  121. module_init(start_periodic_check_for_corruption);