perf_event.c 49 KB

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