xcom_hcall.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *
  16. * Tristan Gingold <tristan.gingold@bull.net>
  17. *
  18. * Copyright (c) 2007
  19. * Isaku Yamahata <yamahata at valinux co jp>
  20. * VA Linux Systems Japan K.K.
  21. * consolidate mini and inline version.
  22. */
  23. #include <linux/module.h>
  24. #include <xen/interface/xen.h>
  25. #include <xen/interface/memory.h>
  26. #include <xen/interface/grant_table.h>
  27. #include <xen/interface/callback.h>
  28. #include <xen/interface/vcpu.h>
  29. #include <asm/xen/hypervisor.h>
  30. #include <asm/xen/xencomm.h>
  31. /* Xencomm notes:
  32. * This file defines hypercalls to be used by xencomm. The hypercalls simply
  33. * create inlines or mini descriptors for pointers and then call the raw arch
  34. * hypercall xencomm_arch_hypercall_XXX
  35. *
  36. * If the arch wants to directly use these hypercalls, simply define macros
  37. * in asm/xen/hypercall.h, eg:
  38. * #define HYPERVISOR_sched_op xencomm_hypercall_sched_op
  39. *
  40. * The arch may also define HYPERVISOR_xxx as a function and do more operations
  41. * before/after doing the hypercall.
  42. *
  43. * Note: because only inline or mini descriptors are created these functions
  44. * must only be called with in kernel memory parameters.
  45. */
  46. int
  47. xencomm_hypercall_console_io(int cmd, int count, char *str)
  48. {
  49. /* xen early printk uses console io hypercall before
  50. * xencomm initialization. In that case, we just ignore it.
  51. */
  52. if (!xencomm_is_initialized())
  53. return 0;
  54. return xencomm_arch_hypercall_console_io
  55. (cmd, count, xencomm_map_no_alloc(str, count));
  56. }
  57. EXPORT_SYMBOL_GPL(xencomm_hypercall_console_io);
  58. int
  59. xencomm_hypercall_event_channel_op(int cmd, void *op)
  60. {
  61. struct xencomm_handle *desc;
  62. desc = xencomm_map_no_alloc(op, sizeof(struct evtchn_op));
  63. if (desc == NULL)
  64. return -EINVAL;
  65. return xencomm_arch_hypercall_event_channel_op(cmd, desc);
  66. }
  67. EXPORT_SYMBOL_GPL(xencomm_hypercall_event_channel_op);
  68. int
  69. xencomm_hypercall_xen_version(int cmd, void *arg)
  70. {
  71. struct xencomm_handle *desc;
  72. unsigned int argsize;
  73. switch (cmd) {
  74. case XENVER_version:
  75. /* do not actually pass an argument */
  76. return xencomm_arch_hypercall_xen_version(cmd, 0);
  77. case XENVER_extraversion:
  78. argsize = sizeof(struct xen_extraversion);
  79. break;
  80. case XENVER_compile_info:
  81. argsize = sizeof(struct xen_compile_info);
  82. break;
  83. case XENVER_capabilities:
  84. argsize = sizeof(struct xen_capabilities_info);
  85. break;
  86. case XENVER_changeset:
  87. argsize = sizeof(struct xen_changeset_info);
  88. break;
  89. case XENVER_platform_parameters:
  90. argsize = sizeof(struct xen_platform_parameters);
  91. break;
  92. case XENVER_get_features:
  93. argsize = (arg == NULL) ? 0 : sizeof(struct xen_feature_info);
  94. break;
  95. default:
  96. printk(KERN_DEBUG
  97. "%s: unknown version op %d\n", __func__, cmd);
  98. return -ENOSYS;
  99. }
  100. desc = xencomm_map_no_alloc(arg, argsize);
  101. if (desc == NULL)
  102. return -EINVAL;
  103. return xencomm_arch_hypercall_xen_version(cmd, desc);
  104. }
  105. EXPORT_SYMBOL_GPL(xencomm_hypercall_xen_version);
  106. int
  107. xencomm_hypercall_physdev_op(int cmd, void *op)
  108. {
  109. unsigned int argsize;
  110. switch (cmd) {
  111. case PHYSDEVOP_apic_read:
  112. case PHYSDEVOP_apic_write:
  113. argsize = sizeof(struct physdev_apic);
  114. break;
  115. case PHYSDEVOP_alloc_irq_vector:
  116. case PHYSDEVOP_free_irq_vector:
  117. argsize = sizeof(struct physdev_irq);
  118. break;
  119. case PHYSDEVOP_irq_status_query:
  120. argsize = sizeof(struct physdev_irq_status_query);
  121. break;
  122. default:
  123. printk(KERN_DEBUG
  124. "%s: unknown physdev op %d\n", __func__, cmd);
  125. return -ENOSYS;
  126. }
  127. return xencomm_arch_hypercall_physdev_op
  128. (cmd, xencomm_map_no_alloc(op, argsize));
  129. }
  130. static int
  131. xencommize_grant_table_op(struct xencomm_mini **xc_area,
  132. unsigned int cmd, void *op, unsigned int count,
  133. struct xencomm_handle **desc)
  134. {
  135. struct xencomm_handle *desc1;
  136. unsigned int argsize;
  137. switch (cmd) {
  138. case GNTTABOP_map_grant_ref:
  139. argsize = sizeof(struct gnttab_map_grant_ref);
  140. break;
  141. case GNTTABOP_unmap_grant_ref:
  142. argsize = sizeof(struct gnttab_unmap_grant_ref);
  143. break;
  144. case GNTTABOP_setup_table:
  145. {
  146. struct gnttab_setup_table *setup = op;
  147. argsize = sizeof(*setup);
  148. if (count != 1)
  149. return -EINVAL;
  150. desc1 = __xencomm_map_no_alloc
  151. (xen_guest_handle(setup->frame_list),
  152. setup->nr_frames *
  153. sizeof(*xen_guest_handle(setup->frame_list)),
  154. *xc_area);
  155. if (desc1 == NULL)
  156. return -EINVAL;
  157. (*xc_area)++;
  158. set_xen_guest_handle(setup->frame_list, (void *)desc1);
  159. break;
  160. }
  161. case GNTTABOP_dump_table:
  162. argsize = sizeof(struct gnttab_dump_table);
  163. break;
  164. case GNTTABOP_transfer:
  165. argsize = sizeof(struct gnttab_transfer);
  166. break;
  167. case GNTTABOP_copy:
  168. argsize = sizeof(struct gnttab_copy);
  169. break;
  170. case GNTTABOP_query_size:
  171. argsize = sizeof(struct gnttab_query_size);
  172. break;
  173. default:
  174. printk(KERN_DEBUG "%s: unknown hypercall grant table op %d\n",
  175. __func__, cmd);
  176. BUG();
  177. }
  178. *desc = __xencomm_map_no_alloc(op, count * argsize, *xc_area);
  179. if (*desc == NULL)
  180. return -EINVAL;
  181. (*xc_area)++;
  182. return 0;
  183. }
  184. int
  185. xencomm_hypercall_grant_table_op(unsigned int cmd, void *op,
  186. unsigned int count)
  187. {
  188. int rc;
  189. struct xencomm_handle *desc;
  190. XENCOMM_MINI_ALIGNED(xc_area, 2);
  191. rc = xencommize_grant_table_op(&xc_area, cmd, op, count, &desc);
  192. if (rc)
  193. return rc;
  194. return xencomm_arch_hypercall_grant_table_op(cmd, desc, count);
  195. }
  196. EXPORT_SYMBOL_GPL(xencomm_hypercall_grant_table_op);
  197. int
  198. xencomm_hypercall_sched_op(int cmd, void *arg)
  199. {
  200. struct xencomm_handle *desc;
  201. unsigned int argsize;
  202. switch (cmd) {
  203. case SCHEDOP_yield:
  204. case SCHEDOP_block:
  205. argsize = 0;
  206. break;
  207. case SCHEDOP_shutdown:
  208. argsize = sizeof(struct sched_shutdown);
  209. break;
  210. case SCHEDOP_poll:
  211. {
  212. struct sched_poll *poll = arg;
  213. struct xencomm_handle *ports;
  214. argsize = sizeof(struct sched_poll);
  215. ports = xencomm_map_no_alloc(xen_guest_handle(poll->ports),
  216. sizeof(*xen_guest_handle(poll->ports)));
  217. set_xen_guest_handle(poll->ports, (void *)ports);
  218. break;
  219. }
  220. default:
  221. printk(KERN_DEBUG "%s: unknown sched op %d\n", __func__, cmd);
  222. return -ENOSYS;
  223. }
  224. desc = xencomm_map_no_alloc(arg, argsize);
  225. if (desc == NULL)
  226. return -EINVAL;
  227. return xencomm_arch_hypercall_sched_op(cmd, desc);
  228. }
  229. EXPORT_SYMBOL_GPL(xencomm_hypercall_sched_op);
  230. int
  231. xencomm_hypercall_multicall(void *call_list, int nr_calls)
  232. {
  233. int rc;
  234. int i;
  235. struct multicall_entry *mce;
  236. struct xencomm_handle *desc;
  237. XENCOMM_MINI_ALIGNED(xc_area, nr_calls * 2);
  238. for (i = 0; i < nr_calls; i++) {
  239. mce = (struct multicall_entry *)call_list + i;
  240. switch (mce->op) {
  241. case __HYPERVISOR_update_va_mapping:
  242. case __HYPERVISOR_mmu_update:
  243. /* No-op on ia64. */
  244. break;
  245. case __HYPERVISOR_grant_table_op:
  246. rc = xencommize_grant_table_op
  247. (&xc_area,
  248. mce->args[0], (void *)mce->args[1],
  249. mce->args[2], &desc);
  250. if (rc)
  251. return rc;
  252. mce->args[1] = (unsigned long)desc;
  253. break;
  254. case __HYPERVISOR_memory_op:
  255. default:
  256. printk(KERN_DEBUG
  257. "%s: unhandled multicall op entry op %lu\n",
  258. __func__, mce->op);
  259. return -ENOSYS;
  260. }
  261. }
  262. desc = xencomm_map_no_alloc(call_list,
  263. nr_calls * sizeof(struct multicall_entry));
  264. if (desc == NULL)
  265. return -EINVAL;
  266. return xencomm_arch_hypercall_multicall(desc, nr_calls);
  267. }
  268. EXPORT_SYMBOL_GPL(xencomm_hypercall_multicall);
  269. int
  270. xencomm_hypercall_callback_op(int cmd, void *arg)
  271. {
  272. unsigned int argsize;
  273. switch (cmd) {
  274. case CALLBACKOP_register:
  275. argsize = sizeof(struct callback_register);
  276. break;
  277. case CALLBACKOP_unregister:
  278. argsize = sizeof(struct callback_unregister);
  279. break;
  280. default:
  281. printk(KERN_DEBUG
  282. "%s: unknown callback op %d\n", __func__, cmd);
  283. return -ENOSYS;
  284. }
  285. return xencomm_arch_hypercall_callback_op
  286. (cmd, xencomm_map_no_alloc(arg, argsize));
  287. }
  288. static int
  289. xencommize_memory_reservation(struct xencomm_mini *xc_area,
  290. struct xen_memory_reservation *mop)
  291. {
  292. struct xencomm_handle *desc;
  293. desc = __xencomm_map_no_alloc(xen_guest_handle(mop->extent_start),
  294. mop->nr_extents *
  295. sizeof(*xen_guest_handle(mop->extent_start)),
  296. xc_area);
  297. if (desc == NULL)
  298. return -EINVAL;
  299. set_xen_guest_handle(mop->extent_start, (void *)desc);
  300. return 0;
  301. }
  302. int
  303. xencomm_hypercall_memory_op(unsigned int cmd, void *arg)
  304. {
  305. GUEST_HANDLE(xen_pfn_t) extent_start_va[2] = { {NULL}, {NULL} };
  306. struct xen_memory_reservation *xmr = NULL;
  307. int rc;
  308. struct xencomm_handle *desc;
  309. unsigned int argsize;
  310. XENCOMM_MINI_ALIGNED(xc_area, 2);
  311. switch (cmd) {
  312. case XENMEM_increase_reservation:
  313. case XENMEM_decrease_reservation:
  314. case XENMEM_populate_physmap:
  315. xmr = (struct xen_memory_reservation *)arg;
  316. set_xen_guest_handle(extent_start_va[0],
  317. xen_guest_handle(xmr->extent_start));
  318. argsize = sizeof(*xmr);
  319. rc = xencommize_memory_reservation(xc_area, xmr);
  320. if (rc)
  321. return rc;
  322. xc_area++;
  323. break;
  324. case XENMEM_maximum_ram_page:
  325. argsize = 0;
  326. break;
  327. case XENMEM_add_to_physmap:
  328. argsize = sizeof(struct xen_add_to_physmap);
  329. break;
  330. default:
  331. printk(KERN_DEBUG "%s: unknown memory op %d\n", __func__, cmd);
  332. return -ENOSYS;
  333. }
  334. desc = xencomm_map_no_alloc(arg, argsize);
  335. if (desc == NULL)
  336. return -EINVAL;
  337. rc = xencomm_arch_hypercall_memory_op(cmd, desc);
  338. switch (cmd) {
  339. case XENMEM_increase_reservation:
  340. case XENMEM_decrease_reservation:
  341. case XENMEM_populate_physmap:
  342. set_xen_guest_handle(xmr->extent_start,
  343. xen_guest_handle(extent_start_va[0]));
  344. break;
  345. }
  346. return rc;
  347. }
  348. EXPORT_SYMBOL_GPL(xencomm_hypercall_memory_op);
  349. int
  350. xencomm_hypercall_suspend(unsigned long srec)
  351. {
  352. struct sched_shutdown arg;
  353. arg.reason = SHUTDOWN_suspend;
  354. return xencomm_arch_hypercall_sched_op(
  355. SCHEDOP_shutdown, xencomm_map_no_alloc(&arg, sizeof(arg)));
  356. }
  357. long
  358. xencomm_hypercall_vcpu_op(int cmd, int cpu, void *arg)
  359. {
  360. unsigned int argsize;
  361. switch (cmd) {
  362. case VCPUOP_register_runstate_memory_area: {
  363. struct vcpu_register_runstate_memory_area *area =
  364. (struct vcpu_register_runstate_memory_area *)arg;
  365. argsize = sizeof(*arg);
  366. set_xen_guest_handle(area->addr.h,
  367. (void *)xencomm_map_no_alloc(area->addr.v,
  368. sizeof(area->addr.v)));
  369. break;
  370. }
  371. default:
  372. printk(KERN_DEBUG "%s: unknown vcpu op %d\n", __func__, cmd);
  373. return -ENOSYS;
  374. }
  375. return xencomm_arch_hypercall_vcpu_op(cmd, cpu,
  376. xencomm_map_no_alloc(arg, argsize));
  377. }
  378. long
  379. xencomm_hypercall_opt_feature(void *arg)
  380. {
  381. return xencomm_arch_hypercall_opt_feature(
  382. xencomm_map_no_alloc(arg,
  383. sizeof(struct xen_ia64_opt_feature)));
  384. }