1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file implements the perfmon-2 subsystem which is used
4 * to program the IA-64 Performance Monitoring Unit (PMU).
6 * The initial version of perfmon.c was written by
7 * Ganesh Venkitachalam, IBM Corp.
9 * Then it was modified for perfmon-1.x by Stephane Eranian and
10 * David Mosberger, Hewlett Packard Co.
12 * Version Perfmon-2.x is a rewrite of perfmon-1.x
13 * by Stephane Eranian, Hewlett Packard Co.
15 * Copyright (C) 1999-2005 Hewlett Packard Co
16 * Stephane Eranian <eranian@hpl.hp.com>
17 * David Mosberger-Tang <davidm@hpl.hp.com>
19 * More information about perfmon available at:
20 * http://www.hpl.hp.com/research/linux/perfmon
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/sched/task.h>
27 #include <linux/sched/task_stack.h>
28 #include <linux/interrupt.h>
29 #include <linux/proc_fs.h>
30 #include <linux/seq_file.h>
31 #include <linux/init.h>
32 #include <linux/vmalloc.h>
34 #include <linux/sysctl.h>
35 #include <linux/list.h>
36 #include <linux/file.h>
37 #include <linux/poll.h>
38 #include <linux/vfs.h>
39 #include <linux/smp.h>
40 #include <linux/pagemap.h>
41 #include <linux/mount.h>
42 #include <linux/bitops.h>
43 #include <linux/capability.h>
44 #include <linux/rcupdate.h>
45 #include <linux/completion.h>
46 #include <linux/tracehook.h>
47 #include <linux/slab.h>
48 #include <linux/cpu.h>
50 #include <asm/errno.h>
51 #include <asm/intrinsics.h>
53 #include <asm/perfmon.h>
54 #include <asm/processor.h>
55 #include <asm/signal.h>
56 #include <linux/uaccess.h>
57 #include <asm/delay.h>
61 * perfmon context state
63 #define PFM_CTX_UNLOADED 1 /* context is not loaded onto any task */
64 #define PFM_CTX_LOADED 2 /* context is loaded onto a task */
65 #define PFM_CTX_MASKED 3 /* context is loaded but monitoring is masked due to overflow */
66 #define PFM_CTX_ZOMBIE 4 /* owner of the context is closing it */
68 #define PFM_INVALID_ACTIVATION (~0UL)
70 #define PFM_NUM_PMC_REGS 64 /* PMC save area for ctxsw */
71 #define PFM_NUM_PMD_REGS 64 /* PMD save area for ctxsw */
74 * depth of message queue
76 #define PFM_MAX_MSGS 32
77 #define PFM_CTXQ_EMPTY(g) ((g)->ctx_msgq_head == (g)->ctx_msgq_tail)
80 * type of a PMU register (bitmask).
82 * bit0 : register implemented
85 * bit4 : pmc has pmc.pm
86 * bit5 : pmc controls a counter (has pmc.oi), pmd is used as counter
87 * bit6-7 : register type
90 #define PFM_REG_NOTIMPL 0x0 /* not implemented at all */
91 #define PFM_REG_IMPL 0x1 /* register implemented */
92 #define PFM_REG_END 0x2 /* end marker */
93 #define PFM_REG_MONITOR (0x1<<4|PFM_REG_IMPL) /* a PMC with a pmc.pm field only */
94 #define PFM_REG_COUNTING (0x2<<4|PFM_REG_MONITOR) /* a monitor + pmc.oi+ PMD used as a counter */
95 #define PFM_REG_CONTROL (0x4<<4|PFM_REG_IMPL) /* PMU control register */
96 #define PFM_REG_CONFIG (0x8<<4|PFM_REG_IMPL) /* configuration register */
97 #define PFM_REG_BUFFER (0xc<<4|PFM_REG_IMPL) /* PMD used as buffer */
99 #define PMC_IS_LAST(i) (pmu_conf->pmc_desc[i].type & PFM_REG_END)
100 #define PMD_IS_LAST(i) (pmu_conf->pmd_desc[i].type & PFM_REG_END)
102 #define PMC_OVFL_NOTIFY(ctx, i) ((ctx)->ctx_pmds[i].flags & PFM_REGFL_OVFL_NOTIFY)
104 /* i assumed unsigned */
105 #define PMC_IS_IMPL(i) (i< PMU_MAX_PMCS && (pmu_conf->pmc_desc[i].type & PFM_REG_IMPL))
106 #define PMD_IS_IMPL(i) (i< PMU_MAX_PMDS && (pmu_conf->pmd_desc[i].type & PFM_REG_IMPL))
108 /* XXX: these assume that register i is implemented */
109 #define PMD_IS_COUNTING(i) ((pmu_conf->pmd_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
110 #define PMC_IS_COUNTING(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
111 #define PMC_IS_MONITOR(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_MONITOR) == PFM_REG_MONITOR)
112 #define PMC_IS_CONTROL(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_CONTROL) == PFM_REG_CONTROL)
114 #define PMC_DFL_VAL(i) pmu_conf->pmc_desc[i].default_value
115 #define PMC_RSVD_MASK(i) pmu_conf->pmc_desc[i].reserved_mask
116 #define PMD_PMD_DEP(i) pmu_conf->pmd_desc[i].dep_pmd[0]
117 #define PMC_PMD_DEP(i) pmu_conf->pmc_desc[i].dep_pmd[0]
119 #define PFM_NUM_IBRS IA64_NUM_DBG_REGS
120 #define PFM_NUM_DBRS IA64_NUM_DBG_REGS
122 #define CTX_OVFL_NOBLOCK(c) ((c)->ctx_fl_block == 0)
123 #define CTX_HAS_SMPL(c) ((c)->ctx_fl_is_sampling)
124 #define PFM_CTX_TASK(h) (h)->ctx_task
126 #define PMU_PMC_OI 5 /* position of pmc.oi bit */
128 /* XXX: does not support more than 64 PMDs */
129 #define CTX_USED_PMD(ctx, mask) (ctx)->ctx_used_pmds[0] |= (mask)
130 #define CTX_IS_USED_PMD(ctx, c) (((ctx)->ctx_used_pmds[0] & (1UL << (c))) != 0UL)
132 #define CTX_USED_MONITOR(ctx, mask) (ctx)->ctx_used_monitors[0] |= (mask)
134 #define CTX_USED_IBR(ctx,n) (ctx)->ctx_used_ibrs[(n)>>6] |= 1UL<< ((n) % 64)
135 #define CTX_USED_DBR(ctx,n) (ctx)->ctx_used_dbrs[(n)>>6] |= 1UL<< ((n) % 64)
136 #define CTX_USES_DBREGS(ctx) (((pfm_context_t *)(ctx))->ctx_fl_using_dbreg==1)
137 #define PFM_CODE_RR 0 /* requesting code range restriction */
138 #define PFM_DATA_RR 1 /* requestion data range restriction */
140 #define PFM_CPUINFO_CLEAR(v) pfm_get_cpu_var(pfm_syst_info) &= ~(v)
141 #define PFM_CPUINFO_SET(v) pfm_get_cpu_var(pfm_syst_info) |= (v)
142 #define PFM_CPUINFO_GET() pfm_get_cpu_var(pfm_syst_info)
144 #define RDEP(x) (1UL<<(x))
147 * context protection macros
149 * - we need to protect against CPU concurrency (spin_lock)
150 * - we need to protect against PMU overflow interrupts (local_irq_disable)
152 * - we need to protect against PMU overflow interrupts (local_irq_disable)
154 * spin_lock_irqsave()/spin_unlock_irqrestore():
155 * in SMP: local_irq_disable + spin_lock
156 * in UP : local_irq_disable
158 * spin_lock()/spin_lock():
159 * in UP : removed automatically
160 * in SMP: protect against context accesses from other CPU. interrupts
161 * are not masked. This is useful for the PMU interrupt handler
162 * because we know we will not get PMU concurrency in that code.
164 #define PROTECT_CTX(c, f) \
166 DPRINT(("spinlock_irq_save ctx %p by [%d]\n", c, task_pid_nr(current))); \
167 spin_lock_irqsave(&(c)->ctx_lock, f); \
168 DPRINT(("spinlocked ctx %p by [%d]\n", c, task_pid_nr(current))); \
171 #define UNPROTECT_CTX(c, f) \
173 DPRINT(("spinlock_irq_restore ctx %p by [%d]\n", c, task_pid_nr(current))); \
174 spin_unlock_irqrestore(&(c)->ctx_lock, f); \
177 #define PROTECT_CTX_NOPRINT(c, f) \
179 spin_lock_irqsave(&(c)->ctx_lock, f); \
183 #define UNPROTECT_CTX_NOPRINT(c, f) \
185 spin_unlock_irqrestore(&(c)->ctx_lock, f); \
189 #define PROTECT_CTX_NOIRQ(c) \
191 spin_lock(&(c)->ctx_lock); \
194 #define UNPROTECT_CTX_NOIRQ(c) \
196 spin_unlock(&(c)->ctx_lock); \
202 #define GET_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)
203 #define INC_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)++
204 #define SET_ACTIVATION(c) (c)->ctx_last_activation = GET_ACTIVATION()
206 #else /* !CONFIG_SMP */
207 #define SET_ACTIVATION(t) do {} while(0)
208 #define GET_ACTIVATION(t) do {} while(0)
209 #define INC_ACTIVATION(t) do {} while(0)
210 #endif /* CONFIG_SMP */
212 #define SET_PMU_OWNER(t, c) do { pfm_get_cpu_var(pmu_owner) = (t); pfm_get_cpu_var(pmu_ctx) = (c); } while(0)
213 #define GET_PMU_OWNER() pfm_get_cpu_var(pmu_owner)
214 #define GET_PMU_CTX() pfm_get_cpu_var(pmu_ctx)
216 #define LOCK_PFS(g) spin_lock_irqsave(&pfm_sessions.pfs_lock, g)
217 #define UNLOCK_PFS(g) spin_unlock_irqrestore(&pfm_sessions.pfs_lock, g)
219 #define PFM_REG_RETFLAG_SET(flags, val) do { flags &= ~PFM_REG_RETFL_MASK; flags |= (val); } while(0)
222 * cmp0 must be the value of pmc0
224 #define PMC0_HAS_OVFL(cmp0) (cmp0 & ~0x1UL)
226 #define PFMFS_MAGIC 0xa0b4d889
231 #define PFM_DEBUGGING 1
235 if (unlikely(pfm_sysctl.debug >0)) { printk("%s.%d: CPU%d [%d] ", __func__, __LINE__, smp_processor_id(), task_pid_nr(current)); printk a; } \
238 #define DPRINT_ovfl(a) \
240 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; } \
245 * 64-bit software counter structure
247 * the next_reset_type is applied to the next call to pfm_reset_regs()
250 unsigned long val; /* virtual 64bit counter value */
251 unsigned long lval; /* last reset value */
252 unsigned long long_reset; /* reset value on sampling overflow */
253 unsigned long short_reset; /* reset value on overflow */
254 unsigned long reset_pmds[4]; /* which other pmds to reset when this counter overflows */
255 unsigned long smpl_pmds[4]; /* which pmds are accessed when counter overflow */
256 unsigned long seed; /* seed for random-number generator */
257 unsigned long mask; /* mask for random-number generator */
258 unsigned int flags; /* notify/do not notify */
259 unsigned long eventid; /* overflow event identifier */
266 unsigned int block:1; /* when 1, task will blocked on user notifications */
267 unsigned int system:1; /* do system wide monitoring */
268 unsigned int using_dbreg:1; /* using range restrictions (debug registers) */
269 unsigned int is_sampling:1; /* true if using a custom format */
270 unsigned int excl_idle:1; /* exclude idle task in system wide session */
271 unsigned int going_zombie:1; /* context is zombie (MASKED+blocking) */
272 unsigned int trap_reason:2; /* reason for going into pfm_handle_work() */
273 unsigned int no_msg:1; /* no message sent on overflow */
274 unsigned int can_restart:1; /* allowed to issue a PFM_RESTART */
275 unsigned int reserved:22;
276 } pfm_context_flags_t;
278 #define PFM_TRAP_REASON_NONE 0x0 /* default value */
279 #define PFM_TRAP_REASON_BLOCK 0x1 /* we need to block on overflow */
280 #define PFM_TRAP_REASON_RESET 0x2 /* we need to reset PMDs */
284 * perfmon context: encapsulates all the state of a monitoring session
287 typedef struct pfm_context {
288 spinlock_t ctx_lock; /* context protection */
290 pfm_context_flags_t ctx_flags; /* bitmask of flags (block reason incl.) */
291 unsigned int ctx_state; /* state: active/inactive (no bitfield) */
293 struct task_struct *ctx_task; /* task to which context is attached */
295 unsigned long ctx_ovfl_regs[4]; /* which registers overflowed (notification) */
297 struct completion ctx_restart_done; /* use for blocking notification mode */
299 unsigned long ctx_used_pmds[4]; /* bitmask of PMD used */
300 unsigned long ctx_all_pmds[4]; /* bitmask of all accessible PMDs */
301 unsigned long ctx_reload_pmds[4]; /* bitmask of force reload PMD on ctxsw in */
303 unsigned long ctx_all_pmcs[4]; /* bitmask of all accessible PMCs */
304 unsigned long ctx_reload_pmcs[4]; /* bitmask of force reload PMC on ctxsw in */
305 unsigned long ctx_used_monitors[4]; /* bitmask of monitor PMC being used */
307 unsigned long ctx_pmcs[PFM_NUM_PMC_REGS]; /* saved copies of PMC values */
309 unsigned int ctx_used_ibrs[1]; /* bitmask of used IBR (speedup ctxsw in) */
310 unsigned int ctx_used_dbrs[1]; /* bitmask of used DBR (speedup ctxsw in) */
311 unsigned long ctx_dbrs[IA64_NUM_DBG_REGS]; /* DBR values (cache) when not loaded */
312 unsigned long ctx_ibrs[IA64_NUM_DBG_REGS]; /* IBR values (cache) when not loaded */
314 pfm_counter_t ctx_pmds[PFM_NUM_PMD_REGS]; /* software state for PMDS */
316 unsigned long th_pmcs[PFM_NUM_PMC_REGS]; /* PMC thread save state */
317 unsigned long th_pmds[PFM_NUM_PMD_REGS]; /* PMD thread save state */
319 unsigned long ctx_saved_psr_up; /* only contains psr.up value */
321 unsigned long ctx_last_activation; /* context last activation number for last_cpu */
322 unsigned int ctx_last_cpu; /* CPU id of current or last CPU used (SMP only) */
323 unsigned int ctx_cpu; /* cpu to which perfmon is applied (system wide) */
325 int ctx_fd; /* file descriptor used my this context */
326 pfm_ovfl_arg_t ctx_ovfl_arg; /* argument to custom buffer format handler */
328 pfm_buffer_fmt_t *ctx_buf_fmt; /* buffer format callbacks */
329 void *ctx_smpl_hdr; /* points to sampling buffer header kernel vaddr */
330 unsigned long ctx_smpl_size; /* size of sampling buffer */
331 void *ctx_smpl_vaddr; /* user level virtual address of smpl buffer */
333 wait_queue_head_t ctx_msgq_wait;
334 pfm_msg_t ctx_msgq[PFM_MAX_MSGS];
337 struct fasync_struct *ctx_async_queue;
339 wait_queue_head_t ctx_zombieq; /* termination cleanup wait queue */
343 * magic number used to verify that structure is really
346 #define PFM_IS_FILE(f) ((f)->f_op == &pfm_file_ops)
348 #define PFM_GET_CTX(t) ((pfm_context_t *)(t)->thread.pfm_context)
351 #define SET_LAST_CPU(ctx, v) (ctx)->ctx_last_cpu = (v)
352 #define GET_LAST_CPU(ctx) (ctx)->ctx_last_cpu
354 #define SET_LAST_CPU(ctx, v) do {} while(0)
355 #define GET_LAST_CPU(ctx) do {} while(0)
359 #define ctx_fl_block ctx_flags.block
360 #define ctx_fl_system ctx_flags.system
361 #define ctx_fl_using_dbreg ctx_flags.using_dbreg
362 #define ctx_fl_is_sampling ctx_flags.is_sampling
363 #define ctx_fl_excl_idle ctx_flags.excl_idle
364 #define ctx_fl_going_zombie ctx_flags.going_zombie
365 #define ctx_fl_trap_reason ctx_flags.trap_reason
366 #define ctx_fl_no_msg ctx_flags.no_msg
367 #define ctx_fl_can_restart ctx_flags.can_restart
369 #define PFM_SET_WORK_PENDING(t, v) do { (t)->thread.pfm_needs_checking = v; } while(0);
370 #define PFM_GET_WORK_PENDING(t) (t)->thread.pfm_needs_checking
373 * global information about all sessions
374 * mostly used to synchronize between system wide and per-process
377 spinlock_t pfs_lock; /* lock the structure */
379 unsigned int pfs_task_sessions; /* number of per task sessions */
380 unsigned int pfs_sys_sessions; /* number of per system wide sessions */
381 unsigned int pfs_sys_use_dbregs; /* incremented when a system wide session uses debug regs */
382 unsigned int pfs_ptrace_use_dbregs; /* incremented when a process uses debug regs */
383 struct task_struct *pfs_sys_session[NR_CPUS]; /* point to task owning a system-wide session */
387 * information about a PMC or PMD.
388 * dep_pmd[]: a bitmask of dependent PMD registers
389 * dep_pmc[]: a bitmask of dependent PMC registers
391 typedef int (*pfm_reg_check_t)(struct task_struct *task, pfm_context_t *ctx, unsigned int cnum, unsigned long *val, struct pt_regs *regs);
395 unsigned long default_value; /* power-on default value */
396 unsigned long reserved_mask; /* bitmask of reserved bits */
397 pfm_reg_check_t read_check;
398 pfm_reg_check_t write_check;
399 unsigned long dep_pmd[4];
400 unsigned long dep_pmc[4];
403 /* assume cnum is a valid monitor */
404 #define PMC_PM(cnum, val) (((val) >> (pmu_conf->pmc_desc[cnum].pm_pos)) & 0x1)
407 * This structure is initialized at boot time and contains
408 * a description of the PMU main characteristics.
410 * If the probe function is defined, detection is based
411 * on its return value:
412 * - 0 means recognized PMU
413 * - anything else means not supported
414 * When the probe function is not defined, then the pmu_family field
415 * is used and it must match the host CPU family such that:
416 * - cpu->family & config->pmu_family != 0
419 unsigned long ovfl_val; /* overflow value for counters */
421 pfm_reg_desc_t *pmc_desc; /* detailed PMC register dependencies descriptions */
422 pfm_reg_desc_t *pmd_desc; /* detailed PMD register dependencies descriptions */
424 unsigned int num_pmcs; /* number of PMCS: computed at init time */
425 unsigned int num_pmds; /* number of PMDS: computed at init time */
426 unsigned long impl_pmcs[4]; /* bitmask of implemented PMCS */
427 unsigned long impl_pmds[4]; /* bitmask of implemented PMDS */
429 char *pmu_name; /* PMU family name */
430 unsigned int pmu_family; /* cpuid family pattern used to identify pmu */
431 unsigned int flags; /* pmu specific flags */
432 unsigned int num_ibrs; /* number of IBRS: computed at init time */
433 unsigned int num_dbrs; /* number of DBRS: computed at init time */
434 unsigned int num_counters; /* PMC/PMD counting pairs : computed at init time */
435 int (*probe)(void); /* customized probe routine */
436 unsigned int use_rr_dbregs:1; /* set if debug registers used for range restriction */
441 #define PFM_PMU_IRQ_RESEND 1 /* PMU needs explicit IRQ resend */
444 * debug register related type definitions
447 unsigned long ibr_mask:56;
448 unsigned long ibr_plm:4;
449 unsigned long ibr_ig:3;
450 unsigned long ibr_x:1;
454 unsigned long dbr_mask:56;
455 unsigned long dbr_plm:4;
456 unsigned long dbr_ig:2;
457 unsigned long dbr_w:1;
458 unsigned long dbr_r:1;
469 * perfmon command descriptions
472 int (*cmd_func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
475 unsigned int cmd_narg;
477 int (*cmd_getsize)(void *arg, size_t *sz);
480 #define PFM_CMD_FD 0x01 /* command requires a file descriptor */
481 #define PFM_CMD_ARG_READ 0x02 /* command must read argument(s) */
482 #define PFM_CMD_ARG_RW 0x04 /* command must read/write argument(s) */
483 #define PFM_CMD_STOP 0x08 /* command does not work on zombie context */
486 #define PFM_CMD_NAME(cmd) pfm_cmd_tab[(cmd)].cmd_name
487 #define PFM_CMD_READ_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_READ)
488 #define PFM_CMD_RW_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_RW)
489 #define PFM_CMD_USE_FD(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_FD)
490 #define PFM_CMD_STOPPED(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_STOP)
492 #define PFM_CMD_ARG_MANY -1 /* cannot be zero */
495 unsigned long pfm_spurious_ovfl_intr_count; /* keep track of spurious ovfl interrupts */
496 unsigned long pfm_replay_ovfl_intr_count; /* keep track of replayed ovfl interrupts */
497 unsigned long pfm_ovfl_intr_count; /* keep track of ovfl interrupts */
498 unsigned long pfm_ovfl_intr_cycles; /* cycles spent processing ovfl interrupts */
499 unsigned long pfm_ovfl_intr_cycles_min; /* min cycles spent processing ovfl interrupts */
500 unsigned long pfm_ovfl_intr_cycles_max; /* max cycles spent processing ovfl interrupts */
501 unsigned long pfm_smpl_handler_calls;
502 unsigned long pfm_smpl_handler_cycles;
503 char pad[SMP_CACHE_BYTES] ____cacheline_aligned;
507 * perfmon internal variables
509 static pfm_stats_t pfm_stats[NR_CPUS];
510 static pfm_session_t pfm_sessions; /* global sessions information */
512 static DEFINE_SPINLOCK(pfm_alt_install_check);
513 static pfm_intr_handler_desc_t *pfm_alt_intr_handler;
515 static struct proc_dir_entry *perfmon_dir;
516 static pfm_uuid_t pfm_null_uuid = {0,};
518 static spinlock_t pfm_buffer_fmt_lock;
519 static LIST_HEAD(pfm_buffer_fmt_list);
521 static pmu_config_t *pmu_conf;
523 /* sysctl() controls */
524 pfm_sysctl_t pfm_sysctl;
525 EXPORT_SYMBOL(pfm_sysctl);
527 static struct ctl_table pfm_ctl_table[] = {
530 .data = &pfm_sysctl.debug,
531 .maxlen = sizeof(int),
533 .proc_handler = proc_dointvec,
536 .procname = "debug_ovfl",
537 .data = &pfm_sysctl.debug_ovfl,
538 .maxlen = sizeof(int),
540 .proc_handler = proc_dointvec,
543 .procname = "fastctxsw",
544 .data = &pfm_sysctl.fastctxsw,
545 .maxlen = sizeof(int),
547 .proc_handler = proc_dointvec,
550 .procname = "expert_mode",
551 .data = &pfm_sysctl.expert_mode,
552 .maxlen = sizeof(int),
554 .proc_handler = proc_dointvec,
558 static struct ctl_table pfm_sysctl_dir[] = {
560 .procname = "perfmon",
562 .child = pfm_ctl_table,
566 static struct ctl_table pfm_sysctl_root[] = {
568 .procname = "kernel",
570 .child = pfm_sysctl_dir,
574 static struct ctl_table_header *pfm_sysctl_header;
576 static int pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
578 #define pfm_get_cpu_var(v) __ia64_per_cpu_var(v)
579 #define pfm_get_cpu_data(a,b) per_cpu(a, b)
582 pfm_put_task(struct task_struct *task)
584 if (task != current) put_task_struct(task);
587 static inline unsigned long
588 pfm_protect_ctx_ctxsw(pfm_context_t *x)
590 spin_lock(&(x)->ctx_lock);
595 pfm_unprotect_ctx_ctxsw(pfm_context_t *x, unsigned long f)
597 spin_unlock(&(x)->ctx_lock);
600 /* forward declaration */
601 static const struct dentry_operations pfmfs_dentry_operations;
603 static struct dentry *
604 pfmfs_mount(struct file_system_type *fs_type, int flags, const char *dev_name, void *data)
606 return mount_pseudo(fs_type, "pfm:", NULL, &pfmfs_dentry_operations,
610 static struct file_system_type pfm_fs_type = {
612 .mount = pfmfs_mount,
613 .kill_sb = kill_anon_super,
615 MODULE_ALIAS_FS("pfmfs");
617 DEFINE_PER_CPU(unsigned long, pfm_syst_info);
618 DEFINE_PER_CPU(struct task_struct *, pmu_owner);
619 DEFINE_PER_CPU(pfm_context_t *, pmu_ctx);
620 DEFINE_PER_CPU(unsigned long, pmu_activation_number);
621 EXPORT_PER_CPU_SYMBOL_GPL(pfm_syst_info);
624 /* forward declaration */
625 static const struct file_operations pfm_file_ops;
628 * forward declarations
631 static void pfm_lazy_save_regs (struct task_struct *ta);
634 void dump_pmu_state(const char *);
635 static int pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
637 #include "perfmon_itanium.h"
638 #include "perfmon_mckinley.h"
639 #include "perfmon_montecito.h"
640 #include "perfmon_generic.h"
642 static pmu_config_t *pmu_confs[]={
646 &pmu_conf_gen, /* must be last */
651 static int pfm_end_notify_user(pfm_context_t *ctx);
654 pfm_clear_psr_pp(void)
656 ia64_rsm(IA64_PSR_PP);
663 ia64_ssm(IA64_PSR_PP);
668 pfm_clear_psr_up(void)
670 ia64_rsm(IA64_PSR_UP);
677 ia64_ssm(IA64_PSR_UP);
681 static inline unsigned long
685 tmp = ia64_getreg(_IA64_REG_PSR);
691 pfm_set_psr_l(unsigned long val)
693 ia64_setreg(_IA64_REG_PSR_L, val);
705 pfm_unfreeze_pmu(void)
712 pfm_restore_ibrs(unsigned long *ibrs, unsigned int nibrs)
716 for (i=0; i < nibrs; i++) {
717 ia64_set_ibr(i, ibrs[i]);
718 ia64_dv_serialize_instruction();
724 pfm_restore_dbrs(unsigned long *dbrs, unsigned int ndbrs)
728 for (i=0; i < ndbrs; i++) {
729 ia64_set_dbr(i, dbrs[i]);
730 ia64_dv_serialize_data();
736 * PMD[i] must be a counter. no check is made
738 static inline unsigned long
739 pfm_read_soft_counter(pfm_context_t *ctx, int i)
741 return ctx->ctx_pmds[i].val + (ia64_get_pmd(i) & pmu_conf->ovfl_val);
745 * PMD[i] must be a counter. no check is made
748 pfm_write_soft_counter(pfm_context_t *ctx, int i, unsigned long val)
750 unsigned long ovfl_val = pmu_conf->ovfl_val;
752 ctx->ctx_pmds[i].val = val & ~ovfl_val;
754 * writing to unimplemented part is ignore, so we do not need to
757 ia64_set_pmd(i, val & ovfl_val);
761 pfm_get_new_msg(pfm_context_t *ctx)
765 next = (ctx->ctx_msgq_tail+1) % PFM_MAX_MSGS;
767 DPRINT(("ctx_fd=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
768 if (next == ctx->ctx_msgq_head) return NULL;
770 idx = ctx->ctx_msgq_tail;
771 ctx->ctx_msgq_tail = next;
773 DPRINT(("ctx=%p head=%d tail=%d msg=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail, idx));
775 return ctx->ctx_msgq+idx;
779 pfm_get_next_msg(pfm_context_t *ctx)
783 DPRINT(("ctx=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
785 if (PFM_CTXQ_EMPTY(ctx)) return NULL;
790 msg = ctx->ctx_msgq+ctx->ctx_msgq_head;
795 ctx->ctx_msgq_head = (ctx->ctx_msgq_head+1) % PFM_MAX_MSGS;
797 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));
803 pfm_reset_msgq(pfm_context_t *ctx)
805 ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
806 DPRINT(("ctx=%p msgq reset\n", ctx));
809 static pfm_context_t *
810 pfm_context_alloc(int ctx_flags)
815 * allocate context descriptor
816 * must be able to free with interrupts disabled
818 ctx = kzalloc(sizeof(pfm_context_t), GFP_KERNEL);
820 DPRINT(("alloc ctx @%p\n", ctx));
823 * init context protection lock
825 spin_lock_init(&ctx->ctx_lock);
828 * context is unloaded
830 ctx->ctx_state = PFM_CTX_UNLOADED;
833 * initialization of context's flags
835 ctx->ctx_fl_block = (ctx_flags & PFM_FL_NOTIFY_BLOCK) ? 1 : 0;
836 ctx->ctx_fl_system = (ctx_flags & PFM_FL_SYSTEM_WIDE) ? 1: 0;
837 ctx->ctx_fl_no_msg = (ctx_flags & PFM_FL_OVFL_NO_MSG) ? 1: 0;
839 * will move to set properties
840 * ctx->ctx_fl_excl_idle = (ctx_flags & PFM_FL_EXCL_IDLE) ? 1: 0;
844 * init restart semaphore to locked
846 init_completion(&ctx->ctx_restart_done);
849 * activation is used in SMP only
851 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
852 SET_LAST_CPU(ctx, -1);
855 * initialize notification message queue
857 ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
858 init_waitqueue_head(&ctx->ctx_msgq_wait);
859 init_waitqueue_head(&ctx->ctx_zombieq);
866 pfm_context_free(pfm_context_t *ctx)
869 DPRINT(("free ctx @%p\n", ctx));
875 pfm_mask_monitoring(struct task_struct *task)
877 pfm_context_t *ctx = PFM_GET_CTX(task);
878 unsigned long mask, val, ovfl_mask;
881 DPRINT_ovfl(("masking monitoring for [%d]\n", task_pid_nr(task)));
883 ovfl_mask = pmu_conf->ovfl_val;
885 * monitoring can only be masked as a result of a valid
886 * counter overflow. In UP, it means that the PMU still
887 * has an owner. Note that the owner can be different
888 * from the current task. However the PMU state belongs
890 * In SMP, a valid overflow only happens when task is
891 * current. Therefore if we come here, we know that
892 * the PMU state belongs to the current task, therefore
893 * we can access the live registers.
895 * So in both cases, the live register contains the owner's
896 * state. We can ONLY touch the PMU registers and NOT the PSR.
898 * As a consequence to this call, the ctx->th_pmds[] array
899 * contains stale information which must be ignored
900 * when context is reloaded AND monitoring is active (see
903 mask = ctx->ctx_used_pmds[0];
904 for (i = 0; mask; i++, mask>>=1) {
905 /* skip non used pmds */
906 if ((mask & 0x1) == 0) continue;
907 val = ia64_get_pmd(i);
909 if (PMD_IS_COUNTING(i)) {
911 * we rebuild the full 64 bit value of the counter
913 ctx->ctx_pmds[i].val += (val & ovfl_mask);
915 ctx->ctx_pmds[i].val = val;
917 DPRINT_ovfl(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
919 ctx->ctx_pmds[i].val,
923 * mask monitoring by setting the privilege level to 0
924 * we cannot use psr.pp/psr.up for this, it is controlled by
927 * if task is current, modify actual registers, otherwise modify
928 * thread save state, i.e., what will be restored in pfm_load_regs()
930 mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
931 for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
932 if ((mask & 0x1) == 0UL) continue;
933 ia64_set_pmc(i, ctx->th_pmcs[i] & ~0xfUL);
934 ctx->th_pmcs[i] &= ~0xfUL;
935 DPRINT_ovfl(("pmc[%d]=0x%lx\n", i, ctx->th_pmcs[i]));
938 * make all of this visible
944 * must always be done with task == current
946 * context must be in MASKED state when calling
949 pfm_restore_monitoring(struct task_struct *task)
951 pfm_context_t *ctx = PFM_GET_CTX(task);
952 unsigned long mask, ovfl_mask;
953 unsigned long psr, val;
956 is_system = ctx->ctx_fl_system;
957 ovfl_mask = pmu_conf->ovfl_val;
959 if (task != current) {
960 printk(KERN_ERR "perfmon.%d: invalid task[%d] current[%d]\n", __LINE__, task_pid_nr(task), task_pid_nr(current));
963 if (ctx->ctx_state != PFM_CTX_MASKED) {
964 printk(KERN_ERR "perfmon.%d: task[%d] current[%d] invalid state=%d\n", __LINE__,
965 task_pid_nr(task), task_pid_nr(current), ctx->ctx_state);
970 * monitoring is masked via the PMC.
971 * As we restore their value, we do not want each counter to
972 * restart right away. We stop monitoring using the PSR,
973 * restore the PMC (and PMD) and then re-establish the psr
974 * as it was. Note that there can be no pending overflow at
975 * this point, because monitoring was MASKED.
977 * system-wide session are pinned and self-monitoring
979 if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
981 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
987 * first, we restore the PMD
989 mask = ctx->ctx_used_pmds[0];
990 for (i = 0; mask; i++, mask>>=1) {
991 /* skip non used pmds */
992 if ((mask & 0x1) == 0) continue;
994 if (PMD_IS_COUNTING(i)) {
996 * we split the 64bit value according to
999 val = ctx->ctx_pmds[i].val & ovfl_mask;
1000 ctx->ctx_pmds[i].val &= ~ovfl_mask;
1002 val = ctx->ctx_pmds[i].val;
1004 ia64_set_pmd(i, val);
1006 DPRINT(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
1008 ctx->ctx_pmds[i].val,
1014 mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
1015 for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
1016 if ((mask & 0x1) == 0UL) continue;
1017 ctx->th_pmcs[i] = ctx->ctx_pmcs[i];
1018 ia64_set_pmc(i, ctx->th_pmcs[i]);
1019 DPRINT(("[%d] pmc[%d]=0x%lx\n",
1020 task_pid_nr(task), i, ctx->th_pmcs[i]));
1025 * must restore DBR/IBR because could be modified while masked
1026 * XXX: need to optimize
1028 if (ctx->ctx_fl_using_dbreg) {
1029 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
1030 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
1036 if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
1038 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
1045 pfm_save_pmds(unsigned long *pmds, unsigned long mask)
1051 for (i=0; mask; i++, mask>>=1) {
1052 if (mask & 0x1) pmds[i] = ia64_get_pmd(i);
1057 * reload from thread state (used for ctxw only)
1060 pfm_restore_pmds(unsigned long *pmds, unsigned long mask)
1063 unsigned long val, ovfl_val = pmu_conf->ovfl_val;
1065 for (i=0; mask; i++, mask>>=1) {
1066 if ((mask & 0x1) == 0) continue;
1067 val = PMD_IS_COUNTING(i) ? pmds[i] & ovfl_val : pmds[i];
1068 ia64_set_pmd(i, val);
1074 * propagate PMD from context to thread-state
1077 pfm_copy_pmds(struct task_struct *task, pfm_context_t *ctx)
1079 unsigned long ovfl_val = pmu_conf->ovfl_val;
1080 unsigned long mask = ctx->ctx_all_pmds[0];
1084 DPRINT(("mask=0x%lx\n", mask));
1086 for (i=0; mask; i++, mask>>=1) {
1088 val = ctx->ctx_pmds[i].val;
1091 * We break up the 64 bit value into 2 pieces
1092 * the lower bits go to the machine state in the
1093 * thread (will be reloaded on ctxsw in).
1094 * The upper part stays in the soft-counter.
1096 if (PMD_IS_COUNTING(i)) {
1097 ctx->ctx_pmds[i].val = val & ~ovfl_val;
1100 ctx->th_pmds[i] = val;
1102 DPRINT(("pmd[%d]=0x%lx soft_val=0x%lx\n",
1105 ctx->ctx_pmds[i].val));
1110 * propagate PMC from context to thread-state
1113 pfm_copy_pmcs(struct task_struct *task, pfm_context_t *ctx)
1115 unsigned long mask = ctx->ctx_all_pmcs[0];
1118 DPRINT(("mask=0x%lx\n", mask));
1120 for (i=0; mask; i++, mask>>=1) {
1121 /* masking 0 with ovfl_val yields 0 */
1122 ctx->th_pmcs[i] = ctx->ctx_pmcs[i];
1123 DPRINT(("pmc[%d]=0x%lx\n", i, ctx->th_pmcs[i]));
1130 pfm_restore_pmcs(unsigned long *pmcs, unsigned long mask)
1134 for (i=0; mask; i++, mask>>=1) {
1135 if ((mask & 0x1) == 0) continue;
1136 ia64_set_pmc(i, pmcs[i]);
1142 pfm_uuid_cmp(pfm_uuid_t a, pfm_uuid_t b)
1144 return memcmp(a, b, sizeof(pfm_uuid_t));
1148 pfm_buf_fmt_exit(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, struct pt_regs *regs)
1151 if (fmt->fmt_exit) ret = (*fmt->fmt_exit)(task, buf, regs);
1156 pfm_buf_fmt_getsize(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags, int cpu, void *arg, unsigned long *size)
1159 if (fmt->fmt_getsize) ret = (*fmt->fmt_getsize)(task, flags, cpu, arg, size);
1165 pfm_buf_fmt_validate(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags,
1169 if (fmt->fmt_validate) ret = (*fmt->fmt_validate)(task, flags, cpu, arg);
1174 pfm_buf_fmt_init(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, unsigned int flags,
1178 if (fmt->fmt_init) ret = (*fmt->fmt_init)(task, buf, flags, cpu, arg);
1183 pfm_buf_fmt_restart(pfm_buffer_fmt_t *fmt, struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
1186 if (fmt->fmt_restart) ret = (*fmt->fmt_restart)(task, ctrl, buf, regs);
1191 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)
1194 if (fmt->fmt_restart_active) ret = (*fmt->fmt_restart_active)(task, ctrl, buf, regs);
1198 static pfm_buffer_fmt_t *
1199 __pfm_find_buffer_fmt(pfm_uuid_t uuid)
1201 struct list_head * pos;
1202 pfm_buffer_fmt_t * entry;
1204 list_for_each(pos, &pfm_buffer_fmt_list) {
1205 entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
1206 if (pfm_uuid_cmp(uuid, entry->fmt_uuid) == 0)
1213 * find a buffer format based on its uuid
1215 static pfm_buffer_fmt_t *
1216 pfm_find_buffer_fmt(pfm_uuid_t uuid)
1218 pfm_buffer_fmt_t * fmt;
1219 spin_lock(&pfm_buffer_fmt_lock);
1220 fmt = __pfm_find_buffer_fmt(uuid);
1221 spin_unlock(&pfm_buffer_fmt_lock);
1226 pfm_register_buffer_fmt(pfm_buffer_fmt_t *fmt)
1230 /* some sanity checks */
1231 if (fmt == NULL || fmt->fmt_name == NULL) return -EINVAL;
1233 /* we need at least a handler */
1234 if (fmt->fmt_handler == NULL) return -EINVAL;
1237 * XXX: need check validity of fmt_arg_size
1240 spin_lock(&pfm_buffer_fmt_lock);
1242 if (__pfm_find_buffer_fmt(fmt->fmt_uuid)) {
1243 printk(KERN_ERR "perfmon: duplicate sampling format: %s\n", fmt->fmt_name);
1247 list_add(&fmt->fmt_list, &pfm_buffer_fmt_list);
1248 printk(KERN_INFO "perfmon: added sampling format %s\n", fmt->fmt_name);
1251 spin_unlock(&pfm_buffer_fmt_lock);
1254 EXPORT_SYMBOL(pfm_register_buffer_fmt);
1257 pfm_unregister_buffer_fmt(pfm_uuid_t uuid)
1259 pfm_buffer_fmt_t *fmt;
1262 spin_lock(&pfm_buffer_fmt_lock);
1264 fmt = __pfm_find_buffer_fmt(uuid);
1266 printk(KERN_ERR "perfmon: cannot unregister format, not found\n");
1270 list_del_init(&fmt->fmt_list);
1271 printk(KERN_INFO "perfmon: removed sampling format: %s\n", fmt->fmt_name);
1274 spin_unlock(&pfm_buffer_fmt_lock);
1278 EXPORT_SYMBOL(pfm_unregister_buffer_fmt);
1281 pfm_reserve_session(struct task_struct *task, int is_syswide, unsigned int cpu)
1283 unsigned long flags;
1285 * validity checks on cpu_mask have been done upstream
1289 DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1290 pfm_sessions.pfs_sys_sessions,
1291 pfm_sessions.pfs_task_sessions,
1292 pfm_sessions.pfs_sys_use_dbregs,
1298 * cannot mix system wide and per-task sessions
1300 if (pfm_sessions.pfs_task_sessions > 0UL) {
1301 DPRINT(("system wide not possible, %u conflicting task_sessions\n",
1302 pfm_sessions.pfs_task_sessions));
1306 if (pfm_sessions.pfs_sys_session[cpu]) goto error_conflict;
1308 DPRINT(("reserving system wide session on CPU%u currently on CPU%u\n", cpu, smp_processor_id()));
1310 pfm_sessions.pfs_sys_session[cpu] = task;
1312 pfm_sessions.pfs_sys_sessions++ ;
1315 if (pfm_sessions.pfs_sys_sessions) goto abort;
1316 pfm_sessions.pfs_task_sessions++;
1319 DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1320 pfm_sessions.pfs_sys_sessions,
1321 pfm_sessions.pfs_task_sessions,
1322 pfm_sessions.pfs_sys_use_dbregs,
1327 * Force idle() into poll mode
1329 cpu_idle_poll_ctrl(true);
1336 DPRINT(("system wide not possible, conflicting session [%d] on CPU%d\n",
1337 task_pid_nr(pfm_sessions.pfs_sys_session[cpu]),
1347 pfm_unreserve_session(pfm_context_t *ctx, int is_syswide, unsigned int cpu)
1349 unsigned long flags;
1351 * validity checks on cpu_mask have been done upstream
1355 DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1356 pfm_sessions.pfs_sys_sessions,
1357 pfm_sessions.pfs_task_sessions,
1358 pfm_sessions.pfs_sys_use_dbregs,
1364 pfm_sessions.pfs_sys_session[cpu] = NULL;
1366 * would not work with perfmon+more than one bit in cpu_mask
1368 if (ctx && ctx->ctx_fl_using_dbreg) {
1369 if (pfm_sessions.pfs_sys_use_dbregs == 0) {
1370 printk(KERN_ERR "perfmon: invalid release for ctx %p sys_use_dbregs=0\n", ctx);
1372 pfm_sessions.pfs_sys_use_dbregs--;
1375 pfm_sessions.pfs_sys_sessions--;
1377 pfm_sessions.pfs_task_sessions--;
1379 DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1380 pfm_sessions.pfs_sys_sessions,
1381 pfm_sessions.pfs_task_sessions,
1382 pfm_sessions.pfs_sys_use_dbregs,
1386 /* Undo forced polling. Last session reenables pal_halt */
1387 cpu_idle_poll_ctrl(false);
1395 * removes virtual mapping of the sampling buffer.
1396 * IMPORTANT: cannot be called with interrupts disable, e.g. inside
1397 * a PROTECT_CTX() section.
1400 pfm_remove_smpl_mapping(void *vaddr, unsigned long size)
1402 struct task_struct *task = current;
1406 if (task->mm == NULL || size == 0UL || vaddr == NULL) {
1407 printk(KERN_ERR "perfmon: pfm_remove_smpl_mapping [%d] invalid context mm=%p\n", task_pid_nr(task), task->mm);
1411 DPRINT(("smpl_vaddr=%p size=%lu\n", vaddr, size));
1414 * does the actual unmapping
1416 r = vm_munmap((unsigned long)vaddr, size);
1419 printk(KERN_ERR "perfmon: [%d] unable to unmap sampling buffer @%p size=%lu\n", task_pid_nr(task), vaddr, size);
1422 DPRINT(("do_unmap(%p, %lu)=%d\n", vaddr, size, r));
1428 * free actual physical storage used by sampling buffer
1432 pfm_free_smpl_buffer(pfm_context_t *ctx)
1434 pfm_buffer_fmt_t *fmt;
1436 if (ctx->ctx_smpl_hdr == NULL) goto invalid_free;
1439 * we won't use the buffer format anymore
1441 fmt = ctx->ctx_buf_fmt;
1443 DPRINT(("sampling buffer @%p size %lu vaddr=%p\n",
1446 ctx->ctx_smpl_vaddr));
1448 pfm_buf_fmt_exit(fmt, current, NULL, NULL);
1453 vfree(ctx->ctx_smpl_hdr);
1455 ctx->ctx_smpl_hdr = NULL;
1456 ctx->ctx_smpl_size = 0UL;
1461 printk(KERN_ERR "perfmon: pfm_free_smpl_buffer [%d] no buffer\n", task_pid_nr(current));
1467 pfm_exit_smpl_buffer(pfm_buffer_fmt_t *fmt)
1469 if (fmt == NULL) return;
1471 pfm_buf_fmt_exit(fmt, current, NULL, NULL);
1476 * pfmfs should _never_ be mounted by userland - too much of security hassle,
1477 * no real gain from having the whole whorehouse mounted. So we don't need
1478 * any operations on the root directory. However, we need a non-trivial
1479 * d_name - pfm: will go nicely and kill the special-casing in procfs.
1481 static struct vfsmount *pfmfs_mnt __read_mostly;
1486 int err = register_filesystem(&pfm_fs_type);
1488 pfmfs_mnt = kern_mount(&pfm_fs_type);
1489 err = PTR_ERR(pfmfs_mnt);
1490 if (IS_ERR(pfmfs_mnt))
1491 unregister_filesystem(&pfm_fs_type);
1499 pfm_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
1504 unsigned long flags;
1505 DECLARE_WAITQUEUE(wait, current);
1506 if (PFM_IS_FILE(filp) == 0) {
1507 printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", task_pid_nr(current));
1511 ctx = filp->private_data;
1513 printk(KERN_ERR "perfmon: pfm_read: NULL ctx [%d]\n", task_pid_nr(current));
1518 * check even when there is no message
1520 if (size < sizeof(pfm_msg_t)) {
1521 DPRINT(("message is too small ctx=%p (>=%ld)\n", ctx, sizeof(pfm_msg_t)));
1525 PROTECT_CTX(ctx, flags);
1528 * put ourselves on the wait queue
1530 add_wait_queue(&ctx->ctx_msgq_wait, &wait);
1538 set_current_state(TASK_INTERRUPTIBLE);
1540 DPRINT(("head=%d tail=%d\n", ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
1543 if(PFM_CTXQ_EMPTY(ctx) == 0) break;
1545 UNPROTECT_CTX(ctx, flags);
1548 * check non-blocking read
1551 if(filp->f_flags & O_NONBLOCK) break;
1554 * check pending signals
1556 if(signal_pending(current)) {
1561 * no message, so wait
1565 PROTECT_CTX(ctx, flags);
1567 DPRINT(("[%d] back to running ret=%ld\n", task_pid_nr(current), ret));
1568 set_current_state(TASK_RUNNING);
1569 remove_wait_queue(&ctx->ctx_msgq_wait, &wait);
1571 if (ret < 0) goto abort;
1574 msg = pfm_get_next_msg(ctx);
1576 printk(KERN_ERR "perfmon: pfm_read no msg for ctx=%p [%d]\n", ctx, task_pid_nr(current));
1580 DPRINT(("fd=%d type=%d\n", msg->pfm_gen_msg.msg_ctx_fd, msg->pfm_gen_msg.msg_type));
1583 if(copy_to_user(buf, msg, sizeof(pfm_msg_t)) == 0) ret = sizeof(pfm_msg_t);
1586 UNPROTECT_CTX(ctx, flags);
1592 pfm_write(struct file *file, const char __user *ubuf,
1593 size_t size, loff_t *ppos)
1595 DPRINT(("pfm_write called\n"));
1600 pfm_poll(struct file *filp, poll_table * wait)
1603 unsigned long flags;
1606 if (PFM_IS_FILE(filp) == 0) {
1607 printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", task_pid_nr(current));
1611 ctx = filp->private_data;
1613 printk(KERN_ERR "perfmon: pfm_poll: NULL ctx [%d]\n", task_pid_nr(current));
1618 DPRINT(("pfm_poll ctx_fd=%d before poll_wait\n", ctx->ctx_fd));
1620 poll_wait(filp, &ctx->ctx_msgq_wait, wait);
1622 PROTECT_CTX(ctx, flags);
1624 if (PFM_CTXQ_EMPTY(ctx) == 0)
1625 mask = EPOLLIN | EPOLLRDNORM;
1627 UNPROTECT_CTX(ctx, flags);
1629 DPRINT(("pfm_poll ctx_fd=%d mask=0x%x\n", ctx->ctx_fd, mask));
1635 pfm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1637 DPRINT(("pfm_ioctl called\n"));
1642 * interrupt cannot be masked when coming here
1645 pfm_do_fasync(int fd, struct file *filp, pfm_context_t *ctx, int on)
1649 ret = fasync_helper (fd, filp, on, &ctx->ctx_async_queue);
1651 DPRINT(("pfm_fasync called by [%d] on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
1652 task_pid_nr(current),
1655 ctx->ctx_async_queue, ret));
1661 pfm_fasync(int fd, struct file *filp, int on)
1666 if (PFM_IS_FILE(filp) == 0) {
1667 printk(KERN_ERR "perfmon: pfm_fasync bad magic [%d]\n", task_pid_nr(current));
1671 ctx = filp->private_data;
1673 printk(KERN_ERR "perfmon: pfm_fasync NULL ctx [%d]\n", task_pid_nr(current));
1677 * we cannot mask interrupts during this call because this may
1678 * may go to sleep if memory is not readily avalaible.
1680 * We are protected from the conetxt disappearing by the get_fd()/put_fd()
1681 * done in caller. Serialization of this function is ensured by caller.
1683 ret = pfm_do_fasync(fd, filp, ctx, on);
1686 DPRINT(("pfm_fasync called on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
1689 ctx->ctx_async_queue, ret));
1696 * this function is exclusively called from pfm_close().
1697 * The context is not protected at that time, nor are interrupts
1698 * on the remote CPU. That's necessary to avoid deadlocks.
1701 pfm_syswide_force_stop(void *info)
1703 pfm_context_t *ctx = (pfm_context_t *)info;
1704 struct pt_regs *regs = task_pt_regs(current);
1705 struct task_struct *owner;
1706 unsigned long flags;
1709 if (ctx->ctx_cpu != smp_processor_id()) {
1710 printk(KERN_ERR "perfmon: pfm_syswide_force_stop for CPU%d but on CPU%d\n",
1712 smp_processor_id());
1715 owner = GET_PMU_OWNER();
1716 if (owner != ctx->ctx_task) {
1717 printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected owner [%d] instead of [%d]\n",
1719 task_pid_nr(owner), task_pid_nr(ctx->ctx_task));
1722 if (GET_PMU_CTX() != ctx) {
1723 printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected ctx %p instead of %p\n",
1725 GET_PMU_CTX(), ctx);
1729 DPRINT(("on CPU%d forcing system wide stop for [%d]\n", smp_processor_id(), task_pid_nr(ctx->ctx_task)));
1731 * the context is already protected in pfm_close(), we simply
1732 * need to mask interrupts to avoid a PMU interrupt race on
1735 local_irq_save(flags);
1737 ret = pfm_context_unload(ctx, NULL, 0, regs);
1739 DPRINT(("context_unload returned %d\n", ret));
1743 * unmask interrupts, PMU interrupts are now spurious here
1745 local_irq_restore(flags);
1749 pfm_syswide_cleanup_other_cpu(pfm_context_t *ctx)
1753 DPRINT(("calling CPU%d for cleanup\n", ctx->ctx_cpu));
1754 ret = smp_call_function_single(ctx->ctx_cpu, pfm_syswide_force_stop, ctx, 1);
1755 DPRINT(("called CPU%d for cleanup ret=%d\n", ctx->ctx_cpu, ret));
1757 #endif /* CONFIG_SMP */
1760 * called for each close(). Partially free resources.
1761 * When caller is self-monitoring, the context is unloaded.
1764 pfm_flush(struct file *filp, fl_owner_t id)
1767 struct task_struct *task;
1768 struct pt_regs *regs;
1769 unsigned long flags;
1770 unsigned long smpl_buf_size = 0UL;
1771 void *smpl_buf_vaddr = NULL;
1772 int state, is_system;
1774 if (PFM_IS_FILE(filp) == 0) {
1775 DPRINT(("bad magic for\n"));
1779 ctx = filp->private_data;
1781 printk(KERN_ERR "perfmon: pfm_flush: NULL ctx [%d]\n", task_pid_nr(current));
1786 * remove our file from the async queue, if we use this mode.
1787 * This can be done without the context being protected. We come
1788 * here when the context has become unreachable by other tasks.
1790 * We may still have active monitoring at this point and we may
1791 * end up in pfm_overflow_handler(). However, fasync_helper()
1792 * operates with interrupts disabled and it cleans up the
1793 * queue. If the PMU handler is called prior to entering
1794 * fasync_helper() then it will send a signal. If it is
1795 * invoked after, it will find an empty queue and no
1796 * signal will be sent. In both case, we are safe
1798 PROTECT_CTX(ctx, flags);
1800 state = ctx->ctx_state;
1801 is_system = ctx->ctx_fl_system;
1803 task = PFM_CTX_TASK(ctx);
1804 regs = task_pt_regs(task);
1806 DPRINT(("ctx_state=%d is_current=%d\n",
1808 task == current ? 1 : 0));
1811 * if state == UNLOADED, then task is NULL
1815 * we must stop and unload because we are losing access to the context.
1817 if (task == current) {
1820 * the task IS the owner but it migrated to another CPU: that's bad
1821 * but we must handle this cleanly. Unfortunately, the kernel does
1822 * not provide a mechanism to block migration (while the context is loaded).
1824 * We need to release the resource on the ORIGINAL cpu.
1826 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
1828 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
1830 * keep context protected but unmask interrupt for IPI
1832 local_irq_restore(flags);
1834 pfm_syswide_cleanup_other_cpu(ctx);
1837 * restore interrupt masking
1839 local_irq_save(flags);
1842 * context is unloaded at this point
1845 #endif /* CONFIG_SMP */
1848 DPRINT(("forcing unload\n"));
1850 * stop and unload, returning with state UNLOADED
1851 * and session unreserved.
1853 pfm_context_unload(ctx, NULL, 0, regs);
1855 DPRINT(("ctx_state=%d\n", ctx->ctx_state));
1860 * remove virtual mapping, if any, for the calling task.
1861 * cannot reset ctx field until last user is calling close().
1863 * ctx_smpl_vaddr must never be cleared because it is needed
1864 * by every task with access to the context
1866 * When called from do_exit(), the mm context is gone already, therefore
1867 * mm is NULL, i.e., the VMA is already gone and we do not have to
1870 if (ctx->ctx_smpl_vaddr && current->mm) {
1871 smpl_buf_vaddr = ctx->ctx_smpl_vaddr;
1872 smpl_buf_size = ctx->ctx_smpl_size;
1875 UNPROTECT_CTX(ctx, flags);
1878 * if there was a mapping, then we systematically remove it
1879 * at this point. Cannot be done inside critical section
1880 * because some VM function reenables interrupts.
1883 if (smpl_buf_vaddr) pfm_remove_smpl_mapping(smpl_buf_vaddr, smpl_buf_size);
1888 * called either on explicit close() or from exit_files().
1889 * Only the LAST user of the file gets to this point, i.e., it is
1892 * IMPORTANT: we get called ONLY when the refcnt on the file gets to zero
1893 * (fput()),i.e, last task to access the file. Nobody else can access the
1894 * file at this point.
1896 * When called from exit_files(), the VMA has been freed because exit_mm()
1897 * is executed before exit_files().
1899 * When called from exit_files(), the current task is not yet ZOMBIE but we
1900 * flush the PMU state to the context.
1903 pfm_close(struct inode *inode, struct file *filp)
1906 struct task_struct *task;
1907 struct pt_regs *regs;
1908 DECLARE_WAITQUEUE(wait, current);
1909 unsigned long flags;
1910 unsigned long smpl_buf_size = 0UL;
1911 void *smpl_buf_addr = NULL;
1912 int free_possible = 1;
1913 int state, is_system;
1915 DPRINT(("pfm_close called private=%p\n", filp->private_data));
1917 if (PFM_IS_FILE(filp) == 0) {
1918 DPRINT(("bad magic\n"));
1922 ctx = filp->private_data;
1924 printk(KERN_ERR "perfmon: pfm_close: NULL ctx [%d]\n", task_pid_nr(current));
1928 PROTECT_CTX(ctx, flags);
1930 state = ctx->ctx_state;
1931 is_system = ctx->ctx_fl_system;
1933 task = PFM_CTX_TASK(ctx);
1934 regs = task_pt_regs(task);
1936 DPRINT(("ctx_state=%d is_current=%d\n",
1938 task == current ? 1 : 0));
1941 * if task == current, then pfm_flush() unloaded the context
1943 if (state == PFM_CTX_UNLOADED) goto doit;
1946 * context is loaded/masked and task != current, we need to
1947 * either force an unload or go zombie
1951 * The task is currently blocked or will block after an overflow.
1952 * we must force it to wakeup to get out of the
1953 * MASKED state and transition to the unloaded state by itself.
1955 * This situation is only possible for per-task mode
1957 if (state == PFM_CTX_MASKED && CTX_OVFL_NOBLOCK(ctx) == 0) {
1960 * set a "partial" zombie state to be checked
1961 * upon return from down() in pfm_handle_work().
1963 * We cannot use the ZOMBIE state, because it is checked
1964 * by pfm_load_regs() which is called upon wakeup from down().
1965 * In such case, it would free the context and then we would
1966 * return to pfm_handle_work() which would access the
1967 * stale context. Instead, we set a flag invisible to pfm_load_regs()
1968 * but visible to pfm_handle_work().
1970 * For some window of time, we have a zombie context with
1971 * ctx_state = MASKED and not ZOMBIE
1973 ctx->ctx_fl_going_zombie = 1;
1976 * force task to wake up from MASKED state
1978 complete(&ctx->ctx_restart_done);
1980 DPRINT(("waking up ctx_state=%d\n", state));
1983 * put ourself to sleep waiting for the other
1984 * task to report completion
1986 * the context is protected by mutex, therefore there
1987 * is no risk of being notified of completion before
1988 * begin actually on the waitq.
1990 set_current_state(TASK_INTERRUPTIBLE);
1991 add_wait_queue(&ctx->ctx_zombieq, &wait);
1993 UNPROTECT_CTX(ctx, flags);
1996 * XXX: check for signals :
1997 * - ok for explicit close
1998 * - not ok when coming from exit_files()
2003 PROTECT_CTX(ctx, flags);
2006 remove_wait_queue(&ctx->ctx_zombieq, &wait);
2007 set_current_state(TASK_RUNNING);
2010 * context is unloaded at this point
2012 DPRINT(("after zombie wakeup ctx_state=%d for\n", state));
2014 else if (task != current) {
2017 * switch context to zombie state
2019 ctx->ctx_state = PFM_CTX_ZOMBIE;
2021 DPRINT(("zombie ctx for [%d]\n", task_pid_nr(task)));
2023 * cannot free the context on the spot. deferred until
2024 * the task notices the ZOMBIE state
2028 pfm_context_unload(ctx, NULL, 0, regs);
2033 /* reload state, may have changed during opening of critical section */
2034 state = ctx->ctx_state;
2037 * the context is still attached to a task (possibly current)
2038 * we cannot destroy it right now
2042 * we must free the sampling buffer right here because
2043 * we cannot rely on it being cleaned up later by the
2044 * monitored task. It is not possible to free vmalloc'ed
2045 * memory in pfm_load_regs(). Instead, we remove the buffer
2046 * now. should there be subsequent PMU overflow originally
2047 * meant for sampling, the will be converted to spurious
2048 * and that's fine because the monitoring tools is gone anyway.
2050 if (ctx->ctx_smpl_hdr) {
2051 smpl_buf_addr = ctx->ctx_smpl_hdr;
2052 smpl_buf_size = ctx->ctx_smpl_size;
2053 /* no more sampling */
2054 ctx->ctx_smpl_hdr = NULL;
2055 ctx->ctx_fl_is_sampling = 0;
2058 DPRINT(("ctx_state=%d free_possible=%d addr=%p size=%lu\n",
2064 if (smpl_buf_addr) pfm_exit_smpl_buffer(ctx->ctx_buf_fmt);
2067 * UNLOADED that the session has already been unreserved.
2069 if (state == PFM_CTX_ZOMBIE) {
2070 pfm_unreserve_session(ctx, ctx->ctx_fl_system , ctx->ctx_cpu);
2074 * disconnect file descriptor from context must be done
2077 filp->private_data = NULL;
2080 * if we free on the spot, the context is now completely unreachable
2081 * from the callers side. The monitored task side is also cut, so we
2084 * If we have a deferred free, only the caller side is disconnected.
2086 UNPROTECT_CTX(ctx, flags);
2089 * All memory free operations (especially for vmalloc'ed memory)
2090 * MUST be done with interrupts ENABLED.
2092 vfree(smpl_buf_addr);
2095 * return the memory used by the context
2097 if (free_possible) pfm_context_free(ctx);
2102 static const struct file_operations pfm_file_ops = {
2103 .llseek = no_llseek,
2107 .unlocked_ioctl = pfm_ioctl,
2108 .fasync = pfm_fasync,
2109 .release = pfm_close,
2113 static char *pfmfs_dname(struct dentry *dentry, char *buffer, int buflen)
2115 return dynamic_dname(dentry, buffer, buflen, "pfm:[%lu]",
2116 d_inode(dentry)->i_ino);
2119 static const struct dentry_operations pfmfs_dentry_operations = {
2120 .d_delete = always_delete_dentry,
2121 .d_dname = pfmfs_dname,
2125 static struct file *
2126 pfm_alloc_file(pfm_context_t *ctx)
2129 struct inode *inode;
2131 struct qstr this = { .name = "" };
2134 * allocate a new inode
2136 inode = new_inode(pfmfs_mnt->mnt_sb);
2138 return ERR_PTR(-ENOMEM);
2140 DPRINT(("new inode ino=%ld @%p\n", inode->i_ino, inode));
2142 inode->i_mode = S_IFCHR|S_IRUGO;
2143 inode->i_uid = current_fsuid();
2144 inode->i_gid = current_fsgid();
2147 * allocate a new dcache entry
2149 path.dentry = d_alloc(pfmfs_mnt->mnt_root, &this);
2152 return ERR_PTR(-ENOMEM);
2154 path.mnt = mntget(pfmfs_mnt);
2156 d_add(path.dentry, inode);
2158 file = alloc_file(&path, FMODE_READ, &pfm_file_ops);
2164 file->f_flags = O_RDONLY;
2165 file->private_data = ctx;
2171 pfm_remap_buffer(struct vm_area_struct *vma, unsigned long buf, unsigned long addr, unsigned long size)
2173 DPRINT(("CPU%d buf=0x%lx addr=0x%lx size=%ld\n", smp_processor_id(), buf, addr, size));
2176 unsigned long pfn = ia64_tpa(buf) >> PAGE_SHIFT;
2179 if (remap_pfn_range(vma, addr, pfn, PAGE_SIZE, PAGE_READONLY))
2190 * allocate a sampling buffer and remaps it into the user address space of the task
2193 pfm_smpl_buffer_alloc(struct task_struct *task, struct file *filp, pfm_context_t *ctx, unsigned long rsize, void **user_vaddr)
2195 struct mm_struct *mm = task->mm;
2196 struct vm_area_struct *vma = NULL;
2202 * the fixed header + requested size and align to page boundary
2204 size = PAGE_ALIGN(rsize);
2206 DPRINT(("sampling buffer rsize=%lu size=%lu bytes\n", rsize, size));
2209 * check requested size to avoid Denial-of-service attacks
2210 * XXX: may have to refine this test
2211 * Check against address space limit.
2213 * if ((mm->total_vm << PAGE_SHIFT) + len> task->rlim[RLIMIT_AS].rlim_cur)
2216 if (size > task_rlimit(task, RLIMIT_MEMLOCK))
2220 * We do the easy to undo allocations first.
2222 smpl_buf = vzalloc(size);
2223 if (smpl_buf == NULL) {
2224 DPRINT(("Can't allocate sampling buffer\n"));
2228 DPRINT(("smpl_buf @%p\n", smpl_buf));
2231 vma = vm_area_alloc(mm);
2233 DPRINT(("Cannot allocate vma\n"));
2238 * partially initialize the vma for the sampling buffer
2240 vma->vm_file = get_file(filp);
2241 vma->vm_flags = VM_READ|VM_MAYREAD|VM_DONTEXPAND|VM_DONTDUMP;
2242 vma->vm_page_prot = PAGE_READONLY; /* XXX may need to change */
2245 * Now we have everything we need and we can initialize
2246 * and connect all the data structures
2249 ctx->ctx_smpl_hdr = smpl_buf;
2250 ctx->ctx_smpl_size = size; /* aligned size */
2253 * Let's do the difficult operations next.
2255 * now we atomically find some area in the address space and
2256 * remap the buffer in it.
2258 down_write(&task->mm->mmap_sem);
2260 /* find some free area in address space, must have mmap sem held */
2261 vma->vm_start = get_unmapped_area(NULL, 0, size, 0, MAP_PRIVATE|MAP_ANONYMOUS);
2262 if (IS_ERR_VALUE(vma->vm_start)) {
2263 DPRINT(("Cannot find unmapped area for size %ld\n", size));
2264 up_write(&task->mm->mmap_sem);
2267 vma->vm_end = vma->vm_start + size;
2268 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2270 DPRINT(("aligned size=%ld, hdr=%p mapped @0x%lx\n", size, ctx->ctx_smpl_hdr, vma->vm_start));
2272 /* can only be applied to current task, need to have the mm semaphore held when called */
2273 if (pfm_remap_buffer(vma, (unsigned long)smpl_buf, vma->vm_start, size)) {
2274 DPRINT(("Can't remap buffer\n"));
2275 up_write(&task->mm->mmap_sem);
2280 * now insert the vma in the vm list for the process, must be
2281 * done with mmap lock held
2283 insert_vm_struct(mm, vma);
2285 vm_stat_account(vma->vm_mm, vma->vm_flags, vma_pages(vma));
2286 up_write(&task->mm->mmap_sem);
2289 * keep track of user level virtual address
2291 ctx->ctx_smpl_vaddr = (void *)vma->vm_start;
2292 *(unsigned long *)user_vaddr = vma->vm_start;
2305 * XXX: do something better here
2308 pfm_bad_permissions(struct task_struct *task)
2310 const struct cred *tcred;
2311 kuid_t uid = current_uid();
2312 kgid_t gid = current_gid();
2316 tcred = __task_cred(task);
2318 /* inspired by ptrace_attach() */
2319 DPRINT(("cur: uid=%d gid=%d task: euid=%d suid=%d uid=%d egid=%d sgid=%d\n",
2320 from_kuid(&init_user_ns, uid),
2321 from_kgid(&init_user_ns, gid),
2322 from_kuid(&init_user_ns, tcred->euid),
2323 from_kuid(&init_user_ns, tcred->suid),
2324 from_kuid(&init_user_ns, tcred->uid),
2325 from_kgid(&init_user_ns, tcred->egid),
2326 from_kgid(&init_user_ns, tcred->sgid)));
2328 ret = ((!uid_eq(uid, tcred->euid))
2329 || (!uid_eq(uid, tcred->suid))
2330 || (!uid_eq(uid, tcred->uid))
2331 || (!gid_eq(gid, tcred->egid))
2332 || (!gid_eq(gid, tcred->sgid))
2333 || (!gid_eq(gid, tcred->gid))) && !capable(CAP_SYS_PTRACE);
2340 pfarg_is_sane(struct task_struct *task, pfarg_context_t *pfx)
2346 ctx_flags = pfx->ctx_flags;
2348 if (ctx_flags & PFM_FL_SYSTEM_WIDE) {
2351 * cannot block in this mode
2353 if (ctx_flags & PFM_FL_NOTIFY_BLOCK) {
2354 DPRINT(("cannot use blocking mode when in system wide monitoring\n"));
2359 /* probably more to add here */
2365 pfm_setup_buffer_fmt(struct task_struct *task, struct file *filp, pfm_context_t *ctx, unsigned int ctx_flags,
2366 unsigned int cpu, pfarg_context_t *arg)
2368 pfm_buffer_fmt_t *fmt = NULL;
2369 unsigned long size = 0UL;
2371 void *fmt_arg = NULL;
2373 #define PFM_CTXARG_BUF_ARG(a) (pfm_buffer_fmt_t *)(a+1)
2375 /* invoke and lock buffer format, if found */
2376 fmt = pfm_find_buffer_fmt(arg->ctx_smpl_buf_id);
2378 DPRINT(("[%d] cannot find buffer format\n", task_pid_nr(task)));
2383 * buffer argument MUST be contiguous to pfarg_context_t
2385 if (fmt->fmt_arg_size) fmt_arg = PFM_CTXARG_BUF_ARG(arg);
2387 ret = pfm_buf_fmt_validate(fmt, task, ctx_flags, cpu, fmt_arg);
2389 DPRINT(("[%d] after validate(0x%x,%d,%p)=%d\n", task_pid_nr(task), ctx_flags, cpu, fmt_arg, ret));
2391 if (ret) goto error;
2393 /* link buffer format and context */
2394 ctx->ctx_buf_fmt = fmt;
2395 ctx->ctx_fl_is_sampling = 1; /* assume record() is defined */
2398 * check if buffer format wants to use perfmon buffer allocation/mapping service
2400 ret = pfm_buf_fmt_getsize(fmt, task, ctx_flags, cpu, fmt_arg, &size);
2401 if (ret) goto error;
2405 * buffer is always remapped into the caller's address space
2407 ret = pfm_smpl_buffer_alloc(current, filp, ctx, size, &uaddr);
2408 if (ret) goto error;
2410 /* keep track of user address of buffer */
2411 arg->ctx_smpl_vaddr = uaddr;
2413 ret = pfm_buf_fmt_init(fmt, task, ctx->ctx_smpl_hdr, ctx_flags, cpu, fmt_arg);
2420 pfm_reset_pmu_state(pfm_context_t *ctx)
2425 * install reset values for PMC.
2427 for (i=1; PMC_IS_LAST(i) == 0; i++) {
2428 if (PMC_IS_IMPL(i) == 0) continue;
2429 ctx->ctx_pmcs[i] = PMC_DFL_VAL(i);
2430 DPRINT(("pmc[%d]=0x%lx\n", i, ctx->ctx_pmcs[i]));
2433 * PMD registers are set to 0UL when the context in memset()
2437 * On context switched restore, we must restore ALL pmc and ALL pmd even
2438 * when they are not actively used by the task. In UP, the incoming process
2439 * may otherwise pick up left over PMC, PMD state from the previous process.
2440 * As opposed to PMD, stale PMC can cause harm to the incoming
2441 * process because they may change what is being measured.
2442 * Therefore, we must systematically reinstall the entire
2443 * PMC state. In SMP, the same thing is possible on the
2444 * same CPU but also on between 2 CPUs.
2446 * The problem with PMD is information leaking especially
2447 * to user level when psr.sp=0
2449 * There is unfortunately no easy way to avoid this problem
2450 * on either UP or SMP. This definitively slows down the
2451 * pfm_load_regs() function.
2455 * bitmask of all PMCs accessible to this context
2457 * PMC0 is treated differently.
2459 ctx->ctx_all_pmcs[0] = pmu_conf->impl_pmcs[0] & ~0x1;
2462 * bitmask of all PMDs that are accessible to this context
2464 ctx->ctx_all_pmds[0] = pmu_conf->impl_pmds[0];
2466 DPRINT(("<%d> all_pmcs=0x%lx all_pmds=0x%lx\n", ctx->ctx_fd, ctx->ctx_all_pmcs[0],ctx->ctx_all_pmds[0]));
2469 * useful in case of re-enable after disable
2471 ctx->ctx_used_ibrs[0] = 0UL;
2472 ctx->ctx_used_dbrs[0] = 0UL;
2476 pfm_ctx_getsize(void *arg, size_t *sz)
2478 pfarg_context_t *req = (pfarg_context_t *)arg;
2479 pfm_buffer_fmt_t *fmt;
2483 if (!pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) return 0;
2485 fmt = pfm_find_buffer_fmt(req->ctx_smpl_buf_id);
2487 DPRINT(("cannot find buffer format\n"));
2490 /* get just enough to copy in user parameters */
2491 *sz = fmt->fmt_arg_size;
2492 DPRINT(("arg_size=%lu\n", *sz));
2500 * cannot attach if :
2502 * - task not owned by caller
2503 * - task incompatible with context mode
2506 pfm_task_incompatible(pfm_context_t *ctx, struct task_struct *task)
2509 * no kernel task or task not owner by caller
2511 if (task->mm == NULL) {
2512 DPRINT(("task [%d] has not memory context (kernel thread)\n", task_pid_nr(task)));
2515 if (pfm_bad_permissions(task)) {
2516 DPRINT(("no permission to attach to [%d]\n", task_pid_nr(task)));
2520 * cannot block in self-monitoring mode
2522 if (CTX_OVFL_NOBLOCK(ctx) == 0 && task == current) {
2523 DPRINT(("cannot load a blocking context on self for [%d]\n", task_pid_nr(task)));
2527 if (task->exit_state == EXIT_ZOMBIE) {
2528 DPRINT(("cannot attach to zombie task [%d]\n", task_pid_nr(task)));
2533 * always ok for self
2535 if (task == current) return 0;
2537 if (!task_is_stopped_or_traced(task)) {
2538 DPRINT(("cannot attach to non-stopped task [%d] state=%ld\n", task_pid_nr(task), task->state));
2542 * make sure the task is off any CPU
2544 wait_task_inactive(task, 0);
2546 /* more to come... */
2552 pfm_get_task(pfm_context_t *ctx, pid_t pid, struct task_struct **task)
2554 struct task_struct *p = current;
2557 /* XXX: need to add more checks here */
2558 if (pid < 2) return -EPERM;
2560 if (pid != task_pid_vnr(current)) {
2561 /* make sure task cannot go away while we operate on it */
2562 p = find_get_task_by_vpid(pid);
2567 ret = pfm_task_incompatible(ctx, p);
2570 } else if (p != current) {
2579 pfm_context_create(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
2581 pfarg_context_t *req = (pfarg_context_t *)arg;
2588 /* let's check the arguments first */
2589 ret = pfarg_is_sane(current, req);
2593 ctx_flags = req->ctx_flags;
2597 fd = get_unused_fd_flags(0);
2601 ctx = pfm_context_alloc(ctx_flags);
2605 filp = pfm_alloc_file(ctx);
2607 ret = PTR_ERR(filp);
2611 req->ctx_fd = ctx->ctx_fd = fd;
2614 * does the user want to sample?
2616 if (pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) {
2617 ret = pfm_setup_buffer_fmt(current, filp, ctx, ctx_flags, 0, req);
2622 DPRINT(("ctx=%p flags=0x%x system=%d notify_block=%d excl_idle=%d no_msg=%d ctx_fd=%d\n",
2627 ctx->ctx_fl_excl_idle,
2632 * initialize soft PMU state
2634 pfm_reset_pmu_state(ctx);
2636 fd_install(fd, filp);
2641 path = filp->f_path;
2645 if (ctx->ctx_buf_fmt) {
2646 pfm_buf_fmt_exit(ctx->ctx_buf_fmt, current, NULL, regs);
2649 pfm_context_free(ctx);
2656 static inline unsigned long
2657 pfm_new_counter_value (pfm_counter_t *reg, int is_long_reset)
2659 unsigned long val = is_long_reset ? reg->long_reset : reg->short_reset;
2660 unsigned long new_seed, old_seed = reg->seed, mask = reg->mask;
2661 extern unsigned long carta_random32 (unsigned long seed);
2663 if (reg->flags & PFM_REGFL_RANDOM) {
2664 new_seed = carta_random32(old_seed);
2665 val -= (old_seed & mask); /* counter values are negative numbers! */
2666 if ((mask >> 32) != 0)
2667 /* construct a full 64-bit random value: */
2668 new_seed |= carta_random32(old_seed >> 32) << 32;
2669 reg->seed = new_seed;
2676 pfm_reset_regs_masked(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
2678 unsigned long mask = ovfl_regs[0];
2679 unsigned long reset_others = 0UL;
2684 * now restore reset value on sampling overflowed counters
2686 mask >>= PMU_FIRST_COUNTER;
2687 for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
2689 if ((mask & 0x1UL) == 0UL) continue;
2691 ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
2692 reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
2694 DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
2698 * Now take care of resetting the other registers
2700 for(i = 0; reset_others; i++, reset_others >>= 1) {
2702 if ((reset_others & 0x1) == 0) continue;
2704 ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
2706 DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
2707 is_long_reset ? "long" : "short", i, val));
2712 pfm_reset_regs(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
2714 unsigned long mask = ovfl_regs[0];
2715 unsigned long reset_others = 0UL;
2719 DPRINT_ovfl(("ovfl_regs=0x%lx is_long_reset=%d\n", ovfl_regs[0], is_long_reset));
2721 if (ctx->ctx_state == PFM_CTX_MASKED) {
2722 pfm_reset_regs_masked(ctx, ovfl_regs, is_long_reset);
2727 * now restore reset value on sampling overflowed counters
2729 mask >>= PMU_FIRST_COUNTER;
2730 for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
2732 if ((mask & 0x1UL) == 0UL) continue;
2734 val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
2735 reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
2737 DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
2739 pfm_write_soft_counter(ctx, i, val);
2743 * Now take care of resetting the other registers
2745 for(i = 0; reset_others; i++, reset_others >>= 1) {
2747 if ((reset_others & 0x1) == 0) continue;
2749 val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
2751 if (PMD_IS_COUNTING(i)) {
2752 pfm_write_soft_counter(ctx, i, val);
2754 ia64_set_pmd(i, val);
2756 DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
2757 is_long_reset ? "long" : "short", i, val));
2763 pfm_write_pmcs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
2765 struct task_struct *task;
2766 pfarg_reg_t *req = (pfarg_reg_t *)arg;
2767 unsigned long value, pmc_pm;
2768 unsigned long smpl_pmds, reset_pmds, impl_pmds;
2769 unsigned int cnum, reg_flags, flags, pmc_type;
2770 int i, can_access_pmu = 0, is_loaded, is_system, expert_mode;
2771 int is_monitor, is_counting, state;
2773 pfm_reg_check_t wr_func;
2774 #define PFM_CHECK_PMC_PM(x, y, z) ((x)->ctx_fl_system ^ PMC_PM(y, z))
2776 state = ctx->ctx_state;
2777 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
2778 is_system = ctx->ctx_fl_system;
2779 task = ctx->ctx_task;
2780 impl_pmds = pmu_conf->impl_pmds[0];
2782 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
2786 * In system wide and when the context is loaded, access can only happen
2787 * when the caller is running on the CPU being monitored by the session.
2788 * It does not have to be the owner (ctx_task) of the context per se.
2790 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
2791 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
2794 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
2796 expert_mode = pfm_sysctl.expert_mode;
2798 for (i = 0; i < count; i++, req++) {
2800 cnum = req->reg_num;
2801 reg_flags = req->reg_flags;
2802 value = req->reg_value;
2803 smpl_pmds = req->reg_smpl_pmds[0];
2804 reset_pmds = req->reg_reset_pmds[0];
2808 if (cnum >= PMU_MAX_PMCS) {
2809 DPRINT(("pmc%u is invalid\n", cnum));
2813 pmc_type = pmu_conf->pmc_desc[cnum].type;
2814 pmc_pm = (value >> pmu_conf->pmc_desc[cnum].pm_pos) & 0x1;
2815 is_counting = (pmc_type & PFM_REG_COUNTING) == PFM_REG_COUNTING ? 1 : 0;
2816 is_monitor = (pmc_type & PFM_REG_MONITOR) == PFM_REG_MONITOR ? 1 : 0;
2819 * we reject all non implemented PMC as well
2820 * as attempts to modify PMC[0-3] which are used
2821 * as status registers by the PMU
2823 if ((pmc_type & PFM_REG_IMPL) == 0 || (pmc_type & PFM_REG_CONTROL) == PFM_REG_CONTROL) {
2824 DPRINT(("pmc%u is unimplemented or no-access pmc_type=%x\n", cnum, pmc_type));
2827 wr_func = pmu_conf->pmc_desc[cnum].write_check;
2829 * If the PMC is a monitor, then if the value is not the default:
2830 * - system-wide session: PMCx.pm=1 (privileged monitor)
2831 * - per-task : PMCx.pm=0 (user monitor)
2833 if (is_monitor && value != PMC_DFL_VAL(cnum) && is_system ^ pmc_pm) {
2834 DPRINT(("pmc%u pmc_pm=%lu is_system=%d\n",
2843 * enforce generation of overflow interrupt. Necessary on all
2846 value |= 1 << PMU_PMC_OI;
2848 if (reg_flags & PFM_REGFL_OVFL_NOTIFY) {
2849 flags |= PFM_REGFL_OVFL_NOTIFY;
2852 if (reg_flags & PFM_REGFL_RANDOM) flags |= PFM_REGFL_RANDOM;
2854 /* verify validity of smpl_pmds */
2855 if ((smpl_pmds & impl_pmds) != smpl_pmds) {
2856 DPRINT(("invalid smpl_pmds 0x%lx for pmc%u\n", smpl_pmds, cnum));
2860 /* verify validity of reset_pmds */
2861 if ((reset_pmds & impl_pmds) != reset_pmds) {
2862 DPRINT(("invalid reset_pmds 0x%lx for pmc%u\n", reset_pmds, cnum));
2866 if (reg_flags & (PFM_REGFL_OVFL_NOTIFY|PFM_REGFL_RANDOM)) {
2867 DPRINT(("cannot set ovfl_notify or random on pmc%u\n", cnum));
2870 /* eventid on non-counting monitors are ignored */
2874 * execute write checker, if any
2876 if (likely(expert_mode == 0 && wr_func)) {
2877 ret = (*wr_func)(task, ctx, cnum, &value, regs);
2878 if (ret) goto error;
2883 * no error on this register
2885 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
2888 * Now we commit the changes to the software state
2892 * update overflow information
2896 * full flag update each time a register is programmed
2898 ctx->ctx_pmds[cnum].flags = flags;
2900 ctx->ctx_pmds[cnum].reset_pmds[0] = reset_pmds;
2901 ctx->ctx_pmds[cnum].smpl_pmds[0] = smpl_pmds;
2902 ctx->ctx_pmds[cnum].eventid = req->reg_smpl_eventid;
2905 * Mark all PMDS to be accessed as used.
2907 * We do not keep track of PMC because we have to
2908 * systematically restore ALL of them.
2910 * We do not update the used_monitors mask, because
2911 * if we have not programmed them, then will be in
2912 * a quiescent state, therefore we will not need to
2913 * mask/restore then when context is MASKED.
2915 CTX_USED_PMD(ctx, reset_pmds);
2916 CTX_USED_PMD(ctx, smpl_pmds);
2918 * make sure we do not try to reset on
2919 * restart because we have established new values
2921 if (state == PFM_CTX_MASKED) ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
2924 * Needed in case the user does not initialize the equivalent
2925 * PMD. Clearing is done indirectly via pfm_reset_pmu_state() so there is no
2926 * possible leak here.
2928 CTX_USED_PMD(ctx, pmu_conf->pmc_desc[cnum].dep_pmd[0]);
2931 * keep track of the monitor PMC that we are using.
2932 * we save the value of the pmc in ctx_pmcs[] and if
2933 * the monitoring is not stopped for the context we also
2934 * place it in the saved state area so that it will be
2935 * picked up later by the context switch code.
2937 * The value in ctx_pmcs[] can only be changed in pfm_write_pmcs().
2939 * The value in th_pmcs[] may be modified on overflow, i.e., when
2940 * monitoring needs to be stopped.
2942 if (is_monitor) CTX_USED_MONITOR(ctx, 1UL << cnum);
2945 * update context state
2947 ctx->ctx_pmcs[cnum] = value;
2951 * write thread state
2953 if (is_system == 0) ctx->th_pmcs[cnum] = value;
2956 * write hardware register if we can
2958 if (can_access_pmu) {
2959 ia64_set_pmc(cnum, value);
2964 * per-task SMP only here
2966 * we are guaranteed that the task is not running on the other CPU,
2967 * we indicate that this PMD will need to be reloaded if the task
2968 * is rescheduled on the CPU it ran last on.
2970 ctx->ctx_reload_pmcs[0] |= 1UL << cnum;
2975 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",
2981 ctx->ctx_all_pmcs[0],
2982 ctx->ctx_used_pmds[0],
2983 ctx->ctx_pmds[cnum].eventid,
2986 ctx->ctx_reload_pmcs[0],
2987 ctx->ctx_used_monitors[0],
2988 ctx->ctx_ovfl_regs[0]));
2992 * make sure the changes are visible
2994 if (can_access_pmu) ia64_srlz_d();
2998 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3003 pfm_write_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3005 struct task_struct *task;
3006 pfarg_reg_t *req = (pfarg_reg_t *)arg;
3007 unsigned long value, hw_value, ovfl_mask;
3009 int i, can_access_pmu = 0, state;
3010 int is_counting, is_loaded, is_system, expert_mode;
3012 pfm_reg_check_t wr_func;
3015 state = ctx->ctx_state;
3016 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3017 is_system = ctx->ctx_fl_system;
3018 ovfl_mask = pmu_conf->ovfl_val;
3019 task = ctx->ctx_task;
3021 if (unlikely(state == PFM_CTX_ZOMBIE)) return -EINVAL;
3024 * on both UP and SMP, we can only write to the PMC when the task is
3025 * the owner of the local PMU.
3027 if (likely(is_loaded)) {
3029 * In system wide and when the context is loaded, access can only happen
3030 * when the caller is running on the CPU being monitored by the session.
3031 * It does not have to be the owner (ctx_task) of the context per se.
3033 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3034 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3037 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3039 expert_mode = pfm_sysctl.expert_mode;
3041 for (i = 0; i < count; i++, req++) {
3043 cnum = req->reg_num;
3044 value = req->reg_value;
3046 if (!PMD_IS_IMPL(cnum)) {
3047 DPRINT(("pmd[%u] is unimplemented or invalid\n", cnum));
3050 is_counting = PMD_IS_COUNTING(cnum);
3051 wr_func = pmu_conf->pmd_desc[cnum].write_check;
3054 * execute write checker, if any
3056 if (unlikely(expert_mode == 0 && wr_func)) {
3057 unsigned long v = value;
3059 ret = (*wr_func)(task, ctx, cnum, &v, regs);
3060 if (ret) goto abort_mission;
3067 * no error on this register
3069 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
3072 * now commit changes to software state
3077 * update virtualized (64bits) counter
3081 * write context state
3083 ctx->ctx_pmds[cnum].lval = value;
3086 * when context is load we use the split value
3089 hw_value = value & ovfl_mask;
3090 value = value & ~ovfl_mask;
3094 * update reset values (not just for counters)
3096 ctx->ctx_pmds[cnum].long_reset = req->reg_long_reset;
3097 ctx->ctx_pmds[cnum].short_reset = req->reg_short_reset;
3100 * update randomization parameters (not just for counters)
3102 ctx->ctx_pmds[cnum].seed = req->reg_random_seed;
3103 ctx->ctx_pmds[cnum].mask = req->reg_random_mask;
3106 * update context value
3108 ctx->ctx_pmds[cnum].val = value;
3111 * Keep track of what we use
3113 * We do not keep track of PMC because we have to
3114 * systematically restore ALL of them.
3116 CTX_USED_PMD(ctx, PMD_PMD_DEP(cnum));
3119 * mark this PMD register used as well
3121 CTX_USED_PMD(ctx, RDEP(cnum));
3124 * make sure we do not try to reset on
3125 * restart because we have established new values
3127 if (is_counting && state == PFM_CTX_MASKED) {
3128 ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
3133 * write thread state
3135 if (is_system == 0) ctx->th_pmds[cnum] = hw_value;
3138 * write hardware register if we can
3140 if (can_access_pmu) {
3141 ia64_set_pmd(cnum, hw_value);
3145 * we are guaranteed that the task is not running on the other CPU,
3146 * we indicate that this PMD will need to be reloaded if the task
3147 * is rescheduled on the CPU it ran last on.
3149 ctx->ctx_reload_pmds[0] |= 1UL << cnum;
3154 DPRINT(("pmd[%u]=0x%lx ld=%d apmu=%d, hw_value=0x%lx ctx_pmd=0x%lx short_reset=0x%lx "
3155 "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",
3161 ctx->ctx_pmds[cnum].val,
3162 ctx->ctx_pmds[cnum].short_reset,
3163 ctx->ctx_pmds[cnum].long_reset,
3164 PMC_OVFL_NOTIFY(ctx, cnum) ? 'Y':'N',
3165 ctx->ctx_pmds[cnum].seed,
3166 ctx->ctx_pmds[cnum].mask,
3167 ctx->ctx_used_pmds[0],
3168 ctx->ctx_pmds[cnum].reset_pmds[0],
3169 ctx->ctx_reload_pmds[0],
3170 ctx->ctx_all_pmds[0],
3171 ctx->ctx_ovfl_regs[0]));
3175 * make changes visible
3177 if (can_access_pmu) ia64_srlz_d();
3183 * for now, we have only one possibility for error
3185 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3190 * By the way of PROTECT_CONTEXT(), interrupts are masked while we are in this function.
3191 * Therefore we know, we do not have to worry about the PMU overflow interrupt. If an
3192 * interrupt is delivered during the call, it will be kept pending until we leave, making
3193 * it appears as if it had been generated at the UNPROTECT_CONTEXT(). At least we are
3194 * guaranteed to return consistent data to the user, it may simply be old. It is not
3195 * trivial to treat the overflow while inside the call because you may end up in
3196 * some module sampling buffer code causing deadlocks.
3199 pfm_read_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3201 struct task_struct *task;
3202 unsigned long val = 0UL, lval, ovfl_mask, sval;
3203 pfarg_reg_t *req = (pfarg_reg_t *)arg;
3204 unsigned int cnum, reg_flags = 0;
3205 int i, can_access_pmu = 0, state;
3206 int is_loaded, is_system, is_counting, expert_mode;
3208 pfm_reg_check_t rd_func;
3211 * access is possible when loaded only for
3212 * self-monitoring tasks or in UP mode
3215 state = ctx->ctx_state;
3216 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3217 is_system = ctx->ctx_fl_system;
3218 ovfl_mask = pmu_conf->ovfl_val;
3219 task = ctx->ctx_task;
3221 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
3223 if (likely(is_loaded)) {
3225 * In system wide and when the context is loaded, access can only happen
3226 * when the caller is running on the CPU being monitored by the session.
3227 * It does not have to be the owner (ctx_task) of the context per se.
3229 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3230 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3234 * this can be true when not self-monitoring only in UP
3236 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3238 if (can_access_pmu) ia64_srlz_d();
3240 expert_mode = pfm_sysctl.expert_mode;
3242 DPRINT(("ld=%d apmu=%d ctx_state=%d\n",
3248 * on both UP and SMP, we can only read the PMD from the hardware register when
3249 * the task is the owner of the local PMU.
3252 for (i = 0; i < count; i++, req++) {
3254 cnum = req->reg_num;
3255 reg_flags = req->reg_flags;
3257 if (unlikely(!PMD_IS_IMPL(cnum))) goto error;
3259 * we can only read the register that we use. That includes
3260 * the one we explicitly initialize AND the one we want included
3261 * in the sampling buffer (smpl_regs).
3263 * Having this restriction allows optimization in the ctxsw routine
3264 * without compromising security (leaks)
3266 if (unlikely(!CTX_IS_USED_PMD(ctx, cnum))) goto error;
3268 sval = ctx->ctx_pmds[cnum].val;
3269 lval = ctx->ctx_pmds[cnum].lval;
3270 is_counting = PMD_IS_COUNTING(cnum);
3273 * If the task is not the current one, then we check if the
3274 * PMU state is still in the local live register due to lazy ctxsw.
3275 * If true, then we read directly from the registers.
3277 if (can_access_pmu){
3278 val = ia64_get_pmd(cnum);
3281 * context has been saved
3282 * if context is zombie, then task does not exist anymore.
3283 * In this case, we use the full value saved in the context (pfm_flush_regs()).
3285 val = is_loaded ? ctx->th_pmds[cnum] : 0UL;
3287 rd_func = pmu_conf->pmd_desc[cnum].read_check;
3291 * XXX: need to check for overflow when loaded
3298 * execute read checker, if any
3300 if (unlikely(expert_mode == 0 && rd_func)) {
3301 unsigned long v = val;
3302 ret = (*rd_func)(ctx->ctx_task, ctx, cnum, &v, regs);
3303 if (ret) goto error;
3308 PFM_REG_RETFLAG_SET(reg_flags, 0);
3310 DPRINT(("pmd[%u]=0x%lx\n", cnum, val));
3313 * update register return value, abort all if problem during copy.
3314 * we only modify the reg_flags field. no check mode is fine because
3315 * access has been verified upfront in sys_perfmonctl().
3317 req->reg_value = val;
3318 req->reg_flags = reg_flags;
3319 req->reg_last_reset_val = lval;
3325 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3330 pfm_mod_write_pmcs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3334 if (req == NULL) return -EINVAL;
3336 ctx = GET_PMU_CTX();
3338 if (ctx == NULL) return -EINVAL;
3341 * for now limit to current task, which is enough when calling
3342 * from overflow handler
3344 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3346 return pfm_write_pmcs(ctx, req, nreq, regs);
3348 EXPORT_SYMBOL(pfm_mod_write_pmcs);
3351 pfm_mod_read_pmds(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3355 if (req == NULL) return -EINVAL;
3357 ctx = GET_PMU_CTX();
3359 if (ctx == NULL) return -EINVAL;
3362 * for now limit to current task, which is enough when calling
3363 * from overflow handler
3365 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3367 return pfm_read_pmds(ctx, req, nreq, regs);
3369 EXPORT_SYMBOL(pfm_mod_read_pmds);
3372 * Only call this function when a process it trying to
3373 * write the debug registers (reading is always allowed)
3376 pfm_use_debug_registers(struct task_struct *task)
3378 pfm_context_t *ctx = task->thread.pfm_context;
3379 unsigned long flags;
3382 if (pmu_conf->use_rr_dbregs == 0) return 0;
3384 DPRINT(("called for [%d]\n", task_pid_nr(task)));
3389 if (task->thread.flags & IA64_THREAD_DBG_VALID) return 0;
3392 * Even on SMP, we do not need to use an atomic here because
3393 * the only way in is via ptrace() and this is possible only when the
3394 * process is stopped. Even in the case where the ctxsw out is not totally
3395 * completed by the time we come here, there is no way the 'stopped' process
3396 * could be in the middle of fiddling with the pfm_write_ibr_dbr() routine.
3397 * So this is always safe.
3399 if (ctx && ctx->ctx_fl_using_dbreg == 1) return -1;
3404 * We cannot allow setting breakpoints when system wide monitoring
3405 * sessions are using the debug registers.
3407 if (pfm_sessions.pfs_sys_use_dbregs> 0)
3410 pfm_sessions.pfs_ptrace_use_dbregs++;
3412 DPRINT(("ptrace_use_dbregs=%u sys_use_dbregs=%u by [%d] ret = %d\n",
3413 pfm_sessions.pfs_ptrace_use_dbregs,
3414 pfm_sessions.pfs_sys_use_dbregs,
3415 task_pid_nr(task), ret));
3423 * This function is called for every task that exits with the
3424 * IA64_THREAD_DBG_VALID set. This indicates a task which was
3425 * able to use the debug registers for debugging purposes via
3426 * ptrace(). Therefore we know it was not using them for
3427 * performance monitoring, so we only decrement the number
3428 * of "ptraced" debug register users to keep the count up to date
3431 pfm_release_debug_registers(struct task_struct *task)
3433 unsigned long flags;
3436 if (pmu_conf->use_rr_dbregs == 0) return 0;
3439 if (pfm_sessions.pfs_ptrace_use_dbregs == 0) {
3440 printk(KERN_ERR "perfmon: invalid release for [%d] ptrace_use_dbregs=0\n", task_pid_nr(task));
3443 pfm_sessions.pfs_ptrace_use_dbregs--;
3452 pfm_restart(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3454 struct task_struct *task;
3455 pfm_buffer_fmt_t *fmt;
3456 pfm_ovfl_ctrl_t rst_ctrl;
3457 int state, is_system;
3460 state = ctx->ctx_state;
3461 fmt = ctx->ctx_buf_fmt;
3462 is_system = ctx->ctx_fl_system;
3463 task = PFM_CTX_TASK(ctx);
3466 case PFM_CTX_MASKED:
3468 case PFM_CTX_LOADED:
3469 if (CTX_HAS_SMPL(ctx) && fmt->fmt_restart_active) break;
3471 case PFM_CTX_UNLOADED:
3472 case PFM_CTX_ZOMBIE:
3473 DPRINT(("invalid state=%d\n", state));
3476 DPRINT(("state=%d, cannot operate (no active_restart handler)\n", state));
3481 * In system wide and when the context is loaded, access can only happen
3482 * when the caller is running on the CPU being monitored by the session.
3483 * It does not have to be the owner (ctx_task) of the context per se.
3485 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
3486 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3491 if (unlikely(task == NULL)) {
3492 printk(KERN_ERR "perfmon: [%d] pfm_restart no task\n", task_pid_nr(current));
3496 if (task == current || is_system) {
3498 fmt = ctx->ctx_buf_fmt;
3500 DPRINT(("restarting self %d ovfl=0x%lx\n",
3502 ctx->ctx_ovfl_regs[0]));
3504 if (CTX_HAS_SMPL(ctx)) {
3506 prefetch(ctx->ctx_smpl_hdr);
3508 rst_ctrl.bits.mask_monitoring = 0;
3509 rst_ctrl.bits.reset_ovfl_pmds = 0;
3511 if (state == PFM_CTX_LOADED)
3512 ret = pfm_buf_fmt_restart_active(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
3514 ret = pfm_buf_fmt_restart(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
3516 rst_ctrl.bits.mask_monitoring = 0;
3517 rst_ctrl.bits.reset_ovfl_pmds = 1;
3521 if (rst_ctrl.bits.reset_ovfl_pmds)
3522 pfm_reset_regs(ctx, ctx->ctx_ovfl_regs, PFM_PMD_LONG_RESET);
3524 if (rst_ctrl.bits.mask_monitoring == 0) {
3525 DPRINT(("resuming monitoring for [%d]\n", task_pid_nr(task)));
3527 if (state == PFM_CTX_MASKED) pfm_restore_monitoring(task);
3529 DPRINT(("keeping monitoring stopped for [%d]\n", task_pid_nr(task)));
3531 // cannot use pfm_stop_monitoring(task, regs);
3535 * clear overflowed PMD mask to remove any stale information
3537 ctx->ctx_ovfl_regs[0] = 0UL;
3540 * back to LOADED state
3542 ctx->ctx_state = PFM_CTX_LOADED;
3545 * XXX: not really useful for self monitoring
3547 ctx->ctx_fl_can_restart = 0;
3553 * restart another task
3557 * When PFM_CTX_MASKED, we cannot issue a restart before the previous
3558 * one is seen by the task.
3560 if (state == PFM_CTX_MASKED) {
3561 if (ctx->ctx_fl_can_restart == 0) return -EINVAL;
3563 * will prevent subsequent restart before this one is
3564 * seen by other task
3566 ctx->ctx_fl_can_restart = 0;
3570 * if blocking, then post the semaphore is PFM_CTX_MASKED, i.e.
3571 * the task is blocked or on its way to block. That's the normal
3572 * restart path. If the monitoring is not masked, then the task
3573 * can be actively monitoring and we cannot directly intervene.
3574 * Therefore we use the trap mechanism to catch the task and
3575 * force it to reset the buffer/reset PMDs.
3577 * if non-blocking, then we ensure that the task will go into
3578 * pfm_handle_work() before returning to user mode.
3580 * We cannot explicitly reset another task, it MUST always
3581 * be done by the task itself. This works for system wide because
3582 * the tool that is controlling the session is logically doing
3583 * "self-monitoring".
3585 if (CTX_OVFL_NOBLOCK(ctx) == 0 && state == PFM_CTX_MASKED) {
3586 DPRINT(("unblocking [%d]\n", task_pid_nr(task)));
3587 complete(&ctx->ctx_restart_done);
3589 DPRINT(("[%d] armed exit trap\n", task_pid_nr(task)));
3591 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_RESET;
3593 PFM_SET_WORK_PENDING(task, 1);
3595 set_notify_resume(task);
3598 * XXX: send reschedule if task runs on another CPU
3605 pfm_debug(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3607 unsigned int m = *(unsigned int *)arg;
3609 pfm_sysctl.debug = m == 0 ? 0 : 1;
3611 printk(KERN_INFO "perfmon debugging %s (timing reset)\n", pfm_sysctl.debug ? "on" : "off");
3614 memset(pfm_stats, 0, sizeof(pfm_stats));
3615 for(m=0; m < NR_CPUS; m++) pfm_stats[m].pfm_ovfl_intr_cycles_min = ~0UL;
3621 * arg can be NULL and count can be zero for this function
3624 pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3626 struct thread_struct *thread = NULL;
3627 struct task_struct *task;
3628 pfarg_dbreg_t *req = (pfarg_dbreg_t *)arg;
3629 unsigned long flags;
3634 int i, can_access_pmu = 0;
3635 int is_system, is_loaded;
3637 if (pmu_conf->use_rr_dbregs == 0) return -EINVAL;
3639 state = ctx->ctx_state;
3640 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3641 is_system = ctx->ctx_fl_system;
3642 task = ctx->ctx_task;
3644 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
3647 * on both UP and SMP, we can only write to the PMC when the task is
3648 * the owner of the local PMU.
3651 thread = &task->thread;
3653 * In system wide and when the context is loaded, access can only happen
3654 * when the caller is running on the CPU being monitored by the session.
3655 * It does not have to be the owner (ctx_task) of the context per se.
3657 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3658 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3661 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3665 * we do not need to check for ipsr.db because we do clear ibr.x, dbr.r, and dbr.w
3666 * ensuring that no real breakpoint can be installed via this call.
3668 * IMPORTANT: regs can be NULL in this function
3671 first_time = ctx->ctx_fl_using_dbreg == 0;
3674 * don't bother if we are loaded and task is being debugged
3676 if (is_loaded && (thread->flags & IA64_THREAD_DBG_VALID) != 0) {
3677 DPRINT(("debug registers already in use for [%d]\n", task_pid_nr(task)));
3682 * check for debug registers in system wide mode
3684 * If though a check is done in pfm_context_load(),
3685 * we must repeat it here, in case the registers are
3686 * written after the context is loaded
3691 if (first_time && is_system) {
3692 if (pfm_sessions.pfs_ptrace_use_dbregs)
3695 pfm_sessions.pfs_sys_use_dbregs++;
3700 if (ret != 0) return ret;
3703 * mark ourself as user of the debug registers for
3706 ctx->ctx_fl_using_dbreg = 1;
3709 * clear hardware registers to make sure we don't
3710 * pick up stale state.
3712 * for a system wide session, we do not use
3713 * thread.dbr, thread.ibr because this process
3714 * never leaves the current CPU and the state
3715 * is shared by all processes running on it
3717 if (first_time && can_access_pmu) {
3718 DPRINT(("[%d] clearing ibrs, dbrs\n", task_pid_nr(task)));
3719 for (i=0; i < pmu_conf->num_ibrs; i++) {
3720 ia64_set_ibr(i, 0UL);
3721 ia64_dv_serialize_instruction();
3724 for (i=0; i < pmu_conf->num_dbrs; i++) {
3725 ia64_set_dbr(i, 0UL);
3726 ia64_dv_serialize_data();
3732 * Now install the values into the registers
3734 for (i = 0; i < count; i++, req++) {
3736 rnum = req->dbreg_num;
3737 dbreg.val = req->dbreg_value;
3741 if ((mode == PFM_CODE_RR && rnum >= PFM_NUM_IBRS) || ((mode == PFM_DATA_RR) && rnum >= PFM_NUM_DBRS)) {
3742 DPRINT(("invalid register %u val=0x%lx mode=%d i=%d count=%d\n",
3743 rnum, dbreg.val, mode, i, count));
3749 * make sure we do not install enabled breakpoint
3752 if (mode == PFM_CODE_RR)
3753 dbreg.ibr.ibr_x = 0;
3755 dbreg.dbr.dbr_r = dbreg.dbr.dbr_w = 0;
3758 PFM_REG_RETFLAG_SET(req->dbreg_flags, 0);
3761 * Debug registers, just like PMC, can only be modified
3762 * by a kernel call. Moreover, perfmon() access to those
3763 * registers are centralized in this routine. The hardware
3764 * does not modify the value of these registers, therefore,
3765 * if we save them as they are written, we can avoid having
3766 * to save them on context switch out. This is made possible
3767 * by the fact that when perfmon uses debug registers, ptrace()
3768 * won't be able to modify them concurrently.
3770 if (mode == PFM_CODE_RR) {
3771 CTX_USED_IBR(ctx, rnum);
3773 if (can_access_pmu) {
3774 ia64_set_ibr(rnum, dbreg.val);
3775 ia64_dv_serialize_instruction();
3778 ctx->ctx_ibrs[rnum] = dbreg.val;
3780 DPRINT(("write ibr%u=0x%lx used_ibrs=0x%x ld=%d apmu=%d\n",
3781 rnum, dbreg.val, ctx->ctx_used_ibrs[0], is_loaded, can_access_pmu));
3783 CTX_USED_DBR(ctx, rnum);
3785 if (can_access_pmu) {
3786 ia64_set_dbr(rnum, dbreg.val);
3787 ia64_dv_serialize_data();
3789 ctx->ctx_dbrs[rnum] = dbreg.val;
3791 DPRINT(("write dbr%u=0x%lx used_dbrs=0x%x ld=%d apmu=%d\n",
3792 rnum, dbreg.val, ctx->ctx_used_dbrs[0], is_loaded, can_access_pmu));
3800 * in case it was our first attempt, we undo the global modifications
3804 if (ctx->ctx_fl_system) {
3805 pfm_sessions.pfs_sys_use_dbregs--;
3808 ctx->ctx_fl_using_dbreg = 0;
3811 * install error return flag
3813 PFM_REG_RETFLAG_SET(req->dbreg_flags, PFM_REG_RETFL_EINVAL);
3819 pfm_write_ibrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3821 return pfm_write_ibr_dbr(PFM_CODE_RR, ctx, arg, count, regs);
3825 pfm_write_dbrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3827 return pfm_write_ibr_dbr(PFM_DATA_RR, ctx, arg, count, regs);
3831 pfm_mod_write_ibrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3835 if (req == NULL) return -EINVAL;
3837 ctx = GET_PMU_CTX();
3839 if (ctx == NULL) return -EINVAL;
3842 * for now limit to current task, which is enough when calling
3843 * from overflow handler
3845 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3847 return pfm_write_ibrs(ctx, req, nreq, regs);
3849 EXPORT_SYMBOL(pfm_mod_write_ibrs);
3852 pfm_mod_write_dbrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3856 if (req == NULL) return -EINVAL;
3858 ctx = GET_PMU_CTX();
3860 if (ctx == NULL) return -EINVAL;
3863 * for now limit to current task, which is enough when calling
3864 * from overflow handler
3866 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3868 return pfm_write_dbrs(ctx, req, nreq, regs);
3870 EXPORT_SYMBOL(pfm_mod_write_dbrs);
3874 pfm_get_features(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3876 pfarg_features_t *req = (pfarg_features_t *)arg;
3878 req->ft_version = PFM_VERSION;
3883 pfm_stop(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3885 struct pt_regs *tregs;
3886 struct task_struct *task = PFM_CTX_TASK(ctx);
3887 int state, is_system;
3889 state = ctx->ctx_state;
3890 is_system = ctx->ctx_fl_system;
3893 * context must be attached to issue the stop command (includes LOADED,MASKED,ZOMBIE)
3895 if (state == PFM_CTX_UNLOADED) return -EINVAL;
3898 * In system wide and when the context is loaded, access can only happen
3899 * when the caller is running on the CPU being monitored by the session.
3900 * It does not have to be the owner (ctx_task) of the context per se.
3902 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
3903 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3906 DPRINT(("task [%d] ctx_state=%d is_system=%d\n",
3907 task_pid_nr(PFM_CTX_TASK(ctx)),
3911 * in system mode, we need to update the PMU directly
3912 * and the user level state of the caller, which may not
3913 * necessarily be the creator of the context.
3917 * Update local PMU first
3921 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
3925 * update local cpuinfo
3927 PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
3930 * stop monitoring, does srlz.i
3935 * stop monitoring in the caller
3937 ia64_psr(regs)->pp = 0;
3945 if (task == current) {
3946 /* stop monitoring at kernel level */
3950 * stop monitoring at the user level
3952 ia64_psr(regs)->up = 0;
3954 tregs = task_pt_regs(task);
3957 * stop monitoring at the user level
3959 ia64_psr(tregs)->up = 0;
3962 * monitoring disabled in kernel at next reschedule
3964 ctx->ctx_saved_psr_up = 0;
3965 DPRINT(("task=[%d]\n", task_pid_nr(task)));
3972 pfm_start(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3974 struct pt_regs *tregs;
3975 int state, is_system;
3977 state = ctx->ctx_state;
3978 is_system = ctx->ctx_fl_system;
3980 if (state != PFM_CTX_LOADED) return -EINVAL;
3983 * In system wide and when the context is loaded, access can only happen
3984 * when the caller is running on the CPU being monitored by the session.
3985 * It does not have to be the owner (ctx_task) of the context per se.
3987 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
3988 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3993 * in system mode, we need to update the PMU directly
3994 * and the user level state of the caller, which may not
3995 * necessarily be the creator of the context.
4000 * set user level psr.pp for the caller
4002 ia64_psr(regs)->pp = 1;
4005 * now update the local PMU and cpuinfo
4007 PFM_CPUINFO_SET(PFM_CPUINFO_DCR_PP);
4010 * start monitoring at kernel level
4015 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
4025 if (ctx->ctx_task == current) {
4027 /* start monitoring at kernel level */
4031 * activate monitoring at user level
4033 ia64_psr(regs)->up = 1;
4036 tregs = task_pt_regs(ctx->ctx_task);
4039 * start monitoring at the kernel level the next
4040 * time the task is scheduled
4042 ctx->ctx_saved_psr_up = IA64_PSR_UP;
4045 * activate monitoring at user level
4047 ia64_psr(tregs)->up = 1;
4053 pfm_get_pmc_reset(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4055 pfarg_reg_t *req = (pfarg_reg_t *)arg;
4060 for (i = 0; i < count; i++, req++) {
4062 cnum = req->reg_num;
4064 if (!PMC_IS_IMPL(cnum)) goto abort_mission;
4066 req->reg_value = PMC_DFL_VAL(cnum);
4068 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
4070 DPRINT(("pmc_reset_val pmc[%u]=0x%lx\n", cnum, req->reg_value));
4075 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
4080 pfm_check_task_exist(pfm_context_t *ctx)
4082 struct task_struct *g, *t;
4085 read_lock(&tasklist_lock);
4087 do_each_thread (g, t) {
4088 if (t->thread.pfm_context == ctx) {
4092 } while_each_thread (g, t);
4094 read_unlock(&tasklist_lock);
4096 DPRINT(("pfm_check_task_exist: ret=%d ctx=%p\n", ret, ctx));
4102 pfm_context_load(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4104 struct task_struct *task;
4105 struct thread_struct *thread;
4106 struct pfm_context_t *old;
4107 unsigned long flags;
4109 struct task_struct *owner_task = NULL;
4111 pfarg_load_t *req = (pfarg_load_t *)arg;
4112 unsigned long *pmcs_source, *pmds_source;
4115 int state, is_system, set_dbregs = 0;
4117 state = ctx->ctx_state;
4118 is_system = ctx->ctx_fl_system;
4120 * can only load from unloaded or terminated state
4122 if (state != PFM_CTX_UNLOADED) {
4123 DPRINT(("cannot load to [%d], invalid ctx_state=%d\n",
4129 DPRINT(("load_pid [%d] using_dbreg=%d\n", req->load_pid, ctx->ctx_fl_using_dbreg));
4131 if (CTX_OVFL_NOBLOCK(ctx) == 0 && req->load_pid == current->pid) {
4132 DPRINT(("cannot use blocking mode on self\n"));
4136 ret = pfm_get_task(ctx, req->load_pid, &task);
4138 DPRINT(("load_pid [%d] get_task=%d\n", req->load_pid, ret));
4145 * system wide is self monitoring only
4147 if (is_system && task != current) {
4148 DPRINT(("system wide is self monitoring only load_pid=%d\n",
4153 thread = &task->thread;
4157 * cannot load a context which is using range restrictions,
4158 * into a task that is being debugged.
4160 if (ctx->ctx_fl_using_dbreg) {
4161 if (thread->flags & IA64_THREAD_DBG_VALID) {
4163 DPRINT(("load_pid [%d] task is debugged, cannot load range restrictions\n", req->load_pid));
4169 if (pfm_sessions.pfs_ptrace_use_dbregs) {
4170 DPRINT(("cannot load [%d] dbregs in use\n",
4171 task_pid_nr(task)));
4174 pfm_sessions.pfs_sys_use_dbregs++;
4175 DPRINT(("load [%d] increased sys_use_dbreg=%u\n", task_pid_nr(task), pfm_sessions.pfs_sys_use_dbregs));
4182 if (ret) goto error;
4186 * SMP system-wide monitoring implies self-monitoring.
4188 * The programming model expects the task to
4189 * be pinned on a CPU throughout the session.
4190 * Here we take note of the current CPU at the
4191 * time the context is loaded. No call from
4192 * another CPU will be allowed.
4194 * The pinning via shed_setaffinity()
4195 * must be done by the calling task prior
4198 * systemwide: keep track of CPU this session is supposed to run on
4200 the_cpu = ctx->ctx_cpu = smp_processor_id();
4204 * now reserve the session
4206 ret = pfm_reserve_session(current, is_system, the_cpu);
4207 if (ret) goto error;
4210 * task is necessarily stopped at this point.
4212 * If the previous context was zombie, then it got removed in
4213 * pfm_save_regs(). Therefore we should not see it here.
4214 * If we see a context, then this is an active context
4216 * XXX: needs to be atomic
4218 DPRINT(("before cmpxchg() old_ctx=%p new_ctx=%p\n",
4219 thread->pfm_context, ctx));
4222 old = ia64_cmpxchg(acq, &thread->pfm_context, NULL, ctx, sizeof(pfm_context_t *));
4224 DPRINT(("load_pid [%d] already has a context\n", req->load_pid));
4228 pfm_reset_msgq(ctx);
4230 ctx->ctx_state = PFM_CTX_LOADED;
4233 * link context to task
4235 ctx->ctx_task = task;
4239 * we load as stopped
4241 PFM_CPUINFO_SET(PFM_CPUINFO_SYST_WIDE);
4242 PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
4244 if (ctx->ctx_fl_excl_idle) PFM_CPUINFO_SET(PFM_CPUINFO_EXCL_IDLE);
4246 thread->flags |= IA64_THREAD_PM_VALID;
4250 * propagate into thread-state
4252 pfm_copy_pmds(task, ctx);
4253 pfm_copy_pmcs(task, ctx);
4255 pmcs_source = ctx->th_pmcs;
4256 pmds_source = ctx->th_pmds;
4259 * always the case for system-wide
4261 if (task == current) {
4263 if (is_system == 0) {
4265 /* allow user level control */
4266 ia64_psr(regs)->sp = 0;
4267 DPRINT(("clearing psr.sp for [%d]\n", task_pid_nr(task)));
4269 SET_LAST_CPU(ctx, smp_processor_id());
4271 SET_ACTIVATION(ctx);
4274 * push the other task out, if any
4276 owner_task = GET_PMU_OWNER();
4277 if (owner_task) pfm_lazy_save_regs(owner_task);
4281 * load all PMD from ctx to PMU (as opposed to thread state)
4282 * restore all PMC from ctx to PMU
4284 pfm_restore_pmds(pmds_source, ctx->ctx_all_pmds[0]);
4285 pfm_restore_pmcs(pmcs_source, ctx->ctx_all_pmcs[0]);
4287 ctx->ctx_reload_pmcs[0] = 0UL;
4288 ctx->ctx_reload_pmds[0] = 0UL;
4291 * guaranteed safe by earlier check against DBG_VALID
4293 if (ctx->ctx_fl_using_dbreg) {
4294 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
4295 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
4300 SET_PMU_OWNER(task, ctx);
4302 DPRINT(("context loaded on PMU for [%d]\n", task_pid_nr(task)));
4305 * when not current, task MUST be stopped, so this is safe
4307 regs = task_pt_regs(task);
4309 /* force a full reload */
4310 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
4311 SET_LAST_CPU(ctx, -1);
4313 /* initial saved psr (stopped) */
4314 ctx->ctx_saved_psr_up = 0UL;
4315 ia64_psr(regs)->up = ia64_psr(regs)->pp = 0;
4321 if (ret) pfm_unreserve_session(ctx, ctx->ctx_fl_system, the_cpu);
4324 * we must undo the dbregs setting (for system-wide)
4326 if (ret && set_dbregs) {
4328 pfm_sessions.pfs_sys_use_dbregs--;
4332 * release task, there is now a link with the context
4334 if (is_system == 0 && task != current) {
4338 ret = pfm_check_task_exist(ctx);
4340 ctx->ctx_state = PFM_CTX_UNLOADED;
4341 ctx->ctx_task = NULL;
4349 * in this function, we do not need to increase the use count
4350 * for the task via get_task_struct(), because we hold the
4351 * context lock. If the task were to disappear while having
4352 * a context attached, it would go through pfm_exit_thread()
4353 * which also grabs the context lock and would therefore be blocked
4354 * until we are here.
4356 static void pfm_flush_pmds(struct task_struct *, pfm_context_t *ctx);
4359 pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4361 struct task_struct *task = PFM_CTX_TASK(ctx);
4362 struct pt_regs *tregs;
4363 int prev_state, is_system;
4366 DPRINT(("ctx_state=%d task [%d]\n", ctx->ctx_state, task ? task_pid_nr(task) : -1));
4368 prev_state = ctx->ctx_state;
4369 is_system = ctx->ctx_fl_system;
4372 * unload only when necessary
4374 if (prev_state == PFM_CTX_UNLOADED) {
4375 DPRINT(("ctx_state=%d, nothing to do\n", prev_state));
4380 * clear psr and dcr bits
4382 ret = pfm_stop(ctx, NULL, 0, regs);
4383 if (ret) return ret;
4385 ctx->ctx_state = PFM_CTX_UNLOADED;
4388 * in system mode, we need to update the PMU directly
4389 * and the user level state of the caller, which may not
4390 * necessarily be the creator of the context.
4397 * local PMU is taken care of in pfm_stop()
4399 PFM_CPUINFO_CLEAR(PFM_CPUINFO_SYST_WIDE);
4400 PFM_CPUINFO_CLEAR(PFM_CPUINFO_EXCL_IDLE);
4403 * save PMDs in context
4406 pfm_flush_pmds(current, ctx);
4409 * at this point we are done with the PMU
4410 * so we can unreserve the resource.
4412 if (prev_state != PFM_CTX_ZOMBIE)
4413 pfm_unreserve_session(ctx, 1 , ctx->ctx_cpu);
4416 * disconnect context from task
4418 task->thread.pfm_context = NULL;
4420 * disconnect task from context
4422 ctx->ctx_task = NULL;
4425 * There is nothing more to cleanup here.
4433 tregs = task == current ? regs : task_pt_regs(task);
4435 if (task == current) {
4437 * cancel user level control
4439 ia64_psr(regs)->sp = 1;
4441 DPRINT(("setting psr.sp for [%d]\n", task_pid_nr(task)));
4444 * save PMDs to context
4447 pfm_flush_pmds(task, ctx);
4450 * at this point we are done with the PMU
4451 * so we can unreserve the resource.
4453 * when state was ZOMBIE, we have already unreserved.
4455 if (prev_state != PFM_CTX_ZOMBIE)
4456 pfm_unreserve_session(ctx, 0 , ctx->ctx_cpu);
4459 * reset activation counter and psr
4461 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
4462 SET_LAST_CPU(ctx, -1);
4465 * PMU state will not be restored
4467 task->thread.flags &= ~IA64_THREAD_PM_VALID;
4470 * break links between context and task
4472 task->thread.pfm_context = NULL;
4473 ctx->ctx_task = NULL;
4475 PFM_SET_WORK_PENDING(task, 0);
4477 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
4478 ctx->ctx_fl_can_restart = 0;
4479 ctx->ctx_fl_going_zombie = 0;
4481 DPRINT(("disconnected [%d] from context\n", task_pid_nr(task)));
4488 * called only from exit_thread()
4489 * we come here only if the task has a context attached (loaded or masked)
4492 pfm_exit_thread(struct task_struct *task)
4495 unsigned long flags;
4496 struct pt_regs *regs = task_pt_regs(task);
4500 ctx = PFM_GET_CTX(task);
4502 PROTECT_CTX(ctx, flags);
4504 DPRINT(("state=%d task [%d]\n", ctx->ctx_state, task_pid_nr(task)));
4506 state = ctx->ctx_state;
4508 case PFM_CTX_UNLOADED:
4510 * only comes to this function if pfm_context is not NULL, i.e., cannot
4511 * be in unloaded state
4513 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] ctx unloaded\n", task_pid_nr(task));
4515 case PFM_CTX_LOADED:
4516 case PFM_CTX_MASKED:
4517 ret = pfm_context_unload(ctx, NULL, 0, regs);
4519 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task_pid_nr(task), state, ret);
4521 DPRINT(("ctx unloaded for current state was %d\n", state));
4523 pfm_end_notify_user(ctx);
4525 case PFM_CTX_ZOMBIE:
4526 ret = pfm_context_unload(ctx, NULL, 0, regs);
4528 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task_pid_nr(task), state, ret);
4533 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] unexpected state=%d\n", task_pid_nr(task), state);
4536 UNPROTECT_CTX(ctx, flags);
4538 { u64 psr = pfm_get_psr();
4539 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
4540 BUG_ON(GET_PMU_OWNER());
4541 BUG_ON(ia64_psr(regs)->up);
4542 BUG_ON(ia64_psr(regs)->pp);
4546 * All memory free operations (especially for vmalloc'ed memory)
4547 * MUST be done with interrupts ENABLED.
4549 if (free_ok) pfm_context_free(ctx);
4553 * functions MUST be listed in the increasing order of their index (see permfon.h)
4555 #define PFM_CMD(name, flags, arg_count, arg_type, getsz) { name, #name, flags, arg_count, sizeof(arg_type), getsz }
4556 #define PFM_CMD_S(name, flags) { name, #name, flags, 0, 0, NULL }
4557 #define PFM_CMD_PCLRWS (PFM_CMD_FD|PFM_CMD_ARG_RW|PFM_CMD_STOP)
4558 #define PFM_CMD_PCLRW (PFM_CMD_FD|PFM_CMD_ARG_RW)
4559 #define PFM_CMD_NONE { NULL, "no-cmd", 0, 0, 0, NULL}
4561 static pfm_cmd_desc_t pfm_cmd_tab[]={
4562 /* 0 */PFM_CMD_NONE,
4563 /* 1 */PFM_CMD(pfm_write_pmcs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4564 /* 2 */PFM_CMD(pfm_write_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4565 /* 3 */PFM_CMD(pfm_read_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4566 /* 4 */PFM_CMD_S(pfm_stop, PFM_CMD_PCLRWS),
4567 /* 5 */PFM_CMD_S(pfm_start, PFM_CMD_PCLRWS),
4568 /* 6 */PFM_CMD_NONE,
4569 /* 7 */PFM_CMD_NONE,
4570 /* 8 */PFM_CMD(pfm_context_create, PFM_CMD_ARG_RW, 1, pfarg_context_t, pfm_ctx_getsize),
4571 /* 9 */PFM_CMD_NONE,
4572 /* 10 */PFM_CMD_S(pfm_restart, PFM_CMD_PCLRW),
4573 /* 11 */PFM_CMD_NONE,
4574 /* 12 */PFM_CMD(pfm_get_features, PFM_CMD_ARG_RW, 1, pfarg_features_t, NULL),
4575 /* 13 */PFM_CMD(pfm_debug, 0, 1, unsigned int, NULL),
4576 /* 14 */PFM_CMD_NONE,
4577 /* 15 */PFM_CMD(pfm_get_pmc_reset, PFM_CMD_ARG_RW, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4578 /* 16 */PFM_CMD(pfm_context_load, PFM_CMD_PCLRWS, 1, pfarg_load_t, NULL),
4579 /* 17 */PFM_CMD_S(pfm_context_unload, PFM_CMD_PCLRWS),
4580 /* 18 */PFM_CMD_NONE,
4581 /* 19 */PFM_CMD_NONE,
4582 /* 20 */PFM_CMD_NONE,
4583 /* 21 */PFM_CMD_NONE,
4584 /* 22 */PFM_CMD_NONE,
4585 /* 23 */PFM_CMD_NONE,
4586 /* 24 */PFM_CMD_NONE,
4587 /* 25 */PFM_CMD_NONE,
4588 /* 26 */PFM_CMD_NONE,
4589 /* 27 */PFM_CMD_NONE,
4590 /* 28 */PFM_CMD_NONE,
4591 /* 29 */PFM_CMD_NONE,
4592 /* 30 */PFM_CMD_NONE,
4593 /* 31 */PFM_CMD_NONE,
4594 /* 32 */PFM_CMD(pfm_write_ibrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL),
4595 /* 33 */PFM_CMD(pfm_write_dbrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL)
4597 #define PFM_CMD_COUNT (sizeof(pfm_cmd_tab)/sizeof(pfm_cmd_desc_t))
4600 pfm_check_task_state(pfm_context_t *ctx, int cmd, unsigned long flags)
4602 struct task_struct *task;
4603 int state, old_state;
4606 state = ctx->ctx_state;
4607 task = ctx->ctx_task;
4610 DPRINT(("context %d no task, state=%d\n", ctx->ctx_fd, state));
4614 DPRINT(("context %d state=%d [%d] task_state=%ld must_stop=%d\n",
4618 task->state, PFM_CMD_STOPPED(cmd)));
4621 * self-monitoring always ok.
4623 * for system-wide the caller can either be the creator of the
4624 * context (to one to which the context is attached to) OR
4625 * a task running on the same CPU as the session.
4627 if (task == current || ctx->ctx_fl_system) return 0;
4630 * we are monitoring another thread
4633 case PFM_CTX_UNLOADED:
4635 * if context is UNLOADED we are safe to go
4638 case PFM_CTX_ZOMBIE:
4640 * no command can operate on a zombie context
4642 DPRINT(("cmd %d state zombie cannot operate on context\n", cmd));
4644 case PFM_CTX_MASKED:
4646 * PMU state has been saved to software even though
4647 * the thread may still be running.
4649 if (cmd != PFM_UNLOAD_CONTEXT) return 0;
4653 * context is LOADED or MASKED. Some commands may need to have
4656 * We could lift this restriction for UP but it would mean that
4657 * the user has no guarantee the task would not run between
4658 * two successive calls to perfmonctl(). That's probably OK.
4659 * If this user wants to ensure the task does not run, then
4660 * the task must be stopped.
4662 if (PFM_CMD_STOPPED(cmd)) {
4663 if (!task_is_stopped_or_traced(task)) {
4664 DPRINT(("[%d] task not in stopped state\n", task_pid_nr(task)));
4668 * task is now stopped, wait for ctxsw out
4670 * This is an interesting point in the code.
4671 * We need to unprotect the context because
4672 * the pfm_save_regs() routines needs to grab
4673 * the same lock. There are danger in doing
4674 * this because it leaves a window open for
4675 * another task to get access to the context
4676 * and possibly change its state. The one thing
4677 * that is not possible is for the context to disappear
4678 * because we are protected by the VFS layer, i.e.,
4679 * get_fd()/put_fd().
4683 UNPROTECT_CTX(ctx, flags);
4685 wait_task_inactive(task, 0);
4687 PROTECT_CTX(ctx, flags);
4690 * we must recheck to verify if state has changed
4692 if (ctx->ctx_state != old_state) {
4693 DPRINT(("old_state=%d new_state=%d\n", old_state, ctx->ctx_state));
4701 * system-call entry point (must return long)
4704 sys_perfmonctl (int fd, int cmd, void __user *arg, int count)
4706 struct fd f = {NULL, 0};
4707 pfm_context_t *ctx = NULL;
4708 unsigned long flags = 0UL;
4709 void *args_k = NULL;
4710 long ret; /* will expand int return types */
4711 size_t base_sz, sz, xtra_sz = 0;
4712 int narg, completed_args = 0, call_made = 0, cmd_flags;
4713 int (*func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
4714 int (*getsize)(void *arg, size_t *sz);
4715 #define PFM_MAX_ARGSIZE 4096
4718 * reject any call if perfmon was disabled at initialization
4720 if (unlikely(pmu_conf == NULL)) return -ENOSYS;
4722 if (unlikely(cmd < 0 || cmd >= PFM_CMD_COUNT)) {
4723 DPRINT(("invalid cmd=%d\n", cmd));
4727 func = pfm_cmd_tab[cmd].cmd_func;
4728 narg = pfm_cmd_tab[cmd].cmd_narg;
4729 base_sz = pfm_cmd_tab[cmd].cmd_argsize;
4730 getsize = pfm_cmd_tab[cmd].cmd_getsize;
4731 cmd_flags = pfm_cmd_tab[cmd].cmd_flags;
4733 if (unlikely(func == NULL)) {
4734 DPRINT(("invalid cmd=%d\n", cmd));
4738 DPRINT(("cmd=%s idx=%d narg=0x%x argsz=%lu count=%d\n",
4746 * check if number of arguments matches what the command expects
4748 if (unlikely((narg == PFM_CMD_ARG_MANY && count <= 0) || (narg > 0 && narg != count)))
4752 sz = xtra_sz + base_sz*count;
4754 * limit abuse to min page size
4756 if (unlikely(sz > PFM_MAX_ARGSIZE)) {
4757 printk(KERN_ERR "perfmon: [%d] argument too big %lu\n", task_pid_nr(current), sz);
4762 * allocate default-sized argument buffer
4764 if (likely(count && args_k == NULL)) {
4765 args_k = kmalloc(PFM_MAX_ARGSIZE, GFP_KERNEL);
4766 if (args_k == NULL) return -ENOMEM;
4774 * assume sz = 0 for command without parameters
4776 if (sz && copy_from_user(args_k, arg, sz)) {
4777 DPRINT(("cannot copy_from_user %lu bytes @%p\n", sz, arg));
4782 * check if command supports extra parameters
4784 if (completed_args == 0 && getsize) {
4786 * get extra parameters size (based on main argument)
4788 ret = (*getsize)(args_k, &xtra_sz);
4789 if (ret) goto error_args;
4793 DPRINT(("restart_args sz=%lu xtra_sz=%lu\n", sz, xtra_sz));
4795 /* retry if necessary */
4796 if (likely(xtra_sz)) goto restart_args;
4799 if (unlikely((cmd_flags & PFM_CMD_FD) == 0)) goto skip_fd;
4804 if (unlikely(f.file == NULL)) {
4805 DPRINT(("invalid fd %d\n", fd));
4808 if (unlikely(PFM_IS_FILE(f.file) == 0)) {
4809 DPRINT(("fd %d not related to perfmon\n", fd));
4813 ctx = f.file->private_data;
4814 if (unlikely(ctx == NULL)) {
4815 DPRINT(("no context for fd %d\n", fd));
4818 prefetch(&ctx->ctx_state);
4820 PROTECT_CTX(ctx, flags);
4823 * check task is stopped
4825 ret = pfm_check_task_state(ctx, cmd, flags);
4826 if (unlikely(ret)) goto abort_locked;
4829 ret = (*func)(ctx, args_k, count, task_pt_regs(current));
4835 DPRINT(("context unlocked\n"));
4836 UNPROTECT_CTX(ctx, flags);
4839 /* copy argument back to user, if needed */
4840 if (call_made && PFM_CMD_RW_ARG(cmd) && copy_to_user(arg, args_k, base_sz*count)) ret = -EFAULT;
4848 DPRINT(("cmd=%s ret=%ld\n", PFM_CMD_NAME(cmd), ret));
4854 pfm_resume_after_ovfl(pfm_context_t *ctx, unsigned long ovfl_regs, struct pt_regs *regs)
4856 pfm_buffer_fmt_t *fmt = ctx->ctx_buf_fmt;
4857 pfm_ovfl_ctrl_t rst_ctrl;
4861 state = ctx->ctx_state;
4863 * Unlock sampling buffer and reset index atomically
4864 * XXX: not really needed when blocking
4866 if (CTX_HAS_SMPL(ctx)) {
4868 rst_ctrl.bits.mask_monitoring = 0;
4869 rst_ctrl.bits.reset_ovfl_pmds = 0;
4871 if (state == PFM_CTX_LOADED)
4872 ret = pfm_buf_fmt_restart_active(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
4874 ret = pfm_buf_fmt_restart(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
4876 rst_ctrl.bits.mask_monitoring = 0;
4877 rst_ctrl.bits.reset_ovfl_pmds = 1;
4881 if (rst_ctrl.bits.reset_ovfl_pmds) {
4882 pfm_reset_regs(ctx, &ovfl_regs, PFM_PMD_LONG_RESET);
4884 if (rst_ctrl.bits.mask_monitoring == 0) {
4885 DPRINT(("resuming monitoring\n"));
4886 if (ctx->ctx_state == PFM_CTX_MASKED) pfm_restore_monitoring(current);
4888 DPRINT(("stopping monitoring\n"));
4889 //pfm_stop_monitoring(current, regs);
4891 ctx->ctx_state = PFM_CTX_LOADED;
4896 * context MUST BE LOCKED when calling
4897 * can only be called for current
4900 pfm_context_force_terminate(pfm_context_t *ctx, struct pt_regs *regs)
4904 DPRINT(("entering for [%d]\n", task_pid_nr(current)));
4906 ret = pfm_context_unload(ctx, NULL, 0, regs);
4908 printk(KERN_ERR "pfm_context_force_terminate: [%d] unloaded failed with %d\n", task_pid_nr(current), ret);
4912 * and wakeup controlling task, indicating we are now disconnected
4914 wake_up_interruptible(&ctx->ctx_zombieq);
4917 * given that context is still locked, the controlling
4918 * task will only get access when we return from
4919 * pfm_handle_work().
4923 static int pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds);
4926 * pfm_handle_work() can be called with interrupts enabled
4927 * (TIF_NEED_RESCHED) or disabled. The down_interruptible
4928 * call may sleep, therefore we must re-enable interrupts
4929 * to avoid deadlocks. It is safe to do so because this function
4930 * is called ONLY when returning to user level (pUStk=1), in which case
4931 * there is no risk of kernel stack overflow due to deep
4932 * interrupt nesting.
4935 pfm_handle_work(void)
4938 struct pt_regs *regs;
4939 unsigned long flags, dummy_flags;
4940 unsigned long ovfl_regs;
4941 unsigned int reason;
4944 ctx = PFM_GET_CTX(current);
4946 printk(KERN_ERR "perfmon: [%d] has no PFM context\n",
4947 task_pid_nr(current));
4951 PROTECT_CTX(ctx, flags);
4953 PFM_SET_WORK_PENDING(current, 0);
4955 regs = task_pt_regs(current);
4958 * extract reason for being here and clear
4960 reason = ctx->ctx_fl_trap_reason;
4961 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
4962 ovfl_regs = ctx->ctx_ovfl_regs[0];
4964 DPRINT(("reason=%d state=%d\n", reason, ctx->ctx_state));
4967 * must be done before we check for simple-reset mode
4969 if (ctx->ctx_fl_going_zombie || ctx->ctx_state == PFM_CTX_ZOMBIE)
4972 //if (CTX_OVFL_NOBLOCK(ctx)) goto skip_blocking;
4973 if (reason == PFM_TRAP_REASON_RESET)
4977 * restore interrupt mask to what it was on entry.
4978 * Could be enabled/diasbled.
4980 UNPROTECT_CTX(ctx, flags);
4983 * force interrupt enable because of down_interruptible()
4987 DPRINT(("before block sleeping\n"));
4990 * may go through without blocking on SMP systems
4991 * if restart has been received already by the time we call down()
4993 ret = wait_for_completion_interruptible(&ctx->ctx_restart_done);
4995 DPRINT(("after block sleeping ret=%d\n", ret));
4998 * lock context and mask interrupts again
4999 * We save flags into a dummy because we may have
5000 * altered interrupts mask compared to entry in this
5003 PROTECT_CTX(ctx, dummy_flags);
5006 * we need to read the ovfl_regs only after wake-up
5007 * because we may have had pfm_write_pmds() in between
5008 * and that can changed PMD values and therefore
5009 * ovfl_regs is reset for these new PMD values.
5011 ovfl_regs = ctx->ctx_ovfl_regs[0];
5013 if (ctx->ctx_fl_going_zombie) {
5015 DPRINT(("context is zombie, bailing out\n"));
5016 pfm_context_force_terminate(ctx, regs);
5020 * in case of interruption of down() we don't restart anything
5026 pfm_resume_after_ovfl(ctx, ovfl_regs, regs);
5027 ctx->ctx_ovfl_regs[0] = 0UL;
5031 * restore flags as they were upon entry
5033 UNPROTECT_CTX(ctx, flags);
5037 pfm_notify_user(pfm_context_t *ctx, pfm_msg_t *msg)
5039 if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
5040 DPRINT(("ignoring overflow notification, owner is zombie\n"));
5044 DPRINT(("waking up somebody\n"));
5046 if (msg) wake_up_interruptible(&ctx->ctx_msgq_wait);
5049 * safe, we are not in intr handler, nor in ctxsw when
5052 kill_fasync (&ctx->ctx_async_queue, SIGIO, POLL_IN);
5058 pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds)
5060 pfm_msg_t *msg = NULL;
5062 if (ctx->ctx_fl_no_msg == 0) {
5063 msg = pfm_get_new_msg(ctx);
5065 printk(KERN_ERR "perfmon: pfm_ovfl_notify_user no more notification msgs\n");
5069 msg->pfm_ovfl_msg.msg_type = PFM_MSG_OVFL;
5070 msg->pfm_ovfl_msg.msg_ctx_fd = ctx->ctx_fd;
5071 msg->pfm_ovfl_msg.msg_active_set = 0;
5072 msg->pfm_ovfl_msg.msg_ovfl_pmds[0] = ovfl_pmds;
5073 msg->pfm_ovfl_msg.msg_ovfl_pmds[1] = 0UL;
5074 msg->pfm_ovfl_msg.msg_ovfl_pmds[2] = 0UL;
5075 msg->pfm_ovfl_msg.msg_ovfl_pmds[3] = 0UL;
5076 msg->pfm_ovfl_msg.msg_tstamp = 0UL;
5079 DPRINT(("ovfl msg: msg=%p no_msg=%d fd=%d ovfl_pmds=0x%lx\n",
5085 return pfm_notify_user(ctx, msg);
5089 pfm_end_notify_user(pfm_context_t *ctx)
5093 msg = pfm_get_new_msg(ctx);
5095 printk(KERN_ERR "perfmon: pfm_end_notify_user no more notification msgs\n");
5099 memset(msg, 0, sizeof(*msg));
5101 msg->pfm_end_msg.msg_type = PFM_MSG_END;
5102 msg->pfm_end_msg.msg_ctx_fd = ctx->ctx_fd;
5103 msg->pfm_ovfl_msg.msg_tstamp = 0UL;
5105 DPRINT(("end msg: msg=%p no_msg=%d ctx_fd=%d\n",
5110 return pfm_notify_user(ctx, msg);
5114 * main overflow processing routine.
5115 * it can be called from the interrupt path or explicitly during the context switch code
5117 static void pfm_overflow_handler(struct task_struct *task, pfm_context_t *ctx,
5118 unsigned long pmc0, struct pt_regs *regs)
5120 pfm_ovfl_arg_t *ovfl_arg;
5122 unsigned long old_val, ovfl_val, new_val;
5123 unsigned long ovfl_notify = 0UL, ovfl_pmds = 0UL, smpl_pmds = 0UL, reset_pmds;
5124 unsigned long tstamp;
5125 pfm_ovfl_ctrl_t ovfl_ctrl;
5126 unsigned int i, has_smpl;
5127 int must_notify = 0;
5129 if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) goto stop_monitoring;
5132 * sanity test. Should never happen
5134 if (unlikely((pmc0 & 0x1) == 0)) goto sanity_check;
5136 tstamp = ia64_get_itc();
5137 mask = pmc0 >> PMU_FIRST_COUNTER;
5138 ovfl_val = pmu_conf->ovfl_val;
5139 has_smpl = CTX_HAS_SMPL(ctx);
5141 DPRINT_ovfl(("pmc0=0x%lx pid=%d iip=0x%lx, %s "
5142 "used_pmds=0x%lx\n",
5144 task ? task_pid_nr(task): -1,
5145 (regs ? regs->cr_iip : 0),
5146 CTX_OVFL_NOBLOCK(ctx) ? "nonblocking" : "blocking",
5147 ctx->ctx_used_pmds[0]));
5151 * first we update the virtual counters
5152 * assume there was a prior ia64_srlz_d() issued
5154 for (i = PMU_FIRST_COUNTER; mask ; i++, mask >>= 1) {
5156 /* skip pmd which did not overflow */
5157 if ((mask & 0x1) == 0) continue;
5160 * Note that the pmd is not necessarily 0 at this point as qualified events
5161 * may have happened before the PMU was frozen. The residual count is not
5162 * taken into consideration here but will be with any read of the pmd via
5165 old_val = new_val = ctx->ctx_pmds[i].val;
5166 new_val += 1 + ovfl_val;
5167 ctx->ctx_pmds[i].val = new_val;
5170 * check for overflow condition
5172 if (likely(old_val > new_val)) {
5173 ovfl_pmds |= 1UL << i;
5174 if (PMC_OVFL_NOTIFY(ctx, i)) ovfl_notify |= 1UL << i;
5177 DPRINT_ovfl(("ctx_pmd[%d].val=0x%lx old_val=0x%lx pmd=0x%lx ovfl_pmds=0x%lx ovfl_notify=0x%lx\n",
5181 ia64_get_pmd(i) & ovfl_val,
5187 * there was no 64-bit overflow, nothing else to do
5189 if (ovfl_pmds == 0UL) return;
5192 * reset all control bits
5198 * if a sampling format module exists, then we "cache" the overflow by
5199 * calling the module's handler() routine.
5202 unsigned long start_cycles, end_cycles;
5203 unsigned long pmd_mask;
5205 int this_cpu = smp_processor_id();
5207 pmd_mask = ovfl_pmds >> PMU_FIRST_COUNTER;
5208 ovfl_arg = &ctx->ctx_ovfl_arg;
5210 prefetch(ctx->ctx_smpl_hdr);
5212 for(i=PMU_FIRST_COUNTER; pmd_mask && ret == 0; i++, pmd_mask >>=1) {
5216 if ((pmd_mask & 0x1) == 0) continue;
5218 ovfl_arg->ovfl_pmd = (unsigned char )i;
5219 ovfl_arg->ovfl_notify = ovfl_notify & mask ? 1 : 0;
5220 ovfl_arg->active_set = 0;
5221 ovfl_arg->ovfl_ctrl.val = 0; /* module must fill in all fields */
5222 ovfl_arg->smpl_pmds[0] = smpl_pmds = ctx->ctx_pmds[i].smpl_pmds[0];
5224 ovfl_arg->pmd_value = ctx->ctx_pmds[i].val;
5225 ovfl_arg->pmd_last_reset = ctx->ctx_pmds[i].lval;
5226 ovfl_arg->pmd_eventid = ctx->ctx_pmds[i].eventid;
5229 * copy values of pmds of interest. Sampling format may copy them
5230 * into sampling buffer.
5233 for(j=0, k=0; smpl_pmds; j++, smpl_pmds >>=1) {
5234 if ((smpl_pmds & 0x1) == 0) continue;
5235 ovfl_arg->smpl_pmds_values[k++] = PMD_IS_COUNTING(j) ? pfm_read_soft_counter(ctx, j) : ia64_get_pmd(j);
5236 DPRINT_ovfl(("smpl_pmd[%d]=pmd%u=0x%lx\n", k-1, j, ovfl_arg->smpl_pmds_values[k-1]));
5240 pfm_stats[this_cpu].pfm_smpl_handler_calls++;
5242 start_cycles = ia64_get_itc();
5245 * call custom buffer format record (handler) routine
5247 ret = (*ctx->ctx_buf_fmt->fmt_handler)(task, ctx->ctx_smpl_hdr, ovfl_arg, regs, tstamp);
5249 end_cycles = ia64_get_itc();
5252 * For those controls, we take the union because they have
5253 * an all or nothing behavior.
5255 ovfl_ctrl.bits.notify_user |= ovfl_arg->ovfl_ctrl.bits.notify_user;
5256 ovfl_ctrl.bits.block_task |= ovfl_arg->ovfl_ctrl.bits.block_task;
5257 ovfl_ctrl.bits.mask_monitoring |= ovfl_arg->ovfl_ctrl.bits.mask_monitoring;
5259 * build the bitmask of pmds to reset now
5261 if (ovfl_arg->ovfl_ctrl.bits.reset_ovfl_pmds) reset_pmds |= mask;
5263 pfm_stats[this_cpu].pfm_smpl_handler_cycles += end_cycles - start_cycles;
5266 * when the module cannot handle the rest of the overflows, we abort right here
5268 if (ret && pmd_mask) {
5269 DPRINT(("handler aborts leftover ovfl_pmds=0x%lx\n",
5270 pmd_mask<<PMU_FIRST_COUNTER));
5273 * remove the pmds we reset now from the set of pmds to reset in pfm_restart()
5275 ovfl_pmds &= ~reset_pmds;
5278 * when no sampling module is used, then the default
5279 * is to notify on overflow if requested by user
5281 ovfl_ctrl.bits.notify_user = ovfl_notify ? 1 : 0;
5282 ovfl_ctrl.bits.block_task = ovfl_notify ? 1 : 0;
5283 ovfl_ctrl.bits.mask_monitoring = ovfl_notify ? 1 : 0; /* XXX: change for saturation */
5284 ovfl_ctrl.bits.reset_ovfl_pmds = ovfl_notify ? 0 : 1;
5286 * if needed, we reset all overflowed pmds
5288 if (ovfl_notify == 0) reset_pmds = ovfl_pmds;
5291 DPRINT_ovfl(("ovfl_pmds=0x%lx reset_pmds=0x%lx\n", ovfl_pmds, reset_pmds));
5294 * reset the requested PMD registers using the short reset values
5297 unsigned long bm = reset_pmds;
5298 pfm_reset_regs(ctx, &bm, PFM_PMD_SHORT_RESET);
5301 if (ovfl_notify && ovfl_ctrl.bits.notify_user) {
5303 * keep track of what to reset when unblocking
5305 ctx->ctx_ovfl_regs[0] = ovfl_pmds;
5308 * check for blocking context
5310 if (CTX_OVFL_NOBLOCK(ctx) == 0 && ovfl_ctrl.bits.block_task) {
5312 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_BLOCK;
5315 * set the perfmon specific checking pending work for the task
5317 PFM_SET_WORK_PENDING(task, 1);
5320 * when coming from ctxsw, current still points to the
5321 * previous task, therefore we must work with task and not current.
5323 set_notify_resume(task);
5326 * defer until state is changed (shorten spin window). the context is locked
5327 * anyway, so the signal receiver would come spin for nothing.
5332 DPRINT_ovfl(("owner [%d] pending=%ld reason=%u ovfl_pmds=0x%lx ovfl_notify=0x%lx masked=%d\n",
5333 GET_PMU_OWNER() ? task_pid_nr(GET_PMU_OWNER()) : -1,
5334 PFM_GET_WORK_PENDING(task),
5335 ctx->ctx_fl_trap_reason,
5338 ovfl_ctrl.bits.mask_monitoring ? 1 : 0));
5340 * in case monitoring must be stopped, we toggle the psr bits
5342 if (ovfl_ctrl.bits.mask_monitoring) {
5343 pfm_mask_monitoring(task);
5344 ctx->ctx_state = PFM_CTX_MASKED;
5345 ctx->ctx_fl_can_restart = 1;
5349 * send notification now
5351 if (must_notify) pfm_ovfl_notify_user(ctx, ovfl_notify);
5356 printk(KERN_ERR "perfmon: CPU%d overflow handler [%d] pmc0=0x%lx\n",
5358 task ? task_pid_nr(task) : -1,
5364 * in SMP, zombie context is never restored but reclaimed in pfm_load_regs().
5365 * Moreover, zombies are also reclaimed in pfm_save_regs(). Therefore we can
5366 * come here as zombie only if the task is the current task. In which case, we
5367 * can access the PMU hardware directly.
5369 * Note that zombies do have PM_VALID set. So here we do the minimal.
5371 * In case the context was zombified it could not be reclaimed at the time
5372 * the monitoring program exited. At this point, the PMU reservation has been
5373 * returned, the sampiing buffer has been freed. We must convert this call
5374 * into a spurious interrupt. However, we must also avoid infinite overflows
5375 * by stopping monitoring for this task. We can only come here for a per-task
5376 * context. All we need to do is to stop monitoring using the psr bits which
5377 * are always task private. By re-enabling secure montioring, we ensure that
5378 * the monitored task will not be able to re-activate monitoring.
5379 * The task will eventually be context switched out, at which point the context
5380 * will be reclaimed (that includes releasing ownership of the PMU).
5382 * So there might be a window of time where the number of per-task session is zero
5383 * yet one PMU might have a owner and get at most one overflow interrupt for a zombie
5384 * context. This is safe because if a per-task session comes in, it will push this one
5385 * out and by the virtue on pfm_save_regs(), this one will disappear. If a system wide
5386 * session is force on that CPU, given that we use task pinning, pfm_save_regs() will
5387 * also push our zombie context out.
5389 * Overall pretty hairy stuff....
5391 DPRINT(("ctx is zombie for [%d], converted to spurious\n", task ? task_pid_nr(task): -1));
5393 ia64_psr(regs)->up = 0;
5394 ia64_psr(regs)->sp = 1;
5399 pfm_do_interrupt_handler(void *arg, struct pt_regs *regs)
5401 struct task_struct *task;
5403 unsigned long flags;
5405 int this_cpu = smp_processor_id();
5408 pfm_stats[this_cpu].pfm_ovfl_intr_count++;
5411 * srlz.d done before arriving here
5413 pmc0 = ia64_get_pmc(0);
5415 task = GET_PMU_OWNER();
5416 ctx = GET_PMU_CTX();
5419 * if we have some pending bits set
5420 * assumes : if any PMC0.bit[63-1] is set, then PMC0.fr = 1
5422 if (PMC0_HAS_OVFL(pmc0) && task) {
5424 * we assume that pmc0.fr is always set here
5428 if (!ctx) goto report_spurious1;
5430 if (ctx->ctx_fl_system == 0 && (task->thread.flags & IA64_THREAD_PM_VALID) == 0)
5431 goto report_spurious2;
5433 PROTECT_CTX_NOPRINT(ctx, flags);
5435 pfm_overflow_handler(task, ctx, pmc0, regs);
5437 UNPROTECT_CTX_NOPRINT(ctx, flags);
5440 pfm_stats[this_cpu].pfm_spurious_ovfl_intr_count++;
5444 * keep it unfrozen at all times
5451 printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d has no PFM context\n",
5452 this_cpu, task_pid_nr(task));
5456 printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d, invalid flag\n",
5464 pfm_interrupt_handler(int irq, void *arg)
5466 unsigned long start_cycles, total_cycles;
5467 unsigned long min, max;
5470 struct pt_regs *regs = get_irq_regs();
5472 this_cpu = get_cpu();
5473 if (likely(!pfm_alt_intr_handler)) {
5474 min = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min;
5475 max = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max;
5477 start_cycles = ia64_get_itc();
5479 ret = pfm_do_interrupt_handler(arg, regs);
5481 total_cycles = ia64_get_itc();
5484 * don't measure spurious interrupts
5486 if (likely(ret == 0)) {
5487 total_cycles -= start_cycles;
5489 if (total_cycles < min) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min = total_cycles;
5490 if (total_cycles > max) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max = total_cycles;
5492 pfm_stats[this_cpu].pfm_ovfl_intr_cycles += total_cycles;
5496 (*pfm_alt_intr_handler->handler)(irq, arg, regs);
5504 * /proc/perfmon interface, for debug only
5507 #define PFM_PROC_SHOW_HEADER ((void *)(long)nr_cpu_ids+1)
5510 pfm_proc_start(struct seq_file *m, loff_t *pos)
5513 return PFM_PROC_SHOW_HEADER;
5516 while (*pos <= nr_cpu_ids) {
5517 if (cpu_online(*pos - 1)) {
5518 return (void *)*pos;
5526 pfm_proc_next(struct seq_file *m, void *v, loff_t *pos)
5529 return pfm_proc_start(m, pos);
5533 pfm_proc_stop(struct seq_file *m, void *v)
5538 pfm_proc_show_header(struct seq_file *m)
5540 struct list_head * pos;
5541 pfm_buffer_fmt_t * entry;
5542 unsigned long flags;
5545 "perfmon version : %u.%u\n"
5548 "expert mode : %s\n"
5549 "ovfl_mask : 0x%lx\n"
5550 "PMU flags : 0x%x\n",
5551 PFM_VERSION_MAJ, PFM_VERSION_MIN,
5553 pfm_sysctl.fastctxsw > 0 ? "Yes": "No",
5554 pfm_sysctl.expert_mode > 0 ? "Yes": "No",
5561 "proc_sessions : %u\n"
5562 "sys_sessions : %u\n"
5563 "sys_use_dbregs : %u\n"
5564 "ptrace_use_dbregs : %u\n",
5565 pfm_sessions.pfs_task_sessions,
5566 pfm_sessions.pfs_sys_sessions,
5567 pfm_sessions.pfs_sys_use_dbregs,
5568 pfm_sessions.pfs_ptrace_use_dbregs);
5572 spin_lock(&pfm_buffer_fmt_lock);
5574 list_for_each(pos, &pfm_buffer_fmt_list) {
5575 entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
5576 seq_printf(m, "format : %16phD %s\n",
5577 entry->fmt_uuid, entry->fmt_name);
5579 spin_unlock(&pfm_buffer_fmt_lock);
5584 pfm_proc_show(struct seq_file *m, void *v)
5590 if (v == PFM_PROC_SHOW_HEADER) {
5591 pfm_proc_show_header(m);
5595 /* show info for CPU (v - 1) */
5599 "CPU%-2d overflow intrs : %lu\n"
5600 "CPU%-2d overflow cycles : %lu\n"
5601 "CPU%-2d overflow min : %lu\n"
5602 "CPU%-2d overflow max : %lu\n"
5603 "CPU%-2d smpl handler calls : %lu\n"
5604 "CPU%-2d smpl handler cycles : %lu\n"
5605 "CPU%-2d spurious intrs : %lu\n"
5606 "CPU%-2d replay intrs : %lu\n"
5607 "CPU%-2d syst_wide : %d\n"
5608 "CPU%-2d dcr_pp : %d\n"
5609 "CPU%-2d exclude idle : %d\n"
5610 "CPU%-2d owner : %d\n"
5611 "CPU%-2d context : %p\n"
5612 "CPU%-2d activations : %lu\n",
5613 cpu, pfm_stats[cpu].pfm_ovfl_intr_count,
5614 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles,
5615 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_min,
5616 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_max,
5617 cpu, pfm_stats[cpu].pfm_smpl_handler_calls,
5618 cpu, pfm_stats[cpu].pfm_smpl_handler_cycles,
5619 cpu, pfm_stats[cpu].pfm_spurious_ovfl_intr_count,
5620 cpu, pfm_stats[cpu].pfm_replay_ovfl_intr_count,
5621 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_SYST_WIDE ? 1 : 0,
5622 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_DCR_PP ? 1 : 0,
5623 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_EXCL_IDLE ? 1 : 0,
5624 cpu, pfm_get_cpu_data(pmu_owner, cpu) ? pfm_get_cpu_data(pmu_owner, cpu)->pid: -1,
5625 cpu, pfm_get_cpu_data(pmu_ctx, cpu),
5626 cpu, pfm_get_cpu_data(pmu_activation_number, cpu));
5628 if (num_online_cpus() == 1 && pfm_sysctl.debug > 0) {
5630 psr = pfm_get_psr();
5635 "CPU%-2d psr : 0x%lx\n"
5636 "CPU%-2d pmc0 : 0x%lx\n",
5638 cpu, ia64_get_pmc(0));
5640 for (i=0; PMC_IS_LAST(i) == 0; i++) {
5641 if (PMC_IS_COUNTING(i) == 0) continue;
5643 "CPU%-2d pmc%u : 0x%lx\n"
5644 "CPU%-2d pmd%u : 0x%lx\n",
5645 cpu, i, ia64_get_pmc(i),
5646 cpu, i, ia64_get_pmd(i));
5652 const struct seq_operations pfm_seq_ops = {
5653 .start = pfm_proc_start,
5654 .next = pfm_proc_next,
5655 .stop = pfm_proc_stop,
5656 .show = pfm_proc_show
5660 * we come here as soon as local_cpu_data->pfm_syst_wide is set. this happens
5661 * during pfm_enable() hence before pfm_start(). We cannot assume monitoring
5662 * is active or inactive based on mode. We must rely on the value in
5663 * local_cpu_data->pfm_syst_info
5666 pfm_syst_wide_update_task(struct task_struct *task, unsigned long info, int is_ctxswin)
5668 struct pt_regs *regs;
5670 unsigned long dcr_pp;
5672 dcr_pp = info & PFM_CPUINFO_DCR_PP ? 1 : 0;
5675 * pid 0 is guaranteed to be the idle task. There is one such task with pid 0
5676 * on every CPU, so we can rely on the pid to identify the idle task.
5678 if ((info & PFM_CPUINFO_EXCL_IDLE) == 0 || task->pid) {
5679 regs = task_pt_regs(task);
5680 ia64_psr(regs)->pp = is_ctxswin ? dcr_pp : 0;
5684 * if monitoring has started
5687 dcr = ia64_getreg(_IA64_REG_CR_DCR);
5689 * context switching in?
5692 /* mask monitoring for the idle task */
5693 ia64_setreg(_IA64_REG_CR_DCR, dcr & ~IA64_DCR_PP);
5699 * context switching out
5700 * restore monitoring for next task
5702 * Due to inlining this odd if-then-else construction generates
5705 ia64_setreg(_IA64_REG_CR_DCR, dcr |IA64_DCR_PP);
5714 pfm_force_cleanup(pfm_context_t *ctx, struct pt_regs *regs)
5716 struct task_struct *task = ctx->ctx_task;
5718 ia64_psr(regs)->up = 0;
5719 ia64_psr(regs)->sp = 1;
5721 if (GET_PMU_OWNER() == task) {
5722 DPRINT(("cleared ownership for [%d]\n",
5723 task_pid_nr(ctx->ctx_task)));
5724 SET_PMU_OWNER(NULL, NULL);
5728 * disconnect the task from the context and vice-versa
5730 PFM_SET_WORK_PENDING(task, 0);
5732 task->thread.pfm_context = NULL;
5733 task->thread.flags &= ~IA64_THREAD_PM_VALID;
5735 DPRINT(("force cleanup for [%d]\n", task_pid_nr(task)));
5740 * in 2.6, interrupts are masked when we come here and the runqueue lock is held
5743 pfm_save_regs(struct task_struct *task)
5746 unsigned long flags;
5750 ctx = PFM_GET_CTX(task);
5751 if (ctx == NULL) return;
5754 * we always come here with interrupts ALREADY disabled by
5755 * the scheduler. So we simply need to protect against concurrent
5756 * access, not CPU concurrency.
5758 flags = pfm_protect_ctx_ctxsw(ctx);
5760 if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
5761 struct pt_regs *regs = task_pt_regs(task);
5765 pfm_force_cleanup(ctx, regs);
5767 BUG_ON(ctx->ctx_smpl_hdr);
5769 pfm_unprotect_ctx_ctxsw(ctx, flags);
5771 pfm_context_free(ctx);
5776 * save current PSR: needed because we modify it
5779 psr = pfm_get_psr();
5781 BUG_ON(psr & (IA64_PSR_I));
5785 * This is the last instruction which may generate an overflow
5787 * We do not need to set psr.sp because, it is irrelevant in kernel.
5788 * It will be restored from ipsr when going back to user level
5793 * keep a copy of psr.up (for reload)
5795 ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
5798 * release ownership of this PMU.
5799 * PM interrupts are masked, so nothing
5802 SET_PMU_OWNER(NULL, NULL);
5805 * we systematically save the PMD as we have no
5806 * guarantee we will be schedule at that same
5809 pfm_save_pmds(ctx->th_pmds, ctx->ctx_used_pmds[0]);
5812 * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
5813 * we will need it on the restore path to check
5814 * for pending overflow.
5816 ctx->th_pmcs[0] = ia64_get_pmc(0);
5819 * unfreeze PMU if had pending overflows
5821 if (ctx->th_pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
5824 * finally, allow context access.
5825 * interrupts will still be masked after this call.
5827 pfm_unprotect_ctx_ctxsw(ctx, flags);
5830 #else /* !CONFIG_SMP */
5832 pfm_save_regs(struct task_struct *task)
5837 ctx = PFM_GET_CTX(task);
5838 if (ctx == NULL) return;
5841 * save current PSR: needed because we modify it
5843 psr = pfm_get_psr();
5845 BUG_ON(psr & (IA64_PSR_I));
5849 * This is the last instruction which may generate an overflow
5851 * We do not need to set psr.sp because, it is irrelevant in kernel.
5852 * It will be restored from ipsr when going back to user level
5857 * keep a copy of psr.up (for reload)
5859 ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
5863 pfm_lazy_save_regs (struct task_struct *task)
5866 unsigned long flags;
5868 { u64 psr = pfm_get_psr();
5869 BUG_ON(psr & IA64_PSR_UP);
5872 ctx = PFM_GET_CTX(task);
5875 * we need to mask PMU overflow here to
5876 * make sure that we maintain pmc0 until
5877 * we save it. overflow interrupts are
5878 * treated as spurious if there is no
5881 * XXX: I don't think this is necessary
5883 PROTECT_CTX(ctx,flags);
5886 * release ownership of this PMU.
5887 * must be done before we save the registers.
5889 * after this call any PMU interrupt is treated
5892 SET_PMU_OWNER(NULL, NULL);
5895 * save all the pmds we use
5897 pfm_save_pmds(ctx->th_pmds, ctx->ctx_used_pmds[0]);
5900 * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
5901 * it is needed to check for pended overflow
5902 * on the restore path
5904 ctx->th_pmcs[0] = ia64_get_pmc(0);
5907 * unfreeze PMU if had pending overflows
5909 if (ctx->th_pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
5912 * now get can unmask PMU interrupts, they will
5913 * be treated as purely spurious and we will not
5914 * lose any information
5916 UNPROTECT_CTX(ctx,flags);
5918 #endif /* CONFIG_SMP */
5922 * in 2.6, interrupts are masked when we come here and the runqueue lock is held
5925 pfm_load_regs (struct task_struct *task)
5928 unsigned long pmc_mask = 0UL, pmd_mask = 0UL;
5929 unsigned long flags;
5931 int need_irq_resend;
5933 ctx = PFM_GET_CTX(task);
5934 if (unlikely(ctx == NULL)) return;
5936 BUG_ON(GET_PMU_OWNER());
5939 * possible on unload
5941 if (unlikely((task->thread.flags & IA64_THREAD_PM_VALID) == 0)) return;
5944 * we always come here with interrupts ALREADY disabled by
5945 * the scheduler. So we simply need to protect against concurrent
5946 * access, not CPU concurrency.
5948 flags = pfm_protect_ctx_ctxsw(ctx);
5949 psr = pfm_get_psr();
5951 need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
5953 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
5954 BUG_ON(psr & IA64_PSR_I);
5956 if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) {
5957 struct pt_regs *regs = task_pt_regs(task);
5959 BUG_ON(ctx->ctx_smpl_hdr);
5961 pfm_force_cleanup(ctx, regs);
5963 pfm_unprotect_ctx_ctxsw(ctx, flags);
5966 * this one (kmalloc'ed) is fine with interrupts disabled
5968 pfm_context_free(ctx);
5974 * we restore ALL the debug registers to avoid picking up
5977 if (ctx->ctx_fl_using_dbreg) {
5978 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
5979 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
5982 * retrieve saved psr.up
5984 psr_up = ctx->ctx_saved_psr_up;
5987 * if we were the last user of the PMU on that CPU,
5988 * then nothing to do except restore psr
5990 if (GET_LAST_CPU(ctx) == smp_processor_id() && ctx->ctx_last_activation == GET_ACTIVATION()) {
5993 * retrieve partial reload masks (due to user modifications)
5995 pmc_mask = ctx->ctx_reload_pmcs[0];
5996 pmd_mask = ctx->ctx_reload_pmds[0];
6000 * To avoid leaking information to the user level when psr.sp=0,
6001 * we must reload ALL implemented pmds (even the ones we don't use).
6002 * In the kernel we only allow PFM_READ_PMDS on registers which
6003 * we initialized or requested (sampling) so there is no risk there.
6005 pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
6008 * ALL accessible PMCs are systematically reloaded, unused registers
6009 * get their default (from pfm_reset_pmu_state()) values to avoid picking
6010 * up stale configuration.
6012 * PMC0 is never in the mask. It is always restored separately.
6014 pmc_mask = ctx->ctx_all_pmcs[0];
6017 * when context is MASKED, we will restore PMC with plm=0
6018 * and PMD with stale information, but that's ok, nothing
6021 * XXX: optimize here
6023 if (pmd_mask) pfm_restore_pmds(ctx->th_pmds, pmd_mask);
6024 if (pmc_mask) pfm_restore_pmcs(ctx->th_pmcs, pmc_mask);
6027 * check for pending overflow at the time the state
6030 if (unlikely(PMC0_HAS_OVFL(ctx->th_pmcs[0]))) {
6032 * reload pmc0 with the overflow information
6033 * On McKinley PMU, this will trigger a PMU interrupt
6035 ia64_set_pmc(0, ctx->th_pmcs[0]);
6037 ctx->th_pmcs[0] = 0UL;
6040 * will replay the PMU interrupt
6042 if (need_irq_resend) ia64_resend_irq(IA64_PERFMON_VECTOR);
6044 pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
6048 * we just did a reload, so we reset the partial reload fields
6050 ctx->ctx_reload_pmcs[0] = 0UL;
6051 ctx->ctx_reload_pmds[0] = 0UL;
6053 SET_LAST_CPU(ctx, smp_processor_id());
6056 * dump activation value for this PMU
6060 * record current activation for this context
6062 SET_ACTIVATION(ctx);
6065 * establish new ownership.
6067 SET_PMU_OWNER(task, ctx);
6070 * restore the psr.up bit. measurement
6072 * no PMU interrupt can happen at this point
6073 * because we still have interrupts disabled.
6075 if (likely(psr_up)) pfm_set_psr_up();
6078 * allow concurrent access to context
6080 pfm_unprotect_ctx_ctxsw(ctx, flags);
6082 #else /* !CONFIG_SMP */
6084 * reload PMU state for UP kernels
6085 * in 2.5 we come here with interrupts disabled
6088 pfm_load_regs (struct task_struct *task)
6091 struct task_struct *owner;
6092 unsigned long pmd_mask, pmc_mask;
6094 int need_irq_resend;
6096 owner = GET_PMU_OWNER();
6097 ctx = PFM_GET_CTX(task);
6098 psr = pfm_get_psr();
6100 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
6101 BUG_ON(psr & IA64_PSR_I);
6104 * we restore ALL the debug registers to avoid picking up
6107 * This must be done even when the task is still the owner
6108 * as the registers may have been modified via ptrace()
6109 * (not perfmon) by the previous task.
6111 if (ctx->ctx_fl_using_dbreg) {
6112 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
6113 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
6117 * retrieved saved psr.up
6119 psr_up = ctx->ctx_saved_psr_up;
6120 need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
6123 * short path, our state is still there, just
6124 * need to restore psr and we go
6126 * we do not touch either PMC nor PMD. the psr is not touched
6127 * by the overflow_handler. So we are safe w.r.t. to interrupt
6128 * concurrency even without interrupt masking.
6130 if (likely(owner == task)) {
6131 if (likely(psr_up)) pfm_set_psr_up();
6136 * someone else is still using the PMU, first push it out and
6137 * then we'll be able to install our stuff !
6139 * Upon return, there will be no owner for the current PMU
6141 if (owner) pfm_lazy_save_regs(owner);
6144 * To avoid leaking information to the user level when psr.sp=0,
6145 * we must reload ALL implemented pmds (even the ones we don't use).
6146 * In the kernel we only allow PFM_READ_PMDS on registers which
6147 * we initialized or requested (sampling) so there is no risk there.
6149 pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
6152 * ALL accessible PMCs are systematically reloaded, unused registers
6153 * get their default (from pfm_reset_pmu_state()) values to avoid picking
6154 * up stale configuration.
6156 * PMC0 is never in the mask. It is always restored separately
6158 pmc_mask = ctx->ctx_all_pmcs[0];
6160 pfm_restore_pmds(ctx->th_pmds, pmd_mask);
6161 pfm_restore_pmcs(ctx->th_pmcs, pmc_mask);
6164 * check for pending overflow at the time the state
6167 if (unlikely(PMC0_HAS_OVFL(ctx->th_pmcs[0]))) {
6169 * reload pmc0 with the overflow information
6170 * On McKinley PMU, this will trigger a PMU interrupt
6172 ia64_set_pmc(0, ctx->th_pmcs[0]);
6175 ctx->th_pmcs[0] = 0UL;
6178 * will replay the PMU interrupt
6180 if (need_irq_resend) ia64_resend_irq(IA64_PERFMON_VECTOR);
6182 pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
6186 * establish new ownership.
6188 SET_PMU_OWNER(task, ctx);
6191 * restore the psr.up bit. measurement
6193 * no PMU interrupt can happen at this point
6194 * because we still have interrupts disabled.
6196 if (likely(psr_up)) pfm_set_psr_up();
6198 #endif /* CONFIG_SMP */
6201 * this function assumes monitoring is stopped
6204 pfm_flush_pmds(struct task_struct *task, pfm_context_t *ctx)
6207 unsigned long mask2, val, pmd_val, ovfl_val;
6208 int i, can_access_pmu = 0;
6212 * is the caller the task being monitored (or which initiated the
6213 * session for system wide measurements)
6215 is_self = ctx->ctx_task == task ? 1 : 0;
6218 * can access PMU is task is the owner of the PMU state on the current CPU
6219 * or if we are running on the CPU bound to the context in system-wide mode
6220 * (that is not necessarily the task the context is attached to in this mode).
6221 * In system-wide we always have can_access_pmu true because a task running on an
6222 * invalid processor is flagged earlier in the call stack (see pfm_stop).
6224 can_access_pmu = (GET_PMU_OWNER() == task) || (ctx->ctx_fl_system && ctx->ctx_cpu == smp_processor_id());
6225 if (can_access_pmu) {
6227 * Mark the PMU as not owned
6228 * This will cause the interrupt handler to do nothing in case an overflow
6229 * interrupt was in-flight
6230 * This also guarantees that pmc0 will contain the final state
6231 * It virtually gives us full control on overflow processing from that point
6234 SET_PMU_OWNER(NULL, NULL);
6235 DPRINT(("releasing ownership\n"));
6238 * read current overflow status:
6240 * we are guaranteed to read the final stable state
6243 pmc0 = ia64_get_pmc(0); /* slow */
6246 * reset freeze bit, overflow status information destroyed
6250 pmc0 = ctx->th_pmcs[0];
6252 * clear whatever overflow status bits there were
6254 ctx->th_pmcs[0] = 0;
6256 ovfl_val = pmu_conf->ovfl_val;
6258 * we save all the used pmds
6259 * we take care of overflows for counting PMDs
6261 * XXX: sampling situation is not taken into account here
6263 mask2 = ctx->ctx_used_pmds[0];
6265 DPRINT(("is_self=%d ovfl_val=0x%lx mask2=0x%lx\n", is_self, ovfl_val, mask2));
6267 for (i = 0; mask2; i++, mask2>>=1) {
6269 /* skip non used pmds */
6270 if ((mask2 & 0x1) == 0) continue;
6273 * can access PMU always true in system wide mode
6275 val = pmd_val = can_access_pmu ? ia64_get_pmd(i) : ctx->th_pmds[i];
6277 if (PMD_IS_COUNTING(i)) {
6278 DPRINT(("[%d] pmd[%d] ctx_pmd=0x%lx hw_pmd=0x%lx\n",
6281 ctx->ctx_pmds[i].val,
6285 * we rebuild the full 64 bit value of the counter
6287 val = ctx->ctx_pmds[i].val + (val & ovfl_val);
6290 * now everything is in ctx_pmds[] and we need
6291 * to clear the saved context from save_regs() such that
6292 * pfm_read_pmds() gets the correct value
6297 * take care of overflow inline
6299 if (pmc0 & (1UL << i)) {
6300 val += 1 + ovfl_val;
6301 DPRINT(("[%d] pmd[%d] overflowed\n", task_pid_nr(task), i));
6305 DPRINT(("[%d] ctx_pmd[%d]=0x%lx pmd_val=0x%lx\n", task_pid_nr(task), i, val, pmd_val));
6307 if (is_self) ctx->th_pmds[i] = pmd_val;
6309 ctx->ctx_pmds[i].val = val;
6313 static struct irqaction perfmon_irqaction = {
6314 .handler = pfm_interrupt_handler,
6319 pfm_alt_save_pmu_state(void *data)
6321 struct pt_regs *regs;
6323 regs = task_pt_regs(current);
6325 DPRINT(("called\n"));
6328 * should not be necessary but
6329 * let's take not risk
6333 ia64_psr(regs)->pp = 0;
6336 * This call is required
6337 * May cause a spurious interrupt on some processors
6345 pfm_alt_restore_pmu_state(void *data)
6347 struct pt_regs *regs;
6349 regs = task_pt_regs(current);
6351 DPRINT(("called\n"));
6354 * put PMU back in state expected
6359 ia64_psr(regs)->pp = 0;
6362 * perfmon runs with PMU unfrozen at all times
6370 pfm_install_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
6375 /* some sanity checks */
6376 if (hdl == NULL || hdl->handler == NULL) return -EINVAL;
6378 /* do the easy test first */
6379 if (pfm_alt_intr_handler) return -EBUSY;
6381 /* one at a time in the install or remove, just fail the others */
6382 if (!spin_trylock(&pfm_alt_install_check)) {
6386 /* reserve our session */
6387 for_each_online_cpu(reserve_cpu) {
6388 ret = pfm_reserve_session(NULL, 1, reserve_cpu);
6389 if (ret) goto cleanup_reserve;
6392 /* save the current system wide pmu states */
6393 ret = on_each_cpu(pfm_alt_save_pmu_state, NULL, 1);
6395 DPRINT(("on_each_cpu() failed: %d\n", ret));
6396 goto cleanup_reserve;
6399 /* officially change to the alternate interrupt handler */
6400 pfm_alt_intr_handler = hdl;
6402 spin_unlock(&pfm_alt_install_check);
6407 for_each_online_cpu(i) {
6408 /* don't unreserve more than we reserved */
6409 if (i >= reserve_cpu) break;
6411 pfm_unreserve_session(NULL, 1, i);
6414 spin_unlock(&pfm_alt_install_check);
6418 EXPORT_SYMBOL_GPL(pfm_install_alt_pmu_interrupt);
6421 pfm_remove_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
6426 if (hdl == NULL) return -EINVAL;
6428 /* cannot remove someone else's handler! */
6429 if (pfm_alt_intr_handler != hdl) return -EINVAL;
6431 /* one at a time in the install or remove, just fail the others */
6432 if (!spin_trylock(&pfm_alt_install_check)) {
6436 pfm_alt_intr_handler = NULL;
6438 ret = on_each_cpu(pfm_alt_restore_pmu_state, NULL, 1);
6440 DPRINT(("on_each_cpu() failed: %d\n", ret));
6443 for_each_online_cpu(i) {
6444 pfm_unreserve_session(NULL, 1, i);
6447 spin_unlock(&pfm_alt_install_check);
6451 EXPORT_SYMBOL_GPL(pfm_remove_alt_pmu_interrupt);
6454 * perfmon initialization routine, called from the initcall() table
6456 static int init_pfm_fs(void);
6464 family = local_cpu_data->family;
6469 if ((*p)->probe() == 0) goto found;
6470 } else if ((*p)->pmu_family == family || (*p)->pmu_family == 0xff) {
6484 unsigned int n, n_counters, i;
6486 printk("perfmon: version %u.%u IRQ %u\n",
6489 IA64_PERFMON_VECTOR);
6491 if (pfm_probe_pmu()) {
6492 printk(KERN_INFO "perfmon: disabled, there is no support for processor family %d\n",
6493 local_cpu_data->family);
6498 * compute the number of implemented PMD/PMC from the
6499 * description tables
6502 for (i=0; PMC_IS_LAST(i) == 0; i++) {
6503 if (PMC_IS_IMPL(i) == 0) continue;
6504 pmu_conf->impl_pmcs[i>>6] |= 1UL << (i&63);
6507 pmu_conf->num_pmcs = n;
6509 n = 0; n_counters = 0;
6510 for (i=0; PMD_IS_LAST(i) == 0; i++) {
6511 if (PMD_IS_IMPL(i) == 0) continue;
6512 pmu_conf->impl_pmds[i>>6] |= 1UL << (i&63);
6514 if (PMD_IS_COUNTING(i)) n_counters++;
6516 pmu_conf->num_pmds = n;
6517 pmu_conf->num_counters = n_counters;
6520 * sanity checks on the number of debug registers
6522 if (pmu_conf->use_rr_dbregs) {
6523 if (pmu_conf->num_ibrs > IA64_NUM_DBG_REGS) {
6524 printk(KERN_INFO "perfmon: unsupported number of code debug registers (%u)\n", pmu_conf->num_ibrs);
6528 if (pmu_conf->num_dbrs > IA64_NUM_DBG_REGS) {
6529 printk(KERN_INFO "perfmon: unsupported number of data debug registers (%u)\n", pmu_conf->num_ibrs);
6535 printk("perfmon: %s PMU detected, %u PMCs, %u PMDs, %u counters (%lu bits)\n",
6539 pmu_conf->num_counters,
6540 ffz(pmu_conf->ovfl_val));
6543 if (pmu_conf->num_pmds >= PFM_NUM_PMD_REGS || pmu_conf->num_pmcs >= PFM_NUM_PMC_REGS) {
6544 printk(KERN_ERR "perfmon: not enough pmc/pmd, perfmon disabled\n");
6550 * create /proc/perfmon (mostly for debugging purposes)
6552 perfmon_dir = proc_create_seq("perfmon", S_IRUGO, NULL, &pfm_seq_ops);
6553 if (perfmon_dir == NULL) {
6554 printk(KERN_ERR "perfmon: cannot create /proc entry, perfmon disabled\n");
6560 * create /proc/sys/kernel/perfmon (for debugging purposes)
6562 pfm_sysctl_header = register_sysctl_table(pfm_sysctl_root);
6565 * initialize all our spinlocks
6567 spin_lock_init(&pfm_sessions.pfs_lock);
6568 spin_lock_init(&pfm_buffer_fmt_lock);
6572 for(i=0; i < NR_CPUS; i++) pfm_stats[i].pfm_ovfl_intr_cycles_min = ~0UL;
6577 __initcall(pfm_init);
6580 * this function is called before pfm_init()
6583 pfm_init_percpu (void)
6585 static int first_time=1;
6587 * make sure no measurement is active
6588 * (may inherit programmed PMCs from EFI).
6594 * we run with the PMU not frozen at all times
6599 register_percpu_irq(IA64_PERFMON_VECTOR, &perfmon_irqaction);
6603 ia64_setreg(_IA64_REG_CR_PMV, IA64_PERFMON_VECTOR);
6608 * used for debug purposes only
6611 dump_pmu_state(const char *from)
6613 struct task_struct *task;
6614 struct pt_regs *regs;
6616 unsigned long psr, dcr, info, flags;
6619 local_irq_save(flags);
6621 this_cpu = smp_processor_id();
6622 regs = task_pt_regs(current);
6623 info = PFM_CPUINFO_GET();
6624 dcr = ia64_getreg(_IA64_REG_CR_DCR);
6626 if (info == 0 && ia64_psr(regs)->pp == 0 && (dcr & IA64_DCR_PP) == 0) {
6627 local_irq_restore(flags);
6631 printk("CPU%d from %s() current [%d] iip=0x%lx %s\n",
6634 task_pid_nr(current),
6638 task = GET_PMU_OWNER();
6639 ctx = GET_PMU_CTX();
6641 printk("->CPU%d owner [%d] ctx=%p\n", this_cpu, task ? task_pid_nr(task) : -1, ctx);
6643 psr = pfm_get_psr();
6645 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",
6648 psr & IA64_PSR_PP ? 1 : 0,
6649 psr & IA64_PSR_UP ? 1 : 0,
6650 dcr & IA64_DCR_PP ? 1 : 0,
6653 ia64_psr(regs)->pp);
6655 ia64_psr(regs)->up = 0;
6656 ia64_psr(regs)->pp = 0;
6658 for (i=1; PMC_IS_LAST(i) == 0; i++) {
6659 if (PMC_IS_IMPL(i) == 0) continue;
6660 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]);
6663 for (i=1; PMD_IS_LAST(i) == 0; i++) {
6664 if (PMD_IS_IMPL(i) == 0) continue;
6665 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]);
6669 printk("->CPU%d ctx_state=%d vaddr=%p addr=%p fd=%d ctx_task=[%d] saved_psr_up=0x%lx\n",
6672 ctx->ctx_smpl_vaddr,
6676 ctx->ctx_saved_psr_up);
6678 local_irq_restore(flags);
6682 * called from process.c:copy_thread(). task is new child.
6685 pfm_inherit(struct task_struct *task, struct pt_regs *regs)
6687 struct thread_struct *thread;
6689 DPRINT(("perfmon: pfm_inherit clearing state for [%d]\n", task_pid_nr(task)));
6691 thread = &task->thread;
6694 * cut links inherited from parent (current)
6696 thread->pfm_context = NULL;
6698 PFM_SET_WORK_PENDING(task, 0);
6701 * the psr bits are already set properly in copy_threads()
6704 #else /* !CONFIG_PERFMON */
6706 sys_perfmonctl (int fd, int cmd, void *arg, int count)
6710 #endif /* CONFIG_PERFMON */