source: trunk/packages/xen-3.1/xen-3.1/xen/arch/ia64/linux-xen/perfmon.c @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

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