2 * This file implements the perfmon-2 subsystem which is used
3 * to program the IA-64 Performance Monitoring Unit (PMU).
5 * The initial version of perfmon.c was written by
6 * Ganesh Venkitachalam, IBM Corp.
8 * Then it was modified for perfmon-1.x by Stephane Eranian and
9 * David Mosberger, Hewlett Packard Co.
11 * Version Perfmon-2.x is a rewrite of perfmon-1.x
12 * by Stephane Eranian, Hewlett Packard Co.
14 * Copyright (C) 1999-2005 Hewlett Packard Co
15 * Stephane Eranian <eranian@hpl.hp.com>
16 * David Mosberger-Tang <davidm@hpl.hp.com>
18 * More information about perfmon available at:
19 * http://www.hpl.hp.com/research/linux/perfmon
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/interrupt.h>
26 #include <linux/proc_fs.h>
27 #include <linux/seq_file.h>
28 #include <linux/init.h>
29 #include <linux/vmalloc.h>
31 #include <linux/sysctl.h>
32 #include <linux/list.h>
33 #include <linux/file.h>
34 #include <linux/poll.h>
35 #include <linux/vfs.h>
36 #include <linux/smp.h>
37 #include <linux/pagemap.h>
38 #include <linux/mount.h>
39 #include <linux/bitops.h>
40 #include <linux/capability.h>
41 #include <linux/rcupdate.h>
42 #include <linux/completion.h>
43 #include <linux/tracehook.h>
44 #include <linux/slab.h>
46 #include <asm/errno.h>
47 #include <asm/intrinsics.h>
49 #include <asm/perfmon.h>
50 #include <asm/processor.h>
51 #include <asm/signal.h>
52 #include <asm/uaccess.h>
53 #include <asm/delay.h>
57 * perfmon context state
59 #define PFM_CTX_UNLOADED 1 /* context is not loaded onto any task */
60 #define PFM_CTX_LOADED 2 /* context is loaded onto a task */
61 #define PFM_CTX_MASKED 3 /* context is loaded but monitoring is masked due to overflow */
62 #define PFM_CTX_ZOMBIE 4 /* owner of the context is closing it */
64 #define PFM_INVALID_ACTIVATION (~0UL)
66 #define PFM_NUM_PMC_REGS 64 /* PMC save area for ctxsw */
67 #define PFM_NUM_PMD_REGS 64 /* PMD save area for ctxsw */
70 * depth of message queue
72 #define PFM_MAX_MSGS 32
73 #define PFM_CTXQ_EMPTY(g) ((g)->ctx_msgq_head == (g)->ctx_msgq_tail)
76 * type of a PMU register (bitmask).
78 * bit0 : register implemented
81 * bit4 : pmc has pmc.pm
82 * bit5 : pmc controls a counter (has pmc.oi), pmd is used as counter
83 * bit6-7 : register type
86 #define PFM_REG_NOTIMPL 0x0 /* not implemented at all */
87 #define PFM_REG_IMPL 0x1 /* register implemented */
88 #define PFM_REG_END 0x2 /* end marker */
89 #define PFM_REG_MONITOR (0x1<<4|PFM_REG_IMPL) /* a PMC with a pmc.pm field only */
90 #define PFM_REG_COUNTING (0x2<<4|PFM_REG_MONITOR) /* a monitor + pmc.oi+ PMD used as a counter */
91 #define PFM_REG_CONTROL (0x4<<4|PFM_REG_IMPL) /* PMU control register */
92 #define PFM_REG_CONFIG (0x8<<4|PFM_REG_IMPL) /* configuration register */
93 #define PFM_REG_BUFFER (0xc<<4|PFM_REG_IMPL) /* PMD used as buffer */
95 #define PMC_IS_LAST(i) (pmu_conf->pmc_desc[i].type & PFM_REG_END)
96 #define PMD_IS_LAST(i) (pmu_conf->pmd_desc[i].type & PFM_REG_END)
98 #define PMC_OVFL_NOTIFY(ctx, i) ((ctx)->ctx_pmds[i].flags & PFM_REGFL_OVFL_NOTIFY)
100 /* i assumed unsigned */
101 #define PMC_IS_IMPL(i) (i< PMU_MAX_PMCS && (pmu_conf->pmc_desc[i].type & PFM_REG_IMPL))
102 #define PMD_IS_IMPL(i) (i< PMU_MAX_PMDS && (pmu_conf->pmd_desc[i].type & PFM_REG_IMPL))
104 /* XXX: these assume that register i is implemented */
105 #define PMD_IS_COUNTING(i) ((pmu_conf->pmd_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
106 #define PMC_IS_COUNTING(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
107 #define PMC_IS_MONITOR(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_MONITOR) == PFM_REG_MONITOR)
108 #define PMC_IS_CONTROL(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_CONTROL) == PFM_REG_CONTROL)
110 #define PMC_DFL_VAL(i) pmu_conf->pmc_desc[i].default_value
111 #define PMC_RSVD_MASK(i) pmu_conf->pmc_desc[i].reserved_mask
112 #define PMD_PMD_DEP(i) pmu_conf->pmd_desc[i].dep_pmd[0]
113 #define PMC_PMD_DEP(i) pmu_conf->pmc_desc[i].dep_pmd[0]
115 #define PFM_NUM_IBRS IA64_NUM_DBG_REGS
116 #define PFM_NUM_DBRS IA64_NUM_DBG_REGS
118 #define CTX_OVFL_NOBLOCK(c) ((c)->ctx_fl_block == 0)
119 #define CTX_HAS_SMPL(c) ((c)->ctx_fl_is_sampling)
120 #define PFM_CTX_TASK(h) (h)->ctx_task
122 #define PMU_PMC_OI 5 /* position of pmc.oi bit */
124 /* XXX: does not support more than 64 PMDs */
125 #define CTX_USED_PMD(ctx, mask) (ctx)->ctx_used_pmds[0] |= (mask)
126 #define CTX_IS_USED_PMD(ctx, c) (((ctx)->ctx_used_pmds[0] & (1UL << (c))) != 0UL)
128 #define CTX_USED_MONITOR(ctx, mask) (ctx)->ctx_used_monitors[0] |= (mask)
130 #define CTX_USED_IBR(ctx,n) (ctx)->ctx_used_ibrs[(n)>>6] |= 1UL<< ((n) % 64)
131 #define CTX_USED_DBR(ctx,n) (ctx)->ctx_used_dbrs[(n)>>6] |= 1UL<< ((n) % 64)
132 #define CTX_USES_DBREGS(ctx) (((pfm_context_t *)(ctx))->ctx_fl_using_dbreg==1)
133 #define PFM_CODE_RR 0 /* requesting code range restriction */
134 #define PFM_DATA_RR 1 /* requestion data range restriction */
136 #define PFM_CPUINFO_CLEAR(v) pfm_get_cpu_var(pfm_syst_info) &= ~(v)
137 #define PFM_CPUINFO_SET(v) pfm_get_cpu_var(pfm_syst_info) |= (v)
138 #define PFM_CPUINFO_GET() pfm_get_cpu_var(pfm_syst_info)
140 #define RDEP(x) (1UL<<(x))
143 * context protection macros
145 * - we need to protect against CPU concurrency (spin_lock)
146 * - we need to protect against PMU overflow interrupts (local_irq_disable)
148 * - we need to protect against PMU overflow interrupts (local_irq_disable)
150 * spin_lock_irqsave()/spin_unlock_irqrestore():
151 * in SMP: local_irq_disable + spin_lock
152 * in UP : local_irq_disable
154 * spin_lock()/spin_lock():
155 * in UP : removed automatically
156 * in SMP: protect against context accesses from other CPU. interrupts
157 * are not masked. This is useful for the PMU interrupt handler
158 * because we know we will not get PMU concurrency in that code.
160 #define PROTECT_CTX(c, f) \
162 DPRINT(("spinlock_irq_save ctx %p by [%d]\n", c, task_pid_nr(current))); \
163 spin_lock_irqsave(&(c)->ctx_lock, f); \
164 DPRINT(("spinlocked ctx %p by [%d]\n", c, task_pid_nr(current))); \
167 #define UNPROTECT_CTX(c, f) \
169 DPRINT(("spinlock_irq_restore ctx %p by [%d]\n", c, task_pid_nr(current))); \
170 spin_unlock_irqrestore(&(c)->ctx_lock, f); \
173 #define PROTECT_CTX_NOPRINT(c, f) \
175 spin_lock_irqsave(&(c)->ctx_lock, f); \
179 #define UNPROTECT_CTX_NOPRINT(c, f) \
181 spin_unlock_irqrestore(&(c)->ctx_lock, f); \
185 #define PROTECT_CTX_NOIRQ(c) \
187 spin_lock(&(c)->ctx_lock); \
190 #define UNPROTECT_CTX_NOIRQ(c) \
192 spin_unlock(&(c)->ctx_lock); \
198 #define GET_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)
199 #define INC_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)++
200 #define SET_ACTIVATION(c) (c)->ctx_last_activation = GET_ACTIVATION()
202 #else /* !CONFIG_SMP */
203 #define SET_ACTIVATION(t) do {} while(0)
204 #define GET_ACTIVATION(t) do {} while(0)
205 #define INC_ACTIVATION(t) do {} while(0)
206 #endif /* CONFIG_SMP */
208 #define SET_PMU_OWNER(t, c) do { pfm_get_cpu_var(pmu_owner) = (t); pfm_get_cpu_var(pmu_ctx) = (c); } while(0)
209 #define GET_PMU_OWNER() pfm_get_cpu_var(pmu_owner)
210 #define GET_PMU_CTX() pfm_get_cpu_var(pmu_ctx)
212 #define LOCK_PFS(g) spin_lock_irqsave(&pfm_sessions.pfs_lock, g)
213 #define UNLOCK_PFS(g) spin_unlock_irqrestore(&pfm_sessions.pfs_lock, g)
215 #define PFM_REG_RETFLAG_SET(flags, val) do { flags &= ~PFM_REG_RETFL_MASK; flags |= (val); } while(0)
218 * cmp0 must be the value of pmc0
220 #define PMC0_HAS_OVFL(cmp0) (cmp0 & ~0x1UL)
222 #define PFMFS_MAGIC 0xa0b4d889
227 #define PFM_DEBUGGING 1
231 if (unlikely(pfm_sysctl.debug >0)) { printk("%s.%d: CPU%d [%d] ", __func__, __LINE__, smp_processor_id(), task_pid_nr(current)); printk a; } \
234 #define DPRINT_ovfl(a) \
236 if (unlikely(pfm_sysctl.debug > 0 && pfm_sysctl.debug_ovfl >0)) { printk("%s.%d: CPU%d [%d] ", __func__, __LINE__, smp_processor_id(), task_pid_nr(current)); printk a; } \
241 * 64-bit software counter structure
243 * the next_reset_type is applied to the next call to pfm_reset_regs()
246 unsigned long val; /* virtual 64bit counter value */
247 unsigned long lval; /* last reset value */
248 unsigned long long_reset; /* reset value on sampling overflow */
249 unsigned long short_reset; /* reset value on overflow */
250 unsigned long reset_pmds[4]; /* which other pmds to reset when this counter overflows */
251 unsigned long smpl_pmds[4]; /* which pmds are accessed when counter overflow */
252 unsigned long seed; /* seed for random-number generator */
253 unsigned long mask; /* mask for random-number generator */
254 unsigned int flags; /* notify/do not notify */
255 unsigned long eventid; /* overflow event identifier */
262 unsigned int block:1; /* when 1, task will blocked on user notifications */
263 unsigned int system:1; /* do system wide monitoring */
264 unsigned int using_dbreg:1; /* using range restrictions (debug registers) */
265 unsigned int is_sampling:1; /* true if using a custom format */
266 unsigned int excl_idle:1; /* exclude idle task in system wide session */
267 unsigned int going_zombie:1; /* context is zombie (MASKED+blocking) */
268 unsigned int trap_reason:2; /* reason for going into pfm_handle_work() */
269 unsigned int no_msg:1; /* no message sent on overflow */
270 unsigned int can_restart:1; /* allowed to issue a PFM_RESTART */
271 unsigned int reserved:22;
272 } pfm_context_flags_t;
274 #define PFM_TRAP_REASON_NONE 0x0 /* default value */
275 #define PFM_TRAP_REASON_BLOCK 0x1 /* we need to block on overflow */
276 #define PFM_TRAP_REASON_RESET 0x2 /* we need to reset PMDs */
280 * perfmon context: encapsulates all the state of a monitoring session
283 typedef struct pfm_context {
284 spinlock_t ctx_lock; /* context protection */
286 pfm_context_flags_t ctx_flags; /* bitmask of flags (block reason incl.) */
287 unsigned int ctx_state; /* state: active/inactive (no bitfield) */
289 struct task_struct *ctx_task; /* task to which context is attached */
291 unsigned long ctx_ovfl_regs[4]; /* which registers overflowed (notification) */
293 struct completion ctx_restart_done; /* use for blocking notification mode */
295 unsigned long ctx_used_pmds[4]; /* bitmask of PMD used */
296 unsigned long ctx_all_pmds[4]; /* bitmask of all accessible PMDs */
297 unsigned long ctx_reload_pmds[4]; /* bitmask of force reload PMD on ctxsw in */
299 unsigned long ctx_all_pmcs[4]; /* bitmask of all accessible PMCs */
300 unsigned long ctx_reload_pmcs[4]; /* bitmask of force reload PMC on ctxsw in */
301 unsigned long ctx_used_monitors[4]; /* bitmask of monitor PMC being used */
303 unsigned long ctx_pmcs[PFM_NUM_PMC_REGS]; /* saved copies of PMC values */
305 unsigned int ctx_used_ibrs[1]; /* bitmask of used IBR (speedup ctxsw in) */
306 unsigned int ctx_used_dbrs[1]; /* bitmask of used DBR (speedup ctxsw in) */
307 unsigned long ctx_dbrs[IA64_NUM_DBG_REGS]; /* DBR values (cache) when not loaded */
308 unsigned long ctx_ibrs[IA64_NUM_DBG_REGS]; /* IBR values (cache) when not loaded */
310 pfm_counter_t ctx_pmds[PFM_NUM_PMD_REGS]; /* software state for PMDS */
312 unsigned long th_pmcs[PFM_NUM_PMC_REGS]; /* PMC thread save state */
313 unsigned long th_pmds[PFM_NUM_PMD_REGS]; /* PMD thread save state */
315 unsigned long ctx_saved_psr_up; /* only contains psr.up value */
317 unsigned long ctx_last_activation; /* context last activation number for last_cpu */
318 unsigned int ctx_last_cpu; /* CPU id of current or last CPU used (SMP only) */
319 unsigned int ctx_cpu; /* cpu to which perfmon is applied (system wide) */
321 int ctx_fd; /* file descriptor used my this context */
322 pfm_ovfl_arg_t ctx_ovfl_arg; /* argument to custom buffer format handler */
324 pfm_buffer_fmt_t *ctx_buf_fmt; /* buffer format callbacks */
325 void *ctx_smpl_hdr; /* points to sampling buffer header kernel vaddr */
326 unsigned long ctx_smpl_size; /* size of sampling buffer */
327 void *ctx_smpl_vaddr; /* user level virtual address of smpl buffer */
329 wait_queue_head_t ctx_msgq_wait;
330 pfm_msg_t ctx_msgq[PFM_MAX_MSGS];
333 struct fasync_struct *ctx_async_queue;
335 wait_queue_head_t ctx_zombieq; /* termination cleanup wait queue */
339 * magic number used to verify that structure is really
342 #define PFM_IS_FILE(f) ((f)->f_op == &pfm_file_ops)
344 #define PFM_GET_CTX(t) ((pfm_context_t *)(t)->thread.pfm_context)
347 #define SET_LAST_CPU(ctx, v) (ctx)->ctx_last_cpu = (v)
348 #define GET_LAST_CPU(ctx) (ctx)->ctx_last_cpu
350 #define SET_LAST_CPU(ctx, v) do {} while(0)
351 #define GET_LAST_CPU(ctx) do {} while(0)
355 #define ctx_fl_block ctx_flags.block
356 #define ctx_fl_system ctx_flags.system
357 #define ctx_fl_using_dbreg ctx_flags.using_dbreg
358 #define ctx_fl_is_sampling ctx_flags.is_sampling
359 #define ctx_fl_excl_idle ctx_flags.excl_idle
360 #define ctx_fl_going_zombie ctx_flags.going_zombie
361 #define ctx_fl_trap_reason ctx_flags.trap_reason
362 #define ctx_fl_no_msg ctx_flags.no_msg
363 #define ctx_fl_can_restart ctx_flags.can_restart
365 #define PFM_SET_WORK_PENDING(t, v) do { (t)->thread.pfm_needs_checking = v; } while(0);
366 #define PFM_GET_WORK_PENDING(t) (t)->thread.pfm_needs_checking
369 * global information about all sessions
370 * mostly used to synchronize between system wide and per-process
373 spinlock_t pfs_lock; /* lock the structure */
375 unsigned int pfs_task_sessions; /* number of per task sessions */
376 unsigned int pfs_sys_sessions; /* number of per system wide sessions */
377 unsigned int pfs_sys_use_dbregs; /* incremented when a system wide session uses debug regs */
378 unsigned int pfs_ptrace_use_dbregs; /* incremented when a process uses debug regs */
379 struct task_struct *pfs_sys_session[NR_CPUS]; /* point to task owning a system-wide session */
383 * information about a PMC or PMD.
384 * dep_pmd[]: a bitmask of dependent PMD registers
385 * dep_pmc[]: a bitmask of dependent PMC registers
387 typedef int (*pfm_reg_check_t)(struct task_struct *task, pfm_context_t *ctx, unsigned int cnum, unsigned long *val, struct pt_regs *regs);
391 unsigned long default_value; /* power-on default value */
392 unsigned long reserved_mask; /* bitmask of reserved bits */
393 pfm_reg_check_t read_check;
394 pfm_reg_check_t write_check;
395 unsigned long dep_pmd[4];
396 unsigned long dep_pmc[4];
399 /* assume cnum is a valid monitor */
400 #define PMC_PM(cnum, val) (((val) >> (pmu_conf->pmc_desc[cnum].pm_pos)) & 0x1)
403 * This structure is initialized at boot time and contains
404 * a description of the PMU main characteristics.
406 * If the probe function is defined, detection is based
407 * on its return value:
408 * - 0 means recognized PMU
409 * - anything else means not supported
410 * When the probe function is not defined, then the pmu_family field
411 * is used and it must match the host CPU family such that:
412 * - cpu->family & config->pmu_family != 0
415 unsigned long ovfl_val; /* overflow value for counters */
417 pfm_reg_desc_t *pmc_desc; /* detailed PMC register dependencies descriptions */
418 pfm_reg_desc_t *pmd_desc; /* detailed PMD register dependencies descriptions */
420 unsigned int num_pmcs; /* number of PMCS: computed at init time */
421 unsigned int num_pmds; /* number of PMDS: computed at init time */
422 unsigned long impl_pmcs[4]; /* bitmask of implemented PMCS */
423 unsigned long impl_pmds[4]; /* bitmask of implemented PMDS */
425 char *pmu_name; /* PMU family name */
426 unsigned int pmu_family; /* cpuid family pattern used to identify pmu */
427 unsigned int flags; /* pmu specific flags */
428 unsigned int num_ibrs; /* number of IBRS: computed at init time */
429 unsigned int num_dbrs; /* number of DBRS: computed at init time */
430 unsigned int num_counters; /* PMC/PMD counting pairs : computed at init time */
431 int (*probe)(void); /* customized probe routine */
432 unsigned int use_rr_dbregs:1; /* set if debug registers used for range restriction */
437 #define PFM_PMU_IRQ_RESEND 1 /* PMU needs explicit IRQ resend */
440 * debug register related type definitions
443 unsigned long ibr_mask:56;
444 unsigned long ibr_plm:4;
445 unsigned long ibr_ig:3;
446 unsigned long ibr_x:1;
450 unsigned long dbr_mask:56;
451 unsigned long dbr_plm:4;
452 unsigned long dbr_ig:2;
453 unsigned long dbr_w:1;
454 unsigned long dbr_r:1;
465 * perfmon command descriptions
468 int (*cmd_func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
471 unsigned int cmd_narg;
473 int (*cmd_getsize)(void *arg, size_t *sz);
476 #define PFM_CMD_FD 0x01 /* command requires a file descriptor */
477 #define PFM_CMD_ARG_READ 0x02 /* command must read argument(s) */
478 #define PFM_CMD_ARG_RW 0x04 /* command must read/write argument(s) */
479 #define PFM_CMD_STOP 0x08 /* command does not work on zombie context */
482 #define PFM_CMD_NAME(cmd) pfm_cmd_tab[(cmd)].cmd_name
483 #define PFM_CMD_READ_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_READ)
484 #define PFM_CMD_RW_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_RW)
485 #define PFM_CMD_USE_FD(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_FD)
486 #define PFM_CMD_STOPPED(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_STOP)
488 #define PFM_CMD_ARG_MANY -1 /* cannot be zero */
491 unsigned long pfm_spurious_ovfl_intr_count; /* keep track of spurious ovfl interrupts */
492 unsigned long pfm_replay_ovfl_intr_count; /* keep track of replayed ovfl interrupts */
493 unsigned long pfm_ovfl_intr_count; /* keep track of ovfl interrupts */
494 unsigned long pfm_ovfl_intr_cycles; /* cycles spent processing ovfl interrupts */
495 unsigned long pfm_ovfl_intr_cycles_min; /* min cycles spent processing ovfl interrupts */
496 unsigned long pfm_ovfl_intr_cycles_max; /* max cycles spent processing ovfl interrupts */
497 unsigned long pfm_smpl_handler_calls;
498 unsigned long pfm_smpl_handler_cycles;
499 char pad[SMP_CACHE_BYTES] ____cacheline_aligned;
503 * perfmon internal variables
505 static pfm_stats_t pfm_stats[NR_CPUS];
506 static pfm_session_t pfm_sessions; /* global sessions information */
508 static DEFINE_SPINLOCK(pfm_alt_install_check);
509 static pfm_intr_handler_desc_t *pfm_alt_intr_handler;
511 static struct proc_dir_entry *perfmon_dir;
512 static pfm_uuid_t pfm_null_uuid = {0,};
514 static spinlock_t pfm_buffer_fmt_lock;
515 static LIST_HEAD(pfm_buffer_fmt_list);
517 static pmu_config_t *pmu_conf;
519 /* sysctl() controls */
520 pfm_sysctl_t pfm_sysctl;
521 EXPORT_SYMBOL(pfm_sysctl);
523 static ctl_table pfm_ctl_table[]={
526 .data = &pfm_sysctl.debug,
527 .maxlen = sizeof(int),
529 .proc_handler = proc_dointvec,
532 .procname = "debug_ovfl",
533 .data = &pfm_sysctl.debug_ovfl,
534 .maxlen = sizeof(int),
536 .proc_handler = proc_dointvec,
539 .procname = "fastctxsw",
540 .data = &pfm_sysctl.fastctxsw,
541 .maxlen = sizeof(int),
543 .proc_handler = proc_dointvec,
546 .procname = "expert_mode",
547 .data = &pfm_sysctl.expert_mode,
548 .maxlen = sizeof(int),
550 .proc_handler = proc_dointvec,
554 static ctl_table pfm_sysctl_dir[] = {
556 .procname = "perfmon",
558 .child = pfm_ctl_table,
562 static ctl_table pfm_sysctl_root[] = {
564 .procname = "kernel",
566 .child = pfm_sysctl_dir,
570 static struct ctl_table_header *pfm_sysctl_header;
572 static int pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
574 #define pfm_get_cpu_var(v) __ia64_per_cpu_var(v)
575 #define pfm_get_cpu_data(a,b) per_cpu(a, b)
578 pfm_put_task(struct task_struct *task)
580 if (task != current) put_task_struct(task);
584 pfm_reserve_page(unsigned long a)
586 SetPageReserved(vmalloc_to_page((void *)a));
589 pfm_unreserve_page(unsigned long a)
591 ClearPageReserved(vmalloc_to_page((void*)a));
594 static inline unsigned long
595 pfm_protect_ctx_ctxsw(pfm_context_t *x)
597 spin_lock(&(x)->ctx_lock);
602 pfm_unprotect_ctx_ctxsw(pfm_context_t *x, unsigned long f)
604 spin_unlock(&(x)->ctx_lock);
607 static inline unsigned long
608 pfm_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags, unsigned long exec)
610 return get_unmapped_area(file, addr, len, pgoff, flags);
613 /* forward declaration */
614 static const struct dentry_operations pfmfs_dentry_operations;
616 static struct dentry *
617 pfmfs_mount(struct file_system_type *fs_type, int flags, const char *dev_name, void *data)
619 return mount_pseudo(fs_type, "pfm:", NULL, &pfmfs_dentry_operations,
623 static struct file_system_type pfm_fs_type = {
625 .mount = pfmfs_mount,
626 .kill_sb = kill_anon_super,
629 DEFINE_PER_CPU(unsigned long, pfm_syst_info);
630 DEFINE_PER_CPU(struct task_struct *, pmu_owner);
631 DEFINE_PER_CPU(pfm_context_t *, pmu_ctx);
632 DEFINE_PER_CPU(unsigned long, pmu_activation_number);
633 EXPORT_PER_CPU_SYMBOL_GPL(pfm_syst_info);
636 /* forward declaration */
637 static const struct file_operations pfm_file_ops;
640 * forward declarations
643 static void pfm_lazy_save_regs (struct task_struct *ta);
646 void dump_pmu_state(const char *);
647 static int pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
649 #include "perfmon_itanium.h"
650 #include "perfmon_mckinley.h"
651 #include "perfmon_montecito.h"
652 #include "perfmon_generic.h"
654 static pmu_config_t *pmu_confs[]={
658 &pmu_conf_gen, /* must be last */
663 static int pfm_end_notify_user(pfm_context_t *ctx);
666 pfm_clear_psr_pp(void)
668 ia64_rsm(IA64_PSR_PP);
675 ia64_ssm(IA64_PSR_PP);
680 pfm_clear_psr_up(void)
682 ia64_rsm(IA64_PSR_UP);
689 ia64_ssm(IA64_PSR_UP);
693 static inline unsigned long
697 tmp = ia64_getreg(_IA64_REG_PSR);
703 pfm_set_psr_l(unsigned long val)
705 ia64_setreg(_IA64_REG_PSR_L, val);
717 pfm_unfreeze_pmu(void)
724 pfm_restore_ibrs(unsigned long *ibrs, unsigned int nibrs)
728 for (i=0; i < nibrs; i++) {
729 ia64_set_ibr(i, ibrs[i]);
730 ia64_dv_serialize_instruction();
736 pfm_restore_dbrs(unsigned long *dbrs, unsigned int ndbrs)
740 for (i=0; i < ndbrs; i++) {
741 ia64_set_dbr(i, dbrs[i]);
742 ia64_dv_serialize_data();
748 * PMD[i] must be a counter. no check is made
750 static inline unsigned long
751 pfm_read_soft_counter(pfm_context_t *ctx, int i)
753 return ctx->ctx_pmds[i].val + (ia64_get_pmd(i) & pmu_conf->ovfl_val);
757 * PMD[i] must be a counter. no check is made
760 pfm_write_soft_counter(pfm_context_t *ctx, int i, unsigned long val)
762 unsigned long ovfl_val = pmu_conf->ovfl_val;
764 ctx->ctx_pmds[i].val = val & ~ovfl_val;
766 * writing to unimplemented part is ignore, so we do not need to
769 ia64_set_pmd(i, val & ovfl_val);
773 pfm_get_new_msg(pfm_context_t *ctx)
777 next = (ctx->ctx_msgq_tail+1) % PFM_MAX_MSGS;
779 DPRINT(("ctx_fd=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
780 if (next == ctx->ctx_msgq_head) return NULL;
782 idx = ctx->ctx_msgq_tail;
783 ctx->ctx_msgq_tail = next;
785 DPRINT(("ctx=%p head=%d tail=%d msg=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail, idx));
787 return ctx->ctx_msgq+idx;
791 pfm_get_next_msg(pfm_context_t *ctx)
795 DPRINT(("ctx=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
797 if (PFM_CTXQ_EMPTY(ctx)) return NULL;
802 msg = ctx->ctx_msgq+ctx->ctx_msgq_head;
807 ctx->ctx_msgq_head = (ctx->ctx_msgq_head+1) % PFM_MAX_MSGS;
809 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));
815 pfm_reset_msgq(pfm_context_t *ctx)
817 ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
818 DPRINT(("ctx=%p msgq reset\n", ctx));
822 pfm_rvmalloc(unsigned long size)
827 size = PAGE_ALIGN(size);
830 //printk("perfmon: CPU%d pfm_rvmalloc(%ld)=%p\n", smp_processor_id(), size, mem);
831 addr = (unsigned long)mem;
833 pfm_reserve_page(addr);
842 pfm_rvfree(void *mem, unsigned long size)
847 DPRINT(("freeing physical buffer @%p size=%lu\n", mem, size));
848 addr = (unsigned long) mem;
849 while ((long) size > 0) {
850 pfm_unreserve_page(addr);
859 static pfm_context_t *
860 pfm_context_alloc(int ctx_flags)
865 * allocate context descriptor
866 * must be able to free with interrupts disabled
868 ctx = kzalloc(sizeof(pfm_context_t), GFP_KERNEL);
870 DPRINT(("alloc ctx @%p\n", ctx));
873 * init context protection lock
875 spin_lock_init(&ctx->ctx_lock);
878 * context is unloaded
880 ctx->ctx_state = PFM_CTX_UNLOADED;
883 * initialization of context's flags
885 ctx->ctx_fl_block = (ctx_flags & PFM_FL_NOTIFY_BLOCK) ? 1 : 0;
886 ctx->ctx_fl_system = (ctx_flags & PFM_FL_SYSTEM_WIDE) ? 1: 0;
887 ctx->ctx_fl_no_msg = (ctx_flags & PFM_FL_OVFL_NO_MSG) ? 1: 0;
889 * will move to set properties
890 * ctx->ctx_fl_excl_idle = (ctx_flags & PFM_FL_EXCL_IDLE) ? 1: 0;
894 * init restart semaphore to locked
896 init_completion(&ctx->ctx_restart_done);
899 * activation is used in SMP only
901 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
902 SET_LAST_CPU(ctx, -1);
905 * initialize notification message queue
907 ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
908 init_waitqueue_head(&ctx->ctx_msgq_wait);
909 init_waitqueue_head(&ctx->ctx_zombieq);
916 pfm_context_free(pfm_context_t *ctx)
919 DPRINT(("free ctx @%p\n", ctx));
925 pfm_mask_monitoring(struct task_struct *task)
927 pfm_context_t *ctx = PFM_GET_CTX(task);
928 unsigned long mask, val, ovfl_mask;
931 DPRINT_ovfl(("masking monitoring for [%d]\n", task_pid_nr(task)));
933 ovfl_mask = pmu_conf->ovfl_val;
935 * monitoring can only be masked as a result of a valid
936 * counter overflow. In UP, it means that the PMU still
937 * has an owner. Note that the owner can be different
938 * from the current task. However the PMU state belongs
940 * In SMP, a valid overflow only happens when task is
941 * current. Therefore if we come here, we know that
942 * the PMU state belongs to the current task, therefore
943 * we can access the live registers.
945 * So in both cases, the live register contains the owner's
946 * state. We can ONLY touch the PMU registers and NOT the PSR.
948 * As a consequence to this call, the ctx->th_pmds[] array
949 * contains stale information which must be ignored
950 * when context is reloaded AND monitoring is active (see
953 mask = ctx->ctx_used_pmds[0];
954 for (i = 0; mask; i++, mask>>=1) {
955 /* skip non used pmds */
956 if ((mask & 0x1) == 0) continue;
957 val = ia64_get_pmd(i);
959 if (PMD_IS_COUNTING(i)) {
961 * we rebuild the full 64 bit value of the counter
963 ctx->ctx_pmds[i].val += (val & ovfl_mask);
965 ctx->ctx_pmds[i].val = val;
967 DPRINT_ovfl(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
969 ctx->ctx_pmds[i].val,
973 * mask monitoring by setting the privilege level to 0
974 * we cannot use psr.pp/psr.up for this, it is controlled by
977 * if task is current, modify actual registers, otherwise modify
978 * thread save state, i.e., what will be restored in pfm_load_regs()
980 mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
981 for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
982 if ((mask & 0x1) == 0UL) continue;
983 ia64_set_pmc(i, ctx->th_pmcs[i] & ~0xfUL);
984 ctx->th_pmcs[i] &= ~0xfUL;
985 DPRINT_ovfl(("pmc[%d]=0x%lx\n", i, ctx->th_pmcs[i]));
988 * make all of this visible
994 * must always be done with task == current
996 * context must be in MASKED state when calling
999 pfm_restore_monitoring(struct task_struct *task)
1001 pfm_context_t *ctx = PFM_GET_CTX(task);
1002 unsigned long mask, ovfl_mask;
1003 unsigned long psr, val;
1006 is_system = ctx->ctx_fl_system;
1007 ovfl_mask = pmu_conf->ovfl_val;
1009 if (task != current) {
1010 printk(KERN_ERR "perfmon.%d: invalid task[%d] current[%d]\n", __LINE__, task_pid_nr(task), task_pid_nr(current));
1013 if (ctx->ctx_state != PFM_CTX_MASKED) {
1014 printk(KERN_ERR "perfmon.%d: task[%d] current[%d] invalid state=%d\n", __LINE__,
1015 task_pid_nr(task), task_pid_nr(current), ctx->ctx_state);
1018 psr = pfm_get_psr();
1020 * monitoring is masked via the PMC.
1021 * As we restore their value, we do not want each counter to
1022 * restart right away. We stop monitoring using the PSR,
1023 * restore the PMC (and PMD) and then re-establish the psr
1024 * as it was. Note that there can be no pending overflow at
1025 * this point, because monitoring was MASKED.
1027 * system-wide session are pinned and self-monitoring
1029 if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
1030 /* disable dcr pp */
1031 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
1037 * first, we restore the PMD
1039 mask = ctx->ctx_used_pmds[0];
1040 for (i = 0; mask; i++, mask>>=1) {
1041 /* skip non used pmds */
1042 if ((mask & 0x1) == 0) continue;
1044 if (PMD_IS_COUNTING(i)) {
1046 * we split the 64bit value according to
1049 val = ctx->ctx_pmds[i].val & ovfl_mask;
1050 ctx->ctx_pmds[i].val &= ~ovfl_mask;
1052 val = ctx->ctx_pmds[i].val;
1054 ia64_set_pmd(i, val);
1056 DPRINT(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
1058 ctx->ctx_pmds[i].val,
1064 mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
1065 for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
1066 if ((mask & 0x1) == 0UL) continue;
1067 ctx->th_pmcs[i] = ctx->ctx_pmcs[i];
1068 ia64_set_pmc(i, ctx->th_pmcs[i]);
1069 DPRINT(("[%d] pmc[%d]=0x%lx\n",
1070 task_pid_nr(task), i, ctx->th_pmcs[i]));
1075 * must restore DBR/IBR because could be modified while masked
1076 * XXX: need to optimize
1078 if (ctx->ctx_fl_using_dbreg) {
1079 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
1080 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
1086 if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
1088 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
1095 pfm_save_pmds(unsigned long *pmds, unsigned long mask)
1101 for (i=0; mask; i++, mask>>=1) {
1102 if (mask & 0x1) pmds[i] = ia64_get_pmd(i);
1107 * reload from thread state (used for ctxw only)
1110 pfm_restore_pmds(unsigned long *pmds, unsigned long mask)
1113 unsigned long val, ovfl_val = pmu_conf->ovfl_val;
1115 for (i=0; mask; i++, mask>>=1) {
1116 if ((mask & 0x1) == 0) continue;
1117 val = PMD_IS_COUNTING(i) ? pmds[i] & ovfl_val : pmds[i];
1118 ia64_set_pmd(i, val);
1124 * propagate PMD from context to thread-state
1127 pfm_copy_pmds(struct task_struct *task, pfm_context_t *ctx)
1129 unsigned long ovfl_val = pmu_conf->ovfl_val;
1130 unsigned long mask = ctx->ctx_all_pmds[0];
1134 DPRINT(("mask=0x%lx\n", mask));
1136 for (i=0; mask; i++, mask>>=1) {
1138 val = ctx->ctx_pmds[i].val;
1141 * We break up the 64 bit value into 2 pieces
1142 * the lower bits go to the machine state in the
1143 * thread (will be reloaded on ctxsw in).
1144 * The upper part stays in the soft-counter.
1146 if (PMD_IS_COUNTING(i)) {
1147 ctx->ctx_pmds[i].val = val & ~ovfl_val;
1150 ctx->th_pmds[i] = val;
1152 DPRINT(("pmd[%d]=0x%lx soft_val=0x%lx\n",
1155 ctx->ctx_pmds[i].val));
1160 * propagate PMC from context to thread-state
1163 pfm_copy_pmcs(struct task_struct *task, pfm_context_t *ctx)
1165 unsigned long mask = ctx->ctx_all_pmcs[0];
1168 DPRINT(("mask=0x%lx\n", mask));
1170 for (i=0; mask; i++, mask>>=1) {
1171 /* masking 0 with ovfl_val yields 0 */
1172 ctx->th_pmcs[i] = ctx->ctx_pmcs[i];
1173 DPRINT(("pmc[%d]=0x%lx\n", i, ctx->th_pmcs[i]));
1180 pfm_restore_pmcs(unsigned long *pmcs, unsigned long mask)
1184 for (i=0; mask; i++, mask>>=1) {
1185 if ((mask & 0x1) == 0) continue;
1186 ia64_set_pmc(i, pmcs[i]);
1192 pfm_uuid_cmp(pfm_uuid_t a, pfm_uuid_t b)
1194 return memcmp(a, b, sizeof(pfm_uuid_t));
1198 pfm_buf_fmt_exit(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, struct pt_regs *regs)
1201 if (fmt->fmt_exit) ret = (*fmt->fmt_exit)(task, buf, regs);
1206 pfm_buf_fmt_getsize(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags, int cpu, void *arg, unsigned long *size)
1209 if (fmt->fmt_getsize) ret = (*fmt->fmt_getsize)(task, flags, cpu, arg, size);
1215 pfm_buf_fmt_validate(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags,
1219 if (fmt->fmt_validate) ret = (*fmt->fmt_validate)(task, flags, cpu, arg);
1224 pfm_buf_fmt_init(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, unsigned int flags,
1228 if (fmt->fmt_init) ret = (*fmt->fmt_init)(task, buf, flags, cpu, arg);
1233 pfm_buf_fmt_restart(pfm_buffer_fmt_t *fmt, struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
1236 if (fmt->fmt_restart) ret = (*fmt->fmt_restart)(task, ctrl, buf, regs);
1241 pfm_buf_fmt_restart_active(pfm_buffer_fmt_t *fmt, struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
1244 if (fmt->fmt_restart_active) ret = (*fmt->fmt_restart_active)(task, ctrl, buf, regs);
1248 static pfm_buffer_fmt_t *
1249 __pfm_find_buffer_fmt(pfm_uuid_t uuid)
1251 struct list_head * pos;
1252 pfm_buffer_fmt_t * entry;
1254 list_for_each(pos, &pfm_buffer_fmt_list) {
1255 entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
1256 if (pfm_uuid_cmp(uuid, entry->fmt_uuid) == 0)
1263 * find a buffer format based on its uuid
1265 static pfm_buffer_fmt_t *
1266 pfm_find_buffer_fmt(pfm_uuid_t uuid)
1268 pfm_buffer_fmt_t * fmt;
1269 spin_lock(&pfm_buffer_fmt_lock);
1270 fmt = __pfm_find_buffer_fmt(uuid);
1271 spin_unlock(&pfm_buffer_fmt_lock);
1276 pfm_register_buffer_fmt(pfm_buffer_fmt_t *fmt)
1280 /* some sanity checks */
1281 if (fmt == NULL || fmt->fmt_name == NULL) return -EINVAL;
1283 /* we need at least a handler */
1284 if (fmt->fmt_handler == NULL) return -EINVAL;
1287 * XXX: need check validity of fmt_arg_size
1290 spin_lock(&pfm_buffer_fmt_lock);
1292 if (__pfm_find_buffer_fmt(fmt->fmt_uuid)) {
1293 printk(KERN_ERR "perfmon: duplicate sampling format: %s\n", fmt->fmt_name);
1297 list_add(&fmt->fmt_list, &pfm_buffer_fmt_list);
1298 printk(KERN_INFO "perfmon: added sampling format %s\n", fmt->fmt_name);
1301 spin_unlock(&pfm_buffer_fmt_lock);
1304 EXPORT_SYMBOL(pfm_register_buffer_fmt);
1307 pfm_unregister_buffer_fmt(pfm_uuid_t uuid)
1309 pfm_buffer_fmt_t *fmt;
1312 spin_lock(&pfm_buffer_fmt_lock);
1314 fmt = __pfm_find_buffer_fmt(uuid);
1316 printk(KERN_ERR "perfmon: cannot unregister format, not found\n");
1320 list_del_init(&fmt->fmt_list);
1321 printk(KERN_INFO "perfmon: removed sampling format: %s\n", fmt->fmt_name);
1324 spin_unlock(&pfm_buffer_fmt_lock);
1328 EXPORT_SYMBOL(pfm_unregister_buffer_fmt);
1330 extern void update_pal_halt_status(int);
1333 pfm_reserve_session(struct task_struct *task, int is_syswide, unsigned int cpu)
1335 unsigned long flags;
1337 * validity checks on cpu_mask have been done upstream
1341 DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1342 pfm_sessions.pfs_sys_sessions,
1343 pfm_sessions.pfs_task_sessions,
1344 pfm_sessions.pfs_sys_use_dbregs,
1350 * cannot mix system wide and per-task sessions
1352 if (pfm_sessions.pfs_task_sessions > 0UL) {
1353 DPRINT(("system wide not possible, %u conflicting task_sessions\n",
1354 pfm_sessions.pfs_task_sessions));
1358 if (pfm_sessions.pfs_sys_session[cpu]) goto error_conflict;
1360 DPRINT(("reserving system wide session on CPU%u currently on CPU%u\n", cpu, smp_processor_id()));
1362 pfm_sessions.pfs_sys_session[cpu] = task;
1364 pfm_sessions.pfs_sys_sessions++ ;
1367 if (pfm_sessions.pfs_sys_sessions) goto abort;
1368 pfm_sessions.pfs_task_sessions++;
1371 DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1372 pfm_sessions.pfs_sys_sessions,
1373 pfm_sessions.pfs_task_sessions,
1374 pfm_sessions.pfs_sys_use_dbregs,
1379 * disable default_idle() to go to PAL_HALT
1381 update_pal_halt_status(0);
1388 DPRINT(("system wide not possible, conflicting session [%d] on CPU%d\n",
1389 task_pid_nr(pfm_sessions.pfs_sys_session[cpu]),
1399 pfm_unreserve_session(pfm_context_t *ctx, int is_syswide, unsigned int cpu)
1401 unsigned long flags;
1403 * validity checks on cpu_mask have been done upstream
1407 DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1408 pfm_sessions.pfs_sys_sessions,
1409 pfm_sessions.pfs_task_sessions,
1410 pfm_sessions.pfs_sys_use_dbregs,
1416 pfm_sessions.pfs_sys_session[cpu] = NULL;
1418 * would not work with perfmon+more than one bit in cpu_mask
1420 if (ctx && ctx->ctx_fl_using_dbreg) {
1421 if (pfm_sessions.pfs_sys_use_dbregs == 0) {
1422 printk(KERN_ERR "perfmon: invalid release for ctx %p sys_use_dbregs=0\n", ctx);
1424 pfm_sessions.pfs_sys_use_dbregs--;
1427 pfm_sessions.pfs_sys_sessions--;
1429 pfm_sessions.pfs_task_sessions--;
1431 DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1432 pfm_sessions.pfs_sys_sessions,
1433 pfm_sessions.pfs_task_sessions,
1434 pfm_sessions.pfs_sys_use_dbregs,
1439 * if possible, enable default_idle() to go into PAL_HALT
1441 if (pfm_sessions.pfs_task_sessions == 0 && pfm_sessions.pfs_sys_sessions == 0)
1442 update_pal_halt_status(1);
1450 * removes virtual mapping of the sampling buffer.
1451 * IMPORTANT: cannot be called with interrupts disable, e.g. inside
1452 * a PROTECT_CTX() section.
1455 pfm_remove_smpl_mapping(void *vaddr, unsigned long size)
1457 struct task_struct *task = current;
1461 if (task->mm == NULL || size == 0UL || vaddr == NULL) {
1462 printk(KERN_ERR "perfmon: pfm_remove_smpl_mapping [%d] invalid context mm=%p\n", task_pid_nr(task), task->mm);
1466 DPRINT(("smpl_vaddr=%p size=%lu\n", vaddr, size));
1469 * does the actual unmapping
1471 r = vm_munmap((unsigned long)vaddr, size);
1474 printk(KERN_ERR "perfmon: [%d] unable to unmap sampling buffer @%p size=%lu\n", task_pid_nr(task), vaddr, size);
1477 DPRINT(("do_unmap(%p, %lu)=%d\n", vaddr, size, r));
1483 * free actual physical storage used by sampling buffer
1487 pfm_free_smpl_buffer(pfm_context_t *ctx)
1489 pfm_buffer_fmt_t *fmt;
1491 if (ctx->ctx_smpl_hdr == NULL) goto invalid_free;
1494 * we won't use the buffer format anymore
1496 fmt = ctx->ctx_buf_fmt;
1498 DPRINT(("sampling buffer @%p size %lu vaddr=%p\n",
1501 ctx->ctx_smpl_vaddr));
1503 pfm_buf_fmt_exit(fmt, current, NULL, NULL);
1508 pfm_rvfree(ctx->ctx_smpl_hdr, ctx->ctx_smpl_size);
1510 ctx->ctx_smpl_hdr = NULL;
1511 ctx->ctx_smpl_size = 0UL;
1516 printk(KERN_ERR "perfmon: pfm_free_smpl_buffer [%d] no buffer\n", task_pid_nr(current));
1522 pfm_exit_smpl_buffer(pfm_buffer_fmt_t *fmt)
1524 if (fmt == NULL) return;
1526 pfm_buf_fmt_exit(fmt, current, NULL, NULL);
1531 * pfmfs should _never_ be mounted by userland - too much of security hassle,
1532 * no real gain from having the whole whorehouse mounted. So we don't need
1533 * any operations on the root directory. However, we need a non-trivial
1534 * d_name - pfm: will go nicely and kill the special-casing in procfs.
1536 static struct vfsmount *pfmfs_mnt __read_mostly;
1541 int err = register_filesystem(&pfm_fs_type);
1543 pfmfs_mnt = kern_mount(&pfm_fs_type);
1544 err = PTR_ERR(pfmfs_mnt);
1545 if (IS_ERR(pfmfs_mnt))
1546 unregister_filesystem(&pfm_fs_type);
1554 pfm_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
1559 unsigned long flags;
1560 DECLARE_WAITQUEUE(wait, current);
1561 if (PFM_IS_FILE(filp) == 0) {
1562 printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", task_pid_nr(current));
1566 ctx = filp->private_data;
1568 printk(KERN_ERR "perfmon: pfm_read: NULL ctx [%d]\n", task_pid_nr(current));
1573 * check even when there is no message
1575 if (size < sizeof(pfm_msg_t)) {
1576 DPRINT(("message is too small ctx=%p (>=%ld)\n", ctx, sizeof(pfm_msg_t)));
1580 PROTECT_CTX(ctx, flags);
1583 * put ourselves on the wait queue
1585 add_wait_queue(&ctx->ctx_msgq_wait, &wait);
1593 set_current_state(TASK_INTERRUPTIBLE);
1595 DPRINT(("head=%d tail=%d\n", ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
1598 if(PFM_CTXQ_EMPTY(ctx) == 0) break;
1600 UNPROTECT_CTX(ctx, flags);
1603 * check non-blocking read
1606 if(filp->f_flags & O_NONBLOCK) break;
1609 * check pending signals
1611 if(signal_pending(current)) {
1616 * no message, so wait
1620 PROTECT_CTX(ctx, flags);
1622 DPRINT(("[%d] back to running ret=%ld\n", task_pid_nr(current), ret));
1623 set_current_state(TASK_RUNNING);
1624 remove_wait_queue(&ctx->ctx_msgq_wait, &wait);
1626 if (ret < 0) goto abort;
1629 msg = pfm_get_next_msg(ctx);
1631 printk(KERN_ERR "perfmon: pfm_read no msg for ctx=%p [%d]\n", ctx, task_pid_nr(current));
1635 DPRINT(("fd=%d type=%d\n", msg->pfm_gen_msg.msg_ctx_fd, msg->pfm_gen_msg.msg_type));
1638 if(copy_to_user(buf, msg, sizeof(pfm_msg_t)) == 0) ret = sizeof(pfm_msg_t);
1641 UNPROTECT_CTX(ctx, flags);
1647 pfm_write(struct file *file, const char __user *ubuf,
1648 size_t size, loff_t *ppos)
1650 DPRINT(("pfm_write called\n"));
1655 pfm_poll(struct file *filp, poll_table * wait)
1658 unsigned long flags;
1659 unsigned int mask = 0;
1661 if (PFM_IS_FILE(filp) == 0) {
1662 printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", task_pid_nr(current));
1666 ctx = filp->private_data;
1668 printk(KERN_ERR "perfmon: pfm_poll: NULL ctx [%d]\n", task_pid_nr(current));
1673 DPRINT(("pfm_poll ctx_fd=%d before poll_wait\n", ctx->ctx_fd));
1675 poll_wait(filp, &ctx->ctx_msgq_wait, wait);
1677 PROTECT_CTX(ctx, flags);
1679 if (PFM_CTXQ_EMPTY(ctx) == 0)
1680 mask = POLLIN | POLLRDNORM;
1682 UNPROTECT_CTX(ctx, flags);
1684 DPRINT(("pfm_poll ctx_fd=%d mask=0x%x\n", ctx->ctx_fd, mask));
1690 pfm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1692 DPRINT(("pfm_ioctl called\n"));
1697 * interrupt cannot be masked when coming here
1700 pfm_do_fasync(int fd, struct file *filp, pfm_context_t *ctx, int on)
1704 ret = fasync_helper (fd, filp, on, &ctx->ctx_async_queue);
1706 DPRINT(("pfm_fasync called by [%d] on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
1707 task_pid_nr(current),
1710 ctx->ctx_async_queue, ret));
1716 pfm_fasync(int fd, struct file *filp, int on)
1721 if (PFM_IS_FILE(filp) == 0) {
1722 printk(KERN_ERR "perfmon: pfm_fasync bad magic [%d]\n", task_pid_nr(current));
1726 ctx = filp->private_data;
1728 printk(KERN_ERR "perfmon: pfm_fasync NULL ctx [%d]\n", task_pid_nr(current));
1732 * we cannot mask interrupts during this call because this may
1733 * may go to sleep if memory is not readily avalaible.
1735 * We are protected from the conetxt disappearing by the get_fd()/put_fd()
1736 * done in caller. Serialization of this function is ensured by caller.
1738 ret = pfm_do_fasync(fd, filp, ctx, on);
1741 DPRINT(("pfm_fasync called on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
1744 ctx->ctx_async_queue, ret));
1751 * this function is exclusively called from pfm_close().
1752 * The context is not protected at that time, nor are interrupts
1753 * on the remote CPU. That's necessary to avoid deadlocks.
1756 pfm_syswide_force_stop(void *info)
1758 pfm_context_t *ctx = (pfm_context_t *)info;
1759 struct pt_regs *regs = task_pt_regs(current);
1760 struct task_struct *owner;
1761 unsigned long flags;
1764 if (ctx->ctx_cpu != smp_processor_id()) {
1765 printk(KERN_ERR "perfmon: pfm_syswide_force_stop for CPU%d but on CPU%d\n",
1767 smp_processor_id());
1770 owner = GET_PMU_OWNER();
1771 if (owner != ctx->ctx_task) {
1772 printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected owner [%d] instead of [%d]\n",
1774 task_pid_nr(owner), task_pid_nr(ctx->ctx_task));
1777 if (GET_PMU_CTX() != ctx) {
1778 printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected ctx %p instead of %p\n",
1780 GET_PMU_CTX(), ctx);
1784 DPRINT(("on CPU%d forcing system wide stop for [%d]\n", smp_processor_id(), task_pid_nr(ctx->ctx_task)));
1786 * the context is already protected in pfm_close(), we simply
1787 * need to mask interrupts to avoid a PMU interrupt race on
1790 local_irq_save(flags);
1792 ret = pfm_context_unload(ctx, NULL, 0, regs);
1794 DPRINT(("context_unload returned %d\n", ret));
1798 * unmask interrupts, PMU interrupts are now spurious here
1800 local_irq_restore(flags);
1804 pfm_syswide_cleanup_other_cpu(pfm_context_t *ctx)
1808 DPRINT(("calling CPU%d for cleanup\n", ctx->ctx_cpu));
1809 ret = smp_call_function_single(ctx->ctx_cpu, pfm_syswide_force_stop, ctx, 1);
1810 DPRINT(("called CPU%d for cleanup ret=%d\n", ctx->ctx_cpu, ret));
1812 #endif /* CONFIG_SMP */
1815 * called for each close(). Partially free resources.
1816 * When caller is self-monitoring, the context is unloaded.
1819 pfm_flush(struct file *filp, fl_owner_t id)
1822 struct task_struct *task;
1823 struct pt_regs *regs;
1824 unsigned long flags;
1825 unsigned long smpl_buf_size = 0UL;
1826 void *smpl_buf_vaddr = NULL;
1827 int state, is_system;
1829 if (PFM_IS_FILE(filp) == 0) {
1830 DPRINT(("bad magic for\n"));
1834 ctx = filp->private_data;
1836 printk(KERN_ERR "perfmon: pfm_flush: NULL ctx [%d]\n", task_pid_nr(current));
1841 * remove our file from the async queue, if we use this mode.
1842 * This can be done without the context being protected. We come
1843 * here when the context has become unreachable by other tasks.
1845 * We may still have active monitoring at this point and we may
1846 * end up in pfm_overflow_handler(). However, fasync_helper()
1847 * operates with interrupts disabled and it cleans up the
1848 * queue. If the PMU handler is called prior to entering
1849 * fasync_helper() then it will send a signal. If it is
1850 * invoked after, it will find an empty queue and no
1851 * signal will be sent. In both case, we are safe
1853 PROTECT_CTX(ctx, flags);
1855 state = ctx->ctx_state;
1856 is_system = ctx->ctx_fl_system;
1858 task = PFM_CTX_TASK(ctx);
1859 regs = task_pt_regs(task);
1861 DPRINT(("ctx_state=%d is_current=%d\n",
1863 task == current ? 1 : 0));
1866 * if state == UNLOADED, then task is NULL
1870 * we must stop and unload because we are losing access to the context.
1872 if (task == current) {
1875 * the task IS the owner but it migrated to another CPU: that's bad
1876 * but we must handle this cleanly. Unfortunately, the kernel does
1877 * not provide a mechanism to block migration (while the context is loaded).
1879 * We need to release the resource on the ORIGINAL cpu.
1881 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
1883 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
1885 * keep context protected but unmask interrupt for IPI
1887 local_irq_restore(flags);
1889 pfm_syswide_cleanup_other_cpu(ctx);
1892 * restore interrupt masking
1894 local_irq_save(flags);
1897 * context is unloaded at this point
1900 #endif /* CONFIG_SMP */
1903 DPRINT(("forcing unload\n"));
1905 * stop and unload, returning with state UNLOADED
1906 * and session unreserved.
1908 pfm_context_unload(ctx, NULL, 0, regs);
1910 DPRINT(("ctx_state=%d\n", ctx->ctx_state));
1915 * remove virtual mapping, if any, for the calling task.
1916 * cannot reset ctx field until last user is calling close().
1918 * ctx_smpl_vaddr must never be cleared because it is needed
1919 * by every task with access to the context
1921 * When called from do_exit(), the mm context is gone already, therefore
1922 * mm is NULL, i.e., the VMA is already gone and we do not have to
1925 if (ctx->ctx_smpl_vaddr && current->mm) {
1926 smpl_buf_vaddr = ctx->ctx_smpl_vaddr;
1927 smpl_buf_size = ctx->ctx_smpl_size;
1930 UNPROTECT_CTX(ctx, flags);
1933 * if there was a mapping, then we systematically remove it
1934 * at this point. Cannot be done inside critical section
1935 * because some VM function reenables interrupts.
1938 if (smpl_buf_vaddr) pfm_remove_smpl_mapping(smpl_buf_vaddr, smpl_buf_size);
1943 * called either on explicit close() or from exit_files().
1944 * Only the LAST user of the file gets to this point, i.e., it is
1947 * IMPORTANT: we get called ONLY when the refcnt on the file gets to zero
1948 * (fput()),i.e, last task to access the file. Nobody else can access the
1949 * file at this point.
1951 * When called from exit_files(), the VMA has been freed because exit_mm()
1952 * is executed before exit_files().
1954 * When called from exit_files(), the current task is not yet ZOMBIE but we
1955 * flush the PMU state to the context.
1958 pfm_close(struct inode *inode, struct file *filp)
1961 struct task_struct *task;
1962 struct pt_regs *regs;
1963 DECLARE_WAITQUEUE(wait, current);
1964 unsigned long flags;
1965 unsigned long smpl_buf_size = 0UL;
1966 void *smpl_buf_addr = NULL;
1967 int free_possible = 1;
1968 int state, is_system;
1970 DPRINT(("pfm_close called private=%p\n", filp->private_data));
1972 if (PFM_IS_FILE(filp) == 0) {
1973 DPRINT(("bad magic\n"));
1977 ctx = filp->private_data;
1979 printk(KERN_ERR "perfmon: pfm_close: NULL ctx [%d]\n", task_pid_nr(current));
1983 PROTECT_CTX(ctx, flags);
1985 state = ctx->ctx_state;
1986 is_system = ctx->ctx_fl_system;
1988 task = PFM_CTX_TASK(ctx);
1989 regs = task_pt_regs(task);
1991 DPRINT(("ctx_state=%d is_current=%d\n",
1993 task == current ? 1 : 0));
1996 * if task == current, then pfm_flush() unloaded the context
1998 if (state == PFM_CTX_UNLOADED) goto doit;
2001 * context is loaded/masked and task != current, we need to
2002 * either force an unload or go zombie
2006 * The task is currently blocked or will block after an overflow.
2007 * we must force it to wakeup to get out of the
2008 * MASKED state and transition to the unloaded state by itself.
2010 * This situation is only possible for per-task mode
2012 if (state == PFM_CTX_MASKED && CTX_OVFL_NOBLOCK(ctx) == 0) {
2015 * set a "partial" zombie state to be checked
2016 * upon return from down() in pfm_handle_work().
2018 * We cannot use the ZOMBIE state, because it is checked
2019 * by pfm_load_regs() which is called upon wakeup from down().
2020 * In such case, it would free the context and then we would
2021 * return to pfm_handle_work() which would access the
2022 * stale context. Instead, we set a flag invisible to pfm_load_regs()
2023 * but visible to pfm_handle_work().
2025 * For some window of time, we have a zombie context with
2026 * ctx_state = MASKED and not ZOMBIE
2028 ctx->ctx_fl_going_zombie = 1;
2031 * force task to wake up from MASKED state
2033 complete(&ctx->ctx_restart_done);
2035 DPRINT(("waking up ctx_state=%d\n", state));
2038 * put ourself to sleep waiting for the other
2039 * task to report completion
2041 * the context is protected by mutex, therefore there
2042 * is no risk of being notified of completion before
2043 * begin actually on the waitq.
2045 set_current_state(TASK_INTERRUPTIBLE);
2046 add_wait_queue(&ctx->ctx_zombieq, &wait);
2048 UNPROTECT_CTX(ctx, flags);
2051 * XXX: check for signals :
2052 * - ok for explicit close
2053 * - not ok when coming from exit_files()
2058 PROTECT_CTX(ctx, flags);
2061 remove_wait_queue(&ctx->ctx_zombieq, &wait);
2062 set_current_state(TASK_RUNNING);
2065 * context is unloaded at this point
2067 DPRINT(("after zombie wakeup ctx_state=%d for\n", state));
2069 else if (task != current) {
2072 * switch context to zombie state
2074 ctx->ctx_state = PFM_CTX_ZOMBIE;
2076 DPRINT(("zombie ctx for [%d]\n", task_pid_nr(task)));
2078 * cannot free the context on the spot. deferred until
2079 * the task notices the ZOMBIE state
2083 pfm_context_unload(ctx, NULL, 0, regs);
2088 /* reload state, may have changed during opening of critical section */
2089 state = ctx->ctx_state;
2092 * the context is still attached to a task (possibly current)
2093 * we cannot destroy it right now
2097 * we must free the sampling buffer right here because
2098 * we cannot rely on it being cleaned up later by the
2099 * monitored task. It is not possible to free vmalloc'ed
2100 * memory in pfm_load_regs(). Instead, we remove the buffer
2101 * now. should there be subsequent PMU overflow originally
2102 * meant for sampling, the will be converted to spurious
2103 * and that's fine because the monitoring tools is gone anyway.
2105 if (ctx->ctx_smpl_hdr) {
2106 smpl_buf_addr = ctx->ctx_smpl_hdr;
2107 smpl_buf_size = ctx->ctx_smpl_size;
2108 /* no more sampling */
2109 ctx->ctx_smpl_hdr = NULL;
2110 ctx->ctx_fl_is_sampling = 0;
2113 DPRINT(("ctx_state=%d free_possible=%d addr=%p size=%lu\n",
2119 if (smpl_buf_addr) pfm_exit_smpl_buffer(ctx->ctx_buf_fmt);
2122 * UNLOADED that the session has already been unreserved.
2124 if (state == PFM_CTX_ZOMBIE) {
2125 pfm_unreserve_session(ctx, ctx->ctx_fl_system , ctx->ctx_cpu);
2129 * disconnect file descriptor from context must be done
2132 filp->private_data = NULL;
2135 * if we free on the spot, the context is now completely unreachable
2136 * from the callers side. The monitored task side is also cut, so we
2139 * If we have a deferred free, only the caller side is disconnected.
2141 UNPROTECT_CTX(ctx, flags);
2144 * All memory free operations (especially for vmalloc'ed memory)
2145 * MUST be done with interrupts ENABLED.
2147 if (smpl_buf_addr) pfm_rvfree(smpl_buf_addr, smpl_buf_size);
2150 * return the memory used by the context
2152 if (free_possible) pfm_context_free(ctx);
2158 pfm_no_open(struct inode *irrelevant, struct file *dontcare)
2160 DPRINT(("pfm_no_open called\n"));
2166 static const struct file_operations pfm_file_ops = {
2167 .llseek = no_llseek,
2171 .unlocked_ioctl = pfm_ioctl,
2172 .open = pfm_no_open, /* special open code to disallow open via /proc */
2173 .fasync = pfm_fasync,
2174 .release = pfm_close,
2179 pfmfs_delete_dentry(const struct dentry *dentry)
2184 static char *pfmfs_dname(struct dentry *dentry, char *buffer, int buflen)
2186 return dynamic_dname(dentry, buffer, buflen, "pfm:[%lu]",
2187 dentry->d_inode->i_ino);
2190 static const struct dentry_operations pfmfs_dentry_operations = {
2191 .d_delete = pfmfs_delete_dentry,
2192 .d_dname = pfmfs_dname,
2196 static struct file *
2197 pfm_alloc_file(pfm_context_t *ctx)
2200 struct inode *inode;
2202 struct qstr this = { .name = "" };
2205 * allocate a new inode
2207 inode = new_inode(pfmfs_mnt->mnt_sb);
2209 return ERR_PTR(-ENOMEM);
2211 DPRINT(("new inode ino=%ld @%p\n", inode->i_ino, inode));
2213 inode->i_mode = S_IFCHR|S_IRUGO;
2214 inode->i_uid = current_fsuid();
2215 inode->i_gid = current_fsgid();
2218 * allocate a new dcache entry
2220 path.dentry = d_alloc(pfmfs_mnt->mnt_root, &this);
2223 return ERR_PTR(-ENOMEM);
2225 path.mnt = mntget(pfmfs_mnt);
2227 d_add(path.dentry, inode);
2229 file = alloc_file(&path, FMODE_READ, &pfm_file_ops);
2232 return ERR_PTR(-ENFILE);
2235 file->f_flags = O_RDONLY;
2236 file->private_data = ctx;
2242 pfm_remap_buffer(struct vm_area_struct *vma, unsigned long buf, unsigned long addr, unsigned long size)
2244 DPRINT(("CPU%d buf=0x%lx addr=0x%lx size=%ld\n", smp_processor_id(), buf, addr, size));
2247 unsigned long pfn = ia64_tpa(buf) >> PAGE_SHIFT;
2250 if (remap_pfn_range(vma, addr, pfn, PAGE_SIZE, PAGE_READONLY))
2261 * allocate a sampling buffer and remaps it into the user address space of the task
2264 pfm_smpl_buffer_alloc(struct task_struct *task, struct file *filp, pfm_context_t *ctx, unsigned long rsize, void **user_vaddr)
2266 struct mm_struct *mm = task->mm;
2267 struct vm_area_struct *vma = NULL;
2273 * the fixed header + requested size and align to page boundary
2275 size = PAGE_ALIGN(rsize);
2277 DPRINT(("sampling buffer rsize=%lu size=%lu bytes\n", rsize, size));
2280 * check requested size to avoid Denial-of-service attacks
2281 * XXX: may have to refine this test
2282 * Check against address space limit.
2284 * if ((mm->total_vm << PAGE_SHIFT) + len> task->rlim[RLIMIT_AS].rlim_cur)
2287 if (size > task_rlimit(task, RLIMIT_MEMLOCK))
2291 * We do the easy to undo allocations first.
2293 * pfm_rvmalloc(), clears the buffer, so there is no leak
2295 smpl_buf = pfm_rvmalloc(size);
2296 if (smpl_buf == NULL) {
2297 DPRINT(("Can't allocate sampling buffer\n"));
2301 DPRINT(("smpl_buf @%p\n", smpl_buf));
2304 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2306 DPRINT(("Cannot allocate vma\n"));
2309 INIT_LIST_HEAD(&vma->anon_vma_chain);
2312 * partially initialize the vma for the sampling buffer
2315 vma->vm_file = filp;
2316 vma->vm_flags = VM_READ| VM_MAYREAD |VM_RESERVED;
2317 vma->vm_page_prot = PAGE_READONLY; /* XXX may need to change */
2320 * Now we have everything we need and we can initialize
2321 * and connect all the data structures
2324 ctx->ctx_smpl_hdr = smpl_buf;
2325 ctx->ctx_smpl_size = size; /* aligned size */
2328 * Let's do the difficult operations next.
2330 * now we atomically find some area in the address space and
2331 * remap the buffer in it.
2333 down_write(&task->mm->mmap_sem);
2335 /* find some free area in address space, must have mmap sem held */
2336 vma->vm_start = pfm_get_unmapped_area(NULL, 0, size, 0, MAP_PRIVATE|MAP_ANONYMOUS, 0);
2337 if (vma->vm_start == 0UL) {
2338 DPRINT(("Cannot find unmapped area for size %ld\n", size));
2339 up_write(&task->mm->mmap_sem);
2342 vma->vm_end = vma->vm_start + size;
2343 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2345 DPRINT(("aligned size=%ld, hdr=%p mapped @0x%lx\n", size, ctx->ctx_smpl_hdr, vma->vm_start));
2347 /* can only be applied to current task, need to have the mm semaphore held when called */
2348 if (pfm_remap_buffer(vma, (unsigned long)smpl_buf, vma->vm_start, size)) {
2349 DPRINT(("Can't remap buffer\n"));
2350 up_write(&task->mm->mmap_sem);
2357 * now insert the vma in the vm list for the process, must be
2358 * done with mmap lock held
2360 insert_vm_struct(mm, vma);
2362 mm->total_vm += size >> PAGE_SHIFT;
2363 vm_stat_account(vma->vm_mm, vma->vm_flags, vma->vm_file,
2365 up_write(&task->mm->mmap_sem);
2368 * keep track of user level virtual address
2370 ctx->ctx_smpl_vaddr = (void *)vma->vm_start;
2371 *(unsigned long *)user_vaddr = vma->vm_start;
2376 kmem_cache_free(vm_area_cachep, vma);
2378 pfm_rvfree(smpl_buf, size);
2384 * XXX: do something better here
2387 pfm_bad_permissions(struct task_struct *task)
2389 const struct cred *tcred;
2390 uid_t uid = current_uid();
2391 gid_t gid = current_gid();
2395 tcred = __task_cred(task);
2397 /* inspired by ptrace_attach() */
2398 DPRINT(("cur: uid=%d gid=%d task: euid=%d suid=%d uid=%d egid=%d sgid=%d\n",
2407 ret = ((uid != tcred->euid)
2408 || (uid != tcred->suid)
2409 || (uid != tcred->uid)
2410 || (gid != tcred->egid)
2411 || (gid != tcred->sgid)
2412 || (gid != tcred->gid)) && !capable(CAP_SYS_PTRACE);
2419 pfarg_is_sane(struct task_struct *task, pfarg_context_t *pfx)
2425 ctx_flags = pfx->ctx_flags;
2427 if (ctx_flags & PFM_FL_SYSTEM_WIDE) {
2430 * cannot block in this mode
2432 if (ctx_flags & PFM_FL_NOTIFY_BLOCK) {
2433 DPRINT(("cannot use blocking mode when in system wide monitoring\n"));
2438 /* probably more to add here */
2444 pfm_setup_buffer_fmt(struct task_struct *task, struct file *filp, pfm_context_t *ctx, unsigned int ctx_flags,
2445 unsigned int cpu, pfarg_context_t *arg)
2447 pfm_buffer_fmt_t *fmt = NULL;
2448 unsigned long size = 0UL;
2450 void *fmt_arg = NULL;
2452 #define PFM_CTXARG_BUF_ARG(a) (pfm_buffer_fmt_t *)(a+1)
2454 /* invoke and lock buffer format, if found */
2455 fmt = pfm_find_buffer_fmt(arg->ctx_smpl_buf_id);
2457 DPRINT(("[%d] cannot find buffer format\n", task_pid_nr(task)));
2462 * buffer argument MUST be contiguous to pfarg_context_t
2464 if (fmt->fmt_arg_size) fmt_arg = PFM_CTXARG_BUF_ARG(arg);
2466 ret = pfm_buf_fmt_validate(fmt, task, ctx_flags, cpu, fmt_arg);
2468 DPRINT(("[%d] after validate(0x%x,%d,%p)=%d\n", task_pid_nr(task), ctx_flags, cpu, fmt_arg, ret));
2470 if (ret) goto error;
2472 /* link buffer format and context */
2473 ctx->ctx_buf_fmt = fmt;
2474 ctx->ctx_fl_is_sampling = 1; /* assume record() is defined */
2477 * check if buffer format wants to use perfmon buffer allocation/mapping service
2479 ret = pfm_buf_fmt_getsize(fmt, task, ctx_flags, cpu, fmt_arg, &size);
2480 if (ret) goto error;
2484 * buffer is always remapped into the caller's address space
2486 ret = pfm_smpl_buffer_alloc(current, filp, ctx, size, &uaddr);
2487 if (ret) goto error;
2489 /* keep track of user address of buffer */
2490 arg->ctx_smpl_vaddr = uaddr;
2492 ret = pfm_buf_fmt_init(fmt, task, ctx->ctx_smpl_hdr, ctx_flags, cpu, fmt_arg);
2499 pfm_reset_pmu_state(pfm_context_t *ctx)
2504 * install reset values for PMC.
2506 for (i=1; PMC_IS_LAST(i) == 0; i++) {
2507 if (PMC_IS_IMPL(i) == 0) continue;
2508 ctx->ctx_pmcs[i] = PMC_DFL_VAL(i);
2509 DPRINT(("pmc[%d]=0x%lx\n", i, ctx->ctx_pmcs[i]));
2512 * PMD registers are set to 0UL when the context in memset()
2516 * On context switched restore, we must restore ALL pmc and ALL pmd even
2517 * when they are not actively used by the task. In UP, the incoming process
2518 * may otherwise pick up left over PMC, PMD state from the previous process.
2519 * As opposed to PMD, stale PMC can cause harm to the incoming
2520 * process because they may change what is being measured.
2521 * Therefore, we must systematically reinstall the entire
2522 * PMC state. In SMP, the same thing is possible on the
2523 * same CPU but also on between 2 CPUs.
2525 * The problem with PMD is information leaking especially
2526 * to user level when psr.sp=0
2528 * There is unfortunately no easy way to avoid this problem
2529 * on either UP or SMP. This definitively slows down the
2530 * pfm_load_regs() function.
2534 * bitmask of all PMCs accessible to this context
2536 * PMC0 is treated differently.
2538 ctx->ctx_all_pmcs[0] = pmu_conf->impl_pmcs[0] & ~0x1;
2541 * bitmask of all PMDs that are accessible to this context
2543 ctx->ctx_all_pmds[0] = pmu_conf->impl_pmds[0];
2545 DPRINT(("<%d> all_pmcs=0x%lx all_pmds=0x%lx\n", ctx->ctx_fd, ctx->ctx_all_pmcs[0],ctx->ctx_all_pmds[0]));
2548 * useful in case of re-enable after disable
2550 ctx->ctx_used_ibrs[0] = 0UL;
2551 ctx->ctx_used_dbrs[0] = 0UL;
2555 pfm_ctx_getsize(void *arg, size_t *sz)
2557 pfarg_context_t *req = (pfarg_context_t *)arg;
2558 pfm_buffer_fmt_t *fmt;
2562 if (!pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) return 0;
2564 fmt = pfm_find_buffer_fmt(req->ctx_smpl_buf_id);
2566 DPRINT(("cannot find buffer format\n"));
2569 /* get just enough to copy in user parameters */
2570 *sz = fmt->fmt_arg_size;
2571 DPRINT(("arg_size=%lu\n", *sz));
2579 * cannot attach if :
2581 * - task not owned by caller
2582 * - task incompatible with context mode
2585 pfm_task_incompatible(pfm_context_t *ctx, struct task_struct *task)
2588 * no kernel task or task not owner by caller
2590 if (task->mm == NULL) {
2591 DPRINT(("task [%d] has not memory context (kernel thread)\n", task_pid_nr(task)));
2594 if (pfm_bad_permissions(task)) {
2595 DPRINT(("no permission to attach to [%d]\n", task_pid_nr(task)));
2599 * cannot block in self-monitoring mode
2601 if (CTX_OVFL_NOBLOCK(ctx) == 0 && task == current) {
2602 DPRINT(("cannot load a blocking context on self for [%d]\n", task_pid_nr(task)));
2606 if (task->exit_state == EXIT_ZOMBIE) {
2607 DPRINT(("cannot attach to zombie task [%d]\n", task_pid_nr(task)));
2612 * always ok for self
2614 if (task == current) return 0;
2616 if (!task_is_stopped_or_traced(task)) {
2617 DPRINT(("cannot attach to non-stopped task [%d] state=%ld\n", task_pid_nr(task), task->state));
2621 * make sure the task is off any CPU
2623 wait_task_inactive(task, 0);
2625 /* more to come... */
2631 pfm_get_task(pfm_context_t *ctx, pid_t pid, struct task_struct **task)
2633 struct task_struct *p = current;
2636 /* XXX: need to add more checks here */
2637 if (pid < 2) return -EPERM;
2639 if (pid != task_pid_vnr(current)) {
2641 read_lock(&tasklist_lock);
2643 p = find_task_by_vpid(pid);
2645 /* make sure task cannot go away while we operate on it */
2646 if (p) get_task_struct(p);
2648 read_unlock(&tasklist_lock);
2650 if (p == NULL) return -ESRCH;
2653 ret = pfm_task_incompatible(ctx, p);
2656 } else if (p != current) {
2665 pfm_context_create(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
2667 pfarg_context_t *req = (pfarg_context_t *)arg;
2674 /* let's check the arguments first */
2675 ret = pfarg_is_sane(current, req);
2679 ctx_flags = req->ctx_flags;
2683 fd = get_unused_fd();
2687 ctx = pfm_context_alloc(ctx_flags);
2691 filp = pfm_alloc_file(ctx);
2693 ret = PTR_ERR(filp);
2697 req->ctx_fd = ctx->ctx_fd = fd;
2700 * does the user want to sample?
2702 if (pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) {
2703 ret = pfm_setup_buffer_fmt(current, filp, ctx, ctx_flags, 0, req);
2708 DPRINT(("ctx=%p flags=0x%x system=%d notify_block=%d excl_idle=%d no_msg=%d ctx_fd=%d\n",
2713 ctx->ctx_fl_excl_idle,
2718 * initialize soft PMU state
2720 pfm_reset_pmu_state(ctx);
2722 fd_install(fd, filp);
2727 path = filp->f_path;
2731 if (ctx->ctx_buf_fmt) {
2732 pfm_buf_fmt_exit(ctx->ctx_buf_fmt, current, NULL, regs);
2735 pfm_context_free(ctx);
2742 static inline unsigned long
2743 pfm_new_counter_value (pfm_counter_t *reg, int is_long_reset)
2745 unsigned long val = is_long_reset ? reg->long_reset : reg->short_reset;
2746 unsigned long new_seed, old_seed = reg->seed, mask = reg->mask;
2747 extern unsigned long carta_random32 (unsigned long seed);
2749 if (reg->flags & PFM_REGFL_RANDOM) {
2750 new_seed = carta_random32(old_seed);
2751 val -= (old_seed & mask); /* counter values are negative numbers! */
2752 if ((mask >> 32) != 0)
2753 /* construct a full 64-bit random value: */
2754 new_seed |= carta_random32(old_seed >> 32) << 32;
2755 reg->seed = new_seed;
2762 pfm_reset_regs_masked(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
2764 unsigned long mask = ovfl_regs[0];
2765 unsigned long reset_others = 0UL;
2770 * now restore reset value on sampling overflowed counters
2772 mask >>= PMU_FIRST_COUNTER;
2773 for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
2775 if ((mask & 0x1UL) == 0UL) continue;
2777 ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
2778 reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
2780 DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
2784 * Now take care of resetting the other registers
2786 for(i = 0; reset_others; i++, reset_others >>= 1) {
2788 if ((reset_others & 0x1) == 0) continue;
2790 ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
2792 DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
2793 is_long_reset ? "long" : "short", i, val));
2798 pfm_reset_regs(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
2800 unsigned long mask = ovfl_regs[0];
2801 unsigned long reset_others = 0UL;
2805 DPRINT_ovfl(("ovfl_regs=0x%lx is_long_reset=%d\n", ovfl_regs[0], is_long_reset));
2807 if (ctx->ctx_state == PFM_CTX_MASKED) {
2808 pfm_reset_regs_masked(ctx, ovfl_regs, is_long_reset);
2813 * now restore reset value on sampling overflowed counters
2815 mask >>= PMU_FIRST_COUNTER;
2816 for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
2818 if ((mask & 0x1UL) == 0UL) continue;
2820 val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
2821 reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
2823 DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
2825 pfm_write_soft_counter(ctx, i, val);
2829 * Now take care of resetting the other registers
2831 for(i = 0; reset_others; i++, reset_others >>= 1) {
2833 if ((reset_others & 0x1) == 0) continue;
2835 val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
2837 if (PMD_IS_COUNTING(i)) {
2838 pfm_write_soft_counter(ctx, i, val);
2840 ia64_set_pmd(i, val);
2842 DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
2843 is_long_reset ? "long" : "short", i, val));
2849 pfm_write_pmcs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
2851 struct task_struct *task;
2852 pfarg_reg_t *req = (pfarg_reg_t *)arg;
2853 unsigned long value, pmc_pm;
2854 unsigned long smpl_pmds, reset_pmds, impl_pmds;
2855 unsigned int cnum, reg_flags, flags, pmc_type;
2856 int i, can_access_pmu = 0, is_loaded, is_system, expert_mode;
2857 int is_monitor, is_counting, state;
2859 pfm_reg_check_t wr_func;
2860 #define PFM_CHECK_PMC_PM(x, y, z) ((x)->ctx_fl_system ^ PMC_PM(y, z))
2862 state = ctx->ctx_state;
2863 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
2864 is_system = ctx->ctx_fl_system;
2865 task = ctx->ctx_task;
2866 impl_pmds = pmu_conf->impl_pmds[0];
2868 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
2872 * In system wide and when the context is loaded, access can only happen
2873 * when the caller is running on the CPU being monitored by the session.
2874 * It does not have to be the owner (ctx_task) of the context per se.
2876 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
2877 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
2880 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
2882 expert_mode = pfm_sysctl.expert_mode;
2884 for (i = 0; i < count; i++, req++) {
2886 cnum = req->reg_num;
2887 reg_flags = req->reg_flags;
2888 value = req->reg_value;
2889 smpl_pmds = req->reg_smpl_pmds[0];
2890 reset_pmds = req->reg_reset_pmds[0];
2894 if (cnum >= PMU_MAX_PMCS) {
2895 DPRINT(("pmc%u is invalid\n", cnum));
2899 pmc_type = pmu_conf->pmc_desc[cnum].type;
2900 pmc_pm = (value >> pmu_conf->pmc_desc[cnum].pm_pos) & 0x1;
2901 is_counting = (pmc_type & PFM_REG_COUNTING) == PFM_REG_COUNTING ? 1 : 0;
2902 is_monitor = (pmc_type & PFM_REG_MONITOR) == PFM_REG_MONITOR ? 1 : 0;
2905 * we reject all non implemented PMC as well
2906 * as attempts to modify PMC[0-3] which are used
2907 * as status registers by the PMU
2909 if ((pmc_type & PFM_REG_IMPL) == 0 || (pmc_type & PFM_REG_CONTROL) == PFM_REG_CONTROL) {
2910 DPRINT(("pmc%u is unimplemented or no-access pmc_type=%x\n", cnum, pmc_type));
2913 wr_func = pmu_conf->pmc_desc[cnum].write_check;
2915 * If the PMC is a monitor, then if the value is not the default:
2916 * - system-wide session: PMCx.pm=1 (privileged monitor)
2917 * - per-task : PMCx.pm=0 (user monitor)
2919 if (is_monitor && value != PMC_DFL_VAL(cnum) && is_system ^ pmc_pm) {
2920 DPRINT(("pmc%u pmc_pm=%lu is_system=%d\n",
2929 * enforce generation of overflow interrupt. Necessary on all
2932 value |= 1 << PMU_PMC_OI;
2934 if (reg_flags & PFM_REGFL_OVFL_NOTIFY) {
2935 flags |= PFM_REGFL_OVFL_NOTIFY;
2938 if (reg_flags & PFM_REGFL_RANDOM) flags |= PFM_REGFL_RANDOM;
2940 /* verify validity of smpl_pmds */
2941 if ((smpl_pmds & impl_pmds) != smpl_pmds) {
2942 DPRINT(("invalid smpl_pmds 0x%lx for pmc%u\n", smpl_pmds, cnum));
2946 /* verify validity of reset_pmds */
2947 if ((reset_pmds & impl_pmds) != reset_pmds) {
2948 DPRINT(("invalid reset_pmds 0x%lx for pmc%u\n", reset_pmds, cnum));
2952 if (reg_flags & (PFM_REGFL_OVFL_NOTIFY|PFM_REGFL_RANDOM)) {
2953 DPRINT(("cannot set ovfl_notify or random on pmc%u\n", cnum));
2956 /* eventid on non-counting monitors are ignored */
2960 * execute write checker, if any
2962 if (likely(expert_mode == 0 && wr_func)) {
2963 ret = (*wr_func)(task, ctx, cnum, &value, regs);
2964 if (ret) goto error;
2969 * no error on this register
2971 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
2974 * Now we commit the changes to the software state
2978 * update overflow information
2982 * full flag update each time a register is programmed
2984 ctx->ctx_pmds[cnum].flags = flags;
2986 ctx->ctx_pmds[cnum].reset_pmds[0] = reset_pmds;
2987 ctx->ctx_pmds[cnum].smpl_pmds[0] = smpl_pmds;
2988 ctx->ctx_pmds[cnum].eventid = req->reg_smpl_eventid;
2991 * Mark all PMDS to be accessed as used.
2993 * We do not keep track of PMC because we have to
2994 * systematically restore ALL of them.
2996 * We do not update the used_monitors mask, because
2997 * if we have not programmed them, then will be in
2998 * a quiescent state, therefore we will not need to
2999 * mask/restore then when context is MASKED.
3001 CTX_USED_PMD(ctx, reset_pmds);
3002 CTX_USED_PMD(ctx, smpl_pmds);
3004 * make sure we do not try to reset on
3005 * restart because we have established new values
3007 if (state == PFM_CTX_MASKED) ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
3010 * Needed in case the user does not initialize the equivalent
3011 * PMD. Clearing is done indirectly via pfm_reset_pmu_state() so there is no
3012 * possible leak here.
3014 CTX_USED_PMD(ctx, pmu_conf->pmc_desc[cnum].dep_pmd[0]);
3017 * keep track of the monitor PMC that we are using.
3018 * we save the value of the pmc in ctx_pmcs[] and if
3019 * the monitoring is not stopped for the context we also
3020 * place it in the saved state area so that it will be
3021 * picked up later by the context switch code.
3023 * The value in ctx_pmcs[] can only be changed in pfm_write_pmcs().
3025 * The value in th_pmcs[] may be modified on overflow, i.e., when
3026 * monitoring needs to be stopped.
3028 if (is_monitor) CTX_USED_MONITOR(ctx, 1UL << cnum);
3031 * update context state
3033 ctx->ctx_pmcs[cnum] = value;
3037 * write thread state
3039 if (is_system == 0) ctx->th_pmcs[cnum] = value;
3042 * write hardware register if we can
3044 if (can_access_pmu) {
3045 ia64_set_pmc(cnum, value);
3050 * per-task SMP only here
3052 * we are guaranteed that the task is not running on the other CPU,
3053 * we indicate that this PMD will need to be reloaded if the task
3054 * is rescheduled on the CPU it ran last on.
3056 ctx->ctx_reload_pmcs[0] |= 1UL << cnum;
3061 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",
3067 ctx->ctx_all_pmcs[0],
3068 ctx->ctx_used_pmds[0],
3069 ctx->ctx_pmds[cnum].eventid,
3072 ctx->ctx_reload_pmcs[0],
3073 ctx->ctx_used_monitors[0],
3074 ctx->ctx_ovfl_regs[0]));
3078 * make sure the changes are visible
3080 if (can_access_pmu) ia64_srlz_d();
3084 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3089 pfm_write_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3091 struct task_struct *task;
3092 pfarg_reg_t *req = (pfarg_reg_t *)arg;
3093 unsigned long value, hw_value, ovfl_mask;
3095 int i, can_access_pmu = 0, state;
3096 int is_counting, is_loaded, is_system, expert_mode;
3098 pfm_reg_check_t wr_func;
3101 state = ctx->ctx_state;
3102 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3103 is_system = ctx->ctx_fl_system;
3104 ovfl_mask = pmu_conf->ovfl_val;
3105 task = ctx->ctx_task;
3107 if (unlikely(state == PFM_CTX_ZOMBIE)) return -EINVAL;
3110 * on both UP and SMP, we can only write to the PMC when the task is
3111 * the owner of the local PMU.
3113 if (likely(is_loaded)) {
3115 * In system wide and when the context is loaded, access can only happen
3116 * when the caller is running on the CPU being monitored by the session.
3117 * It does not have to be the owner (ctx_task) of the context per se.
3119 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3120 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3123 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3125 expert_mode = pfm_sysctl.expert_mode;
3127 for (i = 0; i < count; i++, req++) {
3129 cnum = req->reg_num;
3130 value = req->reg_value;
3132 if (!PMD_IS_IMPL(cnum)) {
3133 DPRINT(("pmd[%u] is unimplemented or invalid\n", cnum));
3136 is_counting = PMD_IS_COUNTING(cnum);
3137 wr_func = pmu_conf->pmd_desc[cnum].write_check;
3140 * execute write checker, if any
3142 if (unlikely(expert_mode == 0 && wr_func)) {
3143 unsigned long v = value;
3145 ret = (*wr_func)(task, ctx, cnum, &v, regs);
3146 if (ret) goto abort_mission;
3153 * no error on this register
3155 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
3158 * now commit changes to software state
3163 * update virtualized (64bits) counter
3167 * write context state
3169 ctx->ctx_pmds[cnum].lval = value;
3172 * when context is load we use the split value
3175 hw_value = value & ovfl_mask;
3176 value = value & ~ovfl_mask;
3180 * update reset values (not just for counters)
3182 ctx->ctx_pmds[cnum].long_reset = req->reg_long_reset;
3183 ctx->ctx_pmds[cnum].short_reset = req->reg_short_reset;
3186 * update randomization parameters (not just for counters)
3188 ctx->ctx_pmds[cnum].seed = req->reg_random_seed;
3189 ctx->ctx_pmds[cnum].mask = req->reg_random_mask;
3192 * update context value
3194 ctx->ctx_pmds[cnum].val = value;
3197 * Keep track of what we use
3199 * We do not keep track of PMC because we have to
3200 * systematically restore ALL of them.
3202 CTX_USED_PMD(ctx, PMD_PMD_DEP(cnum));
3205 * mark this PMD register used as well
3207 CTX_USED_PMD(ctx, RDEP(cnum));
3210 * make sure we do not try to reset on
3211 * restart because we have established new values
3213 if (is_counting && state == PFM_CTX_MASKED) {
3214 ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
3219 * write thread state
3221 if (is_system == 0) ctx->th_pmds[cnum] = hw_value;
3224 * write hardware register if we can
3226 if (can_access_pmu) {
3227 ia64_set_pmd(cnum, hw_value);
3231 * we are guaranteed that the task is not running on the other CPU,
3232 * we indicate that this PMD will need to be reloaded if the task
3233 * is rescheduled on the CPU it ran last on.
3235 ctx->ctx_reload_pmds[0] |= 1UL << cnum;
3240 DPRINT(("pmd[%u]=0x%lx ld=%d apmu=%d, hw_value=0x%lx ctx_pmd=0x%lx short_reset=0x%lx "
3241 "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",
3247 ctx->ctx_pmds[cnum].val,
3248 ctx->ctx_pmds[cnum].short_reset,
3249 ctx->ctx_pmds[cnum].long_reset,
3250 PMC_OVFL_NOTIFY(ctx, cnum) ? 'Y':'N',
3251 ctx->ctx_pmds[cnum].seed,
3252 ctx->ctx_pmds[cnum].mask,
3253 ctx->ctx_used_pmds[0],
3254 ctx->ctx_pmds[cnum].reset_pmds[0],
3255 ctx->ctx_reload_pmds[0],
3256 ctx->ctx_all_pmds[0],
3257 ctx->ctx_ovfl_regs[0]));
3261 * make changes visible
3263 if (can_access_pmu) ia64_srlz_d();
3269 * for now, we have only one possibility for error
3271 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3276 * By the way of PROTECT_CONTEXT(), interrupts are masked while we are in this function.
3277 * Therefore we know, we do not have to worry about the PMU overflow interrupt. If an
3278 * interrupt is delivered during the call, it will be kept pending until we leave, making
3279 * it appears as if it had been generated at the UNPROTECT_CONTEXT(). At least we are
3280 * guaranteed to return consistent data to the user, it may simply be old. It is not
3281 * trivial to treat the overflow while inside the call because you may end up in
3282 * some module sampling buffer code causing deadlocks.
3285 pfm_read_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3287 struct task_struct *task;
3288 unsigned long val = 0UL, lval, ovfl_mask, sval;
3289 pfarg_reg_t *req = (pfarg_reg_t *)arg;
3290 unsigned int cnum, reg_flags = 0;
3291 int i, can_access_pmu = 0, state;
3292 int is_loaded, is_system, is_counting, expert_mode;
3294 pfm_reg_check_t rd_func;
3297 * access is possible when loaded only for
3298 * self-monitoring tasks or in UP mode
3301 state = ctx->ctx_state;
3302 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3303 is_system = ctx->ctx_fl_system;
3304 ovfl_mask = pmu_conf->ovfl_val;
3305 task = ctx->ctx_task;
3307 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
3309 if (likely(is_loaded)) {
3311 * In system wide and when the context is loaded, access can only happen
3312 * when the caller is running on the CPU being monitored by the session.
3313 * It does not have to be the owner (ctx_task) of the context per se.
3315 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3316 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3320 * this can be true when not self-monitoring only in UP
3322 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3324 if (can_access_pmu) ia64_srlz_d();
3326 expert_mode = pfm_sysctl.expert_mode;
3328 DPRINT(("ld=%d apmu=%d ctx_state=%d\n",
3334 * on both UP and SMP, we can only read the PMD from the hardware register when
3335 * the task is the owner of the local PMU.
3338 for (i = 0; i < count; i++, req++) {
3340 cnum = req->reg_num;
3341 reg_flags = req->reg_flags;
3343 if (unlikely(!PMD_IS_IMPL(cnum))) goto error;
3345 * we can only read the register that we use. That includes
3346 * the one we explicitly initialize AND the one we want included
3347 * in the sampling buffer (smpl_regs).
3349 * Having this restriction allows optimization in the ctxsw routine
3350 * without compromising security (leaks)
3352 if (unlikely(!CTX_IS_USED_PMD(ctx, cnum))) goto error;
3354 sval = ctx->ctx_pmds[cnum].val;
3355 lval = ctx->ctx_pmds[cnum].lval;
3356 is_counting = PMD_IS_COUNTING(cnum);
3359 * If the task is not the current one, then we check if the
3360 * PMU state is still in the local live register due to lazy ctxsw.
3361 * If true, then we read directly from the registers.
3363 if (can_access_pmu){
3364 val = ia64_get_pmd(cnum);
3367 * context has been saved
3368 * if context is zombie, then task does not exist anymore.
3369 * In this case, we use the full value saved in the context (pfm_flush_regs()).
3371 val = is_loaded ? ctx->th_pmds[cnum] : 0UL;
3373 rd_func = pmu_conf->pmd_desc[cnum].read_check;
3377 * XXX: need to check for overflow when loaded
3384 * execute read checker, if any
3386 if (unlikely(expert_mode == 0 && rd_func)) {
3387 unsigned long v = val;
3388 ret = (*rd_func)(ctx->ctx_task, ctx, cnum, &v, regs);
3389 if (ret) goto error;
3394 PFM_REG_RETFLAG_SET(reg_flags, 0);
3396 DPRINT(("pmd[%u]=0x%lx\n", cnum, val));
3399 * update register return value, abort all if problem during copy.
3400 * we only modify the reg_flags field. no check mode is fine because
3401 * access has been verified upfront in sys_perfmonctl().
3403 req->reg_value = val;
3404 req->reg_flags = reg_flags;
3405 req->reg_last_reset_val = lval;
3411 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3416 pfm_mod_write_pmcs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3420 if (req == NULL) return -EINVAL;
3422 ctx = GET_PMU_CTX();
3424 if (ctx == NULL) return -EINVAL;
3427 * for now limit to current task, which is enough when calling
3428 * from overflow handler
3430 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3432 return pfm_write_pmcs(ctx, req, nreq, regs);
3434 EXPORT_SYMBOL(pfm_mod_write_pmcs);
3437 pfm_mod_read_pmds(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3441 if (req == NULL) return -EINVAL;
3443 ctx = GET_PMU_CTX();
3445 if (ctx == NULL) return -EINVAL;
3448 * for now limit to current task, which is enough when calling
3449 * from overflow handler
3451 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3453 return pfm_read_pmds(ctx, req, nreq, regs);
3455 EXPORT_SYMBOL(pfm_mod_read_pmds);
3458 * Only call this function when a process it trying to
3459 * write the debug registers (reading is always allowed)
3462 pfm_use_debug_registers(struct task_struct *task)
3464 pfm_context_t *ctx = task->thread.pfm_context;
3465 unsigned long flags;
3468 if (pmu_conf->use_rr_dbregs == 0) return 0;
3470 DPRINT(("called for [%d]\n", task_pid_nr(task)));
3475 if (task->thread.flags & IA64_THREAD_DBG_VALID) return 0;
3478 * Even on SMP, we do not need to use an atomic here because
3479 * the only way in is via ptrace() and this is possible only when the
3480 * process is stopped. Even in the case where the ctxsw out is not totally
3481 * completed by the time we come here, there is no way the 'stopped' process
3482 * could be in the middle of fiddling with the pfm_write_ibr_dbr() routine.
3483 * So this is always safe.
3485 if (ctx && ctx->ctx_fl_using_dbreg == 1) return -1;
3490 * We cannot allow setting breakpoints when system wide monitoring
3491 * sessions are using the debug registers.
3493 if (pfm_sessions.pfs_sys_use_dbregs> 0)
3496 pfm_sessions.pfs_ptrace_use_dbregs++;
3498 DPRINT(("ptrace_use_dbregs=%u sys_use_dbregs=%u by [%d] ret = %d\n",
3499 pfm_sessions.pfs_ptrace_use_dbregs,
3500 pfm_sessions.pfs_sys_use_dbregs,
3501 task_pid_nr(task), ret));
3509 * This function is called for every task that exits with the
3510 * IA64_THREAD_DBG_VALID set. This indicates a task which was
3511 * able to use the debug registers for debugging purposes via
3512 * ptrace(). Therefore we know it was not using them for
3513 * performance monitoring, so we only decrement the number
3514 * of "ptraced" debug register users to keep the count up to date
3517 pfm_release_debug_registers(struct task_struct *task)
3519 unsigned long flags;
3522 if (pmu_conf->use_rr_dbregs == 0) return 0;
3525 if (pfm_sessions.pfs_ptrace_use_dbregs == 0) {
3526 printk(KERN_ERR "perfmon: invalid release for [%d] ptrace_use_dbregs=0\n", task_pid_nr(task));
3529 pfm_sessions.pfs_ptrace_use_dbregs--;
3538 pfm_restart(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3540 struct task_struct *task;
3541 pfm_buffer_fmt_t *fmt;
3542 pfm_ovfl_ctrl_t rst_ctrl;
3543 int state, is_system;
3546 state = ctx->ctx_state;
3547 fmt = ctx->ctx_buf_fmt;
3548 is_system = ctx->ctx_fl_system;
3549 task = PFM_CTX_TASK(ctx);
3552 case PFM_CTX_MASKED:
3554 case PFM_CTX_LOADED:
3555 if (CTX_HAS_SMPL(ctx) && fmt->fmt_restart_active) break;
3557 case PFM_CTX_UNLOADED:
3558 case PFM_CTX_ZOMBIE:
3559 DPRINT(("invalid state=%d\n", state));
3562 DPRINT(("state=%d, cannot operate (no active_restart handler)\n", state));
3567 * In system wide and when the context is loaded, access can only happen
3568 * when the caller is running on the CPU being monitored by the session.
3569 * It does not have to be the owner (ctx_task) of the context per se.
3571 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
3572 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3577 if (unlikely(task == NULL)) {
3578 printk(KERN_ERR "perfmon: [%d] pfm_restart no task\n", task_pid_nr(current));
3582 if (task == current || is_system) {
3584 fmt = ctx->ctx_buf_fmt;
3586 DPRINT(("restarting self %d ovfl=0x%lx\n",
3588 ctx->ctx_ovfl_regs[0]));
3590 if (CTX_HAS_SMPL(ctx)) {
3592 prefetch(ctx->ctx_smpl_hdr);
3594 rst_ctrl.bits.mask_monitoring = 0;
3595 rst_ctrl.bits.reset_ovfl_pmds = 0;
3597 if (state == PFM_CTX_LOADED)
3598 ret = pfm_buf_fmt_restart_active(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
3600 ret = pfm_buf_fmt_restart(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
3602 rst_ctrl.bits.mask_monitoring = 0;
3603 rst_ctrl.bits.reset_ovfl_pmds = 1;
3607 if (rst_ctrl.bits.reset_ovfl_pmds)
3608 pfm_reset_regs(ctx, ctx->ctx_ovfl_regs, PFM_PMD_LONG_RESET);
3610 if (rst_ctrl.bits.mask_monitoring == 0) {
3611 DPRINT(("resuming monitoring for [%d]\n", task_pid_nr(task)));
3613 if (state == PFM_CTX_MASKED) pfm_restore_monitoring(task);
3615 DPRINT(("keeping monitoring stopped for [%d]\n", task_pid_nr(task)));
3617 // cannot use pfm_stop_monitoring(task, regs);
3621 * clear overflowed PMD mask to remove any stale information
3623 ctx->ctx_ovfl_regs[0] = 0UL;
3626 * back to LOADED state
3628 ctx->ctx_state = PFM_CTX_LOADED;
3631 * XXX: not really useful for self monitoring
3633 ctx->ctx_fl_can_restart = 0;
3639 * restart another task
3643 * When PFM_CTX_MASKED, we cannot issue a restart before the previous
3644 * one is seen by the task.
3646 if (state == PFM_CTX_MASKED) {
3647 if (ctx->ctx_fl_can_restart == 0) return -EINVAL;
3649 * will prevent subsequent restart before this one is
3650 * seen by other task
3652 ctx->ctx_fl_can_restart = 0;
3656 * if blocking, then post the semaphore is PFM_CTX_MASKED, i.e.
3657 * the task is blocked or on its way to block. That's the normal
3658 * restart path. If the monitoring is not masked, then the task
3659 * can be actively monitoring and we cannot directly intervene.
3660 * Therefore we use the trap mechanism to catch the task and
3661 * force it to reset the buffer/reset PMDs.
3663 * if non-blocking, then we ensure that the task will go into
3664 * pfm_handle_work() before returning to user mode.
3666 * We cannot explicitly reset another task, it MUST always
3667 * be done by the task itself. This works for system wide because
3668 * the tool that is controlling the session is logically doing
3669 * "self-monitoring".
3671 if (CTX_OVFL_NOBLOCK(ctx) == 0 && state == PFM_CTX_MASKED) {
3672 DPRINT(("unblocking [%d]\n", task_pid_nr(task)));
3673 complete(&ctx->ctx_restart_done);
3675 DPRINT(("[%d] armed exit trap\n", task_pid_nr(task)));
3677 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_RESET;
3679 PFM_SET_WORK_PENDING(task, 1);
3681 set_notify_resume(task);
3684 * XXX: send reschedule if task runs on another CPU
3691 pfm_debug(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3693 unsigned int m = *(unsigned int *)arg;
3695 pfm_sysctl.debug = m == 0 ? 0 : 1;
3697 printk(KERN_INFO "perfmon debugging %s (timing reset)\n", pfm_sysctl.debug ? "on" : "off");
3700 memset(pfm_stats, 0, sizeof(pfm_stats));
3701 for(m=0; m < NR_CPUS; m++) pfm_stats[m].pfm_ovfl_intr_cycles_min = ~0UL;
3707 * arg can be NULL and count can be zero for this function
3710 pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3712 struct thread_struct *thread = NULL;
3713 struct task_struct *task;
3714 pfarg_dbreg_t *req = (pfarg_dbreg_t *)arg;
3715 unsigned long flags;
3720 int i, can_access_pmu = 0;
3721 int is_system, is_loaded;
3723 if (pmu_conf->use_rr_dbregs == 0) return -EINVAL;
3725 state = ctx->ctx_state;
3726 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3727 is_system = ctx->ctx_fl_system;
3728 task = ctx->ctx_task;
3730 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
3733 * on both UP and SMP, we can only write to the PMC when the task is
3734 * the owner of the local PMU.
3737 thread = &task->thread;
3739 * In system wide and when the context is loaded, access can only happen
3740 * when the caller is running on the CPU being monitored by the session.
3741 * It does not have to be the owner (ctx_task) of the context per se.
3743 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3744 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3747 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3751 * we do not need to check for ipsr.db because we do clear ibr.x, dbr.r, and dbr.w
3752 * ensuring that no real breakpoint can be installed via this call.
3754 * IMPORTANT: regs can be NULL in this function
3757 first_time = ctx->ctx_fl_using_dbreg == 0;
3760 * don't bother if we are loaded and task is being debugged
3762 if (is_loaded && (thread->flags & IA64_THREAD_DBG_VALID) != 0) {
3763 DPRINT(("debug registers already in use for [%d]\n", task_pid_nr(task)));
3768 * check for debug registers in system wide mode
3770 * If though a check is done in pfm_context_load(),
3771 * we must repeat it here, in case the registers are
3772 * written after the context is loaded
3777 if (first_time && is_system) {
3778 if (pfm_sessions.pfs_ptrace_use_dbregs)
3781 pfm_sessions.pfs_sys_use_dbregs++;
3786 if (ret != 0) return ret;
3789 * mark ourself as user of the debug registers for
3792 ctx->ctx_fl_using_dbreg = 1;
3795 * clear hardware registers to make sure we don't
3796 * pick up stale state.
3798 * for a system wide session, we do not use
3799 * thread.dbr, thread.ibr because this process
3800 * never leaves the current CPU and the state
3801 * is shared by all processes running on it
3803 if (first_time && can_access_pmu) {
3804 DPRINT(("[%d] clearing ibrs, dbrs\n", task_pid_nr(task)));
3805 for (i=0; i < pmu_conf->num_ibrs; i++) {
3806 ia64_set_ibr(i, 0UL);
3807 ia64_dv_serialize_instruction();
3810 for (i=0; i < pmu_conf->num_dbrs; i++) {
3811 ia64_set_dbr(i, 0UL);
3812 ia64_dv_serialize_data();
3818 * Now install the values into the registers
3820 for (i = 0; i < count; i++, req++) {
3822 rnum = req->dbreg_num;
3823 dbreg.val = req->dbreg_value;
3827 if ((mode == PFM_CODE_RR && rnum >= PFM_NUM_IBRS) || ((mode == PFM_DATA_RR) && rnum >= PFM_NUM_DBRS)) {
3828 DPRINT(("invalid register %u val=0x%lx mode=%d i=%d count=%d\n",
3829 rnum, dbreg.val, mode, i, count));
3835 * make sure we do not install enabled breakpoint
3838 if (mode == PFM_CODE_RR)
3839 dbreg.ibr.ibr_x = 0;
3841 dbreg.dbr.dbr_r = dbreg.dbr.dbr_w = 0;
3844 PFM_REG_RETFLAG_SET(req->dbreg_flags, 0);
3847 * Debug registers, just like PMC, can only be modified
3848 * by a kernel call. Moreover, perfmon() access to those
3849 * registers are centralized in this routine. The hardware
3850 * does not modify the value of these registers, therefore,
3851 * if we save them as they are written, we can avoid having
3852 * to save them on context switch out. This is made possible
3853 * by the fact that when perfmon uses debug registers, ptrace()
3854 * won't be able to modify them concurrently.
3856 if (mode == PFM_CODE_RR) {
3857 CTX_USED_IBR(ctx, rnum);
3859 if (can_access_pmu) {
3860 ia64_set_ibr(rnum, dbreg.val);
3861 ia64_dv_serialize_instruction();
3864 ctx->ctx_ibrs[rnum] = dbreg.val;
3866 DPRINT(("write ibr%u=0x%lx used_ibrs=0x%x ld=%d apmu=%d\n",
3867 rnum, dbreg.val, ctx->ctx_used_ibrs[0], is_loaded, can_access_pmu));
3869 CTX_USED_DBR(ctx, rnum);
3871 if (can_access_pmu) {
3872 ia64_set_dbr(rnum, dbreg.val);
3873 ia64_dv_serialize_data();
3875 ctx->ctx_dbrs[rnum] = dbreg.val;
3877 DPRINT(("write dbr%u=0x%lx used_dbrs=0x%x ld=%d apmu=%d\n",
3878 rnum, dbreg.val, ctx->ctx_used_dbrs[0], is_loaded, can_access_pmu));
3886 * in case it was our first attempt, we undo the global modifications
3890 if (ctx->ctx_fl_system) {
3891 pfm_sessions.pfs_sys_use_dbregs--;
3894 ctx->ctx_fl_using_dbreg = 0;
3897 * install error return flag
3899 PFM_REG_RETFLAG_SET(req->dbreg_flags, PFM_REG_RETFL_EINVAL);
3905 pfm_write_ibrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3907 return pfm_write_ibr_dbr(PFM_CODE_RR, ctx, arg, count, regs);
3911 pfm_write_dbrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3913 return pfm_write_ibr_dbr(PFM_DATA_RR, ctx, arg, count, regs);
3917 pfm_mod_write_ibrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3921 if (req == NULL) return -EINVAL;
3923 ctx = GET_PMU_CTX();
3925 if (ctx == NULL) return -EINVAL;
3928 * for now limit to current task, which is enough when calling
3929 * from overflow handler
3931 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3933 return pfm_write_ibrs(ctx, req, nreq, regs);
3935 EXPORT_SYMBOL(pfm_mod_write_ibrs);
3938 pfm_mod_write_dbrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3942 if (req == NULL) return -EINVAL;
3944 ctx = GET_PMU_CTX();
3946 if (ctx == NULL) return -EINVAL;
3949 * for now limit to current task, which is enough when calling
3950 * from overflow handler
3952 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3954 return pfm_write_dbrs(ctx, req, nreq, regs);
3956 EXPORT_SYMBOL(pfm_mod_write_dbrs);
3960 pfm_get_features(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3962 pfarg_features_t *req = (pfarg_features_t *)arg;
3964 req->ft_version = PFM_VERSION;
3969 pfm_stop(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3971 struct pt_regs *tregs;
3972 struct task_struct *task = PFM_CTX_TASK(ctx);
3973 int state, is_system;
3975 state = ctx->ctx_state;
3976 is_system = ctx->ctx_fl_system;
3979 * context must be attached to issue the stop command (includes LOADED,MASKED,ZOMBIE)
3981 if (state == PFM_CTX_UNLOADED) return -EINVAL;
3984 * In system wide and when the context is loaded, access can only happen
3985 * when the caller is running on the CPU being monitored by the session.
3986 * It does not have to be the owner (ctx_task) of the context per se.
3988 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
3989 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3992 DPRINT(("task [%d] ctx_state=%d is_system=%d\n",
3993 task_pid_nr(PFM_CTX_TASK(ctx)),
3997 * in system mode, we need to update the PMU directly
3998 * and the user level state of the caller, which may not
3999 * necessarily be the creator of the context.
4003 * Update local PMU first
4007 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
4011 * update local cpuinfo
4013 PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
4016 * stop monitoring, does srlz.i
4021 * stop monitoring in the caller
4023 ia64_psr(regs)->pp = 0;
4031 if (task == current) {
4032 /* stop monitoring at kernel level */
4036 * stop monitoring at the user level
4038 ia64_psr(regs)->up = 0;
4040 tregs = task_pt_regs(task);
4043 * stop monitoring at the user level
4045 ia64_psr(tregs)->up = 0;
4048 * monitoring disabled in kernel at next reschedule
4050 ctx->ctx_saved_psr_up = 0;
4051 DPRINT(("task=[%d]\n", task_pid_nr(task)));
4058 pfm_start(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4060 struct pt_regs *tregs;
4061 int state, is_system;
4063 state = ctx->ctx_state;
4064 is_system = ctx->ctx_fl_system;
4066 if (state != PFM_CTX_LOADED) return -EINVAL;
4069 * In system wide and when the context is loaded, access can only happen
4070 * when the caller is running on the CPU being monitored by the session.
4071 * It does not have to be the owner (ctx_task) of the context per se.
4073 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
4074 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
4079 * in system mode, we need to update the PMU directly
4080 * and the user level state of the caller, which may not
4081 * necessarily be the creator of the context.
4086 * set user level psr.pp for the caller
4088 ia64_psr(regs)->pp = 1;
4091 * now update the local PMU and cpuinfo
4093 PFM_CPUINFO_SET(PFM_CPUINFO_DCR_PP);
4096 * start monitoring at kernel level
4101 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
4111 if (ctx->ctx_task == current) {
4113 /* start monitoring at kernel level */
4117 * activate monitoring at user level
4119 ia64_psr(regs)->up = 1;
4122 tregs = task_pt_regs(ctx->ctx_task);
4125 * start monitoring at the kernel level the next
4126 * time the task is scheduled
4128 ctx->ctx_saved_psr_up = IA64_PSR_UP;
4131 * activate monitoring at user level
4133 ia64_psr(tregs)->up = 1;
4139 pfm_get_pmc_reset(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4141 pfarg_reg_t *req = (pfarg_reg_t *)arg;
4146 for (i = 0; i < count; i++, req++) {
4148 cnum = req->reg_num;
4150 if (!PMC_IS_IMPL(cnum)) goto abort_mission;
4152 req->reg_value = PMC_DFL_VAL(cnum);
4154 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
4156 DPRINT(("pmc_reset_val pmc[%u]=0x%lx\n", cnum, req->reg_value));
4161 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
4166 pfm_check_task_exist(pfm_context_t *ctx)
4168 struct task_struct *g, *t;
4171 read_lock(&tasklist_lock);
4173 do_each_thread (g, t) {
4174 if (t->thread.pfm_context == ctx) {
4178 } while_each_thread (g, t);
4180 read_unlock(&tasklist_lock);
4182 DPRINT(("pfm_check_task_exist: ret=%d ctx=%p\n", ret, ctx));
4188 pfm_context_load(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4190 struct task_struct *task;
4191 struct thread_struct *thread;
4192 struct pfm_context_t *old;
4193 unsigned long flags;
4195 struct task_struct *owner_task = NULL;
4197 pfarg_load_t *req = (pfarg_load_t *)arg;
4198 unsigned long *pmcs_source, *pmds_source;
4201 int state, is_system, set_dbregs = 0;
4203 state = ctx->ctx_state;
4204 is_system = ctx->ctx_fl_system;
4206 * can only load from unloaded or terminated state
4208 if (state != PFM_CTX_UNLOADED) {
4209 DPRINT(("cannot load to [%d], invalid ctx_state=%d\n",
4215 DPRINT(("load_pid [%d] using_dbreg=%d\n", req->load_pid, ctx->ctx_fl_using_dbreg));
4217 if (CTX_OVFL_NOBLOCK(ctx) == 0 && req->load_pid == current->pid) {
4218 DPRINT(("cannot use blocking mode on self\n"));
4222 ret = pfm_get_task(ctx, req->load_pid, &task);
4224 DPRINT(("load_pid [%d] get_task=%d\n", req->load_pid, ret));
4231 * system wide is self monitoring only
4233 if (is_system && task != current) {
4234 DPRINT(("system wide is self monitoring only load_pid=%d\n",
4239 thread = &task->thread;
4243 * cannot load a context which is using range restrictions,
4244 * into a task that is being debugged.
4246 if (ctx->ctx_fl_using_dbreg) {
4247 if (thread->flags & IA64_THREAD_DBG_VALID) {
4249 DPRINT(("load_pid [%d] task is debugged, cannot load range restrictions\n", req->load_pid));
4255 if (pfm_sessions.pfs_ptrace_use_dbregs) {
4256 DPRINT(("cannot load [%d] dbregs in use\n",
4257 task_pid_nr(task)));
4260 pfm_sessions.pfs_sys_use_dbregs++;
4261 DPRINT(("load [%d] increased sys_use_dbreg=%u\n", task_pid_nr(task), pfm_sessions.pfs_sys_use_dbregs));
4268 if (ret) goto error;
4272 * SMP system-wide monitoring implies self-monitoring.
4274 * The programming model expects the task to
4275 * be pinned on a CPU throughout the session.
4276 * Here we take note of the current CPU at the
4277 * time the context is loaded. No call from
4278 * another CPU will be allowed.
4280 * The pinning via shed_setaffinity()
4281 * must be done by the calling task prior
4284 * systemwide: keep track of CPU this session is supposed to run on
4286 the_cpu = ctx->ctx_cpu = smp_processor_id();
4290 * now reserve the session
4292 ret = pfm_reserve_session(current, is_system, the_cpu);
4293 if (ret) goto error;
4296 * task is necessarily stopped at this point.
4298 * If the previous context was zombie, then it got removed in
4299 * pfm_save_regs(). Therefore we should not see it here.
4300 * If we see a context, then this is an active context
4302 * XXX: needs to be atomic
4304 DPRINT(("before cmpxchg() old_ctx=%p new_ctx=%p\n",
4305 thread->pfm_context, ctx));
4308 old = ia64_cmpxchg(acq, &thread->pfm_context, NULL, ctx, sizeof(pfm_context_t *));
4310 DPRINT(("load_pid [%d] already has a context\n", req->load_pid));
4314 pfm_reset_msgq(ctx);
4316 ctx->ctx_state = PFM_CTX_LOADED;
4319 * link context to task
4321 ctx->ctx_task = task;
4325 * we load as stopped
4327 PFM_CPUINFO_SET(PFM_CPUINFO_SYST_WIDE);
4328 PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
4330 if (ctx->ctx_fl_excl_idle) PFM_CPUINFO_SET(PFM_CPUINFO_EXCL_IDLE);
4332 thread->flags |= IA64_THREAD_PM_VALID;
4336 * propagate into thread-state
4338 pfm_copy_pmds(task, ctx);
4339 pfm_copy_pmcs(task, ctx);
4341 pmcs_source = ctx->th_pmcs;
4342 pmds_source = ctx->th_pmds;
4345 * always the case for system-wide
4347 if (task == current) {
4349 if (is_system == 0) {
4351 /* allow user level control */
4352 ia64_psr(regs)->sp = 0;
4353 DPRINT(("clearing psr.sp for [%d]\n", task_pid_nr(task)));
4355 SET_LAST_CPU(ctx, smp_processor_id());
4357 SET_ACTIVATION(ctx);
4360 * push the other task out, if any
4362 owner_task = GET_PMU_OWNER();
4363 if (owner_task) pfm_lazy_save_regs(owner_task);
4367 * load all PMD from ctx to PMU (as opposed to thread state)
4368 * restore all PMC from ctx to PMU
4370 pfm_restore_pmds(pmds_source, ctx->ctx_all_pmds[0]);
4371 pfm_restore_pmcs(pmcs_source, ctx->ctx_all_pmcs[0]);
4373 ctx->ctx_reload_pmcs[0] = 0UL;
4374 ctx->ctx_reload_pmds[0] = 0UL;
4377 * guaranteed safe by earlier check against DBG_VALID
4379 if (ctx->ctx_fl_using_dbreg) {
4380 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
4381 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
4386 SET_PMU_OWNER(task, ctx);
4388 DPRINT(("context loaded on PMU for [%d]\n", task_pid_nr(task)));
4391 * when not current, task MUST be stopped, so this is safe
4393 regs = task_pt_regs(task);
4395 /* force a full reload */
4396 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
4397 SET_LAST_CPU(ctx, -1);
4399 /* initial saved psr (stopped) */
4400 ctx->ctx_saved_psr_up = 0UL;
4401 ia64_psr(regs)->up = ia64_psr(regs)->pp = 0;
4407 if (ret) pfm_unreserve_session(ctx, ctx->ctx_fl_system, the_cpu);
4410 * we must undo the dbregs setting (for system-wide)
4412 if (ret && set_dbregs) {
4414 pfm_sessions.pfs_sys_use_dbregs--;
4418 * release task, there is now a link with the context
4420 if (is_system == 0 && task != current) {
4424 ret = pfm_check_task_exist(ctx);
4426 ctx->ctx_state = PFM_CTX_UNLOADED;
4427 ctx->ctx_task = NULL;
4435 * in this function, we do not need to increase the use count
4436 * for the task via get_task_struct(), because we hold the
4437 * context lock. If the task were to disappear while having
4438 * a context attached, it would go through pfm_exit_thread()
4439 * which also grabs the context lock and would therefore be blocked
4440 * until we are here.
4442 static void pfm_flush_pmds(struct task_struct *, pfm_context_t *ctx);
4445 pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4447 struct task_struct *task = PFM_CTX_TASK(ctx);
4448 struct pt_regs *tregs;
4449 int prev_state, is_system;
4452 DPRINT(("ctx_state=%d task [%d]\n", ctx->ctx_state, task ? task_pid_nr(task) : -1));
4454 prev_state = ctx->ctx_state;
4455 is_system = ctx->ctx_fl_system;
4458 * unload only when necessary
4460 if (prev_state == PFM_CTX_UNLOADED) {
4461 DPRINT(("ctx_state=%d, nothing to do\n", prev_state));
4466 * clear psr and dcr bits
4468 ret = pfm_stop(ctx, NULL, 0, regs);
4469 if (ret) return ret;
4471 ctx->ctx_state = PFM_CTX_UNLOADED;
4474 * in system mode, we need to update the PMU directly
4475 * and the user level state of the caller, which may not
4476 * necessarily be the creator of the context.
4483 * local PMU is taken care of in pfm_stop()
4485 PFM_CPUINFO_CLEAR(PFM_CPUINFO_SYST_WIDE);
4486 PFM_CPUINFO_CLEAR(PFM_CPUINFO_EXCL_IDLE);
4489 * save PMDs in context
4492 pfm_flush_pmds(current, ctx);
4495 * at this point we are done with the PMU
4496 * so we can unreserve the resource.
4498 if (prev_state != PFM_CTX_ZOMBIE)
4499 pfm_unreserve_session(ctx, 1 , ctx->ctx_cpu);
4502 * disconnect context from task
4504 task->thread.pfm_context = NULL;
4506 * disconnect task from context
4508 ctx->ctx_task = NULL;
4511 * There is nothing more to cleanup here.
4519 tregs = task == current ? regs : task_pt_regs(task);
4521 if (task == current) {
4523 * cancel user level control
4525 ia64_psr(regs)->sp = 1;
4527 DPRINT(("setting psr.sp for [%d]\n", task_pid_nr(task)));
4530 * save PMDs to context
4533 pfm_flush_pmds(task, ctx);
4536 * at this point we are done with the PMU
4537 * so we can unreserve the resource.
4539 * when state was ZOMBIE, we have already unreserved.
4541 if (prev_state != PFM_CTX_ZOMBIE)
4542 pfm_unreserve_session(ctx, 0 , ctx->ctx_cpu);
4545 * reset activation counter and psr
4547 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
4548 SET_LAST_CPU(ctx, -1);
4551 * PMU state will not be restored
4553 task->thread.flags &= ~IA64_THREAD_PM_VALID;
4556 * break links between context and task
4558 task->thread.pfm_context = NULL;
4559 ctx->ctx_task = NULL;
4561 PFM_SET_WORK_PENDING(task, 0);
4563 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
4564 ctx->ctx_fl_can_restart = 0;
4565 ctx->ctx_fl_going_zombie = 0;
4567 DPRINT(("disconnected [%d] from context\n", task_pid_nr(task)));
4574 * called only from exit_thread(): task == current
4575 * we come here only if current has a context attached (loaded or masked)
4578 pfm_exit_thread(struct task_struct *task)
4581 unsigned long flags;
4582 struct pt_regs *regs = task_pt_regs(task);
4586 ctx = PFM_GET_CTX(task);
4588 PROTECT_CTX(ctx, flags);
4590 DPRINT(("state=%d task [%d]\n", ctx->ctx_state, task_pid_nr(task)));
4592 state = ctx->ctx_state;
4594 case PFM_CTX_UNLOADED:
4596 * only comes to this function if pfm_context is not NULL, i.e., cannot
4597 * be in unloaded state
4599 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] ctx unloaded\n", task_pid_nr(task));
4601 case PFM_CTX_LOADED:
4602 case PFM_CTX_MASKED:
4603 ret = pfm_context_unload(ctx, NULL, 0, regs);
4605 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task_pid_nr(task), state, ret);
4607 DPRINT(("ctx unloaded for current state was %d\n", state));
4609 pfm_end_notify_user(ctx);
4611 case PFM_CTX_ZOMBIE:
4612 ret = pfm_context_unload(ctx, NULL, 0, regs);
4614 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task_pid_nr(task), state, ret);
4619 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] unexpected state=%d\n", task_pid_nr(task), state);
4622 UNPROTECT_CTX(ctx, flags);
4624 { u64 psr = pfm_get_psr();
4625 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
4626 BUG_ON(GET_PMU_OWNER());
4627 BUG_ON(ia64_psr(regs)->up);
4628 BUG_ON(ia64_psr(regs)->pp);
4632 * All memory free operations (especially for vmalloc'ed memory)
4633 * MUST be done with interrupts ENABLED.
4635 if (free_ok) pfm_context_free(ctx);
4639 * functions MUST be listed in the increasing order of their index (see permfon.h)
4641 #define PFM_CMD(name, flags, arg_count, arg_type, getsz) { name, #name, flags, arg_count, sizeof(arg_type), getsz }
4642 #define PFM_CMD_S(name, flags) { name, #name, flags, 0, 0, NULL }
4643 #define PFM_CMD_PCLRWS (PFM_CMD_FD|PFM_CMD_ARG_RW|PFM_CMD_STOP)
4644 #define PFM_CMD_PCLRW (PFM_CMD_FD|PFM_CMD_ARG_RW)
4645 #define PFM_CMD_NONE { NULL, "no-cmd", 0, 0, 0, NULL}
4647 static pfm_cmd_desc_t pfm_cmd_tab[]={
4648 /* 0 */PFM_CMD_NONE,
4649 /* 1 */PFM_CMD(pfm_write_pmcs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4650 /* 2 */PFM_CMD(pfm_write_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4651 /* 3 */PFM_CMD(pfm_read_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4652 /* 4 */PFM_CMD_S(pfm_stop, PFM_CMD_PCLRWS),
4653 /* 5 */PFM_CMD_S(pfm_start, PFM_CMD_PCLRWS),
4654 /* 6 */PFM_CMD_NONE,
4655 /* 7 */PFM_CMD_NONE,
4656 /* 8 */PFM_CMD(pfm_context_create, PFM_CMD_ARG_RW, 1, pfarg_context_t, pfm_ctx_getsize),
4657 /* 9 */PFM_CMD_NONE,
4658 /* 10 */PFM_CMD_S(pfm_restart, PFM_CMD_PCLRW),
4659 /* 11 */PFM_CMD_NONE,
4660 /* 12 */PFM_CMD(pfm_get_features, PFM_CMD_ARG_RW, 1, pfarg_features_t, NULL),
4661 /* 13 */PFM_CMD(pfm_debug, 0, 1, unsigned int, NULL),
4662 /* 14 */PFM_CMD_NONE,
4663 /* 15 */PFM_CMD(pfm_get_pmc_reset, PFM_CMD_ARG_RW, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4664 /* 16 */PFM_CMD(pfm_context_load, PFM_CMD_PCLRWS, 1, pfarg_load_t, NULL),
4665 /* 17 */PFM_CMD_S(pfm_context_unload, PFM_CMD_PCLRWS),
4666 /* 18 */PFM_CMD_NONE,
4667 /* 19 */PFM_CMD_NONE,
4668 /* 20 */PFM_CMD_NONE,
4669 /* 21 */PFM_CMD_NONE,
4670 /* 22 */PFM_CMD_NONE,
4671 /* 23 */PFM_CMD_NONE,
4672 /* 24 */PFM_CMD_NONE,
4673 /* 25 */PFM_CMD_NONE,
4674 /* 26 */PFM_CMD_NONE,
4675 /* 27 */PFM_CMD_NONE,
4676 /* 28 */PFM_CMD_NONE,
4677 /* 29 */PFM_CMD_NONE,
4678 /* 30 */PFM_CMD_NONE,
4679 /* 31 */PFM_CMD_NONE,
4680 /* 32 */PFM_CMD(pfm_write_ibrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL),
4681 /* 33 */PFM_CMD(pfm_write_dbrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL)
4683 #define PFM_CMD_COUNT (sizeof(pfm_cmd_tab)/sizeof(pfm_cmd_desc_t))
4686 pfm_check_task_state(pfm_context_t *ctx, int cmd, unsigned long flags)
4688 struct task_struct *task;
4689 int state, old_state;
4692 state = ctx->ctx_state;
4693 task = ctx->ctx_task;
4696 DPRINT(("context %d no task, state=%d\n", ctx->ctx_fd, state));
4700 DPRINT(("context %d state=%d [%d] task_state=%ld must_stop=%d\n",
4704 task->state, PFM_CMD_STOPPED(cmd)));
4707 * self-monitoring always ok.
4709 * for system-wide the caller can either be the creator of the
4710 * context (to one to which the context is attached to) OR
4711 * a task running on the same CPU as the session.
4713 if (task == current || ctx->ctx_fl_system) return 0;
4716 * we are monitoring another thread
4719 case PFM_CTX_UNLOADED:
4721 * if context is UNLOADED we are safe to go
4724 case PFM_CTX_ZOMBIE:
4726 * no command can operate on a zombie context
4728 DPRINT(("cmd %d state zombie cannot operate on context\n", cmd));
4730 case PFM_CTX_MASKED:
4732 * PMU state has been saved to software even though
4733 * the thread may still be running.
4735 if (cmd != PFM_UNLOAD_CONTEXT) return 0;
4739 * context is LOADED or MASKED. Some commands may need to have
4742 * We could lift this restriction for UP but it would mean that
4743 * the user has no guarantee the task would not run between
4744 * two successive calls to perfmonctl(). That's probably OK.
4745 * If this user wants to ensure the task does not run, then
4746 * the task must be stopped.
4748 if (PFM_CMD_STOPPED(cmd)) {
4749 if (!task_is_stopped_or_traced(task)) {
4750 DPRINT(("[%d] task not in stopped state\n", task_pid_nr(task)));
4754 * task is now stopped, wait for ctxsw out
4756 * This is an interesting point in the code.
4757 * We need to unprotect the context because
4758 * the pfm_save_regs() routines needs to grab
4759 * the same lock. There are danger in doing
4760 * this because it leaves a window open for
4761 * another task to get access to the context
4762 * and possibly change its state. The one thing
4763 * that is not possible is for the context to disappear
4764 * because we are protected by the VFS layer, i.e.,
4765 * get_fd()/put_fd().
4769 UNPROTECT_CTX(ctx, flags);
4771 wait_task_inactive(task, 0);
4773 PROTECT_CTX(ctx, flags);
4776 * we must recheck to verify if state has changed
4778 if (ctx->ctx_state != old_state) {
4779 DPRINT(("old_state=%d new_state=%d\n", old_state, ctx->ctx_state));
4787 * system-call entry point (must return long)
4790 sys_perfmonctl (int fd, int cmd, void __user *arg, int count)
4792 struct file *file = NULL;
4793 pfm_context_t *ctx = NULL;
4794 unsigned long flags = 0UL;
4795 void *args_k = NULL;
4796 long ret; /* will expand int return types */
4797 size_t base_sz, sz, xtra_sz = 0;
4798 int narg, completed_args = 0, call_made = 0, cmd_flags;
4799 int (*func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
4800 int (*getsize)(void *arg, size_t *sz);
4801 #define PFM_MAX_ARGSIZE 4096
4804 * reject any call if perfmon was disabled at initialization
4806 if (unlikely(pmu_conf == NULL)) return -ENOSYS;
4808 if (unlikely(cmd < 0 || cmd >= PFM_CMD_COUNT)) {
4809 DPRINT(("invalid cmd=%d\n", cmd));
4813 func = pfm_cmd_tab[cmd].cmd_func;
4814 narg = pfm_cmd_tab[cmd].cmd_narg;
4815 base_sz = pfm_cmd_tab[cmd].cmd_argsize;
4816 getsize = pfm_cmd_tab[cmd].cmd_getsize;
4817 cmd_flags = pfm_cmd_tab[cmd].cmd_flags;
4819 if (unlikely(func == NULL)) {
4820 DPRINT(("invalid cmd=%d\n", cmd));
4824 DPRINT(("cmd=%s idx=%d narg=0x%x argsz=%lu count=%d\n",
4832 * check if number of arguments matches what the command expects
4834 if (unlikely((narg == PFM_CMD_ARG_MANY && count <= 0) || (narg > 0 && narg != count)))
4838 sz = xtra_sz + base_sz*count;
4840 * limit abuse to min page size
4842 if (unlikely(sz > PFM_MAX_ARGSIZE)) {
4843 printk(KERN_ERR "perfmon: [%d] argument too big %lu\n", task_pid_nr(current), sz);
4848 * allocate default-sized argument buffer
4850 if (likely(count && args_k == NULL)) {
4851 args_k = kmalloc(PFM_MAX_ARGSIZE, GFP_KERNEL);
4852 if (args_k == NULL) return -ENOMEM;
4860 * assume sz = 0 for command without parameters
4862 if (sz && copy_from_user(args_k, arg, sz)) {
4863 DPRINT(("cannot copy_from_user %lu bytes @%p\n", sz, arg));
4868 * check if command supports extra parameters
4870 if (completed_args == 0 && getsize) {
4872 * get extra parameters size (based on main argument)
4874 ret = (*getsize)(args_k, &xtra_sz);
4875 if (ret) goto error_args;
4879 DPRINT(("restart_args sz=%lu xtra_sz=%lu\n", sz, xtra_sz));
4881 /* retry if necessary */
4882 if (likely(xtra_sz)) goto restart_args;
4885 if (unlikely((cmd_flags & PFM_CMD_FD) == 0)) goto skip_fd;
4890 if (unlikely(file == NULL)) {
4891 DPRINT(("invalid fd %d\n", fd));
4894 if (unlikely(PFM_IS_FILE(file) == 0)) {
4895 DPRINT(("fd %d not related to perfmon\n", fd));
4899 ctx = file->private_data;
4900 if (unlikely(ctx == NULL)) {
4901 DPRINT(("no context for fd %d\n", fd));
4904 prefetch(&ctx->ctx_state);
4906 PROTECT_CTX(ctx, flags);
4909 * check task is stopped
4911 ret = pfm_check_task_state(ctx, cmd, flags);
4912 if (unlikely(ret)) goto abort_locked;
4915 ret = (*func)(ctx, args_k, count, task_pt_regs(current));
4921 DPRINT(("context unlocked\n"));
4922 UNPROTECT_CTX(ctx, flags);
4925 /* copy argument back to user, if needed */
4926 if (call_made && PFM_CMD_RW_ARG(cmd) && copy_to_user(arg, args_k, base_sz*count)) ret = -EFAULT;
4934 DPRINT(("cmd=%s ret=%ld\n", PFM_CMD_NAME(cmd), ret));
4940 pfm_resume_after_ovfl(pfm_context_t *ctx, unsigned long ovfl_regs, struct pt_regs *regs)
4942 pfm_buffer_fmt_t *fmt = ctx->ctx_buf_fmt;
4943 pfm_ovfl_ctrl_t rst_ctrl;
4947 state = ctx->ctx_state;
4949 * Unlock sampling buffer and reset index atomically
4950 * XXX: not really needed when blocking
4952 if (CTX_HAS_SMPL(ctx)) {
4954 rst_ctrl.bits.mask_monitoring = 0;
4955 rst_ctrl.bits.reset_ovfl_pmds = 0;
4957 if (state == PFM_CTX_LOADED)
4958 ret = pfm_buf_fmt_restart_active(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
4960 ret = pfm_buf_fmt_restart(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
4962 rst_ctrl.bits.mask_monitoring = 0;
4963 rst_ctrl.bits.reset_ovfl_pmds = 1;
4967 if (rst_ctrl.bits.reset_ovfl_pmds) {
4968 pfm_reset_regs(ctx, &ovfl_regs, PFM_PMD_LONG_RESET);
4970 if (rst_ctrl.bits.mask_monitoring == 0) {
4971 DPRINT(("resuming monitoring\n"));
4972 if (ctx->ctx_state == PFM_CTX_MASKED) pfm_restore_monitoring(current);
4974 DPRINT(("stopping monitoring\n"));
4975 //pfm_stop_monitoring(current, regs);
4977 ctx->ctx_state = PFM_CTX_LOADED;
4982 * context MUST BE LOCKED when calling
4983 * can only be called for current
4986 pfm_context_force_terminate(pfm_context_t *ctx, struct pt_regs *regs)
4990 DPRINT(("entering for [%d]\n", task_pid_nr(current)));
4992 ret = pfm_context_unload(ctx, NULL, 0, regs);
4994 printk(KERN_ERR "pfm_context_force_terminate: [%d] unloaded failed with %d\n", task_pid_nr(current), ret);
4998 * and wakeup controlling task, indicating we are now disconnected
5000 wake_up_interruptible(&ctx->ctx_zombieq);
5003 * given that context is still locked, the controlling
5004 * task will only get access when we return from
5005 * pfm_handle_work().
5009 static int pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds);
5012 * pfm_handle_work() can be called with interrupts enabled
5013 * (TIF_NEED_RESCHED) or disabled. The down_interruptible
5014 * call may sleep, therefore we must re-enable interrupts
5015 * to avoid deadlocks. It is safe to do so because this function
5016 * is called ONLY when returning to user level (pUStk=1), in which case
5017 * there is no risk of kernel stack overflow due to deep
5018 * interrupt nesting.
5021 pfm_handle_work(void)
5024 struct pt_regs *regs;
5025 unsigned long flags, dummy_flags;
5026 unsigned long ovfl_regs;
5027 unsigned int reason;
5030 ctx = PFM_GET_CTX(current);
5032 printk(KERN_ERR "perfmon: [%d] has no PFM context\n",
5033 task_pid_nr(current));
5037 PROTECT_CTX(ctx, flags);
5039 PFM_SET_WORK_PENDING(current, 0);
5041 regs = task_pt_regs(current);
5044 * extract reason for being here and clear
5046 reason = ctx->ctx_fl_trap_reason;
5047 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
5048 ovfl_regs = ctx->ctx_ovfl_regs[0];
5050 DPRINT(("reason=%d state=%d\n", reason, ctx->ctx_state));
5053 * must be done before we check for simple-reset mode
5055 if (ctx->ctx_fl_going_zombie || ctx->ctx_state == PFM_CTX_ZOMBIE)
5058 //if (CTX_OVFL_NOBLOCK(ctx)) goto skip_blocking;
5059 if (reason == PFM_TRAP_REASON_RESET)
5063 * restore interrupt mask to what it was on entry.
5064 * Could be enabled/diasbled.
5066 UNPROTECT_CTX(ctx, flags);
5069 * force interrupt enable because of down_interruptible()
5073 DPRINT(("before block sleeping\n"));
5076 * may go through without blocking on SMP systems
5077 * if restart has been received already by the time we call down()
5079 ret = wait_for_completion_interruptible(&ctx->ctx_restart_done);
5081 DPRINT(("after block sleeping ret=%d\n", ret));
5084 * lock context and mask interrupts again
5085 * We save flags into a dummy because we may have
5086 * altered interrupts mask compared to entry in this
5089 PROTECT_CTX(ctx, dummy_flags);
5092 * we need to read the ovfl_regs only after wake-up
5093 * because we may have had pfm_write_pmds() in between
5094 * and that can changed PMD values and therefore
5095 * ovfl_regs is reset for these new PMD values.
5097 ovfl_regs = ctx->ctx_ovfl_regs[0];
5099 if (ctx->ctx_fl_going_zombie) {
5101 DPRINT(("context is zombie, bailing out\n"));
5102 pfm_context_force_terminate(ctx, regs);
5106 * in case of interruption of down() we don't restart anything
5112 pfm_resume_after_ovfl(ctx, ovfl_regs, regs);
5113 ctx->ctx_ovfl_regs[0] = 0UL;
5117 * restore flags as they were upon entry
5119 UNPROTECT_CTX(ctx, flags);
5123 pfm_notify_user(pfm_context_t *ctx, pfm_msg_t *msg)
5125 if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
5126 DPRINT(("ignoring overflow notification, owner is zombie\n"));
5130 DPRINT(("waking up somebody\n"));
5132 if (msg) wake_up_interruptible(&ctx->ctx_msgq_wait);
5135 * safe, we are not in intr handler, nor in ctxsw when
5138 kill_fasync (&ctx->ctx_async_queue, SIGIO, POLL_IN);
5144 pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds)
5146 pfm_msg_t *msg = NULL;
5148 if (ctx->ctx_fl_no_msg == 0) {
5149 msg = pfm_get_new_msg(ctx);
5151 printk(KERN_ERR "perfmon: pfm_ovfl_notify_user no more notification msgs\n");
5155 msg->pfm_ovfl_msg.msg_type = PFM_MSG_OVFL;
5156 msg->pfm_ovfl_msg.msg_ctx_fd = ctx->ctx_fd;
5157 msg->pfm_ovfl_msg.msg_active_set = 0;
5158 msg->pfm_ovfl_msg.msg_ovfl_pmds[0] = ovfl_pmds;
5159 msg->pfm_ovfl_msg.msg_ovfl_pmds[1] = 0UL;
5160 msg->pfm_ovfl_msg.msg_ovfl_pmds[2] = 0UL;
5161 msg->pfm_ovfl_msg.msg_ovfl_pmds[3] = 0UL;
5162 msg->pfm_ovfl_msg.msg_tstamp = 0UL;
5165 DPRINT(("ovfl msg: msg=%p no_msg=%d fd=%d ovfl_pmds=0x%lx\n",
5171 return pfm_notify_user(ctx, msg);
5175 pfm_end_notify_user(pfm_context_t *ctx)
5179 msg = pfm_get_new_msg(ctx);
5181 printk(KERN_ERR "perfmon: pfm_end_notify_user no more notification msgs\n");
5185 memset(msg, 0, sizeof(*msg));
5187 msg->pfm_end_msg.msg_type = PFM_MSG_END;
5188 msg->pfm_end_msg.msg_ctx_fd = ctx->ctx_fd;
5189 msg->pfm_ovfl_msg.msg_tstamp = 0UL;
5191 DPRINT(("end msg: msg=%p no_msg=%d ctx_fd=%d\n",
5196 return pfm_notify_user(ctx, msg);
5200 * main overflow processing routine.
5201 * it can be called from the interrupt path or explicitly during the context switch code
5203 static void pfm_overflow_handler(struct task_struct *task, pfm_context_t *ctx,
5204 unsigned long pmc0, struct pt_regs *regs)
5206 pfm_ovfl_arg_t *ovfl_arg;
5208 unsigned long old_val, ovfl_val, new_val;
5209 unsigned long ovfl_notify = 0UL, ovfl_pmds = 0UL, smpl_pmds = 0UL, reset_pmds;
5210 unsigned long tstamp;
5211 pfm_ovfl_ctrl_t ovfl_ctrl;
5212 unsigned int i, has_smpl;
5213 int must_notify = 0;
5215 if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) goto stop_monitoring;
5218 * sanity test. Should never happen
5220 if (unlikely((pmc0 & 0x1) == 0)) goto sanity_check;
5222 tstamp = ia64_get_itc();
5223 mask = pmc0 >> PMU_FIRST_COUNTER;
5224 ovfl_val = pmu_conf->ovfl_val;
5225 has_smpl = CTX_HAS_SMPL(ctx);
5227 DPRINT_ovfl(("pmc0=0x%lx pid=%d iip=0x%lx, %s "
5228 "used_pmds=0x%lx\n",
5230 task ? task_pid_nr(task): -1,
5231 (regs ? regs->cr_iip : 0),
5232 CTX_OVFL_NOBLOCK(ctx) ? "nonblocking" : "blocking",
5233 ctx->ctx_used_pmds[0]));
5237 * first we update the virtual counters
5238 * assume there was a prior ia64_srlz_d() issued
5240 for (i = PMU_FIRST_COUNTER; mask ; i++, mask >>= 1) {
5242 /* skip pmd which did not overflow */
5243 if ((mask & 0x1) == 0) continue;
5246 * Note that the pmd is not necessarily 0 at this point as qualified events
5247 * may have happened before the PMU was frozen. The residual count is not
5248 * taken into consideration here but will be with any read of the pmd via
5251 old_val = new_val = ctx->ctx_pmds[i].val;
5252 new_val += 1 + ovfl_val;
5253 ctx->ctx_pmds[i].val = new_val;
5256 * check for overflow condition
5258 if (likely(old_val > new_val)) {
5259 ovfl_pmds |= 1UL << i;
5260 if (PMC_OVFL_NOTIFY(ctx, i)) ovfl_notify |= 1UL << i;
5263 DPRINT_ovfl(("ctx_pmd[%d].val=0x%lx old_val=0x%lx pmd=0x%lx ovfl_pmds=0x%lx ovfl_notify=0x%lx\n",
5267 ia64_get_pmd(i) & ovfl_val,
5273 * there was no 64-bit overflow, nothing else to do
5275 if (ovfl_pmds == 0UL) return;
5278 * reset all control bits
5284 * if a sampling format module exists, then we "cache" the overflow by
5285 * calling the module's handler() routine.
5288 unsigned long start_cycles, end_cycles;
5289 unsigned long pmd_mask;
5291 int this_cpu = smp_processor_id();
5293 pmd_mask = ovfl_pmds >> PMU_FIRST_COUNTER;
5294 ovfl_arg = &ctx->ctx_ovfl_arg;
5296 prefetch(ctx->ctx_smpl_hdr);
5298 for(i=PMU_FIRST_COUNTER; pmd_mask && ret == 0; i++, pmd_mask >>=1) {
5302 if ((pmd_mask & 0x1) == 0) continue;
5304 ovfl_arg->ovfl_pmd = (unsigned char )i;
5305 ovfl_arg->ovfl_notify = ovfl_notify & mask ? 1 : 0;
5306 ovfl_arg->active_set = 0;
5307 ovfl_arg->ovfl_ctrl.val = 0; /* module must fill in all fields */
5308 ovfl_arg->smpl_pmds[0] = smpl_pmds = ctx->ctx_pmds[i].smpl_pmds[0];
5310 ovfl_arg->pmd_value = ctx->ctx_pmds[i].val;
5311 ovfl_arg->pmd_last_reset = ctx->ctx_pmds[i].lval;
5312 ovfl_arg->pmd_eventid = ctx->ctx_pmds[i].eventid;
5315 * copy values of pmds of interest. Sampling format may copy them
5316 * into sampling buffer.
5319 for(j=0, k=0; smpl_pmds; j++, smpl_pmds >>=1) {
5320 if ((smpl_pmds & 0x1) == 0) continue;
5321 ovfl_arg->smpl_pmds_values[k++] = PMD_IS_COUNTING(j) ? pfm_read_soft_counter(ctx, j) : ia64_get_pmd(j);
5322 DPRINT_ovfl(("smpl_pmd[%d]=pmd%u=0x%lx\n", k-1, j, ovfl_arg->smpl_pmds_values[k-1]));
5326 pfm_stats[this_cpu].pfm_smpl_handler_calls++;
5328 start_cycles = ia64_get_itc();
5331 * call custom buffer format record (handler) routine
5333 ret = (*ctx->ctx_buf_fmt->fmt_handler)(task, ctx->ctx_smpl_hdr, ovfl_arg, regs, tstamp);
5335 end_cycles = ia64_get_itc();
5338 * For those controls, we take the union because they have
5339 * an all or nothing behavior.
5341 ovfl_ctrl.bits.notify_user |= ovfl_arg->ovfl_ctrl.bits.notify_user;
5342 ovfl_ctrl.bits.block_task |= ovfl_arg->ovfl_ctrl.bits.block_task;
5343 ovfl_ctrl.bits.mask_monitoring |= ovfl_arg->ovfl_ctrl.bits.mask_monitoring;
5345 * build the bitmask of pmds to reset now
5347 if (ovfl_arg->ovfl_ctrl.bits.reset_ovfl_pmds) reset_pmds |= mask;
5349 pfm_stats[this_cpu].pfm_smpl_handler_cycles += end_cycles - start_cycles;
5352 * when the module cannot handle the rest of the overflows, we abort right here
5354 if (ret && pmd_mask) {
5355 DPRINT(("handler aborts leftover ovfl_pmds=0x%lx\n",
5356 pmd_mask<<PMU_FIRST_COUNTER));
5359 * remove the pmds we reset now from the set of pmds to reset in pfm_restart()
5361 ovfl_pmds &= ~reset_pmds;
5364 * when no sampling module is used, then the default
5365 * is to notify on overflow if requested by user
5367 ovfl_ctrl.bits.notify_user = ovfl_notify ? 1 : 0;
5368 ovfl_ctrl.bits.block_task = ovfl_notify ? 1 : 0;
5369 ovfl_ctrl.bits.mask_monitoring = ovfl_notify ? 1 : 0; /* XXX: change for saturation */
5370 ovfl_ctrl.bits.reset_ovfl_pmds = ovfl_notify ? 0 : 1;
5372 * if needed, we reset all overflowed pmds
5374 if (ovfl_notify == 0) reset_pmds = ovfl_pmds;
5377 DPRINT_ovfl(("ovfl_pmds=0x%lx reset_pmds=0x%lx\n", ovfl_pmds, reset_pmds));
5380 * reset the requested PMD registers using the short reset values
5383 unsigned long bm = reset_pmds;
5384 pfm_reset_regs(ctx, &bm, PFM_PMD_SHORT_RESET);
5387 if (ovfl_notify && ovfl_ctrl.bits.notify_user) {
5389 * keep track of what to reset when unblocking
5391 ctx->ctx_ovfl_regs[0] = ovfl_pmds;
5394 * check for blocking context
5396 if (CTX_OVFL_NOBLOCK(ctx) == 0 && ovfl_ctrl.bits.block_task) {
5398 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_BLOCK;
5401 * set the perfmon specific checking pending work for the task
5403 PFM_SET_WORK_PENDING(task, 1);
5406 * when coming from ctxsw, current still points to the
5407 * previous task, therefore we must work with task and not current.
5409 set_notify_resume(task);
5412 * defer until state is changed (shorten spin window). the context is locked
5413 * anyway, so the signal receiver would come spin for nothing.
5418 DPRINT_ovfl(("owner [%d] pending=%ld reason=%u ovfl_pmds=0x%lx ovfl_notify=0x%lx masked=%d\n",
5419 GET_PMU_OWNER() ? task_pid_nr(GET_PMU_OWNER()) : -1,
5420 PFM_GET_WORK_PENDING(task),
5421 ctx->ctx_fl_trap_reason,
5424 ovfl_ctrl.bits.mask_monitoring ? 1 : 0));
5426 * in case monitoring must be stopped, we toggle the psr bits
5428 if (ovfl_ctrl.bits.mask_monitoring) {
5429 pfm_mask_monitoring(task);
5430 ctx->ctx_state = PFM_CTX_MASKED;
5431 ctx->ctx_fl_can_restart = 1;
5435 * send notification now
5437 if (must_notify) pfm_ovfl_notify_user(ctx, ovfl_notify);
5442 printk(KERN_ERR "perfmon: CPU%d overflow handler [%d] pmc0=0x%lx\n",
5444 task ? task_pid_nr(task) : -1,
5450 * in SMP, zombie context is never restored but reclaimed in pfm_load_regs().
5451 * Moreover, zombies are also reclaimed in pfm_save_regs(). Therefore we can
5452 * come here as zombie only if the task is the current task. In which case, we
5453 * can access the PMU hardware directly.
5455 * Note that zombies do have PM_VALID set. So here we do the minimal.
5457 * In case the context was zombified it could not be reclaimed at the time
5458 * the monitoring program exited. At this point, the PMU reservation has been
5459 * returned, the sampiing buffer has been freed. We must convert this call
5460 * into a spurious interrupt. However, we must also avoid infinite overflows
5461 * by stopping monitoring for this task. We can only come here for a per-task
5462 * context. All we need to do is to stop monitoring using the psr bits which
5463 * are always task private. By re-enabling secure montioring, we ensure that
5464 * the monitored task will not be able to re-activate monitoring.
5465 * The task will eventually be context switched out, at which point the context
5466 * will be reclaimed (that includes releasing ownership of the PMU).
5468 * So there might be a window of time where the number of per-task session is zero
5469 * yet one PMU might have a owner and get at most one overflow interrupt for a zombie
5470 * context. This is safe because if a per-task session comes in, it will push this one
5471 * out and by the virtue on pfm_save_regs(), this one will disappear. If a system wide
5472 * session is force on that CPU, given that we use task pinning, pfm_save_regs() will
5473 * also push our zombie context out.
5475 * Overall pretty hairy stuff....
5477 DPRINT(("ctx is zombie for [%d], converted to spurious\n", task ? task_pid_nr(task): -1));
5479 ia64_psr(regs)->up = 0;
5480 ia64_psr(regs)->sp = 1;
5485 pfm_do_interrupt_handler(void *arg, struct pt_regs *regs)
5487 struct task_struct *task;
5489 unsigned long flags;
5491 int this_cpu = smp_processor_id();
5494 pfm_stats[this_cpu].pfm_ovfl_intr_count++;
5497 * srlz.d done before arriving here
5499 pmc0 = ia64_get_pmc(0);
5501 task = GET_PMU_OWNER();
5502 ctx = GET_PMU_CTX();
5505 * if we have some pending bits set
5506 * assumes : if any PMC0.bit[63-1] is set, then PMC0.fr = 1
5508 if (PMC0_HAS_OVFL(pmc0) && task) {
5510 * we assume that pmc0.fr is always set here
5514 if (!ctx) goto report_spurious1;
5516 if (ctx->ctx_fl_system == 0 && (task->thread.flags & IA64_THREAD_PM_VALID) == 0)
5517 goto report_spurious2;
5519 PROTECT_CTX_NOPRINT(ctx, flags);
5521 pfm_overflow_handler(task, ctx, pmc0, regs);
5523 UNPROTECT_CTX_NOPRINT(ctx, flags);
5526 pfm_stats[this_cpu].pfm_spurious_ovfl_intr_count++;
5530 * keep it unfrozen at all times
5537 printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d has no PFM context\n",
5538 this_cpu, task_pid_nr(task));
5542 printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d, invalid flag\n",
5550 pfm_interrupt_handler(int irq, void *arg)
5552 unsigned long start_cycles, total_cycles;
5553 unsigned long min, max;
5556 struct pt_regs *regs = get_irq_regs();
5558 this_cpu = get_cpu();
5559 if (likely(!pfm_alt_intr_handler)) {
5560 min = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min;
5561 max = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max;
5563 start_cycles = ia64_get_itc();
5565 ret = pfm_do_interrupt_handler(arg, regs);
5567 total_cycles = ia64_get_itc();
5570 * don't measure spurious interrupts
5572 if (likely(ret == 0)) {
5573 total_cycles -= start_cycles;
5575 if (total_cycles < min) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min = total_cycles;
5576 if (total_cycles > max) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max = total_cycles;
5578 pfm_stats[this_cpu].pfm_ovfl_intr_cycles += total_cycles;
5582 (*pfm_alt_intr_handler->handler)(irq, arg, regs);
5590 * /proc/perfmon interface, for debug only
5593 #define PFM_PROC_SHOW_HEADER ((void *)(long)nr_cpu_ids+1)
5596 pfm_proc_start(struct seq_file *m, loff_t *pos)
5599 return PFM_PROC_SHOW_HEADER;
5602 while (*pos <= nr_cpu_ids) {
5603 if (cpu_online(*pos - 1)) {
5604 return (void *)*pos;
5612 pfm_proc_next(struct seq_file *m, void *v, loff_t *pos)
5615 return pfm_proc_start(m, pos);
5619 pfm_proc_stop(struct seq_file *m, void *v)
5624 pfm_proc_show_header(struct seq_file *m)
5626 struct list_head * pos;
5627 pfm_buffer_fmt_t * entry;
5628 unsigned long flags;
5631 "perfmon version : %u.%u\n"
5634 "expert mode : %s\n"
5635 "ovfl_mask : 0x%lx\n"
5636 "PMU flags : 0x%x\n",
5637 PFM_VERSION_MAJ, PFM_VERSION_MIN,
5639 pfm_sysctl.fastctxsw > 0 ? "Yes": "No",
5640 pfm_sysctl.expert_mode > 0 ? "Yes": "No",
5647 "proc_sessions : %u\n"
5648 "sys_sessions : %u\n"
5649 "sys_use_dbregs : %u\n"
5650 "ptrace_use_dbregs : %u\n",
5651 pfm_sessions.pfs_task_sessions,
5652 pfm_sessions.pfs_sys_sessions,
5653 pfm_sessions.pfs_sys_use_dbregs,
5654 pfm_sessions.pfs_ptrace_use_dbregs);
5658 spin_lock(&pfm_buffer_fmt_lock);
5660 list_for_each(pos, &pfm_buffer_fmt_list) {
5661 entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
5662 seq_printf(m, "format : %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x %s\n",
5673 entry->fmt_uuid[10],
5674 entry->fmt_uuid[11],
5675 entry->fmt_uuid[12],
5676 entry->fmt_uuid[13],
5677 entry->fmt_uuid[14],
5678 entry->fmt_uuid[15],
5681 spin_unlock(&pfm_buffer_fmt_lock);
5686 pfm_proc_show(struct seq_file *m, void *v)
5692 if (v == PFM_PROC_SHOW_HEADER) {
5693 pfm_proc_show_header(m);
5697 /* show info for CPU (v - 1) */
5701 "CPU%-2d overflow intrs : %lu\n"
5702 "CPU%-2d overflow cycles : %lu\n"
5703 "CPU%-2d overflow min : %lu\n"
5704 "CPU%-2d overflow max : %lu\n"
5705 "CPU%-2d smpl handler calls : %lu\n"
5706 "CPU%-2d smpl handler cycles : %lu\n"
5707 "CPU%-2d spurious intrs : %lu\n"
5708 "CPU%-2d replay intrs : %lu\n"
5709 "CPU%-2d syst_wide : %d\n"
5710 "CPU%-2d dcr_pp : %d\n"
5711 "CPU%-2d exclude idle : %d\n"
5712 "CPU%-2d owner : %d\n"
5713 "CPU%-2d context : %p\n"
5714 "CPU%-2d activations : %lu\n",
5715 cpu, pfm_stats[cpu].pfm_ovfl_intr_count,
5716 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles,
5717 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_min,
5718 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_max,
5719 cpu, pfm_stats[cpu].pfm_smpl_handler_calls,
5720 cpu, pfm_stats[cpu].pfm_smpl_handler_cycles,
5721 cpu, pfm_stats[cpu].pfm_spurious_ovfl_intr_count,
5722 cpu, pfm_stats[cpu].pfm_replay_ovfl_intr_count,
5723 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_SYST_WIDE ? 1 : 0,
5724 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_DCR_PP ? 1 : 0,
5725 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_EXCL_IDLE ? 1 : 0,
5726 cpu, pfm_get_cpu_data(pmu_owner, cpu) ? pfm_get_cpu_data(pmu_owner, cpu)->pid: -1,
5727 cpu, pfm_get_cpu_data(pmu_ctx, cpu),
5728 cpu, pfm_get_cpu_data(pmu_activation_number, cpu));
5730 if (num_online_cpus() == 1 && pfm_sysctl.debug > 0) {
5732 psr = pfm_get_psr();
5737 "CPU%-2d psr : 0x%lx\n"
5738 "CPU%-2d pmc0 : 0x%lx\n",
5740 cpu, ia64_get_pmc(0));
5742 for (i=0; PMC_IS_LAST(i) == 0; i++) {
5743 if (PMC_IS_COUNTING(i) == 0) continue;
5745 "CPU%-2d pmc%u : 0x%lx\n"
5746 "CPU%-2d pmd%u : 0x%lx\n",
5747 cpu, i, ia64_get_pmc(i),
5748 cpu, i, ia64_get_pmd(i));
5754 const struct seq_operations pfm_seq_ops = {
5755 .start = pfm_proc_start,
5756 .next = pfm_proc_next,
5757 .stop = pfm_proc_stop,
5758 .show = pfm_proc_show
5762 pfm_proc_open(struct inode *inode, struct file *file)
5764 return seq_open(file, &pfm_seq_ops);
5769 * we come here as soon as local_cpu_data->pfm_syst_wide is set. this happens
5770 * during pfm_enable() hence before pfm_start(). We cannot assume monitoring
5771 * is active or inactive based on mode. We must rely on the value in
5772 * local_cpu_data->pfm_syst_info
5775 pfm_syst_wide_update_task(struct task_struct *task, unsigned long info, int is_ctxswin)
5777 struct pt_regs *regs;
5779 unsigned long dcr_pp;
5781 dcr_pp = info & PFM_CPUINFO_DCR_PP ? 1 : 0;
5784 * pid 0 is guaranteed to be the idle task. There is one such task with pid 0
5785 * on every CPU, so we can rely on the pid to identify the idle task.
5787 if ((info & PFM_CPUINFO_EXCL_IDLE) == 0 || task->pid) {
5788 regs = task_pt_regs(task);
5789 ia64_psr(regs)->pp = is_ctxswin ? dcr_pp : 0;
5793 * if monitoring has started
5796 dcr = ia64_getreg(_IA64_REG_CR_DCR);
5798 * context switching in?
5801 /* mask monitoring for the idle task */
5802 ia64_setreg(_IA64_REG_CR_DCR, dcr & ~IA64_DCR_PP);
5808 * context switching out
5809 * restore monitoring for next task
5811 * Due to inlining this odd if-then-else construction generates
5814 ia64_setreg(_IA64_REG_CR_DCR, dcr |IA64_DCR_PP);
5823 pfm_force_cleanup(pfm_context_t *ctx, struct pt_regs *regs)
5825 struct task_struct *task = ctx->ctx_task;
5827 ia64_psr(regs)->up = 0;
5828 ia64_psr(regs)->sp = 1;
5830 if (GET_PMU_OWNER() == task) {
5831 DPRINT(("cleared ownership for [%d]\n",
5832 task_pid_nr(ctx->ctx_task)));
5833 SET_PMU_OWNER(NULL, NULL);
5837 * disconnect the task from the context and vice-versa
5839 PFM_SET_WORK_PENDING(task, 0);
5841 task->thread.pfm_context = NULL;
5842 task->thread.flags &= ~IA64_THREAD_PM_VALID;
5844 DPRINT(("force cleanup for [%d]\n", task_pid_nr(task)));
5849 * in 2.6, interrupts are masked when we come here and the runqueue lock is held
5852 pfm_save_regs(struct task_struct *task)
5855 unsigned long flags;
5859 ctx = PFM_GET_CTX(task);
5860 if (ctx == NULL) return;
5863 * we always come here with interrupts ALREADY disabled by
5864 * the scheduler. So we simply need to protect against concurrent
5865 * access, not CPU concurrency.
5867 flags = pfm_protect_ctx_ctxsw(ctx);
5869 if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
5870 struct pt_regs *regs = task_pt_regs(task);
5874 pfm_force_cleanup(ctx, regs);
5876 BUG_ON(ctx->ctx_smpl_hdr);
5878 pfm_unprotect_ctx_ctxsw(ctx, flags);
5880 pfm_context_free(ctx);
5885 * save current PSR: needed because we modify it
5888 psr = pfm_get_psr();
5890 BUG_ON(psr & (IA64_PSR_I));
5894 * This is the last instruction which may generate an overflow
5896 * We do not need to set psr.sp because, it is irrelevant in kernel.
5897 * It will be restored from ipsr when going back to user level
5902 * keep a copy of psr.up (for reload)
5904 ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
5907 * release ownership of this PMU.
5908 * PM interrupts are masked, so nothing
5911 SET_PMU_OWNER(NULL, NULL);
5914 * we systematically save the PMD as we have no
5915 * guarantee we will be schedule at that same
5918 pfm_save_pmds(ctx->th_pmds, ctx->ctx_used_pmds[0]);
5921 * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
5922 * we will need it on the restore path to check
5923 * for pending overflow.
5925 ctx->th_pmcs[0] = ia64_get_pmc(0);
5928 * unfreeze PMU if had pending overflows
5930 if (ctx->th_pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
5933 * finally, allow context access.
5934 * interrupts will still be masked after this call.
5936 pfm_unprotect_ctx_ctxsw(ctx, flags);
5939 #else /* !CONFIG_SMP */
5941 pfm_save_regs(struct task_struct *task)
5946 ctx = PFM_GET_CTX(task);
5947 if (ctx == NULL) return;
5950 * save current PSR: needed because we modify it
5952 psr = pfm_get_psr();
5954 BUG_ON(psr & (IA64_PSR_I));
5958 * This is the last instruction which may generate an overflow
5960 * We do not need to set psr.sp because, it is irrelevant in kernel.
5961 * It will be restored from ipsr when going back to user level
5966 * keep a copy of psr.up (for reload)
5968 ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
5972 pfm_lazy_save_regs (struct task_struct *task)
5975 unsigned long flags;
5977 { u64 psr = pfm_get_psr();
5978 BUG_ON(psr & IA64_PSR_UP);
5981 ctx = PFM_GET_CTX(task);
5984 * we need to mask PMU overflow here to
5985 * make sure that we maintain pmc0 until
5986 * we save it. overflow interrupts are
5987 * treated as spurious if there is no
5990 * XXX: I don't think this is necessary
5992 PROTECT_CTX(ctx,flags);
5995 * release ownership of this PMU.
5996 * must be done before we save the registers.
5998 * after this call any PMU interrupt is treated
6001 SET_PMU_OWNER(NULL, NULL);
6004 * save all the pmds we use
6006 pfm_save_pmds(ctx->th_pmds, ctx->ctx_used_pmds[0]);
6009 * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
6010 * it is needed to check for pended overflow
6011 * on the restore path
6013 ctx->th_pmcs[0] = ia64_get_pmc(0);
6016 * unfreeze PMU if had pending overflows
6018 if (ctx->th_pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
6021 * now get can unmask PMU interrupts, they will
6022 * be treated as purely spurious and we will not
6023 * lose any information
6025 UNPROTECT_CTX(ctx,flags);
6027 #endif /* CONFIG_SMP */
6031 * in 2.6, interrupts are masked when we come here and the runqueue lock is held
6034 pfm_load_regs (struct task_struct *task)
6037 unsigned long pmc_mask = 0UL, pmd_mask = 0UL;
6038 unsigned long flags;
6040 int need_irq_resend;
6042 ctx = PFM_GET_CTX(task);
6043 if (unlikely(ctx == NULL)) return;
6045 BUG_ON(GET_PMU_OWNER());
6048 * possible on unload
6050 if (unlikely((task->thread.flags & IA64_THREAD_PM_VALID) == 0)) return;
6053 * we always come here with interrupts ALREADY disabled by
6054 * the scheduler. So we simply need to protect against concurrent
6055 * access, not CPU concurrency.
6057 flags = pfm_protect_ctx_ctxsw(ctx);
6058 psr = pfm_get_psr();
6060 need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
6062 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
6063 BUG_ON(psr & IA64_PSR_I);
6065 if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) {
6066 struct pt_regs *regs = task_pt_regs(task);
6068 BUG_ON(ctx->ctx_smpl_hdr);
6070 pfm_force_cleanup(ctx, regs);
6072 pfm_unprotect_ctx_ctxsw(ctx, flags);
6075 * this one (kmalloc'ed) is fine with interrupts disabled
6077 pfm_context_free(ctx);
6083 * we restore ALL the debug registers to avoid picking up
6086 if (ctx->ctx_fl_using_dbreg) {
6087 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
6088 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
6091 * retrieve saved psr.up
6093 psr_up = ctx->ctx_saved_psr_up;
6096 * if we were the last user of the PMU on that CPU,
6097 * then nothing to do except restore psr
6099 if (GET_LAST_CPU(ctx) == smp_processor_id() && ctx->ctx_last_activation == GET_ACTIVATION()) {
6102 * retrieve partial reload masks (due to user modifications)
6104 pmc_mask = ctx->ctx_reload_pmcs[0];
6105 pmd_mask = ctx->ctx_reload_pmds[0];
6109 * To avoid leaking information to the user level when psr.sp=0,
6110 * we must reload ALL implemented pmds (even the ones we don't use).
6111 * In the kernel we only allow PFM_READ_PMDS on registers which
6112 * we initialized or requested (sampling) so there is no risk there.
6114 pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
6117 * ALL accessible PMCs are systematically reloaded, unused registers
6118 * get their default (from pfm_reset_pmu_state()) values to avoid picking
6119 * up stale configuration.
6121 * PMC0 is never in the mask. It is always restored separately.
6123 pmc_mask = ctx->ctx_all_pmcs[0];
6126 * when context is MASKED, we will restore PMC with plm=0
6127 * and PMD with stale information, but that's ok, nothing
6130 * XXX: optimize here
6132 if (pmd_mask) pfm_restore_pmds(ctx->th_pmds, pmd_mask);
6133 if (pmc_mask) pfm_restore_pmcs(ctx->th_pmcs, pmc_mask);
6136 * check for pending overflow at the time the state
6139 if (unlikely(PMC0_HAS_OVFL(ctx->th_pmcs[0]))) {
6141 * reload pmc0 with the overflow information
6142 * On McKinley PMU, this will trigger a PMU interrupt
6144 ia64_set_pmc(0, ctx->th_pmcs[0]);
6146 ctx->th_pmcs[0] = 0UL;
6149 * will replay the PMU interrupt
6151 if (need_irq_resend) ia64_resend_irq(IA64_PERFMON_VECTOR);
6153 pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
6157 * we just did a reload, so we reset the partial reload fields
6159 ctx->ctx_reload_pmcs[0] = 0UL;
6160 ctx->ctx_reload_pmds[0] = 0UL;
6162 SET_LAST_CPU(ctx, smp_processor_id());
6165 * dump activation value for this PMU
6169 * record current activation for this context
6171 SET_ACTIVATION(ctx);
6174 * establish new ownership.
6176 SET_PMU_OWNER(task, ctx);
6179 * restore the psr.up bit. measurement
6181 * no PMU interrupt can happen at this point
6182 * because we still have interrupts disabled.
6184 if (likely(psr_up)) pfm_set_psr_up();
6187 * allow concurrent access to context
6189 pfm_unprotect_ctx_ctxsw(ctx, flags);
6191 #else /* !CONFIG_SMP */
6193 * reload PMU state for UP kernels
6194 * in 2.5 we come here with interrupts disabled
6197 pfm_load_regs (struct task_struct *task)
6200 struct task_struct *owner;
6201 unsigned long pmd_mask, pmc_mask;
6203 int need_irq_resend;
6205 owner = GET_PMU_OWNER();
6206 ctx = PFM_GET_CTX(task);
6207 psr = pfm_get_psr();
6209 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
6210 BUG_ON(psr & IA64_PSR_I);
6213 * we restore ALL the debug registers to avoid picking up
6216 * This must be done even when the task is still the owner
6217 * as the registers may have been modified via ptrace()
6218 * (not perfmon) by the previous task.
6220 if (ctx->ctx_fl_using_dbreg) {
6221 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
6222 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
6226 * retrieved saved psr.up
6228 psr_up = ctx->ctx_saved_psr_up;
6229 need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
6232 * short path, our state is still there, just
6233 * need to restore psr and we go
6235 * we do not touch either PMC nor PMD. the psr is not touched
6236 * by the overflow_handler. So we are safe w.r.t. to interrupt
6237 * concurrency even without interrupt masking.
6239 if (likely(owner == task)) {
6240 if (likely(psr_up)) pfm_set_psr_up();
6245 * someone else is still using the PMU, first push it out and
6246 * then we'll be able to install our stuff !
6248 * Upon return, there will be no owner for the current PMU
6250 if (owner) pfm_lazy_save_regs(owner);
6253 * To avoid leaking information to the user level when psr.sp=0,
6254 * we must reload ALL implemented pmds (even the ones we don't use).
6255 * In the kernel we only allow PFM_READ_PMDS on registers which
6256 * we initialized or requested (sampling) so there is no risk there.
6258 pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
6261 * ALL accessible PMCs are systematically reloaded, unused registers
6262 * get their default (from pfm_reset_pmu_state()) values to avoid picking
6263 * up stale configuration.
6265 * PMC0 is never in the mask. It is always restored separately
6267 pmc_mask = ctx->ctx_all_pmcs[0];
6269 pfm_restore_pmds(ctx->th_pmds, pmd_mask);
6270 pfm_restore_pmcs(ctx->th_pmcs, pmc_mask);
6273 * check for pending overflow at the time the state
6276 if (unlikely(PMC0_HAS_OVFL(ctx->th_pmcs[0]))) {
6278 * reload pmc0 with the overflow information
6279 * On McKinley PMU, this will trigger a PMU interrupt
6281 ia64_set_pmc(0, ctx->th_pmcs[0]);
6284 ctx->th_pmcs[0] = 0UL;
6287 * will replay the PMU interrupt
6289 if (need_irq_resend) ia64_resend_irq(IA64_PERFMON_VECTOR);
6291 pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
6295 * establish new ownership.
6297 SET_PMU_OWNER(task, ctx);
6300 * restore the psr.up bit. measurement
6302 * no PMU interrupt can happen at this point
6303 * because we still have interrupts disabled.
6305 if (likely(psr_up)) pfm_set_psr_up();
6307 #endif /* CONFIG_SMP */
6310 * this function assumes monitoring is stopped
6313 pfm_flush_pmds(struct task_struct *task, pfm_context_t *ctx)
6316 unsigned long mask2, val, pmd_val, ovfl_val;
6317 int i, can_access_pmu = 0;
6321 * is the caller the task being monitored (or which initiated the
6322 * session for system wide measurements)
6324 is_self = ctx->ctx_task == task ? 1 : 0;
6327 * can access PMU is task is the owner of the PMU state on the current CPU
6328 * or if we are running on the CPU bound to the context in system-wide mode
6329 * (that is not necessarily the task the context is attached to in this mode).
6330 * In system-wide we always have can_access_pmu true because a task running on an
6331 * invalid processor is flagged earlier in the call stack (see pfm_stop).
6333 can_access_pmu = (GET_PMU_OWNER() == task) || (ctx->ctx_fl_system && ctx->ctx_cpu == smp_processor_id());
6334 if (can_access_pmu) {
6336 * Mark the PMU as not owned
6337 * This will cause the interrupt handler to do nothing in case an overflow
6338 * interrupt was in-flight
6339 * This also guarantees that pmc0 will contain the final state
6340 * It virtually gives us full control on overflow processing from that point
6343 SET_PMU_OWNER(NULL, NULL);
6344 DPRINT(("releasing ownership\n"));
6347 * read current overflow status:
6349 * we are guaranteed to read the final stable state
6352 pmc0 = ia64_get_pmc(0); /* slow */
6355 * reset freeze bit, overflow status information destroyed
6359 pmc0 = ctx->th_pmcs[0];
6361 * clear whatever overflow status bits there were
6363 ctx->th_pmcs[0] = 0;
6365 ovfl_val = pmu_conf->ovfl_val;
6367 * we save all the used pmds
6368 * we take care of overflows for counting PMDs
6370 * XXX: sampling situation is not taken into account here
6372 mask2 = ctx->ctx_used_pmds[0];
6374 DPRINT(("is_self=%d ovfl_val=0x%lx mask2=0x%lx\n", is_self, ovfl_val, mask2));
6376 for (i = 0; mask2; i++, mask2>>=1) {
6378 /* skip non used pmds */
6379 if ((mask2 & 0x1) == 0) continue;
6382 * can access PMU always true in system wide mode
6384 val = pmd_val = can_access_pmu ? ia64_get_pmd(i) : ctx->th_pmds[i];
6386 if (PMD_IS_COUNTING(i)) {
6387 DPRINT(("[%d] pmd[%d] ctx_pmd=0x%lx hw_pmd=0x%lx\n",
6390 ctx->ctx_pmds[i].val,
6394 * we rebuild the full 64 bit value of the counter
6396 val = ctx->ctx_pmds[i].val + (val & ovfl_val);
6399 * now everything is in ctx_pmds[] and we need
6400 * to clear the saved context from save_regs() such that
6401 * pfm_read_pmds() gets the correct value
6406 * take care of overflow inline
6408 if (pmc0 & (1UL << i)) {
6409 val += 1 + ovfl_val;
6410 DPRINT(("[%d] pmd[%d] overflowed\n", task_pid_nr(task), i));
6414 DPRINT(("[%d] ctx_pmd[%d]=0x%lx pmd_val=0x%lx\n", task_pid_nr(task), i, val, pmd_val));
6416 if (is_self) ctx->th_pmds[i] = pmd_val;
6418 ctx->ctx_pmds[i].val = val;
6422 static struct irqaction perfmon_irqaction = {
6423 .handler = pfm_interrupt_handler,
6424 .flags = IRQF_DISABLED,
6429 pfm_alt_save_pmu_state(void *data)
6431 struct pt_regs *regs;
6433 regs = task_pt_regs(current);
6435 DPRINT(("called\n"));
6438 * should not be necessary but
6439 * let's take not risk
6443 ia64_psr(regs)->pp = 0;
6446 * This call is required
6447 * May cause a spurious interrupt on some processors
6455 pfm_alt_restore_pmu_state(void *data)
6457 struct pt_regs *regs;
6459 regs = task_pt_regs(current);
6461 DPRINT(("called\n"));
6464 * put PMU back in state expected
6469 ia64_psr(regs)->pp = 0;
6472 * perfmon runs with PMU unfrozen at all times
6480 pfm_install_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
6485 /* some sanity checks */
6486 if (hdl == NULL || hdl->handler == NULL) return -EINVAL;
6488 /* do the easy test first */
6489 if (pfm_alt_intr_handler) return -EBUSY;
6491 /* one at a time in the install or remove, just fail the others */
6492 if (!spin_trylock(&pfm_alt_install_check)) {
6496 /* reserve our session */
6497 for_each_online_cpu(reserve_cpu) {
6498 ret = pfm_reserve_session(NULL, 1, reserve_cpu);
6499 if (ret) goto cleanup_reserve;
6502 /* save the current system wide pmu states */
6503 ret = on_each_cpu(pfm_alt_save_pmu_state, NULL, 1);
6505 DPRINT(("on_each_cpu() failed: %d\n", ret));
6506 goto cleanup_reserve;
6509 /* officially change to the alternate interrupt handler */
6510 pfm_alt_intr_handler = hdl;
6512 spin_unlock(&pfm_alt_install_check);
6517 for_each_online_cpu(i) {
6518 /* don't unreserve more than we reserved */
6519 if (i >= reserve_cpu) break;
6521 pfm_unreserve_session(NULL, 1, i);
6524 spin_unlock(&pfm_alt_install_check);
6528 EXPORT_SYMBOL_GPL(pfm_install_alt_pmu_interrupt);
6531 pfm_remove_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
6536 if (hdl == NULL) return -EINVAL;
6538 /* cannot remove someone else's handler! */
6539 if (pfm_alt_intr_handler != hdl) return -EINVAL;
6541 /* one at a time in the install or remove, just fail the others */
6542 if (!spin_trylock(&pfm_alt_install_check)) {
6546 pfm_alt_intr_handler = NULL;
6548 ret = on_each_cpu(pfm_alt_restore_pmu_state, NULL, 1);
6550 DPRINT(("on_each_cpu() failed: %d\n", ret));
6553 for_each_online_cpu(i) {
6554 pfm_unreserve_session(NULL, 1, i);
6557 spin_unlock(&pfm_alt_install_check);
6561 EXPORT_SYMBOL_GPL(pfm_remove_alt_pmu_interrupt);
6564 * perfmon initialization routine, called from the initcall() table
6566 static int init_pfm_fs(void);
6574 family = local_cpu_data->family;
6579 if ((*p)->probe() == 0) goto found;
6580 } else if ((*p)->pmu_family == family || (*p)->pmu_family == 0xff) {
6591 static const struct file_operations pfm_proc_fops = {
6592 .open = pfm_proc_open,
6594 .llseek = seq_lseek,
6595 .release = seq_release,
6601 unsigned int n, n_counters, i;
6603 printk("perfmon: version %u.%u IRQ %u\n",
6606 IA64_PERFMON_VECTOR);
6608 if (pfm_probe_pmu()) {
6609 printk(KERN_INFO "perfmon: disabled, there is no support for processor family %d\n",
6610 local_cpu_data->family);
6615 * compute the number of implemented PMD/PMC from the
6616 * description tables
6619 for (i=0; PMC_IS_LAST(i) == 0; i++) {
6620 if (PMC_IS_IMPL(i) == 0) continue;
6621 pmu_conf->impl_pmcs[i>>6] |= 1UL << (i&63);
6624 pmu_conf->num_pmcs = n;
6626 n = 0; n_counters = 0;
6627 for (i=0; PMD_IS_LAST(i) == 0; i++) {
6628 if (PMD_IS_IMPL(i) == 0) continue;
6629 pmu_conf->impl_pmds[i>>6] |= 1UL << (i&63);
6631 if (PMD_IS_COUNTING(i)) n_counters++;
6633 pmu_conf->num_pmds = n;
6634 pmu_conf->num_counters = n_counters;
6637 * sanity checks on the number of debug registers
6639 if (pmu_conf->use_rr_dbregs) {
6640 if (pmu_conf->num_ibrs > IA64_NUM_DBG_REGS) {
6641 printk(KERN_INFO "perfmon: unsupported number of code debug registers (%u)\n", pmu_conf->num_ibrs);
6645 if (pmu_conf->num_dbrs > IA64_NUM_DBG_REGS) {
6646 printk(KERN_INFO "perfmon: unsupported number of data debug registers (%u)\n", pmu_conf->num_ibrs);
6652 printk("perfmon: %s PMU detected, %u PMCs, %u PMDs, %u counters (%lu bits)\n",
6656 pmu_conf->num_counters,
6657 ffz(pmu_conf->ovfl_val));
6660 if (pmu_conf->num_pmds >= PFM_NUM_PMD_REGS || pmu_conf->num_pmcs >= PFM_NUM_PMC_REGS) {
6661 printk(KERN_ERR "perfmon: not enough pmc/pmd, perfmon disabled\n");
6667 * create /proc/perfmon (mostly for debugging purposes)
6669 perfmon_dir = proc_create("perfmon", S_IRUGO, NULL, &pfm_proc_fops);
6670 if (perfmon_dir == NULL) {
6671 printk(KERN_ERR "perfmon: cannot create /proc entry, perfmon disabled\n");
6677 * create /proc/sys/kernel/perfmon (for debugging purposes)
6679 pfm_sysctl_header = register_sysctl_table(pfm_sysctl_root);
6682 * initialize all our spinlocks
6684 spin_lock_init(&pfm_sessions.pfs_lock);
6685 spin_lock_init(&pfm_buffer_fmt_lock);
6689 for(i=0; i < NR_CPUS; i++) pfm_stats[i].pfm_ovfl_intr_cycles_min = ~0UL;
6694 __initcall(pfm_init);
6697 * this function is called before pfm_init()
6700 pfm_init_percpu (void)
6702 static int first_time=1;
6704 * make sure no measurement is active
6705 * (may inherit programmed PMCs from EFI).
6711 * we run with the PMU not frozen at all times
6716 register_percpu_irq(IA64_PERFMON_VECTOR, &perfmon_irqaction);
6720 ia64_setreg(_IA64_REG_CR_PMV, IA64_PERFMON_VECTOR);
6725 * used for debug purposes only
6728 dump_pmu_state(const char *from)
6730 struct task_struct *task;
6731 struct pt_regs *regs;
6733 unsigned long psr, dcr, info, flags;
6736 local_irq_save(flags);
6738 this_cpu = smp_processor_id();
6739 regs = task_pt_regs(current);
6740 info = PFM_CPUINFO_GET();
6741 dcr = ia64_getreg(_IA64_REG_CR_DCR);
6743 if (info == 0 && ia64_psr(regs)->pp == 0 && (dcr & IA64_DCR_PP) == 0) {
6744 local_irq_restore(flags);
6748 printk("CPU%d from %s() current [%d] iip=0x%lx %s\n",
6751 task_pid_nr(current),
6755 task = GET_PMU_OWNER();
6756 ctx = GET_PMU_CTX();
6758 printk("->CPU%d owner [%d] ctx=%p\n", this_cpu, task ? task_pid_nr(task) : -1, ctx);
6760 psr = pfm_get_psr();
6762 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",
6765 psr & IA64_PSR_PP ? 1 : 0,
6766 psr & IA64_PSR_UP ? 1 : 0,
6767 dcr & IA64_DCR_PP ? 1 : 0,
6770 ia64_psr(regs)->pp);
6772 ia64_psr(regs)->up = 0;
6773 ia64_psr(regs)->pp = 0;
6775 for (i=1; PMC_IS_LAST(i) == 0; i++) {
6776 if (PMC_IS_IMPL(i) == 0) continue;
6777 printk("->CPU%d pmc[%d]=0x%lx thread_pmc[%d]=0x%lx\n", this_cpu, i, ia64_get_pmc(i), i, ctx->th_pmcs[i]);
6780 for (i=1; PMD_IS_LAST(i) == 0; i++) {
6781 if (PMD_IS_IMPL(i) == 0) continue;
6782 printk("->CPU%d pmd[%d]=0x%lx thread_pmd[%d]=0x%lx\n", this_cpu, i, ia64_get_pmd(i), i, ctx->th_pmds[i]);
6786 printk("->CPU%d ctx_state=%d vaddr=%p addr=%p fd=%d ctx_task=[%d] saved_psr_up=0x%lx\n",
6789 ctx->ctx_smpl_vaddr,
6793 ctx->ctx_saved_psr_up);
6795 local_irq_restore(flags);
6799 * called from process.c:copy_thread(). task is new child.
6802 pfm_inherit(struct task_struct *task, struct pt_regs *regs)
6804 struct thread_struct *thread;
6806 DPRINT(("perfmon: pfm_inherit clearing state for [%d]\n", task_pid_nr(task)));
6808 thread = &task->thread;
6811 * cut links inherited from parent (current)
6813 thread->pfm_context = NULL;
6815 PFM_SET_WORK_PENDING(task, 0);
6818 * the psr bits are already set properly in copy_threads()
6821 #else /* !CONFIG_PERFMON */
6823 sys_perfmonctl (int fd, int cmd, void *arg, int count)
6827 #endif /* CONFIG_PERFMON */