perf_event.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  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/highmem.h>
  25. #include <linux/cpu.h>
  26. #include <linux/bitops.h>
  27. #include <asm/apic.h>
  28. #include <asm/stacktrace.h>
  29. #include <asm/nmi.h>
  30. #include <asm/compat.h>
  31. #include <asm/smp.h>
  32. #if 0
  33. #undef wrmsrl
  34. #define wrmsrl(msr, val) \
  35. do { \
  36. trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\
  37. (unsigned long)(val)); \
  38. native_write_msr((msr), (u32)((u64)(val)), \
  39. (u32)((u64)(val) >> 32)); \
  40. } while (0)
  41. #endif
  42. /*
  43. * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
  44. */
  45. static unsigned long
  46. copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
  47. {
  48. unsigned long offset, addr = (unsigned long)from;
  49. unsigned long size, len = 0;
  50. struct page *page;
  51. void *map;
  52. int ret;
  53. do {
  54. ret = __get_user_pages_fast(addr, 1, 0, &page);
  55. if (!ret)
  56. break;
  57. offset = addr & (PAGE_SIZE - 1);
  58. size = min(PAGE_SIZE - offset, n - len);
  59. map = kmap_atomic(page);
  60. memcpy(to, map+offset, size);
  61. kunmap_atomic(map);
  62. put_page(page);
  63. len += size;
  64. to += size;
  65. addr += size;
  66. } while (len < n);
  67. return len;
  68. }
  69. struct event_constraint {
  70. union {
  71. unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  72. u64 idxmsk64;
  73. };
  74. u64 code;
  75. u64 cmask;
  76. int weight;
  77. };
  78. struct amd_nb {
  79. int nb_id; /* NorthBridge id */
  80. int refcnt; /* reference count */
  81. struct perf_event *owners[X86_PMC_IDX_MAX];
  82. struct event_constraint event_constraints[X86_PMC_IDX_MAX];
  83. };
  84. struct intel_percore;
  85. #define MAX_LBR_ENTRIES 16
  86. struct cpu_hw_events {
  87. /*
  88. * Generic x86 PMC bits
  89. */
  90. struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
  91. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  92. unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  93. int enabled;
  94. int n_events;
  95. int n_added;
  96. int n_txn;
  97. int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
  98. u64 tags[X86_PMC_IDX_MAX];
  99. struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
  100. unsigned int group_flag;
  101. /*
  102. * Intel DebugStore bits
  103. */
  104. struct debug_store *ds;
  105. u64 pebs_enabled;
  106. /*
  107. * Intel LBR bits
  108. */
  109. int lbr_users;
  110. void *lbr_context;
  111. struct perf_branch_stack lbr_stack;
  112. struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
  113. /*
  114. * Intel percore register state.
  115. * Coordinate shared resources between HT threads.
  116. */
  117. int percore_used; /* Used by this CPU? */
  118. struct intel_percore *per_core;
  119. /*
  120. * AMD specific bits
  121. */
  122. struct amd_nb *amd_nb;
  123. };
  124. #define __EVENT_CONSTRAINT(c, n, m, w) {\
  125. { .idxmsk64 = (n) }, \
  126. .code = (c), \
  127. .cmask = (m), \
  128. .weight = (w), \
  129. }
  130. #define EVENT_CONSTRAINT(c, n, m) \
  131. __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
  132. /*
  133. * Constraint on the Event code.
  134. */
  135. #define INTEL_EVENT_CONSTRAINT(c, n) \
  136. EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
  137. /*
  138. * Constraint on the Event code + UMask + fixed-mask
  139. *
  140. * filter mask to validate fixed counter events.
  141. * the following filters disqualify for fixed counters:
  142. * - inv
  143. * - edge
  144. * - cnt-mask
  145. * The other filters are supported by fixed counters.
  146. * The any-thread option is supported starting with v3.
  147. */
  148. #define FIXED_EVENT_CONSTRAINT(c, n) \
  149. EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
  150. /*
  151. * Constraint on the Event code + UMask
  152. */
  153. #define INTEL_UEVENT_CONSTRAINT(c, n) \
  154. EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
  155. #define EVENT_CONSTRAINT_END \
  156. EVENT_CONSTRAINT(0, 0, 0)
  157. #define for_each_event_constraint(e, c) \
  158. for ((e) = (c); (e)->weight; (e)++)
  159. /*
  160. * Extra registers for specific events.
  161. * Some events need large masks and require external MSRs.
  162. * Define a mapping to these extra registers.
  163. */
  164. struct extra_reg {
  165. unsigned int event;
  166. unsigned int msr;
  167. u64 config_mask;
  168. u64 valid_mask;
  169. };
  170. #define EVENT_EXTRA_REG(e, ms, m, vm) { \
  171. .event = (e), \
  172. .msr = (ms), \
  173. .config_mask = (m), \
  174. .valid_mask = (vm), \
  175. }
  176. #define INTEL_EVENT_EXTRA_REG(event, msr, vm) \
  177. EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm)
  178. #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0)
  179. union perf_capabilities {
  180. struct {
  181. u64 lbr_format : 6;
  182. u64 pebs_trap : 1;
  183. u64 pebs_arch_reg : 1;
  184. u64 pebs_format : 4;
  185. u64 smm_freeze : 1;
  186. };
  187. u64 capabilities;
  188. };
  189. /*
  190. * struct x86_pmu - generic x86 pmu
  191. */
  192. struct x86_pmu {
  193. /*
  194. * Generic x86 PMC bits
  195. */
  196. const char *name;
  197. int version;
  198. int (*handle_irq)(struct pt_regs *);
  199. void (*disable_all)(void);
  200. void (*enable_all)(int added);
  201. void (*enable)(struct perf_event *);
  202. void (*disable)(struct perf_event *);
  203. int (*hw_config)(struct perf_event *event);
  204. int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
  205. unsigned eventsel;
  206. unsigned perfctr;
  207. u64 (*event_map)(int);
  208. int max_events;
  209. int num_counters;
  210. int num_counters_fixed;
  211. int cntval_bits;
  212. u64 cntval_mask;
  213. int apic;
  214. u64 max_period;
  215. struct event_constraint *
  216. (*get_event_constraints)(struct cpu_hw_events *cpuc,
  217. struct perf_event *event);
  218. void (*put_event_constraints)(struct cpu_hw_events *cpuc,
  219. struct perf_event *event);
  220. struct event_constraint *event_constraints;
  221. struct event_constraint *percore_constraints;
  222. void (*quirks)(void);
  223. int perfctr_second_write;
  224. int (*cpu_prepare)(int cpu);
  225. void (*cpu_starting)(int cpu);
  226. void (*cpu_dying)(int cpu);
  227. void (*cpu_dead)(int cpu);
  228. /*
  229. * Intel Arch Perfmon v2+
  230. */
  231. u64 intel_ctrl;
  232. union perf_capabilities intel_cap;
  233. /*
  234. * Intel DebugStore bits
  235. */
  236. int bts, pebs;
  237. int bts_active, pebs_active;
  238. int pebs_record_size;
  239. void (*drain_pebs)(struct pt_regs *regs);
  240. struct event_constraint *pebs_constraints;
  241. /*
  242. * Intel LBR
  243. */
  244. unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
  245. int lbr_nr; /* hardware stack size */
  246. /*
  247. * Extra registers for events
  248. */
  249. struct extra_reg *extra_regs;
  250. };
  251. static struct x86_pmu x86_pmu __read_mostly;
  252. static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  253. .enabled = 1,
  254. };
  255. static int x86_perf_event_set_period(struct perf_event *event);
  256. /*
  257. * Generalized hw caching related hw_event table, filled
  258. * in on a per model basis. A value of 0 means
  259. * 'not supported', -1 means 'hw_event makes no sense on
  260. * this CPU', any other value means the raw hw_event
  261. * ID.
  262. */
  263. #define C(x) PERF_COUNT_HW_CACHE_##x
  264. static u64 __read_mostly hw_cache_event_ids
  265. [PERF_COUNT_HW_CACHE_MAX]
  266. [PERF_COUNT_HW_CACHE_OP_MAX]
  267. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  268. static u64 __read_mostly hw_cache_extra_regs
  269. [PERF_COUNT_HW_CACHE_MAX]
  270. [PERF_COUNT_HW_CACHE_OP_MAX]
  271. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  272. /*
  273. * Propagate event elapsed time into the generic event.
  274. * Can only be executed on the CPU where the event is active.
  275. * Returns the delta events processed.
  276. */
  277. static u64
  278. x86_perf_event_update(struct perf_event *event)
  279. {
  280. struct hw_perf_event *hwc = &event->hw;
  281. int shift = 64 - x86_pmu.cntval_bits;
  282. u64 prev_raw_count, new_raw_count;
  283. int idx = hwc->idx;
  284. s64 delta;
  285. if (idx == X86_PMC_IDX_FIXED_BTS)
  286. return 0;
  287. /*
  288. * Careful: an NMI might modify the previous event value.
  289. *
  290. * Our tactic to handle this is to first atomically read and
  291. * exchange a new raw count - then add that new-prev delta
  292. * count to the generic event atomically:
  293. */
  294. again:
  295. prev_raw_count = local64_read(&hwc->prev_count);
  296. rdmsrl(hwc->event_base, new_raw_count);
  297. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  298. new_raw_count) != prev_raw_count)
  299. goto again;
  300. /*
  301. * Now we have the new raw value and have updated the prev
  302. * timestamp already. We can now calculate the elapsed delta
  303. * (event-)time and add that to the generic event.
  304. *
  305. * Careful, not all hw sign-extends above the physical width
  306. * of the count.
  307. */
  308. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  309. delta >>= shift;
  310. local64_add(delta, &event->count);
  311. local64_sub(delta, &hwc->period_left);
  312. return new_raw_count;
  313. }
  314. /* using X86_FEATURE_PERFCTR_CORE to later implement ALTERNATIVE() here */
  315. static inline int x86_pmu_addr_offset(int index)
  316. {
  317. if (boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
  318. return index << 1;
  319. return index;
  320. }
  321. static inline unsigned int x86_pmu_config_addr(int index)
  322. {
  323. return x86_pmu.eventsel + x86_pmu_addr_offset(index);
  324. }
  325. static inline unsigned int x86_pmu_event_addr(int index)
  326. {
  327. return x86_pmu.perfctr + x86_pmu_addr_offset(index);
  328. }
  329. /*
  330. * Find and validate any extra registers to set up.
  331. */
  332. static int x86_pmu_extra_regs(u64 config, struct perf_event *event)
  333. {
  334. struct extra_reg *er;
  335. event->hw.extra_reg = 0;
  336. event->hw.extra_config = 0;
  337. if (!x86_pmu.extra_regs)
  338. return 0;
  339. for (er = x86_pmu.extra_regs; er->msr; er++) {
  340. if (er->event != (config & er->config_mask))
  341. continue;
  342. if (event->attr.config1 & ~er->valid_mask)
  343. return -EINVAL;
  344. event->hw.extra_reg = er->msr;
  345. event->hw.extra_config = event->attr.config1;
  346. break;
  347. }
  348. return 0;
  349. }
  350. static atomic_t active_events;
  351. static DEFINE_MUTEX(pmc_reserve_mutex);
  352. #ifdef CONFIG_X86_LOCAL_APIC
  353. static bool reserve_pmc_hardware(void)
  354. {
  355. int i;
  356. for (i = 0; i < x86_pmu.num_counters; i++) {
  357. if (!reserve_perfctr_nmi(x86_pmu_event_addr(i)))
  358. goto perfctr_fail;
  359. }
  360. for (i = 0; i < x86_pmu.num_counters; i++) {
  361. if (!reserve_evntsel_nmi(x86_pmu_config_addr(i)))
  362. goto eventsel_fail;
  363. }
  364. return true;
  365. eventsel_fail:
  366. for (i--; i >= 0; i--)
  367. release_evntsel_nmi(x86_pmu_config_addr(i));
  368. i = x86_pmu.num_counters;
  369. perfctr_fail:
  370. for (i--; i >= 0; i--)
  371. release_perfctr_nmi(x86_pmu_event_addr(i));
  372. return false;
  373. }
  374. static void release_pmc_hardware(void)
  375. {
  376. int i;
  377. for (i = 0; i < x86_pmu.num_counters; i++) {
  378. release_perfctr_nmi(x86_pmu_event_addr(i));
  379. release_evntsel_nmi(x86_pmu_config_addr(i));
  380. }
  381. }
  382. #else
  383. static bool reserve_pmc_hardware(void) { return true; }
  384. static void release_pmc_hardware(void) {}
  385. #endif
  386. static bool check_hw_exists(void)
  387. {
  388. u64 val, val_new = 0;
  389. int i, reg, ret = 0;
  390. /*
  391. * Check to see if the BIOS enabled any of the counters, if so
  392. * complain and bail.
  393. */
  394. for (i = 0; i < x86_pmu.num_counters; i++) {
  395. reg = x86_pmu_config_addr(i);
  396. ret = rdmsrl_safe(reg, &val);
  397. if (ret)
  398. goto msr_fail;
  399. if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
  400. goto bios_fail;
  401. }
  402. if (x86_pmu.num_counters_fixed) {
  403. reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  404. ret = rdmsrl_safe(reg, &val);
  405. if (ret)
  406. goto msr_fail;
  407. for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
  408. if (val & (0x03 << i*4))
  409. goto bios_fail;
  410. }
  411. }
  412. /*
  413. * Now write a value and read it back to see if it matches,
  414. * this is needed to detect certain hardware emulators (qemu/kvm)
  415. * that don't trap on the MSR access and always return 0s.
  416. */
  417. val = 0xabcdUL;
  418. ret = checking_wrmsrl(x86_pmu_event_addr(0), val);
  419. ret |= rdmsrl_safe(x86_pmu_event_addr(0), &val_new);
  420. if (ret || val != val_new)
  421. goto msr_fail;
  422. return true;
  423. bios_fail:
  424. /*
  425. * We still allow the PMU driver to operate:
  426. */
  427. printk(KERN_CONT "Broken BIOS detected, complain to your hardware vendor.\n");
  428. printk(KERN_ERR FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg, val);
  429. return true;
  430. msr_fail:
  431. printk(KERN_CONT "Broken PMU hardware detected, using software events only.\n");
  432. return false;
  433. }
  434. static void reserve_ds_buffers(void);
  435. static void release_ds_buffers(void);
  436. static void hw_perf_event_destroy(struct perf_event *event)
  437. {
  438. if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
  439. release_pmc_hardware();
  440. release_ds_buffers();
  441. mutex_unlock(&pmc_reserve_mutex);
  442. }
  443. }
  444. static inline int x86_pmu_initialized(void)
  445. {
  446. return x86_pmu.handle_irq != NULL;
  447. }
  448. static inline int
  449. set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)
  450. {
  451. struct perf_event_attr *attr = &event->attr;
  452. unsigned int cache_type, cache_op, cache_result;
  453. u64 config, val;
  454. config = attr->config;
  455. cache_type = (config >> 0) & 0xff;
  456. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  457. return -EINVAL;
  458. cache_op = (config >> 8) & 0xff;
  459. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  460. return -EINVAL;
  461. cache_result = (config >> 16) & 0xff;
  462. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  463. return -EINVAL;
  464. val = hw_cache_event_ids[cache_type][cache_op][cache_result];
  465. if (val == 0)
  466. return -ENOENT;
  467. if (val == -1)
  468. return -EINVAL;
  469. hwc->config |= val;
  470. attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result];
  471. return x86_pmu_extra_regs(val, event);
  472. }
  473. static int x86_setup_perfctr(struct perf_event *event)
  474. {
  475. struct perf_event_attr *attr = &event->attr;
  476. struct hw_perf_event *hwc = &event->hw;
  477. u64 config;
  478. if (!is_sampling_event(event)) {
  479. hwc->sample_period = x86_pmu.max_period;
  480. hwc->last_period = hwc->sample_period;
  481. local64_set(&hwc->period_left, hwc->sample_period);
  482. } else {
  483. /*
  484. * If we have a PMU initialized but no APIC
  485. * interrupts, we cannot sample hardware
  486. * events (user-space has to fall back and
  487. * sample via a hrtimer based software event):
  488. */
  489. if (!x86_pmu.apic)
  490. return -EOPNOTSUPP;
  491. }
  492. /*
  493. * Do not allow config1 (extended registers) to propagate,
  494. * there's no sane user-space generalization yet:
  495. */
  496. if (attr->type == PERF_TYPE_RAW)
  497. return 0;
  498. if (attr->type == PERF_TYPE_HW_CACHE)
  499. return set_ext_hw_attr(hwc, event);
  500. if (attr->config >= x86_pmu.max_events)
  501. return -EINVAL;
  502. /*
  503. * The generic map:
  504. */
  505. config = x86_pmu.event_map(attr->config);
  506. if (config == 0)
  507. return -ENOENT;
  508. if (config == -1LL)
  509. return -EINVAL;
  510. /*
  511. * Branch tracing:
  512. */
  513. if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
  514. (hwc->sample_period == 1)) {
  515. /* BTS is not supported by this architecture. */
  516. if (!x86_pmu.bts_active)
  517. return -EOPNOTSUPP;
  518. /* BTS is currently only allowed for user-mode. */
  519. if (!attr->exclude_kernel)
  520. return -EOPNOTSUPP;
  521. }
  522. hwc->config |= config;
  523. return 0;
  524. }
  525. static int x86_pmu_hw_config(struct perf_event *event)
  526. {
  527. if (event->attr.precise_ip) {
  528. int precise = 0;
  529. /* Support for constant skid */
  530. if (x86_pmu.pebs_active) {
  531. precise++;
  532. /* Support for IP fixup */
  533. if (x86_pmu.lbr_nr)
  534. precise++;
  535. }
  536. if (event->attr.precise_ip > precise)
  537. return -EOPNOTSUPP;
  538. }
  539. /*
  540. * Generate PMC IRQs:
  541. * (keep 'enabled' bit clear for now)
  542. */
  543. event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
  544. /*
  545. * Count user and OS events unless requested not to
  546. */
  547. if (!event->attr.exclude_user)
  548. event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
  549. if (!event->attr.exclude_kernel)
  550. event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
  551. if (event->attr.type == PERF_TYPE_RAW)
  552. event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
  553. return x86_setup_perfctr(event);
  554. }
  555. /*
  556. * Setup the hardware configuration for a given attr_type
  557. */
  558. static int __x86_pmu_event_init(struct perf_event *event)
  559. {
  560. int err;
  561. if (!x86_pmu_initialized())
  562. return -ENODEV;
  563. err = 0;
  564. if (!atomic_inc_not_zero(&active_events)) {
  565. mutex_lock(&pmc_reserve_mutex);
  566. if (atomic_read(&active_events) == 0) {
  567. if (!reserve_pmc_hardware())
  568. err = -EBUSY;
  569. else
  570. reserve_ds_buffers();
  571. }
  572. if (!err)
  573. atomic_inc(&active_events);
  574. mutex_unlock(&pmc_reserve_mutex);
  575. }
  576. if (err)
  577. return err;
  578. event->destroy = hw_perf_event_destroy;
  579. event->hw.idx = -1;
  580. event->hw.last_cpu = -1;
  581. event->hw.last_tag = ~0ULL;
  582. return x86_pmu.hw_config(event);
  583. }
  584. static void x86_pmu_disable_all(void)
  585. {
  586. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  587. int idx;
  588. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  589. u64 val;
  590. if (!test_bit(idx, cpuc->active_mask))
  591. continue;
  592. rdmsrl(x86_pmu_config_addr(idx), val);
  593. if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
  594. continue;
  595. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  596. wrmsrl(x86_pmu_config_addr(idx), val);
  597. }
  598. }
  599. static void x86_pmu_disable(struct pmu *pmu)
  600. {
  601. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  602. if (!x86_pmu_initialized())
  603. return;
  604. if (!cpuc->enabled)
  605. return;
  606. cpuc->n_added = 0;
  607. cpuc->enabled = 0;
  608. barrier();
  609. x86_pmu.disable_all();
  610. }
  611. static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
  612. u64 enable_mask)
  613. {
  614. if (hwc->extra_reg)
  615. wrmsrl(hwc->extra_reg, hwc->extra_config);
  616. wrmsrl(hwc->config_base, hwc->config | enable_mask);
  617. }
  618. static void x86_pmu_enable_all(int added)
  619. {
  620. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  621. int idx;
  622. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  623. struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
  624. if (!test_bit(idx, cpuc->active_mask))
  625. continue;
  626. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  627. }
  628. }
  629. static struct pmu pmu;
  630. static inline int is_x86_event(struct perf_event *event)
  631. {
  632. return event->pmu == &pmu;
  633. }
  634. static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  635. {
  636. struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
  637. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  638. int i, j, w, wmax, num = 0;
  639. struct hw_perf_event *hwc;
  640. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  641. for (i = 0; i < n; i++) {
  642. c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
  643. constraints[i] = c;
  644. }
  645. /*
  646. * fastpath, try to reuse previous register
  647. */
  648. for (i = 0; i < n; i++) {
  649. hwc = &cpuc->event_list[i]->hw;
  650. c = constraints[i];
  651. /* never assigned */
  652. if (hwc->idx == -1)
  653. break;
  654. /* constraint still honored */
  655. if (!test_bit(hwc->idx, c->idxmsk))
  656. break;
  657. /* not already used */
  658. if (test_bit(hwc->idx, used_mask))
  659. break;
  660. __set_bit(hwc->idx, used_mask);
  661. if (assign)
  662. assign[i] = hwc->idx;
  663. }
  664. if (i == n)
  665. goto done;
  666. /*
  667. * begin slow path
  668. */
  669. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  670. /*
  671. * weight = number of possible counters
  672. *
  673. * 1 = most constrained, only works on one counter
  674. * wmax = least constrained, works on any counter
  675. *
  676. * assign events to counters starting with most
  677. * constrained events.
  678. */
  679. wmax = x86_pmu.num_counters;
  680. /*
  681. * when fixed event counters are present,
  682. * wmax is incremented by 1 to account
  683. * for one more choice
  684. */
  685. if (x86_pmu.num_counters_fixed)
  686. wmax++;
  687. for (w = 1, num = n; num && w <= wmax; w++) {
  688. /* for each event */
  689. for (i = 0; num && i < n; i++) {
  690. c = constraints[i];
  691. hwc = &cpuc->event_list[i]->hw;
  692. if (c->weight != w)
  693. continue;
  694. for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) {
  695. if (!test_bit(j, used_mask))
  696. break;
  697. }
  698. if (j == X86_PMC_IDX_MAX)
  699. break;
  700. __set_bit(j, used_mask);
  701. if (assign)
  702. assign[i] = j;
  703. num--;
  704. }
  705. }
  706. done:
  707. /*
  708. * scheduling failed or is just a simulation,
  709. * free resources if necessary
  710. */
  711. if (!assign || num) {
  712. for (i = 0; i < n; i++) {
  713. if (x86_pmu.put_event_constraints)
  714. x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
  715. }
  716. }
  717. return num ? -ENOSPC : 0;
  718. }
  719. /*
  720. * dogrp: true if must collect siblings events (group)
  721. * returns total number of events and error code
  722. */
  723. static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
  724. {
  725. struct perf_event *event;
  726. int n, max_count;
  727. max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
  728. /* current number of events already accepted */
  729. n = cpuc->n_events;
  730. if (is_x86_event(leader)) {
  731. if (n >= max_count)
  732. return -ENOSPC;
  733. cpuc->event_list[n] = leader;
  734. n++;
  735. }
  736. if (!dogrp)
  737. return n;
  738. list_for_each_entry(event, &leader->sibling_list, group_entry) {
  739. if (!is_x86_event(event) ||
  740. event->state <= PERF_EVENT_STATE_OFF)
  741. continue;
  742. if (n >= max_count)
  743. return -ENOSPC;
  744. cpuc->event_list[n] = event;
  745. n++;
  746. }
  747. return n;
  748. }
  749. static inline void x86_assign_hw_event(struct perf_event *event,
  750. struct cpu_hw_events *cpuc, int i)
  751. {
  752. struct hw_perf_event *hwc = &event->hw;
  753. hwc->idx = cpuc->assign[i];
  754. hwc->last_cpu = smp_processor_id();
  755. hwc->last_tag = ++cpuc->tags[i];
  756. if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
  757. hwc->config_base = 0;
  758. hwc->event_base = 0;
  759. } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
  760. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  761. hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - X86_PMC_IDX_FIXED);
  762. } else {
  763. hwc->config_base = x86_pmu_config_addr(hwc->idx);
  764. hwc->event_base = x86_pmu_event_addr(hwc->idx);
  765. }
  766. }
  767. static inline int match_prev_assignment(struct hw_perf_event *hwc,
  768. struct cpu_hw_events *cpuc,
  769. int i)
  770. {
  771. return hwc->idx == cpuc->assign[i] &&
  772. hwc->last_cpu == smp_processor_id() &&
  773. hwc->last_tag == cpuc->tags[i];
  774. }
  775. static void x86_pmu_start(struct perf_event *event, int flags);
  776. static void x86_pmu_stop(struct perf_event *event, int flags);
  777. static void x86_pmu_enable(struct pmu *pmu)
  778. {
  779. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  780. struct perf_event *event;
  781. struct hw_perf_event *hwc;
  782. int i, added = cpuc->n_added;
  783. if (!x86_pmu_initialized())
  784. return;
  785. if (cpuc->enabled)
  786. return;
  787. if (cpuc->n_added) {
  788. int n_running = cpuc->n_events - cpuc->n_added;
  789. /*
  790. * apply assignment obtained either from
  791. * hw_perf_group_sched_in() or x86_pmu_enable()
  792. *
  793. * step1: save events moving to new counters
  794. * step2: reprogram moved events into new counters
  795. */
  796. for (i = 0; i < n_running; i++) {
  797. event = cpuc->event_list[i];
  798. hwc = &event->hw;
  799. /*
  800. * we can avoid reprogramming counter if:
  801. * - assigned same counter as last time
  802. * - running on same CPU as last time
  803. * - no other event has used the counter since
  804. */
  805. if (hwc->idx == -1 ||
  806. match_prev_assignment(hwc, cpuc, i))
  807. continue;
  808. /*
  809. * Ensure we don't accidentally enable a stopped
  810. * counter simply because we rescheduled.
  811. */
  812. if (hwc->state & PERF_HES_STOPPED)
  813. hwc->state |= PERF_HES_ARCH;
  814. x86_pmu_stop(event, PERF_EF_UPDATE);
  815. }
  816. for (i = 0; i < cpuc->n_events; i++) {
  817. event = cpuc->event_list[i];
  818. hwc = &event->hw;
  819. if (!match_prev_assignment(hwc, cpuc, i))
  820. x86_assign_hw_event(event, cpuc, i);
  821. else if (i < n_running)
  822. continue;
  823. if (hwc->state & PERF_HES_ARCH)
  824. continue;
  825. x86_pmu_start(event, PERF_EF_RELOAD);
  826. }
  827. cpuc->n_added = 0;
  828. perf_events_lapic_init();
  829. }
  830. cpuc->enabled = 1;
  831. barrier();
  832. x86_pmu.enable_all(added);
  833. }
  834. static inline void x86_pmu_disable_event(struct perf_event *event)
  835. {
  836. struct hw_perf_event *hwc = &event->hw;
  837. wrmsrl(hwc->config_base, hwc->config);
  838. }
  839. static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  840. /*
  841. * Set the next IRQ period, based on the hwc->period_left value.
  842. * To be called with the event disabled in hw:
  843. */
  844. static int
  845. x86_perf_event_set_period(struct perf_event *event)
  846. {
  847. struct hw_perf_event *hwc = &event->hw;
  848. s64 left = local64_read(&hwc->period_left);
  849. s64 period = hwc->sample_period;
  850. int ret = 0, idx = hwc->idx;
  851. if (idx == X86_PMC_IDX_FIXED_BTS)
  852. return 0;
  853. /*
  854. * If we are way outside a reasonable range then just skip forward:
  855. */
  856. if (unlikely(left <= -period)) {
  857. left = period;
  858. local64_set(&hwc->period_left, left);
  859. hwc->last_period = period;
  860. ret = 1;
  861. }
  862. if (unlikely(left <= 0)) {
  863. left += period;
  864. local64_set(&hwc->period_left, left);
  865. hwc->last_period = period;
  866. ret = 1;
  867. }
  868. /*
  869. * Quirk: certain CPUs dont like it if just 1 hw_event is left:
  870. */
  871. if (unlikely(left < 2))
  872. left = 2;
  873. if (left > x86_pmu.max_period)
  874. left = x86_pmu.max_period;
  875. per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
  876. /*
  877. * The hw event starts counting from this event offset,
  878. * mark it to be able to extra future deltas:
  879. */
  880. local64_set(&hwc->prev_count, (u64)-left);
  881. wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
  882. /*
  883. * Due to erratum on certan cpu we need
  884. * a second write to be sure the register
  885. * is updated properly
  886. */
  887. if (x86_pmu.perfctr_second_write) {
  888. wrmsrl(hwc->event_base,
  889. (u64)(-left) & x86_pmu.cntval_mask);
  890. }
  891. perf_event_update_userpage(event);
  892. return ret;
  893. }
  894. static void x86_pmu_enable_event(struct perf_event *event)
  895. {
  896. if (__this_cpu_read(cpu_hw_events.enabled))
  897. __x86_pmu_enable_event(&event->hw,
  898. ARCH_PERFMON_EVENTSEL_ENABLE);
  899. }
  900. /*
  901. * Add a single event to the PMU.
  902. *
  903. * The event is added to the group of enabled events
  904. * but only if it can be scehduled with existing events.
  905. */
  906. static int x86_pmu_add(struct perf_event *event, int flags)
  907. {
  908. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  909. struct hw_perf_event *hwc;
  910. int assign[X86_PMC_IDX_MAX];
  911. int n, n0, ret;
  912. hwc = &event->hw;
  913. perf_pmu_disable(event->pmu);
  914. n0 = cpuc->n_events;
  915. ret = n = collect_events(cpuc, event, false);
  916. if (ret < 0)
  917. goto out;
  918. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  919. if (!(flags & PERF_EF_START))
  920. hwc->state |= PERF_HES_ARCH;
  921. /*
  922. * If group events scheduling transaction was started,
  923. * skip the schedulability test here, it will be performed
  924. * at commit time (->commit_txn) as a whole
  925. */
  926. if (cpuc->group_flag & PERF_EVENT_TXN)
  927. goto done_collect;
  928. ret = x86_pmu.schedule_events(cpuc, n, assign);
  929. if (ret)
  930. goto out;
  931. /*
  932. * copy new assignment, now we know it is possible
  933. * will be used by hw_perf_enable()
  934. */
  935. memcpy(cpuc->assign, assign, n*sizeof(int));
  936. done_collect:
  937. cpuc->n_events = n;
  938. cpuc->n_added += n - n0;
  939. cpuc->n_txn += n - n0;
  940. ret = 0;
  941. out:
  942. perf_pmu_enable(event->pmu);
  943. return ret;
  944. }
  945. static void x86_pmu_start(struct perf_event *event, int flags)
  946. {
  947. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  948. int idx = event->hw.idx;
  949. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  950. return;
  951. if (WARN_ON_ONCE(idx == -1))
  952. return;
  953. if (flags & PERF_EF_RELOAD) {
  954. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  955. x86_perf_event_set_period(event);
  956. }
  957. event->hw.state = 0;
  958. cpuc->events[idx] = event;
  959. __set_bit(idx, cpuc->active_mask);
  960. __set_bit(idx, cpuc->running);
  961. x86_pmu.enable(event);
  962. perf_event_update_userpage(event);
  963. }
  964. void perf_event_print_debug(void)
  965. {
  966. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  967. u64 pebs;
  968. struct cpu_hw_events *cpuc;
  969. unsigned long flags;
  970. int cpu, idx;
  971. if (!x86_pmu.num_counters)
  972. return;
  973. local_irq_save(flags);
  974. cpu = smp_processor_id();
  975. cpuc = &per_cpu(cpu_hw_events, cpu);
  976. if (x86_pmu.version >= 2) {
  977. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  978. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  979. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  980. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  981. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  982. pr_info("\n");
  983. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  984. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  985. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  986. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  987. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  988. }
  989. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  990. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  991. rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
  992. rdmsrl(x86_pmu_event_addr(idx), pmc_count);
  993. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  994. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  995. cpu, idx, pmc_ctrl);
  996. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  997. cpu, idx, pmc_count);
  998. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  999. cpu, idx, prev_left);
  1000. }
  1001. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  1002. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  1003. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  1004. cpu, idx, pmc_count);
  1005. }
  1006. local_irq_restore(flags);
  1007. }
  1008. static void x86_pmu_stop(struct perf_event *event, int flags)
  1009. {
  1010. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1011. struct hw_perf_event *hwc = &event->hw;
  1012. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  1013. x86_pmu.disable(event);
  1014. cpuc->events[hwc->idx] = NULL;
  1015. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  1016. hwc->state |= PERF_HES_STOPPED;
  1017. }
  1018. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  1019. /*
  1020. * Drain the remaining delta count out of a event
  1021. * that we are disabling:
  1022. */
  1023. x86_perf_event_update(event);
  1024. hwc->state |= PERF_HES_UPTODATE;
  1025. }
  1026. }
  1027. static void x86_pmu_del(struct perf_event *event, int flags)
  1028. {
  1029. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1030. int i;
  1031. /*
  1032. * If we're called during a txn, we don't need to do anything.
  1033. * The events never got scheduled and ->cancel_txn will truncate
  1034. * the event_list.
  1035. */
  1036. if (cpuc->group_flag & PERF_EVENT_TXN)
  1037. return;
  1038. x86_pmu_stop(event, PERF_EF_UPDATE);
  1039. for (i = 0; i < cpuc->n_events; i++) {
  1040. if (event == cpuc->event_list[i]) {
  1041. if (x86_pmu.put_event_constraints)
  1042. x86_pmu.put_event_constraints(cpuc, event);
  1043. while (++i < cpuc->n_events)
  1044. cpuc->event_list[i-1] = cpuc->event_list[i];
  1045. --cpuc->n_events;
  1046. break;
  1047. }
  1048. }
  1049. perf_event_update_userpage(event);
  1050. }
  1051. static int x86_pmu_handle_irq(struct pt_regs *regs)
  1052. {
  1053. struct perf_sample_data data;
  1054. struct cpu_hw_events *cpuc;
  1055. struct perf_event *event;
  1056. int idx, handled = 0;
  1057. u64 val;
  1058. perf_sample_data_init(&data, 0);
  1059. cpuc = &__get_cpu_var(cpu_hw_events);
  1060. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1061. if (!test_bit(idx, cpuc->active_mask)) {
  1062. /*
  1063. * Though we deactivated the counter some cpus
  1064. * might still deliver spurious interrupts still
  1065. * in flight. Catch them:
  1066. */
  1067. if (__test_and_clear_bit(idx, cpuc->running))
  1068. handled++;
  1069. continue;
  1070. }
  1071. event = cpuc->events[idx];
  1072. val = x86_perf_event_update(event);
  1073. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  1074. continue;
  1075. /*
  1076. * event overflow
  1077. */
  1078. handled++;
  1079. data.period = event->hw.last_period;
  1080. if (!x86_perf_event_set_period(event))
  1081. continue;
  1082. if (perf_event_overflow(event, 1, &data, regs))
  1083. x86_pmu_stop(event, 0);
  1084. }
  1085. if (handled)
  1086. inc_irq_stat(apic_perf_irqs);
  1087. return handled;
  1088. }
  1089. void perf_events_lapic_init(void)
  1090. {
  1091. if (!x86_pmu.apic || !x86_pmu_initialized())
  1092. return;
  1093. /*
  1094. * Always use NMI for PMU
  1095. */
  1096. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1097. }
  1098. struct pmu_nmi_state {
  1099. unsigned int marked;
  1100. int handled;
  1101. };
  1102. static DEFINE_PER_CPU(struct pmu_nmi_state, pmu_nmi);
  1103. static int __kprobes
  1104. perf_event_nmi_handler(struct notifier_block *self,
  1105. unsigned long cmd, void *__args)
  1106. {
  1107. struct die_args *args = __args;
  1108. unsigned int this_nmi;
  1109. int handled;
  1110. if (!atomic_read(&active_events))
  1111. return NOTIFY_DONE;
  1112. switch (cmd) {
  1113. case DIE_NMI:
  1114. break;
  1115. case DIE_NMIUNKNOWN:
  1116. this_nmi = percpu_read(irq_stat.__nmi_count);
  1117. if (this_nmi != __this_cpu_read(pmu_nmi.marked))
  1118. /* let the kernel handle the unknown nmi */
  1119. return NOTIFY_DONE;
  1120. /*
  1121. * This one is a PMU back-to-back nmi. Two events
  1122. * trigger 'simultaneously' raising two back-to-back
  1123. * NMIs. If the first NMI handles both, the latter
  1124. * will be empty and daze the CPU. So, we drop it to
  1125. * avoid false-positive 'unknown nmi' messages.
  1126. */
  1127. return NOTIFY_STOP;
  1128. default:
  1129. return NOTIFY_DONE;
  1130. }
  1131. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1132. handled = x86_pmu.handle_irq(args->regs);
  1133. if (!handled)
  1134. return NOTIFY_DONE;
  1135. this_nmi = percpu_read(irq_stat.__nmi_count);
  1136. if ((handled > 1) ||
  1137. /* the next nmi could be a back-to-back nmi */
  1138. ((__this_cpu_read(pmu_nmi.marked) == this_nmi) &&
  1139. (__this_cpu_read(pmu_nmi.handled) > 1))) {
  1140. /*
  1141. * We could have two subsequent back-to-back nmis: The
  1142. * first handles more than one counter, the 2nd
  1143. * handles only one counter and the 3rd handles no
  1144. * counter.
  1145. *
  1146. * This is the 2nd nmi because the previous was
  1147. * handling more than one counter. We will mark the
  1148. * next (3rd) and then drop it if unhandled.
  1149. */
  1150. __this_cpu_write(pmu_nmi.marked, this_nmi + 1);
  1151. __this_cpu_write(pmu_nmi.handled, handled);
  1152. }
  1153. return NOTIFY_STOP;
  1154. }
  1155. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1156. .notifier_call = perf_event_nmi_handler,
  1157. .next = NULL,
  1158. .priority = NMI_LOCAL_LOW_PRIOR,
  1159. };
  1160. static struct event_constraint unconstrained;
  1161. static struct event_constraint emptyconstraint;
  1162. static struct event_constraint *
  1163. x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1164. {
  1165. struct event_constraint *c;
  1166. if (x86_pmu.event_constraints) {
  1167. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1168. if ((event->hw.config & c->cmask) == c->code)
  1169. return c;
  1170. }
  1171. }
  1172. return &unconstrained;
  1173. }
  1174. #include "perf_event_amd.c"
  1175. #include "perf_event_p6.c"
  1176. #include "perf_event_p4.c"
  1177. #include "perf_event_intel_lbr.c"
  1178. #include "perf_event_intel_ds.c"
  1179. #include "perf_event_intel.c"
  1180. static int __cpuinit
  1181. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1182. {
  1183. unsigned int cpu = (long)hcpu;
  1184. int ret = NOTIFY_OK;
  1185. switch (action & ~CPU_TASKS_FROZEN) {
  1186. case CPU_UP_PREPARE:
  1187. if (x86_pmu.cpu_prepare)
  1188. ret = x86_pmu.cpu_prepare(cpu);
  1189. break;
  1190. case CPU_STARTING:
  1191. if (x86_pmu.cpu_starting)
  1192. x86_pmu.cpu_starting(cpu);
  1193. break;
  1194. case CPU_DYING:
  1195. if (x86_pmu.cpu_dying)
  1196. x86_pmu.cpu_dying(cpu);
  1197. break;
  1198. case CPU_UP_CANCELED:
  1199. case CPU_DEAD:
  1200. if (x86_pmu.cpu_dead)
  1201. x86_pmu.cpu_dead(cpu);
  1202. break;
  1203. default:
  1204. break;
  1205. }
  1206. return ret;
  1207. }
  1208. static void __init pmu_check_apic(void)
  1209. {
  1210. if (cpu_has_apic)
  1211. return;
  1212. x86_pmu.apic = 0;
  1213. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1214. pr_info("no hardware sampling interrupt available.\n");
  1215. }
  1216. static int __init init_hw_perf_events(void)
  1217. {
  1218. struct event_constraint *c;
  1219. int err;
  1220. pr_info("Performance Events: ");
  1221. switch (boot_cpu_data.x86_vendor) {
  1222. case X86_VENDOR_INTEL:
  1223. err = intel_pmu_init();
  1224. break;
  1225. case X86_VENDOR_AMD:
  1226. err = amd_pmu_init();
  1227. break;
  1228. default:
  1229. return 0;
  1230. }
  1231. if (err != 0) {
  1232. pr_cont("no PMU driver, software events only.\n");
  1233. return 0;
  1234. }
  1235. pmu_check_apic();
  1236. /* sanity check that the hardware exists or is emulated */
  1237. if (!check_hw_exists())
  1238. return 0;
  1239. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1240. if (x86_pmu.quirks)
  1241. x86_pmu.quirks();
  1242. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  1243. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  1244. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  1245. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  1246. }
  1247. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  1248. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  1249. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  1250. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  1251. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  1252. }
  1253. x86_pmu.intel_ctrl |=
  1254. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  1255. perf_events_lapic_init();
  1256. register_die_notifier(&perf_event_nmi_notifier);
  1257. unconstrained = (struct event_constraint)
  1258. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  1259. 0, x86_pmu.num_counters);
  1260. if (x86_pmu.event_constraints) {
  1261. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1262. if (c->cmask != X86_RAW_EVENT_MASK)
  1263. continue;
  1264. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  1265. c->weight += x86_pmu.num_counters;
  1266. }
  1267. }
  1268. pr_info("... version: %d\n", x86_pmu.version);
  1269. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  1270. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  1271. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  1272. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1273. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  1274. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  1275. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1276. perf_cpu_notifier(x86_pmu_notifier);
  1277. return 0;
  1278. }
  1279. early_initcall(init_hw_perf_events);
  1280. static inline void x86_pmu_read(struct perf_event *event)
  1281. {
  1282. x86_perf_event_update(event);
  1283. }
  1284. /*
  1285. * Start group events scheduling transaction
  1286. * Set the flag to make pmu::enable() not perform the
  1287. * schedulability test, it will be performed at commit time
  1288. */
  1289. static void x86_pmu_start_txn(struct pmu *pmu)
  1290. {
  1291. perf_pmu_disable(pmu);
  1292. __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN);
  1293. __this_cpu_write(cpu_hw_events.n_txn, 0);
  1294. }
  1295. /*
  1296. * Stop group events scheduling transaction
  1297. * Clear the flag and pmu::enable() will perform the
  1298. * schedulability test.
  1299. */
  1300. static void x86_pmu_cancel_txn(struct pmu *pmu)
  1301. {
  1302. __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN);
  1303. /*
  1304. * Truncate the collected events.
  1305. */
  1306. __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
  1307. __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
  1308. perf_pmu_enable(pmu);
  1309. }
  1310. /*
  1311. * Commit group events scheduling transaction
  1312. * Perform the group schedulability test as a whole
  1313. * Return 0 if success
  1314. */
  1315. static int x86_pmu_commit_txn(struct pmu *pmu)
  1316. {
  1317. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1318. int assign[X86_PMC_IDX_MAX];
  1319. int n, ret;
  1320. n = cpuc->n_events;
  1321. if (!x86_pmu_initialized())
  1322. return -EAGAIN;
  1323. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1324. if (ret)
  1325. return ret;
  1326. /*
  1327. * copy new assignment, now we know it is possible
  1328. * will be used by hw_perf_enable()
  1329. */
  1330. memcpy(cpuc->assign, assign, n*sizeof(int));
  1331. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1332. perf_pmu_enable(pmu);
  1333. return 0;
  1334. }
  1335. /*
  1336. * validate that we can schedule this event
  1337. */
  1338. static int validate_event(struct perf_event *event)
  1339. {
  1340. struct cpu_hw_events *fake_cpuc;
  1341. struct event_constraint *c;
  1342. int ret = 0;
  1343. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1344. if (!fake_cpuc)
  1345. return -ENOMEM;
  1346. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1347. if (!c || !c->weight)
  1348. ret = -ENOSPC;
  1349. if (x86_pmu.put_event_constraints)
  1350. x86_pmu.put_event_constraints(fake_cpuc, event);
  1351. kfree(fake_cpuc);
  1352. return ret;
  1353. }
  1354. /*
  1355. * validate a single event group
  1356. *
  1357. * validation include:
  1358. * - check events are compatible which each other
  1359. * - events do not compete for the same counter
  1360. * - number of events <= number of counters
  1361. *
  1362. * validation ensures the group can be loaded onto the
  1363. * PMU if it was the only group available.
  1364. */
  1365. static int validate_group(struct perf_event *event)
  1366. {
  1367. struct perf_event *leader = event->group_leader;
  1368. struct cpu_hw_events *fake_cpuc;
  1369. int ret, n;
  1370. ret = -ENOMEM;
  1371. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1372. if (!fake_cpuc)
  1373. goto out;
  1374. /*
  1375. * the event is not yet connected with its
  1376. * siblings therefore we must first collect
  1377. * existing siblings, then add the new event
  1378. * before we can simulate the scheduling
  1379. */
  1380. ret = -ENOSPC;
  1381. n = collect_events(fake_cpuc, leader, true);
  1382. if (n < 0)
  1383. goto out_free;
  1384. fake_cpuc->n_events = n;
  1385. n = collect_events(fake_cpuc, event, false);
  1386. if (n < 0)
  1387. goto out_free;
  1388. fake_cpuc->n_events = n;
  1389. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1390. out_free:
  1391. kfree(fake_cpuc);
  1392. out:
  1393. return ret;
  1394. }
  1395. static int x86_pmu_event_init(struct perf_event *event)
  1396. {
  1397. struct pmu *tmp;
  1398. int err;
  1399. switch (event->attr.type) {
  1400. case PERF_TYPE_RAW:
  1401. case PERF_TYPE_HARDWARE:
  1402. case PERF_TYPE_HW_CACHE:
  1403. break;
  1404. default:
  1405. return -ENOENT;
  1406. }
  1407. err = __x86_pmu_event_init(event);
  1408. if (!err) {
  1409. /*
  1410. * we temporarily connect event to its pmu
  1411. * such that validate_group() can classify
  1412. * it as an x86 event using is_x86_event()
  1413. */
  1414. tmp = event->pmu;
  1415. event->pmu = &pmu;
  1416. if (event->group_leader != event)
  1417. err = validate_group(event);
  1418. else
  1419. err = validate_event(event);
  1420. event->pmu = tmp;
  1421. }
  1422. if (err) {
  1423. if (event->destroy)
  1424. event->destroy(event);
  1425. }
  1426. return err;
  1427. }
  1428. static struct pmu pmu = {
  1429. .pmu_enable = x86_pmu_enable,
  1430. .pmu_disable = x86_pmu_disable,
  1431. .event_init = x86_pmu_event_init,
  1432. .add = x86_pmu_add,
  1433. .del = x86_pmu_del,
  1434. .start = x86_pmu_start,
  1435. .stop = x86_pmu_stop,
  1436. .read = x86_pmu_read,
  1437. .start_txn = x86_pmu_start_txn,
  1438. .cancel_txn = x86_pmu_cancel_txn,
  1439. .commit_txn = x86_pmu_commit_txn,
  1440. };
  1441. /*
  1442. * callchain support
  1443. */
  1444. static void
  1445. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  1446. {
  1447. /* Ignore warnings */
  1448. }
  1449. static void backtrace_warning(void *data, char *msg)
  1450. {
  1451. /* Ignore warnings */
  1452. }
  1453. static int backtrace_stack(void *data, char *name)
  1454. {
  1455. return 0;
  1456. }
  1457. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1458. {
  1459. struct perf_callchain_entry *entry = data;
  1460. perf_callchain_store(entry, addr);
  1461. }
  1462. static const struct stacktrace_ops backtrace_ops = {
  1463. .warning = backtrace_warning,
  1464. .warning_symbol = backtrace_warning_symbol,
  1465. .stack = backtrace_stack,
  1466. .address = backtrace_address,
  1467. .walk_stack = print_context_stack_bp,
  1468. };
  1469. void
  1470. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1471. {
  1472. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1473. /* TODO: We don't support guest os callchain now */
  1474. return;
  1475. }
  1476. perf_callchain_store(entry, regs->ip);
  1477. dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
  1478. }
  1479. #ifdef CONFIG_COMPAT
  1480. static inline int
  1481. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1482. {
  1483. /* 32-bit process in 64-bit kernel. */
  1484. struct stack_frame_ia32 frame;
  1485. const void __user *fp;
  1486. if (!test_thread_flag(TIF_IA32))
  1487. return 0;
  1488. fp = compat_ptr(regs->bp);
  1489. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1490. unsigned long bytes;
  1491. frame.next_frame = 0;
  1492. frame.return_address = 0;
  1493. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1494. if (bytes != sizeof(frame))
  1495. break;
  1496. if (fp < compat_ptr(regs->sp))
  1497. break;
  1498. perf_callchain_store(entry, frame.return_address);
  1499. fp = compat_ptr(frame.next_frame);
  1500. }
  1501. return 1;
  1502. }
  1503. #else
  1504. static inline int
  1505. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1506. {
  1507. return 0;
  1508. }
  1509. #endif
  1510. void
  1511. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1512. {
  1513. struct stack_frame frame;
  1514. const void __user *fp;
  1515. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1516. /* TODO: We don't support guest os callchain now */
  1517. return;
  1518. }
  1519. fp = (void __user *)regs->bp;
  1520. perf_callchain_store(entry, regs->ip);
  1521. if (perf_callchain_user32(regs, entry))
  1522. return;
  1523. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1524. unsigned long bytes;
  1525. frame.next_frame = NULL;
  1526. frame.return_address = 0;
  1527. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1528. if (bytes != sizeof(frame))
  1529. break;
  1530. if ((unsigned long)fp < regs->sp)
  1531. break;
  1532. perf_callchain_store(entry, frame.return_address);
  1533. fp = frame.next_frame;
  1534. }
  1535. }
  1536. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1537. {
  1538. unsigned long ip;
  1539. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1540. ip = perf_guest_cbs->get_guest_ip();
  1541. else
  1542. ip = instruction_pointer(regs);
  1543. return ip;
  1544. }
  1545. unsigned long perf_misc_flags(struct pt_regs *regs)
  1546. {
  1547. int misc = 0;
  1548. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1549. if (perf_guest_cbs->is_user_mode())
  1550. misc |= PERF_RECORD_MISC_GUEST_USER;
  1551. else
  1552. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1553. } else {
  1554. if (user_mode(regs))
  1555. misc |= PERF_RECORD_MISC_USER;
  1556. else
  1557. misc |= PERF_RECORD_MISC_KERNEL;
  1558. }
  1559. if (regs->flags & PERF_EFLAGS_EXACT)
  1560. misc |= PERF_RECORD_MISC_EXACT_IP;
  1561. return misc;
  1562. }