ds.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. spin_lock(&ds_lock);
  188. context = (task ? task->thread.ds_ctx : this_system_context);
  189. if (context)
  190. context->count++;
  191. spin_unlock(&ds_lock);
  192. return context;
  193. }
  194. /*
  195. * Same as ds_get_context, but allocates the context and it's DS
  196. * structure, if necessary; returns NULL; if out of memory.
  197. *
  198. * pre: requires ds_lock to be held
  199. */
  200. static inline struct ds_context *ds_alloc_context(struct task_struct *task)
  201. {
  202. struct ds_context **p_context =
  203. (task ? &task->thread.ds_ctx : &this_system_context);
  204. struct ds_context *context = *p_context;
  205. if (!context) {
  206. spin_unlock(&ds_lock);
  207. context = kzalloc(sizeof(*context), GFP_KERNEL);
  208. if (!context) {
  209. spin_lock(&ds_lock);
  210. return NULL;
  211. }
  212. context->ds = kzalloc(ds_cfg.sizeof_ds, GFP_KERNEL);
  213. if (!context->ds) {
  214. kfree(context);
  215. spin_lock(&ds_lock);
  216. return NULL;
  217. }
  218. spin_lock(&ds_lock);
  219. /*
  220. * Check for race - another CPU could have allocated
  221. * it meanwhile:
  222. */
  223. if (*p_context) {
  224. kfree(context->ds);
  225. kfree(context);
  226. return *p_context;
  227. }
  228. *p_context = context;
  229. context->this = p_context;
  230. context->task = task;
  231. if (task)
  232. set_tsk_thread_flag(task, TIF_DS_AREA_MSR);
  233. if (!task || (task == current))
  234. wrmsr(MSR_IA32_DS_AREA, (unsigned long)context->ds, 0);
  235. get_tracer(task);
  236. }
  237. context->count++;
  238. return context;
  239. }
  240. /*
  241. * Decreases the use count of the parameter context, if not NULL.
  242. * Deallocates the context, if the use count reaches zero.
  243. */
  244. static inline void ds_put_context(struct ds_context *context)
  245. {
  246. if (!context)
  247. return;
  248. spin_lock(&ds_lock);
  249. if (--context->count)
  250. goto out;
  251. *(context->this) = NULL;
  252. if (context->task)
  253. clear_tsk_thread_flag(context->task, TIF_DS_AREA_MSR);
  254. if (!context->task || (context->task == current))
  255. wrmsrl(MSR_IA32_DS_AREA, 0);
  256. put_tracer(context->task);
  257. /* free any leftover buffers from tracers that did not
  258. * deallocate them properly. */
  259. kfree(context->buffer[ds_bts]);
  260. kfree(context->buffer[ds_pebs]);
  261. kfree(context->ds);
  262. kfree(context);
  263. out:
  264. spin_unlock(&ds_lock);
  265. }
  266. /*
  267. * Handle a buffer overflow
  268. *
  269. * task: the task whose buffers are overflowing;
  270. * NULL for a buffer overflow on the current cpu
  271. * context: the ds context
  272. * qual: the buffer type
  273. */
  274. static void ds_overflow(struct task_struct *task, struct ds_context *context,
  275. enum ds_qualifier qual)
  276. {
  277. if (!context)
  278. return;
  279. if (context->callback[qual])
  280. (*context->callback[qual])(task);
  281. /* todo: do some more overflow handling */
  282. }
  283. /*
  284. * Allocate a non-pageable buffer of the parameter size.
  285. * Checks the memory and the locked memory rlimit.
  286. *
  287. * Returns the buffer, if successful;
  288. * NULL, if out of memory or rlimit exceeded.
  289. *
  290. * size: the requested buffer size in bytes
  291. * pages (out): if not NULL, contains the number of pages reserved
  292. */
  293. static inline void *ds_allocate_buffer(size_t size, unsigned int *pages)
  294. {
  295. unsigned long rlim, vm, pgsz;
  296. void *buffer;
  297. pgsz = PAGE_ALIGN(size) >> PAGE_SHIFT;
  298. rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
  299. vm = current->mm->total_vm + pgsz;
  300. if (rlim < vm)
  301. return NULL;
  302. rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
  303. vm = current->mm->locked_vm + pgsz;
  304. if (rlim < vm)
  305. return NULL;
  306. buffer = kzalloc(size, GFP_KERNEL);
  307. if (!buffer)
  308. return NULL;
  309. current->mm->total_vm += pgsz;
  310. current->mm->locked_vm += pgsz;
  311. if (pages)
  312. *pages = pgsz;
  313. return buffer;
  314. }
  315. static int ds_request(struct task_struct *task, void *base, size_t size,
  316. ds_ovfl_callback_t ovfl, enum ds_qualifier qual)
  317. {
  318. struct ds_context *context;
  319. unsigned long buffer, adj;
  320. const unsigned long alignment = (1 << 3);
  321. int error = 0;
  322. if (!ds_cfg.sizeof_ds)
  323. return -EOPNOTSUPP;
  324. /* we require some space to do alignment adjustments below */
  325. if (size < (alignment + ds_cfg.sizeof_rec[qual]))
  326. return -EINVAL;
  327. /* buffer overflow notification is not yet implemented */
  328. if (ovfl)
  329. return -EOPNOTSUPP;
  330. spin_lock(&ds_lock);
  331. error = -ENOMEM;
  332. context = ds_alloc_context(task);
  333. if (!context)
  334. goto out_unlock;
  335. error = -EPERM;
  336. if (!check_tracer(task))
  337. goto out_unlock;
  338. error = -EALREADY;
  339. if (context->owner[qual] == current)
  340. goto out_unlock;
  341. error = -EPERM;
  342. if (context->owner[qual] != NULL)
  343. goto out_unlock;
  344. context->owner[qual] = current;
  345. spin_unlock(&ds_lock);
  346. error = -ENOMEM;
  347. if (!base) {
  348. base = ds_allocate_buffer(size, &context->pages[qual]);
  349. if (!base)
  350. goto out_release;
  351. context->buffer[qual] = base;
  352. }
  353. error = 0;
  354. context->callback[qual] = ovfl;
  355. /* adjust the buffer address and size to meet alignment
  356. * constraints:
  357. * - buffer is double-word aligned
  358. * - size is multiple of record size
  359. *
  360. * We checked the size at the very beginning; we have enough
  361. * space to do the adjustment.
  362. */
  363. buffer = (unsigned long)base;
  364. adj = ALIGN(buffer, alignment) - buffer;
  365. buffer += adj;
  366. size -= adj;
  367. size /= ds_cfg.sizeof_rec[qual];
  368. size *= ds_cfg.sizeof_rec[qual];
  369. ds_set(context->ds, qual, ds_buffer_base, buffer);
  370. ds_set(context->ds, qual, ds_index, buffer);
  371. ds_set(context->ds, qual, ds_absolute_maximum, buffer + size);
  372. if (ovfl) {
  373. /* todo: select a suitable interrupt threshold */
  374. } else
  375. ds_set(context->ds, qual,
  376. ds_interrupt_threshold, buffer + size + 1);
  377. /* we keep the context until ds_release */
  378. return error;
  379. out_release:
  380. context->owner[qual] = NULL;
  381. ds_put_context(context);
  382. return error;
  383. out_unlock:
  384. spin_unlock(&ds_lock);
  385. ds_put_context(context);
  386. return error;
  387. }
  388. int ds_request_bts(struct task_struct *task, void *base, size_t size,
  389. ds_ovfl_callback_t ovfl)
  390. {
  391. return ds_request(task, base, size, ovfl, ds_bts);
  392. }
  393. int ds_request_pebs(struct task_struct *task, void *base, size_t size,
  394. ds_ovfl_callback_t ovfl)
  395. {
  396. return ds_request(task, base, size, ovfl, ds_pebs);
  397. }
  398. static int ds_release(struct task_struct *task, enum ds_qualifier qual)
  399. {
  400. struct ds_context *context;
  401. int error;
  402. context = ds_get_context(task);
  403. error = ds_validate_access(context, qual);
  404. if (error < 0)
  405. goto out;
  406. kfree(context->buffer[qual]);
  407. context->buffer[qual] = NULL;
  408. current->mm->total_vm -= context->pages[qual];
  409. current->mm->locked_vm -= context->pages[qual];
  410. context->pages[qual] = 0;
  411. context->owner[qual] = NULL;
  412. /*
  413. * we put the context twice:
  414. * once for the ds_get_context
  415. * once for the corresponding ds_request
  416. */
  417. ds_put_context(context);
  418. out:
  419. ds_put_context(context);
  420. return error;
  421. }
  422. int ds_release_bts(struct task_struct *task)
  423. {
  424. return ds_release(task, ds_bts);
  425. }
  426. int ds_release_pebs(struct task_struct *task)
  427. {
  428. return ds_release(task, ds_pebs);
  429. }
  430. static int ds_get_index(struct task_struct *task, size_t *pos,
  431. enum ds_qualifier qual)
  432. {
  433. struct ds_context *context;
  434. unsigned long base, index;
  435. int error;
  436. context = ds_get_context(task);
  437. error = ds_validate_access(context, qual);
  438. if (error < 0)
  439. goto out;
  440. base = ds_get(context->ds, qual, ds_buffer_base);
  441. index = ds_get(context->ds, qual, ds_index);
  442. error = ((index - base) / ds_cfg.sizeof_rec[qual]);
  443. if (pos)
  444. *pos = error;
  445. out:
  446. ds_put_context(context);
  447. return error;
  448. }
  449. int ds_get_bts_index(struct task_struct *task, size_t *pos)
  450. {
  451. return ds_get_index(task, pos, ds_bts);
  452. }
  453. int ds_get_pebs_index(struct task_struct *task, size_t *pos)
  454. {
  455. return ds_get_index(task, pos, ds_pebs);
  456. }
  457. static int ds_get_end(struct task_struct *task, size_t *pos,
  458. enum ds_qualifier qual)
  459. {
  460. struct ds_context *context;
  461. unsigned long base, end;
  462. int error;
  463. context = ds_get_context(task);
  464. error = ds_validate_access(context, qual);
  465. if (error < 0)
  466. goto out;
  467. base = ds_get(context->ds, qual, ds_buffer_base);
  468. end = ds_get(context->ds, qual, ds_absolute_maximum);
  469. error = ((end - base) / ds_cfg.sizeof_rec[qual]);
  470. if (pos)
  471. *pos = error;
  472. out:
  473. ds_put_context(context);
  474. return error;
  475. }
  476. int ds_get_bts_end(struct task_struct *task, size_t *pos)
  477. {
  478. return ds_get_end(task, pos, ds_bts);
  479. }
  480. int ds_get_pebs_end(struct task_struct *task, size_t *pos)
  481. {
  482. return ds_get_end(task, pos, ds_pebs);
  483. }
  484. static int ds_access(struct task_struct *task, size_t index,
  485. const void **record, enum ds_qualifier qual)
  486. {
  487. struct ds_context *context;
  488. unsigned long base, idx;
  489. int error;
  490. if (!record)
  491. return -EINVAL;
  492. context = ds_get_context(task);
  493. error = ds_validate_access(context, qual);
  494. if (error < 0)
  495. goto out;
  496. base = ds_get(context->ds, qual, ds_buffer_base);
  497. idx = base + (index * ds_cfg.sizeof_rec[qual]);
  498. error = -EINVAL;
  499. if (idx > ds_get(context->ds, qual, ds_absolute_maximum))
  500. goto out;
  501. *record = (const void *)idx;
  502. error = ds_cfg.sizeof_rec[qual];
  503. out:
  504. ds_put_context(context);
  505. return error;
  506. }
  507. int ds_access_bts(struct task_struct *task, size_t index, const void **record)
  508. {
  509. return ds_access(task, index, record, ds_bts);
  510. }
  511. int ds_access_pebs(struct task_struct *task, size_t index, const void **record)
  512. {
  513. return ds_access(task, index, record, ds_pebs);
  514. }
  515. static int ds_write(struct task_struct *task, const void *record, size_t size,
  516. enum ds_qualifier qual, int force)
  517. {
  518. struct ds_context *context;
  519. int error;
  520. if (!record)
  521. return -EINVAL;
  522. error = -EPERM;
  523. context = ds_get_context(task);
  524. if (!context)
  525. goto out;
  526. if (!force) {
  527. error = ds_validate_access(context, qual);
  528. if (error < 0)
  529. goto out;
  530. }
  531. error = 0;
  532. while (size) {
  533. unsigned long base, index, end, write_end, int_th;
  534. unsigned long write_size, adj_write_size;
  535. /*
  536. * write as much as possible without producing an
  537. * overflow interrupt.
  538. *
  539. * interrupt_threshold must either be
  540. * - bigger than absolute_maximum or
  541. * - point to a record between buffer_base and absolute_maximum
  542. *
  543. * index points to a valid record.
  544. */
  545. base = ds_get(context->ds, qual, ds_buffer_base);
  546. index = ds_get(context->ds, qual, ds_index);
  547. end = ds_get(context->ds, qual, ds_absolute_maximum);
  548. int_th = ds_get(context->ds, qual, ds_interrupt_threshold);
  549. write_end = min(end, int_th);
  550. /* if we are already beyond the interrupt threshold,
  551. * we fill the entire buffer */
  552. if (write_end <= index)
  553. write_end = end;
  554. if (write_end <= index)
  555. goto out;
  556. write_size = min((unsigned long) size, write_end - index);
  557. memcpy((void *)index, record, write_size);
  558. record = (const char *)record + write_size;
  559. size -= write_size;
  560. error += write_size;
  561. adj_write_size = write_size / ds_cfg.sizeof_rec[qual];
  562. adj_write_size *= ds_cfg.sizeof_rec[qual];
  563. /* zero out trailing bytes */
  564. memset((char *)index + write_size, 0,
  565. adj_write_size - write_size);
  566. index += adj_write_size;
  567. if (index >= end)
  568. index = base;
  569. ds_set(context->ds, qual, ds_index, index);
  570. if (index >= int_th)
  571. ds_overflow(task, context, qual);
  572. }
  573. out:
  574. ds_put_context(context);
  575. return error;
  576. }
  577. int ds_write_bts(struct task_struct *task, const void *record, size_t size)
  578. {
  579. return ds_write(task, record, size, ds_bts, /* force = */ 0);
  580. }
  581. int ds_write_pebs(struct task_struct *task, const void *record, size_t size)
  582. {
  583. return ds_write(task, record, size, ds_pebs, /* force = */ 0);
  584. }
  585. int ds_unchecked_write_bts(struct task_struct *task,
  586. const void *record, size_t size)
  587. {
  588. return ds_write(task, record, size, ds_bts, /* force = */ 1);
  589. }
  590. int ds_unchecked_write_pebs(struct task_struct *task,
  591. const void *record, size_t size)
  592. {
  593. return ds_write(task, record, size, ds_pebs, /* force = */ 1);
  594. }
  595. static int ds_reset_or_clear(struct task_struct *task,
  596. enum ds_qualifier qual, int clear)
  597. {
  598. struct ds_context *context;
  599. unsigned long base, end;
  600. int error;
  601. context = ds_get_context(task);
  602. error = ds_validate_access(context, qual);
  603. if (error < 0)
  604. goto out;
  605. base = ds_get(context->ds, qual, ds_buffer_base);
  606. end = ds_get(context->ds, qual, ds_absolute_maximum);
  607. if (clear)
  608. memset((void *)base, 0, end - base);
  609. ds_set(context->ds, qual, ds_index, base);
  610. error = 0;
  611. out:
  612. ds_put_context(context);
  613. return error;
  614. }
  615. int ds_reset_bts(struct task_struct *task)
  616. {
  617. return ds_reset_or_clear(task, ds_bts, /* clear = */ 0);
  618. }
  619. int ds_reset_pebs(struct task_struct *task)
  620. {
  621. return ds_reset_or_clear(task, ds_pebs, /* clear = */ 0);
  622. }
  623. int ds_clear_bts(struct task_struct *task)
  624. {
  625. return ds_reset_or_clear(task, ds_bts, /* clear = */ 1);
  626. }
  627. int ds_clear_pebs(struct task_struct *task)
  628. {
  629. return ds_reset_or_clear(task, ds_pebs, /* clear = */ 1);
  630. }
  631. int ds_get_pebs_reset(struct task_struct *task, u64 *value)
  632. {
  633. struct ds_context *context;
  634. int error;
  635. if (!value)
  636. return -EINVAL;
  637. context = ds_get_context(task);
  638. error = ds_validate_access(context, ds_pebs);
  639. if (error < 0)
  640. goto out;
  641. *value = *(u64 *)(context->ds + (ds_cfg.sizeof_field * 8));
  642. error = 0;
  643. out:
  644. ds_put_context(context);
  645. return error;
  646. }
  647. int ds_set_pebs_reset(struct task_struct *task, u64 value)
  648. {
  649. struct ds_context *context;
  650. int error;
  651. context = ds_get_context(task);
  652. error = ds_validate_access(context, ds_pebs);
  653. if (error < 0)
  654. goto out;
  655. *(u64 *)(context->ds + (ds_cfg.sizeof_field * 8)) = value;
  656. error = 0;
  657. out:
  658. ds_put_context(context);
  659. return error;
  660. }
  661. static const struct ds_configuration ds_cfg_var = {
  662. .sizeof_ds = sizeof(long) * 12,
  663. .sizeof_field = sizeof(long),
  664. .sizeof_rec[ds_bts] = sizeof(long) * 3,
  665. .sizeof_rec[ds_pebs] = sizeof(long) * 10
  666. };
  667. static const struct ds_configuration ds_cfg_64 = {
  668. .sizeof_ds = 8 * 12,
  669. .sizeof_field = 8,
  670. .sizeof_rec[ds_bts] = 8 * 3,
  671. .sizeof_rec[ds_pebs] = 8 * 10
  672. };
  673. static inline void
  674. ds_configure(const struct ds_configuration *cfg)
  675. {
  676. ds_cfg = *cfg;
  677. }
  678. void __cpuinit ds_init_intel(struct cpuinfo_x86 *c)
  679. {
  680. switch (c->x86) {
  681. case 0x6:
  682. switch (c->x86_model) {
  683. case 0xD:
  684. case 0xE: /* Pentium M */
  685. ds_configure(&ds_cfg_var);
  686. break;
  687. case 0xF: /* Core2 */
  688. case 0x1C: /* Atom */
  689. ds_configure(&ds_cfg_64);
  690. break;
  691. default:
  692. /* sorry, don't know about them */
  693. break;
  694. }
  695. break;
  696. case 0xF:
  697. switch (c->x86_model) {
  698. case 0x0:
  699. case 0x1:
  700. case 0x2: /* Netburst */
  701. ds_configure(&ds_cfg_var);
  702. break;
  703. default:
  704. /* sorry, don't know about them */
  705. break;
  706. }
  707. break;
  708. default:
  709. /* sorry, don't know about them */
  710. break;
  711. }
  712. }
  713. void ds_free(struct ds_context *context)
  714. {
  715. /* This is called when the task owning the parameter context
  716. * is dying. There should not be any user of that context left
  717. * to disturb us, anymore. */
  718. unsigned long leftovers = context->count;
  719. while (leftovers--)
  720. ds_put_context(context);
  721. }