debuggers.hg

view xen/arch/ia64/linux-xen/perfmon.c @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children 62cf917432fa
line source
1 /*
2 * This file implements the perfmon-2 subsystem which is used
3 * to program the IA-64 Performance Monitoring Unit (PMU).
4 *
5 * The initial version of perfmon.c was written by
6 * Ganesh Venkitachalam, IBM Corp.
7 *
8 * Then it was modified for perfmon-1.x by Stephane Eranian and
9 * David Mosberger, Hewlett Packard Co.
10 *
11 * Version Perfmon-2.x is a rewrite of perfmon-1.x
12 * by Stephane Eranian, Hewlett Packard Co.
13 *
14 * Copyright (C) 1999-2005 Hewlett Packard Co
15 * Stephane Eranian <eranian@hpl.hp.com>
16 * David Mosberger-Tang <davidm@hpl.hp.com>
17 *
18 * More information about perfmon available at:
19 * http://www.hpl.hp.com/research/linux/perfmon
20 *
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 */
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>
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>
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
73 #define PMC_USER (1UL << 3)
74 #define PMC_KERNEL (1UL << 0)
75 #define PMC_XEN_AND_GUEST ((1UL << 0) | (1UL << 1) | (1UL << 2))
76 #define PMC_PRIV_MONITOR (1UL << 6)
78 #undef ia64_set_pmc
79 #define ia64_set_pmc(index, val) \
80 do { \
81 u64 __index = (index); \
82 u64 __val = (val); \
83 /* bad hack! \
84 * At this moment Linux perfmon knows only kernel and user \
85 * so that it sets only pmc.plm[0] and pmc.plm[3]. \
86 * On the other hand what we want is to sample on the whole \
87 * system. i.e. user, guest kernel and xen VMM. \
88 * Thus here we enable pmc.plm[2:1] too for generic pmc/pmd. \
89 * \
90 * But we can not do it genericly for the implementation \
91 * dependent pmc/pmd. \
92 * Probably such knowlege should be taught to the oprofiled or \
93 * the xenified perfmon. \
94 */ \
95 if (pmu_conf != NULL && PMC_IS_COUNTING(__index) && \
96 (__val & PMC_KERNEL)) \
97 __val |= PMC_XEN_AND_GUEST | PMC_PRIV_MONITOR; \
98 asm volatile ("mov pmc[%0]=%1" :: \
99 "r"(__index), "r"(__val) : "memory"); \
100 } while (0)
101 #endif
103 #ifdef CONFIG_PERFMON
104 /*
105 * perfmon context state
106 */
107 #define PFM_CTX_UNLOADED 1 /* context is not loaded onto any task */
108 #define PFM_CTX_LOADED 2 /* context is loaded onto a task */
109 #define PFM_CTX_MASKED 3 /* context is loaded but monitoring is masked due to overflow */
110 #define PFM_CTX_ZOMBIE 4 /* owner of the context is closing it */
112 #define PFM_INVALID_ACTIVATION (~0UL)
114 /*
115 * depth of message queue
116 */
117 #define PFM_MAX_MSGS 32
118 #define PFM_CTXQ_EMPTY(g) ((g)->ctx_msgq_head == (g)->ctx_msgq_tail)
120 /*
121 * type of a PMU register (bitmask).
122 * bitmask structure:
123 * bit0 : register implemented
124 * bit1 : end marker
125 * bit2-3 : reserved
126 * bit4 : pmc has pmc.pm
127 * bit5 : pmc controls a counter (has pmc.oi), pmd is used as counter
128 * bit6-7 : register type
129 * bit8-31: reserved
130 */
131 #define PFM_REG_NOTIMPL 0x0 /* not implemented at all */
132 #define PFM_REG_IMPL 0x1 /* register implemented */
133 #define PFM_REG_END 0x2 /* end marker */
134 #define PFM_REG_MONITOR (0x1<<4|PFM_REG_IMPL) /* a PMC with a pmc.pm field only */
135 #define PFM_REG_COUNTING (0x2<<4|PFM_REG_MONITOR) /* a monitor + pmc.oi+ PMD used as a counter */
136 #define PFM_REG_CONTROL (0x4<<4|PFM_REG_IMPL) /* PMU control register */
137 #define PFM_REG_CONFIG (0x8<<4|PFM_REG_IMPL) /* configuration register */
138 #define PFM_REG_BUFFER (0xc<<4|PFM_REG_IMPL) /* PMD used as buffer */
140 #define PMC_IS_LAST(i) (pmu_conf->pmc_desc[i].type & PFM_REG_END)
141 #define PMD_IS_LAST(i) (pmu_conf->pmd_desc[i].type & PFM_REG_END)
143 #define PMC_OVFL_NOTIFY(ctx, i) ((ctx)->ctx_pmds[i].flags & PFM_REGFL_OVFL_NOTIFY)
145 /* i assumed unsigned */
146 #define PMC_IS_IMPL(i) (i< PMU_MAX_PMCS && (pmu_conf->pmc_desc[i].type & PFM_REG_IMPL))
147 #define PMD_IS_IMPL(i) (i< PMU_MAX_PMDS && (pmu_conf->pmd_desc[i].type & PFM_REG_IMPL))
149 /* XXX: these assume that register i is implemented */
150 #define PMD_IS_COUNTING(i) ((pmu_conf->pmd_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
151 #define PMC_IS_COUNTING(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
152 #define PMC_IS_MONITOR(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_MONITOR) == PFM_REG_MONITOR)
153 #define PMC_IS_CONTROL(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_CONTROL) == PFM_REG_CONTROL)
155 #define PMC_DFL_VAL(i) pmu_conf->pmc_desc[i].default_value
156 #define PMC_RSVD_MASK(i) pmu_conf->pmc_desc[i].reserved_mask
157 #define PMD_PMD_DEP(i) pmu_conf->pmd_desc[i].dep_pmd[0]
158 #define PMC_PMD_DEP(i) pmu_conf->pmc_desc[i].dep_pmd[0]
160 #define PFM_NUM_IBRS IA64_NUM_DBG_REGS
161 #define PFM_NUM_DBRS IA64_NUM_DBG_REGS
163 #define CTX_OVFL_NOBLOCK(c) ((c)->ctx_fl_block == 0)
164 #define CTX_HAS_SMPL(c) ((c)->ctx_fl_is_sampling)
165 #define PFM_CTX_TASK(h) (h)->ctx_task
167 #define PMU_PMC_OI 5 /* position of pmc.oi bit */
169 /* XXX: does not support more than 64 PMDs */
170 #define CTX_USED_PMD(ctx, mask) (ctx)->ctx_used_pmds[0] |= (mask)
171 #define CTX_IS_USED_PMD(ctx, c) (((ctx)->ctx_used_pmds[0] & (1UL << (c))) != 0UL)
173 #define CTX_USED_MONITOR(ctx, mask) (ctx)->ctx_used_monitors[0] |= (mask)
175 #define CTX_USED_IBR(ctx,n) (ctx)->ctx_used_ibrs[(n)>>6] |= 1UL<< ((n) % 64)
176 #define CTX_USED_DBR(ctx,n) (ctx)->ctx_used_dbrs[(n)>>6] |= 1UL<< ((n) % 64)
177 #define CTX_USES_DBREGS(ctx) (((pfm_context_t *)(ctx))->ctx_fl_using_dbreg==1)
178 #define PFM_CODE_RR 0 /* requesting code range restriction */
179 #define PFM_DATA_RR 1 /* requestion data range restriction */
181 #define PFM_CPUINFO_CLEAR(v) pfm_get_cpu_var(pfm_syst_info) &= ~(v)
182 #define PFM_CPUINFO_SET(v) pfm_get_cpu_var(pfm_syst_info) |= (v)
183 #define PFM_CPUINFO_GET() pfm_get_cpu_var(pfm_syst_info)
185 #define RDEP(x) (1UL<<(x))
187 /*
188 * context protection macros
189 * in SMP:
190 * - we need to protect against CPU concurrency (spin_lock)
191 * - we need to protect against PMU overflow interrupts (local_irq_disable)
192 * in UP:
193 * - we need to protect against PMU overflow interrupts (local_irq_disable)
194 *
195 * spin_lock_irqsave()/spin_lock_irqrestore():
196 * in SMP: local_irq_disable + spin_lock
197 * in UP : local_irq_disable
198 *
199 * spin_lock()/spin_lock():
200 * in UP : removed automatically
201 * in SMP: protect against context accesses from other CPU. interrupts
202 * are not masked. This is useful for the PMU interrupt handler
203 * because we know we will not get PMU concurrency in that code.
204 */
205 #define PROTECT_CTX(c, f) \
206 do { \
207 DPRINT(("spinlock_irq_save ctx %p by [%d]\n", c, current->pid)); \
208 spin_lock_irqsave(&(c)->ctx_lock, f); \
209 DPRINT(("spinlocked ctx %p by [%d]\n", c, current->pid)); \
210 } while(0)
212 #define UNPROTECT_CTX(c, f) \
213 do { \
214 DPRINT(("spinlock_irq_restore ctx %p by [%d]\n", c, current->pid)); \
215 spin_unlock_irqrestore(&(c)->ctx_lock, f); \
216 } while(0)
218 #define PROTECT_CTX_NOPRINT(c, f) \
219 do { \
220 spin_lock_irqsave(&(c)->ctx_lock, f); \
221 } while(0)
224 #define UNPROTECT_CTX_NOPRINT(c, f) \
225 do { \
226 spin_unlock_irqrestore(&(c)->ctx_lock, f); \
227 } while(0)
230 #define PROTECT_CTX_NOIRQ(c) \
231 do { \
232 spin_lock(&(c)->ctx_lock); \
233 } while(0)
235 #define UNPROTECT_CTX_NOIRQ(c) \
236 do { \
237 spin_unlock(&(c)->ctx_lock); \
238 } while(0)
241 #ifdef CONFIG_SMP
243 #define GET_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)
244 #define INC_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)++
245 #define SET_ACTIVATION(c) (c)->ctx_last_activation = GET_ACTIVATION()
247 #else /* !CONFIG_SMP */
248 #define SET_ACTIVATION(t) do {} while(0)
249 #define GET_ACTIVATION(t) do {} while(0)
250 #define INC_ACTIVATION(t) do {} while(0)
251 #endif /* CONFIG_SMP */
253 #define SET_PMU_OWNER(t, c) do { pfm_get_cpu_var(pmu_owner) = (t); pfm_get_cpu_var(pmu_ctx) = (c); } while(0)
254 #define GET_PMU_OWNER() pfm_get_cpu_var(pmu_owner)
255 #define GET_PMU_CTX() pfm_get_cpu_var(pmu_ctx)
257 #define LOCK_PFS(g) spin_lock_irqsave(&pfm_sessions.pfs_lock, g)
258 #define UNLOCK_PFS(g) spin_unlock_irqrestore(&pfm_sessions.pfs_lock, g)
260 #define PFM_REG_RETFLAG_SET(flags, val) do { flags &= ~PFM_REG_RETFL_MASK; flags |= (val); } while(0)
262 /*
263 * cmp0 must be the value of pmc0
264 */
265 #define PMC0_HAS_OVFL(cmp0) (cmp0 & ~0x1UL)
267 #define PFMFS_MAGIC 0xa0b4d889
269 /*
270 * debugging
271 */
272 #define PFM_DEBUGGING 1
273 #ifdef PFM_DEBUGGING
274 #define DPRINT(a) \
275 do { \
276 if (unlikely(pfm_sysctl.debug >0)) { printk("%s.%d: CPU%d [%d] ", __FUNCTION__, __LINE__, smp_processor_id(), current->pid); printk a; } \
277 } while (0)
279 #define DPRINT_ovfl(a) \
280 do { \
281 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; } \
282 } while (0)
283 #endif
285 /*
286 * 64-bit software counter structure
287 *
288 * the next_reset_type is applied to the next call to pfm_reset_regs()
289 */
290 typedef struct {
291 unsigned long val; /* virtual 64bit counter value */
292 unsigned long lval; /* last reset value */
293 unsigned long long_reset; /* reset value on sampling overflow */
294 unsigned long short_reset; /* reset value on overflow */
295 unsigned long reset_pmds[4]; /* which other pmds to reset when this counter overflows */
296 unsigned long smpl_pmds[4]; /* which pmds are accessed when counter overflow */
297 unsigned long seed; /* seed for random-number generator */
298 unsigned long mask; /* mask for random-number generator */
299 unsigned int flags; /* notify/do not notify */
300 unsigned long eventid; /* overflow event identifier */
301 } pfm_counter_t;
303 /*
304 * context flags
305 */
306 typedef struct {
307 unsigned int block:1; /* when 1, task will blocked on user notifications */
308 unsigned int system:1; /* do system wide monitoring */
309 unsigned int using_dbreg:1; /* using range restrictions (debug registers) */
310 unsigned int is_sampling:1; /* true if using a custom format */
311 unsigned int excl_idle:1; /* exclude idle task in system wide session */
312 unsigned int going_zombie:1; /* context is zombie (MASKED+blocking) */
313 unsigned int trap_reason:2; /* reason for going into pfm_handle_work() */
314 unsigned int no_msg:1; /* no message sent on overflow */
315 unsigned int can_restart:1; /* allowed to issue a PFM_RESTART */
316 unsigned int reserved:22;
317 } pfm_context_flags_t;
319 #define PFM_TRAP_REASON_NONE 0x0 /* default value */
320 #define PFM_TRAP_REASON_BLOCK 0x1 /* we need to block on overflow */
321 #define PFM_TRAP_REASON_RESET 0x2 /* we need to reset PMDs */
324 /*
325 * perfmon context: encapsulates all the state of a monitoring session
326 */
328 typedef struct pfm_context {
329 spinlock_t ctx_lock; /* context protection */
331 pfm_context_flags_t ctx_flags; /* bitmask of flags (block reason incl.) */
332 unsigned int ctx_state; /* state: active/inactive (no bitfield) */
334 struct task_struct *ctx_task; /* task to which context is attached */
336 unsigned long ctx_ovfl_regs[4]; /* which registers overflowed (notification) */
338 #ifndef XEN
339 struct completion ctx_restart_done; /* use for blocking notification mode */
340 #endif
342 unsigned long ctx_used_pmds[4]; /* bitmask of PMD used */
343 unsigned long ctx_all_pmds[4]; /* bitmask of all accessible PMDs */
344 unsigned long ctx_reload_pmds[4]; /* bitmask of force reload PMD on ctxsw in */
346 unsigned long ctx_all_pmcs[4]; /* bitmask of all accessible PMCs */
347 unsigned long ctx_reload_pmcs[4]; /* bitmask of force reload PMC on ctxsw in */
348 unsigned long ctx_used_monitors[4]; /* bitmask of monitor PMC being used */
350 unsigned long ctx_pmcs[IA64_NUM_PMC_REGS]; /* saved copies of PMC values */
352 unsigned int ctx_used_ibrs[1]; /* bitmask of used IBR (speedup ctxsw in) */
353 unsigned int ctx_used_dbrs[1]; /* bitmask of used DBR (speedup ctxsw in) */
354 unsigned long ctx_dbrs[IA64_NUM_DBG_REGS]; /* DBR values (cache) when not loaded */
355 unsigned long ctx_ibrs[IA64_NUM_DBG_REGS]; /* IBR values (cache) when not loaded */
357 pfm_counter_t ctx_pmds[IA64_NUM_PMD_REGS]; /* software state for PMDS */
359 u64 ctx_saved_psr_up; /* only contains psr.up value */
361 unsigned long ctx_last_activation; /* context last activation number for last_cpu */
362 unsigned int ctx_last_cpu; /* CPU id of current or last CPU used (SMP only) */
363 unsigned int ctx_cpu; /* cpu to which perfmon is applied (system wide) */
365 int ctx_fd; /* file descriptor used my this context */
366 pfm_ovfl_arg_t ctx_ovfl_arg; /* argument to custom buffer format handler */
368 pfm_buffer_fmt_t *ctx_buf_fmt; /* buffer format callbacks */
369 void *ctx_smpl_hdr; /* points to sampling buffer header kernel vaddr */
370 unsigned long ctx_smpl_size; /* size of sampling buffer */
371 void *ctx_smpl_vaddr; /* user level virtual address of smpl buffer */
373 #ifndef XEN
374 wait_queue_head_t ctx_msgq_wait;
375 pfm_msg_t ctx_msgq[PFM_MAX_MSGS];
376 int ctx_msgq_head;
377 int ctx_msgq_tail;
378 struct fasync_struct *ctx_async_queue;
380 wait_queue_head_t ctx_zombieq; /* termination cleanup wait queue */
381 #endif
382 } pfm_context_t;
384 /*
385 * magic number used to verify that structure is really
386 * a perfmon context
387 */
388 #define PFM_IS_FILE(f) ((f)->f_op == &pfm_file_ops)
390 #define PFM_GET_CTX(t) ((pfm_context_t *)(t)->thread.pfm_context)
392 #ifdef CONFIG_SMP
393 #define SET_LAST_CPU(ctx, v) (ctx)->ctx_last_cpu = (v)
394 #define GET_LAST_CPU(ctx) (ctx)->ctx_last_cpu
395 #else
396 #define SET_LAST_CPU(ctx, v) do {} while(0)
397 #define GET_LAST_CPU(ctx) do {} while(0)
398 #endif
401 #define ctx_fl_block ctx_flags.block
402 #define ctx_fl_system ctx_flags.system
403 #define ctx_fl_using_dbreg ctx_flags.using_dbreg
404 #define ctx_fl_is_sampling ctx_flags.is_sampling
405 #define ctx_fl_excl_idle ctx_flags.excl_idle
406 #define ctx_fl_going_zombie ctx_flags.going_zombie
407 #define ctx_fl_trap_reason ctx_flags.trap_reason
408 #define ctx_fl_no_msg ctx_flags.no_msg
409 #define ctx_fl_can_restart ctx_flags.can_restart
411 #define PFM_SET_WORK_PENDING(t, v) do { (t)->thread.pfm_needs_checking = v; } while(0);
412 #define PFM_GET_WORK_PENDING(t) (t)->thread.pfm_needs_checking
414 /*
415 * global information about all sessions
416 * mostly used to synchronize between system wide and per-process
417 */
418 typedef struct {
419 spinlock_t pfs_lock; /* lock the structure */
421 unsigned int pfs_task_sessions; /* number of per task sessions */
422 unsigned int pfs_sys_sessions; /* number of per system wide sessions */
423 unsigned int pfs_sys_use_dbregs; /* incremented when a system wide session uses debug regs */
424 unsigned int pfs_ptrace_use_dbregs; /* incremented when a process uses debug regs */
425 struct task_struct *pfs_sys_session[NR_CPUS]; /* point to task owning a system-wide session */
426 #ifdef XEN
427 #define XENOPROF_TASK ((struct task_struct*)1)
428 #endif
429 } pfm_session_t;
431 /*
432 * information about a PMC or PMD.
433 * dep_pmd[]: a bitmask of dependent PMD registers
434 * dep_pmc[]: a bitmask of dependent PMC registers
435 */
436 typedef int (*pfm_reg_check_t)(struct task_struct *task, pfm_context_t *ctx, unsigned int cnum, unsigned long *val, struct pt_regs *regs);
437 typedef struct {
438 unsigned int type;
439 int pm_pos;
440 unsigned long default_value; /* power-on default value */
441 unsigned long reserved_mask; /* bitmask of reserved bits */
442 pfm_reg_check_t read_check;
443 pfm_reg_check_t write_check;
444 unsigned long dep_pmd[4];
445 unsigned long dep_pmc[4];
446 } pfm_reg_desc_t;
448 /* assume cnum is a valid monitor */
449 #define PMC_PM(cnum, val) (((val) >> (pmu_conf->pmc_desc[cnum].pm_pos)) & 0x1)
451 /*
452 * This structure is initialized at boot time and contains
453 * a description of the PMU main characteristics.
454 *
455 * If the probe function is defined, detection is based
456 * on its return value:
457 * - 0 means recognized PMU
458 * - anything else means not supported
459 * When the probe function is not defined, then the pmu_family field
460 * is used and it must match the host CPU family such that:
461 * - cpu->family & config->pmu_family != 0
462 */
463 typedef struct {
464 unsigned long ovfl_val; /* overflow value for counters */
466 pfm_reg_desc_t *pmc_desc; /* detailed PMC register dependencies descriptions */
467 pfm_reg_desc_t *pmd_desc; /* detailed PMD register dependencies descriptions */
469 unsigned int num_pmcs; /* number of PMCS: computed at init time */
470 unsigned int num_pmds; /* number of PMDS: computed at init time */
471 unsigned long impl_pmcs[4]; /* bitmask of implemented PMCS */
472 unsigned long impl_pmds[4]; /* bitmask of implemented PMDS */
474 char *pmu_name; /* PMU family name */
475 unsigned int pmu_family; /* cpuid family pattern used to identify pmu */
476 unsigned int flags; /* pmu specific flags */
477 unsigned int num_ibrs; /* number of IBRS: computed at init time */
478 unsigned int num_dbrs; /* number of DBRS: computed at init time */
479 unsigned int num_counters; /* PMC/PMD counting pairs : computed at init time */
480 int (*probe)(void); /* customized probe routine */
481 unsigned int use_rr_dbregs:1; /* set if debug registers used for range restriction */
482 } pmu_config_t;
483 /*
484 * PMU specific flags
485 */
486 #define PFM_PMU_IRQ_RESEND 1 /* PMU needs explicit IRQ resend */
488 /*
489 * debug register related type definitions
490 */
491 typedef struct {
492 unsigned long ibr_mask:56;
493 unsigned long ibr_plm:4;
494 unsigned long ibr_ig:3;
495 unsigned long ibr_x:1;
496 } ibr_mask_reg_t;
498 typedef struct {
499 unsigned long dbr_mask:56;
500 unsigned long dbr_plm:4;
501 unsigned long dbr_ig:2;
502 unsigned long dbr_w:1;
503 unsigned long dbr_r:1;
504 } dbr_mask_reg_t;
506 typedef union {
507 unsigned long val;
508 ibr_mask_reg_t ibr;
509 dbr_mask_reg_t dbr;
510 } dbreg_t;
513 /*
514 * perfmon command descriptions
515 */
516 typedef struct {
517 int (*cmd_func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
518 char *cmd_name;
519 int cmd_flags;
520 unsigned int cmd_narg;
521 size_t cmd_argsize;
522 int (*cmd_getsize)(void *arg, size_t *sz);
523 } pfm_cmd_desc_t;
525 #define PFM_CMD_FD 0x01 /* command requires a file descriptor */
526 #define PFM_CMD_ARG_READ 0x02 /* command must read argument(s) */
527 #define PFM_CMD_ARG_RW 0x04 /* command must read/write argument(s) */
528 #define PFM_CMD_STOP 0x08 /* command does not work on zombie context */
531 #define PFM_CMD_NAME(cmd) pfm_cmd_tab[(cmd)].cmd_name
532 #define PFM_CMD_READ_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_READ)
533 #define PFM_CMD_RW_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_RW)
534 #define PFM_CMD_USE_FD(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_FD)
535 #define PFM_CMD_STOPPED(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_STOP)
537 #define PFM_CMD_ARG_MANY -1 /* cannot be zero */
539 typedef struct {
540 unsigned long pfm_spurious_ovfl_intr_count; /* keep track of spurious ovfl interrupts */
541 unsigned long pfm_replay_ovfl_intr_count; /* keep track of replayed ovfl interrupts */
542 unsigned long pfm_ovfl_intr_count; /* keep track of ovfl interrupts */
543 unsigned long pfm_ovfl_intr_cycles; /* cycles spent processing ovfl interrupts */
544 unsigned long pfm_ovfl_intr_cycles_min; /* min cycles spent processing ovfl interrupts */
545 unsigned long pfm_ovfl_intr_cycles_max; /* max cycles spent processing ovfl interrupts */
546 unsigned long pfm_smpl_handler_calls;
547 unsigned long pfm_smpl_handler_cycles;
548 char pad[SMP_CACHE_BYTES] ____cacheline_aligned;
549 } pfm_stats_t;
551 /*
552 * perfmon internal variables
553 */
554 static pfm_stats_t pfm_stats[NR_CPUS];
555 static pfm_session_t pfm_sessions; /* global sessions information */
557 #ifndef XEN
558 static DEFINE_SPINLOCK(pfm_alt_install_check);
559 #endif
560 static pfm_intr_handler_desc_t *pfm_alt_intr_handler;
562 #ifndef XEN
563 static struct proc_dir_entry *perfmon_dir;
564 #endif
565 static pfm_uuid_t pfm_null_uuid = {0,};
567 static spinlock_t pfm_buffer_fmt_lock;
568 static LIST_HEAD(pfm_buffer_fmt_list);
570 static pmu_config_t *pmu_conf;
572 /* sysctl() controls */
573 pfm_sysctl_t pfm_sysctl;
574 EXPORT_SYMBOL(pfm_sysctl);
576 #ifndef XEN
577 static ctl_table pfm_ctl_table[]={
578 {1, "debug", &pfm_sysctl.debug, sizeof(int), 0666, NULL, &proc_dointvec, NULL,},
579 {2, "debug_ovfl", &pfm_sysctl.debug_ovfl, sizeof(int), 0666, NULL, &proc_dointvec, NULL,},
580 {3, "fastctxsw", &pfm_sysctl.fastctxsw, sizeof(int), 0600, NULL, &proc_dointvec, NULL,},
581 {4, "expert_mode", &pfm_sysctl.expert_mode, sizeof(int), 0600, NULL, &proc_dointvec, NULL,},
582 { 0, },
583 };
584 static ctl_table pfm_sysctl_dir[] = {
585 {1, "perfmon", NULL, 0, 0755, pfm_ctl_table, },
586 {0,},
587 };
588 static ctl_table pfm_sysctl_root[] = {
589 {1, "kernel", NULL, 0, 0755, pfm_sysctl_dir, },
590 {0,},
591 };
592 static struct ctl_table_header *pfm_sysctl_header;
594 static int pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
595 static int pfm_flush(struct file *filp);
596 #endif
598 #define pfm_get_cpu_var(v) __ia64_per_cpu_var(v)
599 #define pfm_get_cpu_data(a,b) per_cpu(a, b)
601 #ifndef XEN
602 static inline void
603 pfm_put_task(struct task_struct *task)
604 {
605 if (task != current) put_task_struct(task);
606 }
608 static inline void
609 pfm_set_task_notify(struct task_struct *task)
610 {
611 struct thread_info *info;
613 info = (struct thread_info *) ((char *) task + IA64_TASK_SIZE);
614 set_bit(TIF_NOTIFY_RESUME, &info->flags);
615 }
617 static inline void
618 pfm_clear_task_notify(void)
619 {
620 clear_thread_flag(TIF_NOTIFY_RESUME);
621 }
623 static inline void
624 pfm_reserve_page(unsigned long a)
625 {
626 SetPageReserved(vmalloc_to_page((void *)a));
627 }
628 static inline void
629 pfm_unreserve_page(unsigned long a)
630 {
631 ClearPageReserved(vmalloc_to_page((void*)a));
632 }
633 #endif
635 static inline unsigned long
636 pfm_protect_ctx_ctxsw(pfm_context_t *x)
637 {
638 spin_lock(&(x)->ctx_lock);
639 return 0UL;
640 }
642 static inline void
643 pfm_unprotect_ctx_ctxsw(pfm_context_t *x, unsigned long f)
644 {
645 spin_unlock(&(x)->ctx_lock);
646 }
648 #ifndef XEN
649 static inline unsigned int
650 pfm_do_munmap(struct mm_struct *mm, unsigned long addr, size_t len, int acct)
651 {
652 return do_munmap(mm, addr, len);
653 }
655 static inline unsigned long
656 pfm_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags, unsigned long exec)
657 {
658 return get_unmapped_area(file, addr, len, pgoff, flags);
659 }
662 static struct super_block *
663 pfmfs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data)
664 {
665 return get_sb_pseudo(fs_type, "pfm:", NULL, PFMFS_MAGIC);
666 }
668 static struct file_system_type pfm_fs_type = {
669 .name = "pfmfs",
670 .get_sb = pfmfs_get_sb,
671 .kill_sb = kill_anon_super,
672 };
673 #endif
675 DEFINE_PER_CPU(unsigned long, pfm_syst_info);
676 DEFINE_PER_CPU(struct task_struct *, pmu_owner);
677 DEFINE_PER_CPU(pfm_context_t *, pmu_ctx);
678 DEFINE_PER_CPU(unsigned long, pmu_activation_number);
679 #ifndef XEN
680 EXPORT_PER_CPU_SYMBOL_GPL(pfm_syst_info);
683 /* forward declaration */
684 static struct file_operations pfm_file_ops;
685 #endif
687 /*
688 * forward declarations
689 */
690 #ifndef CONFIG_SMP
691 static void pfm_lazy_save_regs (struct task_struct *ta);
692 #endif
694 void dump_pmu_state(const char *);
695 static int pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
697 #include "perfmon_itanium.h"
698 #include "perfmon_mckinley.h"
699 #include "perfmon_montecito.h"
700 #include "perfmon_generic.h"
702 static pmu_config_t *pmu_confs[]={
703 &pmu_conf_mont,
704 &pmu_conf_mck,
705 &pmu_conf_ita,
706 &pmu_conf_gen, /* must be last */
707 NULL
708 };
711 #ifndef XEN
712 static int pfm_end_notify_user(pfm_context_t *ctx);
713 #endif
715 static inline void
716 pfm_clear_psr_pp(void)
717 {
718 ia64_rsm(IA64_PSR_PP);
719 ia64_srlz_i();
720 }
722 static inline void
723 pfm_set_psr_pp(void)
724 {
725 ia64_ssm(IA64_PSR_PP);
726 ia64_srlz_i();
727 }
729 static inline void
730 pfm_clear_psr_up(void)
731 {
732 ia64_rsm(IA64_PSR_UP);
733 ia64_srlz_i();
734 }
736 static inline void
737 pfm_set_psr_up(void)
738 {
739 ia64_ssm(IA64_PSR_UP);
740 ia64_srlz_i();
741 }
743 static inline unsigned long
744 pfm_get_psr(void)
745 {
746 unsigned long tmp;
747 tmp = ia64_getreg(_IA64_REG_PSR);
748 ia64_srlz_i();
749 return tmp;
750 }
752 static inline void
753 pfm_set_psr_l(unsigned long val)
754 {
755 ia64_setreg(_IA64_REG_PSR_L, val);
756 ia64_srlz_i();
757 }
759 static inline void
760 pfm_freeze_pmu(void)
761 {
762 ia64_set_pmc(0,1UL);
763 ia64_srlz_d();
764 }
766 static inline void
767 pfm_unfreeze_pmu(void)
768 {
769 ia64_set_pmc(0,0UL);
770 ia64_srlz_d();
771 }
773 static inline void
774 pfm_restore_ibrs(unsigned long *ibrs, unsigned int nibrs)
775 {
776 int i;
778 for (i=0; i < nibrs; i++) {
779 ia64_set_ibr(i, ibrs[i]);
780 ia64_dv_serialize_instruction();
781 }
782 ia64_srlz_i();
783 }
785 static inline void
786 pfm_restore_dbrs(unsigned long *dbrs, unsigned int ndbrs)
787 {
788 int i;
790 for (i=0; i < ndbrs; i++) {
791 ia64_set_dbr(i, dbrs[i]);
792 ia64_dv_serialize_data();
793 }
794 ia64_srlz_d();
795 }
797 /*
798 * PMD[i] must be a counter. no check is made
799 */
800 static inline unsigned long
801 pfm_read_soft_counter(pfm_context_t *ctx, int i)
802 {
803 return ctx->ctx_pmds[i].val + (ia64_get_pmd(i) & pmu_conf->ovfl_val);
804 }
806 /*
807 * PMD[i] must be a counter. no check is made
808 */
809 static inline void
810 pfm_write_soft_counter(pfm_context_t *ctx, int i, unsigned long val)
811 {
812 unsigned long ovfl_val = pmu_conf->ovfl_val;
814 ctx->ctx_pmds[i].val = val & ~ovfl_val;
815 /*
816 * writing to unimplemented part is ignore, so we do not need to
817 * mask off top part
818 */
819 ia64_set_pmd(i, val & ovfl_val);
820 }
822 #ifndef XEN
823 static pfm_msg_t *
824 pfm_get_new_msg(pfm_context_t *ctx)
825 {
826 int idx, next;
828 next = (ctx->ctx_msgq_tail+1) % PFM_MAX_MSGS;
830 DPRINT(("ctx_fd=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
831 if (next == ctx->ctx_msgq_head) return NULL;
833 idx = ctx->ctx_msgq_tail;
834 ctx->ctx_msgq_tail = next;
836 DPRINT(("ctx=%p head=%d tail=%d msg=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail, idx));
838 return ctx->ctx_msgq+idx;
839 }
841 static pfm_msg_t *
842 pfm_get_next_msg(pfm_context_t *ctx)
843 {
844 pfm_msg_t *msg;
846 DPRINT(("ctx=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
848 if (PFM_CTXQ_EMPTY(ctx)) return NULL;
850 /*
851 * get oldest message
852 */
853 msg = ctx->ctx_msgq+ctx->ctx_msgq_head;
855 /*
856 * and move forward
857 */
858 ctx->ctx_msgq_head = (ctx->ctx_msgq_head+1) % PFM_MAX_MSGS;
860 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));
862 return msg;
863 }
865 static void
866 pfm_reset_msgq(pfm_context_t *ctx)
867 {
868 ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
869 DPRINT(("ctx=%p msgq reset\n", ctx));
870 }
872 static void *
873 pfm_rvmalloc(unsigned long size)
874 {
875 void *mem;
876 unsigned long addr;
878 size = PAGE_ALIGN(size);
879 mem = vmalloc(size);
880 if (mem) {
881 //printk("perfmon: CPU%d pfm_rvmalloc(%ld)=%p\n", smp_processor_id(), size, mem);
882 memset(mem, 0, size);
883 addr = (unsigned long)mem;
884 while (size > 0) {
885 pfm_reserve_page(addr);
886 addr+=PAGE_SIZE;
887 size-=PAGE_SIZE;
888 }
889 }
890 return mem;
891 }
893 static void
894 pfm_rvfree(void *mem, unsigned long size)
895 {
896 unsigned long addr;
898 if (mem) {
899 DPRINT(("freeing physical buffer @%p size=%lu\n", mem, size));
900 addr = (unsigned long) mem;
901 while ((long) size > 0) {
902 pfm_unreserve_page(addr);
903 addr+=PAGE_SIZE;
904 size-=PAGE_SIZE;
905 }
906 vfree(mem);
907 }
908 return;
909 }
910 #endif
912 static pfm_context_t *
913 pfm_context_alloc(void)
914 {
915 pfm_context_t *ctx;
917 /*
918 * allocate context descriptor
919 * must be able to free with interrupts disabled
920 */
921 ctx = kmalloc(sizeof(pfm_context_t), GFP_KERNEL);
922 if (ctx) {
923 memset(ctx, 0, sizeof(pfm_context_t));
924 DPRINT(("alloc ctx @%p\n", ctx));
925 }
926 return ctx;
927 }
929 static void
930 pfm_context_free(pfm_context_t *ctx)
931 {
932 if (ctx) {
933 DPRINT(("free ctx @%p\n", ctx));
934 kfree(ctx);
935 }
936 }
938 #ifndef XEN
939 static void
940 pfm_mask_monitoring(struct task_struct *task)
941 {
942 pfm_context_t *ctx = PFM_GET_CTX(task);
943 struct thread_struct *th = &task->thread;
944 unsigned long mask, val, ovfl_mask;
945 int i;
947 DPRINT_ovfl(("masking monitoring for [%d]\n", task->pid));
949 ovfl_mask = pmu_conf->ovfl_val;
950 /*
951 * monitoring can only be masked as a result of a valid
952 * counter overflow. In UP, it means that the PMU still
953 * has an owner. Note that the owner can be different
954 * from the current task. However the PMU state belongs
955 * to the owner.
956 * In SMP, a valid overflow only happens when task is
957 * current. Therefore if we come here, we know that
958 * the PMU state belongs to the current task, therefore
959 * we can access the live registers.
960 *
961 * So in both cases, the live register contains the owner's
962 * state. We can ONLY touch the PMU registers and NOT the PSR.
963 *
964 * As a consequence to this call, the thread->pmds[] array
965 * contains stale information which must be ignored
966 * when context is reloaded AND monitoring is active (see
967 * pfm_restart).
968 */
969 mask = ctx->ctx_used_pmds[0];
970 for (i = 0; mask; i++, mask>>=1) {
971 /* skip non used pmds */
972 if ((mask & 0x1) == 0) continue;
973 val = ia64_get_pmd(i);
975 if (PMD_IS_COUNTING(i)) {
976 /*
977 * we rebuild the full 64 bit value of the counter
978 */
979 ctx->ctx_pmds[i].val += (val & ovfl_mask);
980 } else {
981 ctx->ctx_pmds[i].val = val;
982 }
983 DPRINT_ovfl(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
984 i,
985 ctx->ctx_pmds[i].val,
986 val & ovfl_mask));
987 }
988 /*
989 * mask monitoring by setting the privilege level to 0
990 * we cannot use psr.pp/psr.up for this, it is controlled by
991 * the user
992 *
993 * if task is current, modify actual registers, otherwise modify
994 * thread save state, i.e., what will be restored in pfm_load_regs()
995 */
996 mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
997 for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
998 if ((mask & 0x1) == 0UL) continue;
999 ia64_set_pmc(i, th->pmcs[i] & ~0xfUL);
1000 th->pmcs[i] &= ~0xfUL;
1001 DPRINT_ovfl(("pmc[%d]=0x%lx\n", i, th->pmcs[i]));
1003 /*
1004 * make all of this visible
1005 */
1006 ia64_srlz_d();
1009 /*
1010 * must always be done with task == current
1012 * context must be in MASKED state when calling
1013 */
1014 static void
1015 pfm_restore_monitoring(struct task_struct *task)
1017 pfm_context_t *ctx = PFM_GET_CTX(task);
1018 struct thread_struct *th = &task->thread;
1019 unsigned long mask, ovfl_mask;
1020 unsigned long psr, val;
1021 int i, is_system;
1023 is_system = ctx->ctx_fl_system;
1024 ovfl_mask = pmu_conf->ovfl_val;
1026 if (task != current) {
1027 printk(KERN_ERR "perfmon.%d: invalid task[%d] current[%d]\n", __LINE__, task->pid, current->pid);
1028 return;
1030 if (ctx->ctx_state != PFM_CTX_MASKED) {
1031 printk(KERN_ERR "perfmon.%d: task[%d] current[%d] invalid state=%d\n", __LINE__,
1032 task->pid, current->pid, ctx->ctx_state);
1033 return;
1035 psr = pfm_get_psr();
1036 /*
1037 * monitoring is masked via the PMC.
1038 * As we restore their value, we do not want each counter to
1039 * restart right away. We stop monitoring using the PSR,
1040 * restore the PMC (and PMD) and then re-establish the psr
1041 * as it was. Note that there can be no pending overflow at
1042 * this point, because monitoring was MASKED.
1044 * system-wide session are pinned and self-monitoring
1045 */
1046 if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
1047 /* disable dcr pp */
1048 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
1049 pfm_clear_psr_pp();
1050 } else {
1051 pfm_clear_psr_up();
1053 /*
1054 * first, we restore the PMD
1055 */
1056 mask = ctx->ctx_used_pmds[0];
1057 for (i = 0; mask; i++, mask>>=1) {
1058 /* skip non used pmds */
1059 if ((mask & 0x1) == 0) continue;
1061 if (PMD_IS_COUNTING(i)) {
1062 /*
1063 * we split the 64bit value according to
1064 * counter width
1065 */
1066 val = ctx->ctx_pmds[i].val & ovfl_mask;
1067 ctx->ctx_pmds[i].val &= ~ovfl_mask;
1068 } else {
1069 val = ctx->ctx_pmds[i].val;
1071 ia64_set_pmd(i, val);
1073 DPRINT(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
1074 i,
1075 ctx->ctx_pmds[i].val,
1076 val));
1078 /*
1079 * restore the PMCs
1080 */
1081 mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
1082 for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
1083 if ((mask & 0x1) == 0UL) continue;
1084 th->pmcs[i] = ctx->ctx_pmcs[i];
1085 ia64_set_pmc(i, th->pmcs[i]);
1086 DPRINT(("[%d] pmc[%d]=0x%lx\n", task->pid, i, th->pmcs[i]));
1088 ia64_srlz_d();
1090 /*
1091 * must restore DBR/IBR because could be modified while masked
1092 * XXX: need to optimize
1093 */
1094 if (ctx->ctx_fl_using_dbreg) {
1095 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
1096 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
1099 /*
1100 * now restore PSR
1101 */
1102 if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
1103 /* enable dcr pp */
1104 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
1105 ia64_srlz_i();
1107 pfm_set_psr_l(psr);
1109 #endif
1111 static inline void
1112 pfm_save_pmds(unsigned long *pmds, unsigned long mask)
1114 int i;
1116 ia64_srlz_d();
1118 for (i=0; mask; i++, mask>>=1) {
1119 if (mask & 0x1) pmds[i] = ia64_get_pmd(i);
1123 #ifndef XEN
1124 /*
1125 * reload from thread state (used for ctxw only)
1126 */
1127 static inline void
1128 pfm_restore_pmds(unsigned long *pmds, unsigned long mask)
1130 int i;
1131 unsigned long val, ovfl_val = pmu_conf->ovfl_val;
1133 for (i=0; mask; i++, mask>>=1) {
1134 if ((mask & 0x1) == 0) continue;
1135 val = PMD_IS_COUNTING(i) ? pmds[i] & ovfl_val : pmds[i];
1136 ia64_set_pmd(i, val);
1138 ia64_srlz_d();
1141 /*
1142 * propagate PMD from context to thread-state
1143 */
1144 static inline void
1145 pfm_copy_pmds(struct task_struct *task, pfm_context_t *ctx)
1147 struct thread_struct *thread = &task->thread;
1148 unsigned long ovfl_val = pmu_conf->ovfl_val;
1149 unsigned long mask = ctx->ctx_all_pmds[0];
1150 unsigned long val;
1151 int i;
1153 DPRINT(("mask=0x%lx\n", mask));
1155 for (i=0; mask; i++, mask>>=1) {
1157 val = ctx->ctx_pmds[i].val;
1159 /*
1160 * We break up the 64 bit value into 2 pieces
1161 * the lower bits go to the machine state in the
1162 * thread (will be reloaded on ctxsw in).
1163 * The upper part stays in the soft-counter.
1164 */
1165 if (PMD_IS_COUNTING(i)) {
1166 ctx->ctx_pmds[i].val = val & ~ovfl_val;
1167 val &= ovfl_val;
1169 thread->pmds[i] = val;
1171 DPRINT(("pmd[%d]=0x%lx soft_val=0x%lx\n",
1172 i,
1173 thread->pmds[i],
1174 ctx->ctx_pmds[i].val));
1177 #else
1178 static inline void
1179 xenpfm_restore_pmds(pfm_context_t* ctx)
1181 int i;
1182 unsigned long ovfl_val = pmu_conf->ovfl_val;
1183 unsigned long mask = ctx->ctx_all_pmds[0];
1184 unsigned long val;
1186 for (i = 0; mask; i++, mask >>= 1) {
1187 if ((mask & 0x1) == 0)
1188 continue;
1190 val = ctx->ctx_pmds[i].val;
1191 /*
1192 * We break up the 64 bit value into 2 pieces
1193 * the lower bits go to the machine state in the
1194 * thread (will be reloaded on ctxsw in).
1195 * The upper part stays in the soft-counter.
1196 */
1197 if (PMD_IS_COUNTING(i)) {
1198 ctx->ctx_pmds[i].val = val & ~ovfl_val;
1199 val &= ovfl_val;
1201 ia64_set_pmd(i, val);
1203 ia64_srlz_d();
1205 #endif
1207 #ifndef XEN
1208 /*
1209 * propagate PMC from context to thread-state
1210 */
1211 static inline void
1212 pfm_copy_pmcs(struct task_struct *task, pfm_context_t *ctx)
1214 struct thread_struct *thread = &task->thread;
1215 unsigned long mask = ctx->ctx_all_pmcs[0];
1216 int i;
1218 DPRINT(("mask=0x%lx\n", mask));
1220 for (i=0; mask; i++, mask>>=1) {
1221 /* masking 0 with ovfl_val yields 0 */
1222 thread->pmcs[i] = ctx->ctx_pmcs[i];
1223 DPRINT(("pmc[%d]=0x%lx\n", i, thread->pmcs[i]));
1229 static inline void
1230 pfm_restore_pmcs(unsigned long *pmcs, unsigned long mask)
1232 int i;
1234 for (i=0; mask; i++, mask>>=1) {
1235 if ((mask & 0x1) == 0) continue;
1236 ia64_set_pmc(i, pmcs[i]);
1238 ia64_srlz_d();
1240 #else
1241 static inline void
1242 xenpfm_restore_pmcs(pfm_context_t* ctx)
1244 int i;
1245 unsigned long mask = ctx->ctx_all_pmcs[0];
1247 for (i = 0; mask; i++, mask >>= 1) {
1248 if ((mask & 0x1) == 0)
1249 continue;
1250 ia64_set_pmc(i, ctx->ctx_pmcs[i]);
1251 DPRINT(("pmc[%d]=0x%lx\n", i, ctx->ctx_pmcs[i]));
1253 ia64_srlz_d();
1256 #endif
1258 static inline int
1259 pfm_uuid_cmp(pfm_uuid_t a, pfm_uuid_t b)
1261 return memcmp(a, b, sizeof(pfm_uuid_t));
1264 static inline int
1265 pfm_buf_fmt_exit(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, struct pt_regs *regs)
1267 int ret = 0;
1268 if (fmt->fmt_exit) ret = (*fmt->fmt_exit)(task, buf, regs);
1269 return ret;
1272 static inline int
1273 pfm_buf_fmt_getsize(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags, int cpu, void *arg, unsigned long *size)
1275 int ret = 0;
1276 if (fmt->fmt_getsize) ret = (*fmt->fmt_getsize)(task, flags, cpu, arg, size);
1277 return ret;
1281 static inline int
1282 pfm_buf_fmt_validate(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags,
1283 int cpu, void *arg)
1285 int ret = 0;
1286 if (fmt->fmt_validate) ret = (*fmt->fmt_validate)(task, flags, cpu, arg);
1287 return ret;
1290 static inline int
1291 pfm_buf_fmt_init(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, unsigned int flags,
1292 int cpu, void *arg)
1294 int ret = 0;
1295 if (fmt->fmt_init) ret = (*fmt->fmt_init)(task, buf, flags, cpu, arg);
1296 return ret;
1299 static inline int
1300 pfm_buf_fmt_restart(pfm_buffer_fmt_t *fmt, struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
1302 int ret = 0;
1303 if (fmt->fmt_restart) ret = (*fmt->fmt_restart)(task, ctrl, buf, regs);
1304 return ret;
1307 static inline int
1308 pfm_buf_fmt_restart_active(pfm_buffer_fmt_t *fmt, struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
1310 int ret = 0;
1311 if (fmt->fmt_restart_active) ret = (*fmt->fmt_restart_active)(task, ctrl, buf, regs);
1312 return ret;
1315 static pfm_buffer_fmt_t *
1316 __pfm_find_buffer_fmt(pfm_uuid_t uuid)
1318 struct list_head * pos;
1319 pfm_buffer_fmt_t * entry;
1321 list_for_each(pos, &pfm_buffer_fmt_list) {
1322 entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
1323 if (pfm_uuid_cmp(uuid, entry->fmt_uuid) == 0)
1324 return entry;
1326 return NULL;
1329 /*
1330 * find a buffer format based on its uuid
1331 */
1332 static pfm_buffer_fmt_t *
1333 pfm_find_buffer_fmt(pfm_uuid_t uuid)
1335 pfm_buffer_fmt_t * fmt;
1336 spin_lock(&pfm_buffer_fmt_lock);
1337 fmt = __pfm_find_buffer_fmt(uuid);
1338 spin_unlock(&pfm_buffer_fmt_lock);
1339 return fmt;
1342 int
1343 pfm_register_buffer_fmt(pfm_buffer_fmt_t *fmt)
1345 int ret = 0;
1347 /* some sanity checks */
1348 if (fmt == NULL || fmt->fmt_name == NULL) return -EINVAL;
1350 /* we need at least a handler */
1351 if (fmt->fmt_handler == NULL) return -EINVAL;
1353 /*
1354 * XXX: need check validity of fmt_arg_size
1355 */
1357 spin_lock(&pfm_buffer_fmt_lock);
1359 if (__pfm_find_buffer_fmt(fmt->fmt_uuid)) {
1360 printk(KERN_ERR "perfmon: duplicate sampling format: %s\n", fmt->fmt_name);
1361 ret = -EBUSY;
1362 goto out;
1364 list_add(&fmt->fmt_list, &pfm_buffer_fmt_list);
1365 printk(KERN_INFO "perfmon: added sampling format %s\n", fmt->fmt_name);
1367 out:
1368 spin_unlock(&pfm_buffer_fmt_lock);
1369 return ret;
1371 EXPORT_SYMBOL(pfm_register_buffer_fmt);
1373 int
1374 pfm_unregister_buffer_fmt(pfm_uuid_t uuid)
1376 pfm_buffer_fmt_t *fmt;
1377 int ret = 0;
1379 spin_lock(&pfm_buffer_fmt_lock);
1381 fmt = __pfm_find_buffer_fmt(uuid);
1382 if (!fmt) {
1383 printk(KERN_ERR "perfmon: cannot unregister format, not found\n");
1384 ret = -EINVAL;
1385 goto out;
1387 list_del_init(&fmt->fmt_list);
1388 printk(KERN_INFO "perfmon: removed sampling format: %s\n", fmt->fmt_name);
1390 out:
1391 spin_unlock(&pfm_buffer_fmt_lock);
1392 return ret;
1395 EXPORT_SYMBOL(pfm_unregister_buffer_fmt);
1397 extern void update_pal_halt_status(int);
1399 static int
1400 pfm_reserve_session(struct task_struct *task, int is_syswide, unsigned int cpu)
1402 unsigned long flags;
1403 /*
1404 * validy checks on cpu_mask have been done upstream
1405 */
1406 LOCK_PFS(flags);
1408 DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1409 pfm_sessions.pfs_sys_sessions,
1410 pfm_sessions.pfs_task_sessions,
1411 pfm_sessions.pfs_sys_use_dbregs,
1412 is_syswide,
1413 cpu));
1415 if (is_syswide) {
1416 /*
1417 * cannot mix system wide and per-task sessions
1418 */
1419 if (pfm_sessions.pfs_task_sessions > 0UL) {
1420 DPRINT(("system wide not possible, %u conflicting task_sessions\n",
1421 pfm_sessions.pfs_task_sessions));
1422 goto abort;
1425 if (pfm_sessions.pfs_sys_session[cpu]) goto error_conflict;
1427 DPRINT(("reserving system wide session on CPU%u currently on CPU%u\n", cpu, smp_processor_id()));
1429 #ifndef XEN
1430 pfm_sessions.pfs_sys_session[cpu] = task;
1431 #else
1432 pfm_sessions.pfs_sys_session[cpu] = XENOPROF_TASK;
1433 #endif
1435 pfm_sessions.pfs_sys_sessions++ ;
1437 } else {
1438 if (pfm_sessions.pfs_sys_sessions) goto abort;
1439 pfm_sessions.pfs_task_sessions++;
1442 DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1443 pfm_sessions.pfs_sys_sessions,
1444 pfm_sessions.pfs_task_sessions,
1445 pfm_sessions.pfs_sys_use_dbregs,
1446 is_syswide,
1447 cpu));
1449 /*
1450 * disable default_idle() to go to PAL_HALT
1451 */
1452 update_pal_halt_status(0);
1454 UNLOCK_PFS(flags);
1456 return 0;
1458 error_conflict:
1459 DPRINT(("system wide not possible, conflicting session [%d] on CPU%d\n",
1460 #ifndef XEN
1461 pfm_sessions.pfs_sys_session[cpu]->pid,
1462 #else
1463 -1,
1464 #endif
1465 cpu));
1466 abort:
1467 UNLOCK_PFS(flags);
1469 return -EBUSY;
1473 static int
1474 pfm_unreserve_session(pfm_context_t *ctx, int is_syswide, unsigned int cpu)
1476 unsigned long flags;
1477 /*
1478 * validy checks on cpu_mask have been done upstream
1479 */
1480 LOCK_PFS(flags);
1482 DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1483 pfm_sessions.pfs_sys_sessions,
1484 pfm_sessions.pfs_task_sessions,
1485 pfm_sessions.pfs_sys_use_dbregs,
1486 is_syswide,
1487 cpu));
1490 if (is_syswide) {
1491 pfm_sessions.pfs_sys_session[cpu] = NULL;
1492 /*
1493 * would not work with perfmon+more than one bit in cpu_mask
1494 */
1495 if (ctx && ctx->ctx_fl_using_dbreg) {
1496 if (pfm_sessions.pfs_sys_use_dbregs == 0) {
1497 printk(KERN_ERR "perfmon: invalid release for ctx %p sys_use_dbregs=0\n", ctx);
1498 } else {
1499 pfm_sessions.pfs_sys_use_dbregs--;
1502 pfm_sessions.pfs_sys_sessions--;
1503 } else {
1504 pfm_sessions.pfs_task_sessions--;
1506 DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
1507 pfm_sessions.pfs_sys_sessions,
1508 pfm_sessions.pfs_task_sessions,
1509 pfm_sessions.pfs_sys_use_dbregs,
1510 is_syswide,
1511 cpu));
1513 /*
1514 * if possible, enable default_idle() to go into PAL_HALT
1515 */
1516 if (pfm_sessions.pfs_task_sessions == 0 && pfm_sessions.pfs_sys_sessions == 0)
1517 update_pal_halt_status(1);
1519 UNLOCK_PFS(flags);
1521 return 0;
1524 #ifndef XEN
1525 /*
1526 * removes virtual mapping of the sampling buffer.
1527 * IMPORTANT: cannot be called with interrupts disable, e.g. inside
1528 * a PROTECT_CTX() section.
1529 */
1530 static int
1531 pfm_remove_smpl_mapping(struct task_struct *task, void *vaddr, unsigned long size)
1533 int r;
1535 /* sanity checks */
1536 if (task->mm == NULL || size == 0UL || vaddr == NULL) {
1537 printk(KERN_ERR "perfmon: pfm_remove_smpl_mapping [%d] invalid context mm=%p\n", task->pid, task->mm);
1538 return -EINVAL;
1541 DPRINT(("smpl_vaddr=%p size=%lu\n", vaddr, size));
1543 /*
1544 * does the actual unmapping
1545 */
1546 down_write(&task->mm->mmap_sem);
1548 DPRINT(("down_write done smpl_vaddr=%p size=%lu\n", vaddr, size));
1550 r = pfm_do_munmap(task->mm, (unsigned long)vaddr, size, 0);
1552 up_write(&task->mm->mmap_sem);
1553 if (r !=0) {
1554 printk(KERN_ERR "perfmon: [%d] unable to unmap sampling buffer @%p size=%lu\n", task->pid, vaddr, size);
1557 DPRINT(("do_unmap(%p, %lu)=%d\n", vaddr, size, r));
1559 return 0;
1561 #endif
1563 /*
1564 * free actual physical storage used by sampling buffer
1565 */
1566 #if 0
1567 static int
1568 pfm_free_smpl_buffer(pfm_context_t *ctx)
1570 pfm_buffer_fmt_t *fmt;
1572 if (ctx->ctx_smpl_hdr == NULL) goto invalid_free;
1574 /*
1575 * we won't use the buffer format anymore
1576 */
1577 fmt = ctx->ctx_buf_fmt;
1579 DPRINT(("sampling buffer @%p size %lu vaddr=%p\n",
1580 ctx->ctx_smpl_hdr,
1581 ctx->ctx_smpl_size,
1582 ctx->ctx_smpl_vaddr));
1584 pfm_buf_fmt_exit(fmt, current, NULL, NULL);
1586 /*
1587 * free the buffer
1588 */
1589 pfm_rvfree(ctx->ctx_smpl_hdr, ctx->ctx_smpl_size);
1591 ctx->ctx_smpl_hdr = NULL;
1592 ctx->ctx_smpl_size = 0UL;
1594 return 0;
1596 invalid_free:
1597 printk(KERN_ERR "perfmon: pfm_free_smpl_buffer [%d] no buffer\n", current->pid);
1598 return -EINVAL;
1600 #endif
1602 static inline void
1603 pfm_exit_smpl_buffer(pfm_buffer_fmt_t *fmt)
1605 if (fmt == NULL) return;
1607 pfm_buf_fmt_exit(fmt, current, NULL, NULL);
1611 #ifndef XEN
1612 /*
1613 * pfmfs should _never_ be mounted by userland - too much of security hassle,
1614 * no real gain from having the whole whorehouse mounted. So we don't need
1615 * any operations on the root directory. However, we need a non-trivial
1616 * d_name - pfm: will go nicely and kill the special-casing in procfs.
1617 */
1618 static struct vfsmount *pfmfs_mnt;
1620 static int __init
1621 init_pfm_fs(void)
1623 int err = register_filesystem(&pfm_fs_type);
1624 if (!err) {
1625 pfmfs_mnt = kern_mount(&pfm_fs_type);
1626 err = PTR_ERR(pfmfs_mnt);
1627 if (IS_ERR(pfmfs_mnt))
1628 unregister_filesystem(&pfm_fs_type);
1629 else
1630 err = 0;
1632 return err;
1635 static void __exit
1636 exit_pfm_fs(void)
1638 unregister_filesystem(&pfm_fs_type);
1639 mntput(pfmfs_mnt);
1642 static ssize_t
1643 pfm_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
1645 pfm_context_t *ctx;
1646 pfm_msg_t *msg;
1647 ssize_t ret;
1648 unsigned long flags;
1649 DECLARE_WAITQUEUE(wait, current);
1650 if (PFM_IS_FILE(filp) == 0) {
1651 printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", current->pid);
1652 return -EINVAL;
1655 ctx = (pfm_context_t *)filp->private_data;
1656 if (ctx == NULL) {
1657 printk(KERN_ERR "perfmon: pfm_read: NULL ctx [%d]\n", current->pid);
1658 return -EINVAL;
1661 /*
1662 * check even when there is no message
1663 */
1664 if (size < sizeof(pfm_msg_t)) {
1665 DPRINT(("message is too small ctx=%p (>=%ld)\n", ctx, sizeof(pfm_msg_t)));
1666 return -EINVAL;
1669 PROTECT_CTX(ctx, flags);
1671 /*
1672 * put ourselves on the wait queue
1673 */
1674 add_wait_queue(&ctx->ctx_msgq_wait, &wait);
1677 for(;;) {
1678 /*
1679 * check wait queue
1680 */
1682 set_current_state(TASK_INTERRUPTIBLE);
1684 DPRINT(("head=%d tail=%d\n", ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
1686 ret = 0;
1687 if(PFM_CTXQ_EMPTY(ctx) == 0) break;
1689 UNPROTECT_CTX(ctx, flags);
1691 /*
1692 * check non-blocking read
1693 */
1694 ret = -EAGAIN;
1695 if(filp->f_flags & O_NONBLOCK) break;
1697 /*
1698 * check pending signals
1699 */
1700 if(signal_pending(current)) {
1701 ret = -EINTR;
1702 break;
1704 /*
1705 * no message, so wait
1706 */
1707 schedule();
1709 PROTECT_CTX(ctx, flags);
1711 DPRINT(("[%d] back to running ret=%ld\n", current->pid, ret));
1712 set_current_state(TASK_RUNNING);
1713 remove_wait_queue(&ctx->ctx_msgq_wait, &wait);
1715 if (ret < 0) goto abort;
1717 ret = -EINVAL;
1718 msg = pfm_get_next_msg(ctx);
1719 if (msg == NULL) {
1720 printk(KERN_ERR "perfmon: pfm_read no msg for ctx=%p [%d]\n", ctx, current->pid);
1721 goto abort_locked;
1724 DPRINT(("fd=%d type=%d\n", msg->pfm_gen_msg.msg_ctx_fd, msg->pfm_gen_msg.msg_type));
1726 ret = -EFAULT;
1727 if(copy_to_user(buf, msg, sizeof(pfm_msg_t)) == 0) ret = sizeof(pfm_msg_t);
1729 abort_locked:
1730 UNPROTECT_CTX(ctx, flags);
1731 abort:
1732 return ret;
1735 static ssize_t
1736 pfm_write(struct file *file, const char __user *ubuf,
1737 size_t size, loff_t *ppos)
1739 DPRINT(("pfm_write called\n"));
1740 return -EINVAL;
1743 static unsigned int
1744 pfm_poll(struct file *filp, poll_table * wait)
1746 pfm_context_t *ctx;
1747 unsigned long flags;
1748 unsigned int mask = 0;
1750 if (PFM_IS_FILE(filp) == 0) {
1751 printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", current->pid);
1752 return 0;
1755 ctx = (pfm_context_t *)filp->private_data;
1756 if (ctx == NULL) {
1757 printk(KERN_ERR "perfmon: pfm_poll: NULL ctx [%d]\n", current->pid);
1758 return 0;
1762 DPRINT(("pfm_poll ctx_fd=%d before poll_wait\n", ctx->ctx_fd));
1764 poll_wait(filp, &ctx->ctx_msgq_wait, wait);
1766 PROTECT_CTX(ctx, flags);
1768 if (PFM_CTXQ_EMPTY(ctx) == 0)
1769 mask = POLLIN | POLLRDNORM;
1771 UNPROTECT_CTX(ctx, flags);
1773 DPRINT(("pfm_poll ctx_fd=%d mask=0x%x\n", ctx->ctx_fd, mask));
1775 return mask;
1778 static int
1779 pfm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1781 DPRINT(("pfm_ioctl called\n"));
1782 return -EINVAL;
1785 /*
1786 * interrupt cannot be masked when coming here
1787 */
1788 static inline int
1789 pfm_do_fasync(int fd, struct file *filp, pfm_context_t *ctx, int on)
1791 int ret;
1793 ret = fasync_helper (fd, filp, on, &ctx->ctx_async_queue);
1795 DPRINT(("pfm_fasync called by [%d] on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
1796 current->pid,
1797 fd,
1798 on,
1799 ctx->ctx_async_queue, ret));
1801 return ret;
1804 static int
1805 pfm_fasync(int fd, struct file *filp, int on)
1807 pfm_context_t *ctx;
1808 int ret;
1810 if (PFM_IS_FILE(filp) == 0) {
1811 printk(KERN_ERR "perfmon: pfm_fasync bad magic [%d]\n", current->pid);
1812 return -EBADF;
1815 ctx = (pfm_context_t *)filp->private_data;
1816 if (ctx == NULL) {
1817 printk(KERN_ERR "perfmon: pfm_fasync NULL ctx [%d]\n", current->pid);
1818 return -EBADF;
1820 /*
1821 * we cannot mask interrupts during this call because this may
1822 * may go to sleep if memory is not readily avalaible.
1824 * We are protected from the conetxt disappearing by the get_fd()/put_fd()
1825 * done in caller. Serialization of this function is ensured by caller.
1826 */
1827 ret = pfm_do_fasync(fd, filp, ctx, on);
1830 DPRINT(("pfm_fasync called on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
1831 fd,
1832 on,
1833 ctx->ctx_async_queue, ret));
1835 return ret;
1838 #ifdef CONFIG_SMP
1839 /*
1840 * this function is exclusively called from pfm_close().
1841 * The context is not protected at that time, nor are interrupts
1842 * on the remote CPU. That's necessary to avoid deadlocks.
1843 */
1844 static void
1845 pfm_syswide_force_stop(void *info)
1847 pfm_context_t *ctx = (pfm_context_t *)info;
1848 struct pt_regs *regs = task_pt_regs(current);
1849 struct task_struct *owner;
1850 unsigned long flags;
1851 int ret;
1853 if (ctx->ctx_cpu != smp_processor_id()) {
1854 printk(KERN_ERR "perfmon: pfm_syswide_force_stop for CPU%d but on CPU%d\n",
1855 ctx->ctx_cpu,
1856 smp_processor_id());
1857 return;
1859 owner = GET_PMU_OWNER();
1860 if (owner != ctx->ctx_task) {
1861 printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected owner [%d] instead of [%d]\n",
1862 smp_processor_id(),
1863 owner->pid, ctx->ctx_task->pid);
1864 return;
1866 if (GET_PMU_CTX() != ctx) {
1867 printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected ctx %p instead of %p\n",
1868 smp_processor_id(),
1869 GET_PMU_CTX(), ctx);
1870 return;
1873 DPRINT(("on CPU%d forcing system wide stop for [%d]\n", smp_processor_id(), ctx->ctx_task->pid));
1874 /*
1875 * the context is already protected in pfm_close(), we simply
1876 * need to mask interrupts to avoid a PMU interrupt race on
1877 * this CPU
1878 */
1879 local_irq_save(flags);
1881 ret = pfm_context_unload(ctx, NULL, 0, regs);
1882 if (ret) {
1883 DPRINT(("context_unload returned %d\n", ret));
1886 /*
1887 * unmask interrupts, PMU interrupts are now spurious here
1888 */
1889 local_irq_restore(flags);
1892 static void
1893 pfm_syswide_cleanup_other_cpu(pfm_context_t *ctx)
1895 int ret;
1897 DPRINT(("calling CPU%d for cleanup\n", ctx->ctx_cpu));
1898 ret = smp_call_function_single(ctx->ctx_cpu, pfm_syswide_force_stop, ctx, 0, 1);
1899 DPRINT(("called CPU%d for cleanup ret=%d\n", ctx->ctx_cpu, ret));
1901 #endif /* CONFIG_SMP */
1903 /*
1904 * called for each close(). Partially free resources.
1905 * When caller is self-monitoring, the context is unloaded.
1906 */
1907 static int
1908 pfm_flush(struct file *filp)
1910 pfm_context_t *ctx;
1911 struct task_struct *task;
1912 struct pt_regs *regs;
1913 unsigned long flags;
1914 unsigned long smpl_buf_size = 0UL;
1915 void *smpl_buf_vaddr = NULL;
1916 int state, is_system;
1918 if (PFM_IS_FILE(filp) == 0) {
1919 DPRINT(("bad magic for\n"));
1920 return -EBADF;
1923 ctx = (pfm_context_t *)filp->private_data;
1924 if (ctx == NULL) {
1925 printk(KERN_ERR "perfmon: pfm_flush: NULL ctx [%d]\n", current->pid);
1926 return -EBADF;
1929 /*
1930 * remove our file from the async queue, if we use this mode.
1931 * This can be done without the context being protected. We come
1932 * here when the context has become unreacheable by other tasks.
1934 * We may still have active monitoring at this point and we may
1935 * end up in pfm_overflow_handler(). However, fasync_helper()
1936 * operates with interrupts disabled and it cleans up the
1937 * queue. If the PMU handler is called prior to entering
1938 * fasync_helper() then it will send a signal. If it is
1939 * invoked after, it will find an empty queue and no
1940 * signal will be sent. In both case, we are safe
1941 */
1942 if (filp->f_flags & FASYNC) {
1943 DPRINT(("cleaning up async_queue=%p\n", ctx->ctx_async_queue));
1944 pfm_do_fasync (-1, filp, ctx, 0);
1947 PROTECT_CTX(ctx, flags);
1949 state = ctx->ctx_state;
1950 is_system = ctx->ctx_fl_system;
1952 task = PFM_CTX_TASK(ctx);
1953 regs = task_pt_regs(task);
1955 DPRINT(("ctx_state=%d is_current=%d\n",
1956 state,
1957 task == current ? 1 : 0));
1959 /*
1960 * if state == UNLOADED, then task is NULL
1961 */
1963 /*
1964 * we must stop and unload because we are losing access to the context.
1965 */
1966 if (task == current) {
1967 #ifdef CONFIG_SMP
1968 /*
1969 * the task IS the owner but it migrated to another CPU: that's bad
1970 * but we must handle this cleanly. Unfortunately, the kernel does
1971 * not provide a mechanism to block migration (while the context is loaded).
1973 * We need to release the resource on the ORIGINAL cpu.
1974 */
1975 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
1977 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
1978 /*
1979 * keep context protected but unmask interrupt for IPI
1980 */
1981 local_irq_restore(flags);
1983 pfm_syswide_cleanup_other_cpu(ctx);
1985 /*
1986 * restore interrupt masking
1987 */
1988 local_irq_save(flags);
1990 /*
1991 * context is unloaded at this point
1992 */
1993 } else
1994 #endif /* CONFIG_SMP */
1997 DPRINT(("forcing unload\n"));
1998 /*
1999 * stop and unload, returning with state UNLOADED
2000 * and session unreserved.
2001 */
2002 pfm_context_unload(ctx, NULL, 0, regs);
2004 DPRINT(("ctx_state=%d\n", ctx->ctx_state));
2008 /*
2009 * remove virtual mapping, if any, for the calling task.
2010 * cannot reset ctx field until last user is calling close().
2012 * ctx_smpl_vaddr must never be cleared because it is needed
2013 * by every task with access to the context
2015 * When called from do_exit(), the mm context is gone already, therefore
2016 * mm is NULL, i.e., the VMA is already gone and we do not have to
2017 * do anything here
2018 */
2019 if (ctx->ctx_smpl_vaddr && current->mm) {
2020 smpl_buf_vaddr = ctx->ctx_smpl_vaddr;
2021 smpl_buf_size = ctx->ctx_smpl_size;
2024 UNPROTECT_CTX(ctx, flags);
2026 /*
2027 * if there was a mapping, then we systematically remove it
2028 * at this point. Cannot be done inside critical section
2029 * because some VM function reenables interrupts.
2031 */
2032 if (smpl_buf_vaddr) pfm_remove_smpl_mapping(current, smpl_buf_vaddr, smpl_buf_size);
2034 return 0;
2036 #endif
2037 /*
2038 * called either on explicit close() or from exit_files().
2039 * Only the LAST user of the file gets to this point, i.e., it is
2040 * called only ONCE.
2042 * IMPORTANT: we get called ONLY when the refcnt on the file gets to zero
2043 * (fput()),i.e, last task to access the file. Nobody else can access the
2044 * file at this point.
2046 * When called from exit_files(), the VMA has been freed because exit_mm()
2047 * is executed before exit_files().
2049 * When called from exit_files(), the current task is not yet ZOMBIE but we
2050 * flush the PMU state to the context.
2051 */
2052 #ifndef XEN
2053 static int
2054 pfm_close(struct inode *inode, struct file *filp)
2055 #else
2056 static int
2057 pfm_close(pfm_context_t *ctx)
2058 #endif
2060 #ifndef XEN
2061 pfm_context_t *ctx;
2062 struct task_struct *task;
2063 struct pt_regs *regs;
2064 DECLARE_WAITQUEUE(wait, current);
2065 unsigned long flags;
2066 #endif
2067 unsigned long smpl_buf_size = 0UL;
2068 void *smpl_buf_addr = NULL;
2069 int free_possible = 1;
2070 int state, is_system;
2072 #ifndef XEN
2073 DPRINT(("pfm_close called private=%p\n", filp->private_data));
2075 if (PFM_IS_FILE(filp) == 0) {
2076 DPRINT(("bad magic\n"));
2077 return -EBADF;
2080 ctx = (pfm_context_t *)filp->private_data;
2081 if (ctx == NULL) {
2082 printk(KERN_ERR "perfmon: pfm_close: NULL ctx [%d]\n", current->pid);
2083 return -EBADF;
2086 PROTECT_CTX(ctx, flags);
2087 #else
2088 BUG_ON(!spin_is_locked(&ctx->ctx_lock));
2089 #endif
2091 state = ctx->ctx_state;
2092 is_system = ctx->ctx_fl_system;
2094 #ifndef XEN
2095 task = PFM_CTX_TASK(ctx);
2096 regs = task_pt_regs(task);
2098 DPRINT(("ctx_state=%d is_current=%d\n",
2099 state,
2100 task == current ? 1 : 0));
2102 /*
2103 * if task == current, then pfm_flush() unloaded the context
2104 */
2105 if (state == PFM_CTX_UNLOADED) goto doit;
2107 /*
2108 * context is loaded/masked and task != current, we need to
2109 * either force an unload or go zombie
2110 */
2112 /*
2113 * The task is currently blocked or will block after an overflow.
2114 * we must force it to wakeup to get out of the
2115 * MASKED state and transition to the unloaded state by itself.
2117 * This situation is only possible for per-task mode
2118 */
2119 if (state == PFM_CTX_MASKED && CTX_OVFL_NOBLOCK(ctx) == 0) {
2121 /*
2122 * set a "partial" zombie state to be checked
2123 * upon return from down() in pfm_handle_work().
2125 * We cannot use the ZOMBIE state, because it is checked
2126 * by pfm_load_regs() which is called upon wakeup from down().
2127 * In such case, it would free the context and then we would
2128 * return to pfm_handle_work() which would access the
2129 * stale context. Instead, we set a flag invisible to pfm_load_regs()
2130 * but visible to pfm_handle_work().
2132 * For some window of time, we have a zombie context with
2133 * ctx_state = MASKED and not ZOMBIE
2134 */
2135 ctx->ctx_fl_going_zombie = 1;
2137 /*
2138 * force task to wake up from MASKED state
2139 */
2140 complete(&ctx->ctx_restart_done);
2142 DPRINT(("waking up ctx_state=%d\n", state));
2144 /*
2145 * put ourself to sleep waiting for the other
2146 * task to report completion
2148 * the context is protected by mutex, therefore there
2149 * is no risk of being notified of completion before
2150 * begin actually on the waitq.
2151 */
2152 set_current_state(TASK_INTERRUPTIBLE);
2153 add_wait_queue(&ctx->ctx_zombieq, &wait);
2155 UNPROTECT_CTX(ctx, flags);
2157 /*
2158 * XXX: check for signals :
2159 * - ok for explicit close
2160 * - not ok when coming from exit_files()
2161 */
2162 schedule();
2165 PROTECT_CTX(ctx, flags);
2168 remove_wait_queue(&ctx->ctx_zombieq, &wait);
2169 set_current_state(TASK_RUNNING);
2171 /*
2172 * context is unloaded at this point
2173 */
2174 DPRINT(("after zombie wakeup ctx_state=%d for\n", state));
2176 else if (task != current) {
2177 #ifdef CONFIG_SMP
2178 /*
2179 * switch context to zombie state
2180 */
2181 ctx->ctx_state = PFM_CTX_ZOMBIE;
2183 DPRINT(("zombie ctx for [%d]\n", task->pid));
2184 /*
2185 * cannot free the context on the spot. deferred until
2186 * the task notices the ZOMBIE state
2187 */
2188 free_possible = 0;
2189 #else
2190 pfm_context_unload(ctx, NULL, 0, regs);
2191 #endif
2193 #else
2194 /* XXX XEN */
2195 /* unload context */
2196 BUG_ON(state != PFM_CTX_UNLOADED);
2197 #endif
2199 #ifndef XEN
2200 doit:
2201 #endif
2202 /* reload state, may have changed during opening of critical section */
2203 state = ctx->ctx_state;
2205 /*
2206 * the context is still attached to a task (possibly current)
2207 * we cannot destroy it right now
2208 */
2210 /*
2211 * we must free the sampling buffer right here because
2212 * we cannot rely on it being cleaned up later by the
2213 * monitored task. It is not possible to free vmalloc'ed
2214 * memory in pfm_load_regs(). Instead, we remove the buffer
2215 * now. should there be subsequent PMU overflow originally
2216 * meant for sampling, the will be converted to spurious
2217 * and that's fine because the monitoring tools is gone anyway.
2218 */
2219 if (ctx->ctx_smpl_hdr) {
2220 smpl_buf_addr = ctx->ctx_smpl_hdr;
2221 smpl_buf_size = ctx->ctx_smpl_size;
2222 /* no more sampling */
2223 ctx->ctx_smpl_hdr = NULL;
2224 ctx->ctx_fl_is_sampling = 0;
2227 DPRINT(("ctx_state=%d free_possible=%d addr=%p size=%lu\n",
2228 state,
2229 free_possible,
2230 smpl_buf_addr,
2231 smpl_buf_size));
2233 if (smpl_buf_addr) pfm_exit_smpl_buffer(ctx->ctx_buf_fmt);
2235 /*
2236 * UNLOADED that the session has already been unreserved.
2237 */
2238 if (state == PFM_CTX_ZOMBIE) {
2239 pfm_unreserve_session(ctx, ctx->ctx_fl_system , ctx->ctx_cpu);
2242 #ifndef XEN
2243 /*
2244 * disconnect file descriptor from context must be done
2245 * before we unlock.
2246 */
2247 filp->private_data = NULL;
2249 /*
2250 * if we free on the spot, the context is now completely unreacheable
2251 * from the callers side. The monitored task side is also cut, so we
2252 * can freely cut.
2254 * If we have a deferred free, only the caller side is disconnected.
2255 */
2256 UNPROTECT_CTX(ctx, flags);
2258 /*
2259 * All memory free operations (especially for vmalloc'ed memory)
2260 * MUST be done with interrupts ENABLED.
2261 */
2262 if (smpl_buf_addr) pfm_rvfree(smpl_buf_addr, smpl_buf_size);
2263 #else
2264 UNPROTECT_CTX_NOIRQ(ctx);
2265 #endif
2267 /*
2268 * return the memory used by the context
2269 */
2270 if (free_possible) pfm_context_free(ctx);
2272 return 0;
2275 #ifndef XEN
2276 static int
2277 pfm_no_open(struct inode *irrelevant, struct file *dontcare)
2279 DPRINT(("pfm_no_open called\n"));
2280 return -ENXIO;
2285 static struct file_operations pfm_file_ops = {
2286 .llseek = no_llseek,
2287 .read = pfm_read,
2288 .write = pfm_write,
2289 .poll = pfm_poll,
2290 .ioctl = pfm_ioctl,
2291 .open = pfm_no_open, /* special open code to disallow open via /proc */
2292 .fasync = pfm_fasync,
2293 .release = pfm_close,
2294 .flush = pfm_flush
2295 };
2297 static int
2298 pfmfs_delete_dentry(struct dentry *dentry)
2300 return 1;
2303 static struct dentry_operations pfmfs_dentry_operations = {
2304 .d_delete = pfmfs_delete_dentry,
2305 };
2308 static int
2309 pfm_alloc_fd(struct file **cfile)
2311 int fd, ret = 0;
2312 struct file *file = NULL;
2313 struct inode * inode;
2314 char name[32];
2315 struct qstr this;
2317 fd = get_unused_fd();
2318 if (fd < 0) return -ENFILE;
2320 ret = -ENFILE;
2322 file = get_empty_filp();
2323 if (!file) goto out;
2325 /*
2326 * allocate a new inode
2327 */
2328 inode = new_inode(pfmfs_mnt->mnt_sb);
2329 if (!inode) goto out;
2331 DPRINT(("new inode ino=%ld @%p\n", inode->i_ino, inode));
2333 inode->i_mode = S_IFCHR|S_IRUGO;
2334 inode->i_uid = current->fsuid;
2335 inode->i_gid = current->fsgid;
2337 snprintf(name, sizeof(name), "[%lu]", inode->i_ino);
2338 this.name = name;
2339 this.len = strlen(name);
2340 this.hash = inode->i_ino;
2342 ret = -ENOMEM;
2344 /*
2345 * allocate a new dcache entry
2346 */
2347 file->f_dentry = d_alloc(pfmfs_mnt->mnt_sb->s_root, &this);
2348 if (!file->f_dentry) goto out;
2350 file->f_dentry->d_op = &pfmfs_dentry_operations;
2352 d_add(file->f_dentry, inode);
2353 file->f_vfsmnt = mntget(pfmfs_mnt);
2354 file->f_mapping = inode->i_mapping;
2356 file->f_op = &pfm_file_ops;
2357 file->f_mode = FMODE_READ;
2358 file->f_flags = O_RDONLY;
2359 file->f_pos = 0;
2361 /*
2362 * may have to delay until context is attached?
2363 */
2364 fd_install(fd, file);
2366 /*
2367 * the file structure we will use
2368 */
2369 *cfile = file;
2371 return fd;
2372 out:
2373 if (file) put_filp(file);
2374 put_unused_fd(fd);
2375 return ret;
2378 static void
2379 pfm_free_fd(int fd, struct file *file)
2381 struct files_struct *files = current->files;
2382 struct fdtable *fdt;
2384 /*
2385 * there ie no fd_uninstall(), so we do it here
2386 */
2387 spin_lock(&files->file_lock);
2388 fdt = files_fdtable(files);
2389 rcu_assign_pointer(fdt->fd[fd], NULL);
2390 spin_unlock(&files->file_lock);
2392 if (file)
2393 put_filp(file);
2394 put_unused_fd(fd);
2397 static int
2398 pfm_remap_buffer(struct vm_area_struct *vma, unsigned long buf, unsigned long addr, unsigned long size)
2400 DPRINT(("CPU%d buf=0x%lx addr=0x%lx size=%ld\n", smp_processor_id(), buf, addr, size));
2402 while (size > 0) {
2403 unsigned long pfn = ia64_tpa(buf) >> PAGE_SHIFT;
2406 if (remap_pfn_range(vma, addr, pfn, PAGE_SIZE, PAGE_READONLY))
2407 return -ENOMEM;
2409 addr += PAGE_SIZE;
2410 buf += PAGE_SIZE;
2411 size -= PAGE_SIZE;
2413 return 0;
2415 #endif
2417 /*
2418 * allocate a sampling buffer and remaps it into the user address space of the task
2419 */
2420 static int
2421 pfm_smpl_buffer_alloc(struct task_struct *task, pfm_context_t *ctx, unsigned long rsize, void **user_vaddr)
2423 #ifndef XEN
2424 struct mm_struct *mm = task->mm;
2425 struct vm_area_struct *vma = NULL;
2426 unsigned long size;
2427 void *smpl_buf;
2430 /*
2431 * the fixed header + requested size and align to page boundary
2432 */
2433 size = PAGE_ALIGN(rsize);
2435 DPRINT(("sampling buffer rsize=%lu size=%lu bytes\n", rsize, size));
2437 /*
2438 * check requested size to avoid Denial-of-service attacks
2439 * XXX: may have to refine this test
2440 * Check against address space limit.
2442 * if ((mm->total_vm << PAGE_SHIFT) + len> task->rlim[RLIMIT_AS].rlim_cur)
2443 * return -ENOMEM;
2444 */
2445 if (size > task->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
2446 return -ENOMEM;
2448 /*
2449 * We do the easy to undo allocations first.
2451 * pfm_rvmalloc(), clears the buffer, so there is no leak
2452 */
2453 smpl_buf = pfm_rvmalloc(size);
2454 if (smpl_buf == NULL) {
2455 DPRINT(("Can't allocate sampling buffer\n"));
2456 return -ENOMEM;
2459 DPRINT(("smpl_buf @%p\n", smpl_buf));
2461 /* allocate vma */
2462 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2463 if (!vma) {
2464 DPRINT(("Cannot allocate vma\n"));
2465 goto error_kmem;
2467 memset(vma, 0, sizeof(*vma));
2469 /*
2470 * partially initialize the vma for the sampling buffer
2471 */
2472 vma->vm_mm = mm;
2473 vma->vm_flags = VM_READ| VM_MAYREAD |VM_RESERVED;
2474 vma->vm_page_prot = PAGE_READONLY; /* XXX may need to change */
2476 /*
2477 * Now we have everything we need and we can initialize
2478 * and connect all the data structures
2479 */
2481 ctx->ctx_smpl_hdr = smpl_buf;
2482 ctx->ctx_smpl_size = size; /* aligned size */
2484 /*
2485 * Let's do the difficult operations next.
2487 * now we atomically find some area in the address space and
2488 * remap the buffer in it.
2489 */
2490 down_write(&task->mm->mmap_sem);
2492 /* find some free area in address space, must have mmap sem held */
2493 vma->vm_start = pfm_get_unmapped_area(NULL, 0, size, 0, MAP_PRIVATE|MAP_ANONYMOUS, 0);
2494 if (vma->vm_start == 0UL) {
2495 DPRINT(("Cannot find unmapped area for size %ld\n", size));
2496 up_write(&task->mm->mmap_sem);
2497 goto error;
2499 vma->vm_end = vma->vm_start + size;
2500 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2502 DPRINT(("aligned size=%ld, hdr=%p mapped @0x%lx\n", size, ctx->ctx_smpl_hdr, vma->vm_start));
2504 /* can only be applied to current task, need to have the mm semaphore held when called */
2505 if (pfm_remap_buffer(vma, (unsigned long)smpl_buf, vma->vm_start, size)) {
2506 DPRINT(("Can't remap buffer\n"));
2507 up_write(&task->mm->mmap_sem);
2508 goto error;
2511 /*
2512 * now insert the vma in the vm list for the process, must be
2513 * done with mmap lock held
2514 */
2515 insert_vm_struct(mm, vma);
2517 mm->total_vm += size >> PAGE_SHIFT;
2518 vm_stat_account(vma->vm_mm, vma->vm_flags, vma->vm_file,
2519 vma_pages(vma));
2520 up_write(&task->mm->mmap_sem);
2522 /*
2523 * keep track of user level virtual address
2524 */
2525 ctx->ctx_smpl_vaddr = (void *)vma->vm_start;
2526 *(unsigned long *)user_vaddr = vma->vm_start;
2528 return 0;
2530 error:
2531 kmem_cache_free(vm_area_cachep, vma);
2532 error_kmem:
2533 pfm_rvfree(smpl_buf, size);
2535 return -ENOMEM;
2536 #else
2537 /* XXX */
2538 return 0;
2539 #endif
2542 #ifndef XEN
2543 /*
2544 * XXX: do something better here
2545 */
2546 static int
2547 pfm_bad_permissions(struct task_struct *task)
2549 /* inspired by ptrace_attach() */
2550 DPRINT(("cur: uid=%d gid=%d task: euid=%d suid=%d uid=%d egid=%d sgid=%d\n",
2551 current->uid,
2552 current->gid,
2553 task->euid,
2554 task->suid,
2555 task->uid,
2556 task->egid,
2557 task->sgid));
2559 return ((current->uid != task->euid)
2560 || (current->uid != task->suid)
2561 || (current->uid != task->uid)
2562 || (current->gid != task->egid)
2563 || (current->gid != task->sgid)
2564 || (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE);
2566 #endif
2568 static int
2569 pfarg_is_sane(struct task_struct *task, pfarg_context_t *pfx)
2571 int ctx_flags;
2573 /* valid signal */
2575 ctx_flags = pfx->ctx_flags;
2577 if (ctx_flags & PFM_FL_SYSTEM_WIDE) {
2579 /*
2580 * cannot block in this mode
2581 */
2582 if (ctx_flags & PFM_FL_NOTIFY_BLOCK) {
2583 DPRINT(("cannot use blocking mode when in system wide monitoring\n"));
2584 return -EINVAL;
2586 } else {
2588 /* probably more to add here */
2590 return 0;
2593 static int
2594 pfm_setup_buffer_fmt(struct task_struct *task, pfm_context_t *ctx, unsigned int ctx_flags,
2595 unsigned int cpu, pfarg_context_t *arg)
2597 pfm_buffer_fmt_t *fmt = NULL;
2598 unsigned long size = 0UL;
2599 void *uaddr = NULL;
2600 void *fmt_arg = NULL;
2601 int ret = 0;
2602 #define PFM_CTXARG_BUF_ARG(a) (pfm_buffer_fmt_t *)(a+1)
2604 /* invoke and lock buffer format, if found */
2605 fmt = pfm_find_buffer_fmt(arg->ctx_smpl_buf_id);
2606 if (fmt == NULL) {
2607 DPRINT(("[%d] cannot find buffer format\n", task->pid));
2608 return -EINVAL;
2611 /*
2612 * buffer argument MUST be contiguous to pfarg_context_t
2613 */
2614 if (fmt->fmt_arg_size) fmt_arg = PFM_CTXARG_BUF_ARG(arg);
2616 ret = pfm_buf_fmt_validate(fmt, task, ctx_flags, cpu, fmt_arg);
2618 DPRINT(("[%d] after validate(0x%x,%d,%p)=%d\n", task->pid, ctx_flags, cpu, fmt_arg, ret));
2620 if (ret) goto error;
2622 /* link buffer format and context */
2623 ctx->ctx_buf_fmt = fmt;
2625 /*
2626 * check if buffer format wants to use perfmon buffer allocation/mapping service
2627 */
2628 ret = pfm_buf_fmt_getsize(fmt, task, ctx_flags, cpu, fmt_arg, &size);
2629 if (ret) goto error;
2631 if (size) {
2632 /*
2633 * buffer is always remapped into the caller's address space
2634 */
2635 ret = pfm_smpl_buffer_alloc(current, ctx, size, &uaddr);
2636 if (ret) goto error;
2638 /* keep track of user address of buffer */
2639 arg->ctx_smpl_vaddr = uaddr;
2641 ret = pfm_buf_fmt_init(fmt, task, ctx->ctx_smpl_hdr, ctx_flags, cpu, fmt_arg);
2643 error:
2644 return ret;
2647 static void
2648 pfm_reset_pmu_state(pfm_context_t *ctx)
2650 int i;
2652 /*
2653 * install reset values for PMC.
2654 */
2655 for (i=1; PMC_IS_LAST(i) == 0; i++) {
2656 if (PMC_IS_IMPL(i) == 0) continue;
2657 ctx->ctx_pmcs[i] = PMC_DFL_VAL(i);
2658 DPRINT(("pmc[%d]=0x%lx\n", i, ctx->ctx_pmcs[i]));
2660 /*
2661 * PMD registers are set to 0UL when the context in memset()
2662 */
2664 /*
2665 * On context switched restore, we must restore ALL pmc and ALL pmd even
2666 * when they are not actively used by the task. In UP, the incoming process
2667 * may otherwise pick up left over PMC, PMD state from the previous process.
2668 * As opposed to PMD, stale PMC can cause harm to the incoming
2669 * process because they may change what is being measured.
2670 * Therefore, we must systematically reinstall the entire
2671 * PMC state. In SMP, the same thing is possible on the
2672 * same CPU but also on between 2 CPUs.
2674 * The problem with PMD is information leaking especially
2675 * to user level when psr.sp=0
2677 * There is unfortunately no easy way to avoid this problem
2678 * on either UP or SMP. This definitively slows down the
2679 * pfm_load_regs() function.
2680 */
2682 /*
2683 * bitmask of all PMCs accessible to this context
2685 * PMC0 is treated differently.
2686 */
2687 ctx->ctx_all_pmcs[0] = pmu_conf->impl_pmcs[0] & ~0x1;
2689 /*
2690 * bitmask of all PMDs that are accesible to this context
2691 */
2692 ctx->ctx_all_pmds[0] = pmu_conf->impl_pmds[0];
2694 DPRINT(("<%d> all_pmcs=0x%lx all_pmds=0x%lx\n", ctx->ctx_fd, ctx->ctx_all_pmcs[0],ctx->ctx_all_pmds[0]));
2696 /*
2697 * useful in case of re-enable after disable
2698 */
2699 ctx->ctx_used_ibrs[0] = 0UL;
2700 ctx->ctx_used_dbrs[0] = 0UL;
2703 #ifndef XEN
2704 static int
2705 pfm_ctx_getsize(void *arg, size_t *sz)
2707 pfarg_context_t *req = (pfarg_context_t *)arg;
2708 pfm_buffer_fmt_t *fmt;
2710 *sz = 0;
2712 if (!pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) return 0;
2714 fmt = pfm_find_buffer_fmt(req->ctx_smpl_buf_id);
2715 if (fmt == NULL) {
2716 DPRINT(("cannot find buffer format\n"));
2717 return -EINVAL;
2719 /* get just enough to copy in user parameters */
2720 *sz = fmt->fmt_arg_size;
2721 DPRINT(("arg_size=%lu\n", *sz));
2723 return 0;
2728 /*
2729 * cannot attach if :
2730 * - kernel task
2731 * - task not owned by caller
2732 * - task incompatible with context mode
2733 */
2734 static int
2735 pfm_task_incompatible(pfm_context_t *ctx, struct task_struct *task)
2737 /*
2738 * no kernel task or task not owner by caller
2739 */
2740 if (task->mm == NULL) {
2741 DPRINT(("task [%d] has not memory context (kernel thread)\n", task->pid));
2742 return -EPERM;
2744 if (pfm_bad_permissions(task)) {
2745 DPRINT(("no permission to attach to [%d]\n", task->pid));
2746 return -EPERM;
2748 /*
2749 * cannot block in self-monitoring mode
2750 */
2751 if (CTX_OVFL_NOBLOCK(ctx) == 0 && task == current) {
2752 DPRINT(("cannot load a blocking context on self for [%d]\n", task->pid));
2753 return -EINVAL;
2756 if (task->exit_state == EXIT_ZOMBIE) {
2757 DPRINT(("cannot attach to zombie task [%d]\n", task->pid));
2758 return -EBUSY;
2761 /*
2762 * always ok for self
2763 */
2764 if (task == current) return 0;
2766 if ((task->state != TASK_STOPPED) && (task->state != TASK_TRACED)) {
2767 DPRINT(("cannot attach to non-stopped task [%d] state=%ld\n", task->pid, task->state));
2768 return -EBUSY;
2770 /*
2771 * make sure the task is off any CPU
2772 */
2773 wait_task_inactive(task);
2775 /* more to come... */
2777 return 0;
2780 static int
2781 pfm_get_task(pfm_context_t *ctx, pid_t pid, struct task_struct **task)
2783 struct task_struct *p = current;
2784 int ret;
2786 /* XXX: need to add more checks here */
2787 if (pid < 2) return -EPERM;
2789 if (pid != current->pid) {
2791 read_lock(&tasklist_lock);
2793 p = find_task_by_pid(pid);
2795 /* make sure task cannot go away while we operate on it */
2796 if (p) get_task_struct(p);
2798 read_unlock(&tasklist_lock);
2800 if (p == NULL) return -ESRCH;
2803 ret = pfm_task_incompatible(ctx, p);
2804 if (ret == 0) {
2805 *task = p;
2806 } else if (p != current) {
2807 pfm_put_task(p);
2809 return ret;
2811 #endif
2814 #ifndef XEN
2815 static int
2816 pfm_context_create(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
2817 #else
2818 static pfm_context_t*
2819 pfm_context_create(pfarg_context_t* req)
2820 #endif
2822 #ifndef XEN
2823 pfarg_context_t *req = (pfarg_context_t *)arg;
2824 struct file *filp;
2825 #else
2826 pfm_context_t *ctx;
2827 #endif
2828 int ctx_flags;
2829 int ret;
2831 #ifndef XEN
2832 /* let's check the arguments first */
2833 ret = pfarg_is_sane(current, req);
2834 if (ret < 0) return ret;
2835 #endif
2837 ctx_flags = req->ctx_flags;
2839 ret = -ENOMEM;
2841 ctx = pfm_context_alloc();
2842 if (!ctx) goto error;
2844 #ifndef XEN
2845 ret = pfm_alloc_fd(&filp);
2846 if (ret < 0) goto error_file;
2848 req->ctx_fd = ctx->ctx_fd = ret;
2850 /*
2851 * attach context to file
2852 */
2853 filp->private_data = ctx;
2854 #endif
2856 /*
2857 * does the user want to sample?
2858 */
2859 if (pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) {
2860 ret = pfm_setup_buffer_fmt(current, ctx, ctx_flags, 0, req);
2861 if (ret) goto buffer_error;
2864 /*
2865 * init context protection lock
2866 */
2867 spin_lock_init(&ctx->ctx_lock);
2869 /*
2870 * context is unloaded
2871 */
2872 ctx->ctx_state = PFM_CTX_UNLOADED;
2874 /*
2875 * initialization of context's flags
2876 */
2877 ctx->ctx_fl_block = (ctx_flags & PFM_FL_NOTIFY_BLOCK) ? 1 : 0;
2878 ctx->ctx_fl_system = (ctx_flags & PFM_FL_SYSTEM_WIDE) ? 1: 0;
2879 ctx->ctx_fl_is_sampling = ctx->ctx_buf_fmt ? 1 : 0; /* assume record() is defined */
2880 ctx->ctx_fl_no_msg = (ctx_flags & PFM_FL_OVFL_NO_MSG) ? 1: 0;
2881 /*
2882 * will move to set properties
2883 * ctx->ctx_fl_excl_idle = (ctx_flags & PFM_FL_EXCL_IDLE) ? 1: 0;
2884 */
2886 #ifndef XEN
2887 /*
2888 * init restart semaphore to locked
2889 */
2890 init_completion(&ctx->ctx_restart_done);
2891 #endif
2893 /*
2894 * activation is used in SMP only
2895 */
2896 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
2897 SET_LAST_CPU(ctx, -1);
2899 #ifndef XEN
2900 /*
2901 * initialize notification message queue
2902 */
2903 ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
2904 init_waitqueue_head(&ctx->ctx_msgq_wait);
2905 init_waitqueue_head(&ctx->ctx_zombieq);
2906 #endif
2908 DPRINT(("ctx=%p flags=0x%x system=%d notify_block=%d excl_idle=%d no_msg=%d ctx_fd=%d \n",
2909 ctx,
2910 ctx_flags,
2911 ctx->ctx_fl_system,
2912 ctx->ctx_fl_block,
2913 ctx->ctx_fl_excl_idle,
2914 ctx->ctx_fl_no_msg,
2915 ctx->ctx_fd));
2917 /*
2918 * initialize soft PMU state
2919 */
2920 pfm_reset_pmu_state(ctx);
2922 #ifndef XEN
2923 return 0;
2924 #else
2925 return ctx;
2926 #endif
2928 buffer_error:
2929 #ifndef XEN
2930 pfm_free_fd(ctx->ctx_fd, filp);
2931 #endif
2933 if (ctx->ctx_buf_fmt) {
2934 #ifndef XEN
2935 pfm_buf_fmt_exit(ctx->ctx_buf_fmt, current, NULL, regs);
2936 #else
2937 pfm_buf_fmt_exit(ctx->ctx_buf_fmt, current, NULL, NULL);
2938 #endif
2940 #ifndef XEN
2941 error_file:
2942 #endif
2943 pfm_context_free(ctx);
2945 error:
2946 #ifndef XEN
2947 return ret;
2948 #else
2949 return NULL;
2950 #endif
2953 static inline unsigned long
2954 pfm_new_counter_value (pfm_counter_t *reg, int is_long_reset)
2956 unsigned long val = is_long_reset ? reg->long_reset : reg->short_reset;
2957 unsigned long new_seed, old_seed = reg->seed, mask = reg->mask;
2958 extern unsigned long carta_random32 (unsigned long seed);
2960 if (reg->flags & PFM_REGFL_RANDOM) {
2961 new_seed = carta_random32(old_seed);
2962 val -= (old_seed & mask); /* counter values are negative numbers! */
2963 if ((mask >> 32) != 0)
2964 /* construct a full 64-bit random value: */
2965 new_seed |= carta_random32(old_seed >> 32) << 32;
2966 reg->seed = new_seed;
2968 reg->lval = val;
2969 return val;
2972 static void
2973 pfm_reset_regs_masked(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
2975 unsigned long mask = ovfl_regs[0];
2976 unsigned long reset_others = 0UL;
2977 unsigned long val;
2978 int i;
2980 /*
2981 * now restore reset value on sampling overflowed counters
2982 */
2983 mask >>= PMU_FIRST_COUNTER;
2984 for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
2986 if ((mask & 0x1UL) == 0UL) continue;
2988 ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
2989 reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
2991 DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
2994 /*
2995 * Now take care of resetting the other registers
2996 */
2997 for(i = 0; reset_others; i++, reset_others >>= 1) {
2999 if ((reset_others & 0x1) == 0) continue;
3001 ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
3003 DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
3004 is_long_reset ? "long" : "short", i, val));
3008 static void
3009 pfm_reset_regs(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
3011 unsigned long mask = ovfl_regs[0];
3012 unsigned long reset_others = 0UL;
3013 unsigned long val;
3014 int i;
3016 DPRINT_ovfl(("ovfl_regs=0x%lx is_long_reset=%d\n", ovfl_regs[0], is_long_reset));
3018 if (ctx->ctx_state == PFM_CTX_MASKED) {
3019 pfm_reset_regs_masked(ctx, ovfl_regs, is_long_reset);
3020 return;
3023 /*
3024 * now restore reset value on sampling overflowed counters
3025 */
3026 mask >>= PMU_FIRST_COUNTER;
3027 for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
3029 if ((mask & 0x1UL) == 0UL) continue;
3031 val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
3032 reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
3034 DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
3036 pfm_write_soft_counter(ctx, i, val);
3039 /*
3040 * Now take care of resetting the other registers
3041 */
3042 for(i = 0; reset_others; i++, reset_others >>= 1) {
3044 if ((reset_others & 0x1) == 0) continue;
3046 val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
3048 if (PMD_IS_COUNTING(i)) {
3049 pfm_write_soft_counter(ctx, i, val);
3050 } else {
3051 ia64_set_pmd(i, val);
3053 DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
3054 is_long_reset ? "long" : "short", i, val));
3056 ia64_srlz_d();
3059 static int
3060 pfm_write_pmcs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3062 #ifndef XEN
3063 struct thread_struct *thread = NULL;
3064 #endif
3065 struct task_struct *task;
3066 pfarg_reg_t *req = (pfarg_reg_t *)arg;
3067 unsigned long value, pmc_pm;
3068 unsigned long smpl_pmds, reset_pmds, impl_pmds;
3069 unsigned int cnum, reg_flags, flags, pmc_type;
3070 int i, can_access_pmu = 0, is_loaded, is_system, expert_mode;
3071 int is_monitor, is_counting, state;
3072 int ret = -EINVAL;
3073 pfm_reg_check_t wr_func;
3074 #define PFM_CHECK_PMC_PM(x, y, z) ((x)->ctx_fl_system ^ PMC_PM(y, z))
3076 state = ctx->ctx_state;
3077 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3078 is_system = ctx->ctx_fl_system;
3079 task = ctx->ctx_task;
3080 impl_pmds = pmu_conf->impl_pmds[0];
3081 #ifdef XEN
3082 task = NULL;
3083 BUG_ON(regs != NULL);
3084 #endif
3086 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
3088 #ifndef XEN
3089 if (is_loaded) {
3090 thread = &task->thread;
3091 /*
3092 * In system wide and when the context is loaded, access can only happen
3093 * when the caller is running on the CPU being monitored by the session.
3094 * It does not have to be the owner (ctx_task) of the context per se.
3095 */
3096 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
3097 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3098 return -EBUSY;
3100 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3102 #else
3103 /* XXX FIXME */
3104 if (state != PFM_CTX_UNLOADED) {
3105 gdprintk(XENLOG_DEBUG, "%s state %d\n", __func__, state);
3106 return -EBUSY;
3108 #endif
3110 expert_mode = pfm_sysctl.expert_mode;
3112 for (i = 0; i < count; i++, req++) {
3114 cnum = req->reg_num;
3115 reg_flags = req->reg_flags;
3116 value = req->reg_value;
3117 smpl_pmds = req->reg_smpl_pmds[0];
3118 reset_pmds = req->reg_reset_pmds[0];
3119 flags = 0;
3122 if (cnum >= PMU_MAX_PMCS) {
3123 DPRINT(("pmc%u is invalid\n", cnum));
3124 goto error;
3127 pmc_type = pmu_conf->pmc_desc[cnum].type;
3128 pmc_pm = (value >> pmu_conf->pmc_desc[cnum].pm_pos) & 0x1;
3129 is_counting = (pmc_type & PFM_REG_COUNTING) == PFM_REG_COUNTING ? 1 : 0;
3130 is_monitor = (pmc_type & PFM_REG_MONITOR) == PFM_REG_MONITOR ? 1 : 0;
3132 /*
3133 * we reject all non implemented PMC as well
3134 * as attempts to modify PMC[0-3] which are used
3135 * as status registers by the PMU
3136 */
3137 if ((pmc_type & PFM_REG_IMPL) == 0 || (pmc_type & PFM_REG_CONTROL) == PFM_REG_CONTROL) {
3138 DPRINT(("pmc%u is unimplemented or no-access pmc_type=%x\n", cnum, pmc_type));
3139 goto error;
3141 wr_func = pmu_conf->pmc_desc[cnum].write_check;
3142 /*
3143 * If the PMC is a monitor, then if the value is not the default:
3144 * - system-wide session: PMCx.pm=1 (privileged monitor)
3145 * - per-task : PMCx.pm=0 (user monitor)
3146 */
3147 if (is_monitor && value != PMC_DFL_VAL(cnum) && is_system ^ pmc_pm) {
3148 DPRINT(("pmc%u pmc_pm=%lu is_system=%d\n",
3149 cnum,
3150 pmc_pm,
3151 is_system));
3152 goto error;
3155 if (is_counting) {
3156 /*
3157 * enforce generation of overflow interrupt. Necessary on all
3158 * CPUs.
3159 */
3160 value |= 1 << PMU_PMC_OI;
3162 if (reg_flags & PFM_REGFL_OVFL_NOTIFY) {
3163 flags |= PFM_REGFL_OVFL_NOTIFY;
3166 if (reg_flags & PFM_REGFL_RANDOM) flags |= PFM_REGFL_RANDOM;
3168 /* verify validity of smpl_pmds */
3169 if ((smpl_pmds & impl_pmds) != smpl_pmds) {
3170 DPRINT(("invalid smpl_pmds 0x%lx for pmc%u\n", smpl_pmds, cnum));
3171 goto error;
3174 /* verify validity of reset_pmds */
3175 if ((reset_pmds & impl_pmds) != reset_pmds) {
3176 DPRINT(("invalid reset_pmds 0x%lx for pmc%u\n", reset_pmds, cnum));
3177 goto error;
3179 } else {
3180 if (reg_flags & (PFM_REGFL_OVFL_NOTIFY|PFM_REGFL_RANDOM)) {
3181 DPRINT(("cannot set ovfl_notify or random on pmc%u\n", cnum));
3182 goto error;
3184 /* eventid on non-counting monitors are ignored */
3187 /*
3188 * execute write checker, if any
3189 */
3190 if (likely(expert_mode == 0 && wr_func)) {
3191 ret = (*wr_func)(task, ctx, cnum, &value, regs);
3192 if (ret) goto error;
3193 ret = -EINVAL;
3196 /*
3197 * no error on this register
3198 */
3199 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
3201 /*
3202 * Now we commit the changes to the software state
3203 */
3205 /*
3206 * update overflow information
3207 */
3208 if (is_counting) {
3209 /*
3210 * full flag update each time a register is programmed
3211 */
3212 ctx->ctx_pmds[cnum].flags = flags;
3214 ctx->ctx_pmds[cnum].reset_pmds[0] = reset_pmds;
3215 ctx->ctx_pmds[cnum].smpl_pmds[0] = smpl_pmds;
3216 ctx->ctx_pmds[cnum].eventid = req->reg_smpl_eventid;
3218 /*
3219 * Mark all PMDS to be accessed as used.
3221 * We do not keep track of PMC because we have to
3222 * systematically restore ALL of them.
3224 * We do not update the used_monitors mask, because
3225 * if we have not programmed them, then will be in
3226 * a quiescent state, therefore we will not need to
3227 * mask/restore then when context is MASKED.
3228 */
3229 CTX_USED_PMD(ctx, reset_pmds);
3230 CTX_USED_PMD(ctx, smpl_pmds);
3231 /*
3232 * make sure we do not try to reset on
3233 * restart because we have established new values
3234 */
3235 if (state == PFM_CTX_MASKED) ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
3237 /*
3238 * Needed in case the user does not initialize the equivalent
3239 * PMD. Clearing is done indirectly via pfm_reset_pmu_state() so there is no
3240 * possible leak here.
3241 */
3242 CTX_USED_PMD(ctx, pmu_conf->pmc_desc[cnum].dep_pmd[0]);
3244 /*
3245 * keep track of the monitor PMC that we are using.
3246 * we save the value of the pmc in ctx_pmcs[] and if
3247 * the monitoring is not stopped for the context we also
3248 * place it in the saved state area so that it will be
3249 * picked up later by the context switch code.
3251 * The value in ctx_pmcs[] can only be changed in pfm_write_pmcs().
3253 * The value in thread->pmcs[] may be modified on overflow, i.e., when
3254 * monitoring needs to be stopped.
3255 */
3256 if (is_monitor) CTX_USED_MONITOR(ctx, 1UL << cnum);
3258 /*
3259 * update context state
3260 */
3261 ctx->ctx_pmcs[cnum] = value;
3263 #ifndef XEN
3264 if (is_loaded) {
3265 /*
3266 * write thread state
3267 */
3268 if (is_system == 0) thread->pmcs[cnum] = value;
3270 /*
3271 * write hardware register if we can
3272 */
3273 if (can_access_pmu) {
3274 ia64_set_pmc(cnum, value);
3276 #ifdef CONFIG_SMP
3277 else {
3278 /*
3279 * per-task SMP only here
3281 * we are guaranteed that the task is not running on the other CPU,
3282 * we indicate that this PMD will need to be reloaded if the task
3283 * is rescheduled on the CPU it ran last on.
3284 */
3285 ctx->ctx_reload_pmcs[0] |= 1UL << cnum;
3287 #endif
3289 #endif
3291 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",
3292 cnum,
3293 value,
3294 is_loaded,
3295 can_access_pmu,
3296 flags,
3297 ctx->ctx_all_pmcs[0],
3298 ctx->ctx_used_pmds[0],
3299 ctx->ctx_pmds[cnum].eventid,
3300 smpl_pmds,
3301 reset_pmds,
3302 ctx->ctx_reload_pmcs[0],
3303 ctx->ctx_used_monitors[0],
3304 ctx->ctx_ovfl_regs[0]));
3307 /*
3308 * make sure the changes are visible
3309 */
3310 if (can_access_pmu) ia64_srlz_d();
3312 return 0;
3313 error:
3314 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3315 return ret;
3318 static int
3319 pfm_write_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3321 #ifndef XEN
3322 struct thread_struct *thread = NULL;
3323 #endif
3324 struct task_struct *task;
3325 pfarg_reg_t *req = (pfarg_reg_t *)arg;
3326 unsigned long value, hw_value, ovfl_mask;
3327 unsigned int cnum;
3328 int i, can_access_pmu = 0, state;
3329 int is_counting, is_loaded, is_system, expert_mode;
3330 int ret = -EINVAL;
3331 pfm_reg_check_t wr_func;
3334 state = ctx->ctx_state;
3335 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3336 is_system = ctx->ctx_fl_system;
3337 ovfl_mask = pmu_conf->ovfl_val;
3338 task = ctx->ctx_task;
3339 #ifdef XEN
3340 task = NULL;
3341 BUG_ON(regs != NULL);
3342 #endif
3344 if (unlikely(state == PFM_CTX_ZOMBIE)) return -EINVAL;
3346 #ifndef XEN
3347 /*
3348 * on both UP and SMP, we can only write to the PMC when the task is
3349 * the owner of the local PMU.
3350 */
3351 if (likely(is_loaded)) {
3352 thread = &task->thread;
3353 /*
3354 * In system wide and when the context is loaded, access can only happen
3355 * when the caller is running on the CPU being monitored by the session.
3356 * It does not have to be the owner (ctx_task) of the context per se.
3357 */
3358 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3359 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3360 return -EBUSY;
3362 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3364 #else
3365 /* XXX FIXME */
3366 if (state != PFM_CTX_UNLOADED) {
3367 gdprintk(XENLOG_DEBUG, "%s state %d\n", __func__, state);
3368 return -EBUSY;
3370 #endif
3371 expert_mode = pfm_sysctl.expert_mode;
3373 for (i = 0; i < count; i++, req++) {
3375 cnum = req->reg_num;
3376 value = req->reg_value;
3378 if (!PMD_IS_IMPL(cnum)) {
3379 DPRINT(("pmd[%u] is unimplemented or invalid\n", cnum));
3380 goto abort_mission;
3382 is_counting = PMD_IS_COUNTING(cnum);
3383 wr_func = pmu_conf->pmd_desc[cnum].write_check;
3385 /*
3386 * execute write checker, if any
3387 */
3388 if (unlikely(expert_mode == 0 && wr_func)) {
3389 unsigned long v = value;
3391 ret = (*wr_func)(task, ctx, cnum, &v, regs);
3392 if (ret) goto abort_mission;
3394 value = v;
3395 ret = -EINVAL;
3398 /*
3399 * no error on this register
3400 */
3401 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
3403 /*
3404 * now commit changes to software state
3405 */
3406 hw_value = value;
3408 /*
3409 * update virtualized (64bits) counter
3410 */
3411 if (is_counting) {
3412 /*
3413 * write context state
3414 */
3415 ctx->ctx_pmds[cnum].lval = value;
3417 /*
3418 * when context is load we use the split value
3419 */
3420 if (is_loaded) {
3421 hw_value = value & ovfl_mask;
3422 value = value & ~ovfl_mask;
3425 /*
3426 * update reset values (not just for counters)
3427 */
3428 ctx->ctx_pmds[cnum].long_reset = req->reg_long_reset;
3429 ctx->ctx_pmds[cnum].short_reset = req->reg_short_reset;
3431 /*
3432 * update randomization parameters (not just for counters)
3433 */
3434 ctx->ctx_pmds[cnum].seed = req->reg_random_seed;
3435 ctx->ctx_pmds[cnum].mask = req->reg_random_mask;
3437 /*
3438 * update context value
3439 */
3440 ctx->ctx_pmds[cnum].val = value;
3442 /*
3443 * Keep track of what we use
3445 * We do not keep track of PMC because we have to
3446 * systematically restore ALL of them.
3447 */
3448 CTX_USED_PMD(ctx, PMD_PMD_DEP(cnum));
3450 /*
3451 * mark this PMD register used as well
3452 */
3453 CTX_USED_PMD(ctx, RDEP(cnum));
3455 /*
3456 * make sure we do not try to reset on
3457 * restart because we have established new values
3458 */
3459 if (is_counting && state == PFM_CTX_MASKED) {
3460 ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
3463 /* XXX FIXME */
3464 #ifndef XEN
3465 if (is_loaded) {
3466 /*
3467 * write thread state
3468 */
3469 if (is_system == 0) thread->pmds[cnum] = hw_value;
3471 /*
3472 * write hardware register if we can
3473 */
3474 if (can_access_pmu) {
3475 ia64_set_pmd(cnum, hw_value);
3476 } else {
3477 #ifdef CONFIG_SMP
3478 /*
3479 * we are guaranteed that the task is not running on the other CPU,
3480 * we indicate that this PMD will need to be reloaded if the task
3481 * is rescheduled on the CPU it ran last on.
3482 */
3483 ctx->ctx_reload_pmds[0] |= 1UL << cnum;
3484 #endif
3487 #endif
3489 DPRINT(("pmd[%u]=0x%lx ld=%d apmu=%d, hw_value=0x%lx ctx_pmd=0x%lx short_reset=0x%lx "
3490 "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",
3491 cnum,
3492 value,
3493 is_loaded,
3494 can_access_pmu,
3495 hw_value,
3496 ctx->ctx_pmds[cnum].val,
3497 ctx->ctx_pmds[cnum].short_reset,
3498 ctx->ctx_pmds[cnum].long_reset,
3499 PMC_OVFL_NOTIFY(ctx, cnum) ? 'Y':'N',
3500 ctx->ctx_pmds[cnum].seed,
3501 ctx->ctx_pmds[cnum].mask,
3502 ctx->ctx_used_pmds[0],
3503 ctx->ctx_pmds[cnum].reset_pmds[0],
3504 ctx->ctx_reload_pmds[0],
3505 ctx->ctx_all_pmds[0],
3506 ctx->ctx_ovfl_regs[0]));
3509 /*
3510 * make changes visible
3511 */
3512 if (can_access_pmu) ia64_srlz_d();
3514 return 0;
3516 abort_mission:
3517 /*
3518 * for now, we have only one possibility for error
3519 */
3520 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3521 return ret;
3524 #ifndef XEN
3525 /*
3526 * By the way of PROTECT_CONTEXT(), interrupts are masked while we are in this function.
3527 * Therefore we know, we do not have to worry about the PMU overflow interrupt. If an
3528 * interrupt is delivered during the call, it will be kept pending until we leave, making
3529 * it appears as if it had been generated at the UNPROTECT_CONTEXT(). At least we are
3530 * guaranteed to return consistent data to the user, it may simply be old. It is not
3531 * trivial to treat the overflow while inside the call because you may end up in
3532 * some module sampling buffer code causing deadlocks.
3533 */
3534 static int
3535 pfm_read_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3537 struct thread_struct *thread = NULL;
3538 struct task_struct *task;
3539 unsigned long val = 0UL, lval, ovfl_mask, sval;
3540 pfarg_reg_t *req = (pfarg_reg_t *)arg;
3541 unsigned int cnum, reg_flags = 0;
3542 int i, can_access_pmu = 0, state;
3543 int is_loaded, is_system, is_counting, expert_mode;
3544 int ret = -EINVAL;
3545 pfm_reg_check_t rd_func;
3547 /*
3548 * access is possible when loaded only for
3549 * self-monitoring tasks or in UP mode
3550 */
3552 state = ctx->ctx_state;
3553 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3554 is_system = ctx->ctx_fl_system;
3555 ovfl_mask = pmu_conf->ovfl_val;
3556 task = ctx->ctx_task;
3558 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
3560 if (likely(is_loaded)) {
3561 thread = &task->thread;
3562 /*
3563 * In system wide and when the context is loaded, access can only happen
3564 * when the caller is running on the CPU being monitored by the session.
3565 * It does not have to be the owner (ctx_task) of the context per se.
3566 */
3567 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
3568 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3569 return -EBUSY;
3571 /*
3572 * this can be true when not self-monitoring only in UP
3573 */
3574 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
3576 if (can_access_pmu) ia64_srlz_d();
3578 expert_mode = pfm_sysctl.expert_mode;
3580 DPRINT(("ld=%d apmu=%d ctx_state=%d\n",
3581 is_loaded,
3582 can_access_pmu,
3583 state));
3585 /*
3586 * on both UP and SMP, we can only read the PMD from the hardware register when
3587 * the task is the owner of the local PMU.
3588 */
3590 for (i = 0; i < count; i++, req++) {
3592 cnum = req->reg_num;
3593 reg_flags = req->reg_flags;
3595 if (unlikely(!PMD_IS_IMPL(cnum))) goto error;
3596 /*
3597 * we can only read the register that we use. That includes
3598 * the one we explicitely initialize AND the one we want included
3599 * in the sampling buffer (smpl_regs).
3601 * Having this restriction allows optimization in the ctxsw routine
3602 * without compromising security (leaks)
3603 */
3604 if (unlikely(!CTX_IS_USED_PMD(ctx, cnum))) goto error;
3606 sval = ctx->ctx_pmds[cnum].val;
3607 lval = ctx->ctx_pmds[cnum].lval;
3608 is_counting = PMD_IS_COUNTING(cnum);
3610 /*
3611 * If the task is not the current one, then we check if the
3612 * PMU state is still in the local live register due to lazy ctxsw.
3613 * If true, then we read directly from the registers.
3614 */
3615 if (can_access_pmu){
3616 val = ia64_get_pmd(cnum);
3617 } else {
3618 /*
3619 * context has been saved
3620 * if context is zombie, then task does not exist anymore.
3621 * In this case, we use the full value saved in the context (pfm_flush_regs()).
3622 */
3623 val = is_loaded ? thread->pmds[cnum] : 0UL;
3625 rd_func = pmu_conf->pmd_desc[cnum].read_check;
3627 if (is_counting) {
3628 /*
3629 * XXX: need to check for overflow when loaded
3630 */
3631 val &= ovfl_mask;
3632 val += sval;
3635 /*
3636 * execute read checker, if any
3637 */
3638 if (unlikely(expert_mode == 0 && rd_func)) {
3639 unsigned long v = val;
3640 ret = (*rd_func)(ctx->ctx_task, ctx, cnum, &v, regs);
3641 if (ret) goto error;
3642 val = v;
3643 ret = -EINVAL;
3646 PFM_REG_RETFLAG_SET(reg_flags, 0);
3648 DPRINT(("pmd[%u]=0x%lx\n", cnum, val));
3650 /*
3651 * update register return value, abort all if problem during copy.
3652 * we only modify the reg_flags field. no check mode is fine because
3653 * access has been verified upfront in sys_perfmonctl().
3654 */
3655 req->reg_value = val;
3656 req->reg_flags = reg_flags;
3657 req->reg_last_reset_val = lval;
3660 return 0;
3662 error:
3663 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
3664 return ret;
3667 int
3668 pfm_mod_write_pmcs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3670 pfm_context_t *ctx;
3672 if (req == NULL) return -EINVAL;
3674 ctx = GET_PMU_CTX();
3676 if (ctx == NULL) return -EINVAL;
3678 /*
3679 * for now limit to current task, which is enough when calling
3680 * from overflow handler
3681 */
3682 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3684 return pfm_write_pmcs(ctx, req, nreq, regs);
3686 EXPORT_SYMBOL(pfm_mod_write_pmcs);
3688 int
3689 pfm_mod_read_pmds(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
3691 pfm_context_t *ctx;
3693 if (req == NULL) return -EINVAL;
3695 ctx = GET_PMU_CTX();
3697 if (ctx == NULL) return -EINVAL;
3699 /*
3700 * for now limit to current task, which is enough when calling
3701 * from overflow handler
3702 */
3703 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
3705 return pfm_read_pmds(ctx, req, nreq, regs);
3707 EXPORT_SYMBOL(pfm_mod_read_pmds);
3708 #endif
3710 /*
3711 * Only call this function when a process it trying to
3712 * write the debug registers (reading is always allowed)
3713 */
3714 int
3715 pfm_use_debug_registers(struct task_struct *task)
3717 pfm_context_t *ctx = task->thread.pfm_context;
3718 unsigned long flags;
3719 int ret = 0;
3721 if (pmu_conf->use_rr_dbregs == 0) return 0;
3723 DPRINT(("called for [%d]\n", task->pid));
3725 /*
3726 * do it only once
3727 */
3728 if (task->thread.flags & IA64_THREAD_DBG_VALID) return 0;
3730 /*
3731 * Even on SMP, we do not need to use an atomic here because
3732 * the only way in is via ptrace() and this is possible only when the
3733 * process is stopped. Even in the case where the ctxsw out is not totally
3734 * completed by the time we come here, there is no way the 'stopped' process
3735 * could be in the middle of fiddling with the pfm_write_ibr_dbr() routine.
3736 * So this is always safe.
3737 */
3738 if (ctx && ctx->ctx_fl_using_dbreg == 1) return -1;
3740 LOCK_PFS(flags);
3742 /*
3743 * We cannot allow setting breakpoints when system wide monitoring
3744 * sessions are using the debug registers.
3745 */
3746 if (pfm_sessions.pfs_sys_use_dbregs> 0)
3747 ret = -1;
3748 else
3749 pfm_sessions.pfs_ptrace_use_dbregs++;
3751 DPRINT(("ptrace_use_dbregs=%u sys_use_dbregs=%u by [%d] ret = %d\n",
3752 pfm_sessions.pfs_ptrace_use_dbregs,
3753 pfm_sessions.pfs_sys_use_dbregs,
3754 task->pid, ret));
3756 UNLOCK_PFS(flags);
3758 return ret;
3761 /*
3762 * This function is called for every task that exits with the
3763 * IA64_THREAD_DBG_VALID set. This indicates a task which was
3764 * able to use the debug registers for debugging purposes via
3765 * ptrace(). Therefore we know it was not using them for
3766 * perfmormance monitoring, so we only decrement the number
3767 * of "ptraced" debug register users to keep the count up to date
3768 */
3769 int
3770 pfm_release_debug_registers(struct task_struct *task)
3772 unsigned long flags;
3773 int ret;
3775 if (pmu_conf->use_rr_dbregs == 0) return 0;
3777 LOCK_PFS(flags);
3778 if (pfm_sessions.pfs_ptrace_use_dbregs == 0) {
3779 printk(KERN_ERR "perfmon: invalid release for [%d] ptrace_use_dbregs=0\n", task->pid);
3780 ret = -1;
3781 } else {
3782 pfm_sessions.pfs_ptrace_use_dbregs--;
3783 ret = 0;
3785 UNLOCK_PFS(flags);
3787 return ret;
3790 #ifndef XEN
3791 static int
3792 pfm_restart(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3794 struct task_struct *task;
3795 pfm_buffer_fmt_t *fmt;
3796 pfm_ovfl_ctrl_t rst_ctrl;
3797 int state, is_system;
3798 int ret = 0;
3800 state = ctx->ctx_state;
3801 fmt = ctx->ctx_buf_fmt;
3802 is_system = ctx->ctx_fl_system;
3803 task = PFM_CTX_TASK(ctx);
3805 switch(state) {
3806 case PFM_CTX_MASKED:
3807 break;
3808 case PFM_CTX_LOADED:
3809 if (CTX_HAS_SMPL(ctx) && fmt->fmt_restart_active) break;
3810 /* fall through */
3811 case PFM_CTX_UNLOADED:
3812 case PFM_CTX_ZOMBIE:
3813 DPRINT(("invalid state=%d\n", state));
3814 return -EBUSY;
3815 default:
3816 DPRINT(("state=%d, cannot operate (no active_restart handler)\n", state));
3817 return -EINVAL;
3820 /*
3821 * In system wide and when the context is loaded, access can only happen
3822 * when the caller is running on the CPU being monitored by the session.
3823 * It does not have to be the owner (ctx_task) of the context per se.
3824 */
3825 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
3826 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
3827 return -EBUSY;
3830 /* sanity check */
3831 if (unlikely(task == NULL)) {
3832 printk(KERN_ERR "perfmon: [%d] pfm_restart no task\n", current->pid);
3833 return -EINVAL;
3836 if (task == current || is_system) {
3838 fmt = ctx->ctx_buf_fmt;
3840 DPRINT(("restarting self %d ovfl=0x%lx\n",
3841 task->pid,
3842 ctx->ctx_ovfl_regs[0]));
3844 if (CTX_HAS_SMPL(ctx)) {
3846 prefetch(ctx->ctx_smpl_hdr);
3848 rst_ctrl.bits.mask_monitoring = 0;
3849 rst_ctrl.bits.reset_ovfl_pmds = 0;
3851 if (state == PFM_CTX_LOADED)
3852 ret = pfm_buf_fmt_restart_active(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
3853 else
3854 ret = pfm_buf_fmt_restart(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
3855 } else {
3856 rst_ctrl.bits.mask_monitoring = 0;
3857 rst_ctrl.bits.reset_ovfl_pmds = 1;
3860 if (ret == 0) {
3861 if (rst_ctrl.bits.reset_ovfl_pmds)
3862 pfm_reset_regs(ctx, ctx->ctx_ovfl_regs, PFM_PMD_LONG_RESET);
3864 if (rst_ctrl.bits.mask_monitoring == 0) {
3865 DPRINT(("resuming monitoring for [%d]\n", task->pid));
3867 if (state == PFM_CTX_MASKED) pfm_restore_monitoring(task);
3868 } else {
3869 DPRINT(("keeping monitoring stopped for [%d]\n", task->pid));
3871 // cannot use pfm_stop_monitoring(task, regs);
3874 /*
3875 * clear overflowed PMD mask to remove any stale information
3876 */
3877 ctx->ctx_ovfl_regs[0] = 0UL;
3879 /*
3880 * back to LOADED state
3881 */
3882 ctx->ctx_state = PFM_CTX_LOADED;
3884 /*
3885 * XXX: not really useful for self monitoring
3886 */
3887 ctx->ctx_fl_can_restart = 0;
3889 return 0;
3892 /*
3893 * restart another task
3894 */
3896 /*
3897 * When PFM_CTX_MASKED, we cannot issue a restart before the previous
3898 * one is seen by the task.
3899 */
3900 if (state == PFM_CTX_MASKED) {
3901 if (ctx->ctx_fl_can_restart == 0) return -EINVAL;
3902 /*
3903 * will prevent subsequent restart before this one is
3904 * seen by other task
3905 */
3906 ctx->ctx_fl_can_restart = 0;
3909 /*
3910 * if blocking, then post the semaphore is PFM_CTX_MASKED, i.e.
3911 * the task is blocked or on its way to block. That's the normal
3912 * restart path. If the monitoring is not masked, then the task
3913 * can be actively monitoring and we cannot directly intervene.
3914 * Therefore we use the trap mechanism to catch the task and
3915 * force it to reset the buffer/reset PMDs.
3917 * if non-blocking, then we ensure that the task will go into
3918 * pfm_handle_work() before returning to user mode.
3920 * We cannot explicitely reset another task, it MUST always
3921 * be done by the task itself. This works for system wide because
3922 * the tool that is controlling the session is logically doing
3923 * "self-monitoring".
3924 */
3925 if (CTX_OVFL_NOBLOCK(ctx) == 0 && state == PFM_CTX_MASKED) {
3926 DPRINT(("unblocking [%d] \n", task->pid));
3927 complete(&ctx->ctx_restart_done);
3928 } else {
3929 DPRINT(("[%d] armed exit trap\n", task->pid));
3931 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_RESET;
3933 PFM_SET_WORK_PENDING(task, 1);
3935 pfm_set_task_notify(task);
3937 /*
3938 * XXX: send reschedule if task runs on another CPU
3939 */
3941 return 0;
3944 static int
3945 pfm_debug(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3947 unsigned int m = *(unsigned int *)arg;
3949 pfm_sysctl.debug = m == 0 ? 0 : 1;
3951 printk(KERN_INFO "perfmon debugging %s (timing reset)\n", pfm_sysctl.debug ? "on" : "off");
3953 if (m == 0) {
3954 memset(pfm_stats, 0, sizeof(pfm_stats));
3955 for(m=0; m < NR_CPUS; m++) pfm_stats[m].pfm_ovfl_intr_cycles_min = ~0UL;
3957 return 0;
3959 #endif
3961 /*
3962 * arg can be NULL and count can be zero for this function
3963 */
3964 static int
3965 pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
3967 #ifndef XEN
3968 struct thread_struct *thread = NULL;
3969 #endif
3970 struct task_struct *task;
3971 pfarg_dbreg_t *req = (pfarg_dbreg_t *)arg;
3972 unsigned long flags;
3973 dbreg_t dbreg;
3974 unsigned int rnum;
3975 int first_time;
3976 int ret = 0, state;
3977 int i, can_access_pmu = 0;
3978 int is_system, is_loaded;
3980 if (pmu_conf->use_rr_dbregs == 0) return -EINVAL;
3982 state = ctx->ctx_state;
3983 is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
3984 is_system = ctx->ctx_fl_system;
3985 task = ctx->ctx_task;
3986 #ifdef XEN
3987 task = NULL;
3988 BUG_ON(regs != NULL);
3989 /* currently dbrs, ibrs aren't supported */
3990 BUG();
3991 #endif
3993 if (state == PFM_CTX_ZOMBIE) return -EINVAL;
3995 /*
3996 * on both UP and SMP, we can only write to the PMC when the task is
3997 * the owner of the local PMU.
3998 */
3999 if (is_loaded) {
4000 #ifdef XEN
4001 /* XXX */
4002 return -EBUSY;
4003 #else
4004 thread = &task->thread;
4005 /*
4006 * In system wide and when the context is loaded, access can only happen
4007 * when the caller is running on the CPU being monitored by the session.
4008 * It does not have to be the owner (ctx_task) of the context per se.
4009 */
4010 if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
4011 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
4012 return -EBUSY;
4014 can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
4015 #endif
4018 /*
4019 * we do not need to check for ipsr.db because we do clear ibr.x, dbr.r, and dbr.w
4020 * ensuring that no real breakpoint can be installed via this call.
4022 * IMPORTANT: regs can be NULL in this function
4023 */
4025 first_time = ctx->ctx_fl_using_dbreg == 0;
4027 /*
4028 * don't bother if we are loaded and task is being debugged
4029 */
4030 #ifndef XEN
4031 if (is_loaded && (thread->flags & IA64_THREAD_DBG_VALID) != 0) {
4032 DPRINT(("debug registers already in use for [%d]\n", task->pid));
4033 return -EBUSY;
4035 #else
4036 /* Currently no support for is_loaded, see -EBUSY above */
4037 #endif
4039 /*
4040 * check for debug registers in system wide mode
4042 * If though a check is done in pfm_context_load(),
4043 * we must repeat it here, in case the registers are
4044 * written after the context is loaded
4045 */
4046 if (is_loaded) {
4047 LOCK_PFS(flags);
4049 if (first_time && is_system) {
4050 if (pfm_sessions.pfs_ptrace_use_dbregs)
4051 ret = -EBUSY;
4052 else
4053 pfm_sessions.pfs_sys_use_dbregs++;
4055 UNLOCK_PFS(flags);
4058 if (ret != 0) return ret;
4060 /*
4061 * mark ourself as user of the debug registers for
4062 * perfmon purposes.
4063 */
4064 ctx->ctx_fl_using_dbreg = 1;
4066 /*
4067 * clear hardware registers to make sure we don't
4068 * pick up stale state.
4070 * for a system wide session, we do not use
4071 * thread.dbr, thread.ibr because this process
4072 * never leaves the current CPU and the state
4073 * is shared by all processes running on it
4074 */
4075 if (first_time && can_access_pmu) {
4076 #ifndef XEN
4077 DPRINT(("[%d] clearing ibrs, dbrs\n", task->pid));
4078 #endif
4079 for (i=0; i < pmu_conf->num_ibrs; i++) {
4080 ia64_set_ibr(i, 0UL);
4081 ia64_dv_serialize_instruction();
4083 ia64_srlz_i();
4084 for (i=0; i < pmu_conf->num_dbrs; i++) {
4085 ia64_set_dbr(i, 0UL);
4086 ia64_dv_serialize_data();
4088 ia64_srlz_d();
4091 /*
4092 * Now install the values into the registers
4093 */
4094 for (i = 0; i < count; i++, req++) {
4096 rnum = req->dbreg_num;
4097 dbreg.val = req->dbreg_value;
4099 ret = -EINVAL;
4101 if ((mode == PFM_CODE_RR && rnum >= PFM_NUM_IBRS) || ((mode == PFM_DATA_RR) && rnum >= PFM_NUM_DBRS)) {
4102 DPRINT(("invalid register %u val=0x%lx mode=%d i=%d count=%d\n",
4103 rnum, dbreg.val, mode, i, count));
4105 goto abort_mission;
4108 /*
4109 * make sure we do not install enabled breakpoint
4110 */
4111 if (rnum & 0x1) {
4112 if (mode == PFM_CODE_RR)
4113 dbreg.ibr.ibr_x = 0;
4114 else
4115 dbreg.dbr.dbr_r = dbreg.dbr.dbr_w = 0;
4118 PFM_REG_RETFLAG_SET(req->dbreg_flags, 0);
4120 /*
4121 * Debug registers, just like PMC, can only be modified
4122 * by a kernel call. Moreover, perfmon() access to those
4123 * registers are centralized in this routine. The hardware
4124 * does not modify the value of these registers, therefore,
4125 * if we save them as they are written, we can avoid having
4126 * to save them on context switch out. This is made possible
4127 * by the fact that when perfmon uses debug registers, ptrace()
4128 * won't be able to modify them concurrently.
4129 */
4130 if (mode == PFM_CODE_RR) {
4131 CTX_USED_IBR(ctx, rnum);
4133 if (can_access_pmu) {
4134 ia64_set_ibr(rnum, dbreg.val);
4135 ia64_dv_serialize_instruction();
4138 ctx->ctx_ibrs[rnum] = dbreg.val;
4140 DPRINT(("write ibr%u=0x%lx used_ibrs=0x%x ld=%d apmu=%d\n",
4141 rnum, dbreg.val, ctx->ctx_used_ibrs[0], is_loaded, can_access_pmu));
4142 } else {
4143 CTX_USED_DBR(ctx, rnum);
4145 if (can_access_pmu) {
4146 ia64_set_dbr(rnum, dbreg.val);
4147 ia64_dv_serialize_data();
4149 ctx->ctx_dbrs[rnum] = dbreg.val;
4151 DPRINT(("write dbr%u=0x%lx used_dbrs=0x%x ld=%d apmu=%d\n",
4152 rnum, dbreg.val, ctx->ctx_used_dbrs[0], is_loaded, can_access_pmu));
4156 return 0;
4158 abort_mission:
4159 /*
4160 * in case it was our first attempt, we undo the global modifications
4161 */
4162 if (first_time) {
4163 LOCK_PFS(flags);
4164 if (ctx->ctx_fl_system) {
4165 pfm_sessions.pfs_sys_use_dbregs--;
4167 UNLOCK_PFS(flags);
4168 ctx->ctx_fl_using_dbreg = 0;
4170 /*
4171 * install error return flag
4172 */
4173 PFM_REG_RETFLAG_SET(req->dbreg_flags, PFM_REG_RETFL_EINVAL);
4175 return ret;
4178 static int
4179 pfm_write_ibrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4181 return pfm_write_ibr_dbr(PFM_CODE_RR, ctx, arg, count, regs);
4184 static int
4185 pfm_write_dbrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4187 return pfm_write_ibr_dbr(PFM_DATA_RR, ctx, arg, count, regs);
4190 int
4191 pfm_mod_write_ibrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
4193 pfm_context_t *ctx;
4195 if (req == NULL) return -EINVAL;
4197 ctx = GET_PMU_CTX();
4199 if (ctx == NULL) return -EINVAL;
4201 /*
4202 * for now limit to current task, which is enough when calling
4203 * from overflow handler
4204 */
4205 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
4207 return pfm_write_ibrs(ctx, req, nreq, regs);
4209 EXPORT_SYMBOL(pfm_mod_write_ibrs);
4211 int
4212 pfm_mod_write_dbrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
4214 pfm_context_t *ctx;
4216 if (req == NULL) return -EINVAL;
4218 ctx = GET_PMU_CTX();
4220 if (ctx == NULL) return -EINVAL;
4222 /*
4223 * for now limit to current task, which is enough when calling
4224 * from overflow handler
4225 */
4226 if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
4228 return pfm_write_dbrs(ctx, req, nreq, regs);
4230 EXPORT_SYMBOL(pfm_mod_write_dbrs);
4233 static int
4234 pfm_get_features(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4236 pfarg_features_t *req = (pfarg_features_t *)arg;
4238 req->ft_version = PFM_VERSION;
4239 return 0;
4242 #ifndef XEN
4243 static int
4244 pfm_stop(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4246 struct pt_regs *tregs;
4247 struct task_struct *task = PFM_CTX_TASK(ctx);
4248 int state, is_system;
4250 state = ctx->ctx_state;
4251 is_system = ctx->ctx_fl_system;
4253 /*
4254 * context must be attached to issue the stop command (includes LOADED,MASKED,ZOMBIE)
4255 */
4256 if (state == PFM_CTX_UNLOADED) return -EINVAL;
4258 /*
4259 * In system wide and when the context is loaded, access can only happen
4260 * when the caller is running on the CPU being monitored by the session.
4261 * It does not have to be the owner (ctx_task) of the context per se.
4262 */
4263 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
4264 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
4265 return -EBUSY;
4267 DPRINT(("task [%d] ctx_state=%d is_system=%d\n",
4268 PFM_CTX_TASK(ctx)->pid,
4269 state,
4270 is_system));
4271 /*
4272 * in system mode, we need to update the PMU directly
4273 * and the user level state of the caller, which may not
4274 * necessarily be the creator of the context.
4275 */
4276 if (is_system) {
4277 /*
4278 * Update local PMU first
4280 * disable dcr pp
4281 */
4282 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
4283 ia64_srlz_i();
4285 /*
4286 * update local cpuinfo
4287 */
4288 PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
4290 /*
4291 * stop monitoring, does srlz.i
4292 */
4293 pfm_clear_psr_pp();
4295 /*
4296 * stop monitoring in the caller
4297 */
4298 ia64_psr(regs)->pp = 0;
4300 return 0;
4302 /*
4303 * per-task mode
4304 */
4306 if (task == current) {
4307 /* stop monitoring at kernel level */
4308 pfm_clear_psr_up();
4310 /*
4311 * stop monitoring at the user level
4312 */
4313 ia64_psr(regs)->up = 0;
4314 } else {
4315 tregs = task_pt_regs(task);
4317 /*
4318 * stop monitoring at the user level
4319 */
4320 ia64_psr(tregs)->up = 0;
4322 /*
4323 * monitoring disabled in kernel at next reschedule
4324 */
4325 ctx->ctx_saved_psr_up = 0;
4326 DPRINT(("task=[%d]\n", task->pid));
4328 return 0;
4332 static int
4333 pfm_start(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4335 struct pt_regs *tregs;
4336 int state, is_system;
4338 state = ctx->ctx_state;
4339 is_system = ctx->ctx_fl_system;
4341 if (state != PFM_CTX_LOADED) return -EINVAL;
4343 /*
4344 * In system wide and when the context is loaded, access can only happen
4345 * when the caller is running on the CPU being monitored by the session.
4346 * It does not have to be the owner (ctx_task) of the context per se.
4347 */
4348 if (is_system && ctx->ctx_cpu != smp_processor_id()) {
4349 DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
4350 return -EBUSY;
4353 /*
4354 * in system mode, we need to update the PMU directly
4355 * and the user level state of the caller, which may not
4356 * necessarily be the creator of the context.
4357 */
4358 if (is_system) {
4360 /*
4361 * set user level psr.pp for the caller
4362 */
4363 ia64_psr(regs)->pp = 1;
4365 /*
4366 * now update the local PMU and cpuinfo
4367 */
4368 PFM_CPUINFO_SET(PFM_CPUINFO_DCR_PP);
4370 /*
4371 * start monitoring at kernel level
4372 */
4373 pfm_set_psr_pp();
4375 /* enable dcr pp */
4376 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
4377 ia64_srlz_i();
4379 return 0;
4382 /*
4383 * per-process mode
4384 */
4386 if (ctx->ctx_task == current) {
4388 /* start monitoring at kernel level */
4389 pfm_set_psr_up();
4391 /*
4392 * activate monitoring at user level
4393 */
4394 ia64_psr(regs)->up = 1;
4396 } else {
4397 tregs = task_pt_regs(ctx->ctx_task);
4399 /*
4400 * start monitoring at the kernel level the next
4401 * time the task is scheduled
4402 */
4403 ctx->ctx_saved_psr_up = IA64_PSR_UP;
4405 /*
4406 * activate monitoring at user level
4407 */
4408 ia64_psr(tregs)->up = 1;
4410 return 0;
4413 static int
4414 pfm_get_pmc_reset(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4416 pfarg_reg_t *req = (pfarg_reg_t *)arg;
4417 unsigned int cnum;
4418 int i;
4419 int ret = -EINVAL;
4421 for (i = 0; i < count; i++, req++) {
4423 cnum = req->reg_num;
4425 if (!PMC_IS_IMPL(cnum)) goto abort_mission;
4427 req->reg_value = PMC_DFL_VAL(cnum);
4429 PFM_REG_RETFLAG_SET(req->reg_flags, 0);
4431 DPRINT(("pmc_reset_val pmc[%u]=0x%lx\n", cnum, req->reg_value));
4433 return 0;
4435 abort_mission:
4436 PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
4437 return ret;
4440 static int
4441 pfm_check_task_exist(pfm_context_t *ctx)
4443 struct task_struct *g, *t;
4444 int ret = -ESRCH;
4446 read_lock(&tasklist_lock);
4448 do_each_thread (g, t) {
4449 if (t->thread.pfm_context == ctx) {
4450 ret = 0;
4451 break;
4453 } while_each_thread (g, t);
4455 read_unlock(&tasklist_lock);
4457 DPRINT(("pfm_check_task_exist: ret=%d ctx=%p\n", ret, ctx));
4459 return ret;
4461 #endif
4463 static int
4464 pfm_context_load(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4466 struct task_struct *task;
4467 #ifndef XEN
4468 struct thread_struct *thread;
4469 #endif
4470 struct pfm_context_t *old;
4471 unsigned long flags;
4472 #ifndef CONFIG_SMP
4473 struct task_struct *owner_task = NULL;
4474 #endif
4475 pfarg_load_t *req = (pfarg_load_t *)arg;
4476 unsigned long *pmcs_source, *pmds_source;
4477 int the_cpu;
4478 int ret = 0;
4479 int state, is_system, set_dbregs = 0;
4481 state = ctx->ctx_state;
4482 is_system = ctx->ctx_fl_system;
4483 #ifdef XEN
4484 task = NULL;
4485 old = NULL;
4486 pmcs_source = pmds_source = NULL;
4487 #ifndef CONFIG_SMP
4488 owner_task = NULL;
4489 #endif
4490 flags = 0;
4491 BUG_ON(count != 0);
4492 BUG_ON(regs != NULL);
4493 #endif
4494 /*
4495 * can only load from unloaded or terminated state
4496 */
4497 if (state != PFM_CTX_UNLOADED) {
4498 DPRINT(("cannot load to [%d], invalid ctx_state=%d\n",
4499 req->load_pid,
4500 ctx->ctx_state));
4501 return -EBUSY;
4504 #ifndef XEN
4505 DPRINT(("load_pid [%d] using_dbreg=%d\n", req->load_pid, ctx->ctx_fl_using_dbreg));
4507 if (CTX_OVFL_NOBLOCK(ctx) == 0 && req->load_pid == current->pid) {
4508 DPRINT(("cannot use blocking mode on self\n"));
4509 return -EINVAL;
4512 ret = pfm_get_task(ctx, req->load_pid, &task);
4513 if (ret) {
4514 DPRINT(("load_pid [%d] get_task=%d\n", req->load_pid, ret));
4515 return ret;
4518 ret = -EINVAL;
4520 /*
4521 * system wide is self monitoring only
4522 */
4523 if (is_system && task != current) {
4524 DPRINT(("system wide is self monitoring only load_pid=%d\n",
4525 req->load_pid));
4526 goto error;
4529 thread = &task->thread;
4530 #else
4531 BUG_ON(!spin_is_locked(&ctx->ctx_lock));
4532 if (!is_system) {
4533 ret = -EINVAL;
4534 goto error;
4536 #endif
4538 ret = 0;
4539 #ifndef XEN
4540 /*
4541 * cannot load a context which is using range restrictions,
4542 * into a task that is being debugged.
4543 */
4544 if (ctx->ctx_fl_using_dbreg) {
4545 if (thread->flags & IA64_THREAD_DBG_VALID) {
4546 ret = -EBUSY;
4547 DPRINT(("load_pid [%d] task is debugged, cannot load range restrictions\n", req->load_pid));
4548 goto error;
4550 LOCK_PFS(flags);
4552 if (is_system) {
4553 if (pfm_sessions.pfs_ptrace_use_dbregs) {
4554 DPRINT(("cannot load [%d] dbregs in use\n", task->pid));
4555 ret = -EBUSY;
4556 } else {
4557 pfm_sessions.pfs_sys_use_dbregs++;
4558 DPRINT(("load [%d] increased sys_use_dbreg=%u\n", task->pid, pfm_sessions.pfs_sys_use_dbregs));
4559 set_dbregs = 1;
4563 UNLOCK_PFS(flags);
4565 if (ret) goto error;
4567 #else
4568 BUG_ON(ctx->ctx_fl_using_dbreg);
4569 #endif
4571 /*
4572 * SMP system-wide monitoring implies self-monitoring.
4574 * The programming model expects the task to
4575 * be pinned on a CPU throughout the session.
4576 * Here we take note of the current CPU at the
4577 * time the context is loaded. No call from
4578 * another CPU will be allowed.
4580 * The pinning via shed_setaffinity()
4581 * must be done by the calling task prior
4582 * to this call.
4584 * systemwide: keep track of CPU this session is supposed to run on
4585 */
4586 the_cpu = ctx->ctx_cpu = smp_processor_id();
4588 ret = -EBUSY;
4589 /*
4590 * now reserve the session
4591 */
4592 ret = pfm_reserve_session(current, is_system, the_cpu);
4593 if (ret) goto error;
4595 /*
4596 * task is necessarily stopped at this point.
4598 * If the previous context was zombie, then it got removed in
4599 * pfm_save_regs(). Therefore we should not see it here.
4600 * If we see a context, then this is an active context
4602 * XXX: needs to be atomic
4603 */
4604 #ifndef XEN
4605 DPRINT(("before cmpxchg() old_ctx=%p new_ctx=%p\n",
4606 thread->pfm_context, ctx));
4608 ret = -EBUSY;
4609 old = ia64_cmpxchg(acq, &thread->pfm_context, NULL, ctx, sizeof(pfm_context_t *));
4610 if (old != NULL) {
4611 DPRINT(("load_pid [%d] already has a context\n", req->load_pid));
4612 goto error_unres;
4615 pfm_reset_msgq(ctx);
4616 #endif
4618 ctx->ctx_state = PFM_CTX_LOADED;
4620 /*
4621 * link context to task
4622 */
4623 ctx->ctx_task = task;
4625 if (is_system) {
4626 /*
4627 * we load as stopped
4628 */
4629 PFM_CPUINFO_SET(PFM_CPUINFO_SYST_WIDE);
4630 PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
4632 if (ctx->ctx_fl_excl_idle) PFM_CPUINFO_SET(PFM_CPUINFO_EXCL_IDLE);
4633 } else {
4634 #ifndef XEN
4635 thread->flags |= IA64_THREAD_PM_VALID;
4636 #else
4637 BUG();
4638 #endif
4641 #ifndef XEN
4642 /*
4643 * propagate into thread-state
4644 */
4645 pfm_copy_pmds(task, ctx);
4646 pfm_copy_pmcs(task, ctx);
4648 pmcs_source = thread->pmcs;
4649 pmds_source = thread->pmds;
4651 /*
4652 * always the case for system-wide
4653 */
4654 if (task == current) {
4656 if (is_system == 0) {
4658 /* allow user level control */
4659 ia64_psr(regs)->sp = 0;
4660 DPRINT(("clearing psr.sp for [%d]\n", task->pid));
4662 SET_LAST_CPU(ctx, smp_processor_id());
4663 INC_ACTIVATION();
4664 SET_ACTIVATION(ctx);
4665 #ifndef CONFIG_SMP
4666 /*
4667 * push the other task out, if any
4668 */
4669 owner_task = GET_PMU_OWNER();
4670 if (owner_task) pfm_lazy_save_regs(owner_task);
4671 #endif
4673 /*
4674 * load all PMD from ctx to PMU (as opposed to thread state)
4675 * restore all PMC from ctx to PMU
4676 */
4677 pfm_restore_pmds(pmds_source, ctx->ctx_all_pmds[0]);
4678 pfm_restore_pmcs(pmcs_source, ctx->ctx_all_pmcs[0]);
4680 ctx->ctx_reload_pmcs[0] = 0UL;
4681 ctx->ctx_reload_pmds[0] = 0UL;
4683 /*
4684 * guaranteed safe by earlier check against DBG_VALID
4685 */
4686 if (ctx->ctx_fl_using_dbreg) {
4687 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
4688 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
4690 /*
4691 * set new ownership
4692 */
4693 SET_PMU_OWNER(task, ctx);
4695 DPRINT(("context loaded on PMU for [%d]\n", task->pid));
4696 } else {
4697 /*
4698 * when not current, task MUST be stopped, so this is safe
4699 */
4700 regs = task_pt_regs(task);
4702 /* force a full reload */
4703 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
4704 SET_LAST_CPU(ctx, -1);
4706 /* initial saved psr (stopped) */
4707 ctx->ctx_saved_psr_up = 0UL;
4708 ia64_psr(regs)->up = ia64_psr(regs)->pp = 0;
4710 #else
4711 BUG_ON(!is_system);
4713 /* load pmds, pmcs */
4714 xenpfm_restore_pmds(ctx);
4715 xenpfm_restore_pmcs(ctx);
4717 ctx->ctx_reload_pmcs[0] = 0UL;
4718 ctx->ctx_reload_pmds[0] = 0UL;
4720 BUG_ON(ctx->ctx_fl_using_dbreg);
4722 SET_PMU_OWNER(NULL, ctx);
4723 #endif
4725 ret = 0;
4727 #ifndef XEN
4728 error_unres:
4729 if (ret) pfm_unreserve_session(ctx, ctx->ctx_fl_system, the_cpu);
4730 #endif
4731 error:
4732 #ifndef XEN
4733 /*
4734 * we must undo the dbregs setting (for system-wide)
4735 */
4736 if (ret && set_dbregs) {
4737 LOCK_PFS(flags);
4738 pfm_sessions.pfs_sys_use_dbregs--;
4739 UNLOCK_PFS(flags);
4741 /*
4742 * release task, there is now a link with the context
4743 */
4744 if (is_system == 0 && task != current) {
4745 pfm_put_task(task);
4747 if (ret == 0) {
4748 ret = pfm_check_task_exist(ctx);
4749 if (ret) {
4750 ctx->ctx_state = PFM_CTX_UNLOADED;
4751 ctx->ctx_task = NULL;
4755 #else
4756 BUG_ON(set_dbregs);
4757 #endif
4758 return ret;
4761 /*
4762 * in this function, we do not need to increase the use count
4763 * for the task via get_task_struct(), because we hold the
4764 * context lock. If the task were to disappear while having
4765 * a context attached, it would go through pfm_exit_thread()
4766 * which also grabs the context lock and would therefore be blocked
4767 * until we are here.
4768 */
4769 static void pfm_flush_pmds(struct task_struct *, pfm_context_t *ctx);
4771 static int
4772 pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
4774 struct task_struct *task = PFM_CTX_TASK(ctx);
4775 struct pt_regs *tregs;
4776 int prev_state, is_system;
4777 int ret;
4779 #ifndef XEN
4780 DPRINT(("ctx_state=%d task [%d]\n", ctx->ctx_state, task ? task->pid : -1));
4781 #else
4782 task = NULL;
4783 tregs = NULL;
4784 BUG_ON(arg != NULL);
4785 BUG_ON(count != 0);
4786 BUG_ON(regs != NULL);
4787 #endif
4789 prev_state = ctx->ctx_state;
4790 is_system = ctx->ctx_fl_system;
4792 /*
4793 * unload only when necessary
4794 */
4795 if (prev_state == PFM_CTX_UNLOADED) {
4796 DPRINT(("ctx_state=%d, nothing to do\n", prev_state));
4797 return 0;
4800 /*
4801 * clear psr and dcr bits
4802 */
4803 #ifndef XEN
4804 ret = pfm_stop(ctx, NULL, 0, regs);
4805 if (ret) return ret;
4806 #else
4807 /* caller does it by hand */
4808 ret = 0;
4809 #endif
4811 ctx->ctx_state = PFM_CTX_UNLOADED;
4813 /*
4814 * in system mode, we need to update the PMU directly
4815 * and the user level state of the caller, which may not
4816 * necessarily be the creator of the context.
4817 */
4818 if (is_system) {
4820 /*
4821 * Update cpuinfo
4823 * local PMU is taken care of in pfm_stop()
4824 */
4825 PFM_CPUINFO_CLEAR(PFM_CPUINFO_SYST_WIDE);
4826 PFM_CPUINFO_CLEAR(PFM_CPUINFO_EXCL_IDLE);
4828 /*
4829 * save PMDs in context
4830 * release ownership
4831 */
4832 pfm_flush_pmds(current, ctx);
4834 /*
4835 * at this point we are done with the PMU
4836 * so we can unreserve the resource.
4837 */
4838 if (prev_state != PFM_CTX_ZOMBIE)
4839 pfm_unreserve_session(ctx, 1 , ctx->ctx_cpu);
4841 #ifndef XEN
4842 /*
4843 * disconnect context from task
4844 */
4845 task->thread.pfm_context = NULL;
4846 #endif
4847 /*
4848 * disconnect task from context
4849 */
4850 ctx->ctx_task = NULL;
4852 /*
4853 * There is nothing more to cleanup here.
4854 */
4855 return 0;
4858 #ifndef XEN
4859 /*
4860 * per-task mode
4861 */
4862 tregs = task == current ? regs : task_pt_regs(task);
4864 if (task == current) {
4865 /*
4866 * cancel user level control
4867 */
4868 ia64_psr(regs)->sp = 1;
4870 DPRINT(("setting psr.sp for [%d]\n", task->pid));
4872 /*
4873 * save PMDs to context
4874 * release ownership
4875 */
4876 pfm_flush_pmds(task, ctx);
4878 /*
4879 * at this point we are done with the PMU
4880 * so we can unreserve the resource.
4882 * when state was ZOMBIE, we have already unreserved.
4883 */
4884 if (prev_state != PFM_CTX_ZOMBIE)
4885 pfm_unreserve_session(ctx, 0 , ctx->ctx_cpu);
4887 /*
4888 * reset activation counter and psr
4889 */
4890 ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
4891 SET_LAST_CPU(ctx, -1);
4893 /*
4894 * PMU state will not be restored
4895 */
4896 task->thread.flags &= ~IA64_THREAD_PM_VALID;
4898 /*
4899 * break links between context and task
4900 */
4901 task->thread.pfm_context = NULL;
4902 ctx->ctx_task = NULL;
4904 PFM_SET_WORK_PENDING(task, 0);
4906 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
4907 ctx->ctx_fl_can_restart = 0;
4908 ctx->ctx_fl_going_zombie = 0;
4910 DPRINT(("disconnected [%d] from context\n", task->pid));
4912 return 0;
4913 #else
4914 BUG();
4915 return -EINVAL;
4916 #endif
4920 #ifndef XEN
4921 /*
4922 * called only from exit_thread(): task == current
4923 * we come here only if current has a context attached (loaded or masked)
4924 */
4925 void
4926 pfm_exit_thread(struct task_struct *task)
4928 pfm_context_t *ctx;
4929 unsigned long flags;
4930 struct pt_regs *regs = task_pt_regs(task);
4931 int ret, state;
4932 int free_ok = 0;
4934 ctx = PFM_GET_CTX(task);
4936 PROTECT_CTX(ctx, flags);
4938 DPRINT(("state=%d task [%d]\n", ctx->ctx_state, task->pid));
4940 state = ctx->ctx_state;
4941 switch(state) {
4942 case PFM_CTX_UNLOADED:
4943 /*
4944 * only comes to thios function if pfm_context is not NULL, i.e., cannot
4945 * be in unloaded state
4946 */
4947 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] ctx unloaded\n", task->pid);
4948 break;
4949 case PFM_CTX_LOADED:
4950 case PFM_CTX_MASKED:
4951 ret = pfm_context_unload(ctx, NULL, 0, regs);
4952 if (ret) {
4953 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task->pid, state, ret);
4955 DPRINT(("ctx unloaded for current state was %d\n", state));
4957 pfm_end_notify_user(ctx);
4958 break;
4959 case PFM_CTX_ZOMBIE:
4960 ret = pfm_context_unload(ctx, NULL, 0, regs);
4961 if (ret) {
4962 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task->pid, state, ret);
4964 free_ok = 1;
4965 break;
4966 default:
4967 printk(KERN_ERR "perfmon: pfm_exit_thread [%d] unexpected state=%d\n", task->pid, state);
4968 break;
4970 UNPROTECT_CTX(ctx, flags);
4972 { u64 psr = pfm_get_psr();
4973 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
4974 BUG_ON(GET_PMU_OWNER());
4975 BUG_ON(ia64_psr(regs)->up);
4976 BUG_ON(ia64_psr(regs)->pp);
4979 /*
4980 * All memory free operations (especially for vmalloc'ed memory)
4981 * MUST be done with interrupts ENABLED.
4982 */
4983 if (free_ok) pfm_context_free(ctx);
4986 /*
4987 * functions MUST be listed in the increasing order of their index (see permfon.h)
4988 */
4989 #define PFM_CMD(name, flags, arg_count, arg_type, getsz) { name, #name, flags, arg_count, sizeof(arg_type), getsz }
4990 #define PFM_CMD_S(name, flags) { name, #name, flags, 0, 0, NULL }
4991 #define PFM_CMD_PCLRWS (PFM_CMD_FD|PFM_CMD_ARG_RW|PFM_CMD_STOP)
4992 #define PFM_CMD_PCLRW (PFM_CMD_FD|PFM_CMD_ARG_RW)
4993 #define PFM_CMD_NONE { NULL, "no-cmd", 0, 0, 0, NULL}
4995 static pfm_cmd_desc_t pfm_cmd_tab[]={
4996 /* 0 */PFM_CMD_NONE,
4997 /* 1 */PFM_CMD(pfm_write_pmcs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4998 /* 2 */PFM_CMD(pfm_write_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
4999 /* 3 */PFM_CMD(pfm_read_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
5000 /* 4 */PFM_CMD_S(pfm_stop, PFM_CMD_PCLRWS),
5001 /* 5 */PFM_CMD_S(pfm_start, PFM_CMD_PCLRWS),
5002 /* 6 */PFM_CMD_NONE,
5003 /* 7 */PFM_CMD_NONE,
5004 /* 8 */PFM_CMD(pfm_context_create, PFM_CMD_ARG_RW, 1, pfarg_context_t, pfm_ctx_getsize),
5005 /* 9 */PFM_CMD_NONE,
5006 /* 10 */PFM_CMD_S(pfm_restart, PFM_CMD_PCLRW),
5007 /* 11 */PFM_CMD_NONE,
5008 /* 12 */PFM_CMD(pfm_get_features, PFM_CMD_ARG_RW, 1, pfarg_features_t, NULL),
5009 /* 13 */PFM_CMD(pfm_debug, 0, 1, unsigned int, NULL),
5010 /* 14 */PFM_CMD_NONE,
5011 /* 15 */PFM_CMD(pfm_get_pmc_reset, PFM_CMD_ARG_RW, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
5012 /* 16 */PFM_CMD(pfm_context_load, PFM_CMD_PCLRWS, 1, pfarg_load_t, NULL),
5013 /* 17 */PFM_CMD_S(pfm_context_unload, PFM_CMD_PCLRWS),
5014 /* 18 */PFM_CMD_NONE,
5015 /* 19 */PFM_CMD_NONE,
5016 /* 20 */PFM_CMD_NONE,
5017 /* 21 */PFM_CMD_NONE,
5018 /* 22 */PFM_CMD_NONE,
5019 /* 23 */PFM_CMD_NONE,
5020 /* 24 */PFM_CMD_NONE,
5021 /* 25 */PFM_CMD_NONE,
5022 /* 26 */PFM_CMD_NONE,
5023 /* 27 */PFM_CMD_NONE,
5024 /* 28 */PFM_CMD_NONE,
5025 /* 29 */PFM_CMD_NONE,
5026 /* 30 */PFM_CMD_NONE,
5027 /* 31 */PFM_CMD_NONE,
5028 /* 32 */PFM_CMD(pfm_write_ibrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL),
5029 /* 33 */PFM_CMD(pfm_write_dbrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL)
5030 };
5031 #define PFM_CMD_COUNT (sizeof(pfm_cmd_tab)/sizeof(pfm_cmd_desc_t))
5033 static int
5034 pfm_check_task_state(pfm_context_t *ctx, int cmd, unsigned long flags)
5036 struct task_struct *task;
5037 int state, old_state;
5039 recheck:
5040 state = ctx->ctx_state;
5041 task = ctx->ctx_task;
5043 if (task == NULL) {
5044 DPRINT(("context %d no task, state=%d\n", ctx->ctx_fd, state));
5045 return 0;
5048 DPRINT(("context %d state=%d [%d] task_state=%ld must_stop=%d\n",
5049 ctx->ctx_fd,
5050 state,
5051 task->pid,
5052 task->state, PFM_CMD_STOPPED(cmd)));
5054 /*
5055 * self-monitoring always ok.
5057 * for system-wide the caller can either be the creator of the
5058 * context (to one to which the context is attached to) OR
5059 * a task running on the same CPU as the session.
5060 */
5061 if (task == current || ctx->ctx_fl_system) return 0;
5063 /*
5064 * we are monitoring another thread
5065 */
5066 switch(state) {
5067 case PFM_CTX_UNLOADED:
5068 /*
5069 * if context is UNLOADED we are safe to go
5070 */
5071 return 0;
5072 case PFM_CTX_ZOMBIE:
5073 /*
5074 * no command can operate on a zombie context
5075 */
5076 DPRINT(("cmd %d state zombie cannot operate on context\n", cmd));
5077 return -EINVAL;
5078 case PFM_CTX_MASKED:
5079 /*
5080 * PMU state has been saved to software even though
5081 * the thread may still be running.
5082 */
5083 if (cmd != PFM_UNLOAD_CONTEXT) return 0;
5086 /*
5087 * context is LOADED or MASKED. Some commands may need to have
5088 * the task stopped.
5090 * We could lift this restriction for UP but it would mean that
5091 * the user has no guarantee the task would not run between
5092 * two successive calls to perfmonctl(). That's probably OK.
5093 * If this user wants to ensure the task does not run, then
5094 * the task must be stopped.
5095 */
5096 if (PFM_CMD_STOPPED(cmd)) {
5097 if ((task->state != TASK_STOPPED) && (task->state != TASK_TRACED)) {
5098 DPRINT(("[%d] task not in stopped state\n", task->pid));
5099 return -EBUSY;
5101 /*
5102 * task is now stopped, wait for ctxsw out
5104 * This is an interesting point in the code.
5105 * We need to unprotect the context because
5106 * the pfm_save_regs() routines needs to grab
5107 * the same lock. There are danger in doing
5108 * this because it leaves a window open for
5109 * another task to get access to the context
5110 * and possibly change its state. The one thing
5111 * that is not possible is for the context to disappear
5112 * because we are protected by the VFS layer, i.e.,
5113 * get_fd()/put_fd().
5114 */
5115 old_state = state;
5117 UNPROTECT_CTX(ctx, flags);
5119 wait_task_inactive(task);
5121 PROTECT_CTX(ctx, flags);
5123 /*
5124 * we must recheck to verify if state has changed
5125 */
5126 if (ctx->ctx_state != old_state) {
5127 DPRINT(("old_state=%d new_state=%d\n", old_state, ctx->ctx_state));
5128 goto recheck;
5131 return 0;
5134 /*
5135 * system-call entry point (must return long)
5136 */
5137 asmlinkage long
5138 sys_perfmonctl (int fd, int cmd, void __user *arg, int count)
5140 struct file *file = NULL;
5141 pfm_context_t *ctx = NULL;
5142 unsigned long flags = 0UL;
5143 void *args_k = NULL;
5144 long ret; /* will expand int return types */
5145 size_t base_sz, sz, xtra_sz = 0;
5146 int narg, completed_args = 0, call_made = 0, cmd_flags;
5147 int (*func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
5148 int (*getsize)(void *arg, size_t *sz);
5149 #define PFM_MAX_ARGSIZE 4096
5151 /*
5152 * reject any call if perfmon was disabled at initialization
5153 */
5154 if (unlikely(pmu_conf == NULL)) return -ENOSYS;
5156 if (unlikely(cmd < 0 || cmd >= PFM_CMD_COUNT)) {
5157 DPRINT(("invalid cmd=%d\n", cmd));
5158 return -EINVAL;
5161 func = pfm_cmd_tab[cmd].cmd_func;
5162 narg = pfm_cmd_tab[cmd].cmd_narg;
5163 base_sz = pfm_cmd_tab[cmd].cmd_argsize;
5164 getsize = pfm_cmd_tab[cmd].cmd_getsize;
5165 cmd_flags = pfm_cmd_tab[cmd].cmd_flags;
5167 if (unlikely(func == NULL)) {
5168 DPRINT(("invalid cmd=%d\n", cmd));
5169 return -EINVAL;
5172 DPRINT(("cmd=%s idx=%d narg=0x%x argsz=%lu count=%d\n",
5173 PFM_CMD_NAME(cmd),
5174 cmd,
5175 narg,
5176 base_sz,
5177 count));
5179 /*
5180 * check if number of arguments matches what the command expects
5181 */
5182 if (unlikely((narg == PFM_CMD_ARG_MANY && count <= 0) || (narg > 0 && narg != count)))
5183 return -EINVAL;
5185 restart_args:
5186 sz = xtra_sz + base_sz*count;
5187 /*
5188 * limit abuse to min page size
5189 */
5190 if (unlikely(sz > PFM_MAX_ARGSIZE)) {
5191 printk(KERN_ERR "perfmon: [%d] argument too big %lu\n", current->pid, sz);
5192 return -E2BIG;
5195 /*
5196 * allocate default-sized argument buffer
5197 */
5198 if (likely(count && args_k == NULL)) {
5199 args_k = kmalloc(PFM_MAX_ARGSIZE, GFP_KERNEL);
5200 if (args_k == NULL) return -ENOMEM;
5203 ret = -EFAULT;
5205 /*
5206 * copy arguments
5208 * assume sz = 0 for command without parameters
5209 */
5210 if (sz && copy_from_user(args_k, arg, sz)) {
5211 DPRINT(("cannot copy_from_user %lu bytes @%p\n", sz, arg));
5212 goto error_args;
5215 /*
5216 * check if command supports extra parameters
5217 */
5218 if (completed_args == 0 && getsize) {
5219 /*
5220 * get extra parameters size (based on main argument)
5221 */
5222 ret = (*getsize)(args_k, &xtra_sz);
5223 if (ret) goto error_args;
5225 completed_args = 1;
5227 DPRINT(("restart_args sz=%lu xtra_sz=%lu\n", sz, xtra_sz));
5229 /* retry if necessary */
5230 if (likely(xtra_sz)) goto restart_args;
5233 if (unlikely((cmd_flags & PFM_CMD_FD) == 0)) goto skip_fd;
5235 ret = -EBADF;
5237 file = fget(fd);
5238 if (unlikely(file == NULL)) {
5239 DPRINT(("invalid fd %d\n", fd));
5240 goto error_args;
5242 if (unlikely(PFM_IS_FILE(file) == 0)) {
5243 DPRINT(("fd %d not related to perfmon\n", fd));
5244 goto error_args;
5247 ctx = (pfm_context_t *)file->private_data;
5248 if (unlikely(ctx == NULL)) {
5249 DPRINT(("no context for fd %d\n", fd));
5250 goto error_args;
5252 prefetch(&ctx->ctx_state);
5254 PROTECT_CTX(ctx, flags);
5256 /*
5257 * check task is stopped
5258 */
5259 ret = pfm_check_task_state(ctx, cmd, flags);
5260 if (unlikely(ret)) goto abort_locked;
5262 skip_fd:
5263 ret = (*func)(ctx, args_k, count, task_pt_regs(current));
5265 call_made = 1;
5267 abort_locked:
5268 if (likely(ctx)) {
5269 DPRINT(("context unlocked\n"));
5270 UNPROTECT_CTX(ctx, flags);
5273 /* copy argument back to user, if needed */
5274 if (call_made && PFM_CMD_RW_ARG(cmd) && copy_to_user(arg, args_k, base_sz*count)) ret = -EFAULT;
5276 error_args:
5277 if (file)
5278 fput(file);
5280 kfree(args_k);
5282 DPRINT(("cmd=%s ret=%ld\n", PFM_CMD_NAME(cmd), ret));
5284 return ret;
5287 static void
5288 pfm_resume_after_ovfl(pfm_context_t *ctx, unsigned long ovfl_regs, struct pt_regs *regs)
5290 pfm_buffer_fmt_t *fmt = ctx->ctx_buf_fmt;
5291 pfm_ovfl_ctrl_t rst_ctrl;
5292 int state;
5293 int ret = 0;
5295 state = ctx->ctx_state;
5296 /*
5297 * Unlock sampling buffer and reset index atomically
5298 * XXX: not really needed when blocking
5299 */
5300 if (CTX_HAS_SMPL(ctx)) {
5302 rst_ctrl.bits.mask_monitoring = 0;
5303 rst_ctrl.bits.reset_ovfl_pmds = 0;
5305 if (state == PFM_CTX_LOADED)
5306 ret = pfm_buf_fmt_restart_active(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
5307 else
5308 ret = pfm_buf_fmt_restart(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
5309 } else {
5310 rst_ctrl.bits.mask_monitoring = 0;
5311 rst_ctrl.bits.reset_ovfl_pmds = 1;
5314 if (ret == 0) {
5315 if (rst_ctrl.bits.reset_ovfl_pmds) {
5316 pfm_reset_regs(ctx, &ovfl_regs, PFM_PMD_LONG_RESET);
5318 if (rst_ctrl.bits.mask_monitoring == 0) {
5319 DPRINT(("resuming monitoring\n"));
5320 if (ctx->ctx_state == PFM_CTX_MASKED) pfm_restore_monitoring(current);
5321 } else {
5322 DPRINT(("stopping monitoring\n"));
5323 //pfm_stop_monitoring(current, regs);
5325 ctx->ctx_state = PFM_CTX_LOADED;
5329 /*
5330 * context MUST BE LOCKED when calling
5331 * can only be called for current
5332 */
5333 static void
5334 pfm_context_force_terminate(pfm_context_t *ctx, struct pt_regs *regs)
5336 int ret;
5338 DPRINT(("entering for [%d]\n", current->pid));
5340 ret = pfm_context_unload(ctx, NULL, 0, regs);
5341 if (ret) {
5342 printk(KERN_ERR "pfm_context_force_terminate: [%d] unloaded failed with %d\n", current->pid, ret);
5345 /*
5346 * and wakeup controlling task, indicating we are now disconnected
5347 */
5348 wake_up_interruptible(&ctx->ctx_zombieq);
5350 /*
5351 * given that context is still locked, the controlling
5352 * task will only get access when we return from
5353 * pfm_handle_work().
5354 */
5357 static int pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds);
5358 /*
5359 * pfm_handle_work() can be called with interrupts enabled
5360 * (TIF_NEED_RESCHED) or disabled. The down_interruptible
5361 * call may sleep, therefore we must re-enable interrupts
5362 * to avoid deadlocks. It is safe to do so because this function
5363 * is called ONLY when returning to user level (PUStk=1), in which case
5364 * there is no risk of kernel stack overflow due to deep
5365 * interrupt nesting.
5366 */
5367 void
5368 pfm_handle_work(void)
5370 pfm_context_t *ctx;
5371 struct pt_regs *regs;
5372 unsigned long flags, dummy_flags;
5373 unsigned long ovfl_regs;
5374 unsigned int reason;
5375 int ret;
5377 ctx = PFM_GET_CTX(current);
5378 if (ctx == NULL) {
5379 printk(KERN_ERR "perfmon: [%d] has no PFM context\n", current->pid);
5380 return;
5383 PROTECT_CTX(ctx, flags);
5385 PFM_SET_WORK_PENDING(current, 0);
5387 pfm_clear_task_notify();
5389 regs = task_pt_regs(current);
5391 /*
5392 * extract reason for being here and clear
5393 */
5394 reason = ctx->ctx_fl_trap_reason;
5395 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
5396 ovfl_regs = ctx->ctx_ovfl_regs[0];
5398 DPRINT(("reason=%d state=%d\n", reason, ctx->ctx_state));
5400 /*
5401 * must be done before we check for simple-reset mode
5402 */
5403 if (ctx->ctx_fl_going_zombie || ctx->ctx_state == PFM_CTX_ZOMBIE) goto do_zombie;
5406 //if (CTX_OVFL_NOBLOCK(ctx)) goto skip_blocking;
5407 if (reason == PFM_TRAP_REASON_RESET) goto skip_blocking;
5409 /*
5410 * restore interrupt mask to what it was on entry.
5411 * Could be enabled/diasbled.
5412 */
5413 UNPROTECT_CTX(ctx, flags);
5415 /*
5416 * force interrupt enable because of down_interruptible()
5417 */
5418 local_irq_enable();
5420 DPRINT(("before block sleeping\n"));
5422 /*
5423 * may go through without blocking on SMP systems
5424 * if restart has been received already by the time we call down()
5425 */
5426 ret = wait_for_completion_interruptible(&ctx->ctx_restart_done);
5428 DPRINT(("after block sleeping ret=%d\n", ret));
5430 /*
5431 * lock context and mask interrupts again
5432 * We save flags into a dummy because we may have
5433 * altered interrupts mask compared to entry in this
5434 * function.
5435 */
5436 PROTECT_CTX(ctx, dummy_flags);
5438 /*
5439 * we need to read the ovfl_regs only after wake-up
5440 * because we may have had pfm_write_pmds() in between
5441 * and that can changed PMD values and therefore
5442 * ovfl_regs is reset for these new PMD values.
5443 */
5444 ovfl_regs = ctx->ctx_ovfl_regs[0];
5446 if (ctx->ctx_fl_going_zombie) {
5447 do_zombie:
5448 DPRINT(("context is zombie, bailing out\n"));
5449 pfm_context_force_terminate(ctx, regs);
5450 goto nothing_to_do;
5452 /*
5453 * in case of interruption of down() we don't restart anything
5454 */
5455 if (ret < 0) goto nothing_to_do;
5457 skip_blocking:
5458 pfm_resume_after_ovfl(ctx, ovfl_regs, regs);
5459 ctx->ctx_ovfl_regs[0] = 0UL;
5461 nothing_to_do:
5462 /*
5463 * restore flags as they were upon entry
5464 */
5465 UNPROTECT_CTX(ctx, flags);
5468 static int
5469 pfm_notify_user(pfm_context_t *ctx, pfm_msg_t *msg)
5471 if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
5472 DPRINT(("ignoring overflow notification, owner is zombie\n"));
5473 return 0;
5476 DPRINT(("waking up somebody\n"));
5478 if (msg) wake_up_interruptible(&ctx->ctx_msgq_wait);
5480 /*
5481 * safe, we are not in intr handler, nor in ctxsw when
5482 * we come here
5483 */
5484 kill_fasync (&ctx->ctx_async_queue, SIGIO, POLL_IN);
5486 return 0;
5489 static int
5490 pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds)
5492 pfm_msg_t *msg = NULL;
5494 if (ctx->ctx_fl_no_msg == 0) {
5495 msg = pfm_get_new_msg(ctx);
5496 if (msg == NULL) {
5497 printk(KERN_ERR "perfmon: pfm_ovfl_notify_user no more notification msgs\n");
5498 return -1;
5501 msg->pfm_ovfl_msg.msg_type = PFM_MSG_OVFL;
5502 msg->pfm_ovfl_msg.msg_ctx_fd = ctx->ctx_fd;
5503 msg->pfm_ovfl_msg.msg_active_set = 0;
5504 msg->pfm_ovfl_msg.msg_ovfl_pmds[0] = ovfl_pmds;
5505 msg->pfm_ovfl_msg.msg_ovfl_pmds[1] = 0UL;
5506 msg->pfm_ovfl_msg.msg_ovfl_pmds[2] = 0UL;
5507 msg->pfm_ovfl_msg.msg_ovfl_pmds[3] = 0UL;
5508 msg->pfm_ovfl_msg.msg_tstamp = 0UL;
5511 DPRINT(("ovfl msg: msg=%p no_msg=%d fd=%d ovfl_pmds=0x%lx\n",
5512 msg,
5513 ctx->ctx_fl_no_msg,
5514 ctx->ctx_fd,
5515 ovfl_pmds));
5517 return pfm_notify_user(ctx, msg);
5520 static int
5521 pfm_end_notify_user(pfm_context_t *ctx)
5523 pfm_msg_t *msg;
5525 msg = pfm_get_new_msg(ctx);
5526 if (msg == NULL) {
5527 printk(KERN_ERR "perfmon: pfm_end_notify_user no more notification msgs\n");
5528 return -1;
5530 /* no leak */
5531 memset(msg, 0, sizeof(*msg));
5533 msg->pfm_end_msg.msg_type = PFM_MSG_END;
5534 msg->pfm_end_msg.msg_ctx_fd = ctx->ctx_fd;
5535 msg->pfm_ovfl_msg.msg_tstamp = 0UL;
5537 DPRINT(("end msg: msg=%p no_msg=%d ctx_fd=%d\n",
5538 msg,
5539 ctx->ctx_fl_no_msg,
5540 ctx->ctx_fd));
5542 return pfm_notify_user(ctx, msg);
5544 #else
5545 #define pfm_ovfl_notify_user(ctx, ovfl_pmds) do {} while(0)
5546 #endif
5548 /*
5549 * main overflow processing routine.
5550 * it can be called from the interrupt path or explicitely during the context switch code
5551 */
5552 static void
5553 pfm_overflow_handler(struct task_struct *task, pfm_context_t *ctx, u64 pmc0, struct pt_regs *regs)
5555 pfm_ovfl_arg_t *ovfl_arg;
5556 unsigned long mask;
5557 unsigned long old_val, ovfl_val, new_val;
5558 unsigned long ovfl_notify = 0UL, ovfl_pmds = 0UL, smpl_pmds = 0UL, reset_pmds;
5559 unsigned long tstamp;
5560 pfm_ovfl_ctrl_t ovfl_ctrl;
5561 unsigned int i, has_smpl;
5562 int must_notify = 0;
5563 #ifdef XEN
5564 BUG_ON(task != NULL);
5565 #endif
5567 if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) goto stop_monitoring;
5569 /*
5570 * sanity test. Should never happen
5571 */
5572 if (unlikely((pmc0 & 0x1) == 0)) goto sanity_check;
5574 tstamp = ia64_get_itc();
5575 mask = pmc0 >> PMU_FIRST_COUNTER;
5576 ovfl_val = pmu_conf->ovfl_val;
5577 has_smpl = CTX_HAS_SMPL(ctx);
5579 DPRINT_ovfl(("pmc0=0x%lx pid=%d iip=0x%lx, %s "
5580 "used_pmds=0x%lx\n",
5581 pmc0,
5582 task ? task->pid: -1,
5583 (regs ? regs->cr_iip : 0),
5584 CTX_OVFL_NOBLOCK(ctx) ? "nonblocking" : "blocking",
5585 ctx->ctx_used_pmds[0]));
5588 /*
5589 * first we update the virtual counters
5590 * assume there was a prior ia64_srlz_d() issued
5591 */
5592 for (i = PMU_FIRST_COUNTER; mask ; i++, mask >>= 1) {
5594 /* skip pmd which did not overflow */
5595 if ((mask & 0x1) == 0) continue;
5597 /*
5598 * Note that the pmd is not necessarily 0 at this point as qualified events
5599 * may have happened before the PMU was frozen. The residual count is not
5600 * taken into consideration here but will be with any read of the pmd via
5601 * pfm_read_pmds().
5602 */
5603 old_val = new_val = ctx->ctx_pmds[i].val;
5604 new_val += 1 + ovfl_val;
5605 ctx->ctx_pmds[i].val = new_val;
5607 /*
5608 * check for overflow condition
5609 */
5610 if (likely(old_val > new_val)) {
5611 ovfl_pmds |= 1UL << i;
5612 if (PMC_OVFL_NOTIFY(ctx, i)) ovfl_notify |= 1UL << i;
5615 DPRINT_ovfl(("ctx_pmd[%d].val=0x%lx old_val=0x%lx pmd=0x%lx ovfl_pmds=0x%lx ovfl_notify=0x%lx\n",
5616 i,
5617 new_val,
5618 old_val,
5619 ia64_get_pmd(i) & ovfl_val,
5620 ovfl_pmds,
5621 ovfl_notify));
5624 /*
5625 * there was no 64-bit overflow, nothing else to do
5626 */
5627 if (ovfl_pmds == 0UL) return;
5629 /*
5630 * reset all control bits
5631 */
5632 ovfl_ctrl.val = 0;
5633 reset_pmds = 0UL;
5635 /*
5636 * if a sampling format module exists, then we "cache" the overflow by
5637 * calling the module's handler() routine.
5638 */
5639 if (has_smpl) {
5640 unsigned long start_cycles, end_cycles;
5641 unsigned long pmd_mask;
5642 int j, k, ret = 0;
5643 int this_cpu = smp_processor_id();
5645 pmd_mask = ovfl_pmds >> PMU_FIRST_COUNTER;
5646 ovfl_arg = &ctx->ctx_ovfl_arg;
5648 prefetch(ctx->ctx_smpl_hdr);
5650 for(i=PMU_FIRST_COUNTER; pmd_mask && ret == 0; i++, pmd_mask >>=1) {
5652 mask = 1UL << i;
5654 if ((pmd_mask & 0x1) == 0) continue;
5656 ovfl_arg->ovfl_pmd = (unsigned char )i;
5657 ovfl_arg->ovfl_notify = ovfl_notify & mask ? 1 : 0;
5658 ovfl_arg->active_set = 0;
5659 ovfl_arg->ovfl_ctrl.val = 0; /* module must fill in all fields */
5660 ovfl_arg->smpl_pmds[0] = smpl_pmds = ctx->ctx_pmds[i].smpl_pmds[0];
5662 ovfl_arg->pmd_value = ctx->ctx_pmds[i].val;
5663 ovfl_arg->pmd_last_reset = ctx->ctx_pmds[i].lval;
5664 ovfl_arg->pmd_eventid = ctx->ctx_pmds[i].eventid;
5666 /*
5667 * copy values of pmds of interest. Sampling format may copy them
5668 * into sampling buffer.
5669 */
5670 if (smpl_pmds) {
5671 for(j=0, k=0; smpl_pmds; j++, smpl_pmds >>=1) {
5672 if ((smpl_pmds & 0x1) == 0) continue;
5673 ovfl_arg->smpl_pmds_values[k++] = PMD_IS_COUNTING(j) ? pfm_read_soft_counter(ctx, j) : ia64_get_pmd(j);
5674 DPRINT_ovfl(("smpl_pmd[%d]=pmd%u=0x%lx\n", k-1, j, ovfl_arg->smpl_pmds_values[k-1]));
5678 pfm_stats[this_cpu].pfm_smpl_handler_calls++;
5680 start_cycles = ia64_get_itc();
5682 /*
5683 * call custom buffer format record (handler) routine
5684 */
5685 ret = (*ctx->ctx_buf_fmt->fmt_handler)(task, ctx->ctx_smpl_hdr, ovfl_arg, regs, tstamp);
5687 end_cycles = ia64_get_itc();
5689 /*
5690 * For those controls, we take the union because they have
5691 * an all or nothing behavior.
5692 */
5693 ovfl_ctrl.bits.notify_user |= ovfl_arg->ovfl_ctrl.bits.notify_user;
5694 ovfl_ctrl.bits.block_task |= ovfl_arg->ovfl_ctrl.bits.block_task;
5695 ovfl_ctrl.bits.mask_monitoring |= ovfl_arg->ovfl_ctrl.bits.mask_monitoring;
5696 /*
5697 * build the bitmask of pmds to reset now
5698 */
5699 if (ovfl_arg->ovfl_ctrl.bits.reset_ovfl_pmds) reset_pmds |= mask;
5701 pfm_stats[this_cpu].pfm_smpl_handler_cycles += end_cycles - start_cycles;
5703 /*
5704 * when the module cannot handle the rest of the overflows, we abort right here
5705 */
5706 if (ret && pmd_mask) {
5707 DPRINT(("handler aborts leftover ovfl_pmds=0x%lx\n",
5708 pmd_mask<<PMU_FIRST_COUNTER));
5710 /*
5711 * remove the pmds we reset now from the set of pmds to reset in pfm_restart()
5712 */
5713 ovfl_pmds &= ~reset_pmds;
5714 } else {
5715 /*
5716 * when no sampling module is used, then the default
5717 * is to notify on overflow if requested by user
5718 */
5719 ovfl_ctrl.bits.notify_user = ovfl_notify ? 1 : 0;
5720 ovfl_ctrl.bits.block_task = ovfl_notify ? 1 : 0;
5721 ovfl_ctrl.bits.mask_monitoring = ovfl_notify ? 1 : 0; /* XXX: change for saturation */
5722 ovfl_ctrl.bits.reset_ovfl_pmds = ovfl_notify ? 0 : 1;
5723 /*
5724 * if needed, we reset all overflowed pmds
5725 */
5726 if (ovfl_notify == 0) reset_pmds = ovfl_pmds;
5729 DPRINT_ovfl(("ovfl_pmds=0x%lx reset_pmds=0x%lx\n", ovfl_pmds, reset_pmds));
5731 /*
5732 * reset the requested PMD registers using the short reset values
5733 */
5734 if (reset_pmds) {
5735 unsigned long bm = reset_pmds;
5736 pfm_reset_regs(ctx, &bm, PFM_PMD_SHORT_RESET);
5739 if (ovfl_notify && ovfl_ctrl.bits.notify_user) {
5740 #ifndef XEN
5741 /*
5742 * keep track of what to reset when unblocking
5743 */
5744 ctx->ctx_ovfl_regs[0] = ovfl_pmds;
5746 /*
5747 * check for blocking context
5748 */
5749 if (CTX_OVFL_NOBLOCK(ctx) == 0 && ovfl_ctrl.bits.block_task) {
5751 ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_BLOCK;
5753 /*
5754 * set the perfmon specific checking pending work for the task
5755 */
5756 PFM_SET_WORK_PENDING(task, 1);
5758 /*
5759 * when coming from ctxsw, current still points to the
5760 * previous task, therefore we must work with task and not current.
5761 */
5762 pfm_set_task_notify(task);
5764 /*
5765 * defer until state is changed (shorten spin window). the context is locked
5766 * anyway, so the signal receiver would come spin for nothing.
5767 */
5768 must_notify = 1;
5769 #else
5770 gdprintk(XENLOG_INFO, "%s check!\n", __func__);
5771 #endif
5774 DPRINT_ovfl(("owner [%d] pending=%ld reason=%u ovfl_pmds=0x%lx ovfl_notify=0x%lx masked=%d\n",
5775 #ifndef XEN
5776 GET_PMU_OWNER() ? GET_PMU_OWNER()->pid : -1,
5777 PFM_GET_WORK_PENDING(task),
5778 #else
5779 -1, 0UL,
5780 #endif
5781 ctx->ctx_fl_trap_reason,
5782 ovfl_pmds,
5783 ovfl_notify,
5784 ovfl_ctrl.bits.mask_monitoring ? 1 : 0));
5785 /*
5786 * in case monitoring must be stopped, we toggle the psr bits
5787 */
5788 if (ovfl_ctrl.bits.mask_monitoring) {
5789 #ifndef XEN
5790 pfm_mask_monitoring(task);
5791 ctx->ctx_state = PFM_CTX_MASKED;
5792 ctx->ctx_fl_can_restart = 1;
5793 #else
5794 gdprintk(XENLOG_INFO, "%s check!\n", __func__);
5795 #endif
5798 /*
5799 * send notification now
5800 */
5801 if (must_notify) pfm_ovfl_notify_user(ctx, ovfl_notify);
5803 return;
5805 sanity_check:
5806 printk(KERN_ERR "perfmon: CPU%d overflow handler [%d] pmc0=0x%lx\n",
5807 smp_processor_id(),
5808 task ? task->pid : -1,
5809 pmc0);
5810 return;
5812 stop_monitoring:
5813 /*
5814 * in SMP, zombie context is never restored but reclaimed in pfm_load_regs().
5815 * Moreover, zombies are also reclaimed in pfm_save_regs(). Therefore we can
5816 * come here as zombie only if the task is the current task. In which case, we
5817 * can access the PMU hardware directly.
5819 * Note that zombies do have PM_VALID set. So here we do the minimal.
5821 * In case the context was zombified it could not be reclaimed at the time
5822 * the monitoring program exited. At this point, the PMU reservation has been
5823 * returned, the sampiing buffer has been freed. We must convert this call
5824 * into a spurious interrupt. However, we must also avoid infinite overflows
5825 * by stopping monitoring for this task. We can only come here for a per-task
5826 * context. All we need to do is to stop monitoring using the psr bits which
5827 * are always task private. By re-enabling secure montioring, we ensure that
5828 * the monitored task will not be able to re-activate monitoring.
5829 * The task will eventually be context switched out, at which point the context
5830 * will be reclaimed (that includes releasing ownership of the PMU).
5832 * So there might be a window of time where the number of per-task session is zero
5833 * yet one PMU might have a owner and get at most one overflow interrupt for a zombie
5834 * context. This is safe because if a per-task session comes in, it will push this one
5835 * out and by the virtue on pfm_save_regs(), this one will disappear. If a system wide
5836 * session is force on that CPU, given that we use task pinning, pfm_save_regs() will
5837 * also push our zombie context out.
5839 * Overall pretty hairy stuff....
5840 */
5841 DPRINT(("ctx is zombie for [%d], converted to spurious\n", task ? task->pid: -1));
5842 pfm_clear_psr_up();
5843 ia64_psr(regs)->up = 0;
5844 ia64_psr(regs)->sp = 1;
5845 return;
5848 static int
5849 pfm_do_interrupt_handler(int irq, void *arg, struct pt_regs *regs)
5851 struct task_struct *task;
5852 pfm_context_t *ctx;
5853 unsigned long flags;
5854 u64 pmc0;
5855 int this_cpu = smp_processor_id();
5856 int retval = 0;
5858 pfm_stats[this_cpu].pfm_ovfl_intr_count++;
5860 /*
5861 * srlz.d done before arriving here
5862 */
5863 pmc0 = ia64_get_pmc(0);
5865 #ifndef XEN
5866 task = GET_PMU_OWNER();
5867 #else
5868 task = NULL;
5869 #endif
5870 ctx = GET_PMU_CTX();
5872 /*
5873 * if we have some pending bits set
5874 * assumes : if any PMC0.bit[63-1] is set, then PMC0.fr = 1
5875 */
5876 #ifndef XEN
5877 if (PMC0_HAS_OVFL(pmc0) && task) {
5878 #else
5879 if (PMC0_HAS_OVFL(pmc0)) {
5880 #endif
5881 /*
5882 * we assume that pmc0.fr is always set here
5883 */
5885 /* sanity check */
5886 if (!ctx) goto report_spurious1;
5888 #ifndef XEN
5889 if (ctx->ctx_fl_system == 0 && (task->thread.flags & IA64_THREAD_PM_VALID) == 0)
5890 goto report_spurious2;
5891 #endif
5893 PROTECT_CTX_NOPRINT(ctx, flags);
5895 pfm_overflow_handler(task, ctx, pmc0, regs);
5897 UNPROTECT_CTX_NOPRINT(ctx, flags);
5899 } else {
5900 pfm_stats[this_cpu].pfm_spurious_ovfl_intr_count++;
5901 retval = -1;
5903 /*
5904 * keep it unfrozen at all times
5905 */
5906 pfm_unfreeze_pmu();
5908 return retval;
5910 report_spurious1:
5911 #ifndef XEN
5912 printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d has no PFM context\n",
5913 this_cpu, task->pid);
5914 #endif
5915 pfm_unfreeze_pmu();
5916 return -1;
5917 #ifndef XEN /* XEN path doesn't take this goto */
5918 report_spurious2:
5919 printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d, invalid flag\n",
5920 this_cpu,
5921 task->pid);
5922 pfm_unfreeze_pmu();
5923 return -1;
5924 #endif
5927 static irqreturn_t
5928 pfm_interrupt_handler(int irq, void *arg, struct pt_regs *regs)
5930 unsigned long start_cycles, total_cycles;
5931 unsigned long min, max;
5932 int this_cpu;
5933 int ret;
5935 this_cpu = get_cpu();
5936 if (likely(!pfm_alt_intr_handler)) {
5937 min = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min;
5938 max = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max;
5940 start_cycles = ia64_get_itc();
5942 ret = pfm_do_interrupt_handler(irq, arg, regs);
5944 total_cycles = ia64_get_itc();
5946 /*
5947 * don't measure spurious interrupts
5948 */
5949 if (likely(ret == 0)) {
5950 total_cycles -= start_cycles;
5952 if (total_cycles < min) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min = total_cycles;
5953 if (total_cycles > max) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max = total_cycles;
5955 pfm_stats[this_cpu].pfm_ovfl_intr_cycles += total_cycles;
5958 else {
5959 (*pfm_alt_intr_handler->handler)(irq, arg, regs);
5962 put_cpu_no_resched();
5963 return IRQ_HANDLED;
5966 #ifndef XEN
5967 /*
5968 * /proc/perfmon interface, for debug only
5969 */
5971 #define PFM_PROC_SHOW_HEADER ((void *)NR_CPUS+1)
5973 static void *
5974 pfm_proc_start(struct seq_file *m, loff_t *pos)
5976 if (*pos == 0) {
5977 return PFM_PROC_SHOW_HEADER;
5980 while (*pos <= NR_CPUS) {
5981 if (cpu_online(*pos - 1)) {
5982 return (void *)*pos;
5984 ++*pos;
5986 return NULL;
5989 static void *
5990 pfm_proc_next(struct seq_file *m, void *v, loff_t *pos)
5992 ++*pos;
5993 return pfm_proc_start(m, pos);
5996 static void
5997 pfm_proc_stop(struct seq_file *m, void *v)
6001 static void
6002 pfm_proc_show_header(struct seq_file *m)
6004 struct list_head * pos;
6005 pfm_buffer_fmt_t * entry;
6006 unsigned long flags;
6008 seq_printf(m,
6009 "perfmon version : %u.%u\n"
6010 "model : %s\n"
6011 "fastctxsw : %s\n"
6012 "expert mode : %s\n"
6013 "ovfl_mask : 0x%lx\n"
6014 "PMU flags : 0x%x\n",
6015 PFM_VERSION_MAJ, PFM_VERSION_MIN,
6016 pmu_conf->pmu_name,
6017 pfm_sysctl.fastctxsw > 0 ? "Yes": "No",
6018 pfm_sysctl.expert_mode > 0 ? "Yes": "No",
6019 pmu_conf->ovfl_val,
6020 pmu_conf->flags);
6022 LOCK_PFS(flags);
6024 seq_printf(m,
6025 "proc_sessions : %u\n"
6026 "sys_sessions : %u\n"
6027 "sys_use_dbregs : %u\n"
6028 "ptrace_use_dbregs : %u\n",
6029 pfm_sessions.pfs_task_sessions,
6030 pfm_sessions.pfs_sys_sessions,
6031 pfm_sessions.pfs_sys_use_dbregs,
6032 pfm_sessions.pfs_ptrace_use_dbregs);
6034 UNLOCK_PFS(flags);
6036 spin_lock(&pfm_buffer_fmt_lock);
6038 list_for_each(pos, &pfm_buffer_fmt_list) {
6039 entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
6040 seq_printf(m, "format : %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x %s\n",
6041 entry->fmt_uuid[0],
6042 entry->fmt_uuid[1],
6043 entry->fmt_uuid[2],
6044 entry->fmt_uuid[3],
6045 entry->fmt_uuid[4],
6046 entry->fmt_uuid[5],
6047 entry->fmt_uuid[6],
6048 entry->fmt_uuid[7],
6049 entry->fmt_uuid[8],
6050 entry->fmt_uuid[9],
6051 entry->fmt_uuid[10],
6052 entry->fmt_uuid[11],
6053 entry->fmt_uuid[12],
6054 entry->fmt_uuid[13],
6055 entry->fmt_uuid[14],
6056 entry->fmt_uuid[15],
6057 entry->fmt_name);
6059 spin_unlock(&pfm_buffer_fmt_lock);
6063 static int
6064 pfm_proc_show(struct seq_file *m, void *v)
6066 unsigned long psr;
6067 unsigned int i;
6068 int cpu;
6070 if (v == PFM_PROC_SHOW_HEADER) {
6071 pfm_proc_show_header(m);
6072 return 0;
6075 /* show info for CPU (v - 1) */
6077 cpu = (long)v - 1;
6078 seq_printf(m,
6079 "CPU%-2d overflow intrs : %lu\n"
6080 "CPU%-2d overflow cycles : %lu\n"
6081 "CPU%-2d overflow min : %lu\n"
6082 "CPU%-2d overflow max : %lu\n"
6083 "CPU%-2d smpl handler calls : %lu\n"
6084 "CPU%-2d smpl handler cycles : %lu\n"
6085 "CPU%-2d spurious intrs : %lu\n"
6086 "CPU%-2d replay intrs : %lu\n"
6087 "CPU%-2d syst_wide : %d\n"
6088 "CPU%-2d dcr_pp : %d\n"
6089 "CPU%-2d exclude idle : %d\n"
6090 "CPU%-2d owner : %d\n"
6091 "CPU%-2d context : %p\n"
6092 "CPU%-2d activations : %lu\n",
6093 cpu, pfm_stats[cpu].pfm_ovfl_intr_count,
6094 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles,
6095 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_min,
6096 cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_max,
6097 cpu, pfm_stats[cpu].pfm_smpl_handler_calls,
6098 cpu, pfm_stats[cpu].pfm_smpl_handler_cycles,
6099 cpu, pfm_stats[cpu].pfm_spurious_ovfl_intr_count,
6100 cpu, pfm_stats[cpu].pfm_replay_ovfl_intr_count,
6101 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_SYST_WIDE ? 1 : 0,
6102 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_DCR_PP ? 1 : 0,
6103 cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_EXCL_IDLE ? 1 : 0,
6104 cpu, pfm_get_cpu_data(pmu_owner, cpu) ? pfm_get_cpu_data(pmu_owner, cpu)->pid: -1,
6105 cpu, pfm_get_cpu_data(pmu_ctx, cpu),
6106 cpu, pfm_get_cpu_data(pmu_activation_number, cpu));
6108 if (num_online_cpus() == 1 && pfm_sysctl.debug > 0) {
6110 psr = pfm_get_psr();
6112 ia64_srlz_d();
6114 seq_printf(m,
6115 "CPU%-2d psr : 0x%lx\n"
6116 "CPU%-2d pmc0 : 0x%lx\n",
6117 cpu, psr,
6118 cpu, ia64_get_pmc(0));
6120 for (i=0; PMC_IS_LAST(i) == 0; i++) {
6121 if (PMC_IS_COUNTING(i) == 0) continue;
6122 seq_printf(m,
6123 "CPU%-2d pmc%u : 0x%lx\n"
6124 "CPU%-2d pmd%u : 0x%lx\n",
6125 cpu, i, ia64_get_pmc(i),
6126 cpu, i, ia64_get_pmd(i));
6129 return 0;
6132 struct seq_operations pfm_seq_ops = {
6133 .start = pfm_proc_start,
6134 .next = pfm_proc_next,
6135 .stop = pfm_proc_stop,
6136 .show = pfm_proc_show
6137 };
6139 static int
6140 pfm_proc_open(struct inode *inode, struct file *file)
6142 return seq_open(file, &pfm_seq_ops);
6144 #endif
6147 /*
6148 * we come here as soon as local_cpu_data->pfm_syst_wide is set. this happens
6149 * during pfm_enable() hence before pfm_start(). We cannot assume monitoring
6150 * is active or inactive based on mode. We must rely on the value in
6151 * local_cpu_data->pfm_syst_info
6152 */
6153 void
6154 pfm_syst_wide_update_task(struct task_struct *task, unsigned long info, int is_ctxswin)
6156 struct pt_regs *regs;
6157 unsigned long dcr;
6158 unsigned long dcr_pp;
6160 dcr_pp = info & PFM_CPUINFO_DCR_PP ? 1 : 0;
6162 /*
6163 * pid 0 is guaranteed to be the idle task. There is one such task with pid 0
6164 * on every CPU, so we can rely on the pid to identify the idle task.
6165 */
6166 if ((info & PFM_CPUINFO_EXCL_IDLE) == 0 || task->pid) {
6167 regs = task_pt_regs(task);
6168 ia64_psr(regs)->pp = is_ctxswin ? dcr_pp : 0;
6169 return;
6171 /*
6172 * if monitoring has started
6173 */
6174 if (dcr_pp) {
6175 dcr = ia64_getreg(_IA64_REG_CR_DCR);
6176 /*
6177 * context switching in?
6178 */
6179 if (is_ctxswin) {
6180 /* mask monitoring for the idle task */
6181 ia64_setreg(_IA64_REG_CR_DCR, dcr & ~IA64_DCR_PP);
6182 pfm_clear_psr_pp();
6183 ia64_srlz_i();
6184 return;
6186 /*
6187 * context switching out
6188 * restore monitoring for next task
6190 * Due to inlining this odd if-then-else construction generates
6191 * better code.
6192 */
6193 ia64_setreg(_IA64_REG_CR_DCR, dcr |IA64_DCR_PP);
6194 pfm_set_psr_pp();
6195 ia64_srlz_i();
6199 #ifndef XEN
6200 #ifdef CONFIG_SMP
6202 static void
6203 pfm_force_cleanup(pfm_context_t *ctx, struct pt_regs *regs)
6205 struct task_struct *task = ctx->ctx_task;
6207 ia64_psr(regs)->up = 0;
6208 ia64_psr(regs)->sp = 1;
6210 if (GET_PMU_OWNER() == task) {
6211 DPRINT(("cleared ownership for [%d]\n", ctx->ctx_task->pid));
6212 SET_PMU_OWNER(NULL, NULL);
6215 /*
6216 * disconnect the task from the context and vice-versa
6217 */
6218 PFM_SET_WORK_PENDING(task, 0);
6220 task->thread.pfm_context = NULL;
6221 task->thread.flags &= ~IA64_THREAD_PM_VALID;
6223 DPRINT(("force cleanup for [%d]\n", task->pid));
6227 /*
6228 * in 2.6, interrupts are masked when we come here and the runqueue lock is held
6229 */
6230 void
6231 pfm_save_regs(struct task_struct *task)
6233 pfm_context_t *ctx;
6234 struct thread_struct *t;
6235 unsigned long flags;
6236 u64 psr;
6239 ctx = PFM_GET_CTX(task);
6240 if (ctx == NULL) return;
6241 t = &task->thread;
6243 /*
6244 * we always come here with interrupts ALREADY disabled by
6245 * the scheduler. So we simply need to protect against concurrent
6246 * access, not CPU concurrency.
6247 */
6248 flags = pfm_protect_ctx_ctxsw(ctx);
6250 if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
6251 struct pt_regs *regs = task_pt_regs(task);
6253 pfm_clear_psr_up();
6255 pfm_force_cleanup(ctx, regs);
6257 BUG_ON(ctx->ctx_smpl_hdr);
6259 pfm_unprotect_ctx_ctxsw(ctx, flags);
6261 pfm_context_free(ctx);
6262 return;
6265 /*
6266 * save current PSR: needed because we modify it
6267 */
6268 ia64_srlz_d();
6269 psr = pfm_get_psr();
6271 BUG_ON(psr & (IA64_PSR_I));
6273 /*
6274 * stop monitoring:
6275 * This is the last instruction which may generate an overflow
6277 * We do not need to set psr.sp because, it is irrelevant in kernel.
6278 * It will be restored from ipsr when going back to user level
6279 */
6280 pfm_clear_psr_up();
6282 /*
6283 * keep a copy of psr.up (for reload)
6284 */
6285 ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
6287 /*
6288 * release ownership of this PMU.
6289 * PM interrupts are masked, so nothing
6290 * can happen.
6291 */
6292 SET_PMU_OWNER(NULL, NULL);
6294 /*
6295 * we systematically save the PMD as we have no
6296 * guarantee we will be schedule at that same
6297 * CPU again.
6298 */
6299 pfm_save_pmds(t->pmds, ctx->ctx_used_pmds[0]);
6301 /*
6302 * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
6303 * we will need it on the restore path to check
6304 * for pending overflow.
6305 */
6306 t->pmcs[0] = ia64_get_pmc(0);
6308 /*
6309 * unfreeze PMU if had pending overflows
6310 */
6311 if (t->pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
6313 /*
6314 * finally, allow context access.
6315 * interrupts will still be masked after this call.
6316 */
6317 pfm_unprotect_ctx_ctxsw(ctx, flags);
6320 #else /* !CONFIG_SMP */
6321 void
6322 pfm_save_regs(struct task_struct *task)
6324 pfm_context_t *ctx;
6325 u64 psr;
6327 ctx = PFM_GET_CTX(task);
6328 if (ctx == NULL) return;
6330 /*
6331 * save current PSR: needed because we modify it
6332 */
6333 psr = pfm_get_psr();
6335 BUG_ON(psr & (IA64_PSR_I));
6337 /*
6338 * stop monitoring:
6339 * This is the last instruction which may generate an overflow
6341 * We do not need to set psr.sp because, it is irrelevant in kernel.
6342 * It will be restored from ipsr when going back to user level
6343 */
6344 pfm_clear_psr_up();
6346 /*
6347 * keep a copy of psr.up (for reload)
6348 */
6349 ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
6352 static void
6353 pfm_lazy_save_regs (struct task_struct *task)
6355 pfm_context_t *ctx;
6356 struct thread_struct *t;
6357 unsigned long flags;
6359 { u64 psr = pfm_get_psr();
6360 BUG_ON(psr & IA64_PSR_UP);
6363 ctx = PFM_GET_CTX(task);
6364 t = &task->thread;
6366 /*
6367 * we need to mask PMU overflow here to
6368 * make sure that we maintain pmc0 until
6369 * we save it. overflow interrupts are
6370 * treated as spurious if there is no
6371 * owner.
6373 * XXX: I don't think this is necessary
6374 */
6375 PROTECT_CTX(ctx,flags);
6377 /*
6378 * release ownership of this PMU.
6379 * must be done before we save the registers.
6381 * after this call any PMU interrupt is treated
6382 * as spurious.
6383 */
6384 SET_PMU_OWNER(NULL, NULL);
6386 /*
6387 * save all the pmds we use
6388 */
6389 pfm_save_pmds(t->pmds, ctx->ctx_used_pmds[0]);
6391 /*
6392 * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
6393 * it is needed to check for pended overflow
6394 * on the restore path
6395 */
6396 t->pmcs[0] = ia64_get_pmc(0);
6398 /*
6399 * unfreeze PMU if had pending overflows
6400 */
6401 if (t->pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
6403 /*
6404 * now get can unmask PMU interrupts, they will
6405 * be treated as purely spurious and we will not
6406 * lose any information
6407 */
6408 UNPROTECT_CTX(ctx,flags);
6410 #endif /* CONFIG_SMP */
6412 #ifdef CONFIG_SMP
6413 /*
6414 * in 2.6, interrupts are masked when we come here and the runqueue lock is held
6415 */
6416 void
6417 pfm_load_regs (struct task_struct *task)
6419 pfm_context_t *ctx;
6420 struct thread_struct *t;
6421 unsigned long pmc_mask = 0UL, pmd_mask = 0UL;
6422 unsigned long flags;
6423 u64 psr, psr_up;
6424 int need_irq_resend;
6426 ctx = PFM_GET_CTX(task);
6427 if (unlikely(ctx == NULL)) return;
6429 BUG_ON(GET_PMU_OWNER());
6431 t = &task->thread;
6432 /*
6433 * possible on unload
6434 */
6435 if (unlikely((t->flags & IA64_THREAD_PM_VALID) == 0)) return;
6437 /*
6438 * we always come here with interrupts ALREADY disabled by
6439 * the scheduler. So we simply need to protect against concurrent
6440 * access, not CPU concurrency.
6441 */
6442 flags = pfm_protect_ctx_ctxsw(ctx);
6443 psr = pfm_get_psr();
6445 need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
6447 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
6448 BUG_ON(psr & IA64_PSR_I);
6450 if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) {
6451 struct pt_regs *regs = task_pt_regs(task);
6453 BUG_ON(ctx->ctx_smpl_hdr);
6455 pfm_force_cleanup(ctx, regs);
6457 pfm_unprotect_ctx_ctxsw(ctx, flags);
6459 /*
6460 * this one (kmalloc'ed) is fine with interrupts disabled
6461 */
6462 pfm_context_free(ctx);
6464 return;
6467 /*
6468 * we restore ALL the debug registers to avoid picking up
6469 * stale state.
6470 */
6471 if (ctx->ctx_fl_using_dbreg) {
6472 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
6473 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
6475 /*
6476 * retrieve saved psr.up
6477 */
6478 psr_up = ctx->ctx_saved_psr_up;
6480 /*
6481 * if we were the last user of the PMU on that CPU,
6482 * then nothing to do except restore psr
6483 */
6484 if (GET_LAST_CPU(ctx) == smp_processor_id() && ctx->ctx_last_activation == GET_ACTIVATION()) {
6486 /*
6487 * retrieve partial reload masks (due to user modifications)
6488 */
6489 pmc_mask = ctx->ctx_reload_pmcs[0];
6490 pmd_mask = ctx->ctx_reload_pmds[0];
6492 } else {
6493 /*
6494 * To avoid leaking information to the user level when psr.sp=0,
6495 * we must reload ALL implemented pmds (even the ones we don't use).
6496 * In the kernel we only allow PFM_READ_PMDS on registers which
6497 * we initialized or requested (sampling) so there is no risk there.
6498 */
6499 pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
6501 /*
6502 * ALL accessible PMCs are systematically reloaded, unused registers
6503 * get their default (from pfm_reset_pmu_state()) values to avoid picking
6504 * up stale configuration.
6506 * PMC0 is never in the mask. It is always restored separately.
6507 */
6508 pmc_mask = ctx->ctx_all_pmcs[0];
6510 /*
6511 * when context is MASKED, we will restore PMC with plm=0
6512 * and PMD with stale information, but that's ok, nothing
6513 * will be captured.
6515 * XXX: optimize here
6516 */
6517 if (pmd_mask) pfm_restore_pmds(t->pmds, pmd_mask);
6518 if (pmc_mask) pfm_restore_pmcs(t->pmcs, pmc_mask);
6520 /*
6521 * check for pending overflow at the time the state
6522 * was saved.
6523 */
6524 if (unlikely(PMC0_HAS_OVFL(t->pmcs[0]))) {
6525 /*
6526 * reload pmc0 with the overflow information
6527 * On McKinley PMU, this will trigger a PMU interrupt
6528 */
6529 ia64_set_pmc(0, t->pmcs[0]);
6530 ia64_srlz_d();
6531 t->pmcs[0] = 0UL;
6533 /*
6534 * will replay the PMU interrupt
6535 */
6536 if (need_irq_resend) hw_resend_irq(NULL, IA64_PERFMON_VECTOR);
6538 pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
6541 /*
6542 * we just did a reload, so we reset the partial reload fields
6543 */
6544 ctx->ctx_reload_pmcs[0] = 0UL;
6545 ctx->ctx_reload_pmds[0] = 0UL;
6547 SET_LAST_CPU(ctx, smp_processor_id());
6549 /*
6550 * dump activation value for this PMU
6551 */
6552 INC_ACTIVATION();
6553 /*
6554 * record current activation for this context
6555 */
6556 SET_ACTIVATION(ctx);
6558 /*
6559 * establish new ownership.
6560 */
6561 SET_PMU_OWNER(task, ctx);
6563 /*
6564 * restore the psr.up bit. measurement
6565 * is active again.
6566 * no PMU interrupt can happen at this point
6567 * because we still have interrupts disabled.
6568 */
6569 if (likely(psr_up)) pfm_set_psr_up();
6571 /*
6572 * allow concurrent access to context
6573 */
6574 pfm_unprotect_ctx_ctxsw(ctx, flags);
6576 #else /* !CONFIG_SMP */
6577 /*
6578 * reload PMU state for UP kernels
6579 * in 2.5 we come here with interrupts disabled
6580 */
6581 void
6582 pfm_load_regs (struct task_struct *task)
6584 struct thread_struct *t;
6585 pfm_context_t *ctx;
6586 struct task_struct *owner;
6587 unsigned long pmd_mask, pmc_mask;
6588 u64 psr, psr_up;
6589 int need_irq_resend;
6591 owner = GET_PMU_OWNER();
6592 ctx = PFM_GET_CTX(task);
6593 t = &task->thread;
6594 psr = pfm_get_psr();
6596 BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
6597 BUG_ON(psr & IA64_PSR_I);
6599 /*
6600 * we restore ALL the debug registers to avoid picking up
6601 * stale state.
6603 * This must be done even when the task is still the owner
6604 * as the registers may have been modified via ptrace()
6605 * (not perfmon) by the previous task.
6606 */
6607 if (ctx->ctx_fl_using_dbreg) {
6608 pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
6609 pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
6612 /*
6613 * retrieved saved psr.up
6614 */
6615 psr_up = ctx->ctx_saved_psr_up;
6616 need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
6618 /*
6619 * short path, our state is still there, just
6620 * need to restore psr and we go
6622 * we do not touch either PMC nor PMD. the psr is not touched
6623 * by the overflow_handler. So we are safe w.r.t. to interrupt
6624 * concurrency even without interrupt masking.
6625 */
6626 if (likely(owner == task)) {
6627 if (likely(psr_up)) pfm_set_psr_up();
6628 return;
6631 /*
6632 * someone else is still using the PMU, first push it out and
6633 * then we'll be able to install our stuff !
6635 * Upon return, there will be no owner for the current PMU
6636 */
6637 if (owner) pfm_lazy_save_regs(owner);
6639 /*
6640 * To avoid leaking information to the user level when psr.sp=0,
6641 * we must reload ALL implemented pmds (even the ones we don't use).
6642 * In the kernel we only allow PFM_READ_PMDS on registers which
6643 * we initialized or requested (sampling) so there is no risk there.
6644 */
6645 pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
6647 /*
6648 * ALL accessible PMCs are systematically reloaded, unused registers
6649 * get their default (from pfm_reset_pmu_state()) values to avoid picking
6650 * up stale configuration.
6652 * PMC0 is never in the mask. It is always restored separately
6653 */
6654 pmc_mask = ctx->ctx_all_pmcs[0];
6656 pfm_restore_pmds(t->pmds, pmd_mask);
6657 pfm_restore_pmcs(t->pmcs, pmc_mask);
6659 /*
6660 * check for pending overflow at the time the state
6661 * was saved.
6662 */
6663 if (unlikely(PMC0_HAS_OVFL(t->pmcs[0]))) {
6664 /*
6665 * reload pmc0 with the overflow information
6666 * On McKinley PMU, this will trigger a PMU interrupt
6667 */
6668 ia64_set_pmc(0, t->pmcs[0]);
6669 ia64_srlz_d();
6671 t->pmcs[0] = 0UL;
6673 /*
6674 * will replay the PMU interrupt
6675 */
6676 if (need_irq_resend) hw_resend_irq(NULL, IA64_PERFMON_VECTOR);
6678 pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
6681 /*
6682 * establish new ownership.
6683 */
6684 SET_PMU_OWNER(task, ctx);
6686 /*
6687 * restore the psr.up bit. measurement
6688 * is active again.
6689 * no PMU interrupt can happen at this point
6690 * because we still have interrupts disabled.
6691 */
6692 if (likely(psr_up)) pfm_set_psr_up();
6694 #endif /* CONFIG_SMP */
6695 #endif /* XEN */
6697 /*
6698 * this function assumes monitoring is stopped
6699 */
6700 static void
6701 pfm_flush_pmds(struct task_struct *task, pfm_context_t *ctx)
6703 #ifndef XEN
6704 u64 pmc0;
6705 unsigned long mask2, val, pmd_val, ovfl_val;
6706 int i, can_access_pmu = 0;
6707 int is_self;
6709 /*
6710 * is the caller the task being monitored (or which initiated the
6711 * session for system wide measurements)
6712 */
6713 is_self = ctx->ctx_task == task ? 1 : 0;
6715 /*
6716 * can access PMU is task is the owner of the PMU state on the current CPU
6717 * or if we are running on the CPU bound to the context in system-wide mode
6718 * (that is not necessarily the task the context is attached to in this mode).
6719 * In system-wide we always have can_access_pmu true because a task running on an
6720 * invalid processor is flagged earlier in the call stack (see pfm_stop).
6721 */
6722 can_access_pmu = (GET_PMU_OWNER() == task) || (ctx->ctx_fl_system && ctx->ctx_cpu == smp_processor_id());
6723 if (can_access_pmu) {
6724 /*
6725 * Mark the PMU as not owned
6726 * This will cause the interrupt handler to do nothing in case an overflow
6727 * interrupt was in-flight
6728 * This also guarantees that pmc0 will contain the final state
6729 * It virtually gives us full control on overflow processing from that point
6730 * on.
6731 */
6732 SET_PMU_OWNER(NULL, NULL);
6733 DPRINT(("releasing ownership\n"));
6735 /*
6736 * read current overflow status:
6738 * we are guaranteed to read the final stable state
6739 */
6740 ia64_srlz_d();
6741 pmc0 = ia64_get_pmc(0); /* slow */
6743 /*
6744 * reset freeze bit, overflow status information destroyed
6745 */
6746 pfm_unfreeze_pmu();
6747 } else {
6748 pmc0 = task->thread.pmcs[0];
6749 /*
6750 * clear whatever overflow status bits there were
6751 */
6752 task->thread.pmcs[0] = 0;
6754 ovfl_val = pmu_conf->ovfl_val;
6755 /*
6756 * we save all the used pmds
6757 * we take care of overflows for counting PMDs
6759 * XXX: sampling situation is not taken into account here
6760 */
6761 mask2 = ctx->ctx_used_pmds[0];
6763 DPRINT(("is_self=%d ovfl_val=0x%lx mask2=0x%lx\n", is_self, ovfl_val, mask2));
6765 for (i = 0; mask2; i++, mask2>>=1) {
6767 /* skip non used pmds */
6768 if ((mask2 & 0x1) == 0) continue;
6770 /*
6771 * can access PMU always true in system wide mode
6772 */
6773 val = pmd_val = can_access_pmu ? ia64_get_pmd(i) : task->thread.pmds[i];
6775 if (PMD_IS_COUNTING(i)) {
6776 DPRINT(("[%d] pmd[%d] ctx_pmd=0x%lx hw_pmd=0x%lx\n",
6777 task->pid,
6778 i,
6779 ctx->ctx_pmds[i].val,
6780 val & ovfl_val));
6782 /*
6783 * we rebuild the full 64 bit value of the counter
6784 */
6785 val = ctx->ctx_pmds[i].val + (val & ovfl_val);
6787 /*
6788 * now everything is in ctx_pmds[] and we need
6789 * to clear the saved context from save_regs() such that
6790 * pfm_read_pmds() gets the correct value
6791 */
6792 pmd_val = 0UL;
6794 /*
6795 * take care of overflow inline
6796 */
6797 if (pmc0 & (1UL << i)) {
6798 val += 1 + ovfl_val;
6799 DPRINT(("[%d] pmd[%d] overflowed\n", task->pid, i));
6803 DPRINT(("[%d] ctx_pmd[%d]=0x%lx pmd_val=0x%lx\n", task->pid, i, val, pmd_val));
6805 if (is_self) task->thread.pmds[i] = pmd_val;
6807 ctx->ctx_pmds[i].val = val;
6809 #else
6810 /* XXX */
6811 #endif
6814 static struct irqaction perfmon_irqaction = {
6815 .handler = pfm_interrupt_handler,
6816 #ifndef XEN
6817 .flags = SA_INTERRUPT,
6818 #endif
6819 .name = "perfmon"
6820 };
6822 #ifndef XEN
6823 static void
6824 pfm_alt_save_pmu_state(void *data)
6826 struct pt_regs *regs;
6828 regs = task_pt_regs(current);
6830 DPRINT(("called\n"));
6832 /*
6833 * should not be necessary but
6834 * let's take not risk
6835 */
6836 pfm_clear_psr_up();
6837 pfm_clear_psr_pp();
6838 ia64_psr(regs)->pp = 0;
6840 /*
6841 * This call is required
6842 * May cause a spurious interrupt on some processors
6843 */
6844 pfm_freeze_pmu();
6846 ia64_srlz_d();
6849 void
6850 pfm_alt_restore_pmu_state(void *data)
6852 struct pt_regs *regs;
6854 regs = task_pt_regs(current);
6856 DPRINT(("called\n"));
6858 /*
6859 * put PMU back in state expected
6860 * by perfmon
6861 */
6862 pfm_clear_psr_up();
6863 pfm_clear_psr_pp();
6864 ia64_psr(regs)->pp = 0;
6866 /*
6867 * perfmon runs with PMU unfrozen at all times
6868 */
6869 pfm_unfreeze_pmu();
6871 ia64_srlz_d();
6874 int
6875 pfm_install_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
6877 int ret, i;
6878 int reserve_cpu;
6880 /* some sanity checks */
6881 if (hdl == NULL || hdl->handler == NULL) return -EINVAL;
6883 /* do the easy test first */
6884 if (pfm_alt_intr_handler) return -EBUSY;
6886 /* one at a time in the install or remove, just fail the others */
6887 if (!spin_trylock(&pfm_alt_install_check)) {
6888 return -EBUSY;
6891 /* reserve our session */
6892 for_each_online_cpu(reserve_cpu) {
6893 ret = pfm_reserve_session(NULL, 1, reserve_cpu);
6894 if (ret) goto cleanup_reserve;
6897 /* save the current system wide pmu states */
6898 ret = on_each_cpu(pfm_alt_save_pmu_state, NULL, 0, 1);
6899 if (ret) {
6900 DPRINT(("on_each_cpu() failed: %d\n", ret));
6901 goto cleanup_reserve;
6904 /* officially change to the alternate interrupt handler */
6905 pfm_alt_intr_handler = hdl;
6907 spin_unlock(&pfm_alt_install_check);
6909 return 0;
6911 cleanup_reserve:
6912 for_each_online_cpu(i) {
6913 /* don't unreserve more than we reserved */
6914 if (i >= reserve_cpu) break;
6916 pfm_unreserve_session(NULL, 1, i);
6919 spin_unlock(&pfm_alt_install_check);
6921 return ret;
6923 EXPORT_SYMBOL_GPL(pfm_install_alt_pmu_interrupt);
6925 int
6926 pfm_remove_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
6928 int i;
6929 int ret;
6931 if (hdl == NULL) return -EINVAL;
6933 /* cannot remove someone else's handler! */
6934 if (pfm_alt_intr_handler != hdl) return -EINVAL;
6936 /* one at a time in the install or remove, just fail the others */
6937 if (!spin_trylock(&pfm_alt_install_check)) {
6938 return -EBUSY;
6941 pfm_alt_intr_handler = NULL;
6943 ret = on_each_cpu(pfm_alt_restore_pmu_state, NULL, 0, 1);
6944 if (ret) {
6945 DPRINT(("on_each_cpu() failed: %d\n", ret));
6948 for_each_online_cpu(i) {
6949 pfm_unreserve_session(NULL, 1, i);
6952 spin_unlock(&pfm_alt_install_check);
6954 return 0;
6956 EXPORT_SYMBOL_GPL(pfm_remove_alt_pmu_interrupt);
6957 #endif
6959 /*
6960 * perfmon initialization routine, called from the initcall() table
6961 */
6962 #ifndef XEN
6963 static int init_pfm_fs(void);
6964 #else
6965 #define init_pfm_fs() do {} while(0)
6966 #endif
6968 static int __init
6969 pfm_probe_pmu(void)
6971 pmu_config_t **p;
6972 int family;
6974 family = local_cpu_data->family;
6975 p = pmu_confs;
6977 while(*p) {
6978 if ((*p)->probe) {
6979 if ((*p)->probe() == 0) goto found;
6980 } else if ((*p)->pmu_family == family || (*p)->pmu_family == 0xff) {
6981 goto found;
6983 p++;
6985 return -1;
6986 found:
6987 pmu_conf = *p;
6988 return 0;
6991 #ifndef XEN
6992 static struct file_operations pfm_proc_fops = {
6993 .open = pfm_proc_open,
6994 .read = seq_read,
6995 .llseek = seq_lseek,
6996 .release = seq_release,
6997 };
6998 #endif
7000 int __init
7001 pfm_init(void)
7003 unsigned int n, n_counters, i;
7005 printk("perfmon: version %u.%u IRQ %u\n",
7006 PFM_VERSION_MAJ,
7007 PFM_VERSION_MIN,
7008 IA64_PERFMON_VECTOR);
7010 if (pfm_probe_pmu()) {
7011 printk(KERN_INFO "perfmon: disabled, there is no support for processor family %d\n",
7012 local_cpu_data->family);
7013 return -ENODEV;
7016 /*
7017 * compute the number of implemented PMD/PMC from the
7018 * description tables
7019 */
7020 n = 0;
7021 for (i=0; PMC_IS_LAST(i) == 0; i++) {
7022 if (PMC_IS_IMPL(i) == 0) continue;
7023 pmu_conf->impl_pmcs[i>>6] |= 1UL << (i&63);
7024 n++;
7026 pmu_conf->num_pmcs = n;
7028 n = 0; n_counters = 0;
7029 for (i=0; PMD_IS_LAST(i) == 0; i++) {
7030 if (PMD_IS_IMPL(i) == 0) continue;
7031 pmu_conf->impl_pmds[i>>6] |= 1UL << (i&63);
7032 n++;
7033 if (PMD_IS_COUNTING(i)) n_counters++;
7035 pmu_conf->num_pmds = n;
7036 pmu_conf->num_counters = n_counters;
7038 /*
7039 * sanity checks on the number of debug registers
7040 */
7041 if (pmu_conf->use_rr_dbregs) {
7042 if (pmu_conf->num_ibrs > IA64_NUM_DBG_REGS) {
7043 printk(KERN_INFO "perfmon: unsupported number of code debug registers (%u)\n", pmu_conf->num_ibrs);
7044 pmu_conf = NULL;
7045 return -1;
7047 if (pmu_conf->num_dbrs > IA64_NUM_DBG_REGS) {
7048 printk(KERN_INFO "perfmon: unsupported number of data debug registers (%u)\n", pmu_conf->num_ibrs);
7049 pmu_conf = NULL;
7050 return -1;
7054 printk("perfmon: %s PMU detected, %u PMCs, %u PMDs, %u counters (%lu bits)\n",
7055 pmu_conf->pmu_name,
7056 pmu_conf->num_pmcs,
7057 pmu_conf->num_pmds,
7058 pmu_conf->num_counters,
7059 ffz(pmu_conf->ovfl_val));
7061 /* sanity check */
7062 if (pmu_conf->num_pmds >= IA64_NUM_PMD_REGS || pmu_conf->num_pmcs >= IA64_NUM_PMC_REGS) {
7063 printk(KERN_ERR "perfmon: not enough pmc/pmd, perfmon disabled\n");
7064 pmu_conf = NULL;
7065 return -1;
7068 #ifndef XEN
7069 /*
7070 * create /proc/perfmon (mostly for debugging purposes)
7071 */
7072 perfmon_dir = create_proc_entry("perfmon", S_IRUGO, NULL);
7073 if (perfmon_dir == NULL) {
7074 printk(KERN_ERR "perfmon: cannot create /proc entry, perfmon disabled\n");
7075 pmu_conf = NULL;
7076 return -1;
7078 /*
7079 * install customized file operations for /proc/perfmon entry
7080 */
7081 perfmon_dir->proc_fops = &pfm_proc_fops;
7083 /*
7084 * create /proc/sys/kernel/perfmon (for debugging purposes)
7085 */
7086 pfm_sysctl_header = register_sysctl_table(pfm_sysctl_root, 0);
7087 #endif
7089 /*
7090 * initialize all our spinlocks
7091 */
7092 spin_lock_init(&pfm_sessions.pfs_lock);
7093 spin_lock_init(&pfm_buffer_fmt_lock);
7095 init_pfm_fs();
7097 for(i=0; i < NR_CPUS; i++) pfm_stats[i].pfm_ovfl_intr_cycles_min = ~0UL;
7099 return 0;
7102 __initcall(pfm_init);
7104 /*
7105 * this function is called before pfm_init()
7106 */
7107 void
7108 pfm_init_percpu (void)
7110 /*
7111 * make sure no measurement is active
7112 * (may inherit programmed PMCs from EFI).
7113 */
7114 pfm_clear_psr_pp();
7115 pfm_clear_psr_up();
7117 /*
7118 * we run with the PMU not frozen at all times
7119 */
7120 pfm_unfreeze_pmu();
7122 if (smp_processor_id() == 0)
7123 register_percpu_irq(IA64_PERFMON_VECTOR, &perfmon_irqaction);
7125 ia64_setreg(_IA64_REG_CR_PMV, IA64_PERFMON_VECTOR);
7126 ia64_srlz_d();
7129 /*
7130 * used for debug purposes only
7131 */
7132 void
7133 dump_pmu_state(const char *from)
7135 struct task_struct *task;
7136 struct thread_struct *t;
7137 struct pt_regs *regs;
7138 pfm_context_t *ctx;
7139 unsigned long psr, dcr, info, flags;
7140 int i, this_cpu;
7142 local_irq_save(flags);
7144 this_cpu = smp_processor_id();
7145 regs = task_pt_regs(current);
7146 info = PFM_CPUINFO_GET();
7147 dcr = ia64_getreg(_IA64_REG_CR_DCR);
7149 if (info == 0 && ia64_psr(regs)->pp == 0 && (dcr & IA64_DCR_PP) == 0) {
7150 local_irq_restore(flags);
7151 return;
7154 #ifndef XEN
7155 printk("CPU%d from %s() current [%d] iip=0x%lx %s\n",
7156 this_cpu,
7157 from,
7158 current->pid,
7159 regs->cr_iip,
7160 current->comm);
7161 #endif
7163 task = GET_PMU_OWNER();
7164 ctx = GET_PMU_CTX();
7166 printk("->CPU%d owner [%d] ctx=%p\n", this_cpu, task ? task->pid : -1, ctx);
7168 psr = pfm_get_psr();
7170 printk("->CPU%d pmc0=0x%lx psr.pp=%d psr.up=%d dcr.pp=%d syst_info=0x%lx user_psr.up=%d user_psr.pp=%d\n",
7171 this_cpu,
7172 ia64_get_pmc(0),
7173 psr & IA64_PSR_PP ? 1 : 0,
7174 psr & IA64_PSR_UP ? 1 : 0,
7175 dcr & IA64_DCR_PP ? 1 : 0,
7176 info,
7177 ia64_psr(regs)->up,
7178 ia64_psr(regs)->pp);
7180 ia64_psr(regs)->up = 0;
7181 ia64_psr(regs)->pp = 0;
7183 t = &current->thread;
7185 for (i=1; PMC_IS_LAST(i) == 0; i++) {
7186 if (PMC_IS_IMPL(i) == 0) continue;
7187 printk("->CPU%d pmc[%d]=0x%lx thread_pmc[%d]=0x%lx\n", this_cpu, i, ia64_get_pmc(i), i, t->pmcs[i]);
7190 for (i=1; PMD_IS_LAST(i) == 0; i++) {
7191 if (PMD_IS_IMPL(i) == 0) continue;
7192 printk("->CPU%d pmd[%d]=0x%lx thread_pmd[%d]=0x%lx\n", this_cpu, i, ia64_get_pmd(i), i, t->pmds[i]);
7195 if (ctx) {
7196 #ifndef XEN
7197 printk("->CPU%d ctx_state=%d vaddr=%p addr=%p fd=%d ctx_task=[%d] saved_psr_up=0x%lx\n",
7198 this_cpu,
7199 ctx->ctx_state,
7200 ctx->ctx_smpl_vaddr,
7201 ctx->ctx_smpl_hdr,
7202 ctx->ctx_msgq_head,
7203 ctx->ctx_msgq_tail,
7204 ctx->ctx_saved_psr_up);
7205 #else
7206 printk("->CPU%d ctx_state=%d vaddr=%p addr=%p saved_psr_up=0x%lx\n",
7207 this_cpu,
7208 ctx->ctx_state,
7209 ctx->ctx_smpl_vaddr,
7210 ctx->ctx_smpl_hdr,
7211 ctx->ctx_saved_psr_up);
7212 #endif
7214 local_irq_restore(flags);
7217 #ifndef XEN
7218 /*
7219 * called from process.c:copy_thread(). task is new child.
7220 */
7221 void
7222 pfm_inherit(struct task_struct *task, struct pt_regs *regs)
7224 struct thread_struct *thread;
7226 DPRINT(("perfmon: pfm_inherit clearing state for [%d]\n", task->pid));
7228 thread = &task->thread;
7230 /*
7231 * cut links inherited from parent (current)
7232 */
7233 thread->pfm_context = NULL;
7235 PFM_SET_WORK_PENDING(task, 0);
7237 /*
7238 * the psr bits are already set properly in copy_threads()
7239 */
7241 #endif
7242 #else /* !CONFIG_PERFMON */
7243 asmlinkage long
7244 sys_perfmonctl (int fd, int cmd, void *arg, int count)
7246 return -ENOSYS;
7248 #endif /* CONFIG_PERFMON */
7251 #ifdef XEN
7252 static int xenpfm_context_unload(void);
7253 static int xenpfm_start_stop_locked(int is_start);
7254 DEFINE_PER_CPU(pfm_context_t*, xenpfm_context);
7256 /*
7257 * note: some functions mask interrupt with this lock held
7258 * so that this lock can't be locked from interrupt handler.
7259 */
7260 DEFINE_SPINLOCK(xenpfm_context_lock);
7262 static int
7263 xenpfm_get_features(XEN_GUEST_HANDLE(pfarg_features_t) req)
7265 pfarg_features_t res;
7266 if (guest_handle_is_null(req))
7267 return -EFAULT;
7269 memset(&res, 0, sizeof(res));
7270 pfm_get_features(NULL, &res, 0, NULL);
7271 if (copy_to_guest(req, &res, 1))
7272 return -EFAULT;
7273 return 0;
7276 static int
7277 xenpfm_pfarg_is_sane(pfarg_context_t* pfx)
7279 int error;
7280 int ctx_flags;
7282 error = pfarg_is_sane(NULL, pfx);
7283 if (error)
7284 return error;
7286 ctx_flags = pfx->ctx_flags;
7287 if (!(ctx_flags & PFM_FL_SYSTEM_WIDE) ||
7288 ctx_flags & PFM_FL_NOTIFY_BLOCK ||
7289 ctx_flags & PFM_FL_OVFL_NO_MSG)
7290 return -EINVAL;
7292 /* probably more to add here */
7294 return 0;
7297 static int
7298 xenpfm_context_create(XEN_GUEST_HANDLE(pfarg_context_t) req)
7300 int error;
7301 pfarg_context_t kreq;
7303 int cpu;
7304 pfm_context_t* ctx[NR_CPUS] = {[0 ... (NR_CPUS - 1)] = NULL};
7306 if (copy_from_guest(&kreq, req, 1)) {
7307 error = -EINVAL;
7308 goto out;
7311 error = xenpfm_pfarg_is_sane(&kreq);
7312 if (error)
7313 goto out;
7315 /* XXX fmt */
7316 for_each_cpu(cpu) {
7317 ctx[cpu] = pfm_context_create(&kreq);
7318 if (ctx[cpu] == NULL) {
7319 error = -ENOMEM;
7320 break;
7323 if (error)
7324 goto out;
7326 BUG_ON(in_irq());
7327 spin_lock(&xenpfm_context_lock);
7328 for_each_cpu(cpu) {
7329 if (per_cpu(xenpfm_context, cpu) != NULL) {
7330 error = -EBUSY;
7331 break;
7334 for_each_cpu(cpu) {
7335 per_cpu(xenpfm_context, cpu) = ctx[cpu];
7336 ctx[cpu] = NULL;
7338 spin_unlock(&xenpfm_context_lock);
7340 out:
7341 for_each_cpu(cpu) {
7342 if (ctx[cpu] != NULL)
7343 pfm_context_free(ctx[cpu]);
7345 return error;
7348 static int
7349 xenpfm_context_destroy(void)
7351 int cpu;
7352 pfm_context_t* ctx;
7353 unsigned long flags;
7354 unsigned long need_unload;
7355 int error = 0;
7357 again:
7358 need_unload = 0;
7359 BUG_ON(in_irq());
7360 spin_lock_irqsave(&xenpfm_context_lock, flags);
7361 for_each_cpu(cpu) {
7362 ctx = per_cpu(xenpfm_context, cpu);
7363 if (ctx == NULL) {
7364 error = -EINVAL;
7365 break;
7367 PROTECT_CTX_NOIRQ(ctx);
7368 if (ctx->ctx_state != PFM_CTX_UNLOADED)
7369 need_unload = 1;
7371 if (error) {
7372 for_each_cpu(cpu) {
7373 ctx = per_cpu(xenpfm_context, cpu);
7374 if (ctx == NULL)
7375 break;
7376 UNPROTECT_CTX_NOIRQ(per_cpu(xenpfm_context, cpu));
7378 goto out;
7380 if (need_unload) {
7381 for_each_cpu(cpu)
7382 UNPROTECT_CTX_NOIRQ(per_cpu(xenpfm_context, cpu));
7383 spin_unlock_irqrestore(&xenpfm_context_lock, flags);
7385 error = xenpfm_context_unload();
7386 if (error)
7387 return error;
7388 goto again;
7391 for_each_cpu(cpu) {
7392 pfm_context_t* ctx = per_cpu(xenpfm_context, cpu);
7393 per_cpu(xenpfm_context, cpu) = NULL;
7395 /* pfm_close() unlocks spinlock and free the context. */
7396 error |= pfm_close(ctx);
7398 out:
7399 spin_unlock_irqrestore(&xenpfm_context_lock, flags);
7400 return error;
7403 static int
7404 xenpfm_write_pmcs(XEN_GUEST_HANDLE(pfarg_reg_t) req, unsigned long count)
7406 unsigned long i;
7407 int error = 0;
7408 unsigned long flags;
7410 for (i = 0; i < count; i++) {
7411 pfarg_reg_t kreq;
7412 int cpu;
7413 if (copy_from_guest_offset(&kreq, req, i, 1)) {
7414 error = -EFAULT;
7415 goto out;
7417 BUG_ON(in_irq());
7418 spin_lock_irqsave(&xenpfm_context_lock, flags);
7419 for_each_online_cpu(cpu) {
7420 pfm_context_t* ctx = per_cpu(xenpfm_context, cpu);
7421 BUG_ON(ctx == NULL);
7422 PROTECT_CTX_NOIRQ(ctx);
7423 error |= pfm_write_pmcs(ctx, (void *)&kreq, 1, NULL);
7424 UNPROTECT_CTX_NOIRQ(ctx);
7426 spin_unlock_irqrestore(&xenpfm_context_lock, flags);
7429 /* XXX if is loaded, change all physical cpus pmcs. */
7430 /* Currently results in error */
7431 out:
7432 return error;
7435 static int
7436 xenpfm_write_pmds(XEN_GUEST_HANDLE(pfarg_reg_t) req, unsigned long count)
7438 unsigned long i;
7439 int error = 0;
7441 for (i = 0; i < count; i++) {
7442 pfarg_reg_t kreq;
7443 int cpu;
7444 unsigned long flags;
7445 if (copy_from_guest_offset(&kreq, req, i, 1)) {
7446 error = -EFAULT;
7447 goto out;
7449 BUG_ON(in_irq());
7450 spin_lock_irqsave(&xenpfm_context_lock, flags);
7451 for_each_online_cpu(cpu) {
7452 pfm_context_t* ctx = per_cpu(xenpfm_context, cpu);
7453 BUG_ON(ctx == NULL);
7454 PROTECT_CTX_NOIRQ(ctx);
7455 error |= pfm_write_pmds(ctx, &kreq, 1, NULL);
7456 UNPROTECT_CTX_NOIRQ(ctx);
7458 spin_unlock_irqrestore(&xenpfm_context_lock, flags);
7461 /* XXX if is loaded, change all physical cpus pmds. */
7462 /* Currently results in error */
7463 out:
7464 return error;
7467 struct xenpfm_context_load_arg {
7468 pfarg_load_t* req;
7469 int error[NR_CPUS];
7470 };
7472 static void
7473 xenpfm_context_load_cpu(void* info)
7475 unsigned long flags;
7476 struct xenpfm_context_load_arg* arg = (struct xenpfm_context_load_arg*)info;
7477 pfm_context_t* ctx = __get_cpu_var(xenpfm_context);
7479 BUG_ON(ctx == NULL);
7480 PROTECT_CTX(ctx, flags);
7481 arg->error[smp_processor_id()] = pfm_context_load(ctx, arg->req, 0, NULL);
7482 UNPROTECT_CTX(ctx, flags);
7485 static int
7486 xenpfm_context_load(XEN_GUEST_HANDLE(pfarg_load_t) req)
7488 pfarg_load_t kreq;
7489 int cpu;
7490 struct xenpfm_context_load_arg arg;
7491 int error = 0;
7493 if (copy_from_guest(&kreq, req, 1))
7494 return -EFAULT;
7496 arg.req = &kreq;
7497 for_each_online_cpu(cpu)
7498 arg.error[cpu] = 0;
7500 BUG_ON(in_irq());
7501 spin_lock(&xenpfm_context_lock);
7502 smp_call_function(&xenpfm_context_load_cpu, &arg, 1, 1);
7503 xenpfm_context_load_cpu(&arg);
7504 spin_unlock(&xenpfm_context_lock);
7505 for_each_online_cpu(cpu) {
7506 if (arg.error[cpu]) {
7507 gdprintk(XENLOG_INFO, "%s: cpu %d error %d\n",
7508 __func__, cpu, arg.error[cpu]);
7509 error = arg.error[cpu];
7512 return 0;
7516 struct xenpfm_context_unload_arg {
7517 int error[NR_CPUS];
7518 };
7520 static void
7521 xenpfm_context_unload_cpu(void* info)
7523 unsigned long flags;
7524 struct xenpfm_context_unload_arg* arg = (struct xenpfm_context_unload_arg*)info;
7525 pfm_context_t* ctx = __get_cpu_var(xenpfm_context);
7526 BUG_ON(ctx == NULL);
7527 PROTECT_CTX(ctx, flags);
7528 arg->error[smp_processor_id()] = pfm_context_unload(ctx, NULL, 0, NULL);
7529 UNPROTECT_CTX(ctx, flags);
7532 static int
7533 xenpfm_context_unload(void)
7535 int cpu;
7536 struct xenpfm_context_unload_arg arg;
7537 unsigned long flags;
7538 int error = 0;
7540 for_each_online_cpu(cpu)
7541 arg.error[cpu] = 0;
7543 BUG_ON(in_irq());
7544 local_irq_save(flags);
7545 if (!spin_trylock(&xenpfm_context_lock)) {
7546 local_irq_restore(flags);
7547 return -EAGAIN;
7549 error = xenpfm_start_stop_locked(0);
7550 local_irq_restore(flags);
7551 if (error) {
7552 spin_unlock(&xenpfm_context_lock);
7553 return error;
7556 smp_call_function(&xenpfm_context_unload_cpu, &arg, 1, 1);
7557 xenpfm_context_unload_cpu(&arg);
7558 spin_unlock(&xenpfm_context_lock);
7559 for_each_online_cpu(cpu) {
7560 if (arg.error[cpu]) {
7561 gdprintk(XENLOG_INFO, "%s: cpu %d error %d\n",
7562 __func__, cpu, arg.error[cpu]);
7563 error = arg.error[cpu];
7566 return error;
7569 static int
7570 __xenpfm_start(void)
7572 pfm_context_t* ctx = __get_cpu_var(xenpfm_context);
7573 int state;
7574 int error = 0;
7576 BUG_ON(ctx == NULL);
7577 BUG_ON(local_irq_is_enabled());
7578 PROTECT_CTX_NOIRQ(ctx);
7579 state = ctx->ctx_state;
7580 if (state != PFM_CTX_LOADED) {
7581 gdprintk(XENLOG_DEBUG, "%s state %d\n", __func__, state);
7582 error = -EINVAL;
7583 goto out;
7586 /* now update the local PMU and cpuinfo */
7587 PFM_CPUINFO_SET(PFM_CPUINFO_DCR_PP);
7589 /* start monitoring at kernel level */
7590 pfm_set_psr_pp();
7592 /* start monitoring at kernel level */
7593 pfm_set_psr_up();
7595 /* enable dcr pp */
7596 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
7597 ia64_srlz_i();
7598 out:
7599 UNPROTECT_CTX_NOIRQ(ctx);
7600 return error;
7603 static int
7604 __xenpfm_stop(void)
7606 pfm_context_t* ctx = __get_cpu_var(xenpfm_context);
7607 int state;
7608 int error = 0;
7610 BUG_ON(local_irq_is_enabled());
7611 if (ctx == NULL) {
7612 gdprintk(XENLOG_DEBUG, "%s ctx=NULL p:%2d v:%2d\n",
7613 __func__, smp_processor_id(), current->vcpu_id);
7614 return 0;
7617 PROTECT_CTX_NOIRQ(ctx);
7618 state = ctx->ctx_state;
7619 if (state != PFM_CTX_LOADED) {
7620 gdprintk(XENLOG_DEBUG, "%s state %d p:%2d v:%2d\n",
7621 __func__, state,
7622 smp_processor_id(), current->vcpu_id);
7623 error = -EINVAL;
7624 goto out;
7627 /*
7628 * Update local PMU first
7630 * disable dcr pp
7631 */
7632 ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
7633 ia64_srlz_i();
7635 /* update local cpuinfo */
7636 PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
7638 /* stop monitoring, does srlz.i */
7639 pfm_clear_psr_pp();
7641 /* stop monitoring at kernel level */
7642 pfm_clear_psr_up();
7643 out:
7644 UNPROTECT_CTX_NOIRQ(ctx);
7645 return error;
7648 int
7649 __xenpfm_start_stop(int is_start)
7651 if (is_start)
7652 return __xenpfm_start();
7653 else
7654 return __xenpfm_stop();
7657 struct xenpfm_start_arg {
7658 int is_start;
7659 atomic_t started;
7660 atomic_t finished;
7661 int error[NR_CPUS];
7662 };
7664 static void
7665 xenpfm_start_stop_cpu(void* info)
7667 unsigned long flags;
7668 struct xenpfm_start_arg* arg = (struct xenpfm_start_arg*)info;
7670 local_irq_save(flags);
7671 atomic_inc(&arg->started);
7672 while (!atomic_read(&arg->finished))
7673 cpu_relax();
7675 arg->error[smp_processor_id()] = __xenpfm_start_stop(arg->is_start);
7677 atomic_inc(&arg->finished);
7678 local_irq_restore(flags);
7681 static void
7682 xenpfm_start_stop_vcpu(struct vcpu* v, int is_start)
7684 struct pt_regs *regs = vcpu_regs(v);
7686 if (is_start) {
7687 /* set user level psr.pp for the caller */
7688 ia64_psr(regs)->pp = 1;
7690 /* activate monitoring at user level */
7691 ia64_psr(regs)->up = 1;
7693 /* don't allow user level control */
7694 ia64_psr(regs)->sp = 1;
7695 } else {
7696 /*
7697 * stop monitoring in the caller
7698 */
7699 ia64_psr(regs)->pp = 0;
7701 /*
7702 * stop monitoring at the user level
7703 */
7704 ia64_psr(regs)->up = 0;
7706 #if 0
7707 /*
7708 * cancel user level control
7709 */
7710 ia64_psr(regs)->sp = 0;
7711 #endif
7715 /*
7716 * This is the trickiest part.
7717 * Here we want to enable/disable wide performance monitor including
7718 * all xen context and all guest.
7719 * For interrupt context and running vcpu, set dcr.pp = 1
7720 * For blocked vcpu and idle vcpu, set psr.pp = 1 using timer via softirq.
7721 * (Here IPI doesn't work because psr doesn't preserved over interruption
7722 * when VTi domain.
7723 * If IPI is used, we need to unwind the stack to the interrupt frame
7724 * and set cr_ipsr.pp = 1. but using timer via do_softirq() is easier.)
7725 * For guest set all vcpu_regs(v)->cr_ipsr.pp = 1.
7726 */
7727 static int
7728 xenpfm_start_stop_locked(int is_start)
7730 /* avoid stack over flow. protected by xenpfm_context_lock */
7731 static struct timer xenpfm_timer[NR_CPUS];
7733 struct xenpfm_start_arg arg;
7734 int cpus = num_online_cpus();
7735 int cpu;
7736 struct domain* d;
7737 struct vcpu* v;
7738 int error = 0;
7740 arg.is_start = is_start;
7741 atomic_set(&arg.started, 1); /* 1 for this cpu */
7742 atomic_set(&arg.finished, 0);
7743 for_each_cpu(cpu)
7744 arg.error[cpu] = 0;
7746 BUG_ON(!spin_is_locked(&xenpfm_context_lock));
7747 for_each_online_cpu(cpu) {
7748 struct timer* start_stop_timer = &xenpfm_timer[cpu];
7749 if (cpu == smp_processor_id())
7750 continue;
7751 init_timer(start_stop_timer, &xenpfm_start_stop_cpu,
7752 &arg, cpu);
7753 set_timer(start_stop_timer, 0);/* fire it ASAP */
7756 while (atomic_read(&arg.started) != cpus)
7757 cpu_relax();
7759 rcu_read_lock(&domlist_read_lock);
7760 for_each_domain(d)
7761 for_each_vcpu(d, v)
7762 xenpfm_start_stop_vcpu(v, is_start);
7763 rcu_read_unlock(&domlist_read_lock);
7765 arg.error[smp_processor_id()] = __xenpfm_start_stop(is_start);
7766 atomic_inc(&arg.finished);
7768 while (atomic_read(&arg.finished) != cpus)
7769 cpu_relax();
7771 for_each_online_cpu(cpu) {
7772 if (cpu == smp_processor_id())
7773 continue;
7774 /* xenpfm_timer[] is global so that we have to wait
7775 * for xen timer subsystem to finish them. */
7776 kill_timer(&xenpfm_timer[cpu]);
7777 if (arg.error[cpu]) {
7778 gdprintk(XENLOG_INFO, "%s: cpu %d error %d\n",
7779 __func__, cpu, arg.error[cpu]);
7780 error = arg.error[cpu];
7783 return error;
7786 static int
7787 xenpfm_start_stop(int is_start)
7789 unsigned long flags;
7790 int error;
7792 BUG_ON(in_irq());
7793 local_irq_save(flags);
7794 /*
7795 * Avoid dead lock. At this moment xen has only spin locks and
7796 * doesn't have blocking mutex.
7797 */
7798 if (!spin_trylock(&xenpfm_context_lock)) {
7799 local_irq_restore(flags);
7800 gdprintk(XENLOG_DEBUG, "%s EAGAIN\n", __func__);
7801 return -EAGAIN;
7803 error = xenpfm_start_stop_locked(is_start);
7804 spin_unlock_irqrestore(&xenpfm_context_lock, flags);
7806 return error;
7809 #define NONPRIV_OP(cmd) (((cmd) == PFM_GET_FEATURES))
7811 int
7812 do_perfmon_op(unsigned long cmd,
7813 XEN_GUEST_HANDLE(void) arg1, unsigned long count)
7815 unsigned long error = 0;
7817 if (!NONPRIV_OP(cmd) && current->domain->domain_id !=0) {
7818 gdprintk(XENLOG_INFO, "xen perfmon: "
7819 "dom %d denied privileged operation %ld\n",
7820 current->domain->domain_id, cmd);
7821 return -EPERM;
7823 switch (cmd) {
7824 case PFM_GET_FEATURES:
7825 error = xenpfm_get_features(guest_handle_cast(arg1, pfarg_features_t));
7826 break;
7828 case PFM_CREATE_CONTEXT:
7829 error = xenpfm_context_create(guest_handle_cast(arg1, pfarg_context_t));
7830 break;
7831 case PFM_DESTROY_CONTEXT:
7832 error = xenpfm_context_destroy();
7833 break;
7835 case PFM_WRITE_PMCS:
7836 error = xenpfm_write_pmcs(guest_handle_cast(arg1, pfarg_reg_t), count);
7837 break;
7838 case PFM_WRITE_PMDS:
7839 error = xenpfm_write_pmds(guest_handle_cast(arg1, pfarg_reg_t), count);
7840 break;
7841 case PFM_READ_PMDS:
7842 error = -ENOSYS;
7843 break;
7844 case PFM_GET_PMC_RESET_VAL:
7845 error = -ENOSYS;
7846 break;
7848 case PFM_LOAD_CONTEXT:
7849 error = xenpfm_context_load(guest_handle_cast(arg1, pfarg_load_t));
7850 break;
7851 case PFM_UNLOAD_CONTEXT:
7852 error = xenpfm_context_unload();
7853 break;
7855 case PFM_START:
7856 error = xenpfm_start_stop(1);
7857 break;
7858 case PFM_STOP:
7859 error = xenpfm_start_stop(0);
7860 break;
7861 case PFM_RESTART:
7862 error = -ENOSYS;
7863 break;
7865 case PFM_DEBUG:
7866 error = -ENOSYS;
7867 break;
7869 case PFM_ENABLE:
7870 case PFM_DISABLE:
7871 case PFM_PROTECT_CONTEXT:
7872 case PFM_UNPROTECT_CONTEXT:
7873 default:
7874 error = -EINVAL;
7875 break;
7877 return error;
7879 #endif