- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / include / linux / oprofile.h
1 /**
2  * @file oprofile.h
3  *
4  * API for machine-specific interrupts to interface
5  * to oprofile.
6  *
7  * @remark Copyright 2002 OProfile authors
8  * @remark Read the file COPYING
9  *
10  * @author John Levon <levon@movementarian.org>
11  */
12
13 #ifndef OPROFILE_H
14 #define OPROFILE_H
15
16 #include <linux/types.h>
17 #include <linux/spinlock.h>
18 #include <linux/init.h>
19 #include <linux/errno.h>
20 #include <linux/printk.h>
21 #include <linux/atomic.h>
22 #ifdef CONFIG_XEN
23 #include <xen/interface/xenoprof.h>
24 #endif
25  
26 /* Each escaped entry is prefixed by ESCAPE_CODE
27  * then one of the following codes, then the
28  * relevant data.
29  * These #defines live in this file so that arch-specific
30  * buffer sync'ing code can access them.
31  */
32 #define ESCAPE_CODE                     ~0UL
33 #define CTX_SWITCH_CODE                 1
34 #define CPU_SWITCH_CODE                 2
35 #define COOKIE_SWITCH_CODE              3
36 #define KERNEL_ENTER_SWITCH_CODE        4
37 #define USER_ENTER_SWITCH_CODE          5
38 #define MODULE_LOADED_CODE              6
39 #define CTX_TGID_CODE                   7
40 #define TRACE_BEGIN_CODE                8
41 #define TRACE_END_CODE                  9
42 #define XEN_ENTER_SWITCH_CODE           10
43 #ifndef CONFIG_XEN
44 #define SPU_PROFILING_CODE              11
45 #define SPU_CTX_SWITCH_CODE             12
46 #else
47 #define DOMAIN_SWITCH_CODE              11
48 #endif
49 #define IBS_FETCH_CODE                  13
50 #define IBS_OP_CODE                     14
51
52 struct super_block;
53 struct dentry;
54 struct file_operations;
55 struct pt_regs;
56  
57 /* Operations structure to be filled in */
58 struct oprofile_operations {
59         /* create any necessary configuration files in the oprofile fs.
60          * Optional. */
61         int (*create_files)(struct super_block * sb, struct dentry * root);
62 #ifdef CONFIG_XEN
63         /* setup active domains with Xen */
64         int (*set_active)(int *active_domains, unsigned int adomains);
65         /* setup passive domains with Xen */
66         int (*set_passive)(int *passive_domains, unsigned int pdomains);
67 #endif
68         /* Do any necessary interrupt setup. Optional. */
69         int (*setup)(void);
70         /* Do any necessary interrupt shutdown. Optional. */
71         void (*shutdown)(void);
72         /* Start delivering interrupts. */
73         int (*start)(void);
74         /* Stop delivering interrupts. */
75         void (*stop)(void);
76         /* Arch-specific buffer sync functions.
77          * Return value = 0:  Success
78          * Return value = -1: Failure
79          * Return value = 1:  Run generic sync function
80          */
81         int (*sync_start)(void);
82         int (*sync_stop)(void);
83
84         /* Initiate a stack backtrace. Optional. */
85         void (*backtrace)(struct pt_regs * const regs, unsigned int depth);
86
87         /* Multiplex between different events. Optional. */
88         int (*switch_events)(void);
89         /* CPU identification string. */
90         char * cpu_type;
91 };
92
93 /**
94  * One-time initialisation. *ops must be set to a filled-in
95  * operations structure. This is called even in timer interrupt
96  * mode so an arch can set a backtrace callback.
97  *
98  * If an error occurs, the fields should be left untouched.
99  */
100 int oprofile_arch_init(struct oprofile_operations * ops);
101  
102 /**
103  * One-time exit/cleanup for the arch.
104  */
105 void oprofile_arch_exit(void);
106
107 /**
108  * Add a sample. This may be called from any context.
109  */
110 void oprofile_add_sample(struct pt_regs * const regs, unsigned long event);
111
112 /**
113  * Add an extended sample.  Use this when the PC is not from the regs, and
114  * we cannot determine if we're in kernel mode from the regs.
115  *
116  * This function does perform a backtrace.
117  *
118  */
119 void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
120                                 unsigned long event, int is_kernel);
121
122 /**
123  * Add an hardware sample.
124  */
125 void oprofile_add_ext_hw_sample(unsigned long pc, struct pt_regs * const regs,
126         unsigned long event, int is_kernel,
127         struct task_struct *task);
128
129 /* Use this instead when the PC value is not from the regs. Doesn't
130  * backtrace. */
131 void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event);
132
133 void oprofile_add_mode(int cpu_mode);
134
135 /* add a backtrace entry, to be called from the ->backtrace callback */
136 void oprofile_add_trace(unsigned long eip);
137
138 /* add a domain switch entry */
139 int oprofile_add_domain_switch(int32_t domain_id);
140
141
142 /**
143  * Create a file of the given name as a child of the given root, with
144  * the specified file operations.
145  */
146 int oprofilefs_create_file(struct super_block * sb, struct dentry * root,
147         char const * name, const struct file_operations * fops);
148
149 int oprofilefs_create_file_perm(struct super_block * sb, struct dentry * root,
150         char const * name, const struct file_operations * fops, int perm);
151  
152 /** Create a file for read/write access to an unsigned long. */
153 int oprofilefs_create_ulong(struct super_block * sb, struct dentry * root,
154         char const * name, ulong * val);
155  
156 /** Create a file for read-only access to an unsigned long. */
157 int oprofilefs_create_ro_ulong(struct super_block * sb, struct dentry * root,
158         char const * name, ulong * val);
159  
160 /** Create a file for read-only access to an atomic_t. */
161 int oprofilefs_create_ro_atomic(struct super_block * sb, struct dentry * root,
162         char const * name, atomic_t * val);
163  
164 /** create a directory */
165 struct dentry * oprofilefs_mkdir(struct super_block * sb, struct dentry * root,
166         char const * name);
167
168 /**
169  * Write the given asciz string to the given user buffer @buf, updating *offset
170  * appropriately. Returns bytes written or -EFAULT.
171  */
172 ssize_t oprofilefs_str_to_user(char const * str, char __user * buf, size_t count, loff_t * offset);
173
174 /**
175  * Convert an unsigned long value into ASCII and copy it to the user buffer @buf,
176  * updating *offset appropriately. Returns bytes written or -EFAULT.
177  */
178 ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user * buf, size_t count, loff_t * offset);
179
180 /**
181  * Read an ASCII string for a number from a userspace buffer and fill *val on success.
182  * Returns 0 on success, < 0 on error.
183  */
184 int oprofilefs_ulong_from_user(unsigned long * val, char const __user * buf, size_t count);
185
186 /** lock for read/write safety */
187 extern raw_spinlock_t oprofilefs_lock;
188
189 /**
190  * Add the contents of a circular buffer to the event buffer.
191  */
192 void oprofile_put_buff(unsigned long *buf, unsigned int start,
193                         unsigned int stop, unsigned int max);
194
195 unsigned long oprofile_get_cpu_buffer_size(void);
196 void oprofile_cpu_buffer_inc_smpl_lost(void);
197  
198 /* cpu buffer functions */
199
200 struct op_sample;
201
202 struct op_entry {
203         struct ring_buffer_event *event;
204         struct op_sample *sample;
205         unsigned long size;
206         unsigned long *data;
207 };
208
209 void oprofile_write_reserve(struct op_entry *entry,
210                             struct pt_regs * const regs,
211                             unsigned long pc, int code, int size);
212 int oprofile_add_data(struct op_entry *entry, unsigned long val);
213 int oprofile_add_data64(struct op_entry *entry, u64 val);
214 int oprofile_write_commit(struct op_entry *entry);
215
216 #ifdef CONFIG_HW_PERF_EVENTS
217 int __init oprofile_perf_init(struct oprofile_operations *ops);
218 void oprofile_perf_exit(void);
219 char *op_name_from_perf_id(void);
220 #else
221 static inline int __init oprofile_perf_init(struct oprofile_operations *ops)
222 {
223         pr_info("oprofile: hardware counters not available\n");
224         return -ENODEV;
225 }
226 static inline void oprofile_perf_exit(void) { }
227 #endif /* CONFIG_HW_PERF_EVENTS */
228
229 #endif /* OPROFILE_H */