check.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <linux/module.h>
  2. #include <linux/sched.h>
  3. #include <linux/kthread.h>
  4. #include <linux/workqueue.h>
  5. #include <asm/e820.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. #ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION
  14. #define MAX_SCAN_AREAS 8
  15. static int __read_mostly memory_corruption_check = -1;
  16. static unsigned __read_mostly corruption_check_size = 64*1024;
  17. static unsigned __read_mostly corruption_check_period = 60; /* seconds */
  18. static struct e820entry scan_areas[MAX_SCAN_AREAS];
  19. static int num_scan_areas;
  20. static int set_corruption_check(char *arg)
  21. {
  22. char *end;
  23. memory_corruption_check = simple_strtol(arg, &end, 10);
  24. return (*end == 0) ? 0 : -EINVAL;
  25. }
  26. early_param("memory_corruption_check", set_corruption_check);
  27. static int set_corruption_check_period(char *arg)
  28. {
  29. char *end;
  30. corruption_check_period = simple_strtoul(arg, &end, 10);
  31. return (*end == 0) ? 0 : -EINVAL;
  32. }
  33. early_param("memory_corruption_check_period", set_corruption_check_period);
  34. static int set_corruption_check_size(char *arg)
  35. {
  36. char *end;
  37. unsigned size;
  38. size = memparse(arg, &end);
  39. if (*end == '\0')
  40. corruption_check_size = size;
  41. return (size == corruption_check_size) ? 0 : -EINVAL;
  42. }
  43. early_param("memory_corruption_check_size", set_corruption_check_size);
  44. void __init setup_bios_corruption_check(void)
  45. {
  46. u64 addr = PAGE_SIZE; /* assume first page is reserved anyway */
  47. if (memory_corruption_check == -1) {
  48. memory_corruption_check =
  49. #ifdef CONFIG_X86_BOOTPARAM_MEMORY_CORRUPTION_CHECK
  50. 1
  51. #else
  52. 0
  53. #endif
  54. ;
  55. }
  56. if (corruption_check_size == 0)
  57. memory_corruption_check = 0;
  58. if (!memory_corruption_check)
  59. return;
  60. corruption_check_size = round_up(corruption_check_size, PAGE_SIZE);
  61. while (addr < corruption_check_size && num_scan_areas < MAX_SCAN_AREAS) {
  62. u64 size;
  63. addr = find_e820_area_size(addr, &size, PAGE_SIZE);
  64. if (addr == 0)
  65. break;
  66. if ((addr + size) > corruption_check_size)
  67. size = corruption_check_size - addr;
  68. if (size == 0)
  69. break;
  70. e820_update_range(addr, size, E820_RAM, E820_RESERVED);
  71. scan_areas[num_scan_areas].addr = addr;
  72. scan_areas[num_scan_areas].size = size;
  73. num_scan_areas++;
  74. /* Assume we've already mapped this early memory */
  75. memset(__va(addr), 0, size);
  76. addr += size;
  77. }
  78. printk(KERN_INFO "Scanning %d areas for low memory corruption\n",
  79. num_scan_areas);
  80. update_e820();
  81. }
  82. void check_for_bios_corruption(void)
  83. {
  84. int i;
  85. int corruption = 0;
  86. printk("dot\n");
  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(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 (!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);
  122. #endif