forked from sysml/mini-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnttab.c
256 lines (215 loc) · 6.47 KB
/
gnttab.c
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
****************************************************************************
* (C) 2006 - Cambridge University
****************************************************************************
*
* File: gnttab.c
* Author: Steven Smith ([email protected])
* Changes: Grzegorz Milos ([email protected])
*
* Date: July 2006
*
* Environment: Xen Minimal OS
* Description: Simple grant tables implementation. About as stupid as it's
* possible to be and still work.
*
****************************************************************************
*/
#include <mini-os/os.h>
#include <mini-os/mm.h>
#include <mini-os/gnttab.h>
#include <mini-os/semaphore.h>
#include <xen/memory.h> /* init gnttab for pvh */
#define NR_RESERVED_ENTRIES 8
/* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
#define NR_GRANT_FRAMES 4
#define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(grant_entry_t))
static grant_entry_t *gnttab_table;
static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
#ifdef GNT_DEBUG
static char inuse[NR_GRANT_ENTRIES];
#endif
static __DECLARE_SEMAPHORE_GENERIC(gnttab_sem, 0);
static void
put_free_entry(grant_ref_t ref)
{
unsigned long flags;
local_irq_save(flags);
#ifdef GNT_DEBUG
BUG_ON(!inuse[ref]);
inuse[ref] = 0;
#endif
gnttab_list[ref] = gnttab_list[0];
gnttab_list[0] = ref;
local_irq_restore(flags);
up(&gnttab_sem);
}
static grant_ref_t
get_free_entry(void)
{
unsigned int ref;
unsigned long flags;
down(&gnttab_sem);
local_irq_save(flags);
ref = gnttab_list[0];
BUG_ON(ref < NR_RESERVED_ENTRIES || ref >= NR_GRANT_ENTRIES);
gnttab_list[0] = gnttab_list[ref];
#ifdef GNT_DEBUG
BUG_ON(inuse[ref]);
inuse[ref] = 1;
#endif
local_irq_restore(flags);
return ref;
}
grant_ref_t
gnttab_grant_access(domid_t domid, unsigned long frame, int readonly)
{
grant_ref_t ref;
ref = get_free_entry();
gnttab_table[ref].frame = frame;
gnttab_table[ref].domid = domid;
wmb();
gnttab_table[ref].flags = GTF_permit_access | (readonly ? GTF_readonly : 0x0);
return ref;
}
grant_ref_t
gnttab_grant_transfer(domid_t domid, unsigned long pfn)
{
grant_ref_t ref;
ref = get_free_entry();
gnttab_table[ref].frame = pfn;
gnttab_table[ref].domid = domid;
wmb();
gnttab_table[ref].flags = GTF_accept_transfer;
return ref;
}
int
gnttab_end_access(grant_ref_t ref)
{
uint16_t flags, nflags;
BUG_ON(ref >= NR_GRANT_ENTRIES || ref < NR_RESERVED_ENTRIES);
nflags = gnttab_table[ref].flags;
do {
if ((flags = nflags) & (GTF_reading|GTF_writing)) {
printk("WARNING: g.e. still in use! (%x)\n", flags);
return 0;
}
} while ((nflags = synch_cmpxchg(&gnttab_table[ref].flags, flags, 0)) !=
flags);
put_free_entry(ref);
return 1;
}
unsigned long
gnttab_end_transfer(grant_ref_t ref)
{
unsigned long frame;
uint16_t flags;
BUG_ON(ref >= NR_GRANT_ENTRIES || ref < NR_RESERVED_ENTRIES);
while (!((flags = gnttab_table[ref].flags) & GTF_transfer_committed)) {
if (synch_cmpxchg(&gnttab_table[ref].flags, flags, 0) == flags) {
printk("Release unused transfer grant.\n");
put_free_entry(ref);
return 0;
}
}
/* If a transfer is in progress then wait until it is completed. */
while (!(flags & GTF_transfer_completed)) {
flags = gnttab_table[ref].flags;
}
/* Read the frame number /after/ reading completion status. */
rmb();
frame = gnttab_table[ref].frame;
put_free_entry(ref);
return frame;
}
grant_ref_t
gnttab_alloc_and_grant(void **map)
{
unsigned long mfn;
grant_ref_t gref;
*map = (void *)alloc_page();
mfn = virt_to_mfn(*map);
gref = gnttab_grant_access(0, mfn, 0);
return gref;
}
static const char * const gnttabop_error_msgs[] = GNTTABOP_error_msgs;
const char *
gnttabop_error(int16_t status)
{
status = -status;
if (status < 0 || status >= ARRAY_SIZE(gnttabop_error_msgs))
return "bad status";
else
return gnttabop_error_msgs[status];
}
void
init_gnttab(void)
{
struct gnttab_setup_table setup;
unsigned long frames[NR_GRANT_FRAMES];
int i;
#ifdef GNT_DEBUG
memset(inuse, 1, sizeof(inuse));
#endif
for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
put_free_entry(i);
if (!xen_feature(XENFEAT_auto_translated_physmap)) {
setup.dom = DOMID_SELF;
setup.nr_frames = NR_GRANT_FRAMES;
set_xen_guest_handle(setup.frame_list, frames);
HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
gnttab_table = map_frames(frames, NR_GRANT_FRAMES);
}
else {
struct xen_add_to_physmap xatp;
i = NR_GRANT_FRAMES - 1;
/* map_frames works differently if p2m is autotranslated,
* in that gnttab_table are just mapped with vaddrs that are the same as
* paddrs by mini-os itself, and provided to Xen for p2m mapping */
gnttab_table = map_frames(NULL, NR_GRANT_FRAMES);
for(i = NR_GRANT_FRAMES - 1; i >= 0; i--) {
xatp.domid = DOMID_SELF;
xatp.idx = i;
xatp.space = XENMAPSPACE_grant_table;
xatp.gpfn =
virt_to_pfn((unsigned long)gnttab_table + (i << PAGE_SHIFT));
if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp) != 0) {
printk("grant table add_to_physmap failed\n");
break;
}
}
}
printk("gnttab_table mapped at %p.\n", gnttab_table);
}
void
fini_gnttab(void)
{
struct gnttab_setup_table setup;
setup.dom = DOMID_SELF;
setup.nr_frames = 0;
HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
}
void
suspend_gnttab(void)
{
int i;
for (i = 0; i < NR_GRANT_FRAMES; i++) {
HYPERVISOR_update_va_mapping((unsigned long)(((char *)gnttab_table) + PAGE_SIZE*i),
(pte_t){0x0<<PAGE_SHIFT}, UVMF_INVLPG);
}
}
void
resume_gnttab(void)
{
int i;
struct gnttab_setup_table setup;
unsigned long frames[NR_GRANT_FRAMES];
setup.dom = DOMID_SELF;
setup.nr_frames = NR_GRANT_FRAMES;
set_xen_guest_handle(setup.frame_list, frames);
HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
for (i = 0; i < NR_GRANT_FRAMES; i++) {
HYPERVISOR_update_va_mapping((unsigned long)(((char *)gnttab_table) + PAGE_SIZE*i),
(pte_t){(frames[i] << PAGE_SHIFT) | L1_PROT}, UVMF_INVLPG);
}
}