grant-table.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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))
  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 struct grant_entry *shared;
  61. static struct gnttab_free_callback *gnttab_free_callback_list;
  62. static int gnttab_expand(unsigned int req_entries);
  63. #define RPP (PAGE_SIZE / sizeof(grant_ref_t))
  64. static inline grant_ref_t *__gnttab_entry(grant_ref_t entry)
  65. {
  66. return &gnttab_list[(entry) / RPP][(entry) % RPP];
  67. }
  68. /* This can be used as an l-value */
  69. #define gnttab_entry(entry) (*__gnttab_entry(entry))
  70. static int get_free_entries(unsigned count)
  71. {
  72. unsigned long flags;
  73. int ref, rc;
  74. grant_ref_t head;
  75. spin_lock_irqsave(&gnttab_list_lock, flags);
  76. if ((gnttab_free_count < count) &&
  77. ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) {
  78. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  79. return rc;
  80. }
  81. ref = head = gnttab_free_head;
  82. gnttab_free_count -= count;
  83. while (count-- > 1)
  84. head = gnttab_entry(head);
  85. gnttab_free_head = gnttab_entry(head);
  86. gnttab_entry(head) = GNTTAB_LIST_END;
  87. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  88. return ref;
  89. }
  90. static void do_free_callbacks(void)
  91. {
  92. struct gnttab_free_callback *callback, *next;
  93. callback = gnttab_free_callback_list;
  94. gnttab_free_callback_list = NULL;
  95. while (callback != NULL) {
  96. next = callback->next;
  97. if (gnttab_free_count >= callback->count) {
  98. callback->next = NULL;
  99. callback->fn(callback->arg);
  100. } else {
  101. callback->next = gnttab_free_callback_list;
  102. gnttab_free_callback_list = callback;
  103. }
  104. callback = next;
  105. }
  106. }
  107. static inline void check_free_callbacks(void)
  108. {
  109. if (unlikely(gnttab_free_callback_list))
  110. do_free_callbacks();
  111. }
  112. static void put_free_entry(grant_ref_t ref)
  113. {
  114. unsigned long flags;
  115. spin_lock_irqsave(&gnttab_list_lock, flags);
  116. gnttab_entry(ref) = gnttab_free_head;
  117. gnttab_free_head = ref;
  118. gnttab_free_count++;
  119. check_free_callbacks();
  120. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  121. }
  122. static void update_grant_entry(grant_ref_t ref, domid_t domid,
  123. unsigned long frame, unsigned flags)
  124. {
  125. /*
  126. * Introducing a valid entry into the grant table:
  127. * 1. Write ent->domid.
  128. * 2. Write ent->frame:
  129. * GTF_permit_access: Frame to which access is permitted.
  130. * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
  131. * frame, or zero if none.
  132. * 3. Write memory barrier (WMB).
  133. * 4. Write ent->flags, inc. valid type.
  134. */
  135. shared[ref].frame = frame;
  136. shared[ref].domid = domid;
  137. wmb();
  138. shared[ref].flags = flags;
  139. }
  140. /*
  141. * Public grant-issuing interface functions
  142. */
  143. void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
  144. unsigned long frame, int readonly)
  145. {
  146. update_grant_entry(ref, domid, frame,
  147. GTF_permit_access | (readonly ? GTF_readonly : 0));
  148. }
  149. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref);
  150. int gnttab_grant_foreign_access(domid_t domid, unsigned long frame,
  151. int readonly)
  152. {
  153. int ref;
  154. ref = get_free_entries(1);
  155. if (unlikely(ref < 0))
  156. return -ENOSPC;
  157. gnttab_grant_foreign_access_ref(ref, domid, frame, readonly);
  158. return ref;
  159. }
  160. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
  161. int gnttab_query_foreign_access(grant_ref_t ref)
  162. {
  163. u16 nflags;
  164. nflags = shared[ref].flags;
  165. return (nflags & (GTF_reading|GTF_writing));
  166. }
  167. EXPORT_SYMBOL_GPL(gnttab_query_foreign_access);
  168. int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
  169. {
  170. u16 flags, nflags;
  171. nflags = shared[ref].flags;
  172. do {
  173. flags = nflags;
  174. if (flags & (GTF_reading|GTF_writing)) {
  175. printk(KERN_ALERT "WARNING: g.e. still in use!\n");
  176. return 0;
  177. }
  178. } while ((nflags = sync_cmpxchg(&shared[ref].flags, flags, 0)) != flags);
  179. return 1;
  180. }
  181. EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref);
  182. void gnttab_end_foreign_access(grant_ref_t ref, int readonly,
  183. unsigned long page)
  184. {
  185. if (gnttab_end_foreign_access_ref(ref, readonly)) {
  186. put_free_entry(ref);
  187. if (page != 0)
  188. free_page(page);
  189. } else {
  190. /* XXX This needs to be fixed so that the ref and page are
  191. placed on a list to be freed up later. */
  192. printk(KERN_WARNING
  193. "WARNING: leaking g.e. and page still in use!\n");
  194. }
  195. }
  196. EXPORT_SYMBOL_GPL(gnttab_end_foreign_access);
  197. int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn)
  198. {
  199. int ref;
  200. ref = get_free_entries(1);
  201. if (unlikely(ref < 0))
  202. return -ENOSPC;
  203. gnttab_grant_foreign_transfer_ref(ref, domid, pfn);
  204. return ref;
  205. }
  206. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer);
  207. void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid,
  208. unsigned long pfn)
  209. {
  210. update_grant_entry(ref, domid, pfn, GTF_accept_transfer);
  211. }
  212. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref);
  213. unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref)
  214. {
  215. unsigned long frame;
  216. u16 flags;
  217. /*
  218. * If a transfer is not even yet started, try to reclaim the grant
  219. * reference and return failure (== 0).
  220. */
  221. while (!((flags = shared[ref].flags) & GTF_transfer_committed)) {
  222. if (sync_cmpxchg(&shared[ref].flags, flags, 0) == flags)
  223. return 0;
  224. cpu_relax();
  225. }
  226. /* If a transfer is in progress then wait until it is completed. */
  227. while (!(flags & GTF_transfer_completed)) {
  228. flags = shared[ref].flags;
  229. cpu_relax();
  230. }
  231. rmb(); /* Read the frame number /after/ reading completion status. */
  232. frame = shared[ref].frame;
  233. BUG_ON(frame == 0);
  234. return frame;
  235. }
  236. EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref);
  237. unsigned long gnttab_end_foreign_transfer(grant_ref_t ref)
  238. {
  239. unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
  240. put_free_entry(ref);
  241. return frame;
  242. }
  243. EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer);
  244. void gnttab_free_grant_reference(grant_ref_t ref)
  245. {
  246. put_free_entry(ref);
  247. }
  248. EXPORT_SYMBOL_GPL(gnttab_free_grant_reference);
  249. void gnttab_free_grant_references(grant_ref_t head)
  250. {
  251. grant_ref_t ref;
  252. unsigned long flags;
  253. int count = 1;
  254. if (head == GNTTAB_LIST_END)
  255. return;
  256. spin_lock_irqsave(&gnttab_list_lock, flags);
  257. ref = head;
  258. while (gnttab_entry(ref) != GNTTAB_LIST_END) {
  259. ref = gnttab_entry(ref);
  260. count++;
  261. }
  262. gnttab_entry(ref) = gnttab_free_head;
  263. gnttab_free_head = head;
  264. gnttab_free_count += count;
  265. check_free_callbacks();
  266. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  267. }
  268. EXPORT_SYMBOL_GPL(gnttab_free_grant_references);
  269. int gnttab_alloc_grant_references(u16 count, grant_ref_t *head)
  270. {
  271. int h = get_free_entries(count);
  272. if (h < 0)
  273. return -ENOSPC;
  274. *head = h;
  275. return 0;
  276. }
  277. EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references);
  278. int gnttab_empty_grant_references(const grant_ref_t *private_head)
  279. {
  280. return (*private_head == GNTTAB_LIST_END);
  281. }
  282. EXPORT_SYMBOL_GPL(gnttab_empty_grant_references);
  283. int gnttab_claim_grant_reference(grant_ref_t *private_head)
  284. {
  285. grant_ref_t g = *private_head;
  286. if (unlikely(g == GNTTAB_LIST_END))
  287. return -ENOSPC;
  288. *private_head = gnttab_entry(g);
  289. return g;
  290. }
  291. EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference);
  292. void gnttab_release_grant_reference(grant_ref_t *private_head,
  293. grant_ref_t release)
  294. {
  295. gnttab_entry(release) = *private_head;
  296. *private_head = release;
  297. }
  298. EXPORT_SYMBOL_GPL(gnttab_release_grant_reference);
  299. void gnttab_request_free_callback(struct gnttab_free_callback *callback,
  300. void (*fn)(void *), void *arg, u16 count)
  301. {
  302. unsigned long flags;
  303. spin_lock_irqsave(&gnttab_list_lock, flags);
  304. if (callback->next)
  305. goto out;
  306. callback->fn = fn;
  307. callback->arg = arg;
  308. callback->count = count;
  309. callback->next = gnttab_free_callback_list;
  310. gnttab_free_callback_list = callback;
  311. check_free_callbacks();
  312. out:
  313. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  314. }
  315. EXPORT_SYMBOL_GPL(gnttab_request_free_callback);
  316. void gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
  317. {
  318. struct gnttab_free_callback **pcb;
  319. unsigned long flags;
  320. spin_lock_irqsave(&gnttab_list_lock, flags);
  321. for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
  322. if (*pcb == callback) {
  323. *pcb = callback->next;
  324. break;
  325. }
  326. }
  327. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  328. }
  329. EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback);
  330. static int grow_gnttab_list(unsigned int more_frames)
  331. {
  332. unsigned int new_nr_grant_frames, extra_entries, i;
  333. unsigned int nr_glist_frames, new_nr_glist_frames;
  334. new_nr_grant_frames = nr_grant_frames + more_frames;
  335. extra_entries = more_frames * GREFS_PER_GRANT_FRAME;
  336. nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
  337. new_nr_glist_frames =
  338. (new_nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
  339. for (i = nr_glist_frames; i < new_nr_glist_frames; i++) {
  340. gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC);
  341. if (!gnttab_list[i])
  342. goto grow_nomem;
  343. }
  344. for (i = GREFS_PER_GRANT_FRAME * nr_grant_frames;
  345. i < GREFS_PER_GRANT_FRAME * new_nr_grant_frames - 1; i++)
  346. gnttab_entry(i) = i + 1;
  347. gnttab_entry(i) = gnttab_free_head;
  348. gnttab_free_head = GREFS_PER_GRANT_FRAME * nr_grant_frames;
  349. gnttab_free_count += extra_entries;
  350. nr_grant_frames = new_nr_grant_frames;
  351. check_free_callbacks();
  352. return 0;
  353. grow_nomem:
  354. for ( ; i >= nr_glist_frames; i--)
  355. free_page((unsigned long) gnttab_list[i]);
  356. return -ENOMEM;
  357. }
  358. static unsigned int __max_nr_grant_frames(void)
  359. {
  360. struct gnttab_query_size query;
  361. int rc;
  362. query.dom = DOMID_SELF;
  363. rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
  364. if ((rc < 0) || (query.status != GNTST_okay))
  365. return 4; /* Legacy max supported number of frames */
  366. return query.max_nr_frames;
  367. }
  368. unsigned int gnttab_max_grant_frames(void)
  369. {
  370. unsigned int xen_max = __max_nr_grant_frames();
  371. if (xen_max > boot_max_nr_grant_frames)
  372. return boot_max_nr_grant_frames;
  373. return xen_max;
  374. }
  375. EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
  376. int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
  377. struct page **pages, unsigned int count)
  378. {
  379. int i, ret;
  380. pte_t *pte;
  381. unsigned long mfn;
  382. ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count);
  383. if (ret)
  384. return ret;
  385. if (xen_feature(XENFEAT_auto_translated_physmap))
  386. return ret;
  387. for (i = 0; i < count; i++) {
  388. /* Do not add to override if the map failed. */
  389. if (map_ops[i].status)
  390. continue;
  391. if (map_ops[i].flags & GNTMAP_contains_pte) {
  392. pte = (pte_t *) (mfn_to_virt(PFN_DOWN(map_ops[i].host_addr)) +
  393. (map_ops[i].host_addr & ~PAGE_MASK));
  394. mfn = pte_mfn(*pte);
  395. } else {
  396. /* If you really wanted to do this:
  397. * mfn = PFN_DOWN(map_ops[i].dev_bus_addr);
  398. *
  399. * The reason we do not implement it is b/c on the
  400. * unmap path (gnttab_unmap_refs) we have no means of
  401. * checking whether the page is !GNTMAP_contains_pte.
  402. *
  403. * That is without some extra data-structure to carry
  404. * the struct page, bool clear_pte, and list_head next
  405. * tuples and deal with allocation/delallocation, etc.
  406. *
  407. * The users of this API set the GNTMAP_contains_pte
  408. * flag so lets just return not supported until it
  409. * becomes neccessary to implement.
  410. */
  411. return -EOPNOTSUPP;
  412. }
  413. ret = m2p_add_override(mfn, pages[i],
  414. map_ops[i].flags & GNTMAP_contains_pte);
  415. if (ret)
  416. return ret;
  417. }
  418. return ret;
  419. }
  420. EXPORT_SYMBOL_GPL(gnttab_map_refs);
  421. int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
  422. struct page **pages, unsigned int count)
  423. {
  424. int i, ret;
  425. ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
  426. if (ret)
  427. return ret;
  428. if (xen_feature(XENFEAT_auto_translated_physmap))
  429. return ret;
  430. for (i = 0; i < count; i++) {
  431. ret = m2p_remove_override(pages[i], true /* clear the PTE */);
  432. if (ret)
  433. return ret;
  434. }
  435. return ret;
  436. }
  437. EXPORT_SYMBOL_GPL(gnttab_unmap_refs);
  438. static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
  439. {
  440. struct gnttab_setup_table setup;
  441. unsigned long *frames;
  442. unsigned int nr_gframes = end_idx + 1;
  443. int rc;
  444. if (xen_hvm_domain()) {
  445. struct xen_add_to_physmap xatp;
  446. unsigned int i = end_idx;
  447. rc = 0;
  448. /*
  449. * Loop backwards, so that the first hypercall has the largest
  450. * index, ensuring that the table will grow only once.
  451. */
  452. do {
  453. xatp.domid = DOMID_SELF;
  454. xatp.idx = i;
  455. xatp.space = XENMAPSPACE_grant_table;
  456. xatp.gpfn = (xen_hvm_resume_frames >> PAGE_SHIFT) + i;
  457. rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
  458. if (rc != 0) {
  459. printk(KERN_WARNING
  460. "grant table add_to_physmap failed, err=%d\n", rc);
  461. break;
  462. }
  463. } while (i-- > start_idx);
  464. return rc;
  465. }
  466. frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC);
  467. if (!frames)
  468. return -ENOMEM;
  469. setup.dom = DOMID_SELF;
  470. setup.nr_frames = nr_gframes;
  471. set_xen_guest_handle(setup.frame_list, frames);
  472. rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
  473. if (rc == -ENOSYS) {
  474. kfree(frames);
  475. return -ENOSYS;
  476. }
  477. BUG_ON(rc || setup.status);
  478. rc = arch_gnttab_map_shared(frames, nr_gframes, gnttab_max_grant_frames(),
  479. &shared);
  480. BUG_ON(rc);
  481. kfree(frames);
  482. return 0;
  483. }
  484. int gnttab_resume(void)
  485. {
  486. unsigned int max_nr_gframes;
  487. max_nr_gframes = gnttab_max_grant_frames();
  488. if (max_nr_gframes < nr_grant_frames)
  489. return -ENOSYS;
  490. if (xen_pv_domain())
  491. return gnttab_map(0, nr_grant_frames - 1);
  492. if (!shared) {
  493. shared = ioremap(xen_hvm_resume_frames, PAGE_SIZE * max_nr_gframes);
  494. if (shared == NULL) {
  495. printk(KERN_WARNING
  496. "Failed to ioremap gnttab share frames!");
  497. return -ENOMEM;
  498. }
  499. }
  500. gnttab_map(0, nr_grant_frames - 1);
  501. return 0;
  502. }
  503. int gnttab_suspend(void)
  504. {
  505. arch_gnttab_unmap_shared(shared, nr_grant_frames);
  506. return 0;
  507. }
  508. static int gnttab_expand(unsigned int req_entries)
  509. {
  510. int rc;
  511. unsigned int cur, extra;
  512. cur = nr_grant_frames;
  513. extra = ((req_entries + (GREFS_PER_GRANT_FRAME-1)) /
  514. GREFS_PER_GRANT_FRAME);
  515. if (cur + extra > gnttab_max_grant_frames())
  516. return -ENOSPC;
  517. rc = gnttab_map(cur, cur + extra - 1);
  518. if (rc == 0)
  519. rc = grow_gnttab_list(extra);
  520. return rc;
  521. }
  522. int gnttab_init(void)
  523. {
  524. int i;
  525. unsigned int max_nr_glist_frames, nr_glist_frames;
  526. unsigned int nr_init_grefs;
  527. nr_grant_frames = 1;
  528. boot_max_nr_grant_frames = __max_nr_grant_frames();
  529. /* Determine the maximum number of frames required for the
  530. * grant reference free list on the current hypervisor.
  531. */
  532. max_nr_glist_frames = (boot_max_nr_grant_frames *
  533. GREFS_PER_GRANT_FRAME / RPP);
  534. gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *),
  535. GFP_KERNEL);
  536. if (gnttab_list == NULL)
  537. return -ENOMEM;
  538. nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
  539. for (i = 0; i < nr_glist_frames; i++) {
  540. gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL);
  541. if (gnttab_list[i] == NULL)
  542. goto ini_nomem;
  543. }
  544. if (gnttab_resume() < 0)
  545. return -ENODEV;
  546. nr_init_grefs = nr_grant_frames * GREFS_PER_GRANT_FRAME;
  547. for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
  548. gnttab_entry(i) = i + 1;
  549. gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
  550. gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES;
  551. gnttab_free_head = NR_RESERVED_ENTRIES;
  552. printk("Grant table initialized\n");
  553. return 0;
  554. ini_nomem:
  555. for (i--; i >= 0; i--)
  556. free_page((unsigned long)gnttab_list[i]);
  557. kfree(gnttab_list);
  558. return -ENOMEM;
  559. }
  560. EXPORT_SYMBOL_GPL(gnttab_init);
  561. static int __devinit __gnttab_init(void)
  562. {
  563. /* Delay grant-table initialization in the PV on HVM case */
  564. if (xen_hvm_domain())
  565. return 0;
  566. if (!xen_pv_domain())
  567. return -ENODEV;
  568. return gnttab_init();
  569. }
  570. core_initcall(__gnttab_init);