hypercall.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /******************************************************************************
  2. * hypercall.h
  3. *
  4. * Linux-specific hypervisor handling.
  5. *
  6. * Copyright (c) 2002-2004, K A Fraser
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation; or, when distributed
  11. * separately from the Linux kernel or incorporated into other
  12. * software packages, subject to the following license:
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this source file (the "Software"), to deal in the Software without
  16. * restriction, including without limitation the rights to use, copy, modify,
  17. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  18. * and to permit persons to whom the Software is furnished to do so, subject to
  19. * the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  30. * IN THE SOFTWARE.
  31. */
  32. #ifndef ASM_X86__XEN__HYPERCALL_H
  33. #define ASM_X86__XEN__HYPERCALL_H
  34. #include <linux/errno.h>
  35. #include <linux/string.h>
  36. #include <xen/interface/xen.h>
  37. #include <xen/interface/sched.h>
  38. #include <xen/interface/physdev.h>
  39. /*
  40. * The hypercall asms have to meet several constraints:
  41. * - Work on 32- and 64-bit.
  42. * The two architectures put their arguments in different sets of
  43. * registers.
  44. *
  45. * - Work around asm syntax quirks
  46. * It isn't possible to specify one of the rNN registers in a
  47. * constraint, so we use explicit register variables to get the
  48. * args into the right place.
  49. *
  50. * - Mark all registers as potentially clobbered
  51. * Even unused parameters can be clobbered by the hypervisor, so we
  52. * need to make sure gcc knows it.
  53. *
  54. * - Avoid compiler bugs.
  55. * This is the tricky part. Because x86_32 has such a constrained
  56. * register set, gcc versions below 4.3 have trouble generating
  57. * code when all the arg registers and memory are trashed by the
  58. * asm. There are syntactically simpler ways of achieving the
  59. * semantics below, but they cause the compiler to crash.
  60. *
  61. * The only combination I found which works is:
  62. * - assign the __argX variables first
  63. * - list all actually used parameters as "+r" (__argX)
  64. * - clobber the rest
  65. *
  66. * The result certainly isn't pretty, and it really shows up cpp's
  67. * weakness as as macro language. Sorry. (But let's just give thanks
  68. * there aren't more than 5 arguments...)
  69. */
  70. extern struct { char _entry[32]; } hypercall_page[];
  71. #define __HYPERCALL "call hypercall_page+%c[offset]"
  72. #define __HYPERCALL_ENTRY(x) \
  73. [offset] "i" (__HYPERVISOR_##x * sizeof(hypercall_page[0]))
  74. #ifdef CONFIG_X86_32
  75. #define __HYPERCALL_RETREG "eax"
  76. #define __HYPERCALL_ARG1REG "ebx"
  77. #define __HYPERCALL_ARG2REG "ecx"
  78. #define __HYPERCALL_ARG3REG "edx"
  79. #define __HYPERCALL_ARG4REG "esi"
  80. #define __HYPERCALL_ARG5REG "edi"
  81. #else
  82. #define __HYPERCALL_RETREG "rax"
  83. #define __HYPERCALL_ARG1REG "rdi"
  84. #define __HYPERCALL_ARG2REG "rsi"
  85. #define __HYPERCALL_ARG3REG "rdx"
  86. #define __HYPERCALL_ARG4REG "r10"
  87. #define __HYPERCALL_ARG5REG "r8"
  88. #endif
  89. #define __HYPERCALL_DECLS \
  90. register unsigned long __res asm(__HYPERCALL_RETREG); \
  91. register unsigned long __arg1 asm(__HYPERCALL_ARG1REG) = __arg1; \
  92. register unsigned long __arg2 asm(__HYPERCALL_ARG2REG) = __arg2; \
  93. register unsigned long __arg3 asm(__HYPERCALL_ARG3REG) = __arg3; \
  94. register unsigned long __arg4 asm(__HYPERCALL_ARG4REG) = __arg4; \
  95. register unsigned long __arg5 asm(__HYPERCALL_ARG5REG) = __arg5;
  96. #define __HYPERCALL_0PARAM "=r" (__res)
  97. #define __HYPERCALL_1PARAM __HYPERCALL_0PARAM, "+r" (__arg1)
  98. #define __HYPERCALL_2PARAM __HYPERCALL_1PARAM, "+r" (__arg2)
  99. #define __HYPERCALL_3PARAM __HYPERCALL_2PARAM, "+r" (__arg3)
  100. #define __HYPERCALL_4PARAM __HYPERCALL_3PARAM, "+r" (__arg4)
  101. #define __HYPERCALL_5PARAM __HYPERCALL_4PARAM, "+r" (__arg5)
  102. #define __HYPERCALL_0ARG()
  103. #define __HYPERCALL_1ARG(a1) \
  104. __HYPERCALL_0ARG() __arg1 = (unsigned long)(a1);
  105. #define __HYPERCALL_2ARG(a1,a2) \
  106. __HYPERCALL_1ARG(a1) __arg2 = (unsigned long)(a2);
  107. #define __HYPERCALL_3ARG(a1,a2,a3) \
  108. __HYPERCALL_2ARG(a1,a2) __arg3 = (unsigned long)(a3);
  109. #define __HYPERCALL_4ARG(a1,a2,a3,a4) \
  110. __HYPERCALL_3ARG(a1,a2,a3) __arg4 = (unsigned long)(a4);
  111. #define __HYPERCALL_5ARG(a1,a2,a3,a4,a5) \
  112. __HYPERCALL_4ARG(a1,a2,a3,a4) __arg5 = (unsigned long)(a5);
  113. #define __HYPERCALL_CLOBBER5 "memory"
  114. #define __HYPERCALL_CLOBBER4 __HYPERCALL_CLOBBER5, __HYPERCALL_ARG5REG
  115. #define __HYPERCALL_CLOBBER3 __HYPERCALL_CLOBBER4, __HYPERCALL_ARG4REG
  116. #define __HYPERCALL_CLOBBER2 __HYPERCALL_CLOBBER3, __HYPERCALL_ARG3REG
  117. #define __HYPERCALL_CLOBBER1 __HYPERCALL_CLOBBER2, __HYPERCALL_ARG2REG
  118. #define __HYPERCALL_CLOBBER0 __HYPERCALL_CLOBBER1, __HYPERCALL_ARG1REG
  119. #define _hypercall0(type, name) \
  120. ({ \
  121. __HYPERCALL_DECLS; \
  122. __HYPERCALL_0ARG(); \
  123. asm volatile (__HYPERCALL \
  124. : __HYPERCALL_0PARAM \
  125. : __HYPERCALL_ENTRY(name) \
  126. : __HYPERCALL_CLOBBER0); \
  127. (type)__res; \
  128. })
  129. #define _hypercall1(type, name, a1) \
  130. ({ \
  131. __HYPERCALL_DECLS; \
  132. __HYPERCALL_1ARG(a1); \
  133. asm volatile (__HYPERCALL \
  134. : __HYPERCALL_1PARAM \
  135. : __HYPERCALL_ENTRY(name) \
  136. : __HYPERCALL_CLOBBER1); \
  137. (type)__res; \
  138. })
  139. #define _hypercall2(type, name, a1, a2) \
  140. ({ \
  141. __HYPERCALL_DECLS; \
  142. __HYPERCALL_2ARG(a1, a2); \
  143. asm volatile (__HYPERCALL \
  144. : __HYPERCALL_2PARAM \
  145. : __HYPERCALL_ENTRY(name) \
  146. : __HYPERCALL_CLOBBER2); \
  147. (type)__res; \
  148. })
  149. #define _hypercall3(type, name, a1, a2, a3) \
  150. ({ \
  151. __HYPERCALL_DECLS; \
  152. __HYPERCALL_3ARG(a1, a2, a3); \
  153. asm volatile (__HYPERCALL \
  154. : __HYPERCALL_3PARAM \
  155. : __HYPERCALL_ENTRY(name) \
  156. : __HYPERCALL_CLOBBER3); \
  157. (type)__res; \
  158. })
  159. #define _hypercall4(type, name, a1, a2, a3, a4) \
  160. ({ \
  161. __HYPERCALL_DECLS; \
  162. __HYPERCALL_4ARG(a1, a2, a3, a4); \
  163. asm volatile (__HYPERCALL \
  164. : __HYPERCALL_4PARAM \
  165. : __HYPERCALL_ENTRY(name) \
  166. : __HYPERCALL_CLOBBER4); \
  167. (type)__res; \
  168. })
  169. #define _hypercall5(type, name, a1, a2, a3, a4, a5) \
  170. ({ \
  171. __HYPERCALL_DECLS; \
  172. __HYPERCALL_5ARG(a1, a2, a3, a4, a5); \
  173. asm volatile (__HYPERCALL \
  174. : __HYPERCALL_5PARAM \
  175. : __HYPERCALL_ENTRY(name) \
  176. : __HYPERCALL_CLOBBER5); \
  177. (type)__res; \
  178. })
  179. static inline int
  180. HYPERVISOR_set_trap_table(struct trap_info *table)
  181. {
  182. return _hypercall1(int, set_trap_table, table);
  183. }
  184. static inline int
  185. HYPERVISOR_mmu_update(struct mmu_update *req, int count,
  186. int *success_count, domid_t domid)
  187. {
  188. return _hypercall4(int, mmu_update, req, count, success_count, domid);
  189. }
  190. static inline int
  191. HYPERVISOR_mmuext_op(struct mmuext_op *op, int count,
  192. int *success_count, domid_t domid)
  193. {
  194. return _hypercall4(int, mmuext_op, op, count, success_count, domid);
  195. }
  196. static inline int
  197. HYPERVISOR_set_gdt(unsigned long *frame_list, int entries)
  198. {
  199. return _hypercall2(int, set_gdt, frame_list, entries);
  200. }
  201. static inline int
  202. HYPERVISOR_stack_switch(unsigned long ss, unsigned long esp)
  203. {
  204. return _hypercall2(int, stack_switch, ss, esp);
  205. }
  206. #ifdef CONFIG_X86_32
  207. static inline int
  208. HYPERVISOR_set_callbacks(unsigned long event_selector,
  209. unsigned long event_address,
  210. unsigned long failsafe_selector,
  211. unsigned long failsafe_address)
  212. {
  213. return _hypercall4(int, set_callbacks,
  214. event_selector, event_address,
  215. failsafe_selector, failsafe_address);
  216. }
  217. #else /* CONFIG_X86_64 */
  218. static inline int
  219. HYPERVISOR_set_callbacks(unsigned long event_address,
  220. unsigned long failsafe_address,
  221. unsigned long syscall_address)
  222. {
  223. return _hypercall3(int, set_callbacks,
  224. event_address, failsafe_address,
  225. syscall_address);
  226. }
  227. #endif /* CONFIG_X86_{32,64} */
  228. static inline int
  229. HYPERVISOR_callback_op(int cmd, void *arg)
  230. {
  231. return _hypercall2(int, callback_op, cmd, arg);
  232. }
  233. static inline int
  234. HYPERVISOR_fpu_taskswitch(int set)
  235. {
  236. return _hypercall1(int, fpu_taskswitch, set);
  237. }
  238. static inline int
  239. HYPERVISOR_sched_op(int cmd, void *arg)
  240. {
  241. return _hypercall2(int, sched_op_new, cmd, arg);
  242. }
  243. static inline long
  244. HYPERVISOR_set_timer_op(u64 timeout)
  245. {
  246. unsigned long timeout_hi = (unsigned long)(timeout>>32);
  247. unsigned long timeout_lo = (unsigned long)timeout;
  248. return _hypercall2(long, set_timer_op, timeout_lo, timeout_hi);
  249. }
  250. static inline int
  251. HYPERVISOR_set_debugreg(int reg, unsigned long value)
  252. {
  253. return _hypercall2(int, set_debugreg, reg, value);
  254. }
  255. static inline unsigned long
  256. HYPERVISOR_get_debugreg(int reg)
  257. {
  258. return _hypercall1(unsigned long, get_debugreg, reg);
  259. }
  260. static inline int
  261. HYPERVISOR_update_descriptor(u64 ma, u64 desc)
  262. {
  263. return _hypercall4(int, update_descriptor, ma, ma>>32, desc, desc>>32);
  264. }
  265. static inline int
  266. HYPERVISOR_memory_op(unsigned int cmd, void *arg)
  267. {
  268. return _hypercall2(int, memory_op, cmd, arg);
  269. }
  270. static inline int
  271. HYPERVISOR_multicall(void *call_list, int nr_calls)
  272. {
  273. return _hypercall2(int, multicall, call_list, nr_calls);
  274. }
  275. static inline int
  276. HYPERVISOR_update_va_mapping(unsigned long va, pte_t new_val,
  277. unsigned long flags)
  278. {
  279. if (sizeof(new_val) == sizeof(long))
  280. return _hypercall3(int, update_va_mapping, va,
  281. new_val.pte, flags);
  282. else
  283. return _hypercall4(int, update_va_mapping, va,
  284. new_val.pte, new_val.pte >> 32, flags);
  285. }
  286. static inline int
  287. HYPERVISOR_event_channel_op(int cmd, void *arg)
  288. {
  289. int rc = _hypercall2(int, event_channel_op, cmd, arg);
  290. if (unlikely(rc == -ENOSYS)) {
  291. struct evtchn_op op;
  292. op.cmd = cmd;
  293. memcpy(&op.u, arg, sizeof(op.u));
  294. rc = _hypercall1(int, event_channel_op_compat, &op);
  295. memcpy(arg, &op.u, sizeof(op.u));
  296. }
  297. return rc;
  298. }
  299. static inline int
  300. HYPERVISOR_xen_version(int cmd, void *arg)
  301. {
  302. return _hypercall2(int, xen_version, cmd, arg);
  303. }
  304. static inline int
  305. HYPERVISOR_console_io(int cmd, int count, char *str)
  306. {
  307. return _hypercall3(int, console_io, cmd, count, str);
  308. }
  309. static inline int
  310. HYPERVISOR_physdev_op(int cmd, void *arg)
  311. {
  312. int rc = _hypercall2(int, physdev_op, cmd, arg);
  313. if (unlikely(rc == -ENOSYS)) {
  314. struct physdev_op op;
  315. op.cmd = cmd;
  316. memcpy(&op.u, arg, sizeof(op.u));
  317. rc = _hypercall1(int, physdev_op_compat, &op);
  318. memcpy(arg, &op.u, sizeof(op.u));
  319. }
  320. return rc;
  321. }
  322. static inline int
  323. HYPERVISOR_grant_table_op(unsigned int cmd, void *uop, unsigned int count)
  324. {
  325. return _hypercall3(int, grant_table_op, cmd, uop, count);
  326. }
  327. static inline int
  328. HYPERVISOR_update_va_mapping_otherdomain(unsigned long va, pte_t new_val,
  329. unsigned long flags, domid_t domid)
  330. {
  331. if (sizeof(new_val) == sizeof(long))
  332. return _hypercall4(int, update_va_mapping_otherdomain, va,
  333. new_val.pte, flags, domid);
  334. else
  335. return _hypercall5(int, update_va_mapping_otherdomain, va,
  336. new_val.pte, new_val.pte >> 32,
  337. flags, domid);
  338. }
  339. static inline int
  340. HYPERVISOR_vm_assist(unsigned int cmd, unsigned int type)
  341. {
  342. return _hypercall2(int, vm_assist, cmd, type);
  343. }
  344. static inline int
  345. HYPERVISOR_vcpu_op(int cmd, int vcpuid, void *extra_args)
  346. {
  347. return _hypercall3(int, vcpu_op, cmd, vcpuid, extra_args);
  348. }
  349. #ifdef CONFIG_X86_64
  350. static inline int
  351. HYPERVISOR_set_segment_base(int reg, unsigned long value)
  352. {
  353. return _hypercall2(int, set_segment_base, reg, value);
  354. }
  355. #endif
  356. static inline int
  357. HYPERVISOR_suspend(unsigned long srec)
  358. {
  359. return _hypercall3(int, sched_op, SCHEDOP_shutdown,
  360. SHUTDOWN_suspend, srec);
  361. }
  362. static inline int
  363. HYPERVISOR_nmi_op(unsigned long op, unsigned long arg)
  364. {
  365. return _hypercall2(int, nmi_op, op, arg);
  366. }
  367. static inline void
  368. MULTI_fpu_taskswitch(struct multicall_entry *mcl, int set)
  369. {
  370. mcl->op = __HYPERVISOR_fpu_taskswitch;
  371. mcl->args[0] = set;
  372. }
  373. static inline void
  374. MULTI_update_va_mapping(struct multicall_entry *mcl, unsigned long va,
  375. pte_t new_val, unsigned long flags)
  376. {
  377. mcl->op = __HYPERVISOR_update_va_mapping;
  378. mcl->args[0] = va;
  379. if (sizeof(new_val) == sizeof(long)) {
  380. mcl->args[1] = new_val.pte;
  381. mcl->args[2] = flags;
  382. } else {
  383. mcl->args[1] = new_val.pte;
  384. mcl->args[2] = new_val.pte >> 32;
  385. mcl->args[3] = flags;
  386. }
  387. }
  388. static inline void
  389. MULTI_grant_table_op(struct multicall_entry *mcl, unsigned int cmd,
  390. void *uop, unsigned int count)
  391. {
  392. mcl->op = __HYPERVISOR_grant_table_op;
  393. mcl->args[0] = cmd;
  394. mcl->args[1] = (unsigned long)uop;
  395. mcl->args[2] = count;
  396. }
  397. static inline void
  398. MULTI_update_va_mapping_otherdomain(struct multicall_entry *mcl, unsigned long va,
  399. pte_t new_val, unsigned long flags,
  400. domid_t domid)
  401. {
  402. mcl->op = __HYPERVISOR_update_va_mapping_otherdomain;
  403. mcl->args[0] = va;
  404. if (sizeof(new_val) == sizeof(long)) {
  405. mcl->args[1] = new_val.pte;
  406. mcl->args[2] = flags;
  407. mcl->args[3] = domid;
  408. } else {
  409. mcl->args[1] = new_val.pte;
  410. mcl->args[2] = new_val.pte >> 32;
  411. mcl->args[3] = flags;
  412. mcl->args[4] = domid;
  413. }
  414. }
  415. static inline void
  416. MULTI_update_descriptor(struct multicall_entry *mcl, u64 maddr,
  417. struct desc_struct desc)
  418. {
  419. mcl->op = __HYPERVISOR_update_descriptor;
  420. if (sizeof(maddr) == sizeof(long)) {
  421. mcl->args[0] = maddr;
  422. mcl->args[1] = *(unsigned long *)&desc;
  423. } else {
  424. mcl->args[0] = maddr;
  425. mcl->args[1] = maddr >> 32;
  426. mcl->args[2] = desc.a;
  427. mcl->args[3] = desc.b;
  428. }
  429. }
  430. static inline void
  431. MULTI_memory_op(struct multicall_entry *mcl, unsigned int cmd, void *arg)
  432. {
  433. mcl->op = __HYPERVISOR_memory_op;
  434. mcl->args[0] = cmd;
  435. mcl->args[1] = (unsigned long)arg;
  436. }
  437. static inline void
  438. MULTI_mmu_update(struct multicall_entry *mcl, struct mmu_update *req,
  439. int count, int *success_count, domid_t domid)
  440. {
  441. mcl->op = __HYPERVISOR_mmu_update;
  442. mcl->args[0] = (unsigned long)req;
  443. mcl->args[1] = count;
  444. mcl->args[2] = (unsigned long)success_count;
  445. mcl->args[3] = domid;
  446. }
  447. static inline void
  448. MULTI_mmuext_op(struct multicall_entry *mcl, struct mmuext_op *op, int count,
  449. int *success_count, domid_t domid)
  450. {
  451. mcl->op = __HYPERVISOR_mmuext_op;
  452. mcl->args[0] = (unsigned long)op;
  453. mcl->args[1] = count;
  454. mcl->args[2] = (unsigned long)success_count;
  455. mcl->args[3] = domid;
  456. }
  457. static inline void
  458. MULTI_set_gdt(struct multicall_entry *mcl, unsigned long *frames, int entries)
  459. {
  460. mcl->op = __HYPERVISOR_set_gdt;
  461. mcl->args[0] = (unsigned long)frames;
  462. mcl->args[1] = entries;
  463. }
  464. static inline void
  465. MULTI_stack_switch(struct multicall_entry *mcl,
  466. unsigned long ss, unsigned long esp)
  467. {
  468. mcl->op = __HYPERVISOR_stack_switch;
  469. mcl->args[0] = ss;
  470. mcl->args[1] = esp;
  471. }
  472. #endif /* ASM_X86__XEN__HYPERCALL_H */