ia64/xen-unstable

view linux-2.6-xen-sparse/arch/ia64/kernel/perfmon.c @ 12631:775fea0a4f16

[IA64] remove undefined debugging line

Signed-off-by: Alex Williamson <alex.williamson@hp.com>
author awilliam@xenbuild.aw
date Wed Nov 29 09:16:46 2006 -0700 (2006-11-29)
parents fe565ac4bf25
children 4fad820a2233
line source
1 /*
2 * This file implements the perfmon-2 subsystem which is used
3 * to program the IA-64 Performance Monitoring Unit (PMU).
4 *
5 * The initial version of perfmon.c was written by
6 * Ganesh Venkitachalam, IBM Corp.
7 *
8 * Then it was modified for perfmon-1.x by Stephane Eranian and
9 * David Mosberger, Hewlett Packard Co.
10 *
11 * Version Perfmon-2.x is a rewrite of perfmon-1.x
12 * by Stephane Eranian, Hewlett Packard Co.
13 *
14 * Copyright (C) 1999-2005 Hewlett Packard Co
15 * Stephane Eranian <eranian@hpl.hp.com>
16 * David Mosberger-Tang <davidm@hpl.hp.com>
17 *
18 * More information about perfmon available at:
19 * http://www.hpl.hp.com/research/linux/perfmon
20 */
22 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/interrupt.h>
27 #include <linux/smp_lock.h>
28 #include <linux/proc_fs.h>
29 #include <linux/seq_file.h>
30 #include <linux/init.h>
31 #include <linux/vmalloc.h>
32 #include <linux/mm.h>
33 #include <linux/sysctl.h>
34 #include <linux/list.h>
35 #include <linux/file.h>
36 #include <linux/poll.h>
37 #include <linux/vfs.h>
38 #include <linux/pagemap.h>
39 #include <linux/mount.h>
40 #include <linux/bitops.h>
41 #include <linux/capability.h>
42 #include <linux/rcupdate.h>
43 #include <linux/completion.h>
45 #include <asm/errno.h>
46 #include <asm/intrinsics.h>
47 #include <asm/page.h>
48 #include <asm/perfmon.h>
49 #include <asm/processor.h>
50 #include <asm/signal.h>
51 #include <asm/system.h>
52 #include <asm/uaccess.h>
53 #include <asm/delay.h>
55 #ifdef CONFIG_PERFMON
56 #ifdef CONFIG_XEN
57 //#include <xen/xenoprof.h>
58 #include <xen/interface/xenoprof.h>
60 static int xenoprof_is_primary = 0;
61 #define init_xenoprof_primary(is_primary) (xenoprof_is_primary = (is_primary))
62 #define is_xenoprof_primary() (xenoprof_is_primary)
63 #define XEN_NOT_SUPPORTED_YET \
64 do { \
65 if (is_running_on_xen()) { \
66 printk("%s is not supported yet under xen.\n", \
67 __func__); \
68 return -ENOSYS; \
69 } \
70 } while (0)
71 #else
72 #define init_xenoprof_primary(is_primary) do { } while (0)
73 #define is_xenoprof_primary() (0)
74 #define XEN_NOT_SUPPORTED_YET do { } while (0)
75 #define HYPERVISOR_perfmon_op(cmd, arg, count) do { } while (0)
76 #endif
78 /*
79 * perfmon context state
80 */
81 #define PFM_CTX_UNLOADED 1 /* context is not loaded onto any task */
82 #define PFM_CTX_LOADED 2 /* context is loaded onto a task */
83 #define PFM_CTX_MASKED 3 /* context is loaded but monitoring is masked due to overflow */
84 #define PFM_CTX_ZOMBIE 4 /* owner of the context is closing it */
86 #define PFM_INVALID_ACTIVATION (~0UL)
88 /*
89 * depth of message queue
90 */
91 #define PFM_MAX_MSGS 32
92 #define PFM_CTXQ_EMPTY(g) ((g)->ctx_msgq_head == (g)->ctx_msgq_tail)
94 /*
95 * type of a PMU register (bitmask).
96 * bitmask structure:
97 * bit0 : register implemented
98 * bit1 : end marker
99 * bit2-3 : reserved
100 * bit4 : pmc has pmc.pm
101 * bit5 : pmc controls a counter (has pmc.oi), pmd is used as counter
102 * bit6-7 : register type
103 * bit8-31: reserved
104 */
105 #define PFM_REG_NOTIMPL 0x0 /* not implemented at all */
106 #define PFM_REG_IMPL 0x1 /* register implemented */
107 #define PFM_REG_END 0x2 /* end marker */
108 #define PFM_REG_MONITOR (0x1<<4|PFM_REG_IMPL) /* a PMC with a pmc.pm field only */
109 #define PFM_REG_COUNTING (0x2<<4|PFM_REG_MONITOR) /* a monitor + pmc.oi+ PMD used as a counter */
110 #define PFM_REG_CONTROL (0x4<<4|PFM_REG_IMPL) /* PMU control register */
111 #define PFM_REG_CONFIG (0x8<<4|PFM_REG_IMPL) /* configuration register */
112 #define PFM_REG_BUFFER (0xc<<4|PFM_REG_IMPL) /* PMD used as buffer */
114 #define PMC_IS_LAST(i) (pmu_conf->pmc_desc[i].type & PFM_REG_END)
115 #define PMD_IS_LAST(i) (pmu_conf->pmd_desc[i].type & PFM_REG_END)
117 #define PMC_OVFL_NOTIFY(ctx, i) ((ctx)->ctx_pmds[i].flags & PFM_REGFL_OVFL_NOTIFY)
119 /* i assumed unsigned */
120 #define PMC_IS_IMPL(i) (i< PMU_MAX_PMCS && (pmu_conf->pmc_desc[i].type & PFM_REG_IMPL))
121 #define PMD_IS_IMPL(i) (i< PMU_MAX_PMDS && (pmu_conf->pmd_desc[i].type & PFM_REG_IMPL))
123 /* XXX: these assume that register i is implemented */
124 #define PMD_IS_COUNTING(i) ((pmu_conf->pmd_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
125 #define PMC_IS_COUNTING(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
126 #define PMC_IS_MONITOR(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_MONITOR) == PFM_REG_MONITOR)
127 #define PMC_IS_CONTROL(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_CONTROL) == PFM_REG_CONTROL)
129 #define PMC_DFL_VAL(i) pmu_conf->pmc_desc[i].default_value
130 #define PMC_RSVD_MASK(i) pmu_conf->pmc_desc[i].reserved_mask
131 #define PMD_PMD_DEP(i) pmu_conf->pmd_desc[i].dep_pmd[0]
132 #define PMC_PMD_DEP(i) pmu_conf->pmc_desc[i].dep_pmd[0]
134 #define PFM_NUM_IBRS IA64_NUM_DBG_REGS
135 #define PFM_NUM_DBRS IA64_NUM_DBG_REGS
137 #define CTX_OVFL_NOBLOCK(c) ((c)->ctx_fl_block == 0)
138 #define CTX_HAS_SMPL(c) ((c)->ctx_fl_is_sampling)
139 #define PFM_CTX_TASK(h) (h)->ctx_task
141 #define PMU_PMC_OI 5 /* position of pmc.oi bit */
143 /* XXX: does not support more than 64 PMDs */
144 #define CTX_USED_PMD(ctx, mask) (ctx)->ctx_used_pmds[0] |= (mask)
145 #define CTX_IS_USED_PMD(ctx, c) (((ctx)->ctx_used_pmds[0] & (1UL << (c))) != 0UL)
147 #define CTX_USED_MONITOR(ctx, mask) (ctx)->ctx_used_monitors[0] |= (mask)
149 #define CTX_USED_IBR(ctx,n) (ctx)->ctx_used_ibrs[(n)>>6] |= 1UL<< ((n) % 64)
150 #define CTX_USED_DBR(ctx,n) (ctx)->ctx_used_dbrs[(n)>>6] |= 1UL<< ((n) % 64)
151 #define CTX_USES_DBREGS(ctx) (((pfm_context_t *)(ctx))->ctx_fl_using_dbreg==1)
152 #define PFM_CODE_RR 0 /* requesting code range restriction */
153 #define PFM_DATA_RR 1 /* requestion data range restriction */
155 #define PFM_CPUINFO_CLEAR(v) pfm_get_cpu_var(pfm_syst_info) &= ~(v)
156 #define PFM_CPUINFO_SET(v) pfm_get_cpu_var(pfm_syst_info) |= (v)
157 #define PFM_CPUINFO_GET() pfm_get_cpu_var(pfm_syst_info)
159 #define RDEP(x) (1UL<<(x))
161 /*
162 * context protection macros
163 * in SMP:
164 * - we need to protect against CPU concurrency (spin_lock)
165 * - we need to protect against PMU overflow interrupts (local_irq_disable)
166 * in UP:
167 * - we need to protect against PMU overflow interrupts (local_irq_disable)
168 *
169 * spin_lock_irqsave()/spin_lock_irqrestore():
170 * in SMP: local_irq_disable + spin_lock
171 * in UP : local_irq_disable
172 *
173 * spin_lock()/spin_lock():
174 * in UP : removed automatically
175 * in SMP: protect against context accesses from other CPU. interrupts
176 * are not masked. This is useful for the PMU interrupt handler
177 * because we know we will not get PMU concurrency in that code.
178 */
179 #define PROTECT_CTX(c, f) \
180 do { \
181 DPRINT(("spinlock_irq_save ctx %p by [%d]\n", c, current->pid)); \
182 spin_lock_irqsave(&(c)->ctx_lock, f); \
183 DPRINT(("spinlocked ctx %p by [%d]\n", c, current->pid)); \
184 } while(0)
186 #define UNPROTECT_CTX(c, f) \
187 do { \
188 DPRINT(("spinlock_irq_restore ctx %p by [%d]\n", c, current->pid)); \
189 spin_unlock_irqrestore(&(c)->ctx_lock, f); \
190 } while(0)
192 #define PROTECT_CTX_NOPRINT(c, f) \
193 do { \
194 spin_lock_irqsave(&(c)->ctx_lock, f); \
195 } while(0)
198 #define UNPROTECT_CTX_NOPRINT(c, f) \
199 do { \
200 spin_unlock_irqrestore(&(c)->ctx_lock, f); \
201 } while(0)
204 #define PROTECT_CTX_NOIRQ(c) \
205 do { \
206 spin_lock(&(c)->ctx_lock); \
207 } while(0)
209 #define UNPROTECT_CTX_NOIRQ(c) \
210 do { \
211 spin_unlock(&(c)->ctx_lock); \
212 } while(0)
215 #ifdef CONFIG_SMP
217 #define GET_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)
218 #define INC_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)++
219 #define SET_ACTIVATION(c) (c)->ctx_last_activation = GET_ACTIVATION()
221 #else /* !CONFIG_SMP */
222 #define SET_ACTIVATION(t) do {} while(0)
223 #define GET_ACTIVATION(t) do {} while(0)
224 #define INC_ACTIVATION(t) do {} while(0)
225 #endif /* CONFIG_SMP */
227 #define SET_PMU_OWNER(t, c) do { pfm_get_cpu_var(pmu_owner) = (t); pfm_get_cpu_var(pmu_ctx) = (c); } while(0)
228 #define GET_PMU_OWNER() pfm_get_cpu_var(pmu_owner)
229 #define GET_PMU_CTX() pfm_get_cpu_var(pmu_ctx)
231 #define LOCK_PFS(g) spin_lock_irqsave(&pfm_sessions.pfs_lock, g)
232 #define UNLOCK_PFS(g) spin_unlock_irqrestore(&pfm_sessions.pfs_lock, g)
234 #define PFM_REG_RETFLAG_SET(flags, val) do { flags &= ~PFM_REG_RETFL_MASK; flags |= (val); } while(0)
236 /*
237 * cmp0 must be the value of pmc0
238 */
239 #define PMC0_HAS_OVFL(cmp0) (cmp0 & ~0x1UL)
241 #define PFMFS_MAGIC 0xa0b4d889
243 /*
244 * debugging
245 */
246 #define PFM_DEBUGGING 1
247 #ifdef PFM_DEBUGGING
248 #define DPRINT(a) \
249 do { \
250 if (unlikely(pfm_sysctl.debug >0)) { printk("%s.%d: CPU%d [%d] ", __FUNCTION__, __LINE__, smp_processor_id(), current->pid); printk a; } \
251 } while (0)
253 #define DPRINT_ovfl(a) \
254 do { \
255 if (unlikely(pfm_sysctl.debug > 0 && pfm_sysctl.debug_ovfl >0)) { printk("%s.%d: CPU%d [%d] ", __FUNCTION__, __LINE__, smp_processor_id(), current->pid); printk a; } \
256 } while (0)
257 #endif
259 /*
260 * 64-bit software counter structure
261 *
262 * the next_reset_type is applied to the next call to pfm_reset_regs()
263 */
264 typedef struct {
265 unsigned long val; /* virtual 64bit counter value */
266 unsigned long lval; /* last reset value */
267 unsigned long long_reset; /* reset value on sampling overflow */
268 unsigned long short_reset; /* reset value on overflow */
269 unsigned long reset_pmds[4]; /* which other pmds to reset when this counter overflows */
270 unsigned long smpl_pmds[4]; /* which pmds are accessed when counter overflow */
271 unsigned long seed; /* seed for random-number generator */
272 unsigned long mask; /* mask for random-number generator */
273 unsigned int flags; /* notify/do not notify */
274 unsigned long eventid; /* overflow event identifier */
275 } pfm_counter_t;
277 /*
278 * context flags
279 */
280 typedef struct {
281 unsigned int block:1; /* when 1, task will blocked on user notifications */
282 unsigned int system:1; /* do system wide monitoring */
283 unsigned int using_dbreg:1; /* using range restrictions (debug registers) */
284 unsigned int is_sampling:1; /* true if using a custom format */
285 unsigned int excl_idle:1; /* exclude idle task in system wide session */
286 unsigned int going_zombie:1; /* context is zombie (MASKED+blocking) */
287 unsigned int trap_reason:2; /* reason for going into pfm_handle_work() */
288 unsigned int no_msg:1; /* no message sent on overflow */
289 unsigned int can_restart:1; /* allowed to issue a PFM_RESTART */
290 unsigned int reserved:22;
291 } pfm_context_flags_t;
293 #define PFM_TRAP_REASON_NONE 0x0 /* default value */
294 #define PFM_TRAP_REASON_BLOCK 0x1 /* we need to block on overflow */
295 #define PFM_TRAP_REASON_RESET 0x2 /* we need to reset PMDs */
298 /*
299 * perfmon context: encapsulates all the state of a monitoring session
300 */
302 typedef struct pfm_context {
303 spinlock_t ctx_lock; /* context protection */
305 pfm_context_flags_t ctx_flags; /* bitmask of flags (block reason incl.) */
306 unsigned int ctx_state; /* state: active/inactive (no bitfield) */
308 struct task_struct *ctx_task; /* task to which context is attached */
310 unsigned long ctx_ovfl_regs[4]; /* which registers overflowed (notification) */
312 struct completion ctx_restart_done; /* use for blocking notification mode */
314 unsigned long ctx_used_pmds[4]; /* bitmask of PMD used */
315 unsigned long ctx_all_pmds[4]; /* bitmask of all accessible PMDs */
316 unsigned long ctx_reload_pmds[4]; /* bitmask of force reload PMD on ctxsw in */
318 unsigned long ctx_all_pmcs[4]; /* bitmask of all accessible PMCs */
319 unsigned long ctx_reload_pmcs[4]; /* bitmask of force reload PMC on ctxsw in */
320 unsigned long ctx_used_monitors[4]; /* bitmask of monitor PMC being used */
322 unsigned long ctx_pmcs[IA64_NUM_PMC_REGS]; /* saved copies of PMC values */
324 unsigned int ctx_used_ibrs[1]; /* bitmask of used IBR (speedup ctxsw in) */
325 unsigned int ctx_used_dbrs[1]; /* bitmask of used DBR (speedup ctxsw in) */
326 unsigned long ctx_dbrs[IA64_NUM_DBG_REGS]; /* DBR values (cache) when not loaded */
327 unsigned long ctx_ibrs[IA64_NUM_DBG_REGS]; /* IBR values (cache) when not loaded */
329 pfm_counter_t ctx_pmds[IA64_NUM_PMD_REGS]; /* software state for PMDS */
331 u64 ctx_saved_psr_up; /* only contains psr.up value */
333 unsigned long ctx_last_activation; /* context last activation number for last_cpu */
334 unsigned int ctx_last_cpu; /* CPU id of current or last CPU used (SMP only) */
335 unsigned int ctx_cpu; /* cpu to which perfmon is applied (system wide) */
337 int ctx_fd; /* file descriptor used my this context */
338 pfm_ovfl_arg_t ctx_ovfl_arg; /* argument to custom buffer format handler */
340 pfm_buffer_fmt_t *ctx_buf_fmt; /* buffer format callbacks */
341 void *ctx_smpl_hdr; /* points to sampling buffer header kernel vaddr */
342 unsigned long ctx_smpl_size; /* size of sampling buffer */
343 void *ctx_smpl_vaddr; /* user level virtual address of smpl buffer */
345 wait_queue_head_t ctx_msgq_wait;
346 pfm_msg_t ctx_msgq[PFM_MAX_MSGS];
347 int ctx_msgq_head;
348 int ctx_msgq_tail;
349 struct fasync_struct *ctx_async_queue;
351 wait_queue_head_t ctx_zombieq; /* termination cleanup wait queue */
352 } pfm_context_t;
354 /*
355 * magic number used to verify that structure is really
356 * a perfmon context
357 */
358 #define PFM_IS_FILE(f) ((f)->f_op == &pfm_file_ops)
360 #define PFM_GET_CTX(t) ((pfm_context_t *)(t)->thread.pfm_context)
362 #ifdef CONFIG_SMP
363 #define SET_LAST_CPU(ctx, v) (ctx)->ctx_last_cpu = (v)
364 #define GET_LAST_CPU(ctx) (ctx)->ctx_last_cpu
365 #else
366 #define SET_LAST_CPU(ctx, v) do {} while(0)
367 #define GET_LAST_CPU(ctx) do {} while(0)
368 #endif
371 #define ctx_fl_block ctx_flags.block
372 #define ctx_fl_system ctx_flags.system
373 #define ctx_fl_using_dbreg ctx_flags.using_dbreg
374 #define ctx_fl_is_sampling ctx_flags.is_sampling
375 #define ctx_fl_excl_idle ctx_flags.excl_idle
376 #define ctx_fl_going_zombie ctx_flags.going_zombie
377 #define ctx_fl_trap_reason ctx_flags.trap_reason
378 #define ctx_fl_no_msg ctx_flags.no_msg
379 #define ctx_fl_can_restart ctx_flags.can_restart
381 #define PFM_SET_WORK_PENDING(t, v) do { (t)->thread.pfm_needs_checking = v; } while(0);
382 #define PFM_GET_WORK_PENDING(t) (t)->thread.pfm_needs_checking
384 /*
385 * global information about all sessions
386 * mostly used to synchronize between system wide and per-process
387 */
388 typedef struct {
389 spinlock_t pfs_lock; /* lock the structure */
391 unsigned int pfs_task_sessions; /* number of per task sessions */
392 unsigned int pfs_sys_sessions; /* number of per system wide sessions */
393 unsigned int pfs_sys_use_dbregs; /* incremented when a system wide session uses debug regs */
394 unsigned int pfs_ptrace_use_dbregs; /* incremented when a process uses debug regs */
395 struct task_struct *pfs_sys_session[NR_CPUS]; /* point to task owning a system-wide session */
396 } pfm_session_t;
398 /*
399 * information about a PMC or PMD.
400 * dep_pmd[]: a bitmask of dependent PMD registers
401 * dep_pmc[]: a bitmask of dependent PMC registers
402 */
403 typedef int (*pfm_reg_check_t)(struct task_struct *task, pfm_context_t *ctx, unsigned int cnum, unsigned long *val, struct pt_regs *regs);
404 typedef struct {
405 unsigned int type;
406 int pm_pos;
407 unsigned long default_value; /* power-on default value */
408 unsigned long reserved_mask; /* bitmask of reserved bits */
409 pfm_reg_check_t read_check;
410 pfm_reg_check_t write_check;
411 unsigned long dep_pmd[4];
412 unsigned long dep_pmc[4];
413 } pfm_reg_desc_t;
415 /* assume cnum is a valid monitor */
416 #define PMC_PM(cnum, val) (((val) >> (pmu_conf->pmc_desc[cnum].pm_pos)) & 0x1)
418 /*
419 * This structure is initialized at boot time and contains
420 * a description of the PMU main characteristics.
421 *
422 * If the probe function is defined, detection is based
423 * on its return value:
424 * - 0 means recognized PMU
425 * - anything else means not supported
426 * When the probe function is not defined, then the pmu_family field
427 * is used and it must match the host CPU family such that:
428 * - cpu->family & config->pmu_family != 0
429 */
430 typedef struct {
431 unsigned long ovfl_val; /* overflow value for counters */
433 pfm_reg_desc_t *pmc_desc; /* detailed PMC register dependencies descriptions */
434 pfm_reg_desc_t *pmd_desc; /* detailed PMD register dependencies descriptions */
436 unsigned int num_pmcs; /* number of PMCS: computed at init time */
437 unsigned int num_pmds; /* number of PMDS: computed at init time */
438 unsigned long impl_pmcs[4]; /* bitmask of implemented PMCS */
439 unsigned long impl_pmds[4]; /* bitmask of implemented PMDS */
441 char *pmu_name; /* PMU family name */
442 unsigned int pmu_family; /* cpuid family pattern used to identify pmu */
443 unsigned int flags; /* pmu specific flags */
444 unsigned int num_ibrs; /* number of IBRS: computed at init time */
445 unsigned int num_dbrs; /* number of DBRS: computed at init time */
446 unsigned int num_counters; /* PMC/PMD counting pairs : computed at init time */
447 int (*probe)(void); /* customized probe routine */
448 unsigned int use_rr_dbregs:1; /* set if debug registers used for range restriction */
449 } pmu_config_t;
450 /*
451 * PMU specific flags
452 */
453 #define PFM_PMU_IRQ_RESEND 1 /* PMU needs explicit IRQ resend */
455 /*
456 * debug register related type definitions
457 */
458 typedef struct {
459 unsigned long ibr_mask:56;
460 unsigned long ibr_plm:4;
461 unsigned long ibr_ig:3;
462 unsigned long ibr_x:1;
463 } ibr_mask_reg_t;
465 typedef struct {
466 unsigned long dbr_mask:56;
467 unsigned long dbr_plm:4;
468 unsigned long dbr_ig:2;
469 unsigned long dbr_w:1;
470 unsigned long dbr_r:1;
471 } dbr_mask_reg_t;
473 typedef union {
474 unsigned long val;
475 ibr_mask_reg_t ibr;
476 dbr_mask_reg_t dbr;
477 } dbreg_t;
480 /*
481 * perfmon command descriptions
482 */
483 typedef struct {
484 int (*cmd_func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
485 char *cmd_name;
486 int cmd_flags;
487 unsigned int cmd_narg;
488 size_t cmd_argsize;
489 int (*cmd_getsize)(void *arg, size_t *sz);
490 } pfm_cmd_desc_t;
492 #define PFM_CMD_FD 0x01 /* command requires a file descriptor */
493 #define PFM_CMD_ARG_READ 0x02 /* command must read argument(s) */
494 #define PFM_CMD_ARG_RW 0x04 /* command must read/write argument(s) */
495 #define PFM_CMD_STOP 0x08 /* command does not work on zombie context */
498 #define PFM_CMD_NAME(cmd) pfm_cmd_tab[(cmd)].cmd_name
499 #define PFM_CMD_READ_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_READ)
500 #define PFM_CMD_RW_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_RW)
501 #define PFM_CMD_USE_FD(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_FD)
502 #define PFM_CMD_STOPPED(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_STOP)
504 #define PFM_CMD_ARG_MANY -1 /* cannot be zero */
506 typedef struct {
507 unsigned long pfm_spurious_ovfl_intr_count; /* keep track of spurious ovfl interrupts */
508 unsigned long pfm_replay_ovfl_intr_count; /* keep track of replayed ovfl interrupts */
509 unsigned long pfm_ovfl_intr_count; /* keep track of ovfl interrupts */
510 unsigned long pfm_ovfl_intr_cycles; /* cycles spent processing ovfl interrupts */
511 unsigned long pfm_ovfl_intr_cycles_min; /* min cycles spent processing ovfl interrupts */
512 unsigned long pfm_ovfl_intr_cycles_max; /* max cycles spent processing ovfl interrupts */
513 unsigned long pfm_smpl_handler_calls;
514 unsigned long pfm_smpl_handler_cycles;
515 char pad[SMP_CACHE_BYTES] ____cacheline_aligned;
516 } pfm_stats_t;
518 /*
519 * perfmon internal variables
520 */
521 static pfm_stats_t pfm_stats[NR_CPUS];
522 static pfm_session_t pfm_sessions; /* global sessions information */
524 static DEFINE_SPINLOCK(pfm_alt_install_check);
525 static pfm_intr_handler_desc_t *pfm_alt_intr_handler;
527 static struct proc_dir_entry *perfmon_dir;
528 static pfm_uuid_t pfm_null_uuid = {0,};
530 static spinlock_t pfm_buffer_fmt_lock;
531 static LIST_HEAD(pfm_buffer_fmt_list);
533 static pmu_config_t *pmu_conf;
535 /* sysctl() controls */
536 pfm_sysctl_t pfm_sysctl;
537 EXPORT_SYMBOL(pfm_sysctl);
539 static ctl_table pfm_ctl_table[]={
540 {1, "debug", &pfm_sysctl.debug, sizeof(int), 0666, NULL, &proc_dointvec, NULL,},
541 {2, "debug_ovfl", &pfm_sysctl.debug_ovfl, sizeof(int), 0666, NULL, &proc_dointvec, NULL,},
542 {3, "fastctxsw", &pfm_sysctl.fastctxsw, sizeof(int), 0600, NULL, &proc_dointvec, NULL,},
543 {4, "expert_mode", &pfm_sysctl.expert_mode, sizeof(int), 0600, NULL, &proc_dointvec, NULL,},
544 { 0, },
545 };
546 static ctl_table pfm_sysctl_dir[] = {
547 {1, "perfmon", NULL, 0, 0755, pfm_ctl_table, },
548 {0,},
549 };
550 static ctl_table pfm_sysctl_root[] = {
551 {1, "kernel", NULL, 0, 0755, pfm_sysctl_dir, },
552 {0,},
553 };
554 static struct ctl_table_header *pfm_sysctl_header;
556 static int pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
557 static int pfm_flush(struct file *filp);
559 #define pfm_get_cpu_var(v) __ia64_per_cpu_var(v)
560 #define pfm_get_cpu_data(a,b) per_cpu(a, b)
562 static inline void
563 pfm_put_task(struct task_struct *task)
564 {
565 if (task != current) put_task_struct(task);
566 }
568 static inline void
569 pfm_set_task_notify(struct task_struct *task)
570 {
571 struct thread_info *info;
573 info = (struct thread_info *) ((char *) task + IA64_TASK_SIZE);
574 set_bit(TIF_NOTIFY_RESUME, &info->flags);
575 }
577 static inline void
578 pfm_clear_task_notify(void)
579 {
580 clear_thread_flag(TIF_NOTIFY_RESUME);
581 }
583 static inline void
584 pfm_reserve_page(unsigned long a)
585 {
586 SetPageReserved(vmalloc_to_page((void *)a));
587 }
588 static inline void
589 pfm_unreserve_page(unsigned long a)
590 {
591 ClearPageReserved(vmalloc_to_page((void*)a));
592 }
594 static inline unsigned long
595 pfm_protect_ctx_ctxsw(pfm_context_t *x)
596 {
597 spin_lock(&(x)->ctx_lock);
598 return 0UL;
599 }
601 static inline void
602 pfm_unprotect_ctx_ctxsw(pfm_context_t *x, unsigned long f)
603 {
604 spin_unlock(&(x)->ctx_lock);
605 }
607 static inline unsigned int
608 pfm_do_munmap(struct mm_struct *mm, unsigned long addr, size_t len, int acct)
609 {
610 return do_munmap(mm, addr, len);
611 }
613 static inline unsigned long
614 pfm_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags, unsigned long exec)
615 {
616 return get_unmapped_area(file, addr, len, pgoff, flags);
617 }
620 static struct super_block *
621 pfmfs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data)
622 {
623 return get_sb_pseudo(fs_type, "pfm:", NULL, PFMFS_MAGIC);
624 }
626 static struct file_system_type pfm_fs_type = {
627 .name = "pfmfs",
628 .get_sb = pfmfs_get_sb,
629 .kill_sb = kill_anon_super,
630 };
632 DEFINE_PER_CPU(unsigned long, pfm_syst_info);
633 DEFINE_PER_CPU(struct task_struct *, pmu_owner);
634 DEFINE_PER_CPU(pfm_context_t *, pmu_ctx);
635 DEFINE_PER_CPU(unsigned long, pmu_activation_number);
636 EXPORT_PER_CPU_SYMBOL_GPL(pfm_syst_info);
639 /* forward declaration */
640 static struct file_operations pfm_file_ops;
642 /*
643 * forward declarations
644 */
645 #ifndef CONFIG_SMP
646 static void pfm_lazy_save_regs (struct task_struct *ta);
647 #endif
649 void dump_pmu_state(const char *);
650 static int pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
652 #include "perfmon_itanium.h"
653 #include "perfmon_mckinley.h"
654 #include "perfmon_montecito.h"
655 #include "perfmon_generic.h"
657 static pmu_config_t *pmu_confs[]={
658 &pmu_conf_mont,
659 &pmu_conf_mck,
660 &pmu_conf_ita,
661 &pmu_conf_gen, /* must be last */
662 NULL
663 };
666 static int pfm_end_notify_user(pfm_context_t *ctx);
668 static inline void
669 pfm_clear_psr_pp(void)
670 {
671 ia64_rsm(IA64_PSR_PP);
672 ia64_srlz_i();
673 }
675 static inline void
676 pfm_set_psr_pp(void)
677 {
678 ia64_ssm(IA64_PSR_PP);
679 ia64_srlz_i();
680 }
682 static inline void
683 pfm_clear_psr_up(void)
684 {
685 ia64_rsm(IA64_PSR_UP);
686 ia64_srlz_i();
687 }
689 static inline void
690 pfm_set_psr_up(void)
691 {
692 ia64_ssm(IA64_PSR_UP);
693 ia64_srlz_i();
694 }
696 static inline unsigned long
697 pfm_get_psr(void)
698 {
699 unsigned long tmp;
700 tmp = ia64_getreg(_IA64_REG_PSR);
701 ia64_srlz_i();
702 return tmp;
703 }
705 static inline void
706 pfm_set_psr_l(unsigned long val)
707 {
708 ia64_setreg(_IA64_REG_PSR_L, val);
709 ia64_srlz_i();
710 }
712 static inline void
713 pfm_freeze_pmu(void)
714 {
715 ia64_set_pmc(0,1UL);
716 ia64_srlz_d();
717 }
719 static inline void
720 pfm_unfreeze_pmu(void)
721 {
722 ia64_set_pmc(0,0UL);
723 ia64_srlz_d();
724 }
726 static inline void
727 pfm_restore_ibrs(unsigned long *ibrs, unsigned int nibrs)
728 {
729 int i;
731 for (i=0; i < nibrs; i++) {
732 ia64_set_ibr(i, ibrs[i]);
733 ia64_dv_serialize_instruction();
734 }
735 ia64_srlz_i();
736 }
738 static inline void
739 pfm_restore_dbrs(unsigned long *dbrs, unsigned int ndbrs)
740 {
741 int i;
743 for (i=0; i < ndbrs; i++) {
744 ia64_set_dbr(i, dbrs[i]);
745 ia64_dv_serialize_data();
746 }
747 ia64_srlz_d();
748 }
750 /*
751 * PMD[i] must be a counter. no check is made
752 */
753 static inline unsigned long
754 pfm_read_soft_counter(pfm_context_t *ctx, int i)
755 {
756 return ctx->ctx_pmds[i].val + (ia64_get_pmd(i) & pmu_conf->ovfl_val);
757 }
759 /*
760 * PMD[i] must be a counter. no check is made
761 */
762 static inline void
763 pfm_write_soft_counter(pfm_context_t *ctx, int i, unsigned long val)
764 {
765 unsigned long ovfl_val = pmu_conf->ovfl_val;
767 ctx->ctx_pmds[i].val = val & ~ovfl_val;
768 /*
769 * writing to unimplemented part is ignore, so we do not need to
770 * mask off top part
771 */
772 ia64_set_pmd(i, val & ovfl_val);
773 }
775 static pfm_msg_t *
776 pfm_get_new_msg(pfm_context_t *ctx)
777 {
778 int idx, next;
780 next = (ctx->ctx_msgq_tail+1) % PFM_MAX_MSGS;
782 DPRINT(("ctx_fd=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
783 if (next == ctx->ctx_msgq_head) return NULL;
785 idx = ctx->ctx_msgq_tail;
786 ctx->ctx_msgq_tail = next;
788 DPRINT(("ctx=%p head=%d tail=%d msg=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail, idx));
790 return ctx->ctx_msgq+idx;
791 }
793 static pfm_msg_t *
794 pfm_get_next_msg(pfm_context_t *ctx)
795 {
796 pfm_msg_t *msg;
798 DPRINT(("ctx=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
800 if (PFM_CTXQ_EMPTY(ctx)) return NULL;
802 /*
803 * get oldest message
804 */
805 msg = ctx->ctx_msgq+ctx->ctx_msgq_head;
807 /*
808 * and move forward
809 */
810 ctx->ctx_msgq_head = (ctx->ctx_msgq_head+1) % PFM_MAX_MSGS;
812 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));
814 return msg;
815 }
817 static void
818 pfm_reset_msgq(pfm_context_t *ctx)
819 {
820 ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
821 DPRINT(("ctx=%p msgq reset\n", ctx));
822 }
824 static void *
825 pfm_rvmalloc(unsigned long size)
826 {
827 void *mem;
828 unsigned long addr;
830 size = PAGE_ALIGN(size);
831 mem = vmalloc(size);
832 if (mem) {
833 //printk("perfmon: CPU%d pfm_rvmalloc(%ld)=%p\n", smp_processor_id(), size, mem);
834 memset(mem, 0, size);
835 addr = (unsigned long)mem;
836 while (size > 0) {
837 pfm_reserve_page(addr);
838 addr+=PAGE_SIZE;
839 size-=PAGE_SIZE;
840 }
841 }
842 return mem;
843 }
845 static void
846 pfm_rvfree(void *mem, unsigned long size)
847 {
848 unsigned long addr;
850 if (mem) {
851 DPRINT(("freeing physical buffer @%p size=%lu\n", mem, size));
852 addr = (unsigned long) mem;
853 while ((long) size > 0) {
854 pfm_unreserve_page(addr);
855 addr+=PAGE_SIZE;
856 size-=PAGE_SIZE;
857 }
858 vfree(mem);
859 }
860 return;
861 }
863 static pfm_context_t *
864 pfm_context_alloc(void)
865 {
866 pfm_context_t *ctx;
868 /*
869 * allocate context descriptor
870 * must be able to free with interrupts disabled
871 */
872 ctx = kmalloc(sizeof(pfm_context_t), GFP_KERNEL);
873 if (ctx) {
874 memset(ctx, 0, sizeof(pfm_context_t));
875 DPRINT(("alloc ctx @%p\n", ctx));
876 }
877 return ctx;
878 }
880 static void
881 pfm_context_free(pfm_context_t *ctx)
882 {
883 if (ctx) {
884 DPRINT(("free ctx @%p\n", ctx));
885 kfree(ctx);
886 }
887 }
889 static void
890 pfm_mask_monitoring(struct task_struct *task)
891 {
892 pfm_context_t *ctx = PFM_GET_CTX(task);
893 struct thread_struct *th = &task->thread;
894 unsigned long mask, val, ovfl_mask;
895 int i;
897 DPRINT_ovfl(("masking monitoring for [%d]\n", task->pid));
899 ovfl_mask = pmu_conf->ovfl_val;
900 /*
901 * monitoring can only be masked as a result of a valid
902 * counter overflow. In UP, it means that the PMU still
903 * has an owner. Note that the owner can be different
904 * from the current task. However the PMU state belongs
905 * to the owner.
906 * In SMP, a valid overflow only happens when task is
907 * current. Therefore if we come here, we know that
908 * the PMU state belongs to the current task, therefore
909 * we can access the live registers.
910 *
911 * So in both cases, the live register contains the owner's
912 * state. We can ONLY touch the PMU registers and NOT the PSR.
913 *
914 * As a consequence to this call, the thread->pmds[] array
915 * contains stale information which must be ignored
916 * when context is reloaded AND monitoring is active (see
917 * pfm_restart).
918 */
919 mask = ctx->ctx_used_pmds[0];
920 for (i = 0; mask; i++, mask>>=1) {
921 /* skip non used pmds */
922 if ((mask & 0x1) == 0) continue;
923 val = ia64_get_pmd(i);
925 if (PMD_IS_COUNTING(i)) {
926 /*
927 * we rebuild the full 64 bit value of the counter
928 */
929 ctx->ctx_pmds[i].val += (val & ovfl_mask);
930 } else {
931 ctx->ctx_pmds[i].val = val;
932 }
933 DPRINT_ovfl(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
934 i,
935 ctx->ctx_pmds[i].val,
936 val & ovfl_mask));
937 }
938 /*
939 * mask monitoring by setting the privilege level to 0
940 * we cannot use psr.pp/psr.up for this, it is controlled by
941 * the user
942 *
943 * if task is current, modify actual registers, otherwise modify
944 * thread save state, i.e., what will be restored in pfm_load_regs()
945 */
946 mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
947 for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
948 if ((mask & 0x1) == 0UL) continue;
949 ia64_set_pmc(i, th->pmcs[i] & ~0xfUL);
950 th->pmcs[i] &= ~0xfUL;
951 DPRINT_ovfl(("pmc[%d]=0x%lx\n", i, th->pmcs[i]));
952 }
953 /*
954 * make all of this visible
955 */
956 ia64_srlz_d();
957 }
959 /*
960 * must always be done with task == current
961 *
962 * context must be in MASKED state when calling
963 */
964 static void
965 pfm_restore_monitoring(struct task_struct *task)
966 {
967 pfm_context_t *ctx = PFM_GET_CTX(task);
968 struct thread_struct *th = &task->thread;
969 unsigned long mask, ovfl_mask;
970 unsigned long psr, val;
971 int i, is_system;
973 is_system = ctx->ctx_fl_system;
974 ovfl_mask = pmu_conf->ovfl_val;
976 if (task != current) {
977 printk(KERN_ERR "perfmon.%d: invalid task[%d] current[%d]\n", __LINE__, task->pid, current->pid);
978 return;
979 }
980 if (ctx->ctx_state != PFM_CTX_MASKED) {
981 printk(KERN_ERR "perfmon.%d: task[%d] current[%d] invalid state=%d\n", __LINE__,
982 task->pid, current->pid, ctx->ctx_state);
983 return;
984 }
985 psr = pfm_get_psr();
986 /*
987 * monitoring is masked via the PMC.
988 * As we restore their value, we do not want each counter to
989 * restart right away. We stop monitoring using the PSR,
990 * restore the PMC (and PMD) and then re-establish the psr
991 * as it was. Note that there can be no pending overflow at
992 * this point, because monitoring was MASKED.
993 *
994 * system-wide session are pinned and self-monitoring
995 */
996 if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
997 /* disable dcr pp */
998 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
999 pfm_clear_psr_pp();
1000 } else {
1001 pfm_clear_psr_up();
1003 /*
1004 * first, we restore the PMD
1005 */
1006 mask = ctx->ctx_used_pmds[0];
1007 for (i = 0; mask; i++, mask>>=1) {
1008 /* skip non used pmds */
1009 if ((mask & 0x1) == 0) continue;
1011 if (PMD_IS_COUNTING(i)) {
1012 /*
1013 * we split the 64bit value according to
1014 * counter width
1015 */
1016 val = ctx->ctx_pmds[i].val & ovfl_mask;
1017 ctx->ctx_pmds[i].val &= ~ovfl_mask;
1018 } else {
1019 val = ctx->ctx_pmds[i].val;
1021 ia64_set_pmd(i, val);
1023 DPRINT(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
1024 i,
1025 ctx->ctx_pmds[i].val,
1026 val));
1028 /*
1029 * restore the PMCs
1030 */
1031 mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
1032 for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
1033 if ((mask & 0x1) == 0UL) continue;
1034 th->pmcs[i] = ctx->ctx_pmcs[i];
1035 ia64_set_pmc(i, th->pmcs[i]);
1036 DPRINT(("[%d] pmc[%d]=0x%lx\n", task->pid, i, th->pmcs[i]));
1038 ia64_srlz_d();
1040 /*
1041 * must restore DBR/IBR because could be modified while masked
1042 * XXX: need to optimize
1043 */
1044 if (ctx->ctx_fl_using_dbreg) {
1045 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
1046 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
1049 /*
1050 * now restore PSR
1051 */
1052 if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
1053 /* enable dcr pp */
1054 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
1055 ia64_srlz_i();
1057 pfm_set_psr_l(psr);
1060 static inline void
1061 pfm_save_pmds(unsigned long *pmds, unsigned long mask)
1063 int i;
1065 ia64_srlz_d();
1067 for (i=0; mask; i++, mask>>=1) {
1068 if (mask & 0x1) pmds[i] = ia64_get_pmd(i);
1072 /*
1073 * reload from thread state (used for ctxw only)
1074 */
1075 static inline void
1076 pfm_restore_pmds(unsigned long *pmds, unsigned long mask)
1078 int i;
1079 unsigned long val, ovfl_val = pmu_conf->ovfl_val;
1081 for (i=0; mask; i++, mask>>=1) {
1082 if ((mask & 0x1) == 0) continue;
1083 val = PMD_IS_COUNTING(i) ? pmds[i] & ovfl_val : pmds[i];
1084 ia64_set_pmd(i, val);
1086 ia64_srlz_d();
1089 /*
1090 * propagate PMD from context to thread-state
1091 */
1092 static inline void
1093 pfm_copy_pmds(struct task_struct *task, pfm_context_t *ctx)
1095 struct thread_struct *thread = &task->thread;
1096 unsigned long ovfl_val = pmu_conf->ovfl_val;
1097 unsigned long mask = ctx->ctx_all_pmds[0];
1098 unsigned long val;
1099 int i;
1101 DPRINT(("mask=0x%lx\n", mask));
1103 for (i=0; mask; i++, mask>>=1) {
1105 val = ctx->ctx_pmds[i].val;
1107 /*
1108 * We break up the 64 bit value into 2 pieces
1109 * the lower bits go to the machine state in the
1110 * thread (will be reloaded on ctxsw in).
1111 * The upper part stays in the soft-counter.
1112 */
1113 if (PMD_IS_COUNTING(i)) {
1114 ctx->ctx_pmds[i].val = val & ~ovfl_val;
1115 val &= ovfl_val;
1117 thread->pmds[i] = val;
1119 DPRINT(("pmd[%d]=0x%lx soft_val=0x%lx\n",
1120 i,
1121 thread->pmds[i],
1122 ctx->ctx_pmds[i].val));
1126 /*
1127 * propagate PMC from context to thread-state
1128 */
1129 static inline void
1130 pfm_copy_pmcs(struct task_struct *task, pfm_context_t *ctx)
1132 struct thread_struct *thread = &task->thread;
1133 unsigned long mask = ctx->ctx_all_pmcs[0];
1134 int i;
1136 DPRINT(("mask=0x%lx\n", mask));
1138 for (i=0; mask; i++, mask>>=1) {
1139 /* masking 0 with ovfl_val yields 0 */
1140 thread->pmcs[i] = ctx->ctx_pmcs[i];
1141 DPRINT(("pmc[%d]=0x%lx\n", i, thread->pmcs[i]));
1147 static inline void
1148 pfm_restore_pmcs(unsigned long *pmcs, unsigned long mask)
1150 int i;
1152 for (i=0; mask; i++, mask>>=1) {
1153 if ((mask & 0x1) == 0) continue;
1154 ia64_set_pmc(i, pmcs[i]);
1156 ia64_srlz_d();
1159 static inline int
1160 pfm_uuid_cmp(pfm_uuid_t a, pfm_uuid_t b)
1162 return memcmp(a, b, sizeof(pfm_uuid_t));
1165 static inline int
1166 pfm_buf_fmt_exit(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, struct pt_regs *regs)
1168 int ret = 0;
1169 if (fmt->fmt_exit) ret = (*fmt->fmt_exit)(task, buf, regs);
1170 return ret;
1173 static inline int
1174 pfm_buf_fmt_getsize(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags, int cpu, void *arg, unsigned long *size)
1176 int ret = 0;
1177 if (fmt->fmt_getsize) ret = (*fmt->fmt_getsize)(task, flags, cpu, arg, size);
1178 return ret;
1182 static inline int
1183 pfm_buf_fmt_validate(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags,
1184 int cpu, void *arg)
1186 int ret = 0;
1187 if (fmt->fmt_validate) ret = (*fmt->fmt_validate)(task, flags, cpu, arg);
1188 return ret;
1191 static inline int
1192 pfm_buf_fmt_init(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, unsigned int flags,
1193 int cpu, void *arg)
1195 int ret = 0;
1196 if (fmt->fmt_init) ret = (*fmt->fmt_init)(task, buf, flags, cpu, arg);
1197 return ret;
1200 static inline int
1201 pfm_buf_fmt_restart(pfm_buffer_fmt_t *fmt, struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
1203 int ret = 0;
1204 if (fmt->fmt_restart) ret = (*fmt->fmt_restart)(task, ctrl, buf, regs);
1205 return ret;
1208 static inline int
1209 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)
1211 int ret = 0;
1212 if (fmt->fmt_restart_active) ret = (*fmt->fmt_restart_active)(task, ctrl, buf, regs);
1213 return ret;
1216 static pfm_buffer_fmt_t *
1217 __pfm_find_buffer_fmt(pfm_uuid_t uuid)
1219 struct list_head * pos;
1220 pfm_buffer_fmt_t * entry;
1222 list_for_each(pos, &pfm_buffer_fmt_list) {
1223 entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
1224 if (pfm_uuid_cmp(uuid, entry->fmt_uuid) == 0)
1225 return entry;
1227 return NULL;
1230 /*
1231 * find a buffer format based on its uuid
1232 */
1233 static pfm_buffer_fmt_t *
1234 pfm_find_buffer_fmt(pfm_uuid_t uuid)
1236 pfm_buffer_fmt_t * fmt;
1237 spin_lock(&pfm_buffer_fmt_lock);
1238 fmt = __pfm_find_buffer_fmt(uuid);
1239 spin_unlock(&pfm_buffer_fmt_lock);
1240 return fmt;
1243 int
1244 pfm_register_buffer_fmt(pfm_buffer_fmt_t *fmt)
1246 int ret = 0;
1248 /* some sanity checks */
1249 if (fmt == NULL || fmt->fmt_name == NULL) return -EINVAL;
1251 /* we need at least a handler */
1252 if (fmt->fmt_handler == NULL) return -EINVAL;
1254 /*
1255 * XXX: need check validity of fmt_arg_size
1256 */
1258 spin_lock(&pfm_buffer_fmt_lock);
1260 if (__pfm_find_buffer_fmt(fmt->fmt_uuid)) {
1261 printk(KERN_ERR "perfmon: duplicate sampling format: %s\n", fmt->fmt_name);
1262 ret = -EBUSY;
1263 goto out;
1265 list_add(&fmt->fmt_list, &pfm_buffer_fmt_list);
1266 printk(KERN_INFO "perfmon: added sampling format %s\n", fmt->fmt_name);
1268 out:
1269 spin_unlock(&pfm_buffer_fmt_lock);
1270 return ret;
1272 EXPORT_SYMBOL(pfm_register_buffer_fmt);
1274 int
1275 pfm_unregister_buffer_fmt(pfm_uuid_t uuid)
1277 pfm_buffer_fmt_t *fmt;
1278 int ret = 0;
1280 spin_lock(&pfm_buffer_fmt_lock);
1282 fmt = __pfm_find_buffer_fmt(uuid);
1283 if (!fmt) {
1284 printk(KERN_ERR "perfmon: cannot unregister format, not found\n");
1285 ret = -EINVAL;
1286 goto out;
1288 list_del_init(&fmt->fmt_list);
1289 printk(KERN_INFO "perfmon: removed sampling format: %s\n", fmt->fmt_name);
1291 out:
1292 spin_unlock(&pfm_buffer_fmt_lock);
1293 return ret;
1296 EXPORT_SYMBOL(pfm_unregister_buffer_fmt);
1298 extern void update_pal_halt_status(int);
1300 static int
1301 pfm_reserve_session(struct task_struct *task, int is_syswide, unsigned int cpu)
1303 unsigned long flags;
1304 /*
1305 * validy checks on cpu_mask have been done upstream
1306 */
1307 LOCK_PFS(flags);
1309 DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1310 pfm_sessions.pfs_sys_sessions,
1311 pfm_sessions.pfs_task_sessions,
1312 pfm_sessions.pfs_sys_use_dbregs,
1313 is_syswide,
1314 cpu));
1316 if (is_syswide) {
1317 /*
1318 * cannot mix system wide and per-task sessions
1319 */
1320 if (pfm_sessions.pfs_task_sessions > 0UL) {
1321 DPRINT(("system wide not possible, %u conflicting task_sessions\n",
1322 pfm_sessions.pfs_task_sessions));
1323 goto abort;
1326 if (pfm_sessions.pfs_sys_session[cpu]) goto error_conflict;
1328 DPRINT(("reserving system wide session on CPU%u currently on CPU%u\n", cpu, smp_processor_id()));
1330 pfm_sessions.pfs_sys_session[cpu] = task;
1332 pfm_sessions.pfs_sys_sessions++ ;
1334 } else {
1335 if (pfm_sessions.pfs_sys_sessions) goto abort;
1336 pfm_sessions.pfs_task_sessions++;
1339 DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1340 pfm_sessions.pfs_sys_sessions,
1341 pfm_sessions.pfs_task_sessions,
1342 pfm_sessions.pfs_sys_use_dbregs,
1343 is_syswide,
1344 cpu));
1346 /*
1347 * disable default_idle() to go to PAL_HALT
1348 */
1349 update_pal_halt_status(0);
1351 UNLOCK_PFS(flags);
1353 return 0;
1355 error_conflict:
1356 DPRINT(("system wide not possible, conflicting session [%d] on CPU%d\n",
1357 pfm_sessions.pfs_sys_session[cpu]->pid,
1358 cpu));
1359 abort:
1360 UNLOCK_PFS(flags);
1362 return -EBUSY;
1366 static int
1367 pfm_unreserve_session(pfm_context_t *ctx, int is_syswide, unsigned int cpu)
1369 unsigned long flags;
1370 /*
1371 * validy checks on cpu_mask have been done upstream
1372 */
1373 LOCK_PFS(flags);
1375 DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1376 pfm_sessions.pfs_sys_sessions,
1377 pfm_sessions.pfs_task_sessions,
1378 pfm_sessions.pfs_sys_use_dbregs,
1379 is_syswide,
1380 cpu));
1383 if (is_syswide) {
1384 pfm_sessions.pfs_sys_session[cpu] = NULL;
1385 /*
1386 * would not work with perfmon+more than one bit in cpu_mask
1387 */
1388 if (ctx && ctx->ctx_fl_using_dbreg) {
1389 if (pfm_sessions.pfs_sys_use_dbregs == 0) {
1390 printk(KERN_ERR "perfmon: invalid release for ctx %p sys_use_dbregs=0\n", ctx);
1391 } else {
1392 pfm_sessions.pfs_sys_use_dbregs--;
1395 pfm_sessions.pfs_sys_sessions--;
1396 } else {
1397 pfm_sessions.pfs_task_sessions--;
1399 DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1400 pfm_sessions.pfs_sys_sessions,
1401 pfm_sessions.pfs_task_sessions,
1402 pfm_sessions.pfs_sys_use_dbregs,
1403 is_syswide,
1404 cpu));
1406 /*
1407 * if possible, enable default_idle() to go into PAL_HALT
1408 */
1409 if (pfm_sessions.pfs_task_sessions == 0 && pfm_sessions.pfs_sys_sessions == 0)
1410 update_pal_halt_status(1);
1412 UNLOCK_PFS(flags);
1414 return 0;
1417 /*
1418 * removes virtual mapping of the sampling buffer.
1419 * IMPORTANT: cannot be called with interrupts disable, e.g. inside
1420 * a PROTECT_CTX() section.
1421 */
1422 static int
1423 pfm_remove_smpl_mapping(struct task_struct *task, void *vaddr, unsigned long size)
1425 int r;
1427 /* sanity checks */
1428 if (task->mm == NULL || size == 0UL || vaddr == NULL) {
1429 printk(KERN_ERR "perfmon: pfm_remove_smpl_mapping [%d] invalid context mm=%p\n", task->pid, task->mm);
1430 return -EINVAL;
1433 DPRINT(("smpl_vaddr=%p size=%lu\n", vaddr, size));
1435 /*
1436 * does the actual unmapping
1437 */
1438 down_write(&task->mm->mmap_sem);
1440 DPRINT(("down_write done smpl_vaddr=%p size=%lu\n", vaddr, size));
1442 r = pfm_do_munmap(task->mm, (unsigned long)vaddr, size, 0);
1444 up_write(&task->mm->mmap_sem);
1445 if (r !=0) {
1446 printk(KERN_ERR "perfmon: [%d] unable to unmap sampling buffer @%p size=%lu\n", task->pid, vaddr, size);
1449 DPRINT(("do_unmap(%p, %lu)=%d\n", vaddr, size, r));
1451 return 0;
1454 /*
1455 * free actual physical storage used by sampling buffer
1456 */
1457 #if 0
1458 static int
1459 pfm_free_smpl_buffer(pfm_context_t *ctx)
1461 pfm_buffer_fmt_t *fmt;
1463 if (ctx->ctx_smpl_hdr == NULL) goto invalid_free;
1465 /*
1466 * we won't use the buffer format anymore
1467 */
1468 fmt = ctx->ctx_buf_fmt;
1470 DPRINT(("sampling buffer @%p size %lu vaddr=%p\n",
1471 ctx->ctx_smpl_hdr,
1472 ctx->ctx_smpl_size,
1473 ctx->ctx_smpl_vaddr));
1475 pfm_buf_fmt_exit(fmt, current, NULL, NULL);
1477 /*
1478 * free the buffer
1479 */
1480 pfm_rvfree(ctx->ctx_smpl_hdr, ctx->ctx_smpl_size);
1482 ctx->ctx_smpl_hdr = NULL;
1483 ctx->ctx_smpl_size = 0UL;
1485 return 0;
1487 invalid_free:
1488 printk(KERN_ERR "perfmon: pfm_free_smpl_buffer [%d] no buffer\n", current->pid);
1489 return -EINVAL;
1491 #endif
1493 static inline void
1494 pfm_exit_smpl_buffer(pfm_buffer_fmt_t *fmt)
1496 if (fmt == NULL) return;
1498 pfm_buf_fmt_exit(fmt, current, NULL, NULL);
1502 /*
1503 * pfmfs should _never_ be mounted by userland - too much of security hassle,
1504 * no real gain from having the whole whorehouse mounted. So we don't need
1505 * any operations on the root directory. However, we need a non-trivial
1506 * d_name - pfm: will go nicely and kill the special-casing in procfs.
1507 */
1508 static struct vfsmount *pfmfs_mnt;
1510 static int __init
1511 init_pfm_fs(void)
1513 int err = register_filesystem(&pfm_fs_type);
1514 if (!err) {
1515 pfmfs_mnt = kern_mount(&pfm_fs_type);
1516 err = PTR_ERR(pfmfs_mnt);
1517 if (IS_ERR(pfmfs_mnt))
1518 unregister_filesystem(&pfm_fs_type);
1519 else
1520 err = 0;
1522 return err;
1525 static void __exit
1526 exit_pfm_fs(void)
1528 unregister_filesystem(&pfm_fs_type);
1529 mntput(pfmfs_mnt);
1532 static ssize_t
1533 pfm_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
1535 pfm_context_t *ctx;
1536 pfm_msg_t *msg;
1537 ssize_t ret;
1538 unsigned long flags;
1539 DECLARE_WAITQUEUE(wait, current);
1540 XEN_NOT_SUPPORTED_YET;
1541 if (PFM_IS_FILE(filp) == 0) {
1542 printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", current->pid);
1543 return -EINVAL;
1546 ctx = (pfm_context_t *)filp->private_data;
1547 if (ctx == NULL) {
1548 printk(KERN_ERR "perfmon: pfm_read: NULL ctx [%d]\n", current->pid);
1549 return -EINVAL;
1552 /*
1553 * check even when there is no message
1554 */
1555 if (size < sizeof(pfm_msg_t)) {
1556 DPRINT(("message is too small ctx=%p (>=%ld)\n", ctx, sizeof(pfm_msg_t)));
1557 return -EINVAL;
1560 PROTECT_CTX(ctx, flags);
1562 /*
1563 * put ourselves on the wait queue
1564 */
1565 add_wait_queue(&ctx->ctx_msgq_wait, &wait);
1568 for(;;) {
1569 /*
1570 * check wait queue
1571 */
1573 set_current_state(TASK_INTERRUPTIBLE);
1575 DPRINT(("head=%d tail=%d\n", ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
1577 ret = 0;
1578 if(PFM_CTXQ_EMPTY(ctx) == 0) break;
1580 UNPROTECT_CTX(ctx, flags);
1582 /*
1583 * check non-blocking read
1584 */
1585 ret = -EAGAIN;
1586 if(filp->f_flags & O_NONBLOCK) break;
1588 /*
1589 * check pending signals
1590 */
1591 if(signal_pending(current)) {
1592 ret = -EINTR;
1593 break;
1595 /*
1596 * no message, so wait
1597 */
1598 schedule();
1600 PROTECT_CTX(ctx, flags);
1602 DPRINT(("[%d] back to running ret=%ld\n", current->pid, ret));
1603 set_current_state(TASK_RUNNING);
1604 remove_wait_queue(&ctx->ctx_msgq_wait, &wait);
1606 if (ret < 0) goto abort;
1608 ret = -EINVAL;
1609 msg = pfm_get_next_msg(ctx);
1610 if (msg == NULL) {
1611 printk(KERN_ERR "perfmon: pfm_read no msg for ctx=%p [%d]\n", ctx, current->pid);
1612 goto abort_locked;
1615 DPRINT(("fd=%d type=%d\n", msg->pfm_gen_msg.msg_ctx_fd, msg->pfm_gen_msg.msg_type));
1617 ret = -EFAULT;
1618 if(copy_to_user(buf, msg, sizeof(pfm_msg_t)) == 0) ret = sizeof(pfm_msg_t);
1620 abort_locked:
1621 UNPROTECT_CTX(ctx, flags);
1622 abort:
1623 return ret;
1626 static ssize_t
1627 pfm_write(struct file *file, const char __user *ubuf,
1628 size_t size, loff_t *ppos)
1630 DPRINT(("pfm_write called\n"));
1631 return -EINVAL;
1634 static unsigned int
1635 pfm_poll(struct file *filp, poll_table * wait)
1637 pfm_context_t *ctx;
1638 unsigned long flags;
1639 unsigned int mask = 0;
1641 if (PFM_IS_FILE(filp) == 0) {
1642 printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", current->pid);
1643 return 0;
1646 ctx = (pfm_context_t *)filp->private_data;
1647 if (ctx == NULL) {
1648 printk(KERN_ERR "perfmon: pfm_poll: NULL ctx [%d]\n", current->pid);
1649 return 0;
1653 DPRINT(("pfm_poll ctx_fd=%d before poll_wait\n", ctx->ctx_fd));
1655 poll_wait(filp, &ctx->ctx_msgq_wait, wait);
1657 PROTECT_CTX(ctx, flags);
1659 if (PFM_CTXQ_EMPTY(ctx) == 0)
1660 mask = POLLIN | POLLRDNORM;
1662 UNPROTECT_CTX(ctx, flags);
1664 DPRINT(("pfm_poll ctx_fd=%d mask=0x%x\n", ctx->ctx_fd, mask));
1666 return mask;
1669 static int
1670 pfm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1672 DPRINT(("pfm_ioctl called\n"));
1673 return -EINVAL;
1676 /*
1677 * interrupt cannot be masked when coming here
1678 */
1679 static inline int
1680 pfm_do_fasync(int fd, struct file *filp, pfm_context_t *ctx, int on)
1682 int ret;
1684 ret = fasync_helper (fd, filp, on, &ctx->ctx_async_queue);
1686 DPRINT(("pfm_fasync called by [%d] on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
1687 current->pid,
1688 fd,
1689 on,
1690 ctx->ctx_async_queue, ret));
1692 return ret;
1695 static int
1696 pfm_fasync(int fd, struct file *filp, int on)
1698 pfm_context_t *ctx;
1699 int ret;
1701 if (PFM_IS_FILE(filp) == 0) {
1702 printk(KERN_ERR "perfmon: pfm_fasync bad magic [%d]\n", current->pid);
1703 return -EBADF;
1706 ctx = (pfm_context_t *)filp->private_data;
1707 if (ctx == NULL) {
1708 printk(KERN_ERR "perfmon: pfm_fasync NULL ctx [%d]\n", current->pid);
1709 return -EBADF;
1711 /*
1712 * we cannot mask interrupts during this call because this may
1713 * may go to sleep if memory is not readily avalaible.
1715 * We are protected from the conetxt disappearing by the get_fd()/put_fd()
1716 * done in caller. Serialization of this function is ensured by caller.
1717 */
1718 ret = pfm_do_fasync(fd, filp, ctx, on);
1721 DPRINT(("pfm_fasync called on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
1722 fd,
1723 on,
1724 ctx->ctx_async_queue, ret));
1726 return ret;
1729 #ifdef CONFIG_SMP
1730 /*
1731 * this function is exclusively called from pfm_close().
1732 * The context is not protected at that time, nor are interrupts
1733 * on the remote CPU. That's necessary to avoid deadlocks.
1734 */
1735 static void
1736 pfm_syswide_force_stop(void *info)
1738 pfm_context_t *ctx = (pfm_context_t *)info;
1739 struct pt_regs *regs = task_pt_regs(current);
1740 struct task_struct *owner;
1741 unsigned long flags;
1742 int ret;
1744 if (ctx->ctx_cpu != smp_processor_id()) {
1745 printk(KERN_ERR "perfmon: pfm_syswide_force_stop for CPU%d but on CPU%d\n",
1746 ctx->ctx_cpu,
1747 smp_processor_id());
1748 return;
1750 owner = GET_PMU_OWNER();
1751 if (owner != ctx->ctx_task) {
1752 printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected owner [%d] instead of [%d]\n",
1753 smp_processor_id(),
1754 owner->pid, ctx->ctx_task->pid);
1755 return;
1757 if (GET_PMU_CTX() != ctx) {
1758 printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected ctx %p instead of %p\n",
1759 smp_processor_id(),
1760 GET_PMU_CTX(), ctx);
1761 return;
1764 DPRINT(("on CPU%d forcing system wide stop for [%d]\n", smp_processor_id(), ctx->ctx_task->pid));
1765 /*
1766 * the context is already protected in pfm_close(), we simply
1767 * need to mask interrupts to avoid a PMU interrupt race on
1768 * this CPU
1769 */
1770 local_irq_save(flags);
1772 ret = pfm_context_unload(ctx, NULL, 0, regs);
1773 if (ret) {
1774 DPRINT(("context_unload returned %d\n", ret));
1777 /*
1778 * unmask interrupts, PMU interrupts are now spurious here
1779 */
1780 local_irq_restore(flags);
1783 static void
1784 pfm_syswide_cleanup_other_cpu(pfm_context_t *ctx)
1786 int ret;
1788 DPRINT(("calling CPU%d for cleanup\n", ctx->ctx_cpu));
1789 ret = smp_call_function_single(ctx->ctx_cpu, pfm_syswide_force_stop, ctx, 0, 1);
1790 DPRINT(("called CPU%d for cleanup ret=%d\n", ctx->ctx_cpu, ret));
1792 #endif /* CONFIG_SMP */
1794 /*
1795 * called for each close(). Partially free resources.
1796 * When caller is self-monitoring, the context is unloaded.
1797 */
1798 static int
1799 pfm_flush(struct file *filp)
1801 pfm_context_t *ctx;
1802 struct task_struct *task;
1803 struct pt_regs *regs;
1804 unsigned long flags;
1805 unsigned long smpl_buf_size = 0UL;
1806 void *smpl_buf_vaddr = NULL;
1807 int state, is_system;
1809 if (PFM_IS_FILE(filp) == 0) {
1810 DPRINT(("bad magic for\n"));
1811 return -EBADF;
1814 ctx = (pfm_context_t *)filp->private_data;
1815 if (ctx == NULL) {
1816 printk(KERN_ERR "perfmon: pfm_flush: NULL ctx [%d]\n", current->pid);
1817 return -EBADF;
1820 /*
1821 * remove our file from the async queue, if we use this mode.
1822 * This can be done without the context being protected. We come
1823 * here when the context has become unreacheable by other tasks.
1825 * We may still have active monitoring at this point and we may
1826 * end up in pfm_overflow_handler(). However, fasync_helper()
1827 * operates with interrupts disabled and it cleans up the
1828 * queue. If the PMU handler is called prior to entering
1829 * fasync_helper() then it will send a signal. If it is
1830 * invoked after, it will find an empty queue and no
1831 * signal will be sent. In both case, we are safe
1832 */
1833 if (filp->f_flags & FASYNC) {
1834 DPRINT(("cleaning up async_queue=%p\n", ctx->ctx_async_queue));
1835 pfm_do_fasync (-1, filp, ctx, 0);
1838 PROTECT_CTX(ctx, flags);
1840 state = ctx->ctx_state;
1841 is_system = ctx->ctx_fl_system;
1843 task = PFM_CTX_TASK(ctx);
1844 regs = task_pt_regs(task);
1846 DPRINT(("ctx_state=%d is_current=%d\n",
1847 state,
1848 task == current ? 1 : 0));
1850 /*
1851 * if state == UNLOADED, then task is NULL
1852 */
1854 /*
1855 * we must stop and unload because we are losing access to the context.
1856 */
1857 if (task == current) {
1858 #ifdef CONFIG_SMP
1859 /*
1860 * the task IS the owner but it migrated to another CPU: that's bad
1861 * but we must handle this cleanly. Unfortunately, the kernel does
1862 * not provide a mechanism to block migration (while the context is loaded).
1864 * We need to release the resource on the ORIGINAL cpu.
1865 */
1866 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
1868 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
1869 /*
1870 * keep context protected but unmask interrupt for IPI
1871 */
1872 local_irq_restore(flags);
1874 pfm_syswide_cleanup_other_cpu(ctx);
1876 /*
1877 * restore interrupt masking
1878 */
1879 local_irq_save(flags);
1881 /*
1882 * context is unloaded at this point
1883 */
1884 } else
1885 #endif /* CONFIG_SMP */
1888 DPRINT(("forcing unload\n"));
1889 /*
1890 * stop and unload, returning with state UNLOADED
1891 * and session unreserved.
1892 */
1893 pfm_context_unload(ctx, NULL, 0, regs);
1895 DPRINT(("ctx_state=%d\n", ctx->ctx_state));
1899 /*
1900 * remove virtual mapping, if any, for the calling task.
1901 * cannot reset ctx field until last user is calling close().
1903 * ctx_smpl_vaddr must never be cleared because it is needed
1904 * by every task with access to the context
1906 * When called from do_exit(), the mm context is gone already, therefore
1907 * mm is NULL, i.e., the VMA is already gone and we do not have to
1908 * do anything here
1909 */
1910 if (ctx->ctx_smpl_vaddr && current->mm) {
1911 smpl_buf_vaddr = ctx->ctx_smpl_vaddr;
1912 smpl_buf_size = ctx->ctx_smpl_size;
1915 UNPROTECT_CTX(ctx, flags);
1917 /*
1918 * if there was a mapping, then we systematically remove it
1919 * at this point. Cannot be done inside critical section
1920 * because some VM function reenables interrupts.
1922 */
1923 if (smpl_buf_vaddr) pfm_remove_smpl_mapping(current, smpl_buf_vaddr, smpl_buf_size);
1925 return 0;
1927 /*
1928 * called either on explicit close() or from exit_files().
1929 * Only the LAST user of the file gets to this point, i.e., it is
1930 * called only ONCE.
1932 * IMPORTANT: we get called ONLY when the refcnt on the file gets to zero
1933 * (fput()),i.e, last task to access the file. Nobody else can access the
1934 * file at this point.
1936 * When called from exit_files(), the VMA has been freed because exit_mm()
1937 * is executed before exit_files().
1939 * When called from exit_files(), the current task is not yet ZOMBIE but we
1940 * flush the PMU state to the context.
1941 */
1942 static int
1943 pfm_close(struct inode *inode, struct file *filp)
1945 pfm_context_t *ctx;
1946 struct task_struct *task;
1947 struct pt_regs *regs;
1948 DECLARE_WAITQUEUE(wait, current);
1949 unsigned long flags;
1950 unsigned long smpl_buf_size = 0UL;
1951 void *smpl_buf_addr = NULL;
1952 int free_possible = 1;
1953 int state, is_system;
1955 DPRINT(("pfm_close called private=%p\n", filp->private_data));
1957 if (PFM_IS_FILE(filp) == 0) {
1958 DPRINT(("bad magic\n"));
1959 return -EBADF;
1962 ctx = (pfm_context_t *)filp->private_data;
1963 if (ctx == NULL) {
1964 printk(KERN_ERR "perfmon: pfm_close: NULL ctx [%d]\n", current->pid);
1965 return -EBADF;
1968 PROTECT_CTX(ctx, flags);
1970 state = ctx->ctx_state;
1971 is_system = ctx->ctx_fl_system;
1973 task = PFM_CTX_TASK(ctx);
1974 regs = task_pt_regs(task);
1976 DPRINT(("ctx_state=%d is_current=%d\n",
1977 state,
1978 task == current ? 1 : 0));
1980 /*
1981 * if task == current, then pfm_flush() unloaded the context
1982 */
1983 if (state == PFM_CTX_UNLOADED) goto doit;
1985 /*
1986 * context is loaded/masked and task != current, we need to
1987 * either force an unload or go zombie
1988 */
1990 /*
1991 * The task is currently blocked or will block after an overflow.
1992 * we must force it to wakeup to get out of the
1993 * MASKED state and transition to the unloaded state by itself.
1995 * This situation is only possible for per-task mode
1996 */
1997 if (state == PFM_CTX_MASKED && CTX_OVFL_NOBLOCK(ctx) == 0) {
1999 /*
2000 * set a "partial" zombie state to be checked
2001 * upon return from down() in pfm_handle_work().
2003 * We cannot use the ZOMBIE state, because it is checked
2004 * by pfm_load_regs() which is called upon wakeup from down().
2005 * In such case, it would free the context and then we would
2006 * return to pfm_handle_work() which would access the
2007 * stale context. Instead, we set a flag invisible to pfm_load_regs()
2008 * but visible to pfm_handle_work().
2010 * For some window of time, we have a zombie context with
2011 * ctx_state = MASKED and not ZOMBIE
2012 */
2013 ctx->ctx_fl_going_zombie = 1;
2015 /*
2016 * force task to wake up from MASKED state
2017 */
2018 complete(&ctx->ctx_restart_done);
2020 DPRINT(("waking up ctx_state=%d\n", state));
2022 /*
2023 * put ourself to sleep waiting for the other
2024 * task to report completion
2026 * the context is protected by mutex, therefore there
2027 * is no risk of being notified of completion before
2028 * begin actually on the waitq.
2029 */
2030 set_current_state(TASK_INTERRUPTIBLE);
2031 add_wait_queue(&ctx->ctx_zombieq, &wait);
2033 UNPROTECT_CTX(ctx, flags);
2035 /*
2036 * XXX: check for signals :
2037 * - ok for explicit close
2038 * - not ok when coming from exit_files()
2039 */
2040 schedule();
2043 PROTECT_CTX(ctx, flags);
2046 remove_wait_queue(&ctx->ctx_zombieq, &wait);
2047 set_current_state(TASK_RUNNING);
2049 /*
2050 * context is unloaded at this point
2051 */
2052 DPRINT(("after zombie wakeup ctx_state=%d for\n", state));
2054 else if (task != current) {
2055 #ifdef CONFIG_SMP
2056 /*
2057 * switch context to zombie state
2058 */
2059 ctx->ctx_state = PFM_CTX_ZOMBIE;
2061 DPRINT(("zombie ctx for [%d]\n", task->pid));
2062 /*
2063 * cannot free the context on the spot. deferred until
2064 * the task notices the ZOMBIE state
2065 */
2066 free_possible = 0;
2067 #else
2068 pfm_context_unload(ctx, NULL, 0, regs);
2069 #endif
2072 doit:
2073 /* reload state, may have changed during opening of critical section */
2074 state = ctx->ctx_state;
2076 /*
2077 * the context is still attached to a task (possibly current)
2078 * we cannot destroy it right now
2079 */
2081 /*
2082 * we must free the sampling buffer right here because
2083 * we cannot rely on it being cleaned up later by the
2084 * monitored task. It is not possible to free vmalloc'ed
2085 * memory in pfm_load_regs(). Instead, we remove the buffer
2086 * now. should there be subsequent PMU overflow originally
2087 * meant for sampling, the will be converted to spurious
2088 * and that's fine because the monitoring tools is gone anyway.
2089 */
2090 if (ctx->ctx_smpl_hdr) {
2091 smpl_buf_addr = ctx->ctx_smpl_hdr;
2092 smpl_buf_size = ctx->ctx_smpl_size;
2093 /* no more sampling */
2094 ctx->ctx_smpl_hdr = NULL;
2095 ctx->ctx_fl_is_sampling = 0;
2098 DPRINT(("ctx_state=%d free_possible=%d addr=%p size=%lu\n",
2099 state,
2100 free_possible,
2101 smpl_buf_addr,
2102 smpl_buf_size));
2104 if (smpl_buf_addr) pfm_exit_smpl_buffer(ctx->ctx_buf_fmt);
2106 /*
2107 * UNLOADED that the session has already been unreserved.
2108 */
2109 if (state == PFM_CTX_ZOMBIE) {
2110 pfm_unreserve_session(ctx, ctx->ctx_fl_system , ctx->ctx_cpu);
2113 /*
2114 * disconnect file descriptor from context must be done
2115 * before we unlock.
2116 */
2117 filp->private_data = NULL;
2119 /*
2120 * if we free on the spot, the context is now completely unreacheable
2121 * from the callers side. The monitored task side is also cut, so we
2122 * can freely cut.
2124 * If we have a deferred free, only the caller side is disconnected.
2125 */
2126 UNPROTECT_CTX(ctx, flags);
2128 /*
2129 * All memory free operations (especially for vmalloc'ed memory)
2130 * MUST be done with interrupts ENABLED.
2131 */
2132 if (smpl_buf_addr) pfm_rvfree(smpl_buf_addr, smpl_buf_size);
2134 /*
2135 * return the memory used by the context
2136 */
2137 if (free_possible) pfm_context_free(ctx);
2139 if (is_running_on_xen()) {
2140 if (is_xenoprof_primary()) {
2141 int ret = HYPERVISOR_perfmon_op(PFM_DESTROY_CONTEXT,
2142 NULL, 0);
2143 if (ret)
2144 printk("%s:%d PFM_DESTROY_CONTEXT hypercall "
2145 "failed\n", __func__, __LINE__);
2148 return 0;
2151 static int
2152 pfm_no_open(struct inode *irrelevant, struct file *dontcare)
2154 DPRINT(("pfm_no_open called\n"));
2155 return -ENXIO;
2160 static struct file_operations pfm_file_ops = {
2161 .llseek = no_llseek,
2162 .read = pfm_read,
2163 .write = pfm_write,
2164 .poll = pfm_poll,
2165 .ioctl = pfm_ioctl,
2166 .open = pfm_no_open, /* special open code to disallow open via /proc */
2167 .fasync = pfm_fasync,
2168 .release = pfm_close,
2169 .flush = pfm_flush
2170 };
2172 static int
2173 pfmfs_delete_dentry(struct dentry *dentry)
2175 return 1;
2178 static struct dentry_operations pfmfs_dentry_operations = {
2179 .d_delete = pfmfs_delete_dentry,
2180 };
2183 static int
2184 pfm_alloc_fd(struct file **cfile)
2186 int fd, ret = 0;
2187 struct file *file = NULL;
2188 struct inode * inode;
2189 char name[32];
2190 struct qstr this;
2192 fd = get_unused_fd();
2193 if (fd < 0) return -ENFILE;
2195 ret = -ENFILE;
2197 file = get_empty_filp();
2198 if (!file) goto out;
2200 /*
2201 * allocate a new inode
2202 */
2203 inode = new_inode(pfmfs_mnt->mnt_sb);
2204 if (!inode) goto out;
2206 DPRINT(("new inode ino=%ld @%p\n", inode->i_ino, inode));
2208 inode->i_mode = S_IFCHR|S_IRUGO;
2209 inode->i_uid = current->fsuid;
2210 inode->i_gid = current->fsgid;
2212 sprintf(name, "[%lu]", inode->i_ino);
2213 this.name = name;
2214 this.len = strlen(name);
2215 this.hash = inode->i_ino;
2217 ret = -ENOMEM;
2219 /*
2220 * allocate a new dcache entry
2221 */
2222 file->f_dentry = d_alloc(pfmfs_mnt->mnt_sb->s_root, &this);
2223 if (!file->f_dentry) goto out;
2225 file->f_dentry->d_op = &pfmfs_dentry_operations;
2227 d_add(file->f_dentry, inode);
2228 file->f_vfsmnt = mntget(pfmfs_mnt);
2229 file->f_mapping = inode->i_mapping;
2231 file->f_op = &pfm_file_ops;
2232 file->f_mode = FMODE_READ;
2233 file->f_flags = O_RDONLY;
2234 file->f_pos = 0;
2236 /*
2237 * may have to delay until context is attached?
2238 */
2239 fd_install(fd, file);
2241 /*
2242 * the file structure we will use
2243 */
2244 *cfile = file;
2246 return fd;
2247 out:
2248 if (file) put_filp(file);
2249 put_unused_fd(fd);
2250 return ret;
2253 static void
2254 pfm_free_fd(int fd, struct file *file)
2256 struct files_struct *files = current->files;
2257 struct fdtable *fdt;
2259 /*
2260 * there ie no fd_uninstall(), so we do it here
2261 */
2262 spin_lock(&files->file_lock);
2263 fdt = files_fdtable(files);
2264 rcu_assign_pointer(fdt->fd[fd], NULL);
2265 spin_unlock(&files->file_lock);
2267 if (file)
2268 put_filp(file);
2269 put_unused_fd(fd);
2272 static int
2273 pfm_remap_buffer(struct vm_area_struct *vma, unsigned long buf, unsigned long addr, unsigned long size)
2275 DPRINT(("CPU%d buf=0x%lx addr=0x%lx size=%ld\n", smp_processor_id(), buf, addr, size));
2277 while (size > 0) {
2278 unsigned long pfn = ia64_tpa(buf) >> PAGE_SHIFT;
2281 if (remap_pfn_range(vma, addr, pfn, PAGE_SIZE, PAGE_READONLY))
2282 return -ENOMEM;
2284 addr += PAGE_SIZE;
2285 buf += PAGE_SIZE;
2286 size -= PAGE_SIZE;
2288 return 0;
2291 /*
2292 * allocate a sampling buffer and remaps it into the user address space of the task
2293 */
2294 static int
2295 pfm_smpl_buffer_alloc(struct task_struct *task, pfm_context_t *ctx, unsigned long rsize, void **user_vaddr)
2297 struct mm_struct *mm = task->mm;
2298 struct vm_area_struct *vma = NULL;
2299 unsigned long size;
2300 void *smpl_buf;
2303 /*
2304 * the fixed header + requested size and align to page boundary
2305 */
2306 size = PAGE_ALIGN(rsize);
2308 DPRINT(("sampling buffer rsize=%lu size=%lu bytes\n", rsize, size));
2310 /*
2311 * check requested size to avoid Denial-of-service attacks
2312 * XXX: may have to refine this test
2313 * Check against address space limit.
2315 * if ((mm->total_vm << PAGE_SHIFT) + len> task->rlim[RLIMIT_AS].rlim_cur)
2316 * return -ENOMEM;
2317 */
2318 if (size > task->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
2319 return -ENOMEM;
2321 /*
2322 * We do the easy to undo allocations first.
2324 * pfm_rvmalloc(), clears the buffer, so there is no leak
2325 */
2326 smpl_buf = pfm_rvmalloc(size);
2327 if (smpl_buf == NULL) {
2328 DPRINT(("Can't allocate sampling buffer\n"));
2329 return -ENOMEM;
2332 DPRINT(("smpl_buf @%p\n", smpl_buf));
2334 /* allocate vma */
2335 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2336 if (!vma) {
2337 DPRINT(("Cannot allocate vma\n"));
2338 goto error_kmem;
2340 memset(vma, 0, sizeof(*vma));
2342 /*
2343 * partially initialize the vma for the sampling buffer
2344 */
2345 vma->vm_mm = mm;
2346 vma->vm_flags = VM_READ| VM_MAYREAD |VM_RESERVED;
2347 vma->vm_page_prot = PAGE_READONLY; /* XXX may need to change */
2349 /*
2350 * Now we have everything we need and we can initialize
2351 * and connect all the data structures
2352 */
2354 ctx->ctx_smpl_hdr = smpl_buf;
2355 ctx->ctx_smpl_size = size; /* aligned size */
2357 /*
2358 * Let's do the difficult operations next.
2360 * now we atomically find some area in the address space and
2361 * remap the buffer in it.
2362 */
2363 down_write(&task->mm->mmap_sem);
2365 /* find some free area in address space, must have mmap sem held */
2366 vma->vm_start = pfm_get_unmapped_area(NULL, 0, size, 0, MAP_PRIVATE|MAP_ANONYMOUS, 0);
2367 if (vma->vm_start == 0UL) {
2368 DPRINT(("Cannot find unmapped area for size %ld\n", size));
2369 up_write(&task->mm->mmap_sem);
2370 goto error;
2372 vma->vm_end = vma->vm_start + size;
2373 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2375 DPRINT(("aligned size=%ld, hdr=%p mapped @0x%lx\n", size, ctx->ctx_smpl_hdr, vma->vm_start));
2377 /* can only be applied to current task, need to have the mm semaphore held when called */
2378 if (pfm_remap_buffer(vma, (unsigned long)smpl_buf, vma->vm_start, size)) {
2379 DPRINT(("Can't remap buffer\n"));
2380 up_write(&task->mm->mmap_sem);
2381 goto error;
2384 /*
2385 * now insert the vma in the vm list for the process, must be
2386 * done with mmap lock held
2387 */
2388 insert_vm_struct(mm, vma);
2390 mm->total_vm += size >> PAGE_SHIFT;
2391 vm_stat_account(vma->vm_mm, vma->vm_flags, vma->vm_file,
2392 vma_pages(vma));
2393 up_write(&task->mm->mmap_sem);
2395 /*
2396 * keep track of user level virtual address
2397 */
2398 ctx->ctx_smpl_vaddr = (void *)vma->vm_start;
2399 *(unsigned long *)user_vaddr = vma->vm_start;
2401 return 0;
2403 error:
2404 kmem_cache_free(vm_area_cachep, vma);
2405 error_kmem:
2406 pfm_rvfree(smpl_buf, size);
2408 return -ENOMEM;
2411 /*
2412 * XXX: do something better here
2413 */
2414 static int
2415 pfm_bad_permissions(struct task_struct *task)
2417 /* inspired by ptrace_attach() */
2418 DPRINT(("cur: uid=%d gid=%d task: euid=%d suid=%d uid=%d egid=%d sgid=%d\n",
2419 current->uid,
2420 current->gid,
2421 task->euid,
2422 task->suid,
2423 task->uid,
2424 task->egid,
2425 task->sgid));
2427 return ((current->uid != task->euid)
2428 || (current->uid != task->suid)
2429 || (current->uid != task->uid)
2430 || (current->gid != task->egid)
2431 || (current->gid != task->sgid)
2432 || (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE);
2435 static int
2436 pfarg_is_sane(struct task_struct *task, pfarg_context_t *pfx)
2438 int ctx_flags;
2440 /* valid signal */
2442 ctx_flags = pfx->ctx_flags;
2444 if (ctx_flags & PFM_FL_SYSTEM_WIDE) {
2446 /*
2447 * cannot block in this mode
2448 */
2449 if (ctx_flags & PFM_FL_NOTIFY_BLOCK) {
2450 DPRINT(("cannot use blocking mode when in system wide monitoring\n"));
2451 return -EINVAL;
2453 } else {
2455 /* probably more to add here */
2457 return 0;
2460 static int
2461 pfm_setup_buffer_fmt(struct task_struct *task, pfm_context_t *ctx, unsigned int ctx_flags,
2462 unsigned int cpu, pfarg_context_t *arg)
2464 pfm_buffer_fmt_t *fmt = NULL;
2465 unsigned long size = 0UL;
2466 void *uaddr = NULL;
2467 void *fmt_arg = NULL;
2468 int ret = 0;
2469 #define PFM_CTXARG_BUF_ARG(a) (pfm_buffer_fmt_t *)(a+1)
2471 /* invoke and lock buffer format, if found */
2472 fmt = pfm_find_buffer_fmt(arg->ctx_smpl_buf_id);
2473 if (fmt == NULL) {
2474 DPRINT(("[%d] cannot find buffer format\n", task->pid));
2475 return -EINVAL;
2478 /*
2479 * buffer argument MUST be contiguous to pfarg_context_t
2480 */
2481 if (fmt->fmt_arg_size) fmt_arg = PFM_CTXARG_BUF_ARG(arg);
2483 ret = pfm_buf_fmt_validate(fmt, task, ctx_flags, cpu, fmt_arg);
2485 DPRINT(("[%d] after validate(0x%x,%d,%p)=%d\n", task->pid, ctx_flags, cpu, fmt_arg, ret));
2487 if (ret) goto error;
2489 /* link buffer format and context */
2490 ctx->ctx_buf_fmt = fmt;
2492 /*
2493 * check if buffer format wants to use perfmon buffer allocation/mapping service
2494 */
2495 ret = pfm_buf_fmt_getsize(fmt, task, ctx_flags, cpu, fmt_arg, &size);
2496 if (ret) goto error;
2498 if (size) {
2499 /*
2500 * buffer is always remapped into the caller's address space
2501 */
2502 ret = pfm_smpl_buffer_alloc(current, ctx, size, &uaddr);
2503 if (ret) goto error;
2505 /* keep track of user address of buffer */
2506 arg->ctx_smpl_vaddr = uaddr;
2508 ret = pfm_buf_fmt_init(fmt, task, ctx->ctx_smpl_hdr, ctx_flags, cpu, fmt_arg);
2510 error:
2511 return ret;
2514 static void
2515 pfm_reset_pmu_state(pfm_context_t *ctx)
2517 int i;
2519 /*
2520 * install reset values for PMC.
2521 */
2522 for (i=1; PMC_IS_LAST(i) == 0; i++) {
2523 if (PMC_IS_IMPL(i) == 0) continue;
2524 ctx->ctx_pmcs[i] = PMC_DFL_VAL(i);
2525 DPRINT(("pmc[%d]=0x%lx\n", i, ctx->ctx_pmcs[i]));
2527 /*
2528 * PMD registers are set to 0UL when the context in memset()
2529 */
2531 /*
2532 * On context switched restore, we must restore ALL pmc and ALL pmd even
2533 * when they are not actively used by the task. In UP, the incoming process
2534 * may otherwise pick up left over PMC, PMD state from the previous process.
2535 * As opposed to PMD, stale PMC can cause harm to the incoming
2536 * process because they may change what is being measured.
2537 * Therefore, we must systematically reinstall the entire
2538 * PMC state. In SMP, the same thing is possible on the
2539 * same CPU but also on between 2 CPUs.
2541 * The problem with PMD is information leaking especially
2542 * to user level when psr.sp=0
2544 * There is unfortunately no easy way to avoid this problem
2545 * on either UP or SMP. This definitively slows down the
2546 * pfm_load_regs() function.
2547 */
2549 /*
2550 * bitmask of all PMCs accessible to this context
2552 * PMC0 is treated differently.
2553 */
2554 ctx->ctx_all_pmcs[0] = pmu_conf->impl_pmcs[0] & ~0x1;
2556 /*
2557 * bitmask of all PMDs that are accesible to this context
2558 */
2559 ctx->ctx_all_pmds[0] = pmu_conf->impl_pmds[0];
2561 DPRINT(("<%d> all_pmcs=0x%lx all_pmds=0x%lx\n", ctx->ctx_fd, ctx->ctx_all_pmcs[0],ctx->ctx_all_pmds[0]));
2563 /*
2564 * useful in case of re-enable after disable
2565 */
2566 ctx->ctx_used_ibrs[0] = 0UL;
2567 ctx->ctx_used_dbrs[0] = 0UL;
2570 static int
2571 pfm_ctx_getsize(void *arg, size_t *sz)
2573 pfarg_context_t *req = (pfarg_context_t *)arg;
2574 pfm_buffer_fmt_t *fmt;
2576 *sz = 0;
2578 if (!pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) return 0;
2580 fmt = pfm_find_buffer_fmt(req->ctx_smpl_buf_id);
2581 if (fmt == NULL) {
2582 DPRINT(("cannot find buffer format\n"));
2583 return -EINVAL;
2585 /* get just enough to copy in user parameters */
2586 *sz = fmt->fmt_arg_size;
2587 DPRINT(("arg_size=%lu\n", *sz));
2589 return 0;
2594 /*
2595 * cannot attach if :
2596 * - kernel task
2597 * - task not owned by caller
2598 * - task incompatible with context mode
2599 */
2600 static int
2601 pfm_task_incompatible(pfm_context_t *ctx, struct task_struct *task)
2603 /*
2604 * no kernel task or task not owner by caller
2605 */
2606 if (task->mm == NULL) {
2607 DPRINT(("task [%d] has not memory context (kernel thread)\n", task->pid));
2608 return -EPERM;
2610 if (pfm_bad_permissions(task)) {
2611 DPRINT(("no permission to attach to [%d]\n", task->pid));
2612 return -EPERM;
2614 /*
2615 * cannot block in self-monitoring mode
2616 */
2617 if (CTX_OVFL_NOBLOCK(ctx) == 0 && task == current) {
2618 DPRINT(("cannot load a blocking context on self for [%d]\n", task->pid));
2619 return -EINVAL;
2622 if (task->exit_state == EXIT_ZOMBIE) {
2623 DPRINT(("cannot attach to zombie task [%d]\n", task->pid));
2624 return -EBUSY;
2627 /*
2628 * always ok for self
2629 */
2630 if (task == current) return 0;
2632 if ((task->state != TASK_STOPPED) && (task->state != TASK_TRACED)) {
2633 DPRINT(("cannot attach to non-stopped task [%d] state=%ld\n", task->pid, task->state));
2634 return -EBUSY;
2636 /*
2637 * make sure the task is off any CPU
2638 */
2639 wait_task_inactive(task);
2641 /* more to come... */
2643 return 0;
2646 static int
2647 pfm_get_task(pfm_context_t *ctx, pid_t pid, struct task_struct **task)
2649 struct task_struct *p = current;
2650 int ret;
2652 /* XXX: need to add more checks here */
2653 if (pid < 2) return -EPERM;
2655 if (pid != current->pid) {
2657 read_lock(&tasklist_lock);
2659 p = find_task_by_pid(pid);
2661 /* make sure task cannot go away while we operate on it */
2662 if (p) get_task_struct(p);
2664 read_unlock(&tasklist_lock);
2666 if (p == NULL) return -ESRCH;
2669 ret = pfm_task_incompatible(ctx, p);
2670 if (ret == 0) {
2671 *task = p;
2672 } else if (p != current) {
2673 pfm_put_task(p);
2675 return ret;
2680 static int
2681 pfm_context_create(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
2683 pfarg_context_t *req = (pfarg_context_t *)arg;
2684 struct file *filp;
2685 int ctx_flags;
2686 int ret;
2688 /* let's check the arguments first */
2689 ret = pfarg_is_sane(current, req);
2690 if (ret < 0) return ret;
2692 ctx_flags = req->ctx_flags;
2694 ret = -ENOMEM;
2696 ctx = pfm_context_alloc();
2697 if (!ctx) goto error;
2699 ret = pfm_alloc_fd(&filp);
2700 if (ret < 0) goto error_file;
2702 req->ctx_fd = ctx->ctx_fd = ret;
2704 /*
2705 * attach context to file
2706 */
2707 filp->private_data = ctx;
2709 /*
2710 * does the user want to sample?
2711 */
2712 if (pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) {
2713 ret = pfm_setup_buffer_fmt(current, ctx, ctx_flags, 0, req);
2714 if (ret) goto buffer_error;
2717 /*
2718 * init context protection lock
2719 */
2720 spin_lock_init(&ctx->ctx_lock);
2722 /*
2723 * context is unloaded
2724 */
2725 ctx->ctx_state = PFM_CTX_UNLOADED;
2727 /*
2728 * initialization of context's flags
2729 */
2730 ctx->ctx_fl_block = (ctx_flags & PFM_FL_NOTIFY_BLOCK) ? 1 : 0;
2731 ctx->ctx_fl_system = (ctx_flags & PFM_FL_SYSTEM_WIDE) ? 1: 0;
2732 ctx->ctx_fl_is_sampling = ctx->ctx_buf_fmt ? 1 : 0; /* assume record() is defined */
2733 ctx->ctx_fl_no_msg = (ctx_flags & PFM_FL_OVFL_NO_MSG) ? 1: 0;
2734 /*
2735 * will move to set properties
2736 * ctx->ctx_fl_excl_idle = (ctx_flags & PFM_FL_EXCL_IDLE) ? 1: 0;
2737 */
2739 /*
2740 * init restart semaphore to locked
2741 */
2742 init_completion(&ctx->ctx_restart_done);
2744 /*
2745 * activation is used in SMP only
2746 */
2747 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
2748 SET_LAST_CPU(ctx, -1);
2750 /*
2751 * initialize notification message queue
2752 */
2753 ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
2754 init_waitqueue_head(&ctx->ctx_msgq_wait);
2755 init_waitqueue_head(&ctx->ctx_zombieq);
2757 DPRINT(("ctx=%p flags=0x%x system=%d notify_block=%d excl_idle=%d no_msg=%d ctx_fd=%d \n",
2758 ctx,
2759 ctx_flags,
2760 ctx->ctx_fl_system,
2761 ctx->ctx_fl_block,
2762 ctx->ctx_fl_excl_idle,
2763 ctx->ctx_fl_no_msg,
2764 ctx->ctx_fd));
2766 /*
2767 * initialize soft PMU state
2768 */
2769 pfm_reset_pmu_state(ctx);
2771 if (is_running_on_xen()) {
2772 /*
2773 * kludge to get xenoprof.is_primary.
2774 * XENOPROF_init/ia64 is nop. so it is safe to call it here.
2775 */
2776 struct xenoprof_init init;
2777 ret = HYPERVISOR_xenoprof_op(XENOPROF_init, &init);
2778 if (ret)
2779 goto buffer_error;
2780 init_xenoprof_primary(init.is_primary);
2782 if (is_xenoprof_primary()) {
2783 ret = HYPERVISOR_perfmon_op(PFM_CREATE_CONTEXT, arg, 0);
2784 if (ret)
2785 goto buffer_error;
2788 return 0;
2790 buffer_error:
2791 pfm_free_fd(ctx->ctx_fd, filp);
2793 if (ctx->ctx_buf_fmt) {
2794 pfm_buf_fmt_exit(ctx->ctx_buf_fmt, current, NULL, regs);
2796 error_file:
2797 pfm_context_free(ctx);
2799 error:
2800 return ret;
2803 static inline unsigned long
2804 pfm_new_counter_value (pfm_counter_t *reg, int is_long_reset)
2806 unsigned long val = is_long_reset ? reg->long_reset : reg->short_reset;
2807 unsigned long new_seed, old_seed = reg->seed, mask = reg->mask;
2808 extern unsigned long carta_random32 (unsigned long seed);
2810 if (reg->flags & PFM_REGFL_RANDOM) {
2811 new_seed = carta_random32(old_seed);
2812 val -= (old_seed & mask); /* counter values are negative numbers! */
2813 if ((mask >> 32) != 0)
2814 /* construct a full 64-bit random value: */
2815 new_seed |= carta_random32(old_seed >> 32) << 32;
2816 reg->seed = new_seed;
2818 reg->lval = val;
2819 return val;
2822 static void
2823 pfm_reset_regs_masked(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
2825 unsigned long mask = ovfl_regs[0];
2826 unsigned long reset_others = 0UL;
2827 unsigned long val;
2828 int i;
2830 /*
2831 * now restore reset value on sampling overflowed counters
2832 */
2833 mask >>= PMU_FIRST_COUNTER;
2834 for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
2836 if ((mask & 0x1UL) == 0UL) continue;
2838 ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
2839 reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
2841 DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
2844 /*
2845 * Now take care of resetting the other registers
2846 */
2847 for(i = 0; reset_others; i++, reset_others >>= 1) {
2849 if ((reset_others & 0x1) == 0) continue;
2851 ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
2853 DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
2854 is_long_reset ? "long" : "short", i, val));
2858 static void
2859 pfm_reset_regs(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
2861 unsigned long mask = ovfl_regs[0];
2862 unsigned long reset_others = 0UL;
2863 unsigned long val;
2864 int i;
2866 DPRINT_ovfl(("ovfl_regs=0x%lx is_long_reset=%d\n", ovfl_regs[0], is_long_reset));
2868 if (ctx->ctx_state == PFM_CTX_MASKED) {
2869 pfm_reset_regs_masked(ctx, ovfl_regs, is_long_reset);
2870 return;
2873 /*
2874 * now restore reset value on sampling overflowed counters
2875 */
2876 mask >>= PMU_FIRST_COUNTER;
2877 for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
2879 if ((mask & 0x1UL) == 0UL) continue;
2881 val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
2882 reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
2884 DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
2886 pfm_write_soft_counter(ctx, i, val);
2889 /*
2890 * Now take care of resetting the other registers
2891 */
2892 for(i = 0; reset_others; i++, reset_others >>= 1) {
2894 if ((reset_others & 0x1) == 0) continue;
2896 val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
2898 if (PMD_IS_COUNTING(i)) {
2899 pfm_write_soft_counter(ctx, i, val);
2900 } else {
2901 ia64_set_pmd(i, val);
2903 DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
2904 is_long_reset ? "long" : "short", i, val));
2906 ia64_srlz_d();
2909 static int
2910 pfm_write_pmcs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
2912 struct thread_struct *thread = NULL;
2913 struct task_struct *task;
2914 pfarg_reg_t *req = (pfarg_reg_t *)arg;
2915 unsigned long value, pmc_pm;
2916 unsigned long smpl_pmds, reset_pmds, impl_pmds;
2917 unsigned int cnum, reg_flags, flags, pmc_type;
2918 int i, can_access_pmu = 0, is_loaded, is_system, expert_mode;
2919 int is_monitor, is_counting, state;
2920 int ret = -EINVAL;
2921 pfm_reg_check_t wr_func;
2922 #define PFM_CHECK_PMC_PM(x, y, z) ((x)->ctx_fl_system ^ PMC_PM(y, z))
2924 if (is_running_on_xen()) {
2925 if (is_xenoprof_primary())
2926 return HYPERVISOR_perfmon_op(PFM_WRITE_PMCS,
2927 arg, count);
2928 return 0;
2930 state = ctx->ctx_state;
2931 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
2932 is_system = ctx->ctx_fl_system;
2933 task = ctx->ctx_task;
2934 impl_pmds = pmu_conf->impl_pmds[0];
2936 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
2938 if (is_loaded) {
2939 thread = &task->thread;
2940 /*
2941 * In system wide and when the context is loaded, access can only happen
2942 * when the caller is running on the CPU being monitored by the session.
2943 * It does not have to be the owner (ctx_task) of the context per se.
2944 */
2945 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
2946 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
2947 return -EBUSY;
2949 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
2951 expert_mode = pfm_sysctl.expert_mode;
2953 for (i = 0; i < count; i++, req++) {
2955 cnum = req->reg_num;
2956 reg_flags = req->reg_flags;
2957 value = req->reg_value;
2958 smpl_pmds = req->reg_smpl_pmds[0];
2959 reset_pmds = req->reg_reset_pmds[0];
2960 flags = 0;
2963 if (cnum >= PMU_MAX_PMCS) {
2964 DPRINT(("pmc%u is invalid\n", cnum));
2965 goto error;
2968 pmc_type = pmu_conf->pmc_desc[cnum].type;
2969 pmc_pm = (value >> pmu_conf->pmc_desc[cnum].pm_pos) & 0x1;
2970 is_counting = (pmc_type & PFM_REG_COUNTING) == PFM_REG_COUNTING ? 1 : 0;
2971 is_monitor = (pmc_type & PFM_REG_MONITOR) == PFM_REG_MONITOR ? 1 : 0;
2973 /*
2974 * we reject all non implemented PMC as well
2975 * as attempts to modify PMC[0-3] which are used
2976 * as status registers by the PMU
2977 */
2978 if ((pmc_type & PFM_REG_IMPL) == 0 || (pmc_type & PFM_REG_CONTROL) == PFM_REG_CONTROL) {
2979 DPRINT(("pmc%u is unimplemented or no-access pmc_type=%x\n", cnum, pmc_type));
2980 goto error;
2982 wr_func = pmu_conf->pmc_desc[cnum].write_check;
2983 /*
2984 * If the PMC is a monitor, then if the value is not the default:
2985 * - system-wide session: PMCx.pm=1 (privileged monitor)
2986 * - per-task : PMCx.pm=0 (user monitor)
2987 */
2988 if (is_monitor && value != PMC_DFL_VAL(cnum) && is_system ^ pmc_pm) {
2989 DPRINT(("pmc%u pmc_pm=%lu is_system=%d\n",
2990 cnum,
2991 pmc_pm,
2992 is_system));
2993 goto error;
2996 if (is_counting) {
2997 /*
2998 * enforce generation of overflow interrupt. Necessary on all
2999 * CPUs.
3000 */
3001 value |= 1 << PMU_PMC_OI;
3003 if (reg_flags & PFM_REGFL_OVFL_NOTIFY) {
3004 flags |= PFM_REGFL_OVFL_NOTIFY;
3007 if (reg_flags & PFM_REGFL_RANDOM) flags |= PFM_REGFL_RANDOM;
3009 /* verify validity of smpl_pmds */
3010 if ((smpl_pmds & impl_pmds) != smpl_pmds) {
3011 DPRINT(("invalid smpl_pmds 0x%lx for pmc%u\n", smpl_pmds, cnum));
3012 goto error;
3015 /* verify validity of reset_pmds */
3016 if ((reset_pmds & impl_pmds) != reset_pmds) {
3017 DPRINT(("invalid reset_pmds 0x%lx for pmc%u\n", reset_pmds, cnum));
3018 goto error;
3020 } else {
3021 if (reg_flags & (PFM_REGFL_OVFL_NOTIFY|PFM_REGFL_RANDOM)) {
3022 DPRINT(("cannot set ovfl_notify or random on pmc%u\n", cnum));
3023 goto error;
3025 /* eventid on non-counting monitors are ignored */
3028 /*
3029 * execute write checker, if any
3030 */
3031 if (likely(expert_mode == 0 && wr_func)) {
3032 ret = (*wr_func)(task, ctx, cnum, &value, regs);
3033 if (ret) goto error;
3034 ret = -EINVAL;
3037 /*
3038 * no error on this register
3039 */
3040 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
3042 /*
3043 * Now we commit the changes to the software state
3044 */
3046 /*
3047 * update overflow information
3048 */
3049 if (is_counting) {
3050 /*
3051 * full flag update each time a register is programmed
3052 */
3053 ctx->ctx_pmds[cnum].flags = flags;
3055 ctx->ctx_pmds[cnum].reset_pmds[0] = reset_pmds;
3056 ctx->ctx_pmds[cnum].smpl_pmds[0] = smpl_pmds;
3057 ctx->ctx_pmds[cnum].eventid = req->reg_smpl_eventid;
3059 /*
3060 * Mark all PMDS to be accessed as used.
3062 * We do not keep track of PMC because we have to
3063 * systematically restore ALL of them.
3065 * We do not update the used_monitors mask, because
3066 * if we have not programmed them, then will be in
3067 * a quiescent state, therefore we will not need to
3068 * mask/restore then when context is MASKED.
3069 */
3070 CTX_USED_PMD(ctx, reset_pmds);
3071 CTX_USED_PMD(ctx, smpl_pmds);
3072 /*
3073 * make sure we do not try to reset on
3074 * restart because we have established new values
3075 */
3076 if (state == PFM_CTX_MASKED) ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
3078 /*
3079 * Needed in case the user does not initialize the equivalent
3080 * PMD. Clearing is done indirectly via pfm_reset_pmu_state() so there is no
3081 * possible leak here.
3082 */
3083 CTX_USED_PMD(ctx, pmu_conf->pmc_desc[cnum].dep_pmd[0]);
3085 /*
3086 * keep track of the monitor PMC that we are using.
3087 * we save the value of the pmc in ctx_pmcs[] and if
3088 * the monitoring is not stopped for the context we also
3089 * place it in the saved state area so that it will be
3090 * picked up later by the context switch code.
3092 * The value in ctx_pmcs[] can only be changed in pfm_write_pmcs().
3094 * The value in thread->pmcs[] may be modified on overflow, i.e., when
3095 * monitoring needs to be stopped.
3096 */
3097 if (is_monitor) CTX_USED_MONITOR(ctx, 1UL << cnum);
3099 /*
3100 * update context state
3101 */
3102 ctx->ctx_pmcs[cnum] = value;
3104 if (is_loaded) {
3105 /*
3106 * write thread state
3107 */
3108 if (is_system == 0) thread->pmcs[cnum] = value;
3110 /*
3111 * write hardware register if we can
3112 */
3113 if (can_access_pmu) {
3114 ia64_set_pmc(cnum, value);
3116 #ifdef CONFIG_SMP
3117 else {
3118 /*
3119 * per-task SMP only here
3121 * we are guaranteed that the task is not running on the other CPU,
3122 * we indicate that this PMD will need to be reloaded if the task
3123 * is rescheduled on the CPU it ran last on.
3124 */
3125 ctx->ctx_reload_pmcs[0] |= 1UL << cnum;
3127 #endif
3130 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",
3131 cnum,
3132 value,
3133 is_loaded,
3134 can_access_pmu,
3135 flags,
3136 ctx->ctx_all_pmcs[0],
3137 ctx->ctx_used_pmds[0],
3138 ctx->ctx_pmds[cnum].eventid,
3139 smpl_pmds,
3140 reset_pmds,
3141 ctx->ctx_reload_pmcs[0],
3142 ctx->ctx_used_monitors[0],
3143 ctx->ctx_ovfl_regs[0]));
3146 /*
3147 * make sure the changes are visible
3148 */
3149 if (can_access_pmu) ia64_srlz_d();
3151 return 0;
3152 error:
3153 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3154 return ret;
3157 static int
3158 pfm_write_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3160 struct thread_struct *thread = NULL;
3161 struct task_struct *task;
3162 pfarg_reg_t *req = (pfarg_reg_t *)arg;
3163 unsigned long value, hw_value, ovfl_mask;
3164 unsigned int cnum;
3165 int i, can_access_pmu = 0, state;
3166 int is_counting, is_loaded, is_system, expert_mode;
3167 int ret = -EINVAL;
3168 pfm_reg_check_t wr_func;
3170 if (is_running_on_xen()) {
3171 if (is_xenoprof_primary())
3172 return HYPERVISOR_perfmon_op(PFM_WRITE_PMDS,
3173 arg, count);
3174 return 0;
3177 state = ctx->ctx_state;
3178 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3179 is_system = ctx->ctx_fl_system;
3180 ovfl_mask = pmu_conf->ovfl_val;
3181 task = ctx->ctx_task;
3183 if (unlikely(state == PFM_CTX_ZOMBIE)) return -EINVAL;
3185 /*
3186 * on both UP and SMP, we can only write to the PMC when the task is
3187 * the owner of the local PMU.
3188 */
3189 if (likely(is_loaded)) {
3190 thread = &task->thread;
3191 /*
3192 * In system wide and when the context is loaded, access can only happen
3193 * when the caller is running on the CPU being monitored by the session.
3194 * It does not have to be the owner (ctx_task) of the context per se.
3195 */
3196 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3197 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3198 return -EBUSY;
3200 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3202 expert_mode = pfm_sysctl.expert_mode;
3204 for (i = 0; i < count; i++, req++) {
3206 cnum = req->reg_num;
3207 value = req->reg_value;
3209 if (!PMD_IS_IMPL(cnum)) {
3210 DPRINT(("pmd[%u] is unimplemented or invalid\n", cnum));
3211 goto abort_mission;
3213 is_counting = PMD_IS_COUNTING(cnum);
3214 wr_func = pmu_conf->pmd_desc[cnum].write_check;
3216 /*
3217 * execute write checker, if any
3218 */
3219 if (unlikely(expert_mode == 0 && wr_func)) {
3220 unsigned long v = value;
3222 ret = (*wr_func)(task, ctx, cnum, &v, regs);
3223 if (ret) goto abort_mission;
3225 value = v;
3226 ret = -EINVAL;
3229 /*
3230 * no error on this register
3231 */
3232 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
3234 /*
3235 * now commit changes to software state
3236 */
3237 hw_value = value;
3239 /*
3240 * update virtualized (64bits) counter
3241 */
3242 if (is_counting) {
3243 /*
3244 * write context state
3245 */
3246 ctx->ctx_pmds[cnum].lval = value;
3248 /*
3249 * when context is load we use the split value
3250 */
3251 if (is_loaded) {
3252 hw_value = value & ovfl_mask;
3253 value = value & ~ovfl_mask;
3256 /*
3257 * update reset values (not just for counters)
3258 */
3259 ctx->ctx_pmds[cnum].long_reset = req->reg_long_reset;
3260 ctx->ctx_pmds[cnum].short_reset = req->reg_short_reset;
3262 /*
3263 * update randomization parameters (not just for counters)
3264 */
3265 ctx->ctx_pmds[cnum].seed = req->reg_random_seed;
3266 ctx->ctx_pmds[cnum].mask = req->reg_random_mask;
3268 /*
3269 * update context value
3270 */
3271 ctx->ctx_pmds[cnum].val = value;
3273 /*
3274 * Keep track of what we use
3276 * We do not keep track of PMC because we have to
3277 * systematically restore ALL of them.
3278 */
3279 CTX_USED_PMD(ctx, PMD_PMD_DEP(cnum));
3281 /*
3282 * mark this PMD register used as well
3283 */
3284 CTX_USED_PMD(ctx, RDEP(cnum));
3286 /*
3287 * make sure we do not try to reset on
3288 * restart because we have established new values
3289 */
3290 if (is_counting && state == PFM_CTX_MASKED) {
3291 ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
3294 if (is_loaded) {
3295 /*
3296 * write thread state
3297 */
3298 if (is_system == 0) thread->pmds[cnum] = hw_value;
3300 /*
3301 * write hardware register if we can
3302 */
3303 if (can_access_pmu) {
3304 ia64_set_pmd(cnum, hw_value);
3305 } else {
3306 #ifdef CONFIG_SMP
3307 /*
3308 * we are guaranteed that the task is not running on the other CPU,
3309 * we indicate that this PMD will need to be reloaded if the task
3310 * is rescheduled on the CPU it ran last on.
3311 */
3312 ctx->ctx_reload_pmds[0] |= 1UL << cnum;
3313 #endif
3317 DPRINT(("pmd[%u]=0x%lx ld=%d apmu=%d, hw_value=0x%lx ctx_pmd=0x%lx short_reset=0x%lx "
3318 "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",
3319 cnum,
3320 value,
3321 is_loaded,
3322 can_access_pmu,
3323 hw_value,
3324 ctx->ctx_pmds[cnum].val,
3325 ctx->ctx_pmds[cnum].short_reset,
3326 ctx->ctx_pmds[cnum].long_reset,
3327 PMC_OVFL_NOTIFY(ctx, cnum) ? 'Y':'N',
3328 ctx->ctx_pmds[cnum].seed,
3329 ctx->ctx_pmds[cnum].mask,
3330 ctx->ctx_used_pmds[0],
3331 ctx->ctx_pmds[cnum].reset_pmds[0],
3332 ctx->ctx_reload_pmds[0],
3333 ctx->ctx_all_pmds[0],
3334 ctx->ctx_ovfl_regs[0]));
3337 /*
3338 * make changes visible
3339 */
3340 if (can_access_pmu) ia64_srlz_d();
3342 return 0;
3344 abort_mission:
3345 /*
3346 * for now, we have only one possibility for error
3347 */
3348 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3349 return ret;
3352 /*
3353 * By the way of PROTECT_CONTEXT(), interrupts are masked while we are in this function.
3354 * Therefore we know, we do not have to worry about the PMU overflow interrupt. If an
3355 * interrupt is delivered during the call, it will be kept pending until we leave, making
3356 * it appears as if it had been generated at the UNPROTECT_CONTEXT(). At least we are
3357 * guaranteed to return consistent data to the user, it may simply be old. It is not
3358 * trivial to treat the overflow while inside the call because you may end up in
3359 * some module sampling buffer code causing deadlocks.
3360 */
3361 static int
3362 pfm_read_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3364 struct thread_struct *thread = NULL;
3365 struct task_struct *task;
3366 unsigned long val = 0UL, lval, ovfl_mask, sval;
3367 pfarg_reg_t *req = (pfarg_reg_t *)arg;
3368 unsigned int cnum, reg_flags = 0;
3369 int i, can_access_pmu = 0, state;
3370 int is_loaded, is_system, is_counting, expert_mode;
3371 int ret = -EINVAL;
3372 pfm_reg_check_t rd_func;
3373 XEN_NOT_SUPPORTED_YET;
3375 /*
3376 * access is possible when loaded only for
3377 * self-monitoring tasks or in UP mode
3378 */
3380 state = ctx->ctx_state;
3381 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3382 is_system = ctx->ctx_fl_system;
3383 ovfl_mask = pmu_conf->ovfl_val;
3384 task = ctx->ctx_task;
3386 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
3388 if (likely(is_loaded)) {
3389 thread = &task->thread;
3390 /*
3391 * In system wide and when the context is loaded, access can only happen
3392 * when the caller is running on the CPU being monitored by the session.
3393 * It does not have to be the owner (ctx_task) of the context per se.
3394 */
3395 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3396 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3397 return -EBUSY;
3399 /*
3400 * this can be true when not self-monitoring only in UP
3401 */
3402 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3404 if (can_access_pmu) ia64_srlz_d();
3406 expert_mode = pfm_sysctl.expert_mode;
3408 DPRINT(("ld=%d apmu=%d ctx_state=%d\n",
3409 is_loaded,
3410 can_access_pmu,
3411 state));
3413 /*
3414 * on both UP and SMP, we can only read the PMD from the hardware register when
3415 * the task is the owner of the local PMU.
3416 */
3418 for (i = 0; i < count; i++, req++) {
3420 cnum = req->reg_num;
3421 reg_flags = req->reg_flags;
3423 if (unlikely(!PMD_IS_IMPL(cnum))) goto error;
3424 /*
3425 * we can only read the register that we use. That includes
3426 * the one we explicitely initialize AND the one we want included
3427 * in the sampling buffer (smpl_regs).
3429 * Having this restriction allows optimization in the ctxsw routine
3430 * without compromising security (leaks)
3431 */
3432 if (unlikely(!CTX_IS_USED_PMD(ctx, cnum))) goto error;
3434 sval = ctx->ctx_pmds[cnum].val;
3435 lval = ctx->ctx_pmds[cnum].lval;
3436 is_counting = PMD_IS_COUNTING(cnum);
3438 /*
3439 * If the task is not the current one, then we check if the
3440 * PMU state is still in the local live register due to lazy ctxsw.
3441 * If true, then we read directly from the registers.
3442 */
3443 if (can_access_pmu){
3444 val = ia64_get_pmd(cnum);
3445 } else {
3446 /*
3447 * context has been saved
3448 * if context is zombie, then task does not exist anymore.
3449 * In this case, we use the full value saved in the context (pfm_flush_regs()).
3450 */
3451 val = is_loaded ? thread->pmds[cnum] : 0UL;
3453 rd_func = pmu_conf->pmd_desc[cnum].read_check;
3455 if (is_counting) {
3456 /*
3457 * XXX: need to check for overflow when loaded
3458 */
3459 val &= ovfl_mask;
3460 val += sval;
3463 /*
3464 * execute read checker, if any
3465 */
3466 if (unlikely(expert_mode == 0 && rd_func)) {
3467 unsigned long v = val;
3468 ret = (*rd_func)(ctx->ctx_task, ctx, cnum, &v, regs);
3469 if (ret) goto error;
3470 val = v;
3471 ret = -EINVAL;
3474 PFM_REG_RETFLAG_SET(reg_flags, 0);
3476 DPRINT(("pmd[%u]=0x%lx\n", cnum, val));
3478 /*
3479 * update register return value, abort all if problem during copy.
3480 * we only modify the reg_flags field. no check mode is fine because
3481 * access has been verified upfront in sys_perfmonctl().
3482 */
3483 req->reg_value = val;
3484 req->reg_flags = reg_flags;
3485 req->reg_last_reset_val = lval;
3488 return 0;
3490 error:
3491 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3492 return ret;
3495 int
3496 pfm_mod_write_pmcs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3498 pfm_context_t *ctx;
3500 if (req == NULL) return -EINVAL;
3502 ctx = GET_PMU_CTX();
3504 if (ctx == NULL) return -EINVAL;
3506 /*
3507 * for now limit to current task, which is enough when calling
3508 * from overflow handler
3509 */
3510 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3512 return pfm_write_pmcs(ctx, req, nreq, regs);
3514 EXPORT_SYMBOL(pfm_mod_write_pmcs);
3516 int
3517 pfm_mod_read_pmds(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3519 pfm_context_t *ctx;
3521 if (req == NULL) return -EINVAL;
3523 ctx = GET_PMU_CTX();
3525 if (ctx == NULL) return -EINVAL;
3527 /*
3528 * for now limit to current task, which is enough when calling
3529 * from overflow handler
3530 */
3531 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3533 return pfm_read_pmds(ctx, req, nreq, regs);
3535 EXPORT_SYMBOL(pfm_mod_read_pmds);
3537 /*
3538 * Only call this function when a process it trying to
3539 * write the debug registers (reading is always allowed)
3540 */
3541 int
3542 pfm_use_debug_registers(struct task_struct *task)
3544 pfm_context_t *ctx = task->thread.pfm_context;
3545 unsigned long flags;
3546 int ret = 0;
3548 if (pmu_conf->use_rr_dbregs == 0) return 0;
3550 DPRINT(("called for [%d]\n", task->pid));
3552 /*
3553 * do it only once
3554 */
3555 if (task->thread.flags & IA64_THREAD_DBG_VALID) return 0;
3557 /*
3558 * Even on SMP, we do not need to use an atomic here because
3559 * the only way in is via ptrace() and this is possible only when the
3560 * process is stopped. Even in the case where the ctxsw out is not totally
3561 * completed by the time we come here, there is no way the 'stopped' process
3562 * could be in the middle of fiddling with the pfm_write_ibr_dbr() routine.
3563 * So this is always safe.
3564 */
3565 if (ctx && ctx->ctx_fl_using_dbreg == 1) return -1;
3567 LOCK_PFS(flags);
3569 /*
3570 * We cannot allow setting breakpoints when system wide monitoring
3571 * sessions are using the debug registers.
3572 */
3573 if (pfm_sessions.pfs_sys_use_dbregs> 0)
3574 ret = -1;
3575 else
3576 pfm_sessions.pfs_ptrace_use_dbregs++;
3578 DPRINT(("ptrace_use_dbregs=%u sys_use_dbregs=%u by [%d] ret = %d\n",
3579 pfm_sessions.pfs_ptrace_use_dbregs,
3580 pfm_sessions.pfs_sys_use_dbregs,
3581 task->pid, ret));
3583 UNLOCK_PFS(flags);
3585 return ret;
3588 /*
3589 * This function is called for every task that exits with the
3590 * IA64_THREAD_DBG_VALID set. This indicates a task which was
3591 * able to use the debug registers for debugging purposes via
3592 * ptrace(). Therefore we know it was not using them for
3593 * perfmormance monitoring, so we only decrement the number
3594 * of "ptraced" debug register users to keep the count up to date
3595 */
3596 int
3597 pfm_release_debug_registers(struct task_struct *task)
3599 unsigned long flags;
3600 int ret;
3602 if (pmu_conf->use_rr_dbregs == 0) return 0;
3604 LOCK_PFS(flags);
3605 if (pfm_sessions.pfs_ptrace_use_dbregs == 0) {
3606 printk(KERN_ERR "perfmon: invalid release for [%d] ptrace_use_dbregs=0\n", task->pid);
3607 ret = -1;
3608 } else {
3609 pfm_sessions.pfs_ptrace_use_dbregs--;
3610 ret = 0;
3612 UNLOCK_PFS(flags);
3614 return ret;
3617 static int
3618 pfm_restart(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3620 struct task_struct *task;
3621 pfm_buffer_fmt_t *fmt;
3622 pfm_ovfl_ctrl_t rst_ctrl;
3623 int state, is_system;
3624 int ret = 0;
3625 XEN_NOT_SUPPORTED_YET;
3627 state = ctx->ctx_state;
3628 fmt = ctx->ctx_buf_fmt;
3629 is_system = ctx->ctx_fl_system;
3630 task = PFM_CTX_TASK(ctx);
3632 switch(state) {
3633 case PFM_CTX_MASKED:
3634 break;
3635 case PFM_CTX_LOADED:
3636 if (CTX_HAS_SMPL(ctx) && fmt->fmt_restart_active) break;
3637 /* fall through */
3638 case PFM_CTX_UNLOADED:
3639 case PFM_CTX_ZOMBIE:
3640 DPRINT(("invalid state=%d\n", state));
3641 return -EBUSY;
3642 default:
3643 DPRINT(("state=%d, cannot operate (no active_restart handler)\n", state));
3644 return -EINVAL;
3647 /*
3648 * In system wide and when the context is loaded, access can only happen
3649 * when the caller is running on the CPU being monitored by the session.
3650 * It does not have to be the owner (ctx_task) of the context per se.
3651 */
3652 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
3653 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3654 return -EBUSY;
3657 /* sanity check */
3658 if (unlikely(task == NULL)) {
3659 printk(KERN_ERR "perfmon: [%d] pfm_restart no task\n", current->pid);
3660 return -EINVAL;
3663 if (task == current || is_system) {
3665 fmt = ctx->ctx_buf_fmt;
3667 DPRINT(("restarting self %d ovfl=0x%lx\n",
3668 task->pid,
3669 ctx->ctx_ovfl_regs[0]));
3671 if (CTX_HAS_SMPL(ctx)) {
3673 prefetch(ctx->ctx_smpl_hdr);
3675 rst_ctrl.bits.mask_monitoring = 0;
3676 rst_ctrl.bits.reset_ovfl_pmds = 0;
3678 if (state == PFM_CTX_LOADED)
3679 ret = pfm_buf_fmt_restart_active(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
3680 else
3681 ret = pfm_buf_fmt_restart(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
3682 } else {
3683 rst_ctrl.bits.mask_monitoring = 0;
3684 rst_ctrl.bits.reset_ovfl_pmds = 1;
3687 if (ret == 0) {
3688 if (rst_ctrl.bits.reset_ovfl_pmds)
3689 pfm_reset_regs(ctx, ctx->ctx_ovfl_regs, PFM_PMD_LONG_RESET);
3691 if (rst_ctrl.bits.mask_monitoring == 0) {
3692 DPRINT(("resuming monitoring for [%d]\n", task->pid));
3694 if (state == PFM_CTX_MASKED) pfm_restore_monitoring(task);
3695 } else {
3696 DPRINT(("keeping monitoring stopped for [%d]\n", task->pid));
3698 // cannot use pfm_stop_monitoring(task, regs);
3701 /*
3702 * clear overflowed PMD mask to remove any stale information
3703 */
3704 ctx->ctx_ovfl_regs[0] = 0UL;
3706 /*
3707 * back to LOADED state
3708 */
3709 ctx->ctx_state = PFM_CTX_LOADED;
3711 /*
3712 * XXX: not really useful for self monitoring
3713 */
3714 ctx->ctx_fl_can_restart = 0;
3716 return 0;
3719 /*
3720 * restart another task
3721 */
3723 /*
3724 * When PFM_CTX_MASKED, we cannot issue a restart before the previous
3725 * one is seen by the task.
3726 */
3727 if (state == PFM_CTX_MASKED) {
3728 if (ctx->ctx_fl_can_restart == 0) return -EINVAL;
3729 /*
3730 * will prevent subsequent restart before this one is
3731 * seen by other task
3732 */
3733 ctx->ctx_fl_can_restart = 0;
3736 /*
3737 * if blocking, then post the semaphore is PFM_CTX_MASKED, i.e.
3738 * the task is blocked or on its way to block. That's the normal
3739 * restart path. If the monitoring is not masked, then the task
3740 * can be actively monitoring and we cannot directly intervene.
3741 * Therefore we use the trap mechanism to catch the task and
3742 * force it to reset the buffer/reset PMDs.
3744 * if non-blocking, then we ensure that the task will go into
3745 * pfm_handle_work() before returning to user mode.
3747 * We cannot explicitely reset another task, it MUST always
3748 * be done by the task itself. This works for system wide because
3749 * the tool that is controlling the session is logically doing
3750 * "self-monitoring".
3751 */
3752 if (CTX_OVFL_NOBLOCK(ctx) == 0 && state == PFM_CTX_MASKED) {
3753 DPRINT(("unblocking [%d] \n", task->pid));
3754 complete(&ctx->ctx_restart_done);
3755 } else {
3756 DPRINT(("[%d] armed exit trap\n", task->pid));
3758 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_RESET;
3760 PFM_SET_WORK_PENDING(task, 1);
3762 pfm_set_task_notify(task);
3764 /*
3765 * XXX: send reschedule if task runs on another CPU
3766 */
3768 return 0;
3771 static int
3772 pfm_debug(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3774 unsigned int m = *(unsigned int *)arg;
3775 XEN_NOT_SUPPORTED_YET;
3777 pfm_sysctl.debug = m == 0 ? 0 : 1;
3779 printk(KERN_INFO "perfmon debugging %s (timing reset)\n", pfm_sysctl.debug ? "on" : "off");
3781 if (m == 0) {
3782 memset(pfm_stats, 0, sizeof(pfm_stats));
3783 for(m=0; m < NR_CPUS; m++) pfm_stats[m].pfm_ovfl_intr_cycles_min = ~0UL;
3785 return 0;
3788 /*
3789 * arg can be NULL and count can be zero for this function
3790 */
3791 static int
3792 pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3794 struct thread_struct *thread = NULL;
3795 struct task_struct *task;
3796 pfarg_dbreg_t *req = (pfarg_dbreg_t *)arg;
3797 unsigned long flags;
3798 dbreg_t dbreg;
3799 unsigned int rnum;
3800 int first_time;
3801 int ret = 0, state;
3802 int i, can_access_pmu = 0;
3803 int is_system, is_loaded;
3805 if (pmu_conf->use_rr_dbregs == 0) return -EINVAL;
3807 state = ctx->ctx_state;
3808 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3809 is_system = ctx->ctx_fl_system;
3810 task = ctx->ctx_task;
3812 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
3814 /*
3815 * on both UP and SMP, we can only write to the PMC when the task is
3816 * the owner of the local PMU.
3817 */
3818 if (is_loaded) {
3819 thread = &task->thread;
3820 /*
3821 * In system wide and when the context is loaded, access can only happen
3822 * when the caller is running on the CPU being monitored by the session.
3823 * It does not have to be the owner (ctx_task) of the context per se.
3824 */
3825 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3826 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3827 return -EBUSY;
3829 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3832 /*
3833 * we do not need to check for ipsr.db because we do clear ibr.x, dbr.r, and dbr.w
3834 * ensuring that no real breakpoint can be installed via this call.
3836 * IMPORTANT: regs can be NULL in this function
3837 */
3839 first_time = ctx->ctx_fl_using_dbreg == 0;
3841 /*
3842 * don't bother if we are loaded and task is being debugged
3843 */
3844 if (is_loaded && (thread->flags & IA64_THREAD_DBG_VALID) != 0) {
3845 DPRINT(("debug registers already in use for [%d]\n", task->pid));
3846 return -EBUSY;
3849 /*
3850 * check for debug registers in system wide mode
3852 * If though a check is done in pfm_context_load(),
3853 * we must repeat it here, in case the registers are
3854 * written after the context is loaded
3855 */
3856 if (is_loaded) {
3857 LOCK_PFS(flags);
3859 if (first_time && is_system) {
3860 if (pfm_sessions.pfs_ptrace_use_dbregs)
3861 ret = -EBUSY;
3862 else
3863 pfm_sessions.pfs_sys_use_dbregs++;
3865 UNLOCK_PFS(flags);
3868 if (ret != 0) return ret;
3870 /*
3871 * mark ourself as user of the debug registers for
3872 * perfmon purposes.
3873 */
3874 ctx->ctx_fl_using_dbreg = 1;
3876 /*
3877 * clear hardware registers to make sure we don't
3878 * pick up stale state.
3880 * for a system wide session, we do not use
3881 * thread.dbr, thread.ibr because this process
3882 * never leaves the current CPU and the state
3883 * is shared by all processes running on it
3884 */
3885 if (first_time && can_access_pmu) {
3886 DPRINT(("[%d] clearing ibrs, dbrs\n", task->pid));
3887 for (i=0; i < pmu_conf->num_ibrs; i++) {
3888 ia64_set_ibr(i, 0UL);
3889 ia64_dv_serialize_instruction();
3891 ia64_srlz_i();
3892 for (i=0; i < pmu_conf->num_dbrs; i++) {
3893 ia64_set_dbr(i, 0UL);
3894 ia64_dv_serialize_data();
3896 ia64_srlz_d();
3899 /*
3900 * Now install the values into the registers
3901 */
3902 for (i = 0; i < count; i++, req++) {
3904 rnum = req->dbreg_num;
3905 dbreg.val = req->dbreg_value;
3907 ret = -EINVAL;
3909 if ((mode == PFM_CODE_RR && rnum >= PFM_NUM_IBRS) || ((mode == PFM_DATA_RR) && rnum >= PFM_NUM_DBRS)) {
3910 DPRINT(("invalid register %u val=0x%lx mode=%d i=%d count=%d\n",
3911 rnum, dbreg.val, mode, i, count));
3913 goto abort_mission;
3916 /*
3917 * make sure we do not install enabled breakpoint
3918 */
3919 if (rnum & 0x1) {
3920 if (mode == PFM_CODE_RR)
3921 dbreg.ibr.ibr_x = 0;
3922 else
3923 dbreg.dbr.dbr_r = dbreg.dbr.dbr_w = 0;
3926 PFM_REG_RETFLAG_SET(req->dbreg_flags, 0);
3928 /*
3929 * Debug registers, just like PMC, can only be modified
3930 * by a kernel call. Moreover, perfmon() access to those
3931 * registers are centralized in this routine. The hardware
3932 * does not modify the value of these registers, therefore,
3933 * if we save them as they are written, we can avoid having
3934 * to save them on context switch out. This is made possible
3935 * by the fact that when perfmon uses debug registers, ptrace()
3936 * won't be able to modify them concurrently.
3937 */
3938 if (mode == PFM_CODE_RR) {
3939 CTX_USED_IBR(ctx, rnum);
3941 if (can_access_pmu) {
3942 ia64_set_ibr(rnum, dbreg.val);
3943 ia64_dv_serialize_instruction();
3946 ctx->ctx_ibrs[rnum] = dbreg.val;
3948 DPRINT(("write ibr%u=0x%lx used_ibrs=0x%x ld=%d apmu=%d\n",
3949 rnum, dbreg.val, ctx->ctx_used_ibrs[0], is_loaded, can_access_pmu));
3950 } else {
3951 CTX_USED_DBR(ctx, rnum);
3953 if (can_access_pmu) {
3954 ia64_set_dbr(rnum, dbreg.val);
3955 ia64_dv_serialize_data();
3957 ctx->ctx_dbrs[rnum] = dbreg.val;
3959 DPRINT(("write dbr%u=0x%lx used_dbrs=0x%x ld=%d apmu=%d\n",
3960 rnum, dbreg.val, ctx->ctx_used_dbrs[0], is_loaded, can_access_pmu));
3964 return 0;
3966 abort_mission:
3967 /*
3968 * in case it was our first attempt, we undo the global modifications
3969 */
3970 if (first_time) {
3971 LOCK_PFS(flags);
3972 if (ctx->ctx_fl_system) {
3973 pfm_sessions.pfs_sys_use_dbregs--;
3975 UNLOCK_PFS(flags);
3976 ctx->ctx_fl_using_dbreg = 0;
3978 /*
3979 * install error return flag
3980 */
3981 PFM_REG_RETFLAG_SET(req->dbreg_flags, PFM_REG_RETFL_EINVAL);
3983 return ret;
3986 static int
3987 pfm_write_ibrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3989 return pfm_write_ibr_dbr(PFM_CODE_RR, ctx, arg, count, regs);
3992 static int
3993 pfm_write_dbrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3995 return pfm_write_ibr_dbr(PFM_DATA_RR, ctx, arg, count, regs);
3998 int
3999 pfm_mod_write_ibrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
4001 pfm_context_t *ctx;
4003 if (req == NULL) return -EINVAL;
4005 ctx = GET_PMU_CTX();
4007 if (ctx == NULL) return -EINVAL;
4009 /*
4010 * for now limit to current task, which is enough when calling
4011 * from overflow handler
4012 */
4013 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
4015 return pfm_write_ibrs(ctx, req, nreq, regs);
4017 EXPORT_SYMBOL(pfm_mod_write_ibrs);
4019 int
4020 pfm_mod_write_dbrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
4022 pfm_context_t *ctx;
4024 if (req == NULL) return -EINVAL;
4026 ctx = GET_PMU_CTX();
4028 if (ctx == NULL) return -EINVAL;
4030 /*
4031 * for now limit to current task, which is enough when calling
4032 * from overflow handler
4033 */
4034 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
4036 return pfm_write_dbrs(ctx, req, nreq, regs);
4038 EXPORT_SYMBOL(pfm_mod_write_dbrs);
4041 static int
4042 pfm_get_features(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4044 pfarg_features_t *req = (pfarg_features_t *)arg;
4046 if (is_running_on_xen())
4047 return HYPERVISOR_perfmon_op(PFM_GET_FEATURES, &arg, 0);
4048 req->ft_version = PFM_VERSION;
4049 return 0;
4052 static int
4053 pfm_stop(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4055 struct pt_regs *tregs;
4056 struct task_struct *task = PFM_CTX_TASK(ctx);
4057 int state, is_system;
4059 if (is_running_on_xen()) {
4060 if (is_xenoprof_primary())
4061 return HYPERVISOR_perfmon_op(PFM_STOP, NULL, 0);
4062 return 0;
4065 state = ctx->ctx_state;
4066 is_system = ctx->ctx_fl_system;
4068 /*
4069 * context must be attached to issue the stop command (includes LOADED,MASKED,ZOMBIE)
4070 */
4071 if (state == PFM_CTX_UNLOADED) return -EINVAL;
4073 /*
4074 * In system wide and when the context is loaded, access can only happen
4075 * when the caller is running on the CPU being monitored by the session.
4076 * It does not have to be the owner (ctx_task) of the context per se.
4077 */
4078 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
4079 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
4080 return -EBUSY;
4082 DPRINT(("task [%d] ctx_state=%d is_system=%d\n",
4083 PFM_CTX_TASK(ctx)->pid,
4084 state,
4085 is_system));
4086 /*
4087 * in system mode, we need to update the PMU directly
4088 * and the user level state of the caller, which may not
4089 * necessarily be the creator of the context.
4090 */
4091 if (is_system) {
4092 /*
4093 * Update local PMU first
4095 * disable dcr pp
4096 */
4097 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
4098 ia64_srlz_i();
4100 /*
4101 * update local cpuinfo
4102 */
4103 PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
4105 /*
4106 * stop monitoring, does srlz.i
4107 */
4108 pfm_clear_psr_pp();
4110 /*
4111 * stop monitoring in the caller
4112 */
4113 ia64_psr(regs)->pp = 0;
4115 return 0;
4117 /*
4118 * per-task mode
4119 */
4121 if (task == current) {
4122 /* stop monitoring at kernel level */
4123 pfm_clear_psr_up();
4125 /*
4126 * stop monitoring at the user level
4127 */
4128 ia64_psr(regs)->up = 0;
4129 } else {
4130 tregs = task_pt_regs(task);
4132 /*
4133 * stop monitoring at the user level
4134 */
4135 ia64_psr(tregs)->up = 0;
4137 /*
4138 * monitoring disabled in kernel at next reschedule
4139 */
4140 ctx->ctx_saved_psr_up = 0;
4141 DPRINT(("task=[%d]\n", task->pid));
4143 return 0;
4147 static int
4148 pfm_start(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4150 struct pt_regs *tregs;
4151 int state, is_system;
4153 if (is_running_on_xen()) {
4154 if (is_xenoprof_primary())
4155 return HYPERVISOR_perfmon_op(PFM_START, NULL, 0);
4156 return 0;
4158 state = ctx->ctx_state;
4159 is_system = ctx->ctx_fl_system;
4161 if (state != PFM_CTX_LOADED) return -EINVAL;
4163 /*
4164 * In system wide and when the context is loaded, access can only happen
4165 * when the caller is running on the CPU being monitored by the session.
4166 * It does not have to be the owner (ctx_task) of the context per se.
4167 */
4168 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
4169 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
4170 return -EBUSY;
4173 /*
4174 * in system mode, we need to update the PMU directly
4175 * and the user level state of the caller, which may not
4176 * necessarily be the creator of the context.
4177 */
4178 if (is_system) {
4180 /*
4181 * set user level psr.pp for the caller
4182 */
4183 ia64_psr(regs)->pp = 1;
4185 /*
4186 * now update the local PMU and cpuinfo
4187 */
4188 PFM_CPUINFO_SET(PFM_CPUINFO_DCR_PP);
4190 /*
4191 * start monitoring at kernel level
4192 */
4193 pfm_set_psr_pp();
4195 /* enable dcr pp */
4196 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
4197 ia64_srlz_i();
4199 return 0;
4202 /*
4203 * per-process mode
4204 */
4206 if (ctx->ctx_task == current) {
4208 /* start monitoring at kernel level */
4209 pfm_set_psr_up();
4211 /*
4212 * activate monitoring at user level
4213 */
4214 ia64_psr(regs)->up = 1;
4216 } else {
4217 tregs = task_pt_regs(ctx->ctx_task);
4219 /*
4220 * start monitoring at the kernel level the next
4221 * time the task is scheduled
4222 */
4223 ctx->ctx_saved_psr_up = IA64_PSR_UP;
4225 /*
4226 * activate monitoring at user level
4227 */
4228 ia64_psr(tregs)->up = 1;
4230 return 0;
4233 static int
4234 pfm_get_pmc_reset(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4236 pfarg_reg_t *req = (pfarg_reg_t *)arg;
4237 unsigned int cnum;
4238 int i;
4239 int ret = -EINVAL;
4240 XEN_NOT_SUPPORTED_YET;
4242 for (i = 0; i < count; i++, req++) {
4244 cnum = req->reg_num;
4246 if (!PMC_IS_IMPL(cnum)) goto abort_mission;
4248 req->reg_value = PMC_DFL_VAL(cnum);
4250 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
4252 DPRINT(("pmc_reset_val pmc[%u]=0x%lx\n", cnum, req->reg_value));
4254 return 0;
4256 abort_mission:
4257 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
4258 return ret;
4261 static int
4262 pfm_check_task_exist(pfm_context_t *ctx)
4264 struct task_struct *g, *t;
4265 int ret = -ESRCH;
4267 read_lock(&tasklist_lock);
4269 do_each_thread (g, t) {
4270 if (t->thread.pfm_context == ctx) {
4271 ret = 0;
4272 break;
4274 } while_each_thread (g, t);
4276 read_unlock(&tasklist_lock);
4278 DPRINT(("pfm_check_task_exist: ret=%d ctx=%p\n", ret, ctx));
4280 return ret;
4283 static int
4284 pfm_context_load(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4286 struct task_struct *task;
4287 struct thread_struct *thread;
4288 struct pfm_context_t *old;
4289 unsigned long flags;
4290 #ifndef CONFIG_SMP
4291 struct task_struct *owner_task = NULL;
4292 #endif
4293 pfarg_load_t *req = (pfarg_load_t *)arg;
4294 unsigned long *pmcs_source, *pmds_source;
4295 int the_cpu;
4296 int ret = 0;
4297 int state, is_system, set_dbregs = 0;
4299 if (is_running_on_xen()) {
4300 if (is_xenoprof_primary())
4301 return HYPERVISOR_perfmon_op(PFM_LOAD_CONTEXT, arg, 0);
4302 return 0;
4304 state = ctx->ctx_state;
4305 is_system = ctx->ctx_fl_system;
4306 /*
4307 * can only load from unloaded or terminated state
4308 */
4309 if (state != PFM_CTX_UNLOADED) {
4310 DPRINT(("cannot load to [%d], invalid ctx_state=%d\n",
4311 req->load_pid,
4312 ctx->ctx_state));
4313 return -EBUSY;
4316 DPRINT(("load_pid [%d] using_dbreg=%d\n", req->load_pid, ctx->ctx_fl_using_dbreg));
4318 if (CTX_OVFL_NOBLOCK(ctx) == 0 && req->load_pid == current->pid) {
4319 DPRINT(("cannot use blocking mode on self\n"));
4320 return -EINVAL;
4323 ret = pfm_get_task(ctx, req->load_pid, &task);
4324 if (ret) {
4325 DPRINT(("load_pid [%d] get_task=%d\n", req->load_pid, ret));
4326 return ret;
4329 ret = -EINVAL;
4331 /*
4332 * system wide is self monitoring only
4333 */
4334 if (is_system && task != current) {
4335 DPRINT(("system wide is self monitoring only load_pid=%d\n",
4336 req->load_pid));
4337 goto error;
4340 thread = &task->thread;
4342 ret = 0;
4343 /*
4344 * cannot load a context which is using range restrictions,
4345 * into a task that is being debugged.
4346 */
4347 if (ctx->ctx_fl_using_dbreg) {
4348 if (thread->flags & IA64_THREAD_DBG_VALID) {
4349 ret = -EBUSY;
4350 DPRINT(("load_pid [%d] task is debugged, cannot load range restrictions\n", req->load_pid));
4351 goto error;
4353 LOCK_PFS(flags);
4355 if (is_system) {
4356 if (pfm_sessions.pfs_ptrace_use_dbregs) {
4357 DPRINT(("cannot load [%d] dbregs in use\n", task->pid));
4358 ret = -EBUSY;
4359 } else {
4360 pfm_sessions.pfs_sys_use_dbregs++;
4361 DPRINT(("load [%d] increased sys_use_dbreg=%u\n", task->pid, pfm_sessions.pfs_sys_use_dbregs));
4362 set_dbregs = 1;
4366 UNLOCK_PFS(flags);
4368 if (ret) goto error;
4371 /*
4372 * SMP system-wide monitoring implies self-monitoring.
4374 * The programming model expects the task to
4375 * be pinned on a CPU throughout the session.
4376 * Here we take note of the current CPU at the
4377 * time the context is loaded. No call from
4378 * another CPU will be allowed.
4380 * The pinning via shed_setaffinity()
4381 * must be done by the calling task prior
4382 * to this call.
4384 * systemwide: keep track of CPU this session is supposed to run on
4385 */
4386 the_cpu = ctx->ctx_cpu = smp_processor_id();
4388 ret = -EBUSY;
4389 /*
4390 * now reserve the session
4391 */
4392 ret = pfm_reserve_session(current, is_system, the_cpu);
4393 if (ret) goto error;
4395 /*
4396 * task is necessarily stopped at this point.
4398 * If the previous context was zombie, then it got removed in
4399 * pfm_save_regs(). Therefore we should not see it here.
4400 * If we see a context, then this is an active context
4402 * XXX: needs to be atomic
4403 */
4404 DPRINT(("before cmpxchg() old_ctx=%p new_ctx=%p\n",
4405 thread->pfm_context, ctx));
4407 ret = -EBUSY;
4408 old = ia64_cmpxchg(acq, &thread->pfm_context, NULL, ctx, sizeof(pfm_context_t *));
4409 if (old != NULL) {
4410 DPRINT(("load_pid [%d] already has a context\n", req->load_pid));
4411 goto error_unres;
4414 pfm_reset_msgq(ctx);
4416 ctx->ctx_state = PFM_CTX_LOADED;
4418 /*
4419 * link context to task
4420 */
4421 ctx->ctx_task = task;
4423 if (is_system) {
4424 /*
4425 * we load as stopped
4426 */
4427 PFM_CPUINFO_SET(PFM_CPUINFO_SYST_WIDE);
4428 PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
4430 if (ctx->ctx_fl_excl_idle) PFM_CPUINFO_SET(PFM_CPUINFO_EXCL_IDLE);
4431 } else {
4432 thread->flags |= IA64_THREAD_PM_VALID;
4435 /*
4436 * propagate into thread-state
4437 */
4438 pfm_copy_pmds(task, ctx);
4439 pfm_copy_pmcs(task, ctx);
4441 pmcs_source = thread->pmcs;
4442 pmds_source = thread->pmds;
4444 /*
4445 * always the case for system-wide
4446 */
4447 if (task == current) {
4449 if (is_system == 0) {
4451 /* allow user level control */
4452 ia64_psr(regs)->sp = 0;
4453 DPRINT(("clearing psr.sp for [%d]\n", task->pid));
4455 SET_LAST_CPU(ctx, smp_processor_id());
4456 INC_ACTIVATION();
4457 SET_ACTIVATION(ctx);
4458 #ifndef CONFIG_SMP
4459 /*
4460 * push the other task out, if any
4461 */
4462 owner_task = GET_PMU_OWNER();
4463 if (owner_task) pfm_lazy_save_regs(owner_task);
4464 #endif
4466 /*
4467 * load all PMD from ctx to PMU (as opposed to thread state)
4468 * restore all PMC from ctx to PMU
4469 */
4470 pfm_restore_pmds(pmds_source, ctx->ctx_all_pmds[0]);
4471 pfm_restore_pmcs(pmcs_source, ctx->ctx_all_pmcs[0]);
4473 ctx->ctx_reload_pmcs[0] = 0UL;
4474 ctx->ctx_reload_pmds[0] = 0UL;
4476 /*
4477 * guaranteed safe by earlier check against DBG_VALID
4478 */
4479 if (ctx->ctx_fl_using_dbreg) {
4480 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
4481 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
4483 /*
4484 * set new ownership
4485 */
4486 SET_PMU_OWNER(task, ctx);
4488 DPRINT(("context loaded on PMU for [%d]\n", task->pid));
4489 } else {
4490 /*
4491 * when not current, task MUST be stopped, so this is safe
4492 */
4493 regs = task_pt_regs(task);
4495 /* force a full reload */
4496 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
4497 SET_LAST_CPU(ctx, -1);
4499 /* initial saved psr (stopped) */
4500 ctx->ctx_saved_psr_up = 0UL;
4501 ia64_psr(regs)->up = ia64_psr(regs)->pp = 0;
4504 ret = 0;
4506 error_unres:
4507 if (ret) pfm_unreserve_session(ctx, ctx->ctx_fl_system, the_cpu);
4508 error:
4509 /*
4510 * we must undo the dbregs setting (for system-wide)
4511 */
4512 if (ret && set_dbregs) {
4513 LOCK_PFS(flags);
4514 pfm_sessions.pfs_sys_use_dbregs--;
4515 UNLOCK_PFS(flags);
4517 /*
4518 * release task, there is now a link with the context
4519 */
4520 if (is_system == 0 && task != current) {
4521 pfm_put_task(task);
4523 if (ret == 0) {
4524 ret = pfm_check_task_exist(ctx);
4525 if (ret) {
4526 ctx->ctx_state = PFM_CTX_UNLOADED;
4527 ctx->ctx_task = NULL;
4531 return ret;
4534 /*
4535 * in this function, we do not need to increase the use count
4536 * for the task via get_task_struct(), because we hold the
4537 * context lock. If the task were to disappear while having
4538 * a context attached, it would go through pfm_exit_thread()
4539 * which also grabs the context lock and would therefore be blocked
4540 * until we are here.
4541 */
4542 static void pfm_flush_pmds(struct task_struct *, pfm_context_t *ctx);
4544 static int
4545 pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4547 struct task_struct *task = PFM_CTX_TASK(ctx);
4548 struct pt_regs *tregs;
4549 int prev_state, is_system;
4550 int ret;
4552 if (is_running_on_xen()) {
4553 if (is_xenoprof_primary())
4554 return HYPERVISOR_perfmon_op(PFM_UNLOAD_CONTEXT,
4555 NULL, 0);
4556 return 0;
4558 DPRINT(("ctx_state=%d task [%d]\n", ctx->ctx_state, task ? task->pid : -1));
4560 prev_state = ctx->ctx_state;
4561 is_system = ctx->ctx_fl_system;
4563 /*
4564 * unload only when necessary
4565 */
4566 if (prev_state == PFM_CTX_UNLOADED) {
4567 DPRINT(("ctx_state=%d, nothing to do\n", prev_state));
4568 return 0;
4571 /*
4572 * clear psr and dcr bits
4573 */
4574 ret = pfm_stop(ctx, NULL, 0, regs);
4575 if (ret) return ret;
4577 ctx->ctx_state = PFM_CTX_UNLOADED;
4579 /*
4580 * in system mode, we need to update the PMU directly
4581 * and the user level state of the caller, which may not
4582 * necessarily be the creator of the context.
4583 */
4584 if (is_system) {
4586 /*
4587 * Update cpuinfo
4589 * local PMU is taken care of in pfm_stop()
4590 */
4591 PFM_CPUINFO_CLEAR(PFM_CPUINFO_SYST_WIDE);
4592 PFM_CPUINFO_CLEAR(PFM_CPUINFO_EXCL_IDLE);
4594 /*
4595 * save PMDs in context
4596 * release ownership
4597 */
4598 pfm_flush_pmds(current, ctx);
4600 /*
4601 * at this point we are done with the PMU
4602 * so we can unreserve the resource.
4603 */
4604 if (prev_state != PFM_CTX_ZOMBIE)
4605 pfm_unreserve_session(ctx, 1 , ctx->ctx_cpu);
4607 /*
4608 * disconnect context from task
4609 */
4610 task->thread.pfm_context = NULL;
4611 /*
4612 * disconnect task from context
4613 */
4614 ctx->ctx_task = NULL;
4616 /*
4617 * There is nothing more to cleanup here.
4618 */
4619 return 0;
4622 /*
4623 * per-task mode
4624 */
4625 tregs = task == current ? regs : task_pt_regs(task);
4627 if (task == current) {
4628 /*
4629 * cancel user level control
4630 */
4631 ia64_psr(regs)->sp = 1;
4633 DPRINT(("setting psr.sp for [%d]\n", task->pid));
4635 /*
4636 * save PMDs to context
4637 * release ownership
4638 */
4639 pfm_flush_pmds(task, ctx);
4641 /*
4642 * at this point we are done with the PMU
4643 * so we can unreserve the resource.
4645 * when state was ZOMBIE, we have already unreserved.
4646 */
4647 if (prev_state != PFM_CTX_ZOMBIE)
4648 pfm_unreserve_session(ctx, 0 , ctx->ctx_cpu);
4650 /*
4651 * reset activation counter and psr
4652 */
4653 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
4654 SET_LAST_CPU(ctx, -1);
4656 /*
4657 * PMU state will not be restored
4658 */
4659 task->thread.flags &= ~IA64_THREAD_PM_VALID;
4661 /*
4662 * break links between context and task
4663 */
4664 task->thread.pfm_context = NULL;
4665 ctx->ctx_task = NULL;
4667 PFM_SET_WORK_PENDING(task, 0);
4669 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
4670 ctx->ctx_fl_can_restart = 0;
4671 ctx->ctx_fl_going_zombie = 0;
4673 DPRINT(("disconnected [%d] from context\n", task->pid));
4675 return 0;
4679 /*
4680 * called only from exit_thread(): task == current
4681 * we come here only if current has a context attached (loaded or masked)
4682 */
4683 void
4684 pfm_exit_thread(struct task_struct *task)
4686 pfm_context_t *ctx;
4687 unsigned long flags;
4688 struct pt_regs *regs = task_pt_regs(task);
4689 int ret, state;
4690 int free_ok = 0;
4692 ctx = PFM_GET_CTX(task);
4694 PROTECT_CTX(ctx, flags);
4696 DPRINT(("state=%d task [%d]\n", ctx->ctx_state, task->pid));
4698 state = ctx->ctx_state;
4699 switch(state) {
4700 case PFM_CTX_UNLOADED:
4701 /*
4702 * only comes to thios function if pfm_context is not NULL, i.e., cannot
4703 * be in unloaded state
4704 */
4705 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] ctx unloaded\n", task->pid);
4706 break;
4707 case PFM_CTX_LOADED:
4708 case PFM_CTX_MASKED:
4709 ret = pfm_context_unload(ctx, NULL, 0, regs);
4710 if (ret) {
4711 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task->pid, state, ret);
4713 DPRINT(("ctx unloaded for current state was %d\n", state));
4715 pfm_end_notify_user(ctx);
4716 break;
4717 case PFM_CTX_ZOMBIE:
4718 ret = pfm_context_unload(ctx, NULL, 0, regs);
4719 if (ret) {
4720 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task->pid, state, ret);
4722 free_ok = 1;
4723 break;
4724 default:
4725 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] unexpected state=%d\n", task->pid, state);
4726 break;
4728 UNPROTECT_CTX(ctx, flags);
4730 { u64 psr = pfm_get_psr();
4731 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
4732 BUG_ON(GET_PMU_OWNER());
4733 BUG_ON(ia64_psr(regs)->up);
4734 BUG_ON(ia64_psr(regs)->pp);
4737 /*
4738 * All memory free operations (especially for vmalloc'ed memory)
4739 * MUST be done with interrupts ENABLED.
4740 */
4741 if (free_ok) pfm_context_free(ctx);
4744 /*
4745 * functions MUST be listed in the increasing order of their index (see permfon.h)
4746 */
4747 #define PFM_CMD(name, flags, arg_count, arg_type, getsz) { name, #name, flags, arg_count, sizeof(arg_type), getsz }
4748 #define PFM_CMD_S(name, flags) { name, #name, flags, 0, 0, NULL }
4749 #define PFM_CMD_PCLRWS (PFM_CMD_FD|PFM_CMD_ARG_RW|PFM_CMD_STOP)
4750 #define PFM_CMD_PCLRW (PFM_CMD_FD|PFM_CMD_ARG_RW)
4751 #define PFM_CMD_NONE { NULL, "no-cmd", 0, 0, 0, NULL}
4753 static pfm_cmd_desc_t pfm_cmd_tab[]={
4754 /* 0 */PFM_CMD_NONE,
4755 /* 1 */PFM_CMD(pfm_write_pmcs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4756 /* 2 */PFM_CMD(pfm_write_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4757 /* 3 */PFM_CMD(pfm_read_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4758 /* 4 */PFM_CMD_S(pfm_stop, PFM_CMD_PCLRWS),
4759 /* 5 */PFM_CMD_S(pfm_start, PFM_CMD_PCLRWS),
4760 /* 6 */PFM_CMD_NONE,
4761 /* 7 */PFM_CMD_NONE,
4762 /* 8 */PFM_CMD(pfm_context_create, PFM_CMD_ARG_RW, 1, pfarg_context_t, pfm_ctx_getsize),
4763 /* 9 */PFM_CMD_NONE,
4764 /* 10 */PFM_CMD_S(pfm_restart, PFM_CMD_PCLRW),
4765 /* 11 */PFM_CMD_NONE,
4766 /* 12 */PFM_CMD(pfm_get_features, PFM_CMD_ARG_RW, 1, pfarg_features_t, NULL),
4767 /* 13 */PFM_CMD(pfm_debug, 0, 1, unsigned int, NULL),
4768 /* 14 */PFM_CMD_NONE,
4769 /* 15 */PFM_CMD(pfm_get_pmc_reset, PFM_CMD_ARG_RW, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4770 /* 16 */PFM_CMD(pfm_context_load, PFM_CMD_PCLRWS, 1, pfarg_load_t, NULL),
4771 /* 17 */PFM_CMD_S(pfm_context_unload, PFM_CMD_PCLRWS),
4772 /* 18 */PFM_CMD_NONE,
4773 /* 19 */PFM_CMD_NONE,
4774 /* 20 */PFM_CMD_NONE,
4775 /* 21 */PFM_CMD_NONE,
4776 /* 22 */PFM_CMD_NONE,
4777 /* 23 */PFM_CMD_NONE,
4778 /* 24 */PFM_CMD_NONE,
4779 /* 25 */PFM_CMD_NONE,
4780 /* 26 */PFM_CMD_NONE,
4781 /* 27 */PFM_CMD_NONE,
4782 /* 28 */PFM_CMD_NONE,
4783 /* 29 */PFM_CMD_NONE,
4784 /* 30 */PFM_CMD_NONE,
4785 /* 31 */PFM_CMD_NONE,
4786 /* 32 */PFM_CMD(pfm_write_ibrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL),
4787 /* 33 */PFM_CMD(pfm_write_dbrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL)
4788 };
4789 #define PFM_CMD_COUNT (sizeof(pfm_cmd_tab)/sizeof(pfm_cmd_desc_t))
4791 static int
4792 pfm_check_task_state(pfm_context_t *ctx, int cmd, unsigned long flags)
4794 struct task_struct *task;
4795 int state, old_state;
4797 recheck:
4798 state = ctx->ctx_state;
4799 task = ctx->ctx_task;
4801 if (task == NULL) {
4802 DPRINT(("context %d no task, state=%d\n", ctx->ctx_fd, state));
4803 return 0;
4806 DPRINT(("context %d state=%d [%d] task_state=%ld must_stop=%d\n",
4807 ctx->ctx_fd,
4808 state,
4809 task->pid,
4810 task->state, PFM_CMD_STOPPED(cmd)));
4812 /*
4813 * self-monitoring always ok.
4815 * for system-wide the caller can either be the creator of the
4816 * context (to one to which the context is attached to) OR
4817 * a task running on the same CPU as the session.
4818 */
4819 if (task == current || ctx->ctx_fl_system) return 0;
4821 /*
4822 * we are monitoring another thread
4823 */
4824 switch(state) {
4825 case PFM_CTX_UNLOADED:
4826 /*
4827 * if context is UNLOADED we are safe to go
4828 */
4829 return 0;
4830 case PFM_CTX_ZOMBIE:
4831 /*
4832 * no command can operate on a zombie context
4833 */
4834 DPRINT(("cmd %d state zombie cannot operate on context\n", cmd));
4835 return -EINVAL;
4836 case PFM_CTX_MASKED:
4837 /*
4838 * PMU state has been saved to software even though
4839 * the thread may still be running.
4840 */
4841 if (cmd != PFM_UNLOAD_CONTEXT) return 0;
4844 /*
4845 * context is LOADED or MASKED. Some commands may need to have
4846 * the task stopped.
4848 * We could lift this restriction for UP but it would mean that
4849 * the user has no guarantee the task would not run between
4850 * two successive calls to perfmonctl(). That's probably OK.
4851 * If this user wants to ensure the task does not run, then
4852 * the task must be stopped.
4853 */
4854 if (PFM_CMD_STOPPED(cmd)) {
4855 if ((task->state != TASK_STOPPED) && (task->state != TASK_TRACED)) {
4856 DPRINT(("[%d] task not in stopped state\n", task->pid));
4857 return -EBUSY;
4859 /*
4860 * task is now stopped, wait for ctxsw out
4862 * This is an interesting point in the code.
4863 * We need to unprotect the context because
4864 * the pfm_save_regs() routines needs to grab
4865 * the same lock. There are danger in doing
4866 * this because it leaves a window open for
4867 * another task to get access to the context
4868 * and possibly change its state. The one thing
4869 * that is not possible is for the context to disappear
4870 * because we are protected by the VFS layer, i.e.,
4871 * get_fd()/put_fd().
4872 */
4873 old_state = state;
4875 UNPROTECT_CTX(ctx, flags);
4877 wait_task_inactive(task);
4879 PROTECT_CTX(ctx, flags);
4881 /*
4882 * we must recheck to verify if state has changed
4883 */
4884 if (ctx->ctx_state != old_state) {
4885 DPRINT(("old_state=%d new_state=%d\n", old_state, ctx->ctx_state));
4886 goto recheck;
4889 return 0;
4892 /*
4893 * system-call entry point (must return long)
4894 */
4895 asmlinkage long
4896 sys_perfmonctl (int fd, int cmd, void __user *arg, int count)
4898 struct file *file = NULL;
4899 pfm_context_t *ctx = NULL;
4900 unsigned long flags = 0UL;
4901 void *args_k = NULL;
4902 long ret; /* will expand int return types */
4903 size_t base_sz, sz, xtra_sz = 0;
4904 int narg, completed_args = 0, call_made = 0, cmd_flags;
4905 int (*func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
4906 int (*getsize)(void *arg, size_t *sz);
4907 #define PFM_MAX_ARGSIZE 4096
4909 /*
4910 * reject any call if perfmon was disabled at initialization
4911 */
4912 if (unlikely(pmu_conf == NULL)) return -ENOSYS;
4914 if (unlikely(cmd < 0 || cmd >= PFM_CMD_COUNT)) {
4915 DPRINT(("invalid cmd=%d\n", cmd));
4916 return -EINVAL;
4919 func = pfm_cmd_tab[cmd].cmd_func;
4920 narg = pfm_cmd_tab[cmd].cmd_narg;
4921 base_sz = pfm_cmd_tab[cmd].cmd_argsize;
4922 getsize = pfm_cmd_tab[cmd].cmd_getsize;
4923 cmd_flags = pfm_cmd_tab[cmd].cmd_flags;
4925 if (unlikely(func == NULL)) {
4926 DPRINT(("invalid cmd=%d\n", cmd));
4927 return -EINVAL;
4930 DPRINT(("cmd=%s idx=%d narg=0x%x argsz=%lu count=%d\n",
4931 PFM_CMD_NAME(cmd),
4932 cmd,
4933 narg,
4934 base_sz,
4935 count));
4937 /*
4938 * check if number of arguments matches what the command expects
4939 */
4940 if (unlikely((narg == PFM_CMD_ARG_MANY && count <= 0) || (narg > 0 && narg != count)))
4941 return -EINVAL;
4943 restart_args:
4944 sz = xtra_sz + base_sz*count;
4945 /*
4946 * limit abuse to min page size
4947 */
4948 if (unlikely(sz > PFM_MAX_ARGSIZE)) {
4949 printk(KERN_ERR "perfmon: [%d] argument too big %lu\n", current->pid, sz);
4950 return -E2BIG;
4953 /*
4954 * allocate default-sized argument buffer
4955 */
4956 if (likely(count && args_k == NULL)) {
4957 args_k = kmalloc(PFM_MAX_ARGSIZE, GFP_KERNEL);
4958 if (args_k == NULL) return -ENOMEM;
4961 ret = -EFAULT;
4963 /*
4964 * copy arguments
4966 * assume sz = 0 for command without parameters
4967 */
4968 if (sz && copy_from_user(args_k, arg, sz)) {
4969 DPRINT(("cannot copy_from_user %lu bytes @%p\n", sz, arg));
4970 goto error_args;
4973 /*
4974 * check if command supports extra parameters
4975 */
4976 if (completed_args == 0 && getsize) {
4977 /*
4978 * get extra parameters size (based on main argument)
4979 */
4980 ret = (*getsize)(args_k, &xtra_sz);
4981 if (ret) goto error_args;
4983 completed_args = 1;
4985 DPRINT(("restart_args sz=%lu xtra_sz=%lu\n", sz, xtra_sz));
4987 /* retry if necessary */
4988 if (likely(xtra_sz)) goto restart_args;
4991 if (unlikely((cmd_flags & PFM_CMD_FD) == 0)) goto skip_fd;
4993 ret = -EBADF;
4995 file = fget(fd);
4996 if (unlikely(file == NULL)) {
4997 DPRINT(("invalid fd %d\n", fd));
4998 goto error_args;
5000 if (unlikely(PFM_IS_FILE(file) == 0)) {
5001 DPRINT(("fd %d not related to perfmon\n", fd));
5002 goto error_args;
5005 ctx = (pfm_context_t *)file->private_data;
5006 if (unlikely(ctx == NULL)) {
5007 DPRINT(("no context for fd %d\n", fd));
5008 goto error_args;
5010 prefetch(&ctx->ctx_state);
5012 PROTECT_CTX(ctx, flags);
5014 /*
5015 * check task is stopped
5016 */
5017 ret = pfm_check_task_state(ctx, cmd, flags);
5018 if (unlikely(ret)) goto abort_locked;
5020 skip_fd:
5021 ret = (*func)(ctx, args_k, count, task_pt_regs(current));
5023 call_made = 1;
5025 abort_locked:
5026 if (likely(ctx)) {
5027 DPRINT(("context unlocked\n"));
5028 UNPROTECT_CTX(ctx, flags);
5031 /* copy argument back to user, if needed */
5032 if (call_made && PFM_CMD_RW_ARG(cmd) && copy_to_user(arg, args_k, base_sz*count)) ret = -EFAULT;
5034 error_args:
5035 if (file)
5036 fput(file);
5038 kfree(args_k);
5040 DPRINT(("cmd=%s ret=%ld\n", PFM_CMD_NAME(cmd), ret));
5042 return ret;
5045 static void
5046 pfm_resume_after_ovfl(pfm_context_t *ctx, unsigned long ovfl_regs, struct pt_regs *regs)
5048 pfm_buffer_fmt_t *fmt = ctx->ctx_buf_fmt;
5049 pfm_ovfl_ctrl_t rst_ctrl;
5050 int state;
5051 int ret = 0;
5053 state = ctx->ctx_state;
5054 /*
5055 * Unlock sampling buffer and reset index atomically
5056 * XXX: not really needed when blocking
5057 */
5058 if (CTX_HAS_SMPL(ctx)) {
5060 rst_ctrl.bits.mask_monitoring = 0;
5061 rst_ctrl.bits.reset_ovfl_pmds = 0;
5063 if (state == PFM_CTX_LOADED)
5064 ret = pfm_buf_fmt_restart_active(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
5065 else
5066 ret = pfm_buf_fmt_restart(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
5067 } else {
5068 rst_ctrl.bits.mask_monitoring = 0;
5069 rst_ctrl.bits.reset_ovfl_pmds = 1;
5072 if (ret == 0) {
5073 if (rst_ctrl.bits.reset_ovfl_pmds) {
5074 pfm_reset_regs(ctx, &ovfl_regs, PFM_PMD_LONG_RESET);
5076 if (rst_ctrl.bits.mask_monitoring == 0) {
5077 DPRINT(("resuming monitoring\n"));
5078 if (ctx->ctx_state == PFM_CTX_MASKED) pfm_restore_monitoring(current);
5079 } else {
5080 DPRINT(("stopping monitoring\n"));
5081 //pfm_stop_monitoring(current, regs);
5083 ctx->ctx_state = PFM_CTX_LOADED;
5087 /*
5088 * context MUST BE LOCKED when calling
5089 * can only be called for current
5090 */
5091 static void
5092 pfm_context_force_terminate(pfm_context_t *ctx, struct pt_regs *regs)
5094 int ret;
5096 DPRINT(("entering for [%d]\n", current->pid));
5098 ret = pfm_context_unload(ctx, NULL, 0, regs);
5099 if (ret) {
5100 printk(KERN_ERR "pfm_context_force_terminate: [%d] unloaded failed with %d\n", current->pid, ret);
5103 /*
5104 * and wakeup controlling task, indicating we are now disconnected
5105 */
5106 wake_up_interruptible(&ctx->ctx_zombieq);
5108 /*
5109 * given that context is still locked, the controlling
5110 * task will only get access when we return from
5111 * pfm_handle_work().
5112 */
5115 static int pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds);
5116 /*
5117 * pfm_handle_work() can be called with interrupts enabled
5118 * (TIF_NEED_RESCHED) or disabled. The down_interruptible
5119 * call may sleep, therefore we must re-enable interrupts
5120 * to avoid deadlocks. It is safe to do so because this function
5121 * is called ONLY when returning to user level (PUStk=1), in which case
5122 * there is no risk of kernel stack overflow due to deep
5123 * interrupt nesting.
5124 */
5125 void
5126 pfm_handle_work(void)
5128 pfm_context_t *ctx;
5129 struct pt_regs *regs;
5130 unsigned long flags, dummy_flags;
5131 unsigned long ovfl_regs;
5132 unsigned int reason;
5133 int ret;
5135 ctx = PFM_GET_CTX(current);
5136 if (ctx == NULL) {
5137 printk(KERN_ERR "perfmon: [%d] has no PFM context\n", current->pid);
5138 return;
5141 PROTECT_CTX(ctx, flags);
5143 PFM_SET_WORK_PENDING(current, 0);
5145 pfm_clear_task_notify();
5147 regs = task_pt_regs(current);
5149 /*
5150 * extract reason for being here and clear
5151 */
5152 reason = ctx->ctx_fl_trap_reason;
5153 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
5154 ovfl_regs = ctx->ctx_ovfl_regs[0];
5156 DPRINT(("reason=%d state=%d\n", reason, ctx->ctx_state));
5158 /*
5159 * must be done before we check for simple-reset mode
5160 */
5161 if (ctx->ctx_fl_going_zombie || ctx->ctx_state == PFM_CTX_ZOMBIE) goto do_zombie;
5164 //if (CTX_OVFL_NOBLOCK(ctx)) goto skip_blocking;
5165 if (reason == PFM_TRAP_REASON_RESET) goto skip_blocking;
5167 /*
5168 * restore interrupt mask to what it was on entry.
5169 * Could be enabled/diasbled.
5170 */
5171 UNPROTECT_CTX(ctx, flags);
5173 /*
5174 * force interrupt enable because of down_interruptible()
5175 */
5176 local_irq_enable();
5178 DPRINT(("before block sleeping\n"));
5180 /*
5181 * may go through without blocking on SMP systems
5182 * if restart has been received already by the time we call down()
5183 */
5184 ret = wait_for_completion_interruptible(&ctx->ctx_restart_done);
5186 DPRINT(("after block sleeping ret=%d\n", ret));
5188 /*
5189 * lock context and mask interrupts again
5190 * We save flags into a dummy because we may have
5191 * altered interrupts mask compared to entry in this
5192 * function.
5193 */
5194 PROTECT_CTX(ctx, dummy_flags);
5196 /*
5197 * we need to read the ovfl_regs only after wake-up
5198 * because we may have had pfm_write_pmds() in between
5199 * and that can changed PMD values and therefore
5200 * ovfl_regs is reset for these new PMD values.
5201 */
5202 ovfl_regs = ctx->ctx_ovfl_regs[0];
5204 if (ctx->ctx_fl_going_zombie) {
5205 do_zombie:
5206 DPRINT(("context is zombie, bailing out\n"));
5207 pfm_context_force_terminate(ctx, regs);
5208 goto nothing_to_do;
5210 /*
5211 * in case of interruption of down() we don't restart anything
5212 */
5213 if (ret < 0) goto nothing_to_do;
5215 skip_blocking:
5216 pfm_resume_after_ovfl(ctx, ovfl_regs, regs);
5217 ctx->ctx_ovfl_regs[0] = 0UL;
5219 nothing_to_do:
5220 /*
5221 * restore flags as they were upon entry
5222 */
5223 UNPROTECT_CTX(ctx, flags);
5226 static int
5227 pfm_notify_user(pfm_context_t *ctx, pfm_msg_t *msg)
5229 if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
5230 DPRINT(("ignoring overflow notification, owner is zombie\n"));
5231 return 0;
5234 DPRINT(("waking up somebody\n"));
5236 if (msg) wake_up_interruptible(&ctx->ctx_msgq_wait);
5238 /*
5239 * safe, we are not in intr handler, nor in ctxsw when
5240 * we come here
5241 */
5242 kill_fasync (&ctx->ctx_async_queue, SIGIO, POLL_IN);
5244 return 0;
5247 static int
5248 pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds)
5250 pfm_msg_t *msg = NULL;
5252 if (ctx->ctx_fl_no_msg == 0) {
5253 msg = pfm_get_new_msg(ctx);
5254 if (msg == NULL) {
5255 printk(KERN_ERR "perfmon: pfm_ovfl_notify_user no more notification msgs\n");
5256 return -1;
5259 msg->pfm_ovfl_msg.msg_type = PFM_MSG_OVFL;
5260 msg->pfm_ovfl_msg.msg_ctx_fd = ctx->ctx_fd;
5261 msg->pfm_ovfl_msg.msg_active_set = 0;
5262 msg->pfm_ovfl_msg.msg_ovfl_pmds[0] = ovfl_pmds;
5263 msg->pfm_ovfl_msg.msg_ovfl_pmds[1] = 0UL;
5264 msg->pfm_ovfl_msg.msg_ovfl_pmds[2] = 0UL;
5265 msg->pfm_ovfl_msg.msg_ovfl_pmds[3] = 0UL;
5266 msg->pfm_ovfl_msg.msg_tstamp = 0UL;
5269 DPRINT(("ovfl msg: msg=%p no_msg=%d fd=%d ovfl_pmds=0x%lx\n",
5270 msg,
5271 ctx->ctx_fl_no_msg,
5272 ctx->ctx_fd,
5273 ovfl_pmds));
5275 return pfm_notify_user(ctx, msg);
5278 static int
5279 pfm_end_notify_user(pfm_context_t *ctx)
5281 pfm_msg_t *msg;
5283 msg = pfm_get_new_msg(ctx);
5284 if (msg == NULL) {
5285 printk(KERN_ERR "perfmon: pfm_end_notify_user no more notification msgs\n");
5286 return -1;
5288 /* no leak */
5289 memset(msg, 0, sizeof(*msg));
5291 msg->pfm_end_msg.msg_type = PFM_MSG_END;
5292 msg->pfm_end_msg.msg_ctx_fd = ctx->ctx_fd;
5293 msg->pfm_ovfl_msg.msg_tstamp = 0UL;
5295 DPRINT(("end msg: msg=%p no_msg=%d ctx_fd=%d\n",
5296 msg,
5297 ctx->ctx_fl_no_msg,
5298 ctx->ctx_fd));
5300 return pfm_notify_user(ctx, msg);
5303 /*
5304 * main overflow processing routine.
5305 * it can be called from the interrupt path or explicitely during the context switch code
5306 */
5307 static void
5308 pfm_overflow_handler(struct task_struct *task, pfm_context_t *ctx, u64 pmc0, struct pt_regs *regs)
5310 pfm_ovfl_arg_t *ovfl_arg;
5311 unsigned long mask;
5312 unsigned long old_val, ovfl_val, new_val;
5313 unsigned long ovfl_notify = 0UL, ovfl_pmds = 0UL, smpl_pmds = 0UL, reset_pmds;
5314 unsigned long tstamp;
5315 pfm_ovfl_ctrl_t ovfl_ctrl;
5316 unsigned int i, has_smpl;
5317 int must_notify = 0;
5319 if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) goto stop_monitoring;
5321 /*
5322 * sanity test. Should never happen
5323 */
5324 if (unlikely((pmc0 & 0x1) == 0)) goto sanity_check;
5326 tstamp = ia64_get_itc();
5327 mask = pmc0 >> PMU_FIRST_COUNTER;
5328 ovfl_val = pmu_conf->ovfl_val;
5329 has_smpl = CTX_HAS_SMPL(ctx);
5331 DPRINT_ovfl(("pmc0=0x%lx pid=%d iip=0x%lx, %s "
5332 "used_pmds=0x%lx\n",
5333 pmc0,
5334 task ? task->pid: -1,
5335 (regs ? regs->cr_iip : 0),
5336 CTX_OVFL_NOBLOCK(ctx) ? "nonblocking" : "blocking",
5337 ctx->ctx_used_pmds[0]));
5340 /*
5341 * first we update the virtual counters
5342 * assume there was a prior ia64_srlz_d() issued
5343 */
5344 for (i = PMU_FIRST_COUNTER; mask ; i++, mask >>= 1) {
5346 /* skip pmd which did not overflow */
5347 if ((mask & 0x1) == 0) continue;
5349 /*
5350 * Note that the pmd is not necessarily 0 at this point as qualified events
5351 * may have happened before the PMU was frozen. The residual count is not
5352 * taken into consideration here but will be with any read of the pmd via
5353 * pfm_read_pmds().
5354 */
5355 old_val = new_val = ctx->ctx_pmds[i].val;
5356 new_val += 1 + ovfl_val;
5357 ctx->ctx_pmds[i].val = new_val;
5359 /*
5360 * check for overflow condition
5361 */
5362 if (likely(old_val > new_val)) {
5363 ovfl_pmds |= 1UL << i;
5364 if (PMC_OVFL_NOTIFY(ctx, i)) ovfl_notify |= 1UL << i;
5367 DPRINT_ovfl(("ctx_pmd[%d].val=0x%lx old_val=0x%lx pmd=0x%lx ovfl_pmds=0x%lx ovfl_notify=0x%lx\n",
5368 i,
5369 new_val,
5370 old_val,
5371 ia64_get_pmd(i) & ovfl_val,
5372 ovfl_pmds,
5373 ovfl_notify));
5376 /*
5377 * there was no 64-bit overflow, nothing else to do
5378 */
5379 if (ovfl_pmds == 0UL) return;
5381 /*
5382 * reset all control bits
5383 */
5384 ovfl_ctrl.val = 0;
5385 reset_pmds = 0UL;
5387 /*
5388 * if a sampling format module exists, then we "cache" the overflow by
5389 * calling the module's handler() routine.
5390 */
5391 if (has_smpl) {
5392 unsigned long start_cycles, end_cycles;
5393 unsigned long pmd_mask;
5394 int j, k, ret = 0;
5395 int this_cpu = smp_processor_id();
5397 pmd_mask = ovfl_pmds >> PMU_FIRST_COUNTER;
5398 ovfl_arg = &ctx->ctx_ovfl_arg;
5400 prefetch(ctx->ctx_smpl_hdr);
5402 for(i=PMU_FIRST_COUNTER; pmd_mask && ret == 0; i++, pmd_mask >>=1) {
5404 mask = 1UL << i;
5406 if ((pmd_mask & 0x1) == 0) continue;
5408 ovfl_arg->ovfl_pmd = (unsigned char )i;
5409 ovfl_arg->ovfl_notify = ovfl_notify & mask ? 1 : 0;
5410 ovfl_arg->active_set = 0;
5411 ovfl_arg->ovfl_ctrl.val = 0; /* module must fill in all fields */
5412 ovfl_arg->smpl_pmds[0] = smpl_pmds = ctx->ctx_pmds[i].smpl_pmds[0];
5414 ovfl_arg->pmd_value = ctx->ctx_pmds[i].val;
5415 ovfl_arg->pmd_last_reset = ctx->ctx_pmds[i].lval;
5416 ovfl_arg->pmd_eventid = ctx->ctx_pmds[i].eventid;
5418 /*
5419 * copy values of pmds of interest. Sampling format may copy them
5420 * into sampling buffer.
5421 */
5422 if (smpl_pmds) {
5423 for(j=0, k=0; smpl_pmds; j++, smpl_pmds >>=1) {
5424 if ((smpl_pmds & 0x1) == 0) continue;
5425 ovfl_arg->smpl_pmds_values[k++] = PMD_IS_COUNTING(j) ? pfm_read_soft_counter(ctx, j) : ia64_get_pmd(j);
5426 DPRINT_ovfl(("smpl_pmd[%d]=pmd%u=0x%lx\n", k-1, j, ovfl_arg->smpl_pmds_values[k-1]));
5430 pfm_stats[this_cpu].pfm_smpl_handler_calls++;
5432 start_cycles = ia64_get_itc();
5434 /*
5435 * call custom buffer format record (handler) routine
5436 */
5437 ret = (*ctx->ctx_buf_fmt->fmt_handler)(task, ctx->ctx_smpl_hdr, ovfl_arg, regs, tstamp);
5439 end_cycles = ia64_get_itc();
5441 /*
5442 * For those controls, we take the union because they have
5443 * an all or nothing behavior.
5444 */
5445 ovfl_ctrl.bits.notify_user |= ovfl_arg->ovfl_ctrl.bits.notify_user;
5446 ovfl_ctrl.bits.block_task |= ovfl_arg->ovfl_ctrl.bits.block_task;
5447 ovfl_ctrl.bits.mask_monitoring |= ovfl_arg->ovfl_ctrl.bits.mask_monitoring;
5448 /*
5449 * build the bitmask of pmds to reset now
5450 */
5451 if (ovfl_arg->ovfl_ctrl.bits.reset_ovfl_pmds) reset_pmds |= mask;
5453 pfm_stats[this_cpu].pfm_smpl_handler_cycles += end_cycles - start_cycles;
5455 /*
5456 * when the module cannot handle the rest of the overflows, we abort right here
5457 */
5458 if (ret && pmd_mask) {
5459 DPRINT(("handler aborts leftover ovfl_pmds=0x%lx\n",
5460 pmd_mask<<PMU_FIRST_COUNTER));
5462 /*
5463 * remove the pmds we reset now from the set of pmds to reset in pfm_restart()
5464 */
5465 ovfl_pmds &= ~reset_pmds;
5466 } else {
5467 /*
5468 * when no sampling module is used, then the default
5469 * is to notify on overflow if requested by user
5470 */
5471 ovfl_ctrl.bits.notify_user = ovfl_notify ? 1 : 0;
5472 ovfl_ctrl.bits.block_task = ovfl_notify ? 1 : 0;
5473 ovfl_ctrl.bits.mask_monitoring = ovfl_notify ? 1 : 0; /* XXX: change for saturation */
5474 ovfl_ctrl.bits.reset_ovfl_pmds = ovfl_notify ? 0 : 1;
5475 /*
5476 * if needed, we reset all overflowed pmds
5477 */
5478 if (ovfl_notify == 0) reset_pmds = ovfl_pmds;
5481 DPRINT_ovfl(("ovfl_pmds=0x%lx reset_pmds=0x%lx\n", ovfl_pmds, reset_pmds));
5483 /*
5484 * reset the requested PMD registers using the short reset values
5485 */
5486 if (reset_pmds) {
5487 unsigned long bm = reset_pmds;
5488 pfm_reset_regs(ctx, &bm, PFM_PMD_SHORT_RESET);
5491 if (ovfl_notify && ovfl_ctrl.bits.notify_user) {
5492 /*
5493 * keep track of what to reset when unblocking
5494 */
5495 ctx->ctx_ovfl_regs[0] = ovfl_pmds;
5497 /*
5498 * check for blocking context
5499 */
5500 if (CTX_OVFL_NOBLOCK(ctx) == 0 && ovfl_ctrl.bits.block_task) {
5502 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_BLOCK;
5504 /*
5505 * set the perfmon specific checking pending work for the task
5506 */
5507 PFM_SET_WORK_PENDING(task, 1);
5509 /*
5510 * when coming from ctxsw, current still points to the
5511 * previous task, therefore we must work with task and not current.
5512 */
5513 pfm_set_task_notify(task);
5515 /*
5516 * defer until state is changed (shorten spin window). the context is locked
5517 * anyway, so the signal receiver would come spin for nothing.
5518 */
5519 must_notify = 1;
5522 DPRINT_ovfl(("owner [%d] pending=%ld reason=%u ovfl_pmds=0x%lx ovfl_notify=0x%lx masked=%d\n",
5523 GET_PMU_OWNER() ? GET_PMU_OWNER()->pid : -1,
5524 PFM_GET_WORK_PENDING(task),
5525 ctx->ctx_fl_trap_reason,
5526 ovfl_pmds,
5527 ovfl_notify,
5528 ovfl_ctrl.bits.mask_monitoring ? 1 : 0));
5529 /*
5530 * in case monitoring must be stopped, we toggle the psr bits
5531 */
5532 if (ovfl_ctrl.bits.mask_monitoring) {
5533 pfm_mask_monitoring(task);
5534 ctx->ctx_state = PFM_CTX_MASKED;
5535 ctx->ctx_fl_can_restart = 1;
5538 /*
5539 * send notification now
5540 */
5541 if (must_notify) pfm_ovfl_notify_user(ctx, ovfl_notify);
5543 return;
5545 sanity_check:
5546 printk(KERN_ERR "perfmon: CPU%d overflow handler [%d] pmc0=0x%lx\n",
5547 smp_processor_id(),
5548 task ? task->pid : -1,
5549 pmc0);
5550 return;
5552 stop_monitoring:
5553 /*
5554 * in SMP, zombie context is never restored but reclaimed in pfm_load_regs().
5555 * Moreover, zombies are also reclaimed in pfm_save_regs(). Therefore we can
5556 * come here as zombie only if the task is the current task. In which case, we
5557 * can access the PMU hardware directly.
5559 * Note that zombies do have PM_VALID set. So here we do the minimal.
5561 * In case the context was zombified it could not be reclaimed at the time
5562 * the monitoring program exited. At this point, the PMU reservation has been
5563 * returned, the sampiing buffer has been freed. We must convert this call
5564 * into a spurious interrupt. However, we must also avoid infinite overflows
5565 * by stopping monitoring for this task. We can only come here for a per-task
5566 * context. All we need to do is to stop monitoring using the psr bits which
5567 * are always task private. By re-enabling secure montioring, we ensure that
5568 * the monitored task will not be able to re-activate monitoring.
5569 * The task will eventually be context switched out, at which point the context
5570 * will be reclaimed (that includes releasing ownership of the PMU).
5572 * So there might be a window of time where the number of per-task session is zero
5573 * yet one PMU might have a owner and get at most one overflow interrupt for a zombie
5574 * context. This is safe because if a per-task session comes in, it will push this one
5575 * out and by the virtue on pfm_save_regs(), this one will disappear. If a system wide
5576 * session is force on that CPU, given that we use task pinning, pfm_save_regs() will
5577 * also push our zombie context out.
5579 * Overall pretty hairy stuff....
5580 */
5581 DPRINT(("ctx is zombie for [%d], converted to spurious\n", task ? task->pid: -1));
5582 pfm_clear_psr_up();
5583 ia64_psr(regs)->up = 0;
5584 ia64_psr(regs)->sp = 1;
5585 return;
5588 static int
5589 pfm_do_interrupt_handler(int irq, void *arg, struct pt_regs *regs)
5591 struct task_struct *task;
5592 pfm_context_t *ctx;
5593 unsigned long flags;
5594 u64 pmc0;
5595 int this_cpu = smp_processor_id();
5596 int retval = 0;
5598 pfm_stats[this_cpu].pfm_ovfl_intr_count++;
5600 /*
5601 * srlz.d done before arriving here
5602 */
5603 pmc0 = ia64_get_pmc(0);
5605 task = GET_PMU_OWNER();
5606 ctx = GET_PMU_CTX();
5608 /*
5609 * if we have some pending bits set
5610 * assumes : if any PMC0.bit[63-1] is set, then PMC0.fr = 1
5611 */
5612 if (PMC0_HAS_OVFL(pmc0) && task) {
5613 /*
5614 * we assume that pmc0.fr is always set here
5615 */
5617 /* sanity check */
5618 if (!ctx) goto report_spurious1;
5620 if (ctx->ctx_fl_system == 0 && (task->thread.flags & IA64_THREAD_PM_VALID) == 0)
5621 goto report_spurious2;
5623 PROTECT_CTX_NOPRINT(ctx, flags);
5625 pfm_overflow_handler(task, ctx, pmc0, regs);
5627 UNPROTECT_CTX_NOPRINT(ctx, flags);
5629 } else {
5630 pfm_stats[this_cpu].pfm_spurious_ovfl_intr_count++;
5631 retval = -1;
5633 /*
5634 * keep it unfrozen at all times
5635 */
5636 pfm_unfreeze_pmu();
5638 return retval;
5640 report_spurious1:
5641 printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d has no PFM context\n",
5642 this_cpu, task->pid);
5643 pfm_unfreeze_pmu();
5644 return -1;
5645 report_spurious2:
5646 printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d, invalid flag\n",
5647 this_cpu,
5648 task->pid);
5649 pfm_unfreeze_pmu();
5650 return -1;
5653 static irqreturn_t
5654 pfm_interrupt_handler(int irq, void *arg, struct pt_regs *regs)
5656 unsigned long start_cycles, total_cycles;
5657 unsigned long min, max;
5658 int this_cpu;
5659 int ret;
5661 this_cpu = get_cpu();
5662 if (likely(!pfm_alt_intr_handler)) {
5663 min = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min;
5664 max = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max;
5666 start_cycles = ia64_get_itc();
5668 ret = pfm_do_interrupt_handler(irq, arg, regs);
5670 total_cycles = ia64_get_itc();
5672 /*
5673 * don't measure spurious interrupts
5674 */
5675 if (likely(ret == 0)) {
5676 total_cycles -= start_cycles;
5678 if (total_cycles < min) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min = total_cycles;
5679 if (total_cycles > max) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max = total_cycles;
5681 pfm_stats[this_cpu].pfm_ovfl_intr_cycles += total_cycles;
5684 else {
5685 (*pfm_alt_intr_handler->handler)(irq, arg, regs);
5688 put_cpu_no_resched();
5689 return IRQ_HANDLED;
5692 /*
5693 * /proc/perfmon interface, for debug only
5694 */
5696 #define PFM_PROC_SHOW_HEADER ((void *)NR_CPUS+1)
5698 static void *
5699 pfm_proc_start(struct seq_file *m, loff_t *pos)
5701 if (*pos == 0) {
5702 return PFM_PROC_SHOW_HEADER;
5705 while (*pos <= NR_CPUS) {
5706 if (cpu_online(*pos - 1)) {
5707 return (void *)*pos;
5709 ++*pos;
5711 return NULL;
5714 static void *
5715 pfm_proc_next(struct seq_file *m, void *v, loff_t *pos)
5717 ++*pos;
5718 return pfm_proc_start(m, pos);
5721 static void
5722 pfm_proc_stop(struct seq_file *m, void *v)
5726 static void
5727 pfm_proc_show_header(struct seq_file *m)
5729 struct list_head * pos;
5730 pfm_buffer_fmt_t * entry;
5731 unsigned long flags;
5733 seq_printf(m,
5734 "perfmon version : %u.%u\n"
5735 "model : %s\n"
5736 "fastctxsw : %s\n"
5737 "expert mode : %s\n"
5738 "ovfl_mask : 0x%lx\n"
5739 "PMU flags : 0x%x\n",
5740 PFM_VERSION_MAJ, PFM_VERSION_MIN,
5741 pmu_conf->pmu_name,
5742 pfm_sysctl.fastctxsw > 0 ? "Yes": "No",
5743 pfm_sysctl.expert_mode > 0 ? "Yes": "No",
5744 pmu_conf->ovfl_val,
5745 pmu_conf->flags);
5747 LOCK_PFS(flags);
5749 seq_printf(m,
5750 "proc_sessions : %u\n"
5751 "sys_sessions : %u\n"
5752 "sys_use_dbregs : %u\n"
5753 "ptrace_use_dbregs : %u\n",
5754 pfm_sessions.pfs_task_sessions,
5755 pfm_sessions.pfs_sys_sessions,
5756 pfm_sessions.pfs_sys_use_dbregs,
5757 pfm_sessions.pfs_ptrace_use_dbregs);
5759 UNLOCK_PFS(flags);
5761 spin_lock(&pfm_buffer_fmt_lock);
5763 list_for_each(pos, &pfm_buffer_fmt_list) {
5764 entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
5765 seq_printf(m, "format : %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x %s\n",
5766 entry->fmt_uuid[0],
5767 entry->fmt_uuid[1],
5768 entry->fmt_uuid[2],
5769 entry->fmt_uuid[3],
5770 entry->fmt_uuid[4],
5771 entry->fmt_uuid[5],
5772 entry->fmt_uuid[6],
5773 entry->fmt_uuid[7],
5774 entry->fmt_uuid[8],
5775 entry->fmt_uuid[9],
5776 entry->fmt_uuid[10],
5777 entry->fmt_uuid[11],
5778 entry->fmt_uuid[12],
5779 entry->fmt_uuid[13],
5780 entry->fmt_uuid[14],
5781 entry->fmt_uuid[15],
5782 entry->fmt_name);
5784 spin_unlock(&pfm_buffer_fmt_lock);
5788 static int
5789 pfm_proc_show(struct seq_file *m, void *v)
5791 unsigned long psr;
5792 unsigned int i;
5793 int cpu;
5795 if (v == PFM_PROC_SHOW_HEADER) {
5796 pfm_proc_show_header(m);
5797 return 0;
5800 /* show info for CPU (v - 1) */
5802 cpu = (long)v - 1;
5803 seq_printf(m,
5804 "CPU%-2d overflow intrs : %lu\n"
5805 "CPU%-2d overflow cycles : %lu\n"
5806 "CPU%-2d overflow min : %lu\n"
5807 "CPU%-2d overflow max : %lu\n"
5808 "CPU%-2d smpl handler calls : %lu\n"
5809 "CPU%-2d smpl handler cycles : %lu\n"
5810 "CPU%-2d spurious intrs : %lu\n"
5811 "CPU%-2d replay intrs : %lu\n"
5812 "CPU%-2d syst_wide : %d\n"
5813 "CPU%-2d dcr_pp : %d\n"
5814 "CPU%-2d exclude idle : %d\n"
5815 "CPU%-2d owner : %d\n"
5816 "CPU%-2d context : %p\n"
5817 "CPU%-2d activations : %lu\n",
5818 cpu, pfm_stats[cpu].pfm_ovfl_intr_count,
5819 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles,
5820 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_min,
5821 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_max,
5822 cpu, pfm_stats[cpu].pfm_smpl_handler_calls,
5823 cpu, pfm_stats[cpu].pfm_smpl_handler_cycles,
5824 cpu, pfm_stats[cpu].pfm_spurious_ovfl_intr_count,
5825 cpu, pfm_stats[cpu].pfm_replay_ovfl_intr_count,
5826 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_SYST_WIDE ? 1 : 0,
5827 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_DCR_PP ? 1 : 0,
5828 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_EXCL_IDLE ? 1 : 0,
5829 cpu, pfm_get_cpu_data(pmu_owner, cpu) ? pfm_get_cpu_data(pmu_owner, cpu)->pid: -1,
5830 cpu, pfm_get_cpu_data(pmu_ctx, cpu),
5831 cpu, pfm_get_cpu_data(pmu_activation_number, cpu));
5833 if (num_online_cpus() == 1 && pfm_sysctl.debug > 0) {
5835 psr = pfm_get_psr();
5837 ia64_srlz_d();
5839 seq_printf(m,
5840 "CPU%-2d psr : 0x%lx\n"
5841 "CPU%-2d pmc0 : 0x%lx\n",
5842 cpu, psr,
5843 cpu, ia64_get_pmc(0));
5845 for (i=0; PMC_IS_LAST(i) == 0; i++) {
5846 if (PMC_IS_COUNTING(i) == 0) continue;
5847 seq_printf(m,
5848 "CPU%-2d pmc%u : 0x%lx\n"
5849 "CPU%-2d pmd%u : 0x%lx\n",
5850 cpu, i, ia64_get_pmc(i),
5851 cpu, i, ia64_get_pmd(i));
5854 return 0;
5857 struct seq_operations pfm_seq_ops = {
5858 .start = pfm_proc_start,
5859 .next = pfm_proc_next,
5860 .stop = pfm_proc_stop,
5861 .show = pfm_proc_show
5862 };
5864 static int
5865 pfm_proc_open(struct inode *inode, struct file *file)
5867 return seq_open(file, &pfm_seq_ops);
5871 /*
5872 * we come here as soon as local_cpu_data->pfm_syst_wide is set. this happens
5873 * during pfm_enable() hence before pfm_start(). We cannot assume monitoring
5874 * is active or inactive based on mode. We must rely on the value in
5875 * local_cpu_data->pfm_syst_info
5876 */
5877 void
5878 pfm_syst_wide_update_task(struct task_struct *task, unsigned long info, int is_ctxswin)
5880 struct pt_regs *regs;
5881 unsigned long dcr;
5882 unsigned long dcr_pp;
5884 dcr_pp = info & PFM_CPUINFO_DCR_PP ? 1 : 0;
5886 /*
5887 * pid 0 is guaranteed to be the idle task. There is one such task with pid 0
5888 * on every CPU, so we can rely on the pid to identify the idle task.
5889 */
5890 if ((info & PFM_CPUINFO_EXCL_IDLE) == 0 || task->pid) {
5891 regs = task_pt_regs(task);
5892 ia64_psr(regs)->pp = is_ctxswin ? dcr_pp : 0;
5893 return;
5895 /*
5896 * if monitoring has started
5897 */
5898 if (dcr_pp) {
5899 dcr = ia64_getreg(_IA64_REG_CR_DCR);
5900 /*
5901 * context switching in?
5902 */
5903 if (is_ctxswin) {
5904 /* mask monitoring for the idle task */
5905 ia64_setreg(_IA64_REG_CR_DCR, dcr & ~IA64_DCR_PP);
5906 pfm_clear_psr_pp();
5907 ia64_srlz_i();
5908 return;
5910 /*
5911 * context switching out
5912 * restore monitoring for next task
5914 * Due to inlining this odd if-then-else construction generates
5915 * better code.
5916 */
5917 ia64_setreg(_IA64_REG_CR_DCR, dcr |IA64_DCR_PP);
5918 pfm_set_psr_pp();
5919 ia64_srlz_i();
5923 #ifdef CONFIG_SMP
5925 static void
5926 pfm_force_cleanup(pfm_context_t *ctx, struct pt_regs *regs)
5928 struct task_struct *task = ctx->ctx_task;
5930 ia64_psr(regs)->up = 0;
5931 ia64_psr(regs)->sp = 1;
5933 if (GET_PMU_OWNER() == task) {
5934 DPRINT(("cleared ownership for [%d]\n", ctx->ctx_task->pid));
5935 SET_PMU_OWNER(NULL, NULL);
5938 /*
5939 * disconnect the task from the context and vice-versa
5940 */
5941 PFM_SET_WORK_PENDING(task, 0);
5943 task->thread.pfm_context = NULL;
5944 task->thread.flags &= ~IA64_THREAD_PM_VALID;
5946 DPRINT(("force cleanup for [%d]\n", task->pid));
5950 /*
5951 * in 2.6, interrupts are masked when we come here and the runqueue lock is held
5952 */
5953 void
5954 pfm_save_regs(struct task_struct *task)
5956 pfm_context_t *ctx;
5957 struct thread_struct *t;
5958 unsigned long flags;
5959 u64 psr;
5962 ctx = PFM_GET_CTX(task);
5963 if (ctx == NULL) return;
5964 t = &task->thread;
5966 /*
5967 * we always come here with interrupts ALREADY disabled by
5968 * the scheduler. So we simply need to protect against concurrent
5969 * access, not CPU concurrency.
5970 */
5971 flags = pfm_protect_ctx_ctxsw(ctx);
5973 if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
5974 struct pt_regs *regs = task_pt_regs(task);
5976 pfm_clear_psr_up();
5978 pfm_force_cleanup(ctx, regs);
5980 BUG_ON(ctx->ctx_smpl_hdr);
5982 pfm_unprotect_ctx_ctxsw(ctx, flags);
5984 pfm_context_free(ctx);
5985 return;
5988 /*
5989 * save current PSR: needed because we modify it
5990 */
5991 ia64_srlz_d();
5992 psr = pfm_get_psr();
5994 BUG_ON(psr & (IA64_PSR_I));
5996 /*
5997 * stop monitoring:
5998 * This is the last instruction which may generate an overflow
6000 * We do not need to set psr.sp because, it is irrelevant in kernel.
6001 * It will be restored from ipsr when going back to user level
6002 */
6003 pfm_clear_psr_up();
6005 /*
6006 * keep a copy of psr.up (for reload)
6007 */
6008 ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
6010 /*
6011 * release ownership of this PMU.
6012 * PM interrupts are masked, so nothing
6013 * can happen.
6014 */
6015 SET_PMU_OWNER(NULL, NULL);
6017 /*
6018 * we systematically save the PMD as we have no
6019 * guarantee we will be schedule at that same
6020 * CPU again.
6021 */
6022 pfm_save_pmds(t->pmds, ctx->ctx_used_pmds[0]);
6024 /*
6025 * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
6026 * we will need it on the restore path to check
6027 * for pending overflow.
6028 */
6029 t->pmcs[0] = ia64_get_pmc(0);
6031 /*
6032 * unfreeze PMU if had pending overflows
6033 */
6034 if (t->pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
6036 /*
6037 * finally, allow context access.
6038 * interrupts will still be masked after this call.
6039 */
6040 pfm_unprotect_ctx_ctxsw(ctx, flags);
6043 #else /* !CONFIG_SMP */
6044 void
6045 pfm_save_regs(struct task_struct *task)
6047 pfm_context_t *ctx;
6048 u64 psr;
6050 ctx = PFM_GET_CTX(task);
6051 if (ctx == NULL) return;
6053 /*
6054 * save current PSR: needed because we modify it
6055 */
6056 psr = pfm_get_psr();
6058 BUG_ON(psr & (IA64_PSR_I));
6060 /*
6061 * stop monitoring:
6062 * This is the last instruction which may generate an overflow
6064 * We do not need to set psr.sp because, it is irrelevant in kernel.
6065 * It will be restored from ipsr when going back to user level
6066 */
6067 pfm_clear_psr_up();
6069 /*
6070 * keep a copy of psr.up (for reload)
6071 */
6072 ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
6075 static void
6076 pfm_lazy_save_regs (struct task_struct *task)
6078 pfm_context_t *ctx;
6079 struct thread_struct *t;
6080 unsigned long flags;
6082 { u64 psr = pfm_get_psr();
6083 BUG_ON(psr & IA64_PSR_UP);
6086 ctx = PFM_GET_CTX(task);
6087 t = &task->thread;
6089 /*
6090 * we need to mask PMU overflow here to
6091 * make sure that we maintain pmc0 until
6092 * we save it. overflow interrupts are
6093 * treated as spurious if there is no
6094 * owner.
6096 * XXX: I don't think this is necessary
6097 */
6098 PROTECT_CTX(ctx,flags);
6100 /*
6101 * release ownership of this PMU.
6102 * must be done before we save the registers.
6104 * after this call any PMU interrupt is treated
6105 * as spurious.
6106 */
6107 SET_PMU_OWNER(NULL, NULL);
6109 /*
6110 * save all the pmds we use
6111 */
6112 pfm_save_pmds(t->pmds, ctx->ctx_used_pmds[0]);
6114 /*
6115 * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
6116 * it is needed to check for pended overflow
6117 * on the restore path
6118 */
6119 t->pmcs[0] = ia64_get_pmc(0);
6121 /*
6122 * unfreeze PMU if had pending overflows
6123 */
6124 if (t->pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
6126 /*
6127 * now get can unmask PMU interrupts, they will
6128 * be treated as purely spurious and we will not
6129 * lose any information
6130 */
6131 UNPROTECT_CTX(ctx,flags);
6133 #endif /* CONFIG_SMP */
6135 #ifdef CONFIG_SMP
6136 /*
6137 * in 2.6, interrupts are masked when we come here and the runqueue lock is held
6138 */
6139 void
6140 pfm_load_regs (struct task_struct *task)
6142 pfm_context_t *ctx;
6143 struct thread_struct *t;
6144 unsigned long pmc_mask = 0UL, pmd_mask = 0UL;
6145 unsigned long flags;
6146 u64 psr, psr_up;
6147 int need_irq_resend;
6149 ctx = PFM_GET_CTX(task);
6150 if (unlikely(ctx == NULL)) return;
6152 BUG_ON(GET_PMU_OWNER());
6154 t = &task->thread;
6155 /*
6156 * possible on unload
6157 */
6158 if (unlikely((t->flags & IA64_THREAD_PM_VALID) == 0)) return;
6160 /*
6161 * we always come here with interrupts ALREADY disabled by
6162 * the scheduler. So we simply need to protect against concurrent
6163 * access, not CPU concurrency.
6164 */
6165 flags = pfm_protect_ctx_ctxsw(ctx);
6166 psr = pfm_get_psr();
6168 need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
6170 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
6171 BUG_ON(psr & IA64_PSR_I);
6173 if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) {
6174 struct pt_regs *regs = task_pt_regs(task);
6176 BUG_ON(ctx->ctx_smpl_hdr);
6178 pfm_force_cleanup(ctx, regs);
6180 pfm_unprotect_ctx_ctxsw(ctx, flags);
6182 /*
6183 * this one (kmalloc'ed) is fine with interrupts disabled
6184 */
6185 pfm_context_free(ctx);
6187 return;
6190 /*
6191 * we restore ALL the debug registers to avoid picking up
6192 * stale state.
6193 */
6194 if (ctx->ctx_fl_using_dbreg) {
6195 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
6196 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
6198 /*
6199 * retrieve saved psr.up
6200 */
6201 psr_up = ctx->ctx_saved_psr_up;
6203 /*
6204 * if we were the last user of the PMU on that CPU,
6205 * then nothing to do except restore psr
6206 */
6207 if (GET_LAST_CPU(ctx) == smp_processor_id() && ctx->ctx_last_activation == GET_ACTIVATION()) {
6209 /*
6210 * retrieve partial reload masks (due to user modifications)
6211 */
6212 pmc_mask = ctx->ctx_reload_pmcs[0];
6213 pmd_mask = ctx->ctx_reload_pmds[0];
6215 } else {
6216 /*
6217 * To avoid leaking information to the user level when psr.sp=0,
6218 * we must reload ALL implemented pmds (even the ones we don't use).
6219 * In the kernel we only allow PFM_READ_PMDS on registers which
6220 * we initialized or requested (sampling) so there is no risk there.
6221 */
6222 pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
6224 /*
6225 * ALL accessible PMCs are systematically reloaded, unused registers
6226 * get their default (from pfm_reset_pmu_state()) values to avoid picking
6227 * up stale configuration.
6229 * PMC0 is never in the mask. It is always restored separately.
6230 */
6231 pmc_mask = ctx->ctx_all_pmcs[0];
6233 /*
6234 * when context is MASKED, we will restore PMC with plm=0
6235 * and PMD with stale information, but that's ok, nothing
6236 * will be captured.
6238 * XXX: optimize here
6239 */
6240 if (pmd_mask) pfm_restore_pmds(t->pmds, pmd_mask);
6241 if (pmc_mask) pfm_restore_pmcs(t->pmcs, pmc_mask);
6243 /*
6244 * check for pending overflow at the time the state
6245 * was saved.
6246 */
6247 if (unlikely(PMC0_HAS_OVFL(t->pmcs[0]))) {
6248 /*
6249 * reload pmc0 with the overflow information
6250 * On McKinley PMU, this will trigger a PMU interrupt
6251 */
6252 ia64_set_pmc(0, t->pmcs[0]);
6253 ia64_srlz_d();
6254 t->pmcs[0] = 0UL;
6256 /*
6257 * will replay the PMU interrupt
6258 */
6259 if (need_irq_resend) hw_resend_irq(NULL, IA64_PERFMON_VECTOR);
6261 pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
6264 /*
6265 * we just did a reload, so we reset the partial reload fields
6266 */
6267 ctx->ctx_reload_pmcs[0] = 0UL;
6268 ctx->ctx_reload_pmds[0] = 0UL;
6270 SET_LAST_CPU(ctx, smp_processor_id());
6272 /*
6273 * dump activation value for this PMU
6274 */
6275 INC_ACTIVATION();
6276 /*
6277 * record current activation for this context
6278 */
6279 SET_ACTIVATION(ctx);
6281 /*
6282 * establish new ownership.
6283 */
6284 SET_PMU_OWNER(task, ctx);
6286 /*
6287 * restore the psr.up bit. measurement
6288 * is active again.
6289 * no PMU interrupt can happen at this point
6290 * because we still have interrupts disabled.
6291 */
6292 if (likely(psr_up)) pfm_set_psr_up();
6294 /*
6295 * allow concurrent access to context
6296 */
6297 pfm_unprotect_ctx_ctxsw(ctx, flags);
6299 #else /* !CONFIG_SMP */
6300 /*
6301 * reload PMU state for UP kernels
6302 * in 2.5 we come here with interrupts disabled
6303 */
6304 void
6305 pfm_load_regs (struct task_struct *task)
6307 struct thread_struct *t;
6308 pfm_context_t *ctx;
6309 struct task_struct *owner;
6310 unsigned long pmd_mask, pmc_mask;
6311 u64 psr, psr_up;
6312 int need_irq_resend;
6314 owner = GET_PMU_OWNER();
6315 ctx = PFM_GET_CTX(task);
6316 t = &task->thread;
6317 psr = pfm_get_psr();
6319 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
6320 BUG_ON(psr & IA64_PSR_I);
6322 /*
6323 * we restore ALL the debug registers to avoid picking up
6324 * stale state.
6326 * This must be done even when the task is still the owner
6327 * as the registers may have been modified via ptrace()
6328 * (not perfmon) by the previous task.
6329 */
6330 if (ctx->ctx_fl_using_dbreg) {
6331 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
6332 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
6335 /*
6336 * retrieved saved psr.up
6337 */
6338 psr_up = ctx->ctx_saved_psr_up;
6339 need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
6341 /*
6342 * short path, our state is still there, just
6343 * need to restore psr and we go
6345 * we do not touch either PMC nor PMD. the psr is not touched
6346 * by the overflow_handler. So we are safe w.r.t. to interrupt
6347 * concurrency even without interrupt masking.
6348 */
6349 if (likely(owner == task)) {
6350 if (likely(psr_up)) pfm_set_psr_up();
6351 return;
6354 /*
6355 * someone else is still using the PMU, first push it out and
6356 * then we'll be able to install our stuff !
6358 * Upon return, there will be no owner for the current PMU
6359 */
6360 if (owner) pfm_lazy_save_regs(owner);
6362 /*
6363 * To avoid leaking information to the user level when psr.sp=0,
6364 * we must reload ALL implemented pmds (even the ones we don't use).
6365 * In the kernel we only allow PFM_READ_PMDS on registers which
6366 * we initialized or requested (sampling) so there is no risk there.
6367 */
6368 pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
6370 /*
6371 * ALL accessible PMCs are systematically reloaded, unused registers
6372 * get their default (from pfm_reset_pmu_state()) values to avoid picking
6373 * up stale configuration.
6375 * PMC0 is never in the mask. It is always restored separately
6376 */
6377 pmc_mask = ctx->ctx_all_pmcs[0];
6379 pfm_restore_pmds(t->pmds, pmd_mask);
6380 pfm_restore_pmcs(t->pmcs, pmc_mask);
6382 /*
6383 * check for pending overflow at the time the state
6384 * was saved.
6385 */
6386 if (unlikely(PMC0_HAS_OVFL(t->pmcs[0]))) {
6387 /*
6388 * reload pmc0 with the overflow information
6389 * On McKinley PMU, this will trigger a PMU interrupt
6390 */
6391 ia64_set_pmc(0, t->pmcs[0]);
6392 ia64_srlz_d();
6394 t->pmcs[0] = 0UL;
6396 /*
6397 * will replay the PMU interrupt
6398 */
6399 if (need_irq_resend) hw_resend_irq(NULL, IA64_PERFMON_VECTOR);
6401 pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
6404 /*
6405 * establish new ownership.
6406 */
6407 SET_PMU_OWNER(task, ctx);
6409 /*
6410 * restore the psr.up bit. measurement
6411 * is active again.
6412 * no PMU interrupt can happen at this point
6413 * because we still have interrupts disabled.
6414 */
6415 if (likely(psr_up)) pfm_set_psr_up();
6417 #endif /* CONFIG_SMP */
6419 /*
6420 * this function assumes monitoring is stopped
6421 */
6422 static void
6423 pfm_flush_pmds(struct task_struct *task, pfm_context_t *ctx)
6425 u64 pmc0;
6426 unsigned long mask2, val, pmd_val, ovfl_val;
6427 int i, can_access_pmu = 0;
6428 int is_self;
6430 /*
6431 * is the caller the task being monitored (or which initiated the
6432 * session for system wide measurements)
6433 */
6434 is_self = ctx->ctx_task == task ? 1 : 0;
6436 /*
6437 * can access PMU is task is the owner of the PMU state on the current CPU
6438 * or if we are running on the CPU bound to the context in system-wide mode
6439 * (that is not necessarily the task the context is attached to in this mode).
6440 * In system-wide we always have can_access_pmu true because a task running on an
6441 * invalid processor is flagged earlier in the call stack (see pfm_stop).
6442 */
6443 can_access_pmu = (GET_PMU_OWNER() == task) || (ctx->ctx_fl_system && ctx->ctx_cpu == smp_processor_id());
6444 if (can_access_pmu) {
6445 /*
6446 * Mark the PMU as not owned
6447 * This will cause the interrupt handler to do nothing in case an overflow
6448 * interrupt was in-flight
6449 * This also guarantees that pmc0 will contain the final state
6450 * It virtually gives us full control on overflow processing from that point
6451 * on.
6452 */
6453 SET_PMU_OWNER(NULL, NULL);
6454 DPRINT(("releasing ownership\n"));
6456 /*
6457 * read current overflow status:
6459 * we are guaranteed to read the final stable state
6460 */
6461 ia64_srlz_d();
6462 pmc0 = ia64_get_pmc(0); /* slow */
6464 /*
6465 * reset freeze bit, overflow status information destroyed
6466 */
6467 pfm_unfreeze_pmu();
6468 } else {
6469 pmc0 = task->thread.pmcs[0];
6470 /*
6471 * clear whatever overflow status bits there were
6472 */
6473 task->thread.pmcs[0] = 0;
6475 ovfl_val = pmu_conf->ovfl_val;
6476 /*
6477 * we save all the used pmds
6478 * we take care of overflows for counting PMDs
6480 * XXX: sampling situation is not taken into account here
6481 */
6482 mask2 = ctx->ctx_used_pmds[0];
6484 DPRINT(("is_self=%d ovfl_val=0x%lx mask2=0x%lx\n", is_self, ovfl_val, mask2));
6486 for (i = 0; mask2; i++, mask2>>=1) {
6488 /* skip non used pmds */
6489 if ((mask2 & 0x1) == 0) continue;
6491 /*
6492 * can access PMU always true in system wide mode
6493 */
6494 val = pmd_val = can_access_pmu ? ia64_get_pmd(i) : task->thread.pmds[i];
6496 if (PMD_IS_COUNTING(i)) {
6497 DPRINT(("[%d] pmd[%d] ctx_pmd=0x%lx hw_pmd=0x%lx\n",
6498 task->pid,
6499 i,
6500 ctx->ctx_pmds[i].val,
6501 val & ovfl_val));
6503 /*
6504 * we rebuild the full 64 bit value of the counter
6505 */
6506 val = ctx->ctx_pmds[i].val + (val & ovfl_val);
6508 /*
6509 * now everything is in ctx_pmds[] and we need
6510 * to clear the saved context from save_regs() such that
6511 * pfm_read_pmds() gets the correct value
6512 */
6513 pmd_val = 0UL;
6515 /*
6516 * take care of overflow inline
6517 */
6518 if (pmc0 & (1UL << i)) {
6519 val += 1 + ovfl_val;
6520 DPRINT(("[%d] pmd[%d] overflowed\n", task->pid, i));
6524 DPRINT(("[%d] ctx_pmd[%d]=0x%lx pmd_val=0x%lx\n", task->pid, i, val, pmd_val));
6526 if (is_self) task->thread.pmds[i] = pmd_val;
6528 ctx->ctx_pmds[i].val = val;
6532 static struct irqaction perfmon_irqaction = {
6533 .handler = pfm_interrupt_handler,
6534 .flags = SA_INTERRUPT,
6535 .name = "perfmon"
6536 };
6538 static void
6539 pfm_alt_save_pmu_state(void *data)
6541 struct pt_regs *regs;
6543 regs = task_pt_regs(current);
6545 DPRINT(("called\n"));
6547 /*
6548 * should not be necessary but
6549 * let's take not risk
6550 */
6551 pfm_clear_psr_up();
6552 pfm_clear_psr_pp();
6553 ia64_psr(regs)->pp = 0;
6555 /*
6556 * This call is required
6557 * May cause a spurious interrupt on some processors
6558 */
6559 pfm_freeze_pmu();
6561 ia64_srlz_d();
6564 void
6565 pfm_alt_restore_pmu_state(void *data)
6567 struct pt_regs *regs;
6569 regs = task_pt_regs(current);
6571 DPRINT(("called\n"));
6573 /*
6574 * put PMU back in state expected
6575 * by perfmon
6576 */
6577 pfm_clear_psr_up();
6578 pfm_clear_psr_pp();
6579 ia64_psr(regs)->pp = 0;
6581 /*
6582 * perfmon runs with PMU unfrozen at all times
6583 */
6584 pfm_unfreeze_pmu();
6586 ia64_srlz_d();
6589 int
6590 pfm_install_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
6592 int ret, i;
6593 int reserve_cpu;
6595 /* some sanity checks */
6596 if (hdl == NULL || hdl->handler == NULL) return -EINVAL;
6598 /* do the easy test first */
6599 if (pfm_alt_intr_handler) return -EBUSY;
6601 /* one at a time in the install or remove, just fail the others */
6602 if (!spin_trylock(&pfm_alt_install_check)) {
6603 return -EBUSY;
6606 /* reserve our session */
6607 for_each_online_cpu(reserve_cpu) {
6608 ret = pfm_reserve_session(NULL, 1, reserve_cpu);
6609 if (ret) goto cleanup_reserve;
6612 /* save the current system wide pmu states */
6613 ret = on_each_cpu(pfm_alt_save_pmu_state, NULL, 0, 1);
6614 if (ret) {
6615 DPRINT(("on_each_cpu() failed: %d\n", ret));
6616 goto cleanup_reserve;
6619 /* officially change to the alternate interrupt handler */
6620 pfm_alt_intr_handler = hdl;
6622 spin_unlock(&pfm_alt_install_check);
6624 return 0;
6626 cleanup_reserve:
6627 for_each_online_cpu(i) {
6628 /* don't unreserve more than we reserved */
6629 if (i >= reserve_cpu) break;
6631 pfm_unreserve_session(NULL, 1, i);
6634 spin_unlock(&pfm_alt_install_check);
6636 return ret;
6638 EXPORT_SYMBOL_GPL(pfm_install_alt_pmu_interrupt);
6640 int
6641 pfm_remove_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
6643 int i;
6644 int ret;
6646 if (hdl == NULL) return -EINVAL;
6648 /* cannot remove someone else's handler! */
6649 if (pfm_alt_intr_handler != hdl) return -EINVAL;
6651 /* one at a time in the install or remove, just fail the others */
6652 if (!spin_trylock(&pfm_alt_install_check)) {
6653 return -EBUSY;
6656 pfm_alt_intr_handler = NULL;
6658 ret = on_each_cpu(pfm_alt_restore_pmu_state, NULL, 0, 1);
6659 if (ret) {
6660 DPRINT(("on_each_cpu() failed: %d\n", ret));
6663 for_each_online_cpu(i) {
6664 pfm_unreserve_session(NULL, 1, i);
6667 spin_unlock(&pfm_alt_install_check);
6669 return 0;
6671 EXPORT_SYMBOL_GPL(pfm_remove_alt_pmu_interrupt);
6673 /*
6674 * perfmon initialization routine, called from the initcall() table
6675 */
6676 static int init_pfm_fs(void);
6678 static int __init
6679 pfm_probe_pmu(void)
6681 pmu_config_t **p;
6682 int family;
6684 family = local_cpu_data->family;
6685 p = pmu_confs;
6687 while(*p) {
6688 if ((*p)->probe) {
6689 if ((*p)->probe() == 0) goto found;
6690 } else if ((*p)->pmu_family == family || (*p)->pmu_family == 0xff) {
6691 goto found;
6693 p++;
6695 return -1;
6696 found:
6697 pmu_conf = *p;
6698 return 0;
6701 static struct file_operations pfm_proc_fops = {
6702 .open = pfm_proc_open,
6703 .read = seq_read,
6704 .llseek = seq_lseek,
6705 .release = seq_release,
6706 };
6708 int __init
6709 pfm_init(void)
6711 unsigned int n, n_counters, i;
6713 printk("perfmon: version %u.%u IRQ %u\n",
6714 PFM_VERSION_MAJ,
6715 PFM_VERSION_MIN,
6716 IA64_PERFMON_VECTOR);
6718 if (pfm_probe_pmu()) {
6719 printk(KERN_INFO "perfmon: disabled, there is no support for processor family %d\n",
6720 local_cpu_data->family);
6721 return -ENODEV;
6724 /*
6725 * compute the number of implemented PMD/PMC from the
6726 * description tables
6727 */
6728 n = 0;
6729 for (i=0; PMC_IS_LAST(i) == 0; i++) {
6730 if (PMC_IS_IMPL(i) == 0) continue;
6731 pmu_conf->impl_pmcs[i>>6] |= 1UL << (i&63);
6732 n++;
6734 pmu_conf->num_pmcs = n;
6736 n = 0; n_counters = 0;
6737 for (i=0; PMD_IS_LAST(i) == 0; i++) {
6738 if (PMD_IS_IMPL(i) == 0) continue;
6739 pmu_conf->impl_pmds[i>>6] |= 1UL << (i&63);
6740 n++;
6741 if (PMD_IS_COUNTING(i)) n_counters++;
6743 pmu_conf->num_pmds = n;
6744 pmu_conf->num_counters = n_counters;
6746 /*
6747 * sanity checks on the number of debug registers
6748 */
6749 if (pmu_conf->use_rr_dbregs) {
6750 if (pmu_conf->num_ibrs > IA64_NUM_DBG_REGS) {
6751 printk(KERN_INFO "perfmon: unsupported number of code debug registers (%u)\n", pmu_conf->num_ibrs);
6752 pmu_conf = NULL;
6753 return -1;
6755 if (pmu_conf->num_dbrs > IA64_NUM_DBG_REGS) {
6756 printk(KERN_INFO "perfmon: unsupported number of data debug registers (%u)\n", pmu_conf->num_ibrs);
6757 pmu_conf = NULL;
6758 return -1;
6762 printk("perfmon: %s PMU detected, %u PMCs, %u PMDs, %u counters (%lu bits)\n",
6763 pmu_conf->pmu_name,
6764 pmu_conf->num_pmcs,
6765 pmu_conf->num_pmds,
6766 pmu_conf->num_counters,
6767 ffz(pmu_conf->ovfl_val));
6769 /* sanity check */
6770 if (pmu_conf->num_pmds >= IA64_NUM_PMD_REGS || pmu_conf->num_pmcs >= IA64_NUM_PMC_REGS) {
6771 printk(KERN_ERR "perfmon: not enough pmc/pmd, perfmon disabled\n");
6772 pmu_conf = NULL;
6773 return -1;
6776 /*
6777 * create /proc/perfmon (mostly for debugging purposes)
6778 */
6779 perfmon_dir = create_proc_entry("perfmon", S_IRUGO, NULL);
6780 if (perfmon_dir == NULL) {
6781 printk(KERN_ERR "perfmon: cannot create /proc entry, perfmon disabled\n");
6782 pmu_conf = NULL;
6783 return -1;
6785 /*
6786 * install customized file operations for /proc/perfmon entry
6787 */
6788 perfmon_dir->proc_fops = &pfm_proc_fops;
6790 /*
6791 * create /proc/sys/kernel/perfmon (for debugging purposes)
6792 */
6793 pfm_sysctl_header = register_sysctl_table(pfm_sysctl_root, 0);
6795 /*
6796 * initialize all our spinlocks
6797 */
6798 spin_lock_init(&pfm_sessions.pfs_lock);
6799 spin_lock_init(&pfm_buffer_fmt_lock);
6801 init_pfm_fs();
6803 for(i=0; i < NR_CPUS; i++) pfm_stats[i].pfm_ovfl_intr_cycles_min = ~0UL;
6805 return 0;
6808 __initcall(pfm_init);
6810 /*
6811 * this function is called before pfm_init()
6812 */
6813 void
6814 pfm_init_percpu (void)
6816 /*
6817 * make sure no measurement is active
6818 * (may inherit programmed PMCs from EFI).
6819 */
6820 pfm_clear_psr_pp();
6821 pfm_clear_psr_up();
6823 /*
6824 * we run with the PMU not frozen at all times
6825 */
6826 pfm_unfreeze_pmu();
6828 if (smp_processor_id() == 0)
6829 register_percpu_irq(IA64_PERFMON_VECTOR, &perfmon_irqaction);
6831 ia64_setreg(_IA64_REG_CR_PMV, IA64_PERFMON_VECTOR);
6832 ia64_srlz_d();
6835 /*
6836 * used for debug purposes only
6837 */
6838 void
6839 dump_pmu_state(const char *from)
6841 struct task_struct *task;
6842 struct thread_struct *t;
6843 struct pt_regs *regs;
6844 pfm_context_t *ctx;
6845 unsigned long psr, dcr, info, flags;
6846 int i, this_cpu;
6848 local_irq_save(flags);
6850 this_cpu = smp_processor_id();
6851 regs = task_pt_regs(current);
6852 info = PFM_CPUINFO_GET();
6853 dcr = ia64_getreg(_IA64_REG_CR_DCR);
6855 if (info == 0 && ia64_psr(regs)->pp == 0 && (dcr & IA64_DCR_PP) == 0) {
6856 local_irq_restore(flags);
6857 return;
6860 printk("CPU%d from %s() current [%d] iip=0x%lx %s\n",
6861 this_cpu,
6862 from,
6863 current->pid,
6864 regs->cr_iip,
6865 current->comm);
6867 task = GET_PMU_OWNER();
6868 ctx = GET_PMU_CTX();
6870 printk("->CPU%d owner [%d] ctx=%p\n", this_cpu, task ? task->pid : -1, ctx);
6872 psr = pfm_get_psr();
6874 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",
6875 this_cpu,
6876 ia64_get_pmc(0),
6877 psr & IA64_PSR_PP ? 1 : 0,
6878 psr & IA64_PSR_UP ? 1 : 0,
6879 dcr & IA64_DCR_PP ? 1 : 0,
6880 info,
6881 ia64_psr(regs)->up,
6882 ia64_psr(regs)->pp);
6884 ia64_psr(regs)->up = 0;
6885 ia64_psr(regs)->pp = 0;
6887 t = &current->thread;
6889 for (i=1; PMC_IS_LAST(i) == 0; i++) {
6890 if (PMC_IS_IMPL(i) == 0) continue;
6891 printk("->CPU%d pmc[%d]=0x%lx thread_pmc[%d]=0x%lx\n", this_cpu, i, ia64_get_pmc(i), i, t->pmcs[i]);
6894 for (i=1; PMD_IS_LAST(i) == 0; i++) {
6895 if (PMD_IS_IMPL(i) == 0) continue;
6896 printk("->CPU%d pmd[%d]=0x%lx thread_pmd[%d]=0x%lx\n", this_cpu, i, ia64_get_pmd(i), i, t->pmds[i]);
6899 if (ctx) {
6900 printk("->CPU%d ctx_state=%d vaddr=%p addr=%p fd=%d ctx_task=[%d] saved_psr_up=0x%lx\n",
6901 this_cpu,
6902 ctx->ctx_state,
6903 ctx->ctx_smpl_vaddr,
6904 ctx->ctx_smpl_hdr,
6905 ctx->ctx_msgq_head,
6906 ctx->ctx_msgq_tail,
6907 ctx->ctx_saved_psr_up);
6909 local_irq_restore(flags);
6912 /*
6913 * called from process.c:copy_thread(). task is new child.
6914 */
6915 void
6916 pfm_inherit(struct task_struct *task, struct pt_regs *regs)
6918 struct thread_struct *thread;
6920 DPRINT(("perfmon: pfm_inherit clearing state for [%d]\n", task->pid));
6922 thread = &task->thread;
6924 /*
6925 * cut links inherited from parent (current)
6926 */
6927 thread->pfm_context = NULL;
6929 PFM_SET_WORK_PENDING(task, 0);
6931 /*
6932 * the psr bits are already set properly in copy_threads()
6933 */
6935 #else /* !CONFIG_PERFMON */
6936 asmlinkage long
6937 sys_perfmonctl (int fd, int cmd, void *arg, int count)
6939 return -ENOSYS;
6941 #endif /* CONFIG_PERFMON */