perf_event.c 43 KB

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