ds.c 20 KB

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