blob: 376fcbc3f8da879e5803e1001a219522c53ab075 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file implements the perfmon-2 subsystem which is used
3 * to program the IA-64 Performance Monitoring Unit (PMU).
4 *
5 * The initial version of perfmon.c was written by
6 * Ganesh Venkitachalam, IBM Corp.
7 *
8 * Then it was modified for perfmon-1.x by Stephane Eranian and
9 * David Mosberger, Hewlett Packard Co.
10 *
11 * Version Perfmon-2.x is a rewrite of perfmon-1.x
12 * by Stephane Eranian, Hewlett Packard Co.
13 *
14 * Copyright (C) 1999-2003, 2005 Hewlett Packard Co
15 * Stephane Eranian <eranian@hpl.hp.com>
16 * David Mosberger-Tang <davidm@hpl.hp.com>
17 *
18 * More information about perfmon available at:
19 * http://www.hpl.hp.com/research/linux/perfmon
20 */
21
22#include <linux/config.h>
23#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/sched.h>
26#include <linux/interrupt.h>
27#include <linux/smp_lock.h>
28#include <linux/proc_fs.h>
29#include <linux/seq_file.h>
30#include <linux/init.h>
31#include <linux/vmalloc.h>
32#include <linux/mm.h>
33#include <linux/sysctl.h>
34#include <linux/list.h>
35#include <linux/file.h>
36#include <linux/poll.h>
37#include <linux/vfs.h>
38#include <linux/pagemap.h>
39#include <linux/mount.h>
40#include <linux/version.h>
41#include <linux/bitops.h>
42
43#include <asm/errno.h>
44#include <asm/intrinsics.h>
45#include <asm/page.h>
46#include <asm/perfmon.h>
47#include <asm/processor.h>
48#include <asm/signal.h>
49#include <asm/system.h>
50#include <asm/uaccess.h>
51#include <asm/delay.h>
52
53#ifdef CONFIG_PERFMON
54/*
55 * perfmon context state
56 */
57#define PFM_CTX_UNLOADED 1 /* context is not loaded onto any task */
58#define PFM_CTX_LOADED 2 /* context is loaded onto a task */
59#define PFM_CTX_MASKED 3 /* context is loaded but monitoring is masked due to overflow */
60#define PFM_CTX_ZOMBIE 4 /* owner of the context is closing it */
61
62#define PFM_INVALID_ACTIVATION (~0UL)
63
64/*
65 * depth of message queue
66 */
67#define PFM_MAX_MSGS 32
68#define PFM_CTXQ_EMPTY(g) ((g)->ctx_msgq_head == (g)->ctx_msgq_tail)
69
70/*
71 * type of a PMU register (bitmask).
72 * bitmask structure:
73 * bit0 : register implemented
74 * bit1 : end marker
75 * bit2-3 : reserved
76 * bit4 : pmc has pmc.pm
77 * bit5 : pmc controls a counter (has pmc.oi), pmd is used as counter
78 * bit6-7 : register type
79 * bit8-31: reserved
80 */
81#define PFM_REG_NOTIMPL 0x0 /* not implemented at all */
82#define PFM_REG_IMPL 0x1 /* register implemented */
83#define PFM_REG_END 0x2 /* end marker */
84#define PFM_REG_MONITOR (0x1<<4|PFM_REG_IMPL) /* a PMC with a pmc.pm field only */
85#define PFM_REG_COUNTING (0x2<<4|PFM_REG_MONITOR) /* a monitor + pmc.oi+ PMD used as a counter */
86#define PFM_REG_CONTROL (0x4<<4|PFM_REG_IMPL) /* PMU control register */
87#define PFM_REG_CONFIG (0x8<<4|PFM_REG_IMPL) /* configuration register */
88#define PFM_REG_BUFFER (0xc<<4|PFM_REG_IMPL) /* PMD used as buffer */
89
90#define PMC_IS_LAST(i) (pmu_conf->pmc_desc[i].type & PFM_REG_END)
91#define PMD_IS_LAST(i) (pmu_conf->pmd_desc[i].type & PFM_REG_END)
92
93#define PMC_OVFL_NOTIFY(ctx, i) ((ctx)->ctx_pmds[i].flags & PFM_REGFL_OVFL_NOTIFY)
94
95/* i assumed unsigned */
96#define PMC_IS_IMPL(i) (i< PMU_MAX_PMCS && (pmu_conf->pmc_desc[i].type & PFM_REG_IMPL))
97#define PMD_IS_IMPL(i) (i< PMU_MAX_PMDS && (pmu_conf->pmd_desc[i].type & PFM_REG_IMPL))
98
99/* XXX: these assume that register i is implemented */
100#define PMD_IS_COUNTING(i) ((pmu_conf->pmd_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
101#define PMC_IS_COUNTING(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
102#define PMC_IS_MONITOR(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_MONITOR) == PFM_REG_MONITOR)
103#define PMC_IS_CONTROL(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_CONTROL) == PFM_REG_CONTROL)
104
105#define PMC_DFL_VAL(i) pmu_conf->pmc_desc[i].default_value
106#define PMC_RSVD_MASK(i) pmu_conf->pmc_desc[i].reserved_mask
107#define PMD_PMD_DEP(i) pmu_conf->pmd_desc[i].dep_pmd[0]
108#define PMC_PMD_DEP(i) pmu_conf->pmc_desc[i].dep_pmd[0]
109
110#define PFM_NUM_IBRS IA64_NUM_DBG_REGS
111#define PFM_NUM_DBRS IA64_NUM_DBG_REGS
112
113#define CTX_OVFL_NOBLOCK(c) ((c)->ctx_fl_block == 0)
114#define CTX_HAS_SMPL(c) ((c)->ctx_fl_is_sampling)
115#define PFM_CTX_TASK(h) (h)->ctx_task
116
117#define PMU_PMC_OI 5 /* position of pmc.oi bit */
118
119/* XXX: does not support more than 64 PMDs */
120#define CTX_USED_PMD(ctx, mask) (ctx)->ctx_used_pmds[0] |= (mask)
121#define CTX_IS_USED_PMD(ctx, c) (((ctx)->ctx_used_pmds[0] & (1UL << (c))) != 0UL)
122
123#define CTX_USED_MONITOR(ctx, mask) (ctx)->ctx_used_monitors[0] |= (mask)
124
125#define CTX_USED_IBR(ctx,n) (ctx)->ctx_used_ibrs[(n)>>6] |= 1UL<< ((n) % 64)
126#define CTX_USED_DBR(ctx,n) (ctx)->ctx_used_dbrs[(n)>>6] |= 1UL<< ((n) % 64)
127#define CTX_USES_DBREGS(ctx) (((pfm_context_t *)(ctx))->ctx_fl_using_dbreg==1)
128#define PFM_CODE_RR 0 /* requesting code range restriction */
129#define PFM_DATA_RR 1 /* requestion data range restriction */
130
131#define PFM_CPUINFO_CLEAR(v) pfm_get_cpu_var(pfm_syst_info) &= ~(v)
132#define PFM_CPUINFO_SET(v) pfm_get_cpu_var(pfm_syst_info) |= (v)
133#define PFM_CPUINFO_GET() pfm_get_cpu_var(pfm_syst_info)
134
135#define RDEP(x) (1UL<<(x))
136
137/*
138 * context protection macros
139 * in SMP:
140 * - we need to protect against CPU concurrency (spin_lock)
141 * - we need to protect against PMU overflow interrupts (local_irq_disable)
142 * in UP:
143 * - we need to protect against PMU overflow interrupts (local_irq_disable)
144 *
145 * spin_lock_irqsave()/spin_lock_irqrestore():
146 * in SMP: local_irq_disable + spin_lock
147 * in UP : local_irq_disable
148 *
149 * spin_lock()/spin_lock():
150 * in UP : removed automatically
151 * in SMP: protect against context accesses from other CPU. interrupts
152 * are not masked. This is useful for the PMU interrupt handler
153 * because we know we will not get PMU concurrency in that code.
154 */
155#define PROTECT_CTX(c, f) \
156 do { \
157 DPRINT(("spinlock_irq_save ctx %p by [%d]\n", c, current->pid)); \
158 spin_lock_irqsave(&(c)->ctx_lock, f); \
159 DPRINT(("spinlocked ctx %p by [%d]\n", c, current->pid)); \
160 } while(0)
161
162#define UNPROTECT_CTX(c, f) \
163 do { \
164 DPRINT(("spinlock_irq_restore ctx %p by [%d]\n", c, current->pid)); \
165 spin_unlock_irqrestore(&(c)->ctx_lock, f); \
166 } while(0)
167
168#define PROTECT_CTX_NOPRINT(c, f) \
169 do { \
170 spin_lock_irqsave(&(c)->ctx_lock, f); \
171 } while(0)
172
173
174#define UNPROTECT_CTX_NOPRINT(c, f) \
175 do { \
176 spin_unlock_irqrestore(&(c)->ctx_lock, f); \
177 } while(0)
178
179
180#define PROTECT_CTX_NOIRQ(c) \
181 do { \
182 spin_lock(&(c)->ctx_lock); \
183 } while(0)
184
185#define UNPROTECT_CTX_NOIRQ(c) \
186 do { \
187 spin_unlock(&(c)->ctx_lock); \
188 } while(0)
189
190
191#ifdef CONFIG_SMP
192
193#define GET_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)
194#define INC_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)++
195#define SET_ACTIVATION(c) (c)->ctx_last_activation = GET_ACTIVATION()
196
197#else /* !CONFIG_SMP */
198#define SET_ACTIVATION(t) do {} while(0)
199#define GET_ACTIVATION(t) do {} while(0)
200#define INC_ACTIVATION(t) do {} while(0)
201#endif /* CONFIG_SMP */
202
203#define SET_PMU_OWNER(t, c) do { pfm_get_cpu_var(pmu_owner) = (t); pfm_get_cpu_var(pmu_ctx) = (c); } while(0)
204#define GET_PMU_OWNER() pfm_get_cpu_var(pmu_owner)
205#define GET_PMU_CTX() pfm_get_cpu_var(pmu_ctx)
206
207#define LOCK_PFS(g) spin_lock_irqsave(&pfm_sessions.pfs_lock, g)
208#define UNLOCK_PFS(g) spin_unlock_irqrestore(&pfm_sessions.pfs_lock, g)
209
210#define PFM_REG_RETFLAG_SET(flags, val) do { flags &= ~PFM_REG_RETFL_MASK; flags |= (val); } while(0)
211
212/*
213 * cmp0 must be the value of pmc0
214 */
215#define PMC0_HAS_OVFL(cmp0) (cmp0 & ~0x1UL)
216
217#define PFMFS_MAGIC 0xa0b4d889
218
219/*
220 * debugging
221 */
222#define PFM_DEBUGGING 1
223#ifdef PFM_DEBUGGING
224#define DPRINT(a) \
225 do { \
226 if (unlikely(pfm_sysctl.debug >0)) { printk("%s.%d: CPU%d [%d] ", __FUNCTION__, __LINE__, smp_processor_id(), current->pid); printk a; } \
227 } while (0)
228
229#define DPRINT_ovfl(a) \
230 do { \
231 if (unlikely(pfm_sysctl.debug > 0 && pfm_sysctl.debug_ovfl >0)) { printk("%s.%d: CPU%d [%d] ", __FUNCTION__, __LINE__, smp_processor_id(), current->pid); printk a; } \
232 } while (0)
233#endif
234
235/*
236 * 64-bit software counter structure
237 *
238 * the next_reset_type is applied to the next call to pfm_reset_regs()
239 */
240typedef struct {
241 unsigned long val; /* virtual 64bit counter value */
242 unsigned long lval; /* last reset value */
243 unsigned long long_reset; /* reset value on sampling overflow */
244 unsigned long short_reset; /* reset value on overflow */
245 unsigned long reset_pmds[4]; /* which other pmds to reset when this counter overflows */
246 unsigned long smpl_pmds[4]; /* which pmds are accessed when counter overflow */
247 unsigned long seed; /* seed for random-number generator */
248 unsigned long mask; /* mask for random-number generator */
249 unsigned int flags; /* notify/do not notify */
250 unsigned long eventid; /* overflow event identifier */
251} pfm_counter_t;
252
253/*
254 * context flags
255 */
256typedef struct {
257 unsigned int block:1; /* when 1, task will blocked on user notifications */
258 unsigned int system:1; /* do system wide monitoring */
259 unsigned int using_dbreg:1; /* using range restrictions (debug registers) */
260 unsigned int is_sampling:1; /* true if using a custom format */
261 unsigned int excl_idle:1; /* exclude idle task in system wide session */
262 unsigned int going_zombie:1; /* context is zombie (MASKED+blocking) */
263 unsigned int trap_reason:2; /* reason for going into pfm_handle_work() */
264 unsigned int no_msg:1; /* no message sent on overflow */
265 unsigned int can_restart:1; /* allowed to issue a PFM_RESTART */
266 unsigned int reserved:22;
267} pfm_context_flags_t;
268
269#define PFM_TRAP_REASON_NONE 0x0 /* default value */
270#define PFM_TRAP_REASON_BLOCK 0x1 /* we need to block on overflow */
271#define PFM_TRAP_REASON_RESET 0x2 /* we need to reset PMDs */
272
273
274/*
275 * perfmon context: encapsulates all the state of a monitoring session
276 */
277
278typedef struct pfm_context {
279 spinlock_t ctx_lock; /* context protection */
280
281 pfm_context_flags_t ctx_flags; /* bitmask of flags (block reason incl.) */
282 unsigned int ctx_state; /* state: active/inactive (no bitfield) */
283
284 struct task_struct *ctx_task; /* task to which context is attached */
285
286 unsigned long ctx_ovfl_regs[4]; /* which registers overflowed (notification) */
287
288 struct semaphore ctx_restart_sem; /* use for blocking notification mode */
289
290 unsigned long ctx_used_pmds[4]; /* bitmask of PMD used */
291 unsigned long ctx_all_pmds[4]; /* bitmask of all accessible PMDs */
292 unsigned long ctx_reload_pmds[4]; /* bitmask of force reload PMD on ctxsw in */
293
294 unsigned long ctx_all_pmcs[4]; /* bitmask of all accessible PMCs */
295 unsigned long ctx_reload_pmcs[4]; /* bitmask of force reload PMC on ctxsw in */
296 unsigned long ctx_used_monitors[4]; /* bitmask of monitor PMC being used */
297
298 unsigned long ctx_pmcs[IA64_NUM_PMC_REGS]; /* saved copies of PMC values */
299
300 unsigned int ctx_used_ibrs[1]; /* bitmask of used IBR (speedup ctxsw in) */
301 unsigned int ctx_used_dbrs[1]; /* bitmask of used DBR (speedup ctxsw in) */
302 unsigned long ctx_dbrs[IA64_NUM_DBG_REGS]; /* DBR values (cache) when not loaded */
303 unsigned long ctx_ibrs[IA64_NUM_DBG_REGS]; /* IBR values (cache) when not loaded */
304
305 pfm_counter_t ctx_pmds[IA64_NUM_PMD_REGS]; /* software state for PMDS */
306
307 u64 ctx_saved_psr_up; /* only contains psr.up value */
308
309 unsigned long ctx_last_activation; /* context last activation number for last_cpu */
310 unsigned int ctx_last_cpu; /* CPU id of current or last CPU used (SMP only) */
311 unsigned int ctx_cpu; /* cpu to which perfmon is applied (system wide) */
312
313 int ctx_fd; /* file descriptor used my this context */
314 pfm_ovfl_arg_t ctx_ovfl_arg; /* argument to custom buffer format handler */
315
316 pfm_buffer_fmt_t *ctx_buf_fmt; /* buffer format callbacks */
317 void *ctx_smpl_hdr; /* points to sampling buffer header kernel vaddr */
318 unsigned long ctx_smpl_size; /* size of sampling buffer */
319 void *ctx_smpl_vaddr; /* user level virtual address of smpl buffer */
320
321 wait_queue_head_t ctx_msgq_wait;
322 pfm_msg_t ctx_msgq[PFM_MAX_MSGS];
323 int ctx_msgq_head;
324 int ctx_msgq_tail;
325 struct fasync_struct *ctx_async_queue;
326
327 wait_queue_head_t ctx_zombieq; /* termination cleanup wait queue */
328} pfm_context_t;
329
330/*
331 * magic number used to verify that structure is really
332 * a perfmon context
333 */
334#define PFM_IS_FILE(f) ((f)->f_op == &pfm_file_ops)
335
336#define PFM_GET_CTX(t) ((pfm_context_t *)(t)->thread.pfm_context)
337
338#ifdef CONFIG_SMP
339#define SET_LAST_CPU(ctx, v) (ctx)->ctx_last_cpu = (v)
340#define GET_LAST_CPU(ctx) (ctx)->ctx_last_cpu
341#else
342#define SET_LAST_CPU(ctx, v) do {} while(0)
343#define GET_LAST_CPU(ctx) do {} while(0)
344#endif
345
346
347#define ctx_fl_block ctx_flags.block
348#define ctx_fl_system ctx_flags.system
349#define ctx_fl_using_dbreg ctx_flags.using_dbreg
350#define ctx_fl_is_sampling ctx_flags.is_sampling
351#define ctx_fl_excl_idle ctx_flags.excl_idle
352#define ctx_fl_going_zombie ctx_flags.going_zombie
353#define ctx_fl_trap_reason ctx_flags.trap_reason
354#define ctx_fl_no_msg ctx_flags.no_msg
355#define ctx_fl_can_restart ctx_flags.can_restart
356
357#define PFM_SET_WORK_PENDING(t, v) do { (t)->thread.pfm_needs_checking = v; } while(0);
358#define PFM_GET_WORK_PENDING(t) (t)->thread.pfm_needs_checking
359
360/*
361 * global information about all sessions
362 * mostly used to synchronize between system wide and per-process
363 */
364typedef struct {
365 spinlock_t pfs_lock; /* lock the structure */
366
367 unsigned int pfs_task_sessions; /* number of per task sessions */
368 unsigned int pfs_sys_sessions; /* number of per system wide sessions */
369 unsigned int pfs_sys_use_dbregs; /* incremented when a system wide session uses debug regs */
370 unsigned int pfs_ptrace_use_dbregs; /* incremented when a process uses debug regs */
371 struct task_struct *pfs_sys_session[NR_CPUS]; /* point to task owning a system-wide session */
372} pfm_session_t;
373
374/*
375 * information about a PMC or PMD.
376 * dep_pmd[]: a bitmask of dependent PMD registers
377 * dep_pmc[]: a bitmask of dependent PMC registers
378 */
379typedef int (*pfm_reg_check_t)(struct task_struct *task, pfm_context_t *ctx, unsigned int cnum, unsigned long *val, struct pt_regs *regs);
380typedef struct {
381 unsigned int type;
382 int pm_pos;
383 unsigned long default_value; /* power-on default value */
384 unsigned long reserved_mask; /* bitmask of reserved bits */
385 pfm_reg_check_t read_check;
386 pfm_reg_check_t write_check;
387 unsigned long dep_pmd[4];
388 unsigned long dep_pmc[4];
389} pfm_reg_desc_t;
390
391/* assume cnum is a valid monitor */
392#define PMC_PM(cnum, val) (((val) >> (pmu_conf->pmc_desc[cnum].pm_pos)) & 0x1)
393
394/*
395 * This structure is initialized at boot time and contains
396 * a description of the PMU main characteristics.
397 *
398 * If the probe function is defined, detection is based
399 * on its return value:
400 * - 0 means recognized PMU
401 * - anything else means not supported
402 * When the probe function is not defined, then the pmu_family field
403 * is used and it must match the host CPU family such that:
404 * - cpu->family & config->pmu_family != 0
405 */
406typedef struct {
407 unsigned long ovfl_val; /* overflow value for counters */
408
409 pfm_reg_desc_t *pmc_desc; /* detailed PMC register dependencies descriptions */
410 pfm_reg_desc_t *pmd_desc; /* detailed PMD register dependencies descriptions */
411
412 unsigned int num_pmcs; /* number of PMCS: computed at init time */
413 unsigned int num_pmds; /* number of PMDS: computed at init time */
414 unsigned long impl_pmcs[4]; /* bitmask of implemented PMCS */
415 unsigned long impl_pmds[4]; /* bitmask of implemented PMDS */
416
417 char *pmu_name; /* PMU family name */
418 unsigned int pmu_family; /* cpuid family pattern used to identify pmu */
419 unsigned int flags; /* pmu specific flags */
420 unsigned int num_ibrs; /* number of IBRS: computed at init time */
421 unsigned int num_dbrs; /* number of DBRS: computed at init time */
422 unsigned int num_counters; /* PMC/PMD counting pairs : computed at init time */
423 int (*probe)(void); /* customized probe routine */
424 unsigned int use_rr_dbregs:1; /* set if debug registers used for range restriction */
425} pmu_config_t;
426/*
427 * PMU specific flags
428 */
429#define PFM_PMU_IRQ_RESEND 1 /* PMU needs explicit IRQ resend */
430
431/*
432 * debug register related type definitions
433 */
434typedef struct {
435 unsigned long ibr_mask:56;
436 unsigned long ibr_plm:4;
437 unsigned long ibr_ig:3;
438 unsigned long ibr_x:1;
439} ibr_mask_reg_t;
440
441typedef struct {
442 unsigned long dbr_mask:56;
443 unsigned long dbr_plm:4;
444 unsigned long dbr_ig:2;
445 unsigned long dbr_w:1;
446 unsigned long dbr_r:1;
447} dbr_mask_reg_t;
448
449typedef union {
450 unsigned long val;
451 ibr_mask_reg_t ibr;
452 dbr_mask_reg_t dbr;
453} dbreg_t;
454
455
456/*
457 * perfmon command descriptions
458 */
459typedef struct {
460 int (*cmd_func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
461 char *cmd_name;
462 int cmd_flags;
463 unsigned int cmd_narg;
464 size_t cmd_argsize;
465 int (*cmd_getsize)(void *arg, size_t *sz);
466} pfm_cmd_desc_t;
467
468#define PFM_CMD_FD 0x01 /* command requires a file descriptor */
469#define PFM_CMD_ARG_READ 0x02 /* command must read argument(s) */
470#define PFM_CMD_ARG_RW 0x04 /* command must read/write argument(s) */
471#define PFM_CMD_STOP 0x08 /* command does not work on zombie context */
472
473
474#define PFM_CMD_NAME(cmd) pfm_cmd_tab[(cmd)].cmd_name
475#define PFM_CMD_READ_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_READ)
476#define PFM_CMD_RW_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_RW)
477#define PFM_CMD_USE_FD(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_FD)
478#define PFM_CMD_STOPPED(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_STOP)
479
480#define PFM_CMD_ARG_MANY -1 /* cannot be zero */
481
482typedef struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 unsigned long pfm_spurious_ovfl_intr_count; /* keep track of spurious ovfl interrupts */
484 unsigned long pfm_replay_ovfl_intr_count; /* keep track of replayed ovfl interrupts */
485 unsigned long pfm_ovfl_intr_count; /* keep track of ovfl interrupts */
486 unsigned long pfm_ovfl_intr_cycles; /* cycles spent processing ovfl interrupts */
487 unsigned long pfm_ovfl_intr_cycles_min; /* min cycles spent processing ovfl interrupts */
488 unsigned long pfm_ovfl_intr_cycles_max; /* max cycles spent processing ovfl interrupts */
489 unsigned long pfm_smpl_handler_calls;
490 unsigned long pfm_smpl_handler_cycles;
491 char pad[SMP_CACHE_BYTES] ____cacheline_aligned;
492} pfm_stats_t;
493
494/*
495 * perfmon internal variables
496 */
497static pfm_stats_t pfm_stats[NR_CPUS];
498static pfm_session_t pfm_sessions; /* global sessions information */
499
500static struct proc_dir_entry *perfmon_dir;
501static pfm_uuid_t pfm_null_uuid = {0,};
502
503static spinlock_t pfm_buffer_fmt_lock;
504static LIST_HEAD(pfm_buffer_fmt_list);
505
506static pmu_config_t *pmu_conf;
507
508/* sysctl() controls */
Stephane Eranian49449302005-04-25 13:08:30 -0700509pfm_sysctl_t pfm_sysctl;
510EXPORT_SYMBOL(pfm_sysctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512static ctl_table pfm_ctl_table[]={
513 {1, "debug", &pfm_sysctl.debug, sizeof(int), 0666, NULL, &proc_dointvec, NULL,},
514 {2, "debug_ovfl", &pfm_sysctl.debug_ovfl, sizeof(int), 0666, NULL, &proc_dointvec, NULL,},
515 {3, "fastctxsw", &pfm_sysctl.fastctxsw, sizeof(int), 0600, NULL, &proc_dointvec, NULL,},
516 {4, "expert_mode", &pfm_sysctl.expert_mode, sizeof(int), 0600, NULL, &proc_dointvec, NULL,},
517 { 0, },
518};
519static ctl_table pfm_sysctl_dir[] = {
520 {1, "perfmon", NULL, 0, 0755, pfm_ctl_table, },
521 {0,},
522};
523static ctl_table pfm_sysctl_root[] = {
524 {1, "kernel", NULL, 0, 0755, pfm_sysctl_dir, },
525 {0,},
526};
527static struct ctl_table_header *pfm_sysctl_header;
528
529static int pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
530static int pfm_flush(struct file *filp);
531
532#define pfm_get_cpu_var(v) __ia64_per_cpu_var(v)
533#define pfm_get_cpu_data(a,b) per_cpu(a, b)
534
535static inline void
536pfm_put_task(struct task_struct *task)
537{
538 if (task != current) put_task_struct(task);
539}
540
541static inline void
542pfm_set_task_notify(struct task_struct *task)
543{
544 struct thread_info *info;
545
546 info = (struct thread_info *) ((char *) task + IA64_TASK_SIZE);
547 set_bit(TIF_NOTIFY_RESUME, &info->flags);
548}
549
550static inline void
551pfm_clear_task_notify(void)
552{
553 clear_thread_flag(TIF_NOTIFY_RESUME);
554}
555
556static inline void
557pfm_reserve_page(unsigned long a)
558{
559 SetPageReserved(vmalloc_to_page((void *)a));
560}
561static inline void
562pfm_unreserve_page(unsigned long a)
563{
564 ClearPageReserved(vmalloc_to_page((void*)a));
565}
566
567static inline unsigned long
568pfm_protect_ctx_ctxsw(pfm_context_t *x)
569{
570 spin_lock(&(x)->ctx_lock);
571 return 0UL;
572}
573
574static inline unsigned long
575pfm_unprotect_ctx_ctxsw(pfm_context_t *x, unsigned long f)
576{
577 spin_unlock(&(x)->ctx_lock);
578}
579
580static inline unsigned int
581pfm_do_munmap(struct mm_struct *mm, unsigned long addr, size_t len, int acct)
582{
583 return do_munmap(mm, addr, len);
584}
585
586static inline unsigned long
587pfm_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags, unsigned long exec)
588{
589 return get_unmapped_area(file, addr, len, pgoff, flags);
590}
591
592
593static struct super_block *
594pfmfs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data)
595{
596 return get_sb_pseudo(fs_type, "pfm:", NULL, PFMFS_MAGIC);
597}
598
599static struct file_system_type pfm_fs_type = {
600 .name = "pfmfs",
601 .get_sb = pfmfs_get_sb,
602 .kill_sb = kill_anon_super,
603};
604
605DEFINE_PER_CPU(unsigned long, pfm_syst_info);
606DEFINE_PER_CPU(struct task_struct *, pmu_owner);
607DEFINE_PER_CPU(pfm_context_t *, pmu_ctx);
608DEFINE_PER_CPU(unsigned long, pmu_activation_number);
609
610
611/* forward declaration */
612static struct file_operations pfm_file_ops;
613
614/*
615 * forward declarations
616 */
617#ifndef CONFIG_SMP
618static void pfm_lazy_save_regs (struct task_struct *ta);
619#endif
620
621void dump_pmu_state(const char *);
622static int pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
623
624#include "perfmon_itanium.h"
625#include "perfmon_mckinley.h"
626#include "perfmon_generic.h"
627
628static pmu_config_t *pmu_confs[]={
629 &pmu_conf_mck,
630 &pmu_conf_ita,
631 &pmu_conf_gen, /* must be last */
632 NULL
633};
634
635
636static int pfm_end_notify_user(pfm_context_t *ctx);
637
638static inline void
639pfm_clear_psr_pp(void)
640{
641 ia64_rsm(IA64_PSR_PP);
642 ia64_srlz_i();
643}
644
645static inline void
646pfm_set_psr_pp(void)
647{
648 ia64_ssm(IA64_PSR_PP);
649 ia64_srlz_i();
650}
651
652static inline void
653pfm_clear_psr_up(void)
654{
655 ia64_rsm(IA64_PSR_UP);
656 ia64_srlz_i();
657}
658
659static inline void
660pfm_set_psr_up(void)
661{
662 ia64_ssm(IA64_PSR_UP);
663 ia64_srlz_i();
664}
665
666static inline unsigned long
667pfm_get_psr(void)
668{
669 unsigned long tmp;
670 tmp = ia64_getreg(_IA64_REG_PSR);
671 ia64_srlz_i();
672 return tmp;
673}
674
675static inline void
676pfm_set_psr_l(unsigned long val)
677{
678 ia64_setreg(_IA64_REG_PSR_L, val);
679 ia64_srlz_i();
680}
681
682static inline void
683pfm_freeze_pmu(void)
684{
685 ia64_set_pmc(0,1UL);
686 ia64_srlz_d();
687}
688
689static inline void
690pfm_unfreeze_pmu(void)
691{
692 ia64_set_pmc(0,0UL);
693 ia64_srlz_d();
694}
695
696static inline void
697pfm_restore_ibrs(unsigned long *ibrs, unsigned int nibrs)
698{
699 int i;
700
701 for (i=0; i < nibrs; i++) {
702 ia64_set_ibr(i, ibrs[i]);
703 ia64_dv_serialize_instruction();
704 }
705 ia64_srlz_i();
706}
707
708static inline void
709pfm_restore_dbrs(unsigned long *dbrs, unsigned int ndbrs)
710{
711 int i;
712
713 for (i=0; i < ndbrs; i++) {
714 ia64_set_dbr(i, dbrs[i]);
715 ia64_dv_serialize_data();
716 }
717 ia64_srlz_d();
718}
719
720/*
721 * PMD[i] must be a counter. no check is made
722 */
723static inline unsigned long
724pfm_read_soft_counter(pfm_context_t *ctx, int i)
725{
726 return ctx->ctx_pmds[i].val + (ia64_get_pmd(i) & pmu_conf->ovfl_val);
727}
728
729/*
730 * PMD[i] must be a counter. no check is made
731 */
732static inline void
733pfm_write_soft_counter(pfm_context_t *ctx, int i, unsigned long val)
734{
735 unsigned long ovfl_val = pmu_conf->ovfl_val;
736
737 ctx->ctx_pmds[i].val = val & ~ovfl_val;
738 /*
739 * writing to unimplemented part is ignore, so we do not need to
740 * mask off top part
741 */
742 ia64_set_pmd(i, val & ovfl_val);
743}
744
745static pfm_msg_t *
746pfm_get_new_msg(pfm_context_t *ctx)
747{
748 int idx, next;
749
750 next = (ctx->ctx_msgq_tail+1) % PFM_MAX_MSGS;
751
752 DPRINT(("ctx_fd=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
753 if (next == ctx->ctx_msgq_head) return NULL;
754
755 idx = ctx->ctx_msgq_tail;
756 ctx->ctx_msgq_tail = next;
757
758 DPRINT(("ctx=%p head=%d tail=%d msg=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail, idx));
759
760 return ctx->ctx_msgq+idx;
761}
762
763static pfm_msg_t *
764pfm_get_next_msg(pfm_context_t *ctx)
765{
766 pfm_msg_t *msg;
767
768 DPRINT(("ctx=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
769
770 if (PFM_CTXQ_EMPTY(ctx)) return NULL;
771
772 /*
773 * get oldest message
774 */
775 msg = ctx->ctx_msgq+ctx->ctx_msgq_head;
776
777 /*
778 * and move forward
779 */
780 ctx->ctx_msgq_head = (ctx->ctx_msgq_head+1) % PFM_MAX_MSGS;
781
782 DPRINT(("ctx=%p head=%d tail=%d type=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail, msg->pfm_gen_msg.msg_type));
783
784 return msg;
785}
786
787static void
788pfm_reset_msgq(pfm_context_t *ctx)
789{
790 ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
791 DPRINT(("ctx=%p msgq reset\n", ctx));
792}
793
794static void *
795pfm_rvmalloc(unsigned long size)
796{
797 void *mem;
798 unsigned long addr;
799
800 size = PAGE_ALIGN(size);
801 mem = vmalloc(size);
802 if (mem) {
803 //printk("perfmon: CPU%d pfm_rvmalloc(%ld)=%p\n", smp_processor_id(), size, mem);
804 memset(mem, 0, size);
805 addr = (unsigned long)mem;
806 while (size > 0) {
807 pfm_reserve_page(addr);
808 addr+=PAGE_SIZE;
809 size-=PAGE_SIZE;
810 }
811 }
812 return mem;
813}
814
815static void
816pfm_rvfree(void *mem, unsigned long size)
817{
818 unsigned long addr;
819
820 if (mem) {
821 DPRINT(("freeing physical buffer @%p size=%lu\n", mem, size));
822 addr = (unsigned long) mem;
823 while ((long) size > 0) {
824 pfm_unreserve_page(addr);
825 addr+=PAGE_SIZE;
826 size-=PAGE_SIZE;
827 }
828 vfree(mem);
829 }
830 return;
831}
832
833static pfm_context_t *
834pfm_context_alloc(void)
835{
836 pfm_context_t *ctx;
837
838 /*
839 * allocate context descriptor
840 * must be able to free with interrupts disabled
841 */
842 ctx = kmalloc(sizeof(pfm_context_t), GFP_KERNEL);
843 if (ctx) {
844 memset(ctx, 0, sizeof(pfm_context_t));
845 DPRINT(("alloc ctx @%p\n", ctx));
846 }
847 return ctx;
848}
849
850static void
851pfm_context_free(pfm_context_t *ctx)
852{
853 if (ctx) {
854 DPRINT(("free ctx @%p\n", ctx));
855 kfree(ctx);
856 }
857}
858
859static void
860pfm_mask_monitoring(struct task_struct *task)
861{
862 pfm_context_t *ctx = PFM_GET_CTX(task);
863 struct thread_struct *th = &task->thread;
864 unsigned long mask, val, ovfl_mask;
865 int i;
866
867 DPRINT_ovfl(("masking monitoring for [%d]\n", task->pid));
868
869 ovfl_mask = pmu_conf->ovfl_val;
870 /*
871 * monitoring can only be masked as a result of a valid
872 * counter overflow. In UP, it means that the PMU still
873 * has an owner. Note that the owner can be different
874 * from the current task. However the PMU state belongs
875 * to the owner.
876 * In SMP, a valid overflow only happens when task is
877 * current. Therefore if we come here, we know that
878 * the PMU state belongs to the current task, therefore
879 * we can access the live registers.
880 *
881 * So in both cases, the live register contains the owner's
882 * state. We can ONLY touch the PMU registers and NOT the PSR.
883 *
884 * As a consequence to this call, the thread->pmds[] array
885 * contains stale information which must be ignored
886 * when context is reloaded AND monitoring is active (see
887 * pfm_restart).
888 */
889 mask = ctx->ctx_used_pmds[0];
890 for (i = 0; mask; i++, mask>>=1) {
891 /* skip non used pmds */
892 if ((mask & 0x1) == 0) continue;
893 val = ia64_get_pmd(i);
894
895 if (PMD_IS_COUNTING(i)) {
896 /*
897 * we rebuild the full 64 bit value of the counter
898 */
899 ctx->ctx_pmds[i].val += (val & ovfl_mask);
900 } else {
901 ctx->ctx_pmds[i].val = val;
902 }
903 DPRINT_ovfl(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
904 i,
905 ctx->ctx_pmds[i].val,
906 val & ovfl_mask));
907 }
908 /*
909 * mask monitoring by setting the privilege level to 0
910 * we cannot use psr.pp/psr.up for this, it is controlled by
911 * the user
912 *
913 * if task is current, modify actual registers, otherwise modify
914 * thread save state, i.e., what will be restored in pfm_load_regs()
915 */
916 mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
917 for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
918 if ((mask & 0x1) == 0UL) continue;
919 ia64_set_pmc(i, th->pmcs[i] & ~0xfUL);
920 th->pmcs[i] &= ~0xfUL;
921 DPRINT_ovfl(("pmc[%d]=0x%lx\n", i, th->pmcs[i]));
922 }
923 /*
924 * make all of this visible
925 */
926 ia64_srlz_d();
927}
928
929/*
930 * must always be done with task == current
931 *
932 * context must be in MASKED state when calling
933 */
934static void
935pfm_restore_monitoring(struct task_struct *task)
936{
937 pfm_context_t *ctx = PFM_GET_CTX(task);
938 struct thread_struct *th = &task->thread;
939 unsigned long mask, ovfl_mask;
940 unsigned long psr, val;
941 int i, is_system;
942
943 is_system = ctx->ctx_fl_system;
944 ovfl_mask = pmu_conf->ovfl_val;
945
946 if (task != current) {
947 printk(KERN_ERR "perfmon.%d: invalid task[%d] current[%d]\n", __LINE__, task->pid, current->pid);
948 return;
949 }
950 if (ctx->ctx_state != PFM_CTX_MASKED) {
951 printk(KERN_ERR "perfmon.%d: task[%d] current[%d] invalid state=%d\n", __LINE__,
952 task->pid, current->pid, ctx->ctx_state);
953 return;
954 }
955 psr = pfm_get_psr();
956 /*
957 * monitoring is masked via the PMC.
958 * As we restore their value, we do not want each counter to
959 * restart right away. We stop monitoring using the PSR,
960 * restore the PMC (and PMD) and then re-establish the psr
961 * as it was. Note that there can be no pending overflow at
962 * this point, because monitoring was MASKED.
963 *
964 * system-wide session are pinned and self-monitoring
965 */
966 if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
967 /* disable dcr pp */
968 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
969 pfm_clear_psr_pp();
970 } else {
971 pfm_clear_psr_up();
972 }
973 /*
974 * first, we restore the PMD
975 */
976 mask = ctx->ctx_used_pmds[0];
977 for (i = 0; mask; i++, mask>>=1) {
978 /* skip non used pmds */
979 if ((mask & 0x1) == 0) continue;
980
981 if (PMD_IS_COUNTING(i)) {
982 /*
983 * we split the 64bit value according to
984 * counter width
985 */
986 val = ctx->ctx_pmds[i].val & ovfl_mask;
987 ctx->ctx_pmds[i].val &= ~ovfl_mask;
988 } else {
989 val = ctx->ctx_pmds[i].val;
990 }
991 ia64_set_pmd(i, val);
992
993 DPRINT(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
994 i,
995 ctx->ctx_pmds[i].val,
996 val));
997 }
998 /*
999 * restore the PMCs
1000 */
1001 mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
1002 for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
1003 if ((mask & 0x1) == 0UL) continue;
1004 th->pmcs[i] = ctx->ctx_pmcs[i];
1005 ia64_set_pmc(i, th->pmcs[i]);
1006 DPRINT(("[%d] pmc[%d]=0x%lx\n", task->pid, i, th->pmcs[i]));
1007 }
1008 ia64_srlz_d();
1009
1010 /*
1011 * must restore DBR/IBR because could be modified while masked
1012 * XXX: need to optimize
1013 */
1014 if (ctx->ctx_fl_using_dbreg) {
1015 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
1016 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
1017 }
1018
1019 /*
1020 * now restore PSR
1021 */
1022 if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
1023 /* enable dcr pp */
1024 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
1025 ia64_srlz_i();
1026 }
1027 pfm_set_psr_l(psr);
1028}
1029
1030static inline void
1031pfm_save_pmds(unsigned long *pmds, unsigned long mask)
1032{
1033 int i;
1034
1035 ia64_srlz_d();
1036
1037 for (i=0; mask; i++, mask>>=1) {
1038 if (mask & 0x1) pmds[i] = ia64_get_pmd(i);
1039 }
1040}
1041
1042/*
1043 * reload from thread state (used for ctxw only)
1044 */
1045static inline void
1046pfm_restore_pmds(unsigned long *pmds, unsigned long mask)
1047{
1048 int i;
1049 unsigned long val, ovfl_val = pmu_conf->ovfl_val;
1050
1051 for (i=0; mask; i++, mask>>=1) {
1052 if ((mask & 0x1) == 0) continue;
1053 val = PMD_IS_COUNTING(i) ? pmds[i] & ovfl_val : pmds[i];
1054 ia64_set_pmd(i, val);
1055 }
1056 ia64_srlz_d();
1057}
1058
1059/*
1060 * propagate PMD from context to thread-state
1061 */
1062static inline void
1063pfm_copy_pmds(struct task_struct *task, pfm_context_t *ctx)
1064{
1065 struct thread_struct *thread = &task->thread;
1066 unsigned long ovfl_val = pmu_conf->ovfl_val;
1067 unsigned long mask = ctx->ctx_all_pmds[0];
1068 unsigned long val;
1069 int i;
1070
1071 DPRINT(("mask=0x%lx\n", mask));
1072
1073 for (i=0; mask; i++, mask>>=1) {
1074
1075 val = ctx->ctx_pmds[i].val;
1076
1077 /*
1078 * We break up the 64 bit value into 2 pieces
1079 * the lower bits go to the machine state in the
1080 * thread (will be reloaded on ctxsw in).
1081 * The upper part stays in the soft-counter.
1082 */
1083 if (PMD_IS_COUNTING(i)) {
1084 ctx->ctx_pmds[i].val = val & ~ovfl_val;
1085 val &= ovfl_val;
1086 }
1087 thread->pmds[i] = val;
1088
1089 DPRINT(("pmd[%d]=0x%lx soft_val=0x%lx\n",
1090 i,
1091 thread->pmds[i],
1092 ctx->ctx_pmds[i].val));
1093 }
1094}
1095
1096/*
1097 * propagate PMC from context to thread-state
1098 */
1099static inline void
1100pfm_copy_pmcs(struct task_struct *task, pfm_context_t *ctx)
1101{
1102 struct thread_struct *thread = &task->thread;
1103 unsigned long mask = ctx->ctx_all_pmcs[0];
1104 int i;
1105
1106 DPRINT(("mask=0x%lx\n", mask));
1107
1108 for (i=0; mask; i++, mask>>=1) {
1109 /* masking 0 with ovfl_val yields 0 */
1110 thread->pmcs[i] = ctx->ctx_pmcs[i];
1111 DPRINT(("pmc[%d]=0x%lx\n", i, thread->pmcs[i]));
1112 }
1113}
1114
1115
1116
1117static inline void
1118pfm_restore_pmcs(unsigned long *pmcs, unsigned long mask)
1119{
1120 int i;
1121
1122 for (i=0; mask; i++, mask>>=1) {
1123 if ((mask & 0x1) == 0) continue;
1124 ia64_set_pmc(i, pmcs[i]);
1125 }
1126 ia64_srlz_d();
1127}
1128
1129static inline int
1130pfm_uuid_cmp(pfm_uuid_t a, pfm_uuid_t b)
1131{
1132 return memcmp(a, b, sizeof(pfm_uuid_t));
1133}
1134
1135static inline int
1136pfm_buf_fmt_exit(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, struct pt_regs *regs)
1137{
1138 int ret = 0;
1139 if (fmt->fmt_exit) ret = (*fmt->fmt_exit)(task, buf, regs);
1140 return ret;
1141}
1142
1143static inline int
1144pfm_buf_fmt_getsize(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags, int cpu, void *arg, unsigned long *size)
1145{
1146 int ret = 0;
1147 if (fmt->fmt_getsize) ret = (*fmt->fmt_getsize)(task, flags, cpu, arg, size);
1148 return ret;
1149}
1150
1151
1152static inline int
1153pfm_buf_fmt_validate(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags,
1154 int cpu, void *arg)
1155{
1156 int ret = 0;
1157 if (fmt->fmt_validate) ret = (*fmt->fmt_validate)(task, flags, cpu, arg);
1158 return ret;
1159}
1160
1161static inline int
1162pfm_buf_fmt_init(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, unsigned int flags,
1163 int cpu, void *arg)
1164{
1165 int ret = 0;
1166 if (fmt->fmt_init) ret = (*fmt->fmt_init)(task, buf, flags, cpu, arg);
1167 return ret;
1168}
1169
1170static inline int
1171pfm_buf_fmt_restart(pfm_buffer_fmt_t *fmt, struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
1172{
1173 int ret = 0;
1174 if (fmt->fmt_restart) ret = (*fmt->fmt_restart)(task, ctrl, buf, regs);
1175 return ret;
1176}
1177
1178static inline int
1179pfm_buf_fmt_restart_active(pfm_buffer_fmt_t *fmt, struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
1180{
1181 int ret = 0;
1182 if (fmt->fmt_restart_active) ret = (*fmt->fmt_restart_active)(task, ctrl, buf, regs);
1183 return ret;
1184}
1185
1186static pfm_buffer_fmt_t *
1187__pfm_find_buffer_fmt(pfm_uuid_t uuid)
1188{
1189 struct list_head * pos;
1190 pfm_buffer_fmt_t * entry;
1191
1192 list_for_each(pos, &pfm_buffer_fmt_list) {
1193 entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
1194 if (pfm_uuid_cmp(uuid, entry->fmt_uuid) == 0)
1195 return entry;
1196 }
1197 return NULL;
1198}
1199
1200/*
1201 * find a buffer format based on its uuid
1202 */
1203static pfm_buffer_fmt_t *
1204pfm_find_buffer_fmt(pfm_uuid_t uuid)
1205{
1206 pfm_buffer_fmt_t * fmt;
1207 spin_lock(&pfm_buffer_fmt_lock);
1208 fmt = __pfm_find_buffer_fmt(uuid);
1209 spin_unlock(&pfm_buffer_fmt_lock);
1210 return fmt;
1211}
1212
1213int
1214pfm_register_buffer_fmt(pfm_buffer_fmt_t *fmt)
1215{
1216 int ret = 0;
1217
1218 /* some sanity checks */
1219 if (fmt == NULL || fmt->fmt_name == NULL) return -EINVAL;
1220
1221 /* we need at least a handler */
1222 if (fmt->fmt_handler == NULL) return -EINVAL;
1223
1224 /*
1225 * XXX: need check validity of fmt_arg_size
1226 */
1227
1228 spin_lock(&pfm_buffer_fmt_lock);
1229
1230 if (__pfm_find_buffer_fmt(fmt->fmt_uuid)) {
1231 printk(KERN_ERR "perfmon: duplicate sampling format: %s\n", fmt->fmt_name);
1232 ret = -EBUSY;
1233 goto out;
1234 }
1235 list_add(&fmt->fmt_list, &pfm_buffer_fmt_list);
1236 printk(KERN_INFO "perfmon: added sampling format %s\n", fmt->fmt_name);
1237
1238out:
1239 spin_unlock(&pfm_buffer_fmt_lock);
1240 return ret;
1241}
1242EXPORT_SYMBOL(pfm_register_buffer_fmt);
1243
1244int
1245pfm_unregister_buffer_fmt(pfm_uuid_t uuid)
1246{
1247 pfm_buffer_fmt_t *fmt;
1248 int ret = 0;
1249
1250 spin_lock(&pfm_buffer_fmt_lock);
1251
1252 fmt = __pfm_find_buffer_fmt(uuid);
1253 if (!fmt) {
1254 printk(KERN_ERR "perfmon: cannot unregister format, not found\n");
1255 ret = -EINVAL;
1256 goto out;
1257 }
1258 list_del_init(&fmt->fmt_list);
1259 printk(KERN_INFO "perfmon: removed sampling format: %s\n", fmt->fmt_name);
1260
1261out:
1262 spin_unlock(&pfm_buffer_fmt_lock);
1263 return ret;
1264
1265}
1266EXPORT_SYMBOL(pfm_unregister_buffer_fmt);
1267
1268static int
1269pfm_reserve_session(struct task_struct *task, int is_syswide, unsigned int cpu)
1270{
1271 unsigned long flags;
1272 /*
1273 * validy checks on cpu_mask have been done upstream
1274 */
1275 LOCK_PFS(flags);
1276
1277 DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1278 pfm_sessions.pfs_sys_sessions,
1279 pfm_sessions.pfs_task_sessions,
1280 pfm_sessions.pfs_sys_use_dbregs,
1281 is_syswide,
1282 cpu));
1283
1284 if (is_syswide) {
1285 /*
1286 * cannot mix system wide and per-task sessions
1287 */
1288 if (pfm_sessions.pfs_task_sessions > 0UL) {
1289 DPRINT(("system wide not possible, %u conflicting task_sessions\n",
1290 pfm_sessions.pfs_task_sessions));
1291 goto abort;
1292 }
1293
1294 if (pfm_sessions.pfs_sys_session[cpu]) goto error_conflict;
1295
1296 DPRINT(("reserving system wide session on CPU%u currently on CPU%u\n", cpu, smp_processor_id()));
1297
1298 pfm_sessions.pfs_sys_session[cpu] = task;
1299
1300 pfm_sessions.pfs_sys_sessions++ ;
1301
1302 } else {
1303 if (pfm_sessions.pfs_sys_sessions) goto abort;
1304 pfm_sessions.pfs_task_sessions++;
1305 }
1306
1307 DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1308 pfm_sessions.pfs_sys_sessions,
1309 pfm_sessions.pfs_task_sessions,
1310 pfm_sessions.pfs_sys_use_dbregs,
1311 is_syswide,
1312 cpu));
1313
1314 UNLOCK_PFS(flags);
1315
1316 return 0;
1317
1318error_conflict:
1319 DPRINT(("system wide not possible, conflicting session [%d] on CPU%d\n",
1320 pfm_sessions.pfs_sys_session[cpu]->pid,
1321 smp_processor_id()));
1322abort:
1323 UNLOCK_PFS(flags);
1324
1325 return -EBUSY;
1326
1327}
1328
1329static int
1330pfm_unreserve_session(pfm_context_t *ctx, int is_syswide, unsigned int cpu)
1331{
1332 unsigned long flags;
1333 /*
1334 * validy checks on cpu_mask have been done upstream
1335 */
1336 LOCK_PFS(flags);
1337
1338 DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1339 pfm_sessions.pfs_sys_sessions,
1340 pfm_sessions.pfs_task_sessions,
1341 pfm_sessions.pfs_sys_use_dbregs,
1342 is_syswide,
1343 cpu));
1344
1345
1346 if (is_syswide) {
1347 pfm_sessions.pfs_sys_session[cpu] = NULL;
1348 /*
1349 * would not work with perfmon+more than one bit in cpu_mask
1350 */
1351 if (ctx && ctx->ctx_fl_using_dbreg) {
1352 if (pfm_sessions.pfs_sys_use_dbregs == 0) {
1353 printk(KERN_ERR "perfmon: invalid release for ctx %p sys_use_dbregs=0\n", ctx);
1354 } else {
1355 pfm_sessions.pfs_sys_use_dbregs--;
1356 }
1357 }
1358 pfm_sessions.pfs_sys_sessions--;
1359 } else {
1360 pfm_sessions.pfs_task_sessions--;
1361 }
1362 DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1363 pfm_sessions.pfs_sys_sessions,
1364 pfm_sessions.pfs_task_sessions,
1365 pfm_sessions.pfs_sys_use_dbregs,
1366 is_syswide,
1367 cpu));
1368
1369 UNLOCK_PFS(flags);
1370
1371 return 0;
1372}
1373
1374/*
1375 * removes virtual mapping of the sampling buffer.
1376 * IMPORTANT: cannot be called with interrupts disable, e.g. inside
1377 * a PROTECT_CTX() section.
1378 */
1379static int
1380pfm_remove_smpl_mapping(struct task_struct *task, void *vaddr, unsigned long size)
1381{
1382 int r;
1383
1384 /* sanity checks */
1385 if (task->mm == NULL || size == 0UL || vaddr == NULL) {
1386 printk(KERN_ERR "perfmon: pfm_remove_smpl_mapping [%d] invalid context mm=%p\n", task->pid, task->mm);
1387 return -EINVAL;
1388 }
1389
1390 DPRINT(("smpl_vaddr=%p size=%lu\n", vaddr, size));
1391
1392 /*
1393 * does the actual unmapping
1394 */
1395 down_write(&task->mm->mmap_sem);
1396
1397 DPRINT(("down_write done smpl_vaddr=%p size=%lu\n", vaddr, size));
1398
1399 r = pfm_do_munmap(task->mm, (unsigned long)vaddr, size, 0);
1400
1401 up_write(&task->mm->mmap_sem);
1402 if (r !=0) {
1403 printk(KERN_ERR "perfmon: [%d] unable to unmap sampling buffer @%p size=%lu\n", task->pid, vaddr, size);
1404 }
1405
1406 DPRINT(("do_unmap(%p, %lu)=%d\n", vaddr, size, r));
1407
1408 return 0;
1409}
1410
1411/*
1412 * free actual physical storage used by sampling buffer
1413 */
1414#if 0
1415static int
1416pfm_free_smpl_buffer(pfm_context_t *ctx)
1417{
1418 pfm_buffer_fmt_t *fmt;
1419
1420 if (ctx->ctx_smpl_hdr == NULL) goto invalid_free;
1421
1422 /*
1423 * we won't use the buffer format anymore
1424 */
1425 fmt = ctx->ctx_buf_fmt;
1426
1427 DPRINT(("sampling buffer @%p size %lu vaddr=%p\n",
1428 ctx->ctx_smpl_hdr,
1429 ctx->ctx_smpl_size,
1430 ctx->ctx_smpl_vaddr));
1431
1432 pfm_buf_fmt_exit(fmt, current, NULL, NULL);
1433
1434 /*
1435 * free the buffer
1436 */
1437 pfm_rvfree(ctx->ctx_smpl_hdr, ctx->ctx_smpl_size);
1438
1439 ctx->ctx_smpl_hdr = NULL;
1440 ctx->ctx_smpl_size = 0UL;
1441
1442 return 0;
1443
1444invalid_free:
1445 printk(KERN_ERR "perfmon: pfm_free_smpl_buffer [%d] no buffer\n", current->pid);
1446 return -EINVAL;
1447}
1448#endif
1449
1450static inline void
1451pfm_exit_smpl_buffer(pfm_buffer_fmt_t *fmt)
1452{
1453 if (fmt == NULL) return;
1454
1455 pfm_buf_fmt_exit(fmt, current, NULL, NULL);
1456
1457}
1458
1459/*
1460 * pfmfs should _never_ be mounted by userland - too much of security hassle,
1461 * no real gain from having the whole whorehouse mounted. So we don't need
1462 * any operations on the root directory. However, we need a non-trivial
1463 * d_name - pfm: will go nicely and kill the special-casing in procfs.
1464 */
1465static struct vfsmount *pfmfs_mnt;
1466
1467static int __init
1468init_pfm_fs(void)
1469{
1470 int err = register_filesystem(&pfm_fs_type);
1471 if (!err) {
1472 pfmfs_mnt = kern_mount(&pfm_fs_type);
1473 err = PTR_ERR(pfmfs_mnt);
1474 if (IS_ERR(pfmfs_mnt))
1475 unregister_filesystem(&pfm_fs_type);
1476 else
1477 err = 0;
1478 }
1479 return err;
1480}
1481
1482static void __exit
1483exit_pfm_fs(void)
1484{
1485 unregister_filesystem(&pfm_fs_type);
1486 mntput(pfmfs_mnt);
1487}
1488
1489static ssize_t
1490pfm_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
1491{
1492 pfm_context_t *ctx;
1493 pfm_msg_t *msg;
1494 ssize_t ret;
1495 unsigned long flags;
1496 DECLARE_WAITQUEUE(wait, current);
1497 if (PFM_IS_FILE(filp) == 0) {
1498 printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", current->pid);
1499 return -EINVAL;
1500 }
1501
1502 ctx = (pfm_context_t *)filp->private_data;
1503 if (ctx == NULL) {
1504 printk(KERN_ERR "perfmon: pfm_read: NULL ctx [%d]\n", current->pid);
1505 return -EINVAL;
1506 }
1507
1508 /*
1509 * check even when there is no message
1510 */
1511 if (size < sizeof(pfm_msg_t)) {
1512 DPRINT(("message is too small ctx=%p (>=%ld)\n", ctx, sizeof(pfm_msg_t)));
1513 return -EINVAL;
1514 }
1515
1516 PROTECT_CTX(ctx, flags);
1517
1518 /*
1519 * put ourselves on the wait queue
1520 */
1521 add_wait_queue(&ctx->ctx_msgq_wait, &wait);
1522
1523
1524 for(;;) {
1525 /*
1526 * check wait queue
1527 */
1528
1529 set_current_state(TASK_INTERRUPTIBLE);
1530
1531 DPRINT(("head=%d tail=%d\n", ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
1532
1533 ret = 0;
1534 if(PFM_CTXQ_EMPTY(ctx) == 0) break;
1535
1536 UNPROTECT_CTX(ctx, flags);
1537
1538 /*
1539 * check non-blocking read
1540 */
1541 ret = -EAGAIN;
1542 if(filp->f_flags & O_NONBLOCK) break;
1543
1544 /*
1545 * check pending signals
1546 */
1547 if(signal_pending(current)) {
1548 ret = -EINTR;
1549 break;
1550 }
1551 /*
1552 * no message, so wait
1553 */
1554 schedule();
1555
1556 PROTECT_CTX(ctx, flags);
1557 }
1558 DPRINT(("[%d] back to running ret=%ld\n", current->pid, ret));
1559 set_current_state(TASK_RUNNING);
1560 remove_wait_queue(&ctx->ctx_msgq_wait, &wait);
1561
1562 if (ret < 0) goto abort;
1563
1564 ret = -EINVAL;
1565 msg = pfm_get_next_msg(ctx);
1566 if (msg == NULL) {
1567 printk(KERN_ERR "perfmon: pfm_read no msg for ctx=%p [%d]\n", ctx, current->pid);
1568 goto abort_locked;
1569 }
1570
Stephane Eranian49449302005-04-25 13:08:30 -07001571 DPRINT(("fd=%d type=%d\n", msg->pfm_gen_msg.msg_ctx_fd, msg->pfm_gen_msg.msg_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 ret = -EFAULT;
1574 if(copy_to_user(buf, msg, sizeof(pfm_msg_t)) == 0) ret = sizeof(pfm_msg_t);
1575
1576abort_locked:
1577 UNPROTECT_CTX(ctx, flags);
1578abort:
1579 return ret;
1580}
1581
1582static ssize_t
1583pfm_write(struct file *file, const char __user *ubuf,
1584 size_t size, loff_t *ppos)
1585{
1586 DPRINT(("pfm_write called\n"));
1587 return -EINVAL;
1588}
1589
1590static unsigned int
1591pfm_poll(struct file *filp, poll_table * wait)
1592{
1593 pfm_context_t *ctx;
1594 unsigned long flags;
1595 unsigned int mask = 0;
1596
1597 if (PFM_IS_FILE(filp) == 0) {
1598 printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", current->pid);
1599 return 0;
1600 }
1601
1602 ctx = (pfm_context_t *)filp->private_data;
1603 if (ctx == NULL) {
1604 printk(KERN_ERR "perfmon: pfm_poll: NULL ctx [%d]\n", current->pid);
1605 return 0;
1606 }
1607
1608
1609 DPRINT(("pfm_poll ctx_fd=%d before poll_wait\n", ctx->ctx_fd));
1610
1611 poll_wait(filp, &ctx->ctx_msgq_wait, wait);
1612
1613 PROTECT_CTX(ctx, flags);
1614
1615 if (PFM_CTXQ_EMPTY(ctx) == 0)
1616 mask = POLLIN | POLLRDNORM;
1617
1618 UNPROTECT_CTX(ctx, flags);
1619
1620 DPRINT(("pfm_poll ctx_fd=%d mask=0x%x\n", ctx->ctx_fd, mask));
1621
1622 return mask;
1623}
1624
1625static int
1626pfm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1627{
1628 DPRINT(("pfm_ioctl called\n"));
1629 return -EINVAL;
1630}
1631
1632/*
1633 * interrupt cannot be masked when coming here
1634 */
1635static inline int
1636pfm_do_fasync(int fd, struct file *filp, pfm_context_t *ctx, int on)
1637{
1638 int ret;
1639
1640 ret = fasync_helper (fd, filp, on, &ctx->ctx_async_queue);
1641
1642 DPRINT(("pfm_fasync called by [%d] on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
1643 current->pid,
1644 fd,
1645 on,
1646 ctx->ctx_async_queue, ret));
1647
1648 return ret;
1649}
1650
1651static int
1652pfm_fasync(int fd, struct file *filp, int on)
1653{
1654 pfm_context_t *ctx;
1655 int ret;
1656
1657 if (PFM_IS_FILE(filp) == 0) {
1658 printk(KERN_ERR "perfmon: pfm_fasync bad magic [%d]\n", current->pid);
1659 return -EBADF;
1660 }
1661
1662 ctx = (pfm_context_t *)filp->private_data;
1663 if (ctx == NULL) {
1664 printk(KERN_ERR "perfmon: pfm_fasync NULL ctx [%d]\n", current->pid);
1665 return -EBADF;
1666 }
1667 /*
1668 * we cannot mask interrupts during this call because this may
1669 * may go to sleep if memory is not readily avalaible.
1670 *
1671 * We are protected from the conetxt disappearing by the get_fd()/put_fd()
1672 * done in caller. Serialization of this function is ensured by caller.
1673 */
1674 ret = pfm_do_fasync(fd, filp, ctx, on);
1675
1676
1677 DPRINT(("pfm_fasync called on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
1678 fd,
1679 on,
1680 ctx->ctx_async_queue, ret));
1681
1682 return ret;
1683}
1684
1685#ifdef CONFIG_SMP
1686/*
1687 * this function is exclusively called from pfm_close().
1688 * The context is not protected at that time, nor are interrupts
1689 * on the remote CPU. That's necessary to avoid deadlocks.
1690 */
1691static void
1692pfm_syswide_force_stop(void *info)
1693{
1694 pfm_context_t *ctx = (pfm_context_t *)info;
1695 struct pt_regs *regs = ia64_task_regs(current);
1696 struct task_struct *owner;
1697 unsigned long flags;
1698 int ret;
1699
1700 if (ctx->ctx_cpu != smp_processor_id()) {
1701 printk(KERN_ERR "perfmon: pfm_syswide_force_stop for CPU%d but on CPU%d\n",
1702 ctx->ctx_cpu,
1703 smp_processor_id());
1704 return;
1705 }
1706 owner = GET_PMU_OWNER();
1707 if (owner != ctx->ctx_task) {
1708 printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected owner [%d] instead of [%d]\n",
1709 smp_processor_id(),
1710 owner->pid, ctx->ctx_task->pid);
1711 return;
1712 }
1713 if (GET_PMU_CTX() != ctx) {
1714 printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected ctx %p instead of %p\n",
1715 smp_processor_id(),
1716 GET_PMU_CTX(), ctx);
1717 return;
1718 }
1719
1720 DPRINT(("on CPU%d forcing system wide stop for [%d]\n", smp_processor_id(), ctx->ctx_task->pid));
1721 /*
1722 * the context is already protected in pfm_close(), we simply
1723 * need to mask interrupts to avoid a PMU interrupt race on
1724 * this CPU
1725 */
1726 local_irq_save(flags);
1727
1728 ret = pfm_context_unload(ctx, NULL, 0, regs);
1729 if (ret) {
1730 DPRINT(("context_unload returned %d\n", ret));
1731 }
1732
1733 /*
1734 * unmask interrupts, PMU interrupts are now spurious here
1735 */
1736 local_irq_restore(flags);
1737}
1738
1739static void
1740pfm_syswide_cleanup_other_cpu(pfm_context_t *ctx)
1741{
1742 int ret;
1743
1744 DPRINT(("calling CPU%d for cleanup\n", ctx->ctx_cpu));
1745 ret = smp_call_function_single(ctx->ctx_cpu, pfm_syswide_force_stop, ctx, 0, 1);
1746 DPRINT(("called CPU%d for cleanup ret=%d\n", ctx->ctx_cpu, ret));
1747}
1748#endif /* CONFIG_SMP */
1749
1750/*
1751 * called for each close(). Partially free resources.
1752 * When caller is self-monitoring, the context is unloaded.
1753 */
1754static int
1755pfm_flush(struct file *filp)
1756{
1757 pfm_context_t *ctx;
1758 struct task_struct *task;
1759 struct pt_regs *regs;
1760 unsigned long flags;
1761 unsigned long smpl_buf_size = 0UL;
1762 void *smpl_buf_vaddr = NULL;
1763 int state, is_system;
1764
1765 if (PFM_IS_FILE(filp) == 0) {
1766 DPRINT(("bad magic for\n"));
1767 return -EBADF;
1768 }
1769
1770 ctx = (pfm_context_t *)filp->private_data;
1771 if (ctx == NULL) {
1772 printk(KERN_ERR "perfmon: pfm_flush: NULL ctx [%d]\n", current->pid);
1773 return -EBADF;
1774 }
1775
1776 /*
1777 * remove our file from the async queue, if we use this mode.
1778 * This can be done without the context being protected. We come
1779 * here when the context has become unreacheable by other tasks.
1780 *
1781 * We may still have active monitoring at this point and we may
1782 * end up in pfm_overflow_handler(). However, fasync_helper()
1783 * operates with interrupts disabled and it cleans up the
1784 * queue. If the PMU handler is called prior to entering
1785 * fasync_helper() then it will send a signal. If it is
1786 * invoked after, it will find an empty queue and no
1787 * signal will be sent. In both case, we are safe
1788 */
1789 if (filp->f_flags & FASYNC) {
1790 DPRINT(("cleaning up async_queue=%p\n", ctx->ctx_async_queue));
1791 pfm_do_fasync (-1, filp, ctx, 0);
1792 }
1793
1794 PROTECT_CTX(ctx, flags);
1795
1796 state = ctx->ctx_state;
1797 is_system = ctx->ctx_fl_system;
1798
1799 task = PFM_CTX_TASK(ctx);
1800 regs = ia64_task_regs(task);
1801
1802 DPRINT(("ctx_state=%d is_current=%d\n",
1803 state,
1804 task == current ? 1 : 0));
1805
1806 /*
1807 * if state == UNLOADED, then task is NULL
1808 */
1809
1810 /*
1811 * we must stop and unload because we are losing access to the context.
1812 */
1813 if (task == current) {
1814#ifdef CONFIG_SMP
1815 /*
1816 * the task IS the owner but it migrated to another CPU: that's bad
1817 * but we must handle this cleanly. Unfortunately, the kernel does
1818 * not provide a mechanism to block migration (while the context is loaded).
1819 *
1820 * We need to release the resource on the ORIGINAL cpu.
1821 */
1822 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
1823
1824 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
1825 /*
1826 * keep context protected but unmask interrupt for IPI
1827 */
1828 local_irq_restore(flags);
1829
1830 pfm_syswide_cleanup_other_cpu(ctx);
1831
1832 /*
1833 * restore interrupt masking
1834 */
1835 local_irq_save(flags);
1836
1837 /*
1838 * context is unloaded at this point
1839 */
1840 } else
1841#endif /* CONFIG_SMP */
1842 {
1843
1844 DPRINT(("forcing unload\n"));
1845 /*
1846 * stop and unload, returning with state UNLOADED
1847 * and session unreserved.
1848 */
1849 pfm_context_unload(ctx, NULL, 0, regs);
1850
1851 DPRINT(("ctx_state=%d\n", ctx->ctx_state));
1852 }
1853 }
1854
1855 /*
1856 * remove virtual mapping, if any, for the calling task.
1857 * cannot reset ctx field until last user is calling close().
1858 *
1859 * ctx_smpl_vaddr must never be cleared because it is needed
1860 * by every task with access to the context
1861 *
1862 * When called from do_exit(), the mm context is gone already, therefore
1863 * mm is NULL, i.e., the VMA is already gone and we do not have to
1864 * do anything here
1865 */
1866 if (ctx->ctx_smpl_vaddr && current->mm) {
1867 smpl_buf_vaddr = ctx->ctx_smpl_vaddr;
1868 smpl_buf_size = ctx->ctx_smpl_size;
1869 }
1870
1871 UNPROTECT_CTX(ctx, flags);
1872
1873 /*
1874 * if there was a mapping, then we systematically remove it
1875 * at this point. Cannot be done inside critical section
1876 * because some VM function reenables interrupts.
1877 *
1878 */
1879 if (smpl_buf_vaddr) pfm_remove_smpl_mapping(current, smpl_buf_vaddr, smpl_buf_size);
1880
1881 return 0;
1882}
1883/*
1884 * called either on explicit close() or from exit_files().
1885 * Only the LAST user of the file gets to this point, i.e., it is
1886 * called only ONCE.
1887 *
1888 * IMPORTANT: we get called ONLY when the refcnt on the file gets to zero
1889 * (fput()),i.e, last task to access the file. Nobody else can access the
1890 * file at this point.
1891 *
1892 * When called from exit_files(), the VMA has been freed because exit_mm()
1893 * is executed before exit_files().
1894 *
1895 * When called from exit_files(), the current task is not yet ZOMBIE but we
1896 * flush the PMU state to the context.
1897 */
1898static int
1899pfm_close(struct inode *inode, struct file *filp)
1900{
1901 pfm_context_t *ctx;
1902 struct task_struct *task;
1903 struct pt_regs *regs;
1904 DECLARE_WAITQUEUE(wait, current);
1905 unsigned long flags;
1906 unsigned long smpl_buf_size = 0UL;
1907 void *smpl_buf_addr = NULL;
1908 int free_possible = 1;
1909 int state, is_system;
1910
1911 DPRINT(("pfm_close called private=%p\n", filp->private_data));
1912
1913 if (PFM_IS_FILE(filp) == 0) {
1914 DPRINT(("bad magic\n"));
1915 return -EBADF;
1916 }
1917
1918 ctx = (pfm_context_t *)filp->private_data;
1919 if (ctx == NULL) {
1920 printk(KERN_ERR "perfmon: pfm_close: NULL ctx [%d]\n", current->pid);
1921 return -EBADF;
1922 }
1923
1924 PROTECT_CTX(ctx, flags);
1925
1926 state = ctx->ctx_state;
1927 is_system = ctx->ctx_fl_system;
1928
1929 task = PFM_CTX_TASK(ctx);
1930 regs = ia64_task_regs(task);
1931
1932 DPRINT(("ctx_state=%d is_current=%d\n",
1933 state,
1934 task == current ? 1 : 0));
1935
1936 /*
1937 * if task == current, then pfm_flush() unloaded the context
1938 */
1939 if (state == PFM_CTX_UNLOADED) goto doit;
1940
1941 /*
1942 * context is loaded/masked and task != current, we need to
1943 * either force an unload or go zombie
1944 */
1945
1946 /*
1947 * The task is currently blocked or will block after an overflow.
1948 * we must force it to wakeup to get out of the
1949 * MASKED state and transition to the unloaded state by itself.
1950 *
1951 * This situation is only possible for per-task mode
1952 */
1953 if (state == PFM_CTX_MASKED && CTX_OVFL_NOBLOCK(ctx) == 0) {
1954
1955 /*
1956 * set a "partial" zombie state to be checked
1957 * upon return from down() in pfm_handle_work().
1958 *
1959 * We cannot use the ZOMBIE state, because it is checked
1960 * by pfm_load_regs() which is called upon wakeup from down().
1961 * In such case, it would free the context and then we would
1962 * return to pfm_handle_work() which would access the
1963 * stale context. Instead, we set a flag invisible to pfm_load_regs()
1964 * but visible to pfm_handle_work().
1965 *
1966 * For some window of time, we have a zombie context with
1967 * ctx_state = MASKED and not ZOMBIE
1968 */
1969 ctx->ctx_fl_going_zombie = 1;
1970
1971 /*
1972 * force task to wake up from MASKED state
1973 */
1974 up(&ctx->ctx_restart_sem);
1975
1976 DPRINT(("waking up ctx_state=%d\n", state));
1977
1978 /*
1979 * put ourself to sleep waiting for the other
1980 * task to report completion
1981 *
1982 * the context is protected by mutex, therefore there
1983 * is no risk of being notified of completion before
1984 * begin actually on the waitq.
1985 */
1986 set_current_state(TASK_INTERRUPTIBLE);
1987 add_wait_queue(&ctx->ctx_zombieq, &wait);
1988
1989 UNPROTECT_CTX(ctx, flags);
1990
1991 /*
1992 * XXX: check for signals :
1993 * - ok for explicit close
1994 * - not ok when coming from exit_files()
1995 */
1996 schedule();
1997
1998
1999 PROTECT_CTX(ctx, flags);
2000
2001
2002 remove_wait_queue(&ctx->ctx_zombieq, &wait);
2003 set_current_state(TASK_RUNNING);
2004
2005 /*
2006 * context is unloaded at this point
2007 */
2008 DPRINT(("after zombie wakeup ctx_state=%d for\n", state));
2009 }
2010 else if (task != current) {
2011#ifdef CONFIG_SMP
2012 /*
2013 * switch context to zombie state
2014 */
2015 ctx->ctx_state = PFM_CTX_ZOMBIE;
2016
2017 DPRINT(("zombie ctx for [%d]\n", task->pid));
2018 /*
2019 * cannot free the context on the spot. deferred until
2020 * the task notices the ZOMBIE state
2021 */
2022 free_possible = 0;
2023#else
2024 pfm_context_unload(ctx, NULL, 0, regs);
2025#endif
2026 }
2027
2028doit:
2029 /* reload state, may have changed during opening of critical section */
2030 state = ctx->ctx_state;
2031
2032 /*
2033 * the context is still attached to a task (possibly current)
2034 * we cannot destroy it right now
2035 */
2036
2037 /*
2038 * we must free the sampling buffer right here because
2039 * we cannot rely on it being cleaned up later by the
2040 * monitored task. It is not possible to free vmalloc'ed
2041 * memory in pfm_load_regs(). Instead, we remove the buffer
2042 * now. should there be subsequent PMU overflow originally
2043 * meant for sampling, the will be converted to spurious
2044 * and that's fine because the monitoring tools is gone anyway.
2045 */
2046 if (ctx->ctx_smpl_hdr) {
2047 smpl_buf_addr = ctx->ctx_smpl_hdr;
2048 smpl_buf_size = ctx->ctx_smpl_size;
2049 /* no more sampling */
2050 ctx->ctx_smpl_hdr = NULL;
2051 ctx->ctx_fl_is_sampling = 0;
2052 }
2053
2054 DPRINT(("ctx_state=%d free_possible=%d addr=%p size=%lu\n",
2055 state,
2056 free_possible,
2057 smpl_buf_addr,
2058 smpl_buf_size));
2059
2060 if (smpl_buf_addr) pfm_exit_smpl_buffer(ctx->ctx_buf_fmt);
2061
2062 /*
2063 * UNLOADED that the session has already been unreserved.
2064 */
2065 if (state == PFM_CTX_ZOMBIE) {
2066 pfm_unreserve_session(ctx, ctx->ctx_fl_system , ctx->ctx_cpu);
2067 }
2068
2069 /*
2070 * disconnect file descriptor from context must be done
2071 * before we unlock.
2072 */
2073 filp->private_data = NULL;
2074
2075 /*
2076 * if we free on the spot, the context is now completely unreacheable
2077 * from the callers side. The monitored task side is also cut, so we
2078 * can freely cut.
2079 *
2080 * If we have a deferred free, only the caller side is disconnected.
2081 */
2082 UNPROTECT_CTX(ctx, flags);
2083
2084 /*
2085 * All memory free operations (especially for vmalloc'ed memory)
2086 * MUST be done with interrupts ENABLED.
2087 */
2088 if (smpl_buf_addr) pfm_rvfree(smpl_buf_addr, smpl_buf_size);
2089
2090 /*
2091 * return the memory used by the context
2092 */
2093 if (free_possible) pfm_context_free(ctx);
2094
2095 return 0;
2096}
2097
2098static int
2099pfm_no_open(struct inode *irrelevant, struct file *dontcare)
2100{
2101 DPRINT(("pfm_no_open called\n"));
2102 return -ENXIO;
2103}
2104
2105
2106
2107static struct file_operations pfm_file_ops = {
2108 .llseek = no_llseek,
2109 .read = pfm_read,
2110 .write = pfm_write,
2111 .poll = pfm_poll,
2112 .ioctl = pfm_ioctl,
2113 .open = pfm_no_open, /* special open code to disallow open via /proc */
2114 .fasync = pfm_fasync,
2115 .release = pfm_close,
2116 .flush = pfm_flush
2117};
2118
2119static int
2120pfmfs_delete_dentry(struct dentry *dentry)
2121{
2122 return 1;
2123}
2124
2125static struct dentry_operations pfmfs_dentry_operations = {
2126 .d_delete = pfmfs_delete_dentry,
2127};
2128
2129
2130static int
2131pfm_alloc_fd(struct file **cfile)
2132{
2133 int fd, ret = 0;
2134 struct file *file = NULL;
2135 struct inode * inode;
2136 char name[32];
2137 struct qstr this;
2138
2139 fd = get_unused_fd();
2140 if (fd < 0) return -ENFILE;
2141
2142 ret = -ENFILE;
2143
2144 file = get_empty_filp();
2145 if (!file) goto out;
2146
2147 /*
2148 * allocate a new inode
2149 */
2150 inode = new_inode(pfmfs_mnt->mnt_sb);
2151 if (!inode) goto out;
2152
2153 DPRINT(("new inode ino=%ld @%p\n", inode->i_ino, inode));
2154
2155 inode->i_mode = S_IFCHR|S_IRUGO;
2156 inode->i_uid = current->fsuid;
2157 inode->i_gid = current->fsgid;
2158
2159 sprintf(name, "[%lu]", inode->i_ino);
2160 this.name = name;
2161 this.len = strlen(name);
2162 this.hash = inode->i_ino;
2163
2164 ret = -ENOMEM;
2165
2166 /*
2167 * allocate a new dcache entry
2168 */
2169 file->f_dentry = d_alloc(pfmfs_mnt->mnt_sb->s_root, &this);
2170 if (!file->f_dentry) goto out;
2171
2172 file->f_dentry->d_op = &pfmfs_dentry_operations;
2173
2174 d_add(file->f_dentry, inode);
2175 file->f_vfsmnt = mntget(pfmfs_mnt);
2176 file->f_mapping = inode->i_mapping;
2177
2178 file->f_op = &pfm_file_ops;
2179 file->f_mode = FMODE_READ;
2180 file->f_flags = O_RDONLY;
2181 file->f_pos = 0;
2182
2183 /*
2184 * may have to delay until context is attached?
2185 */
2186 fd_install(fd, file);
2187
2188 /*
2189 * the file structure we will use
2190 */
2191 *cfile = file;
2192
2193 return fd;
2194out:
2195 if (file) put_filp(file);
2196 put_unused_fd(fd);
2197 return ret;
2198}
2199
2200static void
2201pfm_free_fd(int fd, struct file *file)
2202{
2203 struct files_struct *files = current->files;
2204
2205 /*
2206 * there ie no fd_uninstall(), so we do it here
2207 */
2208 spin_lock(&files->file_lock);
2209 files->fd[fd] = NULL;
2210 spin_unlock(&files->file_lock);
2211
2212 if (file) put_filp(file);
2213 put_unused_fd(fd);
2214}
2215
2216static int
2217pfm_remap_buffer(struct vm_area_struct *vma, unsigned long buf, unsigned long addr, unsigned long size)
2218{
2219 DPRINT(("CPU%d buf=0x%lx addr=0x%lx size=%ld\n", smp_processor_id(), buf, addr, size));
2220
2221 while (size > 0) {
2222 unsigned long pfn = ia64_tpa(buf) >> PAGE_SHIFT;
2223
2224
2225 if (remap_pfn_range(vma, addr, pfn, PAGE_SIZE, PAGE_READONLY))
2226 return -ENOMEM;
2227
2228 addr += PAGE_SIZE;
2229 buf += PAGE_SIZE;
2230 size -= PAGE_SIZE;
2231 }
2232 return 0;
2233}
2234
2235/*
2236 * allocate a sampling buffer and remaps it into the user address space of the task
2237 */
2238static int
2239pfm_smpl_buffer_alloc(struct task_struct *task, pfm_context_t *ctx, unsigned long rsize, void **user_vaddr)
2240{
2241 struct mm_struct *mm = task->mm;
2242 struct vm_area_struct *vma = NULL;
2243 unsigned long size;
2244 void *smpl_buf;
2245
2246
2247 /*
2248 * the fixed header + requested size and align to page boundary
2249 */
2250 size = PAGE_ALIGN(rsize);
2251
2252 DPRINT(("sampling buffer rsize=%lu size=%lu bytes\n", rsize, size));
2253
2254 /*
2255 * check requested size to avoid Denial-of-service attacks
2256 * XXX: may have to refine this test
2257 * Check against address space limit.
2258 *
2259 * if ((mm->total_vm << PAGE_SHIFT) + len> task->rlim[RLIMIT_AS].rlim_cur)
2260 * return -ENOMEM;
2261 */
2262 if (size > task->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
2263 return -ENOMEM;
2264
2265 /*
2266 * We do the easy to undo allocations first.
2267 *
2268 * pfm_rvmalloc(), clears the buffer, so there is no leak
2269 */
2270 smpl_buf = pfm_rvmalloc(size);
2271 if (smpl_buf == NULL) {
2272 DPRINT(("Can't allocate sampling buffer\n"));
2273 return -ENOMEM;
2274 }
2275
2276 DPRINT(("smpl_buf @%p\n", smpl_buf));
2277
2278 /* allocate vma */
2279 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2280 if (!vma) {
2281 DPRINT(("Cannot allocate vma\n"));
2282 goto error_kmem;
2283 }
2284 memset(vma, 0, sizeof(*vma));
2285
2286 /*
2287 * partially initialize the vma for the sampling buffer
2288 */
2289 vma->vm_mm = mm;
2290 vma->vm_flags = VM_READ| VM_MAYREAD |VM_RESERVED;
2291 vma->vm_page_prot = PAGE_READONLY; /* XXX may need to change */
2292
2293 /*
2294 * Now we have everything we need and we can initialize
2295 * and connect all the data structures
2296 */
2297
2298 ctx->ctx_smpl_hdr = smpl_buf;
2299 ctx->ctx_smpl_size = size; /* aligned size */
2300
2301 /*
2302 * Let's do the difficult operations next.
2303 *
2304 * now we atomically find some area in the address space and
2305 * remap the buffer in it.
2306 */
2307 down_write(&task->mm->mmap_sem);
2308
2309 /* find some free area in address space, must have mmap sem held */
2310 vma->vm_start = pfm_get_unmapped_area(NULL, 0, size, 0, MAP_PRIVATE|MAP_ANONYMOUS, 0);
2311 if (vma->vm_start == 0UL) {
2312 DPRINT(("Cannot find unmapped area for size %ld\n", size));
2313 up_write(&task->mm->mmap_sem);
2314 goto error;
2315 }
2316 vma->vm_end = vma->vm_start + size;
2317 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2318
2319 DPRINT(("aligned size=%ld, hdr=%p mapped @0x%lx\n", size, ctx->ctx_smpl_hdr, vma->vm_start));
2320
2321 /* can only be applied to current task, need to have the mm semaphore held when called */
2322 if (pfm_remap_buffer(vma, (unsigned long)smpl_buf, vma->vm_start, size)) {
2323 DPRINT(("Can't remap buffer\n"));
2324 up_write(&task->mm->mmap_sem);
2325 goto error;
2326 }
2327
2328 /*
2329 * now insert the vma in the vm list for the process, must be
2330 * done with mmap lock held
2331 */
2332 insert_vm_struct(mm, vma);
2333
2334 mm->total_vm += size >> PAGE_SHIFT;
2335 vm_stat_account(vma);
2336 up_write(&task->mm->mmap_sem);
2337
2338 /*
2339 * keep track of user level virtual address
2340 */
2341 ctx->ctx_smpl_vaddr = (void *)vma->vm_start;
2342 *(unsigned long *)user_vaddr = vma->vm_start;
2343
2344 return 0;
2345
2346error:
2347 kmem_cache_free(vm_area_cachep, vma);
2348error_kmem:
2349 pfm_rvfree(smpl_buf, size);
2350
2351 return -ENOMEM;
2352}
2353
2354/*
2355 * XXX: do something better here
2356 */
2357static int
2358pfm_bad_permissions(struct task_struct *task)
2359{
2360 /* inspired by ptrace_attach() */
2361 DPRINT(("cur: uid=%d gid=%d task: euid=%d suid=%d uid=%d egid=%d sgid=%d\n",
2362 current->uid,
2363 current->gid,
2364 task->euid,
2365 task->suid,
2366 task->uid,
2367 task->egid,
2368 task->sgid));
2369
2370 return ((current->uid != task->euid)
2371 || (current->uid != task->suid)
2372 || (current->uid != task->uid)
2373 || (current->gid != task->egid)
2374 || (current->gid != task->sgid)
2375 || (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE);
2376}
2377
2378static int
2379pfarg_is_sane(struct task_struct *task, pfarg_context_t *pfx)
2380{
2381 int ctx_flags;
2382
2383 /* valid signal */
2384
2385 ctx_flags = pfx->ctx_flags;
2386
2387 if (ctx_flags & PFM_FL_SYSTEM_WIDE) {
2388
2389 /*
2390 * cannot block in this mode
2391 */
2392 if (ctx_flags & PFM_FL_NOTIFY_BLOCK) {
2393 DPRINT(("cannot use blocking mode when in system wide monitoring\n"));
2394 return -EINVAL;
2395 }
2396 } else {
2397 }
2398 /* probably more to add here */
2399
2400 return 0;
2401}
2402
2403static int
2404pfm_setup_buffer_fmt(struct task_struct *task, pfm_context_t *ctx, unsigned int ctx_flags,
2405 unsigned int cpu, pfarg_context_t *arg)
2406{
2407 pfm_buffer_fmt_t *fmt = NULL;
2408 unsigned long size = 0UL;
2409 void *uaddr = NULL;
2410 void *fmt_arg = NULL;
2411 int ret = 0;
2412#define PFM_CTXARG_BUF_ARG(a) (pfm_buffer_fmt_t *)(a+1)
2413
2414 /* invoke and lock buffer format, if found */
2415 fmt = pfm_find_buffer_fmt(arg->ctx_smpl_buf_id);
2416 if (fmt == NULL) {
2417 DPRINT(("[%d] cannot find buffer format\n", task->pid));
2418 return -EINVAL;
2419 }
2420
2421 /*
2422 * buffer argument MUST be contiguous to pfarg_context_t
2423 */
2424 if (fmt->fmt_arg_size) fmt_arg = PFM_CTXARG_BUF_ARG(arg);
2425
2426 ret = pfm_buf_fmt_validate(fmt, task, ctx_flags, cpu, fmt_arg);
2427
2428 DPRINT(("[%d] after validate(0x%x,%d,%p)=%d\n", task->pid, ctx_flags, cpu, fmt_arg, ret));
2429
2430 if (ret) goto error;
2431
2432 /* link buffer format and context */
2433 ctx->ctx_buf_fmt = fmt;
2434
2435 /*
2436 * check if buffer format wants to use perfmon buffer allocation/mapping service
2437 */
2438 ret = pfm_buf_fmt_getsize(fmt, task, ctx_flags, cpu, fmt_arg, &size);
2439 if (ret) goto error;
2440
2441 if (size) {
2442 /*
2443 * buffer is always remapped into the caller's address space
2444 */
2445 ret = pfm_smpl_buffer_alloc(current, ctx, size, &uaddr);
2446 if (ret) goto error;
2447
2448 /* keep track of user address of buffer */
2449 arg->ctx_smpl_vaddr = uaddr;
2450 }
2451 ret = pfm_buf_fmt_init(fmt, task, ctx->ctx_smpl_hdr, ctx_flags, cpu, fmt_arg);
2452
2453error:
2454 return ret;
2455}
2456
2457static void
2458pfm_reset_pmu_state(pfm_context_t *ctx)
2459{
2460 int i;
2461
2462 /*
2463 * install reset values for PMC.
2464 */
2465 for (i=1; PMC_IS_LAST(i) == 0; i++) {
2466 if (PMC_IS_IMPL(i) == 0) continue;
2467 ctx->ctx_pmcs[i] = PMC_DFL_VAL(i);
2468 DPRINT(("pmc[%d]=0x%lx\n", i, ctx->ctx_pmcs[i]));
2469 }
2470 /*
2471 * PMD registers are set to 0UL when the context in memset()
2472 */
2473
2474 /*
2475 * On context switched restore, we must restore ALL pmc and ALL pmd even
2476 * when they are not actively used by the task. In UP, the incoming process
2477 * may otherwise pick up left over PMC, PMD state from the previous process.
2478 * As opposed to PMD, stale PMC can cause harm to the incoming
2479 * process because they may change what is being measured.
2480 * Therefore, we must systematically reinstall the entire
2481 * PMC state. In SMP, the same thing is possible on the
2482 * same CPU but also on between 2 CPUs.
2483 *
2484 * The problem with PMD is information leaking especially
2485 * to user level when psr.sp=0
2486 *
2487 * There is unfortunately no easy way to avoid this problem
2488 * on either UP or SMP. This definitively slows down the
2489 * pfm_load_regs() function.
2490 */
2491
2492 /*
2493 * bitmask of all PMCs accessible to this context
2494 *
2495 * PMC0 is treated differently.
2496 */
2497 ctx->ctx_all_pmcs[0] = pmu_conf->impl_pmcs[0] & ~0x1;
2498
2499 /*
2500 * bitmask of all PMDs that are accesible to this context
2501 */
2502 ctx->ctx_all_pmds[0] = pmu_conf->impl_pmds[0];
2503
2504 DPRINT(("<%d> all_pmcs=0x%lx all_pmds=0x%lx\n", ctx->ctx_fd, ctx->ctx_all_pmcs[0],ctx->ctx_all_pmds[0]));
2505
2506 /*
2507 * useful in case of re-enable after disable
2508 */
2509 ctx->ctx_used_ibrs[0] = 0UL;
2510 ctx->ctx_used_dbrs[0] = 0UL;
2511}
2512
2513static int
2514pfm_ctx_getsize(void *arg, size_t *sz)
2515{
2516 pfarg_context_t *req = (pfarg_context_t *)arg;
2517 pfm_buffer_fmt_t *fmt;
2518
2519 *sz = 0;
2520
2521 if (!pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) return 0;
2522
2523 fmt = pfm_find_buffer_fmt(req->ctx_smpl_buf_id);
2524 if (fmt == NULL) {
2525 DPRINT(("cannot find buffer format\n"));
2526 return -EINVAL;
2527 }
2528 /* get just enough to copy in user parameters */
2529 *sz = fmt->fmt_arg_size;
2530 DPRINT(("arg_size=%lu\n", *sz));
2531
2532 return 0;
2533}
2534
2535
2536
2537/*
2538 * cannot attach if :
2539 * - kernel task
2540 * - task not owned by caller
2541 * - task incompatible with context mode
2542 */
2543static int
2544pfm_task_incompatible(pfm_context_t *ctx, struct task_struct *task)
2545{
2546 /*
2547 * no kernel task or task not owner by caller
2548 */
2549 if (task->mm == NULL) {
2550 DPRINT(("task [%d] has not memory context (kernel thread)\n", task->pid));
2551 return -EPERM;
2552 }
2553 if (pfm_bad_permissions(task)) {
2554 DPRINT(("no permission to attach to [%d]\n", task->pid));
2555 return -EPERM;
2556 }
2557 /*
2558 * cannot block in self-monitoring mode
2559 */
2560 if (CTX_OVFL_NOBLOCK(ctx) == 0 && task == current) {
2561 DPRINT(("cannot load a blocking context on self for [%d]\n", task->pid));
2562 return -EINVAL;
2563 }
2564
2565 if (task->exit_state == EXIT_ZOMBIE) {
2566 DPRINT(("cannot attach to zombie task [%d]\n", task->pid));
2567 return -EBUSY;
2568 }
2569
2570 /*
2571 * always ok for self
2572 */
2573 if (task == current) return 0;
2574
2575 if ((task->state != TASK_STOPPED) && (task->state != TASK_TRACED)) {
2576 DPRINT(("cannot attach to non-stopped task [%d] state=%ld\n", task->pid, task->state));
2577 return -EBUSY;
2578 }
2579 /*
2580 * make sure the task is off any CPU
2581 */
2582 wait_task_inactive(task);
2583
2584 /* more to come... */
2585
2586 return 0;
2587}
2588
2589static int
2590pfm_get_task(pfm_context_t *ctx, pid_t pid, struct task_struct **task)
2591{
2592 struct task_struct *p = current;
2593 int ret;
2594
2595 /* XXX: need to add more checks here */
2596 if (pid < 2) return -EPERM;
2597
2598 if (pid != current->pid) {
2599
2600 read_lock(&tasklist_lock);
2601
2602 p = find_task_by_pid(pid);
2603
2604 /* make sure task cannot go away while we operate on it */
2605 if (p) get_task_struct(p);
2606
2607 read_unlock(&tasklist_lock);
2608
2609 if (p == NULL) return -ESRCH;
2610 }
2611
2612 ret = pfm_task_incompatible(ctx, p);
2613 if (ret == 0) {
2614 *task = p;
2615 } else if (p != current) {
2616 pfm_put_task(p);
2617 }
2618 return ret;
2619}
2620
2621
2622
2623static int
2624pfm_context_create(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
2625{
2626 pfarg_context_t *req = (pfarg_context_t *)arg;
2627 struct file *filp;
2628 int ctx_flags;
2629 int ret;
2630
2631 /* let's check the arguments first */
2632 ret = pfarg_is_sane(current, req);
2633 if (ret < 0) return ret;
2634
2635 ctx_flags = req->ctx_flags;
2636
2637 ret = -ENOMEM;
2638
2639 ctx = pfm_context_alloc();
2640 if (!ctx) goto error;
2641
2642 ret = pfm_alloc_fd(&filp);
2643 if (ret < 0) goto error_file;
2644
2645 req->ctx_fd = ctx->ctx_fd = ret;
2646
2647 /*
2648 * attach context to file
2649 */
2650 filp->private_data = ctx;
2651
2652 /*
2653 * does the user want to sample?
2654 */
2655 if (pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) {
2656 ret = pfm_setup_buffer_fmt(current, ctx, ctx_flags, 0, req);
2657 if (ret) goto buffer_error;
2658 }
2659
2660 /*
2661 * init context protection lock
2662 */
2663 spin_lock_init(&ctx->ctx_lock);
2664
2665 /*
2666 * context is unloaded
2667 */
2668 ctx->ctx_state = PFM_CTX_UNLOADED;
2669
2670 /*
2671 * initialization of context's flags
2672 */
2673 ctx->ctx_fl_block = (ctx_flags & PFM_FL_NOTIFY_BLOCK) ? 1 : 0;
2674 ctx->ctx_fl_system = (ctx_flags & PFM_FL_SYSTEM_WIDE) ? 1: 0;
2675 ctx->ctx_fl_is_sampling = ctx->ctx_buf_fmt ? 1 : 0; /* assume record() is defined */
2676 ctx->ctx_fl_no_msg = (ctx_flags & PFM_FL_OVFL_NO_MSG) ? 1: 0;
2677 /*
2678 * will move to set properties
2679 * ctx->ctx_fl_excl_idle = (ctx_flags & PFM_FL_EXCL_IDLE) ? 1: 0;
2680 */
2681
2682 /*
2683 * init restart semaphore to locked
2684 */
2685 sema_init(&ctx->ctx_restart_sem, 0);
2686
2687 /*
2688 * activation is used in SMP only
2689 */
2690 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
2691 SET_LAST_CPU(ctx, -1);
2692
2693 /*
2694 * initialize notification message queue
2695 */
2696 ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
2697 init_waitqueue_head(&ctx->ctx_msgq_wait);
2698 init_waitqueue_head(&ctx->ctx_zombieq);
2699
2700 DPRINT(("ctx=%p flags=0x%x system=%d notify_block=%d excl_idle=%d no_msg=%d ctx_fd=%d \n",
2701 ctx,
2702 ctx_flags,
2703 ctx->ctx_fl_system,
2704 ctx->ctx_fl_block,
2705 ctx->ctx_fl_excl_idle,
2706 ctx->ctx_fl_no_msg,
2707 ctx->ctx_fd));
2708
2709 /*
2710 * initialize soft PMU state
2711 */
2712 pfm_reset_pmu_state(ctx);
2713
2714 return 0;
2715
2716buffer_error:
2717 pfm_free_fd(ctx->ctx_fd, filp);
2718
2719 if (ctx->ctx_buf_fmt) {
2720 pfm_buf_fmt_exit(ctx->ctx_buf_fmt, current, NULL, regs);
2721 }
2722error_file:
2723 pfm_context_free(ctx);
2724
2725error:
2726 return ret;
2727}
2728
2729static inline unsigned long
2730pfm_new_counter_value (pfm_counter_t *reg, int is_long_reset)
2731{
2732 unsigned long val = is_long_reset ? reg->long_reset : reg->short_reset;
2733 unsigned long new_seed, old_seed = reg->seed, mask = reg->mask;
2734 extern unsigned long carta_random32 (unsigned long seed);
2735
2736 if (reg->flags & PFM_REGFL_RANDOM) {
2737 new_seed = carta_random32(old_seed);
2738 val -= (old_seed & mask); /* counter values are negative numbers! */
2739 if ((mask >> 32) != 0)
2740 /* construct a full 64-bit random value: */
2741 new_seed |= carta_random32(old_seed >> 32) << 32;
2742 reg->seed = new_seed;
2743 }
2744 reg->lval = val;
2745 return val;
2746}
2747
2748static void
2749pfm_reset_regs_masked(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
2750{
2751 unsigned long mask = ovfl_regs[0];
2752 unsigned long reset_others = 0UL;
2753 unsigned long val;
2754 int i;
2755
2756 /*
2757 * now restore reset value on sampling overflowed counters
2758 */
2759 mask >>= PMU_FIRST_COUNTER;
2760 for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
2761
2762 if ((mask & 0x1UL) == 0UL) continue;
2763
2764 ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
2765 reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
2766
2767 DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
2768 }
2769
2770 /*
2771 * Now take care of resetting the other registers
2772 */
2773 for(i = 0; reset_others; i++, reset_others >>= 1) {
2774
2775 if ((reset_others & 0x1) == 0) continue;
2776
2777 ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
2778
2779 DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
2780 is_long_reset ? "long" : "short", i, val));
2781 }
2782}
2783
2784static void
2785pfm_reset_regs(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
2786{
2787 unsigned long mask = ovfl_regs[0];
2788 unsigned long reset_others = 0UL;
2789 unsigned long val;
2790 int i;
2791
2792 DPRINT_ovfl(("ovfl_regs=0x%lx is_long_reset=%d\n", ovfl_regs[0], is_long_reset));
2793
2794 if (ctx->ctx_state == PFM_CTX_MASKED) {
2795 pfm_reset_regs_masked(ctx, ovfl_regs, is_long_reset);
2796 return;
2797 }
2798
2799 /*
2800 * now restore reset value on sampling overflowed counters
2801 */
2802 mask >>= PMU_FIRST_COUNTER;
2803 for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
2804
2805 if ((mask & 0x1UL) == 0UL) continue;
2806
2807 val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
2808 reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
2809
2810 DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
2811
2812 pfm_write_soft_counter(ctx, i, val);
2813 }
2814
2815 /*
2816 * Now take care of resetting the other registers
2817 */
2818 for(i = 0; reset_others; i++, reset_others >>= 1) {
2819
2820 if ((reset_others & 0x1) == 0) continue;
2821
2822 val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
2823
2824 if (PMD_IS_COUNTING(i)) {
2825 pfm_write_soft_counter(ctx, i, val);
2826 } else {
2827 ia64_set_pmd(i, val);
2828 }
2829 DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
2830 is_long_reset ? "long" : "short", i, val));
2831 }
2832 ia64_srlz_d();
2833}
2834
2835static int
2836pfm_write_pmcs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
2837{
2838 struct thread_struct *thread = NULL;
2839 struct task_struct *task;
2840 pfarg_reg_t *req = (pfarg_reg_t *)arg;
2841 unsigned long value, pmc_pm;
2842 unsigned long smpl_pmds, reset_pmds, impl_pmds;
2843 unsigned int cnum, reg_flags, flags, pmc_type;
2844 int i, can_access_pmu = 0, is_loaded, is_system, expert_mode;
2845 int is_monitor, is_counting, state;
2846 int ret = -EINVAL;
2847 pfm_reg_check_t wr_func;
2848#define PFM_CHECK_PMC_PM(x, y, z) ((x)->ctx_fl_system ^ PMC_PM(y, z))
2849
2850 state = ctx->ctx_state;
2851 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
2852 is_system = ctx->ctx_fl_system;
2853 task = ctx->ctx_task;
2854 impl_pmds = pmu_conf->impl_pmds[0];
2855
2856 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
2857
2858 if (is_loaded) {
2859 thread = &task->thread;
2860 /*
2861 * In system wide and when the context is loaded, access can only happen
2862 * when the caller is running on the CPU being monitored by the session.
2863 * It does not have to be the owner (ctx_task) of the context per se.
2864 */
2865 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
2866 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
2867 return -EBUSY;
2868 }
2869 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
2870 }
2871 expert_mode = pfm_sysctl.expert_mode;
2872
2873 for (i = 0; i < count; i++, req++) {
2874
2875 cnum = req->reg_num;
2876 reg_flags = req->reg_flags;
2877 value = req->reg_value;
2878 smpl_pmds = req->reg_smpl_pmds[0];
2879 reset_pmds = req->reg_reset_pmds[0];
2880 flags = 0;
2881
2882
2883 if (cnum >= PMU_MAX_PMCS) {
2884 DPRINT(("pmc%u is invalid\n", cnum));
2885 goto error;
2886 }
2887
2888 pmc_type = pmu_conf->pmc_desc[cnum].type;
2889 pmc_pm = (value >> pmu_conf->pmc_desc[cnum].pm_pos) & 0x1;
2890 is_counting = (pmc_type & PFM_REG_COUNTING) == PFM_REG_COUNTING ? 1 : 0;
2891 is_monitor = (pmc_type & PFM_REG_MONITOR) == PFM_REG_MONITOR ? 1 : 0;
2892
2893 /*
2894 * we reject all non implemented PMC as well
2895 * as attempts to modify PMC[0-3] which are used
2896 * as status registers by the PMU
2897 */
2898 if ((pmc_type & PFM_REG_IMPL) == 0 || (pmc_type & PFM_REG_CONTROL) == PFM_REG_CONTROL) {
2899 DPRINT(("pmc%u is unimplemented or no-access pmc_type=%x\n", cnum, pmc_type));
2900 goto error;
2901 }
2902 wr_func = pmu_conf->pmc_desc[cnum].write_check;
2903 /*
2904 * If the PMC is a monitor, then if the value is not the default:
2905 * - system-wide session: PMCx.pm=1 (privileged monitor)
2906 * - per-task : PMCx.pm=0 (user monitor)
2907 */
2908 if (is_monitor && value != PMC_DFL_VAL(cnum) && is_system ^ pmc_pm) {
2909 DPRINT(("pmc%u pmc_pm=%lu is_system=%d\n",
2910 cnum,
2911 pmc_pm,
2912 is_system));
2913 goto error;
2914 }
2915
2916 if (is_counting) {
2917 /*
2918 * enforce generation of overflow interrupt. Necessary on all
2919 * CPUs.
2920 */
2921 value |= 1 << PMU_PMC_OI;
2922
2923 if (reg_flags & PFM_REGFL_OVFL_NOTIFY) {
2924 flags |= PFM_REGFL_OVFL_NOTIFY;
2925 }
2926
2927 if (reg_flags & PFM_REGFL_RANDOM) flags |= PFM_REGFL_RANDOM;
2928
2929 /* verify validity of smpl_pmds */
2930 if ((smpl_pmds & impl_pmds) != smpl_pmds) {
2931 DPRINT(("invalid smpl_pmds 0x%lx for pmc%u\n", smpl_pmds, cnum));
2932 goto error;
2933 }
2934
2935 /* verify validity of reset_pmds */
2936 if ((reset_pmds & impl_pmds) != reset_pmds) {
2937 DPRINT(("invalid reset_pmds 0x%lx for pmc%u\n", reset_pmds, cnum));
2938 goto error;
2939 }
2940 } else {
2941 if (reg_flags & (PFM_REGFL_OVFL_NOTIFY|PFM_REGFL_RANDOM)) {
2942 DPRINT(("cannot set ovfl_notify or random on pmc%u\n", cnum));
2943 goto error;
2944 }
2945 /* eventid on non-counting monitors are ignored */
2946 }
2947
2948 /*
2949 * execute write checker, if any
2950 */
2951 if (likely(expert_mode == 0 && wr_func)) {
2952 ret = (*wr_func)(task, ctx, cnum, &value, regs);
2953 if (ret) goto error;
2954 ret = -EINVAL;
2955 }
2956
2957 /*
2958 * no error on this register
2959 */
2960 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
2961
2962 /*
2963 * Now we commit the changes to the software state
2964 */
2965
2966 /*
2967 * update overflow information
2968 */
2969 if (is_counting) {
2970 /*
2971 * full flag update each time a register is programmed
2972 */
2973 ctx->ctx_pmds[cnum].flags = flags;
2974
2975 ctx->ctx_pmds[cnum].reset_pmds[0] = reset_pmds;
2976 ctx->ctx_pmds[cnum].smpl_pmds[0] = smpl_pmds;
2977 ctx->ctx_pmds[cnum].eventid = req->reg_smpl_eventid;
2978
2979 /*
2980 * Mark all PMDS to be accessed as used.
2981 *
2982 * We do not keep track of PMC because we have to
2983 * systematically restore ALL of them.
2984 *
2985 * We do not update the used_monitors mask, because
2986 * if we have not programmed them, then will be in
2987 * a quiescent state, therefore we will not need to
2988 * mask/restore then when context is MASKED.
2989 */
2990 CTX_USED_PMD(ctx, reset_pmds);
2991 CTX_USED_PMD(ctx, smpl_pmds);
2992 /*
2993 * make sure we do not try to reset on
2994 * restart because we have established new values
2995 */
2996 if (state == PFM_CTX_MASKED) ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
2997 }
2998 /*
2999 * Needed in case the user does not initialize the equivalent
3000 * PMD. Clearing is done indirectly via pfm_reset_pmu_state() so there is no
3001 * possible leak here.
3002 */
3003 CTX_USED_PMD(ctx, pmu_conf->pmc_desc[cnum].dep_pmd[0]);
3004
3005 /*
3006 * keep track of the monitor PMC that we are using.
3007 * we save the value of the pmc in ctx_pmcs[] and if
3008 * the monitoring is not stopped for the context we also
3009 * place it in the saved state area so that it will be
3010 * picked up later by the context switch code.
3011 *
3012 * The value in ctx_pmcs[] can only be changed in pfm_write_pmcs().
3013 *
3014 * The value in thread->pmcs[] may be modified on overflow, i.e., when
3015 * monitoring needs to be stopped.
3016 */
3017 if (is_monitor) CTX_USED_MONITOR(ctx, 1UL << cnum);
3018
3019 /*
3020 * update context state
3021 */
3022 ctx->ctx_pmcs[cnum] = value;
3023
3024 if (is_loaded) {
3025 /*
3026 * write thread state
3027 */
3028 if (is_system == 0) thread->pmcs[cnum] = value;
3029
3030 /*
3031 * write hardware register if we can
3032 */
3033 if (can_access_pmu) {
3034 ia64_set_pmc(cnum, value);
3035 }
3036#ifdef CONFIG_SMP
3037 else {
3038 /*
3039 * per-task SMP only here
3040 *
3041 * we are guaranteed that the task is not running on the other CPU,
3042 * we indicate that this PMD will need to be reloaded if the task
3043 * is rescheduled on the CPU it ran last on.
3044 */
3045 ctx->ctx_reload_pmcs[0] |= 1UL << cnum;
3046 }
3047#endif
3048 }
3049
3050 DPRINT(("pmc[%u]=0x%lx ld=%d apmu=%d flags=0x%x all_pmcs=0x%lx used_pmds=0x%lx eventid=%ld smpl_pmds=0x%lx reset_pmds=0x%lx reloads_pmcs=0x%lx used_monitors=0x%lx ovfl_regs=0x%lx\n",
3051 cnum,
3052 value,
3053 is_loaded,
3054 can_access_pmu,
3055 flags,
3056 ctx->ctx_all_pmcs[0],
3057 ctx->ctx_used_pmds[0],
3058 ctx->ctx_pmds[cnum].eventid,
3059 smpl_pmds,
3060 reset_pmds,
3061 ctx->ctx_reload_pmcs[0],
3062 ctx->ctx_used_monitors[0],
3063 ctx->ctx_ovfl_regs[0]));
3064 }
3065
3066 /*
3067 * make sure the changes are visible
3068 */
3069 if (can_access_pmu) ia64_srlz_d();
3070
3071 return 0;
3072error:
3073 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3074 return ret;
3075}
3076
3077static int
3078pfm_write_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3079{
3080 struct thread_struct *thread = NULL;
3081 struct task_struct *task;
3082 pfarg_reg_t *req = (pfarg_reg_t *)arg;
3083 unsigned long value, hw_value, ovfl_mask;
3084 unsigned int cnum;
3085 int i, can_access_pmu = 0, state;
3086 int is_counting, is_loaded, is_system, expert_mode;
3087 int ret = -EINVAL;
3088 pfm_reg_check_t wr_func;
3089
3090
3091 state = ctx->ctx_state;
3092 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3093 is_system = ctx->ctx_fl_system;
3094 ovfl_mask = pmu_conf->ovfl_val;
3095 task = ctx->ctx_task;
3096
3097 if (unlikely(state == PFM_CTX_ZOMBIE)) return -EINVAL;
3098
3099 /*
3100 * on both UP and SMP, we can only write to the PMC when the task is
3101 * the owner of the local PMU.
3102 */
3103 if (likely(is_loaded)) {
3104 thread = &task->thread;
3105 /*
3106 * In system wide and when the context is loaded, access can only happen
3107 * when the caller is running on the CPU being monitored by the session.
3108 * It does not have to be the owner (ctx_task) of the context per se.
3109 */
3110 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3111 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3112 return -EBUSY;
3113 }
3114 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3115 }
3116 expert_mode = pfm_sysctl.expert_mode;
3117
3118 for (i = 0; i < count; i++, req++) {
3119
3120 cnum = req->reg_num;
3121 value = req->reg_value;
3122
3123 if (!PMD_IS_IMPL(cnum)) {
3124 DPRINT(("pmd[%u] is unimplemented or invalid\n", cnum));
3125 goto abort_mission;
3126 }
3127 is_counting = PMD_IS_COUNTING(cnum);
3128 wr_func = pmu_conf->pmd_desc[cnum].write_check;
3129
3130 /*
3131 * execute write checker, if any
3132 */
3133 if (unlikely(expert_mode == 0 && wr_func)) {
3134 unsigned long v = value;
3135
3136 ret = (*wr_func)(task, ctx, cnum, &v, regs);
3137 if (ret) goto abort_mission;
3138
3139 value = v;
3140 ret = -EINVAL;
3141 }
3142
3143 /*
3144 * no error on this register
3145 */
3146 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
3147
3148 /*
3149 * now commit changes to software state
3150 */
3151 hw_value = value;
3152
3153 /*
3154 * update virtualized (64bits) counter
3155 */
3156 if (is_counting) {
3157 /*
3158 * write context state
3159 */
3160 ctx->ctx_pmds[cnum].lval = value;
3161
3162 /*
3163 * when context is load we use the split value
3164 */
3165 if (is_loaded) {
3166 hw_value = value & ovfl_mask;
3167 value = value & ~ovfl_mask;
3168 }
3169 }
3170 /*
3171 * update reset values (not just for counters)
3172 */
3173 ctx->ctx_pmds[cnum].long_reset = req->reg_long_reset;
3174 ctx->ctx_pmds[cnum].short_reset = req->reg_short_reset;
3175
3176 /*
3177 * update randomization parameters (not just for counters)
3178 */
3179 ctx->ctx_pmds[cnum].seed = req->reg_random_seed;
3180 ctx->ctx_pmds[cnum].mask = req->reg_random_mask;
3181
3182 /*
3183 * update context value
3184 */
3185 ctx->ctx_pmds[cnum].val = value;
3186
3187 /*
3188 * Keep track of what we use
3189 *
3190 * We do not keep track of PMC because we have to
3191 * systematically restore ALL of them.
3192 */
3193 CTX_USED_PMD(ctx, PMD_PMD_DEP(cnum));
3194
3195 /*
3196 * mark this PMD register used as well
3197 */
3198 CTX_USED_PMD(ctx, RDEP(cnum));
3199
3200 /*
3201 * make sure we do not try to reset on
3202 * restart because we have established new values
3203 */
3204 if (is_counting && state == PFM_CTX_MASKED) {
3205 ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
3206 }
3207
3208 if (is_loaded) {
3209 /*
3210 * write thread state
3211 */
3212 if (is_system == 0) thread->pmds[cnum] = hw_value;
3213
3214 /*
3215 * write hardware register if we can
3216 */
3217 if (can_access_pmu) {
3218 ia64_set_pmd(cnum, hw_value);
3219 } else {
3220#ifdef CONFIG_SMP
3221 /*
3222 * we are guaranteed that the task is not running on the other CPU,
3223 * we indicate that this PMD will need to be reloaded if the task
3224 * is rescheduled on the CPU it ran last on.
3225 */
3226 ctx->ctx_reload_pmds[0] |= 1UL << cnum;
3227#endif
3228 }
3229 }
3230
3231 DPRINT(("pmd[%u]=0x%lx ld=%d apmu=%d, hw_value=0x%lx ctx_pmd=0x%lx short_reset=0x%lx "
3232 "long_reset=0x%lx notify=%c seed=0x%lx mask=0x%lx used_pmds=0x%lx reset_pmds=0x%lx reload_pmds=0x%lx all_pmds=0x%lx ovfl_regs=0x%lx\n",
3233 cnum,
3234 value,
3235 is_loaded,
3236 can_access_pmu,
3237 hw_value,
3238 ctx->ctx_pmds[cnum].val,
3239 ctx->ctx_pmds[cnum].short_reset,
3240 ctx->ctx_pmds[cnum].long_reset,
3241 PMC_OVFL_NOTIFY(ctx, cnum) ? 'Y':'N',
3242 ctx->ctx_pmds[cnum].seed,
3243 ctx->ctx_pmds[cnum].mask,
3244 ctx->ctx_used_pmds[0],
3245 ctx->ctx_pmds[cnum].reset_pmds[0],
3246 ctx->ctx_reload_pmds[0],
3247 ctx->ctx_all_pmds[0],
3248 ctx->ctx_ovfl_regs[0]));
3249 }
3250
3251 /*
3252 * make changes visible
3253 */
3254 if (can_access_pmu) ia64_srlz_d();
3255
3256 return 0;
3257
3258abort_mission:
3259 /*
3260 * for now, we have only one possibility for error
3261 */
3262 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3263 return ret;
3264}
3265
3266/*
3267 * By the way of PROTECT_CONTEXT(), interrupts are masked while we are in this function.
3268 * Therefore we know, we do not have to worry about the PMU overflow interrupt. If an
3269 * interrupt is delivered during the call, it will be kept pending until we leave, making
3270 * it appears as if it had been generated at the UNPROTECT_CONTEXT(). At least we are
3271 * guaranteed to return consistent data to the user, it may simply be old. It is not
3272 * trivial to treat the overflow while inside the call because you may end up in
3273 * some module sampling buffer code causing deadlocks.
3274 */
3275static int
3276pfm_read_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3277{
3278 struct thread_struct *thread = NULL;
3279 struct task_struct *task;
3280 unsigned long val = 0UL, lval, ovfl_mask, sval;
3281 pfarg_reg_t *req = (pfarg_reg_t *)arg;
3282 unsigned int cnum, reg_flags = 0;
3283 int i, can_access_pmu = 0, state;
3284 int is_loaded, is_system, is_counting, expert_mode;
3285 int ret = -EINVAL;
3286 pfm_reg_check_t rd_func;
3287
3288 /*
3289 * access is possible when loaded only for
3290 * self-monitoring tasks or in UP mode
3291 */
3292
3293 state = ctx->ctx_state;
3294 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3295 is_system = ctx->ctx_fl_system;
3296 ovfl_mask = pmu_conf->ovfl_val;
3297 task = ctx->ctx_task;
3298
3299 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
3300
3301 if (likely(is_loaded)) {
3302 thread = &task->thread;
3303 /*
3304 * In system wide and when the context is loaded, access can only happen
3305 * when the caller is running on the CPU being monitored by the session.
3306 * It does not have to be the owner (ctx_task) of the context per se.
3307 */
3308 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3309 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3310 return -EBUSY;
3311 }
3312 /*
3313 * this can be true when not self-monitoring only in UP
3314 */
3315 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3316
3317 if (can_access_pmu) ia64_srlz_d();
3318 }
3319 expert_mode = pfm_sysctl.expert_mode;
3320
3321 DPRINT(("ld=%d apmu=%d ctx_state=%d\n",
3322 is_loaded,
3323 can_access_pmu,
3324 state));
3325
3326 /*
3327 * on both UP and SMP, we can only read the PMD from the hardware register when
3328 * the task is the owner of the local PMU.
3329 */
3330
3331 for (i = 0; i < count; i++, req++) {
3332
3333 cnum = req->reg_num;
3334 reg_flags = req->reg_flags;
3335
3336 if (unlikely(!PMD_IS_IMPL(cnum))) goto error;
3337 /*
3338 * we can only read the register that we use. That includes
3339 * the one we explicitely initialize AND the one we want included
3340 * in the sampling buffer (smpl_regs).
3341 *
3342 * Having this restriction allows optimization in the ctxsw routine
3343 * without compromising security (leaks)
3344 */
3345 if (unlikely(!CTX_IS_USED_PMD(ctx, cnum))) goto error;
3346
3347 sval = ctx->ctx_pmds[cnum].val;
3348 lval = ctx->ctx_pmds[cnum].lval;
3349 is_counting = PMD_IS_COUNTING(cnum);
3350
3351 /*
3352 * If the task is not the current one, then we check if the
3353 * PMU state is still in the local live register due to lazy ctxsw.
3354 * If true, then we read directly from the registers.
3355 */
3356 if (can_access_pmu){
3357 val = ia64_get_pmd(cnum);
3358 } else {
3359 /*
3360 * context has been saved
3361 * if context is zombie, then task does not exist anymore.
3362 * In this case, we use the full value saved in the context (pfm_flush_regs()).
3363 */
3364 val = is_loaded ? thread->pmds[cnum] : 0UL;
3365 }
3366 rd_func = pmu_conf->pmd_desc[cnum].read_check;
3367
3368 if (is_counting) {
3369 /*
3370 * XXX: need to check for overflow when loaded
3371 */
3372 val &= ovfl_mask;
3373 val += sval;
3374 }
3375
3376 /*
3377 * execute read checker, if any
3378 */
3379 if (unlikely(expert_mode == 0 && rd_func)) {
3380 unsigned long v = val;
3381 ret = (*rd_func)(ctx->ctx_task, ctx, cnum, &v, regs);
3382 if (ret) goto error;
3383 val = v;
3384 ret = -EINVAL;
3385 }
3386
3387 PFM_REG_RETFLAG_SET(reg_flags, 0);
3388
3389 DPRINT(("pmd[%u]=0x%lx\n", cnum, val));
3390
3391 /*
3392 * update register return value, abort all if problem during copy.
3393 * we only modify the reg_flags field. no check mode is fine because
3394 * access has been verified upfront in sys_perfmonctl().
3395 */
3396 req->reg_value = val;
3397 req->reg_flags = reg_flags;
3398 req->reg_last_reset_val = lval;
3399 }
3400
3401 return 0;
3402
3403error:
3404 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3405 return ret;
3406}
3407
3408int
3409pfm_mod_write_pmcs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3410{
3411 pfm_context_t *ctx;
3412
3413 if (req == NULL) return -EINVAL;
3414
3415 ctx = GET_PMU_CTX();
3416
3417 if (ctx == NULL) return -EINVAL;
3418
3419 /*
3420 * for now limit to current task, which is enough when calling
3421 * from overflow handler
3422 */
3423 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3424
3425 return pfm_write_pmcs(ctx, req, nreq, regs);
3426}
3427EXPORT_SYMBOL(pfm_mod_write_pmcs);
3428
3429int
3430pfm_mod_read_pmds(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3431{
3432 pfm_context_t *ctx;
3433
3434 if (req == NULL) return -EINVAL;
3435
3436 ctx = GET_PMU_CTX();
3437
3438 if (ctx == NULL) return -EINVAL;
3439
3440 /*
3441 * for now limit to current task, which is enough when calling
3442 * from overflow handler
3443 */
3444 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3445
3446 return pfm_read_pmds(ctx, req, nreq, regs);
3447}
3448EXPORT_SYMBOL(pfm_mod_read_pmds);
3449
3450/*
3451 * Only call this function when a process it trying to
3452 * write the debug registers (reading is always allowed)
3453 */
3454int
3455pfm_use_debug_registers(struct task_struct *task)
3456{
3457 pfm_context_t *ctx = task->thread.pfm_context;
3458 unsigned long flags;
3459 int ret = 0;
3460
3461 if (pmu_conf->use_rr_dbregs == 0) return 0;
3462
3463 DPRINT(("called for [%d]\n", task->pid));
3464
3465 /*
3466 * do it only once
3467 */
3468 if (task->thread.flags & IA64_THREAD_DBG_VALID) return 0;
3469
3470 /*
3471 * Even on SMP, we do not need to use an atomic here because
3472 * the only way in is via ptrace() and this is possible only when the
3473 * process is stopped. Even in the case where the ctxsw out is not totally
3474 * completed by the time we come here, there is no way the 'stopped' process
3475 * could be in the middle of fiddling with the pfm_write_ibr_dbr() routine.
3476 * So this is always safe.
3477 */
3478 if (ctx && ctx->ctx_fl_using_dbreg == 1) return -1;
3479
3480 LOCK_PFS(flags);
3481
3482 /*
3483 * We cannot allow setting breakpoints when system wide monitoring
3484 * sessions are using the debug registers.
3485 */
3486 if (pfm_sessions.pfs_sys_use_dbregs> 0)
3487 ret = -1;
3488 else
3489 pfm_sessions.pfs_ptrace_use_dbregs++;
3490
3491 DPRINT(("ptrace_use_dbregs=%u sys_use_dbregs=%u by [%d] ret = %d\n",
3492 pfm_sessions.pfs_ptrace_use_dbregs,
3493 pfm_sessions.pfs_sys_use_dbregs,
3494 task->pid, ret));
3495
3496 UNLOCK_PFS(flags);
3497
3498 return ret;
3499}
3500
3501/*
3502 * This function is called for every task that exits with the
3503 * IA64_THREAD_DBG_VALID set. This indicates a task which was
3504 * able to use the debug registers for debugging purposes via
3505 * ptrace(). Therefore we know it was not using them for
3506 * perfmormance monitoring, so we only decrement the number
3507 * of "ptraced" debug register users to keep the count up to date
3508 */
3509int
3510pfm_release_debug_registers(struct task_struct *task)
3511{
3512 unsigned long flags;
3513 int ret;
3514
3515 if (pmu_conf->use_rr_dbregs == 0) return 0;
3516
3517 LOCK_PFS(flags);
3518 if (pfm_sessions.pfs_ptrace_use_dbregs == 0) {
3519 printk(KERN_ERR "perfmon: invalid release for [%d] ptrace_use_dbregs=0\n", task->pid);
3520 ret = -1;
3521 } else {
3522 pfm_sessions.pfs_ptrace_use_dbregs--;
3523 ret = 0;
3524 }
3525 UNLOCK_PFS(flags);
3526
3527 return ret;
3528}
3529
3530static int
3531pfm_restart(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3532{
3533 struct task_struct *task;
3534 pfm_buffer_fmt_t *fmt;
3535 pfm_ovfl_ctrl_t rst_ctrl;
3536 int state, is_system;
3537 int ret = 0;
3538
3539 state = ctx->ctx_state;
3540 fmt = ctx->ctx_buf_fmt;
3541 is_system = ctx->ctx_fl_system;
3542 task = PFM_CTX_TASK(ctx);
3543
3544 switch(state) {
3545 case PFM_CTX_MASKED:
3546 break;
3547 case PFM_CTX_LOADED:
3548 if (CTX_HAS_SMPL(ctx) && fmt->fmt_restart_active) break;
3549 /* fall through */
3550 case PFM_CTX_UNLOADED:
3551 case PFM_CTX_ZOMBIE:
3552 DPRINT(("invalid state=%d\n", state));
3553 return -EBUSY;
3554 default:
3555 DPRINT(("state=%d, cannot operate (no active_restart handler)\n", state));
3556 return -EINVAL;
3557 }
3558
3559 /*
3560 * In system wide and when the context is loaded, access can only happen
3561 * when the caller is running on the CPU being monitored by the session.
3562 * It does not have to be the owner (ctx_task) of the context per se.
3563 */
3564 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
3565 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3566 return -EBUSY;
3567 }
3568
3569 /* sanity check */
3570 if (unlikely(task == NULL)) {
3571 printk(KERN_ERR "perfmon: [%d] pfm_restart no task\n", current->pid);
3572 return -EINVAL;
3573 }
3574
3575 if (task == current || is_system) {
3576
3577 fmt = ctx->ctx_buf_fmt;
3578
3579 DPRINT(("restarting self %d ovfl=0x%lx\n",
3580 task->pid,
3581 ctx->ctx_ovfl_regs[0]));
3582
3583 if (CTX_HAS_SMPL(ctx)) {
3584
3585 prefetch(ctx->ctx_smpl_hdr);
3586
3587 rst_ctrl.bits.mask_monitoring = 0;
3588 rst_ctrl.bits.reset_ovfl_pmds = 0;
3589
3590 if (state == PFM_CTX_LOADED)
3591 ret = pfm_buf_fmt_restart_active(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
3592 else
3593 ret = pfm_buf_fmt_restart(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
3594 } else {
3595 rst_ctrl.bits.mask_monitoring = 0;
3596 rst_ctrl.bits.reset_ovfl_pmds = 1;
3597 }
3598
3599 if (ret == 0) {
3600 if (rst_ctrl.bits.reset_ovfl_pmds)
3601 pfm_reset_regs(ctx, ctx->ctx_ovfl_regs, PFM_PMD_LONG_RESET);
3602
3603 if (rst_ctrl.bits.mask_monitoring == 0) {
3604 DPRINT(("resuming monitoring for [%d]\n", task->pid));
3605
3606 if (state == PFM_CTX_MASKED) pfm_restore_monitoring(task);
3607 } else {
3608 DPRINT(("keeping monitoring stopped for [%d]\n", task->pid));
3609
3610 // cannot use pfm_stop_monitoring(task, regs);
3611 }
3612 }
3613 /*
3614 * clear overflowed PMD mask to remove any stale information
3615 */
3616 ctx->ctx_ovfl_regs[0] = 0UL;
3617
3618 /*
3619 * back to LOADED state
3620 */
3621 ctx->ctx_state = PFM_CTX_LOADED;
3622
3623 /*
3624 * XXX: not really useful for self monitoring
3625 */
3626 ctx->ctx_fl_can_restart = 0;
3627
3628 return 0;
3629 }
3630
3631 /*
3632 * restart another task
3633 */
3634
3635 /*
3636 * When PFM_CTX_MASKED, we cannot issue a restart before the previous
3637 * one is seen by the task.
3638 */
3639 if (state == PFM_CTX_MASKED) {
3640 if (ctx->ctx_fl_can_restart == 0) return -EINVAL;
3641 /*
3642 * will prevent subsequent restart before this one is
3643 * seen by other task
3644 */
3645 ctx->ctx_fl_can_restart = 0;
3646 }
3647
3648 /*
3649 * if blocking, then post the semaphore is PFM_CTX_MASKED, i.e.
3650 * the task is blocked or on its way to block. That's the normal
3651 * restart path. If the monitoring is not masked, then the task
3652 * can be actively monitoring and we cannot directly intervene.
3653 * Therefore we use the trap mechanism to catch the task and
3654 * force it to reset the buffer/reset PMDs.
3655 *
3656 * if non-blocking, then we ensure that the task will go into
3657 * pfm_handle_work() before returning to user mode.
3658 *
3659 * We cannot explicitely reset another task, it MUST always
3660 * be done by the task itself. This works for system wide because
3661 * the tool that is controlling the session is logically doing
3662 * "self-monitoring".
3663 */
3664 if (CTX_OVFL_NOBLOCK(ctx) == 0 && state == PFM_CTX_MASKED) {
3665 DPRINT(("unblocking [%d] \n", task->pid));
3666 up(&ctx->ctx_restart_sem);
3667 } else {
3668 DPRINT(("[%d] armed exit trap\n", task->pid));
3669
3670 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_RESET;
3671
3672 PFM_SET_WORK_PENDING(task, 1);
3673
3674 pfm_set_task_notify(task);
3675
3676 /*
3677 * XXX: send reschedule if task runs on another CPU
3678 */
3679 }
3680 return 0;
3681}
3682
3683static int
3684pfm_debug(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3685{
3686 unsigned int m = *(unsigned int *)arg;
3687
3688 pfm_sysctl.debug = m == 0 ? 0 : 1;
3689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 printk(KERN_INFO "perfmon debugging %s (timing reset)\n", pfm_sysctl.debug ? "on" : "off");
3691
3692 if (m == 0) {
3693 memset(pfm_stats, 0, sizeof(pfm_stats));
3694 for(m=0; m < NR_CPUS; m++) pfm_stats[m].pfm_ovfl_intr_cycles_min = ~0UL;
3695 }
3696 return 0;
3697}
3698
3699/*
3700 * arg can be NULL and count can be zero for this function
3701 */
3702static int
3703pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3704{
3705 struct thread_struct *thread = NULL;
3706 struct task_struct *task;
3707 pfarg_dbreg_t *req = (pfarg_dbreg_t *)arg;
3708 unsigned long flags;
3709 dbreg_t dbreg;
3710 unsigned int rnum;
3711 int first_time;
3712 int ret = 0, state;
3713 int i, can_access_pmu = 0;
3714 int is_system, is_loaded;
3715
3716 if (pmu_conf->use_rr_dbregs == 0) return -EINVAL;
3717
3718 state = ctx->ctx_state;
3719 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3720 is_system = ctx->ctx_fl_system;
3721 task = ctx->ctx_task;
3722
3723 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
3724
3725 /*
3726 * on both UP and SMP, we can only write to the PMC when the task is
3727 * the owner of the local PMU.
3728 */
3729 if (is_loaded) {
3730 thread = &task->thread;
3731 /*
3732 * In system wide and when the context is loaded, access can only happen
3733 * when the caller is running on the CPU being monitored by the session.
3734 * It does not have to be the owner (ctx_task) of the context per se.
3735 */
3736 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3737 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3738 return -EBUSY;
3739 }
3740 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3741 }
3742
3743 /*
3744 * we do not need to check for ipsr.db because we do clear ibr.x, dbr.r, and dbr.w
3745 * ensuring that no real breakpoint can be installed via this call.
3746 *
3747 * IMPORTANT: regs can be NULL in this function
3748 */
3749
3750 first_time = ctx->ctx_fl_using_dbreg == 0;
3751
3752 /*
3753 * don't bother if we are loaded and task is being debugged
3754 */
3755 if (is_loaded && (thread->flags & IA64_THREAD_DBG_VALID) != 0) {
3756 DPRINT(("debug registers already in use for [%d]\n", task->pid));
3757 return -EBUSY;
3758 }
3759
3760 /*
3761 * check for debug registers in system wide mode
3762 *
3763 * If though a check is done in pfm_context_load(),
3764 * we must repeat it here, in case the registers are
3765 * written after the context is loaded
3766 */
3767 if (is_loaded) {
3768 LOCK_PFS(flags);
3769
3770 if (first_time && is_system) {
3771 if (pfm_sessions.pfs_ptrace_use_dbregs)
3772 ret = -EBUSY;
3773 else
3774 pfm_sessions.pfs_sys_use_dbregs++;
3775 }
3776 UNLOCK_PFS(flags);
3777 }
3778
3779 if (ret != 0) return ret;
3780
3781 /*
3782 * mark ourself as user of the debug registers for
3783 * perfmon purposes.
3784 */
3785 ctx->ctx_fl_using_dbreg = 1;
3786
3787 /*
3788 * clear hardware registers to make sure we don't
3789 * pick up stale state.
3790 *
3791 * for a system wide session, we do not use
3792 * thread.dbr, thread.ibr because this process
3793 * never leaves the current CPU and the state
3794 * is shared by all processes running on it
3795 */
3796 if (first_time && can_access_pmu) {
3797 DPRINT(("[%d] clearing ibrs, dbrs\n", task->pid));
3798 for (i=0; i < pmu_conf->num_ibrs; i++) {
3799 ia64_set_ibr(i, 0UL);
3800 ia64_dv_serialize_instruction();
3801 }
3802 ia64_srlz_i();
3803 for (i=0; i < pmu_conf->num_dbrs; i++) {
3804 ia64_set_dbr(i, 0UL);
3805 ia64_dv_serialize_data();
3806 }
3807 ia64_srlz_d();
3808 }
3809
3810 /*
3811 * Now install the values into the registers
3812 */
3813 for (i = 0; i < count; i++, req++) {
3814
3815 rnum = req->dbreg_num;
3816 dbreg.val = req->dbreg_value;
3817
3818 ret = -EINVAL;
3819
3820 if ((mode == PFM_CODE_RR && rnum >= PFM_NUM_IBRS) || ((mode == PFM_DATA_RR) && rnum >= PFM_NUM_DBRS)) {
3821 DPRINT(("invalid register %u val=0x%lx mode=%d i=%d count=%d\n",
3822 rnum, dbreg.val, mode, i, count));
3823
3824 goto abort_mission;
3825 }
3826
3827 /*
3828 * make sure we do not install enabled breakpoint
3829 */
3830 if (rnum & 0x1) {
3831 if (mode == PFM_CODE_RR)
3832 dbreg.ibr.ibr_x = 0;
3833 else
3834 dbreg.dbr.dbr_r = dbreg.dbr.dbr_w = 0;
3835 }
3836
3837 PFM_REG_RETFLAG_SET(req->dbreg_flags, 0);
3838
3839 /*
3840 * Debug registers, just like PMC, can only be modified
3841 * by a kernel call. Moreover, perfmon() access to those
3842 * registers are centralized in this routine. The hardware
3843 * does not modify the value of these registers, therefore,
3844 * if we save them as they are written, we can avoid having
3845 * to save them on context switch out. This is made possible
3846 * by the fact that when perfmon uses debug registers, ptrace()
3847 * won't be able to modify them concurrently.
3848 */
3849 if (mode == PFM_CODE_RR) {
3850 CTX_USED_IBR(ctx, rnum);
3851
3852 if (can_access_pmu) {
3853 ia64_set_ibr(rnum, dbreg.val);
3854 ia64_dv_serialize_instruction();
3855 }
3856
3857 ctx->ctx_ibrs[rnum] = dbreg.val;
3858
3859 DPRINT(("write ibr%u=0x%lx used_ibrs=0x%x ld=%d apmu=%d\n",
3860 rnum, dbreg.val, ctx->ctx_used_ibrs[0], is_loaded, can_access_pmu));
3861 } else {
3862 CTX_USED_DBR(ctx, rnum);
3863
3864 if (can_access_pmu) {
3865 ia64_set_dbr(rnum, dbreg.val);
3866 ia64_dv_serialize_data();
3867 }
3868 ctx->ctx_dbrs[rnum] = dbreg.val;
3869
3870 DPRINT(("write dbr%u=0x%lx used_dbrs=0x%x ld=%d apmu=%d\n",
3871 rnum, dbreg.val, ctx->ctx_used_dbrs[0], is_loaded, can_access_pmu));
3872 }
3873 }
3874
3875 return 0;
3876
3877abort_mission:
3878 /*
3879 * in case it was our first attempt, we undo the global modifications
3880 */
3881 if (first_time) {
3882 LOCK_PFS(flags);
3883 if (ctx->ctx_fl_system) {
3884 pfm_sessions.pfs_sys_use_dbregs--;
3885 }
3886 UNLOCK_PFS(flags);
3887 ctx->ctx_fl_using_dbreg = 0;
3888 }
3889 /*
3890 * install error return flag
3891 */
3892 PFM_REG_RETFLAG_SET(req->dbreg_flags, PFM_REG_RETFL_EINVAL);
3893
3894 return ret;
3895}
3896
3897static int
3898pfm_write_ibrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3899{
3900 return pfm_write_ibr_dbr(PFM_CODE_RR, ctx, arg, count, regs);
3901}
3902
3903static int
3904pfm_write_dbrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3905{
3906 return pfm_write_ibr_dbr(PFM_DATA_RR, ctx, arg, count, regs);
3907}
3908
3909int
3910pfm_mod_write_ibrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3911{
3912 pfm_context_t *ctx;
3913
3914 if (req == NULL) return -EINVAL;
3915
3916 ctx = GET_PMU_CTX();
3917
3918 if (ctx == NULL) return -EINVAL;
3919
3920 /*
3921 * for now limit to current task, which is enough when calling
3922 * from overflow handler
3923 */
3924 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3925
3926 return pfm_write_ibrs(ctx, req, nreq, regs);
3927}
3928EXPORT_SYMBOL(pfm_mod_write_ibrs);
3929
3930int
3931pfm_mod_write_dbrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3932{
3933 pfm_context_t *ctx;
3934
3935 if (req == NULL) return -EINVAL;
3936
3937 ctx = GET_PMU_CTX();
3938
3939 if (ctx == NULL) return -EINVAL;
3940
3941 /*
3942 * for now limit to current task, which is enough when calling
3943 * from overflow handler
3944 */
3945 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3946
3947 return pfm_write_dbrs(ctx, req, nreq, regs);
3948}
3949EXPORT_SYMBOL(pfm_mod_write_dbrs);
3950
3951
3952static int
3953pfm_get_features(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3954{
3955 pfarg_features_t *req = (pfarg_features_t *)arg;
3956
3957 req->ft_version = PFM_VERSION;
3958 return 0;
3959}
3960
3961static int
3962pfm_stop(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3963{
3964 struct pt_regs *tregs;
3965 struct task_struct *task = PFM_CTX_TASK(ctx);
3966 int state, is_system;
3967
3968 state = ctx->ctx_state;
3969 is_system = ctx->ctx_fl_system;
3970
3971 /*
3972 * context must be attached to issue the stop command (includes LOADED,MASKED,ZOMBIE)
3973 */
3974 if (state == PFM_CTX_UNLOADED) return -EINVAL;
3975
3976 /*
3977 * In system wide and when the context is loaded, access can only happen
3978 * when the caller is running on the CPU being monitored by the session.
3979 * It does not have to be the owner (ctx_task) of the context per se.
3980 */
3981 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
3982 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3983 return -EBUSY;
3984 }
3985 DPRINT(("task [%d] ctx_state=%d is_system=%d\n",
3986 PFM_CTX_TASK(ctx)->pid,
3987 state,
3988 is_system));
3989 /*
3990 * in system mode, we need to update the PMU directly
3991 * and the user level state of the caller, which may not
3992 * necessarily be the creator of the context.
3993 */
3994 if (is_system) {
3995 /*
3996 * Update local PMU first
3997 *
3998 * disable dcr pp
3999 */
4000 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
4001 ia64_srlz_i();
4002
4003 /*
4004 * update local cpuinfo
4005 */
4006 PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
4007
4008 /*
4009 * stop monitoring, does srlz.i
4010 */
4011 pfm_clear_psr_pp();
4012
4013 /*
4014 * stop monitoring in the caller
4015 */
4016 ia64_psr(regs)->pp = 0;
4017
4018 return 0;
4019 }
4020 /*
4021 * per-task mode
4022 */
4023
4024 if (task == current) {
4025 /* stop monitoring at kernel level */
4026 pfm_clear_psr_up();
4027
4028 /*
4029 * stop monitoring at the user level
4030 */
4031 ia64_psr(regs)->up = 0;
4032 } else {
4033 tregs = ia64_task_regs(task);
4034
4035 /*
4036 * stop monitoring at the user level
4037 */
4038 ia64_psr(tregs)->up = 0;
4039
4040 /*
4041 * monitoring disabled in kernel at next reschedule
4042 */
4043 ctx->ctx_saved_psr_up = 0;
4044 DPRINT(("task=[%d]\n", task->pid));
4045 }
4046 return 0;
4047}
4048
4049
4050static int
4051pfm_start(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4052{
4053 struct pt_regs *tregs;
4054 int state, is_system;
4055
4056 state = ctx->ctx_state;
4057 is_system = ctx->ctx_fl_system;
4058
4059 if (state != PFM_CTX_LOADED) return -EINVAL;
4060
4061 /*
4062 * In system wide and when the context is loaded, access can only happen
4063 * when the caller is running on the CPU being monitored by the session.
4064 * It does not have to be the owner (ctx_task) of the context per se.
4065 */
4066 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
4067 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
4068 return -EBUSY;
4069 }
4070
4071 /*
4072 * in system mode, we need to update the PMU directly
4073 * and the user level state of the caller, which may not
4074 * necessarily be the creator of the context.
4075 */
4076 if (is_system) {
4077
4078 /*
4079 * set user level psr.pp for the caller
4080 */
4081 ia64_psr(regs)->pp = 1;
4082
4083 /*
4084 * now update the local PMU and cpuinfo
4085 */
4086 PFM_CPUINFO_SET(PFM_CPUINFO_DCR_PP);
4087
4088 /*
4089 * start monitoring at kernel level
4090 */
4091 pfm_set_psr_pp();
4092
4093 /* enable dcr pp */
4094 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
4095 ia64_srlz_i();
4096
4097 return 0;
4098 }
4099
4100 /*
4101 * per-process mode
4102 */
4103
4104 if (ctx->ctx_task == current) {
4105
4106 /* start monitoring at kernel level */
4107 pfm_set_psr_up();
4108
4109 /*
4110 * activate monitoring at user level
4111 */
4112 ia64_psr(regs)->up = 1;
4113
4114 } else {
4115 tregs = ia64_task_regs(ctx->ctx_task);
4116
4117 /*
4118 * start monitoring at the kernel level the next
4119 * time the task is scheduled
4120 */
4121 ctx->ctx_saved_psr_up = IA64_PSR_UP;
4122
4123 /*
4124 * activate monitoring at user level
4125 */
4126 ia64_psr(tregs)->up = 1;
4127 }
4128 return 0;
4129}
4130
4131static int
4132pfm_get_pmc_reset(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4133{
4134 pfarg_reg_t *req = (pfarg_reg_t *)arg;
4135 unsigned int cnum;
4136 int i;
4137 int ret = -EINVAL;
4138
4139 for (i = 0; i < count; i++, req++) {
4140
4141 cnum = req->reg_num;
4142
4143 if (!PMC_IS_IMPL(cnum)) goto abort_mission;
4144
4145 req->reg_value = PMC_DFL_VAL(cnum);
4146
4147 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
4148
4149 DPRINT(("pmc_reset_val pmc[%u]=0x%lx\n", cnum, req->reg_value));
4150 }
4151 return 0;
4152
4153abort_mission:
4154 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
4155 return ret;
4156}
4157
4158static int
4159pfm_check_task_exist(pfm_context_t *ctx)
4160{
4161 struct task_struct *g, *t;
4162 int ret = -ESRCH;
4163
4164 read_lock(&tasklist_lock);
4165
4166 do_each_thread (g, t) {
4167 if (t->thread.pfm_context == ctx) {
4168 ret = 0;
4169 break;
4170 }
4171 } while_each_thread (g, t);
4172
4173 read_unlock(&tasklist_lock);
4174
4175 DPRINT(("pfm_check_task_exist: ret=%d ctx=%p\n", ret, ctx));
4176
4177 return ret;
4178}
4179
4180static int
4181pfm_context_load(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4182{
4183 struct task_struct *task;
4184 struct thread_struct *thread;
4185 struct pfm_context_t *old;
4186 unsigned long flags;
4187#ifndef CONFIG_SMP
4188 struct task_struct *owner_task = NULL;
4189#endif
4190 pfarg_load_t *req = (pfarg_load_t *)arg;
4191 unsigned long *pmcs_source, *pmds_source;
4192 int the_cpu;
4193 int ret = 0;
4194 int state, is_system, set_dbregs = 0;
4195
4196 state = ctx->ctx_state;
4197 is_system = ctx->ctx_fl_system;
4198 /*
4199 * can only load from unloaded or terminated state
4200 */
4201 if (state != PFM_CTX_UNLOADED) {
4202 DPRINT(("cannot load to [%d], invalid ctx_state=%d\n",
4203 req->load_pid,
4204 ctx->ctx_state));
4205 return -EINVAL;
4206 }
4207
4208 DPRINT(("load_pid [%d] using_dbreg=%d\n", req->load_pid, ctx->ctx_fl_using_dbreg));
4209
4210 if (CTX_OVFL_NOBLOCK(ctx) == 0 && req->load_pid == current->pid) {
4211 DPRINT(("cannot use blocking mode on self\n"));
4212 return -EINVAL;
4213 }
4214
4215 ret = pfm_get_task(ctx, req->load_pid, &task);
4216 if (ret) {
4217 DPRINT(("load_pid [%d] get_task=%d\n", req->load_pid, ret));
4218 return ret;
4219 }
4220
4221 ret = -EINVAL;
4222
4223 /*
4224 * system wide is self monitoring only
4225 */
4226 if (is_system && task != current) {
4227 DPRINT(("system wide is self monitoring only load_pid=%d\n",
4228 req->load_pid));
4229 goto error;
4230 }
4231
4232 thread = &task->thread;
4233
4234 ret = 0;
4235 /*
4236 * cannot load a context which is using range restrictions,
4237 * into a task that is being debugged.
4238 */
4239 if (ctx->ctx_fl_using_dbreg) {
4240 if (thread->flags & IA64_THREAD_DBG_VALID) {
4241 ret = -EBUSY;
4242 DPRINT(("load_pid [%d] task is debugged, cannot load range restrictions\n", req->load_pid));
4243 goto error;
4244 }
4245 LOCK_PFS(flags);
4246
4247 if (is_system) {
4248 if (pfm_sessions.pfs_ptrace_use_dbregs) {
4249 DPRINT(("cannot load [%d] dbregs in use\n", task->pid));
4250 ret = -EBUSY;
4251 } else {
4252 pfm_sessions.pfs_sys_use_dbregs++;
4253 DPRINT(("load [%d] increased sys_use_dbreg=%u\n", task->pid, pfm_sessions.pfs_sys_use_dbregs));
4254 set_dbregs = 1;
4255 }
4256 }
4257
4258 UNLOCK_PFS(flags);
4259
4260 if (ret) goto error;
4261 }
4262
4263 /*
4264 * SMP system-wide monitoring implies self-monitoring.
4265 *
4266 * The programming model expects the task to
4267 * be pinned on a CPU throughout the session.
4268 * Here we take note of the current CPU at the
4269 * time the context is loaded. No call from
4270 * another CPU will be allowed.
4271 *
4272 * The pinning via shed_setaffinity()
4273 * must be done by the calling task prior
4274 * to this call.
4275 *
4276 * systemwide: keep track of CPU this session is supposed to run on
4277 */
4278 the_cpu = ctx->ctx_cpu = smp_processor_id();
4279
4280 ret = -EBUSY;
4281 /*
4282 * now reserve the session
4283 */
4284 ret = pfm_reserve_session(current, is_system, the_cpu);
4285 if (ret) goto error;
4286
4287 /*
4288 * task is necessarily stopped at this point.
4289 *
4290 * If the previous context was zombie, then it got removed in
4291 * pfm_save_regs(). Therefore we should not see it here.
4292 * If we see a context, then this is an active context
4293 *
4294 * XXX: needs to be atomic
4295 */
4296 DPRINT(("before cmpxchg() old_ctx=%p new_ctx=%p\n",
4297 thread->pfm_context, ctx));
4298
4299 old = ia64_cmpxchg(acq, &thread->pfm_context, NULL, ctx, sizeof(pfm_context_t *));
4300 if (old != NULL) {
4301 DPRINT(("load_pid [%d] already has a context\n", req->load_pid));
4302 goto error_unres;
4303 }
4304
4305 pfm_reset_msgq(ctx);
4306
4307 ctx->ctx_state = PFM_CTX_LOADED;
4308
4309 /*
4310 * link context to task
4311 */
4312 ctx->ctx_task = task;
4313
4314 if (is_system) {
4315 /*
4316 * we load as stopped
4317 */
4318 PFM_CPUINFO_SET(PFM_CPUINFO_SYST_WIDE);
4319 PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
4320
4321 if (ctx->ctx_fl_excl_idle) PFM_CPUINFO_SET(PFM_CPUINFO_EXCL_IDLE);
4322 } else {
4323 thread->flags |= IA64_THREAD_PM_VALID;
4324 }
4325
4326 /*
4327 * propagate into thread-state
4328 */
4329 pfm_copy_pmds(task, ctx);
4330 pfm_copy_pmcs(task, ctx);
4331
4332 pmcs_source = thread->pmcs;
4333 pmds_source = thread->pmds;
4334
4335 /*
4336 * always the case for system-wide
4337 */
4338 if (task == current) {
4339
4340 if (is_system == 0) {
4341
4342 /* allow user level control */
4343 ia64_psr(regs)->sp = 0;
4344 DPRINT(("clearing psr.sp for [%d]\n", task->pid));
4345
4346 SET_LAST_CPU(ctx, smp_processor_id());
4347 INC_ACTIVATION();
4348 SET_ACTIVATION(ctx);
4349#ifndef CONFIG_SMP
4350 /*
4351 * push the other task out, if any
4352 */
4353 owner_task = GET_PMU_OWNER();
4354 if (owner_task) pfm_lazy_save_regs(owner_task);
4355#endif
4356 }
4357 /*
4358 * load all PMD from ctx to PMU (as opposed to thread state)
4359 * restore all PMC from ctx to PMU
4360 */
4361 pfm_restore_pmds(pmds_source, ctx->ctx_all_pmds[0]);
4362 pfm_restore_pmcs(pmcs_source, ctx->ctx_all_pmcs[0]);
4363
4364 ctx->ctx_reload_pmcs[0] = 0UL;
4365 ctx->ctx_reload_pmds[0] = 0UL;
4366
4367 /*
4368 * guaranteed safe by earlier check against DBG_VALID
4369 */
4370 if (ctx->ctx_fl_using_dbreg) {
4371 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
4372 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
4373 }
4374 /*
4375 * set new ownership
4376 */
4377 SET_PMU_OWNER(task, ctx);
4378
4379 DPRINT(("context loaded on PMU for [%d]\n", task->pid));
4380 } else {
4381 /*
4382 * when not current, task MUST be stopped, so this is safe
4383 */
4384 regs = ia64_task_regs(task);
4385
4386 /* force a full reload */
4387 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
4388 SET_LAST_CPU(ctx, -1);
4389
4390 /* initial saved psr (stopped) */
4391 ctx->ctx_saved_psr_up = 0UL;
4392 ia64_psr(regs)->up = ia64_psr(regs)->pp = 0;
4393 }
4394
4395 ret = 0;
4396
4397error_unres:
4398 if (ret) pfm_unreserve_session(ctx, ctx->ctx_fl_system, the_cpu);
4399error:
4400 /*
4401 * we must undo the dbregs setting (for system-wide)
4402 */
4403 if (ret && set_dbregs) {
4404 LOCK_PFS(flags);
4405 pfm_sessions.pfs_sys_use_dbregs--;
4406 UNLOCK_PFS(flags);
4407 }
4408 /*
4409 * release task, there is now a link with the context
4410 */
4411 if (is_system == 0 && task != current) {
4412 pfm_put_task(task);
4413
4414 if (ret == 0) {
4415 ret = pfm_check_task_exist(ctx);
4416 if (ret) {
4417 ctx->ctx_state = PFM_CTX_UNLOADED;
4418 ctx->ctx_task = NULL;
4419 }
4420 }
4421 }
4422 return ret;
4423}
4424
4425/*
4426 * in this function, we do not need to increase the use count
4427 * for the task via get_task_struct(), because we hold the
4428 * context lock. If the task were to disappear while having
4429 * a context attached, it would go through pfm_exit_thread()
4430 * which also grabs the context lock and would therefore be blocked
4431 * until we are here.
4432 */
4433static void pfm_flush_pmds(struct task_struct *, pfm_context_t *ctx);
4434
4435static int
4436pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4437{
4438 struct task_struct *task = PFM_CTX_TASK(ctx);
4439 struct pt_regs *tregs;
4440 int prev_state, is_system;
4441 int ret;
4442
4443 DPRINT(("ctx_state=%d task [%d]\n", ctx->ctx_state, task ? task->pid : -1));
4444
4445 prev_state = ctx->ctx_state;
4446 is_system = ctx->ctx_fl_system;
4447
4448 /*
4449 * unload only when necessary
4450 */
4451 if (prev_state == PFM_CTX_UNLOADED) {
4452 DPRINT(("ctx_state=%d, nothing to do\n", prev_state));
4453 return 0;
4454 }
4455
4456 /*
4457 * clear psr and dcr bits
4458 */
4459 ret = pfm_stop(ctx, NULL, 0, regs);
4460 if (ret) return ret;
4461
4462 ctx->ctx_state = PFM_CTX_UNLOADED;
4463
4464 /*
4465 * in system mode, we need to update the PMU directly
4466 * and the user level state of the caller, which may not
4467 * necessarily be the creator of the context.
4468 */
4469 if (is_system) {
4470
4471 /*
4472 * Update cpuinfo
4473 *
4474 * local PMU is taken care of in pfm_stop()
4475 */
4476 PFM_CPUINFO_CLEAR(PFM_CPUINFO_SYST_WIDE);
4477 PFM_CPUINFO_CLEAR(PFM_CPUINFO_EXCL_IDLE);
4478
4479 /*
4480 * save PMDs in context
4481 * release ownership
4482 */
4483 pfm_flush_pmds(current, ctx);
4484
4485 /*
4486 * at this point we are done with the PMU
4487 * so we can unreserve the resource.
4488 */
4489 if (prev_state != PFM_CTX_ZOMBIE)
4490 pfm_unreserve_session(ctx, 1 , ctx->ctx_cpu);
4491
4492 /*
4493 * disconnect context from task
4494 */
4495 task->thread.pfm_context = NULL;
4496 /*
4497 * disconnect task from context
4498 */
4499 ctx->ctx_task = NULL;
4500
4501 /*
4502 * There is nothing more to cleanup here.
4503 */
4504 return 0;
4505 }
4506
4507 /*
4508 * per-task mode
4509 */
4510 tregs = task == current ? regs : ia64_task_regs(task);
4511
4512 if (task == current) {
4513 /*
4514 * cancel user level control
4515 */
4516 ia64_psr(regs)->sp = 1;
4517
4518 DPRINT(("setting psr.sp for [%d]\n", task->pid));
4519 }
4520 /*
4521 * save PMDs to context
4522 * release ownership
4523 */
4524 pfm_flush_pmds(task, ctx);
4525
4526 /*
4527 * at this point we are done with the PMU
4528 * so we can unreserve the resource.
4529 *
4530 * when state was ZOMBIE, we have already unreserved.
4531 */
4532 if (prev_state != PFM_CTX_ZOMBIE)
4533 pfm_unreserve_session(ctx, 0 , ctx->ctx_cpu);
4534
4535 /*
4536 * reset activation counter and psr
4537 */
4538 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
4539 SET_LAST_CPU(ctx, -1);
4540
4541 /*
4542 * PMU state will not be restored
4543 */
4544 task->thread.flags &= ~IA64_THREAD_PM_VALID;
4545
4546 /*
4547 * break links between context and task
4548 */
4549 task->thread.pfm_context = NULL;
4550 ctx->ctx_task = NULL;
4551
4552 PFM_SET_WORK_PENDING(task, 0);
4553
4554 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
4555 ctx->ctx_fl_can_restart = 0;
4556 ctx->ctx_fl_going_zombie = 0;
4557
4558 DPRINT(("disconnected [%d] from context\n", task->pid));
4559
4560 return 0;
4561}
4562
4563
4564/*
4565 * called only from exit_thread(): task == current
4566 * we come here only if current has a context attached (loaded or masked)
4567 */
4568void
4569pfm_exit_thread(struct task_struct *task)
4570{
4571 pfm_context_t *ctx;
4572 unsigned long flags;
4573 struct pt_regs *regs = ia64_task_regs(task);
4574 int ret, state;
4575 int free_ok = 0;
4576
4577 ctx = PFM_GET_CTX(task);
4578
4579 PROTECT_CTX(ctx, flags);
4580
4581 DPRINT(("state=%d task [%d]\n", ctx->ctx_state, task->pid));
4582
4583 state = ctx->ctx_state;
4584 switch(state) {
4585 case PFM_CTX_UNLOADED:
4586 /*
4587 * only comes to thios function if pfm_context is not NULL, i.e., cannot
4588 * be in unloaded state
4589 */
4590 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] ctx unloaded\n", task->pid);
4591 break;
4592 case PFM_CTX_LOADED:
4593 case PFM_CTX_MASKED:
4594 ret = pfm_context_unload(ctx, NULL, 0, regs);
4595 if (ret) {
4596 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task->pid, state, ret);
4597 }
4598 DPRINT(("ctx unloaded for current state was %d\n", state));
4599
4600 pfm_end_notify_user(ctx);
4601 break;
4602 case PFM_CTX_ZOMBIE:
4603 ret = pfm_context_unload(ctx, NULL, 0, regs);
4604 if (ret) {
4605 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task->pid, state, ret);
4606 }
4607 free_ok = 1;
4608 break;
4609 default:
4610 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] unexpected state=%d\n", task->pid, state);
4611 break;
4612 }
4613 UNPROTECT_CTX(ctx, flags);
4614
4615 { u64 psr = pfm_get_psr();
4616 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
4617 BUG_ON(GET_PMU_OWNER());
4618 BUG_ON(ia64_psr(regs)->up);
4619 BUG_ON(ia64_psr(regs)->pp);
4620 }
4621
4622 /*
4623 * All memory free operations (especially for vmalloc'ed memory)
4624 * MUST be done with interrupts ENABLED.
4625 */
4626 if (free_ok) pfm_context_free(ctx);
4627}
4628
4629/*
4630 * functions MUST be listed in the increasing order of their index (see permfon.h)
4631 */
4632#define PFM_CMD(name, flags, arg_count, arg_type, getsz) { name, #name, flags, arg_count, sizeof(arg_type), getsz }
4633#define PFM_CMD_S(name, flags) { name, #name, flags, 0, 0, NULL }
4634#define PFM_CMD_PCLRWS (PFM_CMD_FD|PFM_CMD_ARG_RW|PFM_CMD_STOP)
4635#define PFM_CMD_PCLRW (PFM_CMD_FD|PFM_CMD_ARG_RW)
4636#define PFM_CMD_NONE { NULL, "no-cmd", 0, 0, 0, NULL}
4637
4638static pfm_cmd_desc_t pfm_cmd_tab[]={
4639/* 0 */PFM_CMD_NONE,
4640/* 1 */PFM_CMD(pfm_write_pmcs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4641/* 2 */PFM_CMD(pfm_write_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4642/* 3 */PFM_CMD(pfm_read_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4643/* 4 */PFM_CMD_S(pfm_stop, PFM_CMD_PCLRWS),
4644/* 5 */PFM_CMD_S(pfm_start, PFM_CMD_PCLRWS),
4645/* 6 */PFM_CMD_NONE,
4646/* 7 */PFM_CMD_NONE,
4647/* 8 */PFM_CMD(pfm_context_create, PFM_CMD_ARG_RW, 1, pfarg_context_t, pfm_ctx_getsize),
4648/* 9 */PFM_CMD_NONE,
4649/* 10 */PFM_CMD_S(pfm_restart, PFM_CMD_PCLRW),
4650/* 11 */PFM_CMD_NONE,
4651/* 12 */PFM_CMD(pfm_get_features, PFM_CMD_ARG_RW, 1, pfarg_features_t, NULL),
4652/* 13 */PFM_CMD(pfm_debug, 0, 1, unsigned int, NULL),
4653/* 14 */PFM_CMD_NONE,
4654/* 15 */PFM_CMD(pfm_get_pmc_reset, PFM_CMD_ARG_RW, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4655/* 16 */PFM_CMD(pfm_context_load, PFM_CMD_PCLRWS, 1, pfarg_load_t, NULL),
4656/* 17 */PFM_CMD_S(pfm_context_unload, PFM_CMD_PCLRWS),
4657/* 18 */PFM_CMD_NONE,
4658/* 19 */PFM_CMD_NONE,
4659/* 20 */PFM_CMD_NONE,
4660/* 21 */PFM_CMD_NONE,
4661/* 22 */PFM_CMD_NONE,
4662/* 23 */PFM_CMD_NONE,
4663/* 24 */PFM_CMD_NONE,
4664/* 25 */PFM_CMD_NONE,
4665/* 26 */PFM_CMD_NONE,
4666/* 27 */PFM_CMD_NONE,
4667/* 28 */PFM_CMD_NONE,
4668/* 29 */PFM_CMD_NONE,
4669/* 30 */PFM_CMD_NONE,
4670/* 31 */PFM_CMD_NONE,
4671/* 32 */PFM_CMD(pfm_write_ibrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL),
4672/* 33 */PFM_CMD(pfm_write_dbrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL)
4673};
4674#define PFM_CMD_COUNT (sizeof(pfm_cmd_tab)/sizeof(pfm_cmd_desc_t))
4675
4676static int
4677pfm_check_task_state(pfm_context_t *ctx, int cmd, unsigned long flags)
4678{
4679 struct task_struct *task;
4680 int state, old_state;
4681
4682recheck:
4683 state = ctx->ctx_state;
4684 task = ctx->ctx_task;
4685
4686 if (task == NULL) {
4687 DPRINT(("context %d no task, state=%d\n", ctx->ctx_fd, state));
4688 return 0;
4689 }
4690
4691 DPRINT(("context %d state=%d [%d] task_state=%ld must_stop=%d\n",
4692 ctx->ctx_fd,
4693 state,
4694 task->pid,
4695 task->state, PFM_CMD_STOPPED(cmd)));
4696
4697 /*
4698 * self-monitoring always ok.
4699 *
4700 * for system-wide the caller can either be the creator of the
4701 * context (to one to which the context is attached to) OR
4702 * a task running on the same CPU as the session.
4703 */
4704 if (task == current || ctx->ctx_fl_system) return 0;
4705
4706 /*
4707 * if context is UNLOADED we are safe to go
4708 */
4709 if (state == PFM_CTX_UNLOADED) return 0;
4710
4711 /*
4712 * no command can operate on a zombie context
4713 */
4714 if (state == PFM_CTX_ZOMBIE) {
4715 DPRINT(("cmd %d state zombie cannot operate on context\n", cmd));
4716 return -EINVAL;
4717 }
4718
4719 /*
4720 * context is LOADED or MASKED. Some commands may need to have
4721 * the task stopped.
4722 *
4723 * We could lift this restriction for UP but it would mean that
4724 * the user has no guarantee the task would not run between
4725 * two successive calls to perfmonctl(). That's probably OK.
4726 * If this user wants to ensure the task does not run, then
4727 * the task must be stopped.
4728 */
4729 if (PFM_CMD_STOPPED(cmd)) {
4730 if ((task->state != TASK_STOPPED) && (task->state != TASK_TRACED)) {
4731 DPRINT(("[%d] task not in stopped state\n", task->pid));
4732 return -EBUSY;
4733 }
4734 /*
4735 * task is now stopped, wait for ctxsw out
4736 *
4737 * This is an interesting point in the code.
4738 * We need to unprotect the context because
4739 * the pfm_save_regs() routines needs to grab
4740 * the same lock. There are danger in doing
4741 * this because it leaves a window open for
4742 * another task to get access to the context
4743 * and possibly change its state. The one thing
4744 * that is not possible is for the context to disappear
4745 * because we are protected by the VFS layer, i.e.,
4746 * get_fd()/put_fd().
4747 */
4748 old_state = state;
4749
4750 UNPROTECT_CTX(ctx, flags);
4751
4752 wait_task_inactive(task);
4753
4754 PROTECT_CTX(ctx, flags);
4755
4756 /*
4757 * we must recheck to verify if state has changed
4758 */
4759 if (ctx->ctx_state != old_state) {
4760 DPRINT(("old_state=%d new_state=%d\n", old_state, ctx->ctx_state));
4761 goto recheck;
4762 }
4763 }
4764 return 0;
4765}
4766
4767/*
4768 * system-call entry point (must return long)
4769 */
4770asmlinkage long
4771sys_perfmonctl (int fd, int cmd, void __user *arg, int count)
4772{
4773 struct file *file = NULL;
4774 pfm_context_t *ctx = NULL;
4775 unsigned long flags = 0UL;
4776 void *args_k = NULL;
4777 long ret; /* will expand int return types */
4778 size_t base_sz, sz, xtra_sz = 0;
4779 int narg, completed_args = 0, call_made = 0, cmd_flags;
4780 int (*func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
4781 int (*getsize)(void *arg, size_t *sz);
4782#define PFM_MAX_ARGSIZE 4096
4783
4784 /*
4785 * reject any call if perfmon was disabled at initialization
4786 */
4787 if (unlikely(pmu_conf == NULL)) return -ENOSYS;
4788
4789 if (unlikely(cmd < 0 || cmd >= PFM_CMD_COUNT)) {
4790 DPRINT(("invalid cmd=%d\n", cmd));
4791 return -EINVAL;
4792 }
4793
4794 func = pfm_cmd_tab[cmd].cmd_func;
4795 narg = pfm_cmd_tab[cmd].cmd_narg;
4796 base_sz = pfm_cmd_tab[cmd].cmd_argsize;
4797 getsize = pfm_cmd_tab[cmd].cmd_getsize;
4798 cmd_flags = pfm_cmd_tab[cmd].cmd_flags;
4799
4800 if (unlikely(func == NULL)) {
4801 DPRINT(("invalid cmd=%d\n", cmd));
4802 return -EINVAL;
4803 }
4804
4805 DPRINT(("cmd=%s idx=%d narg=0x%x argsz=%lu count=%d\n",
4806 PFM_CMD_NAME(cmd),
4807 cmd,
4808 narg,
4809 base_sz,
4810 count));
4811
4812 /*
4813 * check if number of arguments matches what the command expects
4814 */
4815 if (unlikely((narg == PFM_CMD_ARG_MANY && count <= 0) || (narg > 0 && narg != count)))
4816 return -EINVAL;
4817
4818restart_args:
4819 sz = xtra_sz + base_sz*count;
4820 /*
4821 * limit abuse to min page size
4822 */
4823 if (unlikely(sz > PFM_MAX_ARGSIZE)) {
4824 printk(KERN_ERR "perfmon: [%d] argument too big %lu\n", current->pid, sz);
4825 return -E2BIG;
4826 }
4827
4828 /*
4829 * allocate default-sized argument buffer
4830 */
4831 if (likely(count && args_k == NULL)) {
4832 args_k = kmalloc(PFM_MAX_ARGSIZE, GFP_KERNEL);
4833 if (args_k == NULL) return -ENOMEM;
4834 }
4835
4836 ret = -EFAULT;
4837
4838 /*
4839 * copy arguments
4840 *
4841 * assume sz = 0 for command without parameters
4842 */
4843 if (sz && copy_from_user(args_k, arg, sz)) {
4844 DPRINT(("cannot copy_from_user %lu bytes @%p\n", sz, arg));
4845 goto error_args;
4846 }
4847
4848 /*
4849 * check if command supports extra parameters
4850 */
4851 if (completed_args == 0 && getsize) {
4852 /*
4853 * get extra parameters size (based on main argument)
4854 */
4855 ret = (*getsize)(args_k, &xtra_sz);
4856 if (ret) goto error_args;
4857
4858 completed_args = 1;
4859
4860 DPRINT(("restart_args sz=%lu xtra_sz=%lu\n", sz, xtra_sz));
4861
4862 /* retry if necessary */
4863 if (likely(xtra_sz)) goto restart_args;
4864 }
4865
4866 if (unlikely((cmd_flags & PFM_CMD_FD) == 0)) goto skip_fd;
4867
4868 ret = -EBADF;
4869
4870 file = fget(fd);
4871 if (unlikely(file == NULL)) {
4872 DPRINT(("invalid fd %d\n", fd));
4873 goto error_args;
4874 }
4875 if (unlikely(PFM_IS_FILE(file) == 0)) {
4876 DPRINT(("fd %d not related to perfmon\n", fd));
4877 goto error_args;
4878 }
4879
4880 ctx = (pfm_context_t *)file->private_data;
4881 if (unlikely(ctx == NULL)) {
4882 DPRINT(("no context for fd %d\n", fd));
4883 goto error_args;
4884 }
4885 prefetch(&ctx->ctx_state);
4886
4887 PROTECT_CTX(ctx, flags);
4888
4889 /*
4890 * check task is stopped
4891 */
4892 ret = pfm_check_task_state(ctx, cmd, flags);
4893 if (unlikely(ret)) goto abort_locked;
4894
4895skip_fd:
4896 ret = (*func)(ctx, args_k, count, ia64_task_regs(current));
4897
4898 call_made = 1;
4899
4900abort_locked:
4901 if (likely(ctx)) {
4902 DPRINT(("context unlocked\n"));
4903 UNPROTECT_CTX(ctx, flags);
4904 fput(file);
4905 }
4906
4907 /* copy argument back to user, if needed */
4908 if (call_made && PFM_CMD_RW_ARG(cmd) && copy_to_user(arg, args_k, base_sz*count)) ret = -EFAULT;
4909
4910error_args:
4911 if (args_k) kfree(args_k);
4912
4913 DPRINT(("cmd=%s ret=%ld\n", PFM_CMD_NAME(cmd), ret));
4914
4915 return ret;
4916}
4917
4918static void
4919pfm_resume_after_ovfl(pfm_context_t *ctx, unsigned long ovfl_regs, struct pt_regs *regs)
4920{
4921 pfm_buffer_fmt_t *fmt = ctx->ctx_buf_fmt;
4922 pfm_ovfl_ctrl_t rst_ctrl;
4923 int state;
4924 int ret = 0;
4925
4926 state = ctx->ctx_state;
4927 /*
4928 * Unlock sampling buffer and reset index atomically
4929 * XXX: not really needed when blocking
4930 */
4931 if (CTX_HAS_SMPL(ctx)) {
4932
4933 rst_ctrl.bits.mask_monitoring = 0;
4934 rst_ctrl.bits.reset_ovfl_pmds = 0;
4935
4936 if (state == PFM_CTX_LOADED)
4937 ret = pfm_buf_fmt_restart_active(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
4938 else
4939 ret = pfm_buf_fmt_restart(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
4940 } else {
4941 rst_ctrl.bits.mask_monitoring = 0;
4942 rst_ctrl.bits.reset_ovfl_pmds = 1;
4943 }
4944
4945 if (ret == 0) {
4946 if (rst_ctrl.bits.reset_ovfl_pmds) {
4947 pfm_reset_regs(ctx, &ovfl_regs, PFM_PMD_LONG_RESET);
4948 }
4949 if (rst_ctrl.bits.mask_monitoring == 0) {
4950 DPRINT(("resuming monitoring\n"));
4951 if (ctx->ctx_state == PFM_CTX_MASKED) pfm_restore_monitoring(current);
4952 } else {
4953 DPRINT(("stopping monitoring\n"));
4954 //pfm_stop_monitoring(current, regs);
4955 }
4956 ctx->ctx_state = PFM_CTX_LOADED;
4957 }
4958}
4959
4960/*
4961 * context MUST BE LOCKED when calling
4962 * can only be called for current
4963 */
4964static void
4965pfm_context_force_terminate(pfm_context_t *ctx, struct pt_regs *regs)
4966{
4967 int ret;
4968
4969 DPRINT(("entering for [%d]\n", current->pid));
4970
4971 ret = pfm_context_unload(ctx, NULL, 0, regs);
4972 if (ret) {
4973 printk(KERN_ERR "pfm_context_force_terminate: [%d] unloaded failed with %d\n", current->pid, ret);
4974 }
4975
4976 /*
4977 * and wakeup controlling task, indicating we are now disconnected
4978 */
4979 wake_up_interruptible(&ctx->ctx_zombieq);
4980
4981 /*
4982 * given that context is still locked, the controlling
4983 * task will only get access when we return from
4984 * pfm_handle_work().
4985 */
4986}
4987
4988static int pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds);
Stephane Eranian49449302005-04-25 13:08:30 -07004989 /*
4990 * pfm_handle_work() can be called with interrupts enabled
4991 * (TIF_NEED_RESCHED) or disabled. The down_interruptible
4992 * call may sleep, therefore we must re-enable interrupts
4993 * to avoid deadlocks. It is safe to do so because this function
4994 * is called ONLY when returning to user level (PUStk=1), in which case
4995 * there is no risk of kernel stack overflow due to deep
4996 * interrupt nesting.
4997 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998void
4999pfm_handle_work(void)
5000{
5001 pfm_context_t *ctx;
5002 struct pt_regs *regs;
Stephane Eranian49449302005-04-25 13:08:30 -07005003 unsigned long flags, dummy_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 unsigned long ovfl_regs;
5005 unsigned int reason;
5006 int ret;
5007
5008 ctx = PFM_GET_CTX(current);
5009 if (ctx == NULL) {
5010 printk(KERN_ERR "perfmon: [%d] has no PFM context\n", current->pid);
5011 return;
5012 }
5013
5014 PROTECT_CTX(ctx, flags);
5015
5016 PFM_SET_WORK_PENDING(current, 0);
5017
5018 pfm_clear_task_notify();
5019
5020 regs = ia64_task_regs(current);
5021
5022 /*
5023 * extract reason for being here and clear
5024 */
5025 reason = ctx->ctx_fl_trap_reason;
5026 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
5027 ovfl_regs = ctx->ctx_ovfl_regs[0];
5028
5029 DPRINT(("reason=%d state=%d\n", reason, ctx->ctx_state));
5030
5031 /*
5032 * must be done before we check for simple-reset mode
5033 */
5034 if (ctx->ctx_fl_going_zombie || ctx->ctx_state == PFM_CTX_ZOMBIE) goto do_zombie;
5035
5036
5037 //if (CTX_OVFL_NOBLOCK(ctx)) goto skip_blocking;
5038 if (reason == PFM_TRAP_REASON_RESET) goto skip_blocking;
5039
Stephane Eranian49449302005-04-25 13:08:30 -07005040 /*
5041 * restore interrupt mask to what it was on entry.
5042 * Could be enabled/diasbled.
5043 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 UNPROTECT_CTX(ctx, flags);
5045
Stephane Eranian49449302005-04-25 13:08:30 -07005046 /*
5047 * force interrupt enable because of down_interruptible()
5048 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 local_irq_enable();
5050
5051 DPRINT(("before block sleeping\n"));
5052
5053 /*
5054 * may go through without blocking on SMP systems
5055 * if restart has been received already by the time we call down()
5056 */
5057 ret = down_interruptible(&ctx->ctx_restart_sem);
5058
5059 DPRINT(("after block sleeping ret=%d\n", ret));
5060
5061 /*
Stephane Eranian49449302005-04-25 13:08:30 -07005062 * lock context and mask interrupts again
5063 * We save flags into a dummy because we may have
5064 * altered interrupts mask compared to entry in this
5065 * function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 */
Stephane Eranian49449302005-04-25 13:08:30 -07005067 PROTECT_CTX(ctx, dummy_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
5069 /*
5070 * we need to read the ovfl_regs only after wake-up
5071 * because we may have had pfm_write_pmds() in between
5072 * and that can changed PMD values and therefore
5073 * ovfl_regs is reset for these new PMD values.
5074 */
5075 ovfl_regs = ctx->ctx_ovfl_regs[0];
5076
5077 if (ctx->ctx_fl_going_zombie) {
5078do_zombie:
5079 DPRINT(("context is zombie, bailing out\n"));
5080 pfm_context_force_terminate(ctx, regs);
5081 goto nothing_to_do;
5082 }
5083 /*
5084 * in case of interruption of down() we don't restart anything
5085 */
5086 if (ret < 0) goto nothing_to_do;
5087
5088skip_blocking:
5089 pfm_resume_after_ovfl(ctx, ovfl_regs, regs);
5090 ctx->ctx_ovfl_regs[0] = 0UL;
5091
5092nothing_to_do:
Stephane Eranian49449302005-04-25 13:08:30 -07005093 /*
5094 * restore flags as they were upon entry
5095 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 UNPROTECT_CTX(ctx, flags);
5097}
5098
5099static int
5100pfm_notify_user(pfm_context_t *ctx, pfm_msg_t *msg)
5101{
5102 if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
5103 DPRINT(("ignoring overflow notification, owner is zombie\n"));
5104 return 0;
5105 }
5106
5107 DPRINT(("waking up somebody\n"));
5108
5109 if (msg) wake_up_interruptible(&ctx->ctx_msgq_wait);
5110
5111 /*
5112 * safe, we are not in intr handler, nor in ctxsw when
5113 * we come here
5114 */
5115 kill_fasync (&ctx->ctx_async_queue, SIGIO, POLL_IN);
5116
5117 return 0;
5118}
5119
5120static int
5121pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds)
5122{
5123 pfm_msg_t *msg = NULL;
5124
5125 if (ctx->ctx_fl_no_msg == 0) {
5126 msg = pfm_get_new_msg(ctx);
5127 if (msg == NULL) {
5128 printk(KERN_ERR "perfmon: pfm_ovfl_notify_user no more notification msgs\n");
5129 return -1;
5130 }
5131
5132 msg->pfm_ovfl_msg.msg_type = PFM_MSG_OVFL;
5133 msg->pfm_ovfl_msg.msg_ctx_fd = ctx->ctx_fd;
5134 msg->pfm_ovfl_msg.msg_active_set = 0;
5135 msg->pfm_ovfl_msg.msg_ovfl_pmds[0] = ovfl_pmds;
5136 msg->pfm_ovfl_msg.msg_ovfl_pmds[1] = 0UL;
5137 msg->pfm_ovfl_msg.msg_ovfl_pmds[2] = 0UL;
5138 msg->pfm_ovfl_msg.msg_ovfl_pmds[3] = 0UL;
5139 msg->pfm_ovfl_msg.msg_tstamp = 0UL;
5140 }
5141
5142 DPRINT(("ovfl msg: msg=%p no_msg=%d fd=%d ovfl_pmds=0x%lx\n",
5143 msg,
5144 ctx->ctx_fl_no_msg,
5145 ctx->ctx_fd,
5146 ovfl_pmds));
5147
5148 return pfm_notify_user(ctx, msg);
5149}
5150
5151static int
5152pfm_end_notify_user(pfm_context_t *ctx)
5153{
5154 pfm_msg_t *msg;
5155
5156 msg = pfm_get_new_msg(ctx);
5157 if (msg == NULL) {
5158 printk(KERN_ERR "perfmon: pfm_end_notify_user no more notification msgs\n");
5159 return -1;
5160 }
5161 /* no leak */
5162 memset(msg, 0, sizeof(*msg));
5163
5164 msg->pfm_end_msg.msg_type = PFM_MSG_END;
5165 msg->pfm_end_msg.msg_ctx_fd = ctx->ctx_fd;
5166 msg->pfm_ovfl_msg.msg_tstamp = 0UL;
5167
5168 DPRINT(("end msg: msg=%p no_msg=%d ctx_fd=%d\n",
5169 msg,
5170 ctx->ctx_fl_no_msg,
5171 ctx->ctx_fd));
5172
5173 return pfm_notify_user(ctx, msg);
5174}
5175
5176/*
5177 * main overflow processing routine.
5178 * it can be called from the interrupt path or explicitely during the context switch code
5179 */
5180static void
5181pfm_overflow_handler(struct task_struct *task, pfm_context_t *ctx, u64 pmc0, struct pt_regs *regs)
5182{
5183 pfm_ovfl_arg_t *ovfl_arg;
5184 unsigned long mask;
5185 unsigned long old_val, ovfl_val, new_val;
5186 unsigned long ovfl_notify = 0UL, ovfl_pmds = 0UL, smpl_pmds = 0UL, reset_pmds;
5187 unsigned long tstamp;
5188 pfm_ovfl_ctrl_t ovfl_ctrl;
5189 unsigned int i, has_smpl;
5190 int must_notify = 0;
5191
5192 if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) goto stop_monitoring;
5193
5194 /*
5195 * sanity test. Should never happen
5196 */
5197 if (unlikely((pmc0 & 0x1) == 0)) goto sanity_check;
5198
5199 tstamp = ia64_get_itc();
5200 mask = pmc0 >> PMU_FIRST_COUNTER;
5201 ovfl_val = pmu_conf->ovfl_val;
5202 has_smpl = CTX_HAS_SMPL(ctx);
5203
5204 DPRINT_ovfl(("pmc0=0x%lx pid=%d iip=0x%lx, %s "
5205 "used_pmds=0x%lx\n",
5206 pmc0,
5207 task ? task->pid: -1,
5208 (regs ? regs->cr_iip : 0),
5209 CTX_OVFL_NOBLOCK(ctx) ? "nonblocking" : "blocking",
5210 ctx->ctx_used_pmds[0]));
5211
5212
5213 /*
5214 * first we update the virtual counters
5215 * assume there was a prior ia64_srlz_d() issued
5216 */
5217 for (i = PMU_FIRST_COUNTER; mask ; i++, mask >>= 1) {
5218
5219 /* skip pmd which did not overflow */
5220 if ((mask & 0x1) == 0) continue;
5221
5222 /*
5223 * Note that the pmd is not necessarily 0 at this point as qualified events
5224 * may have happened before the PMU was frozen. The residual count is not
5225 * taken into consideration here but will be with any read of the pmd via
5226 * pfm_read_pmds().
5227 */
5228 old_val = new_val = ctx->ctx_pmds[i].val;
5229 new_val += 1 + ovfl_val;
5230 ctx->ctx_pmds[i].val = new_val;
5231
5232 /*
5233 * check for overflow condition
5234 */
5235 if (likely(old_val > new_val)) {
5236 ovfl_pmds |= 1UL << i;
5237 if (PMC_OVFL_NOTIFY(ctx, i)) ovfl_notify |= 1UL << i;
5238 }
5239
5240 DPRINT_ovfl(("ctx_pmd[%d].val=0x%lx old_val=0x%lx pmd=0x%lx ovfl_pmds=0x%lx ovfl_notify=0x%lx\n",
5241 i,
5242 new_val,
5243 old_val,
5244 ia64_get_pmd(i) & ovfl_val,
5245 ovfl_pmds,
5246 ovfl_notify));
5247 }
5248
5249 /*
5250 * there was no 64-bit overflow, nothing else to do
5251 */
5252 if (ovfl_pmds == 0UL) return;
5253
5254 /*
5255 * reset all control bits
5256 */
5257 ovfl_ctrl.val = 0;
5258 reset_pmds = 0UL;
5259
5260 /*
5261 * if a sampling format module exists, then we "cache" the overflow by
5262 * calling the module's handler() routine.
5263 */
5264 if (has_smpl) {
5265 unsigned long start_cycles, end_cycles;
5266 unsigned long pmd_mask;
5267 int j, k, ret = 0;
5268 int this_cpu = smp_processor_id();
5269
5270 pmd_mask = ovfl_pmds >> PMU_FIRST_COUNTER;
5271 ovfl_arg = &ctx->ctx_ovfl_arg;
5272
5273 prefetch(ctx->ctx_smpl_hdr);
5274
5275 for(i=PMU_FIRST_COUNTER; pmd_mask && ret == 0; i++, pmd_mask >>=1) {
5276
5277 mask = 1UL << i;
5278
5279 if ((pmd_mask & 0x1) == 0) continue;
5280
5281 ovfl_arg->ovfl_pmd = (unsigned char )i;
5282 ovfl_arg->ovfl_notify = ovfl_notify & mask ? 1 : 0;
5283 ovfl_arg->active_set = 0;
5284 ovfl_arg->ovfl_ctrl.val = 0; /* module must fill in all fields */
5285 ovfl_arg->smpl_pmds[0] = smpl_pmds = ctx->ctx_pmds[i].smpl_pmds[0];
5286
5287 ovfl_arg->pmd_value = ctx->ctx_pmds[i].val;
5288 ovfl_arg->pmd_last_reset = ctx->ctx_pmds[i].lval;
5289 ovfl_arg->pmd_eventid = ctx->ctx_pmds[i].eventid;
5290
5291 /*
5292 * copy values of pmds of interest. Sampling format may copy them
5293 * into sampling buffer.
5294 */
5295 if (smpl_pmds) {
5296 for(j=0, k=0; smpl_pmds; j++, smpl_pmds >>=1) {
5297 if ((smpl_pmds & 0x1) == 0) continue;
5298 ovfl_arg->smpl_pmds_values[k++] = PMD_IS_COUNTING(j) ? pfm_read_soft_counter(ctx, j) : ia64_get_pmd(j);
5299 DPRINT_ovfl(("smpl_pmd[%d]=pmd%u=0x%lx\n", k-1, j, ovfl_arg->smpl_pmds_values[k-1]));
5300 }
5301 }
5302
5303 pfm_stats[this_cpu].pfm_smpl_handler_calls++;
5304
5305 start_cycles = ia64_get_itc();
5306
5307 /*
5308 * call custom buffer format record (handler) routine
5309 */
5310 ret = (*ctx->ctx_buf_fmt->fmt_handler)(task, ctx->ctx_smpl_hdr, ovfl_arg, regs, tstamp);
5311
5312 end_cycles = ia64_get_itc();
5313
5314 /*
5315 * For those controls, we take the union because they have
5316 * an all or nothing behavior.
5317 */
5318 ovfl_ctrl.bits.notify_user |= ovfl_arg->ovfl_ctrl.bits.notify_user;
5319 ovfl_ctrl.bits.block_task |= ovfl_arg->ovfl_ctrl.bits.block_task;
5320 ovfl_ctrl.bits.mask_monitoring |= ovfl_arg->ovfl_ctrl.bits.mask_monitoring;
5321 /*
5322 * build the bitmask of pmds to reset now
5323 */
5324 if (ovfl_arg->ovfl_ctrl.bits.reset_ovfl_pmds) reset_pmds |= mask;
5325
5326 pfm_stats[this_cpu].pfm_smpl_handler_cycles += end_cycles - start_cycles;
5327 }
5328 /*
5329 * when the module cannot handle the rest of the overflows, we abort right here
5330 */
5331 if (ret && pmd_mask) {
5332 DPRINT(("handler aborts leftover ovfl_pmds=0x%lx\n",
5333 pmd_mask<<PMU_FIRST_COUNTER));
5334 }
5335 /*
5336 * remove the pmds we reset now from the set of pmds to reset in pfm_restart()
5337 */
5338 ovfl_pmds &= ~reset_pmds;
5339 } else {
5340 /*
5341 * when no sampling module is used, then the default
5342 * is to notify on overflow if requested by user
5343 */
5344 ovfl_ctrl.bits.notify_user = ovfl_notify ? 1 : 0;
5345 ovfl_ctrl.bits.block_task = ovfl_notify ? 1 : 0;
5346 ovfl_ctrl.bits.mask_monitoring = ovfl_notify ? 1 : 0; /* XXX: change for saturation */
5347 ovfl_ctrl.bits.reset_ovfl_pmds = ovfl_notify ? 0 : 1;
5348 /*
5349 * if needed, we reset all overflowed pmds
5350 */
5351 if (ovfl_notify == 0) reset_pmds = ovfl_pmds;
5352 }
5353
5354 DPRINT_ovfl(("ovfl_pmds=0x%lx reset_pmds=0x%lx\n", ovfl_pmds, reset_pmds));
5355
5356 /*
5357 * reset the requested PMD registers using the short reset values
5358 */
5359 if (reset_pmds) {
5360 unsigned long bm = reset_pmds;
5361 pfm_reset_regs(ctx, &bm, PFM_PMD_SHORT_RESET);
5362 }
5363
5364 if (ovfl_notify && ovfl_ctrl.bits.notify_user) {
5365 /*
5366 * keep track of what to reset when unblocking
5367 */
5368 ctx->ctx_ovfl_regs[0] = ovfl_pmds;
5369
5370 /*
5371 * check for blocking context
5372 */
5373 if (CTX_OVFL_NOBLOCK(ctx) == 0 && ovfl_ctrl.bits.block_task) {
5374
5375 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_BLOCK;
5376
5377 /*
5378 * set the perfmon specific checking pending work for the task
5379 */
5380 PFM_SET_WORK_PENDING(task, 1);
5381
5382 /*
5383 * when coming from ctxsw, current still points to the
5384 * previous task, therefore we must work with task and not current.
5385 */
5386 pfm_set_task_notify(task);
5387 }
5388 /*
5389 * defer until state is changed (shorten spin window). the context is locked
5390 * anyway, so the signal receiver would come spin for nothing.
5391 */
5392 must_notify = 1;
5393 }
5394
5395 DPRINT_ovfl(("owner [%d] pending=%ld reason=%u ovfl_pmds=0x%lx ovfl_notify=0x%lx masked=%d\n",
5396 GET_PMU_OWNER() ? GET_PMU_OWNER()->pid : -1,
5397 PFM_GET_WORK_PENDING(task),
5398 ctx->ctx_fl_trap_reason,
5399 ovfl_pmds,
5400 ovfl_notify,
5401 ovfl_ctrl.bits.mask_monitoring ? 1 : 0));
5402 /*
5403 * in case monitoring must be stopped, we toggle the psr bits
5404 */
5405 if (ovfl_ctrl.bits.mask_monitoring) {
5406 pfm_mask_monitoring(task);
5407 ctx->ctx_state = PFM_CTX_MASKED;
5408 ctx->ctx_fl_can_restart = 1;
5409 }
5410
5411 /*
5412 * send notification now
5413 */
5414 if (must_notify) pfm_ovfl_notify_user(ctx, ovfl_notify);
5415
5416 return;
5417
5418sanity_check:
5419 printk(KERN_ERR "perfmon: CPU%d overflow handler [%d] pmc0=0x%lx\n",
5420 smp_processor_id(),
5421 task ? task->pid : -1,
5422 pmc0);
5423 return;
5424
5425stop_monitoring:
5426 /*
5427 * in SMP, zombie context is never restored but reclaimed in pfm_load_regs().
5428 * Moreover, zombies are also reclaimed in pfm_save_regs(). Therefore we can
5429 * come here as zombie only if the task is the current task. In which case, we
5430 * can access the PMU hardware directly.
5431 *
5432 * Note that zombies do have PM_VALID set. So here we do the minimal.
5433 *
5434 * In case the context was zombified it could not be reclaimed at the time
5435 * the monitoring program exited. At this point, the PMU reservation has been
5436 * returned, the sampiing buffer has been freed. We must convert this call
5437 * into a spurious interrupt. However, we must also avoid infinite overflows
5438 * by stopping monitoring for this task. We can only come here for a per-task
5439 * context. All we need to do is to stop monitoring using the psr bits which
5440 * are always task private. By re-enabling secure montioring, we ensure that
5441 * the monitored task will not be able to re-activate monitoring.
5442 * The task will eventually be context switched out, at which point the context
5443 * will be reclaimed (that includes releasing ownership of the PMU).
5444 *
5445 * So there might be a window of time where the number of per-task session is zero
5446 * yet one PMU might have a owner and get at most one overflow interrupt for a zombie
5447 * context. This is safe because if a per-task session comes in, it will push this one
5448 * out and by the virtue on pfm_save_regs(), this one will disappear. If a system wide
5449 * session is force on that CPU, given that we use task pinning, pfm_save_regs() will
5450 * also push our zombie context out.
5451 *
5452 * Overall pretty hairy stuff....
5453 */
5454 DPRINT(("ctx is zombie for [%d], converted to spurious\n", task ? task->pid: -1));
5455 pfm_clear_psr_up();
5456 ia64_psr(regs)->up = 0;
5457 ia64_psr(regs)->sp = 1;
5458 return;
5459}
5460
5461static int
5462pfm_do_interrupt_handler(int irq, void *arg, struct pt_regs *regs)
5463{
5464 struct task_struct *task;
5465 pfm_context_t *ctx;
5466 unsigned long flags;
5467 u64 pmc0;
5468 int this_cpu = smp_processor_id();
5469 int retval = 0;
5470
5471 pfm_stats[this_cpu].pfm_ovfl_intr_count++;
5472
5473 /*
5474 * srlz.d done before arriving here
5475 */
5476 pmc0 = ia64_get_pmc(0);
5477
5478 task = GET_PMU_OWNER();
5479 ctx = GET_PMU_CTX();
5480
5481 /*
5482 * if we have some pending bits set
5483 * assumes : if any PMC0.bit[63-1] is set, then PMC0.fr = 1
5484 */
5485 if (PMC0_HAS_OVFL(pmc0) && task) {
5486 /*
5487 * we assume that pmc0.fr is always set here
5488 */
5489
5490 /* sanity check */
5491 if (!ctx) goto report_spurious1;
5492
5493 if (ctx->ctx_fl_system == 0 && (task->thread.flags & IA64_THREAD_PM_VALID) == 0)
5494 goto report_spurious2;
5495
5496 PROTECT_CTX_NOPRINT(ctx, flags);
5497
5498 pfm_overflow_handler(task, ctx, pmc0, regs);
5499
5500 UNPROTECT_CTX_NOPRINT(ctx, flags);
5501
5502 } else {
5503 pfm_stats[this_cpu].pfm_spurious_ovfl_intr_count++;
5504 retval = -1;
5505 }
5506 /*
5507 * keep it unfrozen at all times
5508 */
5509 pfm_unfreeze_pmu();
5510
5511 return retval;
5512
5513report_spurious1:
5514 printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d has no PFM context\n",
5515 this_cpu, task->pid);
5516 pfm_unfreeze_pmu();
5517 return -1;
5518report_spurious2:
5519 printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d, invalid flag\n",
5520 this_cpu,
5521 task->pid);
5522 pfm_unfreeze_pmu();
5523 return -1;
5524}
5525
5526static irqreturn_t
5527pfm_interrupt_handler(int irq, void *arg, struct pt_regs *regs)
5528{
5529 unsigned long start_cycles, total_cycles;
5530 unsigned long min, max;
5531 int this_cpu;
5532 int ret;
5533
5534 this_cpu = get_cpu();
5535 min = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min;
5536 max = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max;
5537
5538 start_cycles = ia64_get_itc();
5539
5540 ret = pfm_do_interrupt_handler(irq, arg, regs);
5541
5542 total_cycles = ia64_get_itc();
5543
5544 /*
5545 * don't measure spurious interrupts
5546 */
5547 if (likely(ret == 0)) {
5548 total_cycles -= start_cycles;
5549
5550 if (total_cycles < min) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min = total_cycles;
5551 if (total_cycles > max) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max = total_cycles;
5552
5553 pfm_stats[this_cpu].pfm_ovfl_intr_cycles += total_cycles;
5554 }
5555 put_cpu_no_resched();
5556 return IRQ_HANDLED;
5557}
5558
5559/*
5560 * /proc/perfmon interface, for debug only
5561 */
5562
5563#define PFM_PROC_SHOW_HEADER ((void *)NR_CPUS+1)
5564
5565static void *
5566pfm_proc_start(struct seq_file *m, loff_t *pos)
5567{
5568 if (*pos == 0) {
5569 return PFM_PROC_SHOW_HEADER;
5570 }
5571
5572 while (*pos <= NR_CPUS) {
5573 if (cpu_online(*pos - 1)) {
5574 return (void *)*pos;
5575 }
5576 ++*pos;
5577 }
5578 return NULL;
5579}
5580
5581static void *
5582pfm_proc_next(struct seq_file *m, void *v, loff_t *pos)
5583{
5584 ++*pos;
5585 return pfm_proc_start(m, pos);
5586}
5587
5588static void
5589pfm_proc_stop(struct seq_file *m, void *v)
5590{
5591}
5592
5593static void
5594pfm_proc_show_header(struct seq_file *m)
5595{
5596 struct list_head * pos;
5597 pfm_buffer_fmt_t * entry;
5598 unsigned long flags;
5599
5600 seq_printf(m,
5601 "perfmon version : %u.%u\n"
5602 "model : %s\n"
5603 "fastctxsw : %s\n"
5604 "expert mode : %s\n"
5605 "ovfl_mask : 0x%lx\n"
5606 "PMU flags : 0x%x\n",
5607 PFM_VERSION_MAJ, PFM_VERSION_MIN,
5608 pmu_conf->pmu_name,
5609 pfm_sysctl.fastctxsw > 0 ? "Yes": "No",
5610 pfm_sysctl.expert_mode > 0 ? "Yes": "No",
5611 pmu_conf->ovfl_val,
5612 pmu_conf->flags);
5613
5614 LOCK_PFS(flags);
5615
5616 seq_printf(m,
5617 "proc_sessions : %u\n"
5618 "sys_sessions : %u\n"
5619 "sys_use_dbregs : %u\n"
5620 "ptrace_use_dbregs : %u\n",
5621 pfm_sessions.pfs_task_sessions,
5622 pfm_sessions.pfs_sys_sessions,
5623 pfm_sessions.pfs_sys_use_dbregs,
5624 pfm_sessions.pfs_ptrace_use_dbregs);
5625
5626 UNLOCK_PFS(flags);
5627
5628 spin_lock(&pfm_buffer_fmt_lock);
5629
5630 list_for_each(pos, &pfm_buffer_fmt_list) {
5631 entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
5632 seq_printf(m, "format : %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x %s\n",
5633 entry->fmt_uuid[0],
5634 entry->fmt_uuid[1],
5635 entry->fmt_uuid[2],
5636 entry->fmt_uuid[3],
5637 entry->fmt_uuid[4],
5638 entry->fmt_uuid[5],
5639 entry->fmt_uuid[6],
5640 entry->fmt_uuid[7],
5641 entry->fmt_uuid[8],
5642 entry->fmt_uuid[9],
5643 entry->fmt_uuid[10],
5644 entry->fmt_uuid[11],
5645 entry->fmt_uuid[12],
5646 entry->fmt_uuid[13],
5647 entry->fmt_uuid[14],
5648 entry->fmt_uuid[15],
5649 entry->fmt_name);
5650 }
5651 spin_unlock(&pfm_buffer_fmt_lock);
5652
5653}
5654
5655static int
5656pfm_proc_show(struct seq_file *m, void *v)
5657{
5658 unsigned long psr;
5659 unsigned int i;
5660 int cpu;
5661
5662 if (v == PFM_PROC_SHOW_HEADER) {
5663 pfm_proc_show_header(m);
5664 return 0;
5665 }
5666
5667 /* show info for CPU (v - 1) */
5668
5669 cpu = (long)v - 1;
5670 seq_printf(m,
5671 "CPU%-2d overflow intrs : %lu\n"
5672 "CPU%-2d overflow cycles : %lu\n"
5673 "CPU%-2d overflow min : %lu\n"
5674 "CPU%-2d overflow max : %lu\n"
5675 "CPU%-2d smpl handler calls : %lu\n"
5676 "CPU%-2d smpl handler cycles : %lu\n"
5677 "CPU%-2d spurious intrs : %lu\n"
5678 "CPU%-2d replay intrs : %lu\n"
5679 "CPU%-2d syst_wide : %d\n"
5680 "CPU%-2d dcr_pp : %d\n"
5681 "CPU%-2d exclude idle : %d\n"
5682 "CPU%-2d owner : %d\n"
5683 "CPU%-2d context : %p\n"
5684 "CPU%-2d activations : %lu\n",
5685 cpu, pfm_stats[cpu].pfm_ovfl_intr_count,
5686 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles,
5687 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_min,
5688 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_max,
5689 cpu, pfm_stats[cpu].pfm_smpl_handler_calls,
5690 cpu, pfm_stats[cpu].pfm_smpl_handler_cycles,
5691 cpu, pfm_stats[cpu].pfm_spurious_ovfl_intr_count,
5692 cpu, pfm_stats[cpu].pfm_replay_ovfl_intr_count,
5693 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_SYST_WIDE ? 1 : 0,
5694 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_DCR_PP ? 1 : 0,
5695 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_EXCL_IDLE ? 1 : 0,
5696 cpu, pfm_get_cpu_data(pmu_owner, cpu) ? pfm_get_cpu_data(pmu_owner, cpu)->pid: -1,
5697 cpu, pfm_get_cpu_data(pmu_ctx, cpu),
5698 cpu, pfm_get_cpu_data(pmu_activation_number, cpu));
5699
5700 if (num_online_cpus() == 1 && pfm_sysctl.debug > 0) {
5701
5702 psr = pfm_get_psr();
5703
5704 ia64_srlz_d();
5705
5706 seq_printf(m,
5707 "CPU%-2d psr : 0x%lx\n"
5708 "CPU%-2d pmc0 : 0x%lx\n",
5709 cpu, psr,
5710 cpu, ia64_get_pmc(0));
5711
5712 for (i=0; PMC_IS_LAST(i) == 0; i++) {
5713 if (PMC_IS_COUNTING(i) == 0) continue;
5714 seq_printf(m,
5715 "CPU%-2d pmc%u : 0x%lx\n"
5716 "CPU%-2d pmd%u : 0x%lx\n",
5717 cpu, i, ia64_get_pmc(i),
5718 cpu, i, ia64_get_pmd(i));
5719 }
5720 }
5721 return 0;
5722}
5723
5724struct seq_operations pfm_seq_ops = {
5725 .start = pfm_proc_start,
5726 .next = pfm_proc_next,
5727 .stop = pfm_proc_stop,
5728 .show = pfm_proc_show
5729};
5730
5731static int
5732pfm_proc_open(struct inode *inode, struct file *file)
5733{
5734 return seq_open(file, &pfm_seq_ops);
5735}
5736
5737
5738/*
5739 * we come here as soon as local_cpu_data->pfm_syst_wide is set. this happens
5740 * during pfm_enable() hence before pfm_start(). We cannot assume monitoring
5741 * is active or inactive based on mode. We must rely on the value in
5742 * local_cpu_data->pfm_syst_info
5743 */
5744void
5745pfm_syst_wide_update_task(struct task_struct *task, unsigned long info, int is_ctxswin)
5746{
5747 struct pt_regs *regs;
5748 unsigned long dcr;
5749 unsigned long dcr_pp;
5750
5751 dcr_pp = info & PFM_CPUINFO_DCR_PP ? 1 : 0;
5752
5753 /*
5754 * pid 0 is guaranteed to be the idle task. There is one such task with pid 0
5755 * on every CPU, so we can rely on the pid to identify the idle task.
5756 */
5757 if ((info & PFM_CPUINFO_EXCL_IDLE) == 0 || task->pid) {
5758 regs = ia64_task_regs(task);
5759 ia64_psr(regs)->pp = is_ctxswin ? dcr_pp : 0;
5760 return;
5761 }
5762 /*
5763 * if monitoring has started
5764 */
5765 if (dcr_pp) {
5766 dcr = ia64_getreg(_IA64_REG_CR_DCR);
5767 /*
5768 * context switching in?
5769 */
5770 if (is_ctxswin) {
5771 /* mask monitoring for the idle task */
5772 ia64_setreg(_IA64_REG_CR_DCR, dcr & ~IA64_DCR_PP);
5773 pfm_clear_psr_pp();
5774 ia64_srlz_i();
5775 return;
5776 }
5777 /*
5778 * context switching out
5779 * restore monitoring for next task
5780 *
5781 * Due to inlining this odd if-then-else construction generates
5782 * better code.
5783 */
5784 ia64_setreg(_IA64_REG_CR_DCR, dcr |IA64_DCR_PP);
5785 pfm_set_psr_pp();
5786 ia64_srlz_i();
5787 }
5788}
5789
5790#ifdef CONFIG_SMP
5791
5792static void
5793pfm_force_cleanup(pfm_context_t *ctx, struct pt_regs *regs)
5794{
5795 struct task_struct *task = ctx->ctx_task;
5796
5797 ia64_psr(regs)->up = 0;
5798 ia64_psr(regs)->sp = 1;
5799
5800 if (GET_PMU_OWNER() == task) {
5801 DPRINT(("cleared ownership for [%d]\n", ctx->ctx_task->pid));
5802 SET_PMU_OWNER(NULL, NULL);
5803 }
5804
5805 /*
5806 * disconnect the task from the context and vice-versa
5807 */
5808 PFM_SET_WORK_PENDING(task, 0);
5809
5810 task->thread.pfm_context = NULL;
5811 task->thread.flags &= ~IA64_THREAD_PM_VALID;
5812
5813 DPRINT(("force cleanup for [%d]\n", task->pid));
5814}
5815
5816
5817/*
5818 * in 2.6, interrupts are masked when we come here and the runqueue lock is held
5819 */
5820void
5821pfm_save_regs(struct task_struct *task)
5822{
5823 pfm_context_t *ctx;
5824 struct thread_struct *t;
5825 unsigned long flags;
5826 u64 psr;
5827
5828
5829 ctx = PFM_GET_CTX(task);
5830 if (ctx == NULL) return;
5831 t = &task->thread;
5832
5833 /*
5834 * we always come here with interrupts ALREADY disabled by
5835 * the scheduler. So we simply need to protect against concurrent
5836 * access, not CPU concurrency.
5837 */
5838 flags = pfm_protect_ctx_ctxsw(ctx);
5839
5840 if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
5841 struct pt_regs *regs = ia64_task_regs(task);
5842
5843 pfm_clear_psr_up();
5844
5845 pfm_force_cleanup(ctx, regs);
5846
5847 BUG_ON(ctx->ctx_smpl_hdr);
5848
5849 pfm_unprotect_ctx_ctxsw(ctx, flags);
5850
5851 pfm_context_free(ctx);
5852 return;
5853 }
5854
5855 /*
5856 * save current PSR: needed because we modify it
5857 */
5858 ia64_srlz_d();
5859 psr = pfm_get_psr();
5860
5861 BUG_ON(psr & (IA64_PSR_I));
5862
5863 /*
5864 * stop monitoring:
5865 * This is the last instruction which may generate an overflow
5866 *
5867 * We do not need to set psr.sp because, it is irrelevant in kernel.
5868 * It will be restored from ipsr when going back to user level
5869 */
5870 pfm_clear_psr_up();
5871
5872 /*
5873 * keep a copy of psr.up (for reload)
5874 */
5875 ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
5876
5877 /*
5878 * release ownership of this PMU.
5879 * PM interrupts are masked, so nothing
5880 * can happen.
5881 */
5882 SET_PMU_OWNER(NULL, NULL);
5883
5884 /*
5885 * we systematically save the PMD as we have no
5886 * guarantee we will be schedule at that same
5887 * CPU again.
5888 */
5889 pfm_save_pmds(t->pmds, ctx->ctx_used_pmds[0]);
5890
5891 /*
5892 * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
5893 * we will need it on the restore path to check
5894 * for pending overflow.
5895 */
5896 t->pmcs[0] = ia64_get_pmc(0);
5897
5898 /*
5899 * unfreeze PMU if had pending overflows
5900 */
5901 if (t->pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
5902
5903 /*
5904 * finally, allow context access.
5905 * interrupts will still be masked after this call.
5906 */
5907 pfm_unprotect_ctx_ctxsw(ctx, flags);
5908}
5909
5910#else /* !CONFIG_SMP */
5911void
5912pfm_save_regs(struct task_struct *task)
5913{
5914 pfm_context_t *ctx;
5915 u64 psr;
5916
5917 ctx = PFM_GET_CTX(task);
5918 if (ctx == NULL) return;
5919
5920 /*
5921 * save current PSR: needed because we modify it
5922 */
5923 psr = pfm_get_psr();
5924
5925 BUG_ON(psr & (IA64_PSR_I));
5926
5927 /*
5928 * stop monitoring:
5929 * This is the last instruction which may generate an overflow
5930 *
5931 * We do not need to set psr.sp because, it is irrelevant in kernel.
5932 * It will be restored from ipsr when going back to user level
5933 */
5934 pfm_clear_psr_up();
5935
5936 /*
5937 * keep a copy of psr.up (for reload)
5938 */
5939 ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
5940}
5941
5942static void
5943pfm_lazy_save_regs (struct task_struct *task)
5944{
5945 pfm_context_t *ctx;
5946 struct thread_struct *t;
5947 unsigned long flags;
5948
5949 { u64 psr = pfm_get_psr();
5950 BUG_ON(psr & IA64_PSR_UP);
5951 }
5952
5953 ctx = PFM_GET_CTX(task);
5954 t = &task->thread;
5955
5956 /*
5957 * we need to mask PMU overflow here to
5958 * make sure that we maintain pmc0 until
5959 * we save it. overflow interrupts are
5960 * treated as spurious if there is no
5961 * owner.
5962 *
5963 * XXX: I don't think this is necessary
5964 */
5965 PROTECT_CTX(ctx,flags);
5966
5967 /*
5968 * release ownership of this PMU.
5969 * must be done before we save the registers.
5970 *
5971 * after this call any PMU interrupt is treated
5972 * as spurious.
5973 */
5974 SET_PMU_OWNER(NULL, NULL);
5975
5976 /*
5977 * save all the pmds we use
5978 */
5979 pfm_save_pmds(t->pmds, ctx->ctx_used_pmds[0]);
5980
5981 /*
5982 * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
5983 * it is needed to check for pended overflow
5984 * on the restore path
5985 */
5986 t->pmcs[0] = ia64_get_pmc(0);
5987
5988 /*
5989 * unfreeze PMU if had pending overflows
5990 */
5991 if (t->pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
5992
5993 /*
5994 * now get can unmask PMU interrupts, they will
5995 * be treated as purely spurious and we will not
5996 * lose any information
5997 */
5998 UNPROTECT_CTX(ctx,flags);
5999}
6000#endif /* CONFIG_SMP */
6001
6002#ifdef CONFIG_SMP
6003/*
6004 * in 2.6, interrupts are masked when we come here and the runqueue lock is held
6005 */
6006void
6007pfm_load_regs (struct task_struct *task)
6008{
6009 pfm_context_t *ctx;
6010 struct thread_struct *t;
6011 unsigned long pmc_mask = 0UL, pmd_mask = 0UL;
6012 unsigned long flags;
6013 u64 psr, psr_up;
6014 int need_irq_resend;
6015
6016 ctx = PFM_GET_CTX(task);
6017 if (unlikely(ctx == NULL)) return;
6018
6019 BUG_ON(GET_PMU_OWNER());
6020
6021 t = &task->thread;
6022 /*
6023 * possible on unload
6024 */
6025 if (unlikely((t->flags & IA64_THREAD_PM_VALID) == 0)) return;
6026
6027 /*
6028 * we always come here with interrupts ALREADY disabled by
6029 * the scheduler. So we simply need to protect against concurrent
6030 * access, not CPU concurrency.
6031 */
6032 flags = pfm_protect_ctx_ctxsw(ctx);
6033 psr = pfm_get_psr();
6034
6035 need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
6036
6037 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
6038 BUG_ON(psr & IA64_PSR_I);
6039
6040 if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) {
6041 struct pt_regs *regs = ia64_task_regs(task);
6042
6043 BUG_ON(ctx->ctx_smpl_hdr);
6044
6045 pfm_force_cleanup(ctx, regs);
6046
6047 pfm_unprotect_ctx_ctxsw(ctx, flags);
6048
6049 /*
6050 * this one (kmalloc'ed) is fine with interrupts disabled
6051 */
6052 pfm_context_free(ctx);
6053
6054 return;
6055 }
6056
6057 /*
6058 * we restore ALL the debug registers to avoid picking up
6059 * stale state.
6060 */
6061 if (ctx->ctx_fl_using_dbreg) {
6062 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
6063 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
6064 }
6065 /*
6066 * retrieve saved psr.up
6067 */
6068 psr_up = ctx->ctx_saved_psr_up;
6069
6070 /*
6071 * if we were the last user of the PMU on that CPU,
6072 * then nothing to do except restore psr
6073 */
6074 if (GET_LAST_CPU(ctx) == smp_processor_id() && ctx->ctx_last_activation == GET_ACTIVATION()) {
6075
6076 /*
6077 * retrieve partial reload masks (due to user modifications)
6078 */
6079 pmc_mask = ctx->ctx_reload_pmcs[0];
6080 pmd_mask = ctx->ctx_reload_pmds[0];
6081
6082 } else {
6083 /*
6084 * To avoid leaking information to the user level when psr.sp=0,
6085 * we must reload ALL implemented pmds (even the ones we don't use).
6086 * In the kernel we only allow PFM_READ_PMDS on registers which
6087 * we initialized or requested (sampling) so there is no risk there.
6088 */
6089 pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
6090
6091 /*
6092 * ALL accessible PMCs are systematically reloaded, unused registers
6093 * get their default (from pfm_reset_pmu_state()) values to avoid picking
6094 * up stale configuration.
6095 *
6096 * PMC0 is never in the mask. It is always restored separately.
6097 */
6098 pmc_mask = ctx->ctx_all_pmcs[0];
6099 }
6100 /*
6101 * when context is MASKED, we will restore PMC with plm=0
6102 * and PMD with stale information, but that's ok, nothing
6103 * will be captured.
6104 *
6105 * XXX: optimize here
6106 */
6107 if (pmd_mask) pfm_restore_pmds(t->pmds, pmd_mask);
6108 if (pmc_mask) pfm_restore_pmcs(t->pmcs, pmc_mask);
6109
6110 /*
6111 * check for pending overflow at the time the state
6112 * was saved.
6113 */
6114 if (unlikely(PMC0_HAS_OVFL(t->pmcs[0]))) {
6115 /*
6116 * reload pmc0 with the overflow information
6117 * On McKinley PMU, this will trigger a PMU interrupt
6118 */
6119 ia64_set_pmc(0, t->pmcs[0]);
6120 ia64_srlz_d();
6121 t->pmcs[0] = 0UL;
6122
6123 /*
6124 * will replay the PMU interrupt
6125 */
6126 if (need_irq_resend) hw_resend_irq(NULL, IA64_PERFMON_VECTOR);
6127
6128 pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
6129 }
6130
6131 /*
6132 * we just did a reload, so we reset the partial reload fields
6133 */
6134 ctx->ctx_reload_pmcs[0] = 0UL;
6135 ctx->ctx_reload_pmds[0] = 0UL;
6136
6137 SET_LAST_CPU(ctx, smp_processor_id());
6138
6139 /*
6140 * dump activation value for this PMU
6141 */
6142 INC_ACTIVATION();
6143 /*
6144 * record current activation for this context
6145 */
6146 SET_ACTIVATION(ctx);
6147
6148 /*
6149 * establish new ownership.
6150 */
6151 SET_PMU_OWNER(task, ctx);
6152
6153 /*
6154 * restore the psr.up bit. measurement
6155 * is active again.
6156 * no PMU interrupt can happen at this point
6157 * because we still have interrupts disabled.
6158 */
6159 if (likely(psr_up)) pfm_set_psr_up();
6160
6161 /*
6162 * allow concurrent access to context
6163 */
6164 pfm_unprotect_ctx_ctxsw(ctx, flags);
6165}
6166#else /* !CONFIG_SMP */
6167/*
6168 * reload PMU state for UP kernels
6169 * in 2.5 we come here with interrupts disabled
6170 */
6171void
6172pfm_load_regs (struct task_struct *task)
6173{
6174 struct thread_struct *t;
6175 pfm_context_t *ctx;
6176 struct task_struct *owner;
6177 unsigned long pmd_mask, pmc_mask;
6178 u64 psr, psr_up;
6179 int need_irq_resend;
6180
6181 owner = GET_PMU_OWNER();
6182 ctx = PFM_GET_CTX(task);
6183 t = &task->thread;
6184 psr = pfm_get_psr();
6185
6186 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
6187 BUG_ON(psr & IA64_PSR_I);
6188
6189 /*
6190 * we restore ALL the debug registers to avoid picking up
6191 * stale state.
6192 *
6193 * This must be done even when the task is still the owner
6194 * as the registers may have been modified via ptrace()
6195 * (not perfmon) by the previous task.
6196 */
6197 if (ctx->ctx_fl_using_dbreg) {
6198 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
6199 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
6200 }
6201
6202 /*
6203 * retrieved saved psr.up
6204 */
6205 psr_up = ctx->ctx_saved_psr_up;
6206 need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
6207
6208 /*
6209 * short path, our state is still there, just
6210 * need to restore psr and we go
6211 *
6212 * we do not touch either PMC nor PMD. the psr is not touched
6213 * by the overflow_handler. So we are safe w.r.t. to interrupt
6214 * concurrency even without interrupt masking.
6215 */
6216 if (likely(owner == task)) {
6217 if (likely(psr_up)) pfm_set_psr_up();
6218 return;
6219 }
6220
6221 /*
6222 * someone else is still using the PMU, first push it out and
6223 * then we'll be able to install our stuff !
6224 *
6225 * Upon return, there will be no owner for the current PMU
6226 */
6227 if (owner) pfm_lazy_save_regs(owner);
6228
6229 /*
6230 * To avoid leaking information to the user level when psr.sp=0,
6231 * we must reload ALL implemented pmds (even the ones we don't use).
6232 * In the kernel we only allow PFM_READ_PMDS on registers which
6233 * we initialized or requested (sampling) so there is no risk there.
6234 */
6235 pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
6236
6237 /*
6238 * ALL accessible PMCs are systematically reloaded, unused registers
6239 * get their default (from pfm_reset_pmu_state()) values to avoid picking
6240 * up stale configuration.
6241 *
6242 * PMC0 is never in the mask. It is always restored separately
6243 */
6244 pmc_mask = ctx->ctx_all_pmcs[0];
6245
6246 pfm_restore_pmds(t->pmds, pmd_mask);
6247 pfm_restore_pmcs(t->pmcs, pmc_mask);
6248
6249 /*
6250 * check for pending overflow at the time the state
6251 * was saved.
6252 */
6253 if (unlikely(PMC0_HAS_OVFL(t->pmcs[0]))) {
6254 /*
6255 * reload pmc0 with the overflow information
6256 * On McKinley PMU, this will trigger a PMU interrupt
6257 */
6258 ia64_set_pmc(0, t->pmcs[0]);
6259 ia64_srlz_d();
6260
6261 t->pmcs[0] = 0UL;
6262
6263 /*
6264 * will replay the PMU interrupt
6265 */
6266 if (need_irq_resend) hw_resend_irq(NULL, IA64_PERFMON_VECTOR);
6267
6268 pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
6269 }
6270
6271 /*
6272 * establish new ownership.
6273 */
6274 SET_PMU_OWNER(task, ctx);
6275
6276 /*
6277 * restore the psr.up bit. measurement
6278 * is active again.
6279 * no PMU interrupt can happen at this point
6280 * because we still have interrupts disabled.
6281 */
6282 if (likely(psr_up)) pfm_set_psr_up();
6283}
6284#endif /* CONFIG_SMP */
6285
6286/*
6287 * this function assumes monitoring is stopped
6288 */
6289static void
6290pfm_flush_pmds(struct task_struct *task, pfm_context_t *ctx)
6291{
6292 u64 pmc0;
6293 unsigned long mask2, val, pmd_val, ovfl_val;
6294 int i, can_access_pmu = 0;
6295 int is_self;
6296
6297 /*
6298 * is the caller the task being monitored (or which initiated the
6299 * session for system wide measurements)
6300 */
6301 is_self = ctx->ctx_task == task ? 1 : 0;
6302
6303 /*
6304 * can access PMU is task is the owner of the PMU state on the current CPU
6305 * or if we are running on the CPU bound to the context in system-wide mode
6306 * (that is not necessarily the task the context is attached to in this mode).
6307 * In system-wide we always have can_access_pmu true because a task running on an
6308 * invalid processor is flagged earlier in the call stack (see pfm_stop).
6309 */
6310 can_access_pmu = (GET_PMU_OWNER() == task) || (ctx->ctx_fl_system && ctx->ctx_cpu == smp_processor_id());
6311 if (can_access_pmu) {
6312 /*
6313 * Mark the PMU as not owned
6314 * This will cause the interrupt handler to do nothing in case an overflow
6315 * interrupt was in-flight
6316 * This also guarantees that pmc0 will contain the final state
6317 * It virtually gives us full control on overflow processing from that point
6318 * on.
6319 */
6320 SET_PMU_OWNER(NULL, NULL);
6321 DPRINT(("releasing ownership\n"));
6322
6323 /*
6324 * read current overflow status:
6325 *
6326 * we are guaranteed to read the final stable state
6327 */
6328 ia64_srlz_d();
6329 pmc0 = ia64_get_pmc(0); /* slow */
6330
6331 /*
6332 * reset freeze bit, overflow status information destroyed
6333 */
6334 pfm_unfreeze_pmu();
6335 } else {
6336 pmc0 = task->thread.pmcs[0];
6337 /*
6338 * clear whatever overflow status bits there were
6339 */
6340 task->thread.pmcs[0] = 0;
6341 }
6342 ovfl_val = pmu_conf->ovfl_val;
6343 /*
6344 * we save all the used pmds
6345 * we take care of overflows for counting PMDs
6346 *
6347 * XXX: sampling situation is not taken into account here
6348 */
6349 mask2 = ctx->ctx_used_pmds[0];
6350
6351 DPRINT(("is_self=%d ovfl_val=0x%lx mask2=0x%lx\n", is_self, ovfl_val, mask2));
6352
6353 for (i = 0; mask2; i++, mask2>>=1) {
6354
6355 /* skip non used pmds */
6356 if ((mask2 & 0x1) == 0) continue;
6357
6358 /*
6359 * can access PMU always true in system wide mode
6360 */
6361 val = pmd_val = can_access_pmu ? ia64_get_pmd(i) : task->thread.pmds[i];
6362
6363 if (PMD_IS_COUNTING(i)) {
6364 DPRINT(("[%d] pmd[%d] ctx_pmd=0x%lx hw_pmd=0x%lx\n",
6365 task->pid,
6366 i,
6367 ctx->ctx_pmds[i].val,
6368 val & ovfl_val));
6369
6370 /*
6371 * we rebuild the full 64 bit value of the counter
6372 */
6373 val = ctx->ctx_pmds[i].val + (val & ovfl_val);
6374
6375 /*
6376 * now everything is in ctx_pmds[] and we need
6377 * to clear the saved context from save_regs() such that
6378 * pfm_read_pmds() gets the correct value
6379 */
6380 pmd_val = 0UL;
6381
6382 /*
6383 * take care of overflow inline
6384 */
6385 if (pmc0 & (1UL << i)) {
6386 val += 1 + ovfl_val;
6387 DPRINT(("[%d] pmd[%d] overflowed\n", task->pid, i));
6388 }
6389 }
6390
6391 DPRINT(("[%d] ctx_pmd[%d]=0x%lx pmd_val=0x%lx\n", task->pid, i, val, pmd_val));
6392
6393 if (is_self) task->thread.pmds[i] = pmd_val;
6394
6395 ctx->ctx_pmds[i].val = val;
6396 }
6397}
6398
6399static struct irqaction perfmon_irqaction = {
6400 .handler = pfm_interrupt_handler,
6401 .flags = SA_INTERRUPT,
6402 .name = "perfmon"
6403};
6404
6405/*
6406 * perfmon initialization routine, called from the initcall() table
6407 */
6408static int init_pfm_fs(void);
6409
6410static int __init
6411pfm_probe_pmu(void)
6412{
6413 pmu_config_t **p;
6414 int family;
6415
6416 family = local_cpu_data->family;
6417 p = pmu_confs;
6418
6419 while(*p) {
6420 if ((*p)->probe) {
6421 if ((*p)->probe() == 0) goto found;
6422 } else if ((*p)->pmu_family == family || (*p)->pmu_family == 0xff) {
6423 goto found;
6424 }
6425 p++;
6426 }
6427 return -1;
6428found:
6429 pmu_conf = *p;
6430 return 0;
6431}
6432
6433static struct file_operations pfm_proc_fops = {
6434 .open = pfm_proc_open,
6435 .read = seq_read,
6436 .llseek = seq_lseek,
6437 .release = seq_release,
6438};
6439
6440int __init
6441pfm_init(void)
6442{
6443 unsigned int n, n_counters, i;
6444
6445 printk("perfmon: version %u.%u IRQ %u\n",
6446 PFM_VERSION_MAJ,
6447 PFM_VERSION_MIN,
6448 IA64_PERFMON_VECTOR);
6449
6450 if (pfm_probe_pmu()) {
6451 printk(KERN_INFO "perfmon: disabled, there is no support for processor family %d\n",
6452 local_cpu_data->family);
6453 return -ENODEV;
6454 }
6455
6456 /*
6457 * compute the number of implemented PMD/PMC from the
6458 * description tables
6459 */
6460 n = 0;
6461 for (i=0; PMC_IS_LAST(i) == 0; i++) {
6462 if (PMC_IS_IMPL(i) == 0) continue;
6463 pmu_conf->impl_pmcs[i>>6] |= 1UL << (i&63);
6464 n++;
6465 }
6466 pmu_conf->num_pmcs = n;
6467
6468 n = 0; n_counters = 0;
6469 for (i=0; PMD_IS_LAST(i) == 0; i++) {
6470 if (PMD_IS_IMPL(i) == 0) continue;
6471 pmu_conf->impl_pmds[i>>6] |= 1UL << (i&63);
6472 n++;
6473 if (PMD_IS_COUNTING(i)) n_counters++;
6474 }
6475 pmu_conf->num_pmds = n;
6476 pmu_conf->num_counters = n_counters;
6477
6478 /*
6479 * sanity checks on the number of debug registers
6480 */
6481 if (pmu_conf->use_rr_dbregs) {
6482 if (pmu_conf->num_ibrs > IA64_NUM_DBG_REGS) {
6483 printk(KERN_INFO "perfmon: unsupported number of code debug registers (%u)\n", pmu_conf->num_ibrs);
6484 pmu_conf = NULL;
6485 return -1;
6486 }
6487 if (pmu_conf->num_dbrs > IA64_NUM_DBG_REGS) {
6488 printk(KERN_INFO "perfmon: unsupported number of data debug registers (%u)\n", pmu_conf->num_ibrs);
6489 pmu_conf = NULL;
6490 return -1;
6491 }
6492 }
6493
6494 printk("perfmon: %s PMU detected, %u PMCs, %u PMDs, %u counters (%lu bits)\n",
6495 pmu_conf->pmu_name,
6496 pmu_conf->num_pmcs,
6497 pmu_conf->num_pmds,
6498 pmu_conf->num_counters,
6499 ffz(pmu_conf->ovfl_val));
6500
6501 /* sanity check */
6502 if (pmu_conf->num_pmds >= IA64_NUM_PMD_REGS || pmu_conf->num_pmcs >= IA64_NUM_PMC_REGS) {
6503 printk(KERN_ERR "perfmon: not enough pmc/pmd, perfmon disabled\n");
6504 pmu_conf = NULL;
6505 return -1;
6506 }
6507
6508 /*
6509 * create /proc/perfmon (mostly for debugging purposes)
6510 */
6511 perfmon_dir = create_proc_entry("perfmon", S_IRUGO, NULL);
6512 if (perfmon_dir == NULL) {
6513 printk(KERN_ERR "perfmon: cannot create /proc entry, perfmon disabled\n");
6514 pmu_conf = NULL;
6515 return -1;
6516 }
6517 /*
6518 * install customized file operations for /proc/perfmon entry
6519 */
6520 perfmon_dir->proc_fops = &pfm_proc_fops;
6521
6522 /*
6523 * create /proc/sys/kernel/perfmon (for debugging purposes)
6524 */
6525 pfm_sysctl_header = register_sysctl_table(pfm_sysctl_root, 0);
6526
6527 /*
6528 * initialize all our spinlocks
6529 */
6530 spin_lock_init(&pfm_sessions.pfs_lock);
6531 spin_lock_init(&pfm_buffer_fmt_lock);
6532
6533 init_pfm_fs();
6534
6535 for(i=0; i < NR_CPUS; i++) pfm_stats[i].pfm_ovfl_intr_cycles_min = ~0UL;
6536
6537 return 0;
6538}
6539
6540__initcall(pfm_init);
6541
6542/*
6543 * this function is called before pfm_init()
6544 */
6545void
6546pfm_init_percpu (void)
6547{
6548 /*
6549 * make sure no measurement is active
6550 * (may inherit programmed PMCs from EFI).
6551 */
6552 pfm_clear_psr_pp();
6553 pfm_clear_psr_up();
6554
6555 /*
6556 * we run with the PMU not frozen at all times
6557 */
6558 pfm_unfreeze_pmu();
6559
6560 if (smp_processor_id() == 0)
6561 register_percpu_irq(IA64_PERFMON_VECTOR, &perfmon_irqaction);
6562
6563 ia64_setreg(_IA64_REG_CR_PMV, IA64_PERFMON_VECTOR);
6564 ia64_srlz_d();
6565}
6566
6567/*
6568 * used for debug purposes only
6569 */
6570void
6571dump_pmu_state(const char *from)
6572{
6573 struct task_struct *task;
6574 struct thread_struct *t;
6575 struct pt_regs *regs;
6576 pfm_context_t *ctx;
6577 unsigned long psr, dcr, info, flags;
6578 int i, this_cpu;
6579
6580 local_irq_save(flags);
6581
6582 this_cpu = smp_processor_id();
6583 regs = ia64_task_regs(current);
6584 info = PFM_CPUINFO_GET();
6585 dcr = ia64_getreg(_IA64_REG_CR_DCR);
6586
6587 if (info == 0 && ia64_psr(regs)->pp == 0 && (dcr & IA64_DCR_PP) == 0) {
6588 local_irq_restore(flags);
6589 return;
6590 }
6591
6592 printk("CPU%d from %s() current [%d] iip=0x%lx %s\n",
6593 this_cpu,
6594 from,
6595 current->pid,
6596 regs->cr_iip,
6597 current->comm);
6598
6599 task = GET_PMU_OWNER();
6600 ctx = GET_PMU_CTX();
6601
6602 printk("->CPU%d owner [%d] ctx=%p\n", this_cpu, task ? task->pid : -1, ctx);
6603
6604 psr = pfm_get_psr();
6605
6606 printk("->CPU%d pmc0=0x%lx psr.pp=%d psr.up=%d dcr.pp=%d syst_info=0x%lx user_psr.up=%d user_psr.pp=%d\n",
6607 this_cpu,
6608 ia64_get_pmc(0),
6609 psr & IA64_PSR_PP ? 1 : 0,
6610 psr & IA64_PSR_UP ? 1 : 0,
6611 dcr & IA64_DCR_PP ? 1 : 0,
6612 info,
6613 ia64_psr(regs)->up,
6614 ia64_psr(regs)->pp);
6615
6616 ia64_psr(regs)->up = 0;
6617 ia64_psr(regs)->pp = 0;
6618
6619 t = &current->thread;
6620
6621 for (i=1; PMC_IS_LAST(i) == 0; i++) {
6622 if (PMC_IS_IMPL(i) == 0) continue;
6623 printk("->CPU%d pmc[%d]=0x%lx thread_pmc[%d]=0x%lx\n", this_cpu, i, ia64_get_pmc(i), i, t->pmcs[i]);
6624 }
6625
6626 for (i=1; PMD_IS_LAST(i) == 0; i++) {
6627 if (PMD_IS_IMPL(i) == 0) continue;
6628 printk("->CPU%d pmd[%d]=0x%lx thread_pmd[%d]=0x%lx\n", this_cpu, i, ia64_get_pmd(i), i, t->pmds[i]);
6629 }
6630
6631 if (ctx) {
6632 printk("->CPU%d ctx_state=%d vaddr=%p addr=%p fd=%d ctx_task=[%d] saved_psr_up=0x%lx\n",
6633 this_cpu,
6634 ctx->ctx_state,
6635 ctx->ctx_smpl_vaddr,
6636 ctx->ctx_smpl_hdr,
6637 ctx->ctx_msgq_head,
6638 ctx->ctx_msgq_tail,
6639 ctx->ctx_saved_psr_up);
6640 }
6641 local_irq_restore(flags);
6642}
6643
6644/*
6645 * called from process.c:copy_thread(). task is new child.
6646 */
6647void
6648pfm_inherit(struct task_struct *task, struct pt_regs *regs)
6649{
6650 struct thread_struct *thread;
6651
6652 DPRINT(("perfmon: pfm_inherit clearing state for [%d]\n", task->pid));
6653
6654 thread = &task->thread;
6655
6656 /*
6657 * cut links inherited from parent (current)
6658 */
6659 thread->pfm_context = NULL;
6660
6661 PFM_SET_WORK_PENDING(task, 0);
6662
6663 /*
6664 * the psr bits are already set properly in copy_threads()
6665 */
6666}
6667#else /* !CONFIG_PERFMON */
6668asmlinkage long
6669sys_perfmonctl (int fd, int cmd, void *arg, int count)
6670{
6671 return -ENOSYS;
6672}
6673#endif /* CONFIG_PERFMON */