perf_event.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  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. printk(KERN_CONT "Broken BIOS detected, using software events only.\n");
  425. printk(KERN_ERR FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg, val);
  426. return false;
  427. msr_fail:
  428. printk(KERN_CONT "Broken PMU hardware detected, using software events only.\n");
  429. return false;
  430. }
  431. static void reserve_ds_buffers(void);
  432. static void release_ds_buffers(void);
  433. static void hw_perf_event_destroy(struct perf_event *event)
  434. {
  435. if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
  436. release_pmc_hardware();
  437. release_ds_buffers();
  438. mutex_unlock(&pmc_reserve_mutex);
  439. }
  440. }
  441. static inline int x86_pmu_initialized(void)
  442. {
  443. return x86_pmu.handle_irq != NULL;
  444. }
  445. static inline int
  446. set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)
  447. {
  448. struct perf_event_attr *attr = &event->attr;
  449. unsigned int cache_type, cache_op, cache_result;
  450. u64 config, val;
  451. config = attr->config;
  452. cache_type = (config >> 0) & 0xff;
  453. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  454. return -EINVAL;
  455. cache_op = (config >> 8) & 0xff;
  456. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  457. return -EINVAL;
  458. cache_result = (config >> 16) & 0xff;
  459. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  460. return -EINVAL;
  461. val = hw_cache_event_ids[cache_type][cache_op][cache_result];
  462. if (val == 0)
  463. return -ENOENT;
  464. if (val == -1)
  465. return -EINVAL;
  466. hwc->config |= val;
  467. attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result];
  468. return x86_pmu_extra_regs(val, event);
  469. }
  470. static int x86_setup_perfctr(struct perf_event *event)
  471. {
  472. struct perf_event_attr *attr = &event->attr;
  473. struct hw_perf_event *hwc = &event->hw;
  474. u64 config;
  475. if (!is_sampling_event(event)) {
  476. hwc->sample_period = x86_pmu.max_period;
  477. hwc->last_period = hwc->sample_period;
  478. local64_set(&hwc->period_left, hwc->sample_period);
  479. } else {
  480. /*
  481. * If we have a PMU initialized but no APIC
  482. * interrupts, we cannot sample hardware
  483. * events (user-space has to fall back and
  484. * sample via a hrtimer based software event):
  485. */
  486. if (!x86_pmu.apic)
  487. return -EOPNOTSUPP;
  488. }
  489. if (attr->type == PERF_TYPE_RAW)
  490. return x86_pmu_extra_regs(event->attr.config, event);
  491. if (attr->type == PERF_TYPE_HW_CACHE)
  492. return set_ext_hw_attr(hwc, event);
  493. if (attr->config >= x86_pmu.max_events)
  494. return -EINVAL;
  495. /*
  496. * The generic map:
  497. */
  498. config = x86_pmu.event_map(attr->config);
  499. if (config == 0)
  500. return -ENOENT;
  501. if (config == -1LL)
  502. return -EINVAL;
  503. /*
  504. * Branch tracing:
  505. */
  506. if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
  507. (hwc->sample_period == 1)) {
  508. /* BTS is not supported by this architecture. */
  509. if (!x86_pmu.bts_active)
  510. return -EOPNOTSUPP;
  511. /* BTS is currently only allowed for user-mode. */
  512. if (!attr->exclude_kernel)
  513. return -EOPNOTSUPP;
  514. }
  515. hwc->config |= config;
  516. return 0;
  517. }
  518. static int x86_pmu_hw_config(struct perf_event *event)
  519. {
  520. if (event->attr.precise_ip) {
  521. int precise = 0;
  522. /* Support for constant skid */
  523. if (x86_pmu.pebs_active) {
  524. precise++;
  525. /* Support for IP fixup */
  526. if (x86_pmu.lbr_nr)
  527. precise++;
  528. }
  529. if (event->attr.precise_ip > precise)
  530. return -EOPNOTSUPP;
  531. }
  532. /*
  533. * Generate PMC IRQs:
  534. * (keep 'enabled' bit clear for now)
  535. */
  536. event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
  537. /*
  538. * Count user and OS events unless requested not to
  539. */
  540. if (!event->attr.exclude_user)
  541. event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
  542. if (!event->attr.exclude_kernel)
  543. event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
  544. if (event->attr.type == PERF_TYPE_RAW)
  545. event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
  546. return x86_setup_perfctr(event);
  547. }
  548. /*
  549. * Setup the hardware configuration for a given attr_type
  550. */
  551. static int __x86_pmu_event_init(struct perf_event *event)
  552. {
  553. int err;
  554. if (!x86_pmu_initialized())
  555. return -ENODEV;
  556. err = 0;
  557. if (!atomic_inc_not_zero(&active_events)) {
  558. mutex_lock(&pmc_reserve_mutex);
  559. if (atomic_read(&active_events) == 0) {
  560. if (!reserve_pmc_hardware())
  561. err = -EBUSY;
  562. else
  563. reserve_ds_buffers();
  564. }
  565. if (!err)
  566. atomic_inc(&active_events);
  567. mutex_unlock(&pmc_reserve_mutex);
  568. }
  569. if (err)
  570. return err;
  571. event->destroy = hw_perf_event_destroy;
  572. event->hw.idx = -1;
  573. event->hw.last_cpu = -1;
  574. event->hw.last_tag = ~0ULL;
  575. return x86_pmu.hw_config(event);
  576. }
  577. static void x86_pmu_disable_all(void)
  578. {
  579. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  580. int idx;
  581. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  582. u64 val;
  583. if (!test_bit(idx, cpuc->active_mask))
  584. continue;
  585. rdmsrl(x86_pmu_config_addr(idx), val);
  586. if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
  587. continue;
  588. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  589. wrmsrl(x86_pmu_config_addr(idx), val);
  590. }
  591. }
  592. static void x86_pmu_disable(struct pmu *pmu)
  593. {
  594. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  595. if (!x86_pmu_initialized())
  596. return;
  597. if (!cpuc->enabled)
  598. return;
  599. cpuc->n_added = 0;
  600. cpuc->enabled = 0;
  601. barrier();
  602. x86_pmu.disable_all();
  603. }
  604. static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
  605. u64 enable_mask)
  606. {
  607. if (hwc->extra_reg)
  608. wrmsrl(hwc->extra_reg, hwc->extra_config);
  609. wrmsrl(hwc->config_base, hwc->config | enable_mask);
  610. }
  611. static void x86_pmu_enable_all(int added)
  612. {
  613. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  614. int idx;
  615. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  616. struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
  617. if (!test_bit(idx, cpuc->active_mask))
  618. continue;
  619. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  620. }
  621. }
  622. static struct pmu pmu;
  623. static inline int is_x86_event(struct perf_event *event)
  624. {
  625. return event->pmu == &pmu;
  626. }
  627. static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  628. {
  629. struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
  630. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  631. int i, j, w, wmax, num = 0;
  632. struct hw_perf_event *hwc;
  633. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  634. for (i = 0; i < n; i++) {
  635. c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
  636. constraints[i] = c;
  637. }
  638. /*
  639. * fastpath, try to reuse previous register
  640. */
  641. for (i = 0; i < n; i++) {
  642. hwc = &cpuc->event_list[i]->hw;
  643. c = constraints[i];
  644. /* never assigned */
  645. if (hwc->idx == -1)
  646. break;
  647. /* constraint still honored */
  648. if (!test_bit(hwc->idx, c->idxmsk))
  649. break;
  650. /* not already used */
  651. if (test_bit(hwc->idx, used_mask))
  652. break;
  653. __set_bit(hwc->idx, used_mask);
  654. if (assign)
  655. assign[i] = hwc->idx;
  656. }
  657. if (i == n)
  658. goto done;
  659. /*
  660. * begin slow path
  661. */
  662. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  663. /*
  664. * weight = number of possible counters
  665. *
  666. * 1 = most constrained, only works on one counter
  667. * wmax = least constrained, works on any counter
  668. *
  669. * assign events to counters starting with most
  670. * constrained events.
  671. */
  672. wmax = x86_pmu.num_counters;
  673. /*
  674. * when fixed event counters are present,
  675. * wmax is incremented by 1 to account
  676. * for one more choice
  677. */
  678. if (x86_pmu.num_counters_fixed)
  679. wmax++;
  680. for (w = 1, num = n; num && w <= wmax; w++) {
  681. /* for each event */
  682. for (i = 0; num && i < n; i++) {
  683. c = constraints[i];
  684. hwc = &cpuc->event_list[i]->hw;
  685. if (c->weight != w)
  686. continue;
  687. for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) {
  688. if (!test_bit(j, used_mask))
  689. break;
  690. }
  691. if (j == X86_PMC_IDX_MAX)
  692. break;
  693. __set_bit(j, used_mask);
  694. if (assign)
  695. assign[i] = j;
  696. num--;
  697. }
  698. }
  699. done:
  700. /*
  701. * scheduling failed or is just a simulation,
  702. * free resources if necessary
  703. */
  704. if (!assign || num) {
  705. for (i = 0; i < n; i++) {
  706. if (x86_pmu.put_event_constraints)
  707. x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
  708. }
  709. }
  710. return num ? -ENOSPC : 0;
  711. }
  712. /*
  713. * dogrp: true if must collect siblings events (group)
  714. * returns total number of events and error code
  715. */
  716. static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
  717. {
  718. struct perf_event *event;
  719. int n, max_count;
  720. max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
  721. /* current number of events already accepted */
  722. n = cpuc->n_events;
  723. if (is_x86_event(leader)) {
  724. if (n >= max_count)
  725. return -ENOSPC;
  726. cpuc->event_list[n] = leader;
  727. n++;
  728. }
  729. if (!dogrp)
  730. return n;
  731. list_for_each_entry(event, &leader->sibling_list, group_entry) {
  732. if (!is_x86_event(event) ||
  733. event->state <= PERF_EVENT_STATE_OFF)
  734. continue;
  735. if (n >= max_count)
  736. return -ENOSPC;
  737. cpuc->event_list[n] = event;
  738. n++;
  739. }
  740. return n;
  741. }
  742. static inline void x86_assign_hw_event(struct perf_event *event,
  743. struct cpu_hw_events *cpuc, int i)
  744. {
  745. struct hw_perf_event *hwc = &event->hw;
  746. hwc->idx = cpuc->assign[i];
  747. hwc->last_cpu = smp_processor_id();
  748. hwc->last_tag = ++cpuc->tags[i];
  749. if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
  750. hwc->config_base = 0;
  751. hwc->event_base = 0;
  752. } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
  753. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  754. hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0;
  755. } else {
  756. hwc->config_base = x86_pmu_config_addr(hwc->idx);
  757. hwc->event_base = x86_pmu_event_addr(hwc->idx);
  758. }
  759. }
  760. static inline int match_prev_assignment(struct hw_perf_event *hwc,
  761. struct cpu_hw_events *cpuc,
  762. int i)
  763. {
  764. return hwc->idx == cpuc->assign[i] &&
  765. hwc->last_cpu == smp_processor_id() &&
  766. hwc->last_tag == cpuc->tags[i];
  767. }
  768. static void x86_pmu_start(struct perf_event *event, int flags);
  769. static void x86_pmu_stop(struct perf_event *event, int flags);
  770. static void x86_pmu_enable(struct pmu *pmu)
  771. {
  772. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  773. struct perf_event *event;
  774. struct hw_perf_event *hwc;
  775. int i, added = cpuc->n_added;
  776. if (!x86_pmu_initialized())
  777. return;
  778. if (cpuc->enabled)
  779. return;
  780. if (cpuc->n_added) {
  781. int n_running = cpuc->n_events - cpuc->n_added;
  782. /*
  783. * apply assignment obtained either from
  784. * hw_perf_group_sched_in() or x86_pmu_enable()
  785. *
  786. * step1: save events moving to new counters
  787. * step2: reprogram moved events into new counters
  788. */
  789. for (i = 0; i < n_running; i++) {
  790. event = cpuc->event_list[i];
  791. hwc = &event->hw;
  792. /*
  793. * we can avoid reprogramming counter if:
  794. * - assigned same counter as last time
  795. * - running on same CPU as last time
  796. * - no other event has used the counter since
  797. */
  798. if (hwc->idx == -1 ||
  799. match_prev_assignment(hwc, cpuc, i))
  800. continue;
  801. /*
  802. * Ensure we don't accidentally enable a stopped
  803. * counter simply because we rescheduled.
  804. */
  805. if (hwc->state & PERF_HES_STOPPED)
  806. hwc->state |= PERF_HES_ARCH;
  807. x86_pmu_stop(event, PERF_EF_UPDATE);
  808. }
  809. for (i = 0; i < cpuc->n_events; i++) {
  810. event = cpuc->event_list[i];
  811. hwc = &event->hw;
  812. if (!match_prev_assignment(hwc, cpuc, i))
  813. x86_assign_hw_event(event, cpuc, i);
  814. else if (i < n_running)
  815. continue;
  816. if (hwc->state & PERF_HES_ARCH)
  817. continue;
  818. x86_pmu_start(event, PERF_EF_RELOAD);
  819. }
  820. cpuc->n_added = 0;
  821. perf_events_lapic_init();
  822. }
  823. cpuc->enabled = 1;
  824. barrier();
  825. x86_pmu.enable_all(added);
  826. }
  827. static inline void x86_pmu_disable_event(struct perf_event *event)
  828. {
  829. struct hw_perf_event *hwc = &event->hw;
  830. wrmsrl(hwc->config_base, hwc->config);
  831. }
  832. static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  833. /*
  834. * Set the next IRQ period, based on the hwc->period_left value.
  835. * To be called with the event disabled in hw:
  836. */
  837. static int
  838. x86_perf_event_set_period(struct perf_event *event)
  839. {
  840. struct hw_perf_event *hwc = &event->hw;
  841. s64 left = local64_read(&hwc->period_left);
  842. s64 period = hwc->sample_period;
  843. int ret = 0, idx = hwc->idx;
  844. if (idx == X86_PMC_IDX_FIXED_BTS)
  845. return 0;
  846. /*
  847. * If we are way outside a reasonable range then just skip forward:
  848. */
  849. if (unlikely(left <= -period)) {
  850. left = period;
  851. local64_set(&hwc->period_left, left);
  852. hwc->last_period = period;
  853. ret = 1;
  854. }
  855. if (unlikely(left <= 0)) {
  856. left += period;
  857. local64_set(&hwc->period_left, left);
  858. hwc->last_period = period;
  859. ret = 1;
  860. }
  861. /*
  862. * Quirk: certain CPUs dont like it if just 1 hw_event is left:
  863. */
  864. if (unlikely(left < 2))
  865. left = 2;
  866. if (left > x86_pmu.max_period)
  867. left = x86_pmu.max_period;
  868. per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
  869. /*
  870. * The hw event starts counting from this event offset,
  871. * mark it to be able to extra future deltas:
  872. */
  873. local64_set(&hwc->prev_count, (u64)-left);
  874. wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
  875. /*
  876. * Due to erratum on certan cpu we need
  877. * a second write to be sure the register
  878. * is updated properly
  879. */
  880. if (x86_pmu.perfctr_second_write) {
  881. wrmsrl(hwc->event_base,
  882. (u64)(-left) & x86_pmu.cntval_mask);
  883. }
  884. perf_event_update_userpage(event);
  885. return ret;
  886. }
  887. static void x86_pmu_enable_event(struct perf_event *event)
  888. {
  889. if (__this_cpu_read(cpu_hw_events.enabled))
  890. __x86_pmu_enable_event(&event->hw,
  891. ARCH_PERFMON_EVENTSEL_ENABLE);
  892. }
  893. /*
  894. * Add a single event to the PMU.
  895. *
  896. * The event is added to the group of enabled events
  897. * but only if it can be scehduled with existing events.
  898. */
  899. static int x86_pmu_add(struct perf_event *event, int flags)
  900. {
  901. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  902. struct hw_perf_event *hwc;
  903. int assign[X86_PMC_IDX_MAX];
  904. int n, n0, ret;
  905. hwc = &event->hw;
  906. perf_pmu_disable(event->pmu);
  907. n0 = cpuc->n_events;
  908. ret = n = collect_events(cpuc, event, false);
  909. if (ret < 0)
  910. goto out;
  911. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  912. if (!(flags & PERF_EF_START))
  913. hwc->state |= PERF_HES_ARCH;
  914. /*
  915. * If group events scheduling transaction was started,
  916. * skip the schedulability test here, it will be performed
  917. * at commit time (->commit_txn) as a whole
  918. */
  919. if (cpuc->group_flag & PERF_EVENT_TXN)
  920. goto done_collect;
  921. ret = x86_pmu.schedule_events(cpuc, n, assign);
  922. if (ret)
  923. goto out;
  924. /*
  925. * copy new assignment, now we know it is possible
  926. * will be used by hw_perf_enable()
  927. */
  928. memcpy(cpuc->assign, assign, n*sizeof(int));
  929. done_collect:
  930. cpuc->n_events = n;
  931. cpuc->n_added += n - n0;
  932. cpuc->n_txn += n - n0;
  933. ret = 0;
  934. out:
  935. perf_pmu_enable(event->pmu);
  936. return ret;
  937. }
  938. static void x86_pmu_start(struct perf_event *event, int flags)
  939. {
  940. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  941. int idx = event->hw.idx;
  942. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  943. return;
  944. if (WARN_ON_ONCE(idx == -1))
  945. return;
  946. if (flags & PERF_EF_RELOAD) {
  947. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  948. x86_perf_event_set_period(event);
  949. }
  950. event->hw.state = 0;
  951. cpuc->events[idx] = event;
  952. __set_bit(idx, cpuc->active_mask);
  953. __set_bit(idx, cpuc->running);
  954. x86_pmu.enable(event);
  955. perf_event_update_userpage(event);
  956. }
  957. void perf_event_print_debug(void)
  958. {
  959. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  960. u64 pebs;
  961. struct cpu_hw_events *cpuc;
  962. unsigned long flags;
  963. int cpu, idx;
  964. if (!x86_pmu.num_counters)
  965. return;
  966. local_irq_save(flags);
  967. cpu = smp_processor_id();
  968. cpuc = &per_cpu(cpu_hw_events, cpu);
  969. if (x86_pmu.version >= 2) {
  970. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  971. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  972. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  973. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  974. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  975. pr_info("\n");
  976. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  977. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  978. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  979. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  980. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  981. }
  982. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  983. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  984. rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
  985. rdmsrl(x86_pmu_event_addr(idx), pmc_count);
  986. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  987. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  988. cpu, idx, pmc_ctrl);
  989. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  990. cpu, idx, pmc_count);
  991. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  992. cpu, idx, prev_left);
  993. }
  994. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  995. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  996. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  997. cpu, idx, pmc_count);
  998. }
  999. local_irq_restore(flags);
  1000. }
  1001. static void x86_pmu_stop(struct perf_event *event, int flags)
  1002. {
  1003. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1004. struct hw_perf_event *hwc = &event->hw;
  1005. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  1006. x86_pmu.disable(event);
  1007. cpuc->events[hwc->idx] = NULL;
  1008. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  1009. hwc->state |= PERF_HES_STOPPED;
  1010. }
  1011. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  1012. /*
  1013. * Drain the remaining delta count out of a event
  1014. * that we are disabling:
  1015. */
  1016. x86_perf_event_update(event);
  1017. hwc->state |= PERF_HES_UPTODATE;
  1018. }
  1019. }
  1020. static void x86_pmu_del(struct perf_event *event, int flags)
  1021. {
  1022. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1023. int i;
  1024. /*
  1025. * If we're called during a txn, we don't need to do anything.
  1026. * The events never got scheduled and ->cancel_txn will truncate
  1027. * the event_list.
  1028. */
  1029. if (cpuc->group_flag & PERF_EVENT_TXN)
  1030. return;
  1031. x86_pmu_stop(event, PERF_EF_UPDATE);
  1032. for (i = 0; i < cpuc->n_events; i++) {
  1033. if (event == cpuc->event_list[i]) {
  1034. if (x86_pmu.put_event_constraints)
  1035. x86_pmu.put_event_constraints(cpuc, event);
  1036. while (++i < cpuc->n_events)
  1037. cpuc->event_list[i-1] = cpuc->event_list[i];
  1038. --cpuc->n_events;
  1039. break;
  1040. }
  1041. }
  1042. perf_event_update_userpage(event);
  1043. }
  1044. static int x86_pmu_handle_irq(struct pt_regs *regs)
  1045. {
  1046. struct perf_sample_data data;
  1047. struct cpu_hw_events *cpuc;
  1048. struct perf_event *event;
  1049. int idx, handled = 0;
  1050. u64 val;
  1051. perf_sample_data_init(&data, 0);
  1052. cpuc = &__get_cpu_var(cpu_hw_events);
  1053. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1054. if (!test_bit(idx, cpuc->active_mask)) {
  1055. /*
  1056. * Though we deactivated the counter some cpus
  1057. * might still deliver spurious interrupts still
  1058. * in flight. Catch them:
  1059. */
  1060. if (__test_and_clear_bit(idx, cpuc->running))
  1061. handled++;
  1062. continue;
  1063. }
  1064. event = cpuc->events[idx];
  1065. val = x86_perf_event_update(event);
  1066. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  1067. continue;
  1068. /*
  1069. * event overflow
  1070. */
  1071. handled++;
  1072. data.period = event->hw.last_period;
  1073. if (!x86_perf_event_set_period(event))
  1074. continue;
  1075. if (perf_event_overflow(event, 1, &data, regs))
  1076. x86_pmu_stop(event, 0);
  1077. }
  1078. if (handled)
  1079. inc_irq_stat(apic_perf_irqs);
  1080. return handled;
  1081. }
  1082. void perf_events_lapic_init(void)
  1083. {
  1084. if (!x86_pmu.apic || !x86_pmu_initialized())
  1085. return;
  1086. /*
  1087. * Always use NMI for PMU
  1088. */
  1089. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1090. }
  1091. struct pmu_nmi_state {
  1092. unsigned int marked;
  1093. int handled;
  1094. };
  1095. static DEFINE_PER_CPU(struct pmu_nmi_state, pmu_nmi);
  1096. static int __kprobes
  1097. perf_event_nmi_handler(struct notifier_block *self,
  1098. unsigned long cmd, void *__args)
  1099. {
  1100. struct die_args *args = __args;
  1101. unsigned int this_nmi;
  1102. int handled;
  1103. if (!atomic_read(&active_events))
  1104. return NOTIFY_DONE;
  1105. switch (cmd) {
  1106. case DIE_NMI:
  1107. break;
  1108. case DIE_NMIUNKNOWN:
  1109. this_nmi = percpu_read(irq_stat.__nmi_count);
  1110. if (this_nmi != __this_cpu_read(pmu_nmi.marked))
  1111. /* let the kernel handle the unknown nmi */
  1112. return NOTIFY_DONE;
  1113. /*
  1114. * This one is a PMU back-to-back nmi. Two events
  1115. * trigger 'simultaneously' raising two back-to-back
  1116. * NMIs. If the first NMI handles both, the latter
  1117. * will be empty and daze the CPU. So, we drop it to
  1118. * avoid false-positive 'unknown nmi' messages.
  1119. */
  1120. return NOTIFY_STOP;
  1121. default:
  1122. return NOTIFY_DONE;
  1123. }
  1124. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1125. handled = x86_pmu.handle_irq(args->regs);
  1126. if (!handled)
  1127. return NOTIFY_DONE;
  1128. this_nmi = percpu_read(irq_stat.__nmi_count);
  1129. if ((handled > 1) ||
  1130. /* the next nmi could be a back-to-back nmi */
  1131. ((__this_cpu_read(pmu_nmi.marked) == this_nmi) &&
  1132. (__this_cpu_read(pmu_nmi.handled) > 1))) {
  1133. /*
  1134. * We could have two subsequent back-to-back nmis: The
  1135. * first handles more than one counter, the 2nd
  1136. * handles only one counter and the 3rd handles no
  1137. * counter.
  1138. *
  1139. * This is the 2nd nmi because the previous was
  1140. * handling more than one counter. We will mark the
  1141. * next (3rd) and then drop it if unhandled.
  1142. */
  1143. __this_cpu_write(pmu_nmi.marked, this_nmi + 1);
  1144. __this_cpu_write(pmu_nmi.handled, handled);
  1145. }
  1146. return NOTIFY_STOP;
  1147. }
  1148. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1149. .notifier_call = perf_event_nmi_handler,
  1150. .next = NULL,
  1151. .priority = NMI_LOCAL_LOW_PRIOR,
  1152. };
  1153. static struct event_constraint unconstrained;
  1154. static struct event_constraint emptyconstraint;
  1155. static struct event_constraint *
  1156. x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1157. {
  1158. struct event_constraint *c;
  1159. if (x86_pmu.event_constraints) {
  1160. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1161. if ((event->hw.config & c->cmask) == c->code)
  1162. return c;
  1163. }
  1164. }
  1165. return &unconstrained;
  1166. }
  1167. #include "perf_event_amd.c"
  1168. #include "perf_event_p6.c"
  1169. #include "perf_event_p4.c"
  1170. #include "perf_event_intel_lbr.c"
  1171. #include "perf_event_intel_ds.c"
  1172. #include "perf_event_intel.c"
  1173. static int __cpuinit
  1174. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1175. {
  1176. unsigned int cpu = (long)hcpu;
  1177. int ret = NOTIFY_OK;
  1178. switch (action & ~CPU_TASKS_FROZEN) {
  1179. case CPU_UP_PREPARE:
  1180. if (x86_pmu.cpu_prepare)
  1181. ret = x86_pmu.cpu_prepare(cpu);
  1182. break;
  1183. case CPU_STARTING:
  1184. if (x86_pmu.cpu_starting)
  1185. x86_pmu.cpu_starting(cpu);
  1186. break;
  1187. case CPU_DYING:
  1188. if (x86_pmu.cpu_dying)
  1189. x86_pmu.cpu_dying(cpu);
  1190. break;
  1191. case CPU_UP_CANCELED:
  1192. case CPU_DEAD:
  1193. if (x86_pmu.cpu_dead)
  1194. x86_pmu.cpu_dead(cpu);
  1195. break;
  1196. default:
  1197. break;
  1198. }
  1199. return ret;
  1200. }
  1201. static void __init pmu_check_apic(void)
  1202. {
  1203. if (cpu_has_apic)
  1204. return;
  1205. x86_pmu.apic = 0;
  1206. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1207. pr_info("no hardware sampling interrupt available.\n");
  1208. }
  1209. static int __init init_hw_perf_events(void)
  1210. {
  1211. struct event_constraint *c;
  1212. int err;
  1213. pr_info("Performance Events: ");
  1214. switch (boot_cpu_data.x86_vendor) {
  1215. case X86_VENDOR_INTEL:
  1216. err = intel_pmu_init();
  1217. break;
  1218. case X86_VENDOR_AMD:
  1219. err = amd_pmu_init();
  1220. break;
  1221. default:
  1222. return 0;
  1223. }
  1224. if (err != 0) {
  1225. pr_cont("no PMU driver, software events only.\n");
  1226. return 0;
  1227. }
  1228. pmu_check_apic();
  1229. /* sanity check that the hardware exists or is emulated */
  1230. if (!check_hw_exists())
  1231. return 0;
  1232. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1233. if (x86_pmu.quirks)
  1234. x86_pmu.quirks();
  1235. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  1236. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  1237. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  1238. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  1239. }
  1240. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  1241. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  1242. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  1243. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  1244. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  1245. }
  1246. x86_pmu.intel_ctrl |=
  1247. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  1248. perf_events_lapic_init();
  1249. register_die_notifier(&perf_event_nmi_notifier);
  1250. unconstrained = (struct event_constraint)
  1251. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  1252. 0, x86_pmu.num_counters);
  1253. if (x86_pmu.event_constraints) {
  1254. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1255. if (c->cmask != X86_RAW_EVENT_MASK)
  1256. continue;
  1257. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  1258. c->weight += x86_pmu.num_counters;
  1259. }
  1260. }
  1261. pr_info("... version: %d\n", x86_pmu.version);
  1262. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  1263. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  1264. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  1265. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1266. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  1267. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  1268. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1269. perf_cpu_notifier(x86_pmu_notifier);
  1270. return 0;
  1271. }
  1272. early_initcall(init_hw_perf_events);
  1273. static inline void x86_pmu_read(struct perf_event *event)
  1274. {
  1275. x86_perf_event_update(event);
  1276. }
  1277. /*
  1278. * Start group events scheduling transaction
  1279. * Set the flag to make pmu::enable() not perform the
  1280. * schedulability test, it will be performed at commit time
  1281. */
  1282. static void x86_pmu_start_txn(struct pmu *pmu)
  1283. {
  1284. perf_pmu_disable(pmu);
  1285. __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN);
  1286. __this_cpu_write(cpu_hw_events.n_txn, 0);
  1287. }
  1288. /*
  1289. * Stop group events scheduling transaction
  1290. * Clear the flag and pmu::enable() will perform the
  1291. * schedulability test.
  1292. */
  1293. static void x86_pmu_cancel_txn(struct pmu *pmu)
  1294. {
  1295. __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN);
  1296. /*
  1297. * Truncate the collected events.
  1298. */
  1299. __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
  1300. __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
  1301. perf_pmu_enable(pmu);
  1302. }
  1303. /*
  1304. * Commit group events scheduling transaction
  1305. * Perform the group schedulability test as a whole
  1306. * Return 0 if success
  1307. */
  1308. static int x86_pmu_commit_txn(struct pmu *pmu)
  1309. {
  1310. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1311. int assign[X86_PMC_IDX_MAX];
  1312. int n, ret;
  1313. n = cpuc->n_events;
  1314. if (!x86_pmu_initialized())
  1315. return -EAGAIN;
  1316. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1317. if (ret)
  1318. return ret;
  1319. /*
  1320. * copy new assignment, now we know it is possible
  1321. * will be used by hw_perf_enable()
  1322. */
  1323. memcpy(cpuc->assign, assign, n*sizeof(int));
  1324. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1325. perf_pmu_enable(pmu);
  1326. return 0;
  1327. }
  1328. /*
  1329. * validate that we can schedule this event
  1330. */
  1331. static int validate_event(struct perf_event *event)
  1332. {
  1333. struct cpu_hw_events *fake_cpuc;
  1334. struct event_constraint *c;
  1335. int ret = 0;
  1336. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1337. if (!fake_cpuc)
  1338. return -ENOMEM;
  1339. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1340. if (!c || !c->weight)
  1341. ret = -ENOSPC;
  1342. if (x86_pmu.put_event_constraints)
  1343. x86_pmu.put_event_constraints(fake_cpuc, event);
  1344. kfree(fake_cpuc);
  1345. return ret;
  1346. }
  1347. /*
  1348. * validate a single event group
  1349. *
  1350. * validation include:
  1351. * - check events are compatible which each other
  1352. * - events do not compete for the same counter
  1353. * - number of events <= number of counters
  1354. *
  1355. * validation ensures the group can be loaded onto the
  1356. * PMU if it was the only group available.
  1357. */
  1358. static int validate_group(struct perf_event *event)
  1359. {
  1360. struct perf_event *leader = event->group_leader;
  1361. struct cpu_hw_events *fake_cpuc;
  1362. int ret, n;
  1363. ret = -ENOMEM;
  1364. fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
  1365. if (!fake_cpuc)
  1366. goto out;
  1367. /*
  1368. * the event is not yet connected with its
  1369. * siblings therefore we must first collect
  1370. * existing siblings, then add the new event
  1371. * before we can simulate the scheduling
  1372. */
  1373. ret = -ENOSPC;
  1374. n = collect_events(fake_cpuc, leader, true);
  1375. if (n < 0)
  1376. goto out_free;
  1377. fake_cpuc->n_events = n;
  1378. n = collect_events(fake_cpuc, event, false);
  1379. if (n < 0)
  1380. goto out_free;
  1381. fake_cpuc->n_events = n;
  1382. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1383. out_free:
  1384. kfree(fake_cpuc);
  1385. out:
  1386. return ret;
  1387. }
  1388. static int x86_pmu_event_init(struct perf_event *event)
  1389. {
  1390. struct pmu *tmp;
  1391. int err;
  1392. switch (event->attr.type) {
  1393. case PERF_TYPE_RAW:
  1394. case PERF_TYPE_HARDWARE:
  1395. case PERF_TYPE_HW_CACHE:
  1396. break;
  1397. default:
  1398. return -ENOENT;
  1399. }
  1400. err = __x86_pmu_event_init(event);
  1401. if (!err) {
  1402. /*
  1403. * we temporarily connect event to its pmu
  1404. * such that validate_group() can classify
  1405. * it as an x86 event using is_x86_event()
  1406. */
  1407. tmp = event->pmu;
  1408. event->pmu = &pmu;
  1409. if (event->group_leader != event)
  1410. err = validate_group(event);
  1411. else
  1412. err = validate_event(event);
  1413. event->pmu = tmp;
  1414. }
  1415. if (err) {
  1416. if (event->destroy)
  1417. event->destroy(event);
  1418. }
  1419. return err;
  1420. }
  1421. static struct pmu pmu = {
  1422. .pmu_enable = x86_pmu_enable,
  1423. .pmu_disable = x86_pmu_disable,
  1424. .event_init = x86_pmu_event_init,
  1425. .add = x86_pmu_add,
  1426. .del = x86_pmu_del,
  1427. .start = x86_pmu_start,
  1428. .stop = x86_pmu_stop,
  1429. .read = x86_pmu_read,
  1430. .start_txn = x86_pmu_start_txn,
  1431. .cancel_txn = x86_pmu_cancel_txn,
  1432. .commit_txn = x86_pmu_commit_txn,
  1433. };
  1434. /*
  1435. * callchain support
  1436. */
  1437. static void
  1438. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  1439. {
  1440. /* Ignore warnings */
  1441. }
  1442. static void backtrace_warning(void *data, char *msg)
  1443. {
  1444. /* Ignore warnings */
  1445. }
  1446. static int backtrace_stack(void *data, char *name)
  1447. {
  1448. return 0;
  1449. }
  1450. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1451. {
  1452. struct perf_callchain_entry *entry = data;
  1453. perf_callchain_store(entry, addr);
  1454. }
  1455. static const struct stacktrace_ops backtrace_ops = {
  1456. .warning = backtrace_warning,
  1457. .warning_symbol = backtrace_warning_symbol,
  1458. .stack = backtrace_stack,
  1459. .address = backtrace_address,
  1460. .walk_stack = print_context_stack_bp,
  1461. };
  1462. void
  1463. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1464. {
  1465. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1466. /* TODO: We don't support guest os callchain now */
  1467. return;
  1468. }
  1469. perf_callchain_store(entry, regs->ip);
  1470. dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
  1471. }
  1472. #ifdef CONFIG_COMPAT
  1473. static inline int
  1474. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1475. {
  1476. /* 32-bit process in 64-bit kernel. */
  1477. struct stack_frame_ia32 frame;
  1478. const void __user *fp;
  1479. if (!test_thread_flag(TIF_IA32))
  1480. return 0;
  1481. fp = compat_ptr(regs->bp);
  1482. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1483. unsigned long bytes;
  1484. frame.next_frame = 0;
  1485. frame.return_address = 0;
  1486. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1487. if (bytes != sizeof(frame))
  1488. break;
  1489. if (fp < compat_ptr(regs->sp))
  1490. break;
  1491. perf_callchain_store(entry, frame.return_address);
  1492. fp = compat_ptr(frame.next_frame);
  1493. }
  1494. return 1;
  1495. }
  1496. #else
  1497. static inline int
  1498. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1499. {
  1500. return 0;
  1501. }
  1502. #endif
  1503. void
  1504. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1505. {
  1506. struct stack_frame frame;
  1507. const void __user *fp;
  1508. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1509. /* TODO: We don't support guest os callchain now */
  1510. return;
  1511. }
  1512. fp = (void __user *)regs->bp;
  1513. perf_callchain_store(entry, regs->ip);
  1514. if (perf_callchain_user32(regs, entry))
  1515. return;
  1516. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1517. unsigned long bytes;
  1518. frame.next_frame = NULL;
  1519. frame.return_address = 0;
  1520. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1521. if (bytes != sizeof(frame))
  1522. break;
  1523. if ((unsigned long)fp < regs->sp)
  1524. break;
  1525. perf_callchain_store(entry, frame.return_address);
  1526. fp = frame.next_frame;
  1527. }
  1528. }
  1529. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1530. {
  1531. unsigned long ip;
  1532. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1533. ip = perf_guest_cbs->get_guest_ip();
  1534. else
  1535. ip = instruction_pointer(regs);
  1536. return ip;
  1537. }
  1538. unsigned long perf_misc_flags(struct pt_regs *regs)
  1539. {
  1540. int misc = 0;
  1541. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1542. if (perf_guest_cbs->is_user_mode())
  1543. misc |= PERF_RECORD_MISC_GUEST_USER;
  1544. else
  1545. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1546. } else {
  1547. if (user_mode(regs))
  1548. misc |= PERF_RECORD_MISC_USER;
  1549. else
  1550. misc |= PERF_RECORD_MISC_KERNEL;
  1551. }
  1552. if (regs->flags & PERF_EFLAGS_EXACT)
  1553. misc |= PERF_RECORD_MISC_EXACT_IP;
  1554. return misc;
  1555. }