1 /* 2 * drivers/base/power/trace.c 3 * 4 * Copyright (C) 2006 Linus Torvalds 5 * 6 * Trace facility for suspend/resume problems, when none of the 7 * devices may be working. 8 */ 9 10 #include <linux/pm-trace.h> 11 #include <linux/export.h> 12 #include <linux/rtc.h> 13 #include <linux/suspend.h> 14 15 #include <linux/mc146818rtc.h> 16 17 #include "power.h" 18 19 /* 20 * Horrid, horrid, horrid. 21 * 22 * It turns out that the _only_ piece of hardware that actually 23 * keeps its value across a hard boot (and, more importantly, the 24 * POST init sequence) is literally the realtime clock. 25 * 26 * Never mind that an RTC chip has 114 bytes (and often a whole 27 * other bank of an additional 128 bytes) of nice SRAM that is 28 * _designed_ to keep data - the POST will clear it. So we literally 29 * can just use the few bytes of actual time data, which means that 30 * we're really limited. 31 * 32 * It means, for example, that we can't use the seconds at all 33 * (since the time between the hang and the boot might be more 34 * than a minute), and we'd better not depend on the low bits of 35 * the minutes either. 36 * 37 * There are the wday fields etc, but I wouldn't guarantee those 38 * are dependable either. And if the date isn't valid, either the 39 * hw or POST will do strange things. 40 * 41 * So we're left with: 42 * - year: 0-99 43 * - month: 0-11 44 * - day-of-month: 1-28 45 * - hour: 0-23 46 * - min: (0-30)*2 47 * 48 * Giving us a total range of 0-16128000 (0xf61800), ie less 49 * than 24 bits of actual data we can save across reboots. 50 * 51 * And if your box can't boot in less than three minutes, 52 * you're screwed. 53 * 54 * Now, almost 24 bits of data is pitifully small, so we need 55 * to be pretty dense if we want to use it for anything nice. 56 * What we do is that instead of saving off nice readable info, 57 * we save off _hashes_ of information that we can hopefully 58 * regenerate after the reboot. 59 * 60 * In particular, this means that we might be unlucky, and hit 61 * a case where we have a hash collision, and we end up not 62 * being able to tell for certain exactly which case happened. 63 * But that's hopefully unlikely. 64 * 65 * What we do is to take the bits we can fit, and split them 66 * into three parts (16*997*1009 = 16095568), and use the values 67 * for: 68 * - 0-15: user-settable 69 * - 0-996: file + line number 70 * - 0-1008: device 71 */ 72 #define USERHASH (16) 73 #define FILEHASH (997) 74 #define DEVHASH (1009) 75 76 #define DEVSEED (7919) 77 78 bool pm_trace_rtc_abused __read_mostly; 79 EXPORT_SYMBOL_GPL(pm_trace_rtc_abused); 80 81 static unsigned int dev_hash_value; 82 83 static int set_magic_time(unsigned int user, unsigned int file, unsigned int device) 84 { 85 unsigned int n = user + USERHASH*(file + FILEHASH*device); 86 87 // June 7th, 2006 88 static struct rtc_time time = { 89 .tm_sec = 0, 90 .tm_min = 0, 91 .tm_hour = 0, 92 .tm_mday = 7, 93 .tm_mon = 5, // June - counting from zero 94 .tm_year = 106, 95 .tm_wday = 3, 96 .tm_yday = 160, 97 .tm_isdst = 1 98 }; 99 100 time.tm_year = (n % 100); 101 n /= 100; 102 time.tm_mon = (n % 12); 103 n /= 12; 104 time.tm_mday = (n % 28) + 1; 105 n /= 28; 106 time.tm_hour = (n % 24); 107 n /= 24; 108 time.tm_min = (n % 20) * 3; 109 n /= 20; 110 mc146818_set_time(&time); 111 pm_trace_rtc_abused = true; 112 return n ? -1 : 0; 113 } 114 115 static unsigned int read_magic_time(void) 116 { 117 struct rtc_time time; 118 unsigned int val; 119 120 mc146818_get_time(&time); 121 pr_info("RTC time: %ptRt, date: %ptRd\n", &time, &time); 122 val = time.tm_year; /* 100 years */ 123 if (val > 100) 124 val -= 100; 125 val += time.tm_mon * 100; /* 12 months */ 126 val += (time.tm_mday-1) * 100 * 12; /* 28 month-days */ 127 val += time.tm_hour * 100 * 12 * 28; /* 24 hours */ 128 val += (time.tm_min / 3) * 100 * 12 * 28 * 24; /* 20 3-minute intervals */ 129 return val; 130 } 131 132 /* 133 * This is just the sdbm hash function with a user-supplied 134 * seed and final size parameter. 135 */ 136 static unsigned int hash_string(unsigned int seed, const char *data, unsigned int mod) 137 { 138 unsigned char c; 139 while ((c = *data++) != 0) { 140 seed = (seed << 16) + (seed << 6) - seed + c; 141 } 142 return seed % mod; 143 } 144 145 void set_trace_device(struct device *dev) 146 { 147 dev_hash_value = hash_string(DEVSEED, dev_name(dev), DEVHASH); 148 } 149 EXPORT_SYMBOL(set_trace_device); 150 151 /* 152 * We could just take the "tracedata" index into the .tracedata 153 * section instead. Generating a hash of the data gives us a 154 * chance to work across kernel versions, and perhaps more 155 * importantly it also gives us valid/invalid check (ie we will 156 * likely not give totally bogus reports - if the hash matches, 157 * it's not any guarantee, but it's a high _likelihood_ that 158 * the match is valid). 159 */ 160 void generate_pm_trace(const void *tracedata, unsigned int user) 161 { 162 unsigned short lineno = *(unsigned short *)tracedata; 163 const char *file = *(const char **)(tracedata + 2); 164 unsigned int user_hash_value, file_hash_value; 165 166 user_hash_value = user % USERHASH; 167 file_hash_value = hash_string(lineno, file, FILEHASH); 168 set_magic_time(user_hash_value, file_hash_value, dev_hash_value); 169 } 170 EXPORT_SYMBOL(generate_pm_trace); 171 172 extern char __tracedata_start[], __tracedata_end[]; 173 static int show_file_hash(unsigned int value) 174 { 175 int match; 176 char *tracedata; 177 178 match = 0; 179 for (tracedata = __tracedata_start ; tracedata < __tracedata_end ; 180 tracedata += 2 + sizeof(unsigned long)) { 181 unsigned short lineno = *(unsigned short *)tracedata; 182 const char *file = *(const char **)(tracedata + 2); 183 unsigned int hash = hash_string(lineno, file, FILEHASH); 184 if (hash != value) 185 continue; 186 pr_info(" hash matches %s:%u\n", file, lineno); 187 match++; 188 } 189 return match; 190 } 191 192 static int show_dev_hash(unsigned int value) 193 { 194 int match = 0; 195 struct list_head *entry; 196 197 device_pm_lock(); 198 entry = dpm_list.prev; 199 while (entry != &dpm_list) { 200 struct device * dev = to_device(entry); 201 unsigned int hash = hash_string(DEVSEED, dev_name(dev), DEVHASH); 202 if (hash == value) { 203 dev_info(dev, "hash matches\n"); 204 match++; 205 } 206 entry = entry->prev; 207 } 208 device_pm_unlock(); 209 return match; 210 } 211 212 static unsigned int hash_value_early_read; 213 214 int show_trace_dev_match(char *buf, size_t size) 215 { 216 unsigned int value = hash_value_early_read / (USERHASH * FILEHASH); 217 int ret = 0; 218 struct list_head *entry; 219 220 /* 221 * It's possible that multiple devices will match the hash and we can't 222 * tell which is the culprit, so it's best to output them all. 223 */ 224 device_pm_lock(); 225 entry = dpm_list.prev; 226 while (size && entry != &dpm_list) { 227 struct device *dev = to_device(entry); 228 unsigned int hash = hash_string(DEVSEED, dev_name(dev), 229 DEVHASH); 230 if (hash == value) { 231 int len = snprintf(buf, size, "%s\n", 232 dev_driver_string(dev)); 233 if (len > size) 234 len = size; 235 buf += len; 236 ret += len; 237 size -= len; 238 } 239 entry = entry->prev; 240 } 241 device_pm_unlock(); 242 return ret; 243 } 244 245 static int 246 pm_trace_notify(struct notifier_block *nb, unsigned long mode, void *_unused) 247 { 248 switch (mode) { 249 case PM_POST_HIBERNATION: 250 case PM_POST_SUSPEND: 251 if (pm_trace_rtc_abused) { 252 pm_trace_rtc_abused = false; 253 pr_warn("Possible incorrect RTC due to pm_trace, please use 'ntpdate' or 'rdate' to reset it.\n"); 254 } 255 break; 256 default: 257 break; 258 } 259 return 0; 260 } 261 262 static struct notifier_block pm_trace_nb = { 263 .notifier_call = pm_trace_notify, 264 }; 265 266 static int early_resume_init(void) 267 { 268 hash_value_early_read = read_magic_time(); 269 register_pm_notifier(&pm_trace_nb); 270 return 0; 271 } 272 273 static int late_resume_init(void) 274 { 275 unsigned int val = hash_value_early_read; 276 unsigned int user, file, dev; 277 278 user = val % USERHASH; 279 val = val / USERHASH; 280 file = val % FILEHASH; 281 val = val / FILEHASH; 282 dev = val /* % DEVHASH */; 283 284 pr_info(" Magic number: %d:%d:%d\n", user, file, dev); 285 show_file_hash(file); 286 show_dev_hash(dev); 287 return 0; 288 } 289 290 core_initcall(early_resume_init); 291 late_initcall(late_resume_init); 292