blob: a4c562453f6b726e0abe6f2b62c66f34a3a07946 [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 Sharma60063492011-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 super_block;
46struct dentry;
47struct file_operations;
48struct pt_regs;
49
50/* Operations structure to be filled in */
51struct oprofile_operations {
52 /* create any necessary configuration files in the oprofile fs.
53 * Optional. */
54 int (*create_files)(struct super_block * sb, struct dentry * root);
55 /* Do any necessary interrupt setup. Optional. */
56 int (*setup)(void);
57 /* Do any necessary interrupt shutdown. Optional. */
58 void (*shutdown)(void);
59 /* Start delivering interrupts. */
60 int (*start)(void);
61 /* Stop delivering interrupts. */
62 void (*stop)(void);
Bob Nelson14748552007-07-20 21:39:53 +020063 /* Arch-specific buffer sync functions.
64 * Return value = 0: Success
65 * Return value = -1: Failure
66 * Return value = 1: Run generic sync function
67 */
68 int (*sync_start)(void);
69 int (*sync_stop)(void);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 /* Initiate a stack backtrace. Optional. */
72 void (*backtrace)(struct pt_regs * const regs, unsigned int depth);
Jason Yeh4d4036e2009-07-08 13:49:38 +020073
74 /* Multiplex between different events. Optional. */
75 int (*switch_events)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 /* CPU identification string. */
77 char * cpu_type;
78};
79
80/**
81 * One-time initialisation. *ops must be set to a filled-in
82 * operations structure. This is called even in timer interrupt
83 * mode so an arch can set a backtrace callback.
84 *
85 * If an error occurs, the fields should be left untouched.
86 */
87int oprofile_arch_init(struct oprofile_operations * ops);
88
89/**
90 * One-time exit/cleanup for the arch.
91 */
92void oprofile_arch_exit(void);
93
94/**
Robert Richter58494482008-11-26 12:02:53 +010095 * Add a sample. This may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
97void oprofile_add_sample(struct pt_regs * const regs, unsigned long event);
98
Brian Rogan27357712006-03-28 01:56:20 -080099/**
100 * Add an extended sample. Use this when the PC is not from the regs, and
101 * we cannot determine if we're in kernel mode from the regs.
102 *
103 * This function does perform a backtrace.
104 *
105 */
106void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
107 unsigned long event, int is_kernel);
108
Heinz Graalfs54ebbe72011-01-21 10:06:54 +0000109/**
110 * Add an hardware sample.
111 */
112void oprofile_add_ext_hw_sample(unsigned long pc, struct pt_regs * const regs,
113 unsigned long event, int is_kernel,
114 struct task_struct *task);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/* Use this instead when the PC value is not from the regs. Doesn't
117 * backtrace. */
118void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event);
119
120/* add a backtrace entry, to be called from the ->backtrace callback */
121void oprofile_add_trace(unsigned long eip);
122
123
124/**
125 * Create a file of the given name as a child of the given root, with
126 * the specified file operations.
127 */
128int oprofilefs_create_file(struct super_block * sb, struct dentry * root,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800129 char const * name, const struct file_operations * fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131int oprofilefs_create_file_perm(struct super_block * sb, struct dentry * root,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800132 char const * name, const struct file_operations * fops, int perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/** Create a file for read/write access to an unsigned long. */
135int oprofilefs_create_ulong(struct super_block * sb, struct dentry * root,
136 char const * name, ulong * val);
137
138/** Create a file for read-only access to an unsigned long. */
139int oprofilefs_create_ro_ulong(struct super_block * sb, struct dentry * root,
140 char const * name, ulong * val);
141
142/** Create a file for read-only access to an atomic_t. */
143int oprofilefs_create_ro_atomic(struct super_block * sb, struct dentry * root,
144 char const * name, atomic_t * val);
145
146/** create a directory */
147struct dentry * oprofilefs_mkdir(struct super_block * sb, struct dentry * root,
148 char const * name);
149
150/**
151 * Write the given asciz string to the given user buffer @buf, updating *offset
152 * appropriately. Returns bytes written or -EFAULT.
153 */
154ssize_t oprofilefs_str_to_user(char const * str, char __user * buf, size_t count, loff_t * offset);
155
156/**
157 * Convert an unsigned long value into ASCII and copy it to the user buffer @buf,
158 * updating *offset appropriately. Returns bytes written or -EFAULT.
159 */
160ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user * buf, size_t count, loff_t * offset);
161
162/**
163 * Read an ASCII string for a number from a userspace buffer and fill *val on success.
164 * Returns 0 on success, < 0 on error.
165 */
166int oprofilefs_ulong_from_user(unsigned long * val, char const __user * buf, size_t count);
167
168/** lock for read/write safety */
Thomas Gleixner2d21a292009-07-25 16:18:34 +0200169extern raw_spinlock_t oprofilefs_lock;
Carl Lovea5598ca2008-10-14 23:37:01 +0000170
171/**
172 * Add the contents of a circular buffer to the event buffer.
173 */
174void oprofile_put_buff(unsigned long *buf, unsigned int start,
175 unsigned int stop, unsigned int max);
176
177unsigned long oprofile_get_cpu_buffer_size(void);
178void oprofile_cpu_buffer_inc_smpl_lost(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Robert Richter14f0ca82009-01-07 21:50:22 +0100180/* cpu buffer functions */
181
182struct op_sample;
183
184struct op_entry {
185 struct ring_buffer_event *event;
186 struct op_sample *sample;
Robert Richter14f0ca82009-01-07 21:50:22 +0100187 unsigned long size;
188 unsigned long *data;
189};
190
191void oprofile_write_reserve(struct op_entry *entry,
192 struct pt_regs * const regs,
193 unsigned long pc, int code, int size);
194int oprofile_add_data(struct op_entry *entry, unsigned long val);
Robert Richter51563a02009-06-03 20:54:56 +0200195int oprofile_add_data64(struct op_entry *entry, u64 val);
Robert Richter14f0ca82009-01-07 21:50:22 +0100196int oprofile_write_commit(struct op_entry *entry);
197
Ari Kauppi1ea1bdf2011-01-20 13:57:18 -0500198#ifdef CONFIG_HW_PERF_EVENTS
Matt Fleming3d90a002010-09-27 20:45:08 +0100199int __init oprofile_perf_init(struct oprofile_operations *ops);
Anand Gadiyarb3b3a9b2010-10-14 11:31:42 -0400200void oprofile_perf_exit(void);
Matt Fleming56946332010-10-08 21:42:17 +0100201char *op_name_from_perf_id(void);
Ari Kauppid14dd7e2011-01-20 13:57:19 -0500202#else
203static inline int __init oprofile_perf_init(struct oprofile_operations *ops)
204{
205 pr_info("oprofile: hardware counters not available\n");
206 return -ENODEV;
207}
208static inline void oprofile_perf_exit(void) { }
Ari Kauppi1ea1bdf2011-01-20 13:57:18 -0500209#endif /* CONFIG_HW_PERF_EVENTS */
Matt Fleming56946332010-10-08 21:42:17 +0100210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#endif /* OPROFILE_H */