iwlwifi: Packing all 4965 parameters
[linux/fpc-iii.git] / arch / ia64 / kernel / perfmon.c
bloba2aabfdc80d9ecaf4a8d1f4853956ca765e73f64
1 /*
2 * This file implements the perfmon-2 subsystem which is used
3 * to program the IA-64 Performance Monitoring Unit (PMU).
5 * The initial version of perfmon.c was written by
6 * Ganesh Venkitachalam, IBM Corp.
8 * Then it was modified for perfmon-1.x by Stephane Eranian and
9 * David Mosberger, Hewlett Packard Co.
11 * Version Perfmon-2.x is a rewrite of perfmon-1.x
12 * by Stephane Eranian, Hewlett Packard Co.
14 * Copyright (C) 1999-2005 Hewlett Packard Co
15 * Stephane Eranian <eranian@hpl.hp.com>
16 * David Mosberger-Tang <davidm@hpl.hp.com>
18 * More information about perfmon available at:
19 * http://www.hpl.hp.com/research/linux/perfmon
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/interrupt.h>
26 #include <linux/proc_fs.h>
27 #include <linux/seq_file.h>
28 #include <linux/init.h>
29 #include <linux/vmalloc.h>
30 #include <linux/mm.h>
31 #include <linux/sysctl.h>
32 #include <linux/list.h>
33 #include <linux/file.h>
34 #include <linux/poll.h>
35 #include <linux/vfs.h>
36 #include <linux/smp.h>
37 #include <linux/pagemap.h>
38 #include <linux/mount.h>
39 #include <linux/bitops.h>
40 #include <linux/capability.h>
41 #include <linux/rcupdate.h>
42 #include <linux/completion.h>
44 #include <asm/errno.h>
45 #include <asm/intrinsics.h>
46 #include <asm/page.h>
47 #include <asm/perfmon.h>
48 #include <asm/processor.h>
49 #include <asm/signal.h>
50 #include <asm/system.h>
51 #include <asm/uaccess.h>
52 #include <asm/delay.h>
54 #ifdef CONFIG_PERFMON
56 * perfmon context state
58 #define PFM_CTX_UNLOADED 1 /* context is not loaded onto any task */
59 #define PFM_CTX_LOADED 2 /* context is loaded onto a task */
60 #define PFM_CTX_MASKED 3 /* context is loaded but monitoring is masked due to overflow */
61 #define PFM_CTX_ZOMBIE 4 /* owner of the context is closing it */
63 #define PFM_INVALID_ACTIVATION (~0UL)
65 #define PFM_NUM_PMC_REGS 64 /* PMC save area for ctxsw */
66 #define PFM_NUM_PMD_REGS 64 /* PMD save area for ctxsw */
69 * depth of message queue
71 #define PFM_MAX_MSGS 32
72 #define PFM_CTXQ_EMPTY(g) ((g)->ctx_msgq_head == (g)->ctx_msgq_tail)
75 * type of a PMU register (bitmask).
76 * bitmask structure:
77 * bit0 : register implemented
78 * bit1 : end marker
79 * bit2-3 : reserved
80 * bit4 : pmc has pmc.pm
81 * bit5 : pmc controls a counter (has pmc.oi), pmd is used as counter
82 * bit6-7 : register type
83 * bit8-31: reserved
85 #define PFM_REG_NOTIMPL 0x0 /* not implemented at all */
86 #define PFM_REG_IMPL 0x1 /* register implemented */
87 #define PFM_REG_END 0x2 /* end marker */
88 #define PFM_REG_MONITOR (0x1<<4|PFM_REG_IMPL) /* a PMC with a pmc.pm field only */
89 #define PFM_REG_COUNTING (0x2<<4|PFM_REG_MONITOR) /* a monitor + pmc.oi+ PMD used as a counter */
90 #define PFM_REG_CONTROL (0x4<<4|PFM_REG_IMPL) /* PMU control register */
91 #define PFM_REG_CONFIG (0x8<<4|PFM_REG_IMPL) /* configuration register */
92 #define PFM_REG_BUFFER (0xc<<4|PFM_REG_IMPL) /* PMD used as buffer */
94 #define PMC_IS_LAST(i) (pmu_conf->pmc_desc[i].type & PFM_REG_END)
95 #define PMD_IS_LAST(i) (pmu_conf->pmd_desc[i].type & PFM_REG_END)
97 #define PMC_OVFL_NOTIFY(ctx, i) ((ctx)->ctx_pmds[i].flags & PFM_REGFL_OVFL_NOTIFY)
99 /* i assumed unsigned */
100 #define PMC_IS_IMPL(i) (i< PMU_MAX_PMCS && (pmu_conf->pmc_desc[i].type & PFM_REG_IMPL))
101 #define PMD_IS_IMPL(i) (i< PMU_MAX_PMDS && (pmu_conf->pmd_desc[i].type & PFM_REG_IMPL))
103 /* XXX: these assume that register i is implemented */
104 #define PMD_IS_COUNTING(i) ((pmu_conf->pmd_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
105 #define PMC_IS_COUNTING(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
106 #define PMC_IS_MONITOR(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_MONITOR) == PFM_REG_MONITOR)
107 #define PMC_IS_CONTROL(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_CONTROL) == PFM_REG_CONTROL)
109 #define PMC_DFL_VAL(i) pmu_conf->pmc_desc[i].default_value
110 #define PMC_RSVD_MASK(i) pmu_conf->pmc_desc[i].reserved_mask
111 #define PMD_PMD_DEP(i) pmu_conf->pmd_desc[i].dep_pmd[0]
112 #define PMC_PMD_DEP(i) pmu_conf->pmc_desc[i].dep_pmd[0]
114 #define PFM_NUM_IBRS IA64_NUM_DBG_REGS
115 #define PFM_NUM_DBRS IA64_NUM_DBG_REGS
117 #define CTX_OVFL_NOBLOCK(c) ((c)->ctx_fl_block == 0)
118 #define CTX_HAS_SMPL(c) ((c)->ctx_fl_is_sampling)
119 #define PFM_CTX_TASK(h) (h)->ctx_task
121 #define PMU_PMC_OI 5 /* position of pmc.oi bit */
123 /* XXX: does not support more than 64 PMDs */
124 #define CTX_USED_PMD(ctx, mask) (ctx)->ctx_used_pmds[0] |= (mask)
125 #define CTX_IS_USED_PMD(ctx, c) (((ctx)->ctx_used_pmds[0] & (1UL << (c))) != 0UL)
127 #define CTX_USED_MONITOR(ctx, mask) (ctx)->ctx_used_monitors[0] |= (mask)
129 #define CTX_USED_IBR(ctx,n) (ctx)->ctx_used_ibrs[(n)>>6] |= 1UL<< ((n) % 64)
130 #define CTX_USED_DBR(ctx,n) (ctx)->ctx_used_dbrs[(n)>>6] |= 1UL<< ((n) % 64)
131 #define CTX_USES_DBREGS(ctx) (((pfm_context_t *)(ctx))->ctx_fl_using_dbreg==1)
132 #define PFM_CODE_RR 0 /* requesting code range restriction */
133 #define PFM_DATA_RR 1 /* requestion data range restriction */
135 #define PFM_CPUINFO_CLEAR(v) pfm_get_cpu_var(pfm_syst_info) &= ~(v)
136 #define PFM_CPUINFO_SET(v) pfm_get_cpu_var(pfm_syst_info) |= (v)
137 #define PFM_CPUINFO_GET() pfm_get_cpu_var(pfm_syst_info)
139 #define RDEP(x) (1UL<<(x))
142 * context protection macros
143 * in SMP:
144 * - we need to protect against CPU concurrency (spin_lock)
145 * - we need to protect against PMU overflow interrupts (local_irq_disable)
146 * in UP:
147 * - we need to protect against PMU overflow interrupts (local_irq_disable)
149 * spin_lock_irqsave()/spin_unlock_irqrestore():
150 * in SMP: local_irq_disable + spin_lock
151 * in UP : local_irq_disable
153 * spin_lock()/spin_lock():
154 * in UP : removed automatically
155 * in SMP: protect against context accesses from other CPU. interrupts
156 * are not masked. This is useful for the PMU interrupt handler
157 * because we know we will not get PMU concurrency in that code.
159 #define PROTECT_CTX(c, f) \
160 do { \
161 DPRINT(("spinlock_irq_save ctx %p by [%d]\n", c, task_pid_nr(current))); \
162 spin_lock_irqsave(&(c)->ctx_lock, f); \
163 DPRINT(("spinlocked ctx %p by [%d]\n", c, task_pid_nr(current))); \
164 } while(0)
166 #define UNPROTECT_CTX(c, f) \
167 do { \
168 DPRINT(("spinlock_irq_restore ctx %p by [%d]\n", c, task_pid_nr(current))); \
169 spin_unlock_irqrestore(&(c)->ctx_lock, f); \
170 } while(0)
172 #define PROTECT_CTX_NOPRINT(c, f) \
173 do { \
174 spin_lock_irqsave(&(c)->ctx_lock, f); \
175 } while(0)
178 #define UNPROTECT_CTX_NOPRINT(c, f) \
179 do { \
180 spin_unlock_irqrestore(&(c)->ctx_lock, f); \
181 } while(0)
184 #define PROTECT_CTX_NOIRQ(c) \
185 do { \
186 spin_lock(&(c)->ctx_lock); \
187 } while(0)
189 #define UNPROTECT_CTX_NOIRQ(c) \
190 do { \
191 spin_unlock(&(c)->ctx_lock); \
192 } while(0)
195 #ifdef CONFIG_SMP
197 #define GET_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)
198 #define INC_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)++
199 #define SET_ACTIVATION(c) (c)->ctx_last_activation = GET_ACTIVATION()
201 #else /* !CONFIG_SMP */
202 #define SET_ACTIVATION(t) do {} while(0)
203 #define GET_ACTIVATION(t) do {} while(0)
204 #define INC_ACTIVATION(t) do {} while(0)
205 #endif /* CONFIG_SMP */
207 #define SET_PMU_OWNER(t, c) do { pfm_get_cpu_var(pmu_owner) = (t); pfm_get_cpu_var(pmu_ctx) = (c); } while(0)
208 #define GET_PMU_OWNER() pfm_get_cpu_var(pmu_owner)
209 #define GET_PMU_CTX() pfm_get_cpu_var(pmu_ctx)
211 #define LOCK_PFS(g) spin_lock_irqsave(&pfm_sessions.pfs_lock, g)
212 #define UNLOCK_PFS(g) spin_unlock_irqrestore(&pfm_sessions.pfs_lock, g)
214 #define PFM_REG_RETFLAG_SET(flags, val) do { flags &= ~PFM_REG_RETFL_MASK; flags |= (val); } while(0)
217 * cmp0 must be the value of pmc0
219 #define PMC0_HAS_OVFL(cmp0) (cmp0 & ~0x1UL)
221 #define PFMFS_MAGIC 0xa0b4d889
224 * debugging
226 #define PFM_DEBUGGING 1
227 #ifdef PFM_DEBUGGING
228 #define DPRINT(a) \
229 do { \
230 if (unlikely(pfm_sysctl.debug >0)) { printk("%s.%d: CPU%d [%d] ", __func__, __LINE__, smp_processor_id(), task_pid_nr(current)); printk a; } \
231 } while (0)
233 #define DPRINT_ovfl(a) \
234 do { \
235 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; } \
236 } while (0)
237 #endif
240 * 64-bit software counter structure
242 * the next_reset_type is applied to the next call to pfm_reset_regs()
244 typedef struct {
245 unsigned long val; /* virtual 64bit counter value */
246 unsigned long lval; /* last reset value */
247 unsigned long long_reset; /* reset value on sampling overflow */
248 unsigned long short_reset; /* reset value on overflow */
249 unsigned long reset_pmds[4]; /* which other pmds to reset when this counter overflows */
250 unsigned long smpl_pmds[4]; /* which pmds are accessed when counter overflow */
251 unsigned long seed; /* seed for random-number generator */
252 unsigned long mask; /* mask for random-number generator */
253 unsigned int flags; /* notify/do not notify */
254 unsigned long eventid; /* overflow event identifier */
255 } pfm_counter_t;
258 * context flags
260 typedef struct {
261 unsigned int block:1; /* when 1, task will blocked on user notifications */
262 unsigned int system:1; /* do system wide monitoring */
263 unsigned int using_dbreg:1; /* using range restrictions (debug registers) */
264 unsigned int is_sampling:1; /* true if using a custom format */
265 unsigned int excl_idle:1; /* exclude idle task in system wide session */
266 unsigned int going_zombie:1; /* context is zombie (MASKED+blocking) */
267 unsigned int trap_reason:2; /* reason for going into pfm_handle_work() */
268 unsigned int no_msg:1; /* no message sent on overflow */
269 unsigned int can_restart:1; /* allowed to issue a PFM_RESTART */
270 unsigned int reserved:22;
271 } pfm_context_flags_t;
273 #define PFM_TRAP_REASON_NONE 0x0 /* default value */
274 #define PFM_TRAP_REASON_BLOCK 0x1 /* we need to block on overflow */
275 #define PFM_TRAP_REASON_RESET 0x2 /* we need to reset PMDs */
279 * perfmon context: encapsulates all the state of a monitoring session
282 typedef struct pfm_context {
283 spinlock_t ctx_lock; /* context protection */
285 pfm_context_flags_t ctx_flags; /* bitmask of flags (block reason incl.) */
286 unsigned int ctx_state; /* state: active/inactive (no bitfield) */
288 struct task_struct *ctx_task; /* task to which context is attached */
290 unsigned long ctx_ovfl_regs[4]; /* which registers overflowed (notification) */
292 struct completion ctx_restart_done; /* use for blocking notification mode */
294 unsigned long ctx_used_pmds[4]; /* bitmask of PMD used */
295 unsigned long ctx_all_pmds[4]; /* bitmask of all accessible PMDs */
296 unsigned long ctx_reload_pmds[4]; /* bitmask of force reload PMD on ctxsw in */
298 unsigned long ctx_all_pmcs[4]; /* bitmask of all accessible PMCs */
299 unsigned long ctx_reload_pmcs[4]; /* bitmask of force reload PMC on ctxsw in */
300 unsigned long ctx_used_monitors[4]; /* bitmask of monitor PMC being used */
302 unsigned long ctx_pmcs[PFM_NUM_PMC_REGS]; /* saved copies of PMC values */
304 unsigned int ctx_used_ibrs[1]; /* bitmask of used IBR (speedup ctxsw in) */
305 unsigned int ctx_used_dbrs[1]; /* bitmask of used DBR (speedup ctxsw in) */
306 unsigned long ctx_dbrs[IA64_NUM_DBG_REGS]; /* DBR values (cache) when not loaded */
307 unsigned long ctx_ibrs[IA64_NUM_DBG_REGS]; /* IBR values (cache) when not loaded */
309 pfm_counter_t ctx_pmds[PFM_NUM_PMD_REGS]; /* software state for PMDS */
311 unsigned long th_pmcs[PFM_NUM_PMC_REGS]; /* PMC thread save state */
312 unsigned long th_pmds[PFM_NUM_PMD_REGS]; /* PMD thread save state */
314 u64 ctx_saved_psr_up; /* only contains psr.up value */
316 unsigned long ctx_last_activation; /* context last activation number for last_cpu */
317 unsigned int ctx_last_cpu; /* CPU id of current or last CPU used (SMP only) */
318 unsigned int ctx_cpu; /* cpu to which perfmon is applied (system wide) */
320 int ctx_fd; /* file descriptor used my this context */
321 pfm_ovfl_arg_t ctx_ovfl_arg; /* argument to custom buffer format handler */
323 pfm_buffer_fmt_t *ctx_buf_fmt; /* buffer format callbacks */
324 void *ctx_smpl_hdr; /* points to sampling buffer header kernel vaddr */
325 unsigned long ctx_smpl_size; /* size of sampling buffer */
326 void *ctx_smpl_vaddr; /* user level virtual address of smpl buffer */
328 wait_queue_head_t ctx_msgq_wait;
329 pfm_msg_t ctx_msgq[PFM_MAX_MSGS];
330 int ctx_msgq_head;
331 int ctx_msgq_tail;
332 struct fasync_struct *ctx_async_queue;
334 wait_queue_head_t ctx_zombieq; /* termination cleanup wait queue */
335 } pfm_context_t;
338 * magic number used to verify that structure is really
339 * a perfmon context
341 #define PFM_IS_FILE(f) ((f)->f_op == &pfm_file_ops)
343 #define PFM_GET_CTX(t) ((pfm_context_t *)(t)->thread.pfm_context)
345 #ifdef CONFIG_SMP
346 #define SET_LAST_CPU(ctx, v) (ctx)->ctx_last_cpu = (v)
347 #define GET_LAST_CPU(ctx) (ctx)->ctx_last_cpu
348 #else
349 #define SET_LAST_CPU(ctx, v) do {} while(0)
350 #define GET_LAST_CPU(ctx) do {} while(0)
351 #endif
354 #define ctx_fl_block ctx_flags.block
355 #define ctx_fl_system ctx_flags.system
356 #define ctx_fl_using_dbreg ctx_flags.using_dbreg
357 #define ctx_fl_is_sampling ctx_flags.is_sampling
358 #define ctx_fl_excl_idle ctx_flags.excl_idle
359 #define ctx_fl_going_zombie ctx_flags.going_zombie
360 #define ctx_fl_trap_reason ctx_flags.trap_reason
361 #define ctx_fl_no_msg ctx_flags.no_msg
362 #define ctx_fl_can_restart ctx_flags.can_restart
364 #define PFM_SET_WORK_PENDING(t, v) do { (t)->thread.pfm_needs_checking = v; } while(0);
365 #define PFM_GET_WORK_PENDING(t) (t)->thread.pfm_needs_checking
368 * global information about all sessions
369 * mostly used to synchronize between system wide and per-process
371 typedef struct {
372 spinlock_t pfs_lock; /* lock the structure */
374 unsigned int pfs_task_sessions; /* number of per task sessions */
375 unsigned int pfs_sys_sessions; /* number of per system wide sessions */
376 unsigned int pfs_sys_use_dbregs; /* incremented when a system wide session uses debug regs */
377 unsigned int pfs_ptrace_use_dbregs; /* incremented when a process uses debug regs */
378 struct task_struct *pfs_sys_session[NR_CPUS]; /* point to task owning a system-wide session */
379 } pfm_session_t;
382 * information about a PMC or PMD.
383 * dep_pmd[]: a bitmask of dependent PMD registers
384 * dep_pmc[]: a bitmask of dependent PMC registers
386 typedef int (*pfm_reg_check_t)(struct task_struct *task, pfm_context_t *ctx, unsigned int cnum, unsigned long *val, struct pt_regs *regs);
387 typedef struct {
388 unsigned int type;
389 int pm_pos;
390 unsigned long default_value; /* power-on default value */
391 unsigned long reserved_mask; /* bitmask of reserved bits */
392 pfm_reg_check_t read_check;
393 pfm_reg_check_t write_check;
394 unsigned long dep_pmd[4];
395 unsigned long dep_pmc[4];
396 } pfm_reg_desc_t;
398 /* assume cnum is a valid monitor */
399 #define PMC_PM(cnum, val) (((val) >> (pmu_conf->pmc_desc[cnum].pm_pos)) & 0x1)
402 * This structure is initialized at boot time and contains
403 * a description of the PMU main characteristics.
405 * If the probe function is defined, detection is based
406 * on its return value:
407 * - 0 means recognized PMU
408 * - anything else means not supported
409 * When the probe function is not defined, then the pmu_family field
410 * is used and it must match the host CPU family such that:
411 * - cpu->family & config->pmu_family != 0
413 typedef struct {
414 unsigned long ovfl_val; /* overflow value for counters */
416 pfm_reg_desc_t *pmc_desc; /* detailed PMC register dependencies descriptions */
417 pfm_reg_desc_t *pmd_desc; /* detailed PMD register dependencies descriptions */
419 unsigned int num_pmcs; /* number of PMCS: computed at init time */
420 unsigned int num_pmds; /* number of PMDS: computed at init time */
421 unsigned long impl_pmcs[4]; /* bitmask of implemented PMCS */
422 unsigned long impl_pmds[4]; /* bitmask of implemented PMDS */
424 char *pmu_name; /* PMU family name */
425 unsigned int pmu_family; /* cpuid family pattern used to identify pmu */
426 unsigned int flags; /* pmu specific flags */
427 unsigned int num_ibrs; /* number of IBRS: computed at init time */
428 unsigned int num_dbrs; /* number of DBRS: computed at init time */
429 unsigned int num_counters; /* PMC/PMD counting pairs : computed at init time */
430 int (*probe)(void); /* customized probe routine */
431 unsigned int use_rr_dbregs:1; /* set if debug registers used for range restriction */
432 } pmu_config_t;
434 * PMU specific flags
436 #define PFM_PMU_IRQ_RESEND 1 /* PMU needs explicit IRQ resend */
439 * debug register related type definitions
441 typedef struct {
442 unsigned long ibr_mask:56;
443 unsigned long ibr_plm:4;
444 unsigned long ibr_ig:3;
445 unsigned long ibr_x:1;
446 } ibr_mask_reg_t;
448 typedef struct {
449 unsigned long dbr_mask:56;
450 unsigned long dbr_plm:4;
451 unsigned long dbr_ig:2;
452 unsigned long dbr_w:1;
453 unsigned long dbr_r:1;
454 } dbr_mask_reg_t;
456 typedef union {
457 unsigned long val;
458 ibr_mask_reg_t ibr;
459 dbr_mask_reg_t dbr;
460 } dbreg_t;
464 * perfmon command descriptions
466 typedef struct {
467 int (*cmd_func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
468 char *cmd_name;
469 int cmd_flags;
470 unsigned int cmd_narg;
471 size_t cmd_argsize;
472 int (*cmd_getsize)(void *arg, size_t *sz);
473 } pfm_cmd_desc_t;
475 #define PFM_CMD_FD 0x01 /* command requires a file descriptor */
476 #define PFM_CMD_ARG_READ 0x02 /* command must read argument(s) */
477 #define PFM_CMD_ARG_RW 0x04 /* command must read/write argument(s) */
478 #define PFM_CMD_STOP 0x08 /* command does not work on zombie context */
481 #define PFM_CMD_NAME(cmd) pfm_cmd_tab[(cmd)].cmd_name
482 #define PFM_CMD_READ_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_READ)
483 #define PFM_CMD_RW_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_RW)
484 #define PFM_CMD_USE_FD(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_FD)
485 #define PFM_CMD_STOPPED(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_STOP)
487 #define PFM_CMD_ARG_MANY -1 /* cannot be zero */
489 typedef struct {
490 unsigned long pfm_spurious_ovfl_intr_count; /* keep track of spurious ovfl interrupts */
491 unsigned long pfm_replay_ovfl_intr_count; /* keep track of replayed ovfl interrupts */
492 unsigned long pfm_ovfl_intr_count; /* keep track of ovfl interrupts */
493 unsigned long pfm_ovfl_intr_cycles; /* cycles spent processing ovfl interrupts */
494 unsigned long pfm_ovfl_intr_cycles_min; /* min cycles spent processing ovfl interrupts */
495 unsigned long pfm_ovfl_intr_cycles_max; /* max cycles spent processing ovfl interrupts */
496 unsigned long pfm_smpl_handler_calls;
497 unsigned long pfm_smpl_handler_cycles;
498 char pad[SMP_CACHE_BYTES] ____cacheline_aligned;
499 } pfm_stats_t;
502 * perfmon internal variables
504 static pfm_stats_t pfm_stats[NR_CPUS];
505 static pfm_session_t pfm_sessions; /* global sessions information */
507 static DEFINE_SPINLOCK(pfm_alt_install_check);
508 static pfm_intr_handler_desc_t *pfm_alt_intr_handler;
510 static struct proc_dir_entry *perfmon_dir;
511 static pfm_uuid_t pfm_null_uuid = {0,};
513 static spinlock_t pfm_buffer_fmt_lock;
514 static LIST_HEAD(pfm_buffer_fmt_list);
516 static pmu_config_t *pmu_conf;
518 /* sysctl() controls */
519 pfm_sysctl_t pfm_sysctl;
520 EXPORT_SYMBOL(pfm_sysctl);
522 static ctl_table pfm_ctl_table[]={
524 .ctl_name = CTL_UNNUMBERED,
525 .procname = "debug",
526 .data = &pfm_sysctl.debug,
527 .maxlen = sizeof(int),
528 .mode = 0666,
529 .proc_handler = &proc_dointvec,
532 .ctl_name = CTL_UNNUMBERED,
533 .procname = "debug_ovfl",
534 .data = &pfm_sysctl.debug_ovfl,
535 .maxlen = sizeof(int),
536 .mode = 0666,
537 .proc_handler = &proc_dointvec,
540 .ctl_name = CTL_UNNUMBERED,
541 .procname = "fastctxsw",
542 .data = &pfm_sysctl.fastctxsw,
543 .maxlen = sizeof(int),
544 .mode = 0600,
545 .proc_handler = &proc_dointvec,
548 .ctl_name = CTL_UNNUMBERED,
549 .procname = "expert_mode",
550 .data = &pfm_sysctl.expert_mode,
551 .maxlen = sizeof(int),
552 .mode = 0600,
553 .proc_handler = &proc_dointvec,
557 static ctl_table pfm_sysctl_dir[] = {
559 .ctl_name = CTL_UNNUMBERED,
560 .procname = "perfmon",
561 .mode = 0555,
562 .child = pfm_ctl_table,
566 static ctl_table pfm_sysctl_root[] = {
568 .ctl_name = CTL_KERN,
569 .procname = "kernel",
570 .mode = 0555,
571 .child = pfm_sysctl_dir,
575 static struct ctl_table_header *pfm_sysctl_header;
577 static int pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
579 #define pfm_get_cpu_var(v) __ia64_per_cpu_var(v)
580 #define pfm_get_cpu_data(a,b) per_cpu(a, b)
582 static inline void
583 pfm_put_task(struct task_struct *task)
585 if (task != current) put_task_struct(task);
588 static inline void
589 pfm_reserve_page(unsigned long a)
591 SetPageReserved(vmalloc_to_page((void *)a));
593 static inline void
594 pfm_unreserve_page(unsigned long a)
596 ClearPageReserved(vmalloc_to_page((void*)a));
599 static inline unsigned long
600 pfm_protect_ctx_ctxsw(pfm_context_t *x)
602 spin_lock(&(x)->ctx_lock);
603 return 0UL;
606 static inline void
607 pfm_unprotect_ctx_ctxsw(pfm_context_t *x, unsigned long f)
609 spin_unlock(&(x)->ctx_lock);
612 static inline unsigned int
613 pfm_do_munmap(struct mm_struct *mm, unsigned long addr, size_t len, int acct)
615 return do_munmap(mm, addr, len);
618 static inline unsigned long
619 pfm_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags, unsigned long exec)
621 return get_unmapped_area(file, addr, len, pgoff, flags);
625 static int
626 pfmfs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data,
627 struct vfsmount *mnt)
629 return get_sb_pseudo(fs_type, "pfm:", NULL, PFMFS_MAGIC, mnt);
632 static struct file_system_type pfm_fs_type = {
633 .name = "pfmfs",
634 .get_sb = pfmfs_get_sb,
635 .kill_sb = kill_anon_super,
638 DEFINE_PER_CPU(unsigned long, pfm_syst_info);
639 DEFINE_PER_CPU(struct task_struct *, pmu_owner);
640 DEFINE_PER_CPU(pfm_context_t *, pmu_ctx);
641 DEFINE_PER_CPU(unsigned long, pmu_activation_number);
642 EXPORT_PER_CPU_SYMBOL_GPL(pfm_syst_info);
645 /* forward declaration */
646 static const struct file_operations pfm_file_ops;
649 * forward declarations
651 #ifndef CONFIG_SMP
652 static void pfm_lazy_save_regs (struct task_struct *ta);
653 #endif
655 void dump_pmu_state(const char *);
656 static int pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
658 #include "perfmon_itanium.h"
659 #include "perfmon_mckinley.h"
660 #include "perfmon_montecito.h"
661 #include "perfmon_generic.h"
663 static pmu_config_t *pmu_confs[]={
664 &pmu_conf_mont,
665 &pmu_conf_mck,
666 &pmu_conf_ita,
667 &pmu_conf_gen, /* must be last */
668 NULL
672 static int pfm_end_notify_user(pfm_context_t *ctx);
674 static inline void
675 pfm_clear_psr_pp(void)
677 ia64_rsm(IA64_PSR_PP);
678 ia64_srlz_i();
681 static inline void
682 pfm_set_psr_pp(void)
684 ia64_ssm(IA64_PSR_PP);
685 ia64_srlz_i();
688 static inline void
689 pfm_clear_psr_up(void)
691 ia64_rsm(IA64_PSR_UP);
692 ia64_srlz_i();
695 static inline void
696 pfm_set_psr_up(void)
698 ia64_ssm(IA64_PSR_UP);
699 ia64_srlz_i();
702 static inline unsigned long
703 pfm_get_psr(void)
705 unsigned long tmp;
706 tmp = ia64_getreg(_IA64_REG_PSR);
707 ia64_srlz_i();
708 return tmp;
711 static inline void
712 pfm_set_psr_l(unsigned long val)
714 ia64_setreg(_IA64_REG_PSR_L, val);
715 ia64_srlz_i();
718 static inline void
719 pfm_freeze_pmu(void)
721 ia64_set_pmc(0,1UL);
722 ia64_srlz_d();
725 static inline void
726 pfm_unfreeze_pmu(void)
728 ia64_set_pmc(0,0UL);
729 ia64_srlz_d();
732 static inline void
733 pfm_restore_ibrs(unsigned long *ibrs, unsigned int nibrs)
735 int i;
737 for (i=0; i < nibrs; i++) {
738 ia64_set_ibr(i, ibrs[i]);
739 ia64_dv_serialize_instruction();
741 ia64_srlz_i();
744 static inline void
745 pfm_restore_dbrs(unsigned long *dbrs, unsigned int ndbrs)
747 int i;
749 for (i=0; i < ndbrs; i++) {
750 ia64_set_dbr(i, dbrs[i]);
751 ia64_dv_serialize_data();
753 ia64_srlz_d();
757 * PMD[i] must be a counter. no check is made
759 static inline unsigned long
760 pfm_read_soft_counter(pfm_context_t *ctx, int i)
762 return ctx->ctx_pmds[i].val + (ia64_get_pmd(i) & pmu_conf->ovfl_val);
766 * PMD[i] must be a counter. no check is made
768 static inline void
769 pfm_write_soft_counter(pfm_context_t *ctx, int i, unsigned long val)
771 unsigned long ovfl_val = pmu_conf->ovfl_val;
773 ctx->ctx_pmds[i].val = val & ~ovfl_val;
775 * writing to unimplemented part is ignore, so we do not need to
776 * mask off top part
778 ia64_set_pmd(i, val & ovfl_val);
781 static pfm_msg_t *
782 pfm_get_new_msg(pfm_context_t *ctx)
784 int idx, next;
786 next = (ctx->ctx_msgq_tail+1) % PFM_MAX_MSGS;
788 DPRINT(("ctx_fd=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
789 if (next == ctx->ctx_msgq_head) return NULL;
791 idx = ctx->ctx_msgq_tail;
792 ctx->ctx_msgq_tail = next;
794 DPRINT(("ctx=%p head=%d tail=%d msg=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail, idx));
796 return ctx->ctx_msgq+idx;
799 static pfm_msg_t *
800 pfm_get_next_msg(pfm_context_t *ctx)
802 pfm_msg_t *msg;
804 DPRINT(("ctx=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
806 if (PFM_CTXQ_EMPTY(ctx)) return NULL;
809 * get oldest message
811 msg = ctx->ctx_msgq+ctx->ctx_msgq_head;
814 * and move forward
816 ctx->ctx_msgq_head = (ctx->ctx_msgq_head+1) % PFM_MAX_MSGS;
818 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));
820 return msg;
823 static void
824 pfm_reset_msgq(pfm_context_t *ctx)
826 ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
827 DPRINT(("ctx=%p msgq reset\n", ctx));
830 static void *
831 pfm_rvmalloc(unsigned long size)
833 void *mem;
834 unsigned long addr;
836 size = PAGE_ALIGN(size);
837 mem = vmalloc(size);
838 if (mem) {
839 //printk("perfmon: CPU%d pfm_rvmalloc(%ld)=%p\n", smp_processor_id(), size, mem);
840 memset(mem, 0, size);
841 addr = (unsigned long)mem;
842 while (size > 0) {
843 pfm_reserve_page(addr);
844 addr+=PAGE_SIZE;
845 size-=PAGE_SIZE;
848 return mem;
851 static void
852 pfm_rvfree(void *mem, unsigned long size)
854 unsigned long addr;
856 if (mem) {
857 DPRINT(("freeing physical buffer @%p size=%lu\n", mem, size));
858 addr = (unsigned long) mem;
859 while ((long) size > 0) {
860 pfm_unreserve_page(addr);
861 addr+=PAGE_SIZE;
862 size-=PAGE_SIZE;
864 vfree(mem);
866 return;
869 static pfm_context_t *
870 pfm_context_alloc(void)
872 pfm_context_t *ctx;
875 * allocate context descriptor
876 * must be able to free with interrupts disabled
878 ctx = kzalloc(sizeof(pfm_context_t), GFP_KERNEL);
879 if (ctx) {
880 DPRINT(("alloc ctx @%p\n", ctx));
882 return ctx;
885 static void
886 pfm_context_free(pfm_context_t *ctx)
888 if (ctx) {
889 DPRINT(("free ctx @%p\n", ctx));
890 kfree(ctx);
894 static void
895 pfm_mask_monitoring(struct task_struct *task)
897 pfm_context_t *ctx = PFM_GET_CTX(task);
898 unsigned long mask, val, ovfl_mask;
899 int i;
901 DPRINT_ovfl(("masking monitoring for [%d]\n", task_pid_nr(task)));
903 ovfl_mask = pmu_conf->ovfl_val;
905 * monitoring can only be masked as a result of a valid
906 * counter overflow. In UP, it means that the PMU still
907 * has an owner. Note that the owner can be different
908 * from the current task. However the PMU state belongs
909 * to the owner.
910 * In SMP, a valid overflow only happens when task is
911 * current. Therefore if we come here, we know that
912 * the PMU state belongs to the current task, therefore
913 * we can access the live registers.
915 * So in both cases, the live register contains the owner's
916 * state. We can ONLY touch the PMU registers and NOT the PSR.
918 * As a consequence to this call, the ctx->th_pmds[] array
919 * contains stale information which must be ignored
920 * when context is reloaded AND monitoring is active (see
921 * pfm_restart).
923 mask = ctx->ctx_used_pmds[0];
924 for (i = 0; mask; i++, mask>>=1) {
925 /* skip non used pmds */
926 if ((mask & 0x1) == 0) continue;
927 val = ia64_get_pmd(i);
929 if (PMD_IS_COUNTING(i)) {
931 * we rebuild the full 64 bit value of the counter
933 ctx->ctx_pmds[i].val += (val & ovfl_mask);
934 } else {
935 ctx->ctx_pmds[i].val = val;
937 DPRINT_ovfl(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
939 ctx->ctx_pmds[i].val,
940 val & ovfl_mask));
943 * mask monitoring by setting the privilege level to 0
944 * we cannot use psr.pp/psr.up for this, it is controlled by
945 * the user
947 * if task is current, modify actual registers, otherwise modify
948 * thread save state, i.e., what will be restored in pfm_load_regs()
950 mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
951 for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
952 if ((mask & 0x1) == 0UL) continue;
953 ia64_set_pmc(i, ctx->th_pmcs[i] & ~0xfUL);
954 ctx->th_pmcs[i] &= ~0xfUL;
955 DPRINT_ovfl(("pmc[%d]=0x%lx\n", i, ctx->th_pmcs[i]));
958 * make all of this visible
960 ia64_srlz_d();
964 * must always be done with task == current
966 * context must be in MASKED state when calling
968 static void
969 pfm_restore_monitoring(struct task_struct *task)
971 pfm_context_t *ctx = PFM_GET_CTX(task);
972 unsigned long mask, ovfl_mask;
973 unsigned long psr, val;
974 int i, is_system;
976 is_system = ctx->ctx_fl_system;
977 ovfl_mask = pmu_conf->ovfl_val;
979 if (task != current) {
980 printk(KERN_ERR "perfmon.%d: invalid task[%d] current[%d]\n", __LINE__, task_pid_nr(task), task_pid_nr(current));
981 return;
983 if (ctx->ctx_state != PFM_CTX_MASKED) {
984 printk(KERN_ERR "perfmon.%d: task[%d] current[%d] invalid state=%d\n", __LINE__,
985 task_pid_nr(task), task_pid_nr(current), ctx->ctx_state);
986 return;
988 psr = pfm_get_psr();
990 * monitoring is masked via the PMC.
991 * As we restore their value, we do not want each counter to
992 * restart right away. We stop monitoring using the PSR,
993 * restore the PMC (and PMD) and then re-establish the psr
994 * as it was. Note that there can be no pending overflow at
995 * this point, because monitoring was MASKED.
997 * system-wide session are pinned and self-monitoring
999 if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
1000 /* disable dcr pp */
1001 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
1002 pfm_clear_psr_pp();
1003 } else {
1004 pfm_clear_psr_up();
1007 * first, we restore the PMD
1009 mask = ctx->ctx_used_pmds[0];
1010 for (i = 0; mask; i++, mask>>=1) {
1011 /* skip non used pmds */
1012 if ((mask & 0x1) == 0) continue;
1014 if (PMD_IS_COUNTING(i)) {
1016 * we split the 64bit value according to
1017 * counter width
1019 val = ctx->ctx_pmds[i].val & ovfl_mask;
1020 ctx->ctx_pmds[i].val &= ~ovfl_mask;
1021 } else {
1022 val = ctx->ctx_pmds[i].val;
1024 ia64_set_pmd(i, val);
1026 DPRINT(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
1028 ctx->ctx_pmds[i].val,
1029 val));
1032 * restore the PMCs
1034 mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
1035 for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
1036 if ((mask & 0x1) == 0UL) continue;
1037 ctx->th_pmcs[i] = ctx->ctx_pmcs[i];
1038 ia64_set_pmc(i, ctx->th_pmcs[i]);
1039 DPRINT(("[%d] pmc[%d]=0x%lx\n",
1040 task_pid_nr(task), i, ctx->th_pmcs[i]));
1042 ia64_srlz_d();
1045 * must restore DBR/IBR because could be modified while masked
1046 * XXX: need to optimize
1048 if (ctx->ctx_fl_using_dbreg) {
1049 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
1050 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
1054 * now restore PSR
1056 if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
1057 /* enable dcr pp */
1058 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
1059 ia64_srlz_i();
1061 pfm_set_psr_l(psr);
1064 static inline void
1065 pfm_save_pmds(unsigned long *pmds, unsigned long mask)
1067 int i;
1069 ia64_srlz_d();
1071 for (i=0; mask; i++, mask>>=1) {
1072 if (mask & 0x1) pmds[i] = ia64_get_pmd(i);
1077 * reload from thread state (used for ctxw only)
1079 static inline void
1080 pfm_restore_pmds(unsigned long *pmds, unsigned long mask)
1082 int i;
1083 unsigned long val, ovfl_val = pmu_conf->ovfl_val;
1085 for (i=0; mask; i++, mask>>=1) {
1086 if ((mask & 0x1) == 0) continue;
1087 val = PMD_IS_COUNTING(i) ? pmds[i] & ovfl_val : pmds[i];
1088 ia64_set_pmd(i, val);
1090 ia64_srlz_d();
1094 * propagate PMD from context to thread-state
1096 static inline void
1097 pfm_copy_pmds(struct task_struct *task, pfm_context_t *ctx)
1099 unsigned long ovfl_val = pmu_conf->ovfl_val;
1100 unsigned long mask = ctx->ctx_all_pmds[0];
1101 unsigned long val;
1102 int i;
1104 DPRINT(("mask=0x%lx\n", mask));
1106 for (i=0; mask; i++, mask>>=1) {
1108 val = ctx->ctx_pmds[i].val;
1111 * We break up the 64 bit value into 2 pieces
1112 * the lower bits go to the machine state in the
1113 * thread (will be reloaded on ctxsw in).
1114 * The upper part stays in the soft-counter.
1116 if (PMD_IS_COUNTING(i)) {
1117 ctx->ctx_pmds[i].val = val & ~ovfl_val;
1118 val &= ovfl_val;
1120 ctx->th_pmds[i] = val;
1122 DPRINT(("pmd[%d]=0x%lx soft_val=0x%lx\n",
1124 ctx->th_pmds[i],
1125 ctx->ctx_pmds[i].val));
1130 * propagate PMC from context to thread-state
1132 static inline void
1133 pfm_copy_pmcs(struct task_struct *task, pfm_context_t *ctx)
1135 unsigned long mask = ctx->ctx_all_pmcs[0];
1136 int i;
1138 DPRINT(("mask=0x%lx\n", mask));
1140 for (i=0; mask; i++, mask>>=1) {
1141 /* masking 0 with ovfl_val yields 0 */
1142 ctx->th_pmcs[i] = ctx->ctx_pmcs[i];
1143 DPRINT(("pmc[%d]=0x%lx\n", i, ctx->th_pmcs[i]));
1149 static inline void
1150 pfm_restore_pmcs(unsigned long *pmcs, unsigned long mask)
1152 int i;
1154 for (i=0; mask; i++, mask>>=1) {
1155 if ((mask & 0x1) == 0) continue;
1156 ia64_set_pmc(i, pmcs[i]);
1158 ia64_srlz_d();
1161 static inline int
1162 pfm_uuid_cmp(pfm_uuid_t a, pfm_uuid_t b)
1164 return memcmp(a, b, sizeof(pfm_uuid_t));
1167 static inline int
1168 pfm_buf_fmt_exit(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, struct pt_regs *regs)
1170 int ret = 0;
1171 if (fmt->fmt_exit) ret = (*fmt->fmt_exit)(task, buf, regs);
1172 return ret;
1175 static inline int
1176 pfm_buf_fmt_getsize(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags, int cpu, void *arg, unsigned long *size)
1178 int ret = 0;
1179 if (fmt->fmt_getsize) ret = (*fmt->fmt_getsize)(task, flags, cpu, arg, size);
1180 return ret;
1184 static inline int
1185 pfm_buf_fmt_validate(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags,
1186 int cpu, void *arg)
1188 int ret = 0;
1189 if (fmt->fmt_validate) ret = (*fmt->fmt_validate)(task, flags, cpu, arg);
1190 return ret;
1193 static inline int
1194 pfm_buf_fmt_init(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, unsigned int flags,
1195 int cpu, void *arg)
1197 int ret = 0;
1198 if (fmt->fmt_init) ret = (*fmt->fmt_init)(task, buf, flags, cpu, arg);
1199 return ret;
1202 static inline int
1203 pfm_buf_fmt_restart(pfm_buffer_fmt_t *fmt, struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
1205 int ret = 0;
1206 if (fmt->fmt_restart) ret = (*fmt->fmt_restart)(task, ctrl, buf, regs);
1207 return ret;
1210 static inline int
1211 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)
1213 int ret = 0;
1214 if (fmt->fmt_restart_active) ret = (*fmt->fmt_restart_active)(task, ctrl, buf, regs);
1215 return ret;
1218 static pfm_buffer_fmt_t *
1219 __pfm_find_buffer_fmt(pfm_uuid_t uuid)
1221 struct list_head * pos;
1222 pfm_buffer_fmt_t * entry;
1224 list_for_each(pos, &pfm_buffer_fmt_list) {
1225 entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
1226 if (pfm_uuid_cmp(uuid, entry->fmt_uuid) == 0)
1227 return entry;
1229 return NULL;
1233 * find a buffer format based on its uuid
1235 static pfm_buffer_fmt_t *
1236 pfm_find_buffer_fmt(pfm_uuid_t uuid)
1238 pfm_buffer_fmt_t * fmt;
1239 spin_lock(&pfm_buffer_fmt_lock);
1240 fmt = __pfm_find_buffer_fmt(uuid);
1241 spin_unlock(&pfm_buffer_fmt_lock);
1242 return fmt;
1246 pfm_register_buffer_fmt(pfm_buffer_fmt_t *fmt)
1248 int ret = 0;
1250 /* some sanity checks */
1251 if (fmt == NULL || fmt->fmt_name == NULL) return -EINVAL;
1253 /* we need at least a handler */
1254 if (fmt->fmt_handler == NULL) return -EINVAL;
1257 * XXX: need check validity of fmt_arg_size
1260 spin_lock(&pfm_buffer_fmt_lock);
1262 if (__pfm_find_buffer_fmt(fmt->fmt_uuid)) {
1263 printk(KERN_ERR "perfmon: duplicate sampling format: %s\n", fmt->fmt_name);
1264 ret = -EBUSY;
1265 goto out;
1267 list_add(&fmt->fmt_list, &pfm_buffer_fmt_list);
1268 printk(KERN_INFO "perfmon: added sampling format %s\n", fmt->fmt_name);
1270 out:
1271 spin_unlock(&pfm_buffer_fmt_lock);
1272 return ret;
1274 EXPORT_SYMBOL(pfm_register_buffer_fmt);
1277 pfm_unregister_buffer_fmt(pfm_uuid_t uuid)
1279 pfm_buffer_fmt_t *fmt;
1280 int ret = 0;
1282 spin_lock(&pfm_buffer_fmt_lock);
1284 fmt = __pfm_find_buffer_fmt(uuid);
1285 if (!fmt) {
1286 printk(KERN_ERR "perfmon: cannot unregister format, not found\n");
1287 ret = -EINVAL;
1288 goto out;
1290 list_del_init(&fmt->fmt_list);
1291 printk(KERN_INFO "perfmon: removed sampling format: %s\n", fmt->fmt_name);
1293 out:
1294 spin_unlock(&pfm_buffer_fmt_lock);
1295 return ret;
1298 EXPORT_SYMBOL(pfm_unregister_buffer_fmt);
1300 extern void update_pal_halt_status(int);
1302 static int
1303 pfm_reserve_session(struct task_struct *task, int is_syswide, unsigned int cpu)
1305 unsigned long flags;
1307 * validity checks on cpu_mask have been done upstream
1309 LOCK_PFS(flags);
1311 DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1312 pfm_sessions.pfs_sys_sessions,
1313 pfm_sessions.pfs_task_sessions,
1314 pfm_sessions.pfs_sys_use_dbregs,
1315 is_syswide,
1316 cpu));
1318 if (is_syswide) {
1320 * cannot mix system wide and per-task sessions
1322 if (pfm_sessions.pfs_task_sessions > 0UL) {
1323 DPRINT(("system wide not possible, %u conflicting task_sessions\n",
1324 pfm_sessions.pfs_task_sessions));
1325 goto abort;
1328 if (pfm_sessions.pfs_sys_session[cpu]) goto error_conflict;
1330 DPRINT(("reserving system wide session on CPU%u currently on CPU%u\n", cpu, smp_processor_id()));
1332 pfm_sessions.pfs_sys_session[cpu] = task;
1334 pfm_sessions.pfs_sys_sessions++ ;
1336 } else {
1337 if (pfm_sessions.pfs_sys_sessions) goto abort;
1338 pfm_sessions.pfs_task_sessions++;
1341 DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1342 pfm_sessions.pfs_sys_sessions,
1343 pfm_sessions.pfs_task_sessions,
1344 pfm_sessions.pfs_sys_use_dbregs,
1345 is_syswide,
1346 cpu));
1349 * disable default_idle() to go to PAL_HALT
1351 update_pal_halt_status(0);
1353 UNLOCK_PFS(flags);
1355 return 0;
1357 error_conflict:
1358 DPRINT(("system wide not possible, conflicting session [%d] on CPU%d\n",
1359 task_pid_nr(pfm_sessions.pfs_sys_session[cpu]),
1360 cpu));
1361 abort:
1362 UNLOCK_PFS(flags);
1364 return -EBUSY;
1368 static int
1369 pfm_unreserve_session(pfm_context_t *ctx, int is_syswide, unsigned int cpu)
1371 unsigned long flags;
1373 * validity checks on cpu_mask have been done upstream
1375 LOCK_PFS(flags);
1377 DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1378 pfm_sessions.pfs_sys_sessions,
1379 pfm_sessions.pfs_task_sessions,
1380 pfm_sessions.pfs_sys_use_dbregs,
1381 is_syswide,
1382 cpu));
1385 if (is_syswide) {
1386 pfm_sessions.pfs_sys_session[cpu] = NULL;
1388 * would not work with perfmon+more than one bit in cpu_mask
1390 if (ctx && ctx->ctx_fl_using_dbreg) {
1391 if (pfm_sessions.pfs_sys_use_dbregs == 0) {
1392 printk(KERN_ERR "perfmon: invalid release for ctx %p sys_use_dbregs=0\n", ctx);
1393 } else {
1394 pfm_sessions.pfs_sys_use_dbregs--;
1397 pfm_sessions.pfs_sys_sessions--;
1398 } else {
1399 pfm_sessions.pfs_task_sessions--;
1401 DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1402 pfm_sessions.pfs_sys_sessions,
1403 pfm_sessions.pfs_task_sessions,
1404 pfm_sessions.pfs_sys_use_dbregs,
1405 is_syswide,
1406 cpu));
1409 * if possible, enable default_idle() to go into PAL_HALT
1411 if (pfm_sessions.pfs_task_sessions == 0 && pfm_sessions.pfs_sys_sessions == 0)
1412 update_pal_halt_status(1);
1414 UNLOCK_PFS(flags);
1416 return 0;
1420 * removes virtual mapping of the sampling buffer.
1421 * IMPORTANT: cannot be called with interrupts disable, e.g. inside
1422 * a PROTECT_CTX() section.
1424 static int
1425 pfm_remove_smpl_mapping(struct task_struct *task, void *vaddr, unsigned long size)
1427 int r;
1429 /* sanity checks */
1430 if (task->mm == NULL || size == 0UL || vaddr == NULL) {
1431 printk(KERN_ERR "perfmon: pfm_remove_smpl_mapping [%d] invalid context mm=%p\n", task_pid_nr(task), task->mm);
1432 return -EINVAL;
1435 DPRINT(("smpl_vaddr=%p size=%lu\n", vaddr, size));
1438 * does the actual unmapping
1440 down_write(&task->mm->mmap_sem);
1442 DPRINT(("down_write done smpl_vaddr=%p size=%lu\n", vaddr, size));
1444 r = pfm_do_munmap(task->mm, (unsigned long)vaddr, size, 0);
1446 up_write(&task->mm->mmap_sem);
1447 if (r !=0) {
1448 printk(KERN_ERR "perfmon: [%d] unable to unmap sampling buffer @%p size=%lu\n", task_pid_nr(task), vaddr, size);
1451 DPRINT(("do_unmap(%p, %lu)=%d\n", vaddr, size, r));
1453 return 0;
1457 * free actual physical storage used by sampling buffer
1459 #if 0
1460 static int
1461 pfm_free_smpl_buffer(pfm_context_t *ctx)
1463 pfm_buffer_fmt_t *fmt;
1465 if (ctx->ctx_smpl_hdr == NULL) goto invalid_free;
1468 * we won't use the buffer format anymore
1470 fmt = ctx->ctx_buf_fmt;
1472 DPRINT(("sampling buffer @%p size %lu vaddr=%p\n",
1473 ctx->ctx_smpl_hdr,
1474 ctx->ctx_smpl_size,
1475 ctx->ctx_smpl_vaddr));
1477 pfm_buf_fmt_exit(fmt, current, NULL, NULL);
1480 * free the buffer
1482 pfm_rvfree(ctx->ctx_smpl_hdr, ctx->ctx_smpl_size);
1484 ctx->ctx_smpl_hdr = NULL;
1485 ctx->ctx_smpl_size = 0UL;
1487 return 0;
1489 invalid_free:
1490 printk(KERN_ERR "perfmon: pfm_free_smpl_buffer [%d] no buffer\n", task_pid_nr(current));
1491 return -EINVAL;
1493 #endif
1495 static inline void
1496 pfm_exit_smpl_buffer(pfm_buffer_fmt_t *fmt)
1498 if (fmt == NULL) return;
1500 pfm_buf_fmt_exit(fmt, current, NULL, NULL);
1505 * pfmfs should _never_ be mounted by userland - too much of security hassle,
1506 * no real gain from having the whole whorehouse mounted. So we don't need
1507 * any operations on the root directory. However, we need a non-trivial
1508 * d_name - pfm: will go nicely and kill the special-casing in procfs.
1510 static struct vfsmount *pfmfs_mnt;
1512 static int __init
1513 init_pfm_fs(void)
1515 int err = register_filesystem(&pfm_fs_type);
1516 if (!err) {
1517 pfmfs_mnt = kern_mount(&pfm_fs_type);
1518 err = PTR_ERR(pfmfs_mnt);
1519 if (IS_ERR(pfmfs_mnt))
1520 unregister_filesystem(&pfm_fs_type);
1521 else
1522 err = 0;
1524 return err;
1527 static ssize_t
1528 pfm_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
1530 pfm_context_t *ctx;
1531 pfm_msg_t *msg;
1532 ssize_t ret;
1533 unsigned long flags;
1534 DECLARE_WAITQUEUE(wait, current);
1535 if (PFM_IS_FILE(filp) == 0) {
1536 printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", task_pid_nr(current));
1537 return -EINVAL;
1540 ctx = (pfm_context_t *)filp->private_data;
1541 if (ctx == NULL) {
1542 printk(KERN_ERR "perfmon: pfm_read: NULL ctx [%d]\n", task_pid_nr(current));
1543 return -EINVAL;
1547 * check even when there is no message
1549 if (size < sizeof(pfm_msg_t)) {
1550 DPRINT(("message is too small ctx=%p (>=%ld)\n", ctx, sizeof(pfm_msg_t)));
1551 return -EINVAL;
1554 PROTECT_CTX(ctx, flags);
1557 * put ourselves on the wait queue
1559 add_wait_queue(&ctx->ctx_msgq_wait, &wait);
1562 for(;;) {
1564 * check wait queue
1567 set_current_state(TASK_INTERRUPTIBLE);
1569 DPRINT(("head=%d tail=%d\n", ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
1571 ret = 0;
1572 if(PFM_CTXQ_EMPTY(ctx) == 0) break;
1574 UNPROTECT_CTX(ctx, flags);
1577 * check non-blocking read
1579 ret = -EAGAIN;
1580 if(filp->f_flags & O_NONBLOCK) break;
1583 * check pending signals
1585 if(signal_pending(current)) {
1586 ret = -EINTR;
1587 break;
1590 * no message, so wait
1592 schedule();
1594 PROTECT_CTX(ctx, flags);
1596 DPRINT(("[%d] back to running ret=%ld\n", task_pid_nr(current), ret));
1597 set_current_state(TASK_RUNNING);
1598 remove_wait_queue(&ctx->ctx_msgq_wait, &wait);
1600 if (ret < 0) goto abort;
1602 ret = -EINVAL;
1603 msg = pfm_get_next_msg(ctx);
1604 if (msg == NULL) {
1605 printk(KERN_ERR "perfmon: pfm_read no msg for ctx=%p [%d]\n", ctx, task_pid_nr(current));
1606 goto abort_locked;
1609 DPRINT(("fd=%d type=%d\n", msg->pfm_gen_msg.msg_ctx_fd, msg->pfm_gen_msg.msg_type));
1611 ret = -EFAULT;
1612 if(copy_to_user(buf, msg, sizeof(pfm_msg_t)) == 0) ret = sizeof(pfm_msg_t);
1614 abort_locked:
1615 UNPROTECT_CTX(ctx, flags);
1616 abort:
1617 return ret;
1620 static ssize_t
1621 pfm_write(struct file *file, const char __user *ubuf,
1622 size_t size, loff_t *ppos)
1624 DPRINT(("pfm_write called\n"));
1625 return -EINVAL;
1628 static unsigned int
1629 pfm_poll(struct file *filp, poll_table * wait)
1631 pfm_context_t *ctx;
1632 unsigned long flags;
1633 unsigned int mask = 0;
1635 if (PFM_IS_FILE(filp) == 0) {
1636 printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", task_pid_nr(current));
1637 return 0;
1640 ctx = (pfm_context_t *)filp->private_data;
1641 if (ctx == NULL) {
1642 printk(KERN_ERR "perfmon: pfm_poll: NULL ctx [%d]\n", task_pid_nr(current));
1643 return 0;
1647 DPRINT(("pfm_poll ctx_fd=%d before poll_wait\n", ctx->ctx_fd));
1649 poll_wait(filp, &ctx->ctx_msgq_wait, wait);
1651 PROTECT_CTX(ctx, flags);
1653 if (PFM_CTXQ_EMPTY(ctx) == 0)
1654 mask = POLLIN | POLLRDNORM;
1656 UNPROTECT_CTX(ctx, flags);
1658 DPRINT(("pfm_poll ctx_fd=%d mask=0x%x\n", ctx->ctx_fd, mask));
1660 return mask;
1663 static int
1664 pfm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1666 DPRINT(("pfm_ioctl called\n"));
1667 return -EINVAL;
1671 * interrupt cannot be masked when coming here
1673 static inline int
1674 pfm_do_fasync(int fd, struct file *filp, pfm_context_t *ctx, int on)
1676 int ret;
1678 ret = fasync_helper (fd, filp, on, &ctx->ctx_async_queue);
1680 DPRINT(("pfm_fasync called by [%d] on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
1681 task_pid_nr(current),
1684 ctx->ctx_async_queue, ret));
1686 return ret;
1689 static int
1690 pfm_fasync(int fd, struct file *filp, int on)
1692 pfm_context_t *ctx;
1693 int ret;
1695 if (PFM_IS_FILE(filp) == 0) {
1696 printk(KERN_ERR "perfmon: pfm_fasync bad magic [%d]\n", task_pid_nr(current));
1697 return -EBADF;
1700 ctx = (pfm_context_t *)filp->private_data;
1701 if (ctx == NULL) {
1702 printk(KERN_ERR "perfmon: pfm_fasync NULL ctx [%d]\n", task_pid_nr(current));
1703 return -EBADF;
1706 * we cannot mask interrupts during this call because this may
1707 * may go to sleep if memory is not readily avalaible.
1709 * We are protected from the conetxt disappearing by the get_fd()/put_fd()
1710 * done in caller. Serialization of this function is ensured by caller.
1712 ret = pfm_do_fasync(fd, filp, ctx, on);
1715 DPRINT(("pfm_fasync called on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
1718 ctx->ctx_async_queue, ret));
1720 return ret;
1723 #ifdef CONFIG_SMP
1725 * this function is exclusively called from pfm_close().
1726 * The context is not protected at that time, nor are interrupts
1727 * on the remote CPU. That's necessary to avoid deadlocks.
1729 static void
1730 pfm_syswide_force_stop(void *info)
1732 pfm_context_t *ctx = (pfm_context_t *)info;
1733 struct pt_regs *regs = task_pt_regs(current);
1734 struct task_struct *owner;
1735 unsigned long flags;
1736 int ret;
1738 if (ctx->ctx_cpu != smp_processor_id()) {
1739 printk(KERN_ERR "perfmon: pfm_syswide_force_stop for CPU%d but on CPU%d\n",
1740 ctx->ctx_cpu,
1741 smp_processor_id());
1742 return;
1744 owner = GET_PMU_OWNER();
1745 if (owner != ctx->ctx_task) {
1746 printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected owner [%d] instead of [%d]\n",
1747 smp_processor_id(),
1748 task_pid_nr(owner), task_pid_nr(ctx->ctx_task));
1749 return;
1751 if (GET_PMU_CTX() != ctx) {
1752 printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected ctx %p instead of %p\n",
1753 smp_processor_id(),
1754 GET_PMU_CTX(), ctx);
1755 return;
1758 DPRINT(("on CPU%d forcing system wide stop for [%d]\n", smp_processor_id(), task_pid_nr(ctx->ctx_task)));
1760 * the context is already protected in pfm_close(), we simply
1761 * need to mask interrupts to avoid a PMU interrupt race on
1762 * this CPU
1764 local_irq_save(flags);
1766 ret = pfm_context_unload(ctx, NULL, 0, regs);
1767 if (ret) {
1768 DPRINT(("context_unload returned %d\n", ret));
1772 * unmask interrupts, PMU interrupts are now spurious here
1774 local_irq_restore(flags);
1777 static void
1778 pfm_syswide_cleanup_other_cpu(pfm_context_t *ctx)
1780 int ret;
1782 DPRINT(("calling CPU%d for cleanup\n", ctx->ctx_cpu));
1783 ret = smp_call_function_single(ctx->ctx_cpu, pfm_syswide_force_stop, ctx, 0, 1);
1784 DPRINT(("called CPU%d for cleanup ret=%d\n", ctx->ctx_cpu, ret));
1786 #endif /* CONFIG_SMP */
1789 * called for each close(). Partially free resources.
1790 * When caller is self-monitoring, the context is unloaded.
1792 static int
1793 pfm_flush(struct file *filp, fl_owner_t id)
1795 pfm_context_t *ctx;
1796 struct task_struct *task;
1797 struct pt_regs *regs;
1798 unsigned long flags;
1799 unsigned long smpl_buf_size = 0UL;
1800 void *smpl_buf_vaddr = NULL;
1801 int state, is_system;
1803 if (PFM_IS_FILE(filp) == 0) {
1804 DPRINT(("bad magic for\n"));
1805 return -EBADF;
1808 ctx = (pfm_context_t *)filp->private_data;
1809 if (ctx == NULL) {
1810 printk(KERN_ERR "perfmon: pfm_flush: NULL ctx [%d]\n", task_pid_nr(current));
1811 return -EBADF;
1815 * remove our file from the async queue, if we use this mode.
1816 * This can be done without the context being protected. We come
1817 * here when the context has become unreachable by other tasks.
1819 * We may still have active monitoring at this point and we may
1820 * end up in pfm_overflow_handler(). However, fasync_helper()
1821 * operates with interrupts disabled and it cleans up the
1822 * queue. If the PMU handler is called prior to entering
1823 * fasync_helper() then it will send a signal. If it is
1824 * invoked after, it will find an empty queue and no
1825 * signal will be sent. In both case, we are safe
1827 if (filp->f_flags & FASYNC) {
1828 DPRINT(("cleaning up async_queue=%p\n", ctx->ctx_async_queue));
1829 pfm_do_fasync (-1, filp, ctx, 0);
1832 PROTECT_CTX(ctx, flags);
1834 state = ctx->ctx_state;
1835 is_system = ctx->ctx_fl_system;
1837 task = PFM_CTX_TASK(ctx);
1838 regs = task_pt_regs(task);
1840 DPRINT(("ctx_state=%d is_current=%d\n",
1841 state,
1842 task == current ? 1 : 0));
1845 * if state == UNLOADED, then task is NULL
1849 * we must stop and unload because we are losing access to the context.
1851 if (task == current) {
1852 #ifdef CONFIG_SMP
1854 * the task IS the owner but it migrated to another CPU: that's bad
1855 * but we must handle this cleanly. Unfortunately, the kernel does
1856 * not provide a mechanism to block migration (while the context is loaded).
1858 * We need to release the resource on the ORIGINAL cpu.
1860 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
1862 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
1864 * keep context protected but unmask interrupt for IPI
1866 local_irq_restore(flags);
1868 pfm_syswide_cleanup_other_cpu(ctx);
1871 * restore interrupt masking
1873 local_irq_save(flags);
1876 * context is unloaded at this point
1878 } else
1879 #endif /* CONFIG_SMP */
1882 DPRINT(("forcing unload\n"));
1884 * stop and unload, returning with state UNLOADED
1885 * and session unreserved.
1887 pfm_context_unload(ctx, NULL, 0, regs);
1889 DPRINT(("ctx_state=%d\n", ctx->ctx_state));
1894 * remove virtual mapping, if any, for the calling task.
1895 * cannot reset ctx field until last user is calling close().
1897 * ctx_smpl_vaddr must never be cleared because it is needed
1898 * by every task with access to the context
1900 * When called from do_exit(), the mm context is gone already, therefore
1901 * mm is NULL, i.e., the VMA is already gone and we do not have to
1902 * do anything here
1904 if (ctx->ctx_smpl_vaddr && current->mm) {
1905 smpl_buf_vaddr = ctx->ctx_smpl_vaddr;
1906 smpl_buf_size = ctx->ctx_smpl_size;
1909 UNPROTECT_CTX(ctx, flags);
1912 * if there was a mapping, then we systematically remove it
1913 * at this point. Cannot be done inside critical section
1914 * because some VM function reenables interrupts.
1917 if (smpl_buf_vaddr) pfm_remove_smpl_mapping(current, smpl_buf_vaddr, smpl_buf_size);
1919 return 0;
1922 * called either on explicit close() or from exit_files().
1923 * Only the LAST user of the file gets to this point, i.e., it is
1924 * called only ONCE.
1926 * IMPORTANT: we get called ONLY when the refcnt on the file gets to zero
1927 * (fput()),i.e, last task to access the file. Nobody else can access the
1928 * file at this point.
1930 * When called from exit_files(), the VMA has been freed because exit_mm()
1931 * is executed before exit_files().
1933 * When called from exit_files(), the current task is not yet ZOMBIE but we
1934 * flush the PMU state to the context.
1936 static int
1937 pfm_close(struct inode *inode, struct file *filp)
1939 pfm_context_t *ctx;
1940 struct task_struct *task;
1941 struct pt_regs *regs;
1942 DECLARE_WAITQUEUE(wait, current);
1943 unsigned long flags;
1944 unsigned long smpl_buf_size = 0UL;
1945 void *smpl_buf_addr = NULL;
1946 int free_possible = 1;
1947 int state, is_system;
1949 DPRINT(("pfm_close called private=%p\n", filp->private_data));
1951 if (PFM_IS_FILE(filp) == 0) {
1952 DPRINT(("bad magic\n"));
1953 return -EBADF;
1956 ctx = (pfm_context_t *)filp->private_data;
1957 if (ctx == NULL) {
1958 printk(KERN_ERR "perfmon: pfm_close: NULL ctx [%d]\n", task_pid_nr(current));
1959 return -EBADF;
1962 PROTECT_CTX(ctx, flags);
1964 state = ctx->ctx_state;
1965 is_system = ctx->ctx_fl_system;
1967 task = PFM_CTX_TASK(ctx);
1968 regs = task_pt_regs(task);
1970 DPRINT(("ctx_state=%d is_current=%d\n",
1971 state,
1972 task == current ? 1 : 0));
1975 * if task == current, then pfm_flush() unloaded the context
1977 if (state == PFM_CTX_UNLOADED) goto doit;
1980 * context is loaded/masked and task != current, we need to
1981 * either force an unload or go zombie
1985 * The task is currently blocked or will block after an overflow.
1986 * we must force it to wakeup to get out of the
1987 * MASKED state and transition to the unloaded state by itself.
1989 * This situation is only possible for per-task mode
1991 if (state == PFM_CTX_MASKED && CTX_OVFL_NOBLOCK(ctx) == 0) {
1994 * set a "partial" zombie state to be checked
1995 * upon return from down() in pfm_handle_work().
1997 * We cannot use the ZOMBIE state, because it is checked
1998 * by pfm_load_regs() which is called upon wakeup from down().
1999 * In such case, it would free the context and then we would
2000 * return to pfm_handle_work() which would access the
2001 * stale context. Instead, we set a flag invisible to pfm_load_regs()
2002 * but visible to pfm_handle_work().
2004 * For some window of time, we have a zombie context with
2005 * ctx_state = MASKED and not ZOMBIE
2007 ctx->ctx_fl_going_zombie = 1;
2010 * force task to wake up from MASKED state
2012 complete(&ctx->ctx_restart_done);
2014 DPRINT(("waking up ctx_state=%d\n", state));
2017 * put ourself to sleep waiting for the other
2018 * task to report completion
2020 * the context is protected by mutex, therefore there
2021 * is no risk of being notified of completion before
2022 * begin actually on the waitq.
2024 set_current_state(TASK_INTERRUPTIBLE);
2025 add_wait_queue(&ctx->ctx_zombieq, &wait);
2027 UNPROTECT_CTX(ctx, flags);
2030 * XXX: check for signals :
2031 * - ok for explicit close
2032 * - not ok when coming from exit_files()
2034 schedule();
2037 PROTECT_CTX(ctx, flags);
2040 remove_wait_queue(&ctx->ctx_zombieq, &wait);
2041 set_current_state(TASK_RUNNING);
2044 * context is unloaded at this point
2046 DPRINT(("after zombie wakeup ctx_state=%d for\n", state));
2048 else if (task != current) {
2049 #ifdef CONFIG_SMP
2051 * switch context to zombie state
2053 ctx->ctx_state = PFM_CTX_ZOMBIE;
2055 DPRINT(("zombie ctx for [%d]\n", task_pid_nr(task)));
2057 * cannot free the context on the spot. deferred until
2058 * the task notices the ZOMBIE state
2060 free_possible = 0;
2061 #else
2062 pfm_context_unload(ctx, NULL, 0, regs);
2063 #endif
2066 doit:
2067 /* reload state, may have changed during opening of critical section */
2068 state = ctx->ctx_state;
2071 * the context is still attached to a task (possibly current)
2072 * we cannot destroy it right now
2076 * we must free the sampling buffer right here because
2077 * we cannot rely on it being cleaned up later by the
2078 * monitored task. It is not possible to free vmalloc'ed
2079 * memory in pfm_load_regs(). Instead, we remove the buffer
2080 * now. should there be subsequent PMU overflow originally
2081 * meant for sampling, the will be converted to spurious
2082 * and that's fine because the monitoring tools is gone anyway.
2084 if (ctx->ctx_smpl_hdr) {
2085 smpl_buf_addr = ctx->ctx_smpl_hdr;
2086 smpl_buf_size = ctx->ctx_smpl_size;
2087 /* no more sampling */
2088 ctx->ctx_smpl_hdr = NULL;
2089 ctx->ctx_fl_is_sampling = 0;
2092 DPRINT(("ctx_state=%d free_possible=%d addr=%p size=%lu\n",
2093 state,
2094 free_possible,
2095 smpl_buf_addr,
2096 smpl_buf_size));
2098 if (smpl_buf_addr) pfm_exit_smpl_buffer(ctx->ctx_buf_fmt);
2101 * UNLOADED that the session has already been unreserved.
2103 if (state == PFM_CTX_ZOMBIE) {
2104 pfm_unreserve_session(ctx, ctx->ctx_fl_system , ctx->ctx_cpu);
2108 * disconnect file descriptor from context must be done
2109 * before we unlock.
2111 filp->private_data = NULL;
2114 * if we free on the spot, the context is now completely unreachable
2115 * from the callers side. The monitored task side is also cut, so we
2116 * can freely cut.
2118 * If we have a deferred free, only the caller side is disconnected.
2120 UNPROTECT_CTX(ctx, flags);
2123 * All memory free operations (especially for vmalloc'ed memory)
2124 * MUST be done with interrupts ENABLED.
2126 if (smpl_buf_addr) pfm_rvfree(smpl_buf_addr, smpl_buf_size);
2129 * return the memory used by the context
2131 if (free_possible) pfm_context_free(ctx);
2133 return 0;
2136 static int
2137 pfm_no_open(struct inode *irrelevant, struct file *dontcare)
2139 DPRINT(("pfm_no_open called\n"));
2140 return -ENXIO;
2145 static const struct file_operations pfm_file_ops = {
2146 .llseek = no_llseek,
2147 .read = pfm_read,
2148 .write = pfm_write,
2149 .poll = pfm_poll,
2150 .ioctl = pfm_ioctl,
2151 .open = pfm_no_open, /* special open code to disallow open via /proc */
2152 .fasync = pfm_fasync,
2153 .release = pfm_close,
2154 .flush = pfm_flush
2157 static int
2158 pfmfs_delete_dentry(struct dentry *dentry)
2160 return 1;
2163 static struct dentry_operations pfmfs_dentry_operations = {
2164 .d_delete = pfmfs_delete_dentry,
2168 static int
2169 pfm_alloc_fd(struct file **cfile)
2171 int fd, ret = 0;
2172 struct file *file = NULL;
2173 struct inode * inode;
2174 char name[32];
2175 struct qstr this;
2177 fd = get_unused_fd();
2178 if (fd < 0) return -ENFILE;
2180 ret = -ENFILE;
2182 file = get_empty_filp();
2183 if (!file) goto out;
2186 * allocate a new inode
2188 inode = new_inode(pfmfs_mnt->mnt_sb);
2189 if (!inode) goto out;
2191 DPRINT(("new inode ino=%ld @%p\n", inode->i_ino, inode));
2193 inode->i_mode = S_IFCHR|S_IRUGO;
2194 inode->i_uid = current->fsuid;
2195 inode->i_gid = current->fsgid;
2197 sprintf(name, "[%lu]", inode->i_ino);
2198 this.name = name;
2199 this.len = strlen(name);
2200 this.hash = inode->i_ino;
2202 ret = -ENOMEM;
2205 * allocate a new dcache entry
2207 file->f_path.dentry = d_alloc(pfmfs_mnt->mnt_sb->s_root, &this);
2208 if (!file->f_path.dentry) goto out;
2210 file->f_path.dentry->d_op = &pfmfs_dentry_operations;
2212 d_add(file->f_path.dentry, inode);
2213 file->f_path.mnt = mntget(pfmfs_mnt);
2214 file->f_mapping = inode->i_mapping;
2216 file->f_op = &pfm_file_ops;
2217 file->f_mode = FMODE_READ;
2218 file->f_flags = O_RDONLY;
2219 file->f_pos = 0;
2222 * may have to delay until context is attached?
2224 fd_install(fd, file);
2227 * the file structure we will use
2229 *cfile = file;
2231 return fd;
2232 out:
2233 if (file) put_filp(file);
2234 put_unused_fd(fd);
2235 return ret;
2238 static void
2239 pfm_free_fd(int fd, struct file *file)
2241 struct files_struct *files = current->files;
2242 struct fdtable *fdt;
2245 * there ie no fd_uninstall(), so we do it here
2247 spin_lock(&files->file_lock);
2248 fdt = files_fdtable(files);
2249 rcu_assign_pointer(fdt->fd[fd], NULL);
2250 spin_unlock(&files->file_lock);
2252 if (file)
2253 put_filp(file);
2254 put_unused_fd(fd);
2257 static int
2258 pfm_remap_buffer(struct vm_area_struct *vma, unsigned long buf, unsigned long addr, unsigned long size)
2260 DPRINT(("CPU%d buf=0x%lx addr=0x%lx size=%ld\n", smp_processor_id(), buf, addr, size));
2262 while (size > 0) {
2263 unsigned long pfn = ia64_tpa(buf) >> PAGE_SHIFT;
2266 if (remap_pfn_range(vma, addr, pfn, PAGE_SIZE, PAGE_READONLY))
2267 return -ENOMEM;
2269 addr += PAGE_SIZE;
2270 buf += PAGE_SIZE;
2271 size -= PAGE_SIZE;
2273 return 0;
2277 * allocate a sampling buffer and remaps it into the user address space of the task
2279 static int
2280 pfm_smpl_buffer_alloc(struct task_struct *task, struct file *filp, pfm_context_t *ctx, unsigned long rsize, void **user_vaddr)
2282 struct mm_struct *mm = task->mm;
2283 struct vm_area_struct *vma = NULL;
2284 unsigned long size;
2285 void *smpl_buf;
2289 * the fixed header + requested size and align to page boundary
2291 size = PAGE_ALIGN(rsize);
2293 DPRINT(("sampling buffer rsize=%lu size=%lu bytes\n", rsize, size));
2296 * check requested size to avoid Denial-of-service attacks
2297 * XXX: may have to refine this test
2298 * Check against address space limit.
2300 * if ((mm->total_vm << PAGE_SHIFT) + len> task->rlim[RLIMIT_AS].rlim_cur)
2301 * return -ENOMEM;
2303 if (size > task->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
2304 return -ENOMEM;
2307 * We do the easy to undo allocations first.
2309 * pfm_rvmalloc(), clears the buffer, so there is no leak
2311 smpl_buf = pfm_rvmalloc(size);
2312 if (smpl_buf == NULL) {
2313 DPRINT(("Can't allocate sampling buffer\n"));
2314 return -ENOMEM;
2317 DPRINT(("smpl_buf @%p\n", smpl_buf));
2319 /* allocate vma */
2320 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2321 if (!vma) {
2322 DPRINT(("Cannot allocate vma\n"));
2323 goto error_kmem;
2327 * partially initialize the vma for the sampling buffer
2329 vma->vm_mm = mm;
2330 vma->vm_file = filp;
2331 vma->vm_flags = VM_READ| VM_MAYREAD |VM_RESERVED;
2332 vma->vm_page_prot = PAGE_READONLY; /* XXX may need to change */
2335 * Now we have everything we need and we can initialize
2336 * and connect all the data structures
2339 ctx->ctx_smpl_hdr = smpl_buf;
2340 ctx->ctx_smpl_size = size; /* aligned size */
2343 * Let's do the difficult operations next.
2345 * now we atomically find some area in the address space and
2346 * remap the buffer in it.
2348 down_write(&task->mm->mmap_sem);
2350 /* find some free area in address space, must have mmap sem held */
2351 vma->vm_start = pfm_get_unmapped_area(NULL, 0, size, 0, MAP_PRIVATE|MAP_ANONYMOUS, 0);
2352 if (vma->vm_start == 0UL) {
2353 DPRINT(("Cannot find unmapped area for size %ld\n", size));
2354 up_write(&task->mm->mmap_sem);
2355 goto error;
2357 vma->vm_end = vma->vm_start + size;
2358 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2360 DPRINT(("aligned size=%ld, hdr=%p mapped @0x%lx\n", size, ctx->ctx_smpl_hdr, vma->vm_start));
2362 /* can only be applied to current task, need to have the mm semaphore held when called */
2363 if (pfm_remap_buffer(vma, (unsigned long)smpl_buf, vma->vm_start, size)) {
2364 DPRINT(("Can't remap buffer\n"));
2365 up_write(&task->mm->mmap_sem);
2366 goto error;
2369 get_file(filp);
2372 * now insert the vma in the vm list for the process, must be
2373 * done with mmap lock held
2375 insert_vm_struct(mm, vma);
2377 mm->total_vm += size >> PAGE_SHIFT;
2378 vm_stat_account(vma->vm_mm, vma->vm_flags, vma->vm_file,
2379 vma_pages(vma));
2380 up_write(&task->mm->mmap_sem);
2383 * keep track of user level virtual address
2385 ctx->ctx_smpl_vaddr = (void *)vma->vm_start;
2386 *(unsigned long *)user_vaddr = vma->vm_start;
2388 return 0;
2390 error:
2391 kmem_cache_free(vm_area_cachep, vma);
2392 error_kmem:
2393 pfm_rvfree(smpl_buf, size);
2395 return -ENOMEM;
2399 * XXX: do something better here
2401 static int
2402 pfm_bad_permissions(struct task_struct *task)
2404 /* inspired by ptrace_attach() */
2405 DPRINT(("cur: uid=%d gid=%d task: euid=%d suid=%d uid=%d egid=%d sgid=%d\n",
2406 current->uid,
2407 current->gid,
2408 task->euid,
2409 task->suid,
2410 task->uid,
2411 task->egid,
2412 task->sgid));
2414 return ((current->uid != task->euid)
2415 || (current->uid != task->suid)
2416 || (current->uid != task->uid)
2417 || (current->gid != task->egid)
2418 || (current->gid != task->sgid)
2419 || (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE);
2422 static int
2423 pfarg_is_sane(struct task_struct *task, pfarg_context_t *pfx)
2425 int ctx_flags;
2427 /* valid signal */
2429 ctx_flags = pfx->ctx_flags;
2431 if (ctx_flags & PFM_FL_SYSTEM_WIDE) {
2434 * cannot block in this mode
2436 if (ctx_flags & PFM_FL_NOTIFY_BLOCK) {
2437 DPRINT(("cannot use blocking mode when in system wide monitoring\n"));
2438 return -EINVAL;
2440 } else {
2442 /* probably more to add here */
2444 return 0;
2447 static int
2448 pfm_setup_buffer_fmt(struct task_struct *task, struct file *filp, pfm_context_t *ctx, unsigned int ctx_flags,
2449 unsigned int cpu, pfarg_context_t *arg)
2451 pfm_buffer_fmt_t *fmt = NULL;
2452 unsigned long size = 0UL;
2453 void *uaddr = NULL;
2454 void *fmt_arg = NULL;
2455 int ret = 0;
2456 #define PFM_CTXARG_BUF_ARG(a) (pfm_buffer_fmt_t *)(a+1)
2458 /* invoke and lock buffer format, if found */
2459 fmt = pfm_find_buffer_fmt(arg->ctx_smpl_buf_id);
2460 if (fmt == NULL) {
2461 DPRINT(("[%d] cannot find buffer format\n", task_pid_nr(task)));
2462 return -EINVAL;
2466 * buffer argument MUST be contiguous to pfarg_context_t
2468 if (fmt->fmt_arg_size) fmt_arg = PFM_CTXARG_BUF_ARG(arg);
2470 ret = pfm_buf_fmt_validate(fmt, task, ctx_flags, cpu, fmt_arg);
2472 DPRINT(("[%d] after validate(0x%x,%d,%p)=%d\n", task_pid_nr(task), ctx_flags, cpu, fmt_arg, ret));
2474 if (ret) goto error;
2476 /* link buffer format and context */
2477 ctx->ctx_buf_fmt = fmt;
2480 * check if buffer format wants to use perfmon buffer allocation/mapping service
2482 ret = pfm_buf_fmt_getsize(fmt, task, ctx_flags, cpu, fmt_arg, &size);
2483 if (ret) goto error;
2485 if (size) {
2487 * buffer is always remapped into the caller's address space
2489 ret = pfm_smpl_buffer_alloc(current, filp, ctx, size, &uaddr);
2490 if (ret) goto error;
2492 /* keep track of user address of buffer */
2493 arg->ctx_smpl_vaddr = uaddr;
2495 ret = pfm_buf_fmt_init(fmt, task, ctx->ctx_smpl_hdr, ctx_flags, cpu, fmt_arg);
2497 error:
2498 return ret;
2501 static void
2502 pfm_reset_pmu_state(pfm_context_t *ctx)
2504 int i;
2507 * install reset values for PMC.
2509 for (i=1; PMC_IS_LAST(i) == 0; i++) {
2510 if (PMC_IS_IMPL(i) == 0) continue;
2511 ctx->ctx_pmcs[i] = PMC_DFL_VAL(i);
2512 DPRINT(("pmc[%d]=0x%lx\n", i, ctx->ctx_pmcs[i]));
2515 * PMD registers are set to 0UL when the context in memset()
2519 * On context switched restore, we must restore ALL pmc and ALL pmd even
2520 * when they are not actively used by the task. In UP, the incoming process
2521 * may otherwise pick up left over PMC, PMD state from the previous process.
2522 * As opposed to PMD, stale PMC can cause harm to the incoming
2523 * process because they may change what is being measured.
2524 * Therefore, we must systematically reinstall the entire
2525 * PMC state. In SMP, the same thing is possible on the
2526 * same CPU but also on between 2 CPUs.
2528 * The problem with PMD is information leaking especially
2529 * to user level when psr.sp=0
2531 * There is unfortunately no easy way to avoid this problem
2532 * on either UP or SMP. This definitively slows down the
2533 * pfm_load_regs() function.
2537 * bitmask of all PMCs accessible to this context
2539 * PMC0 is treated differently.
2541 ctx->ctx_all_pmcs[0] = pmu_conf->impl_pmcs[0] & ~0x1;
2544 * bitmask of all PMDs that are accessible to this context
2546 ctx->ctx_all_pmds[0] = pmu_conf->impl_pmds[0];
2548 DPRINT(("<%d> all_pmcs=0x%lx all_pmds=0x%lx\n", ctx->ctx_fd, ctx->ctx_all_pmcs[0],ctx->ctx_all_pmds[0]));
2551 * useful in case of re-enable after disable
2553 ctx->ctx_used_ibrs[0] = 0UL;
2554 ctx->ctx_used_dbrs[0] = 0UL;
2557 static int
2558 pfm_ctx_getsize(void *arg, size_t *sz)
2560 pfarg_context_t *req = (pfarg_context_t *)arg;
2561 pfm_buffer_fmt_t *fmt;
2563 *sz = 0;
2565 if (!pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) return 0;
2567 fmt = pfm_find_buffer_fmt(req->ctx_smpl_buf_id);
2568 if (fmt == NULL) {
2569 DPRINT(("cannot find buffer format\n"));
2570 return -EINVAL;
2572 /* get just enough to copy in user parameters */
2573 *sz = fmt->fmt_arg_size;
2574 DPRINT(("arg_size=%lu\n", *sz));
2576 return 0;
2582 * cannot attach if :
2583 * - kernel task
2584 * - task not owned by caller
2585 * - task incompatible with context mode
2587 static int
2588 pfm_task_incompatible(pfm_context_t *ctx, struct task_struct *task)
2591 * no kernel task or task not owner by caller
2593 if (task->mm == NULL) {
2594 DPRINT(("task [%d] has not memory context (kernel thread)\n", task_pid_nr(task)));
2595 return -EPERM;
2597 if (pfm_bad_permissions(task)) {
2598 DPRINT(("no permission to attach to [%d]\n", task_pid_nr(task)));
2599 return -EPERM;
2602 * cannot block in self-monitoring mode
2604 if (CTX_OVFL_NOBLOCK(ctx) == 0 && task == current) {
2605 DPRINT(("cannot load a blocking context on self for [%d]\n", task_pid_nr(task)));
2606 return -EINVAL;
2609 if (task->exit_state == EXIT_ZOMBIE) {
2610 DPRINT(("cannot attach to zombie task [%d]\n", task_pid_nr(task)));
2611 return -EBUSY;
2615 * always ok for self
2617 if (task == current) return 0;
2619 if (!task_is_stopped_or_traced(task)) {
2620 DPRINT(("cannot attach to non-stopped task [%d] state=%ld\n", task_pid_nr(task), task->state));
2621 return -EBUSY;
2624 * make sure the task is off any CPU
2626 wait_task_inactive(task);
2628 /* more to come... */
2630 return 0;
2633 static int
2634 pfm_get_task(pfm_context_t *ctx, pid_t pid, struct task_struct **task)
2636 struct task_struct *p = current;
2637 int ret;
2639 /* XXX: need to add more checks here */
2640 if (pid < 2) return -EPERM;
2642 if (pid != task_pid_vnr(current)) {
2644 read_lock(&tasklist_lock);
2646 p = find_task_by_vpid(pid);
2648 /* make sure task cannot go away while we operate on it */
2649 if (p) get_task_struct(p);
2651 read_unlock(&tasklist_lock);
2653 if (p == NULL) return -ESRCH;
2656 ret = pfm_task_incompatible(ctx, p);
2657 if (ret == 0) {
2658 *task = p;
2659 } else if (p != current) {
2660 pfm_put_task(p);
2662 return ret;
2667 static int
2668 pfm_context_create(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
2670 pfarg_context_t *req = (pfarg_context_t *)arg;
2671 struct file *filp;
2672 int ctx_flags;
2673 int ret;
2675 /* let's check the arguments first */
2676 ret = pfarg_is_sane(current, req);
2677 if (ret < 0) return ret;
2679 ctx_flags = req->ctx_flags;
2681 ret = -ENOMEM;
2683 ctx = pfm_context_alloc();
2684 if (!ctx) goto error;
2686 ret = pfm_alloc_fd(&filp);
2687 if (ret < 0) goto error_file;
2689 req->ctx_fd = ctx->ctx_fd = ret;
2692 * attach context to file
2694 filp->private_data = ctx;
2697 * does the user want to sample?
2699 if (pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) {
2700 ret = pfm_setup_buffer_fmt(current, filp, ctx, ctx_flags, 0, req);
2701 if (ret) goto buffer_error;
2705 * init context protection lock
2707 spin_lock_init(&ctx->ctx_lock);
2710 * context is unloaded
2712 ctx->ctx_state = PFM_CTX_UNLOADED;
2715 * initialization of context's flags
2717 ctx->ctx_fl_block = (ctx_flags & PFM_FL_NOTIFY_BLOCK) ? 1 : 0;
2718 ctx->ctx_fl_system = (ctx_flags & PFM_FL_SYSTEM_WIDE) ? 1: 0;
2719 ctx->ctx_fl_is_sampling = ctx->ctx_buf_fmt ? 1 : 0; /* assume record() is defined */
2720 ctx->ctx_fl_no_msg = (ctx_flags & PFM_FL_OVFL_NO_MSG) ? 1: 0;
2722 * will move to set properties
2723 * ctx->ctx_fl_excl_idle = (ctx_flags & PFM_FL_EXCL_IDLE) ? 1: 0;
2727 * init restart semaphore to locked
2729 init_completion(&ctx->ctx_restart_done);
2732 * activation is used in SMP only
2734 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
2735 SET_LAST_CPU(ctx, -1);
2738 * initialize notification message queue
2740 ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
2741 init_waitqueue_head(&ctx->ctx_msgq_wait);
2742 init_waitqueue_head(&ctx->ctx_zombieq);
2744 DPRINT(("ctx=%p flags=0x%x system=%d notify_block=%d excl_idle=%d no_msg=%d ctx_fd=%d \n",
2745 ctx,
2746 ctx_flags,
2747 ctx->ctx_fl_system,
2748 ctx->ctx_fl_block,
2749 ctx->ctx_fl_excl_idle,
2750 ctx->ctx_fl_no_msg,
2751 ctx->ctx_fd));
2754 * initialize soft PMU state
2756 pfm_reset_pmu_state(ctx);
2758 return 0;
2760 buffer_error:
2761 pfm_free_fd(ctx->ctx_fd, filp);
2763 if (ctx->ctx_buf_fmt) {
2764 pfm_buf_fmt_exit(ctx->ctx_buf_fmt, current, NULL, regs);
2766 error_file:
2767 pfm_context_free(ctx);
2769 error:
2770 return ret;
2773 static inline unsigned long
2774 pfm_new_counter_value (pfm_counter_t *reg, int is_long_reset)
2776 unsigned long val = is_long_reset ? reg->long_reset : reg->short_reset;
2777 unsigned long new_seed, old_seed = reg->seed, mask = reg->mask;
2778 extern unsigned long carta_random32 (unsigned long seed);
2780 if (reg->flags & PFM_REGFL_RANDOM) {
2781 new_seed = carta_random32(old_seed);
2782 val -= (old_seed & mask); /* counter values are negative numbers! */
2783 if ((mask >> 32) != 0)
2784 /* construct a full 64-bit random value: */
2785 new_seed |= carta_random32(old_seed >> 32) << 32;
2786 reg->seed = new_seed;
2788 reg->lval = val;
2789 return val;
2792 static void
2793 pfm_reset_regs_masked(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
2795 unsigned long mask = ovfl_regs[0];
2796 unsigned long reset_others = 0UL;
2797 unsigned long val;
2798 int i;
2801 * now restore reset value on sampling overflowed counters
2803 mask >>= PMU_FIRST_COUNTER;
2804 for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
2806 if ((mask & 0x1UL) == 0UL) continue;
2808 ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
2809 reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
2811 DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
2815 * Now take care of resetting the other registers
2817 for(i = 0; reset_others; i++, reset_others >>= 1) {
2819 if ((reset_others & 0x1) == 0) continue;
2821 ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
2823 DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
2824 is_long_reset ? "long" : "short", i, val));
2828 static void
2829 pfm_reset_regs(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
2831 unsigned long mask = ovfl_regs[0];
2832 unsigned long reset_others = 0UL;
2833 unsigned long val;
2834 int i;
2836 DPRINT_ovfl(("ovfl_regs=0x%lx is_long_reset=%d\n", ovfl_regs[0], is_long_reset));
2838 if (ctx->ctx_state == PFM_CTX_MASKED) {
2839 pfm_reset_regs_masked(ctx, ovfl_regs, is_long_reset);
2840 return;
2844 * now restore reset value on sampling overflowed counters
2846 mask >>= PMU_FIRST_COUNTER;
2847 for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
2849 if ((mask & 0x1UL) == 0UL) continue;
2851 val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
2852 reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
2854 DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
2856 pfm_write_soft_counter(ctx, i, val);
2860 * Now take care of resetting the other registers
2862 for(i = 0; reset_others; i++, reset_others >>= 1) {
2864 if ((reset_others & 0x1) == 0) continue;
2866 val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
2868 if (PMD_IS_COUNTING(i)) {
2869 pfm_write_soft_counter(ctx, i, val);
2870 } else {
2871 ia64_set_pmd(i, val);
2873 DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
2874 is_long_reset ? "long" : "short", i, val));
2876 ia64_srlz_d();
2879 static int
2880 pfm_write_pmcs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
2882 struct task_struct *task;
2883 pfarg_reg_t *req = (pfarg_reg_t *)arg;
2884 unsigned long value, pmc_pm;
2885 unsigned long smpl_pmds, reset_pmds, impl_pmds;
2886 unsigned int cnum, reg_flags, flags, pmc_type;
2887 int i, can_access_pmu = 0, is_loaded, is_system, expert_mode;
2888 int is_monitor, is_counting, state;
2889 int ret = -EINVAL;
2890 pfm_reg_check_t wr_func;
2891 #define PFM_CHECK_PMC_PM(x, y, z) ((x)->ctx_fl_system ^ PMC_PM(y, z))
2893 state = ctx->ctx_state;
2894 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
2895 is_system = ctx->ctx_fl_system;
2896 task = ctx->ctx_task;
2897 impl_pmds = pmu_conf->impl_pmds[0];
2899 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
2901 if (is_loaded) {
2903 * In system wide and when the context is loaded, access can only happen
2904 * when the caller is running on the CPU being monitored by the session.
2905 * It does not have to be the owner (ctx_task) of the context per se.
2907 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
2908 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
2909 return -EBUSY;
2911 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
2913 expert_mode = pfm_sysctl.expert_mode;
2915 for (i = 0; i < count; i++, req++) {
2917 cnum = req->reg_num;
2918 reg_flags = req->reg_flags;
2919 value = req->reg_value;
2920 smpl_pmds = req->reg_smpl_pmds[0];
2921 reset_pmds = req->reg_reset_pmds[0];
2922 flags = 0;
2925 if (cnum >= PMU_MAX_PMCS) {
2926 DPRINT(("pmc%u is invalid\n", cnum));
2927 goto error;
2930 pmc_type = pmu_conf->pmc_desc[cnum].type;
2931 pmc_pm = (value >> pmu_conf->pmc_desc[cnum].pm_pos) & 0x1;
2932 is_counting = (pmc_type & PFM_REG_COUNTING) == PFM_REG_COUNTING ? 1 : 0;
2933 is_monitor = (pmc_type & PFM_REG_MONITOR) == PFM_REG_MONITOR ? 1 : 0;
2936 * we reject all non implemented PMC as well
2937 * as attempts to modify PMC[0-3] which are used
2938 * as status registers by the PMU
2940 if ((pmc_type & PFM_REG_IMPL) == 0 || (pmc_type & PFM_REG_CONTROL) == PFM_REG_CONTROL) {
2941 DPRINT(("pmc%u is unimplemented or no-access pmc_type=%x\n", cnum, pmc_type));
2942 goto error;
2944 wr_func = pmu_conf->pmc_desc[cnum].write_check;
2946 * If the PMC is a monitor, then if the value is not the default:
2947 * - system-wide session: PMCx.pm=1 (privileged monitor)
2948 * - per-task : PMCx.pm=0 (user monitor)
2950 if (is_monitor && value != PMC_DFL_VAL(cnum) && is_system ^ pmc_pm) {
2951 DPRINT(("pmc%u pmc_pm=%lu is_system=%d\n",
2952 cnum,
2953 pmc_pm,
2954 is_system));
2955 goto error;
2958 if (is_counting) {
2960 * enforce generation of overflow interrupt. Necessary on all
2961 * CPUs.
2963 value |= 1 << PMU_PMC_OI;
2965 if (reg_flags & PFM_REGFL_OVFL_NOTIFY) {
2966 flags |= PFM_REGFL_OVFL_NOTIFY;
2969 if (reg_flags & PFM_REGFL_RANDOM) flags |= PFM_REGFL_RANDOM;
2971 /* verify validity of smpl_pmds */
2972 if ((smpl_pmds & impl_pmds) != smpl_pmds) {
2973 DPRINT(("invalid smpl_pmds 0x%lx for pmc%u\n", smpl_pmds, cnum));
2974 goto error;
2977 /* verify validity of reset_pmds */
2978 if ((reset_pmds & impl_pmds) != reset_pmds) {
2979 DPRINT(("invalid reset_pmds 0x%lx for pmc%u\n", reset_pmds, cnum));
2980 goto error;
2982 } else {
2983 if (reg_flags & (PFM_REGFL_OVFL_NOTIFY|PFM_REGFL_RANDOM)) {
2984 DPRINT(("cannot set ovfl_notify or random on pmc%u\n", cnum));
2985 goto error;
2987 /* eventid on non-counting monitors are ignored */
2991 * execute write checker, if any
2993 if (likely(expert_mode == 0 && wr_func)) {
2994 ret = (*wr_func)(task, ctx, cnum, &value, regs);
2995 if (ret) goto error;
2996 ret = -EINVAL;
3000 * no error on this register
3002 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
3005 * Now we commit the changes to the software state
3009 * update overflow information
3011 if (is_counting) {
3013 * full flag update each time a register is programmed
3015 ctx->ctx_pmds[cnum].flags = flags;
3017 ctx->ctx_pmds[cnum].reset_pmds[0] = reset_pmds;
3018 ctx->ctx_pmds[cnum].smpl_pmds[0] = smpl_pmds;
3019 ctx->ctx_pmds[cnum].eventid = req->reg_smpl_eventid;
3022 * Mark all PMDS to be accessed as used.
3024 * We do not keep track of PMC because we have to
3025 * systematically restore ALL of them.
3027 * We do not update the used_monitors mask, because
3028 * if we have not programmed them, then will be in
3029 * a quiescent state, therefore we will not need to
3030 * mask/restore then when context is MASKED.
3032 CTX_USED_PMD(ctx, reset_pmds);
3033 CTX_USED_PMD(ctx, smpl_pmds);
3035 * make sure we do not try to reset on
3036 * restart because we have established new values
3038 if (state == PFM_CTX_MASKED) ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
3041 * Needed in case the user does not initialize the equivalent
3042 * PMD. Clearing is done indirectly via pfm_reset_pmu_state() so there is no
3043 * possible leak here.
3045 CTX_USED_PMD(ctx, pmu_conf->pmc_desc[cnum].dep_pmd[0]);
3048 * keep track of the monitor PMC that we are using.
3049 * we save the value of the pmc in ctx_pmcs[] and if
3050 * the monitoring is not stopped for the context we also
3051 * place it in the saved state area so that it will be
3052 * picked up later by the context switch code.
3054 * The value in ctx_pmcs[] can only be changed in pfm_write_pmcs().
3056 * The value in th_pmcs[] may be modified on overflow, i.e., when
3057 * monitoring needs to be stopped.
3059 if (is_monitor) CTX_USED_MONITOR(ctx, 1UL << cnum);
3062 * update context state
3064 ctx->ctx_pmcs[cnum] = value;
3066 if (is_loaded) {
3068 * write thread state
3070 if (is_system == 0) ctx->th_pmcs[cnum] = value;
3073 * write hardware register if we can
3075 if (can_access_pmu) {
3076 ia64_set_pmc(cnum, value);
3078 #ifdef CONFIG_SMP
3079 else {
3081 * per-task SMP only here
3083 * we are guaranteed that the task is not running on the other CPU,
3084 * we indicate that this PMD will need to be reloaded if the task
3085 * is rescheduled on the CPU it ran last on.
3087 ctx->ctx_reload_pmcs[0] |= 1UL << cnum;
3089 #endif
3092 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",
3093 cnum,
3094 value,
3095 is_loaded,
3096 can_access_pmu,
3097 flags,
3098 ctx->ctx_all_pmcs[0],
3099 ctx->ctx_used_pmds[0],
3100 ctx->ctx_pmds[cnum].eventid,
3101 smpl_pmds,
3102 reset_pmds,
3103 ctx->ctx_reload_pmcs[0],
3104 ctx->ctx_used_monitors[0],
3105 ctx->ctx_ovfl_regs[0]));
3109 * make sure the changes are visible
3111 if (can_access_pmu) ia64_srlz_d();
3113 return 0;
3114 error:
3115 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3116 return ret;
3119 static int
3120 pfm_write_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3122 struct task_struct *task;
3123 pfarg_reg_t *req = (pfarg_reg_t *)arg;
3124 unsigned long value, hw_value, ovfl_mask;
3125 unsigned int cnum;
3126 int i, can_access_pmu = 0, state;
3127 int is_counting, is_loaded, is_system, expert_mode;
3128 int ret = -EINVAL;
3129 pfm_reg_check_t wr_func;
3132 state = ctx->ctx_state;
3133 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3134 is_system = ctx->ctx_fl_system;
3135 ovfl_mask = pmu_conf->ovfl_val;
3136 task = ctx->ctx_task;
3138 if (unlikely(state == PFM_CTX_ZOMBIE)) return -EINVAL;
3141 * on both UP and SMP, we can only write to the PMC when the task is
3142 * the owner of the local PMU.
3144 if (likely(is_loaded)) {
3146 * In system wide and when the context is loaded, access can only happen
3147 * when the caller is running on the CPU being monitored by the session.
3148 * It does not have to be the owner (ctx_task) of the context per se.
3150 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3151 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3152 return -EBUSY;
3154 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3156 expert_mode = pfm_sysctl.expert_mode;
3158 for (i = 0; i < count; i++, req++) {
3160 cnum = req->reg_num;
3161 value = req->reg_value;
3163 if (!PMD_IS_IMPL(cnum)) {
3164 DPRINT(("pmd[%u] is unimplemented or invalid\n", cnum));
3165 goto abort_mission;
3167 is_counting = PMD_IS_COUNTING(cnum);
3168 wr_func = pmu_conf->pmd_desc[cnum].write_check;
3171 * execute write checker, if any
3173 if (unlikely(expert_mode == 0 && wr_func)) {
3174 unsigned long v = value;
3176 ret = (*wr_func)(task, ctx, cnum, &v, regs);
3177 if (ret) goto abort_mission;
3179 value = v;
3180 ret = -EINVAL;
3184 * no error on this register
3186 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
3189 * now commit changes to software state
3191 hw_value = value;
3194 * update virtualized (64bits) counter
3196 if (is_counting) {
3198 * write context state
3200 ctx->ctx_pmds[cnum].lval = value;
3203 * when context is load we use the split value
3205 if (is_loaded) {
3206 hw_value = value & ovfl_mask;
3207 value = value & ~ovfl_mask;
3211 * update reset values (not just for counters)
3213 ctx->ctx_pmds[cnum].long_reset = req->reg_long_reset;
3214 ctx->ctx_pmds[cnum].short_reset = req->reg_short_reset;
3217 * update randomization parameters (not just for counters)
3219 ctx->ctx_pmds[cnum].seed = req->reg_random_seed;
3220 ctx->ctx_pmds[cnum].mask = req->reg_random_mask;
3223 * update context value
3225 ctx->ctx_pmds[cnum].val = value;
3228 * Keep track of what we use
3230 * We do not keep track of PMC because we have to
3231 * systematically restore ALL of them.
3233 CTX_USED_PMD(ctx, PMD_PMD_DEP(cnum));
3236 * mark this PMD register used as well
3238 CTX_USED_PMD(ctx, RDEP(cnum));
3241 * make sure we do not try to reset on
3242 * restart because we have established new values
3244 if (is_counting && state == PFM_CTX_MASKED) {
3245 ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
3248 if (is_loaded) {
3250 * write thread state
3252 if (is_system == 0) ctx->th_pmds[cnum] = hw_value;
3255 * write hardware register if we can
3257 if (can_access_pmu) {
3258 ia64_set_pmd(cnum, hw_value);
3259 } else {
3260 #ifdef CONFIG_SMP
3262 * we are guaranteed that the task is not running on the other CPU,
3263 * we indicate that this PMD will need to be reloaded if the task
3264 * is rescheduled on the CPU it ran last on.
3266 ctx->ctx_reload_pmds[0] |= 1UL << cnum;
3267 #endif
3271 DPRINT(("pmd[%u]=0x%lx ld=%d apmu=%d, hw_value=0x%lx ctx_pmd=0x%lx short_reset=0x%lx "
3272 "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",
3273 cnum,
3274 value,
3275 is_loaded,
3276 can_access_pmu,
3277 hw_value,
3278 ctx->ctx_pmds[cnum].val,
3279 ctx->ctx_pmds[cnum].short_reset,
3280 ctx->ctx_pmds[cnum].long_reset,
3281 PMC_OVFL_NOTIFY(ctx, cnum) ? 'Y':'N',
3282 ctx->ctx_pmds[cnum].seed,
3283 ctx->ctx_pmds[cnum].mask,
3284 ctx->ctx_used_pmds[0],
3285 ctx->ctx_pmds[cnum].reset_pmds[0],
3286 ctx->ctx_reload_pmds[0],
3287 ctx->ctx_all_pmds[0],
3288 ctx->ctx_ovfl_regs[0]));
3292 * make changes visible
3294 if (can_access_pmu) ia64_srlz_d();
3296 return 0;
3298 abort_mission:
3300 * for now, we have only one possibility for error
3302 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3303 return ret;
3307 * By the way of PROTECT_CONTEXT(), interrupts are masked while we are in this function.
3308 * Therefore we know, we do not have to worry about the PMU overflow interrupt. If an
3309 * interrupt is delivered during the call, it will be kept pending until we leave, making
3310 * it appears as if it had been generated at the UNPROTECT_CONTEXT(). At least we are
3311 * guaranteed to return consistent data to the user, it may simply be old. It is not
3312 * trivial to treat the overflow while inside the call because you may end up in
3313 * some module sampling buffer code causing deadlocks.
3315 static int
3316 pfm_read_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3318 struct task_struct *task;
3319 unsigned long val = 0UL, lval, ovfl_mask, sval;
3320 pfarg_reg_t *req = (pfarg_reg_t *)arg;
3321 unsigned int cnum, reg_flags = 0;
3322 int i, can_access_pmu = 0, state;
3323 int is_loaded, is_system, is_counting, expert_mode;
3324 int ret = -EINVAL;
3325 pfm_reg_check_t rd_func;
3328 * access is possible when loaded only for
3329 * self-monitoring tasks or in UP mode
3332 state = ctx->ctx_state;
3333 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3334 is_system = ctx->ctx_fl_system;
3335 ovfl_mask = pmu_conf->ovfl_val;
3336 task = ctx->ctx_task;
3338 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
3340 if (likely(is_loaded)) {
3342 * In system wide and when the context is loaded, access can only happen
3343 * when the caller is running on the CPU being monitored by the session.
3344 * It does not have to be the owner (ctx_task) of the context per se.
3346 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3347 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3348 return -EBUSY;
3351 * this can be true when not self-monitoring only in UP
3353 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3355 if (can_access_pmu) ia64_srlz_d();
3357 expert_mode = pfm_sysctl.expert_mode;
3359 DPRINT(("ld=%d apmu=%d ctx_state=%d\n",
3360 is_loaded,
3361 can_access_pmu,
3362 state));
3365 * on both UP and SMP, we can only read the PMD from the hardware register when
3366 * the task is the owner of the local PMU.
3369 for (i = 0; i < count; i++, req++) {
3371 cnum = req->reg_num;
3372 reg_flags = req->reg_flags;
3374 if (unlikely(!PMD_IS_IMPL(cnum))) goto error;
3376 * we can only read the register that we use. That includes
3377 * the one we explicitly initialize AND the one we want included
3378 * in the sampling buffer (smpl_regs).
3380 * Having this restriction allows optimization in the ctxsw routine
3381 * without compromising security (leaks)
3383 if (unlikely(!CTX_IS_USED_PMD(ctx, cnum))) goto error;
3385 sval = ctx->ctx_pmds[cnum].val;
3386 lval = ctx->ctx_pmds[cnum].lval;
3387 is_counting = PMD_IS_COUNTING(cnum);
3390 * If the task is not the current one, then we check if the
3391 * PMU state is still in the local live register due to lazy ctxsw.
3392 * If true, then we read directly from the registers.
3394 if (can_access_pmu){
3395 val = ia64_get_pmd(cnum);
3396 } else {
3398 * context has been saved
3399 * if context is zombie, then task does not exist anymore.
3400 * In this case, we use the full value saved in the context (pfm_flush_regs()).
3402 val = is_loaded ? ctx->th_pmds[cnum] : 0UL;
3404 rd_func = pmu_conf->pmd_desc[cnum].read_check;
3406 if (is_counting) {
3408 * XXX: need to check for overflow when loaded
3410 val &= ovfl_mask;
3411 val += sval;
3415 * execute read checker, if any
3417 if (unlikely(expert_mode == 0 && rd_func)) {
3418 unsigned long v = val;
3419 ret = (*rd_func)(ctx->ctx_task, ctx, cnum, &v, regs);
3420 if (ret) goto error;
3421 val = v;
3422 ret = -EINVAL;
3425 PFM_REG_RETFLAG_SET(reg_flags, 0);
3427 DPRINT(("pmd[%u]=0x%lx\n", cnum, val));
3430 * update register return value, abort all if problem during copy.
3431 * we only modify the reg_flags field. no check mode is fine because
3432 * access has been verified upfront in sys_perfmonctl().
3434 req->reg_value = val;
3435 req->reg_flags = reg_flags;
3436 req->reg_last_reset_val = lval;
3439 return 0;
3441 error:
3442 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3443 return ret;
3447 pfm_mod_write_pmcs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3449 pfm_context_t *ctx;
3451 if (req == NULL) return -EINVAL;
3453 ctx = GET_PMU_CTX();
3455 if (ctx == NULL) return -EINVAL;
3458 * for now limit to current task, which is enough when calling
3459 * from overflow handler
3461 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3463 return pfm_write_pmcs(ctx, req, nreq, regs);
3465 EXPORT_SYMBOL(pfm_mod_write_pmcs);
3468 pfm_mod_read_pmds(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3470 pfm_context_t *ctx;
3472 if (req == NULL) return -EINVAL;
3474 ctx = GET_PMU_CTX();
3476 if (ctx == NULL) return -EINVAL;
3479 * for now limit to current task, which is enough when calling
3480 * from overflow handler
3482 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3484 return pfm_read_pmds(ctx, req, nreq, regs);
3486 EXPORT_SYMBOL(pfm_mod_read_pmds);
3489 * Only call this function when a process it trying to
3490 * write the debug registers (reading is always allowed)
3493 pfm_use_debug_registers(struct task_struct *task)
3495 pfm_context_t *ctx = task->thread.pfm_context;
3496 unsigned long flags;
3497 int ret = 0;
3499 if (pmu_conf->use_rr_dbregs == 0) return 0;
3501 DPRINT(("called for [%d]\n", task_pid_nr(task)));
3504 * do it only once
3506 if (task->thread.flags & IA64_THREAD_DBG_VALID) return 0;
3509 * Even on SMP, we do not need to use an atomic here because
3510 * the only way in is via ptrace() and this is possible only when the
3511 * process is stopped. Even in the case where the ctxsw out is not totally
3512 * completed by the time we come here, there is no way the 'stopped' process
3513 * could be in the middle of fiddling with the pfm_write_ibr_dbr() routine.
3514 * So this is always safe.
3516 if (ctx && ctx->ctx_fl_using_dbreg == 1) return -1;
3518 LOCK_PFS(flags);
3521 * We cannot allow setting breakpoints when system wide monitoring
3522 * sessions are using the debug registers.
3524 if (pfm_sessions.pfs_sys_use_dbregs> 0)
3525 ret = -1;
3526 else
3527 pfm_sessions.pfs_ptrace_use_dbregs++;
3529 DPRINT(("ptrace_use_dbregs=%u sys_use_dbregs=%u by [%d] ret = %d\n",
3530 pfm_sessions.pfs_ptrace_use_dbregs,
3531 pfm_sessions.pfs_sys_use_dbregs,
3532 task_pid_nr(task), ret));
3534 UNLOCK_PFS(flags);
3536 return ret;
3540 * This function is called for every task that exits with the
3541 * IA64_THREAD_DBG_VALID set. This indicates a task which was
3542 * able to use the debug registers for debugging purposes via
3543 * ptrace(). Therefore we know it was not using them for
3544 * perfmormance monitoring, so we only decrement the number
3545 * of "ptraced" debug register users to keep the count up to date
3548 pfm_release_debug_registers(struct task_struct *task)
3550 unsigned long flags;
3551 int ret;
3553 if (pmu_conf->use_rr_dbregs == 0) return 0;
3555 LOCK_PFS(flags);
3556 if (pfm_sessions.pfs_ptrace_use_dbregs == 0) {
3557 printk(KERN_ERR "perfmon: invalid release for [%d] ptrace_use_dbregs=0\n", task_pid_nr(task));
3558 ret = -1;
3559 } else {
3560 pfm_sessions.pfs_ptrace_use_dbregs--;
3561 ret = 0;
3563 UNLOCK_PFS(flags);
3565 return ret;
3568 static int
3569 pfm_restart(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3571 struct task_struct *task;
3572 pfm_buffer_fmt_t *fmt;
3573 pfm_ovfl_ctrl_t rst_ctrl;
3574 int state, is_system;
3575 int ret = 0;
3577 state = ctx->ctx_state;
3578 fmt = ctx->ctx_buf_fmt;
3579 is_system = ctx->ctx_fl_system;
3580 task = PFM_CTX_TASK(ctx);
3582 switch(state) {
3583 case PFM_CTX_MASKED:
3584 break;
3585 case PFM_CTX_LOADED:
3586 if (CTX_HAS_SMPL(ctx) && fmt->fmt_restart_active) break;
3587 /* fall through */
3588 case PFM_CTX_UNLOADED:
3589 case PFM_CTX_ZOMBIE:
3590 DPRINT(("invalid state=%d\n", state));
3591 return -EBUSY;
3592 default:
3593 DPRINT(("state=%d, cannot operate (no active_restart handler)\n", state));
3594 return -EINVAL;
3598 * In system wide and when the context is loaded, access can only happen
3599 * when the caller is running on the CPU being monitored by the session.
3600 * It does not have to be the owner (ctx_task) of the context per se.
3602 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
3603 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3604 return -EBUSY;
3607 /* sanity check */
3608 if (unlikely(task == NULL)) {
3609 printk(KERN_ERR "perfmon: [%d] pfm_restart no task\n", task_pid_nr(current));
3610 return -EINVAL;
3613 if (task == current || is_system) {
3615 fmt = ctx->ctx_buf_fmt;
3617 DPRINT(("restarting self %d ovfl=0x%lx\n",
3618 task_pid_nr(task),
3619 ctx->ctx_ovfl_regs[0]));
3621 if (CTX_HAS_SMPL(ctx)) {
3623 prefetch(ctx->ctx_smpl_hdr);
3625 rst_ctrl.bits.mask_monitoring = 0;
3626 rst_ctrl.bits.reset_ovfl_pmds = 0;
3628 if (state == PFM_CTX_LOADED)
3629 ret = pfm_buf_fmt_restart_active(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
3630 else
3631 ret = pfm_buf_fmt_restart(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
3632 } else {
3633 rst_ctrl.bits.mask_monitoring = 0;
3634 rst_ctrl.bits.reset_ovfl_pmds = 1;
3637 if (ret == 0) {
3638 if (rst_ctrl.bits.reset_ovfl_pmds)
3639 pfm_reset_regs(ctx, ctx->ctx_ovfl_regs, PFM_PMD_LONG_RESET);
3641 if (rst_ctrl.bits.mask_monitoring == 0) {
3642 DPRINT(("resuming monitoring for [%d]\n", task_pid_nr(task)));
3644 if (state == PFM_CTX_MASKED) pfm_restore_monitoring(task);
3645 } else {
3646 DPRINT(("keeping monitoring stopped for [%d]\n", task_pid_nr(task)));
3648 // cannot use pfm_stop_monitoring(task, regs);
3652 * clear overflowed PMD mask to remove any stale information
3654 ctx->ctx_ovfl_regs[0] = 0UL;
3657 * back to LOADED state
3659 ctx->ctx_state = PFM_CTX_LOADED;
3662 * XXX: not really useful for self monitoring
3664 ctx->ctx_fl_can_restart = 0;
3666 return 0;
3670 * restart another task
3674 * When PFM_CTX_MASKED, we cannot issue a restart before the previous
3675 * one is seen by the task.
3677 if (state == PFM_CTX_MASKED) {
3678 if (ctx->ctx_fl_can_restart == 0) return -EINVAL;
3680 * will prevent subsequent restart before this one is
3681 * seen by other task
3683 ctx->ctx_fl_can_restart = 0;
3687 * if blocking, then post the semaphore is PFM_CTX_MASKED, i.e.
3688 * the task is blocked or on its way to block. That's the normal
3689 * restart path. If the monitoring is not masked, then the task
3690 * can be actively monitoring and we cannot directly intervene.
3691 * Therefore we use the trap mechanism to catch the task and
3692 * force it to reset the buffer/reset PMDs.
3694 * if non-blocking, then we ensure that the task will go into
3695 * pfm_handle_work() before returning to user mode.
3697 * We cannot explicitly reset another task, it MUST always
3698 * be done by the task itself. This works for system wide because
3699 * the tool that is controlling the session is logically doing
3700 * "self-monitoring".
3702 if (CTX_OVFL_NOBLOCK(ctx) == 0 && state == PFM_CTX_MASKED) {
3703 DPRINT(("unblocking [%d] \n", task_pid_nr(task)));
3704 complete(&ctx->ctx_restart_done);
3705 } else {
3706 DPRINT(("[%d] armed exit trap\n", task_pid_nr(task)));
3708 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_RESET;
3710 PFM_SET_WORK_PENDING(task, 1);
3712 tsk_set_notify_resume(task);
3715 * XXX: send reschedule if task runs on another CPU
3718 return 0;
3721 static int
3722 pfm_debug(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3724 unsigned int m = *(unsigned int *)arg;
3726 pfm_sysctl.debug = m == 0 ? 0 : 1;
3728 printk(KERN_INFO "perfmon debugging %s (timing reset)\n", pfm_sysctl.debug ? "on" : "off");
3730 if (m == 0) {
3731 memset(pfm_stats, 0, sizeof(pfm_stats));
3732 for(m=0; m < NR_CPUS; m++) pfm_stats[m].pfm_ovfl_intr_cycles_min = ~0UL;
3734 return 0;
3738 * arg can be NULL and count can be zero for this function
3740 static int
3741 pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3743 struct thread_struct *thread = NULL;
3744 struct task_struct *task;
3745 pfarg_dbreg_t *req = (pfarg_dbreg_t *)arg;
3746 unsigned long flags;
3747 dbreg_t dbreg;
3748 unsigned int rnum;
3749 int first_time;
3750 int ret = 0, state;
3751 int i, can_access_pmu = 0;
3752 int is_system, is_loaded;
3754 if (pmu_conf->use_rr_dbregs == 0) return -EINVAL;
3756 state = ctx->ctx_state;
3757 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3758 is_system = ctx->ctx_fl_system;
3759 task = ctx->ctx_task;
3761 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
3764 * on both UP and SMP, we can only write to the PMC when the task is
3765 * the owner of the local PMU.
3767 if (is_loaded) {
3768 thread = &task->thread;
3770 * In system wide and when the context is loaded, access can only happen
3771 * when the caller is running on the CPU being monitored by the session.
3772 * It does not have to be the owner (ctx_task) of the context per se.
3774 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3775 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3776 return -EBUSY;
3778 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3782 * we do not need to check for ipsr.db because we do clear ibr.x, dbr.r, and dbr.w
3783 * ensuring that no real breakpoint can be installed via this call.
3785 * IMPORTANT: regs can be NULL in this function
3788 first_time = ctx->ctx_fl_using_dbreg == 0;
3791 * don't bother if we are loaded and task is being debugged
3793 if (is_loaded && (thread->flags & IA64_THREAD_DBG_VALID) != 0) {
3794 DPRINT(("debug registers already in use for [%d]\n", task_pid_nr(task)));
3795 return -EBUSY;
3799 * check for debug registers in system wide mode
3801 * If though a check is done in pfm_context_load(),
3802 * we must repeat it here, in case the registers are
3803 * written after the context is loaded
3805 if (is_loaded) {
3806 LOCK_PFS(flags);
3808 if (first_time && is_system) {
3809 if (pfm_sessions.pfs_ptrace_use_dbregs)
3810 ret = -EBUSY;
3811 else
3812 pfm_sessions.pfs_sys_use_dbregs++;
3814 UNLOCK_PFS(flags);
3817 if (ret != 0) return ret;
3820 * mark ourself as user of the debug registers for
3821 * perfmon purposes.
3823 ctx->ctx_fl_using_dbreg = 1;
3826 * clear hardware registers to make sure we don't
3827 * pick up stale state.
3829 * for a system wide session, we do not use
3830 * thread.dbr, thread.ibr because this process
3831 * never leaves the current CPU and the state
3832 * is shared by all processes running on it
3834 if (first_time && can_access_pmu) {
3835 DPRINT(("[%d] clearing ibrs, dbrs\n", task_pid_nr(task)));
3836 for (i=0; i < pmu_conf->num_ibrs; i++) {
3837 ia64_set_ibr(i, 0UL);
3838 ia64_dv_serialize_instruction();
3840 ia64_srlz_i();
3841 for (i=0; i < pmu_conf->num_dbrs; i++) {
3842 ia64_set_dbr(i, 0UL);
3843 ia64_dv_serialize_data();
3845 ia64_srlz_d();
3849 * Now install the values into the registers
3851 for (i = 0; i < count; i++, req++) {
3853 rnum = req->dbreg_num;
3854 dbreg.val = req->dbreg_value;
3856 ret = -EINVAL;
3858 if ((mode == PFM_CODE_RR && rnum >= PFM_NUM_IBRS) || ((mode == PFM_DATA_RR) && rnum >= PFM_NUM_DBRS)) {
3859 DPRINT(("invalid register %u val=0x%lx mode=%d i=%d count=%d\n",
3860 rnum, dbreg.val, mode, i, count));
3862 goto abort_mission;
3866 * make sure we do not install enabled breakpoint
3868 if (rnum & 0x1) {
3869 if (mode == PFM_CODE_RR)
3870 dbreg.ibr.ibr_x = 0;
3871 else
3872 dbreg.dbr.dbr_r = dbreg.dbr.dbr_w = 0;
3875 PFM_REG_RETFLAG_SET(req->dbreg_flags, 0);
3878 * Debug registers, just like PMC, can only be modified
3879 * by a kernel call. Moreover, perfmon() access to those
3880 * registers are centralized in this routine. The hardware
3881 * does not modify the value of these registers, therefore,
3882 * if we save them as they are written, we can avoid having
3883 * to save them on context switch out. This is made possible
3884 * by the fact that when perfmon uses debug registers, ptrace()
3885 * won't be able to modify them concurrently.
3887 if (mode == PFM_CODE_RR) {
3888 CTX_USED_IBR(ctx, rnum);
3890 if (can_access_pmu) {
3891 ia64_set_ibr(rnum, dbreg.val);
3892 ia64_dv_serialize_instruction();
3895 ctx->ctx_ibrs[rnum] = dbreg.val;
3897 DPRINT(("write ibr%u=0x%lx used_ibrs=0x%x ld=%d apmu=%d\n",
3898 rnum, dbreg.val, ctx->ctx_used_ibrs[0], is_loaded, can_access_pmu));
3899 } else {
3900 CTX_USED_DBR(ctx, rnum);
3902 if (can_access_pmu) {
3903 ia64_set_dbr(rnum, dbreg.val);
3904 ia64_dv_serialize_data();
3906 ctx->ctx_dbrs[rnum] = dbreg.val;
3908 DPRINT(("write dbr%u=0x%lx used_dbrs=0x%x ld=%d apmu=%d\n",
3909 rnum, dbreg.val, ctx->ctx_used_dbrs[0], is_loaded, can_access_pmu));
3913 return 0;
3915 abort_mission:
3917 * in case it was our first attempt, we undo the global modifications
3919 if (first_time) {
3920 LOCK_PFS(flags);
3921 if (ctx->ctx_fl_system) {
3922 pfm_sessions.pfs_sys_use_dbregs--;
3924 UNLOCK_PFS(flags);
3925 ctx->ctx_fl_using_dbreg = 0;
3928 * install error return flag
3930 PFM_REG_RETFLAG_SET(req->dbreg_flags, PFM_REG_RETFL_EINVAL);
3932 return ret;
3935 static int
3936 pfm_write_ibrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3938 return pfm_write_ibr_dbr(PFM_CODE_RR, ctx, arg, count, regs);
3941 static int
3942 pfm_write_dbrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3944 return pfm_write_ibr_dbr(PFM_DATA_RR, ctx, arg, count, regs);
3948 pfm_mod_write_ibrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3950 pfm_context_t *ctx;
3952 if (req == NULL) return -EINVAL;
3954 ctx = GET_PMU_CTX();
3956 if (ctx == NULL) return -EINVAL;
3959 * for now limit to current task, which is enough when calling
3960 * from overflow handler
3962 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3964 return pfm_write_ibrs(ctx, req, nreq, regs);
3966 EXPORT_SYMBOL(pfm_mod_write_ibrs);
3969 pfm_mod_write_dbrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3971 pfm_context_t *ctx;
3973 if (req == NULL) return -EINVAL;
3975 ctx = GET_PMU_CTX();
3977 if (ctx == NULL) return -EINVAL;
3980 * for now limit to current task, which is enough when calling
3981 * from overflow handler
3983 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3985 return pfm_write_dbrs(ctx, req, nreq, regs);
3987 EXPORT_SYMBOL(pfm_mod_write_dbrs);
3990 static int
3991 pfm_get_features(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3993 pfarg_features_t *req = (pfarg_features_t *)arg;
3995 req->ft_version = PFM_VERSION;
3996 return 0;
3999 static int
4000 pfm_stop(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4002 struct pt_regs *tregs;
4003 struct task_struct *task = PFM_CTX_TASK(ctx);
4004 int state, is_system;
4006 state = ctx->ctx_state;
4007 is_system = ctx->ctx_fl_system;
4010 * context must be attached to issue the stop command (includes LOADED,MASKED,ZOMBIE)
4012 if (state == PFM_CTX_UNLOADED) return -EINVAL;
4015 * In system wide and when the context is loaded, access can only happen
4016 * when the caller is running on the CPU being monitored by the session.
4017 * It does not have to be the owner (ctx_task) of the context per se.
4019 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
4020 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
4021 return -EBUSY;
4023 DPRINT(("task [%d] ctx_state=%d is_system=%d\n",
4024 task_pid_nr(PFM_CTX_TASK(ctx)),
4025 state,
4026 is_system));
4028 * in system mode, we need to update the PMU directly
4029 * and the user level state of the caller, which may not
4030 * necessarily be the creator of the context.
4032 if (is_system) {
4034 * Update local PMU first
4036 * disable dcr pp
4038 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
4039 ia64_srlz_i();
4042 * update local cpuinfo
4044 PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
4047 * stop monitoring, does srlz.i
4049 pfm_clear_psr_pp();
4052 * stop monitoring in the caller
4054 ia64_psr(regs)->pp = 0;
4056 return 0;
4059 * per-task mode
4062 if (task == current) {
4063 /* stop monitoring at kernel level */
4064 pfm_clear_psr_up();
4067 * stop monitoring at the user level
4069 ia64_psr(regs)->up = 0;
4070 } else {
4071 tregs = task_pt_regs(task);
4074 * stop monitoring at the user level
4076 ia64_psr(tregs)->up = 0;
4079 * monitoring disabled in kernel at next reschedule
4081 ctx->ctx_saved_psr_up = 0;
4082 DPRINT(("task=[%d]\n", task_pid_nr(task)));
4084 return 0;
4088 static int
4089 pfm_start(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4091 struct pt_regs *tregs;
4092 int state, is_system;
4094 state = ctx->ctx_state;
4095 is_system = ctx->ctx_fl_system;
4097 if (state != PFM_CTX_LOADED) return -EINVAL;
4100 * In system wide and when the context is loaded, access can only happen
4101 * when the caller is running on the CPU being monitored by the session.
4102 * It does not have to be the owner (ctx_task) of the context per se.
4104 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
4105 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
4106 return -EBUSY;
4110 * in system mode, we need to update the PMU directly
4111 * and the user level state of the caller, which may not
4112 * necessarily be the creator of the context.
4114 if (is_system) {
4117 * set user level psr.pp for the caller
4119 ia64_psr(regs)->pp = 1;
4122 * now update the local PMU and cpuinfo
4124 PFM_CPUINFO_SET(PFM_CPUINFO_DCR_PP);
4127 * start monitoring at kernel level
4129 pfm_set_psr_pp();
4131 /* enable dcr pp */
4132 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
4133 ia64_srlz_i();
4135 return 0;
4139 * per-process mode
4142 if (ctx->ctx_task == current) {
4144 /* start monitoring at kernel level */
4145 pfm_set_psr_up();
4148 * activate monitoring at user level
4150 ia64_psr(regs)->up = 1;
4152 } else {
4153 tregs = task_pt_regs(ctx->ctx_task);
4156 * start monitoring at the kernel level the next
4157 * time the task is scheduled
4159 ctx->ctx_saved_psr_up = IA64_PSR_UP;
4162 * activate monitoring at user level
4164 ia64_psr(tregs)->up = 1;
4166 return 0;
4169 static int
4170 pfm_get_pmc_reset(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4172 pfarg_reg_t *req = (pfarg_reg_t *)arg;
4173 unsigned int cnum;
4174 int i;
4175 int ret = -EINVAL;
4177 for (i = 0; i < count; i++, req++) {
4179 cnum = req->reg_num;
4181 if (!PMC_IS_IMPL(cnum)) goto abort_mission;
4183 req->reg_value = PMC_DFL_VAL(cnum);
4185 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
4187 DPRINT(("pmc_reset_val pmc[%u]=0x%lx\n", cnum, req->reg_value));
4189 return 0;
4191 abort_mission:
4192 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
4193 return ret;
4196 static int
4197 pfm_check_task_exist(pfm_context_t *ctx)
4199 struct task_struct *g, *t;
4200 int ret = -ESRCH;
4202 read_lock(&tasklist_lock);
4204 do_each_thread (g, t) {
4205 if (t->thread.pfm_context == ctx) {
4206 ret = 0;
4207 break;
4209 } while_each_thread (g, t);
4211 read_unlock(&tasklist_lock);
4213 DPRINT(("pfm_check_task_exist: ret=%d ctx=%p\n", ret, ctx));
4215 return ret;
4218 static int
4219 pfm_context_load(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4221 struct task_struct *task;
4222 struct thread_struct *thread;
4223 struct pfm_context_t *old;
4224 unsigned long flags;
4225 #ifndef CONFIG_SMP
4226 struct task_struct *owner_task = NULL;
4227 #endif
4228 pfarg_load_t *req = (pfarg_load_t *)arg;
4229 unsigned long *pmcs_source, *pmds_source;
4230 int the_cpu;
4231 int ret = 0;
4232 int state, is_system, set_dbregs = 0;
4234 state = ctx->ctx_state;
4235 is_system = ctx->ctx_fl_system;
4237 * can only load from unloaded or terminated state
4239 if (state != PFM_CTX_UNLOADED) {
4240 DPRINT(("cannot load to [%d], invalid ctx_state=%d\n",
4241 req->load_pid,
4242 ctx->ctx_state));
4243 return -EBUSY;
4246 DPRINT(("load_pid [%d] using_dbreg=%d\n", req->load_pid, ctx->ctx_fl_using_dbreg));
4248 if (CTX_OVFL_NOBLOCK(ctx) == 0 && req->load_pid == current->pid) {
4249 DPRINT(("cannot use blocking mode on self\n"));
4250 return -EINVAL;
4253 ret = pfm_get_task(ctx, req->load_pid, &task);
4254 if (ret) {
4255 DPRINT(("load_pid [%d] get_task=%d\n", req->load_pid, ret));
4256 return ret;
4259 ret = -EINVAL;
4262 * system wide is self monitoring only
4264 if (is_system && task != current) {
4265 DPRINT(("system wide is self monitoring only load_pid=%d\n",
4266 req->load_pid));
4267 goto error;
4270 thread = &task->thread;
4272 ret = 0;
4274 * cannot load a context which is using range restrictions,
4275 * into a task that is being debugged.
4277 if (ctx->ctx_fl_using_dbreg) {
4278 if (thread->flags & IA64_THREAD_DBG_VALID) {
4279 ret = -EBUSY;
4280 DPRINT(("load_pid [%d] task is debugged, cannot load range restrictions\n", req->load_pid));
4281 goto error;
4283 LOCK_PFS(flags);
4285 if (is_system) {
4286 if (pfm_sessions.pfs_ptrace_use_dbregs) {
4287 DPRINT(("cannot load [%d] dbregs in use\n",
4288 task_pid_nr(task)));
4289 ret = -EBUSY;
4290 } else {
4291 pfm_sessions.pfs_sys_use_dbregs++;
4292 DPRINT(("load [%d] increased sys_use_dbreg=%u\n", task_pid_nr(task), pfm_sessions.pfs_sys_use_dbregs));
4293 set_dbregs = 1;
4297 UNLOCK_PFS(flags);
4299 if (ret) goto error;
4303 * SMP system-wide monitoring implies self-monitoring.
4305 * The programming model expects the task to
4306 * be pinned on a CPU throughout the session.
4307 * Here we take note of the current CPU at the
4308 * time the context is loaded. No call from
4309 * another CPU will be allowed.
4311 * The pinning via shed_setaffinity()
4312 * must be done by the calling task prior
4313 * to this call.
4315 * systemwide: keep track of CPU this session is supposed to run on
4317 the_cpu = ctx->ctx_cpu = smp_processor_id();
4319 ret = -EBUSY;
4321 * now reserve the session
4323 ret = pfm_reserve_session(current, is_system, the_cpu);
4324 if (ret) goto error;
4327 * task is necessarily stopped at this point.
4329 * If the previous context was zombie, then it got removed in
4330 * pfm_save_regs(). Therefore we should not see it here.
4331 * If we see a context, then this is an active context
4333 * XXX: needs to be atomic
4335 DPRINT(("before cmpxchg() old_ctx=%p new_ctx=%p\n",
4336 thread->pfm_context, ctx));
4338 ret = -EBUSY;
4339 old = ia64_cmpxchg(acq, &thread->pfm_context, NULL, ctx, sizeof(pfm_context_t *));
4340 if (old != NULL) {
4341 DPRINT(("load_pid [%d] already has a context\n", req->load_pid));
4342 goto error_unres;
4345 pfm_reset_msgq(ctx);
4347 ctx->ctx_state = PFM_CTX_LOADED;
4350 * link context to task
4352 ctx->ctx_task = task;
4354 if (is_system) {
4356 * we load as stopped
4358 PFM_CPUINFO_SET(PFM_CPUINFO_SYST_WIDE);
4359 PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
4361 if (ctx->ctx_fl_excl_idle) PFM_CPUINFO_SET(PFM_CPUINFO_EXCL_IDLE);
4362 } else {
4363 thread->flags |= IA64_THREAD_PM_VALID;
4367 * propagate into thread-state
4369 pfm_copy_pmds(task, ctx);
4370 pfm_copy_pmcs(task, ctx);
4372 pmcs_source = ctx->th_pmcs;
4373 pmds_source = ctx->th_pmds;
4376 * always the case for system-wide
4378 if (task == current) {
4380 if (is_system == 0) {
4382 /* allow user level control */
4383 ia64_psr(regs)->sp = 0;
4384 DPRINT(("clearing psr.sp for [%d]\n", task_pid_nr(task)));
4386 SET_LAST_CPU(ctx, smp_processor_id());
4387 INC_ACTIVATION();
4388 SET_ACTIVATION(ctx);
4389 #ifndef CONFIG_SMP
4391 * push the other task out, if any
4393 owner_task = GET_PMU_OWNER();
4394 if (owner_task) pfm_lazy_save_regs(owner_task);
4395 #endif
4398 * load all PMD from ctx to PMU (as opposed to thread state)
4399 * restore all PMC from ctx to PMU
4401 pfm_restore_pmds(pmds_source, ctx->ctx_all_pmds[0]);
4402 pfm_restore_pmcs(pmcs_source, ctx->ctx_all_pmcs[0]);
4404 ctx->ctx_reload_pmcs[0] = 0UL;
4405 ctx->ctx_reload_pmds[0] = 0UL;
4408 * guaranteed safe by earlier check against DBG_VALID
4410 if (ctx->ctx_fl_using_dbreg) {
4411 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
4412 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
4415 * set new ownership
4417 SET_PMU_OWNER(task, ctx);
4419 DPRINT(("context loaded on PMU for [%d]\n", task_pid_nr(task)));
4420 } else {
4422 * when not current, task MUST be stopped, so this is safe
4424 regs = task_pt_regs(task);
4426 /* force a full reload */
4427 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
4428 SET_LAST_CPU(ctx, -1);
4430 /* initial saved psr (stopped) */
4431 ctx->ctx_saved_psr_up = 0UL;
4432 ia64_psr(regs)->up = ia64_psr(regs)->pp = 0;
4435 ret = 0;
4437 error_unres:
4438 if (ret) pfm_unreserve_session(ctx, ctx->ctx_fl_system, the_cpu);
4439 error:
4441 * we must undo the dbregs setting (for system-wide)
4443 if (ret && set_dbregs) {
4444 LOCK_PFS(flags);
4445 pfm_sessions.pfs_sys_use_dbregs--;
4446 UNLOCK_PFS(flags);
4449 * release task, there is now a link with the context
4451 if (is_system == 0 && task != current) {
4452 pfm_put_task(task);
4454 if (ret == 0) {
4455 ret = pfm_check_task_exist(ctx);
4456 if (ret) {
4457 ctx->ctx_state = PFM_CTX_UNLOADED;
4458 ctx->ctx_task = NULL;
4462 return ret;
4466 * in this function, we do not need to increase the use count
4467 * for the task via get_task_struct(), because we hold the
4468 * context lock. If the task were to disappear while having
4469 * a context attached, it would go through pfm_exit_thread()
4470 * which also grabs the context lock and would therefore be blocked
4471 * until we are here.
4473 static void pfm_flush_pmds(struct task_struct *, pfm_context_t *ctx);
4475 static int
4476 pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4478 struct task_struct *task = PFM_CTX_TASK(ctx);
4479 struct pt_regs *tregs;
4480 int prev_state, is_system;
4481 int ret;
4483 DPRINT(("ctx_state=%d task [%d]\n", ctx->ctx_state, task ? task_pid_nr(task) : -1));
4485 prev_state = ctx->ctx_state;
4486 is_system = ctx->ctx_fl_system;
4489 * unload only when necessary
4491 if (prev_state == PFM_CTX_UNLOADED) {
4492 DPRINT(("ctx_state=%d, nothing to do\n", prev_state));
4493 return 0;
4497 * clear psr and dcr bits
4499 ret = pfm_stop(ctx, NULL, 0, regs);
4500 if (ret) return ret;
4502 ctx->ctx_state = PFM_CTX_UNLOADED;
4505 * in system mode, we need to update the PMU directly
4506 * and the user level state of the caller, which may not
4507 * necessarily be the creator of the context.
4509 if (is_system) {
4512 * Update cpuinfo
4514 * local PMU is taken care of in pfm_stop()
4516 PFM_CPUINFO_CLEAR(PFM_CPUINFO_SYST_WIDE);
4517 PFM_CPUINFO_CLEAR(PFM_CPUINFO_EXCL_IDLE);
4520 * save PMDs in context
4521 * release ownership
4523 pfm_flush_pmds(current, ctx);
4526 * at this point we are done with the PMU
4527 * so we can unreserve the resource.
4529 if (prev_state != PFM_CTX_ZOMBIE)
4530 pfm_unreserve_session(ctx, 1 , ctx->ctx_cpu);
4533 * disconnect context from task
4535 task->thread.pfm_context = NULL;
4537 * disconnect task from context
4539 ctx->ctx_task = NULL;
4542 * There is nothing more to cleanup here.
4544 return 0;
4548 * per-task mode
4550 tregs = task == current ? regs : task_pt_regs(task);
4552 if (task == current) {
4554 * cancel user level control
4556 ia64_psr(regs)->sp = 1;
4558 DPRINT(("setting psr.sp for [%d]\n", task_pid_nr(task)));
4561 * save PMDs to context
4562 * release ownership
4564 pfm_flush_pmds(task, ctx);
4567 * at this point we are done with the PMU
4568 * so we can unreserve the resource.
4570 * when state was ZOMBIE, we have already unreserved.
4572 if (prev_state != PFM_CTX_ZOMBIE)
4573 pfm_unreserve_session(ctx, 0 , ctx->ctx_cpu);
4576 * reset activation counter and psr
4578 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
4579 SET_LAST_CPU(ctx, -1);
4582 * PMU state will not be restored
4584 task->thread.flags &= ~IA64_THREAD_PM_VALID;
4587 * break links between context and task
4589 task->thread.pfm_context = NULL;
4590 ctx->ctx_task = NULL;
4592 PFM_SET_WORK_PENDING(task, 0);
4594 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
4595 ctx->ctx_fl_can_restart = 0;
4596 ctx->ctx_fl_going_zombie = 0;
4598 DPRINT(("disconnected [%d] from context\n", task_pid_nr(task)));
4600 return 0;
4605 * called only from exit_thread(): task == current
4606 * we come here only if current has a context attached (loaded or masked)
4608 void
4609 pfm_exit_thread(struct task_struct *task)
4611 pfm_context_t *ctx;
4612 unsigned long flags;
4613 struct pt_regs *regs = task_pt_regs(task);
4614 int ret, state;
4615 int free_ok = 0;
4617 ctx = PFM_GET_CTX(task);
4619 PROTECT_CTX(ctx, flags);
4621 DPRINT(("state=%d task [%d]\n", ctx->ctx_state, task_pid_nr(task)));
4623 state = ctx->ctx_state;
4624 switch(state) {
4625 case PFM_CTX_UNLOADED:
4627 * only comes to this function if pfm_context is not NULL, i.e., cannot
4628 * be in unloaded state
4630 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] ctx unloaded\n", task_pid_nr(task));
4631 break;
4632 case PFM_CTX_LOADED:
4633 case PFM_CTX_MASKED:
4634 ret = pfm_context_unload(ctx, NULL, 0, regs);
4635 if (ret) {
4636 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task_pid_nr(task), state, ret);
4638 DPRINT(("ctx unloaded for current state was %d\n", state));
4640 pfm_end_notify_user(ctx);
4641 break;
4642 case PFM_CTX_ZOMBIE:
4643 ret = pfm_context_unload(ctx, NULL, 0, regs);
4644 if (ret) {
4645 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task_pid_nr(task), state, ret);
4647 free_ok = 1;
4648 break;
4649 default:
4650 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] unexpected state=%d\n", task_pid_nr(task), state);
4651 break;
4653 UNPROTECT_CTX(ctx, flags);
4655 { u64 psr = pfm_get_psr();
4656 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
4657 BUG_ON(GET_PMU_OWNER());
4658 BUG_ON(ia64_psr(regs)->up);
4659 BUG_ON(ia64_psr(regs)->pp);
4663 * All memory free operations (especially for vmalloc'ed memory)
4664 * MUST be done with interrupts ENABLED.
4666 if (free_ok) pfm_context_free(ctx);
4670 * functions MUST be listed in the increasing order of their index (see permfon.h)
4672 #define PFM_CMD(name, flags, arg_count, arg_type, getsz) { name, #name, flags, arg_count, sizeof(arg_type), getsz }
4673 #define PFM_CMD_S(name, flags) { name, #name, flags, 0, 0, NULL }
4674 #define PFM_CMD_PCLRWS (PFM_CMD_FD|PFM_CMD_ARG_RW|PFM_CMD_STOP)
4675 #define PFM_CMD_PCLRW (PFM_CMD_FD|PFM_CMD_ARG_RW)
4676 #define PFM_CMD_NONE { NULL, "no-cmd", 0, 0, 0, NULL}
4678 static pfm_cmd_desc_t pfm_cmd_tab[]={
4679 /* 0 */PFM_CMD_NONE,
4680 /* 1 */PFM_CMD(pfm_write_pmcs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4681 /* 2 */PFM_CMD(pfm_write_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4682 /* 3 */PFM_CMD(pfm_read_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4683 /* 4 */PFM_CMD_S(pfm_stop, PFM_CMD_PCLRWS),
4684 /* 5 */PFM_CMD_S(pfm_start, PFM_CMD_PCLRWS),
4685 /* 6 */PFM_CMD_NONE,
4686 /* 7 */PFM_CMD_NONE,
4687 /* 8 */PFM_CMD(pfm_context_create, PFM_CMD_ARG_RW, 1, pfarg_context_t, pfm_ctx_getsize),
4688 /* 9 */PFM_CMD_NONE,
4689 /* 10 */PFM_CMD_S(pfm_restart, PFM_CMD_PCLRW),
4690 /* 11 */PFM_CMD_NONE,
4691 /* 12 */PFM_CMD(pfm_get_features, PFM_CMD_ARG_RW, 1, pfarg_features_t, NULL),
4692 /* 13 */PFM_CMD(pfm_debug, 0, 1, unsigned int, NULL),
4693 /* 14 */PFM_CMD_NONE,
4694 /* 15 */PFM_CMD(pfm_get_pmc_reset, PFM_CMD_ARG_RW, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4695 /* 16 */PFM_CMD(pfm_context_load, PFM_CMD_PCLRWS, 1, pfarg_load_t, NULL),
4696 /* 17 */PFM_CMD_S(pfm_context_unload, PFM_CMD_PCLRWS),
4697 /* 18 */PFM_CMD_NONE,
4698 /* 19 */PFM_CMD_NONE,
4699 /* 20 */PFM_CMD_NONE,
4700 /* 21 */PFM_CMD_NONE,
4701 /* 22 */PFM_CMD_NONE,
4702 /* 23 */PFM_CMD_NONE,
4703 /* 24 */PFM_CMD_NONE,
4704 /* 25 */PFM_CMD_NONE,
4705 /* 26 */PFM_CMD_NONE,
4706 /* 27 */PFM_CMD_NONE,
4707 /* 28 */PFM_CMD_NONE,
4708 /* 29 */PFM_CMD_NONE,
4709 /* 30 */PFM_CMD_NONE,
4710 /* 31 */PFM_CMD_NONE,
4711 /* 32 */PFM_CMD(pfm_write_ibrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL),
4712 /* 33 */PFM_CMD(pfm_write_dbrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL)
4714 #define PFM_CMD_COUNT (sizeof(pfm_cmd_tab)/sizeof(pfm_cmd_desc_t))
4716 static int
4717 pfm_check_task_state(pfm_context_t *ctx, int cmd, unsigned long flags)
4719 struct task_struct *task;
4720 int state, old_state;
4722 recheck:
4723 state = ctx->ctx_state;
4724 task = ctx->ctx_task;
4726 if (task == NULL) {
4727 DPRINT(("context %d no task, state=%d\n", ctx->ctx_fd, state));
4728 return 0;
4731 DPRINT(("context %d state=%d [%d] task_state=%ld must_stop=%d\n",
4732 ctx->ctx_fd,
4733 state,
4734 task_pid_nr(task),
4735 task->state, PFM_CMD_STOPPED(cmd)));
4738 * self-monitoring always ok.
4740 * for system-wide the caller can either be the creator of the
4741 * context (to one to which the context is attached to) OR
4742 * a task running on the same CPU as the session.
4744 if (task == current || ctx->ctx_fl_system) return 0;
4747 * we are monitoring another thread
4749 switch(state) {
4750 case PFM_CTX_UNLOADED:
4752 * if context is UNLOADED we are safe to go
4754 return 0;
4755 case PFM_CTX_ZOMBIE:
4757 * no command can operate on a zombie context
4759 DPRINT(("cmd %d state zombie cannot operate on context\n", cmd));
4760 return -EINVAL;
4761 case PFM_CTX_MASKED:
4763 * PMU state has been saved to software even though
4764 * the thread may still be running.
4766 if (cmd != PFM_UNLOAD_CONTEXT) return 0;
4770 * context is LOADED or MASKED. Some commands may need to have
4771 * the task stopped.
4773 * We could lift this restriction for UP but it would mean that
4774 * the user has no guarantee the task would not run between
4775 * two successive calls to perfmonctl(). That's probably OK.
4776 * If this user wants to ensure the task does not run, then
4777 * the task must be stopped.
4779 if (PFM_CMD_STOPPED(cmd)) {
4780 if (!task_is_stopped_or_traced(task)) {
4781 DPRINT(("[%d] task not in stopped state\n", task_pid_nr(task)));
4782 return -EBUSY;
4785 * task is now stopped, wait for ctxsw out
4787 * This is an interesting point in the code.
4788 * We need to unprotect the context because
4789 * the pfm_save_regs() routines needs to grab
4790 * the same lock. There are danger in doing
4791 * this because it leaves a window open for
4792 * another task to get access to the context
4793 * and possibly change its state. The one thing
4794 * that is not possible is for the context to disappear
4795 * because we are protected by the VFS layer, i.e.,
4796 * get_fd()/put_fd().
4798 old_state = state;
4800 UNPROTECT_CTX(ctx, flags);
4802 wait_task_inactive(task);
4804 PROTECT_CTX(ctx, flags);
4807 * we must recheck to verify if state has changed
4809 if (ctx->ctx_state != old_state) {
4810 DPRINT(("old_state=%d new_state=%d\n", old_state, ctx->ctx_state));
4811 goto recheck;
4814 return 0;
4818 * system-call entry point (must return long)
4820 asmlinkage long
4821 sys_perfmonctl (int fd, int cmd, void __user *arg, int count)
4823 struct file *file = NULL;
4824 pfm_context_t *ctx = NULL;
4825 unsigned long flags = 0UL;
4826 void *args_k = NULL;
4827 long ret; /* will expand int return types */
4828 size_t base_sz, sz, xtra_sz = 0;
4829 int narg, completed_args = 0, call_made = 0, cmd_flags;
4830 int (*func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
4831 int (*getsize)(void *arg, size_t *sz);
4832 #define PFM_MAX_ARGSIZE 4096
4835 * reject any call if perfmon was disabled at initialization
4837 if (unlikely(pmu_conf == NULL)) return -ENOSYS;
4839 if (unlikely(cmd < 0 || cmd >= PFM_CMD_COUNT)) {
4840 DPRINT(("invalid cmd=%d\n", cmd));
4841 return -EINVAL;
4844 func = pfm_cmd_tab[cmd].cmd_func;
4845 narg = pfm_cmd_tab[cmd].cmd_narg;
4846 base_sz = pfm_cmd_tab[cmd].cmd_argsize;
4847 getsize = pfm_cmd_tab[cmd].cmd_getsize;
4848 cmd_flags = pfm_cmd_tab[cmd].cmd_flags;
4850 if (unlikely(func == NULL)) {
4851 DPRINT(("invalid cmd=%d\n", cmd));
4852 return -EINVAL;
4855 DPRINT(("cmd=%s idx=%d narg=0x%x argsz=%lu count=%d\n",
4856 PFM_CMD_NAME(cmd),
4857 cmd,
4858 narg,
4859 base_sz,
4860 count));
4863 * check if number of arguments matches what the command expects
4865 if (unlikely((narg == PFM_CMD_ARG_MANY && count <= 0) || (narg > 0 && narg != count)))
4866 return -EINVAL;
4868 restart_args:
4869 sz = xtra_sz + base_sz*count;
4871 * limit abuse to min page size
4873 if (unlikely(sz > PFM_MAX_ARGSIZE)) {
4874 printk(KERN_ERR "perfmon: [%d] argument too big %lu\n", task_pid_nr(current), sz);
4875 return -E2BIG;
4879 * allocate default-sized argument buffer
4881 if (likely(count && args_k == NULL)) {
4882 args_k = kmalloc(PFM_MAX_ARGSIZE, GFP_KERNEL);
4883 if (args_k == NULL) return -ENOMEM;
4886 ret = -EFAULT;
4889 * copy arguments
4891 * assume sz = 0 for command without parameters
4893 if (sz && copy_from_user(args_k, arg, sz)) {
4894 DPRINT(("cannot copy_from_user %lu bytes @%p\n", sz, arg));
4895 goto error_args;
4899 * check if command supports extra parameters
4901 if (completed_args == 0 && getsize) {
4903 * get extra parameters size (based on main argument)
4905 ret = (*getsize)(args_k, &xtra_sz);
4906 if (ret) goto error_args;
4908 completed_args = 1;
4910 DPRINT(("restart_args sz=%lu xtra_sz=%lu\n", sz, xtra_sz));
4912 /* retry if necessary */
4913 if (likely(xtra_sz)) goto restart_args;
4916 if (unlikely((cmd_flags & PFM_CMD_FD) == 0)) goto skip_fd;
4918 ret = -EBADF;
4920 file = fget(fd);
4921 if (unlikely(file == NULL)) {
4922 DPRINT(("invalid fd %d\n", fd));
4923 goto error_args;
4925 if (unlikely(PFM_IS_FILE(file) == 0)) {
4926 DPRINT(("fd %d not related to perfmon\n", fd));
4927 goto error_args;
4930 ctx = (pfm_context_t *)file->private_data;
4931 if (unlikely(ctx == NULL)) {
4932 DPRINT(("no context for fd %d\n", fd));
4933 goto error_args;
4935 prefetch(&ctx->ctx_state);
4937 PROTECT_CTX(ctx, flags);
4940 * check task is stopped
4942 ret = pfm_check_task_state(ctx, cmd, flags);
4943 if (unlikely(ret)) goto abort_locked;
4945 skip_fd:
4946 ret = (*func)(ctx, args_k, count, task_pt_regs(current));
4948 call_made = 1;
4950 abort_locked:
4951 if (likely(ctx)) {
4952 DPRINT(("context unlocked\n"));
4953 UNPROTECT_CTX(ctx, flags);
4956 /* copy argument back to user, if needed */
4957 if (call_made && PFM_CMD_RW_ARG(cmd) && copy_to_user(arg, args_k, base_sz*count)) ret = -EFAULT;
4959 error_args:
4960 if (file)
4961 fput(file);
4963 kfree(args_k);
4965 DPRINT(("cmd=%s ret=%ld\n", PFM_CMD_NAME(cmd), ret));
4967 return ret;
4970 static void
4971 pfm_resume_after_ovfl(pfm_context_t *ctx, unsigned long ovfl_regs, struct pt_regs *regs)
4973 pfm_buffer_fmt_t *fmt = ctx->ctx_buf_fmt;
4974 pfm_ovfl_ctrl_t rst_ctrl;
4975 int state;
4976 int ret = 0;
4978 state = ctx->ctx_state;
4980 * Unlock sampling buffer and reset index atomically
4981 * XXX: not really needed when blocking
4983 if (CTX_HAS_SMPL(ctx)) {
4985 rst_ctrl.bits.mask_monitoring = 0;
4986 rst_ctrl.bits.reset_ovfl_pmds = 0;
4988 if (state == PFM_CTX_LOADED)
4989 ret = pfm_buf_fmt_restart_active(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
4990 else
4991 ret = pfm_buf_fmt_restart(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
4992 } else {
4993 rst_ctrl.bits.mask_monitoring = 0;
4994 rst_ctrl.bits.reset_ovfl_pmds = 1;
4997 if (ret == 0) {
4998 if (rst_ctrl.bits.reset_ovfl_pmds) {
4999 pfm_reset_regs(ctx, &ovfl_regs, PFM_PMD_LONG_RESET);
5001 if (rst_ctrl.bits.mask_monitoring == 0) {
5002 DPRINT(("resuming monitoring\n"));
5003 if (ctx->ctx_state == PFM_CTX_MASKED) pfm_restore_monitoring(current);
5004 } else {
5005 DPRINT(("stopping monitoring\n"));
5006 //pfm_stop_monitoring(current, regs);
5008 ctx->ctx_state = PFM_CTX_LOADED;
5013 * context MUST BE LOCKED when calling
5014 * can only be called for current
5016 static void
5017 pfm_context_force_terminate(pfm_context_t *ctx, struct pt_regs *regs)
5019 int ret;
5021 DPRINT(("entering for [%d]\n", task_pid_nr(current)));
5023 ret = pfm_context_unload(ctx, NULL, 0, regs);
5024 if (ret) {
5025 printk(KERN_ERR "pfm_context_force_terminate: [%d] unloaded failed with %d\n", task_pid_nr(current), ret);
5029 * and wakeup controlling task, indicating we are now disconnected
5031 wake_up_interruptible(&ctx->ctx_zombieq);
5034 * given that context is still locked, the controlling
5035 * task will only get access when we return from
5036 * pfm_handle_work().
5040 static int pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds);
5042 * pfm_handle_work() can be called with interrupts enabled
5043 * (TIF_NEED_RESCHED) or disabled. The down_interruptible
5044 * call may sleep, therefore we must re-enable interrupts
5045 * to avoid deadlocks. It is safe to do so because this function
5046 * is called ONLY when returning to user level (PUStk=1), in which case
5047 * there is no risk of kernel stack overflow due to deep
5048 * interrupt nesting.
5050 void
5051 pfm_handle_work(void)
5053 pfm_context_t *ctx;
5054 struct pt_regs *regs;
5055 unsigned long flags, dummy_flags;
5056 unsigned long ovfl_regs;
5057 unsigned int reason;
5058 int ret;
5060 ctx = PFM_GET_CTX(current);
5061 if (ctx == NULL) {
5062 printk(KERN_ERR "perfmon: [%d] has no PFM context\n", task_pid_nr(current));
5063 return;
5066 PROTECT_CTX(ctx, flags);
5068 PFM_SET_WORK_PENDING(current, 0);
5070 tsk_clear_notify_resume(current);
5072 regs = task_pt_regs(current);
5075 * extract reason for being here and clear
5077 reason = ctx->ctx_fl_trap_reason;
5078 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
5079 ovfl_regs = ctx->ctx_ovfl_regs[0];
5081 DPRINT(("reason=%d state=%d\n", reason, ctx->ctx_state));
5084 * must be done before we check for simple-reset mode
5086 if (ctx->ctx_fl_going_zombie || ctx->ctx_state == PFM_CTX_ZOMBIE) goto do_zombie;
5089 //if (CTX_OVFL_NOBLOCK(ctx)) goto skip_blocking;
5090 if (reason == PFM_TRAP_REASON_RESET) goto skip_blocking;
5093 * restore interrupt mask to what it was on entry.
5094 * Could be enabled/diasbled.
5096 UNPROTECT_CTX(ctx, flags);
5099 * force interrupt enable because of down_interruptible()
5101 local_irq_enable();
5103 DPRINT(("before block sleeping\n"));
5106 * may go through without blocking on SMP systems
5107 * if restart has been received already by the time we call down()
5109 ret = wait_for_completion_interruptible(&ctx->ctx_restart_done);
5111 DPRINT(("after block sleeping ret=%d\n", ret));
5114 * lock context and mask interrupts again
5115 * We save flags into a dummy because we may have
5116 * altered interrupts mask compared to entry in this
5117 * function.
5119 PROTECT_CTX(ctx, dummy_flags);
5122 * we need to read the ovfl_regs only after wake-up
5123 * because we may have had pfm_write_pmds() in between
5124 * and that can changed PMD values and therefore
5125 * ovfl_regs is reset for these new PMD values.
5127 ovfl_regs = ctx->ctx_ovfl_regs[0];
5129 if (ctx->ctx_fl_going_zombie) {
5130 do_zombie:
5131 DPRINT(("context is zombie, bailing out\n"));
5132 pfm_context_force_terminate(ctx, regs);
5133 goto nothing_to_do;
5136 * in case of interruption of down() we don't restart anything
5138 if (ret < 0) goto nothing_to_do;
5140 skip_blocking:
5141 pfm_resume_after_ovfl(ctx, ovfl_regs, regs);
5142 ctx->ctx_ovfl_regs[0] = 0UL;
5144 nothing_to_do:
5146 * restore flags as they were upon entry
5148 UNPROTECT_CTX(ctx, flags);
5151 static int
5152 pfm_notify_user(pfm_context_t *ctx, pfm_msg_t *msg)
5154 if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
5155 DPRINT(("ignoring overflow notification, owner is zombie\n"));
5156 return 0;
5159 DPRINT(("waking up somebody\n"));
5161 if (msg) wake_up_interruptible(&ctx->ctx_msgq_wait);
5164 * safe, we are not in intr handler, nor in ctxsw when
5165 * we come here
5167 kill_fasync (&ctx->ctx_async_queue, SIGIO, POLL_IN);
5169 return 0;
5172 static int
5173 pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds)
5175 pfm_msg_t *msg = NULL;
5177 if (ctx->ctx_fl_no_msg == 0) {
5178 msg = pfm_get_new_msg(ctx);
5179 if (msg == NULL) {
5180 printk(KERN_ERR "perfmon: pfm_ovfl_notify_user no more notification msgs\n");
5181 return -1;
5184 msg->pfm_ovfl_msg.msg_type = PFM_MSG_OVFL;
5185 msg->pfm_ovfl_msg.msg_ctx_fd = ctx->ctx_fd;
5186 msg->pfm_ovfl_msg.msg_active_set = 0;
5187 msg->pfm_ovfl_msg.msg_ovfl_pmds[0] = ovfl_pmds;
5188 msg->pfm_ovfl_msg.msg_ovfl_pmds[1] = 0UL;
5189 msg->pfm_ovfl_msg.msg_ovfl_pmds[2] = 0UL;
5190 msg->pfm_ovfl_msg.msg_ovfl_pmds[3] = 0UL;
5191 msg->pfm_ovfl_msg.msg_tstamp = 0UL;
5194 DPRINT(("ovfl msg: msg=%p no_msg=%d fd=%d ovfl_pmds=0x%lx\n",
5195 msg,
5196 ctx->ctx_fl_no_msg,
5197 ctx->ctx_fd,
5198 ovfl_pmds));
5200 return pfm_notify_user(ctx, msg);
5203 static int
5204 pfm_end_notify_user(pfm_context_t *ctx)
5206 pfm_msg_t *msg;
5208 msg = pfm_get_new_msg(ctx);
5209 if (msg == NULL) {
5210 printk(KERN_ERR "perfmon: pfm_end_notify_user no more notification msgs\n");
5211 return -1;
5213 /* no leak */
5214 memset(msg, 0, sizeof(*msg));
5216 msg->pfm_end_msg.msg_type = PFM_MSG_END;
5217 msg->pfm_end_msg.msg_ctx_fd = ctx->ctx_fd;
5218 msg->pfm_ovfl_msg.msg_tstamp = 0UL;
5220 DPRINT(("end msg: msg=%p no_msg=%d ctx_fd=%d\n",
5221 msg,
5222 ctx->ctx_fl_no_msg,
5223 ctx->ctx_fd));
5225 return pfm_notify_user(ctx, msg);
5229 * main overflow processing routine.
5230 * it can be called from the interrupt path or explicitly during the context switch code
5232 static void
5233 pfm_overflow_handler(struct task_struct *task, pfm_context_t *ctx, u64 pmc0, struct pt_regs *regs)
5235 pfm_ovfl_arg_t *ovfl_arg;
5236 unsigned long mask;
5237 unsigned long old_val, ovfl_val, new_val;
5238 unsigned long ovfl_notify = 0UL, ovfl_pmds = 0UL, smpl_pmds = 0UL, reset_pmds;
5239 unsigned long tstamp;
5240 pfm_ovfl_ctrl_t ovfl_ctrl;
5241 unsigned int i, has_smpl;
5242 int must_notify = 0;
5244 if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) goto stop_monitoring;
5247 * sanity test. Should never happen
5249 if (unlikely((pmc0 & 0x1) == 0)) goto sanity_check;
5251 tstamp = ia64_get_itc();
5252 mask = pmc0 >> PMU_FIRST_COUNTER;
5253 ovfl_val = pmu_conf->ovfl_val;
5254 has_smpl = CTX_HAS_SMPL(ctx);
5256 DPRINT_ovfl(("pmc0=0x%lx pid=%d iip=0x%lx, %s "
5257 "used_pmds=0x%lx\n",
5258 pmc0,
5259 task ? task_pid_nr(task): -1,
5260 (regs ? regs->cr_iip : 0),
5261 CTX_OVFL_NOBLOCK(ctx) ? "nonblocking" : "blocking",
5262 ctx->ctx_used_pmds[0]));
5266 * first we update the virtual counters
5267 * assume there was a prior ia64_srlz_d() issued
5269 for (i = PMU_FIRST_COUNTER; mask ; i++, mask >>= 1) {
5271 /* skip pmd which did not overflow */
5272 if ((mask & 0x1) == 0) continue;
5275 * Note that the pmd is not necessarily 0 at this point as qualified events
5276 * may have happened before the PMU was frozen. The residual count is not
5277 * taken into consideration here but will be with any read of the pmd via
5278 * pfm_read_pmds().
5280 old_val = new_val = ctx->ctx_pmds[i].val;
5281 new_val += 1 + ovfl_val;
5282 ctx->ctx_pmds[i].val = new_val;
5285 * check for overflow condition
5287 if (likely(old_val > new_val)) {
5288 ovfl_pmds |= 1UL << i;
5289 if (PMC_OVFL_NOTIFY(ctx, i)) ovfl_notify |= 1UL << i;
5292 DPRINT_ovfl(("ctx_pmd[%d].val=0x%lx old_val=0x%lx pmd=0x%lx ovfl_pmds=0x%lx ovfl_notify=0x%lx\n",
5294 new_val,
5295 old_val,
5296 ia64_get_pmd(i) & ovfl_val,
5297 ovfl_pmds,
5298 ovfl_notify));
5302 * there was no 64-bit overflow, nothing else to do
5304 if (ovfl_pmds == 0UL) return;
5307 * reset all control bits
5309 ovfl_ctrl.val = 0;
5310 reset_pmds = 0UL;
5313 * if a sampling format module exists, then we "cache" the overflow by
5314 * calling the module's handler() routine.
5316 if (has_smpl) {
5317 unsigned long start_cycles, end_cycles;
5318 unsigned long pmd_mask;
5319 int j, k, ret = 0;
5320 int this_cpu = smp_processor_id();
5322 pmd_mask = ovfl_pmds >> PMU_FIRST_COUNTER;
5323 ovfl_arg = &ctx->ctx_ovfl_arg;
5325 prefetch(ctx->ctx_smpl_hdr);
5327 for(i=PMU_FIRST_COUNTER; pmd_mask && ret == 0; i++, pmd_mask >>=1) {
5329 mask = 1UL << i;
5331 if ((pmd_mask & 0x1) == 0) continue;
5333 ovfl_arg->ovfl_pmd = (unsigned char )i;
5334 ovfl_arg->ovfl_notify = ovfl_notify & mask ? 1 : 0;
5335 ovfl_arg->active_set = 0;
5336 ovfl_arg->ovfl_ctrl.val = 0; /* module must fill in all fields */
5337 ovfl_arg->smpl_pmds[0] = smpl_pmds = ctx->ctx_pmds[i].smpl_pmds[0];
5339 ovfl_arg->pmd_value = ctx->ctx_pmds[i].val;
5340 ovfl_arg->pmd_last_reset = ctx->ctx_pmds[i].lval;
5341 ovfl_arg->pmd_eventid = ctx->ctx_pmds[i].eventid;
5344 * copy values of pmds of interest. Sampling format may copy them
5345 * into sampling buffer.
5347 if (smpl_pmds) {
5348 for(j=0, k=0; smpl_pmds; j++, smpl_pmds >>=1) {
5349 if ((smpl_pmds & 0x1) == 0) continue;
5350 ovfl_arg->smpl_pmds_values[k++] = PMD_IS_COUNTING(j) ? pfm_read_soft_counter(ctx, j) : ia64_get_pmd(j);
5351 DPRINT_ovfl(("smpl_pmd[%d]=pmd%u=0x%lx\n", k-1, j, ovfl_arg->smpl_pmds_values[k-1]));
5355 pfm_stats[this_cpu].pfm_smpl_handler_calls++;
5357 start_cycles = ia64_get_itc();
5360 * call custom buffer format record (handler) routine
5362 ret = (*ctx->ctx_buf_fmt->fmt_handler)(task, ctx->ctx_smpl_hdr, ovfl_arg, regs, tstamp);
5364 end_cycles = ia64_get_itc();
5367 * For those controls, we take the union because they have
5368 * an all or nothing behavior.
5370 ovfl_ctrl.bits.notify_user |= ovfl_arg->ovfl_ctrl.bits.notify_user;
5371 ovfl_ctrl.bits.block_task |= ovfl_arg->ovfl_ctrl.bits.block_task;
5372 ovfl_ctrl.bits.mask_monitoring |= ovfl_arg->ovfl_ctrl.bits.mask_monitoring;
5374 * build the bitmask of pmds to reset now
5376 if (ovfl_arg->ovfl_ctrl.bits.reset_ovfl_pmds) reset_pmds |= mask;
5378 pfm_stats[this_cpu].pfm_smpl_handler_cycles += end_cycles - start_cycles;
5381 * when the module cannot handle the rest of the overflows, we abort right here
5383 if (ret && pmd_mask) {
5384 DPRINT(("handler aborts leftover ovfl_pmds=0x%lx\n",
5385 pmd_mask<<PMU_FIRST_COUNTER));
5388 * remove the pmds we reset now from the set of pmds to reset in pfm_restart()
5390 ovfl_pmds &= ~reset_pmds;
5391 } else {
5393 * when no sampling module is used, then the default
5394 * is to notify on overflow if requested by user
5396 ovfl_ctrl.bits.notify_user = ovfl_notify ? 1 : 0;
5397 ovfl_ctrl.bits.block_task = ovfl_notify ? 1 : 0;
5398 ovfl_ctrl.bits.mask_monitoring = ovfl_notify ? 1 : 0; /* XXX: change for saturation */
5399 ovfl_ctrl.bits.reset_ovfl_pmds = ovfl_notify ? 0 : 1;
5401 * if needed, we reset all overflowed pmds
5403 if (ovfl_notify == 0) reset_pmds = ovfl_pmds;
5406 DPRINT_ovfl(("ovfl_pmds=0x%lx reset_pmds=0x%lx\n", ovfl_pmds, reset_pmds));
5409 * reset the requested PMD registers using the short reset values
5411 if (reset_pmds) {
5412 unsigned long bm = reset_pmds;
5413 pfm_reset_regs(ctx, &bm, PFM_PMD_SHORT_RESET);
5416 if (ovfl_notify && ovfl_ctrl.bits.notify_user) {
5418 * keep track of what to reset when unblocking
5420 ctx->ctx_ovfl_regs[0] = ovfl_pmds;
5423 * check for blocking context
5425 if (CTX_OVFL_NOBLOCK(ctx) == 0 && ovfl_ctrl.bits.block_task) {
5427 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_BLOCK;
5430 * set the perfmon specific checking pending work for the task
5432 PFM_SET_WORK_PENDING(task, 1);
5435 * when coming from ctxsw, current still points to the
5436 * previous task, therefore we must work with task and not current.
5438 tsk_set_notify_resume(task);
5441 * defer until state is changed (shorten spin window). the context is locked
5442 * anyway, so the signal receiver would come spin for nothing.
5444 must_notify = 1;
5447 DPRINT_ovfl(("owner [%d] pending=%ld reason=%u ovfl_pmds=0x%lx ovfl_notify=0x%lx masked=%d\n",
5448 GET_PMU_OWNER() ? task_pid_nr(GET_PMU_OWNER()) : -1,
5449 PFM_GET_WORK_PENDING(task),
5450 ctx->ctx_fl_trap_reason,
5451 ovfl_pmds,
5452 ovfl_notify,
5453 ovfl_ctrl.bits.mask_monitoring ? 1 : 0));
5455 * in case monitoring must be stopped, we toggle the psr bits
5457 if (ovfl_ctrl.bits.mask_monitoring) {
5458 pfm_mask_monitoring(task);
5459 ctx->ctx_state = PFM_CTX_MASKED;
5460 ctx->ctx_fl_can_restart = 1;
5464 * send notification now
5466 if (must_notify) pfm_ovfl_notify_user(ctx, ovfl_notify);
5468 return;
5470 sanity_check:
5471 printk(KERN_ERR "perfmon: CPU%d overflow handler [%d] pmc0=0x%lx\n",
5472 smp_processor_id(),
5473 task ? task_pid_nr(task) : -1,
5474 pmc0);
5475 return;
5477 stop_monitoring:
5479 * in SMP, zombie context is never restored but reclaimed in pfm_load_regs().
5480 * Moreover, zombies are also reclaimed in pfm_save_regs(). Therefore we can
5481 * come here as zombie only if the task is the current task. In which case, we
5482 * can access the PMU hardware directly.
5484 * Note that zombies do have PM_VALID set. So here we do the minimal.
5486 * In case the context was zombified it could not be reclaimed at the time
5487 * the monitoring program exited. At this point, the PMU reservation has been
5488 * returned, the sampiing buffer has been freed. We must convert this call
5489 * into a spurious interrupt. However, we must also avoid infinite overflows
5490 * by stopping monitoring for this task. We can only come here for a per-task
5491 * context. All we need to do is to stop monitoring using the psr bits which
5492 * are always task private. By re-enabling secure montioring, we ensure that
5493 * the monitored task will not be able to re-activate monitoring.
5494 * The task will eventually be context switched out, at which point the context
5495 * will be reclaimed (that includes releasing ownership of the PMU).
5497 * So there might be a window of time where the number of per-task session is zero
5498 * yet one PMU might have a owner and get at most one overflow interrupt for a zombie
5499 * context. This is safe because if a per-task session comes in, it will push this one
5500 * out and by the virtue on pfm_save_regs(), this one will disappear. If a system wide
5501 * session is force on that CPU, given that we use task pinning, pfm_save_regs() will
5502 * also push our zombie context out.
5504 * Overall pretty hairy stuff....
5506 DPRINT(("ctx is zombie for [%d], converted to spurious\n", task ? task_pid_nr(task): -1));
5507 pfm_clear_psr_up();
5508 ia64_psr(regs)->up = 0;
5509 ia64_psr(regs)->sp = 1;
5510 return;
5513 static int
5514 pfm_do_interrupt_handler(int irq, void *arg, struct pt_regs *regs)
5516 struct task_struct *task;
5517 pfm_context_t *ctx;
5518 unsigned long flags;
5519 u64 pmc0;
5520 int this_cpu = smp_processor_id();
5521 int retval = 0;
5523 pfm_stats[this_cpu].pfm_ovfl_intr_count++;
5526 * srlz.d done before arriving here
5528 pmc0 = ia64_get_pmc(0);
5530 task = GET_PMU_OWNER();
5531 ctx = GET_PMU_CTX();
5534 * if we have some pending bits set
5535 * assumes : if any PMC0.bit[63-1] is set, then PMC0.fr = 1
5537 if (PMC0_HAS_OVFL(pmc0) && task) {
5539 * we assume that pmc0.fr is always set here
5542 /* sanity check */
5543 if (!ctx) goto report_spurious1;
5545 if (ctx->ctx_fl_system == 0 && (task->thread.flags & IA64_THREAD_PM_VALID) == 0)
5546 goto report_spurious2;
5548 PROTECT_CTX_NOPRINT(ctx, flags);
5550 pfm_overflow_handler(task, ctx, pmc0, regs);
5552 UNPROTECT_CTX_NOPRINT(ctx, flags);
5554 } else {
5555 pfm_stats[this_cpu].pfm_spurious_ovfl_intr_count++;
5556 retval = -1;
5559 * keep it unfrozen at all times
5561 pfm_unfreeze_pmu();
5563 return retval;
5565 report_spurious1:
5566 printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d has no PFM context\n",
5567 this_cpu, task_pid_nr(task));
5568 pfm_unfreeze_pmu();
5569 return -1;
5570 report_spurious2:
5571 printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d, invalid flag\n",
5572 this_cpu,
5573 task_pid_nr(task));
5574 pfm_unfreeze_pmu();
5575 return -1;
5578 static irqreturn_t
5579 pfm_interrupt_handler(int irq, void *arg)
5581 unsigned long start_cycles, total_cycles;
5582 unsigned long min, max;
5583 int this_cpu;
5584 int ret;
5585 struct pt_regs *regs = get_irq_regs();
5587 this_cpu = get_cpu();
5588 if (likely(!pfm_alt_intr_handler)) {
5589 min = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min;
5590 max = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max;
5592 start_cycles = ia64_get_itc();
5594 ret = pfm_do_interrupt_handler(irq, arg, regs);
5596 total_cycles = ia64_get_itc();
5599 * don't measure spurious interrupts
5601 if (likely(ret == 0)) {
5602 total_cycles -= start_cycles;
5604 if (total_cycles < min) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min = total_cycles;
5605 if (total_cycles > max) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max = total_cycles;
5607 pfm_stats[this_cpu].pfm_ovfl_intr_cycles += total_cycles;
5610 else {
5611 (*pfm_alt_intr_handler->handler)(irq, arg, regs);
5614 put_cpu_no_resched();
5615 return IRQ_HANDLED;
5619 * /proc/perfmon interface, for debug only
5622 #define PFM_PROC_SHOW_HEADER ((void *)NR_CPUS+1)
5624 static void *
5625 pfm_proc_start(struct seq_file *m, loff_t *pos)
5627 if (*pos == 0) {
5628 return PFM_PROC_SHOW_HEADER;
5631 while (*pos <= NR_CPUS) {
5632 if (cpu_online(*pos - 1)) {
5633 return (void *)*pos;
5635 ++*pos;
5637 return NULL;
5640 static void *
5641 pfm_proc_next(struct seq_file *m, void *v, loff_t *pos)
5643 ++*pos;
5644 return pfm_proc_start(m, pos);
5647 static void
5648 pfm_proc_stop(struct seq_file *m, void *v)
5652 static void
5653 pfm_proc_show_header(struct seq_file *m)
5655 struct list_head * pos;
5656 pfm_buffer_fmt_t * entry;
5657 unsigned long flags;
5659 seq_printf(m,
5660 "perfmon version : %u.%u\n"
5661 "model : %s\n"
5662 "fastctxsw : %s\n"
5663 "expert mode : %s\n"
5664 "ovfl_mask : 0x%lx\n"
5665 "PMU flags : 0x%x\n",
5666 PFM_VERSION_MAJ, PFM_VERSION_MIN,
5667 pmu_conf->pmu_name,
5668 pfm_sysctl.fastctxsw > 0 ? "Yes": "No",
5669 pfm_sysctl.expert_mode > 0 ? "Yes": "No",
5670 pmu_conf->ovfl_val,
5671 pmu_conf->flags);
5673 LOCK_PFS(flags);
5675 seq_printf(m,
5676 "proc_sessions : %u\n"
5677 "sys_sessions : %u\n"
5678 "sys_use_dbregs : %u\n"
5679 "ptrace_use_dbregs : %u\n",
5680 pfm_sessions.pfs_task_sessions,
5681 pfm_sessions.pfs_sys_sessions,
5682 pfm_sessions.pfs_sys_use_dbregs,
5683 pfm_sessions.pfs_ptrace_use_dbregs);
5685 UNLOCK_PFS(flags);
5687 spin_lock(&pfm_buffer_fmt_lock);
5689 list_for_each(pos, &pfm_buffer_fmt_list) {
5690 entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
5691 seq_printf(m, "format : %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x %s\n",
5692 entry->fmt_uuid[0],
5693 entry->fmt_uuid[1],
5694 entry->fmt_uuid[2],
5695 entry->fmt_uuid[3],
5696 entry->fmt_uuid[4],
5697 entry->fmt_uuid[5],
5698 entry->fmt_uuid[6],
5699 entry->fmt_uuid[7],
5700 entry->fmt_uuid[8],
5701 entry->fmt_uuid[9],
5702 entry->fmt_uuid[10],
5703 entry->fmt_uuid[11],
5704 entry->fmt_uuid[12],
5705 entry->fmt_uuid[13],
5706 entry->fmt_uuid[14],
5707 entry->fmt_uuid[15],
5708 entry->fmt_name);
5710 spin_unlock(&pfm_buffer_fmt_lock);
5714 static int
5715 pfm_proc_show(struct seq_file *m, void *v)
5717 unsigned long psr;
5718 unsigned int i;
5719 int cpu;
5721 if (v == PFM_PROC_SHOW_HEADER) {
5722 pfm_proc_show_header(m);
5723 return 0;
5726 /* show info for CPU (v - 1) */
5728 cpu = (long)v - 1;
5729 seq_printf(m,
5730 "CPU%-2d overflow intrs : %lu\n"
5731 "CPU%-2d overflow cycles : %lu\n"
5732 "CPU%-2d overflow min : %lu\n"
5733 "CPU%-2d overflow max : %lu\n"
5734 "CPU%-2d smpl handler calls : %lu\n"
5735 "CPU%-2d smpl handler cycles : %lu\n"
5736 "CPU%-2d spurious intrs : %lu\n"
5737 "CPU%-2d replay intrs : %lu\n"
5738 "CPU%-2d syst_wide : %d\n"
5739 "CPU%-2d dcr_pp : %d\n"
5740 "CPU%-2d exclude idle : %d\n"
5741 "CPU%-2d owner : %d\n"
5742 "CPU%-2d context : %p\n"
5743 "CPU%-2d activations : %lu\n",
5744 cpu, pfm_stats[cpu].pfm_ovfl_intr_count,
5745 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles,
5746 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_min,
5747 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_max,
5748 cpu, pfm_stats[cpu].pfm_smpl_handler_calls,
5749 cpu, pfm_stats[cpu].pfm_smpl_handler_cycles,
5750 cpu, pfm_stats[cpu].pfm_spurious_ovfl_intr_count,
5751 cpu, pfm_stats[cpu].pfm_replay_ovfl_intr_count,
5752 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_SYST_WIDE ? 1 : 0,
5753 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_DCR_PP ? 1 : 0,
5754 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_EXCL_IDLE ? 1 : 0,
5755 cpu, pfm_get_cpu_data(pmu_owner, cpu) ? pfm_get_cpu_data(pmu_owner, cpu)->pid: -1,
5756 cpu, pfm_get_cpu_data(pmu_ctx, cpu),
5757 cpu, pfm_get_cpu_data(pmu_activation_number, cpu));
5759 if (num_online_cpus() == 1 && pfm_sysctl.debug > 0) {
5761 psr = pfm_get_psr();
5763 ia64_srlz_d();
5765 seq_printf(m,
5766 "CPU%-2d psr : 0x%lx\n"
5767 "CPU%-2d pmc0 : 0x%lx\n",
5768 cpu, psr,
5769 cpu, ia64_get_pmc(0));
5771 for (i=0; PMC_IS_LAST(i) == 0; i++) {
5772 if (PMC_IS_COUNTING(i) == 0) continue;
5773 seq_printf(m,
5774 "CPU%-2d pmc%u : 0x%lx\n"
5775 "CPU%-2d pmd%u : 0x%lx\n",
5776 cpu, i, ia64_get_pmc(i),
5777 cpu, i, ia64_get_pmd(i));
5780 return 0;
5783 const struct seq_operations pfm_seq_ops = {
5784 .start = pfm_proc_start,
5785 .next = pfm_proc_next,
5786 .stop = pfm_proc_stop,
5787 .show = pfm_proc_show
5790 static int
5791 pfm_proc_open(struct inode *inode, struct file *file)
5793 return seq_open(file, &pfm_seq_ops);
5798 * we come here as soon as local_cpu_data->pfm_syst_wide is set. this happens
5799 * during pfm_enable() hence before pfm_start(). We cannot assume monitoring
5800 * is active or inactive based on mode. We must rely on the value in
5801 * local_cpu_data->pfm_syst_info
5803 void
5804 pfm_syst_wide_update_task(struct task_struct *task, unsigned long info, int is_ctxswin)
5806 struct pt_regs *regs;
5807 unsigned long dcr;
5808 unsigned long dcr_pp;
5810 dcr_pp = info & PFM_CPUINFO_DCR_PP ? 1 : 0;
5813 * pid 0 is guaranteed to be the idle task. There is one such task with pid 0
5814 * on every CPU, so we can rely on the pid to identify the idle task.
5816 if ((info & PFM_CPUINFO_EXCL_IDLE) == 0 || task->pid) {
5817 regs = task_pt_regs(task);
5818 ia64_psr(regs)->pp = is_ctxswin ? dcr_pp : 0;
5819 return;
5822 * if monitoring has started
5824 if (dcr_pp) {
5825 dcr = ia64_getreg(_IA64_REG_CR_DCR);
5827 * context switching in?
5829 if (is_ctxswin) {
5830 /* mask monitoring for the idle task */
5831 ia64_setreg(_IA64_REG_CR_DCR, dcr & ~IA64_DCR_PP);
5832 pfm_clear_psr_pp();
5833 ia64_srlz_i();
5834 return;
5837 * context switching out
5838 * restore monitoring for next task
5840 * Due to inlining this odd if-then-else construction generates
5841 * better code.
5843 ia64_setreg(_IA64_REG_CR_DCR, dcr |IA64_DCR_PP);
5844 pfm_set_psr_pp();
5845 ia64_srlz_i();
5849 #ifdef CONFIG_SMP
5851 static void
5852 pfm_force_cleanup(pfm_context_t *ctx, struct pt_regs *regs)
5854 struct task_struct *task = ctx->ctx_task;
5856 ia64_psr(regs)->up = 0;
5857 ia64_psr(regs)->sp = 1;
5859 if (GET_PMU_OWNER() == task) {
5860 DPRINT(("cleared ownership for [%d]\n",
5861 task_pid_nr(ctx->ctx_task)));
5862 SET_PMU_OWNER(NULL, NULL);
5866 * disconnect the task from the context and vice-versa
5868 PFM_SET_WORK_PENDING(task, 0);
5870 task->thread.pfm_context = NULL;
5871 task->thread.flags &= ~IA64_THREAD_PM_VALID;
5873 DPRINT(("force cleanup for [%d]\n", task_pid_nr(task)));
5878 * in 2.6, interrupts are masked when we come here and the runqueue lock is held
5880 void
5881 pfm_save_regs(struct task_struct *task)
5883 pfm_context_t *ctx;
5884 unsigned long flags;
5885 u64 psr;
5888 ctx = PFM_GET_CTX(task);
5889 if (ctx == NULL) return;
5892 * we always come here with interrupts ALREADY disabled by
5893 * the scheduler. So we simply need to protect against concurrent
5894 * access, not CPU concurrency.
5896 flags = pfm_protect_ctx_ctxsw(ctx);
5898 if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
5899 struct pt_regs *regs = task_pt_regs(task);
5901 pfm_clear_psr_up();
5903 pfm_force_cleanup(ctx, regs);
5905 BUG_ON(ctx->ctx_smpl_hdr);
5907 pfm_unprotect_ctx_ctxsw(ctx, flags);
5909 pfm_context_free(ctx);
5910 return;
5914 * save current PSR: needed because we modify it
5916 ia64_srlz_d();
5917 psr = pfm_get_psr();
5919 BUG_ON(psr & (IA64_PSR_I));
5922 * stop monitoring:
5923 * This is the last instruction which may generate an overflow
5925 * We do not need to set psr.sp because, it is irrelevant in kernel.
5926 * It will be restored from ipsr when going back to user level
5928 pfm_clear_psr_up();
5931 * keep a copy of psr.up (for reload)
5933 ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
5936 * release ownership of this PMU.
5937 * PM interrupts are masked, so nothing
5938 * can happen.
5940 SET_PMU_OWNER(NULL, NULL);
5943 * we systematically save the PMD as we have no
5944 * guarantee we will be schedule at that same
5945 * CPU again.
5947 pfm_save_pmds(ctx->th_pmds, ctx->ctx_used_pmds[0]);
5950 * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
5951 * we will need it on the restore path to check
5952 * for pending overflow.
5954 ctx->th_pmcs[0] = ia64_get_pmc(0);
5957 * unfreeze PMU if had pending overflows
5959 if (ctx->th_pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
5962 * finally, allow context access.
5963 * interrupts will still be masked after this call.
5965 pfm_unprotect_ctx_ctxsw(ctx, flags);
5968 #else /* !CONFIG_SMP */
5969 void
5970 pfm_save_regs(struct task_struct *task)
5972 pfm_context_t *ctx;
5973 u64 psr;
5975 ctx = PFM_GET_CTX(task);
5976 if (ctx == NULL) return;
5979 * save current PSR: needed because we modify it
5981 psr = pfm_get_psr();
5983 BUG_ON(psr & (IA64_PSR_I));
5986 * stop monitoring:
5987 * This is the last instruction which may generate an overflow
5989 * We do not need to set psr.sp because, it is irrelevant in kernel.
5990 * It will be restored from ipsr when going back to user level
5992 pfm_clear_psr_up();
5995 * keep a copy of psr.up (for reload)
5997 ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
6000 static void
6001 pfm_lazy_save_regs (struct task_struct *task)
6003 pfm_context_t *ctx;
6004 unsigned long flags;
6006 { u64 psr = pfm_get_psr();
6007 BUG_ON(psr & IA64_PSR_UP);
6010 ctx = PFM_GET_CTX(task);
6013 * we need to mask PMU overflow here to
6014 * make sure that we maintain pmc0 until
6015 * we save it. overflow interrupts are
6016 * treated as spurious if there is no
6017 * owner.
6019 * XXX: I don't think this is necessary
6021 PROTECT_CTX(ctx,flags);
6024 * release ownership of this PMU.
6025 * must be done before we save the registers.
6027 * after this call any PMU interrupt is treated
6028 * as spurious.
6030 SET_PMU_OWNER(NULL, NULL);
6033 * save all the pmds we use
6035 pfm_save_pmds(ctx->th_pmds, ctx->ctx_used_pmds[0]);
6038 * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
6039 * it is needed to check for pended overflow
6040 * on the restore path
6042 ctx->th_pmcs[0] = ia64_get_pmc(0);
6045 * unfreeze PMU if had pending overflows
6047 if (ctx->th_pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
6050 * now get can unmask PMU interrupts, they will
6051 * be treated as purely spurious and we will not
6052 * lose any information
6054 UNPROTECT_CTX(ctx,flags);
6056 #endif /* CONFIG_SMP */
6058 #ifdef CONFIG_SMP
6060 * in 2.6, interrupts are masked when we come here and the runqueue lock is held
6062 void
6063 pfm_load_regs (struct task_struct *task)
6065 pfm_context_t *ctx;
6066 unsigned long pmc_mask = 0UL, pmd_mask = 0UL;
6067 unsigned long flags;
6068 u64 psr, psr_up;
6069 int need_irq_resend;
6071 ctx = PFM_GET_CTX(task);
6072 if (unlikely(ctx == NULL)) return;
6074 BUG_ON(GET_PMU_OWNER());
6077 * possible on unload
6079 if (unlikely((task->thread.flags & IA64_THREAD_PM_VALID) == 0)) return;
6082 * we always come here with interrupts ALREADY disabled by
6083 * the scheduler. So we simply need to protect against concurrent
6084 * access, not CPU concurrency.
6086 flags = pfm_protect_ctx_ctxsw(ctx);
6087 psr = pfm_get_psr();
6089 need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
6091 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
6092 BUG_ON(psr & IA64_PSR_I);
6094 if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) {
6095 struct pt_regs *regs = task_pt_regs(task);
6097 BUG_ON(ctx->ctx_smpl_hdr);
6099 pfm_force_cleanup(ctx, regs);
6101 pfm_unprotect_ctx_ctxsw(ctx, flags);
6104 * this one (kmalloc'ed) is fine with interrupts disabled
6106 pfm_context_free(ctx);
6108 return;
6112 * we restore ALL the debug registers to avoid picking up
6113 * stale state.
6115 if (ctx->ctx_fl_using_dbreg) {
6116 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
6117 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
6120 * retrieve saved psr.up
6122 psr_up = ctx->ctx_saved_psr_up;
6125 * if we were the last user of the PMU on that CPU,
6126 * then nothing to do except restore psr
6128 if (GET_LAST_CPU(ctx) == smp_processor_id() && ctx->ctx_last_activation == GET_ACTIVATION()) {
6131 * retrieve partial reload masks (due to user modifications)
6133 pmc_mask = ctx->ctx_reload_pmcs[0];
6134 pmd_mask = ctx->ctx_reload_pmds[0];
6136 } else {
6138 * To avoid leaking information to the user level when psr.sp=0,
6139 * we must reload ALL implemented pmds (even the ones we don't use).
6140 * In the kernel we only allow PFM_READ_PMDS on registers which
6141 * we initialized or requested (sampling) so there is no risk there.
6143 pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
6146 * ALL accessible PMCs are systematically reloaded, unused registers
6147 * get their default (from pfm_reset_pmu_state()) values to avoid picking
6148 * up stale configuration.
6150 * PMC0 is never in the mask. It is always restored separately.
6152 pmc_mask = ctx->ctx_all_pmcs[0];
6155 * when context is MASKED, we will restore PMC with plm=0
6156 * and PMD with stale information, but that's ok, nothing
6157 * will be captured.
6159 * XXX: optimize here
6161 if (pmd_mask) pfm_restore_pmds(ctx->th_pmds, pmd_mask);
6162 if (pmc_mask) pfm_restore_pmcs(ctx->th_pmcs, pmc_mask);
6165 * check for pending overflow at the time the state
6166 * was saved.
6168 if (unlikely(PMC0_HAS_OVFL(ctx->th_pmcs[0]))) {
6170 * reload pmc0 with the overflow information
6171 * On McKinley PMU, this will trigger a PMU interrupt
6173 ia64_set_pmc(0, ctx->th_pmcs[0]);
6174 ia64_srlz_d();
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 * we just did a reload, so we reset the partial reload fields
6188 ctx->ctx_reload_pmcs[0] = 0UL;
6189 ctx->ctx_reload_pmds[0] = 0UL;
6191 SET_LAST_CPU(ctx, smp_processor_id());
6194 * dump activation value for this PMU
6196 INC_ACTIVATION();
6198 * record current activation for this context
6200 SET_ACTIVATION(ctx);
6203 * establish new ownership.
6205 SET_PMU_OWNER(task, ctx);
6208 * restore the psr.up bit. measurement
6209 * is active again.
6210 * no PMU interrupt can happen at this point
6211 * because we still have interrupts disabled.
6213 if (likely(psr_up)) pfm_set_psr_up();
6216 * allow concurrent access to context
6218 pfm_unprotect_ctx_ctxsw(ctx, flags);
6220 #else /* !CONFIG_SMP */
6222 * reload PMU state for UP kernels
6223 * in 2.5 we come here with interrupts disabled
6225 void
6226 pfm_load_regs (struct task_struct *task)
6228 pfm_context_t *ctx;
6229 struct task_struct *owner;
6230 unsigned long pmd_mask, pmc_mask;
6231 u64 psr, psr_up;
6232 int need_irq_resend;
6234 owner = GET_PMU_OWNER();
6235 ctx = PFM_GET_CTX(task);
6236 psr = pfm_get_psr();
6238 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
6239 BUG_ON(psr & IA64_PSR_I);
6242 * we restore ALL the debug registers to avoid picking up
6243 * stale state.
6245 * This must be done even when the task is still the owner
6246 * as the registers may have been modified via ptrace()
6247 * (not perfmon) by the previous task.
6249 if (ctx->ctx_fl_using_dbreg) {
6250 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
6251 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
6255 * retrieved saved psr.up
6257 psr_up = ctx->ctx_saved_psr_up;
6258 need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
6261 * short path, our state is still there, just
6262 * need to restore psr and we go
6264 * we do not touch either PMC nor PMD. the psr is not touched
6265 * by the overflow_handler. So we are safe w.r.t. to interrupt
6266 * concurrency even without interrupt masking.
6268 if (likely(owner == task)) {
6269 if (likely(psr_up)) pfm_set_psr_up();
6270 return;
6274 * someone else is still using the PMU, first push it out and
6275 * then we'll be able to install our stuff !
6277 * Upon return, there will be no owner for the current PMU
6279 if (owner) pfm_lazy_save_regs(owner);
6282 * To avoid leaking information to the user level when psr.sp=0,
6283 * we must reload ALL implemented pmds (even the ones we don't use).
6284 * In the kernel we only allow PFM_READ_PMDS on registers which
6285 * we initialized or requested (sampling) so there is no risk there.
6287 pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
6290 * ALL accessible PMCs are systematically reloaded, unused registers
6291 * get their default (from pfm_reset_pmu_state()) values to avoid picking
6292 * up stale configuration.
6294 * PMC0 is never in the mask. It is always restored separately
6296 pmc_mask = ctx->ctx_all_pmcs[0];
6298 pfm_restore_pmds(ctx->th_pmds, pmd_mask);
6299 pfm_restore_pmcs(ctx->th_pmcs, pmc_mask);
6302 * check for pending overflow at the time the state
6303 * was saved.
6305 if (unlikely(PMC0_HAS_OVFL(ctx->th_pmcs[0]))) {
6307 * reload pmc0 with the overflow information
6308 * On McKinley PMU, this will trigger a PMU interrupt
6310 ia64_set_pmc(0, ctx->th_pmcs[0]);
6311 ia64_srlz_d();
6313 ctx->th_pmcs[0] = 0UL;
6316 * will replay the PMU interrupt
6318 if (need_irq_resend) ia64_resend_irq(IA64_PERFMON_VECTOR);
6320 pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
6324 * establish new ownership.
6326 SET_PMU_OWNER(task, ctx);
6329 * restore the psr.up bit. measurement
6330 * is active again.
6331 * no PMU interrupt can happen at this point
6332 * because we still have interrupts disabled.
6334 if (likely(psr_up)) pfm_set_psr_up();
6336 #endif /* CONFIG_SMP */
6339 * this function assumes monitoring is stopped
6341 static void
6342 pfm_flush_pmds(struct task_struct *task, pfm_context_t *ctx)
6344 u64 pmc0;
6345 unsigned long mask2, val, pmd_val, ovfl_val;
6346 int i, can_access_pmu = 0;
6347 int is_self;
6350 * is the caller the task being monitored (or which initiated the
6351 * session for system wide measurements)
6353 is_self = ctx->ctx_task == task ? 1 : 0;
6356 * can access PMU is task is the owner of the PMU state on the current CPU
6357 * or if we are running on the CPU bound to the context in system-wide mode
6358 * (that is not necessarily the task the context is attached to in this mode).
6359 * In system-wide we always have can_access_pmu true because a task running on an
6360 * invalid processor is flagged earlier in the call stack (see pfm_stop).
6362 can_access_pmu = (GET_PMU_OWNER() == task) || (ctx->ctx_fl_system && ctx->ctx_cpu == smp_processor_id());
6363 if (can_access_pmu) {
6365 * Mark the PMU as not owned
6366 * This will cause the interrupt handler to do nothing in case an overflow
6367 * interrupt was in-flight
6368 * This also guarantees that pmc0 will contain the final state
6369 * It virtually gives us full control on overflow processing from that point
6370 * on.
6372 SET_PMU_OWNER(NULL, NULL);
6373 DPRINT(("releasing ownership\n"));
6376 * read current overflow status:
6378 * we are guaranteed to read the final stable state
6380 ia64_srlz_d();
6381 pmc0 = ia64_get_pmc(0); /* slow */
6384 * reset freeze bit, overflow status information destroyed
6386 pfm_unfreeze_pmu();
6387 } else {
6388 pmc0 = ctx->th_pmcs[0];
6390 * clear whatever overflow status bits there were
6392 ctx->th_pmcs[0] = 0;
6394 ovfl_val = pmu_conf->ovfl_val;
6396 * we save all the used pmds
6397 * we take care of overflows for counting PMDs
6399 * XXX: sampling situation is not taken into account here
6401 mask2 = ctx->ctx_used_pmds[0];
6403 DPRINT(("is_self=%d ovfl_val=0x%lx mask2=0x%lx\n", is_self, ovfl_val, mask2));
6405 for (i = 0; mask2; i++, mask2>>=1) {
6407 /* skip non used pmds */
6408 if ((mask2 & 0x1) == 0) continue;
6411 * can access PMU always true in system wide mode
6413 val = pmd_val = can_access_pmu ? ia64_get_pmd(i) : ctx->th_pmds[i];
6415 if (PMD_IS_COUNTING(i)) {
6416 DPRINT(("[%d] pmd[%d] ctx_pmd=0x%lx hw_pmd=0x%lx\n",
6417 task_pid_nr(task),
6419 ctx->ctx_pmds[i].val,
6420 val & ovfl_val));
6423 * we rebuild the full 64 bit value of the counter
6425 val = ctx->ctx_pmds[i].val + (val & ovfl_val);
6428 * now everything is in ctx_pmds[] and we need
6429 * to clear the saved context from save_regs() such that
6430 * pfm_read_pmds() gets the correct value
6432 pmd_val = 0UL;
6435 * take care of overflow inline
6437 if (pmc0 & (1UL << i)) {
6438 val += 1 + ovfl_val;
6439 DPRINT(("[%d] pmd[%d] overflowed\n", task_pid_nr(task), i));
6443 DPRINT(("[%d] ctx_pmd[%d]=0x%lx pmd_val=0x%lx\n", task_pid_nr(task), i, val, pmd_val));
6445 if (is_self) ctx->th_pmds[i] = pmd_val;
6447 ctx->ctx_pmds[i].val = val;
6451 static struct irqaction perfmon_irqaction = {
6452 .handler = pfm_interrupt_handler,
6453 .flags = IRQF_DISABLED,
6454 .name = "perfmon"
6457 static void
6458 pfm_alt_save_pmu_state(void *data)
6460 struct pt_regs *regs;
6462 regs = task_pt_regs(current);
6464 DPRINT(("called\n"));
6467 * should not be necessary but
6468 * let's take not risk
6470 pfm_clear_psr_up();
6471 pfm_clear_psr_pp();
6472 ia64_psr(regs)->pp = 0;
6475 * This call is required
6476 * May cause a spurious interrupt on some processors
6478 pfm_freeze_pmu();
6480 ia64_srlz_d();
6483 void
6484 pfm_alt_restore_pmu_state(void *data)
6486 struct pt_regs *regs;
6488 regs = task_pt_regs(current);
6490 DPRINT(("called\n"));
6493 * put PMU back in state expected
6494 * by perfmon
6496 pfm_clear_psr_up();
6497 pfm_clear_psr_pp();
6498 ia64_psr(regs)->pp = 0;
6501 * perfmon runs with PMU unfrozen at all times
6503 pfm_unfreeze_pmu();
6505 ia64_srlz_d();
6509 pfm_install_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
6511 int ret, i;
6512 int reserve_cpu;
6514 /* some sanity checks */
6515 if (hdl == NULL || hdl->handler == NULL) return -EINVAL;
6517 /* do the easy test first */
6518 if (pfm_alt_intr_handler) return -EBUSY;
6520 /* one at a time in the install or remove, just fail the others */
6521 if (!spin_trylock(&pfm_alt_install_check)) {
6522 return -EBUSY;
6525 /* reserve our session */
6526 for_each_online_cpu(reserve_cpu) {
6527 ret = pfm_reserve_session(NULL, 1, reserve_cpu);
6528 if (ret) goto cleanup_reserve;
6531 /* save the current system wide pmu states */
6532 ret = on_each_cpu(pfm_alt_save_pmu_state, NULL, 0, 1);
6533 if (ret) {
6534 DPRINT(("on_each_cpu() failed: %d\n", ret));
6535 goto cleanup_reserve;
6538 /* officially change to the alternate interrupt handler */
6539 pfm_alt_intr_handler = hdl;
6541 spin_unlock(&pfm_alt_install_check);
6543 return 0;
6545 cleanup_reserve:
6546 for_each_online_cpu(i) {
6547 /* don't unreserve more than we reserved */
6548 if (i >= reserve_cpu) break;
6550 pfm_unreserve_session(NULL, 1, i);
6553 spin_unlock(&pfm_alt_install_check);
6555 return ret;
6557 EXPORT_SYMBOL_GPL(pfm_install_alt_pmu_interrupt);
6560 pfm_remove_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
6562 int i;
6563 int ret;
6565 if (hdl == NULL) return -EINVAL;
6567 /* cannot remove someone else's handler! */
6568 if (pfm_alt_intr_handler != hdl) return -EINVAL;
6570 /* one at a time in the install or remove, just fail the others */
6571 if (!spin_trylock(&pfm_alt_install_check)) {
6572 return -EBUSY;
6575 pfm_alt_intr_handler = NULL;
6577 ret = on_each_cpu(pfm_alt_restore_pmu_state, NULL, 0, 1);
6578 if (ret) {
6579 DPRINT(("on_each_cpu() failed: %d\n", ret));
6582 for_each_online_cpu(i) {
6583 pfm_unreserve_session(NULL, 1, i);
6586 spin_unlock(&pfm_alt_install_check);
6588 return 0;
6590 EXPORT_SYMBOL_GPL(pfm_remove_alt_pmu_interrupt);
6593 * perfmon initialization routine, called from the initcall() table
6595 static int init_pfm_fs(void);
6597 static int __init
6598 pfm_probe_pmu(void)
6600 pmu_config_t **p;
6601 int family;
6603 family = local_cpu_data->family;
6604 p = pmu_confs;
6606 while(*p) {
6607 if ((*p)->probe) {
6608 if ((*p)->probe() == 0) goto found;
6609 } else if ((*p)->pmu_family == family || (*p)->pmu_family == 0xff) {
6610 goto found;
6612 p++;
6614 return -1;
6615 found:
6616 pmu_conf = *p;
6617 return 0;
6620 static const struct file_operations pfm_proc_fops = {
6621 .open = pfm_proc_open,
6622 .read = seq_read,
6623 .llseek = seq_lseek,
6624 .release = seq_release,
6627 int __init
6628 pfm_init(void)
6630 unsigned int n, n_counters, i;
6632 printk("perfmon: version %u.%u IRQ %u\n",
6633 PFM_VERSION_MAJ,
6634 PFM_VERSION_MIN,
6635 IA64_PERFMON_VECTOR);
6637 if (pfm_probe_pmu()) {
6638 printk(KERN_INFO "perfmon: disabled, there is no support for processor family %d\n",
6639 local_cpu_data->family);
6640 return -ENODEV;
6644 * compute the number of implemented PMD/PMC from the
6645 * description tables
6647 n = 0;
6648 for (i=0; PMC_IS_LAST(i) == 0; i++) {
6649 if (PMC_IS_IMPL(i) == 0) continue;
6650 pmu_conf->impl_pmcs[i>>6] |= 1UL << (i&63);
6651 n++;
6653 pmu_conf->num_pmcs = n;
6655 n = 0; n_counters = 0;
6656 for (i=0; PMD_IS_LAST(i) == 0; i++) {
6657 if (PMD_IS_IMPL(i) == 0) continue;
6658 pmu_conf->impl_pmds[i>>6] |= 1UL << (i&63);
6659 n++;
6660 if (PMD_IS_COUNTING(i)) n_counters++;
6662 pmu_conf->num_pmds = n;
6663 pmu_conf->num_counters = n_counters;
6666 * sanity checks on the number of debug registers
6668 if (pmu_conf->use_rr_dbregs) {
6669 if (pmu_conf->num_ibrs > IA64_NUM_DBG_REGS) {
6670 printk(KERN_INFO "perfmon: unsupported number of code debug registers (%u)\n", pmu_conf->num_ibrs);
6671 pmu_conf = NULL;
6672 return -1;
6674 if (pmu_conf->num_dbrs > IA64_NUM_DBG_REGS) {
6675 printk(KERN_INFO "perfmon: unsupported number of data debug registers (%u)\n", pmu_conf->num_ibrs);
6676 pmu_conf = NULL;
6677 return -1;
6681 printk("perfmon: %s PMU detected, %u PMCs, %u PMDs, %u counters (%lu bits)\n",
6682 pmu_conf->pmu_name,
6683 pmu_conf->num_pmcs,
6684 pmu_conf->num_pmds,
6685 pmu_conf->num_counters,
6686 ffz(pmu_conf->ovfl_val));
6688 /* sanity check */
6689 if (pmu_conf->num_pmds >= PFM_NUM_PMD_REGS || pmu_conf->num_pmcs >= PFM_NUM_PMC_REGS) {
6690 printk(KERN_ERR "perfmon: not enough pmc/pmd, perfmon disabled\n");
6691 pmu_conf = NULL;
6692 return -1;
6696 * create /proc/perfmon (mostly for debugging purposes)
6698 perfmon_dir = create_proc_entry("perfmon", S_IRUGO, NULL);
6699 if (perfmon_dir == NULL) {
6700 printk(KERN_ERR "perfmon: cannot create /proc entry, perfmon disabled\n");
6701 pmu_conf = NULL;
6702 return -1;
6705 * install customized file operations for /proc/perfmon entry
6707 perfmon_dir->proc_fops = &pfm_proc_fops;
6710 * create /proc/sys/kernel/perfmon (for debugging purposes)
6712 pfm_sysctl_header = register_sysctl_table(pfm_sysctl_root);
6715 * initialize all our spinlocks
6717 spin_lock_init(&pfm_sessions.pfs_lock);
6718 spin_lock_init(&pfm_buffer_fmt_lock);
6720 init_pfm_fs();
6722 for(i=0; i < NR_CPUS; i++) pfm_stats[i].pfm_ovfl_intr_cycles_min = ~0UL;
6724 return 0;
6727 __initcall(pfm_init);
6730 * this function is called before pfm_init()
6732 void
6733 pfm_init_percpu (void)
6735 static int first_time=1;
6737 * make sure no measurement is active
6738 * (may inherit programmed PMCs from EFI).
6740 pfm_clear_psr_pp();
6741 pfm_clear_psr_up();
6744 * we run with the PMU not frozen at all times
6746 pfm_unfreeze_pmu();
6748 if (first_time) {
6749 register_percpu_irq(IA64_PERFMON_VECTOR, &perfmon_irqaction);
6750 first_time=0;
6753 ia64_setreg(_IA64_REG_CR_PMV, IA64_PERFMON_VECTOR);
6754 ia64_srlz_d();
6758 * used for debug purposes only
6760 void
6761 dump_pmu_state(const char *from)
6763 struct task_struct *task;
6764 struct pt_regs *regs;
6765 pfm_context_t *ctx;
6766 unsigned long psr, dcr, info, flags;
6767 int i, this_cpu;
6769 local_irq_save(flags);
6771 this_cpu = smp_processor_id();
6772 regs = task_pt_regs(current);
6773 info = PFM_CPUINFO_GET();
6774 dcr = ia64_getreg(_IA64_REG_CR_DCR);
6776 if (info == 0 && ia64_psr(regs)->pp == 0 && (dcr & IA64_DCR_PP) == 0) {
6777 local_irq_restore(flags);
6778 return;
6781 printk("CPU%d from %s() current [%d] iip=0x%lx %s\n",
6782 this_cpu,
6783 from,
6784 task_pid_nr(current),
6785 regs->cr_iip,
6786 current->comm);
6788 task = GET_PMU_OWNER();
6789 ctx = GET_PMU_CTX();
6791 printk("->CPU%d owner [%d] ctx=%p\n", this_cpu, task ? task_pid_nr(task) : -1, ctx);
6793 psr = pfm_get_psr();
6795 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",
6796 this_cpu,
6797 ia64_get_pmc(0),
6798 psr & IA64_PSR_PP ? 1 : 0,
6799 psr & IA64_PSR_UP ? 1 : 0,
6800 dcr & IA64_DCR_PP ? 1 : 0,
6801 info,
6802 ia64_psr(regs)->up,
6803 ia64_psr(regs)->pp);
6805 ia64_psr(regs)->up = 0;
6806 ia64_psr(regs)->pp = 0;
6808 for (i=1; PMC_IS_LAST(i) == 0; i++) {
6809 if (PMC_IS_IMPL(i) == 0) continue;
6810 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]);
6813 for (i=1; PMD_IS_LAST(i) == 0; i++) {
6814 if (PMD_IS_IMPL(i) == 0) continue;
6815 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]);
6818 if (ctx) {
6819 printk("->CPU%d ctx_state=%d vaddr=%p addr=%p fd=%d ctx_task=[%d] saved_psr_up=0x%lx\n",
6820 this_cpu,
6821 ctx->ctx_state,
6822 ctx->ctx_smpl_vaddr,
6823 ctx->ctx_smpl_hdr,
6824 ctx->ctx_msgq_head,
6825 ctx->ctx_msgq_tail,
6826 ctx->ctx_saved_psr_up);
6828 local_irq_restore(flags);
6832 * called from process.c:copy_thread(). task is new child.
6834 void
6835 pfm_inherit(struct task_struct *task, struct pt_regs *regs)
6837 struct thread_struct *thread;
6839 DPRINT(("perfmon: pfm_inherit clearing state for [%d]\n", task_pid_nr(task)));
6841 thread = &task->thread;
6844 * cut links inherited from parent (current)
6846 thread->pfm_context = NULL;
6848 PFM_SET_WORK_PENDING(task, 0);
6851 * the psr bits are already set properly in copy_threads()
6854 #else /* !CONFIG_PERFMON */
6855 asmlinkage long
6856 sys_perfmonctl (int fd, int cmd, void *arg, int count)
6858 return -ENOSYS;
6860 #endif /* CONFIG_PERFMON */