blob: b2a0f15f11feaf196fb4212791b14f08289c2ad7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
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>
Matt Fleming3d90a002010-09-27 20:45:08 +010018#include <linux/init.h>
Ari Kauppid14dd7e2011-01-20 13:57:19 -050019#include <linux/errno.h>
20#include <linux/printk.h>
Arun Sharma600634972011-07-26 16:09:06 -070021#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Bob Nelson14748552007-07-20 21:39:53 +020023/* Each escaped entry is prefixed by ESCAPE_CODE
24 * then one of the following codes, then the
25 * relevant data.
26 * These #defines live in this file so that arch-specific
27 * buffer sync'ing code can access them.
28 */
29#define ESCAPE_CODE ~0UL
30#define CTX_SWITCH_CODE 1
31#define CPU_SWITCH_CODE 2
32#define COOKIE_SWITCH_CODE 3
33#define KERNEL_ENTER_SWITCH_CODE 4
34#define KERNEL_EXIT_SWITCH_CODE 5
35#define MODULE_LOADED_CODE 6
36#define CTX_TGID_CODE 7
37#define TRACE_BEGIN_CODE 8
38#define TRACE_END_CODE 9
39#define XEN_ENTER_SWITCH_CODE 10
40#define SPU_PROFILING_CODE 11
41#define SPU_CTX_SWITCH_CODE 12
Robert Richteree648bc2008-07-22 21:08:53 +020042#define IBS_FETCH_CODE 13
43#define IBS_OP_CODE 14
Bob Nelson14748552007-07-20 21:39:53 +020044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045struct dentry;
46struct file_operations;
47struct pt_regs;
48
49/* Operations structure to be filled in */
50struct oprofile_operations {
51 /* create any necessary configuration files in the oprofile fs.
52 * Optional. */
Al Viroef7bca12013-07-19 15:52:42 +040053 int (*create_files)(struct dentry * root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 /* Do any necessary interrupt setup. Optional. */
55 int (*setup)(void);
56 /* Do any necessary interrupt shutdown. Optional. */
57 void (*shutdown)(void);
58 /* Start delivering interrupts. */
59 int (*start)(void);
60 /* Stop delivering interrupts. */
61 void (*stop)(void);
Bob Nelson14748552007-07-20 21:39:53 +020062 /* Arch-specific buffer sync functions.
63 * Return value = 0: Success
64 * Return value = -1: Failure
65 * Return value = 1: Run generic sync function
66 */
67 int (*sync_start)(void);
68 int (*sync_stop)(void);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 /* Initiate a stack backtrace. Optional. */
71 void (*backtrace)(struct pt_regs * const regs, unsigned int depth);
Jason Yeh4d4036e2009-07-08 13:49:38 +020072
73 /* Multiplex between different events. Optional. */
74 int (*switch_events)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 /* CPU identification string. */
76 char * cpu_type;
77};
78
79/**
80 * One-time initialisation. *ops must be set to a filled-in
81 * operations structure. This is called even in timer interrupt
82 * mode so an arch can set a backtrace callback.
83 *
84 * If an error occurs, the fields should be left untouched.
85 */
86int oprofile_arch_init(struct oprofile_operations * ops);
87
88/**
89 * One-time exit/cleanup for the arch.
90 */
91void oprofile_arch_exit(void);
92
93/**
Robert Richter584944872008-11-26 12:02:53 +010094 * Add a sample. This may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
96void oprofile_add_sample(struct pt_regs * const regs, unsigned long event);
97
Brian Rogan27357712006-03-28 01:56:20 -080098/**
99 * Add an extended sample. Use this when the PC is not from the regs, and
100 * we cannot determine if we're in kernel mode from the regs.
101 *
102 * This function does perform a backtrace.
103 *
104 */
105void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
106 unsigned long event, int is_kernel);
107
Heinz Graalfs54ebbe72011-01-21 10:06:54 +0000108/**
109 * Add an hardware sample.
110 */
111void oprofile_add_ext_hw_sample(unsigned long pc, struct pt_regs * const regs,
112 unsigned long event, int is_kernel,
113 struct task_struct *task);
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* Use this instead when the PC value is not from the regs. Doesn't
116 * backtrace. */
117void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event);
118
119/* add a backtrace entry, to be called from the ->backtrace callback */
120void oprofile_add_trace(unsigned long eip);
121
122
123/**
124 * Create a file of the given name as a child of the given root, with
125 * the specified file operations.
126 */
Al Viro6af4ea02013-07-19 16:10:36 +0400127int oprofilefs_create_file(struct dentry * root,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800128 char const * name, const struct file_operations * fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Al Viro6af4ea02013-07-19 16:10:36 +0400130int oprofilefs_create_file_perm(struct dentry * root,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800131 char const * name, const struct file_operations * fops, int perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/** Create a file for read/write access to an unsigned long. */
Al Viro6af4ea02013-07-19 16:10:36 +0400134int oprofilefs_create_ulong(struct dentry * root,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 char const * name, ulong * val);
136
137/** Create a file for read-only access to an unsigned long. */
Al Viro6af4ea02013-07-19 16:10:36 +0400138int oprofilefs_create_ro_ulong(struct dentry * root,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 char const * name, ulong * val);
140
141/** Create a file for read-only access to an atomic_t. */
Al Viro6af4ea02013-07-19 16:10:36 +0400142int oprofilefs_create_ro_atomic(struct dentry * root,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 char const * name, atomic_t * val);
144
145/** create a directory */
Al Viroecde2822013-07-19 15:58:27 +0400146struct dentry *oprofilefs_mkdir(struct dentry *parent, char const *name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/**
149 * Write the given asciz string to the given user buffer @buf, updating *offset
150 * appropriately. Returns bytes written or -EFAULT.
151 */
152ssize_t oprofilefs_str_to_user(char const * str, char __user * buf, size_t count, loff_t * offset);
153
154/**
155 * Convert an unsigned long value into ASCII and copy it to the user buffer @buf,
156 * updating *offset appropriately. Returns bytes written or -EFAULT.
157 */
158ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user * buf, size_t count, loff_t * offset);
159
160/**
161 * Read an ASCII string for a number from a userspace buffer and fill *val on success.
162 * Returns 0 on success, < 0 on error.
163 */
164int oprofilefs_ulong_from_user(unsigned long * val, char const __user * buf, size_t count);
165
166/** lock for read/write safety */
Thomas Gleixner2d21a292009-07-25 16:18:34 +0200167extern raw_spinlock_t oprofilefs_lock;
Carl Lovea5598ca2008-10-14 23:37:01 +0000168
169/**
170 * Add the contents of a circular buffer to the event buffer.
171 */
172void oprofile_put_buff(unsigned long *buf, unsigned int start,
173 unsigned int stop, unsigned int max);
174
175unsigned long oprofile_get_cpu_buffer_size(void);
176void oprofile_cpu_buffer_inc_smpl_lost(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Robert Richter14f0ca82009-01-07 21:50:22 +0100178/* cpu buffer functions */
179
180struct op_sample;
181
182struct op_entry {
183 struct ring_buffer_event *event;
184 struct op_sample *sample;
Robert Richter14f0ca82009-01-07 21:50:22 +0100185 unsigned long size;
186 unsigned long *data;
187};
188
189void oprofile_write_reserve(struct op_entry *entry,
190 struct pt_regs * const regs,
191 unsigned long pc, int code, int size);
192int oprofile_add_data(struct op_entry *entry, unsigned long val);
Robert Richter51563a02009-06-03 20:54:56 +0200193int oprofile_add_data64(struct op_entry *entry, u64 val);
Robert Richter14f0ca82009-01-07 21:50:22 +0100194int oprofile_write_commit(struct op_entry *entry);
195
Ari Kauppi1ea1bdf2011-01-20 13:57:18 -0500196#ifdef CONFIG_HW_PERF_EVENTS
Matt Fleming3d90a002010-09-27 20:45:08 +0100197int __init oprofile_perf_init(struct oprofile_operations *ops);
Anand Gadiyarb3b3a9b2010-10-14 11:31:42 -0400198void oprofile_perf_exit(void);
Matt Fleming56946332010-10-08 21:42:17 +0100199char *op_name_from_perf_id(void);
Ari Kauppid14dd7e2011-01-20 13:57:19 -0500200#else
201static inline int __init oprofile_perf_init(struct oprofile_operations *ops)
202{
203 pr_info("oprofile: hardware counters not available\n");
204 return -ENODEV;
205}
206static inline void oprofile_perf_exit(void) { }
Ari Kauppi1ea1bdf2011-01-20 13:57:18 -0500207#endif /* CONFIG_HW_PERF_EVENTS */
Matt Fleming56946332010-10-08 21:42:17 +0100208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#endif /* OPROFILE_H */