perf_event.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475
  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. *
  11. * For licencing details see kernel-base/COPYING
  12. */
  13. #include <linux/perf_event.h>
  14. #include <linux/capability.h>
  15. #include <linux/notifier.h>
  16. #include <linux/hardirq.h>
  17. #include <linux/kprobes.h>
  18. #include <linux/module.h>
  19. #include <linux/kdebug.h>
  20. #include <linux/sched.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/highmem.h>
  23. #include <linux/cpu.h>
  24. #include <asm/apic.h>
  25. #include <asm/stacktrace.h>
  26. #include <asm/nmi.h>
  27. static u64 perf_event_mask __read_mostly;
  28. /* The maximal number of PEBS events: */
  29. #define MAX_PEBS_EVENTS 4
  30. /* The size of a BTS record in bytes: */
  31. #define BTS_RECORD_SIZE 24
  32. /* The size of a per-cpu BTS buffer in bytes: */
  33. #define BTS_BUFFER_SIZE (BTS_RECORD_SIZE * 2048)
  34. /* The BTS overflow threshold in bytes from the end of the buffer: */
  35. #define BTS_OVFL_TH (BTS_RECORD_SIZE * 128)
  36. /*
  37. * Bits in the debugctlmsr controlling branch tracing.
  38. */
  39. #define X86_DEBUGCTL_TR (1 << 6)
  40. #define X86_DEBUGCTL_BTS (1 << 7)
  41. #define X86_DEBUGCTL_BTINT (1 << 8)
  42. #define X86_DEBUGCTL_BTS_OFF_OS (1 << 9)
  43. #define X86_DEBUGCTL_BTS_OFF_USR (1 << 10)
  44. /*
  45. * A debug store configuration.
  46. *
  47. * We only support architectures that use 64bit fields.
  48. */
  49. struct debug_store {
  50. u64 bts_buffer_base;
  51. u64 bts_index;
  52. u64 bts_absolute_maximum;
  53. u64 bts_interrupt_threshold;
  54. u64 pebs_buffer_base;
  55. u64 pebs_index;
  56. u64 pebs_absolute_maximum;
  57. u64 pebs_interrupt_threshold;
  58. u64 pebs_event_reset[MAX_PEBS_EVENTS];
  59. };
  60. struct cpu_hw_events {
  61. struct perf_event *events[X86_PMC_IDX_MAX];
  62. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  63. unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  64. unsigned long interrupts;
  65. int enabled;
  66. struct debug_store *ds;
  67. };
  68. struct event_constraint {
  69. unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  70. int code;
  71. };
  72. #define EVENT_CONSTRAINT(c, m) { .code = (c), .idxmsk[0] = (m) }
  73. #define EVENT_CONSTRAINT_END { .code = 0, .idxmsk[0] = 0 }
  74. #define for_each_event_constraint(e, c) \
  75. for ((e) = (c); (e)->idxmsk[0]; (e)++)
  76. /*
  77. * struct x86_pmu - generic x86 pmu
  78. */
  79. struct x86_pmu {
  80. const char *name;
  81. int version;
  82. int (*handle_irq)(struct pt_regs *);
  83. void (*disable_all)(void);
  84. void (*enable_all)(void);
  85. void (*enable)(struct hw_perf_event *, int);
  86. void (*disable)(struct hw_perf_event *, int);
  87. unsigned eventsel;
  88. unsigned perfctr;
  89. u64 (*event_map)(int);
  90. u64 (*raw_event)(u64);
  91. int max_events;
  92. int num_events;
  93. int num_events_fixed;
  94. int event_bits;
  95. u64 event_mask;
  96. int apic;
  97. u64 max_period;
  98. u64 intel_ctrl;
  99. void (*enable_bts)(u64 config);
  100. void (*disable_bts)(void);
  101. int (*get_event_idx)(struct cpu_hw_events *cpuc,
  102. struct hw_perf_event *hwc);
  103. };
  104. static struct x86_pmu x86_pmu __read_mostly;
  105. static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  106. .enabled = 1,
  107. };
  108. static const struct event_constraint *event_constraints;
  109. /*
  110. * Not sure about some of these
  111. */
  112. static const u64 p6_perfmon_event_map[] =
  113. {
  114. [PERF_COUNT_HW_CPU_CYCLES] = 0x0079,
  115. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  116. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0f2e,
  117. [PERF_COUNT_HW_CACHE_MISSES] = 0x012e,
  118. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
  119. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
  120. [PERF_COUNT_HW_BUS_CYCLES] = 0x0062,
  121. };
  122. static u64 p6_pmu_event_map(int hw_event)
  123. {
  124. return p6_perfmon_event_map[hw_event];
  125. }
  126. /*
  127. * Event setting that is specified not to count anything.
  128. * We use this to effectively disable a counter.
  129. *
  130. * L2_RQSTS with 0 MESI unit mask.
  131. */
  132. #define P6_NOP_EVENT 0x0000002EULL
  133. static u64 p6_pmu_raw_event(u64 hw_event)
  134. {
  135. #define P6_EVNTSEL_EVENT_MASK 0x000000FFULL
  136. #define P6_EVNTSEL_UNIT_MASK 0x0000FF00ULL
  137. #define P6_EVNTSEL_EDGE_MASK 0x00040000ULL
  138. #define P6_EVNTSEL_INV_MASK 0x00800000ULL
  139. #define P6_EVNTSEL_REG_MASK 0xFF000000ULL
  140. #define P6_EVNTSEL_MASK \
  141. (P6_EVNTSEL_EVENT_MASK | \
  142. P6_EVNTSEL_UNIT_MASK | \
  143. P6_EVNTSEL_EDGE_MASK | \
  144. P6_EVNTSEL_INV_MASK | \
  145. P6_EVNTSEL_REG_MASK)
  146. return hw_event & P6_EVNTSEL_MASK;
  147. }
  148. static const struct event_constraint intel_p6_event_constraints[] =
  149. {
  150. EVENT_CONSTRAINT(0xc1, 0x1), /* FLOPS */
  151. EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
  152. EVENT_CONSTRAINT(0x11, 0x1), /* FP_ASSIST */
  153. EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
  154. EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
  155. EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
  156. EVENT_CONSTRAINT_END
  157. };
  158. /*
  159. * Intel PerfMon v3. Used on Core2 and later.
  160. */
  161. static const u64 intel_perfmon_event_map[] =
  162. {
  163. [PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
  164. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  165. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e,
  166. [PERF_COUNT_HW_CACHE_MISSES] = 0x412e,
  167. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
  168. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
  169. [PERF_COUNT_HW_BUS_CYCLES] = 0x013c,
  170. };
  171. static const struct event_constraint intel_core_event_constraints[] =
  172. {
  173. EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
  174. EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
  175. EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
  176. EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
  177. EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
  178. EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */
  179. EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
  180. EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */
  181. EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */
  182. EVENT_CONSTRAINT_END
  183. };
  184. static const struct event_constraint intel_nehalem_event_constraints[] =
  185. {
  186. EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */
  187. EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */
  188. EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */
  189. EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */
  190. EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */
  191. EVENT_CONSTRAINT(0x4c, 0x3), /* LOAD_HIT_PRE */
  192. EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
  193. EVENT_CONSTRAINT(0x52, 0x3), /* L1D_CACHE_PREFETCH_LOCK_FB_HIT */
  194. EVENT_CONSTRAINT(0x53, 0x3), /* L1D_CACHE_LOCK_FB_HIT */
  195. EVENT_CONSTRAINT(0xc5, 0x3), /* CACHE_LOCK_CYCLES */
  196. EVENT_CONSTRAINT_END
  197. };
  198. static u64 intel_pmu_event_map(int hw_event)
  199. {
  200. return intel_perfmon_event_map[hw_event];
  201. }
  202. /*
  203. * Generalized hw caching related hw_event table, filled
  204. * in on a per model basis. A value of 0 means
  205. * 'not supported', -1 means 'hw_event makes no sense on
  206. * this CPU', any other value means the raw hw_event
  207. * ID.
  208. */
  209. #define C(x) PERF_COUNT_HW_CACHE_##x
  210. static u64 __read_mostly hw_cache_event_ids
  211. [PERF_COUNT_HW_CACHE_MAX]
  212. [PERF_COUNT_HW_CACHE_OP_MAX]
  213. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  214. static __initconst u64 nehalem_hw_cache_event_ids
  215. [PERF_COUNT_HW_CACHE_MAX]
  216. [PERF_COUNT_HW_CACHE_OP_MAX]
  217. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  218. {
  219. [ C(L1D) ] = {
  220. [ C(OP_READ) ] = {
  221. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
  222. [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
  223. },
  224. [ C(OP_WRITE) ] = {
  225. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
  226. [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
  227. },
  228. [ C(OP_PREFETCH) ] = {
  229. [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
  230. [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
  231. },
  232. },
  233. [ C(L1I ) ] = {
  234. [ C(OP_READ) ] = {
  235. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  236. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  237. },
  238. [ C(OP_WRITE) ] = {
  239. [ C(RESULT_ACCESS) ] = -1,
  240. [ C(RESULT_MISS) ] = -1,
  241. },
  242. [ C(OP_PREFETCH) ] = {
  243. [ C(RESULT_ACCESS) ] = 0x0,
  244. [ C(RESULT_MISS) ] = 0x0,
  245. },
  246. },
  247. [ C(LL ) ] = {
  248. [ C(OP_READ) ] = {
  249. [ C(RESULT_ACCESS) ] = 0x0324, /* L2_RQSTS.LOADS */
  250. [ C(RESULT_MISS) ] = 0x0224, /* L2_RQSTS.LD_MISS */
  251. },
  252. [ C(OP_WRITE) ] = {
  253. [ C(RESULT_ACCESS) ] = 0x0c24, /* L2_RQSTS.RFOS */
  254. [ C(RESULT_MISS) ] = 0x0824, /* L2_RQSTS.RFO_MISS */
  255. },
  256. [ C(OP_PREFETCH) ] = {
  257. [ C(RESULT_ACCESS) ] = 0x4f2e, /* LLC Reference */
  258. [ C(RESULT_MISS) ] = 0x412e, /* LLC Misses */
  259. },
  260. },
  261. [ C(DTLB) ] = {
  262. [ C(OP_READ) ] = {
  263. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  264. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
  265. },
  266. [ C(OP_WRITE) ] = {
  267. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  268. [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
  269. },
  270. [ C(OP_PREFETCH) ] = {
  271. [ C(RESULT_ACCESS) ] = 0x0,
  272. [ C(RESULT_MISS) ] = 0x0,
  273. },
  274. },
  275. [ C(ITLB) ] = {
  276. [ C(OP_READ) ] = {
  277. [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
  278. [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */
  279. },
  280. [ C(OP_WRITE) ] = {
  281. [ C(RESULT_ACCESS) ] = -1,
  282. [ C(RESULT_MISS) ] = -1,
  283. },
  284. [ C(OP_PREFETCH) ] = {
  285. [ C(RESULT_ACCESS) ] = -1,
  286. [ C(RESULT_MISS) ] = -1,
  287. },
  288. },
  289. [ C(BPU ) ] = {
  290. [ C(OP_READ) ] = {
  291. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  292. [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
  293. },
  294. [ C(OP_WRITE) ] = {
  295. [ C(RESULT_ACCESS) ] = -1,
  296. [ C(RESULT_MISS) ] = -1,
  297. },
  298. [ C(OP_PREFETCH) ] = {
  299. [ C(RESULT_ACCESS) ] = -1,
  300. [ C(RESULT_MISS) ] = -1,
  301. },
  302. },
  303. };
  304. static __initconst u64 core2_hw_cache_event_ids
  305. [PERF_COUNT_HW_CACHE_MAX]
  306. [PERF_COUNT_HW_CACHE_OP_MAX]
  307. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  308. {
  309. [ C(L1D) ] = {
  310. [ C(OP_READ) ] = {
  311. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
  312. [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
  313. },
  314. [ C(OP_WRITE) ] = {
  315. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
  316. [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
  317. },
  318. [ C(OP_PREFETCH) ] = {
  319. [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
  320. [ C(RESULT_MISS) ] = 0,
  321. },
  322. },
  323. [ C(L1I ) ] = {
  324. [ C(OP_READ) ] = {
  325. [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */
  326. [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */
  327. },
  328. [ C(OP_WRITE) ] = {
  329. [ C(RESULT_ACCESS) ] = -1,
  330. [ C(RESULT_MISS) ] = -1,
  331. },
  332. [ C(OP_PREFETCH) ] = {
  333. [ C(RESULT_ACCESS) ] = 0,
  334. [ C(RESULT_MISS) ] = 0,
  335. },
  336. },
  337. [ C(LL ) ] = {
  338. [ C(OP_READ) ] = {
  339. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  340. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  341. },
  342. [ C(OP_WRITE) ] = {
  343. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  344. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  345. },
  346. [ C(OP_PREFETCH) ] = {
  347. [ C(RESULT_ACCESS) ] = 0,
  348. [ C(RESULT_MISS) ] = 0,
  349. },
  350. },
  351. [ C(DTLB) ] = {
  352. [ C(OP_READ) ] = {
  353. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  354. [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
  355. },
  356. [ C(OP_WRITE) ] = {
  357. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  358. [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
  359. },
  360. [ C(OP_PREFETCH) ] = {
  361. [ C(RESULT_ACCESS) ] = 0,
  362. [ C(RESULT_MISS) ] = 0,
  363. },
  364. },
  365. [ C(ITLB) ] = {
  366. [ C(OP_READ) ] = {
  367. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  368. [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */
  369. },
  370. [ C(OP_WRITE) ] = {
  371. [ C(RESULT_ACCESS) ] = -1,
  372. [ C(RESULT_MISS) ] = -1,
  373. },
  374. [ C(OP_PREFETCH) ] = {
  375. [ C(RESULT_ACCESS) ] = -1,
  376. [ C(RESULT_MISS) ] = -1,
  377. },
  378. },
  379. [ C(BPU ) ] = {
  380. [ C(OP_READ) ] = {
  381. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  382. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  383. },
  384. [ C(OP_WRITE) ] = {
  385. [ C(RESULT_ACCESS) ] = -1,
  386. [ C(RESULT_MISS) ] = -1,
  387. },
  388. [ C(OP_PREFETCH) ] = {
  389. [ C(RESULT_ACCESS) ] = -1,
  390. [ C(RESULT_MISS) ] = -1,
  391. },
  392. },
  393. };
  394. static __initconst u64 atom_hw_cache_event_ids
  395. [PERF_COUNT_HW_CACHE_MAX]
  396. [PERF_COUNT_HW_CACHE_OP_MAX]
  397. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  398. {
  399. [ C(L1D) ] = {
  400. [ C(OP_READ) ] = {
  401. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */
  402. [ C(RESULT_MISS) ] = 0,
  403. },
  404. [ C(OP_WRITE) ] = {
  405. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */
  406. [ C(RESULT_MISS) ] = 0,
  407. },
  408. [ C(OP_PREFETCH) ] = {
  409. [ C(RESULT_ACCESS) ] = 0x0,
  410. [ C(RESULT_MISS) ] = 0,
  411. },
  412. },
  413. [ C(L1I ) ] = {
  414. [ C(OP_READ) ] = {
  415. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  416. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  417. },
  418. [ C(OP_WRITE) ] = {
  419. [ C(RESULT_ACCESS) ] = -1,
  420. [ C(RESULT_MISS) ] = -1,
  421. },
  422. [ C(OP_PREFETCH) ] = {
  423. [ C(RESULT_ACCESS) ] = 0,
  424. [ C(RESULT_MISS) ] = 0,
  425. },
  426. },
  427. [ C(LL ) ] = {
  428. [ C(OP_READ) ] = {
  429. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  430. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  431. },
  432. [ C(OP_WRITE) ] = {
  433. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  434. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  435. },
  436. [ C(OP_PREFETCH) ] = {
  437. [ C(RESULT_ACCESS) ] = 0,
  438. [ C(RESULT_MISS) ] = 0,
  439. },
  440. },
  441. [ C(DTLB) ] = {
  442. [ C(OP_READ) ] = {
  443. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
  444. [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
  445. },
  446. [ C(OP_WRITE) ] = {
  447. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
  448. [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
  449. },
  450. [ C(OP_PREFETCH) ] = {
  451. [ C(RESULT_ACCESS) ] = 0,
  452. [ C(RESULT_MISS) ] = 0,
  453. },
  454. },
  455. [ C(ITLB) ] = {
  456. [ C(OP_READ) ] = {
  457. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  458. [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */
  459. },
  460. [ C(OP_WRITE) ] = {
  461. [ C(RESULT_ACCESS) ] = -1,
  462. [ C(RESULT_MISS) ] = -1,
  463. },
  464. [ C(OP_PREFETCH) ] = {
  465. [ C(RESULT_ACCESS) ] = -1,
  466. [ C(RESULT_MISS) ] = -1,
  467. },
  468. },
  469. [ C(BPU ) ] = {
  470. [ C(OP_READ) ] = {
  471. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  472. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  473. },
  474. [ C(OP_WRITE) ] = {
  475. [ C(RESULT_ACCESS) ] = -1,
  476. [ C(RESULT_MISS) ] = -1,
  477. },
  478. [ C(OP_PREFETCH) ] = {
  479. [ C(RESULT_ACCESS) ] = -1,
  480. [ C(RESULT_MISS) ] = -1,
  481. },
  482. },
  483. };
  484. static u64 intel_pmu_raw_event(u64 hw_event)
  485. {
  486. #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
  487. #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
  488. #define CORE_EVNTSEL_EDGE_MASK 0x00040000ULL
  489. #define CORE_EVNTSEL_INV_MASK 0x00800000ULL
  490. #define CORE_EVNTSEL_REG_MASK 0xFF000000ULL
  491. #define CORE_EVNTSEL_MASK \
  492. (CORE_EVNTSEL_EVENT_MASK | \
  493. CORE_EVNTSEL_UNIT_MASK | \
  494. CORE_EVNTSEL_EDGE_MASK | \
  495. CORE_EVNTSEL_INV_MASK | \
  496. CORE_EVNTSEL_REG_MASK)
  497. return hw_event & CORE_EVNTSEL_MASK;
  498. }
  499. static __initconst u64 amd_hw_cache_event_ids
  500. [PERF_COUNT_HW_CACHE_MAX]
  501. [PERF_COUNT_HW_CACHE_OP_MAX]
  502. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  503. {
  504. [ C(L1D) ] = {
  505. [ C(OP_READ) ] = {
  506. [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
  507. [ C(RESULT_MISS) ] = 0x0041, /* Data Cache Misses */
  508. },
  509. [ C(OP_WRITE) ] = {
  510. [ C(RESULT_ACCESS) ] = 0x0142, /* Data Cache Refills :system */
  511. [ C(RESULT_MISS) ] = 0,
  512. },
  513. [ C(OP_PREFETCH) ] = {
  514. [ C(RESULT_ACCESS) ] = 0x0267, /* Data Prefetcher :attempts */
  515. [ C(RESULT_MISS) ] = 0x0167, /* Data Prefetcher :cancelled */
  516. },
  517. },
  518. [ C(L1I ) ] = {
  519. [ C(OP_READ) ] = {
  520. [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction cache fetches */
  521. [ C(RESULT_MISS) ] = 0x0081, /* Instruction cache misses */
  522. },
  523. [ C(OP_WRITE) ] = {
  524. [ C(RESULT_ACCESS) ] = -1,
  525. [ C(RESULT_MISS) ] = -1,
  526. },
  527. [ C(OP_PREFETCH) ] = {
  528. [ C(RESULT_ACCESS) ] = 0x014B, /* Prefetch Instructions :Load */
  529. [ C(RESULT_MISS) ] = 0,
  530. },
  531. },
  532. [ C(LL ) ] = {
  533. [ C(OP_READ) ] = {
  534. [ C(RESULT_ACCESS) ] = 0x037D, /* Requests to L2 Cache :IC+DC */
  535. [ C(RESULT_MISS) ] = 0x037E, /* L2 Cache Misses : IC+DC */
  536. },
  537. [ C(OP_WRITE) ] = {
  538. [ C(RESULT_ACCESS) ] = 0x017F, /* L2 Fill/Writeback */
  539. [ C(RESULT_MISS) ] = 0,
  540. },
  541. [ C(OP_PREFETCH) ] = {
  542. [ C(RESULT_ACCESS) ] = 0,
  543. [ C(RESULT_MISS) ] = 0,
  544. },
  545. },
  546. [ C(DTLB) ] = {
  547. [ C(OP_READ) ] = {
  548. [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
  549. [ C(RESULT_MISS) ] = 0x0046, /* L1 DTLB and L2 DLTB Miss */
  550. },
  551. [ C(OP_WRITE) ] = {
  552. [ C(RESULT_ACCESS) ] = 0,
  553. [ C(RESULT_MISS) ] = 0,
  554. },
  555. [ C(OP_PREFETCH) ] = {
  556. [ C(RESULT_ACCESS) ] = 0,
  557. [ C(RESULT_MISS) ] = 0,
  558. },
  559. },
  560. [ C(ITLB) ] = {
  561. [ C(OP_READ) ] = {
  562. [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction fecthes */
  563. [ C(RESULT_MISS) ] = 0x0085, /* Instr. fetch ITLB misses */
  564. },
  565. [ C(OP_WRITE) ] = {
  566. [ C(RESULT_ACCESS) ] = -1,
  567. [ C(RESULT_MISS) ] = -1,
  568. },
  569. [ C(OP_PREFETCH) ] = {
  570. [ C(RESULT_ACCESS) ] = -1,
  571. [ C(RESULT_MISS) ] = -1,
  572. },
  573. },
  574. [ C(BPU ) ] = {
  575. [ C(OP_READ) ] = {
  576. [ C(RESULT_ACCESS) ] = 0x00c2, /* Retired Branch Instr. */
  577. [ C(RESULT_MISS) ] = 0x00c3, /* Retired Mispredicted BI */
  578. },
  579. [ C(OP_WRITE) ] = {
  580. [ C(RESULT_ACCESS) ] = -1,
  581. [ C(RESULT_MISS) ] = -1,
  582. },
  583. [ C(OP_PREFETCH) ] = {
  584. [ C(RESULT_ACCESS) ] = -1,
  585. [ C(RESULT_MISS) ] = -1,
  586. },
  587. },
  588. };
  589. /*
  590. * AMD Performance Monitor K7 and later.
  591. */
  592. static const u64 amd_perfmon_event_map[] =
  593. {
  594. [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
  595. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  596. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0080,
  597. [PERF_COUNT_HW_CACHE_MISSES] = 0x0081,
  598. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
  599. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
  600. };
  601. static u64 amd_pmu_event_map(int hw_event)
  602. {
  603. return amd_perfmon_event_map[hw_event];
  604. }
  605. static u64 amd_pmu_raw_event(u64 hw_event)
  606. {
  607. #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
  608. #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
  609. #define K7_EVNTSEL_EDGE_MASK 0x000040000ULL
  610. #define K7_EVNTSEL_INV_MASK 0x000800000ULL
  611. #define K7_EVNTSEL_REG_MASK 0x0FF000000ULL
  612. #define K7_EVNTSEL_MASK \
  613. (K7_EVNTSEL_EVENT_MASK | \
  614. K7_EVNTSEL_UNIT_MASK | \
  615. K7_EVNTSEL_EDGE_MASK | \
  616. K7_EVNTSEL_INV_MASK | \
  617. K7_EVNTSEL_REG_MASK)
  618. return hw_event & K7_EVNTSEL_MASK;
  619. }
  620. /*
  621. * Propagate event elapsed time into the generic event.
  622. * Can only be executed on the CPU where the event is active.
  623. * Returns the delta events processed.
  624. */
  625. static u64
  626. x86_perf_event_update(struct perf_event *event,
  627. struct hw_perf_event *hwc, int idx)
  628. {
  629. int shift = 64 - x86_pmu.event_bits;
  630. u64 prev_raw_count, new_raw_count;
  631. s64 delta;
  632. if (idx == X86_PMC_IDX_FIXED_BTS)
  633. return 0;
  634. /*
  635. * Careful: an NMI might modify the previous event value.
  636. *
  637. * Our tactic to handle this is to first atomically read and
  638. * exchange a new raw count - then add that new-prev delta
  639. * count to the generic event atomically:
  640. */
  641. again:
  642. prev_raw_count = atomic64_read(&hwc->prev_count);
  643. rdmsrl(hwc->event_base + idx, new_raw_count);
  644. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  645. new_raw_count) != prev_raw_count)
  646. goto again;
  647. /*
  648. * Now we have the new raw value and have updated the prev
  649. * timestamp already. We can now calculate the elapsed delta
  650. * (event-)time and add that to the generic event.
  651. *
  652. * Careful, not all hw sign-extends above the physical width
  653. * of the count.
  654. */
  655. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  656. delta >>= shift;
  657. atomic64_add(delta, &event->count);
  658. atomic64_sub(delta, &hwc->period_left);
  659. return new_raw_count;
  660. }
  661. static atomic_t active_events;
  662. static DEFINE_MUTEX(pmc_reserve_mutex);
  663. static bool reserve_pmc_hardware(void)
  664. {
  665. #ifdef CONFIG_X86_LOCAL_APIC
  666. int i;
  667. if (nmi_watchdog == NMI_LOCAL_APIC)
  668. disable_lapic_nmi_watchdog();
  669. for (i = 0; i < x86_pmu.num_events; i++) {
  670. if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
  671. goto perfctr_fail;
  672. }
  673. for (i = 0; i < x86_pmu.num_events; i++) {
  674. if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
  675. goto eventsel_fail;
  676. }
  677. #endif
  678. return true;
  679. #ifdef CONFIG_X86_LOCAL_APIC
  680. eventsel_fail:
  681. for (i--; i >= 0; i--)
  682. release_evntsel_nmi(x86_pmu.eventsel + i);
  683. i = x86_pmu.num_events;
  684. perfctr_fail:
  685. for (i--; i >= 0; i--)
  686. release_perfctr_nmi(x86_pmu.perfctr + i);
  687. if (nmi_watchdog == NMI_LOCAL_APIC)
  688. enable_lapic_nmi_watchdog();
  689. return false;
  690. #endif
  691. }
  692. static void release_pmc_hardware(void)
  693. {
  694. #ifdef CONFIG_X86_LOCAL_APIC
  695. int i;
  696. for (i = 0; i < x86_pmu.num_events; i++) {
  697. release_perfctr_nmi(x86_pmu.perfctr + i);
  698. release_evntsel_nmi(x86_pmu.eventsel + i);
  699. }
  700. if (nmi_watchdog == NMI_LOCAL_APIC)
  701. enable_lapic_nmi_watchdog();
  702. #endif
  703. }
  704. static inline bool bts_available(void)
  705. {
  706. return x86_pmu.enable_bts != NULL;
  707. }
  708. static inline void init_debug_store_on_cpu(int cpu)
  709. {
  710. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  711. if (!ds)
  712. return;
  713. wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
  714. (u32)((u64)(unsigned long)ds),
  715. (u32)((u64)(unsigned long)ds >> 32));
  716. }
  717. static inline void fini_debug_store_on_cpu(int cpu)
  718. {
  719. if (!per_cpu(cpu_hw_events, cpu).ds)
  720. return;
  721. wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
  722. }
  723. static void release_bts_hardware(void)
  724. {
  725. int cpu;
  726. if (!bts_available())
  727. return;
  728. get_online_cpus();
  729. for_each_online_cpu(cpu)
  730. fini_debug_store_on_cpu(cpu);
  731. for_each_possible_cpu(cpu) {
  732. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  733. if (!ds)
  734. continue;
  735. per_cpu(cpu_hw_events, cpu).ds = NULL;
  736. kfree((void *)(unsigned long)ds->bts_buffer_base);
  737. kfree(ds);
  738. }
  739. put_online_cpus();
  740. }
  741. static int reserve_bts_hardware(void)
  742. {
  743. int cpu, err = 0;
  744. if (!bts_available())
  745. return 0;
  746. get_online_cpus();
  747. for_each_possible_cpu(cpu) {
  748. struct debug_store *ds;
  749. void *buffer;
  750. err = -ENOMEM;
  751. buffer = kzalloc(BTS_BUFFER_SIZE, GFP_KERNEL);
  752. if (unlikely(!buffer))
  753. break;
  754. ds = kzalloc(sizeof(*ds), GFP_KERNEL);
  755. if (unlikely(!ds)) {
  756. kfree(buffer);
  757. break;
  758. }
  759. ds->bts_buffer_base = (u64)(unsigned long)buffer;
  760. ds->bts_index = ds->bts_buffer_base;
  761. ds->bts_absolute_maximum =
  762. ds->bts_buffer_base + BTS_BUFFER_SIZE;
  763. ds->bts_interrupt_threshold =
  764. ds->bts_absolute_maximum - BTS_OVFL_TH;
  765. per_cpu(cpu_hw_events, cpu).ds = ds;
  766. err = 0;
  767. }
  768. if (err)
  769. release_bts_hardware();
  770. else {
  771. for_each_online_cpu(cpu)
  772. init_debug_store_on_cpu(cpu);
  773. }
  774. put_online_cpus();
  775. return err;
  776. }
  777. static void hw_perf_event_destroy(struct perf_event *event)
  778. {
  779. if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
  780. release_pmc_hardware();
  781. release_bts_hardware();
  782. mutex_unlock(&pmc_reserve_mutex);
  783. }
  784. }
  785. static inline int x86_pmu_initialized(void)
  786. {
  787. return x86_pmu.handle_irq != NULL;
  788. }
  789. static inline int
  790. set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event_attr *attr)
  791. {
  792. unsigned int cache_type, cache_op, cache_result;
  793. u64 config, val;
  794. config = attr->config;
  795. cache_type = (config >> 0) & 0xff;
  796. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  797. return -EINVAL;
  798. cache_op = (config >> 8) & 0xff;
  799. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  800. return -EINVAL;
  801. cache_result = (config >> 16) & 0xff;
  802. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  803. return -EINVAL;
  804. val = hw_cache_event_ids[cache_type][cache_op][cache_result];
  805. if (val == 0)
  806. return -ENOENT;
  807. if (val == -1)
  808. return -EINVAL;
  809. hwc->config |= val;
  810. return 0;
  811. }
  812. static void intel_pmu_enable_bts(u64 config)
  813. {
  814. unsigned long debugctlmsr;
  815. debugctlmsr = get_debugctlmsr();
  816. debugctlmsr |= X86_DEBUGCTL_TR;
  817. debugctlmsr |= X86_DEBUGCTL_BTS;
  818. debugctlmsr |= X86_DEBUGCTL_BTINT;
  819. if (!(config & ARCH_PERFMON_EVENTSEL_OS))
  820. debugctlmsr |= X86_DEBUGCTL_BTS_OFF_OS;
  821. if (!(config & ARCH_PERFMON_EVENTSEL_USR))
  822. debugctlmsr |= X86_DEBUGCTL_BTS_OFF_USR;
  823. update_debugctlmsr(debugctlmsr);
  824. }
  825. static void intel_pmu_disable_bts(void)
  826. {
  827. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  828. unsigned long debugctlmsr;
  829. if (!cpuc->ds)
  830. return;
  831. debugctlmsr = get_debugctlmsr();
  832. debugctlmsr &=
  833. ~(X86_DEBUGCTL_TR | X86_DEBUGCTL_BTS | X86_DEBUGCTL_BTINT |
  834. X86_DEBUGCTL_BTS_OFF_OS | X86_DEBUGCTL_BTS_OFF_USR);
  835. update_debugctlmsr(debugctlmsr);
  836. }
  837. /*
  838. * Setup the hardware configuration for a given attr_type
  839. */
  840. static int __hw_perf_event_init(struct perf_event *event)
  841. {
  842. struct perf_event_attr *attr = &event->attr;
  843. struct hw_perf_event *hwc = &event->hw;
  844. u64 config;
  845. int err;
  846. if (!x86_pmu_initialized())
  847. return -ENODEV;
  848. err = 0;
  849. if (!atomic_inc_not_zero(&active_events)) {
  850. mutex_lock(&pmc_reserve_mutex);
  851. if (atomic_read(&active_events) == 0) {
  852. if (!reserve_pmc_hardware())
  853. err = -EBUSY;
  854. else
  855. err = reserve_bts_hardware();
  856. }
  857. if (!err)
  858. atomic_inc(&active_events);
  859. mutex_unlock(&pmc_reserve_mutex);
  860. }
  861. if (err)
  862. return err;
  863. event->destroy = hw_perf_event_destroy;
  864. /*
  865. * Generate PMC IRQs:
  866. * (keep 'enabled' bit clear for now)
  867. */
  868. hwc->config = ARCH_PERFMON_EVENTSEL_INT;
  869. hwc->idx = -1;
  870. /*
  871. * Count user and OS events unless requested not to.
  872. */
  873. if (!attr->exclude_user)
  874. hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
  875. if (!attr->exclude_kernel)
  876. hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
  877. if (!hwc->sample_period) {
  878. hwc->sample_period = x86_pmu.max_period;
  879. hwc->last_period = hwc->sample_period;
  880. atomic64_set(&hwc->period_left, hwc->sample_period);
  881. } else {
  882. /*
  883. * If we have a PMU initialized but no APIC
  884. * interrupts, we cannot sample hardware
  885. * events (user-space has to fall back and
  886. * sample via a hrtimer based software event):
  887. */
  888. if (!x86_pmu.apic)
  889. return -EOPNOTSUPP;
  890. }
  891. /*
  892. * Raw hw_event type provide the config in the hw_event structure
  893. */
  894. if (attr->type == PERF_TYPE_RAW) {
  895. hwc->config |= x86_pmu.raw_event(attr->config);
  896. return 0;
  897. }
  898. if (attr->type == PERF_TYPE_HW_CACHE)
  899. return set_ext_hw_attr(hwc, attr);
  900. if (attr->config >= x86_pmu.max_events)
  901. return -EINVAL;
  902. /*
  903. * The generic map:
  904. */
  905. config = x86_pmu.event_map(attr->config);
  906. if (config == 0)
  907. return -ENOENT;
  908. if (config == -1LL)
  909. return -EINVAL;
  910. /*
  911. * Branch tracing:
  912. */
  913. if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
  914. (hwc->sample_period == 1)) {
  915. /* BTS is not supported by this architecture. */
  916. if (!bts_available())
  917. return -EOPNOTSUPP;
  918. /* BTS is currently only allowed for user-mode. */
  919. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  920. return -EOPNOTSUPP;
  921. }
  922. hwc->config |= config;
  923. return 0;
  924. }
  925. static void p6_pmu_disable_all(void)
  926. {
  927. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  928. u64 val;
  929. if (!cpuc->enabled)
  930. return;
  931. cpuc->enabled = 0;
  932. barrier();
  933. /* p6 only has one enable register */
  934. rdmsrl(MSR_P6_EVNTSEL0, val);
  935. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  936. wrmsrl(MSR_P6_EVNTSEL0, val);
  937. }
  938. static void intel_pmu_disable_all(void)
  939. {
  940. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  941. if (!cpuc->enabled)
  942. return;
  943. cpuc->enabled = 0;
  944. barrier();
  945. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  946. if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask))
  947. intel_pmu_disable_bts();
  948. }
  949. static void amd_pmu_disable_all(void)
  950. {
  951. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  952. int idx;
  953. if (!cpuc->enabled)
  954. return;
  955. cpuc->enabled = 0;
  956. /*
  957. * ensure we write the disable before we start disabling the
  958. * events proper, so that amd_pmu_enable_event() does the
  959. * right thing.
  960. */
  961. barrier();
  962. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  963. u64 val;
  964. if (!test_bit(idx, cpuc->active_mask))
  965. continue;
  966. rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
  967. if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
  968. continue;
  969. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  970. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  971. }
  972. }
  973. void hw_perf_disable(void)
  974. {
  975. if (!x86_pmu_initialized())
  976. return;
  977. return x86_pmu.disable_all();
  978. }
  979. static void p6_pmu_enable_all(void)
  980. {
  981. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  982. unsigned long val;
  983. if (cpuc->enabled)
  984. return;
  985. cpuc->enabled = 1;
  986. barrier();
  987. /* p6 only has one enable register */
  988. rdmsrl(MSR_P6_EVNTSEL0, val);
  989. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  990. wrmsrl(MSR_P6_EVNTSEL0, val);
  991. }
  992. static void intel_pmu_enable_all(void)
  993. {
  994. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  995. if (cpuc->enabled)
  996. return;
  997. cpuc->enabled = 1;
  998. barrier();
  999. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
  1000. if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
  1001. struct perf_event *event =
  1002. cpuc->events[X86_PMC_IDX_FIXED_BTS];
  1003. if (WARN_ON_ONCE(!event))
  1004. return;
  1005. intel_pmu_enable_bts(event->hw.config);
  1006. }
  1007. }
  1008. static void amd_pmu_enable_all(void)
  1009. {
  1010. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1011. int idx;
  1012. if (cpuc->enabled)
  1013. return;
  1014. cpuc->enabled = 1;
  1015. barrier();
  1016. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  1017. struct perf_event *event = cpuc->events[idx];
  1018. u64 val;
  1019. if (!test_bit(idx, cpuc->active_mask))
  1020. continue;
  1021. val = event->hw.config;
  1022. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  1023. wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
  1024. }
  1025. }
  1026. void hw_perf_enable(void)
  1027. {
  1028. if (!x86_pmu_initialized())
  1029. return;
  1030. x86_pmu.enable_all();
  1031. }
  1032. static inline u64 intel_pmu_get_status(void)
  1033. {
  1034. u64 status;
  1035. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  1036. return status;
  1037. }
  1038. static inline void intel_pmu_ack_status(u64 ack)
  1039. {
  1040. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  1041. }
  1042. static inline void x86_pmu_enable_event(struct hw_perf_event *hwc, int idx)
  1043. {
  1044. (void)checking_wrmsrl(hwc->config_base + idx,
  1045. hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
  1046. }
  1047. static inline void x86_pmu_disable_event(struct hw_perf_event *hwc, int idx)
  1048. {
  1049. (void)checking_wrmsrl(hwc->config_base + idx, hwc->config);
  1050. }
  1051. static inline void
  1052. intel_pmu_disable_fixed(struct hw_perf_event *hwc, int __idx)
  1053. {
  1054. int idx = __idx - X86_PMC_IDX_FIXED;
  1055. u64 ctrl_val, mask;
  1056. mask = 0xfULL << (idx * 4);
  1057. rdmsrl(hwc->config_base, ctrl_val);
  1058. ctrl_val &= ~mask;
  1059. (void)checking_wrmsrl(hwc->config_base, ctrl_val);
  1060. }
  1061. static inline void
  1062. p6_pmu_disable_event(struct hw_perf_event *hwc, int idx)
  1063. {
  1064. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1065. u64 val = P6_NOP_EVENT;
  1066. if (cpuc->enabled)
  1067. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  1068. (void)checking_wrmsrl(hwc->config_base + idx, val);
  1069. }
  1070. static inline void
  1071. intel_pmu_disable_event(struct hw_perf_event *hwc, int idx)
  1072. {
  1073. if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) {
  1074. intel_pmu_disable_bts();
  1075. return;
  1076. }
  1077. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  1078. intel_pmu_disable_fixed(hwc, idx);
  1079. return;
  1080. }
  1081. x86_pmu_disable_event(hwc, idx);
  1082. }
  1083. static inline void
  1084. amd_pmu_disable_event(struct hw_perf_event *hwc, int idx)
  1085. {
  1086. x86_pmu_disable_event(hwc, idx);
  1087. }
  1088. static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
  1089. /*
  1090. * Set the next IRQ period, based on the hwc->period_left value.
  1091. * To be called with the event disabled in hw:
  1092. */
  1093. static int
  1094. x86_perf_event_set_period(struct perf_event *event,
  1095. struct hw_perf_event *hwc, int idx)
  1096. {
  1097. s64 left = atomic64_read(&hwc->period_left);
  1098. s64 period = hwc->sample_period;
  1099. int err, ret = 0;
  1100. if (idx == X86_PMC_IDX_FIXED_BTS)
  1101. return 0;
  1102. /*
  1103. * If we are way outside a reasonable range then just skip forward:
  1104. */
  1105. if (unlikely(left <= -period)) {
  1106. left = period;
  1107. atomic64_set(&hwc->period_left, left);
  1108. hwc->last_period = period;
  1109. ret = 1;
  1110. }
  1111. if (unlikely(left <= 0)) {
  1112. left += period;
  1113. atomic64_set(&hwc->period_left, left);
  1114. hwc->last_period = period;
  1115. ret = 1;
  1116. }
  1117. /*
  1118. * Quirk: certain CPUs dont like it if just 1 hw_event is left:
  1119. */
  1120. if (unlikely(left < 2))
  1121. left = 2;
  1122. if (left > x86_pmu.max_period)
  1123. left = x86_pmu.max_period;
  1124. per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
  1125. /*
  1126. * The hw event starts counting from this event offset,
  1127. * mark it to be able to extra future deltas:
  1128. */
  1129. atomic64_set(&hwc->prev_count, (u64)-left);
  1130. err = checking_wrmsrl(hwc->event_base + idx,
  1131. (u64)(-left) & x86_pmu.event_mask);
  1132. perf_event_update_userpage(event);
  1133. return ret;
  1134. }
  1135. static inline void
  1136. intel_pmu_enable_fixed(struct hw_perf_event *hwc, int __idx)
  1137. {
  1138. int idx = __idx - X86_PMC_IDX_FIXED;
  1139. u64 ctrl_val, bits, mask;
  1140. int err;
  1141. /*
  1142. * Enable IRQ generation (0x8),
  1143. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  1144. * if requested:
  1145. */
  1146. bits = 0x8ULL;
  1147. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  1148. bits |= 0x2;
  1149. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  1150. bits |= 0x1;
  1151. /*
  1152. * ANY bit is supported in v3 and up
  1153. */
  1154. if (x86_pmu.version > 2 && hwc->config & ARCH_PERFMON_EVENTSEL_ANY)
  1155. bits |= 0x4;
  1156. bits <<= (idx * 4);
  1157. mask = 0xfULL << (idx * 4);
  1158. rdmsrl(hwc->config_base, ctrl_val);
  1159. ctrl_val &= ~mask;
  1160. ctrl_val |= bits;
  1161. err = checking_wrmsrl(hwc->config_base, ctrl_val);
  1162. }
  1163. static void p6_pmu_enable_event(struct hw_perf_event *hwc, int idx)
  1164. {
  1165. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1166. u64 val;
  1167. val = hwc->config;
  1168. if (cpuc->enabled)
  1169. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  1170. (void)checking_wrmsrl(hwc->config_base + idx, val);
  1171. }
  1172. static void intel_pmu_enable_event(struct hw_perf_event *hwc, int idx)
  1173. {
  1174. if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) {
  1175. if (!__get_cpu_var(cpu_hw_events).enabled)
  1176. return;
  1177. intel_pmu_enable_bts(hwc->config);
  1178. return;
  1179. }
  1180. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  1181. intel_pmu_enable_fixed(hwc, idx);
  1182. return;
  1183. }
  1184. x86_pmu_enable_event(hwc, idx);
  1185. }
  1186. static void amd_pmu_enable_event(struct hw_perf_event *hwc, int idx)
  1187. {
  1188. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1189. if (cpuc->enabled)
  1190. x86_pmu_enable_event(hwc, idx);
  1191. }
  1192. static int fixed_mode_idx(struct hw_perf_event *hwc)
  1193. {
  1194. unsigned int hw_event;
  1195. hw_event = hwc->config & ARCH_PERFMON_EVENT_MASK;
  1196. if (unlikely((hw_event ==
  1197. x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS)) &&
  1198. (hwc->sample_period == 1)))
  1199. return X86_PMC_IDX_FIXED_BTS;
  1200. if (!x86_pmu.num_events_fixed)
  1201. return -1;
  1202. /*
  1203. * fixed counters do not take all possible filters
  1204. */
  1205. if (hwc->config & ARCH_PERFMON_EVENT_FILTER_MASK)
  1206. return -1;
  1207. if (unlikely(hw_event == x86_pmu.event_map(PERF_COUNT_HW_INSTRUCTIONS)))
  1208. return X86_PMC_IDX_FIXED_INSTRUCTIONS;
  1209. if (unlikely(hw_event == x86_pmu.event_map(PERF_COUNT_HW_CPU_CYCLES)))
  1210. return X86_PMC_IDX_FIXED_CPU_CYCLES;
  1211. if (unlikely(hw_event == x86_pmu.event_map(PERF_COUNT_HW_BUS_CYCLES)))
  1212. return X86_PMC_IDX_FIXED_BUS_CYCLES;
  1213. return -1;
  1214. }
  1215. /*
  1216. * generic counter allocator: get next free counter
  1217. */
  1218. static int
  1219. gen_get_event_idx(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
  1220. {
  1221. int idx;
  1222. idx = find_first_zero_bit(cpuc->used_mask, x86_pmu.num_events);
  1223. return idx == x86_pmu.num_events ? -1 : idx;
  1224. }
  1225. /*
  1226. * intel-specific counter allocator: check event constraints
  1227. */
  1228. static int
  1229. intel_get_event_idx(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
  1230. {
  1231. const struct event_constraint *event_constraint;
  1232. int i, code;
  1233. if (!event_constraints)
  1234. goto skip;
  1235. code = hwc->config & CORE_EVNTSEL_EVENT_MASK;
  1236. for_each_event_constraint(event_constraint, event_constraints) {
  1237. if (code == event_constraint->code) {
  1238. for_each_bit(i, event_constraint->idxmsk, X86_PMC_IDX_MAX) {
  1239. if (!test_and_set_bit(i, cpuc->used_mask))
  1240. return i;
  1241. }
  1242. return -1;
  1243. }
  1244. }
  1245. skip:
  1246. return gen_get_event_idx(cpuc, hwc);
  1247. }
  1248. static int
  1249. x86_schedule_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
  1250. {
  1251. int idx;
  1252. idx = fixed_mode_idx(hwc);
  1253. if (idx == X86_PMC_IDX_FIXED_BTS) {
  1254. /* BTS is already occupied. */
  1255. if (test_and_set_bit(idx, cpuc->used_mask))
  1256. return -EAGAIN;
  1257. hwc->config_base = 0;
  1258. hwc->event_base = 0;
  1259. hwc->idx = idx;
  1260. } else if (idx >= 0) {
  1261. /*
  1262. * Try to get the fixed event, if that is already taken
  1263. * then try to get a generic event:
  1264. */
  1265. if (test_and_set_bit(idx, cpuc->used_mask))
  1266. goto try_generic;
  1267. hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
  1268. /*
  1269. * We set it so that event_base + idx in wrmsr/rdmsr maps to
  1270. * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
  1271. */
  1272. hwc->event_base =
  1273. MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
  1274. hwc->idx = idx;
  1275. } else {
  1276. idx = hwc->idx;
  1277. /* Try to get the previous generic event again */
  1278. if (idx == -1 || test_and_set_bit(idx, cpuc->used_mask)) {
  1279. try_generic:
  1280. idx = x86_pmu.get_event_idx(cpuc, hwc);
  1281. if (idx == -1)
  1282. return -EAGAIN;
  1283. set_bit(idx, cpuc->used_mask);
  1284. hwc->idx = idx;
  1285. }
  1286. hwc->config_base = x86_pmu.eventsel;
  1287. hwc->event_base = x86_pmu.perfctr;
  1288. }
  1289. return idx;
  1290. }
  1291. /*
  1292. * Find a PMC slot for the freshly enabled / scheduled in event:
  1293. */
  1294. static int x86_pmu_enable(struct perf_event *event)
  1295. {
  1296. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1297. struct hw_perf_event *hwc = &event->hw;
  1298. int idx;
  1299. idx = x86_schedule_event(cpuc, hwc);
  1300. if (idx < 0)
  1301. return idx;
  1302. perf_events_lapic_init();
  1303. x86_pmu.disable(hwc, idx);
  1304. cpuc->events[idx] = event;
  1305. set_bit(idx, cpuc->active_mask);
  1306. x86_perf_event_set_period(event, hwc, idx);
  1307. x86_pmu.enable(hwc, idx);
  1308. perf_event_update_userpage(event);
  1309. return 0;
  1310. }
  1311. static void x86_pmu_unthrottle(struct perf_event *event)
  1312. {
  1313. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1314. struct hw_perf_event *hwc = &event->hw;
  1315. if (WARN_ON_ONCE(hwc->idx >= X86_PMC_IDX_MAX ||
  1316. cpuc->events[hwc->idx] != event))
  1317. return;
  1318. x86_pmu.enable(hwc, hwc->idx);
  1319. }
  1320. void perf_event_print_debug(void)
  1321. {
  1322. u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
  1323. struct cpu_hw_events *cpuc;
  1324. unsigned long flags;
  1325. int cpu, idx;
  1326. if (!x86_pmu.num_events)
  1327. return;
  1328. local_irq_save(flags);
  1329. cpu = smp_processor_id();
  1330. cpuc = &per_cpu(cpu_hw_events, cpu);
  1331. if (x86_pmu.version >= 2) {
  1332. rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
  1333. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  1334. rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
  1335. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
  1336. pr_info("\n");
  1337. pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
  1338. pr_info("CPU#%d: status: %016llx\n", cpu, status);
  1339. pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
  1340. pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
  1341. }
  1342. pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask);
  1343. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  1344. rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
  1345. rdmsrl(x86_pmu.perfctr + idx, pmc_count);
  1346. prev_left = per_cpu(pmc_prev_left[idx], cpu);
  1347. pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
  1348. cpu, idx, pmc_ctrl);
  1349. pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
  1350. cpu, idx, pmc_count);
  1351. pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
  1352. cpu, idx, prev_left);
  1353. }
  1354. for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) {
  1355. rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
  1356. pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
  1357. cpu, idx, pmc_count);
  1358. }
  1359. local_irq_restore(flags);
  1360. }
  1361. static void intel_pmu_drain_bts_buffer(struct cpu_hw_events *cpuc)
  1362. {
  1363. struct debug_store *ds = cpuc->ds;
  1364. struct bts_record {
  1365. u64 from;
  1366. u64 to;
  1367. u64 flags;
  1368. };
  1369. struct perf_event *event = cpuc->events[X86_PMC_IDX_FIXED_BTS];
  1370. struct bts_record *at, *top;
  1371. struct perf_output_handle handle;
  1372. struct perf_event_header header;
  1373. struct perf_sample_data data;
  1374. struct pt_regs regs;
  1375. if (!event)
  1376. return;
  1377. if (!ds)
  1378. return;
  1379. at = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
  1380. top = (struct bts_record *)(unsigned long)ds->bts_index;
  1381. if (top <= at)
  1382. return;
  1383. ds->bts_index = ds->bts_buffer_base;
  1384. data.period = event->hw.last_period;
  1385. data.addr = 0;
  1386. data.raw = NULL;
  1387. regs.ip = 0;
  1388. /*
  1389. * Prepare a generic sample, i.e. fill in the invariant fields.
  1390. * We will overwrite the from and to address before we output
  1391. * the sample.
  1392. */
  1393. perf_prepare_sample(&header, &data, event, &regs);
  1394. if (perf_output_begin(&handle, event,
  1395. header.size * (top - at), 1, 1))
  1396. return;
  1397. for (; at < top; at++) {
  1398. data.ip = at->from;
  1399. data.addr = at->to;
  1400. perf_output_sample(&handle, &header, &data, event);
  1401. }
  1402. perf_output_end(&handle);
  1403. /* There's new data available. */
  1404. event->hw.interrupts++;
  1405. event->pending_kill = POLL_IN;
  1406. }
  1407. static void x86_pmu_disable(struct perf_event *event)
  1408. {
  1409. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1410. struct hw_perf_event *hwc = &event->hw;
  1411. int idx = hwc->idx;
  1412. /*
  1413. * Must be done before we disable, otherwise the nmi handler
  1414. * could reenable again:
  1415. */
  1416. clear_bit(idx, cpuc->active_mask);
  1417. x86_pmu.disable(hwc, idx);
  1418. /*
  1419. * Make sure the cleared pointer becomes visible before we
  1420. * (potentially) free the event:
  1421. */
  1422. barrier();
  1423. /*
  1424. * Drain the remaining delta count out of a event
  1425. * that we are disabling:
  1426. */
  1427. x86_perf_event_update(event, hwc, idx);
  1428. /* Drain the remaining BTS records. */
  1429. if (unlikely(idx == X86_PMC_IDX_FIXED_BTS))
  1430. intel_pmu_drain_bts_buffer(cpuc);
  1431. cpuc->events[idx] = NULL;
  1432. clear_bit(idx, cpuc->used_mask);
  1433. perf_event_update_userpage(event);
  1434. }
  1435. /*
  1436. * Save and restart an expired event. Called by NMI contexts,
  1437. * so it has to be careful about preempting normal event ops:
  1438. */
  1439. static int intel_pmu_save_and_restart(struct perf_event *event)
  1440. {
  1441. struct hw_perf_event *hwc = &event->hw;
  1442. int idx = hwc->idx;
  1443. int ret;
  1444. x86_perf_event_update(event, hwc, idx);
  1445. ret = x86_perf_event_set_period(event, hwc, idx);
  1446. if (event->state == PERF_EVENT_STATE_ACTIVE)
  1447. intel_pmu_enable_event(hwc, idx);
  1448. return ret;
  1449. }
  1450. static void intel_pmu_reset(void)
  1451. {
  1452. struct debug_store *ds = __get_cpu_var(cpu_hw_events).ds;
  1453. unsigned long flags;
  1454. int idx;
  1455. if (!x86_pmu.num_events)
  1456. return;
  1457. local_irq_save(flags);
  1458. printk("clearing PMU state on CPU#%d\n", smp_processor_id());
  1459. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  1460. checking_wrmsrl(x86_pmu.eventsel + idx, 0ull);
  1461. checking_wrmsrl(x86_pmu.perfctr + idx, 0ull);
  1462. }
  1463. for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) {
  1464. checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
  1465. }
  1466. if (ds)
  1467. ds->bts_index = ds->bts_buffer_base;
  1468. local_irq_restore(flags);
  1469. }
  1470. static int p6_pmu_handle_irq(struct pt_regs *regs)
  1471. {
  1472. struct perf_sample_data data;
  1473. struct cpu_hw_events *cpuc;
  1474. struct perf_event *event;
  1475. struct hw_perf_event *hwc;
  1476. int idx, handled = 0;
  1477. u64 val;
  1478. data.addr = 0;
  1479. data.raw = NULL;
  1480. cpuc = &__get_cpu_var(cpu_hw_events);
  1481. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  1482. if (!test_bit(idx, cpuc->active_mask))
  1483. continue;
  1484. event = cpuc->events[idx];
  1485. hwc = &event->hw;
  1486. val = x86_perf_event_update(event, hwc, idx);
  1487. if (val & (1ULL << (x86_pmu.event_bits - 1)))
  1488. continue;
  1489. /*
  1490. * event overflow
  1491. */
  1492. handled = 1;
  1493. data.period = event->hw.last_period;
  1494. if (!x86_perf_event_set_period(event, hwc, idx))
  1495. continue;
  1496. if (perf_event_overflow(event, 1, &data, regs))
  1497. p6_pmu_disable_event(hwc, idx);
  1498. }
  1499. if (handled)
  1500. inc_irq_stat(apic_perf_irqs);
  1501. return handled;
  1502. }
  1503. /*
  1504. * This handler is triggered by the local APIC, so the APIC IRQ handling
  1505. * rules apply:
  1506. */
  1507. static int intel_pmu_handle_irq(struct pt_regs *regs)
  1508. {
  1509. struct perf_sample_data data;
  1510. struct cpu_hw_events *cpuc;
  1511. int bit, loops;
  1512. u64 ack, status;
  1513. data.addr = 0;
  1514. data.raw = NULL;
  1515. cpuc = &__get_cpu_var(cpu_hw_events);
  1516. perf_disable();
  1517. intel_pmu_drain_bts_buffer(cpuc);
  1518. status = intel_pmu_get_status();
  1519. if (!status) {
  1520. perf_enable();
  1521. return 0;
  1522. }
  1523. loops = 0;
  1524. again:
  1525. if (++loops > 100) {
  1526. WARN_ONCE(1, "perfevents: irq loop stuck!\n");
  1527. perf_event_print_debug();
  1528. intel_pmu_reset();
  1529. perf_enable();
  1530. return 1;
  1531. }
  1532. inc_irq_stat(apic_perf_irqs);
  1533. ack = status;
  1534. for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  1535. struct perf_event *event = cpuc->events[bit];
  1536. clear_bit(bit, (unsigned long *) &status);
  1537. if (!test_bit(bit, cpuc->active_mask))
  1538. continue;
  1539. if (!intel_pmu_save_and_restart(event))
  1540. continue;
  1541. data.period = event->hw.last_period;
  1542. if (perf_event_overflow(event, 1, &data, regs))
  1543. intel_pmu_disable_event(&event->hw, bit);
  1544. }
  1545. intel_pmu_ack_status(ack);
  1546. /*
  1547. * Repeat if there is more work to be done:
  1548. */
  1549. status = intel_pmu_get_status();
  1550. if (status)
  1551. goto again;
  1552. perf_enable();
  1553. return 1;
  1554. }
  1555. static int amd_pmu_handle_irq(struct pt_regs *regs)
  1556. {
  1557. struct perf_sample_data data;
  1558. struct cpu_hw_events *cpuc;
  1559. struct perf_event *event;
  1560. struct hw_perf_event *hwc;
  1561. int idx, handled = 0;
  1562. u64 val;
  1563. data.addr = 0;
  1564. data.raw = NULL;
  1565. cpuc = &__get_cpu_var(cpu_hw_events);
  1566. for (idx = 0; idx < x86_pmu.num_events; idx++) {
  1567. if (!test_bit(idx, cpuc->active_mask))
  1568. continue;
  1569. event = cpuc->events[idx];
  1570. hwc = &event->hw;
  1571. val = x86_perf_event_update(event, hwc, idx);
  1572. if (val & (1ULL << (x86_pmu.event_bits - 1)))
  1573. continue;
  1574. /*
  1575. * event overflow
  1576. */
  1577. handled = 1;
  1578. data.period = event->hw.last_period;
  1579. if (!x86_perf_event_set_period(event, hwc, idx))
  1580. continue;
  1581. if (perf_event_overflow(event, 1, &data, regs))
  1582. amd_pmu_disable_event(hwc, idx);
  1583. }
  1584. if (handled)
  1585. inc_irq_stat(apic_perf_irqs);
  1586. return handled;
  1587. }
  1588. void smp_perf_pending_interrupt(struct pt_regs *regs)
  1589. {
  1590. irq_enter();
  1591. ack_APIC_irq();
  1592. inc_irq_stat(apic_pending_irqs);
  1593. perf_event_do_pending();
  1594. irq_exit();
  1595. }
  1596. void set_perf_event_pending(void)
  1597. {
  1598. #ifdef CONFIG_X86_LOCAL_APIC
  1599. if (!x86_pmu.apic || !x86_pmu_initialized())
  1600. return;
  1601. apic->send_IPI_self(LOCAL_PENDING_VECTOR);
  1602. #endif
  1603. }
  1604. void perf_events_lapic_init(void)
  1605. {
  1606. #ifdef CONFIG_X86_LOCAL_APIC
  1607. if (!x86_pmu.apic || !x86_pmu_initialized())
  1608. return;
  1609. /*
  1610. * Always use NMI for PMU
  1611. */
  1612. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1613. #endif
  1614. }
  1615. static int __kprobes
  1616. perf_event_nmi_handler(struct notifier_block *self,
  1617. unsigned long cmd, void *__args)
  1618. {
  1619. struct die_args *args = __args;
  1620. struct pt_regs *regs;
  1621. if (!atomic_read(&active_events))
  1622. return NOTIFY_DONE;
  1623. switch (cmd) {
  1624. case DIE_NMI:
  1625. case DIE_NMI_IPI:
  1626. break;
  1627. default:
  1628. return NOTIFY_DONE;
  1629. }
  1630. regs = args->regs;
  1631. #ifdef CONFIG_X86_LOCAL_APIC
  1632. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1633. #endif
  1634. /*
  1635. * Can't rely on the handled return value to say it was our NMI, two
  1636. * events could trigger 'simultaneously' raising two back-to-back NMIs.
  1637. *
  1638. * If the first NMI handles both, the latter will be empty and daze
  1639. * the CPU.
  1640. */
  1641. x86_pmu.handle_irq(regs);
  1642. return NOTIFY_STOP;
  1643. }
  1644. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1645. .notifier_call = perf_event_nmi_handler,
  1646. .next = NULL,
  1647. .priority = 1
  1648. };
  1649. static __initconst struct x86_pmu p6_pmu = {
  1650. .name = "p6",
  1651. .handle_irq = p6_pmu_handle_irq,
  1652. .disable_all = p6_pmu_disable_all,
  1653. .enable_all = p6_pmu_enable_all,
  1654. .enable = p6_pmu_enable_event,
  1655. .disable = p6_pmu_disable_event,
  1656. .eventsel = MSR_P6_EVNTSEL0,
  1657. .perfctr = MSR_P6_PERFCTR0,
  1658. .event_map = p6_pmu_event_map,
  1659. .raw_event = p6_pmu_raw_event,
  1660. .max_events = ARRAY_SIZE(p6_perfmon_event_map),
  1661. .apic = 1,
  1662. .max_period = (1ULL << 31) - 1,
  1663. .version = 0,
  1664. .num_events = 2,
  1665. /*
  1666. * Events have 40 bits implemented. However they are designed such
  1667. * that bits [32-39] are sign extensions of bit 31. As such the
  1668. * effective width of a event for P6-like PMU is 32 bits only.
  1669. *
  1670. * See IA-32 Intel Architecture Software developer manual Vol 3B
  1671. */
  1672. .event_bits = 32,
  1673. .event_mask = (1ULL << 32) - 1,
  1674. .get_event_idx = intel_get_event_idx,
  1675. };
  1676. static __initconst struct x86_pmu intel_pmu = {
  1677. .name = "Intel",
  1678. .handle_irq = intel_pmu_handle_irq,
  1679. .disable_all = intel_pmu_disable_all,
  1680. .enable_all = intel_pmu_enable_all,
  1681. .enable = intel_pmu_enable_event,
  1682. .disable = intel_pmu_disable_event,
  1683. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  1684. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  1685. .event_map = intel_pmu_event_map,
  1686. .raw_event = intel_pmu_raw_event,
  1687. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  1688. .apic = 1,
  1689. /*
  1690. * Intel PMCs cannot be accessed sanely above 32 bit width,
  1691. * so we install an artificial 1<<31 period regardless of
  1692. * the generic event period:
  1693. */
  1694. .max_period = (1ULL << 31) - 1,
  1695. .enable_bts = intel_pmu_enable_bts,
  1696. .disable_bts = intel_pmu_disable_bts,
  1697. .get_event_idx = intel_get_event_idx,
  1698. };
  1699. static __initconst struct x86_pmu amd_pmu = {
  1700. .name = "AMD",
  1701. .handle_irq = amd_pmu_handle_irq,
  1702. .disable_all = amd_pmu_disable_all,
  1703. .enable_all = amd_pmu_enable_all,
  1704. .enable = amd_pmu_enable_event,
  1705. .disable = amd_pmu_disable_event,
  1706. .eventsel = MSR_K7_EVNTSEL0,
  1707. .perfctr = MSR_K7_PERFCTR0,
  1708. .event_map = amd_pmu_event_map,
  1709. .raw_event = amd_pmu_raw_event,
  1710. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  1711. .num_events = 4,
  1712. .event_bits = 48,
  1713. .event_mask = (1ULL << 48) - 1,
  1714. .apic = 1,
  1715. /* use highest bit to detect overflow */
  1716. .max_period = (1ULL << 47) - 1,
  1717. .get_event_idx = gen_get_event_idx,
  1718. };
  1719. static __init int p6_pmu_init(void)
  1720. {
  1721. switch (boot_cpu_data.x86_model) {
  1722. case 1:
  1723. case 3: /* Pentium Pro */
  1724. case 5:
  1725. case 6: /* Pentium II */
  1726. case 7:
  1727. case 8:
  1728. case 11: /* Pentium III */
  1729. event_constraints = intel_p6_event_constraints;
  1730. break;
  1731. case 9:
  1732. case 13:
  1733. /* Pentium M */
  1734. event_constraints = intel_p6_event_constraints;
  1735. break;
  1736. default:
  1737. pr_cont("unsupported p6 CPU model %d ",
  1738. boot_cpu_data.x86_model);
  1739. return -ENODEV;
  1740. }
  1741. x86_pmu = p6_pmu;
  1742. return 0;
  1743. }
  1744. static __init int intel_pmu_init(void)
  1745. {
  1746. union cpuid10_edx edx;
  1747. union cpuid10_eax eax;
  1748. unsigned int unused;
  1749. unsigned int ebx;
  1750. int version;
  1751. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  1752. /* check for P6 processor family */
  1753. if (boot_cpu_data.x86 == 6) {
  1754. return p6_pmu_init();
  1755. } else {
  1756. return -ENODEV;
  1757. }
  1758. }
  1759. /*
  1760. * Check whether the Architectural PerfMon supports
  1761. * Branch Misses Retired hw_event or not.
  1762. */
  1763. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  1764. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  1765. return -ENODEV;
  1766. version = eax.split.version_id;
  1767. if (version < 2)
  1768. return -ENODEV;
  1769. x86_pmu = intel_pmu;
  1770. x86_pmu.version = version;
  1771. x86_pmu.num_events = eax.split.num_events;
  1772. x86_pmu.event_bits = eax.split.bit_width;
  1773. x86_pmu.event_mask = (1ULL << eax.split.bit_width) - 1;
  1774. /*
  1775. * Quirk: v2 perfmon does not report fixed-purpose events, so
  1776. * assume at least 3 events:
  1777. */
  1778. x86_pmu.num_events_fixed = max((int)edx.split.num_events_fixed, 3);
  1779. /*
  1780. * Install the hw-cache-events table:
  1781. */
  1782. switch (boot_cpu_data.x86_model) {
  1783. case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */
  1784. case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */
  1785. case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */
  1786. case 29: /* six-core 45 nm xeon "Dunnington" */
  1787. memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
  1788. sizeof(hw_cache_event_ids));
  1789. pr_cont("Core2 events, ");
  1790. event_constraints = intel_core_event_constraints;
  1791. break;
  1792. default:
  1793. case 26:
  1794. memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
  1795. sizeof(hw_cache_event_ids));
  1796. event_constraints = intel_nehalem_event_constraints;
  1797. pr_cont("Nehalem/Corei7 events, ");
  1798. break;
  1799. case 28:
  1800. memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
  1801. sizeof(hw_cache_event_ids));
  1802. pr_cont("Atom events, ");
  1803. break;
  1804. }
  1805. return 0;
  1806. }
  1807. static __init int amd_pmu_init(void)
  1808. {
  1809. /* Performance-monitoring supported from K7 and later: */
  1810. if (boot_cpu_data.x86 < 6)
  1811. return -ENODEV;
  1812. x86_pmu = amd_pmu;
  1813. /* Events are common for all AMDs */
  1814. memcpy(hw_cache_event_ids, amd_hw_cache_event_ids,
  1815. sizeof(hw_cache_event_ids));
  1816. return 0;
  1817. }
  1818. static void __init pmu_check_apic(void)
  1819. {
  1820. if (cpu_has_apic)
  1821. return;
  1822. x86_pmu.apic = 0;
  1823. pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
  1824. pr_info("no hardware sampling interrupt available.\n");
  1825. }
  1826. void __init init_hw_perf_events(void)
  1827. {
  1828. int err;
  1829. pr_info("Performance Events: ");
  1830. switch (boot_cpu_data.x86_vendor) {
  1831. case X86_VENDOR_INTEL:
  1832. err = intel_pmu_init();
  1833. break;
  1834. case X86_VENDOR_AMD:
  1835. err = amd_pmu_init();
  1836. break;
  1837. default:
  1838. return;
  1839. }
  1840. if (err != 0) {
  1841. pr_cont("no PMU driver, software events only.\n");
  1842. return;
  1843. }
  1844. pmu_check_apic();
  1845. pr_cont("%s PMU driver.\n", x86_pmu.name);
  1846. if (x86_pmu.num_events > X86_PMC_MAX_GENERIC) {
  1847. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  1848. x86_pmu.num_events, X86_PMC_MAX_GENERIC);
  1849. x86_pmu.num_events = X86_PMC_MAX_GENERIC;
  1850. }
  1851. perf_event_mask = (1 << x86_pmu.num_events) - 1;
  1852. perf_max_events = x86_pmu.num_events;
  1853. if (x86_pmu.num_events_fixed > X86_PMC_MAX_FIXED) {
  1854. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  1855. x86_pmu.num_events_fixed, X86_PMC_MAX_FIXED);
  1856. x86_pmu.num_events_fixed = X86_PMC_MAX_FIXED;
  1857. }
  1858. perf_event_mask |=
  1859. ((1LL << x86_pmu.num_events_fixed)-1) << X86_PMC_IDX_FIXED;
  1860. x86_pmu.intel_ctrl = perf_event_mask;
  1861. perf_events_lapic_init();
  1862. register_die_notifier(&perf_event_nmi_notifier);
  1863. pr_info("... version: %d\n", x86_pmu.version);
  1864. pr_info("... bit width: %d\n", x86_pmu.event_bits);
  1865. pr_info("... generic registers: %d\n", x86_pmu.num_events);
  1866. pr_info("... value mask: %016Lx\n", x86_pmu.event_mask);
  1867. pr_info("... max period: %016Lx\n", x86_pmu.max_period);
  1868. pr_info("... fixed-purpose events: %d\n", x86_pmu.num_events_fixed);
  1869. pr_info("... event mask: %016Lx\n", perf_event_mask);
  1870. }
  1871. static inline void x86_pmu_read(struct perf_event *event)
  1872. {
  1873. x86_perf_event_update(event, &event->hw, event->hw.idx);
  1874. }
  1875. static const struct pmu pmu = {
  1876. .enable = x86_pmu_enable,
  1877. .disable = x86_pmu_disable,
  1878. .read = x86_pmu_read,
  1879. .unthrottle = x86_pmu_unthrottle,
  1880. };
  1881. static int
  1882. validate_event(struct cpu_hw_events *cpuc, struct perf_event *event)
  1883. {
  1884. struct hw_perf_event fake_event = event->hw;
  1885. if (event->pmu && event->pmu != &pmu)
  1886. return 0;
  1887. return x86_schedule_event(cpuc, &fake_event) >= 0;
  1888. }
  1889. static int validate_group(struct perf_event *event)
  1890. {
  1891. struct perf_event *sibling, *leader = event->group_leader;
  1892. struct cpu_hw_events fake_pmu;
  1893. memset(&fake_pmu, 0, sizeof(fake_pmu));
  1894. if (!validate_event(&fake_pmu, leader))
  1895. return -ENOSPC;
  1896. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  1897. if (!validate_event(&fake_pmu, sibling))
  1898. return -ENOSPC;
  1899. }
  1900. if (!validate_event(&fake_pmu, event))
  1901. return -ENOSPC;
  1902. return 0;
  1903. }
  1904. const struct pmu *hw_perf_event_init(struct perf_event *event)
  1905. {
  1906. int err;
  1907. err = __hw_perf_event_init(event);
  1908. if (!err) {
  1909. if (event->group_leader != event)
  1910. err = validate_group(event);
  1911. }
  1912. if (err) {
  1913. if (event->destroy)
  1914. event->destroy(event);
  1915. return ERR_PTR(err);
  1916. }
  1917. return &pmu;
  1918. }
  1919. /*
  1920. * callchain support
  1921. */
  1922. static inline
  1923. void callchain_store(struct perf_callchain_entry *entry, u64 ip)
  1924. {
  1925. if (entry->nr < PERF_MAX_STACK_DEPTH)
  1926. entry->ip[entry->nr++] = ip;
  1927. }
  1928. static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry);
  1929. static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_nmi_entry);
  1930. static DEFINE_PER_CPU(int, in_ignored_frame);
  1931. static void
  1932. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  1933. {
  1934. /* Ignore warnings */
  1935. }
  1936. static void backtrace_warning(void *data, char *msg)
  1937. {
  1938. /* Ignore warnings */
  1939. }
  1940. static int backtrace_stack(void *data, char *name)
  1941. {
  1942. per_cpu(in_ignored_frame, smp_processor_id()) =
  1943. x86_is_stack_id(NMI_STACK, name) ||
  1944. x86_is_stack_id(DEBUG_STACK, name);
  1945. return 0;
  1946. }
  1947. static void backtrace_address(void *data, unsigned long addr, int reliable)
  1948. {
  1949. struct perf_callchain_entry *entry = data;
  1950. if (per_cpu(in_ignored_frame, smp_processor_id()))
  1951. return;
  1952. if (reliable)
  1953. callchain_store(entry, addr);
  1954. }
  1955. static const struct stacktrace_ops backtrace_ops = {
  1956. .warning = backtrace_warning,
  1957. .warning_symbol = backtrace_warning_symbol,
  1958. .stack = backtrace_stack,
  1959. .address = backtrace_address,
  1960. .walk_stack = print_context_stack_bp,
  1961. };
  1962. #include "../dumpstack.h"
  1963. static void
  1964. perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
  1965. {
  1966. callchain_store(entry, PERF_CONTEXT_KERNEL);
  1967. callchain_store(entry, regs->ip);
  1968. dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry);
  1969. }
  1970. /*
  1971. * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
  1972. */
  1973. static unsigned long
  1974. copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
  1975. {
  1976. unsigned long offset, addr = (unsigned long)from;
  1977. int type = in_nmi() ? KM_NMI : KM_IRQ0;
  1978. unsigned long size, len = 0;
  1979. struct page *page;
  1980. void *map;
  1981. int ret;
  1982. do {
  1983. ret = __get_user_pages_fast(addr, 1, 0, &page);
  1984. if (!ret)
  1985. break;
  1986. offset = addr & (PAGE_SIZE - 1);
  1987. size = min(PAGE_SIZE - offset, n - len);
  1988. map = kmap_atomic(page, type);
  1989. memcpy(to, map+offset, size);
  1990. kunmap_atomic(map, type);
  1991. put_page(page);
  1992. len += size;
  1993. to += size;
  1994. addr += size;
  1995. } while (len < n);
  1996. return len;
  1997. }
  1998. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  1999. {
  2000. unsigned long bytes;
  2001. bytes = copy_from_user_nmi(frame, fp, sizeof(*frame));
  2002. return bytes == sizeof(*frame);
  2003. }
  2004. static void
  2005. perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
  2006. {
  2007. struct stack_frame frame;
  2008. const void __user *fp;
  2009. if (!user_mode(regs))
  2010. regs = task_pt_regs(current);
  2011. fp = (void __user *)regs->bp;
  2012. callchain_store(entry, PERF_CONTEXT_USER);
  2013. callchain_store(entry, regs->ip);
  2014. while (entry->nr < PERF_MAX_STACK_DEPTH) {
  2015. frame.next_frame = NULL;
  2016. frame.return_address = 0;
  2017. if (!copy_stack_frame(fp, &frame))
  2018. break;
  2019. if ((unsigned long)fp < regs->sp)
  2020. break;
  2021. callchain_store(entry, frame.return_address);
  2022. fp = frame.next_frame;
  2023. }
  2024. }
  2025. static void
  2026. perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
  2027. {
  2028. int is_user;
  2029. if (!regs)
  2030. return;
  2031. is_user = user_mode(regs);
  2032. if (!current || current->pid == 0)
  2033. return;
  2034. if (is_user && current->state != TASK_RUNNING)
  2035. return;
  2036. if (!is_user)
  2037. perf_callchain_kernel(regs, entry);
  2038. if (current->mm)
  2039. perf_callchain_user(regs, entry);
  2040. }
  2041. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  2042. {
  2043. struct perf_callchain_entry *entry;
  2044. if (in_nmi())
  2045. entry = &__get_cpu_var(pmc_nmi_entry);
  2046. else
  2047. entry = &__get_cpu_var(pmc_irq_entry);
  2048. entry->nr = 0;
  2049. perf_do_callchain(regs, entry);
  2050. return entry;
  2051. }
  2052. void hw_perf_event_setup_online(int cpu)
  2053. {
  2054. init_debug_store_on_cpu(cpu);
  2055. }