trace.c 7.0 KB

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