perf_event.c 38 KB

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