// SPDX-License-Identifier: LGPL-2.1
/*
* Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "kbuffer.h"
#define MISSING_EVENTS (1UL << 31)
#define MISSING_STORED (1UL << 30)
#define COMMIT_MASK ((1 << 27) - 1)
enum {
KBUFFER_FL_HOST_BIG_ENDIAN = (1<<0),
KBUFFER_FL_BIG_ENDIAN = (1<<1),
KBUFFER_FL_LONG_8 = (1<<2),
KBUFFER_FL_OLD_FORMAT = (1<<3),
};
#define ENDIAN_MASK (KBUFFER_FL_HOST_BIG_ENDIAN | KBUFFER_FL_BIG_ENDIAN)
/** kbuffer
* @timestamp - timestamp of current event
* @lost_events - # of lost events between this subbuffer and previous
* @flags - special flags of the kbuffer
* @subbuffer - pointer to the sub-buffer page
* @data - pointer to the start of data on the sub-buffer page
* @index - index from @data to the @curr event data
* @curr - offset from @data to the start of current event
* (includes metadata)
* @next - offset from @data to the start of next event
* @size - The size of data on @data
* @start - The offset from @subbuffer where @data lives
*
* @read_4 - Function to read 4 raw bytes (may swap)
* @read_8 - Function to read 8 raw bytes (may swap)
* @read_long - Function to read a long word (4 or 8 bytes with needed swap)
*/
struct kbuffer {
unsigned long long timestamp;
long long lost_events;
unsigned long flags;
void *subbuffer;
void *data;
unsigned int index;
unsigned int curr;
unsigned int next;
unsigned int size;
unsigned int start;
unsigned int (*read_4)(void *ptr);
unsigned long long (*read_8)(void *ptr);
unsigned long long (*read_long)(struct kbuffer *kbuf, void *ptr);
int (*next_event)(struct kbuffer *kbuf);
};
static void *zmalloc(size_t size)
{
return calloc(1, size);
}
static int host_is_bigendian(void)
{
unsigned char str[] = { 0x1, 0x2, 0x3, 0x4 };
unsigned int *ptr;
ptr = (unsigned int *)str;
return *ptr == 0x01020304;
}
static int do_swap(struct kbuffer *kbuf)
{
return ((kbuf->flags & KBUFFER_FL_HOST_BIG_ENDIAN) + kbuf->flags) &
ENDIAN_MASK;
}
static unsigned long long __read_8(void *ptr)
{
unsigned long long data = *(unsigned long long *)ptr;
return data;
}
static unsigned long long __read_8_sw(void *ptr)
{
unsigned long long data = *(unsigned long long *)ptr;
unsigned long long swap;
swap = ((data & 0xffULL) << 56) |
((data & (0xffULL << 8)) << 40) |
((data & (0xffULL << 16)) << 24) |
((data & (0xffULL << 24)) << 8) |
((data & (0xffULL << 32)) >> 8) |
((data & (0xffULL << 40)) >> 24) |
((data & (0xffULL << 48)) >> 40) |
((data & (0xffULL << 56)) >> 56);
return swap;
}
static unsigned int __read_4(void *ptr)
{
unsigned int data = *(unsigned int *)ptr;
return data;
}
static unsigned int __read_4_sw(void *ptr)
{
unsigned int data = *(unsigned int *)ptr;
unsigned int swap;
swap = ((data & 0xffULL) << 24) |
((data & (0xffULL << 8)) << 8) |
((data & (0xffULL << 16)) >> 8) |
((data & (0xffULL << 24)) >> 24);
return swap;
}
static unsigned long long read_8(struct kbuffer *kbuf, void *ptr)
{
return kbuf->read_8(ptr);
}
static unsigned int read_4(struct kbuffer *kbuf, void *ptr)
{
return kbuf->read_4(ptr);
}
static unsigned long long __read_long_8(struct kbuffer *kbuf, void *ptr)
{
return kbuf->read_8(ptr);
}
static unsigned long long __read_long_4(struct kbuffer *kbuf, void *ptr)
{
return kbuf->read_4(ptr);
}
static unsigned long long read_long(struct kbuffer *kbuf, void *ptr)
{
return kbuf->read_long(kbuf, ptr);
}
static int calc_index(struct kbuffer *kbuf, void *ptr)
{
return (unsigned long)ptr - (unsigned long)kbuf->data;
}
static int __next_event(struct kbuffer *kbuf);
/**
* kbuffer_alloc - allocat a new kbuffer
* @size; enum to denote size of word
* @endian: enum to denote endianness
*
* Allocates and returns a new kbuffer.
*/
struct kbuffer *
kbuffer_alloc(enum kbuffer_long_size size, enum kbuffer_endian endian)
{