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