perf_event.c 50 KB

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