ds.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. /*
  2. * Debug Store support
  3. *
  4. * This provides a low-level interface to the hardware's Debug Store
  5. * feature that is used for branch trace store (BTS) and
  6. * precise-event based sampling (PEBS).
  7. *
  8. * It manages:
  9. * - DS and BTS hardware configuration
  10. * - buffer overflow handling (to be done)
  11. * - buffer access
  12. *
  13. * It does not do:
  14. * - security checking (is the caller allowed to trace the task)
  15. * - buffer allocation (memory accounting)
  16. *
  17. *
  18. * Copyright (C) 2007-2009 Intel Corporation.
  19. * Markus Metzger <markus.t.metzger@intel.com>, 2007-2009
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/string.h>
  23. #include <linux/errno.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/trace_clock.h>
  28. #include <asm/ds.h>
  29. #include "ds_selftest.h"
  30. /*
  31. * The configuration for a particular DS hardware implementation:
  32. */
  33. struct ds_configuration {
  34. /* The name of the configuration: */
  35. const char *name;
  36. /* The size of pointer-typed fields in DS, BTS, and PEBS: */
  37. unsigned char sizeof_ptr_field;
  38. /* The size of a BTS/PEBS record in bytes: */
  39. unsigned char sizeof_rec[2];
  40. /* The number of pebs counter reset values in the DS structure. */
  41. unsigned char nr_counter_reset;
  42. /* Control bit-masks indexed by enum ds_feature: */
  43. unsigned long ctl[dsf_ctl_max];
  44. };
  45. static struct ds_configuration ds_cfg __read_mostly;
  46. /* Maximal size of a DS configuration: */
  47. #define MAX_SIZEOF_DS 0x80
  48. /* Maximal size of a BTS record: */
  49. #define MAX_SIZEOF_BTS (3 * 8)
  50. /* BTS and PEBS buffer alignment: */
  51. #define DS_ALIGNMENT (1 << 3)
  52. /* Number of buffer pointers in DS: */
  53. #define NUM_DS_PTR_FIELDS 8
  54. /* Size of a pebs reset value in DS: */
  55. #define PEBS_RESET_FIELD_SIZE 8
  56. /* Mask of control bits in the DS MSR register: */
  57. #define BTS_CONTROL \
  58. ( ds_cfg.ctl[dsf_bts] | \
  59. ds_cfg.ctl[dsf_bts_kernel] | \
  60. ds_cfg.ctl[dsf_bts_user] | \
  61. ds_cfg.ctl[dsf_bts_overflow] )
  62. /*
  63. * A BTS or PEBS tracer.
  64. *
  65. * This holds the configuration of the tracer and serves as a handle
  66. * to identify tracers.
  67. */
  68. struct ds_tracer {
  69. /* The DS context (partially) owned by this tracer. */
  70. struct ds_context *context;
  71. /* The buffer provided on ds_request() and its size in bytes. */
  72. void *buffer;
  73. size_t size;
  74. };
  75. struct bts_tracer {
  76. /* The common DS part: */
  77. struct ds_tracer ds;
  78. /* The trace including the DS configuration: */
  79. struct bts_trace trace;
  80. /* Buffer overflow notification function: */
  81. bts_ovfl_callback_t ovfl;
  82. /* Active flags affecting trace collection. */
  83. unsigned int flags;
  84. };
  85. struct pebs_tracer {
  86. /* The common DS part: */
  87. struct ds_tracer ds;
  88. /* The trace including the DS configuration: */
  89. struct pebs_trace trace;
  90. /* Buffer overflow notification function: */
  91. pebs_ovfl_callback_t ovfl;
  92. };
  93. /*
  94. * Debug Store (DS) save area configuration (see Intel64 and IA32
  95. * Architectures Software Developer's Manual, section 18.5)
  96. *
  97. * The DS configuration consists of the following fields; different
  98. * architetures vary in the size of those fields.
  99. *
  100. * - double-word aligned base linear address of the BTS buffer
  101. * - write pointer into the BTS buffer
  102. * - end linear address of the BTS buffer (one byte beyond the end of
  103. * the buffer)
  104. * - interrupt pointer into BTS buffer
  105. * (interrupt occurs when write pointer passes interrupt pointer)
  106. * - double-word aligned base linear address of the PEBS buffer
  107. * - write pointer into the PEBS buffer
  108. * - end linear address of the PEBS buffer (one byte beyond the end of
  109. * the buffer)
  110. * - interrupt pointer into PEBS buffer
  111. * (interrupt occurs when write pointer passes interrupt pointer)
  112. * - value to which counter is reset following counter overflow
  113. *
  114. * Later architectures use 64bit pointers throughout, whereas earlier
  115. * architectures use 32bit pointers in 32bit mode.
  116. *
  117. *
  118. * We compute the base address for the first 8 fields based on:
  119. * - the field size stored in the DS configuration
  120. * - the relative field position
  121. * - an offset giving the start of the respective region
  122. *
  123. * This offset is further used to index various arrays holding
  124. * information for BTS and PEBS at the respective index.
  125. *
  126. * On later 32bit processors, we only access the lower 32bit of the
  127. * 64bit pointer fields. The upper halves will be zeroed out.
  128. */
  129. enum ds_field {
  130. ds_buffer_base = 0,
  131. ds_index,
  132. ds_absolute_maximum,
  133. ds_interrupt_threshold,
  134. };
  135. enum ds_qualifier {
  136. ds_bts = 0,
  137. ds_pebs
  138. };
  139. static inline unsigned long
  140. ds_get(const unsigned char *base, enum ds_qualifier qual, enum ds_field field)
  141. {
  142. base += (ds_cfg.sizeof_ptr_field * (field + (4 * qual)));
  143. return *(unsigned long *)base;
  144. }
  145. static inline void
  146. ds_set(unsigned char *base, enum ds_qualifier qual, enum ds_field field,
  147. unsigned long value)
  148. {
  149. base += (ds_cfg.sizeof_ptr_field * (field + (4 * qual)));
  150. (*(unsigned long *)base) = value;
  151. }
  152. /*
  153. * Locking is done only for allocating BTS or PEBS resources.
  154. */
  155. static DEFINE_SPINLOCK(ds_lock);
  156. /*
  157. * We either support (system-wide) per-cpu or per-thread allocation.
  158. * We distinguish the two based on the task_struct pointer, where a
  159. * NULL pointer indicates per-cpu allocation for the current cpu.
  160. *
  161. * Allocations are use-counted. As soon as resources are allocated,
  162. * further allocations must be of the same type (per-cpu or
  163. * per-thread). We model this by counting allocations (i.e. the number
  164. * of tracers of a certain type) for one type negatively:
  165. * =0 no tracers
  166. * >0 number of per-thread tracers
  167. * <0 number of per-cpu tracers
  168. *
  169. * Tracers essentially gives the number of ds contexts for a certain
  170. * type of allocation.
  171. */
  172. static atomic_t tracers = ATOMIC_INIT(0);
  173. static inline int get_tracer(struct task_struct *task)
  174. {
  175. int error;
  176. spin_lock_irq(&ds_lock);
  177. if (task) {
  178. error = -EPERM;
  179. if (atomic_read(&tracers) < 0)
  180. goto out;
  181. atomic_inc(&tracers);
  182. } else {
  183. error = -EPERM;
  184. if (atomic_read(&tracers) > 0)
  185. goto out;
  186. atomic_dec(&tracers);
  187. }
  188. error = 0;
  189. out:
  190. spin_unlock_irq(&ds_lock);
  191. return error;
  192. }
  193. static inline void put_tracer(struct task_struct *task)
  194. {
  195. if (task)
  196. atomic_dec(&tracers);
  197. else
  198. atomic_inc(&tracers);
  199. }
  200. /*
  201. * The DS context is either attached to a thread or to a cpu:
  202. * - in the former case, the thread_struct contains a pointer to the
  203. * attached context.
  204. * - in the latter case, we use a static array of per-cpu context
  205. * pointers.
  206. *
  207. * Contexts are use-counted. They are allocated on first access and
  208. * deallocated when the last user puts the context.
  209. */
  210. struct ds_context {
  211. /* The DS configuration; goes into MSR_IA32_DS_AREA: */
  212. unsigned char ds[MAX_SIZEOF_DS];
  213. /* The owner of the BTS and PEBS configuration, respectively: */
  214. struct bts_tracer *bts_master;
  215. struct pebs_tracer *pebs_master;
  216. /* Use count: */
  217. unsigned long count;
  218. /* Pointer to the context pointer field: */
  219. struct ds_context **this;
  220. /* The traced task; NULL for cpu tracing: */
  221. struct task_struct *task;
  222. /* The traced cpu; only valid if task is NULL: */
  223. int cpu;
  224. };
  225. static DEFINE_PER_CPU(struct ds_context *, cpu_context);
  226. static struct ds_context *ds_get_context(struct task_struct *task, int cpu)
  227. {
  228. struct ds_context **p_context =
  229. (task ? &task->thread.ds_ctx : &per_cpu(cpu_context, cpu));
  230. struct ds_context *context = NULL;
  231. struct ds_context *new_context = NULL;
  232. /* Chances are small that we already have a context. */
  233. new_context = kzalloc(sizeof(*new_context), GFP_KERNEL);
  234. if (!new_context)
  235. return NULL;
  236. spin_lock_irq(&ds_lock);
  237. context = *p_context;
  238. if (likely(!context)) {
  239. context = new_context;
  240. context->this = p_context;
  241. context->task = task;
  242. context->cpu = cpu;
  243. context->count = 0;
  244. *p_context = context;
  245. }
  246. context->count++;
  247. spin_unlock_irq(&ds_lock);
  248. if (context != new_context)
  249. kfree(new_context);
  250. return context;
  251. }
  252. static void ds_put_context(struct ds_context *context)
  253. {
  254. struct task_struct *task;
  255. unsigned long irq;
  256. if (!context)
  257. return;
  258. spin_lock_irqsave(&ds_lock, irq);
  259. if (--context->count) {
  260. spin_unlock_irqrestore(&ds_lock, irq);
  261. return;
  262. }
  263. *(context->this) = NULL;
  264. task = context->task;
  265. if (task)
  266. clear_tsk_thread_flag(task, TIF_DS_AREA_MSR);
  267. /*
  268. * We leave the (now dangling) pointer to the DS configuration in
  269. * the DS_AREA msr. This is as good or as bad as replacing it with
  270. * NULL - the hardware would crash if we enabled tracing.
  271. *
  272. * This saves us some problems with having to write an msr on a
  273. * different cpu while preventing others from doing the same for the
  274. * next context for that same cpu.
  275. */
  276. spin_unlock_irqrestore(&ds_lock, irq);
  277. /* The context might still be in use for context switching. */
  278. if (task && (task != current))
  279. wait_task_context_switch(task);
  280. kfree(context);
  281. }
  282. static void ds_install_ds_area(struct ds_context *context)
  283. {
  284. unsigned long ds;
  285. ds = (unsigned long)context->ds;
  286. /*
  287. * There is a race between the bts master and the pebs master.
  288. *
  289. * The thread/cpu access is synchronized via get/put_cpu() for
  290. * task tracing and via wrmsr_on_cpu for cpu tracing.
  291. *
  292. * If bts and pebs are collected for the same task or same cpu,
  293. * the same confiuration is written twice.
  294. */
  295. if (context->task) {
  296. get_cpu();
  297. if (context->task == current)
  298. wrmsrl(MSR_IA32_DS_AREA, ds);
  299. set_tsk_thread_flag(context->task, TIF_DS_AREA_MSR);
  300. put_cpu();
  301. } else
  302. wrmsr_on_cpu(context->cpu, MSR_IA32_DS_AREA,
  303. (u32)((u64)ds), (u32)((u64)ds >> 32));
  304. }
  305. /*
  306. * Call the tracer's callback on a buffer overflow.
  307. *
  308. * context: the ds context
  309. * qual: the buffer type
  310. */
  311. static void ds_overflow(struct ds_context *context, enum ds_qualifier qual)
  312. {
  313. switch (qual) {
  314. case ds_bts:
  315. if (context->bts_master &&
  316. context->bts_master->ovfl)
  317. context->bts_master->ovfl(context->bts_master);
  318. break;
  319. case ds_pebs:
  320. if (context->pebs_master &&
  321. context->pebs_master->ovfl)
  322. context->pebs_master->ovfl(context->pebs_master);
  323. break;
  324. }
  325. }
  326. /*
  327. * Write raw data into the BTS or PEBS buffer.
  328. *
  329. * The remainder of any partially written record is zeroed out.
  330. *
  331. * context: the DS context
  332. * qual: the buffer type
  333. * record: the data to write
  334. * size: the size of the data
  335. */
  336. static int ds_write(struct ds_context *context, enum ds_qualifier qual,
  337. const void *record, size_t size)
  338. {
  339. int bytes_written = 0;
  340. if (!record)
  341. return -EINVAL;
  342. while (size) {
  343. unsigned long base, index, end, write_end, int_th;
  344. unsigned long write_size, adj_write_size;
  345. /*
  346. * Write as much as possible without producing an
  347. * overflow interrupt.
  348. *
  349. * Interrupt_threshold must either be
  350. * - bigger than absolute_maximum or
  351. * - point to a record between buffer_base and absolute_maximum
  352. *
  353. * Index points to a valid record.
  354. */
  355. base = ds_get(context->ds, qual, ds_buffer_base);
  356. index = ds_get(context->ds, qual, ds_index);
  357. end = ds_get(context->ds, qual, ds_absolute_maximum);
  358. int_th = ds_get(context->ds, qual, ds_interrupt_threshold);
  359. write_end = min(end, int_th);
  360. /*
  361. * If we are already beyond the interrupt threshold,
  362. * we fill the entire buffer.
  363. */
  364. if (write_end <= index)
  365. write_end = end;
  366. if (write_end <= index)
  367. break;
  368. write_size = min((unsigned long) size, write_end - index);
  369. memcpy((void *)index, record, write_size);
  370. record = (const char *)record + write_size;
  371. size -= write_size;
  372. bytes_written += write_size;
  373. adj_write_size = write_size / ds_cfg.sizeof_rec[qual];
  374. adj_write_size *= ds_cfg.sizeof_rec[qual];
  375. /* Zero out trailing bytes. */
  376. memset((char *)index + write_size, 0,
  377. adj_write_size - write_size);
  378. index += adj_write_size;
  379. if (index >= end)
  380. index = base;
  381. ds_set(context->ds, qual, ds_index, index);
  382. if (index >= int_th)
  383. ds_overflow(context, qual);
  384. }
  385. return bytes_written;
  386. }
  387. /*
  388. * Branch Trace Store (BTS) uses the following format. Different
  389. * architectures vary in the size of those fields.
  390. * - source linear address
  391. * - destination linear address
  392. * - flags
  393. *
  394. * Later architectures use 64bit pointers throughout, whereas earlier
  395. * architectures use 32bit pointers in 32bit mode.
  396. *
  397. * We compute the base address for the fields based on:
  398. * - the field size stored in the DS configuration
  399. * - the relative field position
  400. *
  401. * In order to store additional information in the BTS buffer, we use
  402. * a special source address to indicate that the record requires
  403. * special interpretation.
  404. *
  405. * Netburst indicated via a bit in the flags field whether the branch
  406. * was predicted; this is ignored.
  407. *
  408. * We use two levels of abstraction:
  409. * - the raw data level defined here
  410. * - an arch-independent level defined in ds.h
  411. */
  412. enum bts_field {
  413. bts_from,
  414. bts_to,
  415. bts_flags,
  416. bts_qual = bts_from,
  417. bts_clock = bts_to,
  418. bts_pid = bts_flags,
  419. bts_qual_mask = (bts_qual_max - 1),
  420. bts_escape = ((unsigned long)-1 & ~bts_qual_mask)
  421. };
  422. static inline unsigned long bts_get(const char *base, enum bts_field field)
  423. {
  424. base += (ds_cfg.sizeof_ptr_field * field);
  425. return *(unsigned long *)base;
  426. }
  427. static inline void bts_set(char *base, enum bts_field field, unsigned long val)
  428. {
  429. base += (ds_cfg.sizeof_ptr_field * field);;
  430. (*(unsigned long *)base) = val;
  431. }
  432. /*
  433. * The raw BTS data is architecture dependent.
  434. *
  435. * For higher-level users, we give an arch-independent view.
  436. * - ds.h defines struct bts_struct
  437. * - bts_read translates one raw bts record into a bts_struct
  438. * - bts_write translates one bts_struct into the raw format and
  439. * writes it into the top of the parameter tracer's buffer.
  440. *
  441. * return: bytes read/written on success; -Eerrno, otherwise
  442. */
  443. static int
  444. bts_read(struct bts_tracer *tracer, const void *at, struct bts_struct *out)
  445. {
  446. if (!tracer)
  447. return -EINVAL;
  448. if (at < tracer->trace.ds.begin)
  449. return -EINVAL;
  450. if (tracer->trace.ds.end < (at + tracer->trace.ds.size))
  451. return -EINVAL;
  452. memset(out, 0, sizeof(*out));
  453. if ((bts_get(at, bts_qual) & ~bts_qual_mask) == bts_escape) {
  454. out->qualifier = (bts_get(at, bts_qual) & bts_qual_mask);
  455. out->variant.event.clock = bts_get(at, bts_clock);
  456. out->variant.event.pid = bts_get(at, bts_pid);
  457. } else {
  458. out->qualifier = bts_branch;
  459. out->variant.lbr.from = bts_get(at, bts_from);
  460. out->variant.lbr.to = bts_get(at, bts_to);
  461. if (!out->variant.lbr.from && !out->variant.lbr.to)
  462. out->qualifier = bts_invalid;
  463. }
  464. return ds_cfg.sizeof_rec[ds_bts];
  465. }
  466. static int bts_write(struct bts_tracer *tracer, const struct bts_struct *in)
  467. {
  468. unsigned char raw[MAX_SIZEOF_BTS];
  469. if (!tracer)
  470. return -EINVAL;
  471. if (MAX_SIZEOF_BTS < ds_cfg.sizeof_rec[ds_bts])
  472. return -EOVERFLOW;
  473. switch (in->qualifier) {
  474. case bts_invalid:
  475. bts_set(raw, bts_from, 0);
  476. bts_set(raw, bts_to, 0);
  477. bts_set(raw, bts_flags, 0);
  478. break;
  479. case bts_branch:
  480. bts_set(raw, bts_from, in->variant.lbr.from);
  481. bts_set(raw, bts_to, in->variant.lbr.to);
  482. bts_set(raw, bts_flags, 0);
  483. break;
  484. case bts_task_arrives:
  485. case bts_task_departs:
  486. bts_set(raw, bts_qual, (bts_escape | in->qualifier));
  487. bts_set(raw, bts_clock, in->variant.event.clock);
  488. bts_set(raw, bts_pid, in->variant.event.pid);
  489. break;
  490. default:
  491. return -EINVAL;
  492. }
  493. return ds_write(tracer->ds.context, ds_bts, raw,
  494. ds_cfg.sizeof_rec[ds_bts]);
  495. }
  496. static void ds_write_config(struct ds_context *context,
  497. struct ds_trace *cfg, enum ds_qualifier qual)
  498. {
  499. unsigned char *ds = context->ds;
  500. ds_set(ds, qual, ds_buffer_base, (unsigned long)cfg->begin);
  501. ds_set(ds, qual, ds_index, (unsigned long)cfg->top);
  502. ds_set(ds, qual, ds_absolute_maximum, (unsigned long)cfg->end);
  503. ds_set(ds, qual, ds_interrupt_threshold, (unsigned long)cfg->ith);
  504. }
  505. static void ds_read_config(struct ds_context *context,
  506. struct ds_trace *cfg, enum ds_qualifier qual)
  507. {
  508. unsigned char *ds = context->ds;
  509. cfg->begin = (void *)ds_get(ds, qual, ds_buffer_base);
  510. cfg->top = (void *)ds_get(ds, qual, ds_index);
  511. cfg->end = (void *)ds_get(ds, qual, ds_absolute_maximum);
  512. cfg->ith = (void *)ds_get(ds, qual, ds_interrupt_threshold);
  513. }
  514. static void ds_init_ds_trace(struct ds_trace *trace, enum ds_qualifier qual,
  515. void *base, size_t size, size_t ith,
  516. unsigned int flags) {
  517. unsigned long buffer, adj;
  518. /*
  519. * Adjust the buffer address and size to meet alignment
  520. * constraints:
  521. * - buffer is double-word aligned
  522. * - size is multiple of record size
  523. *
  524. * We checked the size at the very beginning; we have enough
  525. * space to do the adjustment.
  526. */
  527. buffer = (unsigned long)base;
  528. adj = ALIGN(buffer, DS_ALIGNMENT) - buffer;
  529. buffer += adj;
  530. size -= adj;
  531. trace->n = size / ds_cfg.sizeof_rec[qual];
  532. trace->size = ds_cfg.sizeof_rec[qual];
  533. size = (trace->n * trace->size);
  534. trace->begin = (void *)buffer;
  535. trace->top = trace->begin;
  536. trace->end = (void *)(buffer + size);
  537. /*
  538. * The value for 'no threshold' is -1, which will set the
  539. * threshold outside of the buffer, just like we want it.
  540. */
  541. ith *= ds_cfg.sizeof_rec[qual];
  542. trace->ith = (void *)(buffer + size - ith);
  543. trace->flags = flags;
  544. }
  545. static int ds_request(struct ds_tracer *tracer, struct ds_trace *trace,
  546. enum ds_qualifier qual, struct task_struct *task,
  547. int cpu, void *base, size_t size, size_t th)
  548. {
  549. struct ds_context *context;
  550. int error;
  551. size_t req_size;
  552. error = -EOPNOTSUPP;
  553. if (!ds_cfg.sizeof_rec[qual])
  554. goto out;
  555. error = -EINVAL;
  556. if (!base)
  557. goto out;
  558. req_size = ds_cfg.sizeof_rec[qual];
  559. /* We might need space for alignment adjustments. */
  560. if (!IS_ALIGNED((unsigned long)base, DS_ALIGNMENT))
  561. req_size += DS_ALIGNMENT;
  562. error = -EINVAL;
  563. if (size < req_size)
  564. goto out;
  565. if (th != (size_t)-1) {
  566. th *= ds_cfg.sizeof_rec[qual];
  567. error = -EINVAL;
  568. if (size <= th)
  569. goto out;
  570. }
  571. tracer->buffer = base;
  572. tracer->size = size;
  573. error = -ENOMEM;
  574. context = ds_get_context(task, cpu);
  575. if (!context)
  576. goto out;
  577. tracer->context = context;
  578. /*
  579. * Defer any tracer-specific initialization work for the context until
  580. * context ownership has been clarified.
  581. */
  582. error = 0;
  583. out:
  584. return error;
  585. }
  586. static struct bts_tracer *ds_request_bts(struct task_struct *task, int cpu,
  587. void *base, size_t size,
  588. bts_ovfl_callback_t ovfl, size_t th,
  589. unsigned int flags)
  590. {
  591. struct bts_tracer *tracer;
  592. int error;
  593. /* Buffer overflow notification is not yet implemented. */
  594. error = -EOPNOTSUPP;
  595. if (ovfl)
  596. goto out;
  597. error = get_tracer(task);
  598. if (error < 0)
  599. goto out;
  600. error = -ENOMEM;
  601. tracer = kzalloc(sizeof(*tracer), GFP_KERNEL);
  602. if (!tracer)
  603. goto out_put_tracer;
  604. tracer->ovfl = ovfl;
  605. /* Do some more error checking and acquire a tracing context. */
  606. error = ds_request(&tracer->ds, &tracer->trace.ds,
  607. ds_bts, task, cpu, base, size, th);
  608. if (error < 0)
  609. goto out_tracer;
  610. /* Claim the bts part of the tracing context we acquired above. */
  611. spin_lock_irq(&ds_lock);
  612. error = -EPERM;
  613. if (tracer->ds.context->bts_master)
  614. goto out_unlock;
  615. tracer->ds.context->bts_master = tracer;
  616. spin_unlock_irq(&ds_lock);
  617. /*
  618. * Now that we own the bts part of the context, let's complete the
  619. * initialization for that part.
  620. */
  621. ds_init_ds_trace(&tracer->trace.ds, ds_bts, base, size, th, flags);
  622. ds_write_config(tracer->ds.context, &tracer->trace.ds, ds_bts);
  623. ds_install_ds_area(tracer->ds.context);
  624. tracer->trace.read = bts_read;
  625. tracer->trace.write = bts_write;
  626. /* Start tracing. */
  627. ds_resume_bts(tracer);
  628. return tracer;
  629. out_unlock:
  630. spin_unlock_irq(&ds_lock);
  631. ds_put_context(tracer->ds.context);
  632. out_tracer:
  633. kfree(tracer);
  634. out_put_tracer:
  635. put_tracer(task);
  636. out:
  637. return ERR_PTR(error);
  638. }
  639. struct bts_tracer *ds_request_bts_task(struct task_struct *task,
  640. void *base, size_t size,
  641. bts_ovfl_callback_t ovfl,
  642. size_t th, unsigned int flags)
  643. {
  644. return ds_request_bts(task, 0, base, size, ovfl, th, flags);
  645. }
  646. struct bts_tracer *ds_request_bts_cpu(int cpu, void *base, size_t size,
  647. bts_ovfl_callback_t ovfl,
  648. size_t th, unsigned int flags)
  649. {
  650. return ds_request_bts(NULL, cpu, base, size, ovfl, th, flags);
  651. }
  652. static struct pebs_tracer *ds_request_pebs(struct task_struct *task, int cpu,
  653. void *base, size_t size,
  654. pebs_ovfl_callback_t ovfl, size_t th,
  655. unsigned int flags)
  656. {
  657. struct pebs_tracer *tracer;
  658. int error;
  659. /* Buffer overflow notification is not yet implemented. */
  660. error = -EOPNOTSUPP;
  661. if (ovfl)
  662. goto out;
  663. error = get_tracer(task);
  664. if (error < 0)
  665. goto out;
  666. error = -ENOMEM;
  667. tracer = kzalloc(sizeof(*tracer), GFP_KERNEL);
  668. if (!tracer)
  669. goto out_put_tracer;
  670. tracer->ovfl = ovfl;
  671. /* Do some more error checking and acquire a tracing context. */
  672. error = ds_request(&tracer->ds, &tracer->trace.ds,
  673. ds_pebs, task, cpu, base, size, th);
  674. if (error < 0)
  675. goto out_tracer;
  676. /* Claim the pebs part of the tracing context we acquired above. */
  677. spin_lock_irq(&ds_lock);
  678. error = -EPERM;
  679. if (tracer->ds.context->pebs_master)
  680. goto out_unlock;
  681. tracer->ds.context->pebs_master = tracer;
  682. spin_unlock_irq(&ds_lock);
  683. /*
  684. * Now that we own the pebs part of the context, let's complete the
  685. * initialization for that part.
  686. */
  687. ds_init_ds_trace(&tracer->trace.ds, ds_pebs, base, size, th, flags);
  688. ds_write_config(tracer->ds.context, &tracer->trace.ds, ds_pebs);
  689. ds_install_ds_area(tracer->ds.context);
  690. /* Start tracing. */
  691. ds_resume_pebs(tracer);
  692. return tracer;
  693. out_unlock:
  694. spin_unlock_irq(&ds_lock);
  695. ds_put_context(tracer->ds.context);
  696. out_tracer:
  697. kfree(tracer);
  698. out_put_tracer:
  699. put_tracer(task);
  700. out:
  701. return ERR_PTR(error);
  702. }
  703. struct pebs_tracer *ds_request_pebs_task(struct task_struct *task,
  704. void *base, size_t size,
  705. pebs_ovfl_callback_t ovfl,
  706. size_t th, unsigned int flags)
  707. {
  708. return ds_request_pebs(task, 0, base, size, ovfl, th, flags);
  709. }
  710. struct pebs_tracer *ds_request_pebs_cpu(int cpu, void *base, size_t size,
  711. pebs_ovfl_callback_t ovfl,
  712. size_t th, unsigned int flags)
  713. {
  714. return ds_request_pebs(NULL, cpu, base, size, ovfl, th, flags);
  715. }
  716. static void ds_free_bts(struct bts_tracer *tracer)
  717. {
  718. struct task_struct *task;
  719. task = tracer->ds.context->task;
  720. WARN_ON_ONCE(tracer->ds.context->bts_master != tracer);
  721. tracer->ds.context->bts_master = NULL;
  722. /* Make sure tracing stopped and the tracer is not in use. */
  723. if (task && (task != current))
  724. wait_task_context_switch(task);
  725. ds_put_context(tracer->ds.context);
  726. put_tracer(task);
  727. kfree(tracer);
  728. }
  729. void ds_release_bts(struct bts_tracer *tracer)
  730. {
  731. might_sleep();
  732. if (!tracer)
  733. return;
  734. ds_suspend_bts(tracer);
  735. ds_free_bts(tracer);
  736. }
  737. int ds_release_bts_noirq(struct bts_tracer *tracer)
  738. {
  739. struct task_struct *task;
  740. unsigned long irq;
  741. int error;
  742. if (!tracer)
  743. return 0;
  744. task = tracer->ds.context->task;
  745. local_irq_save(irq);
  746. error = -EPERM;
  747. if (!task &&
  748. (tracer->ds.context->cpu != smp_processor_id()))
  749. goto out;
  750. error = -EPERM;
  751. if (task && (task != current))
  752. goto out;
  753. ds_suspend_bts_noirq(tracer);
  754. ds_free_bts(tracer);
  755. error = 0;
  756. out:
  757. local_irq_restore(irq);
  758. return error;
  759. }
  760. static void update_task_debugctlmsr(struct task_struct *task,
  761. unsigned long debugctlmsr)
  762. {
  763. task->thread.debugctlmsr = debugctlmsr;
  764. get_cpu();
  765. if (task == current)
  766. update_debugctlmsr(debugctlmsr);
  767. put_cpu();
  768. }
  769. void ds_suspend_bts(struct bts_tracer *tracer)
  770. {
  771. struct task_struct *task;
  772. unsigned long debugctlmsr;
  773. int cpu;
  774. if (!tracer)
  775. return;
  776. tracer->flags = 0;
  777. task = tracer->ds.context->task;
  778. cpu = tracer->ds.context->cpu;
  779. WARN_ON(!task && irqs_disabled());
  780. debugctlmsr = (task ?
  781. task->thread.debugctlmsr :
  782. get_debugctlmsr_on_cpu(cpu));
  783. debugctlmsr &= ~BTS_CONTROL;
  784. if (task)
  785. update_task_debugctlmsr(task, debugctlmsr);
  786. else
  787. update_debugctlmsr_on_cpu(cpu, debugctlmsr);
  788. }
  789. int ds_suspend_bts_noirq(struct bts_tracer *tracer)
  790. {
  791. struct task_struct *task;
  792. unsigned long debugctlmsr, irq;
  793. int cpu, error = 0;
  794. if (!tracer)
  795. return 0;
  796. tracer->flags = 0;
  797. task = tracer->ds.context->task;
  798. cpu = tracer->ds.context->cpu;
  799. local_irq_save(irq);
  800. error = -EPERM;
  801. if (!task && (cpu != smp_processor_id()))
  802. goto out;
  803. debugctlmsr = (task ?
  804. task->thread.debugctlmsr :
  805. get_debugctlmsr());
  806. debugctlmsr &= ~BTS_CONTROL;
  807. if (task)
  808. update_task_debugctlmsr(task, debugctlmsr);
  809. else
  810. update_debugctlmsr(debugctlmsr);
  811. error = 0;
  812. out:
  813. local_irq_restore(irq);
  814. return error;
  815. }
  816. static unsigned long ds_bts_control(struct bts_tracer *tracer)
  817. {
  818. unsigned long control;
  819. control = ds_cfg.ctl[dsf_bts];
  820. if (!(tracer->trace.ds.flags & BTS_KERNEL))
  821. control |= ds_cfg.ctl[dsf_bts_kernel];
  822. if (!(tracer->trace.ds.flags & BTS_USER))
  823. control |= ds_cfg.ctl[dsf_bts_user];
  824. return control;
  825. }
  826. void ds_resume_bts(struct bts_tracer *tracer)
  827. {
  828. struct task_struct *task;
  829. unsigned long debugctlmsr;
  830. int cpu;
  831. if (!tracer)
  832. return;
  833. tracer->flags = tracer->trace.ds.flags;
  834. task = tracer->ds.context->task;
  835. cpu = tracer->ds.context->cpu;
  836. WARN_ON(!task && irqs_disabled());
  837. debugctlmsr = (task ?
  838. task->thread.debugctlmsr :
  839. get_debugctlmsr_on_cpu(cpu));
  840. debugctlmsr |= ds_bts_control(tracer);
  841. if (task)
  842. update_task_debugctlmsr(task, debugctlmsr);
  843. else
  844. update_debugctlmsr_on_cpu(cpu, debugctlmsr);
  845. }
  846. int ds_resume_bts_noirq(struct bts_tracer *tracer)
  847. {
  848. struct task_struct *task;
  849. unsigned long debugctlmsr, irq;
  850. int cpu, error = 0;
  851. if (!tracer)
  852. return 0;
  853. tracer->flags = tracer->trace.ds.flags;
  854. task = tracer->ds.context->task;
  855. cpu = tracer->ds.context->cpu;
  856. local_irq_save(irq);
  857. error = -EPERM;
  858. if (!task && (cpu != smp_processor_id()))
  859. goto out;
  860. debugctlmsr = (task ?
  861. task->thread.debugctlmsr :
  862. get_debugctlmsr());
  863. debugctlmsr |= ds_bts_control(tracer);
  864. if (task)
  865. update_task_debugctlmsr(task, debugctlmsr);
  866. else
  867. update_debugctlmsr(debugctlmsr);
  868. error = 0;
  869. out:
  870. local_irq_restore(irq);
  871. return error;
  872. }
  873. static void ds_free_pebs(struct pebs_tracer *tracer)
  874. {
  875. struct task_struct *task;
  876. task = tracer->ds.context->task;
  877. WARN_ON_ONCE(tracer->ds.context->pebs_master != tracer);
  878. tracer->ds.context->pebs_master = NULL;
  879. ds_put_context(tracer->ds.context);
  880. put_tracer(task);
  881. kfree(tracer);
  882. }
  883. void ds_release_pebs(struct pebs_tracer *tracer)
  884. {
  885. might_sleep();
  886. if (!tracer)
  887. return;
  888. ds_suspend_pebs(tracer);
  889. ds_free_pebs(tracer);
  890. }
  891. int ds_release_pebs_noirq(struct pebs_tracer *tracer)
  892. {
  893. struct task_struct *task;
  894. unsigned long irq;
  895. int error;
  896. if (!tracer)
  897. return 0;
  898. task = tracer->ds.context->task;
  899. local_irq_save(irq);
  900. error = -EPERM;
  901. if (!task &&
  902. (tracer->ds.context->cpu != smp_processor_id()))
  903. goto out;
  904. error = -EPERM;
  905. if (task && (task != current))
  906. goto out;
  907. ds_suspend_pebs_noirq(tracer);
  908. ds_free_pebs(tracer);
  909. error = 0;
  910. out:
  911. local_irq_restore(irq);
  912. return error;
  913. }
  914. void ds_suspend_pebs(struct pebs_tracer *tracer)
  915. {
  916. }
  917. int ds_suspend_pebs_noirq(struct pebs_tracer *tracer)
  918. {
  919. return 0;
  920. }
  921. void ds_resume_pebs(struct pebs_tracer *tracer)
  922. {
  923. }
  924. int ds_resume_pebs_noirq(struct pebs_tracer *tracer)
  925. {
  926. return 0;
  927. }
  928. const struct bts_trace *ds_read_bts(struct bts_tracer *tracer)
  929. {
  930. if (!tracer)
  931. return NULL;
  932. ds_read_config(tracer->ds.context, &tracer->trace.ds, ds_bts);
  933. return &tracer->trace;
  934. }
  935. const struct pebs_trace *ds_read_pebs(struct pebs_tracer *tracer)
  936. {
  937. if (!tracer)
  938. return NULL;
  939. ds_read_config(tracer->ds.context, &tracer->trace.ds, ds_pebs);
  940. tracer->trace.counters = ds_cfg.nr_counter_reset;
  941. memcpy(tracer->trace.counter_reset,
  942. tracer->ds.context->ds +
  943. (NUM_DS_PTR_FIELDS * ds_cfg.sizeof_ptr_field),
  944. ds_cfg.nr_counter_reset * PEBS_RESET_FIELD_SIZE);
  945. return &tracer->trace;
  946. }
  947. int ds_reset_bts(struct bts_tracer *tracer)
  948. {
  949. if (!tracer)
  950. return -EINVAL;
  951. tracer->trace.ds.top = tracer->trace.ds.begin;
  952. ds_set(tracer->ds.context->ds, ds_bts, ds_index,
  953. (unsigned long)tracer->trace.ds.top);
  954. return 0;
  955. }
  956. int ds_reset_pebs(struct pebs_tracer *tracer)
  957. {
  958. if (!tracer)
  959. return -EINVAL;
  960. tracer->trace.ds.top = tracer->trace.ds.begin;
  961. ds_set(tracer->ds.context->ds, ds_pebs, ds_index,
  962. (unsigned long)tracer->trace.ds.top);
  963. return 0;
  964. }
  965. int ds_set_pebs_reset(struct pebs_tracer *tracer,
  966. unsigned int counter, u64 value)
  967. {
  968. if (!tracer)
  969. return -EINVAL;
  970. if (ds_cfg.nr_counter_reset < counter)
  971. return -EINVAL;
  972. *(u64 *)(tracer->ds.context->ds +
  973. (NUM_DS_PTR_FIELDS * ds_cfg.sizeof_ptr_field) +
  974. (counter * PEBS_RESET_FIELD_SIZE)) = value;
  975. return 0;
  976. }
  977. static const struct ds_configuration ds_cfg_netburst = {
  978. .name = "Netburst",
  979. .ctl[dsf_bts] = (1 << 2) | (1 << 3),
  980. .ctl[dsf_bts_kernel] = (1 << 5),
  981. .ctl[dsf_bts_user] = (1 << 6),
  982. .nr_counter_reset = 1,
  983. };
  984. static const struct ds_configuration ds_cfg_pentium_m = {
  985. .name = "Pentium M",
  986. .ctl[dsf_bts] = (1 << 6) | (1 << 7),
  987. .nr_counter_reset = 1,
  988. };
  989. static const struct ds_configuration ds_cfg_core2_atom = {
  990. .name = "Core 2/Atom",
  991. .ctl[dsf_bts] = (1 << 6) | (1 << 7),
  992. .ctl[dsf_bts_kernel] = (1 << 9),
  993. .ctl[dsf_bts_user] = (1 << 10),
  994. .nr_counter_reset = 1,
  995. };
  996. static const struct ds_configuration ds_cfg_core_i7 = {
  997. .name = "Core i7",
  998. .ctl[dsf_bts] = (1 << 6) | (1 << 7),
  999. .ctl[dsf_bts_kernel] = (1 << 9),
  1000. .ctl[dsf_bts_user] = (1 << 10),
  1001. .nr_counter_reset = 4,
  1002. };
  1003. static void
  1004. ds_configure(const struct ds_configuration *cfg,
  1005. struct cpuinfo_x86 *cpu)
  1006. {
  1007. unsigned long nr_pebs_fields = 0;
  1008. printk(KERN_INFO "[ds] using %s configuration\n", cfg->name);
  1009. #ifdef __i386__
  1010. nr_pebs_fields = 10;
  1011. #else
  1012. nr_pebs_fields = 18;
  1013. #endif
  1014. /*
  1015. * Starting with version 2, architectural performance
  1016. * monitoring supports a format specifier.
  1017. */
  1018. if ((cpuid_eax(0xa) & 0xff) > 1) {
  1019. unsigned long perf_capabilities, format;
  1020. rdmsrl(MSR_IA32_PERF_CAPABILITIES, perf_capabilities);
  1021. format = (perf_capabilities >> 8) & 0xf;
  1022. switch (format) {
  1023. case 0:
  1024. nr_pebs_fields = 18;
  1025. break;
  1026. case 1:
  1027. nr_pebs_fields = 22;
  1028. break;
  1029. default:
  1030. printk(KERN_INFO
  1031. "[ds] unknown PEBS format: %lu\n", format);
  1032. nr_pebs_fields = 0;
  1033. break;
  1034. }
  1035. }
  1036. memset(&ds_cfg, 0, sizeof(ds_cfg));
  1037. ds_cfg = *cfg;
  1038. ds_cfg.sizeof_ptr_field =
  1039. (cpu_has(cpu, X86_FEATURE_DTES64) ? 8 : 4);
  1040. ds_cfg.sizeof_rec[ds_bts] = ds_cfg.sizeof_ptr_field * 3;
  1041. ds_cfg.sizeof_rec[ds_pebs] = ds_cfg.sizeof_ptr_field * nr_pebs_fields;
  1042. if (!cpu_has(cpu, X86_FEATURE_BTS)) {
  1043. ds_cfg.sizeof_rec[ds_bts] = 0;
  1044. printk(KERN_INFO "[ds] bts not available\n");
  1045. }
  1046. if (!cpu_has(cpu, X86_FEATURE_PEBS)) {
  1047. ds_cfg.sizeof_rec[ds_pebs] = 0;
  1048. printk(KERN_INFO "[ds] pebs not available\n");
  1049. }
  1050. printk(KERN_INFO "[ds] sizes: address: %u bit, ",
  1051. 8 * ds_cfg.sizeof_ptr_field);
  1052. printk("bts/pebs record: %u/%u bytes\n",
  1053. ds_cfg.sizeof_rec[ds_bts], ds_cfg.sizeof_rec[ds_pebs]);
  1054. WARN_ON_ONCE(MAX_PEBS_COUNTERS < ds_cfg.nr_counter_reset);
  1055. }
  1056. void __cpuinit ds_init_intel(struct cpuinfo_x86 *c)
  1057. {
  1058. /* Only configure the first cpu. Others are identical. */
  1059. if (ds_cfg.name)
  1060. return;
  1061. switch (c->x86) {
  1062. case 0x6:
  1063. switch (c->x86_model) {
  1064. case 0x9:
  1065. case 0xd: /* Pentium M */
  1066. ds_configure(&ds_cfg_pentium_m, c);
  1067. break;
  1068. case 0xf:
  1069. case 0x17: /* Core2 */
  1070. case 0x1c: /* Atom */
  1071. ds_configure(&ds_cfg_core2_atom, c);
  1072. break;
  1073. case 0x1a: /* Core i7 */
  1074. ds_configure(&ds_cfg_core_i7, c);
  1075. break;
  1076. default:
  1077. /* Sorry, don't know about them. */
  1078. break;
  1079. }
  1080. break;
  1081. case 0xf:
  1082. switch (c->x86_model) {
  1083. case 0x0:
  1084. case 0x1:
  1085. case 0x2: /* Netburst */
  1086. ds_configure(&ds_cfg_netburst, c);
  1087. break;
  1088. default:
  1089. /* Sorry, don't know about them. */
  1090. break;
  1091. }
  1092. break;
  1093. default:
  1094. /* Sorry, don't know about them. */
  1095. break;
  1096. }
  1097. }
  1098. static inline void ds_take_timestamp(struct ds_context *context,
  1099. enum bts_qualifier qualifier,
  1100. struct task_struct *task)
  1101. {
  1102. struct bts_tracer *tracer = context->bts_master;
  1103. struct bts_struct ts;
  1104. /* Prevent compilers from reading the tracer pointer twice. */
  1105. barrier();
  1106. if (!tracer || !(tracer->flags & BTS_TIMESTAMPS))
  1107. return;
  1108. memset(&ts, 0, sizeof(ts));
  1109. ts.qualifier = qualifier;
  1110. ts.variant.event.clock = trace_clock_global();
  1111. ts.variant.event.pid = task->pid;
  1112. bts_write(tracer, &ts);
  1113. }
  1114. /*
  1115. * Change the DS configuration from tracing prev to tracing next.
  1116. */
  1117. void ds_switch_to(struct task_struct *prev, struct task_struct *next)
  1118. {
  1119. struct ds_context *prev_ctx = prev->thread.ds_ctx;
  1120. struct ds_context *next_ctx = next->thread.ds_ctx;
  1121. unsigned long debugctlmsr = next->thread.debugctlmsr;
  1122. /* Make sure all data is read before we start. */
  1123. barrier();
  1124. if (prev_ctx) {
  1125. update_debugctlmsr(0);
  1126. ds_take_timestamp(prev_ctx, bts_task_departs, prev);
  1127. }
  1128. if (next_ctx) {
  1129. ds_take_timestamp(next_ctx, bts_task_arrives, next);
  1130. wrmsrl(MSR_IA32_DS_AREA, (unsigned long)next_ctx->ds);
  1131. }
  1132. update_debugctlmsr(debugctlmsr);
  1133. }
  1134. static __init int ds_selftest(void)
  1135. {
  1136. if (ds_cfg.sizeof_rec[ds_bts]) {
  1137. int error;
  1138. error = ds_selftest_bts();
  1139. if (error) {
  1140. WARN(1, "[ds] selftest failed. disabling bts.\n");
  1141. ds_cfg.sizeof_rec[ds_bts] = 0;
  1142. }
  1143. }
  1144. if (ds_cfg.sizeof_rec[ds_pebs]) {
  1145. int error;
  1146. error = ds_selftest_pebs();
  1147. if (error) {
  1148. WARN(1, "[ds] selftest failed. disabling pebs.\n");
  1149. ds_cfg.sizeof_rec[ds_pebs] = 0;
  1150. }
  1151. }
  1152. return 0;
  1153. }
  1154. device_initcall(ds_selftest);