perf_event.c 38 KB

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