1.. SPDX-License-Identifier: CC-BY-4.0
2
3Hypercall ABI
4=============
5
6Hypercalls are system calls to Xen.  Two modes of guest operation are
7supported, and up to 6 individual parameters are supported.
8
9Hypercalls may only be issued by kernel-level software [1]_.
10
11Registers
12---------
13
14The registers used for hypercalls depends on the operating mode of the guest.
15
16.. list-table::
17   :header-rows: 1
18
19   * - ABI
20     - Hypercall Index
21     - Parameters (1 - 6)
22     - Result
23
24   * - 64bit
25     - RAX
26     - RDI RSI RDX R10 R8 R9
27     - RAX
28
29   * - 32bit
30     - EAX
31     - EBX ECX EDX ESI EDI EBP
32     - EAX
33
3432 and 64bit PV guests have an ABI fixed by their guest type.  The ABI for an
35HVM guest depends on whether the vCPU is operating in a 64bit segment or not
36[2]_.
37
38
39Parameters
40----------
41
42Different hypercalls take a different number of parameters.  Each hypercall
43potentially clobbers each of its parameter registers; a guest may not rely on
44the parameter registers staying the same.  A debug build of Xen checks this by
45deliberately poisoning the parameter registers before returning back to the
46guest.
47
48
49Mode transfer
50-------------
51
52The exact sequence of instructions required to issue a hypercall differs
53between virtualisation mode and hardware vendor.
54
55.. list-table::
56   :header-rows: 1
57
58   * - Guest
59     - Transfer instruction
60
61   * - 32bit PV
62     - INT 0x82
63
64   * - 64bit PV
65     - SYSCALL
66
67   * - Intel HVM
68     - VMCALL
69
70   * - AMD HVM
71     - VMMCALL
72
73To abstract away the details, Xen implements an interface known as the
74Hypercall Page.  This allows a guest to make a hypercall without needing to
75perform mode-specific or vendor-specific setup.
76
77
78Hypercall Page
79==============
80
81The hypercall page is a page of guest RAM into which Xen will write suitable
82transfer stubs.
83
84Creating a hypercall page is an isolated operation from Xen's point of view.
85It is the guests responsibility to ensure that the hypercall page, once
86written by Xen, is mapped with executable permissions so it may be used.
87Multiple hypercall pages may be created by the guest, if it wishes.
88
89The stubs are arranged by hypercall index, and start on 32-byte boundaries.
90To invoke a specific hypercall, ``call`` the relevant stub [3]_:
91
92.. code-block:: none
93
94   call hypercall_page + index * 32
95
96There result is an ABI which is invariant of the exact operating mode or
97hardware vendor.  This is intended to simplify guest kernel interfaces by
98abstracting away the details of how it is currently running.
99
100
101Creating Hypercall Pages
102------------------------
103
104Guests which are started using the PV boot protocol may set set
105``XEN_ELFNOTE_HYPERCALL_PAGE`` to have the nominated page written as a
106hypercall page during construction.  This mechanism is common for PV guests,
107and allows hypercalls to be issued with no additional setup.
108
109Any guest can locate the Xen CPUID leaves and read the *hypercall transfer
110page* information, which specifies an MSR that can be used to create
111additional hypercall pages.  When a guest physical address is written to the
112MSR, Xen writes a hypercall page into the nominated guest page.  This
113mechanism is common for HVM guests which are typically started via legacy
114means.
115
116
117.. rubric:: Footnotes
118
119.. [1] For HVM guests, ``HVMOP_guest_request_vm_event`` may be configured to
120       be usable from userspace, but this behaviour is not default.
121
122.. [2] While it is possible to use compatibility mode segments in a 64bit
123       kernel, hypercalls issues from such a mode will be interpreted with the
124       32bit ABI.  Such a setup is not expected in production scenarios.
125
126.. [3] ``HYPERCALL_iret`` is special.  It is only implemented for PV guests
127       and takes all its parameters on the stack.  This stub should be
128       ``jmp``'d to, rather than ``call``'d.  HVM guests have this stub
129       implemented as ``ud2a`` to prevent accidental use.
130