Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 38 | #include <linux/vmalloc.h> |
| 39 | #include <linux/uaccess.h> |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 40 | #include <linux/io.h> |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 41 | |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 42 | #include <xen/xen.h> |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 43 | #include <xen/interface/xen.h> |
| 44 | #include <xen/page.h> |
| 45 | #include <xen/grant_table.h> |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 46 | #include <xen/interface/memory.h> |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 47 | #include <xen/hvc-console.h> |
Jeremy Fitzhardinge | ecbf29c | 2008-12-16 12:37:07 -0800 | [diff] [blame] | 48 | #include <asm/xen/hypercall.h> |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 49 | |
| 50 | #include <asm/pgtable.h> |
| 51 | #include <asm/sync_bitops.h> |
| 52 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 53 | /* External tools reserve first few grant table entries. */ |
| 54 | #define NR_RESERVED_ENTRIES 8 |
| 55 | #define GNTTAB_LIST_END 0xffffffff |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 56 | #define GREFS_PER_GRANT_FRAME \ |
| 57 | (grant_table_version == 1 ? \ |
| 58 | (PAGE_SIZE / sizeof(struct grant_entry_v1)) : \ |
| 59 | (PAGE_SIZE / sizeof(union grant_entry_v2))) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 60 | |
| 61 | static grant_ref_t **gnttab_list; |
| 62 | static unsigned int nr_grant_frames; |
| 63 | static unsigned int boot_max_nr_grant_frames; |
| 64 | static int gnttab_free_count; |
| 65 | static grant_ref_t gnttab_free_head; |
| 66 | static DEFINE_SPINLOCK(gnttab_list_lock); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 67 | unsigned long xen_hvm_resume_frames; |
| 68 | EXPORT_SYMBOL_GPL(xen_hvm_resume_frames); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 69 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 70 | static union { |
| 71 | struct grant_entry_v1 *v1; |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 72 | union grant_entry_v2 *v2; |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 73 | void *addr; |
| 74 | } gnttab_shared; |
| 75 | |
| 76 | /*This is a structure of function pointers for grant table*/ |
| 77 | struct gnttab_ops { |
| 78 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame^] | 79 | * Mapping a list of frames for storing grant entries. Frames parameter |
| 80 | * is used to store grant table address when grant table being setup, |
| 81 | * nr_gframes is the number of frames to map grant table. Returning |
| 82 | * GNTST_okay means success and negative value means failure. |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 83 | */ |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame^] | 84 | int (*map_frames)(unsigned long *frames, unsigned int nr_gframes); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 85 | /* |
| 86 | * Release a list of frames which are mapped in map_frames for grant |
| 87 | * entry status. |
| 88 | */ |
| 89 | void (*unmap_frames)(void); |
| 90 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame^] | 91 | * Introducing a valid entry into the grant table, granting the frame of |
| 92 | * this grant entry to domain for accessing or transfering. Ref |
| 93 | * parameter is reference of this introduced grant entry, domid is id of |
| 94 | * granted domain, frame is the page frame to be granted, and flags is |
| 95 | * status of the grant entry to be updated. |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 96 | */ |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame^] | 97 | void (*update_entry)(grant_ref_t ref, domid_t domid, |
| 98 | unsigned long frame, unsigned flags); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 99 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame^] | 100 | * Stop granting a grant entry to domain for accessing. Ref parameter is |
| 101 | * reference of a grant entry whose grant access will be stopped, |
| 102 | * readonly is not in use in this function. If the grant entry is |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 103 | * currently mapped for reading or writing, just return failure(==0) |
| 104 | * directly and don't tear down the grant access. Otherwise, stop grant |
| 105 | * access for this entry and return success(==1). |
| 106 | */ |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame^] | 107 | int (*end_foreign_access_ref)(grant_ref_t ref, int readonly); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 108 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame^] | 109 | * Stop granting a grant entry to domain for transfer. Ref parameter is |
| 110 | * reference of a grant entry whose grant transfer will be stopped. If |
| 111 | * tranfer has not started, just reclaim the grant entry and return |
| 112 | * failure(==0). Otherwise, wait for the transfer to complete and then |
| 113 | * return the frame. |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 114 | */ |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame^] | 115 | unsigned long (*end_foreign_transfer_ref)(grant_ref_t ref); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 116 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame^] | 117 | * Query the status of a grant entry. Ref parameter is reference of |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 118 | * queried grant entry, return value is the status of queried entry. |
| 119 | * Detailed status(writing/reading) can be gotten from the return value |
| 120 | * by bit operations. |
| 121 | */ |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame^] | 122 | int (*query_foreign_access)(grant_ref_t ref); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static struct gnttab_ops *gnttab_interface; |
| 126 | |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 127 | /*This reflects status of grant entries, so act as a global value*/ |
| 128 | static grant_status_t *grstatus; |
| 129 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 130 | static int grant_table_version; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 131 | |
| 132 | static struct gnttab_free_callback *gnttab_free_callback_list; |
| 133 | |
| 134 | static int gnttab_expand(unsigned int req_entries); |
| 135 | |
| 136 | #define RPP (PAGE_SIZE / sizeof(grant_ref_t)) |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 137 | #define SPP (PAGE_SIZE / sizeof(grant_status_t)) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 138 | |
| 139 | static inline grant_ref_t *__gnttab_entry(grant_ref_t entry) |
| 140 | { |
| 141 | return &gnttab_list[(entry) / RPP][(entry) % RPP]; |
| 142 | } |
| 143 | /* This can be used as an l-value */ |
| 144 | #define gnttab_entry(entry) (*__gnttab_entry(entry)) |
| 145 | |
| 146 | static int get_free_entries(unsigned count) |
| 147 | { |
| 148 | unsigned long flags; |
Konrad Rzeszutek Wilk | 272800d | 2011-07-22 14:00:06 -0400 | [diff] [blame] | 149 | int ref, rc = 0; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 150 | grant_ref_t head; |
| 151 | |
| 152 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 153 | |
| 154 | if ((gnttab_free_count < count) && |
| 155 | ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) { |
| 156 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 157 | return rc; |
| 158 | } |
| 159 | |
| 160 | ref = head = gnttab_free_head; |
| 161 | gnttab_free_count -= count; |
| 162 | while (count-- > 1) |
| 163 | head = gnttab_entry(head); |
| 164 | gnttab_free_head = gnttab_entry(head); |
| 165 | gnttab_entry(head) = GNTTAB_LIST_END; |
| 166 | |
| 167 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 168 | |
| 169 | return ref; |
| 170 | } |
| 171 | |
| 172 | static void do_free_callbacks(void) |
| 173 | { |
| 174 | struct gnttab_free_callback *callback, *next; |
| 175 | |
| 176 | callback = gnttab_free_callback_list; |
| 177 | gnttab_free_callback_list = NULL; |
| 178 | |
| 179 | while (callback != NULL) { |
| 180 | next = callback->next; |
| 181 | if (gnttab_free_count >= callback->count) { |
| 182 | callback->next = NULL; |
| 183 | callback->fn(callback->arg); |
| 184 | } else { |
| 185 | callback->next = gnttab_free_callback_list; |
| 186 | gnttab_free_callback_list = callback; |
| 187 | } |
| 188 | callback = next; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static inline void check_free_callbacks(void) |
| 193 | { |
| 194 | if (unlikely(gnttab_free_callback_list)) |
| 195 | do_free_callbacks(); |
| 196 | } |
| 197 | |
| 198 | static void put_free_entry(grant_ref_t ref) |
| 199 | { |
| 200 | unsigned long flags; |
| 201 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 202 | gnttab_entry(ref) = gnttab_free_head; |
| 203 | gnttab_free_head = ref; |
| 204 | gnttab_free_count++; |
| 205 | check_free_callbacks(); |
| 206 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 207 | } |
| 208 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 209 | /* |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 210 | * Following applies to gnttab_update_entry_v1 and gnttab_update_entry_v2. |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 211 | * Introducing a valid entry into the grant table: |
| 212 | * 1. Write ent->domid. |
| 213 | * 2. Write ent->frame: |
| 214 | * GTF_permit_access: Frame to which access is permitted. |
| 215 | * GTF_accept_transfer: Pseudo-phys frame slot being filled by new |
| 216 | * frame, or zero if none. |
| 217 | * 3. Write memory barrier (WMB). |
| 218 | * 4. Write ent->flags, inc. valid type. |
| 219 | */ |
| 220 | static void gnttab_update_entry_v1(grant_ref_t ref, domid_t domid, |
| 221 | unsigned long frame, unsigned flags) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 222 | { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 223 | gnttab_shared.v1[ref].domid = domid; |
| 224 | gnttab_shared.v1[ref].frame = frame; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 225 | wmb(); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 226 | gnttab_shared.v1[ref].flags = flags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 229 | static void gnttab_update_entry_v2(grant_ref_t ref, domid_t domid, |
| 230 | unsigned long frame, unsigned flags) |
| 231 | { |
| 232 | gnttab_shared.v2[ref].hdr.domid = domid; |
| 233 | gnttab_shared.v2[ref].full_page.frame = frame; |
| 234 | wmb(); |
| 235 | gnttab_shared.v2[ref].hdr.flags = GTF_permit_access | flags; |
| 236 | } |
| 237 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 238 | /* |
| 239 | * Public grant-issuing interface functions |
| 240 | */ |
| 241 | void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid, |
| 242 | unsigned long frame, int readonly) |
| 243 | { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 244 | gnttab_interface->update_entry(ref, domid, frame, |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 245 | GTF_permit_access | (readonly ? GTF_readonly : 0)); |
| 246 | } |
| 247 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref); |
| 248 | |
| 249 | int gnttab_grant_foreign_access(domid_t domid, unsigned long frame, |
| 250 | int readonly) |
| 251 | { |
| 252 | int ref; |
| 253 | |
| 254 | ref = get_free_entries(1); |
| 255 | if (unlikely(ref < 0)) |
| 256 | return -ENOSPC; |
| 257 | |
| 258 | gnttab_grant_foreign_access_ref(ref, domid, frame, readonly); |
| 259 | |
| 260 | return ref; |
| 261 | } |
| 262 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access); |
| 263 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 264 | static int gnttab_query_foreign_access_v1(grant_ref_t ref) |
| 265 | { |
| 266 | return gnttab_shared.v1[ref].flags & (GTF_reading|GTF_writing); |
| 267 | } |
| 268 | |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 269 | static int gnttab_query_foreign_access_v2(grant_ref_t ref) |
| 270 | { |
| 271 | return grstatus[ref] & (GTF_reading|GTF_writing); |
| 272 | } |
| 273 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 274 | int gnttab_query_foreign_access(grant_ref_t ref) |
| 275 | { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 276 | return gnttab_interface->query_foreign_access(ref); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 277 | } |
| 278 | EXPORT_SYMBOL_GPL(gnttab_query_foreign_access); |
| 279 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 280 | static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref, int readonly) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 281 | { |
| 282 | u16 flags, nflags; |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 283 | u16 *pflags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 284 | |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 285 | pflags = &gnttab_shared.v1[ref].flags; |
| 286 | nflags = *pflags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 287 | do { |
| 288 | flags = nflags; |
| 289 | if (flags & (GTF_reading|GTF_writing)) { |
| 290 | printk(KERN_ALERT "WARNING: g.e. still in use!\n"); |
| 291 | return 0; |
| 292 | } |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 293 | } while ((nflags = sync_cmpxchg(pflags, flags, 0)) != flags); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 294 | |
| 295 | return 1; |
| 296 | } |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 297 | |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 298 | static int gnttab_end_foreign_access_ref_v2(grant_ref_t ref, int readonly) |
| 299 | { |
| 300 | gnttab_shared.v2[ref].hdr.flags = 0; |
| 301 | mb(); |
| 302 | if (grstatus[ref] & (GTF_reading|GTF_writing)) { |
| 303 | return 0; |
| 304 | } else { |
| 305 | /* The read of grstatus needs to have acquire |
| 306 | semantics. On x86, reads already have |
| 307 | that, and we just need to protect against |
| 308 | compiler reorderings. On other |
| 309 | architectures we may need a full |
| 310 | barrier. */ |
| 311 | #ifdef CONFIG_X86 |
| 312 | barrier(); |
| 313 | #else |
| 314 | mb(); |
| 315 | #endif |
| 316 | } |
| 317 | |
| 318 | return 1; |
| 319 | } |
| 320 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 321 | int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly) |
| 322 | { |
| 323 | return gnttab_interface->end_foreign_access_ref(ref, readonly); |
| 324 | } |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 325 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref); |
| 326 | |
| 327 | void gnttab_end_foreign_access(grant_ref_t ref, int readonly, |
| 328 | unsigned long page) |
| 329 | { |
| 330 | if (gnttab_end_foreign_access_ref(ref, readonly)) { |
| 331 | put_free_entry(ref); |
| 332 | if (page != 0) |
| 333 | free_page(page); |
| 334 | } else { |
| 335 | /* XXX This needs to be fixed so that the ref and page are |
| 336 | placed on a list to be freed up later. */ |
| 337 | printk(KERN_WARNING |
| 338 | "WARNING: leaking g.e. and page still in use!\n"); |
| 339 | } |
| 340 | } |
| 341 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_access); |
| 342 | |
| 343 | int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn) |
| 344 | { |
| 345 | int ref; |
| 346 | |
| 347 | ref = get_free_entries(1); |
| 348 | if (unlikely(ref < 0)) |
| 349 | return -ENOSPC; |
| 350 | gnttab_grant_foreign_transfer_ref(ref, domid, pfn); |
| 351 | |
| 352 | return ref; |
| 353 | } |
| 354 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer); |
| 355 | |
| 356 | void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid, |
| 357 | unsigned long pfn) |
| 358 | { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 359 | gnttab_interface->update_entry(ref, domid, pfn, GTF_accept_transfer); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 360 | } |
| 361 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref); |
| 362 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 363 | static unsigned long gnttab_end_foreign_transfer_ref_v1(grant_ref_t ref) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 364 | { |
| 365 | unsigned long frame; |
| 366 | u16 flags; |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 367 | u16 *pflags; |
| 368 | |
| 369 | pflags = &gnttab_shared.v1[ref].flags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * If a transfer is not even yet started, try to reclaim the grant |
| 373 | * reference and return failure (== 0). |
| 374 | */ |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 375 | while (!((flags = *pflags) & GTF_transfer_committed)) { |
| 376 | if (sync_cmpxchg(pflags, flags, 0) == flags) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 377 | return 0; |
| 378 | cpu_relax(); |
| 379 | } |
| 380 | |
| 381 | /* If a transfer is in progress then wait until it is completed. */ |
| 382 | while (!(flags & GTF_transfer_completed)) { |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 383 | flags = *pflags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 384 | cpu_relax(); |
| 385 | } |
| 386 | |
| 387 | rmb(); /* Read the frame number /after/ reading completion status. */ |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 388 | frame = gnttab_shared.v1[ref].frame; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 389 | BUG_ON(frame == 0); |
| 390 | |
| 391 | return frame; |
| 392 | } |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 393 | |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 394 | static unsigned long gnttab_end_foreign_transfer_ref_v2(grant_ref_t ref) |
| 395 | { |
| 396 | unsigned long frame; |
| 397 | u16 flags; |
| 398 | u16 *pflags; |
| 399 | |
| 400 | pflags = &gnttab_shared.v2[ref].hdr.flags; |
| 401 | |
| 402 | /* |
| 403 | * If a transfer is not even yet started, try to reclaim the grant |
| 404 | * reference and return failure (== 0). |
| 405 | */ |
| 406 | while (!((flags = *pflags) & GTF_transfer_committed)) { |
| 407 | if (sync_cmpxchg(pflags, flags, 0) == flags) |
| 408 | return 0; |
| 409 | cpu_relax(); |
| 410 | } |
| 411 | |
| 412 | /* If a transfer is in progress then wait until it is completed. */ |
| 413 | while (!(flags & GTF_transfer_completed)) { |
| 414 | flags = *pflags; |
| 415 | cpu_relax(); |
| 416 | } |
| 417 | |
| 418 | rmb(); /* Read the frame number /after/ reading completion status. */ |
| 419 | frame = gnttab_shared.v2[ref].full_page.frame; |
| 420 | BUG_ON(frame == 0); |
| 421 | |
| 422 | return frame; |
| 423 | } |
| 424 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 425 | unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref) |
| 426 | { |
| 427 | return gnttab_interface->end_foreign_transfer_ref(ref); |
| 428 | } |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 429 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref); |
| 430 | |
| 431 | unsigned long gnttab_end_foreign_transfer(grant_ref_t ref) |
| 432 | { |
| 433 | unsigned long frame = gnttab_end_foreign_transfer_ref(ref); |
| 434 | put_free_entry(ref); |
| 435 | return frame; |
| 436 | } |
| 437 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer); |
| 438 | |
| 439 | void gnttab_free_grant_reference(grant_ref_t ref) |
| 440 | { |
| 441 | put_free_entry(ref); |
| 442 | } |
| 443 | EXPORT_SYMBOL_GPL(gnttab_free_grant_reference); |
| 444 | |
| 445 | void gnttab_free_grant_references(grant_ref_t head) |
| 446 | { |
| 447 | grant_ref_t ref; |
| 448 | unsigned long flags; |
| 449 | int count = 1; |
| 450 | if (head == GNTTAB_LIST_END) |
| 451 | return; |
| 452 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 453 | ref = head; |
| 454 | while (gnttab_entry(ref) != GNTTAB_LIST_END) { |
| 455 | ref = gnttab_entry(ref); |
| 456 | count++; |
| 457 | } |
| 458 | gnttab_entry(ref) = gnttab_free_head; |
| 459 | gnttab_free_head = head; |
| 460 | gnttab_free_count += count; |
| 461 | check_free_callbacks(); |
| 462 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 463 | } |
| 464 | EXPORT_SYMBOL_GPL(gnttab_free_grant_references); |
| 465 | |
| 466 | int gnttab_alloc_grant_references(u16 count, grant_ref_t *head) |
| 467 | { |
| 468 | int h = get_free_entries(count); |
| 469 | |
| 470 | if (h < 0) |
| 471 | return -ENOSPC; |
| 472 | |
| 473 | *head = h; |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references); |
| 478 | |
| 479 | int gnttab_empty_grant_references(const grant_ref_t *private_head) |
| 480 | { |
| 481 | return (*private_head == GNTTAB_LIST_END); |
| 482 | } |
| 483 | EXPORT_SYMBOL_GPL(gnttab_empty_grant_references); |
| 484 | |
| 485 | int gnttab_claim_grant_reference(grant_ref_t *private_head) |
| 486 | { |
| 487 | grant_ref_t g = *private_head; |
| 488 | if (unlikely(g == GNTTAB_LIST_END)) |
| 489 | return -ENOSPC; |
| 490 | *private_head = gnttab_entry(g); |
| 491 | return g; |
| 492 | } |
| 493 | EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference); |
| 494 | |
| 495 | void gnttab_release_grant_reference(grant_ref_t *private_head, |
| 496 | grant_ref_t release) |
| 497 | { |
| 498 | gnttab_entry(release) = *private_head; |
| 499 | *private_head = release; |
| 500 | } |
| 501 | EXPORT_SYMBOL_GPL(gnttab_release_grant_reference); |
| 502 | |
| 503 | void gnttab_request_free_callback(struct gnttab_free_callback *callback, |
| 504 | void (*fn)(void *), void *arg, u16 count) |
| 505 | { |
| 506 | unsigned long flags; |
| 507 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 508 | if (callback->next) |
| 509 | goto out; |
| 510 | callback->fn = fn; |
| 511 | callback->arg = arg; |
| 512 | callback->count = count; |
| 513 | callback->next = gnttab_free_callback_list; |
| 514 | gnttab_free_callback_list = callback; |
| 515 | check_free_callbacks(); |
| 516 | out: |
| 517 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 518 | } |
| 519 | EXPORT_SYMBOL_GPL(gnttab_request_free_callback); |
| 520 | |
| 521 | void gnttab_cancel_free_callback(struct gnttab_free_callback *callback) |
| 522 | { |
| 523 | struct gnttab_free_callback **pcb; |
| 524 | unsigned long flags; |
| 525 | |
| 526 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 527 | for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) { |
| 528 | if (*pcb == callback) { |
| 529 | *pcb = callback->next; |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 534 | } |
| 535 | EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback); |
| 536 | |
| 537 | static int grow_gnttab_list(unsigned int more_frames) |
| 538 | { |
| 539 | unsigned int new_nr_grant_frames, extra_entries, i; |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 540 | unsigned int nr_glist_frames, new_nr_glist_frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 541 | |
| 542 | new_nr_grant_frames = nr_grant_frames + more_frames; |
| 543 | extra_entries = more_frames * GREFS_PER_GRANT_FRAME; |
| 544 | |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 545 | nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP; |
| 546 | new_nr_glist_frames = |
| 547 | (new_nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP; |
| 548 | for (i = nr_glist_frames; i < new_nr_glist_frames; i++) { |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 549 | gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC); |
| 550 | if (!gnttab_list[i]) |
| 551 | goto grow_nomem; |
| 552 | } |
| 553 | |
| 554 | |
| 555 | for (i = GREFS_PER_GRANT_FRAME * nr_grant_frames; |
| 556 | i < GREFS_PER_GRANT_FRAME * new_nr_grant_frames - 1; i++) |
| 557 | gnttab_entry(i) = i + 1; |
| 558 | |
| 559 | gnttab_entry(i) = gnttab_free_head; |
| 560 | gnttab_free_head = GREFS_PER_GRANT_FRAME * nr_grant_frames; |
| 561 | gnttab_free_count += extra_entries; |
| 562 | |
| 563 | nr_grant_frames = new_nr_grant_frames; |
| 564 | |
| 565 | check_free_callbacks(); |
| 566 | |
| 567 | return 0; |
| 568 | |
| 569 | grow_nomem: |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 570 | for ( ; i >= nr_glist_frames; i--) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 571 | free_page((unsigned long) gnttab_list[i]); |
| 572 | return -ENOMEM; |
| 573 | } |
| 574 | |
| 575 | static unsigned int __max_nr_grant_frames(void) |
| 576 | { |
| 577 | struct gnttab_query_size query; |
| 578 | int rc; |
| 579 | |
| 580 | query.dom = DOMID_SELF; |
| 581 | |
| 582 | rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1); |
| 583 | if ((rc < 0) || (query.status != GNTST_okay)) |
| 584 | return 4; /* Legacy max supported number of frames */ |
| 585 | |
| 586 | return query.max_nr_frames; |
| 587 | } |
| 588 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 589 | unsigned int gnttab_max_grant_frames(void) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 590 | { |
| 591 | unsigned int xen_max = __max_nr_grant_frames(); |
| 592 | |
| 593 | if (xen_max > boot_max_nr_grant_frames) |
| 594 | return boot_max_nr_grant_frames; |
| 595 | return xen_max; |
| 596 | } |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 597 | EXPORT_SYMBOL_GPL(gnttab_max_grant_frames); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 598 | |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 599 | int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops, |
Annie Li | c123799 | 2011-11-22 09:59:56 +0800 | [diff] [blame] | 600 | struct gnttab_map_grant_ref *kmap_ops, |
| 601 | struct page **pages, unsigned int count) |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 602 | { |
| 603 | int i, ret; |
| 604 | pte_t *pte; |
| 605 | unsigned long mfn; |
| 606 | |
| 607 | ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count); |
Jeremy Fitzhardinge | 87f1d40 | 2010-12-13 14:42:30 +0000 | [diff] [blame] | 608 | if (ret) |
| 609 | return ret; |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 610 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 611 | if (xen_feature(XENFEAT_auto_translated_physmap)) |
| 612 | return ret; |
| 613 | |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 614 | for (i = 0; i < count; i++) { |
Ian Campbell | dc4972a | 2011-03-04 17:38:21 +0000 | [diff] [blame] | 615 | /* Do not add to override if the map failed. */ |
| 616 | if (map_ops[i].status) |
| 617 | continue; |
| 618 | |
Konrad Rzeszutek Wilk | cf8d916 | 2011-02-28 17:58:48 -0500 | [diff] [blame] | 619 | if (map_ops[i].flags & GNTMAP_contains_pte) { |
| 620 | pte = (pte_t *) (mfn_to_virt(PFN_DOWN(map_ops[i].host_addr)) + |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 621 | (map_ops[i].host_addr & ~PAGE_MASK)); |
Konrad Rzeszutek Wilk | cf8d916 | 2011-02-28 17:58:48 -0500 | [diff] [blame] | 622 | mfn = pte_mfn(*pte); |
| 623 | } else { |
| 624 | /* If you really wanted to do this: |
| 625 | * mfn = PFN_DOWN(map_ops[i].dev_bus_addr); |
| 626 | * |
| 627 | * The reason we do not implement it is b/c on the |
| 628 | * unmap path (gnttab_unmap_refs) we have no means of |
| 629 | * checking whether the page is !GNTMAP_contains_pte. |
| 630 | * |
| 631 | * That is without some extra data-structure to carry |
| 632 | * the struct page, bool clear_pte, and list_head next |
| 633 | * tuples and deal with allocation/delallocation, etc. |
| 634 | * |
| 635 | * The users of this API set the GNTMAP_contains_pte |
| 636 | * flag so lets just return not supported until it |
| 637 | * becomes neccessary to implement. |
| 638 | */ |
| 639 | return -EOPNOTSUPP; |
| 640 | } |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 641 | ret = m2p_add_override(mfn, pages[i], &kmap_ops[i]); |
Jeremy Fitzhardinge | 87f1d40 | 2010-12-13 14:42:30 +0000 | [diff] [blame] | 642 | if (ret) |
| 643 | return ret; |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | return ret; |
| 647 | } |
| 648 | EXPORT_SYMBOL_GPL(gnttab_map_refs); |
| 649 | |
| 650 | int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops, |
Annie Li | c123799 | 2011-11-22 09:59:56 +0800 | [diff] [blame] | 651 | struct page **pages, unsigned int count) |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 652 | { |
| 653 | int i, ret; |
| 654 | |
| 655 | ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count); |
Jeremy Fitzhardinge | 87f1d40 | 2010-12-13 14:42:30 +0000 | [diff] [blame] | 656 | if (ret) |
| 657 | return ret; |
| 658 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 659 | if (xen_feature(XENFEAT_auto_translated_physmap)) |
| 660 | return ret; |
| 661 | |
Jeremy Fitzhardinge | 87f1d40 | 2010-12-13 14:42:30 +0000 | [diff] [blame] | 662 | for (i = 0; i < count; i++) { |
Konrad Rzeszutek Wilk | cf8d916 | 2011-02-28 17:58:48 -0500 | [diff] [blame] | 663 | ret = m2p_remove_override(pages[i], true /* clear the PTE */); |
Jeremy Fitzhardinge | 87f1d40 | 2010-12-13 14:42:30 +0000 | [diff] [blame] | 664 | if (ret) |
| 665 | return ret; |
| 666 | } |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 667 | |
| 668 | return ret; |
| 669 | } |
| 670 | EXPORT_SYMBOL_GPL(gnttab_unmap_refs); |
| 671 | |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 672 | static unsigned nr_status_frames(unsigned nr_grant_frames) |
| 673 | { |
| 674 | return (nr_grant_frames * GREFS_PER_GRANT_FRAME + SPP - 1) / SPP; |
| 675 | } |
| 676 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 677 | static int gnttab_map_frames_v1(unsigned long *frames, unsigned int nr_gframes) |
| 678 | { |
| 679 | int rc; |
| 680 | |
| 681 | rc = arch_gnttab_map_shared(frames, nr_gframes, |
| 682 | gnttab_max_grant_frames(), |
| 683 | &gnttab_shared.addr); |
| 684 | BUG_ON(rc); |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static void gnttab_unmap_frames_v1(void) |
| 690 | { |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 691 | arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames); |
| 692 | } |
| 693 | |
| 694 | static int gnttab_map_frames_v2(unsigned long *frames, unsigned int nr_gframes) |
| 695 | { |
| 696 | uint64_t *sframes; |
| 697 | unsigned int nr_sframes; |
| 698 | struct gnttab_get_status_frames getframes; |
| 699 | int rc; |
| 700 | |
| 701 | nr_sframes = nr_status_frames(nr_gframes); |
| 702 | |
| 703 | /* No need for kzalloc as it is initialized in following hypercall |
| 704 | * GNTTABOP_get_status_frames. |
| 705 | */ |
| 706 | sframes = kmalloc(nr_sframes * sizeof(uint64_t), GFP_ATOMIC); |
| 707 | if (!sframes) |
| 708 | return -ENOMEM; |
| 709 | |
| 710 | getframes.dom = DOMID_SELF; |
| 711 | getframes.nr_frames = nr_sframes; |
| 712 | set_xen_guest_handle(getframes.frame_list, sframes); |
| 713 | |
| 714 | rc = HYPERVISOR_grant_table_op(GNTTABOP_get_status_frames, |
| 715 | &getframes, 1); |
| 716 | if (rc == -ENOSYS) { |
| 717 | kfree(sframes); |
| 718 | return -ENOSYS; |
| 719 | } |
| 720 | |
| 721 | BUG_ON(rc || getframes.status); |
| 722 | |
| 723 | rc = arch_gnttab_map_status(sframes, nr_sframes, |
| 724 | nr_status_frames(gnttab_max_grant_frames()), |
| 725 | &grstatus); |
| 726 | BUG_ON(rc); |
| 727 | kfree(sframes); |
| 728 | |
| 729 | rc = arch_gnttab_map_shared(frames, nr_gframes, |
| 730 | gnttab_max_grant_frames(), |
| 731 | &gnttab_shared.addr); |
| 732 | BUG_ON(rc); |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | static void gnttab_unmap_frames_v2(void) |
| 738 | { |
| 739 | arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames); |
| 740 | arch_gnttab_unmap(grstatus, nr_status_frames(nr_grant_frames)); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 741 | } |
| 742 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 743 | static int gnttab_map(unsigned int start_idx, unsigned int end_idx) |
| 744 | { |
| 745 | struct gnttab_setup_table setup; |
| 746 | unsigned long *frames; |
| 747 | unsigned int nr_gframes = end_idx + 1; |
| 748 | int rc; |
| 749 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 750 | if (xen_hvm_domain()) { |
| 751 | struct xen_add_to_physmap xatp; |
| 752 | unsigned int i = end_idx; |
| 753 | rc = 0; |
| 754 | /* |
| 755 | * Loop backwards, so that the first hypercall has the largest |
| 756 | * index, ensuring that the table will grow only once. |
| 757 | */ |
| 758 | do { |
| 759 | xatp.domid = DOMID_SELF; |
| 760 | xatp.idx = i; |
| 761 | xatp.space = XENMAPSPACE_grant_table; |
| 762 | xatp.gpfn = (xen_hvm_resume_frames >> PAGE_SHIFT) + i; |
| 763 | rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp); |
| 764 | if (rc != 0) { |
| 765 | printk(KERN_WARNING |
| 766 | "grant table add_to_physmap failed, err=%d\n", rc); |
| 767 | break; |
| 768 | } |
| 769 | } while (i-- > start_idx); |
| 770 | |
| 771 | return rc; |
| 772 | } |
| 773 | |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 774 | /* No need for kzalloc as it is initialized in following hypercall |
| 775 | * GNTTABOP_setup_table. |
| 776 | */ |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 777 | frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC); |
| 778 | if (!frames) |
| 779 | return -ENOMEM; |
| 780 | |
| 781 | setup.dom = DOMID_SELF; |
| 782 | setup.nr_frames = nr_gframes; |
Isaku Yamahata | 87e27cf | 2008-04-02 10:53:52 -0700 | [diff] [blame] | 783 | set_xen_guest_handle(setup.frame_list, frames); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 784 | |
| 785 | rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1); |
| 786 | if (rc == -ENOSYS) { |
| 787 | kfree(frames); |
| 788 | return -ENOSYS; |
| 789 | } |
| 790 | |
| 791 | BUG_ON(rc || setup.status); |
| 792 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 793 | rc = gnttab_interface->map_frames(frames, nr_gframes); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 794 | |
| 795 | kfree(frames); |
| 796 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 797 | return rc; |
| 798 | } |
| 799 | |
| 800 | static struct gnttab_ops gnttab_v1_ops = { |
| 801 | .map_frames = gnttab_map_frames_v1, |
| 802 | .unmap_frames = gnttab_unmap_frames_v1, |
| 803 | .update_entry = gnttab_update_entry_v1, |
| 804 | .end_foreign_access_ref = gnttab_end_foreign_access_ref_v1, |
| 805 | .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v1, |
| 806 | .query_foreign_access = gnttab_query_foreign_access_v1, |
| 807 | }; |
| 808 | |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 809 | static struct gnttab_ops gnttab_v2_ops = { |
| 810 | .map_frames = gnttab_map_frames_v2, |
| 811 | .unmap_frames = gnttab_unmap_frames_v2, |
| 812 | .update_entry = gnttab_update_entry_v2, |
| 813 | .end_foreign_access_ref = gnttab_end_foreign_access_ref_v2, |
| 814 | .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v2, |
| 815 | .query_foreign_access = gnttab_query_foreign_access_v2, |
| 816 | }; |
| 817 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 818 | static void gnttab_request_version(void) |
| 819 | { |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 820 | int rc; |
| 821 | struct gnttab_set_version gsv; |
| 822 | |
| 823 | gsv.version = 2; |
| 824 | rc = HYPERVISOR_grant_table_op(GNTTABOP_set_version, &gsv, 1); |
| 825 | if (rc == 0) { |
| 826 | grant_table_version = 2; |
| 827 | gnttab_interface = &gnttab_v2_ops; |
| 828 | } else if (grant_table_version == 2) { |
| 829 | /* |
| 830 | * If we've already used version 2 features, |
| 831 | * but then suddenly discover that they're not |
| 832 | * available (e.g. migrating to an older |
| 833 | * version of Xen), almost unbounded badness |
| 834 | * can happen. |
| 835 | */ |
| 836 | panic("we need grant tables version 2, but only version 1 is available"); |
| 837 | } else { |
| 838 | grant_table_version = 1; |
| 839 | gnttab_interface = &gnttab_v1_ops; |
| 840 | } |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 841 | printk(KERN_INFO "Grant tables using version %d layout.\n", |
| 842 | grant_table_version); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 845 | int gnttab_resume(void) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 846 | { |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 847 | unsigned int max_nr_gframes; |
| 848 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 849 | gnttab_request_version(); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 850 | max_nr_gframes = gnttab_max_grant_frames(); |
| 851 | if (max_nr_gframes < nr_grant_frames) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 852 | return -ENOSYS; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 853 | |
| 854 | if (xen_pv_domain()) |
| 855 | return gnttab_map(0, nr_grant_frames - 1); |
| 856 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 857 | if (gnttab_shared.addr == NULL) { |
| 858 | gnttab_shared.addr = ioremap(xen_hvm_resume_frames, |
| 859 | PAGE_SIZE * max_nr_gframes); |
| 860 | if (gnttab_shared.addr == NULL) { |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 861 | printk(KERN_WARNING |
| 862 | "Failed to ioremap gnttab share frames!"); |
| 863 | return -ENOMEM; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | gnttab_map(0, nr_grant_frames - 1); |
| 868 | |
| 869 | return 0; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 872 | int gnttab_suspend(void) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 873 | { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 874 | gnttab_interface->unmap_frames(); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | static int gnttab_expand(unsigned int req_entries) |
| 879 | { |
| 880 | int rc; |
| 881 | unsigned int cur, extra; |
| 882 | |
| 883 | cur = nr_grant_frames; |
| 884 | extra = ((req_entries + (GREFS_PER_GRANT_FRAME-1)) / |
| 885 | GREFS_PER_GRANT_FRAME); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 886 | if (cur + extra > gnttab_max_grant_frames()) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 887 | return -ENOSPC; |
| 888 | |
| 889 | rc = gnttab_map(cur, cur + extra - 1); |
| 890 | if (rc == 0) |
| 891 | rc = grow_gnttab_list(extra); |
| 892 | |
| 893 | return rc; |
| 894 | } |
| 895 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 896 | int gnttab_init(void) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 897 | { |
| 898 | int i; |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 899 | unsigned int max_nr_glist_frames, nr_glist_frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 900 | unsigned int nr_init_grefs; |
| 901 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 902 | nr_grant_frames = 1; |
| 903 | boot_max_nr_grant_frames = __max_nr_grant_frames(); |
| 904 | |
| 905 | /* Determine the maximum number of frames required for the |
| 906 | * grant reference free list on the current hypervisor. |
| 907 | */ |
| 908 | max_nr_glist_frames = (boot_max_nr_grant_frames * |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 909 | GREFS_PER_GRANT_FRAME / RPP); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 910 | |
| 911 | gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *), |
| 912 | GFP_KERNEL); |
| 913 | if (gnttab_list == NULL) |
| 914 | return -ENOMEM; |
| 915 | |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 916 | nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP; |
| 917 | for (i = 0; i < nr_glist_frames; i++) { |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 918 | gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL); |
| 919 | if (gnttab_list[i] == NULL) |
| 920 | goto ini_nomem; |
| 921 | } |
| 922 | |
| 923 | if (gnttab_resume() < 0) |
| 924 | return -ENODEV; |
| 925 | |
| 926 | nr_init_grefs = nr_grant_frames * GREFS_PER_GRANT_FRAME; |
| 927 | |
| 928 | for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++) |
| 929 | gnttab_entry(i) = i + 1; |
| 930 | |
| 931 | gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END; |
| 932 | gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES; |
| 933 | gnttab_free_head = NR_RESERVED_ENTRIES; |
| 934 | |
| 935 | printk("Grant table initialized\n"); |
| 936 | return 0; |
| 937 | |
| 938 | ini_nomem: |
| 939 | for (i--; i >= 0; i--) |
| 940 | free_page((unsigned long)gnttab_list[i]); |
| 941 | kfree(gnttab_list); |
| 942 | return -ENOMEM; |
| 943 | } |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 944 | EXPORT_SYMBOL_GPL(gnttab_init); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 945 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 946 | static int __devinit __gnttab_init(void) |
| 947 | { |
| 948 | /* Delay grant-table initialization in the PV on HVM case */ |
| 949 | if (xen_hvm_domain()) |
| 950 | return 0; |
| 951 | |
| 952 | if (!xen_pv_domain()) |
| 953 | return -ENODEV; |
| 954 | |
| 955 | return gnttab_init(); |
| 956 | } |
| 957 | |
| 958 | core_initcall(__gnttab_init); |