xref: /freebsd-src/sys/contrib/xen/vcpu.h (revision 3a9fd8242b35884921dfc4e886f284a75870a536)
1*3a9fd824SRoger Pau Monné /******************************************************************************
2*3a9fd824SRoger Pau Monné  * vcpu.h
3*3a9fd824SRoger Pau Monné  *
4*3a9fd824SRoger Pau Monné  * VCPU initialisation, query, and hotplug.
5*3a9fd824SRoger Pau Monné  *
6*3a9fd824SRoger Pau Monné  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*3a9fd824SRoger Pau Monné  * of this software and associated documentation files (the "Software"), to
8*3a9fd824SRoger Pau Monné  * deal in the Software without restriction, including without limitation the
9*3a9fd824SRoger Pau Monné  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*3a9fd824SRoger Pau Monné  * sell copies of the Software, and to permit persons to whom the Software is
11*3a9fd824SRoger Pau Monné  * furnished to do so, subject to the following conditions:
12*3a9fd824SRoger Pau Monné  *
13*3a9fd824SRoger Pau Monné  * The above copyright notice and this permission notice shall be included in
14*3a9fd824SRoger Pau Monné  * all copies or substantial portions of the Software.
15*3a9fd824SRoger Pau Monné  *
16*3a9fd824SRoger Pau Monné  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*3a9fd824SRoger Pau Monné  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*3a9fd824SRoger Pau Monné  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*3a9fd824SRoger Pau Monné  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*3a9fd824SRoger Pau Monné  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21*3a9fd824SRoger Pau Monné  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22*3a9fd824SRoger Pau Monné  * DEALINGS IN THE SOFTWARE.
23*3a9fd824SRoger Pau Monné  *
24*3a9fd824SRoger Pau Monné  * Copyright (c) 2005, Keir Fraser <keir@xensource.com>
25*3a9fd824SRoger Pau Monné  */
26*3a9fd824SRoger Pau Monné 
27*3a9fd824SRoger Pau Monné #ifndef __XEN_PUBLIC_VCPU_H__
28*3a9fd824SRoger Pau Monné #define __XEN_PUBLIC_VCPU_H__
29*3a9fd824SRoger Pau Monné 
30*3a9fd824SRoger Pau Monné #include "xen.h"
31*3a9fd824SRoger Pau Monné 
32*3a9fd824SRoger Pau Monné /*
33*3a9fd824SRoger Pau Monné  * Prototype for this hypercall is:
34*3a9fd824SRoger Pau Monné  *  long vcpu_op(int cmd, unsigned int vcpuid, void *extra_args)
35*3a9fd824SRoger Pau Monné  * @cmd        == VCPUOP_??? (VCPU operation).
36*3a9fd824SRoger Pau Monné  * @vcpuid     == VCPU to operate on.
37*3a9fd824SRoger Pau Monné  * @extra_args == Operation-specific extra arguments (NULL if none).
38*3a9fd824SRoger Pau Monné  */
39*3a9fd824SRoger Pau Monné 
40*3a9fd824SRoger Pau Monné /*
41*3a9fd824SRoger Pau Monné  * Initialise a VCPU. Each VCPU can be initialised only once. A
42*3a9fd824SRoger Pau Monné  * newly-initialised VCPU will not run until it is brought up by VCPUOP_up.
43*3a9fd824SRoger Pau Monné  *
44*3a9fd824SRoger Pau Monné  * @extra_arg == For PV or ARM guests this is a pointer to a vcpu_guest_context
45*3a9fd824SRoger Pau Monné  *               structure containing the initial state for the VCPU. For x86
46*3a9fd824SRoger Pau Monné  *               HVM based guests this is a pointer to a vcpu_hvm_context
47*3a9fd824SRoger Pau Monné  *               structure.
48*3a9fd824SRoger Pau Monné  */
49*3a9fd824SRoger Pau Monné #define VCPUOP_initialise            0
50*3a9fd824SRoger Pau Monné 
51*3a9fd824SRoger Pau Monné /*
52*3a9fd824SRoger Pau Monné  * Bring up a VCPU. This makes the VCPU runnable. This operation will fail
53*3a9fd824SRoger Pau Monné  * if the VCPU has not been initialised (VCPUOP_initialise).
54*3a9fd824SRoger Pau Monné  */
55*3a9fd824SRoger Pau Monné #define VCPUOP_up                    1
56*3a9fd824SRoger Pau Monné 
57*3a9fd824SRoger Pau Monné /*
58*3a9fd824SRoger Pau Monné  * Bring down a VCPU (i.e., make it non-runnable).
59*3a9fd824SRoger Pau Monné  * There are a few caveats that callers should observe:
60*3a9fd824SRoger Pau Monné  *  1. This operation may return, and VCPU_is_up may return false, before the
61*3a9fd824SRoger Pau Monné  *     VCPU stops running (i.e., the command is asynchronous). It is a good
62*3a9fd824SRoger Pau Monné  *     idea to ensure that the VCPU has entered a non-critical loop before
63*3a9fd824SRoger Pau Monné  *     bringing it down. Alternatively, this operation is guaranteed
64*3a9fd824SRoger Pau Monné  *     synchronous if invoked by the VCPU itself.
65*3a9fd824SRoger Pau Monné  *  2. After a VCPU is initialised, there is currently no way to drop all its
66*3a9fd824SRoger Pau Monné  *     references to domain memory. Even a VCPU that is down still holds
67*3a9fd824SRoger Pau Monné  *     memory references via its pagetable base pointer and GDT. It is good
68*3a9fd824SRoger Pau Monné  *     practise to move a VCPU onto an 'idle' or default page table, LDT and
69*3a9fd824SRoger Pau Monné  *     GDT before bringing it down.
70*3a9fd824SRoger Pau Monné  */
71*3a9fd824SRoger Pau Monné #define VCPUOP_down                  2
72*3a9fd824SRoger Pau Monné 
73*3a9fd824SRoger Pau Monné /* Returns 1 if the given VCPU is up. */
74*3a9fd824SRoger Pau Monné #define VCPUOP_is_up                 3
75*3a9fd824SRoger Pau Monné 
76*3a9fd824SRoger Pau Monné /*
77*3a9fd824SRoger Pau Monné  * Return information about the state and running time of a VCPU.
78*3a9fd824SRoger Pau Monné  * @extra_arg == pointer to vcpu_runstate_info structure.
79*3a9fd824SRoger Pau Monné  */
80*3a9fd824SRoger Pau Monné #define VCPUOP_get_runstate_info     4
81*3a9fd824SRoger Pau Monné struct vcpu_runstate_info {
82*3a9fd824SRoger Pau Monné     /* VCPU's current state (RUNSTATE_*). */
83*3a9fd824SRoger Pau Monné     int      state;
84*3a9fd824SRoger Pau Monné     /* When was current state entered (system time, ns)? */
85*3a9fd824SRoger Pau Monné     uint64_t state_entry_time;
86*3a9fd824SRoger Pau Monné     /*
87*3a9fd824SRoger Pau Monné      * Update indicator set in state_entry_time:
88*3a9fd824SRoger Pau Monné      * When activated via VMASST_TYPE_runstate_update_flag, set during
89*3a9fd824SRoger Pau Monné      * updates in guest memory mapped copy of vcpu_runstate_info.
90*3a9fd824SRoger Pau Monné      */
91*3a9fd824SRoger Pau Monné #define XEN_RUNSTATE_UPDATE          (xen_mk_ullong(1) << 63)
92*3a9fd824SRoger Pau Monné     /*
93*3a9fd824SRoger Pau Monné      * Time spent in each RUNSTATE_* (ns). The sum of these times is
94*3a9fd824SRoger Pau Monné      * guaranteed not to drift from system time.
95*3a9fd824SRoger Pau Monné      */
96*3a9fd824SRoger Pau Monné     uint64_t time[4];
97*3a9fd824SRoger Pau Monné };
98*3a9fd824SRoger Pau Monné typedef struct vcpu_runstate_info vcpu_runstate_info_t;
99*3a9fd824SRoger Pau Monné DEFINE_XEN_GUEST_HANDLE(vcpu_runstate_info_t);
100*3a9fd824SRoger Pau Monné 
101*3a9fd824SRoger Pau Monné /* VCPU is currently running on a physical CPU. */
102*3a9fd824SRoger Pau Monné #define RUNSTATE_running  0
103*3a9fd824SRoger Pau Monné 
104*3a9fd824SRoger Pau Monné /* VCPU is runnable, but not currently scheduled on any physical CPU. */
105*3a9fd824SRoger Pau Monné #define RUNSTATE_runnable 1
106*3a9fd824SRoger Pau Monné 
107*3a9fd824SRoger Pau Monné /* VCPU is blocked (a.k.a. idle). It is therefore not runnable. */
108*3a9fd824SRoger Pau Monné #define RUNSTATE_blocked  2
109*3a9fd824SRoger Pau Monné 
110*3a9fd824SRoger Pau Monné /*
111*3a9fd824SRoger Pau Monné  * VCPU is not runnable, but it is not blocked.
112*3a9fd824SRoger Pau Monné  * This is a 'catch all' state for things like hotplug and pauses by the
113*3a9fd824SRoger Pau Monné  * system administrator (or for critical sections in the hypervisor).
114*3a9fd824SRoger Pau Monné  * RUNSTATE_blocked dominates this state (it is the preferred state).
115*3a9fd824SRoger Pau Monné  */
116*3a9fd824SRoger Pau Monné #define RUNSTATE_offline  3
117*3a9fd824SRoger Pau Monné 
118*3a9fd824SRoger Pau Monné /*
119*3a9fd824SRoger Pau Monné  * Register a shared memory area from which the guest may obtain its own
120*3a9fd824SRoger Pau Monné  * runstate information without needing to execute a hypercall.
121*3a9fd824SRoger Pau Monné  * Notes:
122*3a9fd824SRoger Pau Monné  *  1. The registered address may be virtual or physical or guest handle,
123*3a9fd824SRoger Pau Monné  *     depending on the platform. Virtual address or guest handle should be
124*3a9fd824SRoger Pau Monné  *     registered on x86 systems.
125*3a9fd824SRoger Pau Monné  *  2. Only one shared area may be registered per VCPU. The shared area is
126*3a9fd824SRoger Pau Monné  *     updated by the hypervisor each time the VCPU is scheduled. Thus
127*3a9fd824SRoger Pau Monné  *     runstate.state will always be RUNSTATE_running and
128*3a9fd824SRoger Pau Monné  *     runstate.state_entry_time will indicate the system time at which the
129*3a9fd824SRoger Pau Monné  *     VCPU was last scheduled to run.
130*3a9fd824SRoger Pau Monné  * @extra_arg == pointer to vcpu_register_runstate_memory_area structure.
131*3a9fd824SRoger Pau Monné  */
132*3a9fd824SRoger Pau Monné #define VCPUOP_register_runstate_memory_area 5
133*3a9fd824SRoger Pau Monné struct vcpu_register_runstate_memory_area {
134*3a9fd824SRoger Pau Monné     union {
135*3a9fd824SRoger Pau Monné         XEN_GUEST_HANDLE(vcpu_runstate_info_t) h;
136*3a9fd824SRoger Pau Monné         struct vcpu_runstate_info *v;
137*3a9fd824SRoger Pau Monné         uint64_t p;
138*3a9fd824SRoger Pau Monné     } addr;
139*3a9fd824SRoger Pau Monné };
140*3a9fd824SRoger Pau Monné typedef struct vcpu_register_runstate_memory_area vcpu_register_runstate_memory_area_t;
141*3a9fd824SRoger Pau Monné DEFINE_XEN_GUEST_HANDLE(vcpu_register_runstate_memory_area_t);
142*3a9fd824SRoger Pau Monné 
143*3a9fd824SRoger Pau Monné /*
144*3a9fd824SRoger Pau Monné  * Set or stop a VCPU's periodic timer. Every VCPU has one periodic timer
145*3a9fd824SRoger Pau Monné  * which can be set via these commands. Periods smaller than one millisecond
146*3a9fd824SRoger Pau Monné  * may not be supported.
147*3a9fd824SRoger Pau Monné  */
148*3a9fd824SRoger Pau Monné #define VCPUOP_set_periodic_timer    6 /* arg == vcpu_set_periodic_timer_t */
149*3a9fd824SRoger Pau Monné #define VCPUOP_stop_periodic_timer   7 /* arg == NULL */
150*3a9fd824SRoger Pau Monné struct vcpu_set_periodic_timer {
151*3a9fd824SRoger Pau Monné     uint64_t period_ns;
152*3a9fd824SRoger Pau Monné };
153*3a9fd824SRoger Pau Monné typedef struct vcpu_set_periodic_timer vcpu_set_periodic_timer_t;
154*3a9fd824SRoger Pau Monné DEFINE_XEN_GUEST_HANDLE(vcpu_set_periodic_timer_t);
155*3a9fd824SRoger Pau Monné 
156*3a9fd824SRoger Pau Monné /*
157*3a9fd824SRoger Pau Monné  * Set or stop a VCPU's single-shot timer. Every VCPU has one single-shot
158*3a9fd824SRoger Pau Monné  * timer which can be set via these commands.
159*3a9fd824SRoger Pau Monné  */
160*3a9fd824SRoger Pau Monné #define VCPUOP_set_singleshot_timer  8 /* arg == vcpu_set_singleshot_timer_t */
161*3a9fd824SRoger Pau Monné #define VCPUOP_stop_singleshot_timer 9 /* arg == NULL */
162*3a9fd824SRoger Pau Monné struct vcpu_set_singleshot_timer {
163*3a9fd824SRoger Pau Monné     uint64_t timeout_abs_ns;   /* Absolute system time value in nanoseconds. */
164*3a9fd824SRoger Pau Monné     uint32_t flags;            /* VCPU_SSHOTTMR_??? */
165*3a9fd824SRoger Pau Monné };
166*3a9fd824SRoger Pau Monné typedef struct vcpu_set_singleshot_timer vcpu_set_singleshot_timer_t;
167*3a9fd824SRoger Pau Monné DEFINE_XEN_GUEST_HANDLE(vcpu_set_singleshot_timer_t);
168*3a9fd824SRoger Pau Monné 
169*3a9fd824SRoger Pau Monné /* Flags to VCPUOP_set_singleshot_timer. */
170*3a9fd824SRoger Pau Monné  /* Require the timeout to be in the future (return -ETIME if it's passed). */
171*3a9fd824SRoger Pau Monné #define _VCPU_SSHOTTMR_future (0)
172*3a9fd824SRoger Pau Monné #define VCPU_SSHOTTMR_future  (1U << _VCPU_SSHOTTMR_future)
173*3a9fd824SRoger Pau Monné 
174*3a9fd824SRoger Pau Monné /*
175*3a9fd824SRoger Pau Monné  * Register a memory location in the guest address space for the
176*3a9fd824SRoger Pau Monné  * vcpu_info structure.  This allows the guest to place the vcpu_info
177*3a9fd824SRoger Pau Monné  * structure in a convenient place, such as in a per-cpu data area.
178*3a9fd824SRoger Pau Monné  * The pointer need not be page aligned, but the structure must not
179*3a9fd824SRoger Pau Monné  * cross a page boundary.
180*3a9fd824SRoger Pau Monné  *
181*3a9fd824SRoger Pau Monné  * This may be called only once per vcpu.
182*3a9fd824SRoger Pau Monné  */
183*3a9fd824SRoger Pau Monné #define VCPUOP_register_vcpu_info   10  /* arg == vcpu_register_vcpu_info_t */
184*3a9fd824SRoger Pau Monné struct vcpu_register_vcpu_info {
185*3a9fd824SRoger Pau Monné     uint64_t mfn;    /* mfn of page to place vcpu_info */
186*3a9fd824SRoger Pau Monné     uint32_t offset; /* offset within page */
187*3a9fd824SRoger Pau Monné     uint32_t rsvd;   /* unused */
188*3a9fd824SRoger Pau Monné };
189*3a9fd824SRoger Pau Monné typedef struct vcpu_register_vcpu_info vcpu_register_vcpu_info_t;
190*3a9fd824SRoger Pau Monné DEFINE_XEN_GUEST_HANDLE(vcpu_register_vcpu_info_t);
191*3a9fd824SRoger Pau Monné 
192*3a9fd824SRoger Pau Monné /* Send an NMI to the specified VCPU. @extra_arg == NULL. */
193*3a9fd824SRoger Pau Monné #define VCPUOP_send_nmi             11
194*3a9fd824SRoger Pau Monné 
195*3a9fd824SRoger Pau Monné /*
196*3a9fd824SRoger Pau Monné  * Get the physical ID information for a pinned vcpu's underlying physical
197*3a9fd824SRoger Pau Monné  * processor.  The physical ID informmation is architecture-specific.
198*3a9fd824SRoger Pau Monné  * On x86: id[31:0]=apic_id, id[63:32]=acpi_id.
199*3a9fd824SRoger Pau Monné  * This command returns -EINVAL if it is not a valid operation for this VCPU.
200*3a9fd824SRoger Pau Monné  */
201*3a9fd824SRoger Pau Monné #define VCPUOP_get_physid           12 /* arg == vcpu_get_physid_t */
202*3a9fd824SRoger Pau Monné struct vcpu_get_physid {
203*3a9fd824SRoger Pau Monné     uint64_t phys_id;
204*3a9fd824SRoger Pau Monné };
205*3a9fd824SRoger Pau Monné typedef struct vcpu_get_physid vcpu_get_physid_t;
206*3a9fd824SRoger Pau Monné DEFINE_XEN_GUEST_HANDLE(vcpu_get_physid_t);
207*3a9fd824SRoger Pau Monné #define xen_vcpu_physid_to_x86_apicid(physid) ((uint32_t)(physid))
208*3a9fd824SRoger Pau Monné #define xen_vcpu_physid_to_x86_acpiid(physid) ((uint32_t)((physid) >> 32))
209*3a9fd824SRoger Pau Monné 
210*3a9fd824SRoger Pau Monné /*
211*3a9fd824SRoger Pau Monné  * Register a memory location to get a secondary copy of the vcpu time
212*3a9fd824SRoger Pau Monné  * parameters.  The master copy still exists as part of the vcpu shared
213*3a9fd824SRoger Pau Monné  * memory area, and this secondary copy is updated whenever the master copy
214*3a9fd824SRoger Pau Monné  * is updated (and using the same versioning scheme for synchronisation).
215*3a9fd824SRoger Pau Monné  *
216*3a9fd824SRoger Pau Monné  * The intent is that this copy may be mapped (RO) into userspace so
217*3a9fd824SRoger Pau Monné  * that usermode can compute system time using the time info and the
218*3a9fd824SRoger Pau Monné  * tsc.  Usermode will see an array of vcpu_time_info structures, one
219*3a9fd824SRoger Pau Monné  * for each vcpu, and choose the right one by an existing mechanism
220*3a9fd824SRoger Pau Monné  * which allows it to get the current vcpu number (such as via a
221*3a9fd824SRoger Pau Monné  * segment limit).  It can then apply the normal algorithm to compute
222*3a9fd824SRoger Pau Monné  * system time from the tsc.
223*3a9fd824SRoger Pau Monné  *
224*3a9fd824SRoger Pau Monné  * @extra_arg == pointer to vcpu_register_time_info_memory_area structure.
225*3a9fd824SRoger Pau Monné  */
226*3a9fd824SRoger Pau Monné #define VCPUOP_register_vcpu_time_memory_area   13
227*3a9fd824SRoger Pau Monné DEFINE_XEN_GUEST_HANDLE(vcpu_time_info_t);
228*3a9fd824SRoger Pau Monné struct vcpu_register_time_memory_area {
229*3a9fd824SRoger Pau Monné     union {
230*3a9fd824SRoger Pau Monné         XEN_GUEST_HANDLE(vcpu_time_info_t) h;
231*3a9fd824SRoger Pau Monné         struct vcpu_time_info *v;
232*3a9fd824SRoger Pau Monné         uint64_t p;
233*3a9fd824SRoger Pau Monné     } addr;
234*3a9fd824SRoger Pau Monné };
235*3a9fd824SRoger Pau Monné typedef struct vcpu_register_time_memory_area vcpu_register_time_memory_area_t;
236*3a9fd824SRoger Pau Monné DEFINE_XEN_GUEST_HANDLE(vcpu_register_time_memory_area_t);
237*3a9fd824SRoger Pau Monné 
238*3a9fd824SRoger Pau Monné #endif /* __XEN_PUBLIC_VCPU_H__ */
239*3a9fd824SRoger Pau Monné 
240*3a9fd824SRoger Pau Monné /*
241*3a9fd824SRoger Pau Monné  * Local variables:
242*3a9fd824SRoger Pau Monné  * mode: C
243*3a9fd824SRoger Pau Monné  * c-file-style: "BSD"
244*3a9fd824SRoger Pau Monné  * c-basic-offset: 4
245*3a9fd824SRoger Pau Monné  * tab-width: 4
246*3a9fd824SRoger Pau Monné  * indent-tabs-mode: nil
247*3a9fd824SRoger Pau Monné  * End:
248*3a9fd824SRoger Pau Monné  */
249