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>
10 * Some BIOSes seem to corrupt the low 64k of memory during events
11 * like suspend/resume and unplugging an HDMI cable. Reserve all
12 * remaining free memory in that area and fill it with a distinct
15 #define MAX_SCAN_AREAS 8
17 static int __read_mostly memory_corruption_check
= -1;
19 static unsigned __read_mostly corruption_check_size
= 64*1024;
20 static unsigned __read_mostly corruption_check_period
= 60; /* seconds */
22 static struct scan_area
{
25 } scan_areas
[MAX_SCAN_AREAS
];
26 static int num_scan_areas
;
28 static __init
int set_corruption_check(char *arg
)
32 memory_corruption_check
= simple_strtol(arg
, &end
, 10);
34 return (*end
== 0) ? 0 : -EINVAL
;
36 early_param("memory_corruption_check", set_corruption_check
);
38 static __init
int set_corruption_check_period(char *arg
)
42 corruption_check_period
= simple_strtoul(arg
, &end
, 10);
44 return (*end
== 0) ? 0 : -EINVAL
;
46 early_param("memory_corruption_check_period", set_corruption_check_period
);
48 static __init
int set_corruption_check_size(char *arg
)
53 size
= memparse(arg
, &end
);
56 corruption_check_size
= size
;
58 return (size
== corruption_check_size
) ? 0 : -EINVAL
;
60 early_param("memory_corruption_check_size", set_corruption_check_size
);
63 void __init
setup_bios_corruption_check(void)
65 phys_addr_t start
, end
;
68 if (memory_corruption_check
== -1) {
69 memory_corruption_check
=
70 #ifdef CONFIG_X86_BOOTPARAM_MEMORY_CORRUPTION_CHECK
78 if (corruption_check_size
== 0)
79 memory_corruption_check
= 0;
81 if (!memory_corruption_check
)
84 corruption_check_size
= round_up(corruption_check_size
, PAGE_SIZE
);
86 for_each_free_mem_range(i
, MAX_NUMNODES
, &start
, &end
, NULL
) {
87 start
= clamp_t(phys_addr_t
, round_up(start
, PAGE_SIZE
),
88 PAGE_SIZE
, corruption_check_size
);
89 end
= clamp_t(phys_addr_t
, round_down(end
, PAGE_SIZE
),
90 PAGE_SIZE
, corruption_check_size
);
94 memblock_reserve(start
, end
- start
);
95 scan_areas
[num_scan_areas
].addr
= start
;
96 scan_areas
[num_scan_areas
].size
= end
- start
;
98 /* Assume we've already mapped this early memory */
99 memset(__va(start
), 0, end
- start
);
101 if (++num_scan_areas
>= MAX_SCAN_AREAS
)
106 printk(KERN_INFO
"Scanning %d areas for low memory corruption\n", num_scan_areas
);
110 void check_for_bios_corruption(void)
115 if (!memory_corruption_check
)
118 for (i
= 0; i
< num_scan_areas
; i
++) {
119 unsigned long *addr
= __va(scan_areas
[i
].addr
);
120 unsigned long size
= scan_areas
[i
].size
;
122 for (; size
; addr
++, size
-= sizeof(unsigned long)) {
125 printk(KERN_ERR
"Corrupted low memory at %p (%lx phys) = %08lx\n",
126 addr
, __pa(addr
), *addr
);
132 WARN_ONCE(corruption
, KERN_ERR
"Memory corruption detected in low memory\n");
135 static void check_corruption(struct work_struct
*dummy
);
136 static DECLARE_DELAYED_WORK(bios_check_work
, check_corruption
);
138 static void check_corruption(struct work_struct
*dummy
)
140 check_for_bios_corruption();
141 schedule_delayed_work(&bios_check_work
,
142 round_jiffies_relative(corruption_check_period
*HZ
));
145 static int start_periodic_check_for_corruption(void)
147 if (!num_scan_areas
|| !memory_corruption_check
|| corruption_check_period
== 0)
150 printk(KERN_INFO
"Scanning for low memory corruption every %d seconds\n",
151 corruption_check_period
);
153 /* First time we run the checks right away */
154 schedule_delayed_work(&bios_check_work
, 0);
158 module_init(start_periodic_check_for_corruption
);