hypercall.h 16 KB

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