summaryrefslogtreecommitdiff
path: root/xen/grant.c
blob: ae3a7bfca319f849dc8dc07f63c88f0454ad62f8 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 *  Copyright (C) 2006-2009 Free Software Foundation
 *
 * This program is free software ; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation ; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the program ; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <sys/types.h>
#include <mach/vm_param.h>
#include <machine/spl.h>
#include <vm/pmap.h>
#include <vm/vm_map.h>
#include <vm/vm_kern.h>
#include "grant.h"

#define NR_RESERVED_ENTRIES 8
#define NR_GRANT_PAGES 8

decl_simple_lock_data(static,lock);
static struct grant_entry *grants;
static vm_map_entry_t grants_map_entry;
static int last_grant = NR_RESERVED_ENTRIES;

static grant_ref_t free_grants = -1;

static grant_ref_t grant_alloc(void) {
	grant_ref_t grant;
	if (free_grants != -1) {
		grant = free_grants;
		free_grants = grants[grant].frame;
	} else {
		grant = last_grant++;
		if (grant == (NR_GRANT_PAGES * PAGE_SIZE)/sizeof(*grants))
			panic("not enough grant entries, increase NR_GRANT_PAGES");
	}
	return grant;
}

static void grant_free(grant_ref_t grant) {
	grants[grant].frame = free_grants;
	free_grants = grant;
}

static grant_ref_t grant_set(domid_t domid, unsigned long mfn, uint16_t flags) {
	spl_t spl = splhigh();
	simple_lock(&lock);

	grant_ref_t grant = grant_alloc();
	grants[grant].domid = domid;
	grants[grant].frame = mfn;
	wmb();
	grants[grant].flags = flags;

	simple_unlock(&lock);
	splx(spl);
	return grant;
}

grant_ref_t hyp_grant_give(domid_t domid, unsigned long frame, int readonly) {
	return grant_set(domid, pfn_to_mfn(frame),
		GTF_permit_access | (readonly ? GTF_readonly : 0));
}

grant_ref_t hyp_grant_accept_transfer(domid_t domid, unsigned long frame) {
	return grant_set(domid, frame, GTF_accept_transfer);
}

unsigned long hyp_grant_finish_transfer(grant_ref_t grant) {
	unsigned long frame;
	spl_t spl = splhigh();
	simple_lock(&lock);

	if (!(grants[grant].flags & GTF_transfer_committed))
		panic("grant transfer %x not committed\n", grant);
	while (!(grants[grant].flags & GTF_transfer_completed))
		machine_relax();
	rmb();
	frame = grants[grant].frame;
	grant_free(grant);

	simple_unlock(&lock);
	splx(spl);
	return frame;
}

void hyp_grant_takeback(grant_ref_t grant) {
	spl_t spl = splhigh();
	simple_lock(&lock);

	if (grants[grant].flags & (GTF_reading|GTF_writing))
		panic("grant %d still in use (%lx)\n", grant, grants[grant].flags);

	/* Note: this is not safe, a cmpxchg is needed, see grant_table.h */
	grants[grant].flags = 0;
	wmb();

	grant_free(grant);

	simple_unlock(&lock);
	splx(spl);
}

void *hyp_grant_address(grant_ref_t grant) {
	return &grants[grant];
}

void hyp_grant_init(void) {
	struct gnttab_setup_table setup;
	unsigned long frame[NR_GRANT_PAGES];
	long ret;
	int i;
	vm_offset_t addr;

	setup.dom = DOMID_SELF;
	setup.nr_frames = NR_GRANT_PAGES;
	setup.frame_list = (void*) kvtolin(frame);

	ret = hyp_grant_table_op(GNTTABOP_setup_table, kvtolin(&setup), 1);
	if (ret)
		panic("setup grant table error %d", ret);
	if (setup.status)
		panic("setup grant table: %d\n", setup.status);
	
	simple_lock_init(&lock);
	vm_map_find_entry(kernel_map, &addr, NR_GRANT_PAGES * PAGE_SIZE,
			  (vm_offset_t) 0, kernel_object, &grants_map_entry);
	grants = (void*) addr;

	for (i = 0; i < NR_GRANT_PAGES; i++)
		pmap_map_mfn((void *)grants + i * PAGE_SIZE, frame[i]);
}