blob: 5171639ecf0ff070e74d884deb8c969e3b88132d [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>
18#include <asm/atomic.h>
19
Bob Nelson14748552007-07-20 21:39:53 +020020/* Each escaped entry is prefixed by ESCAPE_CODE
21 * then one of the following codes, then the
22 * relevant data.
23 * These #defines live in this file so that arch-specific
24 * buffer sync'ing code can access them.
25 */
26#define ESCAPE_CODE ~0UL
27#define CTX_SWITCH_CODE 1
28#define CPU_SWITCH_CODE 2
29#define COOKIE_SWITCH_CODE 3
30#define KERNEL_ENTER_SWITCH_CODE 4
31#define KERNEL_EXIT_SWITCH_CODE 5
32#define MODULE_LOADED_CODE 6
33#define CTX_TGID_CODE 7
34#define TRACE_BEGIN_CODE 8
35#define TRACE_END_CODE 9
36#define XEN_ENTER_SWITCH_CODE 10
37#define SPU_PROFILING_CODE 11
38#define SPU_CTX_SWITCH_CODE 12
Robert Richteree648bc2008-07-22 21:08:53 +020039#define IBS_FETCH_CODE 13
40#define IBS_OP_CODE 14
Bob Nelson14748552007-07-20 21:39:53 +020041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042struct super_block;
43struct dentry;
44struct file_operations;
45struct pt_regs;
46
47/* Operations structure to be filled in */
48struct oprofile_operations {
49 /* create any necessary configuration files in the oprofile fs.
50 * Optional. */
51 int (*create_files)(struct super_block * sb, struct dentry * root);
52 /* Do any necessary interrupt setup. Optional. */
53 int (*setup)(void);
54 /* Do any necessary interrupt shutdown. Optional. */
55 void (*shutdown)(void);
56 /* Start delivering interrupts. */
57 int (*start)(void);
58 /* Stop delivering interrupts. */
59 void (*stop)(void);
Bob Nelson14748552007-07-20 21:39:53 +020060 /* Arch-specific buffer sync functions.
61 * Return value = 0: Success
62 * Return value = -1: Failure
63 * Return value = 1: Run generic sync function
64 */
65 int (*sync_start)(void);
66 int (*sync_stop)(void);
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 /* Initiate a stack backtrace. Optional. */
69 void (*backtrace)(struct pt_regs * const regs, unsigned int depth);
Jason Yeh4d4036e2009-07-08 13:49:38 +020070
71 /* Multiplex between different events. Optional. */
72 int (*switch_events)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 /* CPU identification string. */
74 char * cpu_type;
75};
76
77/**
78 * One-time initialisation. *ops must be set to a filled-in
79 * operations structure. This is called even in timer interrupt
80 * mode so an arch can set a backtrace callback.
81 *
82 * If an error occurs, the fields should be left untouched.
83 */
84int oprofile_arch_init(struct oprofile_operations * ops);
85
86/**
87 * One-time exit/cleanup for the arch.
88 */
89void oprofile_arch_exit(void);
90
91/**
Robert Richter58494482008-11-26 12:02:53 +010092 * Add a sample. This may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
94void oprofile_add_sample(struct pt_regs * const regs, unsigned long event);
95
Brian Rogan27357712006-03-28 01:56:20 -080096/**
97 * Add an extended sample. Use this when the PC is not from the regs, and
98 * we cannot determine if we're in kernel mode from the regs.
99 *
100 * This function does perform a backtrace.
101 *
102 */
103void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
104 unsigned long event, int is_kernel);
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* Use this instead when the PC value is not from the regs. Doesn't
107 * backtrace. */
108void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event);
109
110/* add a backtrace entry, to be called from the ->backtrace callback */
111void oprofile_add_trace(unsigned long eip);
112
113
114/**
115 * Create a file of the given name as a child of the given root, with
116 * the specified file operations.
117 */
118int oprofilefs_create_file(struct super_block * sb, struct dentry * root,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800119 char const * name, const struct file_operations * fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121int oprofilefs_create_file_perm(struct super_block * sb, struct dentry * root,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800122 char const * name, const struct file_operations * fops, int perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124/** Create a file for read/write access to an unsigned long. */
125int oprofilefs_create_ulong(struct super_block * sb, struct dentry * root,
126 char const * name, ulong * val);
127
128/** Create a file for read-only access to an unsigned long. */
129int oprofilefs_create_ro_ulong(struct super_block * sb, struct dentry * root,
130 char const * name, ulong * val);
131
132/** Create a file for read-only access to an atomic_t. */
133int oprofilefs_create_ro_atomic(struct super_block * sb, struct dentry * root,
134 char const * name, atomic_t * val);
135
136/** create a directory */
137struct dentry * oprofilefs_mkdir(struct super_block * sb, struct dentry * root,
138 char const * name);
139
140/**
141 * Write the given asciz string to the given user buffer @buf, updating *offset
142 * appropriately. Returns bytes written or -EFAULT.
143 */
144ssize_t oprofilefs_str_to_user(char const * str, char __user * buf, size_t count, loff_t * offset);
145
146/**
147 * Convert an unsigned long value into ASCII and copy it to the user buffer @buf,
148 * updating *offset appropriately. Returns bytes written or -EFAULT.
149 */
150ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user * buf, size_t count, loff_t * offset);
151
152/**
153 * Read an ASCII string for a number from a userspace buffer and fill *val on success.
154 * Returns 0 on success, < 0 on error.
155 */
156int oprofilefs_ulong_from_user(unsigned long * val, char const __user * buf, size_t count);
157
158/** lock for read/write safety */
159extern spinlock_t oprofilefs_lock;
Carl Lovea5598ca2008-10-14 23:37:01 +0000160
161/**
162 * Add the contents of a circular buffer to the event buffer.
163 */
164void oprofile_put_buff(unsigned long *buf, unsigned int start,
165 unsigned int stop, unsigned int max);
166
167unsigned long oprofile_get_cpu_buffer_size(void);
168void oprofile_cpu_buffer_inc_smpl_lost(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Robert Richter14f0ca82009-01-07 21:50:22 +0100170/* cpu buffer functions */
171
172struct op_sample;
173
174struct op_entry {
175 struct ring_buffer_event *event;
176 struct op_sample *sample;
Robert Richter14f0ca82009-01-07 21:50:22 +0100177 unsigned long size;
178 unsigned long *data;
179};
180
181void oprofile_write_reserve(struct op_entry *entry,
182 struct pt_regs * const regs,
183 unsigned long pc, int code, int size);
184int oprofile_add_data(struct op_entry *entry, unsigned long val);
Robert Richter51563a02009-06-03 20:54:56 +0200185int oprofile_add_data64(struct op_entry *entry, u64 val);
Robert Richter14f0ca82009-01-07 21:50:22 +0100186int oprofile_write_commit(struct op_entry *entry);
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188#endif /* OPROFILE_H */