perf_event.c 42 KB

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