grant-table.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. #include <linux/module.h>
  34. #include <linux/sched.h>
  35. #include <linux/mm.h>
  36. #include <linux/slab.h>
  37. #include <linux/vmalloc.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/io.h>
  40. #include <xen/xen.h>
  41. #include <xen/interface/xen.h>
  42. #include <xen/page.h>
  43. #include <xen/grant_table.h>
  44. #include <xen/interface/memory.h>
  45. #include <asm/xen/hypercall.h>
  46. #include <asm/pgtable.h>
  47. #include <asm/sync_bitops.h>
  48. /* External tools reserve first few grant table entries. */
  49. #define NR_RESERVED_ENTRIES 8
  50. #define GNTTAB_LIST_END 0xffffffff
  51. #define GREFS_PER_GRANT_FRAME (PAGE_SIZE / sizeof(struct grant_entry_v1))
  52. static grant_ref_t **gnttab_list;
  53. static unsigned int nr_grant_frames;
  54. static unsigned int boot_max_nr_grant_frames;
  55. static int gnttab_free_count;
  56. static grant_ref_t gnttab_free_head;
  57. static DEFINE_SPINLOCK(gnttab_list_lock);
  58. unsigned long xen_hvm_resume_frames;
  59. EXPORT_SYMBOL_GPL(xen_hvm_resume_frames);
  60. static union {
  61. struct grant_entry_v1 *v1;
  62. void *addr;
  63. } gnttab_shared;
  64. /*This is a structure of function pointers for grant table*/
  65. struct gnttab_ops {
  66. /*
  67. * Mapping a list of frames for storing grant entries. First input
  68. * parameter is used to storing grant table address when grant table
  69. * being setup, second parameter is the number of frames to map grant
  70. * table. Returning GNTST_okay means success and negative value means
  71. * failure.
  72. */
  73. int (*map_frames)(unsigned long *, unsigned int);
  74. /*
  75. * Release a list of frames which are mapped in map_frames for grant
  76. * entry status.
  77. */
  78. void (*unmap_frames)(void);
  79. /*
  80. * Introducing a valid entry into the grant table, granting the frame
  81. * of this grant entry to domain for accessing, or transfering, or
  82. * transitively accessing. First input parameter is reference of this
  83. * introduced grant entry, second one is domid of granted domain, third
  84. * one is the frame to be granted, and the last one is status of the
  85. * grant entry to be updated.
  86. */
  87. void (*update_entry)(grant_ref_t, domid_t, unsigned long, unsigned);
  88. /*
  89. * Stop granting a grant entry to domain for accessing. First input
  90. * parameter is reference of a grant entry whose grant access will be
  91. * stopped, second one is not in use now. If the grant entry is
  92. * currently mapped for reading or writing, just return failure(==0)
  93. * directly and don't tear down the grant access. Otherwise, stop grant
  94. * access for this entry and return success(==1).
  95. */
  96. int (*end_foreign_access_ref)(grant_ref_t, int);
  97. /*
  98. * Stop granting a grant entry to domain for transfer. If tranfer has
  99. * not started, just reclaim the grant entry and return failure(==0).
  100. * Otherwise, wait for the transfer to complete and then return the
  101. * frame.
  102. */
  103. unsigned long (*end_foreign_transfer_ref)(grant_ref_t);
  104. /*
  105. * Query the status of a grant entry. Input parameter is reference of
  106. * queried grant entry, return value is the status of queried entry.
  107. * Detailed status(writing/reading) can be gotten from the return value
  108. * by bit operations.
  109. */
  110. int (*query_foreign_access)(grant_ref_t);
  111. };
  112. static struct gnttab_ops *gnttab_interface;
  113. static int grant_table_version;
  114. static struct gnttab_free_callback *gnttab_free_callback_list;
  115. static int gnttab_expand(unsigned int req_entries);
  116. #define RPP (PAGE_SIZE / sizeof(grant_ref_t))
  117. static inline grant_ref_t *__gnttab_entry(grant_ref_t entry)
  118. {
  119. return &gnttab_list[(entry) / RPP][(entry) % RPP];
  120. }
  121. /* This can be used as an l-value */
  122. #define gnttab_entry(entry) (*__gnttab_entry(entry))
  123. static int get_free_entries(unsigned count)
  124. {
  125. unsigned long flags;
  126. int ref, rc = 0;
  127. grant_ref_t head;
  128. spin_lock_irqsave(&gnttab_list_lock, flags);
  129. if ((gnttab_free_count < count) &&
  130. ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) {
  131. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  132. return rc;
  133. }
  134. ref = head = gnttab_free_head;
  135. gnttab_free_count -= count;
  136. while (count-- > 1)
  137. head = gnttab_entry(head);
  138. gnttab_free_head = gnttab_entry(head);
  139. gnttab_entry(head) = GNTTAB_LIST_END;
  140. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  141. return ref;
  142. }
  143. static void do_free_callbacks(void)
  144. {
  145. struct gnttab_free_callback *callback, *next;
  146. callback = gnttab_free_callback_list;
  147. gnttab_free_callback_list = NULL;
  148. while (callback != NULL) {
  149. next = callback->next;
  150. if (gnttab_free_count >= callback->count) {
  151. callback->next = NULL;
  152. callback->fn(callback->arg);
  153. } else {
  154. callback->next = gnttab_free_callback_list;
  155. gnttab_free_callback_list = callback;
  156. }
  157. callback = next;
  158. }
  159. }
  160. static inline void check_free_callbacks(void)
  161. {
  162. if (unlikely(gnttab_free_callback_list))
  163. do_free_callbacks();
  164. }
  165. static void put_free_entry(grant_ref_t ref)
  166. {
  167. unsigned long flags;
  168. spin_lock_irqsave(&gnttab_list_lock, flags);
  169. gnttab_entry(ref) = gnttab_free_head;
  170. gnttab_free_head = ref;
  171. gnttab_free_count++;
  172. check_free_callbacks();
  173. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  174. }
  175. /*
  176. * Introducing a valid entry into the grant table:
  177. * 1. Write ent->domid.
  178. * 2. Write ent->frame:
  179. * GTF_permit_access: Frame to which access is permitted.
  180. * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
  181. * frame, or zero if none.
  182. * 3. Write memory barrier (WMB).
  183. * 4. Write ent->flags, inc. valid type.
  184. */
  185. static void gnttab_update_entry_v1(grant_ref_t ref, domid_t domid,
  186. unsigned long frame, unsigned flags)
  187. {
  188. gnttab_shared.v1[ref].domid = domid;
  189. gnttab_shared.v1[ref].frame = frame;
  190. wmb();
  191. gnttab_shared.v1[ref].flags = flags;
  192. }
  193. /*
  194. * Public grant-issuing interface functions
  195. */
  196. void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
  197. unsigned long frame, int readonly)
  198. {
  199. gnttab_interface->update_entry(ref, domid, frame,
  200. GTF_permit_access | (readonly ? GTF_readonly : 0));
  201. }
  202. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref);
  203. int gnttab_grant_foreign_access(domid_t domid, unsigned long frame,
  204. int readonly)
  205. {
  206. int ref;
  207. ref = get_free_entries(1);
  208. if (unlikely(ref < 0))
  209. return -ENOSPC;
  210. gnttab_grant_foreign_access_ref(ref, domid, frame, readonly);
  211. return ref;
  212. }
  213. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
  214. static int gnttab_query_foreign_access_v1(grant_ref_t ref)
  215. {
  216. return gnttab_shared.v1[ref].flags & (GTF_reading|GTF_writing);
  217. }
  218. int gnttab_query_foreign_access(grant_ref_t ref)
  219. {
  220. return gnttab_interface->query_foreign_access(ref);
  221. }
  222. EXPORT_SYMBOL_GPL(gnttab_query_foreign_access);
  223. static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref, int readonly)
  224. {
  225. u16 flags, nflags;
  226. u16 *pflags;
  227. pflags = &gnttab_shared.v1[ref].flags;
  228. nflags = *pflags;
  229. do {
  230. flags = nflags;
  231. if (flags & (GTF_reading|GTF_writing)) {
  232. printk(KERN_ALERT "WARNING: g.e. still in use!\n");
  233. return 0;
  234. }
  235. } while ((nflags = sync_cmpxchg(pflags, flags, 0)) != flags);
  236. return 1;
  237. }
  238. int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
  239. {
  240. return gnttab_interface->end_foreign_access_ref(ref, readonly);
  241. }
  242. EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref);
  243. void gnttab_end_foreign_access(grant_ref_t ref, int readonly,
  244. unsigned long page)
  245. {
  246. if (gnttab_end_foreign_access_ref(ref, readonly)) {
  247. put_free_entry(ref);
  248. if (page != 0)
  249. free_page(page);
  250. } else {
  251. /* XXX This needs to be fixed so that the ref and page are
  252. placed on a list to be freed up later. */
  253. printk(KERN_WARNING
  254. "WARNING: leaking g.e. and page still in use!\n");
  255. }
  256. }
  257. EXPORT_SYMBOL_GPL(gnttab_end_foreign_access);
  258. int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn)
  259. {
  260. int ref;
  261. ref = get_free_entries(1);
  262. if (unlikely(ref < 0))
  263. return -ENOSPC;
  264. gnttab_grant_foreign_transfer_ref(ref, domid, pfn);
  265. return ref;
  266. }
  267. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer);
  268. void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid,
  269. unsigned long pfn)
  270. {
  271. gnttab_interface->update_entry(ref, domid, pfn, GTF_accept_transfer);
  272. }
  273. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref);
  274. static unsigned long gnttab_end_foreign_transfer_ref_v1(grant_ref_t ref)
  275. {
  276. unsigned long frame;
  277. u16 flags;
  278. u16 *pflags;
  279. pflags = &gnttab_shared.v1[ref].flags;
  280. /*
  281. * If a transfer is not even yet started, try to reclaim the grant
  282. * reference and return failure (== 0).
  283. */
  284. while (!((flags = *pflags) & GTF_transfer_committed)) {
  285. if (sync_cmpxchg(pflags, flags, 0) == flags)
  286. return 0;
  287. cpu_relax();
  288. }
  289. /* If a transfer is in progress then wait until it is completed. */
  290. while (!(flags & GTF_transfer_completed)) {
  291. flags = *pflags;
  292. cpu_relax();
  293. }
  294. rmb(); /* Read the frame number /after/ reading completion status. */
  295. frame = gnttab_shared.v1[ref].frame;
  296. BUG_ON(frame == 0);
  297. return frame;
  298. }
  299. unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref)
  300. {
  301. return gnttab_interface->end_foreign_transfer_ref(ref);
  302. }
  303. EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref);
  304. unsigned long gnttab_end_foreign_transfer(grant_ref_t ref)
  305. {
  306. unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
  307. put_free_entry(ref);
  308. return frame;
  309. }
  310. EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer);
  311. void gnttab_free_grant_reference(grant_ref_t ref)
  312. {
  313. put_free_entry(ref);
  314. }
  315. EXPORT_SYMBOL_GPL(gnttab_free_grant_reference);
  316. void gnttab_free_grant_references(grant_ref_t head)
  317. {
  318. grant_ref_t ref;
  319. unsigned long flags;
  320. int count = 1;
  321. if (head == GNTTAB_LIST_END)
  322. return;
  323. spin_lock_irqsave(&gnttab_list_lock, flags);
  324. ref = head;
  325. while (gnttab_entry(ref) != GNTTAB_LIST_END) {
  326. ref = gnttab_entry(ref);
  327. count++;
  328. }
  329. gnttab_entry(ref) = gnttab_free_head;
  330. gnttab_free_head = head;
  331. gnttab_free_count += count;
  332. check_free_callbacks();
  333. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  334. }
  335. EXPORT_SYMBOL_GPL(gnttab_free_grant_references);
  336. int gnttab_alloc_grant_references(u16 count, grant_ref_t *head)
  337. {
  338. int h = get_free_entries(count);
  339. if (h < 0)
  340. return -ENOSPC;
  341. *head = h;
  342. return 0;
  343. }
  344. EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references);
  345. int gnttab_empty_grant_references(const grant_ref_t *private_head)
  346. {
  347. return (*private_head == GNTTAB_LIST_END);
  348. }
  349. EXPORT_SYMBOL_GPL(gnttab_empty_grant_references);
  350. int gnttab_claim_grant_reference(grant_ref_t *private_head)
  351. {
  352. grant_ref_t g = *private_head;
  353. if (unlikely(g == GNTTAB_LIST_END))
  354. return -ENOSPC;
  355. *private_head = gnttab_entry(g);
  356. return g;
  357. }
  358. EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference);
  359. void gnttab_release_grant_reference(grant_ref_t *private_head,
  360. grant_ref_t release)
  361. {
  362. gnttab_entry(release) = *private_head;
  363. *private_head = release;
  364. }
  365. EXPORT_SYMBOL_GPL(gnttab_release_grant_reference);
  366. void gnttab_request_free_callback(struct gnttab_free_callback *callback,
  367. void (*fn)(void *), void *arg, u16 count)
  368. {
  369. unsigned long flags;
  370. spin_lock_irqsave(&gnttab_list_lock, flags);
  371. if (callback->next)
  372. goto out;
  373. callback->fn = fn;
  374. callback->arg = arg;
  375. callback->count = count;
  376. callback->next = gnttab_free_callback_list;
  377. gnttab_free_callback_list = callback;
  378. check_free_callbacks();
  379. out:
  380. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  381. }
  382. EXPORT_SYMBOL_GPL(gnttab_request_free_callback);
  383. void gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
  384. {
  385. struct gnttab_free_callback **pcb;
  386. unsigned long flags;
  387. spin_lock_irqsave(&gnttab_list_lock, flags);
  388. for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
  389. if (*pcb == callback) {
  390. *pcb = callback->next;
  391. break;
  392. }
  393. }
  394. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  395. }
  396. EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback);
  397. static int grow_gnttab_list(unsigned int more_frames)
  398. {
  399. unsigned int new_nr_grant_frames, extra_entries, i;
  400. unsigned int nr_glist_frames, new_nr_glist_frames;
  401. new_nr_grant_frames = nr_grant_frames + more_frames;
  402. extra_entries = more_frames * GREFS_PER_GRANT_FRAME;
  403. nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
  404. new_nr_glist_frames =
  405. (new_nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
  406. for (i = nr_glist_frames; i < new_nr_glist_frames; i++) {
  407. gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC);
  408. if (!gnttab_list[i])
  409. goto grow_nomem;
  410. }
  411. for (i = GREFS_PER_GRANT_FRAME * nr_grant_frames;
  412. i < GREFS_PER_GRANT_FRAME * new_nr_grant_frames - 1; i++)
  413. gnttab_entry(i) = i + 1;
  414. gnttab_entry(i) = gnttab_free_head;
  415. gnttab_free_head = GREFS_PER_GRANT_FRAME * nr_grant_frames;
  416. gnttab_free_count += extra_entries;
  417. nr_grant_frames = new_nr_grant_frames;
  418. check_free_callbacks();
  419. return 0;
  420. grow_nomem:
  421. for ( ; i >= nr_glist_frames; i--)
  422. free_page((unsigned long) gnttab_list[i]);
  423. return -ENOMEM;
  424. }
  425. static unsigned int __max_nr_grant_frames(void)
  426. {
  427. struct gnttab_query_size query;
  428. int rc;
  429. query.dom = DOMID_SELF;
  430. rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
  431. if ((rc < 0) || (query.status != GNTST_okay))
  432. return 4; /* Legacy max supported number of frames */
  433. return query.max_nr_frames;
  434. }
  435. unsigned int gnttab_max_grant_frames(void)
  436. {
  437. unsigned int xen_max = __max_nr_grant_frames();
  438. if (xen_max > boot_max_nr_grant_frames)
  439. return boot_max_nr_grant_frames;
  440. return xen_max;
  441. }
  442. EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
  443. int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
  444. struct gnttab_map_grant_ref *kmap_ops,
  445. struct page **pages, unsigned int count)
  446. {
  447. int i, ret;
  448. pte_t *pte;
  449. unsigned long mfn;
  450. ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count);
  451. if (ret)
  452. return ret;
  453. if (xen_feature(XENFEAT_auto_translated_physmap))
  454. return ret;
  455. for (i = 0; i < count; i++) {
  456. /* Do not add to override if the map failed. */
  457. if (map_ops[i].status)
  458. continue;
  459. if (map_ops[i].flags & GNTMAP_contains_pte) {
  460. pte = (pte_t *) (mfn_to_virt(PFN_DOWN(map_ops[i].host_addr)) +
  461. (map_ops[i].host_addr & ~PAGE_MASK));
  462. mfn = pte_mfn(*pte);
  463. } else {
  464. /* If you really wanted to do this:
  465. * mfn = PFN_DOWN(map_ops[i].dev_bus_addr);
  466. *
  467. * The reason we do not implement it is b/c on the
  468. * unmap path (gnttab_unmap_refs) we have no means of
  469. * checking whether the page is !GNTMAP_contains_pte.
  470. *
  471. * That is without some extra data-structure to carry
  472. * the struct page, bool clear_pte, and list_head next
  473. * tuples and deal with allocation/delallocation, etc.
  474. *
  475. * The users of this API set the GNTMAP_contains_pte
  476. * flag so lets just return not supported until it
  477. * becomes neccessary to implement.
  478. */
  479. return -EOPNOTSUPP;
  480. }
  481. ret = m2p_add_override(mfn, pages[i], &kmap_ops[i]);
  482. if (ret)
  483. return ret;
  484. }
  485. return ret;
  486. }
  487. EXPORT_SYMBOL_GPL(gnttab_map_refs);
  488. int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
  489. struct page **pages, unsigned int count)
  490. {
  491. int i, ret;
  492. ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
  493. if (ret)
  494. return ret;
  495. if (xen_feature(XENFEAT_auto_translated_physmap))
  496. return ret;
  497. for (i = 0; i < count; i++) {
  498. ret = m2p_remove_override(pages[i], true /* clear the PTE */);
  499. if (ret)
  500. return ret;
  501. }
  502. return ret;
  503. }
  504. EXPORT_SYMBOL_GPL(gnttab_unmap_refs);
  505. static int gnttab_map_frames_v1(unsigned long *frames, unsigned int nr_gframes)
  506. {
  507. int rc;
  508. rc = arch_gnttab_map_shared(frames, nr_gframes,
  509. gnttab_max_grant_frames(),
  510. &gnttab_shared.addr);
  511. BUG_ON(rc);
  512. return 0;
  513. }
  514. static void gnttab_unmap_frames_v1(void)
  515. {
  516. arch_gnttab_unmap_shared(gnttab_shared.addr, nr_grant_frames);
  517. }
  518. static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
  519. {
  520. struct gnttab_setup_table setup;
  521. unsigned long *frames;
  522. unsigned int nr_gframes = end_idx + 1;
  523. int rc;
  524. if (xen_hvm_domain()) {
  525. struct xen_add_to_physmap xatp;
  526. unsigned int i = end_idx;
  527. rc = 0;
  528. /*
  529. * Loop backwards, so that the first hypercall has the largest
  530. * index, ensuring that the table will grow only once.
  531. */
  532. do {
  533. xatp.domid = DOMID_SELF;
  534. xatp.idx = i;
  535. xatp.space = XENMAPSPACE_grant_table;
  536. xatp.gpfn = (xen_hvm_resume_frames >> PAGE_SHIFT) + i;
  537. rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
  538. if (rc != 0) {
  539. printk(KERN_WARNING
  540. "grant table add_to_physmap failed, err=%d\n", rc);
  541. break;
  542. }
  543. } while (i-- > start_idx);
  544. return rc;
  545. }
  546. frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC);
  547. if (!frames)
  548. return -ENOMEM;
  549. setup.dom = DOMID_SELF;
  550. setup.nr_frames = nr_gframes;
  551. set_xen_guest_handle(setup.frame_list, frames);
  552. rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
  553. if (rc == -ENOSYS) {
  554. kfree(frames);
  555. return -ENOSYS;
  556. }
  557. BUG_ON(rc || setup.status);
  558. rc = gnttab_interface->map_frames(frames, nr_gframes);
  559. kfree(frames);
  560. return rc;
  561. }
  562. static struct gnttab_ops gnttab_v1_ops = {
  563. .map_frames = gnttab_map_frames_v1,
  564. .unmap_frames = gnttab_unmap_frames_v1,
  565. .update_entry = gnttab_update_entry_v1,
  566. .end_foreign_access_ref = gnttab_end_foreign_access_ref_v1,
  567. .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v1,
  568. .query_foreign_access = gnttab_query_foreign_access_v1,
  569. };
  570. static void gnttab_request_version(void)
  571. {
  572. grant_table_version = 1;
  573. gnttab_interface = &gnttab_v1_ops;
  574. printk(KERN_INFO "Grant tables using version %d layout.\n",
  575. grant_table_version);
  576. }
  577. int gnttab_resume(void)
  578. {
  579. unsigned int max_nr_gframes;
  580. gnttab_request_version();
  581. max_nr_gframes = gnttab_max_grant_frames();
  582. if (max_nr_gframes < nr_grant_frames)
  583. return -ENOSYS;
  584. if (xen_pv_domain())
  585. return gnttab_map(0, nr_grant_frames - 1);
  586. if (gnttab_shared.addr == NULL) {
  587. gnttab_shared.addr = ioremap(xen_hvm_resume_frames,
  588. PAGE_SIZE * max_nr_gframes);
  589. if (gnttab_shared.addr == NULL) {
  590. printk(KERN_WARNING
  591. "Failed to ioremap gnttab share frames!");
  592. return -ENOMEM;
  593. }
  594. }
  595. gnttab_map(0, nr_grant_frames - 1);
  596. return 0;
  597. }
  598. int gnttab_suspend(void)
  599. {
  600. gnttab_interface->unmap_frames();
  601. return 0;
  602. }
  603. static int gnttab_expand(unsigned int req_entries)
  604. {
  605. int rc;
  606. unsigned int cur, extra;
  607. cur = nr_grant_frames;
  608. extra = ((req_entries + (GREFS_PER_GRANT_FRAME-1)) /
  609. GREFS_PER_GRANT_FRAME);
  610. if (cur + extra > gnttab_max_grant_frames())
  611. return -ENOSPC;
  612. rc = gnttab_map(cur, cur + extra - 1);
  613. if (rc == 0)
  614. rc = grow_gnttab_list(extra);
  615. return rc;
  616. }
  617. int gnttab_init(void)
  618. {
  619. int i;
  620. unsigned int max_nr_glist_frames, nr_glist_frames;
  621. unsigned int nr_init_grefs;
  622. nr_grant_frames = 1;
  623. boot_max_nr_grant_frames = __max_nr_grant_frames();
  624. /* Determine the maximum number of frames required for the
  625. * grant reference free list on the current hypervisor.
  626. */
  627. max_nr_glist_frames = (boot_max_nr_grant_frames *
  628. GREFS_PER_GRANT_FRAME / RPP);
  629. gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *),
  630. GFP_KERNEL);
  631. if (gnttab_list == NULL)
  632. return -ENOMEM;
  633. nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
  634. for (i = 0; i < nr_glist_frames; i++) {
  635. gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL);
  636. if (gnttab_list[i] == NULL)
  637. goto ini_nomem;
  638. }
  639. if (gnttab_resume() < 0)
  640. return -ENODEV;
  641. nr_init_grefs = nr_grant_frames * GREFS_PER_GRANT_FRAME;
  642. for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
  643. gnttab_entry(i) = i + 1;
  644. gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
  645. gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES;
  646. gnttab_free_head = NR_RESERVED_ENTRIES;
  647. printk("Grant table initialized\n");
  648. return 0;
  649. ini_nomem:
  650. for (i--; i >= 0; i--)
  651. free_page((unsigned long)gnttab_list[i]);
  652. kfree(gnttab_list);
  653. return -ENOMEM;
  654. }
  655. EXPORT_SYMBOL_GPL(gnttab_init);
  656. static int __devinit __gnttab_init(void)
  657. {
  658. /* Delay grant-table initialization in the PV on HVM case */
  659. if (xen_hvm_domain())
  660. return 0;
  661. if (!xen_pv_domain())
  662. return -ENODEV;
  663. return gnttab_init();
  664. }
  665. core_initcall(__gnttab_init);