perf_event.c 38 KB

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