2 * drivers/base/power/trace.c
4 * Copyright (C) 2006 Linus Torvalds
6 * Trace facility for suspend/resume problems, when none of the
7 * devices may be working.
10 #include <linux/pm-trace.h>
11 #include <linux/export.h>
12 #include <linux/rtc.h>
13 #include <linux/suspend.h>
14 #include <linux/init.h>
16 #include <linux/mc146818rtc.h>
21 * Horrid, horrid, horrid.
23 * It turns out that the _only_ piece of hardware that actually
24 * keeps its value across a hard boot (and, more importantly, the
25 * POST init sequence) is literally the realtime clock.
27 * Never mind that an RTC chip has 114 bytes (and often a whole
28 * other bank of an additional 128 bytes) of nice SRAM that is
29 * _designed_ to keep data - the POST will clear it. So we literally
30 * can just use the few bytes of actual time data, which means that
31 * we're really limited.
33 * It means, for example, that we can't use the seconds at all
34 * (since the time between the hang and the boot might be more
35 * than a minute), and we'd better not depend on the low bits of
38 * There are the wday fields etc, but I wouldn't guarantee those
39 * are dependable either. And if the date isn't valid, either the
40 * hw or POST will do strange things.
45 * - day-of-month: 1-28
49 * Giving us a total range of 0-16128000 (0xf61800), ie less
50 * than 24 bits of actual data we can save across reboots.
52 * And if your box can't boot in less than three minutes,
55 * Now, almost 24 bits of data is pitifully small, so we need
56 * to be pretty dense if we want to use it for anything nice.
57 * What we do is that instead of saving off nice readable info,
58 * we save off _hashes_ of information that we can hopefully
59 * regenerate after the reboot.
61 * In particular, this means that we might be unlucky, and hit
62 * a case where we have a hash collision, and we end up not
63 * being able to tell for certain exactly which case happened.
64 * But that's hopefully unlikely.
66 * What we do is to take the bits we can fit, and split them
67 * into three parts (16*997*1009 = 16095568), and use the values
69 * - 0-15: user-settable
70 * - 0-996: file + line number
74 #define FILEHASH (997)
75 #define DEVHASH (1009)
77 #define DEVSEED (7919)
79 bool pm_trace_rtc_abused __read_mostly;
80 EXPORT_SYMBOL_GPL(pm_trace_rtc_abused);
82 static unsigned int dev_hash_value;
84 static int set_magic_time(unsigned int user, unsigned int file, unsigned int device)
86 unsigned int n = user + USERHASH*(file + FILEHASH*device);
89 static struct rtc_time time = {
94 .tm_mon = 5, // June - counting from zero
101 time.tm_year = (n % 100);
103 time.tm_mon = (n % 12);
105 time.tm_mday = (n % 28) + 1;
107 time.tm_hour = (n % 24);
109 time.tm_min = (n % 20) * 3;
111 mc146818_set_time(&time);
112 pm_trace_rtc_abused = true;
116 static unsigned int read_magic_time(void)
118 struct rtc_time time;
121 mc146818_get_time(&time);
122 pr_info("RTC time: %2d:%02d:%02d, date: %02d/%02d/%02d\n",
123 time.tm_hour, time.tm_min, time.tm_sec,
124 time.tm_mon + 1, time.tm_mday, time.tm_year % 100);
125 val = time.tm_year; /* 100 years */
128 val += time.tm_mon * 100; /* 12 months */
129 val += (time.tm_mday-1) * 100 * 12; /* 28 month-days */
130 val += time.tm_hour * 100 * 12 * 28; /* 24 hours */
131 val += (time.tm_min / 3) * 100 * 12 * 28 * 24; /* 20 3-minute intervals */
136 * This is just the sdbm hash function with a user-supplied
137 * seed and final size parameter.
139 static unsigned int hash_string(unsigned int seed, const char *data, unsigned int mod)
142 while ((c = *data++) != 0) {
143 seed = (seed << 16) + (seed << 6) - seed + c;
148 void set_trace_device(struct device *dev)
150 dev_hash_value = hash_string(DEVSEED, dev_name(dev), DEVHASH);
152 EXPORT_SYMBOL(set_trace_device);
155 * We could just take the "tracedata" index into the .tracedata
156 * section instead. Generating a hash of the data gives us a
157 * chance to work across kernel versions, and perhaps more
158 * importantly it also gives us valid/invalid check (ie we will
159 * likely not give totally bogus reports - if the hash matches,
160 * it's not any guarantee, but it's a high _likelihood_ that
161 * the match is valid).
163 void generate_pm_trace(const void *tracedata, unsigned int user)
165 unsigned short lineno = *(unsigned short *)tracedata;
166 const char *file = *(const char **)(tracedata + 2);
167 unsigned int user_hash_value, file_hash_value;
169 if (!x86_platform.legacy.rtc)
172 user_hash_value = user % USERHASH;
173 file_hash_value = hash_string(lineno, file, FILEHASH);
174 set_magic_time(user_hash_value, file_hash_value, dev_hash_value);
176 EXPORT_SYMBOL(generate_pm_trace);
178 extern char __tracedata_start[], __tracedata_end[];
179 static int show_file_hash(unsigned int value)
185 for (tracedata = __tracedata_start ; tracedata < __tracedata_end ;
186 tracedata += 2 + sizeof(unsigned long)) {
187 unsigned short lineno = *(unsigned short *)tracedata;
188 const char *file = *(const char **)(tracedata + 2);
189 unsigned int hash = hash_string(lineno, file, FILEHASH);
192 pr_info(" hash matches %s:%u\n", file, lineno);
198 static int show_dev_hash(unsigned int value)
201 struct list_head *entry;
204 entry = dpm_list.prev;
205 while (entry != &dpm_list) {
206 struct device * dev = to_device(entry);
207 unsigned int hash = hash_string(DEVSEED, dev_name(dev), DEVHASH);
209 dev_info(dev, "hash matches\n");
218 static unsigned int hash_value_early_read;
220 int show_trace_dev_match(char *buf, size_t size)
222 unsigned int value = hash_value_early_read / (USERHASH * FILEHASH);
224 struct list_head *entry;
227 * It's possible that multiple devices will match the hash and we can't
228 * tell which is the culprit, so it's best to output them all.
231 entry = dpm_list.prev;
232 while (size && entry != &dpm_list) {
233 struct device *dev = to_device(entry);
234 unsigned int hash = hash_string(DEVSEED, dev_name(dev),
237 int len = snprintf(buf, size, "%s\n",
238 dev_driver_string(dev));
252 pm_trace_notify(struct notifier_block *nb, unsigned long mode, void *_unused)
255 case PM_POST_HIBERNATION:
256 case PM_POST_SUSPEND:
257 if (pm_trace_rtc_abused) {
258 pm_trace_rtc_abused = false;
259 pr_warn("Possible incorrect RTC due to pm_trace, please use 'ntpdate' or 'rdate' to reset it.\n");
268 static struct notifier_block pm_trace_nb = {
269 .notifier_call = pm_trace_notify,
272 static int early_resume_init(void)
274 if (!x86_platform.legacy.rtc)
277 hash_value_early_read = read_magic_time();
278 register_pm_notifier(&pm_trace_nb);
282 static int late_resume_init(void)
284 unsigned int val = hash_value_early_read;
285 unsigned int user, file, dev;
287 if (!x86_platform.legacy.rtc)
290 user = val % USERHASH;
291 val = val / USERHASH;
292 file = val % FILEHASH;
293 val = val / FILEHASH;
294 dev = val /* % DEVHASH */;
296 pr_info(" Magic number: %d:%d:%d\n", user, file, dev);
297 show_file_hash(file);
302 core_initcall(early_resume_init);
303 late_initcall(late_resume_init);