perf_event.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180
  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 <linux/device.h>
  27. #include <asm/apic.h>
  28. #include <asm/stacktrace.h>
  29. #include <asm/nmi.h>
  30. #include <asm/smp.h>
  31. #include <asm/alternative.h>
  32. #include <asm/timer.h>
  33. #include <asm/desc.h>
  34. #include <asm/ldt.h>
  35. #include "perf_event.h"
  36. struct x86_pmu x86_pmu __read_mostly;
  37. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  38. .enabled = 1,
  39. };
  40. u64 __read_mostly hw_cache_event_ids
  41. [PERF_COUNT_HW_CACHE_MAX]
  42. [PERF_COUNT_HW_CACHE_OP_MAX]
  43. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  44. u64 __read_mostly hw_cache_extra_regs
  45. [PERF_COUNT_HW_CACHE_MAX]
  46. [PERF_COUNT_HW_CACHE_OP_MAX]
  47. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  48. /*
  49. * Propagate event elapsed time into the generic event.
  50. * Can only be executed on the CPU where the event is active.
  51. * Returns the delta events processed.
  52. */
  53. u64 x86_perf_event_update(struct perf_event *event)
  54. {
  55. struct hw_perf_event *hwc = &event->hw;
  56. int shift = 64 - x86_pmu.cntval_bits;
  57. u64 prev_raw_count, new_raw_count;
  58. int idx = hwc->idx;
  59. s64 delta;
  60. if (idx == INTEL_PMC_IDX_FIXED_BTS)
  61. return 0;
  62. /*
  63. * Careful: an NMI might modify the previous event value.
  64. *
  65. * Our tactic to handle this is to first atomically read and
  66. * exchange a new raw count - then add that new-prev delta
  67. * count to the generic event atomically:
  68. */
  69. again:
  70. prev_raw_count = local64_read(&hwc->prev_count);
  71. rdpmcl(hwc->event_base_rdpmc, new_raw_count);
  72. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  73. new_raw_count) != prev_raw_count)
  74. goto again;
  75. /*
  76. * Now we have the new raw value and have updated the prev
  77. * timestamp already. We can now calculate the elapsed delta
  78. * (event-)time and add that to the generic event.
  79. *
  80. * Careful, not all hw sign-extends above the physical width
  81. * of the count.
  82. */
  83. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  84. delta >>= shift;
  85. local64_add(delta, &event->count);
  86. local64_sub(delta, &hwc->period_left);
  87. return new_raw_count;
  88. }
  89. /*
  90. * Find and validate any extra registers to set up.
  91. */
  92. static int x86_pmu_extra_regs(u64 config, struct perf_event *event)
  93. {
  94. struct hw_perf_event_extra *reg;
  95. struct extra_reg *er;
  96. reg = &event->hw.extra_reg;
  97. if (!x86_pmu.extra_regs)
  98. return 0;
  99. for (er = x86_pmu.extra_regs; er->msr; er++) {
  100. if (er->event != (config & er->config_mask))
  101. continue;
  102. if (event->attr.config1 & ~er->valid_mask)
  103. return -EINVAL;
  104. /* Check if the extra msrs can be safely accessed*/
  105. if (!er->extra_msr_access)
  106. return -ENXIO;
  107. reg->idx = er->idx;
  108. reg->config = event->attr.config1;
  109. reg->reg = er->msr;
  110. break;
  111. }
  112. return 0;
  113. }
  114. static atomic_t active_events;
  115. static DEFINE_MUTEX(pmc_reserve_mutex);
  116. #ifdef CONFIG_X86_LOCAL_APIC
  117. static bool reserve_pmc_hardware(void)
  118. {
  119. int i;
  120. for (i = 0; i < x86_pmu.num_counters; i++) {
  121. if (!reserve_perfctr_nmi(x86_pmu_event_addr(i)))
  122. goto perfctr_fail;
  123. }
  124. for (i = 0; i < x86_pmu.num_counters; i++) {
  125. if (!reserve_evntsel_nmi(x86_pmu_config_addr(i)))
  126. goto eventsel_fail;
  127. }
  128. return true;
  129. eventsel_fail:
  130. for (i--; i >= 0; i--)
  131. release_evntsel_nmi(x86_pmu_config_addr(i));
  132. i = x86_pmu.num_counters;
  133. perfctr_fail:
  134. for (i--; i >= 0; i--)
  135. release_perfctr_nmi(x86_pmu_event_addr(i));
  136. return false;
  137. }
  138. static void release_pmc_hardware(void)
  139. {
  140. int i;
  141. for (i = 0; i < x86_pmu.num_counters; i++) {
  142. release_perfctr_nmi(x86_pmu_event_addr(i));
  143. release_evntsel_nmi(x86_pmu_config_addr(i));
  144. }
  145. }
  146. #else
  147. static bool reserve_pmc_hardware(void) { return true; }
  148. static void release_pmc_hardware(void) {}
  149. #endif
  150. static bool check_hw_exists(void)
  151. {
  152. u64 val, val_fail, val_new= ~0;
  153. int i, reg, reg_fail, ret = 0;
  154. int bios_fail = 0;
  155. /*
  156. * Check to see if the BIOS enabled any of the counters, if so
  157. * complain and bail.
  158. */
  159. for (i = 0; i < x86_pmu.num_counters; i++) {
  160. reg = x86_pmu_config_addr(i);
  161. ret = rdmsrl_safe(reg, &val);
  162. if (ret)
  163. goto msr_fail;
  164. if (val & ARCH_PERFMON_EVENTSEL_ENABLE) {
  165. bios_fail = 1;
  166. val_fail = val;
  167. reg_fail = reg;
  168. }
  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. bios_fail = 1;
  178. val_fail = val;
  179. reg_fail = reg;
  180. }
  181. }
  182. }
  183. /*
  184. * Read the current value, change it and read it back to see if it
  185. * matches, this is needed to detect certain hardware emulators
  186. * (qemu/kvm) that don't trap on the MSR access and always return 0s.
  187. */
  188. reg = x86_pmu_event_addr(0);
  189. if (rdmsrl_safe(reg, &val))
  190. goto msr_fail;
  191. val ^= 0xffffUL;
  192. ret = wrmsrl_safe(reg, val);
  193. ret |= rdmsrl_safe(reg, &val_new);
  194. if (ret || val != val_new)
  195. goto msr_fail;
  196. /*
  197. * We still allow the PMU driver to operate:
  198. */
  199. if (bios_fail) {
  200. printk(KERN_CONT "Broken BIOS detected, complain to your hardware vendor.\n");
  201. printk(KERN_ERR FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg_fail, val_fail);
  202. }
  203. return true;
  204. msr_fail:
  205. printk(KERN_CONT "Broken PMU hardware detected, using software events only.\n");
  206. printk(boot_cpu_has(X86_FEATURE_HYPERVISOR) ? KERN_INFO : KERN_ERR
  207. "Failed to access perfctr msr (MSR %x is %Lx)\n", reg, val_new);
  208. return false;
  209. }
  210. static void hw_perf_event_destroy(struct perf_event *event)
  211. {
  212. if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
  213. release_pmc_hardware();
  214. release_ds_buffers();
  215. mutex_unlock(&pmc_reserve_mutex);
  216. }
  217. }
  218. static inline int x86_pmu_initialized(void)
  219. {
  220. return x86_pmu.handle_irq != NULL;
  221. }
  222. static inline int
  223. set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)
  224. {
  225. struct perf_event_attr *attr = &event->attr;
  226. unsigned int cache_type, cache_op, cache_result;
  227. u64 config, val;
  228. config = attr->config;
  229. cache_type = (config >> 0) & 0xff;
  230. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  231. return -EINVAL;
  232. cache_op = (config >> 8) & 0xff;
  233. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  234. return -EINVAL;
  235. cache_result = (config >> 16) & 0xff;
  236. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  237. return -EINVAL;
  238. val = hw_cache_event_ids[cache_type][cache_op][cache_result];
  239. if (val == 0)
  240. return -ENOENT;
  241. if (val == -1)
  242. return -EINVAL;
  243. hwc->config |= val;
  244. attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result];
  245. return x86_pmu_extra_regs(val, event);
  246. }
  247. int x86_setup_perfctr(struct perf_event *event)
  248. {
  249. struct perf_event_attr *attr = &event->attr;
  250. struct hw_perf_event *hwc = &event->hw;
  251. u64 config;
  252. if (!is_sampling_event(event)) {
  253. hwc->sample_period = x86_pmu.max_period;
  254. hwc->last_period = hwc->sample_period;
  255. local64_set(&hwc->period_left, hwc->sample_period);
  256. }
  257. if (attr->type == PERF_TYPE_RAW)
  258. return x86_pmu_extra_regs(event->attr.config, event);
  259. if (attr->type == PERF_TYPE_HW_CACHE)
  260. return set_ext_hw_attr(hwc, event);
  261. if (attr->config >= x86_pmu.max_events)
  262. return -EINVAL;
  263. /*
  264. * The generic map:
  265. */
  266. config = x86_pmu.event_map(attr->config);
  267. if (config == 0)
  268. return -ENOENT;
  269. if (config == -1LL)
  270. return -EINVAL;
  271. /*
  272. * Branch tracing:
  273. */
  274. if (attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
  275. !attr->freq && hwc->sample_period == 1) {
  276. /* BTS is not supported by this architecture. */
  277. if (!x86_pmu.bts_active)
  278. return -EOPNOTSUPP;
  279. /* BTS is currently only allowed for user-mode. */
  280. if (!attr->exclude_kernel)
  281. return -EOPNOTSUPP;
  282. }
  283. hwc->config |= config;
  284. return 0;
  285. }
  286. /*
  287. * check that branch_sample_type is compatible with
  288. * settings needed for precise_ip > 1 which implies
  289. * using the LBR to capture ALL taken branches at the
  290. * priv levels of the measurement
  291. */
  292. static inline int precise_br_compat(struct perf_event *event)
  293. {
  294. u64 m = event->attr.branch_sample_type;
  295. u64 b = 0;
  296. /* must capture all branches */
  297. if (!(m & PERF_SAMPLE_BRANCH_ANY))
  298. return 0;
  299. m &= PERF_SAMPLE_BRANCH_KERNEL | PERF_SAMPLE_BRANCH_USER;
  300. if (!event->attr.exclude_user)
  301. b |= PERF_SAMPLE_BRANCH_USER;
  302. if (!event->attr.exclude_kernel)
  303. b |= PERF_SAMPLE_BRANCH_KERNEL;
  304. /*
  305. * ignore PERF_SAMPLE_BRANCH_HV, not supported on x86
  306. */
  307. return m == b;
  308. }
  309. int x86_pmu_hw_config(struct perf_event *event)
  310. {
  311. if (event->attr.precise_ip) {
  312. int precise = 0;
  313. /* Support for constant skid */
  314. if (x86_pmu.pebs_active && !x86_pmu.pebs_broken) {
  315. precise++;
  316. /* Support for IP fixup */
  317. if (x86_pmu.lbr_nr || x86_pmu.intel_cap.pebs_format >= 2)
  318. precise++;
  319. }
  320. if (event->attr.precise_ip > precise)
  321. return -EOPNOTSUPP;
  322. /*
  323. * check that PEBS LBR correction does not conflict with
  324. * whatever the user is asking with attr->branch_sample_type
  325. */
  326. if (event->attr.precise_ip > 1 &&
  327. x86_pmu.intel_cap.pebs_format < 2) {
  328. u64 *br_type = &event->attr.branch_sample_type;
  329. if (has_branch_stack(event)) {
  330. if (!precise_br_compat(event))
  331. return -EOPNOTSUPP;
  332. /* branch_sample_type is compatible */
  333. } else {
  334. /*
  335. * user did not specify branch_sample_type
  336. *
  337. * For PEBS fixups, we capture all
  338. * the branches at the priv level of the
  339. * event.
  340. */
  341. *br_type = PERF_SAMPLE_BRANCH_ANY;
  342. if (!event->attr.exclude_user)
  343. *br_type |= PERF_SAMPLE_BRANCH_USER;
  344. if (!event->attr.exclude_kernel)
  345. *br_type |= PERF_SAMPLE_BRANCH_KERNEL;
  346. }
  347. }
  348. }
  349. /*
  350. * Generate PMC IRQs:
  351. * (keep 'enabled' bit clear for now)
  352. */
  353. event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
  354. /*
  355. * Count user and OS events unless requested not to
  356. */
  357. if (!event->attr.exclude_user)
  358. event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
  359. if (!event->attr.exclude_kernel)
  360. event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
  361. if (event->attr.type == PERF_TYPE_RAW)
  362. event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
  363. if (event->attr.sample_period && x86_pmu.limit_period) {
  364. if (x86_pmu.limit_period(event, event->attr.sample_period) >
  365. event->attr.sample_period)
  366. return -EINVAL;
  367. }
  368. return x86_setup_perfctr(event);
  369. }
  370. /*
  371. * Setup the hardware configuration for a given attr_type
  372. */
  373. static int __x86_pmu_event_init(struct perf_event *event)
  374. {
  375. int err;
  376. if (!x86_pmu_initialized())
  377. return -ENODEV;
  378. err = 0;
  379. if (!atomic_inc_not_zero(&active_events)) {
  380. mutex_lock(&pmc_reserve_mutex);
  381. if (atomic_read(&active_events) == 0) {
  382. if (!reserve_pmc_hardware())
  383. err = -EBUSY;
  384. else
  385. reserve_ds_buffers();
  386. }
  387. if (!err)
  388. atomic_inc(&active_events);
  389. mutex_unlock(&pmc_reserve_mutex);
  390. }
  391. if (err)
  392. return err;
  393. event->destroy = hw_perf_event_destroy;
  394. event->hw.idx = -1;
  395. event->hw.last_cpu = -1;
  396. event->hw.last_tag = ~0ULL;
  397. /* mark unused */
  398. event->hw.extra_reg.idx = EXTRA_REG_NONE;
  399. event->hw.branch_reg.idx = EXTRA_REG_NONE;
  400. return x86_pmu.hw_config(event);
  401. }
  402. void x86_pmu_disable_all(void)
  403. {
  404. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  405. int idx;
  406. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  407. u64 val;
  408. if (!test_bit(idx, cpuc->active_mask))
  409. continue;
  410. rdmsrl(x86_pmu_config_addr(idx), val);
  411. if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
  412. continue;
  413. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  414. wrmsrl(x86_pmu_config_addr(idx), val);
  415. }
  416. }
  417. static void x86_pmu_disable(struct pmu *pmu)
  418. {
  419. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  420. if (!x86_pmu_initialized())
  421. return;
  422. if (!cpuc->enabled)
  423. return;
  424. cpuc->n_added = 0;
  425. cpuc->enabled = 0;
  426. barrier();
  427. x86_pmu.disable_all();
  428. }
  429. void x86_pmu_enable_all(int added)
  430. {
  431. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  432. int idx;
  433. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  434. struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
  435. if (!test_bit(idx, cpuc->active_mask))
  436. continue;
  437. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  438. }
  439. }
  440. static struct pmu pmu;
  441. static inline int is_x86_event(struct perf_event *event)
  442. {
  443. return event->pmu == &pmu;
  444. }
  445. /*
  446. * Event scheduler state:
  447. *
  448. * Assign events iterating over all events and counters, beginning
  449. * with events with least weights first. Keep the current iterator
  450. * state in struct sched_state.
  451. */
  452. struct sched_state {
  453. int weight;
  454. int event; /* event index */
  455. int counter; /* counter index */
  456. int unassigned; /* number of events to be assigned left */
  457. unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  458. };
  459. /* Total max is X86_PMC_IDX_MAX, but we are O(n!) limited */
  460. #define SCHED_STATES_MAX 2
  461. struct perf_sched {
  462. int max_weight;
  463. int max_events;
  464. struct perf_event **events;
  465. struct sched_state state;
  466. int saved_states;
  467. struct sched_state saved[SCHED_STATES_MAX];
  468. };
  469. /*
  470. * Initialize interator that runs through all events and counters.
  471. */
  472. static void perf_sched_init(struct perf_sched *sched, struct perf_event **events,
  473. int num, int wmin, int wmax)
  474. {
  475. int idx;
  476. memset(sched, 0, sizeof(*sched));
  477. sched->max_events = num;
  478. sched->max_weight = wmax;
  479. sched->events = events;
  480. for (idx = 0; idx < num; idx++) {
  481. if (events[idx]->hw.constraint->weight == wmin)
  482. break;
  483. }
  484. sched->state.event = idx; /* start with min weight */
  485. sched->state.weight = wmin;
  486. sched->state.unassigned = num;
  487. }
  488. static void perf_sched_save_state(struct perf_sched *sched)
  489. {
  490. if (WARN_ON_ONCE(sched->saved_states >= SCHED_STATES_MAX))
  491. return;
  492. sched->saved[sched->saved_states] = sched->state;
  493. sched->saved_states++;
  494. }
  495. static bool perf_sched_restore_state(struct perf_sched *sched)
  496. {
  497. if (!sched->saved_states)
  498. return false;
  499. sched->saved_states--;
  500. sched->state = sched->saved[sched->saved_states];
  501. /* continue with next counter: */
  502. clear_bit(sched->state.counter++, sched->state.used);
  503. return true;
  504. }
  505. /*
  506. * Select a counter for the current event to schedule. Return true on
  507. * success.
  508. */
  509. static bool __perf_sched_find_counter(struct perf_sched *sched)
  510. {
  511. struct event_constraint *c;
  512. int idx;
  513. if (!sched->state.unassigned)
  514. return false;
  515. if (sched->state.event >= sched->max_events)
  516. return false;
  517. c = sched->events[sched->state.event]->hw.constraint;
  518. /* Prefer fixed purpose counters */
  519. if (c->idxmsk64 & (~0ULL << INTEL_PMC_IDX_FIXED)) {
  520. idx = INTEL_PMC_IDX_FIXED;
  521. for_each_set_bit_from(idx, c->idxmsk, X86_PMC_IDX_MAX) {
  522. if (!__test_and_set_bit(idx, sched->state.used))
  523. goto done;
  524. }
  525. }
  526. /* Grab the first unused counter starting with idx */
  527. idx = sched->state.counter;
  528. for_each_set_bit_from(idx, c->idxmsk, INTEL_PMC_IDX_FIXED) {
  529. if (!__test_and_set_bit(idx, sched->state.used))
  530. goto done;
  531. }
  532. return false;
  533. done:
  534. sched->state.counter = idx;
  535. if (c->overlap)
  536. perf_sched_save_state(sched);
  537. return true;
  538. }
  539. static bool perf_sched_find_counter(struct perf_sched *sched)
  540. {
  541. while (!__perf_sched_find_counter(sched)) {
  542. if (!perf_sched_restore_state(sched))
  543. return false;
  544. }
  545. return true;
  546. }
  547. /*
  548. * Go through all unassigned events and find the next one to schedule.
  549. * Take events with the least weight first. Return true on success.
  550. */
  551. static bool perf_sched_next_event(struct perf_sched *sched)
  552. {
  553. struct event_constraint *c;
  554. if (!sched->state.unassigned || !--sched->state.unassigned)
  555. return false;
  556. do {
  557. /* next event */
  558. sched->state.event++;
  559. if (sched->state.event >= sched->max_events) {
  560. /* next weight */
  561. sched->state.event = 0;
  562. sched->state.weight++;
  563. if (sched->state.weight > sched->max_weight)
  564. return false;
  565. }
  566. c = sched->events[sched->state.event]->hw.constraint;
  567. } while (c->weight != sched->state.weight);
  568. sched->state.counter = 0; /* start with first counter */
  569. return true;
  570. }
  571. /*
  572. * Assign a counter for each event.
  573. */
  574. int perf_assign_events(struct perf_event **events, int n,
  575. int wmin, int wmax, int *assign)
  576. {
  577. struct perf_sched sched;
  578. perf_sched_init(&sched, events, n, wmin, wmax);
  579. do {
  580. if (!perf_sched_find_counter(&sched))
  581. break; /* failed */
  582. if (assign)
  583. assign[sched.state.event] = sched.state.counter;
  584. } while (perf_sched_next_event(&sched));
  585. return sched.state.unassigned;
  586. }
  587. EXPORT_SYMBOL_GPL(perf_assign_events);
  588. int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  589. {
  590. struct event_constraint *c;
  591. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  592. struct perf_event *e;
  593. int i, wmin, wmax, num = 0;
  594. struct hw_perf_event *hwc;
  595. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  596. for (i = 0, wmin = X86_PMC_IDX_MAX, wmax = 0; i < n; i++) {
  597. hwc = &cpuc->event_list[i]->hw;
  598. c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
  599. hwc->constraint = c;
  600. wmin = min(wmin, c->weight);
  601. wmax = max(wmax, c->weight);
  602. }
  603. /*
  604. * fastpath, try to reuse previous register
  605. */
  606. for (i = 0; i < n; i++) {
  607. hwc = &cpuc->event_list[i]->hw;
  608. c = hwc->constraint;
  609. /* never assigned */
  610. if (hwc->idx == -1)
  611. break;
  612. /* constraint still honored */
  613. if (!test_bit(hwc->idx, c->idxmsk))
  614. break;
  615. /* not already used */
  616. if (test_bit(hwc->idx, used_mask))
  617. break;
  618. __set_bit(hwc->idx, used_mask);
  619. if (assign)
  620. assign[i] = hwc->idx;
  621. }
  622. /* slow path */
  623. if (i != n)
  624. num = perf_assign_events(cpuc->event_list, n, wmin,
  625. wmax, assign);
  626. /*
  627. * Mark the event as committed, so we do not put_constraint()
  628. * in case new events are added and fail scheduling.
  629. */
  630. if (!num && assign) {
  631. for (i = 0; i < n; i++) {
  632. e = cpuc->event_list[i];
  633. e->hw.flags |= PERF_X86_EVENT_COMMITTED;
  634. }
  635. }
  636. /*
  637. * scheduling failed or is just a simulation,
  638. * free resources if necessary
  639. */
  640. if (!assign || num) {
  641. for (i = 0; i < n; i++) {
  642. e = cpuc->event_list[i];
  643. /*
  644. * do not put_constraint() on comitted events,
  645. * because they are good to go
  646. */
  647. if ((e->hw.flags & PERF_X86_EVENT_COMMITTED))
  648. continue;
  649. if (x86_pmu.put_event_constraints)
  650. x86_pmu.put_event_constraints(cpuc, e);
  651. }
  652. }
  653. return num ? -EINVAL : 0;
  654. }
  655. /*
  656. * dogrp: true if must collect siblings events (group)
  657. * returns total number of events and error code
  658. */
  659. static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
  660. {
  661. struct perf_event *event;
  662. int n, max_count;
  663. max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
  664. /* current number of events already accepted */
  665. n = cpuc->n_events;
  666. if (is_x86_event(leader)) {
  667. if (n >= max_count)
  668. return -EINVAL;
  669. cpuc->event_list[n] = leader;
  670. n++;
  671. }
  672. if (!dogrp)
  673. return n;
  674. list_for_each_entry(event, &leader->sibling_list, group_entry) {
  675. if (!is_x86_event(event) ||
  676. event->state <= PERF_EVENT_STATE_OFF)
  677. continue;
  678. if (n >= max_count)
  679. return -EINVAL;
  680. cpuc->event_list[n] = event;
  681. n++;
  682. }
  683. return n;
  684. }
  685. static inline void x86_assign_hw_event(struct perf_event *event,
  686. struct cpu_hw_events *cpuc, int i)
  687. {
  688. struct hw_perf_event *hwc = &event->hw;
  689. hwc->idx = cpuc->assign[i];
  690. hwc->last_cpu = smp_processor_id();
  691. hwc->last_tag = ++cpuc->tags[i];
  692. if (hwc->idx == INTEL_PMC_IDX_FIXED_BTS) {
  693. hwc->config_base = 0;
  694. hwc->event_base = 0;
  695. } else if (hwc->idx >= INTEL_PMC_IDX_FIXED) {
  696. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  697. hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - INTEL_PMC_IDX_FIXED);
  698. hwc->event_base_rdpmc = (hwc->idx - INTEL_PMC_IDX_FIXED) | 1<<30;
  699. } else {
  700. hwc->config_base = x86_pmu_config_addr(hwc->idx);
  701. hwc->event_base = x86_pmu_event_addr(hwc->idx);
  702. hwc->event_base_rdpmc = x86_pmu_rdpmc_index(hwc->idx);
  703. }
  704. }
  705. static inline int match_prev_assignment(struct hw_perf_event *hwc,
  706. struct cpu_hw_events *cpuc,
  707. int i)
  708. {
  709. return hwc->idx == cpuc->assign[i] &&
  710. hwc->last_cpu == smp_processor_id() &&
  711. hwc->last_tag == cpuc->tags[i];
  712. }
  713. static void x86_pmu_start(struct perf_event *event, int flags);
  714. static void x86_pmu_enable(struct pmu *pmu)
  715. {
  716. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  717. struct perf_event *event;
  718. struct hw_perf_event *hwc;
  719. int i, added = cpuc->n_added;
  720. if (!x86_pmu_initialized())
  721. return;
  722. if (cpuc->enabled)
  723. return;
  724. if (cpuc->n_added) {
  725. int n_running = cpuc->n_events - cpuc->n_added;
  726. /*
  727. * apply assignment obtained either from
  728. * hw_perf_group_sched_in() or x86_pmu_enable()
  729. *
  730. * step1: save events moving to new counters
  731. */
  732. for (i = 0; i < n_running; i++) {
  733. event = cpuc->event_list[i];
  734. hwc = &event->hw;
  735. /*
  736. * we can avoid reprogramming counter if:
  737. * - assigned same counter as last time
  738. * - running on same CPU as last time
  739. * - no other event has used the counter since
  740. */
  741. if (hwc->idx == -1 ||
  742. match_prev_assignment(hwc, cpuc, i))
  743. continue;
  744. /*
  745. * Ensure we don't accidentally enable a stopped
  746. * counter simply because we rescheduled.
  747. */
  748. if (hwc->state & PERF_HES_STOPPED)
  749. hwc->state |= PERF_HES_ARCH;
  750. x86_pmu_stop(event, PERF_EF_UPDATE);
  751. }
  752. /*
  753. * step2: reprogram moved events into new counters
  754. */
  755. for (i = 0; i < cpuc->n_events; i++) {
  756. event = cpuc->event_list[i];
  757. hwc = &event->hw;
  758. if (!match_prev_assignment(hwc, cpuc, i))
  759. x86_assign_hw_event(event, cpuc, i);
  760. else if (i < n_running)
  761. continue;
  762. if (hwc->state & PERF_HES_ARCH)
  763. continue;
  764. x86_pmu_start(event, PERF_EF_RELOAD);
  765. }
  766. cpuc->n_added = 0;
  767. perf_events_lapic_init();
  768. }
  769. cpuc->enabled = 1;
  770. barrier();
  771. x86_pmu.enable_all(added);
  772. }
  773. static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  774. /*
  775. * Set the next IRQ period, based on the hwc->period_left value.
  776. * To be called with the event disabled in hw:
  777. */
  778. int x86_perf_event_set_period(struct perf_event *event)
  779. {
  780. struct hw_perf_event *hwc = &event->hw;
  781. s64 left = local64_read(&hwc->period_left);
  782. s64 period = hwc->sample_period;
  783. int ret = 0, idx = hwc->idx;
  784. if (idx == INTEL_PMC_IDX_FIXED_BTS)
  785. return 0;
  786. /*
  787. * If we are way outside a reasonable range then just skip forward:
  788. */
  789. if (unlikely(left <= -period)) {
  790. left = period;
  791. local64_set(&hwc->period_left, left);
  792. hwc->last_period = period;
  793. ret = 1;
  794. }
  795. if (unlikely(left <= 0)) {
  796. left += period;
  797. local64_set(&hwc->period_left, left);
  798. hwc->last_period = period;
  799. ret = 1;
  800. }
  801. /*
  802. * Quirk: certain CPUs dont like it if just 1 hw_event is left:
  803. */
  804. if (unlikely(left < 2))
  805. left = 2;
  806. if (left > x86_pmu.max_period)
  807. left = x86_pmu.max_period;
  808. if (x86_pmu.limit_period)
  809. left = x86_pmu.limit_period(event, left);
  810. per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
  811. /*
  812. * The hw event starts counting from this event offset,
  813. * mark it to be able to extra future deltas:
  814. */
  815. local64_set(&hwc->prev_count, (u64)-left);
  816. wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
  817. /*
  818. * Due to erratum on certan cpu we need
  819. * a second write to be sure the register
  820. * is updated properly
  821. */
  822. if (x86_pmu.perfctr_second_write) {
  823. wrmsrl(hwc->event_base,
  824. (u64)(-left) & x86_pmu.cntval_mask);
  825. }
  826. perf_event_update_userpage(event);
  827. return ret;
  828. }
  829. void x86_pmu_enable_event(struct perf_event *event)
  830. {
  831. if (__this_cpu_read(cpu_hw_events.enabled))
  832. __x86_pmu_enable_event(&event->hw,
  833. ARCH_PERFMON_EVENTSEL_ENABLE);
  834. }
  835. /*
  836. * Add a single event to the PMU.
  837. *
  838. * The event is added to the group of enabled events
  839. * but only if it can be scehduled with existing events.
  840. */
  841. static int x86_pmu_add(struct perf_event *event, int flags)
  842. {
  843. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  844. struct hw_perf_event *hwc;
  845. int assign[X86_PMC_IDX_MAX];
  846. int n, n0, ret;
  847. hwc = &event->hw;
  848. perf_pmu_disable(event->pmu);
  849. n0 = cpuc->n_events;
  850. ret = n = collect_events(cpuc, event, false);
  851. if (ret < 0)
  852. goto out;
  853. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  854. if (!(flags & PERF_EF_START))
  855. hwc->state |= PERF_HES_ARCH;
  856. /*
  857. * If group events scheduling transaction was started,
  858. * skip the schedulability test here, it will be performed
  859. * at commit time (->commit_txn) as a whole.
  860. */
  861. if (cpuc->group_flag & PERF_EVENT_TXN)
  862. goto done_collect;
  863. ret = x86_pmu.schedule_events(cpuc, n, assign);
  864. if (ret)
  865. goto out;
  866. /*
  867. * copy new assignment, now we know it is possible
  868. * will be used by hw_perf_enable()
  869. */
  870. memcpy(cpuc->assign, assign, n*sizeof(int));
  871. done_collect:
  872. /*
  873. * Commit the collect_events() state. See x86_pmu_del() and
  874. * x86_pmu_*_txn().
  875. */
  876. cpuc->n_events = n;
  877. cpuc->n_added += n - n0;
  878. cpuc->n_txn += n - n0;
  879. ret = 0;
  880. out:
  881. perf_pmu_enable(event->pmu);
  882. return ret;
  883. }
  884. static void x86_pmu_start(struct perf_event *event, int flags)
  885. {
  886. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  887. int idx = event->hw.idx;
  888. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  889. return;
  890. if (WARN_ON_ONCE(idx == -1))
  891. return;
  892. if (flags & PERF_EF_RELOAD) {
  893. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  894. x86_perf_event_set_period(event);
  895. }
  896. event->hw.state = 0;
  897. cpuc->events[idx] = event;
  898. __set_bit(idx, cpuc->active_mask);
  899. __set_bit(idx, cpuc->running);
  900. x86_pmu.enable(event);
  901. perf_event_update_userpage(event);
  902. }
  903. void perf_event_print_debug(void)
  904. {
  905. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  906. u64 pebs;
  907. struct cpu_hw_events *cpuc;
  908. unsigned long flags;
  909. int cpu, idx;
  910. if (!x86_pmu.num_counters)
  911. return;
  912. local_irq_save(flags);
  913. cpu = smp_processor_id();
  914. cpuc = &per_cpu(cpu_hw_events, cpu);
  915. if (x86_pmu.version >= 2) {
  916. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  917. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  918. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  919. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  920. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  921. pr_info("\n");
  922. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  923. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  924. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  925. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  926. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  927. }
  928. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  929. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  930. rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
  931. rdmsrl(x86_pmu_event_addr(idx), pmc_count);
  932. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  933. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  934. cpu, idx, pmc_ctrl);
  935. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  936. cpu, idx, pmc_count);
  937. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  938. cpu, idx, prev_left);
  939. }
  940. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  941. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  942. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  943. cpu, idx, pmc_count);
  944. }
  945. local_irq_restore(flags);
  946. }
  947. void x86_pmu_stop(struct perf_event *event, int flags)
  948. {
  949. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  950. struct hw_perf_event *hwc = &event->hw;
  951. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  952. x86_pmu.disable(event);
  953. cpuc->events[hwc->idx] = NULL;
  954. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  955. hwc->state |= PERF_HES_STOPPED;
  956. }
  957. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  958. /*
  959. * Drain the remaining delta count out of a event
  960. * that we are disabling:
  961. */
  962. x86_perf_event_update(event);
  963. hwc->state |= PERF_HES_UPTODATE;
  964. }
  965. }
  966. static void x86_pmu_del(struct perf_event *event, int flags)
  967. {
  968. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  969. int i;
  970. /*
  971. * event is descheduled
  972. */
  973. event->hw.flags &= ~PERF_X86_EVENT_COMMITTED;
  974. /*
  975. * If we're called during a txn, we don't need to do anything.
  976. * The events never got scheduled and ->cancel_txn will truncate
  977. * the event_list.
  978. *
  979. * XXX assumes any ->del() called during a TXN will only be on
  980. * an event added during that same TXN.
  981. */
  982. if (cpuc->group_flag & PERF_EVENT_TXN)
  983. return;
  984. /*
  985. * Not a TXN, therefore cleanup properly.
  986. */
  987. x86_pmu_stop(event, PERF_EF_UPDATE);
  988. for (i = 0; i < cpuc->n_events; i++) {
  989. if (event == cpuc->event_list[i])
  990. break;
  991. }
  992. if (WARN_ON_ONCE(i == cpuc->n_events)) /* called ->del() without ->add() ? */
  993. return;
  994. /* If we have a newly added event; make sure to decrease n_added. */
  995. if (i >= cpuc->n_events - cpuc->n_added)
  996. --cpuc->n_added;
  997. if (x86_pmu.put_event_constraints)
  998. x86_pmu.put_event_constraints(cpuc, event);
  999. /* Delete the array entry. */
  1000. while (++i < cpuc->n_events)
  1001. cpuc->event_list[i-1] = cpuc->event_list[i];
  1002. --cpuc->n_events;
  1003. perf_event_update_userpage(event);
  1004. }
  1005. int x86_pmu_handle_irq(struct pt_regs *regs)
  1006. {
  1007. struct perf_sample_data data;
  1008. struct cpu_hw_events *cpuc;
  1009. struct perf_event *event;
  1010. int idx, handled = 0;
  1011. u64 val;
  1012. cpuc = this_cpu_ptr(&cpu_hw_events);
  1013. /*
  1014. * Some chipsets need to unmask the LVTPC in a particular spot
  1015. * inside the nmi handler. As a result, the unmasking was pushed
  1016. * into all the nmi handlers.
  1017. *
  1018. * This generic handler doesn't seem to have any issues where the
  1019. * unmasking occurs so it was left at the top.
  1020. */
  1021. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1022. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1023. if (!test_bit(idx, cpuc->active_mask)) {
  1024. /*
  1025. * Though we deactivated the counter some cpus
  1026. * might still deliver spurious interrupts still
  1027. * in flight. Catch them:
  1028. */
  1029. if (__test_and_clear_bit(idx, cpuc->running))
  1030. handled++;
  1031. continue;
  1032. }
  1033. event = cpuc->events[idx];
  1034. val = x86_perf_event_update(event);
  1035. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  1036. continue;
  1037. /*
  1038. * event overflow
  1039. */
  1040. handled++;
  1041. perf_sample_data_init(&data, 0, event->hw.last_period);
  1042. if (!x86_perf_event_set_period(event))
  1043. continue;
  1044. if (perf_event_overflow(event, &data, regs))
  1045. x86_pmu_stop(event, 0);
  1046. }
  1047. if (handled)
  1048. inc_irq_stat(apic_perf_irqs);
  1049. return handled;
  1050. }
  1051. void perf_events_lapic_init(void)
  1052. {
  1053. if (!x86_pmu.apic || !x86_pmu_initialized())
  1054. return;
  1055. /*
  1056. * Always use NMI for PMU
  1057. */
  1058. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1059. }
  1060. static int
  1061. perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  1062. {
  1063. u64 start_clock;
  1064. u64 finish_clock;
  1065. int ret;
  1066. if (!atomic_read(&active_events))
  1067. return NMI_DONE;
  1068. start_clock = sched_clock();
  1069. ret = x86_pmu.handle_irq(regs);
  1070. finish_clock = sched_clock();
  1071. perf_sample_event_took(finish_clock - start_clock);
  1072. return ret;
  1073. }
  1074. NOKPROBE_SYMBOL(perf_event_nmi_handler);
  1075. struct event_constraint emptyconstraint;
  1076. struct event_constraint unconstrained;
  1077. static int
  1078. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1079. {
  1080. unsigned int cpu = (long)hcpu;
  1081. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  1082. int ret = NOTIFY_OK;
  1083. switch (action & ~CPU_TASKS_FROZEN) {
  1084. case CPU_UP_PREPARE:
  1085. cpuc->kfree_on_online = NULL;
  1086. if (x86_pmu.cpu_prepare)
  1087. ret = x86_pmu.cpu_prepare(cpu);
  1088. break;
  1089. case CPU_STARTING:
  1090. if (x86_pmu.attr_rdpmc)
  1091. set_in_cr4(X86_CR4_PCE);
  1092. if (x86_pmu.cpu_starting)
  1093. x86_pmu.cpu_starting(cpu);
  1094. break;
  1095. case CPU_ONLINE:
  1096. kfree(cpuc->kfree_on_online);
  1097. break;
  1098. case CPU_DYING:
  1099. if (x86_pmu.cpu_dying)
  1100. x86_pmu.cpu_dying(cpu);
  1101. break;
  1102. case CPU_UP_CANCELED:
  1103. case CPU_DEAD:
  1104. if (x86_pmu.cpu_dead)
  1105. x86_pmu.cpu_dead(cpu);
  1106. break;
  1107. default:
  1108. break;
  1109. }
  1110. return ret;
  1111. }
  1112. static void __init pmu_check_apic(void)
  1113. {
  1114. if (cpu_has_apic)
  1115. return;
  1116. x86_pmu.apic = 0;
  1117. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1118. pr_info("no hardware sampling interrupt available.\n");
  1119. /*
  1120. * If we have a PMU initialized but no APIC
  1121. * interrupts, we cannot sample hardware
  1122. * events (user-space has to fall back and
  1123. * sample via a hrtimer based software event):
  1124. */
  1125. pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  1126. }
  1127. static struct attribute_group x86_pmu_format_group = {
  1128. .name = "format",
  1129. .attrs = NULL,
  1130. };
  1131. /*
  1132. * Remove all undefined events (x86_pmu.event_map(id) == 0)
  1133. * out of events_attr attributes.
  1134. */
  1135. static void __init filter_events(struct attribute **attrs)
  1136. {
  1137. struct device_attribute *d;
  1138. struct perf_pmu_events_attr *pmu_attr;
  1139. int i, j;
  1140. for (i = 0; attrs[i]; i++) {
  1141. d = (struct device_attribute *)attrs[i];
  1142. pmu_attr = container_of(d, struct perf_pmu_events_attr, attr);
  1143. /* str trumps id */
  1144. if (pmu_attr->event_str)
  1145. continue;
  1146. if (x86_pmu.event_map(i))
  1147. continue;
  1148. for (j = i; attrs[j]; j++)
  1149. attrs[j] = attrs[j + 1];
  1150. /* Check the shifted attr. */
  1151. i--;
  1152. }
  1153. }
  1154. /* Merge two pointer arrays */
  1155. static __init struct attribute **merge_attr(struct attribute **a, struct attribute **b)
  1156. {
  1157. struct attribute **new;
  1158. int j, i;
  1159. for (j = 0; a[j]; j++)
  1160. ;
  1161. for (i = 0; b[i]; i++)
  1162. j++;
  1163. j++;
  1164. new = kmalloc(sizeof(struct attribute *) * j, GFP_KERNEL);
  1165. if (!new)
  1166. return NULL;
  1167. j = 0;
  1168. for (i = 0; a[i]; i++)
  1169. new[j++] = a[i];
  1170. for (i = 0; b[i]; i++)
  1171. new[j++] = b[i];
  1172. new[j] = NULL;
  1173. return new;
  1174. }
  1175. ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
  1176. char *page)
  1177. {
  1178. struct perf_pmu_events_attr *pmu_attr = \
  1179. container_of(attr, struct perf_pmu_events_attr, attr);
  1180. u64 config = x86_pmu.event_map(pmu_attr->id);
  1181. /* string trumps id */
  1182. if (pmu_attr->event_str)
  1183. return sprintf(page, "%s", pmu_attr->event_str);
  1184. return x86_pmu.events_sysfs_show(page, config);
  1185. }
  1186. EVENT_ATTR(cpu-cycles, CPU_CYCLES );
  1187. EVENT_ATTR(instructions, INSTRUCTIONS );
  1188. EVENT_ATTR(cache-references, CACHE_REFERENCES );
  1189. EVENT_ATTR(cache-misses, CACHE_MISSES );
  1190. EVENT_ATTR(branch-instructions, BRANCH_INSTRUCTIONS );
  1191. EVENT_ATTR(branch-misses, BRANCH_MISSES );
  1192. EVENT_ATTR(bus-cycles, BUS_CYCLES );
  1193. EVENT_ATTR(stalled-cycles-frontend, STALLED_CYCLES_FRONTEND );
  1194. EVENT_ATTR(stalled-cycles-backend, STALLED_CYCLES_BACKEND );
  1195. EVENT_ATTR(ref-cycles, REF_CPU_CYCLES );
  1196. static struct attribute *empty_attrs;
  1197. static struct attribute *events_attr[] = {
  1198. EVENT_PTR(CPU_CYCLES),
  1199. EVENT_PTR(INSTRUCTIONS),
  1200. EVENT_PTR(CACHE_REFERENCES),
  1201. EVENT_PTR(CACHE_MISSES),
  1202. EVENT_PTR(BRANCH_INSTRUCTIONS),
  1203. EVENT_PTR(BRANCH_MISSES),
  1204. EVENT_PTR(BUS_CYCLES),
  1205. EVENT_PTR(STALLED_CYCLES_FRONTEND),
  1206. EVENT_PTR(STALLED_CYCLES_BACKEND),
  1207. EVENT_PTR(REF_CPU_CYCLES),
  1208. NULL,
  1209. };
  1210. static struct attribute_group x86_pmu_events_group = {
  1211. .name = "events",
  1212. .attrs = events_attr,
  1213. };
  1214. ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event)
  1215. {
  1216. u64 umask = (config & ARCH_PERFMON_EVENTSEL_UMASK) >> 8;
  1217. u64 cmask = (config & ARCH_PERFMON_EVENTSEL_CMASK) >> 24;
  1218. bool edge = (config & ARCH_PERFMON_EVENTSEL_EDGE);
  1219. bool pc = (config & ARCH_PERFMON_EVENTSEL_PIN_CONTROL);
  1220. bool any = (config & ARCH_PERFMON_EVENTSEL_ANY);
  1221. bool inv = (config & ARCH_PERFMON_EVENTSEL_INV);
  1222. ssize_t ret;
  1223. /*
  1224. * We have whole page size to spend and just little data
  1225. * to write, so we can safely use sprintf.
  1226. */
  1227. ret = sprintf(page, "event=0x%02llx", event);
  1228. if (umask)
  1229. ret += sprintf(page + ret, ",umask=0x%02llx", umask);
  1230. if (edge)
  1231. ret += sprintf(page + ret, ",edge");
  1232. if (pc)
  1233. ret += sprintf(page + ret, ",pc");
  1234. if (any)
  1235. ret += sprintf(page + ret, ",any");
  1236. if (inv)
  1237. ret += sprintf(page + ret, ",inv");
  1238. if (cmask)
  1239. ret += sprintf(page + ret, ",cmask=0x%02llx", cmask);
  1240. ret += sprintf(page + ret, "\n");
  1241. return ret;
  1242. }
  1243. static int __init init_hw_perf_events(void)
  1244. {
  1245. struct x86_pmu_quirk *quirk;
  1246. int err;
  1247. pr_info("Performance Events: ");
  1248. switch (boot_cpu_data.x86_vendor) {
  1249. case X86_VENDOR_INTEL:
  1250. err = intel_pmu_init();
  1251. break;
  1252. case X86_VENDOR_AMD:
  1253. err = amd_pmu_init();
  1254. break;
  1255. default:
  1256. err = -ENOTSUPP;
  1257. }
  1258. if (err != 0) {
  1259. pr_cont("no PMU driver, software events only.\n");
  1260. return 0;
  1261. }
  1262. pmu_check_apic();
  1263. /* sanity check that the hardware exists or is emulated */
  1264. if (!check_hw_exists())
  1265. return 0;
  1266. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1267. x86_pmu.attr_rdpmc = 1; /* enable userspace RDPMC usage by default */
  1268. for (quirk = x86_pmu.quirks; quirk; quirk = quirk->next)
  1269. quirk->func();
  1270. if (!x86_pmu.intel_ctrl)
  1271. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  1272. perf_events_lapic_init();
  1273. register_nmi_handler(NMI_LOCAL, perf_event_nmi_handler, 0, "PMI");
  1274. unconstrained = (struct event_constraint)
  1275. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  1276. 0, x86_pmu.num_counters, 0, 0);
  1277. x86_pmu_format_group.attrs = x86_pmu.format_attrs;
  1278. if (x86_pmu.event_attrs)
  1279. x86_pmu_events_group.attrs = x86_pmu.event_attrs;
  1280. if (!x86_pmu.events_sysfs_show)
  1281. x86_pmu_events_group.attrs = &empty_attrs;
  1282. else
  1283. filter_events(x86_pmu_events_group.attrs);
  1284. if (x86_pmu.cpu_events) {
  1285. struct attribute **tmp;
  1286. tmp = merge_attr(x86_pmu_events_group.attrs, x86_pmu.cpu_events);
  1287. if (!WARN_ON(!tmp))
  1288. x86_pmu_events_group.attrs = tmp;
  1289. }
  1290. pr_info("... version: %d\n", x86_pmu.version);
  1291. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  1292. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  1293. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  1294. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1295. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  1296. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  1297. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1298. perf_cpu_notifier(x86_pmu_notifier);
  1299. return 0;
  1300. }
  1301. early_initcall(init_hw_perf_events);
  1302. static inline void x86_pmu_read(struct perf_event *event)
  1303. {
  1304. x86_perf_event_update(event);
  1305. }
  1306. /*
  1307. * Start group events scheduling transaction
  1308. * Set the flag to make pmu::enable() not perform the
  1309. * schedulability test, it will be performed at commit time
  1310. */
  1311. static void x86_pmu_start_txn(struct pmu *pmu)
  1312. {
  1313. perf_pmu_disable(pmu);
  1314. __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN);
  1315. __this_cpu_write(cpu_hw_events.n_txn, 0);
  1316. }
  1317. /*
  1318. * Stop group events scheduling transaction
  1319. * Clear the flag and pmu::enable() will perform the
  1320. * schedulability test.
  1321. */
  1322. static void x86_pmu_cancel_txn(struct pmu *pmu)
  1323. {
  1324. __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN);
  1325. /*
  1326. * Truncate collected array by the number of events added in this
  1327. * transaction. See x86_pmu_add() and x86_pmu_*_txn().
  1328. */
  1329. __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
  1330. __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
  1331. perf_pmu_enable(pmu);
  1332. }
  1333. /*
  1334. * Commit group events scheduling transaction
  1335. * Perform the group schedulability test as a whole
  1336. * Return 0 if success
  1337. *
  1338. * Does not cancel the transaction on failure; expects the caller to do this.
  1339. */
  1340. static int x86_pmu_commit_txn(struct pmu *pmu)
  1341. {
  1342. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1343. int assign[X86_PMC_IDX_MAX];
  1344. int n, ret;
  1345. n = cpuc->n_events;
  1346. if (!x86_pmu_initialized())
  1347. return -EAGAIN;
  1348. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1349. if (ret)
  1350. return ret;
  1351. /*
  1352. * copy new assignment, now we know it is possible
  1353. * will be used by hw_perf_enable()
  1354. */
  1355. memcpy(cpuc->assign, assign, n*sizeof(int));
  1356. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1357. perf_pmu_enable(pmu);
  1358. return 0;
  1359. }
  1360. /*
  1361. * a fake_cpuc is used to validate event groups. Due to
  1362. * the extra reg logic, we need to also allocate a fake
  1363. * per_core and per_cpu structure. Otherwise, group events
  1364. * using extra reg may conflict without the kernel being
  1365. * able to catch this when the last event gets added to
  1366. * the group.
  1367. */
  1368. static void free_fake_cpuc(struct cpu_hw_events *cpuc)
  1369. {
  1370. kfree(cpuc->shared_regs);
  1371. kfree(cpuc);
  1372. }
  1373. static struct cpu_hw_events *allocate_fake_cpuc(void)
  1374. {
  1375. struct cpu_hw_events *cpuc;
  1376. int cpu = raw_smp_processor_id();
  1377. cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL);
  1378. if (!cpuc)
  1379. return ERR_PTR(-ENOMEM);
  1380. /* only needed, if we have extra_regs */
  1381. if (x86_pmu.extra_regs) {
  1382. cpuc->shared_regs = allocate_shared_regs(cpu);
  1383. if (!cpuc->shared_regs)
  1384. goto error;
  1385. }
  1386. cpuc->is_fake = 1;
  1387. return cpuc;
  1388. error:
  1389. free_fake_cpuc(cpuc);
  1390. return ERR_PTR(-ENOMEM);
  1391. }
  1392. /*
  1393. * validate that we can schedule this event
  1394. */
  1395. static int validate_event(struct perf_event *event)
  1396. {
  1397. struct cpu_hw_events *fake_cpuc;
  1398. struct event_constraint *c;
  1399. int ret = 0;
  1400. fake_cpuc = allocate_fake_cpuc();
  1401. if (IS_ERR(fake_cpuc))
  1402. return PTR_ERR(fake_cpuc);
  1403. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1404. if (!c || !c->weight)
  1405. ret = -EINVAL;
  1406. if (x86_pmu.put_event_constraints)
  1407. x86_pmu.put_event_constraints(fake_cpuc, event);
  1408. free_fake_cpuc(fake_cpuc);
  1409. return ret;
  1410. }
  1411. /*
  1412. * validate a single event group
  1413. *
  1414. * validation include:
  1415. * - check events are compatible which each other
  1416. * - events do not compete for the same counter
  1417. * - number of events <= number of counters
  1418. *
  1419. * validation ensures the group can be loaded onto the
  1420. * PMU if it was the only group available.
  1421. */
  1422. static int validate_group(struct perf_event *event)
  1423. {
  1424. struct perf_event *leader = event->group_leader;
  1425. struct cpu_hw_events *fake_cpuc;
  1426. int ret = -EINVAL, n;
  1427. fake_cpuc = allocate_fake_cpuc();
  1428. if (IS_ERR(fake_cpuc))
  1429. return PTR_ERR(fake_cpuc);
  1430. /*
  1431. * the event is not yet connected with its
  1432. * siblings therefore we must first collect
  1433. * existing siblings, then add the new event
  1434. * before we can simulate the scheduling
  1435. */
  1436. n = collect_events(fake_cpuc, leader, true);
  1437. if (n < 0)
  1438. goto out;
  1439. fake_cpuc->n_events = n;
  1440. n = collect_events(fake_cpuc, event, false);
  1441. if (n < 0)
  1442. goto out;
  1443. fake_cpuc->n_events = n;
  1444. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1445. out:
  1446. free_fake_cpuc(fake_cpuc);
  1447. return ret;
  1448. }
  1449. static int x86_pmu_event_init(struct perf_event *event)
  1450. {
  1451. struct pmu *tmp;
  1452. int err;
  1453. switch (event->attr.type) {
  1454. case PERF_TYPE_RAW:
  1455. case PERF_TYPE_HARDWARE:
  1456. case PERF_TYPE_HW_CACHE:
  1457. break;
  1458. default:
  1459. return -ENOENT;
  1460. }
  1461. err = __x86_pmu_event_init(event);
  1462. if (!err) {
  1463. /*
  1464. * we temporarily connect event to its pmu
  1465. * such that validate_group() can classify
  1466. * it as an x86 event using is_x86_event()
  1467. */
  1468. tmp = event->pmu;
  1469. event->pmu = &pmu;
  1470. if (event->group_leader != event)
  1471. err = validate_group(event);
  1472. else
  1473. err = validate_event(event);
  1474. event->pmu = tmp;
  1475. }
  1476. if (err) {
  1477. if (event->destroy)
  1478. event->destroy(event);
  1479. }
  1480. return err;
  1481. }
  1482. static int x86_pmu_event_idx(struct perf_event *event)
  1483. {
  1484. int idx = event->hw.idx;
  1485. if (!x86_pmu.attr_rdpmc)
  1486. return 0;
  1487. if (x86_pmu.num_counters_fixed && idx >= INTEL_PMC_IDX_FIXED) {
  1488. idx -= INTEL_PMC_IDX_FIXED;
  1489. idx |= 1 << 30;
  1490. }
  1491. return idx + 1;
  1492. }
  1493. static ssize_t get_attr_rdpmc(struct device *cdev,
  1494. struct device_attribute *attr,
  1495. char *buf)
  1496. {
  1497. return snprintf(buf, 40, "%d\n", x86_pmu.attr_rdpmc);
  1498. }
  1499. static void change_rdpmc(void *info)
  1500. {
  1501. bool enable = !!(unsigned long)info;
  1502. if (enable)
  1503. set_in_cr4(X86_CR4_PCE);
  1504. else
  1505. clear_in_cr4(X86_CR4_PCE);
  1506. }
  1507. static ssize_t set_attr_rdpmc(struct device *cdev,
  1508. struct device_attribute *attr,
  1509. const char *buf, size_t count)
  1510. {
  1511. unsigned long val;
  1512. ssize_t ret;
  1513. ret = kstrtoul(buf, 0, &val);
  1514. if (ret)
  1515. return ret;
  1516. if (x86_pmu.attr_rdpmc_broken)
  1517. return -ENOTSUPP;
  1518. if (!!val != !!x86_pmu.attr_rdpmc) {
  1519. x86_pmu.attr_rdpmc = !!val;
  1520. on_each_cpu(change_rdpmc, (void *)val, 1);
  1521. }
  1522. return count;
  1523. }
  1524. static DEVICE_ATTR(rdpmc, S_IRUSR | S_IWUSR, get_attr_rdpmc, set_attr_rdpmc);
  1525. static struct attribute *x86_pmu_attrs[] = {
  1526. &dev_attr_rdpmc.attr,
  1527. NULL,
  1528. };
  1529. static struct attribute_group x86_pmu_attr_group = {
  1530. .attrs = x86_pmu_attrs,
  1531. };
  1532. static const struct attribute_group *x86_pmu_attr_groups[] = {
  1533. &x86_pmu_attr_group,
  1534. &x86_pmu_format_group,
  1535. &x86_pmu_events_group,
  1536. NULL,
  1537. };
  1538. static void x86_pmu_flush_branch_stack(void)
  1539. {
  1540. if (x86_pmu.flush_branch_stack)
  1541. x86_pmu.flush_branch_stack();
  1542. }
  1543. void perf_check_microcode(void)
  1544. {
  1545. if (x86_pmu.check_microcode)
  1546. x86_pmu.check_microcode();
  1547. }
  1548. EXPORT_SYMBOL_GPL(perf_check_microcode);
  1549. static struct pmu pmu = {
  1550. .pmu_enable = x86_pmu_enable,
  1551. .pmu_disable = x86_pmu_disable,
  1552. .attr_groups = x86_pmu_attr_groups,
  1553. .event_init = x86_pmu_event_init,
  1554. .add = x86_pmu_add,
  1555. .del = x86_pmu_del,
  1556. .start = x86_pmu_start,
  1557. .stop = x86_pmu_stop,
  1558. .read = x86_pmu_read,
  1559. .start_txn = x86_pmu_start_txn,
  1560. .cancel_txn = x86_pmu_cancel_txn,
  1561. .commit_txn = x86_pmu_commit_txn,
  1562. .event_idx = x86_pmu_event_idx,
  1563. .flush_branch_stack = x86_pmu_flush_branch_stack,
  1564. };
  1565. void arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
  1566. {
  1567. struct cyc2ns_data *data;
  1568. userpg->cap_user_time = 0;
  1569. userpg->cap_user_time_zero = 0;
  1570. userpg->cap_user_rdpmc = x86_pmu.attr_rdpmc;
  1571. userpg->pmc_width = x86_pmu.cntval_bits;
  1572. if (!sched_clock_stable())
  1573. return;
  1574. data = cyc2ns_read_begin();
  1575. userpg->cap_user_time = 1;
  1576. userpg->time_mult = data->cyc2ns_mul;
  1577. userpg->time_shift = data->cyc2ns_shift;
  1578. userpg->time_offset = data->cyc2ns_offset - now;
  1579. userpg->cap_user_time_zero = 1;
  1580. userpg->time_zero = data->cyc2ns_offset;
  1581. cyc2ns_read_end(data);
  1582. }
  1583. /*
  1584. * callchain support
  1585. */
  1586. static int backtrace_stack(void *data, char *name)
  1587. {
  1588. return 0;
  1589. }
  1590. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1591. {
  1592. struct perf_callchain_entry *entry = data;
  1593. perf_callchain_store(entry, addr);
  1594. }
  1595. static const struct stacktrace_ops backtrace_ops = {
  1596. .stack = backtrace_stack,
  1597. .address = backtrace_address,
  1598. .walk_stack = print_context_stack_bp,
  1599. };
  1600. void
  1601. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1602. {
  1603. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1604. /* TODO: We don't support guest os callchain now */
  1605. return;
  1606. }
  1607. perf_callchain_store(entry, regs->ip);
  1608. dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
  1609. }
  1610. static inline int
  1611. valid_user_frame(const void __user *fp, unsigned long size)
  1612. {
  1613. return (__range_not_ok(fp, size, TASK_SIZE) == 0);
  1614. }
  1615. static unsigned long get_segment_base(unsigned int segment)
  1616. {
  1617. struct desc_struct *desc;
  1618. int idx = segment >> 3;
  1619. if ((segment & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  1620. if (idx > LDT_ENTRIES)
  1621. return 0;
  1622. if (idx > current->active_mm->context.size)
  1623. return 0;
  1624. desc = current->active_mm->context.ldt;
  1625. } else {
  1626. if (idx > GDT_ENTRIES)
  1627. return 0;
  1628. desc = raw_cpu_ptr(gdt_page.gdt);
  1629. }
  1630. return get_desc_base(desc + idx);
  1631. }
  1632. #ifdef CONFIG_COMPAT
  1633. #include <asm/compat.h>
  1634. static inline int
  1635. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1636. {
  1637. /* 32-bit process in 64-bit kernel. */
  1638. unsigned long ss_base, cs_base;
  1639. struct stack_frame_ia32 frame;
  1640. const void __user *fp;
  1641. if (!test_thread_flag(TIF_IA32))
  1642. return 0;
  1643. cs_base = get_segment_base(regs->cs);
  1644. ss_base = get_segment_base(regs->ss);
  1645. fp = compat_ptr(ss_base + regs->bp);
  1646. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1647. unsigned long bytes;
  1648. frame.next_frame = 0;
  1649. frame.return_address = 0;
  1650. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1651. if (bytes != 0)
  1652. break;
  1653. if (!valid_user_frame(fp, sizeof(frame)))
  1654. break;
  1655. perf_callchain_store(entry, cs_base + frame.return_address);
  1656. fp = compat_ptr(ss_base + frame.next_frame);
  1657. }
  1658. return 1;
  1659. }
  1660. #else
  1661. static inline int
  1662. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1663. {
  1664. return 0;
  1665. }
  1666. #endif
  1667. void
  1668. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1669. {
  1670. struct stack_frame frame;
  1671. const void __user *fp;
  1672. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1673. /* TODO: We don't support guest os callchain now */
  1674. return;
  1675. }
  1676. /*
  1677. * We don't know what to do with VM86 stacks.. ignore them for now.
  1678. */
  1679. if (regs->flags & (X86_VM_MASK | PERF_EFLAGS_VM))
  1680. return;
  1681. fp = (void __user *)regs->bp;
  1682. perf_callchain_store(entry, regs->ip);
  1683. if (!current->mm)
  1684. return;
  1685. if (perf_callchain_user32(regs, entry))
  1686. return;
  1687. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1688. unsigned long bytes;
  1689. frame.next_frame = NULL;
  1690. frame.return_address = 0;
  1691. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1692. if (bytes != 0)
  1693. break;
  1694. if (!valid_user_frame(fp, sizeof(frame)))
  1695. break;
  1696. perf_callchain_store(entry, frame.return_address);
  1697. fp = frame.next_frame;
  1698. }
  1699. }
  1700. /*
  1701. * Deal with code segment offsets for the various execution modes:
  1702. *
  1703. * VM86 - the good olde 16 bit days, where the linear address is
  1704. * 20 bits and we use regs->ip + 0x10 * regs->cs.
  1705. *
  1706. * IA32 - Where we need to look at GDT/LDT segment descriptor tables
  1707. * to figure out what the 32bit base address is.
  1708. *
  1709. * X32 - has TIF_X32 set, but is running in x86_64
  1710. *
  1711. * X86_64 - CS,DS,SS,ES are all zero based.
  1712. */
  1713. static unsigned long code_segment_base(struct pt_regs *regs)
  1714. {
  1715. /*
  1716. * If we are in VM86 mode, add the segment offset to convert to a
  1717. * linear address.
  1718. */
  1719. if (regs->flags & X86_VM_MASK)
  1720. return 0x10 * regs->cs;
  1721. /*
  1722. * For IA32 we look at the GDT/LDT segment base to convert the
  1723. * effective IP to a linear address.
  1724. */
  1725. #ifdef CONFIG_X86_32
  1726. if (user_mode(regs) && regs->cs != __USER_CS)
  1727. return get_segment_base(regs->cs);
  1728. #else
  1729. if (test_thread_flag(TIF_IA32)) {
  1730. if (user_mode(regs) && regs->cs != __USER32_CS)
  1731. return get_segment_base(regs->cs);
  1732. }
  1733. #endif
  1734. return 0;
  1735. }
  1736. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1737. {
  1738. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1739. return perf_guest_cbs->get_guest_ip();
  1740. return regs->ip + code_segment_base(regs);
  1741. }
  1742. unsigned long perf_misc_flags(struct pt_regs *regs)
  1743. {
  1744. int misc = 0;
  1745. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1746. if (perf_guest_cbs->is_user_mode())
  1747. misc |= PERF_RECORD_MISC_GUEST_USER;
  1748. else
  1749. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1750. } else {
  1751. if (user_mode(regs))
  1752. misc |= PERF_RECORD_MISC_USER;
  1753. else
  1754. misc |= PERF_RECORD_MISC_KERNEL;
  1755. }
  1756. if (regs->flags & PERF_EFLAGS_EXACT)
  1757. misc |= PERF_RECORD_MISC_EXACT_IP;
  1758. return misc;
  1759. }
  1760. void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
  1761. {
  1762. cap->version = x86_pmu.version;
  1763. cap->num_counters_gp = x86_pmu.num_counters;
  1764. cap->num_counters_fixed = x86_pmu.num_counters_fixed;
  1765. cap->bit_width_gp = x86_pmu.cntval_bits;
  1766. cap->bit_width_fixed = x86_pmu.cntval_bits;
  1767. cap->events_mask = (unsigned int)x86_pmu.events_maskl;
  1768. cap->events_mask_len = x86_pmu.events_mask_len;
  1769. }
  1770. EXPORT_SYMBOL_GPL(perf_get_x86_pmu_capability);