]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86/mm/memtest.c
Merge branch 'topic/slab/earlyboot' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / mm / memtest.c
1 #include <linux/kernel.h>
2 #include <linux/errno.h>
3 #include <linux/string.h>
4 #include <linux/types.h>
5 #include <linux/mm.h>
6 #include <linux/smp.h>
7 #include <linux/init.h>
8 #include <linux/pfn.h>
9
10 #include <asm/e820.h>
11
12 static u64 patterns[] __initdata = {
13 0,
14 0xffffffffffffffffULL,
15 0x5555555555555555ULL,
16 0xaaaaaaaaaaaaaaaaULL,
17 0x1111111111111111ULL,
18 0x2222222222222222ULL,
19 0x4444444444444444ULL,
20 0x8888888888888888ULL,
21 0x3333333333333333ULL,
22 0x6666666666666666ULL,
23 0x9999999999999999ULL,
24 0xccccccccccccccccULL,
25 0x7777777777777777ULL,
26 0xbbbbbbbbbbbbbbbbULL,
27 0xddddddddddddddddULL,
28 0xeeeeeeeeeeeeeeeeULL,
29 0x7a6c7258554e494cULL, /* yeah ;-) */
30 };
31
32 static void __init reserve_bad_mem(u64 pattern, u64 start_bad, u64 end_bad)
33 {
34 printk(KERN_INFO " %016llx bad mem addr %010llx - %010llx reserved\n",
35 (unsigned long long) pattern,
36 (unsigned long long) start_bad,
37 (unsigned long long) end_bad);
38 reserve_early(start_bad, end_bad, "BAD RAM");
39 }
40
41 static void __init memtest(u64 pattern, u64 start_phys, u64 size)
42 {
43 u64 *p, *start, *end;
44 u64 start_bad, last_bad;
45 u64 start_phys_aligned;
46 const size_t incr = sizeof(pattern);
47
48 start_phys_aligned = ALIGN(start_phys, incr);
49 start = __va(start_phys_aligned);
50 end = start + (size - (start_phys_aligned - start_phys)) / incr;
51 start_bad = 0;
52 last_bad = 0;
53
54 for (p = start; p < end; p++)
55 *p = pattern;
56
57 for (p = start; p < end; p++, start_phys_aligned += incr) {
58 if (*p == pattern)
59 continue;
60 if (start_phys_aligned == last_bad + incr) {
61 last_bad += incr;
62 continue;
63 }
64 if (start_bad)
65 reserve_bad_mem(pattern, start_bad, last_bad + incr);
66 start_bad = last_bad = start_phys_aligned;
67 }
68 if (start_bad)
69 reserve_bad_mem(pattern, start_bad, last_bad + incr);
70 }
71
72 static void __init do_one_pass(u64 pattern, u64 start, u64 end)
73 {
74 u64 size = 0;
75
76 while (start < end) {
77 start = find_e820_area_size(start, &size, 1);
78
79 /* done ? */
80 if (start >= end)
81 break;
82 if (start + size > end)
83 size = end - start;
84
85 printk(KERN_INFO " %010llx - %010llx pattern %016llx\n",
86 (unsigned long long) start,
87 (unsigned long long) start + size,
88 (unsigned long long) cpu_to_be64(pattern));
89 memtest(pattern, start, size);
90
91 start += size;
92 }
93 }
94
95 /* default is disabled */
96 static int memtest_pattern __initdata;
97
98 static int __init parse_memtest(char *arg)
99 {
100 if (arg)
101 memtest_pattern = simple_strtoul(arg, NULL, 0);
102 else
103 memtest_pattern = ARRAY_SIZE(patterns);
104
105 return 0;
106 }
107
108 early_param("memtest", parse_memtest);
109
110 void __init early_memtest(unsigned long start, unsigned long end)
111 {
112 unsigned int i;
113 unsigned int idx = 0;
114
115 if (!memtest_pattern)
116 return;
117
118 printk(KERN_INFO "early_memtest: # of tests: %d\n", memtest_pattern);
119 for (i = 0; i < memtest_pattern; i++) {
120 idx = i % ARRAY_SIZE(patterns);
121 do_one_pass(patterns[idx], start, end);
122 }
123
124 if (idx > 0) {
125 printk(KERN_INFO "early_memtest: wipe out "
126 "test pattern from memory\n");
127 /* additional test with pattern 0 will do this */
128 do_one_pass(0, start, end);
129 }
130 }