perf_event.c 47 KB

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