cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

seq_buf.h (4030B)


      1/* SPDX-License-Identifier: GPL-2.0 */
      2#ifndef _LINUX_SEQ_BUF_H
      3#define _LINUX_SEQ_BUF_H
      4
      5#include <linux/fs.h>
      6
      7/*
      8 * Trace sequences are used to allow a function to call several other functions
      9 * to create a string of data to use.
     10 */
     11
     12/**
     13 * seq_buf - seq buffer structure
     14 * @buffer:	pointer to the buffer
     15 * @size:	size of the buffer
     16 * @len:	the amount of data inside the buffer
     17 * @readpos:	The next position to read in the buffer.
     18 */
     19struct seq_buf {
     20	char			*buffer;
     21	size_t			size;
     22	size_t			len;
     23	loff_t			readpos;
     24};
     25
     26static inline void seq_buf_clear(struct seq_buf *s)
     27{
     28	s->len = 0;
     29	s->readpos = 0;
     30}
     31
     32static inline void
     33seq_buf_init(struct seq_buf *s, char *buf, unsigned int size)
     34{
     35	s->buffer = buf;
     36	s->size = size;
     37	seq_buf_clear(s);
     38}
     39
     40/*
     41 * seq_buf have a buffer that might overflow. When this happens
     42 * the len and size are set to be equal.
     43 */
     44static inline bool
     45seq_buf_has_overflowed(struct seq_buf *s)
     46{
     47	return s->len > s->size;
     48}
     49
     50static inline void
     51seq_buf_set_overflow(struct seq_buf *s)
     52{
     53	s->len = s->size + 1;
     54}
     55
     56/*
     57 * How much buffer is left on the seq_buf?
     58 */
     59static inline unsigned int
     60seq_buf_buffer_left(struct seq_buf *s)
     61{
     62	if (seq_buf_has_overflowed(s))
     63		return 0;
     64
     65	return s->size - s->len;
     66}
     67
     68/* How much buffer was written? */
     69static inline unsigned int seq_buf_used(struct seq_buf *s)
     70{
     71	return min(s->len, s->size);
     72}
     73
     74/**
     75 * seq_buf_terminate - Make sure buffer is nul terminated
     76 * @s: the seq_buf descriptor to terminate.
     77 *
     78 * This makes sure that the buffer in @s is nul terminated and
     79 * safe to read as a string.
     80 *
     81 * Note, if this is called when the buffer has overflowed, then
     82 * the last byte of the buffer is zeroed, and the len will still
     83 * point passed it.
     84 *
     85 * After this function is called, s->buffer is safe to use
     86 * in string operations.
     87 */
     88static inline void seq_buf_terminate(struct seq_buf *s)
     89{
     90	if (WARN_ON(s->size == 0))
     91		return;
     92
     93	if (seq_buf_buffer_left(s))
     94		s->buffer[s->len] = 0;
     95	else
     96		s->buffer[s->size - 1] = 0;
     97}
     98
     99/**
    100 * seq_buf_get_buf - get buffer to write arbitrary data to
    101 * @s: the seq_buf handle
    102 * @bufp: the beginning of the buffer is stored here
    103 *
    104 * Return the number of bytes available in the buffer, or zero if
    105 * there's no space.
    106 */
    107static inline size_t seq_buf_get_buf(struct seq_buf *s, char **bufp)
    108{
    109	WARN_ON(s->len > s->size + 1);
    110
    111	if (s->len < s->size) {
    112		*bufp = s->buffer + s->len;
    113		return s->size - s->len;
    114	}
    115
    116	*bufp = NULL;
    117	return 0;
    118}
    119
    120/**
    121 * seq_buf_commit - commit data to the buffer
    122 * @s: the seq_buf handle
    123 * @num: the number of bytes to commit
    124 *
    125 * Commit @num bytes of data written to a buffer previously acquired
    126 * by seq_buf_get.  To signal an error condition, or that the data
    127 * didn't fit in the available space, pass a negative @num value.
    128 */
    129static inline void seq_buf_commit(struct seq_buf *s, int num)
    130{
    131	if (num < 0) {
    132		seq_buf_set_overflow(s);
    133	} else {
    134		/* num must be negative on overflow */
    135		BUG_ON(s->len + num > s->size);
    136		s->len += num;
    137	}
    138}
    139
    140extern __printf(2, 3)
    141int seq_buf_printf(struct seq_buf *s, const char *fmt, ...);
    142extern __printf(2, 0)
    143int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args);
    144extern int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s);
    145extern int seq_buf_to_user(struct seq_buf *s, char __user *ubuf,
    146			   int cnt);
    147extern int seq_buf_puts(struct seq_buf *s, const char *str);
    148extern int seq_buf_putc(struct seq_buf *s, unsigned char c);
    149extern int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len);
    150extern int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
    151			      unsigned int len);
    152extern int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc);
    153extern int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str,
    154			    int prefix_type, int rowsize, int groupsize,
    155			    const void *buf, size_t len, bool ascii);
    156
    157#ifdef CONFIG_BINARY_PRINTF
    158extern int
    159seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary);
    160#endif
    161
    162#endif /* _LINUX_SEQ_BUF_H */