grant-table.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  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. #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  34. #include <linux/bootmem.h>
  35. #include <linux/sched.h>
  36. #include <linux/mm.h>
  37. #include <linux/slab.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/uaccess.h>
  40. #include <linux/io.h>
  41. #include <linux/delay.h>
  42. #include <linux/hardirq.h>
  43. #include <linux/workqueue.h>
  44. #include <linux/ratelimit.h>
  45. #include <linux/moduleparam.h>
  46. #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
  47. #include <linux/dma-mapping.h>
  48. #endif
  49. #include <xen/xen.h>
  50. #include <xen/interface/xen.h>
  51. #include <xen/page.h>
  52. #include <xen/grant_table.h>
  53. #include <xen/interface/memory.h>
  54. #include <xen/hvc-console.h>
  55. #include <xen/swiotlb-xen.h>
  56. #include <xen/balloon.h>
  57. #ifdef CONFIG_X86
  58. #include <asm/xen/cpuid.h>
  59. #endif
  60. #include <xen/mem-reservation.h>
  61. #include <asm/xen/hypercall.h>
  62. #include <asm/xen/interface.h>
  63. #include <asm/pgtable.h>
  64. #include <asm/sync_bitops.h>
  65. /* External tools reserve first few grant table entries. */
  66. #define NR_RESERVED_ENTRIES 8
  67. #define GNTTAB_LIST_END 0xffffffff
  68. static grant_ref_t **gnttab_list;
  69. static unsigned int nr_grant_frames;
  70. static int gnttab_free_count;
  71. static grant_ref_t gnttab_free_head;
  72. static DEFINE_SPINLOCK(gnttab_list_lock);
  73. struct grant_frames xen_auto_xlat_grant_frames;
  74. static unsigned int xen_gnttab_version;
  75. module_param_named(version, xen_gnttab_version, uint, 0);
  76. static union {
  77. struct grant_entry_v1 *v1;
  78. union grant_entry_v2 *v2;
  79. void *addr;
  80. } gnttab_shared;
  81. /*This is a structure of function pointers for grant table*/
  82. struct gnttab_ops {
  83. /*
  84. * Version of the grant interface.
  85. */
  86. unsigned int version;
  87. /*
  88. * Grant refs per grant frame.
  89. */
  90. unsigned int grefs_per_grant_frame;
  91. /*
  92. * Mapping a list of frames for storing grant entries. Frames parameter
  93. * is used to store grant table address when grant table being setup,
  94. * nr_gframes is the number of frames to map grant table. Returning
  95. * GNTST_okay means success and negative value means failure.
  96. */
  97. int (*map_frames)(xen_pfn_t *frames, unsigned int nr_gframes);
  98. /*
  99. * Release a list of frames which are mapped in map_frames for grant
  100. * entry status.
  101. */
  102. void (*unmap_frames)(void);
  103. /*
  104. * Introducing a valid entry into the grant table, granting the frame of
  105. * this grant entry to domain for accessing or transfering. Ref
  106. * parameter is reference of this introduced grant entry, domid is id of
  107. * granted domain, frame is the page frame to be granted, and flags is
  108. * status of the grant entry to be updated.
  109. */
  110. void (*update_entry)(grant_ref_t ref, domid_t domid,
  111. unsigned long frame, unsigned flags);
  112. /*
  113. * Stop granting a grant entry to domain for accessing. Ref parameter is
  114. * reference of a grant entry whose grant access will be stopped,
  115. * readonly is not in use in this function. If the grant entry is
  116. * currently mapped for reading or writing, just return failure(==0)
  117. * directly and don't tear down the grant access. Otherwise, stop grant
  118. * access for this entry and return success(==1).
  119. */
  120. int (*end_foreign_access_ref)(grant_ref_t ref, int readonly);
  121. /*
  122. * Stop granting a grant entry to domain for transfer. Ref parameter is
  123. * reference of a grant entry whose grant transfer will be stopped. If
  124. * tranfer has not started, just reclaim the grant entry and return
  125. * failure(==0). Otherwise, wait for the transfer to complete and then
  126. * return the frame.
  127. */
  128. unsigned long (*end_foreign_transfer_ref)(grant_ref_t ref);
  129. /*
  130. * Query the status of a grant entry. Ref parameter is reference of
  131. * queried grant entry, return value is the status of queried entry.
  132. * Detailed status(writing/reading) can be gotten from the return value
  133. * by bit operations.
  134. */
  135. int (*query_foreign_access)(grant_ref_t ref);
  136. };
  137. struct unmap_refs_callback_data {
  138. struct completion completion;
  139. int result;
  140. };
  141. static const struct gnttab_ops *gnttab_interface;
  142. /* This reflects status of grant entries, so act as a global value. */
  143. static grant_status_t *grstatus;
  144. static struct gnttab_free_callback *gnttab_free_callback_list;
  145. static int gnttab_expand(unsigned int req_entries);
  146. #define RPP (PAGE_SIZE / sizeof(grant_ref_t))
  147. #define SPP (PAGE_SIZE / sizeof(grant_status_t))
  148. static inline grant_ref_t *__gnttab_entry(grant_ref_t entry)
  149. {
  150. return &gnttab_list[(entry) / RPP][(entry) % RPP];
  151. }
  152. /* This can be used as an l-value */
  153. #define gnttab_entry(entry) (*__gnttab_entry(entry))
  154. static int get_free_entries(unsigned count)
  155. {
  156. unsigned long flags;
  157. int ref, rc = 0;
  158. grant_ref_t head;
  159. spin_lock_irqsave(&gnttab_list_lock, flags);
  160. if ((gnttab_free_count < count) &&
  161. ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) {
  162. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  163. return rc;
  164. }
  165. ref = head = gnttab_free_head;
  166. gnttab_free_count -= count;
  167. while (count-- > 1)
  168. head = gnttab_entry(head);
  169. gnttab_free_head = gnttab_entry(head);
  170. gnttab_entry(head) = GNTTAB_LIST_END;
  171. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  172. return ref;
  173. }
  174. static void do_free_callbacks(void)
  175. {
  176. struct gnttab_free_callback *callback, *next;
  177. callback = gnttab_free_callback_list;
  178. gnttab_free_callback_list = NULL;
  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. static inline void check_free_callbacks(void)
  192. {
  193. if (unlikely(gnttab_free_callback_list))
  194. do_free_callbacks();
  195. }
  196. static void put_free_entry(grant_ref_t ref)
  197. {
  198. unsigned long flags;
  199. spin_lock_irqsave(&gnttab_list_lock, flags);
  200. gnttab_entry(ref) = gnttab_free_head;
  201. gnttab_free_head = ref;
  202. gnttab_free_count++;
  203. check_free_callbacks();
  204. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  205. }
  206. /*
  207. * Following applies to gnttab_update_entry_v1 and gnttab_update_entry_v2.
  208. * Introducing a valid entry into the grant table:
  209. * 1. Write ent->domid.
  210. * 2. Write ent->frame:
  211. * GTF_permit_access: Frame to which access is permitted.
  212. * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
  213. * frame, or zero if none.
  214. * 3. Write memory barrier (WMB).
  215. * 4. Write ent->flags, inc. valid type.
  216. */
  217. static void gnttab_update_entry_v1(grant_ref_t ref, domid_t domid,
  218. unsigned long frame, unsigned flags)
  219. {
  220. gnttab_shared.v1[ref].domid = domid;
  221. gnttab_shared.v1[ref].frame = frame;
  222. wmb();
  223. gnttab_shared.v1[ref].flags = flags;
  224. }
  225. static void gnttab_update_entry_v2(grant_ref_t ref, domid_t domid,
  226. unsigned long frame, unsigned int flags)
  227. {
  228. gnttab_shared.v2[ref].hdr.domid = domid;
  229. gnttab_shared.v2[ref].full_page.frame = frame;
  230. wmb(); /* Hypervisor concurrent accesses. */
  231. gnttab_shared.v2[ref].hdr.flags = GTF_permit_access | flags;
  232. }
  233. /*
  234. * Public grant-issuing interface functions
  235. */
  236. void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
  237. unsigned long frame, int readonly)
  238. {
  239. gnttab_interface->update_entry(ref, domid, frame,
  240. GTF_permit_access | (readonly ? GTF_readonly : 0));
  241. }
  242. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref);
  243. int gnttab_grant_foreign_access(domid_t domid, unsigned long frame,
  244. int readonly)
  245. {
  246. int ref;
  247. ref = get_free_entries(1);
  248. if (unlikely(ref < 0))
  249. return -ENOSPC;
  250. gnttab_grant_foreign_access_ref(ref, domid, frame, readonly);
  251. return ref;
  252. }
  253. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
  254. static int gnttab_query_foreign_access_v1(grant_ref_t ref)
  255. {
  256. return gnttab_shared.v1[ref].flags & (GTF_reading|GTF_writing);
  257. }
  258. static int gnttab_query_foreign_access_v2(grant_ref_t ref)
  259. {
  260. return grstatus[ref] & (GTF_reading|GTF_writing);
  261. }
  262. int gnttab_query_foreign_access(grant_ref_t ref)
  263. {
  264. return gnttab_interface->query_foreign_access(ref);
  265. }
  266. EXPORT_SYMBOL_GPL(gnttab_query_foreign_access);
  267. static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref, int readonly)
  268. {
  269. u16 flags, nflags;
  270. u16 *pflags;
  271. pflags = &gnttab_shared.v1[ref].flags;
  272. nflags = *pflags;
  273. do {
  274. flags = nflags;
  275. if (flags & (GTF_reading|GTF_writing))
  276. return 0;
  277. } while ((nflags = sync_cmpxchg(pflags, flags, 0)) != flags);
  278. return 1;
  279. }
  280. static int gnttab_end_foreign_access_ref_v2(grant_ref_t ref, int readonly)
  281. {
  282. gnttab_shared.v2[ref].hdr.flags = 0;
  283. mb(); /* Concurrent access by hypervisor. */
  284. if (grstatus[ref] & (GTF_reading|GTF_writing)) {
  285. return 0;
  286. } else {
  287. /*
  288. * The read of grstatus needs to have acquire semantics.
  289. * On x86, reads already have that, and we just need to
  290. * protect against compiler reorderings.
  291. * On other architectures we may need a full barrier.
  292. */
  293. #ifdef CONFIG_X86
  294. barrier();
  295. #else
  296. mb();
  297. #endif
  298. }
  299. return 1;
  300. }
  301. static inline int _gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
  302. {
  303. return gnttab_interface->end_foreign_access_ref(ref, readonly);
  304. }
  305. int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
  306. {
  307. if (_gnttab_end_foreign_access_ref(ref, readonly))
  308. return 1;
  309. pr_warn("WARNING: g.e. %#x still in use!\n", ref);
  310. return 0;
  311. }
  312. EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref);
  313. struct deferred_entry {
  314. struct list_head list;
  315. grant_ref_t ref;
  316. bool ro;
  317. uint16_t warn_delay;
  318. struct page *page;
  319. };
  320. static LIST_HEAD(deferred_list);
  321. static void gnttab_handle_deferred(struct timer_list *);
  322. static DEFINE_TIMER(deferred_timer, gnttab_handle_deferred);
  323. static void gnttab_handle_deferred(struct timer_list *unused)
  324. {
  325. unsigned int nr = 10;
  326. struct deferred_entry *first = NULL;
  327. unsigned long flags;
  328. spin_lock_irqsave(&gnttab_list_lock, flags);
  329. while (nr--) {
  330. struct deferred_entry *entry
  331. = list_first_entry(&deferred_list,
  332. struct deferred_entry, list);
  333. if (entry == first)
  334. break;
  335. list_del(&entry->list);
  336. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  337. if (_gnttab_end_foreign_access_ref(entry->ref, entry->ro)) {
  338. put_free_entry(entry->ref);
  339. if (entry->page) {
  340. pr_debug("freeing g.e. %#x (pfn %#lx)\n",
  341. entry->ref, page_to_pfn(entry->page));
  342. put_page(entry->page);
  343. } else
  344. pr_info("freeing g.e. %#x\n", entry->ref);
  345. kfree(entry);
  346. entry = NULL;
  347. } else {
  348. if (!--entry->warn_delay)
  349. pr_info("g.e. %#x still pending\n", entry->ref);
  350. if (!first)
  351. first = entry;
  352. }
  353. spin_lock_irqsave(&gnttab_list_lock, flags);
  354. if (entry)
  355. list_add_tail(&entry->list, &deferred_list);
  356. else if (list_empty(&deferred_list))
  357. break;
  358. }
  359. if (!list_empty(&deferred_list) && !timer_pending(&deferred_timer)) {
  360. deferred_timer.expires = jiffies + HZ;
  361. add_timer(&deferred_timer);
  362. }
  363. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  364. }
  365. static void gnttab_add_deferred(grant_ref_t ref, bool readonly,
  366. struct page *page)
  367. {
  368. struct deferred_entry *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  369. const char *what = KERN_WARNING "leaking";
  370. if (entry) {
  371. unsigned long flags;
  372. entry->ref = ref;
  373. entry->ro = readonly;
  374. entry->page = page;
  375. entry->warn_delay = 60;
  376. spin_lock_irqsave(&gnttab_list_lock, flags);
  377. list_add_tail(&entry->list, &deferred_list);
  378. if (!timer_pending(&deferred_timer)) {
  379. deferred_timer.expires = jiffies + HZ;
  380. add_timer(&deferred_timer);
  381. }
  382. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  383. what = KERN_DEBUG "deferring";
  384. }
  385. printk("%s g.e. %#x (pfn %#lx)\n",
  386. what, ref, page ? page_to_pfn(page) : -1);
  387. }
  388. void gnttab_end_foreign_access(grant_ref_t ref, int readonly,
  389. unsigned long page)
  390. {
  391. if (gnttab_end_foreign_access_ref(ref, readonly)) {
  392. put_free_entry(ref);
  393. if (page != 0)
  394. put_page(virt_to_page(page));
  395. } else
  396. gnttab_add_deferred(ref, readonly,
  397. page ? virt_to_page(page) : NULL);
  398. }
  399. EXPORT_SYMBOL_GPL(gnttab_end_foreign_access);
  400. int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn)
  401. {
  402. int ref;
  403. ref = get_free_entries(1);
  404. if (unlikely(ref < 0))
  405. return -ENOSPC;
  406. gnttab_grant_foreign_transfer_ref(ref, domid, pfn);
  407. return ref;
  408. }
  409. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer);
  410. void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid,
  411. unsigned long pfn)
  412. {
  413. gnttab_interface->update_entry(ref, domid, pfn, GTF_accept_transfer);
  414. }
  415. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref);
  416. static unsigned long gnttab_end_foreign_transfer_ref_v1(grant_ref_t ref)
  417. {
  418. unsigned long frame;
  419. u16 flags;
  420. u16 *pflags;
  421. pflags = &gnttab_shared.v1[ref].flags;
  422. /*
  423. * If a transfer is not even yet started, try to reclaim the grant
  424. * reference and return failure (== 0).
  425. */
  426. while (!((flags = *pflags) & GTF_transfer_committed)) {
  427. if (sync_cmpxchg(pflags, flags, 0) == flags)
  428. return 0;
  429. cpu_relax();
  430. }
  431. /* If a transfer is in progress then wait until it is completed. */
  432. while (!(flags & GTF_transfer_completed)) {
  433. flags = *pflags;
  434. cpu_relax();
  435. }
  436. rmb(); /* Read the frame number /after/ reading completion status. */
  437. frame = gnttab_shared.v1[ref].frame;
  438. BUG_ON(frame == 0);
  439. return frame;
  440. }
  441. static unsigned long gnttab_end_foreign_transfer_ref_v2(grant_ref_t ref)
  442. {
  443. unsigned long frame;
  444. u16 flags;
  445. u16 *pflags;
  446. pflags = &gnttab_shared.v2[ref].hdr.flags;
  447. /*
  448. * If a transfer is not even yet started, try to reclaim the grant
  449. * reference and return failure (== 0).
  450. */
  451. while (!((flags = *pflags) & GTF_transfer_committed)) {
  452. if (sync_cmpxchg(pflags, flags, 0) == flags)
  453. return 0;
  454. cpu_relax();
  455. }
  456. /* If a transfer is in progress then wait until it is completed. */
  457. while (!(flags & GTF_transfer_completed)) {
  458. flags = *pflags;
  459. cpu_relax();
  460. }
  461. rmb(); /* Read the frame number /after/ reading completion status. */
  462. frame = gnttab_shared.v2[ref].full_page.frame;
  463. BUG_ON(frame == 0);
  464. return frame;
  465. }
  466. unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref)
  467. {
  468. return gnttab_interface->end_foreign_transfer_ref(ref);
  469. }
  470. EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref);
  471. unsigned long gnttab_end_foreign_transfer(grant_ref_t ref)
  472. {
  473. unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
  474. put_free_entry(ref);
  475. return frame;
  476. }
  477. EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer);
  478. void gnttab_free_grant_reference(grant_ref_t ref)
  479. {
  480. put_free_entry(ref);
  481. }
  482. EXPORT_SYMBOL_GPL(gnttab_free_grant_reference);
  483. void gnttab_free_grant_references(grant_ref_t head)
  484. {
  485. grant_ref_t ref;
  486. unsigned long flags;
  487. int count = 1;
  488. if (head == GNTTAB_LIST_END)
  489. return;
  490. spin_lock_irqsave(&gnttab_list_lock, flags);
  491. ref = head;
  492. while (gnttab_entry(ref) != GNTTAB_LIST_END) {
  493. ref = gnttab_entry(ref);
  494. count++;
  495. }
  496. gnttab_entry(ref) = gnttab_free_head;
  497. gnttab_free_head = head;
  498. gnttab_free_count += count;
  499. check_free_callbacks();
  500. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  501. }
  502. EXPORT_SYMBOL_GPL(gnttab_free_grant_references);
  503. int gnttab_alloc_grant_references(u16 count, grant_ref_t *head)
  504. {
  505. int h = get_free_entries(count);
  506. if (h < 0)
  507. return -ENOSPC;
  508. *head = h;
  509. return 0;
  510. }
  511. EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references);
  512. int gnttab_empty_grant_references(const grant_ref_t *private_head)
  513. {
  514. return (*private_head == GNTTAB_LIST_END);
  515. }
  516. EXPORT_SYMBOL_GPL(gnttab_empty_grant_references);
  517. int gnttab_claim_grant_reference(grant_ref_t *private_head)
  518. {
  519. grant_ref_t g = *private_head;
  520. if (unlikely(g == GNTTAB_LIST_END))
  521. return -ENOSPC;
  522. *private_head = gnttab_entry(g);
  523. return g;
  524. }
  525. EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference);
  526. void gnttab_release_grant_reference(grant_ref_t *private_head,
  527. grant_ref_t release)
  528. {
  529. gnttab_entry(release) = *private_head;
  530. *private_head = release;
  531. }
  532. EXPORT_SYMBOL_GPL(gnttab_release_grant_reference);
  533. void gnttab_request_free_callback(struct gnttab_free_callback *callback,
  534. void (*fn)(void *), void *arg, u16 count)
  535. {
  536. unsigned long flags;
  537. struct gnttab_free_callback *cb;
  538. spin_lock_irqsave(&gnttab_list_lock, flags);
  539. /* Check if the callback is already on the list */
  540. cb = gnttab_free_callback_list;
  541. while (cb) {
  542. if (cb == callback)
  543. goto out;
  544. cb = cb->next;
  545. }
  546. callback->fn = fn;
  547. callback->arg = arg;
  548. callback->count = count;
  549. callback->next = gnttab_free_callback_list;
  550. gnttab_free_callback_list = callback;
  551. check_free_callbacks();
  552. out:
  553. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  554. }
  555. EXPORT_SYMBOL_GPL(gnttab_request_free_callback);
  556. void gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
  557. {
  558. struct gnttab_free_callback **pcb;
  559. unsigned long flags;
  560. spin_lock_irqsave(&gnttab_list_lock, flags);
  561. for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
  562. if (*pcb == callback) {
  563. *pcb = callback->next;
  564. break;
  565. }
  566. }
  567. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  568. }
  569. EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback);
  570. static unsigned int gnttab_frames(unsigned int frames, unsigned int align)
  571. {
  572. return (frames * gnttab_interface->grefs_per_grant_frame + align - 1) /
  573. align;
  574. }
  575. static int grow_gnttab_list(unsigned int more_frames)
  576. {
  577. unsigned int new_nr_grant_frames, extra_entries, i;
  578. unsigned int nr_glist_frames, new_nr_glist_frames;
  579. unsigned int grefs_per_frame;
  580. BUG_ON(gnttab_interface == NULL);
  581. grefs_per_frame = gnttab_interface->grefs_per_grant_frame;
  582. new_nr_grant_frames = nr_grant_frames + more_frames;
  583. extra_entries = more_frames * grefs_per_frame;
  584. nr_glist_frames = gnttab_frames(nr_grant_frames, RPP);
  585. new_nr_glist_frames = gnttab_frames(new_nr_grant_frames, RPP);
  586. for (i = nr_glist_frames; i < new_nr_glist_frames; i++) {
  587. gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC);
  588. if (!gnttab_list[i])
  589. goto grow_nomem;
  590. }
  591. for (i = grefs_per_frame * nr_grant_frames;
  592. i < grefs_per_frame * new_nr_grant_frames - 1; i++)
  593. gnttab_entry(i) = i + 1;
  594. gnttab_entry(i) = gnttab_free_head;
  595. gnttab_free_head = grefs_per_frame * nr_grant_frames;
  596. gnttab_free_count += extra_entries;
  597. nr_grant_frames = new_nr_grant_frames;
  598. check_free_callbacks();
  599. return 0;
  600. grow_nomem:
  601. while (i-- > nr_glist_frames)
  602. free_page((unsigned long) gnttab_list[i]);
  603. return -ENOMEM;
  604. }
  605. static unsigned int __max_nr_grant_frames(void)
  606. {
  607. struct gnttab_query_size query;
  608. int rc;
  609. query.dom = DOMID_SELF;
  610. rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
  611. if ((rc < 0) || (query.status != GNTST_okay))
  612. return 4; /* Legacy max supported number of frames */
  613. return query.max_nr_frames;
  614. }
  615. unsigned int gnttab_max_grant_frames(void)
  616. {
  617. unsigned int xen_max = __max_nr_grant_frames();
  618. static unsigned int boot_max_nr_grant_frames;
  619. /* First time, initialize it properly. */
  620. if (!boot_max_nr_grant_frames)
  621. boot_max_nr_grant_frames = __max_nr_grant_frames();
  622. if (xen_max > boot_max_nr_grant_frames)
  623. return boot_max_nr_grant_frames;
  624. return xen_max;
  625. }
  626. EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
  627. int gnttab_setup_auto_xlat_frames(phys_addr_t addr)
  628. {
  629. xen_pfn_t *pfn;
  630. unsigned int max_nr_gframes = __max_nr_grant_frames();
  631. unsigned int i;
  632. void *vaddr;
  633. if (xen_auto_xlat_grant_frames.count)
  634. return -EINVAL;
  635. vaddr = xen_remap(addr, XEN_PAGE_SIZE * max_nr_gframes);
  636. if (vaddr == NULL) {
  637. pr_warn("Failed to ioremap gnttab share frames (addr=%pa)!\n",
  638. &addr);
  639. return -ENOMEM;
  640. }
  641. pfn = kcalloc(max_nr_gframes, sizeof(pfn[0]), GFP_KERNEL);
  642. if (!pfn) {
  643. xen_unmap(vaddr);
  644. return -ENOMEM;
  645. }
  646. for (i = 0; i < max_nr_gframes; i++)
  647. pfn[i] = XEN_PFN_DOWN(addr) + i;
  648. xen_auto_xlat_grant_frames.vaddr = vaddr;
  649. xen_auto_xlat_grant_frames.pfn = pfn;
  650. xen_auto_xlat_grant_frames.count = max_nr_gframes;
  651. return 0;
  652. }
  653. EXPORT_SYMBOL_GPL(gnttab_setup_auto_xlat_frames);
  654. void gnttab_free_auto_xlat_frames(void)
  655. {
  656. if (!xen_auto_xlat_grant_frames.count)
  657. return;
  658. kfree(xen_auto_xlat_grant_frames.pfn);
  659. xen_unmap(xen_auto_xlat_grant_frames.vaddr);
  660. xen_auto_xlat_grant_frames.pfn = NULL;
  661. xen_auto_xlat_grant_frames.count = 0;
  662. xen_auto_xlat_grant_frames.vaddr = NULL;
  663. }
  664. EXPORT_SYMBOL_GPL(gnttab_free_auto_xlat_frames);
  665. int gnttab_pages_set_private(int nr_pages, struct page **pages)
  666. {
  667. int i;
  668. for (i = 0; i < nr_pages; i++) {
  669. #if BITS_PER_LONG < 64
  670. struct xen_page_foreign *foreign;
  671. foreign = kzalloc(sizeof(*foreign), GFP_KERNEL);
  672. if (!foreign)
  673. return -ENOMEM;
  674. set_page_private(pages[i], (unsigned long)foreign);
  675. #endif
  676. SetPagePrivate(pages[i]);
  677. }
  678. return 0;
  679. }
  680. EXPORT_SYMBOL_GPL(gnttab_pages_set_private);
  681. /**
  682. * gnttab_alloc_pages - alloc pages suitable for grant mapping into
  683. * @nr_pages: number of pages to alloc
  684. * @pages: returns the pages
  685. */
  686. int gnttab_alloc_pages(int nr_pages, struct page **pages)
  687. {
  688. int ret;
  689. ret = alloc_xenballooned_pages(nr_pages, pages);
  690. if (ret < 0)
  691. return ret;
  692. ret = gnttab_pages_set_private(nr_pages, pages);
  693. if (ret < 0)
  694. gnttab_free_pages(nr_pages, pages);
  695. return ret;
  696. }
  697. EXPORT_SYMBOL_GPL(gnttab_alloc_pages);
  698. void gnttab_pages_clear_private(int nr_pages, struct page **pages)
  699. {
  700. int i;
  701. for (i = 0; i < nr_pages; i++) {
  702. if (PagePrivate(pages[i])) {
  703. #if BITS_PER_LONG < 64
  704. kfree((void *)page_private(pages[i]));
  705. #endif
  706. ClearPagePrivate(pages[i]);
  707. }
  708. }
  709. }
  710. EXPORT_SYMBOL_GPL(gnttab_pages_clear_private);
  711. /**
  712. * gnttab_free_pages - free pages allocated by gnttab_alloc_pages()
  713. * @nr_pages; number of pages to free
  714. * @pages: the pages
  715. */
  716. void gnttab_free_pages(int nr_pages, struct page **pages)
  717. {
  718. gnttab_pages_clear_private(nr_pages, pages);
  719. free_xenballooned_pages(nr_pages, pages);
  720. }
  721. EXPORT_SYMBOL_GPL(gnttab_free_pages);
  722. #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
  723. /**
  724. * gnttab_dma_alloc_pages - alloc DMAable pages suitable for grant mapping into
  725. * @args: arguments to the function
  726. */
  727. int gnttab_dma_alloc_pages(struct gnttab_dma_alloc_args *args)
  728. {
  729. unsigned long pfn, start_pfn;
  730. size_t size;
  731. int i, ret;
  732. size = args->nr_pages << PAGE_SHIFT;
  733. if (args->coherent)
  734. args->vaddr = dma_alloc_coherent(args->dev, size,
  735. &args->dev_bus_addr,
  736. GFP_KERNEL | __GFP_NOWARN);
  737. else
  738. args->vaddr = dma_alloc_wc(args->dev, size,
  739. &args->dev_bus_addr,
  740. GFP_KERNEL | __GFP_NOWARN);
  741. if (!args->vaddr) {
  742. pr_debug("Failed to allocate DMA buffer of size %zu\n", size);
  743. return -ENOMEM;
  744. }
  745. start_pfn = __phys_to_pfn(args->dev_bus_addr);
  746. for (pfn = start_pfn, i = 0; pfn < start_pfn + args->nr_pages;
  747. pfn++, i++) {
  748. struct page *page = pfn_to_page(pfn);
  749. args->pages[i] = page;
  750. args->frames[i] = xen_page_to_gfn(page);
  751. xenmem_reservation_scrub_page(page);
  752. }
  753. xenmem_reservation_va_mapping_reset(args->nr_pages, args->pages);
  754. ret = xenmem_reservation_decrease(args->nr_pages, args->frames);
  755. if (ret != args->nr_pages) {
  756. pr_debug("Failed to decrease reservation for DMA buffer\n");
  757. ret = -EFAULT;
  758. goto fail;
  759. }
  760. ret = gnttab_pages_set_private(args->nr_pages, args->pages);
  761. if (ret < 0)
  762. goto fail;
  763. return 0;
  764. fail:
  765. gnttab_dma_free_pages(args);
  766. return ret;
  767. }
  768. EXPORT_SYMBOL_GPL(gnttab_dma_alloc_pages);
  769. /**
  770. * gnttab_dma_free_pages - free DMAable pages
  771. * @args: arguments to the function
  772. */
  773. int gnttab_dma_free_pages(struct gnttab_dma_alloc_args *args)
  774. {
  775. size_t size;
  776. int i, ret;
  777. gnttab_pages_clear_private(args->nr_pages, args->pages);
  778. for (i = 0; i < args->nr_pages; i++)
  779. args->frames[i] = page_to_xen_pfn(args->pages[i]);
  780. ret = xenmem_reservation_increase(args->nr_pages, args->frames);
  781. if (ret != args->nr_pages) {
  782. pr_debug("Failed to increase reservation for DMA buffer\n");
  783. ret = -EFAULT;
  784. } else {
  785. ret = 0;
  786. }
  787. xenmem_reservation_va_mapping_update(args->nr_pages, args->pages,
  788. args->frames);
  789. size = args->nr_pages << PAGE_SHIFT;
  790. if (args->coherent)
  791. dma_free_coherent(args->dev, size,
  792. args->vaddr, args->dev_bus_addr);
  793. else
  794. dma_free_wc(args->dev, size,
  795. args->vaddr, args->dev_bus_addr);
  796. return ret;
  797. }
  798. EXPORT_SYMBOL_GPL(gnttab_dma_free_pages);
  799. #endif
  800. /* Handling of paged out grant targets (GNTST_eagain) */
  801. #define MAX_DELAY 256
  802. static inline void
  803. gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
  804. const char *func)
  805. {
  806. unsigned delay = 1;
  807. do {
  808. BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
  809. if (*status == GNTST_eagain)
  810. msleep(delay++);
  811. } while ((*status == GNTST_eagain) && (delay < MAX_DELAY));
  812. if (delay >= MAX_DELAY) {
  813. pr_err("%s: %s eagain grant\n", func, current->comm);
  814. *status = GNTST_bad_page;
  815. }
  816. }
  817. void gnttab_batch_map(struct gnttab_map_grant_ref *batch, unsigned count)
  818. {
  819. struct gnttab_map_grant_ref *op;
  820. if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, batch, count))
  821. BUG();
  822. for (op = batch; op < batch + count; op++)
  823. if (op->status == GNTST_eagain)
  824. gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, op,
  825. &op->status, __func__);
  826. }
  827. EXPORT_SYMBOL_GPL(gnttab_batch_map);
  828. void gnttab_batch_copy(struct gnttab_copy *batch, unsigned count)
  829. {
  830. struct gnttab_copy *op;
  831. if (HYPERVISOR_grant_table_op(GNTTABOP_copy, batch, count))
  832. BUG();
  833. for (op = batch; op < batch + count; op++)
  834. if (op->status == GNTST_eagain)
  835. gnttab_retry_eagain_gop(GNTTABOP_copy, op,
  836. &op->status, __func__);
  837. }
  838. EXPORT_SYMBOL_GPL(gnttab_batch_copy);
  839. void gnttab_foreach_grant_in_range(struct page *page,
  840. unsigned int offset,
  841. unsigned int len,
  842. xen_grant_fn_t fn,
  843. void *data)
  844. {
  845. unsigned int goffset;
  846. unsigned int glen;
  847. unsigned long xen_pfn;
  848. len = min_t(unsigned int, PAGE_SIZE - offset, len);
  849. goffset = xen_offset_in_page(offset);
  850. xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(offset);
  851. while (len) {
  852. glen = min_t(unsigned int, XEN_PAGE_SIZE - goffset, len);
  853. fn(pfn_to_gfn(xen_pfn), goffset, glen, data);
  854. goffset = 0;
  855. xen_pfn++;
  856. len -= glen;
  857. }
  858. }
  859. EXPORT_SYMBOL_GPL(gnttab_foreach_grant_in_range);
  860. void gnttab_foreach_grant(struct page **pages,
  861. unsigned int nr_grefs,
  862. xen_grant_fn_t fn,
  863. void *data)
  864. {
  865. unsigned int goffset = 0;
  866. unsigned long xen_pfn = 0;
  867. unsigned int i;
  868. for (i = 0; i < nr_grefs; i++) {
  869. if ((i % XEN_PFN_PER_PAGE) == 0) {
  870. xen_pfn = page_to_xen_pfn(pages[i / XEN_PFN_PER_PAGE]);
  871. goffset = 0;
  872. }
  873. fn(pfn_to_gfn(xen_pfn), goffset, XEN_PAGE_SIZE, data);
  874. goffset += XEN_PAGE_SIZE;
  875. xen_pfn++;
  876. }
  877. }
  878. int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
  879. struct gnttab_map_grant_ref *kmap_ops,
  880. struct page **pages, unsigned int count)
  881. {
  882. int i, ret;
  883. ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count);
  884. if (ret)
  885. return ret;
  886. for (i = 0; i < count; i++) {
  887. switch (map_ops[i].status) {
  888. case GNTST_okay:
  889. {
  890. struct xen_page_foreign *foreign;
  891. SetPageForeign(pages[i]);
  892. foreign = xen_page_foreign(pages[i]);
  893. foreign->domid = map_ops[i].dom;
  894. foreign->gref = map_ops[i].ref;
  895. break;
  896. }
  897. case GNTST_no_device_space:
  898. pr_warn_ratelimited("maptrack limit reached, can't map all guest pages\n");
  899. break;
  900. case GNTST_eagain:
  901. /* Retry eagain maps */
  902. gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref,
  903. map_ops + i,
  904. &map_ops[i].status, __func__);
  905. /* Test status in next loop iteration. */
  906. i--;
  907. break;
  908. default:
  909. break;
  910. }
  911. }
  912. return set_foreign_p2m_mapping(map_ops, kmap_ops, pages, count);
  913. }
  914. EXPORT_SYMBOL_GPL(gnttab_map_refs);
  915. int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
  916. struct gnttab_unmap_grant_ref *kunmap_ops,
  917. struct page **pages, unsigned int count)
  918. {
  919. unsigned int i;
  920. int ret;
  921. ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
  922. if (ret)
  923. return ret;
  924. for (i = 0; i < count; i++)
  925. ClearPageForeign(pages[i]);
  926. return clear_foreign_p2m_mapping(unmap_ops, kunmap_ops, pages, count);
  927. }
  928. EXPORT_SYMBOL_GPL(gnttab_unmap_refs);
  929. #define GNTTAB_UNMAP_REFS_DELAY 5
  930. static void __gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item);
  931. static void gnttab_unmap_work(struct work_struct *work)
  932. {
  933. struct gntab_unmap_queue_data
  934. *unmap_data = container_of(work,
  935. struct gntab_unmap_queue_data,
  936. gnttab_work.work);
  937. if (unmap_data->age != UINT_MAX)
  938. unmap_data->age++;
  939. __gnttab_unmap_refs_async(unmap_data);
  940. }
  941. static void __gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
  942. {
  943. int ret;
  944. int pc;
  945. for (pc = 0; pc < item->count; pc++) {
  946. if (page_count(item->pages[pc]) > 1) {
  947. unsigned long delay = GNTTAB_UNMAP_REFS_DELAY * (item->age + 1);
  948. schedule_delayed_work(&item->gnttab_work,
  949. msecs_to_jiffies(delay));
  950. return;
  951. }
  952. }
  953. ret = gnttab_unmap_refs(item->unmap_ops, item->kunmap_ops,
  954. item->pages, item->count);
  955. item->done(ret, item);
  956. }
  957. void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
  958. {
  959. INIT_DELAYED_WORK(&item->gnttab_work, gnttab_unmap_work);
  960. item->age = 0;
  961. __gnttab_unmap_refs_async(item);
  962. }
  963. EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async);
  964. static void unmap_refs_callback(int result,
  965. struct gntab_unmap_queue_data *data)
  966. {
  967. struct unmap_refs_callback_data *d = data->data;
  968. d->result = result;
  969. complete(&d->completion);
  970. }
  971. int gnttab_unmap_refs_sync(struct gntab_unmap_queue_data *item)
  972. {
  973. struct unmap_refs_callback_data data;
  974. init_completion(&data.completion);
  975. item->data = &data;
  976. item->done = &unmap_refs_callback;
  977. gnttab_unmap_refs_async(item);
  978. wait_for_completion(&data.completion);
  979. return data.result;
  980. }
  981. EXPORT_SYMBOL_GPL(gnttab_unmap_refs_sync);
  982. static unsigned int nr_status_frames(unsigned int nr_grant_frames)
  983. {
  984. BUG_ON(gnttab_interface == NULL);
  985. return gnttab_frames(nr_grant_frames, SPP);
  986. }
  987. static int gnttab_map_frames_v1(xen_pfn_t *frames, unsigned int nr_gframes)
  988. {
  989. int rc;
  990. rc = arch_gnttab_map_shared(frames, nr_gframes,
  991. gnttab_max_grant_frames(),
  992. &gnttab_shared.addr);
  993. BUG_ON(rc);
  994. return 0;
  995. }
  996. static void gnttab_unmap_frames_v1(void)
  997. {
  998. arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames);
  999. }
  1000. static int gnttab_map_frames_v2(xen_pfn_t *frames, unsigned int nr_gframes)
  1001. {
  1002. uint64_t *sframes;
  1003. unsigned int nr_sframes;
  1004. struct gnttab_get_status_frames getframes;
  1005. int rc;
  1006. nr_sframes = nr_status_frames(nr_gframes);
  1007. /* No need for kzalloc as it is initialized in following hypercall
  1008. * GNTTABOP_get_status_frames.
  1009. */
  1010. sframes = kmalloc_array(nr_sframes, sizeof(uint64_t), GFP_ATOMIC);
  1011. if (!sframes)
  1012. return -ENOMEM;
  1013. getframes.dom = DOMID_SELF;
  1014. getframes.nr_frames = nr_sframes;
  1015. set_xen_guest_handle(getframes.frame_list, sframes);
  1016. rc = HYPERVISOR_grant_table_op(GNTTABOP_get_status_frames,
  1017. &getframes, 1);
  1018. if (rc == -ENOSYS) {
  1019. kfree(sframes);
  1020. return -ENOSYS;
  1021. }
  1022. BUG_ON(rc || getframes.status);
  1023. rc = arch_gnttab_map_status(sframes, nr_sframes,
  1024. nr_status_frames(gnttab_max_grant_frames()),
  1025. &grstatus);
  1026. BUG_ON(rc);
  1027. kfree(sframes);
  1028. rc = arch_gnttab_map_shared(frames, nr_gframes,
  1029. gnttab_max_grant_frames(),
  1030. &gnttab_shared.addr);
  1031. BUG_ON(rc);
  1032. return 0;
  1033. }
  1034. static void gnttab_unmap_frames_v2(void)
  1035. {
  1036. arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames);
  1037. arch_gnttab_unmap(grstatus, nr_status_frames(nr_grant_frames));
  1038. }
  1039. static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
  1040. {
  1041. struct gnttab_setup_table setup;
  1042. xen_pfn_t *frames;
  1043. unsigned int nr_gframes = end_idx + 1;
  1044. int rc;
  1045. if (xen_feature(XENFEAT_auto_translated_physmap)) {
  1046. struct xen_add_to_physmap xatp;
  1047. unsigned int i = end_idx;
  1048. rc = 0;
  1049. BUG_ON(xen_auto_xlat_grant_frames.count < nr_gframes);
  1050. /*
  1051. * Loop backwards, so that the first hypercall has the largest
  1052. * index, ensuring that the table will grow only once.
  1053. */
  1054. do {
  1055. xatp.domid = DOMID_SELF;
  1056. xatp.idx = i;
  1057. xatp.space = XENMAPSPACE_grant_table;
  1058. xatp.gpfn = xen_auto_xlat_grant_frames.pfn[i];
  1059. rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
  1060. if (rc != 0) {
  1061. pr_warn("grant table add_to_physmap failed, err=%d\n",
  1062. rc);
  1063. break;
  1064. }
  1065. } while (i-- > start_idx);
  1066. return rc;
  1067. }
  1068. /* No need for kzalloc as it is initialized in following hypercall
  1069. * GNTTABOP_setup_table.
  1070. */
  1071. frames = kmalloc_array(nr_gframes, sizeof(unsigned long), GFP_ATOMIC);
  1072. if (!frames)
  1073. return -ENOMEM;
  1074. setup.dom = DOMID_SELF;
  1075. setup.nr_frames = nr_gframes;
  1076. set_xen_guest_handle(setup.frame_list, frames);
  1077. rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
  1078. if (rc == -ENOSYS) {
  1079. kfree(frames);
  1080. return -ENOSYS;
  1081. }
  1082. BUG_ON(rc || setup.status);
  1083. rc = gnttab_interface->map_frames(frames, nr_gframes);
  1084. kfree(frames);
  1085. return rc;
  1086. }
  1087. static const struct gnttab_ops gnttab_v1_ops = {
  1088. .version = 1,
  1089. .grefs_per_grant_frame = XEN_PAGE_SIZE /
  1090. sizeof(struct grant_entry_v1),
  1091. .map_frames = gnttab_map_frames_v1,
  1092. .unmap_frames = gnttab_unmap_frames_v1,
  1093. .update_entry = gnttab_update_entry_v1,
  1094. .end_foreign_access_ref = gnttab_end_foreign_access_ref_v1,
  1095. .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v1,
  1096. .query_foreign_access = gnttab_query_foreign_access_v1,
  1097. };
  1098. static const struct gnttab_ops gnttab_v2_ops = {
  1099. .version = 2,
  1100. .grefs_per_grant_frame = XEN_PAGE_SIZE /
  1101. sizeof(union grant_entry_v2),
  1102. .map_frames = gnttab_map_frames_v2,
  1103. .unmap_frames = gnttab_unmap_frames_v2,
  1104. .update_entry = gnttab_update_entry_v2,
  1105. .end_foreign_access_ref = gnttab_end_foreign_access_ref_v2,
  1106. .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v2,
  1107. .query_foreign_access = gnttab_query_foreign_access_v2,
  1108. };
  1109. static bool gnttab_need_v2(void)
  1110. {
  1111. #ifdef CONFIG_X86
  1112. uint32_t base, width;
  1113. if (xen_pv_domain()) {
  1114. base = xen_cpuid_base();
  1115. if (cpuid_eax(base) < 5)
  1116. return false; /* Information not available, use V1. */
  1117. width = cpuid_ebx(base + 5) &
  1118. XEN_CPUID_MACHINE_ADDRESS_WIDTH_MASK;
  1119. return width > 32 + PAGE_SHIFT;
  1120. }
  1121. #endif
  1122. return !!(max_possible_pfn >> 32);
  1123. }
  1124. static void gnttab_request_version(void)
  1125. {
  1126. long rc;
  1127. struct gnttab_set_version gsv;
  1128. if (gnttab_need_v2())
  1129. gsv.version = 2;
  1130. else
  1131. gsv.version = 1;
  1132. /* Boot parameter overrides automatic selection. */
  1133. if (xen_gnttab_version >= 1 && xen_gnttab_version <= 2)
  1134. gsv.version = xen_gnttab_version;
  1135. rc = HYPERVISOR_grant_table_op(GNTTABOP_set_version, &gsv, 1);
  1136. if (rc == 0 && gsv.version == 2)
  1137. gnttab_interface = &gnttab_v2_ops;
  1138. else
  1139. gnttab_interface = &gnttab_v1_ops;
  1140. pr_info("Grant tables using version %d layout\n",
  1141. gnttab_interface->version);
  1142. }
  1143. static int gnttab_setup(void)
  1144. {
  1145. unsigned int max_nr_gframes;
  1146. max_nr_gframes = gnttab_max_grant_frames();
  1147. if (max_nr_gframes < nr_grant_frames)
  1148. return -ENOSYS;
  1149. if (xen_feature(XENFEAT_auto_translated_physmap) && gnttab_shared.addr == NULL) {
  1150. gnttab_shared.addr = xen_auto_xlat_grant_frames.vaddr;
  1151. if (gnttab_shared.addr == NULL) {
  1152. pr_warn("gnttab share frames (addr=0x%08lx) is not mapped!\n",
  1153. (unsigned long)xen_auto_xlat_grant_frames.vaddr);
  1154. return -ENOMEM;
  1155. }
  1156. }
  1157. return gnttab_map(0, nr_grant_frames - 1);
  1158. }
  1159. int gnttab_resume(void)
  1160. {
  1161. gnttab_request_version();
  1162. return gnttab_setup();
  1163. }
  1164. int gnttab_suspend(void)
  1165. {
  1166. if (!xen_feature(XENFEAT_auto_translated_physmap))
  1167. gnttab_interface->unmap_frames();
  1168. return 0;
  1169. }
  1170. static int gnttab_expand(unsigned int req_entries)
  1171. {
  1172. int rc;
  1173. unsigned int cur, extra;
  1174. BUG_ON(gnttab_interface == NULL);
  1175. cur = nr_grant_frames;
  1176. extra = ((req_entries + gnttab_interface->grefs_per_grant_frame - 1) /
  1177. gnttab_interface->grefs_per_grant_frame);
  1178. if (cur + extra > gnttab_max_grant_frames()) {
  1179. pr_warn_ratelimited("xen/grant-table: max_grant_frames reached"
  1180. " cur=%u extra=%u limit=%u"
  1181. " gnttab_free_count=%u req_entries=%u\n",
  1182. cur, extra, gnttab_max_grant_frames(),
  1183. gnttab_free_count, req_entries);
  1184. return -ENOSPC;
  1185. }
  1186. rc = gnttab_map(cur, cur + extra - 1);
  1187. if (rc == 0)
  1188. rc = grow_gnttab_list(extra);
  1189. return rc;
  1190. }
  1191. int gnttab_init(void)
  1192. {
  1193. int i;
  1194. unsigned long max_nr_grant_frames;
  1195. unsigned int max_nr_glist_frames, nr_glist_frames;
  1196. unsigned int nr_init_grefs;
  1197. int ret;
  1198. gnttab_request_version();
  1199. max_nr_grant_frames = gnttab_max_grant_frames();
  1200. nr_grant_frames = 1;
  1201. /* Determine the maximum number of frames required for the
  1202. * grant reference free list on the current hypervisor.
  1203. */
  1204. BUG_ON(gnttab_interface == NULL);
  1205. max_nr_glist_frames = (max_nr_grant_frames *
  1206. gnttab_interface->grefs_per_grant_frame / RPP);
  1207. gnttab_list = kmalloc_array(max_nr_glist_frames,
  1208. sizeof(grant_ref_t *),
  1209. GFP_KERNEL);
  1210. if (gnttab_list == NULL)
  1211. return -ENOMEM;
  1212. nr_glist_frames = gnttab_frames(nr_grant_frames, RPP);
  1213. for (i = 0; i < nr_glist_frames; i++) {
  1214. gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL);
  1215. if (gnttab_list[i] == NULL) {
  1216. ret = -ENOMEM;
  1217. goto ini_nomem;
  1218. }
  1219. }
  1220. ret = arch_gnttab_init(max_nr_grant_frames,
  1221. nr_status_frames(max_nr_grant_frames));
  1222. if (ret < 0)
  1223. goto ini_nomem;
  1224. if (gnttab_setup() < 0) {
  1225. ret = -ENODEV;
  1226. goto ini_nomem;
  1227. }
  1228. nr_init_grefs = nr_grant_frames *
  1229. gnttab_interface->grefs_per_grant_frame;
  1230. for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
  1231. gnttab_entry(i) = i + 1;
  1232. gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
  1233. gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES;
  1234. gnttab_free_head = NR_RESERVED_ENTRIES;
  1235. printk("Grant table initialized\n");
  1236. return 0;
  1237. ini_nomem:
  1238. for (i--; i >= 0; i--)
  1239. free_page((unsigned long)gnttab_list[i]);
  1240. kfree(gnttab_list);
  1241. return ret;
  1242. }
  1243. EXPORT_SYMBOL_GPL(gnttab_init);
  1244. static int __gnttab_init(void)
  1245. {
  1246. if (!xen_domain())
  1247. return -ENODEV;
  1248. /* Delay grant-table initialization in the PV on HVM case */
  1249. if (xen_hvm_domain() && !xen_pvh_domain())
  1250. return 0;
  1251. return gnttab_init();
  1252. }
  1253. /* Starts after core_initcall so that xen_pvh_gnttab_setup can be called
  1254. * beforehand to initialize xen_auto_xlat_grant_frames. */
  1255. core_initcall_sync(__gnttab_init);