ds.c 20 KB

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