cap.c 1.56 KB
Newer Older
Charlie Jacobsen's avatar
Charlie Jacobsen committed
1 2 3 4 5 6 7 8
/* 
 * Author: Anton Burtsev <aburtsev@flux.utah.edu>
 * Copyright: University of Utah
 */
#include <linux/slab.h>
#include "defs.h"
#include "../include/common.h"

Charlie Jacobsen's avatar
Charlie Jacobsen committed
9
int lcd_mk_cspace(struct cspace **cspace_ptr)
Charlie Jacobsen's avatar
Charlie Jacobsen committed
10 11 12 13 14 15 16 17
{
	struct cspace *cspace;
	int i;
	/*
	 * Allocate cspace
	 */
	cspace = kmalloc(sizeof(*cspace), GFP_KERNEL);
	if (!cspace)
Charlie Jacobsen's avatar
Charlie Jacobsen committed
18 19 20
		return -ENOMEM;
	/* 
	 * Sets cnode types to unformed, and free_slot to 0
Charlie Jacobsen's avatar
Charlie Jacobsen committed
21
	 */
Charlie Jacobsen's avatar
Charlie Jacobsen committed
22
	memset(cspace, 0, sizeof(*cspace));
Charlie Jacobsen's avatar
Charlie Jacobsen committed
23 24 25 26
	/*
	 * Initialize lock
	 */
	mutex_init(&cspace->lock);
Charlie Jacobsen's avatar
Charlie Jacobsen committed
27 28 29 30 31
	/*
	 * Done
	 */
	*cspace_ptr = cspace;
	return 0;
Charlie Jacobsen's avatar
Charlie Jacobsen committed
32 33 34 35 36
}

int lcd_cap_insert(struct cspace *cspace, cptr_t cptr, void *object,
		enum lcd_cap_type type)
{
Charlie Jacobsen's avatar
Charlie Jacobsen committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	struct cnode *cnode = NULL;
	int ret;
	/*
	 * Lookup cnode
	 */
	ret = lcd_lock_cspace(cspace);
	if (!ret) {
		cnode = __lcd_cnode_lookup(cspace, cptr);
		lcd_unlock_cspace(cspace);
	} else {
		return ret;
	}
	/*
	 * Set vals
	 */
	if (cnode && cnode->type == LCD_CAP_TYPE_UNFORMED) {
		cnode->object = object;
		cnode->type = type;
		return 0;
	} else {
		return -EINVAL;
	}
Charlie Jacobsen's avatar
Charlie Jacobsen committed
59 60
}

Charlie Jacobsen's avatar
Charlie Jacobsen committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
int lcd_cap_grant(struct cspace *src_cspace, struct cspace *dest_cspace,
		cptr_t src_cptr, cptr_t dest_cptr)
{
	struct cnode *src_cnode;
	int ret;
	/*
	 * Get source capability data
	 */
	ret = lcd_cnode_lookup(src_cspace, src_cptr, &src_cnode);
	if (ret)
		return ret;
	if (!src_cnode)
		return -EINVAL;
	if (!__lcd_cap_can_grant(src_cnode))
		return -EINVAL;
	/*
	 * Insert into destination
	 */
	return lcd_cap_insert(dest_cspace, dest_cptr, src_cnode->object,
			src_cnode->type);
}
Charlie Jacobsen's avatar
Charlie Jacobsen committed
82

Charlie Jacobsen's avatar
Charlie Jacobsen committed
83 84 85 86
void lcd_rm_cspace(struct cspace *cspace)
{
	kfree(cspace);
}