perf_event.c 43 KB

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