perf_event.c 44 KB

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