ds.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. * - per-thread and per-cpu allocation of BTS and PEBS
  10. * - buffer memory allocation (optional)
  11. * - buffer overflow handling
  12. * - buffer access
  13. *
  14. * It assumes:
  15. * - get_task_struct on all parameter tasks
  16. * - current is allowed to trace parameter tasks
  17. *
  18. *
  19. * Copyright (C) 2007-2008 Intel Corporation.
  20. * Markus Metzger <markus.t.metzger@intel.com>, 2007-2008
  21. */
  22. #include <asm/ds.h>
  23. #include <linux/errno.h>
  24. #include <linux/string.h>
  25. #include <linux/slab.h>
  26. #include <linux/sched.h>
  27. #include <linux/mm.h>
  28. /*
  29. * The configuration for a particular DS hardware implementation.
  30. */
  31. struct ds_configuration {
  32. /* the size of the DS structure in bytes */
  33. unsigned char sizeof_ds;
  34. /* the size of one pointer-typed field in the DS structure in bytes;
  35. this covers the first 8 fields related to buffer management. */
  36. unsigned char sizeof_field;
  37. /* the size of a BTS/PEBS record in bytes */
  38. unsigned char sizeof_rec[2];
  39. };
  40. static struct ds_configuration ds_cfg;
  41. /*
  42. * Debug Store (DS) save area configuration (see Intel64 and IA32
  43. * Architectures Software Developer's Manual, section 18.5)
  44. *
  45. * The DS configuration consists of the following fields; different
  46. * architetures vary in the size of those fields.
  47. * - double-word aligned base linear address of the BTS buffer
  48. * - write pointer into the BTS buffer
  49. * - end linear address of the BTS buffer (one byte beyond the end of
  50. * the buffer)
  51. * - interrupt pointer into BTS buffer
  52. * (interrupt occurs when write pointer passes interrupt pointer)
  53. * - double-word aligned base linear address of the PEBS buffer
  54. * - write pointer into the PEBS buffer
  55. * - end linear address of the PEBS buffer (one byte beyond the end of
  56. * the buffer)
  57. * - interrupt pointer into PEBS buffer
  58. * (interrupt occurs when write pointer passes interrupt pointer)
  59. * - value to which counter is reset following counter overflow
  60. *
  61. * Later architectures use 64bit pointers throughout, whereas earlier
  62. * architectures use 32bit pointers in 32bit mode.
  63. *
  64. *
  65. * We compute the base address for the first 8 fields based on:
  66. * - the field size stored in the DS configuration
  67. * - the relative field position
  68. * - an offset giving the start of the respective region
  69. *
  70. * This offset is further used to index various arrays holding
  71. * information for BTS and PEBS at the respective index.
  72. *
  73. * On later 32bit processors, we only access the lower 32bit of the
  74. * 64bit pointer fields. The upper halves will be zeroed out.
  75. */
  76. enum ds_field {
  77. ds_buffer_base = 0,
  78. ds_index,
  79. ds_absolute_maximum,
  80. ds_interrupt_threshold,
  81. };
  82. enum ds_qualifier {
  83. ds_bts = 0,
  84. ds_pebs
  85. };
  86. static inline unsigned long ds_get(const unsigned char *base,
  87. enum ds_qualifier qual, enum ds_field field)
  88. {
  89. base += (ds_cfg.sizeof_field * (field + (4 * qual)));
  90. return *(unsigned long *)base;
  91. }
  92. static inline void ds_set(unsigned char *base, enum ds_qualifier qual,
  93. enum ds_field field, unsigned long value)
  94. {
  95. base += (ds_cfg.sizeof_field * (field + (4 * qual)));
  96. (*(unsigned long *)base) = value;
  97. }
  98. /*
  99. * Locking is done only for allocating BTS or PEBS resources and for
  100. * guarding context and buffer memory allocation.
  101. *
  102. * Most functions require the current task to own the ds context part
  103. * they are going to access. All the locking is done when validating
  104. * access to the context.
  105. */
  106. static spinlock_t ds_lock = __SPIN_LOCK_UNLOCKED(ds_lock);
  107. /*
  108. * Validate that the current task is allowed to access the BTS/PEBS
  109. * buffer of the parameter task.
  110. *
  111. * Returns 0, if access is granted; -Eerrno, otherwise.
  112. */
  113. static inline int ds_validate_access(struct ds_context *context,
  114. enum ds_qualifier qual)
  115. {
  116. if (!context)
  117. return -EPERM;
  118. if (context->owner[qual] == current)
  119. return 0;
  120. return -EPERM;
  121. }
  122. /*
  123. * We either support (system-wide) per-cpu or per-thread allocation.
  124. * We distinguish the two based on the task_struct pointer, where a
  125. * NULL pointer indicates per-cpu allocation for the current cpu.
  126. *
  127. * Allocations are use-counted. As soon as resources are allocated,
  128. * further allocations must be of the same type (per-cpu or
  129. * per-thread). We model this by counting allocations (i.e. the number
  130. * of tracers of a certain type) for one type negatively:
  131. * =0 no tracers
  132. * >0 number of per-thread tracers
  133. * <0 number of per-cpu tracers
  134. *
  135. * The below functions to get and put tracers and to check the
  136. * allocation type require the ds_lock to be held by the caller.
  137. *
  138. * Tracers essentially gives the number of ds contexts for a certain
  139. * type of allocation.
  140. */
  141. static long tracers;
  142. static inline void get_tracer(struct task_struct *task)
  143. {
  144. tracers += (task ? 1 : -1);
  145. }
  146. static inline void put_tracer(struct task_struct *task)
  147. {
  148. tracers -= (task ? 1 : -1);
  149. }
  150. static inline int check_tracer(struct task_struct *task)
  151. {
  152. return (task ? (tracers >= 0) : (tracers <= 0));
  153. }
  154. /*
  155. * The DS context is either attached to a thread or to a cpu:
  156. * - in the former case, the thread_struct contains a pointer to the
  157. * attached context.
  158. * - in the latter case, we use a static array of per-cpu context
  159. * pointers.
  160. *
  161. * Contexts are use-counted. They are allocated on first access and
  162. * deallocated when the last user puts the context.
  163. *
  164. * We distinguish between an allocating and a non-allocating get of a
  165. * context:
  166. * - the allocating get is used for requesting BTS/PEBS resources. It
  167. * requires the caller to hold the global ds_lock.
  168. * - the non-allocating get is used for all other cases. A
  169. * non-existing context indicates an error. It acquires and releases
  170. * the ds_lock itself for obtaining the context.
  171. *
  172. * A context and its DS configuration are allocated and deallocated
  173. * together. A context always has a DS configuration of the
  174. * appropriate size.
  175. */
  176. static DEFINE_PER_CPU(struct ds_context *, system_context);
  177. #define this_system_context per_cpu(system_context, smp_processor_id())
  178. /*
  179. * Returns the pointer to the parameter task's context or to the
  180. * system-wide context, if task is NULL.
  181. *
  182. * Increases the use count of the returned context, if not NULL.
  183. */
  184. static inline struct ds_context *ds_get_context(struct task_struct *task)
  185. {
  186. struct ds_context *context;
  187. unsigned long irq;
  188. spin_lock_irqsave(&ds_lock, irq);
  189. context = (task ? task->thread.ds_ctx : this_system_context);
  190. if (context)
  191. context->count++;
  192. spin_unlock_irqrestore(&ds_lock, irq);
  193. return context;
  194. }
  195. /*
  196. * Same as ds_get_context, but allocates the context and it's DS
  197. * structure, if necessary; returns NULL; if out of memory.
  198. */
  199. static inline struct ds_context *ds_alloc_context(struct task_struct *task)
  200. {
  201. struct ds_context **p_context =
  202. (task ? &task->thread.ds_ctx : &this_system_context);
  203. struct ds_context *context = *p_context;
  204. unsigned long irq;
  205. if (!context) {
  206. context = kzalloc(sizeof(*context), GFP_KERNEL);
  207. if (!context)
  208. return NULL;
  209. context->ds = kzalloc(ds_cfg.sizeof_ds, GFP_KERNEL);
  210. if (!context->ds) {
  211. kfree(context);
  212. return NULL;
  213. }
  214. spin_lock_irqsave(&ds_lock, irq);
  215. if (*p_context) {
  216. kfree(context->ds);
  217. kfree(context);
  218. context = *p_context;
  219. } else {
  220. *p_context = context;
  221. context->this = p_context;
  222. context->task = task;
  223. if (task)
  224. set_tsk_thread_flag(task, TIF_DS_AREA_MSR);
  225. if (!task || (task == current))
  226. wrmsrl(MSR_IA32_DS_AREA,
  227. (unsigned long)context->ds);
  228. }
  229. spin_unlock_irqrestore(&ds_lock, irq);
  230. }
  231. context->count++;
  232. return context;
  233. }
  234. /*
  235. * Decreases the use count of the parameter context, if not NULL.
  236. * Deallocates the context, if the use count reaches zero.
  237. */
  238. static inline void ds_put_context(struct ds_context *context)
  239. {
  240. unsigned long irq;
  241. if (!context)
  242. return;
  243. spin_lock_irqsave(&ds_lock, irq);
  244. if (--context->count)
  245. goto out;
  246. *(context->this) = NULL;
  247. if (context->task)
  248. clear_tsk_thread_flag(context->task, TIF_DS_AREA_MSR);
  249. if (!context->task || (context->task == current))
  250. wrmsrl(MSR_IA32_DS_AREA, 0);
  251. put_tracer(context->task);
  252. /* free any leftover buffers from tracers that did not
  253. * deallocate them properly. */
  254. kfree(context->buffer[ds_bts]);
  255. kfree(context->buffer[ds_pebs]);
  256. kfree(context->ds);
  257. kfree(context);
  258. out:
  259. spin_unlock_irqrestore(&ds_lock, irq);
  260. }
  261. /*
  262. * Handle a buffer overflow
  263. *
  264. * task: the task whose buffers are overflowing;
  265. * NULL for a buffer overflow on the current cpu
  266. * context: the ds context
  267. * qual: the buffer type
  268. */
  269. static void ds_overflow(struct task_struct *task, struct ds_context *context,
  270. enum ds_qualifier qual)
  271. {
  272. if (!context)
  273. return;
  274. if (context->callback[qual])
  275. (*context->callback[qual])(task);
  276. /* todo: do some more overflow handling */
  277. }
  278. /*
  279. * Allocate a non-pageable buffer of the parameter size.
  280. * Checks the memory and the locked memory rlimit.
  281. *
  282. * Returns the buffer, if successful;
  283. * NULL, if out of memory or rlimit exceeded.
  284. *
  285. * size: the requested buffer size in bytes
  286. * pages (out): if not NULL, contains the number of pages reserved
  287. */
  288. static inline void *ds_allocate_buffer(size_t size, unsigned int *pages)
  289. {
  290. unsigned long rlim, vm, pgsz;
  291. void *buffer;
  292. pgsz = PAGE_ALIGN(size) >> PAGE_SHIFT;
  293. rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
  294. vm = current->mm->total_vm + pgsz;
  295. if (rlim < vm)
  296. return NULL;
  297. rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
  298. vm = current->mm->locked_vm + pgsz;
  299. if (rlim < vm)
  300. return NULL;
  301. buffer = kzalloc(size, GFP_KERNEL);
  302. if (!buffer)
  303. return NULL;
  304. current->mm->total_vm += pgsz;
  305. current->mm->locked_vm += pgsz;
  306. if (pages)
  307. *pages = pgsz;
  308. return buffer;
  309. }
  310. static int ds_request(struct task_struct *task, void *base, size_t size,
  311. ds_ovfl_callback_t ovfl, enum ds_qualifier qual)
  312. {
  313. struct ds_context *context;
  314. unsigned long buffer, adj;
  315. const unsigned long alignment = (1 << 3);
  316. unsigned long irq;
  317. int error = 0;
  318. if (!ds_cfg.sizeof_ds)
  319. return -EOPNOTSUPP;
  320. /* we require some space to do alignment adjustments below */
  321. if (size < (alignment + ds_cfg.sizeof_rec[qual]))
  322. return -EINVAL;
  323. /* buffer overflow notification is not yet implemented */
  324. if (ovfl)
  325. return -EOPNOTSUPP;
  326. context = ds_alloc_context(task);
  327. if (!context)
  328. return -ENOMEM;
  329. spin_lock_irqsave(&ds_lock, irq);
  330. error = -EPERM;
  331. if (!check_tracer(task))
  332. goto out_unlock;
  333. get_tracer(task);
  334. error = -EALREADY;
  335. if (context->owner[qual] == current)
  336. goto out_put_tracer;
  337. error = -EPERM;
  338. if (context->owner[qual] != NULL)
  339. goto out_put_tracer;
  340. context->owner[qual] = current;
  341. spin_unlock_irqrestore(&ds_lock, irq);
  342. error = -ENOMEM;
  343. if (!base) {
  344. base = ds_allocate_buffer(size, &context->pages[qual]);
  345. if (!base)
  346. goto out_release;
  347. context->buffer[qual] = base;
  348. }
  349. error = 0;
  350. context->callback[qual] = ovfl;
  351. /* adjust the buffer address and size to meet alignment
  352. * constraints:
  353. * - buffer is double-word aligned
  354. * - size is multiple of record size
  355. *
  356. * We checked the size at the very beginning; we have enough
  357. * space to do the adjustment.
  358. */
  359. buffer = (unsigned long)base;
  360. adj = ALIGN(buffer, alignment) - buffer;
  361. buffer += adj;
  362. size -= adj;
  363. size /= ds_cfg.sizeof_rec[qual];
  364. size *= ds_cfg.sizeof_rec[qual];
  365. ds_set(context->ds, qual, ds_buffer_base, buffer);
  366. ds_set(context->ds, qual, ds_index, buffer);
  367. ds_set(context->ds, qual, ds_absolute_maximum, buffer + size);
  368. if (ovfl) {
  369. /* todo: select a suitable interrupt threshold */
  370. } else
  371. ds_set(context->ds, qual,
  372. ds_interrupt_threshold, buffer + size + 1);
  373. /* we keep the context until ds_release */
  374. return error;
  375. out_release:
  376. context->owner[qual] = NULL;
  377. ds_put_context(context);
  378. put_tracer(task);
  379. return error;
  380. out_put_tracer:
  381. spin_unlock_irqrestore(&ds_lock, irq);
  382. ds_put_context(context);
  383. put_tracer(task);
  384. return error;
  385. out_unlock:
  386. spin_unlock_irqrestore(&ds_lock, irq);
  387. ds_put_context(context);
  388. return error;
  389. }
  390. int ds_request_bts(struct task_struct *task, void *base, size_t size,
  391. ds_ovfl_callback_t ovfl)
  392. {
  393. return ds_request(task, base, size, ovfl, ds_bts);
  394. }
  395. int ds_request_pebs(struct task_struct *task, void *base, size_t size,
  396. ds_ovfl_callback_t ovfl)
  397. {
  398. return ds_request(task, base, size, ovfl, ds_pebs);
  399. }
  400. static int ds_release(struct task_struct *task, enum ds_qualifier qual)
  401. {
  402. struct ds_context *context;
  403. int error;
  404. context = ds_get_context(task);
  405. error = ds_validate_access(context, qual);
  406. if (error < 0)
  407. goto out;
  408. kfree(context->buffer[qual]);
  409. context->buffer[qual] = NULL;
  410. current->mm->total_vm -= context->pages[qual];
  411. current->mm->locked_vm -= context->pages[qual];
  412. context->pages[qual] = 0;
  413. context->owner[qual] = NULL;
  414. /*
  415. * we put the context twice:
  416. * once for the ds_get_context
  417. * once for the corresponding ds_request
  418. */
  419. ds_put_context(context);
  420. out:
  421. ds_put_context(context);
  422. return error;
  423. }
  424. int ds_release_bts(struct task_struct *task)
  425. {
  426. return ds_release(task, ds_bts);
  427. }
  428. int ds_release_pebs(struct task_struct *task)
  429. {
  430. return ds_release(task, ds_pebs);
  431. }
  432. static int ds_get_index(struct task_struct *task, size_t *pos,
  433. enum ds_qualifier qual)
  434. {
  435. struct ds_context *context;
  436. unsigned long base, index;
  437. int error;
  438. context = ds_get_context(task);
  439. error = ds_validate_access(context, qual);
  440. if (error < 0)
  441. goto out;
  442. base = ds_get(context->ds, qual, ds_buffer_base);
  443. index = ds_get(context->ds, qual, ds_index);
  444. error = ((index - base) / ds_cfg.sizeof_rec[qual]);
  445. if (pos)
  446. *pos = error;
  447. out:
  448. ds_put_context(context);
  449. return error;
  450. }
  451. int ds_get_bts_index(struct task_struct *task, size_t *pos)
  452. {
  453. return ds_get_index(task, pos, ds_bts);
  454. }
  455. int ds_get_pebs_index(struct task_struct *task, size_t *pos)
  456. {
  457. return ds_get_index(task, pos, ds_pebs);
  458. }
  459. static int ds_get_end(struct task_struct *task, size_t *pos,
  460. enum ds_qualifier qual)
  461. {
  462. struct ds_context *context;
  463. unsigned long base, end;
  464. int error;
  465. context = ds_get_context(task);
  466. error = ds_validate_access(context, qual);
  467. if (error < 0)
  468. goto out;
  469. base = ds_get(context->ds, qual, ds_buffer_base);
  470. end = ds_get(context->ds, qual, ds_absolute_maximum);
  471. error = ((end - base) / ds_cfg.sizeof_rec[qual]);
  472. if (pos)
  473. *pos = error;
  474. out:
  475. ds_put_context(context);
  476. return error;
  477. }
  478. int ds_get_bts_end(struct task_struct *task, size_t *pos)
  479. {
  480. return ds_get_end(task, pos, ds_bts);
  481. }
  482. int ds_get_pebs_end(struct task_struct *task, size_t *pos)
  483. {
  484. return ds_get_end(task, pos, ds_pebs);
  485. }
  486. static int ds_access(struct task_struct *task, size_t index,
  487. const void **record, enum ds_qualifier qual)
  488. {
  489. struct ds_context *context;
  490. unsigned long base, idx;
  491. int error;
  492. if (!record)
  493. return -EINVAL;
  494. context = ds_get_context(task);
  495. error = ds_validate_access(context, qual);
  496. if (error < 0)
  497. goto out;
  498. base = ds_get(context->ds, qual, ds_buffer_base);
  499. idx = base + (index * ds_cfg.sizeof_rec[qual]);
  500. error = -EINVAL;
  501. if (idx > ds_get(context->ds, qual, ds_absolute_maximum))
  502. goto out;
  503. *record = (const void *)idx;
  504. error = ds_cfg.sizeof_rec[qual];
  505. out:
  506. ds_put_context(context);
  507. return error;
  508. }
  509. int ds_access_bts(struct task_struct *task, size_t index, const void **record)
  510. {
  511. return ds_access(task, index, record, ds_bts);
  512. }
  513. int ds_access_pebs(struct task_struct *task, size_t index, const void **record)
  514. {
  515. return ds_access(task, index, record, ds_pebs);
  516. }
  517. static int ds_write(struct task_struct *task, const void *record, size_t size,
  518. enum ds_qualifier qual, int force)
  519. {
  520. struct ds_context *context;
  521. int error;
  522. if (!record)
  523. return -EINVAL;
  524. error = -EPERM;
  525. context = ds_get_context(task);
  526. if (!context)
  527. goto out;
  528. if (!force) {
  529. error = ds_validate_access(context, qual);
  530. if (error < 0)
  531. goto out;
  532. }
  533. error = 0;
  534. while (size) {
  535. unsigned long base, index, end, write_end, int_th;
  536. unsigned long write_size, adj_write_size;
  537. /*
  538. * write as much as possible without producing an
  539. * overflow interrupt.
  540. *
  541. * interrupt_threshold must either be
  542. * - bigger than absolute_maximum or
  543. * - point to a record between buffer_base and absolute_maximum
  544. *
  545. * index points to a valid record.
  546. */
  547. base = ds_get(context->ds, qual, ds_buffer_base);
  548. index = ds_get(context->ds, qual, ds_index);
  549. end = ds_get(context->ds, qual, ds_absolute_maximum);
  550. int_th = ds_get(context->ds, qual, ds_interrupt_threshold);
  551. write_end = min(end, int_th);
  552. /* if we are already beyond the interrupt threshold,
  553. * we fill the entire buffer */
  554. if (write_end <= index)
  555. write_end = end;
  556. if (write_end <= index)
  557. goto out;
  558. write_size = min((unsigned long) size, write_end - index);
  559. memcpy((void *)index, record, write_size);
  560. record = (const char *)record + write_size;
  561. size -= write_size;
  562. error += write_size;
  563. adj_write_size = write_size / ds_cfg.sizeof_rec[qual];
  564. adj_write_size *= ds_cfg.sizeof_rec[qual];
  565. /* zero out trailing bytes */
  566. memset((char *)index + write_size, 0,
  567. adj_write_size - write_size);
  568. index += adj_write_size;
  569. if (index >= end)
  570. index = base;
  571. ds_set(context->ds, qual, ds_index, index);
  572. if (index >= int_th)
  573. ds_overflow(task, context, qual);
  574. }
  575. out:
  576. ds_put_context(context);
  577. return error;
  578. }
  579. int ds_write_bts(struct task_struct *task, const void *record, size_t size)
  580. {
  581. return ds_write(task, record, size, ds_bts, /* force = */ 0);
  582. }
  583. int ds_write_pebs(struct task_struct *task, const void *record, size_t size)
  584. {
  585. return ds_write(task, record, size, ds_pebs, /* force = */ 0);
  586. }
  587. int ds_unchecked_write_bts(struct task_struct *task,
  588. const void *record, size_t size)
  589. {
  590. return ds_write(task, record, size, ds_bts, /* force = */ 1);
  591. }
  592. int ds_unchecked_write_pebs(struct task_struct *task,
  593. const void *record, size_t size)
  594. {
  595. return ds_write(task, record, size, ds_pebs, /* force = */ 1);
  596. }
  597. static int ds_reset_or_clear(struct task_struct *task,
  598. enum ds_qualifier qual, int clear)
  599. {
  600. struct ds_context *context;
  601. unsigned long base, end;
  602. int error;
  603. context = ds_get_context(task);
  604. error = ds_validate_access(context, qual);
  605. if (error < 0)
  606. goto out;
  607. base = ds_get(context->ds, qual, ds_buffer_base);
  608. end = ds_get(context->ds, qual, ds_absolute_maximum);
  609. if (clear)
  610. memset((void *)base, 0, end - base);
  611. ds_set(context->ds, qual, ds_index, base);
  612. error = 0;
  613. out:
  614. ds_put_context(context);
  615. return error;
  616. }
  617. int ds_reset_bts(struct task_struct *task)
  618. {
  619. return ds_reset_or_clear(task, ds_bts, /* clear = */ 0);
  620. }
  621. int ds_reset_pebs(struct task_struct *task)
  622. {
  623. return ds_reset_or_clear(task, ds_pebs, /* clear = */ 0);
  624. }
  625. int ds_clear_bts(struct task_struct *task)
  626. {
  627. return ds_reset_or_clear(task, ds_bts, /* clear = */ 1);
  628. }
  629. int ds_clear_pebs(struct task_struct *task)
  630. {
  631. return ds_reset_or_clear(task, ds_pebs, /* clear = */ 1);
  632. }
  633. int ds_get_pebs_reset(struct task_struct *task, u64 *value)
  634. {
  635. struct ds_context *context;
  636. int error;
  637. if (!value)
  638. return -EINVAL;
  639. context = ds_get_context(task);
  640. error = ds_validate_access(context, ds_pebs);
  641. if (error < 0)
  642. goto out;
  643. *value = *(u64 *)(context->ds + (ds_cfg.sizeof_field * 8));
  644. error = 0;
  645. out:
  646. ds_put_context(context);
  647. return error;
  648. }
  649. int ds_set_pebs_reset(struct task_struct *task, u64 value)
  650. {
  651. struct ds_context *context;
  652. int error;
  653. context = ds_get_context(task);
  654. error = ds_validate_access(context, ds_pebs);
  655. if (error < 0)
  656. goto out;
  657. *(u64 *)(context->ds + (ds_cfg.sizeof_field * 8)) = value;
  658. error = 0;
  659. out:
  660. ds_put_context(context);
  661. return error;
  662. }
  663. static const struct ds_configuration ds_cfg_var = {
  664. .sizeof_ds = sizeof(long) * 12,
  665. .sizeof_field = sizeof(long),
  666. .sizeof_rec[ds_bts] = sizeof(long) * 3,
  667. #ifdef __i386__
  668. .sizeof_rec[ds_pebs] = sizeof(long) * 10
  669. #else
  670. .sizeof_rec[ds_pebs] = sizeof(long) * 18
  671. #endif
  672. };
  673. static const struct ds_configuration ds_cfg_64 = {
  674. .sizeof_ds = 8 * 12,
  675. .sizeof_field = 8,
  676. .sizeof_rec[ds_bts] = 8 * 3,
  677. #ifdef __i386__
  678. .sizeof_rec[ds_pebs] = 8 * 10
  679. #else
  680. .sizeof_rec[ds_pebs] = 8 * 18
  681. #endif
  682. };
  683. static inline void
  684. ds_configure(const struct ds_configuration *cfg)
  685. {
  686. ds_cfg = *cfg;
  687. }
  688. void __cpuinit ds_init_intel(struct cpuinfo_x86 *c)
  689. {
  690. switch (c->x86) {
  691. case 0x6:
  692. switch (c->x86_model) {
  693. case 0xD:
  694. case 0xE: /* Pentium M */
  695. ds_configure(&ds_cfg_var);
  696. break;
  697. case 0xF: /* Core2 */
  698. case 0x1C: /* Atom */
  699. ds_configure(&ds_cfg_64);
  700. break;
  701. default:
  702. /* sorry, don't know about them */
  703. break;
  704. }
  705. break;
  706. case 0xF:
  707. switch (c->x86_model) {
  708. case 0x0:
  709. case 0x1:
  710. case 0x2: /* Netburst */
  711. ds_configure(&ds_cfg_var);
  712. break;
  713. default:
  714. /* sorry, don't know about them */
  715. break;
  716. }
  717. break;
  718. default:
  719. /* sorry, don't know about them */
  720. break;
  721. }
  722. }
  723. void ds_free(struct ds_context *context)
  724. {
  725. /* This is called when the task owning the parameter context
  726. * is dying. There should not be any user of that context left
  727. * to disturb us, anymore. */
  728. unsigned long leftovers = context->count;
  729. while (leftovers--)
  730. ds_put_context(context);
  731. }