perf_event.c 39 KB

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