perf_event.c 43 KB

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