libcap_types.h 2.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/* 
 * types.h
 *
 * Author: Charles Jacobsen <charlesj@cs.utah.edu>
 * Copyright: University of Utah
 *
 */
#ifndef __LIBCAP_TYPES_H__
#define __LIBCAP_TYPES_H__

/* CPTRs -------------------------------------------------- */

typedef struct {
	unsigned long cptr;
} cptr_t;

static inline cptr_t __cptr(unsigned long cptr)
{
	return (cptr_t) {cptr};
}

static inline unsigned long cptr_val(cptr_t c)
{
	return c.cptr;
}

/*
 * Reserved cnodes:
 *
 * cptr = 0 is always null
 */
#define CAP_CPTR_NULL __cptr(0)

static inline int cptr_is_null(cptr_t c)
{
	return cptr_val(c) == cptr_val(CAP_CPTR_NULL);
}

#define CAP_CPTR_DEPTH_BITS  3	/* max depth of 3, zero indexed         */
#define CAP_CPTR_FANOUT_BITS 3	/* each level fans out by a factor of 4 */
#define CAP_CPTR_SLOT_BITS   3	/* each node contains 4 cap slots       */
#define CAP_CNODE_TABLE_NUM_SLOTS ((1 << CAP_CPTR_SLOT_BITS) + \
					(1 << CAP_CPTR_FANOUT_BITS))
#define CAP_CPTR_LEVEL_SHIFT (((1 << CAP_CPTR_DEPTH_BITS) - 1) * \
				CAP_CPTR_FANOUT_BITS + CAP_CPTR_SLOT_BITS)

static inline unsigned long cap_cptr_slot(cptr_t c)
{
	/*
	 * Mask off low bits
	 */
	return cptr_val(c) & ((1 << CAP_CPTR_SLOT_BITS) - 1);
}

/* 
 * Gives fanout index for going *from* lvl to lvl + 1, where 
 * 0 <= lvl < 2^CAP_CPTR_DEPTH_BITS - 1 (i.e., we can't go anywhere
 * if lvl = 2^CAP_CPTR_DEPTH_BITS - 1, because we are at the deepest
 * level).
 */
static inline unsigned long cap_cptr_fanout(cptr_t c, int lvl)
{
	unsigned long i;

	i = cptr_val(c);
	/*
	 * Shift and mask off bits at correct section
	 */
	i >>= (lvl * CAP_CPTR_FANOUT_BITS + CAP_CPTR_SLOT_BITS);
	i &= ((1 << CAP_CPTR_FANOUT_BITS) - 1);

	return i;
}

/*
 * Gives depth/level of cptr, zero indexed (0 means the root cnode table)
 */
static inline unsigned long cap_cptr_level(cptr_t c)
{
	unsigned long i;

	i = cptr_val(c);
	/*
	 * Shift and mask
	 */
	i >>= CAP_CPTR_LEVEL_SHIFT;
	i &= ((1 << CAP_CPTR_DEPTH_BITS) - 1);

	return i;
}

/* CPTR CACHE -------------------------------------------------- */

struct cptr_cache {
	unsigned long *bmaps[1 << CAP_CPTR_DEPTH_BITS];
};

#endif				/* __LIBCAP_TYPES_H__ */