perf_event.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  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. } else {
  253. /*
  254. * If we have a PMU initialized but no APIC
  255. * interrupts, we cannot sample hardware
  256. * events (user-space has to fall back and
  257. * sample via a hrtimer based software event):
  258. */
  259. if (!x86_pmu.apic)
  260. return -EOPNOTSUPP;
  261. }
  262. if (attr->type == PERF_TYPE_RAW)
  263. return x86_pmu_extra_regs(event->attr.config, event);
  264. if (attr->type == PERF_TYPE_HW_CACHE)
  265. return set_ext_hw_attr(hwc, event);
  266. if (attr->config >= x86_pmu.max_events)
  267. return -EINVAL;
  268. /*
  269. * The generic map:
  270. */
  271. config = x86_pmu.event_map(attr->config);
  272. if (config == 0)
  273. return -ENOENT;
  274. if (config == -1LL)
  275. return -EINVAL;
  276. /*
  277. * Branch tracing:
  278. */
  279. if (attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
  280. !attr->freq && hwc->sample_period == 1) {
  281. /* BTS is not supported by this architecture. */
  282. if (!x86_pmu.bts_active)
  283. return -EOPNOTSUPP;
  284. /* BTS is currently only allowed for user-mode. */
  285. if (!attr->exclude_kernel)
  286. return -EOPNOTSUPP;
  287. }
  288. hwc->config |= config;
  289. return 0;
  290. }
  291. /*
  292. * check that branch_sample_type is compatible with
  293. * settings needed for precise_ip > 1 which implies
  294. * using the LBR to capture ALL taken branches at the
  295. * priv levels of the measurement
  296. */
  297. static inline int precise_br_compat(struct perf_event *event)
  298. {
  299. u64 m = event->attr.branch_sample_type;
  300. u64 b = 0;
  301. /* must capture all branches */
  302. if (!(m & PERF_SAMPLE_BRANCH_ANY))
  303. return 0;
  304. m &= PERF_SAMPLE_BRANCH_KERNEL | PERF_SAMPLE_BRANCH_USER;
  305. if (!event->attr.exclude_user)
  306. b |= PERF_SAMPLE_BRANCH_USER;
  307. if (!event->attr.exclude_kernel)
  308. b |= PERF_SAMPLE_BRANCH_KERNEL;
  309. /*
  310. * ignore PERF_SAMPLE_BRANCH_HV, not supported on x86
  311. */
  312. return m == b;
  313. }
  314. int x86_pmu_hw_config(struct perf_event *event)
  315. {
  316. if (event->attr.precise_ip) {
  317. int precise = 0;
  318. /* Support for constant skid */
  319. if (x86_pmu.pebs_active && !x86_pmu.pebs_broken) {
  320. precise++;
  321. /* Support for IP fixup */
  322. if (x86_pmu.lbr_nr)
  323. precise++;
  324. }
  325. if (event->attr.precise_ip > precise)
  326. return -EOPNOTSUPP;
  327. /*
  328. * check that PEBS LBR correction does not conflict with
  329. * whatever the user is asking with attr->branch_sample_type
  330. */
  331. if (event->attr.precise_ip > 1) {
  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 = &__get_cpu_var(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 = &__get_cpu_var(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 = &__get_cpu_var(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 event_constraint **constraints;
  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 event_constraint **c,
  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->constraints = c;
  479. for (idx = 0; idx < num; idx++) {
  480. if (c[idx]->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->constraints[sched->state.event];
  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->constraints[sched->state.event];
  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 event_constraint **constraints, int n,
  574. int wmin, int wmax, int *assign)
  575. {
  576. struct perf_sched sched;
  577. perf_sched_init(&sched, constraints, 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. int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  587. {
  588. struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
  589. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  590. int i, wmin, wmax, num = 0;
  591. struct hw_perf_event *hwc;
  592. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  593. for (i = 0, wmin = X86_PMC_IDX_MAX, wmax = 0; i < n; i++) {
  594. c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
  595. constraints[i] = c;
  596. wmin = min(wmin, c->weight);
  597. wmax = max(wmax, c->weight);
  598. }
  599. /*
  600. * fastpath, try to reuse previous register
  601. */
  602. for (i = 0; i < n; i++) {
  603. hwc = &cpuc->event_list[i]->hw;
  604. c = constraints[i];
  605. /* never assigned */
  606. if (hwc->idx == -1)
  607. break;
  608. /* constraint still honored */
  609. if (!test_bit(hwc->idx, c->idxmsk))
  610. break;
  611. /* not already used */
  612. if (test_bit(hwc->idx, used_mask))
  613. break;
  614. __set_bit(hwc->idx, used_mask);
  615. if (assign)
  616. assign[i] = hwc->idx;
  617. }
  618. /* slow path */
  619. if (i != n)
  620. num = perf_assign_events(constraints, n, wmin, wmax, assign);
  621. /*
  622. * scheduling failed or is just a simulation,
  623. * free resources if necessary
  624. */
  625. if (!assign || num) {
  626. for (i = 0; i < n; i++) {
  627. if (x86_pmu.put_event_constraints)
  628. x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
  629. }
  630. }
  631. return num ? -EINVAL : 0;
  632. }
  633. /*
  634. * dogrp: true if must collect siblings events (group)
  635. * returns total number of events and error code
  636. */
  637. static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
  638. {
  639. struct perf_event *event;
  640. int n, max_count;
  641. max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
  642. /* current number of events already accepted */
  643. n = cpuc->n_events;
  644. if (is_x86_event(leader)) {
  645. if (n >= max_count)
  646. return -EINVAL;
  647. cpuc->event_list[n] = leader;
  648. n++;
  649. }
  650. if (!dogrp)
  651. return n;
  652. list_for_each_entry(event, &leader->sibling_list, group_entry) {
  653. if (!is_x86_event(event) ||
  654. event->state <= PERF_EVENT_STATE_OFF)
  655. continue;
  656. if (n >= max_count)
  657. return -EINVAL;
  658. cpuc->event_list[n] = event;
  659. n++;
  660. }
  661. return n;
  662. }
  663. static inline void x86_assign_hw_event(struct perf_event *event,
  664. struct cpu_hw_events *cpuc, int i)
  665. {
  666. struct hw_perf_event *hwc = &event->hw;
  667. hwc->idx = cpuc->assign[i];
  668. hwc->last_cpu = smp_processor_id();
  669. hwc->last_tag = ++cpuc->tags[i];
  670. if (hwc->idx == INTEL_PMC_IDX_FIXED_BTS) {
  671. hwc->config_base = 0;
  672. hwc->event_base = 0;
  673. } else if (hwc->idx >= INTEL_PMC_IDX_FIXED) {
  674. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  675. hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - INTEL_PMC_IDX_FIXED);
  676. hwc->event_base_rdpmc = (hwc->idx - INTEL_PMC_IDX_FIXED) | 1<<30;
  677. } else {
  678. hwc->config_base = x86_pmu_config_addr(hwc->idx);
  679. hwc->event_base = x86_pmu_event_addr(hwc->idx);
  680. hwc->event_base_rdpmc = x86_pmu_rdpmc_index(hwc->idx);
  681. }
  682. }
  683. static inline int match_prev_assignment(struct hw_perf_event *hwc,
  684. struct cpu_hw_events *cpuc,
  685. int i)
  686. {
  687. return hwc->idx == cpuc->assign[i] &&
  688. hwc->last_cpu == smp_processor_id() &&
  689. hwc->last_tag == cpuc->tags[i];
  690. }
  691. static void x86_pmu_start(struct perf_event *event, int flags);
  692. static void x86_pmu_enable(struct pmu *pmu)
  693. {
  694. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  695. struct perf_event *event;
  696. struct hw_perf_event *hwc;
  697. int i, added = cpuc->n_added;
  698. if (!x86_pmu_initialized())
  699. return;
  700. if (cpuc->enabled)
  701. return;
  702. if (cpuc->n_added) {
  703. int n_running = cpuc->n_events - cpuc->n_added;
  704. /*
  705. * apply assignment obtained either from
  706. * hw_perf_group_sched_in() or x86_pmu_enable()
  707. *
  708. * step1: save events moving to new counters
  709. * step2: reprogram moved events into new counters
  710. */
  711. for (i = 0; i < n_running; i++) {
  712. event = cpuc->event_list[i];
  713. hwc = &event->hw;
  714. /*
  715. * we can avoid reprogramming counter if:
  716. * - assigned same counter as last time
  717. * - running on same CPU as last time
  718. * - no other event has used the counter since
  719. */
  720. if (hwc->idx == -1 ||
  721. match_prev_assignment(hwc, cpuc, i))
  722. continue;
  723. /*
  724. * Ensure we don't accidentally enable a stopped
  725. * counter simply because we rescheduled.
  726. */
  727. if (hwc->state & PERF_HES_STOPPED)
  728. hwc->state |= PERF_HES_ARCH;
  729. x86_pmu_stop(event, PERF_EF_UPDATE);
  730. }
  731. for (i = 0; i < cpuc->n_events; i++) {
  732. event = cpuc->event_list[i];
  733. hwc = &event->hw;
  734. if (!match_prev_assignment(hwc, cpuc, i))
  735. x86_assign_hw_event(event, cpuc, i);
  736. else if (i < n_running)
  737. continue;
  738. if (hwc->state & PERF_HES_ARCH)
  739. continue;
  740. x86_pmu_start(event, PERF_EF_RELOAD);
  741. }
  742. cpuc->n_added = 0;
  743. perf_events_lapic_init();
  744. }
  745. cpuc->enabled = 1;
  746. barrier();
  747. x86_pmu.enable_all(added);
  748. }
  749. static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  750. /*
  751. * Set the next IRQ period, based on the hwc->period_left value.
  752. * To be called with the event disabled in hw:
  753. */
  754. int x86_perf_event_set_period(struct perf_event *event)
  755. {
  756. struct hw_perf_event *hwc = &event->hw;
  757. s64 left = local64_read(&hwc->period_left);
  758. s64 period = hwc->sample_period;
  759. int ret = 0, idx = hwc->idx;
  760. if (idx == INTEL_PMC_IDX_FIXED_BTS)
  761. return 0;
  762. /*
  763. * If we are way outside a reasonable range then just skip forward:
  764. */
  765. if (unlikely(left <= -period)) {
  766. left = period;
  767. local64_set(&hwc->period_left, left);
  768. hwc->last_period = period;
  769. ret = 1;
  770. }
  771. if (unlikely(left <= 0)) {
  772. left += period;
  773. local64_set(&hwc->period_left, left);
  774. hwc->last_period = period;
  775. ret = 1;
  776. }
  777. /*
  778. * Quirk: certain CPUs dont like it if just 1 hw_event is left:
  779. */
  780. if (unlikely(left < 2))
  781. left = 2;
  782. if (left > x86_pmu.max_period)
  783. left = x86_pmu.max_period;
  784. per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
  785. /*
  786. * The hw event starts counting from this event offset,
  787. * mark it to be able to extra future deltas:
  788. */
  789. local64_set(&hwc->prev_count, (u64)-left);
  790. wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
  791. /*
  792. * Due to erratum on certan cpu we need
  793. * a second write to be sure the register
  794. * is updated properly
  795. */
  796. if (x86_pmu.perfctr_second_write) {
  797. wrmsrl(hwc->event_base,
  798. (u64)(-left) & x86_pmu.cntval_mask);
  799. }
  800. perf_event_update_userpage(event);
  801. return ret;
  802. }
  803. void x86_pmu_enable_event(struct perf_event *event)
  804. {
  805. if (__this_cpu_read(cpu_hw_events.enabled))
  806. __x86_pmu_enable_event(&event->hw,
  807. ARCH_PERFMON_EVENTSEL_ENABLE);
  808. }
  809. /*
  810. * Add a single event to the PMU.
  811. *
  812. * The event is added to the group of enabled events
  813. * but only if it can be scehduled with existing events.
  814. */
  815. static int x86_pmu_add(struct perf_event *event, int flags)
  816. {
  817. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  818. struct hw_perf_event *hwc;
  819. int assign[X86_PMC_IDX_MAX];
  820. int n, n0, ret;
  821. hwc = &event->hw;
  822. perf_pmu_disable(event->pmu);
  823. n0 = cpuc->n_events;
  824. ret = n = collect_events(cpuc, event, false);
  825. if (ret < 0)
  826. goto out;
  827. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  828. if (!(flags & PERF_EF_START))
  829. hwc->state |= PERF_HES_ARCH;
  830. /*
  831. * If group events scheduling transaction was started,
  832. * skip the schedulability test here, it will be performed
  833. * at commit time (->commit_txn) as a whole
  834. */
  835. if (cpuc->group_flag & PERF_EVENT_TXN)
  836. goto done_collect;
  837. ret = x86_pmu.schedule_events(cpuc, n, assign);
  838. if (ret)
  839. goto out;
  840. /*
  841. * copy new assignment, now we know it is possible
  842. * will be used by hw_perf_enable()
  843. */
  844. memcpy(cpuc->assign, assign, n*sizeof(int));
  845. done_collect:
  846. cpuc->n_events = n;
  847. cpuc->n_added += n - n0;
  848. cpuc->n_txn += n - n0;
  849. ret = 0;
  850. out:
  851. perf_pmu_enable(event->pmu);
  852. return ret;
  853. }
  854. static void x86_pmu_start(struct perf_event *event, int flags)
  855. {
  856. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  857. int idx = event->hw.idx;
  858. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  859. return;
  860. if (WARN_ON_ONCE(idx == -1))
  861. return;
  862. if (flags & PERF_EF_RELOAD) {
  863. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  864. x86_perf_event_set_period(event);
  865. }
  866. event->hw.state = 0;
  867. cpuc->events[idx] = event;
  868. __set_bit(idx, cpuc->active_mask);
  869. __set_bit(idx, cpuc->running);
  870. x86_pmu.enable(event);
  871. perf_event_update_userpage(event);
  872. }
  873. void perf_event_print_debug(void)
  874. {
  875. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  876. u64 pebs;
  877. struct cpu_hw_events *cpuc;
  878. unsigned long flags;
  879. int cpu, idx;
  880. if (!x86_pmu.num_counters)
  881. return;
  882. local_irq_save(flags);
  883. cpu = smp_processor_id();
  884. cpuc = &per_cpu(cpu_hw_events, cpu);
  885. if (x86_pmu.version >= 2) {
  886. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  887. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  888. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  889. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  890. rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
  891. pr_info("\n");
  892. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  893. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  894. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  895. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  896. pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
  897. }
  898. pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
  899. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  900. rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
  901. rdmsrl(x86_pmu_event_addr(idx), pmc_count);
  902. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  903. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  904. cpu, idx, pmc_ctrl);
  905. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  906. cpu, idx, pmc_count);
  907. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  908. cpu, idx, prev_left);
  909. }
  910. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
  911. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  912. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  913. cpu, idx, pmc_count);
  914. }
  915. local_irq_restore(flags);
  916. }
  917. void x86_pmu_stop(struct perf_event *event, int flags)
  918. {
  919. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  920. struct hw_perf_event *hwc = &event->hw;
  921. if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
  922. x86_pmu.disable(event);
  923. cpuc->events[hwc->idx] = NULL;
  924. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  925. hwc->state |= PERF_HES_STOPPED;
  926. }
  927. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  928. /*
  929. * Drain the remaining delta count out of a event
  930. * that we are disabling:
  931. */
  932. x86_perf_event_update(event);
  933. hwc->state |= PERF_HES_UPTODATE;
  934. }
  935. }
  936. static void x86_pmu_del(struct perf_event *event, int flags)
  937. {
  938. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  939. int i;
  940. /*
  941. * If we're called during a txn, we don't need to do anything.
  942. * The events never got scheduled and ->cancel_txn will truncate
  943. * the event_list.
  944. */
  945. if (cpuc->group_flag & PERF_EVENT_TXN)
  946. return;
  947. x86_pmu_stop(event, PERF_EF_UPDATE);
  948. for (i = 0; i < cpuc->n_events; i++) {
  949. if (event == cpuc->event_list[i]) {
  950. if (x86_pmu.put_event_constraints)
  951. x86_pmu.put_event_constraints(cpuc, event);
  952. while (++i < cpuc->n_events)
  953. cpuc->event_list[i-1] = cpuc->event_list[i];
  954. --cpuc->n_events;
  955. break;
  956. }
  957. }
  958. perf_event_update_userpage(event);
  959. }
  960. int x86_pmu_handle_irq(struct pt_regs *regs)
  961. {
  962. struct perf_sample_data data;
  963. struct cpu_hw_events *cpuc;
  964. struct perf_event *event;
  965. int idx, handled = 0;
  966. u64 val;
  967. cpuc = &__get_cpu_var(cpu_hw_events);
  968. /*
  969. * Some chipsets need to unmask the LVTPC in a particular spot
  970. * inside the nmi handler. As a result, the unmasking was pushed
  971. * into all the nmi handlers.
  972. *
  973. * This generic handler doesn't seem to have any issues where the
  974. * unmasking occurs so it was left at the top.
  975. */
  976. apic_write(APIC_LVTPC, APIC_DM_NMI);
  977. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  978. if (!test_bit(idx, cpuc->active_mask)) {
  979. /*
  980. * Though we deactivated the counter some cpus
  981. * might still deliver spurious interrupts still
  982. * in flight. Catch them:
  983. */
  984. if (__test_and_clear_bit(idx, cpuc->running))
  985. handled++;
  986. continue;
  987. }
  988. event = cpuc->events[idx];
  989. val = x86_perf_event_update(event);
  990. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  991. continue;
  992. /*
  993. * event overflow
  994. */
  995. handled++;
  996. perf_sample_data_init(&data, 0, event->hw.last_period);
  997. if (!x86_perf_event_set_period(event))
  998. continue;
  999. if (perf_event_overflow(event, &data, regs))
  1000. x86_pmu_stop(event, 0);
  1001. }
  1002. if (handled)
  1003. inc_irq_stat(apic_perf_irqs);
  1004. return handled;
  1005. }
  1006. void perf_events_lapic_init(void)
  1007. {
  1008. if (!x86_pmu.apic || !x86_pmu_initialized())
  1009. return;
  1010. /*
  1011. * Always use NMI for PMU
  1012. */
  1013. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1014. }
  1015. static int __kprobes
  1016. perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  1017. {
  1018. if (!atomic_read(&active_events))
  1019. return NMI_DONE;
  1020. return x86_pmu.handle_irq(regs);
  1021. }
  1022. struct event_constraint emptyconstraint;
  1023. struct event_constraint unconstrained;
  1024. static int __cpuinit
  1025. x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  1026. {
  1027. unsigned int cpu = (long)hcpu;
  1028. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  1029. int ret = NOTIFY_OK;
  1030. switch (action & ~CPU_TASKS_FROZEN) {
  1031. case CPU_UP_PREPARE:
  1032. cpuc->kfree_on_online = NULL;
  1033. if (x86_pmu.cpu_prepare)
  1034. ret = x86_pmu.cpu_prepare(cpu);
  1035. break;
  1036. case CPU_STARTING:
  1037. if (x86_pmu.attr_rdpmc)
  1038. set_in_cr4(X86_CR4_PCE);
  1039. if (x86_pmu.cpu_starting)
  1040. x86_pmu.cpu_starting(cpu);
  1041. break;
  1042. case CPU_ONLINE:
  1043. kfree(cpuc->kfree_on_online);
  1044. break;
  1045. case CPU_DYING:
  1046. if (x86_pmu.cpu_dying)
  1047. x86_pmu.cpu_dying(cpu);
  1048. break;
  1049. case CPU_UP_CANCELED:
  1050. case CPU_DEAD:
  1051. if (x86_pmu.cpu_dead)
  1052. x86_pmu.cpu_dead(cpu);
  1053. break;
  1054. default:
  1055. break;
  1056. }
  1057. return ret;
  1058. }
  1059. static void __init pmu_check_apic(void)
  1060. {
  1061. if (cpu_has_apic)
  1062. return;
  1063. x86_pmu.apic = 0;
  1064. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1065. pr_info("no hardware sampling interrupt available.\n");
  1066. }
  1067. static struct attribute_group x86_pmu_format_group = {
  1068. .name = "format",
  1069. .attrs = NULL,
  1070. };
  1071. /*
  1072. * Remove all undefined events (x86_pmu.event_map(id) == 0)
  1073. * out of events_attr attributes.
  1074. */
  1075. static void __init filter_events(struct attribute **attrs)
  1076. {
  1077. struct device_attribute *d;
  1078. struct perf_pmu_events_attr *pmu_attr;
  1079. int i, j;
  1080. for (i = 0; attrs[i]; i++) {
  1081. d = (struct device_attribute *)attrs[i];
  1082. pmu_attr = container_of(d, struct perf_pmu_events_attr, attr);
  1083. /* str trumps id */
  1084. if (pmu_attr->event_str)
  1085. continue;
  1086. if (x86_pmu.event_map(i))
  1087. continue;
  1088. for (j = i; attrs[j]; j++)
  1089. attrs[j] = attrs[j + 1];
  1090. /* Check the shifted attr. */
  1091. i--;
  1092. }
  1093. }
  1094. /* Merge two pointer arrays */
  1095. static __init struct attribute **merge_attr(struct attribute **a, struct attribute **b)
  1096. {
  1097. struct attribute **new;
  1098. int j, i;
  1099. for (j = 0; a[j]; j++)
  1100. ;
  1101. for (i = 0; b[i]; i++)
  1102. j++;
  1103. j++;
  1104. new = kmalloc(sizeof(struct attribute *) * j, GFP_KERNEL);
  1105. if (!new)
  1106. return NULL;
  1107. j = 0;
  1108. for (i = 0; a[i]; i++)
  1109. new[j++] = a[i];
  1110. for (i = 0; b[i]; i++)
  1111. new[j++] = b[i];
  1112. new[j] = NULL;
  1113. return new;
  1114. }
  1115. ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
  1116. char *page)
  1117. {
  1118. struct perf_pmu_events_attr *pmu_attr = \
  1119. container_of(attr, struct perf_pmu_events_attr, attr);
  1120. u64 config = x86_pmu.event_map(pmu_attr->id);
  1121. /* string trumps id */
  1122. if (pmu_attr->event_str)
  1123. return sprintf(page, "%s", pmu_attr->event_str);
  1124. return x86_pmu.events_sysfs_show(page, config);
  1125. }
  1126. EVENT_ATTR(cpu-cycles, CPU_CYCLES );
  1127. EVENT_ATTR(instructions, INSTRUCTIONS );
  1128. EVENT_ATTR(cache-references, CACHE_REFERENCES );
  1129. EVENT_ATTR(cache-misses, CACHE_MISSES );
  1130. EVENT_ATTR(branch-instructions, BRANCH_INSTRUCTIONS );
  1131. EVENT_ATTR(branch-misses, BRANCH_MISSES );
  1132. EVENT_ATTR(bus-cycles, BUS_CYCLES );
  1133. EVENT_ATTR(stalled-cycles-frontend, STALLED_CYCLES_FRONTEND );
  1134. EVENT_ATTR(stalled-cycles-backend, STALLED_CYCLES_BACKEND );
  1135. EVENT_ATTR(ref-cycles, REF_CPU_CYCLES );
  1136. static struct attribute *empty_attrs;
  1137. static struct attribute *events_attr[] = {
  1138. EVENT_PTR(CPU_CYCLES),
  1139. EVENT_PTR(INSTRUCTIONS),
  1140. EVENT_PTR(CACHE_REFERENCES),
  1141. EVENT_PTR(CACHE_MISSES),
  1142. EVENT_PTR(BRANCH_INSTRUCTIONS),
  1143. EVENT_PTR(BRANCH_MISSES),
  1144. EVENT_PTR(BUS_CYCLES),
  1145. EVENT_PTR(STALLED_CYCLES_FRONTEND),
  1146. EVENT_PTR(STALLED_CYCLES_BACKEND),
  1147. EVENT_PTR(REF_CPU_CYCLES),
  1148. NULL,
  1149. };
  1150. static struct attribute_group x86_pmu_events_group = {
  1151. .name = "events",
  1152. .attrs = events_attr,
  1153. };
  1154. ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event)
  1155. {
  1156. u64 umask = (config & ARCH_PERFMON_EVENTSEL_UMASK) >> 8;
  1157. u64 cmask = (config & ARCH_PERFMON_EVENTSEL_CMASK) >> 24;
  1158. bool edge = (config & ARCH_PERFMON_EVENTSEL_EDGE);
  1159. bool pc = (config & ARCH_PERFMON_EVENTSEL_PIN_CONTROL);
  1160. bool any = (config & ARCH_PERFMON_EVENTSEL_ANY);
  1161. bool inv = (config & ARCH_PERFMON_EVENTSEL_INV);
  1162. ssize_t ret;
  1163. /*
  1164. * We have whole page size to spend and just little data
  1165. * to write, so we can safely use sprintf.
  1166. */
  1167. ret = sprintf(page, "event=0x%02llx", event);
  1168. if (umask)
  1169. ret += sprintf(page + ret, ",umask=0x%02llx", umask);
  1170. if (edge)
  1171. ret += sprintf(page + ret, ",edge");
  1172. if (pc)
  1173. ret += sprintf(page + ret, ",pc");
  1174. if (any)
  1175. ret += sprintf(page + ret, ",any");
  1176. if (inv)
  1177. ret += sprintf(page + ret, ",inv");
  1178. if (cmask)
  1179. ret += sprintf(page + ret, ",cmask=0x%02llx", cmask);
  1180. ret += sprintf(page + ret, "\n");
  1181. return ret;
  1182. }
  1183. static int __init init_hw_perf_events(void)
  1184. {
  1185. struct x86_pmu_quirk *quirk;
  1186. int err;
  1187. pr_info("Performance Events: ");
  1188. switch (boot_cpu_data.x86_vendor) {
  1189. case X86_VENDOR_INTEL:
  1190. err = intel_pmu_init();
  1191. break;
  1192. case X86_VENDOR_AMD:
  1193. err = amd_pmu_init();
  1194. break;
  1195. default:
  1196. return 0;
  1197. }
  1198. if (err != 0) {
  1199. pr_cont("no PMU driver, software events only.\n");
  1200. return 0;
  1201. }
  1202. pmu_check_apic();
  1203. /* sanity check that the hardware exists or is emulated */
  1204. if (!check_hw_exists())
  1205. return 0;
  1206. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1207. for (quirk = x86_pmu.quirks; quirk; quirk = quirk->next)
  1208. quirk->func();
  1209. if (!x86_pmu.intel_ctrl)
  1210. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  1211. perf_events_lapic_init();
  1212. register_nmi_handler(NMI_LOCAL, perf_event_nmi_handler, 0, "PMI");
  1213. unconstrained = (struct event_constraint)
  1214. __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
  1215. 0, x86_pmu.num_counters, 0, 0);
  1216. x86_pmu.attr_rdpmc = 1; /* enable userspace RDPMC usage by default */
  1217. x86_pmu_format_group.attrs = x86_pmu.format_attrs;
  1218. if (x86_pmu.event_attrs)
  1219. x86_pmu_events_group.attrs = x86_pmu.event_attrs;
  1220. if (!x86_pmu.events_sysfs_show)
  1221. x86_pmu_events_group.attrs = &empty_attrs;
  1222. else
  1223. filter_events(x86_pmu_events_group.attrs);
  1224. if (x86_pmu.cpu_events) {
  1225. struct attribute **tmp;
  1226. tmp = merge_attr(x86_pmu_events_group.attrs, x86_pmu.cpu_events);
  1227. if (!WARN_ON(!tmp))
  1228. x86_pmu_events_group.attrs = tmp;
  1229. }
  1230. pr_info("... version: %d\n", x86_pmu.version);
  1231. pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
  1232. pr_info("... generic registers: %d\n", x86_pmu.num_counters);
  1233. pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
  1234. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1235. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
  1236. pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
  1237. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1238. perf_cpu_notifier(x86_pmu_notifier);
  1239. return 0;
  1240. }
  1241. early_initcall(init_hw_perf_events);
  1242. static inline void x86_pmu_read(struct perf_event *event)
  1243. {
  1244. x86_perf_event_update(event);
  1245. }
  1246. /*
  1247. * Start group events scheduling transaction
  1248. * Set the flag to make pmu::enable() not perform the
  1249. * schedulability test, it will be performed at commit time
  1250. */
  1251. static void x86_pmu_start_txn(struct pmu *pmu)
  1252. {
  1253. perf_pmu_disable(pmu);
  1254. __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN);
  1255. __this_cpu_write(cpu_hw_events.n_txn, 0);
  1256. }
  1257. /*
  1258. * Stop group events scheduling transaction
  1259. * Clear the flag and pmu::enable() will perform the
  1260. * schedulability test.
  1261. */
  1262. static void x86_pmu_cancel_txn(struct pmu *pmu)
  1263. {
  1264. __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN);
  1265. /*
  1266. * Truncate the collected events.
  1267. */
  1268. __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
  1269. __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
  1270. perf_pmu_enable(pmu);
  1271. }
  1272. /*
  1273. * Commit group events scheduling transaction
  1274. * Perform the group schedulability test as a whole
  1275. * Return 0 if success
  1276. */
  1277. static int x86_pmu_commit_txn(struct pmu *pmu)
  1278. {
  1279. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1280. int assign[X86_PMC_IDX_MAX];
  1281. int n, ret;
  1282. n = cpuc->n_events;
  1283. if (!x86_pmu_initialized())
  1284. return -EAGAIN;
  1285. ret = x86_pmu.schedule_events(cpuc, n, assign);
  1286. if (ret)
  1287. return ret;
  1288. /*
  1289. * copy new assignment, now we know it is possible
  1290. * will be used by hw_perf_enable()
  1291. */
  1292. memcpy(cpuc->assign, assign, n*sizeof(int));
  1293. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1294. perf_pmu_enable(pmu);
  1295. return 0;
  1296. }
  1297. /*
  1298. * a fake_cpuc is used to validate event groups. Due to
  1299. * the extra reg logic, we need to also allocate a fake
  1300. * per_core and per_cpu structure. Otherwise, group events
  1301. * using extra reg may conflict without the kernel being
  1302. * able to catch this when the last event gets added to
  1303. * the group.
  1304. */
  1305. static void free_fake_cpuc(struct cpu_hw_events *cpuc)
  1306. {
  1307. kfree(cpuc->shared_regs);
  1308. kfree(cpuc);
  1309. }
  1310. static struct cpu_hw_events *allocate_fake_cpuc(void)
  1311. {
  1312. struct cpu_hw_events *cpuc;
  1313. int cpu = raw_smp_processor_id();
  1314. cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL);
  1315. if (!cpuc)
  1316. return ERR_PTR(-ENOMEM);
  1317. /* only needed, if we have extra_regs */
  1318. if (x86_pmu.extra_regs) {
  1319. cpuc->shared_regs = allocate_shared_regs(cpu);
  1320. if (!cpuc->shared_regs)
  1321. goto error;
  1322. }
  1323. cpuc->is_fake = 1;
  1324. return cpuc;
  1325. error:
  1326. free_fake_cpuc(cpuc);
  1327. return ERR_PTR(-ENOMEM);
  1328. }
  1329. /*
  1330. * validate that we can schedule this event
  1331. */
  1332. static int validate_event(struct perf_event *event)
  1333. {
  1334. struct cpu_hw_events *fake_cpuc;
  1335. struct event_constraint *c;
  1336. int ret = 0;
  1337. fake_cpuc = allocate_fake_cpuc();
  1338. if (IS_ERR(fake_cpuc))
  1339. return PTR_ERR(fake_cpuc);
  1340. c = x86_pmu.get_event_constraints(fake_cpuc, event);
  1341. if (!c || !c->weight)
  1342. ret = -EINVAL;
  1343. if (x86_pmu.put_event_constraints)
  1344. x86_pmu.put_event_constraints(fake_cpuc, event);
  1345. free_fake_cpuc(fake_cpuc);
  1346. return ret;
  1347. }
  1348. /*
  1349. * validate a single event group
  1350. *
  1351. * validation include:
  1352. * - check events are compatible which each other
  1353. * - events do not compete for the same counter
  1354. * - number of events <= number of counters
  1355. *
  1356. * validation ensures the group can be loaded onto the
  1357. * PMU if it was the only group available.
  1358. */
  1359. static int validate_group(struct perf_event *event)
  1360. {
  1361. struct perf_event *leader = event->group_leader;
  1362. struct cpu_hw_events *fake_cpuc;
  1363. int ret = -EINVAL, n;
  1364. fake_cpuc = allocate_fake_cpuc();
  1365. if (IS_ERR(fake_cpuc))
  1366. return PTR_ERR(fake_cpuc);
  1367. /*
  1368. * the event is not yet connected with its
  1369. * siblings therefore we must first collect
  1370. * existing siblings, then add the new event
  1371. * before we can simulate the scheduling
  1372. */
  1373. n = collect_events(fake_cpuc, leader, true);
  1374. if (n < 0)
  1375. goto out;
  1376. fake_cpuc->n_events = n;
  1377. n = collect_events(fake_cpuc, event, false);
  1378. if (n < 0)
  1379. goto out;
  1380. fake_cpuc->n_events = n;
  1381. ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
  1382. out:
  1383. free_fake_cpuc(fake_cpuc);
  1384. return ret;
  1385. }
  1386. static int x86_pmu_event_init(struct perf_event *event)
  1387. {
  1388. struct pmu *tmp;
  1389. int err;
  1390. switch (event->attr.type) {
  1391. case PERF_TYPE_RAW:
  1392. case PERF_TYPE_HARDWARE:
  1393. case PERF_TYPE_HW_CACHE:
  1394. break;
  1395. default:
  1396. return -ENOENT;
  1397. }
  1398. err = __x86_pmu_event_init(event);
  1399. if (!err) {
  1400. /*
  1401. * we temporarily connect event to its pmu
  1402. * such that validate_group() can classify
  1403. * it as an x86 event using is_x86_event()
  1404. */
  1405. tmp = event->pmu;
  1406. event->pmu = &pmu;
  1407. if (event->group_leader != event)
  1408. err = validate_group(event);
  1409. else
  1410. err = validate_event(event);
  1411. event->pmu = tmp;
  1412. }
  1413. if (err) {
  1414. if (event->destroy)
  1415. event->destroy(event);
  1416. }
  1417. return err;
  1418. }
  1419. static int x86_pmu_event_idx(struct perf_event *event)
  1420. {
  1421. int idx = event->hw.idx;
  1422. if (!x86_pmu.attr_rdpmc)
  1423. return 0;
  1424. if (x86_pmu.num_counters_fixed && idx >= INTEL_PMC_IDX_FIXED) {
  1425. idx -= INTEL_PMC_IDX_FIXED;
  1426. idx |= 1 << 30;
  1427. }
  1428. return idx + 1;
  1429. }
  1430. static ssize_t get_attr_rdpmc(struct device *cdev,
  1431. struct device_attribute *attr,
  1432. char *buf)
  1433. {
  1434. return snprintf(buf, 40, "%d\n", x86_pmu.attr_rdpmc);
  1435. }
  1436. static void change_rdpmc(void *info)
  1437. {
  1438. bool enable = !!(unsigned long)info;
  1439. if (enable)
  1440. set_in_cr4(X86_CR4_PCE);
  1441. else
  1442. clear_in_cr4(X86_CR4_PCE);
  1443. }
  1444. static ssize_t set_attr_rdpmc(struct device *cdev,
  1445. struct device_attribute *attr,
  1446. const char *buf, size_t count)
  1447. {
  1448. unsigned long val;
  1449. ssize_t ret;
  1450. ret = kstrtoul(buf, 0, &val);
  1451. if (ret)
  1452. return ret;
  1453. if (!!val != !!x86_pmu.attr_rdpmc) {
  1454. x86_pmu.attr_rdpmc = !!val;
  1455. smp_call_function(change_rdpmc, (void *)val, 1);
  1456. }
  1457. return count;
  1458. }
  1459. static DEVICE_ATTR(rdpmc, S_IRUSR | S_IWUSR, get_attr_rdpmc, set_attr_rdpmc);
  1460. static struct attribute *x86_pmu_attrs[] = {
  1461. &dev_attr_rdpmc.attr,
  1462. NULL,
  1463. };
  1464. static struct attribute_group x86_pmu_attr_group = {
  1465. .attrs = x86_pmu_attrs,
  1466. };
  1467. static const struct attribute_group *x86_pmu_attr_groups[] = {
  1468. &x86_pmu_attr_group,
  1469. &x86_pmu_format_group,
  1470. &x86_pmu_events_group,
  1471. NULL,
  1472. };
  1473. static void x86_pmu_flush_branch_stack(void)
  1474. {
  1475. if (x86_pmu.flush_branch_stack)
  1476. x86_pmu.flush_branch_stack();
  1477. }
  1478. void perf_check_microcode(void)
  1479. {
  1480. if (x86_pmu.check_microcode)
  1481. x86_pmu.check_microcode();
  1482. }
  1483. EXPORT_SYMBOL_GPL(perf_check_microcode);
  1484. static struct pmu pmu = {
  1485. .pmu_enable = x86_pmu_enable,
  1486. .pmu_disable = x86_pmu_disable,
  1487. .attr_groups = x86_pmu_attr_groups,
  1488. .event_init = x86_pmu_event_init,
  1489. .add = x86_pmu_add,
  1490. .del = x86_pmu_del,
  1491. .start = x86_pmu_start,
  1492. .stop = x86_pmu_stop,
  1493. .read = x86_pmu_read,
  1494. .start_txn = x86_pmu_start_txn,
  1495. .cancel_txn = x86_pmu_cancel_txn,
  1496. .commit_txn = x86_pmu_commit_txn,
  1497. .event_idx = x86_pmu_event_idx,
  1498. .flush_branch_stack = x86_pmu_flush_branch_stack,
  1499. };
  1500. void arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
  1501. {
  1502. userpg->cap_usr_time = 0;
  1503. userpg->cap_usr_rdpmc = x86_pmu.attr_rdpmc;
  1504. userpg->pmc_width = x86_pmu.cntval_bits;
  1505. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  1506. return;
  1507. if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  1508. return;
  1509. userpg->cap_usr_time = 1;
  1510. userpg->time_mult = this_cpu_read(cyc2ns);
  1511. userpg->time_shift = CYC2NS_SCALE_FACTOR;
  1512. userpg->time_offset = this_cpu_read(cyc2ns_offset) - now;
  1513. }
  1514. /*
  1515. * callchain support
  1516. */
  1517. static int backtrace_stack(void *data, char *name)
  1518. {
  1519. return 0;
  1520. }
  1521. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1522. {
  1523. struct perf_callchain_entry *entry = data;
  1524. perf_callchain_store(entry, addr);
  1525. }
  1526. static const struct stacktrace_ops backtrace_ops = {
  1527. .stack = backtrace_stack,
  1528. .address = backtrace_address,
  1529. .walk_stack = print_context_stack_bp,
  1530. };
  1531. void
  1532. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1533. {
  1534. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1535. /* TODO: We don't support guest os callchain now */
  1536. return;
  1537. }
  1538. perf_callchain_store(entry, regs->ip);
  1539. dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
  1540. }
  1541. static inline int
  1542. valid_user_frame(const void __user *fp, unsigned long size)
  1543. {
  1544. return (__range_not_ok(fp, size, TASK_SIZE) == 0);
  1545. }
  1546. static unsigned long get_segment_base(unsigned int segment)
  1547. {
  1548. struct desc_struct *desc;
  1549. int idx = segment >> 3;
  1550. if ((segment & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  1551. if (idx > LDT_ENTRIES)
  1552. return 0;
  1553. if (idx > current->active_mm->context.size)
  1554. return 0;
  1555. desc = current->active_mm->context.ldt;
  1556. } else {
  1557. if (idx > GDT_ENTRIES)
  1558. return 0;
  1559. desc = __this_cpu_ptr(&gdt_page.gdt[0]);
  1560. }
  1561. return get_desc_base(desc + idx);
  1562. }
  1563. #ifdef CONFIG_COMPAT
  1564. #include <asm/compat.h>
  1565. static inline int
  1566. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1567. {
  1568. /* 32-bit process in 64-bit kernel. */
  1569. unsigned long ss_base, cs_base;
  1570. struct stack_frame_ia32 frame;
  1571. const void __user *fp;
  1572. if (!test_thread_flag(TIF_IA32))
  1573. return 0;
  1574. cs_base = get_segment_base(regs->cs);
  1575. ss_base = get_segment_base(regs->ss);
  1576. fp = compat_ptr(ss_base + regs->bp);
  1577. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1578. unsigned long bytes;
  1579. frame.next_frame = 0;
  1580. frame.return_address = 0;
  1581. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1582. if (bytes != sizeof(frame))
  1583. break;
  1584. if (!valid_user_frame(fp, sizeof(frame)))
  1585. break;
  1586. perf_callchain_store(entry, cs_base + frame.return_address);
  1587. fp = compat_ptr(ss_base + frame.next_frame);
  1588. }
  1589. return 1;
  1590. }
  1591. #else
  1592. static inline int
  1593. perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1594. {
  1595. return 0;
  1596. }
  1597. #endif
  1598. void
  1599. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1600. {
  1601. struct stack_frame frame;
  1602. const void __user *fp;
  1603. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1604. /* TODO: We don't support guest os callchain now */
  1605. return;
  1606. }
  1607. /*
  1608. * We don't know what to do with VM86 stacks.. ignore them for now.
  1609. */
  1610. if (regs->flags & (X86_VM_MASK | PERF_EFLAGS_VM))
  1611. return;
  1612. fp = (void __user *)regs->bp;
  1613. perf_callchain_store(entry, regs->ip);
  1614. if (!current->mm)
  1615. return;
  1616. if (perf_callchain_user32(regs, entry))
  1617. return;
  1618. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  1619. unsigned long bytes;
  1620. frame.next_frame = NULL;
  1621. frame.return_address = 0;
  1622. bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
  1623. if (bytes != sizeof(frame))
  1624. break;
  1625. if (!valid_user_frame(fp, sizeof(frame)))
  1626. break;
  1627. perf_callchain_store(entry, frame.return_address);
  1628. fp = frame.next_frame;
  1629. }
  1630. }
  1631. /*
  1632. * Deal with code segment offsets for the various execution modes:
  1633. *
  1634. * VM86 - the good olde 16 bit days, where the linear address is
  1635. * 20 bits and we use regs->ip + 0x10 * regs->cs.
  1636. *
  1637. * IA32 - Where we need to look at GDT/LDT segment descriptor tables
  1638. * to figure out what the 32bit base address is.
  1639. *
  1640. * X32 - has TIF_X32 set, but is running in x86_64
  1641. *
  1642. * X86_64 - CS,DS,SS,ES are all zero based.
  1643. */
  1644. static unsigned long code_segment_base(struct pt_regs *regs)
  1645. {
  1646. /*
  1647. * If we are in VM86 mode, add the segment offset to convert to a
  1648. * linear address.
  1649. */
  1650. if (regs->flags & X86_VM_MASK)
  1651. return 0x10 * regs->cs;
  1652. /*
  1653. * For IA32 we look at the GDT/LDT segment base to convert the
  1654. * effective IP to a linear address.
  1655. */
  1656. #ifdef CONFIG_X86_32
  1657. if (user_mode(regs) && regs->cs != __USER_CS)
  1658. return get_segment_base(regs->cs);
  1659. #else
  1660. if (test_thread_flag(TIF_IA32)) {
  1661. if (user_mode(regs) && regs->cs != __USER32_CS)
  1662. return get_segment_base(regs->cs);
  1663. }
  1664. #endif
  1665. return 0;
  1666. }
  1667. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1668. {
  1669. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1670. return perf_guest_cbs->get_guest_ip();
  1671. return regs->ip + code_segment_base(regs);
  1672. }
  1673. unsigned long perf_misc_flags(struct pt_regs *regs)
  1674. {
  1675. int misc = 0;
  1676. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1677. if (perf_guest_cbs->is_user_mode())
  1678. misc |= PERF_RECORD_MISC_GUEST_USER;
  1679. else
  1680. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1681. } else {
  1682. if (user_mode(regs))
  1683. misc |= PERF_RECORD_MISC_USER;
  1684. else
  1685. misc |= PERF_RECORD_MISC_KERNEL;
  1686. }
  1687. if (regs->flags & PERF_EFLAGS_EXACT)
  1688. misc |= PERF_RECORD_MISC_EXACT_IP;
  1689. return misc;
  1690. }
  1691. void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
  1692. {
  1693. cap->version = x86_pmu.version;
  1694. cap->num_counters_gp = x86_pmu.num_counters;
  1695. cap->num_counters_fixed = x86_pmu.num_counters_fixed;
  1696. cap->bit_width_gp = x86_pmu.cntval_bits;
  1697. cap->bit_width_fixed = x86_pmu.cntval_bits;
  1698. cap->events_mask = (unsigned int)x86_pmu.events_maskl;
  1699. cap->events_mask_len = x86_pmu.events_mask_len;
  1700. }
  1701. EXPORT_SYMBOL_GPL(perf_get_x86_pmu_capability);