]>
Commit | Line | Data |
---|---|---|
ad9a8612 JF |
1 | /****************************************************************************** |
2 | * grant_table.c | |
3 | * | |
4 | * Granting foreign access to our memory reservation. | |
5 | * | |
6 | * Copyright (c) 2005-2006, Christopher Clark | |
7 | * Copyright (c) 2004-2005, K A Fraser | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License version 2 | |
11 | * as published by the Free Software Foundation; or, when distributed | |
12 | * separately from the Linux kernel or incorporated into other | |
13 | * software packages, subject to the following license: | |
14 | * | |
15 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
16 | * of this source file (the "Software"), to deal in the Software without | |
17 | * restriction, including without limitation the rights to use, copy, modify, | |
18 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
19 | * and to permit persons to whom the Software is furnished to do so, subject to | |
20 | * the following conditions: | |
21 | * | |
22 | * The above copyright notice and this permission notice shall be included in | |
23 | * all copies or substantial portions of the Software. | |
24 | * | |
25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
26 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
27 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
28 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
29 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
30 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
31 | * IN THE SOFTWARE. | |
32 | */ | |
33 | ||
34 | #include <linux/module.h> | |
35 | #include <linux/sched.h> | |
36 | #include <linux/mm.h> | |
5a0e3ad6 | 37 | #include <linux/slab.h> |
ad9a8612 JF |
38 | #include <linux/vmalloc.h> |
39 | #include <linux/uaccess.h> | |
183d03cc | 40 | #include <linux/io.h> |
ad9a8612 | 41 | |
1ccbf534 | 42 | #include <xen/xen.h> |
ad9a8612 JF |
43 | #include <xen/interface/xen.h> |
44 | #include <xen/page.h> | |
45 | #include <xen/grant_table.h> | |
183d03cc | 46 | #include <xen/interface/memory.h> |
ecbf29cd | 47 | #include <asm/xen/hypercall.h> |
ad9a8612 JF |
48 | |
49 | #include <asm/pgtable.h> | |
50 | #include <asm/sync_bitops.h> | |
51 | ||
52 | ||
53 | /* External tools reserve first few grant table entries. */ | |
54 | #define NR_RESERVED_ENTRIES 8 | |
55 | #define GNTTAB_LIST_END 0xffffffff | |
0f9f5a95 | 56 | #define GREFS_PER_GRANT_FRAME (PAGE_SIZE / sizeof(struct grant_entry_v1)) |
ad9a8612 JF |
57 | |
58 | static grant_ref_t **gnttab_list; | |
59 | static unsigned int nr_grant_frames; | |
60 | static unsigned int boot_max_nr_grant_frames; | |
61 | static int gnttab_free_count; | |
62 | static grant_ref_t gnttab_free_head; | |
63 | static DEFINE_SPINLOCK(gnttab_list_lock); | |
183d03cc SS |
64 | unsigned long xen_hvm_resume_frames; |
65 | EXPORT_SYMBOL_GPL(xen_hvm_resume_frames); | |
ad9a8612 | 66 | |
0f9f5a95 AL |
67 | static union { |
68 | struct grant_entry_v1 *v1; | |
69 | void *addr; | |
70 | } gnttab_shared; | |
71 | ||
72 | /*This is a structure of function pointers for grant table*/ | |
73 | struct gnttab_ops { | |
74 | /* | |
75 | * Mapping a list of frames for storing grant entries. First input | |
76 | * parameter is used to storing grant table address when grant table | |
77 | * being setup, second parameter is the number of frames to map grant | |
78 | * table. Returning GNTST_okay means success and negative value means | |
79 | * failure. | |
80 | */ | |
81 | int (*map_frames)(unsigned long *, unsigned int); | |
82 | /* | |
83 | * Release a list of frames which are mapped in map_frames for grant | |
84 | * entry status. | |
85 | */ | |
86 | void (*unmap_frames)(void); | |
87 | /* | |
88 | * Introducing a valid entry into the grant table, granting the frame | |
89 | * of this grant entry to domain for accessing, or transfering, or | |
90 | * transitively accessing. First input parameter is reference of this | |
91 | * introduced grant entry, second one is domid of granted domain, third | |
92 | * one is the frame to be granted, and the last one is status of the | |
93 | * grant entry to be updated. | |
94 | */ | |
95 | void (*update_entry)(grant_ref_t, domid_t, unsigned long, unsigned); | |
96 | /* | |
97 | * Stop granting a grant entry to domain for accessing. First input | |
98 | * parameter is reference of a grant entry whose grant access will be | |
99 | * stopped, second one is not in use now. If the grant entry is | |
100 | * currently mapped for reading or writing, just return failure(==0) | |
101 | * directly and don't tear down the grant access. Otherwise, stop grant | |
102 | * access for this entry and return success(==1). | |
103 | */ | |
104 | int (*end_foreign_access_ref)(grant_ref_t, int); | |
105 | /* | |
106 | * Stop granting a grant entry to domain for transfer. If tranfer has | |
107 | * not started, just reclaim the grant entry and return failure(==0). | |
108 | * Otherwise, wait for the transfer to complete and then return the | |
109 | * frame. | |
110 | */ | |
111 | unsigned long (*end_foreign_transfer_ref)(grant_ref_t); | |
112 | /* | |
113 | * Query the status of a grant entry. Input parameter is reference of | |
114 | * queried grant entry, return value is the status of queried entry. | |
115 | * Detailed status(writing/reading) can be gotten from the return value | |
116 | * by bit operations. | |
117 | */ | |
118 | int (*query_foreign_access)(grant_ref_t); | |
119 | }; | |
120 | ||
121 | static struct gnttab_ops *gnttab_interface; | |
122 | ||
123 | static int grant_table_version; | |
ad9a8612 JF |
124 | |
125 | static struct gnttab_free_callback *gnttab_free_callback_list; | |
126 | ||
127 | static int gnttab_expand(unsigned int req_entries); | |
128 | ||
129 | #define RPP (PAGE_SIZE / sizeof(grant_ref_t)) | |
130 | ||
131 | static inline grant_ref_t *__gnttab_entry(grant_ref_t entry) | |
132 | { | |
133 | return &gnttab_list[(entry) / RPP][(entry) % RPP]; | |
134 | } | |
135 | /* This can be used as an l-value */ | |
136 | #define gnttab_entry(entry) (*__gnttab_entry(entry)) | |
137 | ||
138 | static int get_free_entries(unsigned count) | |
139 | { | |
140 | unsigned long flags; | |
272800dc | 141 | int ref, rc = 0; |
ad9a8612 JF |
142 | grant_ref_t head; |
143 | ||
144 | spin_lock_irqsave(&gnttab_list_lock, flags); | |
145 | ||
146 | if ((gnttab_free_count < count) && | |
147 | ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) { | |
148 | spin_unlock_irqrestore(&gnttab_list_lock, flags); | |
149 | return rc; | |
150 | } | |
151 | ||
152 | ref = head = gnttab_free_head; | |
153 | gnttab_free_count -= count; | |
154 | while (count-- > 1) | |
155 | head = gnttab_entry(head); | |
156 | gnttab_free_head = gnttab_entry(head); | |
157 | gnttab_entry(head) = GNTTAB_LIST_END; | |
158 | ||
159 | spin_unlock_irqrestore(&gnttab_list_lock, flags); | |
160 | ||
161 | return ref; | |
162 | } | |
163 | ||
164 | static void do_free_callbacks(void) | |
165 | { | |
166 | struct gnttab_free_callback *callback, *next; | |
167 | ||
168 | callback = gnttab_free_callback_list; | |
169 | gnttab_free_callback_list = NULL; | |
170 | ||
171 | while (callback != NULL) { | |
172 | next = callback->next; | |
173 | if (gnttab_free_count >= callback->count) { | |
174 | callback->next = NULL; | |
175 | callback->fn(callback->arg); | |
176 | } else { | |
177 | callback->next = gnttab_free_callback_list; | |
178 | gnttab_free_callback_list = callback; | |
179 | } | |
180 | callback = next; | |
181 | } | |
182 | } | |
183 | ||
184 | static inline void check_free_callbacks(void) | |
185 | { | |
186 | if (unlikely(gnttab_free_callback_list)) | |
187 | do_free_callbacks(); | |
188 | } | |
189 | ||
190 | static void put_free_entry(grant_ref_t ref) | |
191 | { | |
192 | unsigned long flags; | |
193 | spin_lock_irqsave(&gnttab_list_lock, flags); | |
194 | gnttab_entry(ref) = gnttab_free_head; | |
195 | gnttab_free_head = ref; | |
196 | gnttab_free_count++; | |
197 | check_free_callbacks(); | |
198 | spin_unlock_irqrestore(&gnttab_list_lock, flags); | |
199 | } | |
200 | ||
0f9f5a95 AL |
201 | /* |
202 | * Introducing a valid entry into the grant table: | |
203 | * 1. Write ent->domid. | |
204 | * 2. Write ent->frame: | |
205 | * GTF_permit_access: Frame to which access is permitted. | |
206 | * GTF_accept_transfer: Pseudo-phys frame slot being filled by new | |
207 | * frame, or zero if none. | |
208 | * 3. Write memory barrier (WMB). | |
209 | * 4. Write ent->flags, inc. valid type. | |
210 | */ | |
211 | static void gnttab_update_entry_v1(grant_ref_t ref, domid_t domid, | |
212 | unsigned long frame, unsigned flags) | |
ad9a8612 | 213 | { |
0f9f5a95 AL |
214 | gnttab_shared.v1[ref].domid = domid; |
215 | gnttab_shared.v1[ref].frame = frame; | |
ad9a8612 | 216 | wmb(); |
0f9f5a95 | 217 | gnttab_shared.v1[ref].flags = flags; |
ad9a8612 JF |
218 | } |
219 | ||
220 | /* | |
221 | * Public grant-issuing interface functions | |
222 | */ | |
223 | void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid, | |
224 | unsigned long frame, int readonly) | |
225 | { | |
0f9f5a95 | 226 | gnttab_interface->update_entry(ref, domid, frame, |
ad9a8612 JF |
227 | GTF_permit_access | (readonly ? GTF_readonly : 0)); |
228 | } | |
229 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref); | |
230 | ||
231 | int gnttab_grant_foreign_access(domid_t domid, unsigned long frame, | |
232 | int readonly) | |
233 | { | |
234 | int ref; | |
235 | ||
236 | ref = get_free_entries(1); | |
237 | if (unlikely(ref < 0)) | |
238 | return -ENOSPC; | |
239 | ||
240 | gnttab_grant_foreign_access_ref(ref, domid, frame, readonly); | |
241 | ||
242 | return ref; | |
243 | } | |
244 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access); | |
245 | ||
0f9f5a95 | 246 | static int gnttab_query_foreign_access_v1(grant_ref_t ref) |
ad9a8612 | 247 | { |
0f9f5a95 AL |
248 | return gnttab_shared.v1[ref].flags & (GTF_reading|GTF_writing); |
249 | } | |
ad9a8612 | 250 | |
0f9f5a95 AL |
251 | int gnttab_query_foreign_access(grant_ref_t ref) |
252 | { | |
253 | return gnttab_interface->query_foreign_access(ref); | |
ad9a8612 JF |
254 | } |
255 | EXPORT_SYMBOL_GPL(gnttab_query_foreign_access); | |
256 | ||
0f9f5a95 | 257 | static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref, int readonly) |
ad9a8612 JF |
258 | { |
259 | u16 flags, nflags; | |
260 | ||
0f9f5a95 | 261 | nflags = gnttab_shared.v1[ref].flags; |
ad9a8612 JF |
262 | do { |
263 | flags = nflags; | |
264 | if (flags & (GTF_reading|GTF_writing)) { | |
265 | printk(KERN_ALERT "WARNING: g.e. still in use!\n"); | |
266 | return 0; | |
267 | } | |
0f9f5a95 | 268 | } while ((nflags = sync_cmpxchg(&gnttab_shared.v1[ref].flags, flags, 0)) != flags); |
ad9a8612 JF |
269 | |
270 | return 1; | |
271 | } | |
0f9f5a95 AL |
272 | |
273 | int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly) | |
274 | { | |
275 | return gnttab_interface->end_foreign_access_ref(ref, readonly); | |
276 | } | |
ad9a8612 JF |
277 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref); |
278 | ||
279 | void gnttab_end_foreign_access(grant_ref_t ref, int readonly, | |
280 | unsigned long page) | |
281 | { | |
282 | if (gnttab_end_foreign_access_ref(ref, readonly)) { | |
283 | put_free_entry(ref); | |
284 | if (page != 0) | |
285 | free_page(page); | |
286 | } else { | |
287 | /* XXX This needs to be fixed so that the ref and page are | |
288 | placed on a list to be freed up later. */ | |
289 | printk(KERN_WARNING | |
290 | "WARNING: leaking g.e. and page still in use!\n"); | |
291 | } | |
292 | } | |
293 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_access); | |
294 | ||
295 | int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn) | |
296 | { | |
297 | int ref; | |
298 | ||
299 | ref = get_free_entries(1); | |
300 | if (unlikely(ref < 0)) | |
301 | return -ENOSPC; | |
302 | gnttab_grant_foreign_transfer_ref(ref, domid, pfn); | |
303 | ||
304 | return ref; | |
305 | } | |
306 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer); | |
307 | ||
308 | void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid, | |
309 | unsigned long pfn) | |
310 | { | |
0f9f5a95 | 311 | gnttab_interface->update_entry(ref, domid, pfn, GTF_accept_transfer); |
ad9a8612 JF |
312 | } |
313 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref); | |
314 | ||
0f9f5a95 | 315 | static unsigned long gnttab_end_foreign_transfer_ref_v1(grant_ref_t ref) |
ad9a8612 JF |
316 | { |
317 | unsigned long frame; | |
318 | u16 flags; | |
319 | ||
320 | /* | |
321 | * If a transfer is not even yet started, try to reclaim the grant | |
322 | * reference and return failure (== 0). | |
323 | */ | |
0f9f5a95 AL |
324 | while (!((flags = gnttab_shared.v1[ref].flags) & GTF_transfer_committed)) { |
325 | if (sync_cmpxchg(&gnttab_shared.v1[ref].flags, flags, 0) == flags) | |
ad9a8612 JF |
326 | return 0; |
327 | cpu_relax(); | |
328 | } | |
329 | ||
330 | /* If a transfer is in progress then wait until it is completed. */ | |
331 | while (!(flags & GTF_transfer_completed)) { | |
0f9f5a95 | 332 | flags = gnttab_shared.v1[ref].flags; |
ad9a8612 JF |
333 | cpu_relax(); |
334 | } | |
335 | ||
336 | rmb(); /* Read the frame number /after/ reading completion status. */ | |
0f9f5a95 | 337 | frame = gnttab_shared.v1[ref].frame; |
ad9a8612 JF |
338 | BUG_ON(frame == 0); |
339 | ||
340 | return frame; | |
341 | } | |
0f9f5a95 AL |
342 | |
343 | unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref) | |
344 | { | |
345 | return gnttab_interface->end_foreign_transfer_ref(ref); | |
346 | } | |
ad9a8612 JF |
347 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref); |
348 | ||
349 | unsigned long gnttab_end_foreign_transfer(grant_ref_t ref) | |
350 | { | |
351 | unsigned long frame = gnttab_end_foreign_transfer_ref(ref); | |
352 | put_free_entry(ref); | |
353 | return frame; | |
354 | } | |
355 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer); | |
356 | ||
357 | void gnttab_free_grant_reference(grant_ref_t ref) | |
358 | { | |
359 | put_free_entry(ref); | |
360 | } | |
361 | EXPORT_SYMBOL_GPL(gnttab_free_grant_reference); | |
362 | ||
363 | void gnttab_free_grant_references(grant_ref_t head) | |
364 | { | |
365 | grant_ref_t ref; | |
366 | unsigned long flags; | |
367 | int count = 1; | |
368 | if (head == GNTTAB_LIST_END) | |
369 | return; | |
370 | spin_lock_irqsave(&gnttab_list_lock, flags); | |
371 | ref = head; | |
372 | while (gnttab_entry(ref) != GNTTAB_LIST_END) { | |
373 | ref = gnttab_entry(ref); | |
374 | count++; | |
375 | } | |
376 | gnttab_entry(ref) = gnttab_free_head; | |
377 | gnttab_free_head = head; | |
378 | gnttab_free_count += count; | |
379 | check_free_callbacks(); | |
380 | spin_unlock_irqrestore(&gnttab_list_lock, flags); | |
381 | } | |
382 | EXPORT_SYMBOL_GPL(gnttab_free_grant_references); | |
383 | ||
384 | int gnttab_alloc_grant_references(u16 count, grant_ref_t *head) | |
385 | { | |
386 | int h = get_free_entries(count); | |
387 | ||
388 | if (h < 0) | |
389 | return -ENOSPC; | |
390 | ||
391 | *head = h; | |
392 | ||
393 | return 0; | |
394 | } | |
395 | EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references); | |
396 | ||
397 | int gnttab_empty_grant_references(const grant_ref_t *private_head) | |
398 | { | |
399 | return (*private_head == GNTTAB_LIST_END); | |
400 | } | |
401 | EXPORT_SYMBOL_GPL(gnttab_empty_grant_references); | |
402 | ||
403 | int gnttab_claim_grant_reference(grant_ref_t *private_head) | |
404 | { | |
405 | grant_ref_t g = *private_head; | |
406 | if (unlikely(g == GNTTAB_LIST_END)) | |
407 | return -ENOSPC; | |
408 | *private_head = gnttab_entry(g); | |
409 | return g; | |
410 | } | |
411 | EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference); | |
412 | ||
413 | void gnttab_release_grant_reference(grant_ref_t *private_head, | |
414 | grant_ref_t release) | |
415 | { | |
416 | gnttab_entry(release) = *private_head; | |
417 | *private_head = release; | |
418 | } | |
419 | EXPORT_SYMBOL_GPL(gnttab_release_grant_reference); | |
420 | ||
421 | void gnttab_request_free_callback(struct gnttab_free_callback *callback, | |
422 | void (*fn)(void *), void *arg, u16 count) | |
423 | { | |
424 | unsigned long flags; | |
425 | spin_lock_irqsave(&gnttab_list_lock, flags); | |
426 | if (callback->next) | |
427 | goto out; | |
428 | callback->fn = fn; | |
429 | callback->arg = arg; | |
430 | callback->count = count; | |
431 | callback->next = gnttab_free_callback_list; | |
432 | gnttab_free_callback_list = callback; | |
433 | check_free_callbacks(); | |
434 | out: | |
435 | spin_unlock_irqrestore(&gnttab_list_lock, flags); | |
436 | } | |
437 | EXPORT_SYMBOL_GPL(gnttab_request_free_callback); | |
438 | ||
439 | void gnttab_cancel_free_callback(struct gnttab_free_callback *callback) | |
440 | { | |
441 | struct gnttab_free_callback **pcb; | |
442 | unsigned long flags; | |
443 | ||
444 | spin_lock_irqsave(&gnttab_list_lock, flags); | |
445 | for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) { | |
446 | if (*pcb == callback) { | |
447 | *pcb = callback->next; | |
448 | break; | |
449 | } | |
450 | } | |
451 | spin_unlock_irqrestore(&gnttab_list_lock, flags); | |
452 | } | |
453 | EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback); | |
454 | ||
455 | static int grow_gnttab_list(unsigned int more_frames) | |
456 | { | |
457 | unsigned int new_nr_grant_frames, extra_entries, i; | |
bbc60c18 | 458 | unsigned int nr_glist_frames, new_nr_glist_frames; |
ad9a8612 JF |
459 | |
460 | new_nr_grant_frames = nr_grant_frames + more_frames; | |
461 | extra_entries = more_frames * GREFS_PER_GRANT_FRAME; | |
462 | ||
bbc60c18 MAEM |
463 | nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP; |
464 | new_nr_glist_frames = | |
465 | (new_nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP; | |
466 | for (i = nr_glist_frames; i < new_nr_glist_frames; i++) { | |
ad9a8612 JF |
467 | gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC); |
468 | if (!gnttab_list[i]) | |
469 | goto grow_nomem; | |
470 | } | |
471 | ||
472 | ||
473 | for (i = GREFS_PER_GRANT_FRAME * nr_grant_frames; | |
474 | i < GREFS_PER_GRANT_FRAME * new_nr_grant_frames - 1; i++) | |
475 | gnttab_entry(i) = i + 1; | |
476 | ||
477 | gnttab_entry(i) = gnttab_free_head; | |
478 | gnttab_free_head = GREFS_PER_GRANT_FRAME * nr_grant_frames; | |
479 | gnttab_free_count += extra_entries; | |
480 | ||
481 | nr_grant_frames = new_nr_grant_frames; | |
482 | ||
483 | check_free_callbacks(); | |
484 | ||
485 | return 0; | |
486 | ||
487 | grow_nomem: | |
bbc60c18 | 488 | for ( ; i >= nr_glist_frames; i--) |
ad9a8612 JF |
489 | free_page((unsigned long) gnttab_list[i]); |
490 | return -ENOMEM; | |
491 | } | |
492 | ||
493 | static unsigned int __max_nr_grant_frames(void) | |
494 | { | |
495 | struct gnttab_query_size query; | |
496 | int rc; | |
497 | ||
498 | query.dom = DOMID_SELF; | |
499 | ||
500 | rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1); | |
501 | if ((rc < 0) || (query.status != GNTST_okay)) | |
502 | return 4; /* Legacy max supported number of frames */ | |
503 | ||
504 | return query.max_nr_frames; | |
505 | } | |
506 | ||
183d03cc | 507 | unsigned int gnttab_max_grant_frames(void) |
ad9a8612 JF |
508 | { |
509 | unsigned int xen_max = __max_nr_grant_frames(); | |
510 | ||
511 | if (xen_max > boot_max_nr_grant_frames) | |
512 | return boot_max_nr_grant_frames; | |
513 | return xen_max; | |
514 | } | |
183d03cc | 515 | EXPORT_SYMBOL_GPL(gnttab_max_grant_frames); |
ad9a8612 | 516 | |
289b777e | 517 | int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops, |
0930bba6 SS |
518 | struct gnttab_map_grant_ref *kmap_ops, |
519 | struct page **pages, unsigned int count) | |
289b777e SS |
520 | { |
521 | int i, ret; | |
522 | pte_t *pte; | |
523 | unsigned long mfn; | |
524 | ||
525 | ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count); | |
87f1d40a JF |
526 | if (ret) |
527 | return ret; | |
289b777e | 528 | |
aab8f11a DDG |
529 | if (xen_feature(XENFEAT_auto_translated_physmap)) |
530 | return ret; | |
531 | ||
289b777e | 532 | for (i = 0; i < count; i++) { |
dc4972a4 IC |
533 | /* Do not add to override if the map failed. */ |
534 | if (map_ops[i].status) | |
535 | continue; | |
536 | ||
cf8d9163 KRW |
537 | if (map_ops[i].flags & GNTMAP_contains_pte) { |
538 | pte = (pte_t *) (mfn_to_virt(PFN_DOWN(map_ops[i].host_addr)) + | |
289b777e | 539 | (map_ops[i].host_addr & ~PAGE_MASK)); |
cf8d9163 KRW |
540 | mfn = pte_mfn(*pte); |
541 | } else { | |
542 | /* If you really wanted to do this: | |
543 | * mfn = PFN_DOWN(map_ops[i].dev_bus_addr); | |
544 | * | |
545 | * The reason we do not implement it is b/c on the | |
546 | * unmap path (gnttab_unmap_refs) we have no means of | |
547 | * checking whether the page is !GNTMAP_contains_pte. | |
548 | * | |
549 | * That is without some extra data-structure to carry | |
550 | * the struct page, bool clear_pte, and list_head next | |
551 | * tuples and deal with allocation/delallocation, etc. | |
552 | * | |
553 | * The users of this API set the GNTMAP_contains_pte | |
554 | * flag so lets just return not supported until it | |
555 | * becomes neccessary to implement. | |
556 | */ | |
557 | return -EOPNOTSUPP; | |
558 | } | |
0930bba6 | 559 | ret = m2p_add_override(mfn, pages[i], &kmap_ops[i]); |
87f1d40a JF |
560 | if (ret) |
561 | return ret; | |
289b777e SS |
562 | } |
563 | ||
564 | return ret; | |
565 | } | |
566 | EXPORT_SYMBOL_GPL(gnttab_map_refs); | |
567 | ||
568 | int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops, | |
569 | struct page **pages, unsigned int count) | |
570 | { | |
571 | int i, ret; | |
572 | ||
573 | ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count); | |
87f1d40a JF |
574 | if (ret) |
575 | return ret; | |
576 | ||
aab8f11a DDG |
577 | if (xen_feature(XENFEAT_auto_translated_physmap)) |
578 | return ret; | |
579 | ||
87f1d40a | 580 | for (i = 0; i < count; i++) { |
cf8d9163 | 581 | ret = m2p_remove_override(pages[i], true /* clear the PTE */); |
87f1d40a JF |
582 | if (ret) |
583 | return ret; | |
584 | } | |
289b777e SS |
585 | |
586 | return ret; | |
587 | } | |
588 | EXPORT_SYMBOL_GPL(gnttab_unmap_refs); | |
589 | ||
0f9f5a95 AL |
590 | static int gnttab_map_frames_v1(unsigned long *frames, unsigned int nr_gframes) |
591 | { | |
592 | int rc; | |
593 | ||
594 | rc = arch_gnttab_map_shared(frames, nr_gframes, | |
595 | gnttab_max_grant_frames(), | |
596 | &gnttab_shared.addr); | |
597 | BUG_ON(rc); | |
598 | ||
599 | return 0; | |
600 | } | |
601 | ||
602 | static void gnttab_unmap_frames_v1(void) | |
603 | { | |
604 | arch_gnttab_unmap_shared(gnttab_shared.addr, nr_grant_frames); | |
605 | } | |
606 | ||
ad9a8612 JF |
607 | static int gnttab_map(unsigned int start_idx, unsigned int end_idx) |
608 | { | |
609 | struct gnttab_setup_table setup; | |
610 | unsigned long *frames; | |
611 | unsigned int nr_gframes = end_idx + 1; | |
612 | int rc; | |
613 | ||
183d03cc SS |
614 | if (xen_hvm_domain()) { |
615 | struct xen_add_to_physmap xatp; | |
616 | unsigned int i = end_idx; | |
617 | rc = 0; | |
618 | /* | |
619 | * Loop backwards, so that the first hypercall has the largest | |
620 | * index, ensuring that the table will grow only once. | |
621 | */ | |
622 | do { | |
623 | xatp.domid = DOMID_SELF; | |
624 | xatp.idx = i; | |
625 | xatp.space = XENMAPSPACE_grant_table; | |
626 | xatp.gpfn = (xen_hvm_resume_frames >> PAGE_SHIFT) + i; | |
627 | rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp); | |
628 | if (rc != 0) { | |
629 | printk(KERN_WARNING | |
630 | "grant table add_to_physmap failed, err=%d\n", rc); | |
631 | break; | |
632 | } | |
633 | } while (i-- > start_idx); | |
634 | ||
635 | return rc; | |
636 | } | |
637 | ||
ad9a8612 JF |
638 | frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC); |
639 | if (!frames) | |
640 | return -ENOMEM; | |
641 | ||
642 | setup.dom = DOMID_SELF; | |
643 | setup.nr_frames = nr_gframes; | |
87e27cf6 | 644 | set_xen_guest_handle(setup.frame_list, frames); |
ad9a8612 JF |
645 | |
646 | rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1); | |
647 | if (rc == -ENOSYS) { | |
648 | kfree(frames); | |
649 | return -ENOSYS; | |
650 | } | |
651 | ||
652 | BUG_ON(rc || setup.status); | |
653 | ||
0f9f5a95 | 654 | rc = gnttab_interface->map_frames(frames, nr_gframes); |
ad9a8612 JF |
655 | |
656 | kfree(frames); | |
657 | ||
0f9f5a95 AL |
658 | return rc; |
659 | } | |
660 | ||
661 | static struct gnttab_ops gnttab_v1_ops = { | |
662 | .map_frames = gnttab_map_frames_v1, | |
663 | .unmap_frames = gnttab_unmap_frames_v1, | |
664 | .update_entry = gnttab_update_entry_v1, | |
665 | .end_foreign_access_ref = gnttab_end_foreign_access_ref_v1, | |
666 | .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v1, | |
667 | .query_foreign_access = gnttab_query_foreign_access_v1, | |
668 | }; | |
669 | ||
670 | static void gnttab_request_version(void) | |
671 | { | |
672 | grant_table_version = 1; | |
673 | gnttab_interface = &gnttab_v1_ops; | |
674 | printk(KERN_INFO "Grant tables using version %d layout.\n", | |
675 | grant_table_version); | |
ad9a8612 JF |
676 | } |
677 | ||
0e91398f | 678 | int gnttab_resume(void) |
ad9a8612 | 679 | { |
183d03cc SS |
680 | unsigned int max_nr_gframes; |
681 | ||
0f9f5a95 | 682 | gnttab_request_version(); |
183d03cc SS |
683 | max_nr_gframes = gnttab_max_grant_frames(); |
684 | if (max_nr_gframes < nr_grant_frames) | |
ad9a8612 | 685 | return -ENOSYS; |
183d03cc SS |
686 | |
687 | if (xen_pv_domain()) | |
688 | return gnttab_map(0, nr_grant_frames - 1); | |
689 | ||
0f9f5a95 AL |
690 | if (gnttab_shared.addr == NULL) { |
691 | gnttab_shared.addr = ioremap(xen_hvm_resume_frames, | |
692 | PAGE_SIZE * max_nr_gframes); | |
693 | if (gnttab_shared.addr == NULL) { | |
183d03cc SS |
694 | printk(KERN_WARNING |
695 | "Failed to ioremap gnttab share frames!"); | |
696 | return -ENOMEM; | |
697 | } | |
698 | } | |
699 | ||
700 | gnttab_map(0, nr_grant_frames - 1); | |
701 | ||
702 | return 0; | |
ad9a8612 JF |
703 | } |
704 | ||
0e91398f | 705 | int gnttab_suspend(void) |
ad9a8612 | 706 | { |
0f9f5a95 | 707 | gnttab_interface->unmap_frames(); |
ad9a8612 JF |
708 | return 0; |
709 | } | |
710 | ||
711 | static int gnttab_expand(unsigned int req_entries) | |
712 | { | |
713 | int rc; | |
714 | unsigned int cur, extra; | |
715 | ||
716 | cur = nr_grant_frames; | |
717 | extra = ((req_entries + (GREFS_PER_GRANT_FRAME-1)) / | |
718 | GREFS_PER_GRANT_FRAME); | |
183d03cc | 719 | if (cur + extra > gnttab_max_grant_frames()) |
ad9a8612 JF |
720 | return -ENOSPC; |
721 | ||
722 | rc = gnttab_map(cur, cur + extra - 1); | |
723 | if (rc == 0) | |
724 | rc = grow_gnttab_list(extra); | |
725 | ||
726 | return rc; | |
727 | } | |
728 | ||
183d03cc | 729 | int gnttab_init(void) |
ad9a8612 JF |
730 | { |
731 | int i; | |
bbc60c18 | 732 | unsigned int max_nr_glist_frames, nr_glist_frames; |
ad9a8612 JF |
733 | unsigned int nr_init_grefs; |
734 | ||
ad9a8612 JF |
735 | nr_grant_frames = 1; |
736 | boot_max_nr_grant_frames = __max_nr_grant_frames(); | |
737 | ||
738 | /* Determine the maximum number of frames required for the | |
739 | * grant reference free list on the current hypervisor. | |
740 | */ | |
741 | max_nr_glist_frames = (boot_max_nr_grant_frames * | |
bbc60c18 | 742 | GREFS_PER_GRANT_FRAME / RPP); |
ad9a8612 JF |
743 | |
744 | gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *), | |
745 | GFP_KERNEL); | |
746 | if (gnttab_list == NULL) | |
747 | return -ENOMEM; | |
748 | ||
bbc60c18 MAEM |
749 | nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP; |
750 | for (i = 0; i < nr_glist_frames; i++) { | |
ad9a8612 JF |
751 | gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL); |
752 | if (gnttab_list[i] == NULL) | |
753 | goto ini_nomem; | |
754 | } | |
755 | ||
756 | if (gnttab_resume() < 0) | |
757 | return -ENODEV; | |
758 | ||
759 | nr_init_grefs = nr_grant_frames * GREFS_PER_GRANT_FRAME; | |
760 | ||
761 | for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++) | |
762 | gnttab_entry(i) = i + 1; | |
763 | ||
764 | gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END; | |
765 | gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES; | |
766 | gnttab_free_head = NR_RESERVED_ENTRIES; | |
767 | ||
768 | printk("Grant table initialized\n"); | |
769 | return 0; | |
770 | ||
771 | ini_nomem: | |
772 | for (i--; i >= 0; i--) | |
773 | free_page((unsigned long)gnttab_list[i]); | |
774 | kfree(gnttab_list); | |
775 | return -ENOMEM; | |
776 | } | |
183d03cc SS |
777 | EXPORT_SYMBOL_GPL(gnttab_init); |
778 | ||
779 | static int __devinit __gnttab_init(void) | |
780 | { | |
781 | /* Delay grant-table initialization in the PV on HVM case */ | |
782 | if (xen_hvm_domain()) | |
783 | return 0; | |
784 | ||
785 | if (!xen_pv_domain()) | |
786 | return -ENODEV; | |
787 | ||
788 | return gnttab_init(); | |
789 | } | |
ad9a8612 | 790 | |
183d03cc | 791 | core_initcall(__gnttab_init); |