perf_event.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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/cpu.h>
  25. #include <linux/bitops.h>
  26. #include <asm/apic.h>
  27. #include <asm/stacktrace.h>
  28. #include <asm/nmi.h>
  29. #include <asm/compat.h>
  30. #include <asm/smp.h>
  31. #include <asm/alternative.h>
  32. #include "perf_event.h"
  33. #if 0
  34. #undef wrmsrl
  35. #define wrmsrl(msr, val) \
  36. do { \
  37. trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\
  38. (unsigned long)(val)); \
  39. native_write_msr((msr), (u32)((u64)(val)), \
  40. (u32)((u64)(val) >> 32)); \
  41. } while (0)
  42. #endif
  43. struct x86_pmu x86_pmu __read_mostly;
  44. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  45. .enabled = 1,
  46. };
  47. u64 __read_mostly hw_cache_event_ids
  48. [PERF_COUNT_HW_CACHE_MAX]
  49. [PERF_COUNT_HW_CACHE_OP_MAX]
  50. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  51. u64 __read_mostly hw_cache_extra_regs
  52. [PERF_COUNT_HW_CACHE_MAX]
  53. [PERF_COUNT_HW_CACHE_OP_MAX]
  54. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  55. /*
  56. * Propagate event elapsed time into the generic event.
  57. * Can only be executed on the CPU where the event is active.
  58. * Returns the delta events processed.
  59. */
  60. u64 x86_perf_event_update(struct perf_event *event)
  61. {
  62. struct hw_perf_event *hwc = &event->hw;
  63. int shift = 64 - x86_pmu.cntval_bits;
  64. u64 prev_raw_count, new_raw_count;
  65. int idx = hwc->idx;
  66. s64 delta;
  67. if (idx == X86_PMC_IDX_FIXED_BTS)
  68. return 0;
  69. /*
  70. * Careful: an NMI might modify the previous event value.
  71. *
  72. * Our tactic to handle this is to first atomically read and
  73. * exchange a new raw count - then add that new-prev delta
  74. * count to the generic event atomically:
  75. */
  76. again:
  77. prev_raw_count = local64_read(&hwc->prev_count);
  78. rdmsrl(hwc->event_base, new_raw_count);
  79. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  80. new_raw_count) != prev_raw_count)
  81. goto again;
  82. /*
  83. * Now we have the new raw value and have updated the prev
  84. * timestamp already. We can now calculate the elapsed delta
  85. * (event-)time and add that to the generic event.
  86. *
  87. * Careful, not all hw sign-extends above the physical width
  88. * of the count.
  89. */
  90. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  91. delta >>= shift;
  92. local64_add(delta, &event->count);
  93. local64_sub(delta, &hwc->period_left);
  94. return new_raw_count;
  95. }
  96. /*
  97. * Find and validate any extra registers to set up.
  98. */
  99. static int x86_pmu_extra_regs(u64 config, struct perf_event *event)
  100. {
  101. struct hw_perf_event_extra *reg;
  102. struct extra_reg *er;
  103. reg = &event->hw.extra_reg;
  104. if (!x86_pmu.extra_regs)
  105. return 0;
  106. for (er = x86_pmu.extra_regs; er->msr; er++) {
  107. if (er->event != (config & er->config_mask))
  108. continue;
  109. if (event->attr.config1 & ~er->valid_mask)
  110. return -EINVAL;
  111. reg->idx = er->idx;
  112. reg->config = event->attr.config1;
  113. reg->reg = er->msr;
  114. break;
  115. }
  116. return 0;
  117. }
  118. static atomic_t active_events;
  119. static DEFINE_MUTEX(pmc_reserve_mutex);
  120. #ifdef CONFIG_X86_LOCAL_APIC
  121. static bool reserve_pmc_hardware(void)
  122. {
  123. int i;
  124. for (i = 0; i < x86_pmu.num_counters; i++) {
  125. if (!reserve_perfctr_nmi(x86_pmu_event_addr(i)))
  126. goto perfctr_fail;
  127. }
  128. for (i = 0; i < x86_pmu.num_counters; i++) {
  129. if (!reserve_evntsel_nmi(x86_pmu_config_addr(i)))
  130. goto eventsel_fail;
  131. }
  132. return true;
  133. eventsel_fail:
  134. for (i--; i >= 0; i--)
  135. release_evntsel_nmi(x86_pmu_config_addr(i));
  136. i = x86_pmu.num_counters;
  137. perfctr_fail:
  138. for (i--; i >= 0; i--)
  139. release_perfctr_nmi(x86_pmu_event_addr(i));
  140. return false;
  141. }
  142. static void release_pmc_hardware(void)
  143. {
  144. int i;
  145. for (i = 0; i < x86_pmu.num_counters; i++) {
  146. release_perfctr_nmi(x86_pmu_event_addr(i));
  147. release_evntsel_nmi(x86_pmu_config_addr(i));
  148. }
  149. }
  150. #else
  151. static bool reserve_pmc_hardware(void) { return true; }
  152. static void release_pmc_hardware(void) {}
  153. #endif
  154. static bool check_hw_exists(void)
  155. {
  156. u64 val, val_new = 0;
  157. int i, reg, ret = 0;
  158. /*
  159. * Check to see if the BIOS enabled any of the counters, if so
  160. * complain and bail.
  161. */
  162. for (i = 0; i < x86_pmu.num_counters; i++) {
  163. reg = x86_pmu_config_addr(i);
  164. ret = rdmsrl_safe(reg, &val);
  165. if (ret)
  166. goto msr_fail;
  167. if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
  168. goto bios_fail;
  169. }
  170. if (x86_pmu.num_counters_fixed) {
  171. reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  172. ret = rdmsrl_safe(reg, &val);
  173. if (ret)
  174. goto msr_fail;
  175. for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
  176. if (val & (0x03 << i*4))
  177. goto bios_fail;
  178. }
  179. }
  180. /*
  181. * Now write a value and read it back to see if it matches,
  182. * this is needed to detect certain hardware emulators (qemu/kvm)
  183. * that don't trap on the MSR access and always return 0s.
  184. */
  185. val = 0xabcdUL;
  186. ret = checking_wrmsrl(x86_pmu_event_addr(0), val);
  187. ret |= rdmsrl_safe(x86_pmu_event_addr(0), &val_new);
  188. if (ret || val != val_new)
  189. goto msr_fail;
  190. return true;
  191. bios_fail:
  192. /*
  193. * We still allow the PMU driver to operate:
  194. */
  195. printk(KERN_CONT "Broken BIOS detected, complain to your hardware vendor.\n");
  196. printk(KERN_ERR FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg, val);
  197. return true;
  198. msr_fail:
  199. printk(KERN_CONT "Broken PMU hardware detected, using software events only.\n");
  200. return false;
  201. }
  202. static void hw_perf_event_destroy(struct perf_event *event)
  203. {
  204. if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
  205. release_pmc_hardware();
  206. release_ds_buffers();
  207. mutex_unlock(&pmc_reserve_mutex);
  208. }
  209. }
  210. static inline int x86_pmu_initialized(void)
  211. {
  212. return x86_pmu.handle_irq != NULL;
  213. }
  214. static inline int
  215. set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)
  216. {
  217. struct perf_event_attr *attr = &event->attr;
  218. unsigned int cache_type, cache_op, cache_result;
  219. u64 config, val;
  220. config = attr->config;
  221. cache_type = (config >> 0) & 0xff;
  222. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  223. return -EINVAL;
  224. cache_op = (config >> 8) & 0xff;
  225. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  226. return -EINVAL;
  227. cache_result = (config >> 16) & 0xff;
  228. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  229. return -EINVAL;
  230. val = hw_cache_event_ids[cache_type][cache_op][cache_result];
  231. if (val == 0)
  232. return -ENOENT;
  233. if (val == -1)
  234. return -EINVAL;
  235. hwc->config |= val;
  236. attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result];
  237. return x86_pmu_extra_regs(val, event);
  238. }
  239. int x86_setup_perfctr(struct perf_event *event)
  240. {
  241. struct perf_event_attr *attr = &event->attr;
  242. struct hw_perf_event *hwc = &event->hw;
  243. u64 config;
  244. if (!is_sampling_event(event)) {
  245. hwc->sample_period = x86_pmu.max_period;
  246. hwc->last_period = hwc->sample_period;
  247. local64_set(&hwc->period_left, hwc->sample_period);
  248. } else {
  249. /*
  250. * If we have a PMU initialized but no APIC
  251. * interrupts, we cannot sample hardware
  252. * events (user-space has to fall back and
  253. * sample via a hrtimer based software event):
  254. */
  255. if (!x86_pmu.apic)
  256. return -EOPNOTSUPP;
  257. }
  258. if (attr->type == PERF_TYPE_RAW)
  259. return x86_pmu_extra_regs(event->attr.config, event);
  260. if (attr->type == PERF_TYPE_HW_CACHE)
  261. return set_ext_hw_attr(hwc, event);
  262. if (attr->config >= x86_pmu.max_events)
  263. return -EINVAL;
  264. /*
  265. * The generic map:
  266. */
  267. config = x86_pmu.event_map(attr->config);
  268. if (config == 0)
  269. return -ENOENT;
  270. if (config == -1LL)
  271. return -EINVAL;
  272. /*
  273. * Branch tracing:
  274. */
  275. if (attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
  276. !attr->freq && hwc->sample_period == 1) {
  277. /* BTS is not supported by this architecture. */
  278. if (!x86_pmu.bts_active)
  279. return -EOPNOTSUPP;
  280. /* BTS is currently only allowed for user-mode. */
  281. if (!attr->exclude_kernel)
  282. return -EOPNOTSUPP;
  283. }
  284. hwc->config |= config;
  285. return 0;
  286. }
  287. int x86_pmu_hw_config(struct perf_event *event)
  288. {
  289. if (event->attr.precise_ip) {
  290. int precise = 0;
  291. /* Support for constant skid */
  292. if (x86_pmu.pebs_active) {
  293. precise++;
  294. /* Support for IP fixup */
  295. if (x86_pmu.lbr_nr)
  296. precise++;
  297. }
  298. if (event->attr.precise_ip > precise)
  299. return -EOPNOTSUPP;
  300. }
  301. /*
  302. * Generate PMC IRQs:
  303. * (keep 'enabled' bit clear for now)
  304. */
  305. event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
  306. /*
  307. * Count user and OS events unless requested not to
  308. */
  309. if (!event->attr.exclude_user)
  310. event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
  311. if (!event->attr.exclude_kernel)
  312. event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
  313. if (event->attr.type == PERF_TYPE_RAW)
  314. event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
  315. return x86_setup_perfctr(event);
  316. }
  317. /*
  318. * Setup the hardware configuration for a given attr_type
  319. */
  320. static int __x86_pmu_event_init(struct perf_event *event)
  321. {
  322. int err;
  323. if (!x86_pmu_initialized())
  324. return -ENODEV;
  325. err = 0;
  326. if (!atomic_inc_not_zero(&active_events)) {
  327. mutex_lock(&pmc_reserve_mutex);
  328. if (atomic_read(&active_events) == 0) {
  329. if (!reserve_pmc_hardware())
  330. err = -EBUSY;
  331. else
  332. reserve_ds_buffers();
  333. }
  334. if (!err)
  335. atomic_inc(&active_events);
  336. mutex_unlock(&pmc_reserve_mutex);
  337. }
  338. if (err)
  339. return err;
  340. event->destroy = hw_perf_event_destroy;
  341. event->hw.idx = -1;
  342. event->hw.last_cpu = -1;
  343. event->hw.last_tag = ~0ULL;
  344. /* mark unused */
  345. event->hw.extra_reg.idx = EXTRA_REG_NONE;
  346. return x86_pmu.hw_config(event);
  347. }
  348. void x86_pmu_disable_all(void)
  349. {
  350. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  351. int idx;
  352. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  353. u64 val;
  354. if (!test_bit(idx, cpuc->active_mask))
  355. continue;
  356. rdmsrl(x86_pmu_config_addr(idx), val);
  357. if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
  358. continue;
  359. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  360. wrmsrl(x86_pmu_config_addr(idx), val);
  361. }
  362. }
  363. static void x86_pmu_disable(struct pmu *pmu)
  364. {
  365. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  366. if (!x86_pmu_initialized())
  367. return;
  368. if (!cpuc->enabled)
  369. return;
  370. cpuc->n_added = 0;
  371. cpuc->enabled = 0;
  372. barrier();
  373. x86_pmu.disable_all();
  374. }
  375. void x86_pmu_enable_all(int added)
  376. {
  377. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  378. int idx;
  379. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  380. struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
  381. if (!test_bit(idx, cpuc->active_mask))
  382. continue;
  383. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  384. }
  385. }
  386. static struct pmu pmu;
  387. static inline int is_x86_event(struct perf_event *event)
  388. {
  389. return event->pmu == &pmu;
  390. }
  391. int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  392. {
  393. struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
  394. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  395. int i, j, w, wmax, num = 0;
  396. struct hw_perf_event *hwc;
  397. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  398. for (i = 0; i < n; i++) {
  399. c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
  400. constraints[i] = c;
  401. }
  402. /*
  403. * fastpath, try to reuse previous register
  404. */
  405. for (i = 0; i < n; i++) {
  406. hwc = &cpuc->event_list[i]->hw;
  407. c = constraints[i];
  408. /* never assigned */
  409. if (hwc->idx == -1)
  410. break;
  411. /* constraint still honored */
  412. if (!test_bit(hwc->idx, c->idxmsk))
  413. break;
  414. /* not already used */
  415. if (test_bit(hwc->idx, used_mask))
  416. break;
  417. __set_bit(hwc->idx, used_mask);
  418. if (assign)
  419. assign[i] = hwc->idx;
  420. }
  421. if (i == n)
  422. goto done;
  423. /*
  424. * begin slow path
  425. */
  426. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  427. /*
  428. * weight = number of possible counters
  429. *
  430. * 1 = most constrained, only works on one counter
  431. * wmax = least constrained, works on any counter
  432. *
  433. * assign events to counters starting with most
  434. * constrained events.
  435. */
  436. wmax = x86_pmu.num_counters;
  437. /*
  438. * when fixed event counters are present,
  439. * wmax is incremented by 1 to account
  440. * for one more choice
  441. */
  442. if (x86_pmu.num_counters_fixed)
  443. wmax++;
  444. for (w = 1, num = n; num && w <= wmax; w++) {
  445. /* for each event */
  446. for (i = 0; num && i < n; i++) {
  447. c = constraints[i];
  448. hwc = &cpuc->event_list[i]->hw;
  449. if (c->weight != w)
  450. continue;
  451. for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) {
  452. if (!test_bit(j, used_mask))
  453. break;
  454. }
  455. if (j == X86_PMC_IDX_MAX)
  456. break;
  457. __set_bit(j, used_mask);
  458. if (assign)
  459. assign[i] = j;
  460. num--;
  461. }
  462. }
  463. done:
  464. /*
  465. * scheduling failed or is just a simulation,
  466. * free resources if necessary
  467. */
  468. if (!assign || num) {
  469. for (i = 0; i < n; i++) {
  470. if (x86_pmu.put_event_constraints)
  471. x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
  472. }
  473. }
  474. return num ? -EINVAL : 0;
  475. }
  476. /*
  477. * dogrp: true if must collect siblings events (group)
  478. * returns total number of events and error code
  479. */
  480. static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
  481. {
  482. struct perf_event *event;
  483. int n, max_count;
  484. max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
  485. /* current number of events already accepted */
  486. n = cpuc->n_events;
  487. if (is_x86_event(leader)) {
  488. if (n >= max_count)
  489. return -EINVAL;
  490. cpuc->event_list[n] = leader;
  491. n++;
  492. }
  493. if (!dogrp)
  494. return n;
  495. list_for_each_entry(event, &leader->sibling_list, group_entry) {
  496. if (!is_x86_event(event) ||
  497. event->state <= PERF_EVENT_STATE_OFF)
  498. continue;
  499. if (n >= max_count)
  500. return -EINVAL;
  501. cpuc->event_list[n] = event;
  502. n++;
  503. }
  504. return n;
  505. }
  506. static inline void x86_assign_hw_event(struct perf_event *event,
  507. struct cpu_hw_events *cpuc, int i)
  508. {
  509. struct hw_perf_event *hwc = &event->hw;
  510. hwc->idx = cpuc->assign[i];
  511. hwc->last_cpu = smp_processor_id();
  512. hwc->last_tag = ++cpuc->tags[i];
  513. if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
  514. hwc->config_base = 0;
  515. hwc->event_base = 0;
  516. } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
  517. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  518. hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - X86_PMC_IDX_FIXED);
  519. } else {
  520. hwc->config_base = x86_pmu_config_addr(hwc->idx);
  521. hwc->event_base = x86_pmu_event_addr(hwc->idx);
  522. }
  523. }
  524. static inline int match_prev_assignment(struct hw_perf_event *hwc,
  525. struct cpu_hw_events *cpuc,
  526. int i)
  527. {
  528. return hwc->idx == cpuc->assign[i] &&
  529. hwc->last_cpu == smp_processor_id() &&
  530. hwc->last_tag == cpuc->tags[i];
  531. }
  532. static void x86_pmu_start(struct perf_event *event, int flags);
  533. static void x86_pmu_enable(struct pmu *pmu)
  534. {
  535. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  536. struct perf_event *event;
  537. struct hw_perf_event *hwc;
  538. int i, added = cpuc->n_added;
  539. if (!x86_pmu_initialized())
  540. return;
  541. if (cpuc->enabled)
  542. return;
  543. if (cpuc->n_added) {
  544. int n_running = cpuc->n_events - cpuc->n_added;
  545. /*
  546. * apply assignment obtained either from
  547. * hw_perf_group_sched_in() or x86_pmu_enable()
  548. *
  549. * step1: save events moving to new counters
  550. * step2: reprogram moved events into new counters
  551. */
  552. for (i = 0; i < n_running; i++) {
  553. event = cpuc->event_list[i];
  554. hwc = &event->hw;
  555. /*
  556. * we can avoid reprogramming counter if:
  557. * - assigned same counter as last time
  558. * - running on same CPU as last time
  559. * - no other event has used the counter since
  560. */
  561. if (hwc->idx == -1 ||
  562. match_prev_assignment(hwc, cpuc, i))
  563. continue;
  564. /*
  565. * Ensure we don't accidentally enable a stopped
  566. * counter simply because we rescheduled.
  567. */
  568. if (hwc->state & PERF_HES_STOPPED)
  569. hwc->state |= PERF_HES_ARCH;
  570. x86_pmu_stop(event, PERF_EF_UPDATE);
  571. }
  572. for (i = 0; i < cpuc->n_events; i++) {
  573. event = cpuc->event_list[i];
  574. hwc = &event->hw;
  575. if (!match_prev_assignment(hwc, cpuc, i))
  576. x86_assign_hw_event(event, cpuc, i);
  577. else if (i < n_running)
  578. continue;
  579. if (hwc->state & PERF_HES_ARCH)
  580. continue;
  581. x86_pmu_start(event, PERF_EF_RELOAD);
  582. }
  583. cpuc->n_added = 0;
  584. perf_events_lapic_init();
  585. }
  586. cpuc->enabled = 1;
  587. barrier();
  588. x86_pmu.enable_all(added);
  589. }
  590. static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  591. /*
  592. * Set the next IRQ period, based on the hwc->period_left value.
  593. * To be called with the event disabled in hw:
  594. */
  595. int x86_perf_event_set_period(struct perf_event *event)
  596. {
  597. struct hw_perf_event *hwc = &event->hw;
  598. s64 left = local64_read(&hwc->period_left);
  599. s64 period = hwc->sample_period;
  600. int ret = 0, idx = hwc->idx;
  601. if (idx == X86_PMC_IDX_FIXED_BTS)
  602. return 0;
  603. /*
  604. * If we are way outside a reasonable range then just skip forward:
  605. */
  606. if (unlikely(left <= -period)) {
  607. left = period;
  608. local64_set(&hwc->period_left, left);
  609. hwc->last_period = period;
  610. ret = 1;
  611. }
  612. if (unlikely(left <= 0)) {
  613. left += period;
  614. local64_set(&hwc->period_left, left);
  615. hwc->last_period = period;
  616. ret = 1;
  617. }
  618. /*
  619. * Quirk: certain CPUs dont like it if just 1 hw_event is left:
  620. */
  621. if (unlikely(left < 2))
  622. left = 2;
  623. if (left > x86_pmu.max_period)
  624. left = x86_pmu.max_period;
  625. per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
  626. /*
  627. * The hw event starts counting from this event offset,
  628. * mark it to be able to extra future deltas:
  629. */
  630. local64_set(&hwc->prev_count, (u64)-left);
  631. wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
  632. /*
  633. * Due to erratum on certan cpu we need
  634. * a second write to be sure the register
  635. * is updated properly
  636. */
  637. if (x86_pmu.perfctr_second_write) {
  638. wrmsrl(hwc->event_base,
  639. (u64)(-left) & x86_pmu.cntval_mask);
  640. }
  641. perf_event_update_userpage(event);
  642. return ret;
  643. }
  644. void x86_pmu_enable_event(struct perf_event *event)
  645. {
  646. if (__this_cpu_read(cpu_hw_events.enabled))
  647. __x86_pmu_enable_event(&event->hw,
  648. ARCH_PERFMON_EVENTSEL_ENABLE);
  649. }
  650. /*
  651. * Add a single event to the PMU.
  652. *
  653. * The event is added to the group of enabled events
  654. * but only if it can be scehduled with existing events.
  655. */
  656. static int x86_pmu_add(struct perf_event *event, int flags)
  657. {
  658. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  659. struct hw_perf_event *hwc;
  660. int assign[X86_PMC_IDX_MAX];
  661. int n, n0, ret;
  662. hwc = &event->hw;
  663. perf_pmu_disable(event->pmu);
  664. n0 = cpuc->n_events;
  665. ret = n = collect_events(cpuc, event, false);
  666. if (ret < 0)
  667. goto out;
  668. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  669. if (!(flags & PERF_EF_START))
  670. hwc->state |= PERF_HES_ARCH;
  671. /*
  672. * If group events scheduling transaction was started,
  673. * skip the schedulability test here, it will be performed
  674. * at commit time (->commit_txn) as a whole
  675. */
  676. if (cpuc->group_flag & PERF_EVENT_TXN)
  677. goto done_collect;
  678. ret = x86_pmu.schedule_events(cpuc, n, assign);
  679. if (ret)
  680. goto out;
  681. /*
  682. * copy new assignment, now we know it is possible
  683. * will be used by hw_perf_enable()
  684. */
  685. memcpy(cpuc->assign, assign, n*sizeof(int));
  686. done_collect:
  687. cpuc->n_events = n;
  688. cpuc->n_added += n - n0;
  689. cpuc->n_txn += n - n0;
  690. ret = 0;
  691. out:
  692. perf_pmu_enable(event->pmu);
  693. return ret;
  694. }
  695. static void x86_pmu_start(struct perf_event *event, int flags)
  696. {
  697. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  698. int idx = event->hw.idx;
  699. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  700. return;
  701. if (WARN_ON_ONCE(idx == -1))
  702. return;
  703. if (flags & PERF_EF_RELOAD) {
  704. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  705. x86_perf_event_set_period(event);
  706. }
  707. event->hw.state = 0;
  708. cpuc->events[idx] = event;
  709. __set_bit(idx, cpuc->active_mask);
  710. __set_bit(idx, cpuc->running);
  711. x86_pmu.enable(event);
  712. perf_event_update_userpage(event);
  713. }
  714. void perf_event_print_debug(void)
  715. {
  716. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  717. u64 pebs;
  718. struct cpu_hw_events *cpuc;
  719. unsigned long flags;
  720. int cpu, idx;
  721. if (!x86_pmu.num_counters)
  722. return;
  723. local_irq_save(flags);
  724. cpu = smp_processor_id();
  725. cpuc = &per_cpu(cpu_hw_events, cpu);
  726. if (x86_pmu.version >= 2) {
  727. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  728. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  729. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  730. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  731. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  732. pr_info("\n");
  733. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  734. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  735. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  736. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  737. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  738. }
  739. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  740. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  741. rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
  742. rdmsrl(x86_pmu_event_addr(idx), pmc_count);
  743. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  744. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  745. cpu, idx, pmc_ctrl);
  746. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  747. cpu, idx, pmc_count);
  748. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  749. cpu, idx, prev_left);
  750. }
  751. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  752. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  753. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  754. cpu, idx, pmc_count);
  755. }
  756. local_irq_restore(flags);
  757. }
  758. void x86_pmu_stop(struct perf_event *event, int flags)
  759. {
  760. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  761. struct hw_perf_event *hwc = &event->hw;
  762. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  763. x86_pmu.disable(event);
  764. cpuc->events[hwc->idx] = NULL;
  765. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  766. hwc->state |= PERF_HES_STOPPED;
  767. }
  768. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  769. /*
  770. * Drain the remaining delta count out of a event
  771. * that we are disabling:
  772. */
  773. x86_perf_event_update(event);
  774. hwc->state |= PERF_HES_UPTODATE;
  775. }
  776. }
  777. static void x86_pmu_del(struct perf_event *event, int flags)
  778. {
  779. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  780. int i;
  781. /*
  782. * If we're called during a txn, we don't need to do anything.
  783. * The events never got scheduled and ->cancel_txn will truncate
  784. * the event_list.
  785. */
  786. if (cpuc->group_flag & PERF_EVENT_TXN)
  787. return;
  788. x86_pmu_stop(event, PERF_EF_UPDATE);
  789. for (i = 0; i < cpuc->n_events; i++) {
  790. if (event == cpuc->event_list[i]) {
  791. if (x86_pmu.put_event_constraints)
  792. x86_pmu.put_event_constraints(cpuc, event);
  793. while (++i < cpuc->n_events)
  794. cpuc->event_list[i-1] = cpuc->event_list[i];
  795. --cpuc->n_events;
  796. break;
  797. }
  798. }
  799. perf_event_update_userpage(event);
  800. }
  801. int x86_pmu_handle_irq(struct pt_regs *regs)
  802. {
  803. struct perf_sample_data data;
  804. struct cpu_hw_events *cpuc;
  805. struct perf_event *event;
  806. int idx, handled = 0;
  807. u64 val;
  808. perf_sample_data_init(&data, 0);
  809. cpuc = &__get_cpu_var(cpu_hw_events);
  810. /*
  811. * Some chipsets need to unmask the LVTPC in a particular spot
  812. * inside the nmi handler. As a result, the unmasking was pushed
  813. * into all the nmi handlers.
  814. *
  815. * This generic handler doesn't seem to have any issues where the
  816. * unmasking occurs so it was left at the top.
  817. */
  818. apic_write(APIC_LVTPC, APIC_DM_NMI);
  819. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  820. if (!test_bit(idx, cpuc->active_mask)) {
  821. /*
  822. * Though we deactivated the counter some cpus
  823. * might still deliver spurious interrupts still
  824. * in flight. Catch them:
  825. */
  826. if (__test_and_clear_bit(idx, cpuc->running))
  827. handled++;
  828. continue;
  829. }
  830. event = cpuc->events[idx];
  831. val = x86_perf_event_update(event);
  832. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  833. continue;
  834. /*
  835. * event overflow
  836. */
  837. handled++;
  838. data.period = event->hw.last_period;
  839. if (!x86_perf_event_set_period(event))
  840. continue;
  841. if (perf_event_overflow(event, &data, regs))
  842. x86_pmu_stop(event, 0);
  843. }
  844. if (handled)
  845. inc_irq_stat(apic_perf_irqs);
  846. return handled;
  847. }
  848. void perf_events_lapic_init(void)
  849. {
  850. if (!x86_pmu.apic || !x86_pmu_initialized())
  851. return;
  852. /*
  853. * Always use NMI for PMU
  854. */
  855. apic_write(APIC_LVTPC, APIC_DM_NMI);
  856. }
  857. static int __kprobes
  858. perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  859. {
  860. if (!atomic_read(&active_events))
  861. return NMI_DONE;
  862. return x86_pmu.handle_irq(regs);
  863. }
  864. struct event_constraint emptyconstraint;
  865. struct event_constraint unconstrained;
  866. static int __cpuinit
  867. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  868. {
  869. unsigned int cpu = (long)hcpu;
  870. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  871. int ret = NOTIFY_OK;
  872. switch (action & ~CPU_TASKS_FROZEN) {
  873. case CPU_UP_PREPARE:
  874. cpuc->kfree_on_online = NULL;
  875. if (x86_pmu.cpu_prepare)
  876. ret = x86_pmu.cpu_prepare(cpu);
  877. break;
  878. case CPU_STARTING:
  879. if (x86_pmu.cpu_starting)
  880. x86_pmu.cpu_starting(cpu);
  881. break;
  882. case CPU_ONLINE:
  883. kfree(cpuc->kfree_on_online);
  884. break;
  885. case CPU_DYING:
  886. if (x86_pmu.cpu_dying)
  887. x86_pmu.cpu_dying(cpu);
  888. break;
  889. case CPU_UP_CANCELED:
  890. case CPU_DEAD:
  891. if (x86_pmu.cpu_dead)
  892. x86_pmu.cpu_dead(cpu);
  893. break;
  894. default:
  895. break;
  896. }
  897. return ret;
  898. }
  899. static void __init pmu_check_apic(void)
  900. {
  901. if (cpu_has_apic)
  902. return;
  903. x86_pmu.apic = 0;
  904. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  905. pr_info("no hardware sampling interrupt available.\n");
  906. }
  907. static int __init init_hw_perf_events(void)
  908. {
  909. struct event_constraint *c;
  910. int err;
  911. pr_info("Performance Events: ");
  912. switch (boot_cpu_data.x86_vendor) {
  913. case X86_VENDOR_INTEL:
  914. err = intel_pmu_init();
  915. break;
  916. case X86_VENDOR_AMD:
  917. err = amd_pmu_init();
  918. break;
  919. default:
  920. return 0;
  921. }
  922. if (err != 0) {
  923. pr_cont("no PMU driver, software events only.\n");
  924. return 0;
  925. }
  926. pmu_check_apic();
  927. /* sanity check that the hardware exists or is emulated */
  928. if (!check_hw_exists())
  929. return 0;
  930. pr_cont("%s PMU driver.\n", x86_pmu.name);
  931. if (x86_pmu.quirks)
  932. x86_pmu.quirks();
  933. if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
  934. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  935. x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
  936. x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
  937. }
  938. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  939. if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
  940. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  941. x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
  942. x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
  943. }
  944. x86_pmu.intel_ctrl |=
  945. ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
  946. perf_events_lapic_init();
  947. register_nmi_handler(NMI_LOCAL, perf_event_nmi_handler, 0, "PMI");
  948. unconstrained = (struct event_constraint)
  949. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  950. 0, x86_pmu.num_counters);
  951. if (x86_pmu.event_constraints) {
  952. for_each_event_constraint(c, x86_pmu.event_constraints) {
  953. if (c->cmask != X86_RAW_EVENT_MASK)
  954. continue;
  955. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  956. c->weight += x86_pmu.num_counters;
  957. }
  958. }
  959. pr_info("... version: %d\n", x86_pmu.version);
  960. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  961. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  962. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  963. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  964. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  965. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  966. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  967. perf_cpu_notifier(x86_pmu_notifier);
  968. return 0;
  969. }
  970. early_initcall(init_hw_perf_events);
  971. static inline void x86_pmu_read(struct perf_event *event)
  972. {
  973. x86_perf_event_update(event);
  974. }
  975. /*
  976. * Start group events scheduling transaction
  977. * Set the flag to make pmu::enable() not perform the
  978. * schedulability test, it will be performed at commit time
  979. */
  980. static void x86_pmu_start_txn(struct pmu *pmu)
  981. {
  982. perf_pmu_disable(pmu);
  983. __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN);
  984. __this_cpu_write(cpu_hw_events.n_txn, 0);
  985. }
  986. /*
  987. * Stop group events scheduling transaction
  988. * Clear the flag and pmu::enable() will perform the
  989. * schedulability test.
  990. */
  991. static void x86_pmu_cancel_txn(struct pmu *pmu)
  992. {
  993. __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN);
  994. /*
  995. * Truncate the collected events.
  996. */
  997. __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
  998. __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
  999. perf_pmu_enable(pmu);
  1000. }
  1001. /*
  1002. * Commit group events scheduling transaction
  1003. * Perform the group schedulability test as a whole
  1004. * Return 0 if success
  1005. */
  1006. static int x86_pmu_commit_txn(struct pmu *pmu)
  1007. {
  1008. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1009. int assign[X86_PMC_IDX_MAX];
  1010. int n, ret;
  1011. n = cpuc->n_events;
  1012. if (!x86_pmu_initialized())
  1013. return -EAGAIN;
  1014. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1015. if (ret)
  1016. return ret;
  1017. /*
  1018. * copy new assignment, now we know it is possible
  1019. * will be used by hw_perf_enable()
  1020. */
  1021. memcpy(cpuc->assign, assign, n*sizeof(int));
  1022. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1023. perf_pmu_enable(pmu);
  1024. return 0;
  1025. }
  1026. /*
  1027. * a fake_cpuc is used to validate event groups. Due to
  1028. * the extra reg logic, we need to also allocate a fake
  1029. * per_core and per_cpu structure. Otherwise, group events
  1030. * using extra reg may conflict without the kernel being
  1031. * able to catch this when the last event gets added to
  1032. * the group.
  1033. */
  1034. static void free_fake_cpuc(struct cpu_hw_events *cpuc)
  1035. {
  1036. kfree(cpuc->shared_regs);
  1037. kfree(cpuc);
  1038. }
  1039. static struct cpu_hw_events *allocate_fake_cpuc(void)
  1040. {
  1041. struct cpu_hw_events *cpuc;
  1042. int cpu = raw_smp_processor_id();
  1043. cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL);
  1044. if (!cpuc)
  1045. return ERR_PTR(-ENOMEM);
  1046. /* only needed, if we have extra_regs */
  1047. if (x86_pmu.extra_regs) {
  1048. cpuc->shared_regs = allocate_shared_regs(cpu);
  1049. if (!cpuc->shared_regs)
  1050. goto error;
  1051. }
  1052. return cpuc;
  1053. error:
  1054. free_fake_cpuc(cpuc);
  1055. return ERR_PTR(-ENOMEM);
  1056. }
  1057. /*
  1058. * validate that we can schedule this event
  1059. */
  1060. static int validate_event(struct perf_event *event)
  1061. {
  1062. struct cpu_hw_events *fake_cpuc;
  1063. struct event_constraint *c;
  1064. int ret = 0;
  1065. fake_cpuc = allocate_fake_cpuc();
  1066. if (IS_ERR(fake_cpuc))
  1067. return PTR_ERR(fake_cpuc);
  1068. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1069. if (!c || !c->weight)
  1070. ret = -EINVAL;
  1071. if (x86_pmu.put_event_constraints)
  1072. x86_pmu.put_event_constraints(fake_cpuc, event);
  1073. free_fake_cpuc(fake_cpuc);
  1074. return ret;
  1075. }
  1076. /*
  1077. * validate a single event group
  1078. *
  1079. * validation include:
  1080. * - check events are compatible which each other
  1081. * - events do not compete for the same counter
  1082. * - number of events <= number of counters
  1083. *
  1084. * validation ensures the group can be loaded onto the
  1085. * PMU if it was the only group available.
  1086. */
  1087. static int validate_group(struct perf_event *event)
  1088. {
  1089. struct perf_event *leader = event->group_leader;
  1090. struct cpu_hw_events *fake_cpuc;
  1091. int ret = -EINVAL, n;
  1092. fake_cpuc = allocate_fake_cpuc();
  1093. if (IS_ERR(fake_cpuc))
  1094. return PTR_ERR(fake_cpuc);
  1095. /*
  1096. * the event is not yet connected with its
  1097. * siblings therefore we must first collect
  1098. * existing siblings, then add the new event
  1099. * before we can simulate the scheduling
  1100. */
  1101. n = collect_events(fake_cpuc, leader, true);
  1102. if (n < 0)
  1103. goto out;
  1104. fake_cpuc->n_events = n;
  1105. n = collect_events(fake_cpuc, event, false);
  1106. if (n < 0)
  1107. goto out;
  1108. fake_cpuc->n_events = n;
  1109. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1110. out:
  1111. free_fake_cpuc(fake_cpuc);
  1112. return ret;
  1113. }
  1114. static int x86_pmu_event_init(struct perf_event *event)
  1115. {
  1116. struct pmu *tmp;
  1117. int err;
  1118. switch (event->attr.type) {
  1119. case PERF_TYPE_RAW:
  1120. case PERF_TYPE_HARDWARE:
  1121. case PERF_TYPE_HW_CACHE:
  1122. break;
  1123. default:
  1124. return -ENOENT;
  1125. }
  1126. err = __x86_pmu_event_init(event);
  1127. if (!err) {
  1128. /*
  1129. * we temporarily connect event to its pmu
  1130. * such that validate_group() can classify
  1131. * it as an x86 event using is_x86_event()
  1132. */
  1133. tmp = event->pmu;
  1134. event->pmu = &pmu;
  1135. if (event->group_leader != event)
  1136. err = validate_group(event);
  1137. else
  1138. err = validate_event(event);
  1139. event->pmu = tmp;
  1140. }
  1141. if (err) {
  1142. if (event->destroy)
  1143. event->destroy(event);
  1144. }
  1145. return err;
  1146. }
  1147. static struct pmu pmu = {
  1148. .pmu_enable = x86_pmu_enable,
  1149. .pmu_disable = x86_pmu_disable,
  1150. .event_init = x86_pmu_event_init,
  1151. .add = x86_pmu_add,
  1152. .del = x86_pmu_del,
  1153. .start = x86_pmu_start,
  1154. .stop = x86_pmu_stop,
  1155. .read = x86_pmu_read,
  1156. .start_txn = x86_pmu_start_txn,
  1157. .cancel_txn = x86_pmu_cancel_txn,
  1158. .commit_txn = x86_pmu_commit_txn,
  1159. };
  1160. /*
  1161. * callchain support
  1162. */
  1163. static int backtrace_stack(void *data, char *name)
  1164. {
  1165. return 0;
  1166. }
  1167. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1168. {
  1169. struct perf_callchain_entry *entry = data;
  1170. perf_callchain_store(entry, addr);
  1171. }
  1172. static const struct stacktrace_ops backtrace_ops = {
  1173. .stack = backtrace_stack,
  1174. .address = backtrace_address,
  1175. .walk_stack = print_context_stack_bp,
  1176. };
  1177. void
  1178. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1179. {
  1180. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1181. /* TODO: We don't support guest os callchain now */
  1182. return;
  1183. }
  1184. perf_callchain_store(entry, regs->ip);
  1185. dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
  1186. }
  1187. #ifdef CONFIG_COMPAT
  1188. static inline int
  1189. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1190. {
  1191. /* 32-bit process in 64-bit kernel. */
  1192. struct stack_frame_ia32 frame;
  1193. const void __user *fp;
  1194. if (!test_thread_flag(TIF_IA32))
  1195. return 0;
  1196. fp = compat_ptr(regs->bp);
  1197. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1198. unsigned long bytes;
  1199. frame.next_frame = 0;
  1200. frame.return_address = 0;
  1201. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1202. if (bytes != sizeof(frame))
  1203. break;
  1204. if (fp < compat_ptr(regs->sp))
  1205. break;
  1206. perf_callchain_store(entry, frame.return_address);
  1207. fp = compat_ptr(frame.next_frame);
  1208. }
  1209. return 1;
  1210. }
  1211. #else
  1212. static inline int
  1213. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1214. {
  1215. return 0;
  1216. }
  1217. #endif
  1218. void
  1219. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1220. {
  1221. struct stack_frame frame;
  1222. const void __user *fp;
  1223. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1224. /* TODO: We don't support guest os callchain now */
  1225. return;
  1226. }
  1227. fp = (void __user *)regs->bp;
  1228. perf_callchain_store(entry, regs->ip);
  1229. if (!current->mm)
  1230. return;
  1231. if (perf_callchain_user32(regs, entry))
  1232. return;
  1233. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1234. unsigned long bytes;
  1235. frame.next_frame = NULL;
  1236. frame.return_address = 0;
  1237. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1238. if (bytes != sizeof(frame))
  1239. break;
  1240. if ((unsigned long)fp < regs->sp)
  1241. break;
  1242. perf_callchain_store(entry, frame.return_address);
  1243. fp = frame.next_frame;
  1244. }
  1245. }
  1246. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1247. {
  1248. unsigned long ip;
  1249. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1250. ip = perf_guest_cbs->get_guest_ip();
  1251. else
  1252. ip = instruction_pointer(regs);
  1253. return ip;
  1254. }
  1255. unsigned long perf_misc_flags(struct pt_regs *regs)
  1256. {
  1257. int misc = 0;
  1258. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1259. if (perf_guest_cbs->is_user_mode())
  1260. misc |= PERF_RECORD_MISC_GUEST_USER;
  1261. else
  1262. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1263. } else {
  1264. if (user_mode(regs))
  1265. misc |= PERF_RECORD_MISC_USER;
  1266. else
  1267. misc |= PERF_RECORD_MISC_KERNEL;
  1268. }
  1269. if (regs->flags & PERF_EFLAGS_EXACT)
  1270. misc |= PERF_RECORD_MISC_EXACT_IP;
  1271. return misc;
  1272. }