/*
* Debug Store support
*
* This provides a low-level interface to the hardware's Debug Store
* feature that is used for branch trace store (BTS) and
* precise-event based sampling (PEBS).
*
* It manages:
* - DS and BTS hardware configuration
* - buffer overflow handling (to be done)
* - buffer access
*
* It does not do:
* - security checking (is the caller allowed to trace the task)
* - buffer allocation (memory accounting)
*
*
* Copyright (C) 2007-2009 Intel Corporation.
* Markus Metzger <markus.t.metzger@intel.com>, 2007-2009
*/
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/trace_clock.h>
#include <asm/ds.h>
#include "ds_selftest.h"
/*
* The configuration for a particular DS hardware implementation:
*/
struct ds_configuration {
/* The name of the configuration: */
const char *name;
/* The size of pointer-typed fields in DS, BTS, and PEBS: */
unsigned char sizeof_ptr_field;
/* The size of a BTS/PEBS record in bytes: */
unsigned char sizeof_rec[2];
/* The number of pebs counter reset values in the DS structure. */
unsigned char nr_counter_reset;
/* Control bit-masks indexed by enum ds_feature: */
unsigned long ctl[dsf_ctl_max];
};
static struct ds_configuration ds_cfg __read_mostly;
/* Maximal size of a DS configuration: */
#define MAX_SIZEOF_DS 0x80
/* Maximal size of a BTS record: */
#define MAX_SIZEOF_BTS (3 * 8)
/* BTS and PEBS buffer alignment: */
#define DS_ALIGNMENT (1 << 3)
/* Number of buffer pointers in DS: */
#define NUM_DS_PTR_FIELDS 8
/* Size of a pebs reset value in DS: */
#define PEBS_RESET_FIELD_SIZE 8
/* Mask of control bits in the DS MSR register: */
#define BTS_CONTROL \
( ds_cfg.ctl[dsf_bts] | \
ds_cfg.ctl[dsf_bts_kernel] | \
ds_cfg.ctl[dsf_bts_user] | \
ds_cfg.ctl[dsf_bts_overflow] )
/*
* A BTS or PEBS tracer.
*
* This holds the configuration of the tracer and serves as a handle
* to identify tracers.
*/
struct ds_tracer {
/* The DS context (partially) owned by this tracer. */
struct ds_context *context;
/* The buffer provided on ds_request() and its size in bytes. */
void *buffer;
size_t size;
};
struct bts_tracer {
/* The common DS part: */
struct ds_tracer ds;
/* The trace including the DS configuration: */
struct bts_trace trace;
/* Buffer overflow notification function: */
bts_ovfl_callback_t ovfl;
/* Active flags affecting trace collection. */
unsigned int flags;
};
struct pebs_tracer {
/* The common DS part: */
struct ds_tracer ds;
/* The trace including the DS configuration: */
struct pebs_trace trace;
/* Buffer overflow notification function: */
pebs_ovfl_callback_t ovfl;
};
/*
* Debug Store (DS) save area configuration (see Intel64 and IA32
* Architectures Software Developer's Manual, section 18.5)
*
* The DS configuration consists of the following fields; different
* architetures vary in the size of those fields.
*
* - double-word aligned base linear address of the BTS buffer
* - write pointer into the BTS buffer
* - end linear address of the BTS buffer (one byte beyond the end of
* the buffer)
* - interrupt pointer into BTS buffer
* (interrupt occurs when write pointer passes interrupt pointer)
* - double-word aligned base linear address of the PEBS buffer
* - write pointer into the PEBS buffer
* - end linear address of the PEBS buffer (one byte beyond the end of
* the buffer)
* - interrupt pointer into PEBS buffer
* (interrupt occurs when write pointer passes interrupt pointer)
* - value to which counter is reset following counter overflow
*
* Later architectures use 64bit pointers throughout, whereas earlier
* architectures use 32bit pointers in 32bit mode.
*
*
* We compute the base address for the first 8 fields based on:
* - the field size stored in the DS configuration
* - the relative field position
* - an offset giving the start of the respective region
*
* This offset is further used to index various arrays holding
* information for BTS and PEBS at the respective index.
*
* On later 32bit processors, we only access the lower 32bit of the
* 64bit pointer fields. The upper halves will be zeroed out.
*/
enum ds_field {
ds_buffer_base = 0,
ds_index,
ds_absolute_maximum,
ds_interrupt_threshold,
};
enum ds_qualifier {
ds_bts = 0,
ds_pebs
};
static inline unsigned long
ds_get(const unsigned char *base, enum ds_qualifier qual, enum ds_field field)
{
base += (ds_cfg.sizeof_ptr_field * (field + (4 * qual)));
return *(unsigned long *)base;
}
static inline void
ds_set(unsigned char *base, enum ds_qualifier qual, enum ds_field field,
unsigned long value)
{
base += (ds_cfg.sizeof_ptr_field * (field + (4 * qual)));
(*(unsigned long *)base) = value
|