perf_event.c 42 KB

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