perf_event.c 47 KB

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