perf_event.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. /*
  2. * Performance events x86 architecture code
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2009 Jaswinder Singh Rajput
  7. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  8. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  9. * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
  10. * Copyright (C) 2009 Google, Inc., Stephane Eranian
  11. *
  12. * For licencing details see kernel-base/COPYING
  13. */
  14. #include <linux/perf_event.h>
  15. #include <linux/capability.h>
  16. #include <linux/notifier.h>
  17. #include <linux/hardirq.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/module.h>
  20. #include <linux/kdebug.h>
  21. #include <linux/sched.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/slab.h>
  24. #include <linux/cpu.h>
  25. #include <linux/bitops.h>
  26. #include <asm/apic.h>
  27. #include <asm/stacktrace.h>
  28. #include <asm/nmi.h>
  29. #include <asm/compat.h>
  30. #include <asm/smp.h>
  31. #include <asm/alternative.h>
  32. #include "perf_event.h"
  33. #if 0
  34. #undef wrmsrl
  35. #define wrmsrl(msr, val) \
  36. do { \
  37. trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\
  38. (unsigned long)(val)); \
  39. native_write_msr((msr), (u32)((u64)(val)), \
  40. (u32)((u64)(val) >> 32)); \
  41. } while (0)
  42. #endif
  43. struct x86_pmu x86_pmu __read_mostly;
  44. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  45. .enabled = 1,
  46. };
  47. u64 __read_mostly hw_cache_event_ids
  48. [PERF_COUNT_HW_CACHE_MAX]
  49. [PERF_COUNT_HW_CACHE_OP_MAX]
  50. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  51. u64 __read_mostly hw_cache_extra_regs
  52. [PERF_COUNT_HW_CACHE_MAX]
  53. [PERF_COUNT_HW_CACHE_OP_MAX]
  54. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  55. /*
  56. * Propagate event elapsed time into the generic event.
  57. * Can only be executed on the CPU where the event is active.
  58. * Returns the delta events processed.
  59. */
  60. u64 x86_perf_event_update(struct perf_event *event)
  61. {
  62. struct hw_perf_event *hwc = &event->hw;
  63. int shift = 64 - x86_pmu.cntval_bits;
  64. u64 prev_raw_count, new_raw_count;
  65. int idx = hwc->idx;
  66. s64 delta;
  67. if (idx == X86_PMC_IDX_FIXED_BTS)
  68. return 0;
  69. /*
  70. * Careful: an NMI might modify the previous event value.
  71. *
  72. * Our tactic to handle this is to first atomically read and
  73. * exchange a new raw count - then add that new-prev delta
  74. * count to the generic event atomically:
  75. */
  76. again:
  77. prev_raw_count = local64_read(&hwc->prev_count);
  78. rdmsrl(hwc->event_base, new_raw_count);
  79. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  80. new_raw_count) != prev_raw_count)
  81. goto again;
  82. /*
  83. * Now we have the new raw value and have updated the prev
  84. * timestamp already. We can now calculate the elapsed delta
  85. * (event-)time and add that to the generic event.
  86. *
  87. * Careful, not all hw sign-extends above the physical width
  88. * of the count.
  89. */
  90. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  91. delta >>= shift;
  92. local64_add(delta, &event->count);
  93. local64_sub(delta, &hwc->period_left);
  94. return new_raw_count;
  95. }
  96. /*
  97. * Find and validate any extra registers to set up.
  98. */
  99. static int x86_pmu_extra_regs(u64 config, struct perf_event *event)
  100. {
  101. struct hw_perf_event_extra *reg;
  102. struct extra_reg *er;
  103. reg = &event->hw.extra_reg;
  104. if (!x86_pmu.extra_regs)
  105. return 0;
  106. for (er = x86_pmu.extra_regs; er->msr; er++) {
  107. if (er->event != (config & er->config_mask))
  108. continue;
  109. if (event->attr.config1 & ~er->valid_mask)
  110. return -EINVAL;
  111. reg->idx = er->idx;
  112. reg->config = event->attr.config1;
  113. reg->reg = er->msr;
  114. break;
  115. }
  116. return 0;
  117. }
  118. static atomic_t active_events;
  119. static DEFINE_MUTEX(pmc_reserve_mutex);
  120. #ifdef CONFIG_X86_LOCAL_APIC
  121. static bool reserve_pmc_hardware(void)
  122. {
  123. int i;
  124. for (i = 0; i < x86_pmu.num_counters; i++) {
  125. if (!reserve_perfctr_nmi(x86_pmu_event_addr(i)))
  126. goto perfctr_fail;
  127. }
  128. for (i = 0; i < x86_pmu.num_counters; i++) {
  129. if (!reserve_evntsel_nmi(x86_pmu_config_addr(i)))
  130. goto eventsel_fail;
  131. }
  132. return true;
  133. eventsel_fail:
  134. for (i--; i >= 0; i--)
  135. release_evntsel_nmi(x86_pmu_config_addr(i));
  136. i = x86_pmu.num_counters;
  137. perfctr_fail:
  138. for (i--; i >= 0; i--)
  139. release_perfctr_nmi(x86_pmu_event_addr(i));
  140. return false;
  141. }
  142. static void release_pmc_hardware(void)
  143. {
  144. int i;
  145. for (i = 0; i < x86_pmu.num_counters; i++) {
  146. release_perfctr_nmi(x86_pmu_event_addr(i));
  147. release_evntsel_nmi(x86_pmu_config_addr(i));
  148. }
  149. }
  150. #else
  151. static bool reserve_pmc_hardware(void) { return true; }
  152. static void release_pmc_hardware(void) {}
  153. #endif
  154. static bool check_hw_exists(void)
  155. {
  156. u64 val, val_new = 0;
  157. int i, reg, ret = 0;
  158. /*
  159. * Check to see if the BIOS enabled any of the counters, if so
  160. * complain and bail.
  161. */
  162. for (i = 0; i < x86_pmu.num_counters; i++) {
  163. reg = x86_pmu_config_addr(i);
  164. ret = rdmsrl_safe(reg, &val);
  165. if (ret)
  166. goto msr_fail;
  167. if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
  168. goto bios_fail;
  169. }
  170. if (x86_pmu.num_counters_fixed) {
  171. reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  172. ret = rdmsrl_safe(reg, &val);
  173. if (ret)
  174. goto msr_fail;
  175. for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
  176. if (val & (0x03 << i*4))
  177. goto bios_fail;
  178. }
  179. }
  180. /*
  181. * Now write a value and read it back to see if it matches,
  182. * this is needed to detect certain hardware emulators (qemu/kvm)
  183. * that don't trap on the MSR access and always return 0s.
  184. */
  185. val = 0xabcdUL;
  186. ret = checking_wrmsrl(x86_pmu_event_addr(0), val);
  187. ret |= rdmsrl_safe(x86_pmu_event_addr(0), &val_new);
  188. if (ret || val != val_new)
  189. goto msr_fail;
  190. return true;
  191. bios_fail:
  192. /*
  193. * We still allow the PMU driver to operate:
  194. */
  195. printk(KERN_CONT "Broken BIOS detected, complain to your hardware vendor.\n");
  196. printk(KERN_ERR FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg, val);
  197. return true;
  198. msr_fail:
  199. printk(KERN_CONT "Broken PMU hardware detected, using software events only.\n");
  200. return false;
  201. }
  202. static void hw_perf_event_destroy(struct perf_event *event)
  203. {
  204. if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
  205. release_pmc_hardware();
  206. release_ds_buffers();
  207. mutex_unlock(&pmc_reserve_mutex);
  208. }
  209. }
  210. static inline int x86_pmu_initialized(void)
  211. {
  212. return x86_pmu.handle_irq != NULL;
  213. }
  214. static inline int
  215. set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)
  216. {
  217. struct perf_event_attr *attr = &event->attr;
  218. unsigned int cache_type, cache_op, cache_result;
  219. u64 config, val;
  220. config = attr->config;
  221. cache_type = (config >> 0) & 0xff;
  222. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  223. return -EINVAL;
  224. cache_op = (config >> 8) & 0xff;
  225. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  226. return -EINVAL;
  227. cache_result = (config >> 16) & 0xff;
  228. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  229. return -EINVAL;
  230. val = hw_cache_event_ids[cache_type][cache_op][cache_result];
  231. if (val == 0)
  232. return -ENOENT;
  233. if (val == -1)
  234. return -EINVAL;
  235. hwc->config |= val;
  236. attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result];
  237. return x86_pmu_extra_regs(val, event);
  238. }
  239. int x86_setup_perfctr(struct perf_event *event)
  240. {
  241. struct perf_event_attr *attr = &event->attr;
  242. struct hw_perf_event *hwc = &event->hw;
  243. u64 config;
  244. if (!is_sampling_event(event)) {
  245. hwc->sample_period = x86_pmu.max_period;
  246. hwc->last_period = hwc->sample_period;
  247. local64_set(&hwc->period_left, hwc->sample_period);
  248. } else {
  249. /*
  250. * If we have a PMU initialized but no APIC
  251. * interrupts, we cannot sample hardware
  252. * events (user-space has to fall back and
  253. * sample via a hrtimer based software event):
  254. */
  255. if (!x86_pmu.apic)
  256. return -EOPNOTSUPP;
  257. }
  258. /*
  259. * Do not allow config1 (extended registers) to propagate,
  260. * there's no sane user-space generalization yet:
  261. */
  262. if (attr->type == PERF_TYPE_RAW)
  263. return 0;
  264. if (attr->type == PERF_TYPE_HW_CACHE)
  265. return set_ext_hw_attr(hwc, event);
  266. if (attr->config >= x86_pmu.max_events)
  267. return -EINVAL;
  268. /*
  269. * The generic map:
  270. */
  271. config = x86_pmu.event_map(attr->config);
  272. if (config == 0)
  273. return -ENOENT;
  274. if (config == -1LL)
  275. return -EINVAL;
  276. /*
  277. * Branch tracing:
  278. */
  279. if (attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
  280. !attr->freq && hwc->sample_period == 1) {
  281. /* BTS is not supported by this architecture. */
  282. if (!x86_pmu.bts_active)
  283. return -EOPNOTSUPP;
  284. /* BTS is currently only allowed for user-mode. */
  285. if (!attr->exclude_kernel)
  286. return -EOPNOTSUPP;
  287. }
  288. hwc->config |= config;
  289. return 0;
  290. }
  291. int x86_pmu_hw_config(struct perf_event *event)
  292. {
  293. if (event->attr.precise_ip) {
  294. int precise = 0;
  295. /* Support for constant skid */
  296. if (x86_pmu.pebs_active) {
  297. precise++;
  298. /* Support for IP fixup */
  299. if (x86_pmu.lbr_nr)
  300. precise++;
  301. }
  302. if (event->attr.precise_ip > precise)
  303. return -EOPNOTSUPP;
  304. }
  305. /*
  306. * Generate PMC IRQs:
  307. * (keep 'enabled' bit clear for now)
  308. */
  309. event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
  310. /*
  311. * Count user and OS events unless requested not to
  312. */
  313. if (!event->attr.exclude_user)
  314. event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
  315. if (!event->attr.exclude_kernel)
  316. event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
  317. if (event->attr.type == PERF_TYPE_RAW)
  318. event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
  319. return x86_setup_perfctr(event);
  320. }
  321. /*
  322. * Setup the hardware configuration for a given attr_type
  323. */
  324. static int __x86_pmu_event_init(struct perf_event *event)
  325. {
  326. int err;
  327. if (!x86_pmu_initialized())
  328. return -ENODEV;
  329. err = 0;
  330. if (!atomic_inc_not_zero(&active_events)) {
  331. mutex_lock(&pmc_reserve_mutex);
  332. if (atomic_read(&active_events) == 0) {
  333. if (!reserve_pmc_hardware())
  334. err = -EBUSY;
  335. else
  336. reserve_ds_buffers();
  337. }
  338. if (!err)
  339. atomic_inc(&active_events);
  340. mutex_unlock(&pmc_reserve_mutex);
  341. }
  342. if (err)
  343. return err;
  344. event->destroy = hw_perf_event_destroy;
  345. event->hw.idx = -1;
  346. event->hw.last_cpu = -1;
  347. event->hw.last_tag = ~0ULL;
  348. /* mark unused */
  349. event->hw.extra_reg.idx = EXTRA_REG_NONE;
  350. return x86_pmu.hw_config(event);
  351. }
  352. void x86_pmu_disable_all(void)
  353. {
  354. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  355. int idx;
  356. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  357. u64 val;
  358. if (!test_bit(idx, cpuc->active_mask))
  359. continue;
  360. rdmsrl(x86_pmu_config_addr(idx), val);
  361. if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
  362. continue;
  363. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  364. wrmsrl(x86_pmu_config_addr(idx), val);
  365. }
  366. }
  367. static void x86_pmu_disable(struct pmu *pmu)
  368. {
  369. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  370. if (!x86_pmu_initialized())
  371. return;
  372. if (!cpuc->enabled)
  373. return;
  374. cpuc->n_added = 0;
  375. cpuc->enabled = 0;
  376. barrier();
  377. x86_pmu.disable_all();
  378. }
  379. void x86_pmu_enable_all(int added)
  380. {
  381. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  382. int idx;
  383. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  384. struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
  385. if (!test_bit(idx, cpuc->active_mask))
  386. continue;
  387. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  388. }
  389. }
  390. static struct pmu pmu;
  391. static inline int is_x86_event(struct perf_event *event)
  392. {
  393. return event->pmu == &pmu;
  394. }
  395. int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  396. {
  397. struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
  398. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  399. int i, j, w, wmax, num = 0;
  400. struct hw_perf_event *hwc;
  401. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  402. for (i = 0; i < n; i++) {
  403. c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
  404. constraints[i] = c;
  405. }
  406. /*
  407. * fastpath, try to reuse previous register
  408. */
  409. for (i = 0; i < n; i++) {
  410. hwc = &cpuc->event_list[i]->hw;
  411. c = constraints[i];
  412. /* never assigned */
  413. if (hwc->idx == -1)
  414. break;
  415. /* constraint still honored */
  416. if (!test_bit(hwc->idx, c->idxmsk))
  417. break;
  418. /* not already used */
  419. if (test_bit(hwc->idx, used_mask))
  420. break;
  421. __set_bit(hwc->idx, used_mask);
  422. if (assign)
  423. assign[i] = hwc->idx;
  424. }
  425. if (i == n)
  426. goto done;
  427. /*
  428. * begin slow path
  429. */
  430. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  431. /*
  432. * weight = number of possible counters
  433. *
  434. * 1 = most constrained, only works on one counter
  435. * wmax = least constrained, works on any counter
  436. *
  437. * assign events to counters starting with most
  438. * constrained events.
  439. */
  440. wmax = x86_pmu.num_counters;
  441. /*
  442. * when fixed event counters are present,
  443. * wmax is incremented by 1 to account
  444. * for one more choice
  445. */
  446. if (x86_pmu.num_counters_fixed)
  447. wmax++;
  448. for (w = 1, num = n; num && w <= wmax; w++) {
  449. /* for each event */
  450. for (i = 0; num && i < n; i++) {
  451. c = constraints[i];
  452. hwc = &cpuc->event_list[i]->hw;
  453. if (c->weight != w)
  454. continue;
  455. for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) {
  456. if (!test_bit(j, used_mask))
  457. break;
  458. }
  459. if (j == X86_PMC_IDX_MAX)
  460. break;
  461. __set_bit(j, used_mask);
  462. if (assign)
  463. assign[i] = j;
  464. num--;
  465. }
  466. }
  467. done:
  468. /*
  469. * scheduling failed or is just a simulation,
  470. * free resources if necessary
  471. */
  472. if (!assign || num) {
  473. for (i = 0; i < n; i++) {
  474. if (x86_pmu.put_event_constraints)
  475. x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
  476. }
  477. }
  478. return num ? -EINVAL : 0;
  479. }
  480. /*
  481. * dogrp: true if must collect siblings events (group)
  482. * returns total number of events and error code
  483. */
  484. static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
  485. {
  486. struct perf_event *event;
  487. int n, max_count;
  488. max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
  489. /* current number of events already accepted */
  490. n = cpuc->n_events;
  491. if (is_x86_event(leader)) {
  492. if (n >= max_count)
  493. return -EINVAL;
  494. cpuc->event_list[n] = leader;
  495. n++;
  496. }
  497. if (!dogrp)
  498. return n;
  499. list_for_each_entry(event, &leader->sibling_list, group_entry) {
  500. if (!is_x86_event(event) ||
  501. event->state <= PERF_EVENT_STATE_OFF)
  502. continue;
  503. if (n >= max_count)
  504. return -EINVAL;
  505. cpuc->event_list[n] = event;
  506. n++;
  507. }
  508. return n;
  509. }
  510. static inline void x86_assign_hw_event(struct perf_event *event,
  511. struct cpu_hw_events *cpuc, int i)
  512. {
  513. struct hw_perf_event *hwc = &event->hw;
  514. hwc->idx = cpuc->assign[i];
  515. hwc->last_cpu = smp_processor_id();
  516. hwc->last_tag = ++cpuc->tags[i];
  517. if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
  518. hwc->config_base = 0;
  519. hwc->event_base = 0;
  520. } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
  521. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  522. hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - X86_PMC_IDX_FIXED);
  523. } else {
  524. hwc->config_base = x86_pmu_config_addr(hwc->idx);
  525. hwc->event_base = x86_pmu_event_addr(hwc->idx);
  526. }
  527. }
  528. static inline int match_prev_assignment(struct hw_perf_event *hwc,
  529. struct cpu_hw_events *cpuc,
  530. int i)
  531. {
  532. return hwc->idx == cpuc->assign[i] &&
  533. hwc->last_cpu == smp_processor_id() &&
  534. hwc->last_tag == cpuc->tags[i];
  535. }
  536. static void x86_pmu_start(struct perf_event *event, int flags);
  537. static void x86_pmu_enable(struct pmu *pmu)
  538. {
  539. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  540. struct perf_event *event;
  541. struct hw_perf_event *hwc;
  542. int i, added = cpuc->n_added;
  543. if (!x86_pmu_initialized())
  544. return;
  545. if (cpuc->enabled)
  546. return;
  547. if (cpuc->n_added) {
  548. int n_running = cpuc->n_events - cpuc->n_added;
  549. /*
  550. * apply assignment obtained either from
  551. * hw_perf_group_sched_in() or x86_pmu_enable()
  552. *
  553. * step1: save events moving to new counters
  554. * step2: reprogram moved events into new counters
  555. */
  556. for (i = 0; i < n_running; i++) {
  557. event = cpuc->event_list[i];
  558. hwc = &event->hw;
  559. /*
  560. * we can avoid reprogramming counter if:
  561. * - assigned same counter as last time
  562. * - running on same CPU as last time
  563. * - no other event has used the counter since
  564. */
  565. if (hwc->idx == -1 ||
  566. match_prev_assignment(hwc, cpuc, i))
  567. continue;
  568. /*
  569. * Ensure we don't accidentally enable a stopped
  570. * counter simply because we rescheduled.
  571. */
  572. if (hwc->state & PERF_HES_STOPPED)
  573. hwc->state |= PERF_HES_ARCH;
  574. x86_pmu_stop(event, PERF_EF_UPDATE);
  575. }
  576. for (i = 0; i < cpuc->n_events; i++) {
  577. event = cpuc->event_list[i];
  578. hwc = &event->hw;
  579. if (!match_prev_assignment(hwc, cpuc, i))
  580. x86_assign_hw_event(event, cpuc, i);
  581. else if (i < n_running)
  582. continue;
  583. if (hwc->state & PERF_HES_ARCH)
  584. continue;
  585. x86_pmu_start(event, PERF_EF_RELOAD);
  586. }
  587. cpuc->n_added = 0;
  588. perf_events_lapic_init();
  589. }
  590. cpuc->enabled = 1;
  591. barrier();
  592. x86_pmu.enable_all(added);
  593. }
  594. static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  595. /*
  596. * Set the next IRQ period, based on the hwc->period_left value.
  597. * To be called with the event disabled in hw:
  598. */
  599. int x86_perf_event_set_period(struct perf_event *event)
  600. {
  601. struct hw_perf_event *hwc = &event->hw;
  602. s64 left = local64_read(&hwc->period_left);
  603. s64 period = hwc->sample_period;
  604. int ret = 0, idx = hwc->idx;
  605. if (idx == X86_PMC_IDX_FIXED_BTS)
  606. return 0;
  607. /*
  608. * If we are way outside a reasonable range then just skip forward:
  609. */
  610. if (unlikely(left <= -period)) {
  611. left = period;
  612. local64_set(&hwc->period_left, left);
  613. hwc->last_period = period;
  614. ret = 1;
  615. }
  616. if (unlikely(left <= 0)) {
  617. left += period;
  618. local64_set(&hwc->period_left, left);
  619. hwc->last_period = period;
  620. ret = 1;
  621. }
  622. /*
  623. * Quirk: certain CPUs dont like it if just 1 hw_event is left:
  624. */
  625. if (unlikely(left < 2))
  626. left = 2;
  627. if (left > x86_pmu.max_period)
  628. left = x86_pmu.max_period;
  629. per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
  630. /*
  631. * The hw event starts counting from this event offset,
  632. * mark it to be able to extra future deltas:
  633. */
  634. local64_set(&hwc->prev_count, (u64)-left);
  635. wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
  636. /*
  637. * Due to erratum on certan cpu we need
  638. * a second write to be sure the register
  639. * is updated properly
  640. */
  641. if (x86_pmu.perfctr_second_write) {
  642. wrmsrl(hwc->event_base,
  643. (u64)(-left) & x86_pmu.cntval_mask);
  644. }
  645. perf_event_update_userpage(event);
  646. return ret;
  647. }
  648. void x86_pmu_enable_event(struct perf_event *event)
  649. {
  650. if (__this_cpu_read(cpu_hw_events.enabled))
  651. __x86_pmu_enable_event(&event->hw,
  652. ARCH_PERFMON_EVENTSEL_ENABLE);
  653. }
  654. /*
  655. * Add a single event to the PMU.
  656. *
  657. * The event is added to the group of enabled events
  658. * but only if it can be scehduled with existing events.
  659. */
  660. static int x86_pmu_add(struct perf_event *event, int flags)
  661. {
  662. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  663. struct hw_perf_event *hwc;
  664. int assign[X86_PMC_IDX_MAX];
  665. int n, n0, ret;
  666. hwc = &event->hw;
  667. perf_pmu_disable(event->pmu);
  668. n0 = cpuc->n_events;
  669. ret = n = collect_events(cpuc, event, false);
  670. if (ret < 0)
  671. goto out;
  672. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  673. if (!(flags & PERF_EF_START))
  674. hwc->state |= PERF_HES_ARCH;
  675. /*
  676. * If group events scheduling transaction was started,
  677. * skip the schedulability test here, it will be performed
  678. * at commit time (->commit_txn) as a whole
  679. */
  680. if (cpuc->group_flag & PERF_EVENT_TXN)
  681. goto done_collect;
  682. ret = x86_pmu.schedule_events(cpuc, n, assign);
  683. if (ret)
  684. goto out;
  685. /*
  686. * copy new assignment, now we know it is possible
  687. * will be used by hw_perf_enable()
  688. */
  689. memcpy(cpuc->assign, assign, n*sizeof(int));
  690. done_collect:
  691. cpuc->n_events = n;
  692. cpuc->n_added += n - n0;
  693. cpuc->n_txn += n - n0;
  694. ret = 0;
  695. out:
  696. perf_pmu_enable(event->pmu);
  697. return ret;
  698. }
  699. static void x86_pmu_start(struct perf_event *event, int flags)
  700. {
  701. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  702. int idx = event->hw.idx;
  703. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  704. return;
  705. if (WARN_ON_ONCE(idx == -1))
  706. return;
  707. if (flags & PERF_EF_RELOAD) {
  708. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  709. x86_perf_event_set_period(event);
  710. }
  711. event->hw.state = 0;
  712. cpuc->events[idx] = event;
  713. __set_bit(idx, cpuc->active_mask);
  714. __set_bit(idx, cpuc->running);
  715. x86_pmu.enable(event);
  716. perf_event_update_userpage(event);
  717. }
  718. void perf_event_print_debug(void)
  719. {
  720. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  721. u64 pebs;
  722. struct cpu_hw_events *cpuc;
  723. unsigned long flags;
  724. int cpu, idx;
  725. if (!x86_pmu.num_counters)
  726. return;
  727. local_irq_save(flags);
  728. cpu = smp_processor_id();
  729. cpuc = &per_cpu(cpu_hw_events, cpu);
  730. if (x86_pmu.version >= 2) {
  731. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  732. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  733. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  734. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  735. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  736. pr_info("\n");
  737. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  738. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  739. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  740. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  741. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  742. }
  743. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  744. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  745. rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
  746. rdmsrl(x86_pmu_event_addr(idx), pmc_count);
  747. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  748. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  749. cpu, idx, pmc_ctrl);
  750. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  751. cpu, idx, pmc_count);
  752. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  753. cpu, idx, prev_left);
  754. }
  755. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  756. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  757. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  758. cpu, idx, pmc_count);
  759. }
  760. local_irq_restore(flags);
  761. }
  762. void x86_pmu_stop(struct perf_event *event, int flags)
  763. {
  764. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  765. struct hw_perf_event *hwc = &event->hw;
  766. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  767. x86_pmu.disable(event);
  768. cpuc->events[hwc->idx] = NULL;
  769. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  770. hwc->state |= PERF_HES_STOPPED;
  771. }
  772. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  773. /*
  774. * Drain the remaining delta count out of a event
  775. * that we are disabling:
  776. */
  777. x86_perf_event_update(event);
  778. hwc->state |= PERF_HES_UPTODATE;
  779. }
  780. }
  781. static void x86_pmu_del(struct perf_event *event, int flags)
  782. {
  783. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  784. int i;
  785. /*
  786. * If we're called during a txn, we don't need to do anything.
  787. * The events never got scheduled and ->cancel_txn will truncate
  788. * the event_list.
  789. */
  790. if (cpuc->group_flag & PERF_EVENT_TXN)
  791. return;
  792. x86_pmu_stop(event, PERF_EF_UPDATE);
  793. for (i = 0; i < cpuc->n_events; i++) {
  794. if (event == cpuc->event_list[i]) {
  795. if (x86_pmu.put_event_constraints)
  796. x86_pmu.put_event_constraints(cpuc, event);
  797. while (++i < cpuc->n_events)
  798. cpuc->event_list[i-1] = cpuc->event_list[i];
  799. --cpuc->n_events;
  800. break;
  801. }
  802. }
  803. perf_event_update_userpage(event);
  804. }
  805. int x86_pmu_handle_irq(struct pt_regs *regs)
  806. {
  807. struct perf_sample_data data;
  808. struct cpu_hw_events *cpuc;
  809. struct perf_event *event;
  810. int idx, handled = 0;
  811. u64 val;
  812. perf_sample_data_init(&data, 0);
  813. cpuc = &__get_cpu_var(cpu_hw_events);
  814. /*
  815. * Some chipsets need to unmask the LVTPC in a particular spot
  816. * inside the nmi handler. As a result, the unmasking was pushed
  817. * into all the nmi handlers.
  818. *
  819. * This generic handler doesn't seem to have any issues where the
  820. * unmasking occurs so it was left at the top.
  821. */
  822. apic_write(APIC_LVTPC, APIC_DM_NMI);
  823. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  824. if (!test_bit(idx, cpuc->active_mask)) {
  825. /*
  826. * Though we deactivated the counter some cpus
  827. * might still deliver spurious interrupts still
  828. * in flight. Catch them:
  829. */
  830. if (__test_and_clear_bit(idx, cpuc->running))
  831. handled++;
  832. continue;
  833. }
  834. event = cpuc->events[idx];
  835. val = x86_perf_event_update(event);
  836. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  837. continue;
  838. /*
  839. * event overflow
  840. */
  841. handled++;
  842. data.period = event->hw.last_period;
  843. if (!x86_perf_event_set_period(event))
  844. continue;
  845. if (perf_event_overflow(event, &data, regs))
  846. x86_pmu_stop(event, 0);
  847. }
  848. if (handled)
  849. inc_irq_stat(apic_perf_irqs);
  850. return handled;
  851. }
  852. void perf_events_lapic_init(void)
  853. {
  854. if (!x86_pmu.apic || !x86_pmu_initialized())
  855. return;
  856. /*
  857. * Always use NMI for PMU
  858. */
  859. apic_write(APIC_LVTPC, APIC_DM_NMI);
  860. }
  861. static int __kprobes
  862. perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  863. {
  864. if (!atomic_read(&active_events))
  865. return NMI_DONE;
  866. return x86_pmu.handle_irq(regs);
  867. }
  868. struct event_constraint emptyconstraint;
  869. struct event_constraint unconstrained;
  870. static int __cpuinit
  871. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  872. {
  873. unsigned int cpu = (long)hcpu;
  874. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  875. int ret = NOTIFY_OK;
  876. switch (action & ~CPU_TASKS_FROZEN) {
  877. case CPU_UP_PREPARE:
  878. cpuc->kfree_on_online = NULL;
  879. if (x86_pmu.cpu_prepare)
  880. ret = x86_pmu.cpu_prepare(cpu);
  881. break;
  882. case CPU_STARTING:
  883. if (x86_pmu.cpu_starting)
  884. x86_pmu.cpu_starting(cpu);
  885. break;
  886. case CPU_ONLINE:
  887. kfree(cpuc->kfree_on_online);
  888. break;
  889. case CPU_DYING:
  890. if (x86_pmu.cpu_dying)
  891. x86_pmu.cpu_dying(cpu);
  892. break;
  893. case CPU_UP_CANCELED:
  894. case CPU_DEAD:
  895. if (x86_pmu.cpu_dead)
  896. x86_pmu.cpu_dead(cpu);
  897. break;
  898. default:
  899. break;
  900. }
  901. return ret;
  902. }
  903. static void __init pmu_check_apic(void)
  904. {
  905. if (cpu_has_apic)
  906. return;
  907. x86_pmu.apic = 0;
  908. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  909. pr_info("no hardware sampling interrupt available.\n");
  910. }
  911. static int __init init_hw_perf_events(void)
  912. {
  913. struct event_constraint *c;
  914. int err;
  915. pr_info("Performance Events: ");
  916. switch (boot_cpu_data.x86_vendor) {
  917. case X86_VENDOR_INTEL:
  918. err = intel_pmu_init();
  919. break;
  920. case X86_VENDOR_AMD:
  921. err = amd_pmu_init();
  922. break;
  923. default:
  924. return 0;
  925. }
  926. if (err != 0) {
  927. pr_cont("no PMU driver, software events only.\n");
  928. return 0;
  929. }
  930. pmu_check_apic();
  931. /* sanity check that the hardware exists or is emulated */
  932. if (!check_hw_exists())
  933. return 0;
  934. pr_cont("%s PMU driver.\n", x86_pmu.name);
  935. if (x86_pmu.quirks)
  936. x86_pmu.quirks();
  937. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  938. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  939. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  940. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  941. }
  942. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  943. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  944. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  945. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  946. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  947. }
  948. x86_pmu.intel_ctrl |=
  949. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  950. perf_events_lapic_init();
  951. register_nmi_handler(NMI_LOCAL, perf_event_nmi_handler, 0, "PMI");
  952. unconstrained = (struct event_constraint)
  953. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  954. 0, x86_pmu.num_counters);
  955. if (x86_pmu.event_constraints) {
  956. for_each_event_constraint(c, x86_pmu.event_constraints) {
  957. if (c->cmask != X86_RAW_EVENT_MASK)
  958. continue;
  959. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  960. c->weight += x86_pmu.num_counters;
  961. }
  962. }
  963. pr_info("... version: %d\n", x86_pmu.version);
  964. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  965. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  966. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  967. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  968. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  969. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  970. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  971. perf_cpu_notifier(x86_pmu_notifier);
  972. return 0;
  973. }
  974. early_initcall(init_hw_perf_events);
  975. static inline void x86_pmu_read(struct perf_event *event)
  976. {
  977. x86_perf_event_update(event);
  978. }
  979. /*
  980. * Start group events scheduling transaction
  981. * Set the flag to make pmu::enable() not perform the
  982. * schedulability test, it will be performed at commit time
  983. */
  984. static void x86_pmu_start_txn(struct pmu *pmu)
  985. {
  986. perf_pmu_disable(pmu);
  987. __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN);
  988. __this_cpu_write(cpu_hw_events.n_txn, 0);
  989. }
  990. /*
  991. * Stop group events scheduling transaction
  992. * Clear the flag and pmu::enable() will perform the
  993. * schedulability test.
  994. */
  995. static void x86_pmu_cancel_txn(struct pmu *pmu)
  996. {
  997. __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN);
  998. /*
  999. * Truncate the collected events.
  1000. */
  1001. __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
  1002. __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
  1003. perf_pmu_enable(pmu);
  1004. }
  1005. /*
  1006. * Commit group events scheduling transaction
  1007. * Perform the group schedulability test as a whole
  1008. * Return 0 if success
  1009. */
  1010. static int x86_pmu_commit_txn(struct pmu *pmu)
  1011. {
  1012. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1013. int assign[X86_PMC_IDX_MAX];
  1014. int n, ret;
  1015. n = cpuc->n_events;
  1016. if (!x86_pmu_initialized())
  1017. return -EAGAIN;
  1018. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1019. if (ret)
  1020. return ret;
  1021. /*
  1022. * copy new assignment, now we know it is possible
  1023. * will be used by hw_perf_enable()
  1024. */
  1025. memcpy(cpuc->assign, assign, n*sizeof(int));
  1026. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1027. perf_pmu_enable(pmu);
  1028. return 0;
  1029. }
  1030. /*
  1031. * a fake_cpuc is used to validate event groups. Due to
  1032. * the extra reg logic, we need to also allocate a fake
  1033. * per_core and per_cpu structure. Otherwise, group events
  1034. * using extra reg may conflict without the kernel being
  1035. * able to catch this when the last event gets added to
  1036. * the group.
  1037. */
  1038. static void free_fake_cpuc(struct cpu_hw_events *cpuc)
  1039. {
  1040. kfree(cpuc->shared_regs);
  1041. kfree(cpuc);
  1042. }
  1043. static struct cpu_hw_events *allocate_fake_cpuc(void)
  1044. {
  1045. struct cpu_hw_events *cpuc;
  1046. int cpu = raw_smp_processor_id();
  1047. cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL);
  1048. if (!cpuc)
  1049. return ERR_PTR(-ENOMEM);
  1050. /* only needed, if we have extra_regs */
  1051. if (x86_pmu.extra_regs) {
  1052. cpuc->shared_regs = allocate_shared_regs(cpu);
  1053. if (!cpuc->shared_regs)
  1054. goto error;
  1055. }
  1056. return cpuc;
  1057. error:
  1058. free_fake_cpuc(cpuc);
  1059. return ERR_PTR(-ENOMEM);
  1060. }
  1061. /*
  1062. * validate that we can schedule this event
  1063. */
  1064. static int validate_event(struct perf_event *event)
  1065. {
  1066. struct cpu_hw_events *fake_cpuc;
  1067. struct event_constraint *c;
  1068. int ret = 0;
  1069. fake_cpuc = allocate_fake_cpuc();
  1070. if (IS_ERR(fake_cpuc))
  1071. return PTR_ERR(fake_cpuc);
  1072. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1073. if (!c || !c->weight)
  1074. ret = -EINVAL;
  1075. if (x86_pmu.put_event_constraints)
  1076. x86_pmu.put_event_constraints(fake_cpuc, event);
  1077. free_fake_cpuc(fake_cpuc);
  1078. return ret;
  1079. }
  1080. /*
  1081. * validate a single event group
  1082. *
  1083. * validation include:
  1084. * - check events are compatible which each other
  1085. * - events do not compete for the same counter
  1086. * - number of events <= number of counters
  1087. *
  1088. * validation ensures the group can be loaded onto the
  1089. * PMU if it was the only group available.
  1090. */
  1091. static int validate_group(struct perf_event *event)
  1092. {
  1093. struct perf_event *leader = event->group_leader;
  1094. struct cpu_hw_events *fake_cpuc;
  1095. int ret = -EINVAL, n;
  1096. fake_cpuc = allocate_fake_cpuc();
  1097. if (IS_ERR(fake_cpuc))
  1098. return PTR_ERR(fake_cpuc);
  1099. /*
  1100. * the event is not yet connected with its
  1101. * siblings therefore we must first collect
  1102. * existing siblings, then add the new event
  1103. * before we can simulate the scheduling
  1104. */
  1105. n = collect_events(fake_cpuc, leader, true);
  1106. if (n < 0)
  1107. goto out;
  1108. fake_cpuc->n_events = n;
  1109. n = collect_events(fake_cpuc, event, false);
  1110. if (n < 0)
  1111. goto out;
  1112. fake_cpuc->n_events = n;
  1113. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1114. out:
  1115. free_fake_cpuc(fake_cpuc);
  1116. return ret;
  1117. }
  1118. static int x86_pmu_event_init(struct perf_event *event)
  1119. {
  1120. struct pmu *tmp;
  1121. int err;
  1122. switch (event->attr.type) {
  1123. case PERF_TYPE_RAW:
  1124. case PERF_TYPE_HARDWARE:
  1125. case PERF_TYPE_HW_CACHE:
  1126. break;
  1127. default:
  1128. return -ENOENT;
  1129. }
  1130. err = __x86_pmu_event_init(event);
  1131. if (!err) {
  1132. /*
  1133. * we temporarily connect event to its pmu
  1134. * such that validate_group() can classify
  1135. * it as an x86 event using is_x86_event()
  1136. */
  1137. tmp = event->pmu;
  1138. event->pmu = &pmu;
  1139. if (event->group_leader != event)
  1140. err = validate_group(event);
  1141. else
  1142. err = validate_event(event);
  1143. event->pmu = tmp;
  1144. }
  1145. if (err) {
  1146. if (event->destroy)
  1147. event->destroy(event);
  1148. }
  1149. return err;
  1150. }
  1151. static struct pmu pmu = {
  1152. .pmu_enable = x86_pmu_enable,
  1153. .pmu_disable = x86_pmu_disable,
  1154. .event_init = x86_pmu_event_init,
  1155. .add = x86_pmu_add,
  1156. .del = x86_pmu_del,
  1157. .start = x86_pmu_start,
  1158. .stop = x86_pmu_stop,
  1159. .read = x86_pmu_read,
  1160. .start_txn = x86_pmu_start_txn,
  1161. .cancel_txn = x86_pmu_cancel_txn,
  1162. .commit_txn = x86_pmu_commit_txn,
  1163. };
  1164. /*
  1165. * callchain support
  1166. */
  1167. static int backtrace_stack(void *data, char *name)
  1168. {
  1169. return 0;
  1170. }
  1171. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1172. {
  1173. struct perf_callchain_entry *entry = data;
  1174. perf_callchain_store(entry, addr);
  1175. }
  1176. static const struct stacktrace_ops backtrace_ops = {
  1177. .stack = backtrace_stack,
  1178. .address = backtrace_address,
  1179. .walk_stack = print_context_stack_bp,
  1180. };
  1181. void
  1182. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1183. {
  1184. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1185. /* TODO: We don't support guest os callchain now */
  1186. return;
  1187. }
  1188. perf_callchain_store(entry, regs->ip);
  1189. dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
  1190. }
  1191. #ifdef CONFIG_COMPAT
  1192. static inline int
  1193. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1194. {
  1195. /* 32-bit process in 64-bit kernel. */
  1196. struct stack_frame_ia32 frame;
  1197. const void __user *fp;
  1198. if (!test_thread_flag(TIF_IA32))
  1199. return 0;
  1200. fp = compat_ptr(regs->bp);
  1201. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1202. unsigned long bytes;
  1203. frame.next_frame = 0;
  1204. frame.return_address = 0;
  1205. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1206. if (bytes != sizeof(frame))
  1207. break;
  1208. if (fp < compat_ptr(regs->sp))
  1209. break;
  1210. perf_callchain_store(entry, frame.return_address);
  1211. fp = compat_ptr(frame.next_frame);
  1212. }
  1213. return 1;
  1214. }
  1215. #else
  1216. static inline int
  1217. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1218. {
  1219. return 0;
  1220. }
  1221. #endif
  1222. void
  1223. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1224. {
  1225. struct stack_frame frame;
  1226. const void __user *fp;
  1227. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1228. /* TODO: We don't support guest os callchain now */
  1229. return;
  1230. }
  1231. fp = (void __user *)regs->bp;
  1232. perf_callchain_store(entry, regs->ip);
  1233. if (!current->mm)
  1234. return;
  1235. if (perf_callchain_user32(regs, entry))
  1236. return;
  1237. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1238. unsigned long bytes;
  1239. frame.next_frame = NULL;
  1240. frame.return_address = 0;
  1241. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1242. if (bytes != sizeof(frame))
  1243. break;
  1244. if ((unsigned long)fp < regs->sp)
  1245. break;
  1246. perf_callchain_store(entry, frame.return_address);
  1247. fp = frame.next_frame;
  1248. }
  1249. }
  1250. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1251. {
  1252. unsigned long ip;
  1253. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1254. ip = perf_guest_cbs->get_guest_ip();
  1255. else
  1256. ip = instruction_pointer(regs);
  1257. return ip;
  1258. }
  1259. unsigned long perf_misc_flags(struct pt_regs *regs)
  1260. {
  1261. int misc = 0;
  1262. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1263. if (perf_guest_cbs->is_user_mode())
  1264. misc |= PERF_RECORD_MISC_GUEST_USER;
  1265. else
  1266. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1267. } else {
  1268. if (user_mode(regs))
  1269. misc |= PERF_RECORD_MISC_USER;
  1270. else
  1271. misc |= PERF_RECORD_MISC_KERNEL;
  1272. }
  1273. if (regs->flags & PERF_EFLAGS_EXACT)
  1274. misc |= PERF_RECORD_MISC_EXACT_IP;
  1275. return misc;
  1276. }