perf_event.c 49 KB

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