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