ds_selftest.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Debug Store support - selftest
  3. *
  4. *
  5. * Copyright (C) 2009 Intel Corporation.
  6. * Markus Metzger <markus.t.metzger@intel.com>, 2009
  7. */
  8. #include "ds_selftest.h"
  9. #include <linux/kernel.h>
  10. #include <linux/string.h>
  11. #include <linux/smp.h>
  12. #include <linux/cpu.h>
  13. #include <asm/ds.h>
  14. #define BUFFER_SIZE 521 /* Intentionally chose an odd size. */
  15. #define SMALL_BUFFER_SIZE 24 /* A single bts entry. */
  16. struct ds_selftest_bts_conf {
  17. struct bts_tracer *tracer;
  18. int error;
  19. int (*suspend)(struct bts_tracer *);
  20. int (*resume)(struct bts_tracer *);
  21. };
  22. static int ds_selftest_bts_consistency(const struct bts_trace *trace)
  23. {
  24. int error = 0;
  25. if (!trace) {
  26. printk(KERN_CONT "failed to access trace...");
  27. /* Bail out. Other tests are pointless. */
  28. return -1;
  29. }
  30. if (!trace->read) {
  31. printk(KERN_CONT "bts read not available...");
  32. error = -1;
  33. }
  34. /* Do some sanity checks on the trace configuration. */
  35. if (!trace->ds.n) {
  36. printk(KERN_CONT "empty bts buffer...");
  37. error = -1;
  38. }
  39. if (!trace->ds.size) {
  40. printk(KERN_CONT "bad bts trace setup...");
  41. error = -1;
  42. }
  43. if (trace->ds.end !=
  44. (char *)trace->ds.begin + (trace->ds.n * trace->ds.size)) {
  45. printk(KERN_CONT "bad bts buffer setup...");
  46. error = -1;
  47. }
  48. /*
  49. * We allow top in [begin; end], since its not clear when the
  50. * overflow adjustment happens: after the increment or before the
  51. * write.
  52. */
  53. if ((trace->ds.top < trace->ds.begin) ||
  54. (trace->ds.end < trace->ds.top)) {
  55. printk(KERN_CONT "bts top out of bounds...");
  56. error = -1;
  57. }
  58. return error;
  59. }
  60. static int ds_selftest_bts_read(struct bts_tracer *tracer,
  61. const struct bts_trace *trace,
  62. const void *from, const void *to)
  63. {
  64. const unsigned char *at;
  65. /*
  66. * Check a few things which do not belong to this test.
  67. * They should be covered by other tests.
  68. */
  69. if (!trace)
  70. return -1;
  71. if (!trace->read)
  72. return -1;
  73. if (to < from)
  74. return -1;
  75. if (from < trace->ds.begin)
  76. return -1;
  77. if (trace->ds.end < to)
  78. return -1;
  79. if (!trace->ds.size)
  80. return -1;
  81. /* Now to the test itself. */
  82. for (at = from; (void *)at < to; at += trace->ds.size) {
  83. struct bts_struct bts;
  84. unsigned long index;
  85. int error;
  86. if (((void *)at - trace->ds.begin) % trace->ds.size) {
  87. printk(KERN_CONT
  88. "read from non-integer index...");
  89. return -1;
  90. }
  91. index = ((void *)at - trace->ds.begin) / trace->ds.size;
  92. memset(&bts, 0, sizeof(bts));
  93. error = trace->read(tracer, at, &bts);
  94. if (error < 0) {
  95. printk(KERN_CONT
  96. "error reading bts trace at [%lu] (0x%p)...",
  97. index, at);
  98. return error;
  99. }
  100. switch (bts.qualifier) {
  101. case BTS_BRANCH:
  102. break;
  103. default:
  104. printk(KERN_CONT
  105. "unexpected bts entry %llu at [%lu] (0x%p)...",
  106. bts.qualifier, index, at);
  107. return -1;
  108. }
  109. }
  110. return 0;
  111. }
  112. static void ds_selftest_bts_cpu(void *arg)
  113. {
  114. struct ds_selftest_bts_conf *conf = arg;
  115. const struct bts_trace *trace;
  116. void *top;
  117. if (IS_ERR(conf->tracer)) {
  118. conf->error = PTR_ERR(conf->tracer);
  119. conf->tracer = NULL;
  120. printk(KERN_CONT
  121. "initialization failed (err: %d)...", conf->error);
  122. return;
  123. }
  124. /* We should meanwhile have enough trace. */
  125. conf->error = conf->suspend(conf->tracer);
  126. if (conf->error < 0)
  127. return;
  128. /* Let's see if we can access the trace. */
  129. trace = ds_read_bts(conf->tracer);
  130. conf->error = ds_selftest_bts_consistency(trace);
  131. if (conf->error < 0)
  132. return;
  133. /* If everything went well, we should have a few trace entries. */
  134. if (trace->ds.top == trace->ds.begin) {
  135. /*
  136. * It is possible but highly unlikely that we got a
  137. * buffer overflow and end up at exactly the same
  138. * position we started from.
  139. * Let's issue a warning, but continue.
  140. */
  141. printk(KERN_CONT "no trace/overflow...");
  142. }
  143. /* Let's try to read the trace we collected. */
  144. conf->error =
  145. ds_selftest_bts_read(conf->tracer, trace,
  146. trace->ds.begin, trace->ds.top);
  147. if (conf->error < 0)
  148. return;
  149. /*
  150. * Let's read the trace again.
  151. * Since we suspended tracing, we should get the same result.
  152. */
  153. top = trace->ds.top;
  154. trace = ds_read_bts(conf->tracer);
  155. conf->error = ds_selftest_bts_consistency(trace);
  156. if (conf->error < 0)
  157. return;
  158. if (top != trace->ds.top) {
  159. printk(KERN_CONT "suspend not working...");
  160. conf->error = -1;
  161. return;
  162. }
  163. /* Let's collect some more trace - see if resume is working. */
  164. conf->error = conf->resume(conf->tracer);
  165. if (conf->error < 0)
  166. return;
  167. conf->error = conf->suspend(conf->tracer);
  168. if (conf->error < 0)
  169. return;
  170. trace = ds_read_bts(conf->tracer);
  171. conf->error = ds_selftest_bts_consistency(trace);
  172. if (conf->error < 0)
  173. return;
  174. if (trace->ds.top == top) {
  175. /*
  176. * It is possible but highly unlikely that we got a
  177. * buffer overflow and end up at exactly the same
  178. * position we started from.
  179. * Let's issue a warning and check the full trace.
  180. */
  181. printk(KERN_CONT
  182. "no resume progress/overflow...");
  183. conf->error =
  184. ds_selftest_bts_read(conf->tracer, trace,
  185. trace->ds.begin, trace->ds.end);
  186. } else if (trace->ds.top < top) {
  187. /*
  188. * We had a buffer overflow - the entire buffer should
  189. * contain trace records.
  190. */
  191. conf->error =
  192. ds_selftest_bts_read(conf->tracer, trace,
  193. trace->ds.begin, trace->ds.end);
  194. } else {
  195. /*
  196. * It is quite likely that the buffer did not overflow.
  197. * Let's just check the delta trace.
  198. */
  199. conf->error =
  200. ds_selftest_bts_read(conf->tracer, trace, top,
  201. trace->ds.top);
  202. }
  203. if (conf->error < 0)
  204. return;
  205. conf->error = 0;
  206. }
  207. static int ds_suspend_bts_wrap(struct bts_tracer *tracer)
  208. {
  209. ds_suspend_bts(tracer);
  210. return 0;
  211. }
  212. static int ds_resume_bts_wrap(struct bts_tracer *tracer)
  213. {
  214. ds_resume_bts(tracer);
  215. return 0;
  216. }
  217. static void ds_release_bts_noirq_wrap(void *tracer)
  218. {
  219. (void)ds_release_bts_noirq(tracer);
  220. }
  221. static int ds_selftest_bts_bad_release_noirq(int cpu,
  222. struct bts_tracer *tracer)
  223. {
  224. int error = -EPERM;
  225. /* Try to release the tracer on the wrong cpu. */
  226. get_cpu();
  227. if (cpu != smp_processor_id()) {
  228. error = ds_release_bts_noirq(tracer);
  229. if (error != -EPERM)
  230. printk(KERN_CONT "release on wrong cpu...");
  231. }
  232. put_cpu();
  233. return error ? 0 : -1;
  234. }
  235. static int ds_selftest_bts_bad_request_cpu(int cpu, void *buffer)
  236. {
  237. struct bts_tracer *tracer;
  238. int error;
  239. /* Try to request cpu tracing while task tracing is active. */
  240. tracer = ds_request_bts_cpu(cpu, buffer, BUFFER_SIZE, NULL,
  241. (size_t)-1, BTS_KERNEL);
  242. error = PTR_ERR(tracer);
  243. if (!IS_ERR(tracer)) {
  244. ds_release_bts(tracer);
  245. error = 0;
  246. }
  247. if (error != -EPERM)
  248. printk(KERN_CONT "cpu/task tracing overlap...");
  249. return error ? 0 : -1;
  250. }
  251. static int ds_selftest_bts_bad_request_task(void *buffer)
  252. {
  253. struct bts_tracer *tracer;
  254. int error;
  255. /* Try to request cpu tracing while task tracing is active. */
  256. tracer = ds_request_bts_task(current, buffer, BUFFER_SIZE, NULL,
  257. (size_t)-1, BTS_KERNEL);
  258. error = PTR_ERR(tracer);
  259. if (!IS_ERR(tracer)) {
  260. error = 0;
  261. ds_release_bts(tracer);
  262. }
  263. if (error != -EPERM)
  264. printk(KERN_CONT "task/cpu tracing overlap...");
  265. return error ? 0 : -1;
  266. }
  267. int ds_selftest_bts(void)
  268. {
  269. struct ds_selftest_bts_conf conf;
  270. unsigned char buffer[BUFFER_SIZE], *small_buffer;
  271. unsigned long irq;
  272. int cpu;
  273. printk(KERN_INFO "[ds] bts selftest...");
  274. conf.error = 0;
  275. small_buffer = (unsigned char *)ALIGN((unsigned long)buffer, 8) + 8;
  276. get_online_cpus();
  277. for_each_online_cpu(cpu) {
  278. conf.suspend = ds_suspend_bts_wrap;
  279. conf.resume = ds_resume_bts_wrap;
  280. conf.tracer =
  281. ds_request_bts_cpu(cpu, buffer, BUFFER_SIZE,
  282. NULL, (size_t)-1, BTS_KERNEL);
  283. ds_selftest_bts_cpu(&conf);
  284. if (conf.error >= 0)
  285. conf.error = ds_selftest_bts_bad_request_task(buffer);
  286. ds_release_bts(conf.tracer);
  287. if (conf.error < 0)
  288. goto out;
  289. conf.suspend = ds_suspend_bts_noirq;
  290. conf.resume = ds_resume_bts_noirq;
  291. conf.tracer =
  292. ds_request_bts_cpu(cpu, buffer, BUFFER_SIZE,
  293. NULL, (size_t)-1, BTS_KERNEL);
  294. smp_call_function_single(cpu, ds_selftest_bts_cpu, &conf, 1);
  295. if (conf.error >= 0) {
  296. conf.error =
  297. ds_selftest_bts_bad_release_noirq(cpu,
  298. conf.tracer);
  299. /* We must not release the tracer twice. */
  300. if (conf.error < 0)
  301. conf.tracer = NULL;
  302. }
  303. if (conf.error >= 0)
  304. conf.error = ds_selftest_bts_bad_request_task(buffer);
  305. smp_call_function_single(cpu, ds_release_bts_noirq_wrap,
  306. conf.tracer, 1);
  307. if (conf.error < 0)
  308. goto out;
  309. }
  310. conf.suspend = ds_suspend_bts_wrap;
  311. conf.resume = ds_resume_bts_wrap;
  312. conf.tracer =
  313. ds_request_bts_task(current, buffer, BUFFER_SIZE,
  314. NULL, (size_t)-1, BTS_KERNEL);
  315. ds_selftest_bts_cpu(&conf);
  316. if (conf.error >= 0)
  317. conf.error = ds_selftest_bts_bad_request_cpu(0, buffer);
  318. ds_release_bts(conf.tracer);
  319. if (conf.error < 0)
  320. goto out;
  321. conf.suspend = ds_suspend_bts_noirq;
  322. conf.resume = ds_resume_bts_noirq;
  323. conf.tracer =
  324. ds_request_bts_task(current, small_buffer, SMALL_BUFFER_SIZE,
  325. NULL, (size_t)-1, BTS_KERNEL);
  326. local_irq_save(irq);
  327. ds_selftest_bts_cpu(&conf);
  328. if (conf.error >= 0)
  329. conf.error = ds_selftest_bts_bad_request_cpu(0, buffer);
  330. ds_release_bts_noirq(conf.tracer);
  331. local_irq_restore(irq);
  332. if (conf.error < 0)
  333. goto out;
  334. conf.error = 0;
  335. out:
  336. put_online_cpus();
  337. printk(KERN_CONT "%s.\n", (conf.error ? "failed" : "passed"));
  338. return conf.error;
  339. }
  340. int ds_selftest_pebs(void)
  341. {
  342. return 0;
  343. }