1 /******************************************************************************
2  * xenctrl.h
3  *
4  * A library for low-level access to the Xen control interfaces.
5  *
6  * Copyright (c) 2003-2004, K A Fraser.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation;
11  * version 2.1 of the License.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef XENCTRL_H
23 #define XENCTRL_H
24 
25 /* Tell the Xen public headers we are a user-space tools build. */
26 #ifndef __XEN_TOOLS__
27 #define __XEN_TOOLS__ 1
28 #endif
29 
30 #include <unistd.h>
31 #include <stddef.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdbool.h>
35 #include <xen/xen.h>
36 #include <xen/domctl.h>
37 #include <xen/physdev.h>
38 #include <xen/sysctl.h>
39 #include <xen/version.h>
40 #include <xen/event_channel.h>
41 #include <xen/sched.h>
42 #include <xen/memory.h>
43 #include <xen/grant_table.h>
44 #include <xen/hvm/dm_op.h>
45 #include <xen/hvm/params.h>
46 #include <xen/xsm/flask_op.h>
47 #include <xen/kexec.h>
48 #include <xen/platform.h>
49 
50 #include "xentoollog.h"
51 
52 #if defined(__i386__) || defined(__x86_64__)
53 #include <xen/foreign/x86_32.h>
54 #include <xen/foreign/x86_64.h>
55 #include <xen/arch-x86/xen-mca.h>
56 #endif
57 
58 #define XC_PAGE_SHIFT           12
59 #define XC_PAGE_SIZE            (1UL << XC_PAGE_SHIFT)
60 #define XC_PAGE_MASK            (~(XC_PAGE_SIZE-1))
61 
62 #define INVALID_MFN  (~0UL)
63 
64 /*
65  *  DEFINITIONS FOR CPU BARRIERS
66  */
67 
68 #define xen_barrier() asm volatile ( "" : : : "memory")
69 
70 #if defined(__i386__)
71 #define xen_mb()  asm volatile ( "lock; addl $0,0(%%esp)" : : : "memory" )
72 #define xen_rmb() xen_barrier()
73 #define xen_wmb() xen_barrier()
74 #elif defined(__x86_64__)
75 #define xen_mb()  asm volatile ( "mfence" : : : "memory")
76 #define xen_rmb() xen_barrier()
77 #define xen_wmb() xen_barrier()
78 #elif defined(__arm__)
79 #define xen_mb()   asm volatile ("dmb" : : : "memory")
80 #define xen_rmb()  asm volatile ("dmb" : : : "memory")
81 #define xen_wmb()  asm volatile ("dmb" : : : "memory")
82 #elif defined(__aarch64__)
83 #define xen_mb()   asm volatile ("dmb sy" : : : "memory")
84 #define xen_rmb()  asm volatile ("dmb sy" : : : "memory")
85 #define xen_wmb()  asm volatile ("dmb sy" : : : "memory")
86 #else
87 #error "Define barriers"
88 #endif
89 
90 
91 #define XENCTRL_HAS_XC_INTERFACE 1
92 /* In Xen 4.0 and earlier, xc_interface_open and xc_evtchn_open would
93  * both return ints being the file descriptor.  In 4.1 and later, they
94  * return an xc_interface* and xc_evtchn*, respectively - ie, a
95  * pointer to an opaque struct.  This #define is provided in 4.1 and
96  * later, allowing out-of-tree callers to more easily distinguish
97  * between, and be compatible with, both versions.
98  */
99 
100 
101 /*
102  *  GENERAL
103  *
104  * Unless otherwise specified, each function here returns zero or a
105  * non-null pointer on success; or in case of failure, sets errno and
106  * returns -1 or a null pointer.
107  *
108  * Unless otherwise specified, errors result in a call to the error
109  * handler function, which by default prints a message to the
110  * FILE* passed as the caller_data, which by default is stderr.
111  * (This is described below as "logging errors".)
112  *
113  * The error handler can safely trash errno, as libxc saves it across
114  * the callback.
115  */
116 
117 typedef struct xc_interface_core xc_interface;
118 
119 enum xc_error_code {
120   XC_ERROR_NONE = 0,
121   XC_INTERNAL_ERROR = 1,
122   XC_INVALID_KERNEL = 2,
123   XC_INVALID_PARAM = 3,
124   XC_OUT_OF_MEMORY = 4,
125   /* new codes need to be added to xc_error_level_to_desc too */
126 };
127 
128 typedef enum xc_error_code xc_error_code;
129 
130 
131 /*
132  *  INITIALIZATION FUNCTIONS
133  */
134 
135 /**
136  * This function opens a handle to the hypervisor interface.  This function can
137  * be called multiple times within a single process.  Multiple processes can
138  * have an open hypervisor interface at the same time.
139  *
140  * Note:
141  * After fork a child process must not use any opened xc interface
142  * handle inherited from their parent. They must open a new handle if
143  * they want to interact with xc.
144  *
145  * Each call to this function should have a corresponding call to
146  * xc_interface_close().
147  *
148  * This function can fail if the caller does not have superuser permission or
149  * if a Xen-enabled kernel is not currently running.
150  *
151  * @return a handle to the hypervisor interface
152  */
153 xc_interface *xc_interface_open(xentoollog_logger *logger,
154                                 xentoollog_logger *dombuild_logger,
155                                 unsigned open_flags);
156   /* if logger==NULL, will log to stderr
157    * if dombuild_logger=NULL, will log to a file
158    */
159 
160 /*
161  * Note: if XC_OPENFLAG_NON_REENTRANT is passed then libxc must not be
162  * called reentrantly and the calling application is responsible for
163  * providing mutual exclusion surrounding all libxc calls itself.
164  *
165  * In particular xc_{get,clear}_last_error only remain valid for the
166  * duration of the critical section containing the call which failed.
167  */
168 enum xc_open_flags {
169     XC_OPENFLAG_DUMMY =  1<<0, /* do not actually open a xenctrl interface */
170     XC_OPENFLAG_NON_REENTRANT = 1<<1, /* assume library is only every called from a single thread */
171 };
172 
173 /**
174  * This function closes an open hypervisor interface.
175  *
176  * This function can fail if the handle does not represent an open interface or
177  * if there were problems closing the interface.  In the latter case
178  * the interface is still closed.
179  *
180  * @parm xch a handle to an open hypervisor interface
181  * @return 0 on success, -1 otherwise.
182  */
183 int xc_interface_close(xc_interface *xch);
184 
185 /**
186  * Return the handles which xch has opened and will use for
187  * hypercalls, foreign memory accesses and device model operations.
188  * These may be used with the corresponding libraries so long as the
189  * xch itself remains open.
190  */
191 struct xencall_handle *xc_interface_xcall_handle(xc_interface *xch);
192 struct xenforeignmemory_handle *xc_interface_fmem_handle(xc_interface *xch);
193 struct xendevicemodel_handle *xc_interface_dmod_handle(xc_interface *xch);
194 
195 /*
196  * HYPERCALL SAFE MEMORY BUFFER
197  *
198  * Ensure that memory which is passed to a hypercall has been
199  * specially allocated in order to be safe to access from the
200  * hypervisor.
201  *
202  * Each user data pointer is shadowed by an xc_hypercall_buffer data
203  * structure. You should never define an xc_hypercall_buffer type
204  * directly, instead use the DECLARE_HYPERCALL_BUFFER* macros below.
205  *
206  * The strucuture should be considered opaque and all access should be
207  * via the macros and helper functions defined below.
208  *
209  * Once the buffer is declared the user is responsible for explicitly
210  * allocating and releasing the memory using
211  * xc_hypercall_buffer_alloc(_pages) and
212  * xc_hypercall_buffer_free(_pages).
213  *
214  * Once the buffer has been allocated the user can initialise the data
215  * via the normal pointer. The xc_hypercall_buffer structure is
216  * transparently referenced by the helper macros (such as
217  * xen_set_guest_handle) in order to check at compile time that the
218  * correct type of memory is being used.
219  */
220 struct xc_hypercall_buffer {
221     /* Hypercall safe memory buffer. */
222     void *hbuf;
223 
224     /*
225      * Reference to xc_hypercall_buffer passed as argument to the
226      * current function.
227      */
228     struct xc_hypercall_buffer *param_shadow;
229 
230     /*
231      * Direction of copy for bounce buffering.
232      */
233     int dir;
234 
235     /* Used iff dir != 0. */
236     void *ubuf;
237     size_t sz;
238 };
239 typedef struct xc_hypercall_buffer xc_hypercall_buffer_t;
240 
241 /*
242  * Construct the name of the hypercall buffer for a given variable.
243  * For internal use only
244  */
245 #define XC__HYPERCALL_BUFFER_NAME(_name) xc__hypercall_buffer_##_name
246 
247 /*
248  * Returns the hypercall_buffer associated with a variable.
249  */
250 #define HYPERCALL_BUFFER(_name)                                 \
251     ({  xc_hypercall_buffer_t _hcbuf_buf1;                      \
252         typeof(XC__HYPERCALL_BUFFER_NAME(_name)) *_hcbuf_buf2 = \
253                 &XC__HYPERCALL_BUFFER_NAME(_name);              \
254         (void)(&_hcbuf_buf1 == _hcbuf_buf2);                    \
255         (_hcbuf_buf2)->param_shadow ?                           \
256                 (_hcbuf_buf2)->param_shadow : (_hcbuf_buf2);    \
257      })
258 
259 #define HYPERCALL_BUFFER_INIT_NO_BOUNCE .dir = 0, .sz = 0, .ubuf = (void *)-1
260 
261 /*
262  * Defines a hypercall buffer and user pointer with _name of _type.
263  *
264  * The user accesses the data as normal via _name which will be
265  * transparently converted to the hypercall buffer as necessary.
266  */
267 #define DECLARE_HYPERCALL_BUFFER(_type, _name)                 \
268     _type *(_name) = NULL;                                     \
269     xc_hypercall_buffer_t XC__HYPERCALL_BUFFER_NAME(_name) = { \
270         .hbuf = NULL,                                          \
271         .param_shadow = NULL,                                  \
272         HYPERCALL_BUFFER_INIT_NO_BOUNCE                        \
273     }
274 
275 /*
276  * Like DECLARE_HYPERCALL_BUFFER() but using an already allocated
277  * hypercall buffer, _hbuf.
278  *
279  * Useful when a hypercall buffer is passed to a function and access
280  * via the user pointer is required.
281  *
282  * See DECLARE_HYPERCALL_BUFFER_ARGUMENT() if the user pointer is not
283  * required.
284  */
285 #define DECLARE_HYPERCALL_BUFFER_SHADOW(_type, _name, _hbuf)   \
286     _type *(_name) = (_hbuf)->hbuf;                            \
287     __attribute__((unused))                                    \
288     xc_hypercall_buffer_t XC__HYPERCALL_BUFFER_NAME(_name) = { \
289         .hbuf = (void *)-1,                                    \
290         .param_shadow = (_hbuf),                               \
291         HYPERCALL_BUFFER_INIT_NO_BOUNCE                        \
292     }
293 
294 /*
295  * Declare the necessary data structure to allow a hypercall buffer
296  * passed as an argument to a function to be used in the normal way.
297  */
298 #define DECLARE_HYPERCALL_BUFFER_ARGUMENT(_name)               \
299     xc_hypercall_buffer_t XC__HYPERCALL_BUFFER_NAME(_name) = { \
300         .hbuf = (void *)-1,                                    \
301         .param_shadow = (_name),                               \
302         HYPERCALL_BUFFER_INIT_NO_BOUNCE                        \
303     }
304 
305 /*
306  * Get the hypercall buffer data pointer in a form suitable for use
307  * directly as a hypercall argument.
308  */
309 #define HYPERCALL_BUFFER_AS_ARG(_name)                          \
310     ({  xc_hypercall_buffer_t _hcbuf_arg1;                      \
311         typeof(XC__HYPERCALL_BUFFER_NAME(_name)) *_hcbuf_arg2 = \
312                 HYPERCALL_BUFFER(_name);                        \
313         (void)(&_hcbuf_arg1 == _hcbuf_arg2);                    \
314         (unsigned long)(_hcbuf_arg2)->hbuf;                     \
315      })
316 
317 /*
318  * Set a xen_guest_handle in a type safe manner, ensuring that the
319  * data pointer has been correctly allocated.
320  */
321 #define set_xen_guest_handle_impl(_hnd, _val, _byte_off)        \
322     do {                                                        \
323         xc_hypercall_buffer_t _hcbuf_hnd1;                      \
324         typeof(XC__HYPERCALL_BUFFER_NAME(_val)) *_hcbuf_hnd2 =  \
325                 HYPERCALL_BUFFER(_val);                         \
326         (void) (&_hcbuf_hnd1 == _hcbuf_hnd2);                   \
327         set_xen_guest_handle_raw(_hnd,                          \
328                 (_hcbuf_hnd2)->hbuf + (_byte_off));             \
329     } while (0)
330 
331 #undef set_xen_guest_handle
332 #define set_xen_guest_handle(_hnd, _val)                        \
333     set_xen_guest_handle_impl(_hnd, _val, 0)
334 
335 #define set_xen_guest_handle_offset(_hnd, _val, _off)           \
336     set_xen_guest_handle_impl(_hnd, _val,                       \
337             ((sizeof(*_val)*(_off))))
338 
339 /* Use with set_xen_guest_handle in place of NULL */
340 extern xc_hypercall_buffer_t XC__HYPERCALL_BUFFER_NAME(HYPERCALL_BUFFER_NULL);
341 
342 /*
343  * Allocate and free hypercall buffers with byte granularity.
344  */
345 void *xc__hypercall_buffer_alloc(xc_interface *xch, xc_hypercall_buffer_t *b, size_t size);
346 #define xc_hypercall_buffer_alloc(_xch, _name, _size) xc__hypercall_buffer_alloc(_xch, HYPERCALL_BUFFER(_name), _size)
347 void xc__hypercall_buffer_free(xc_interface *xch, xc_hypercall_buffer_t *b);
348 #define xc_hypercall_buffer_free(_xch, _name) xc__hypercall_buffer_free(_xch, HYPERCALL_BUFFER(_name))
349 
350 /*
351  * Allocate and free hypercall buffers with page alignment.
352  */
353 void *xc__hypercall_buffer_alloc_pages(xc_interface *xch, xc_hypercall_buffer_t *b, int nr_pages);
354 #define xc_hypercall_buffer_alloc_pages(_xch, _name, _nr) xc__hypercall_buffer_alloc_pages(_xch, HYPERCALL_BUFFER(_name), _nr)
355 void xc__hypercall_buffer_free_pages(xc_interface *xch, xc_hypercall_buffer_t *b, int nr_pages);
356 #define xc_hypercall_buffer_free_pages(_xch, _name, _nr)                    \
357     do {                                                                    \
358         if ( _name )                                                        \
359             xc__hypercall_buffer_free_pages(_xch, HYPERCALL_BUFFER(_name),  \
360                                             _nr);                           \
361     } while (0)
362 
363 /*
364  * Array of hypercall buffers.
365  *
366  * Create an array with xc_hypercall_buffer_array_create() and
367  * populate it by declaring one hypercall buffer in a loop and
368  * allocating the buffer with xc_hypercall_buffer_array_alloc().
369  *
370  * To access a previously allocated buffers, declare a new hypercall
371  * buffer and call xc_hypercall_buffer_array_get().
372  *
373  * Destroy the array with xc_hypercall_buffer_array_destroy() to free
374  * the array and all its allocated hypercall buffers.
375  */
376 struct xc_hypercall_buffer_array;
377 typedef struct xc_hypercall_buffer_array xc_hypercall_buffer_array_t;
378 
379 xc_hypercall_buffer_array_t *xc_hypercall_buffer_array_create(xc_interface *xch, unsigned n);
380 void *xc__hypercall_buffer_array_alloc(xc_interface *xch, xc_hypercall_buffer_array_t *array,
381                                        unsigned index, xc_hypercall_buffer_t *hbuf, size_t size);
382 #define xc_hypercall_buffer_array_alloc(_xch, _array, _index, _name, _size) \
383     xc__hypercall_buffer_array_alloc(_xch, _array, _index, HYPERCALL_BUFFER(_name), _size)
384 void *xc__hypercall_buffer_array_get(xc_interface *xch, xc_hypercall_buffer_array_t *array,
385                                      unsigned index, xc_hypercall_buffer_t *hbuf);
386 #define xc_hypercall_buffer_array_get(_xch, _array, _index, _name, _size) \
387     xc__hypercall_buffer_array_get(_xch, _array, _index, HYPERCALL_BUFFER(_name))
388 void xc_hypercall_buffer_array_destroy(xc_interface *xc, xc_hypercall_buffer_array_t *array);
389 
390 /*
391  * CPUMAP handling
392  */
393 typedef uint8_t *xc_cpumap_t;
394 
395 /* return maximum number of cpus the hypervisor supports */
396 int xc_get_max_cpus(xc_interface *xch);
397 
398 /* return the number of online cpus */
399 int xc_get_online_cpus(xc_interface *xch);
400 
401 /* return array size for cpumap */
402 int xc_get_cpumap_size(xc_interface *xch);
403 
404 /* allocate a cpumap */
405 xc_cpumap_t xc_cpumap_alloc(xc_interface *xch);
406 
407 /* clear an CPU from the cpumap. */
408 void xc_cpumap_clearcpu(int cpu, xc_cpumap_t map);
409 
410 /* set an CPU in the cpumap. */
411 void xc_cpumap_setcpu(int cpu, xc_cpumap_t map);
412 
413 /* Test whether the CPU in cpumap is set. */
414 int xc_cpumap_testcpu(int cpu, xc_cpumap_t map);
415 
416 /*
417  * NODEMAP handling
418  */
419 typedef uint8_t *xc_nodemap_t;
420 
421 /* return maximum number of NUMA nodes the hypervisor supports */
422 int xc_get_max_nodes(xc_interface *xch);
423 
424 /* return array size for nodemap */
425 int xc_get_nodemap_size(xc_interface *xch);
426 
427 /* allocate a nodemap */
428 xc_nodemap_t xc_nodemap_alloc(xc_interface *xch);
429 
430 /*
431  * DOMAIN DEBUGGING FUNCTIONS
432  */
433 
434 typedef struct xc_core_header {
435     unsigned int xch_magic;
436     unsigned int xch_nr_vcpus;
437     unsigned int xch_nr_pages;
438     unsigned int xch_ctxt_offset;
439     unsigned int xch_index_offset;
440     unsigned int xch_pages_offset;
441 } xc_core_header_t;
442 
443 #define XC_CORE_MAGIC     0xF00FEBED
444 #define XC_CORE_MAGIC_HVM 0xF00FEBEE
445 
446 /*
447  * DOMAIN MANAGEMENT FUNCTIONS
448  */
449 
450 typedef struct xc_dominfo {
451     uint32_t      domid;
452     uint32_t      ssidref;
453     unsigned int  dying:1, crashed:1, shutdown:1,
454                   paused:1, blocked:1, running:1,
455                   hvm:1, debugged:1, xenstore:1, hap:1;
456     unsigned int  shutdown_reason; /* only meaningful if shutdown==1 */
457     unsigned long nr_pages; /* current number, not maximum */
458     unsigned long nr_outstanding_pages;
459     unsigned long nr_shared_pages;
460     unsigned long nr_paged_pages;
461     unsigned long shared_info_frame;
462     uint64_t      cpu_time;
463     unsigned long max_memkb;
464     unsigned int  nr_online_vcpus;
465     unsigned int  max_vcpu_id;
466     xen_domain_handle_t handle;
467     unsigned int  cpupool;
468     struct xen_arch_domainconfig arch_config;
469 } xc_dominfo_t;
470 
471 typedef xen_domctl_getdomaininfo_t xc_domaininfo_t;
472 
473 typedef union
474 {
475 #if defined(__i386__) || defined(__x86_64__)
476     vcpu_guest_context_x86_64_t x64;
477     vcpu_guest_context_x86_32_t x32;
478 #endif
479     vcpu_guest_context_t c;
480 } vcpu_guest_context_any_t;
481 
482 typedef union
483 {
484 #if defined(__i386__) || defined(__x86_64__)
485     shared_info_x86_64_t x64;
486     shared_info_x86_32_t x32;
487 #endif
488     shared_info_t s;
489 } shared_info_any_t;
490 
491 #if defined(__i386__) || defined(__x86_64__)
492 typedef union
493 {
494     start_info_x86_64_t x64;
495     start_info_x86_32_t x32;
496     start_info_t s;
497 } start_info_any_t;
498 #endif
499 
500 typedef struct xc_vcpu_extstate {
501     uint64_t xfeature_mask;
502     uint64_t size;
503     void *buffer;
504 } xc_vcpu_extstate_t;
505 
506 int xc_domain_create(xc_interface *xch, uint32_t *pdomid,
507                      struct xen_domctl_createdomain *config);
508 
509 
510 /* Functions to produce a dump of a given domain
511  *  xc_domain_dumpcore - produces a dump to a specified file
512  *  xc_domain_dumpcore_via_callback - produces a dump, using a specified
513  *                                    callback function
514  */
515 int xc_domain_dumpcore(xc_interface *xch,
516                        uint32_t domid,
517                        const char *corename);
518 
519 /* Define the callback function type for xc_domain_dumpcore_via_callback.
520  *
521  * This function is called by the coredump code for every "write",
522  * and passes an opaque object for the use of the function and
523  * created by the caller of xc_domain_dumpcore_via_callback.
524  */
525 typedef int (dumpcore_rtn_t)(xc_interface *xch,
526                              void *arg, char *buffer, unsigned int length);
527 
528 int xc_domain_dumpcore_via_callback(xc_interface *xch,
529                                     uint32_t domid,
530                                     void *arg,
531                                     dumpcore_rtn_t dump_rtn);
532 
533 /*
534  * This function sets the maximum number of vcpus that a domain may create.
535  *
536  * @parm xch a handle to an open hypervisor interface.
537  * @parm domid the domain id in which vcpus are to be created.
538  * @parm max the maximum number of vcpus that the domain may create.
539  * @return 0 on success, -1 on failure.
540  */
541 int xc_domain_max_vcpus(xc_interface *xch,
542                         uint32_t domid,
543                         unsigned int max);
544 
545 /**
546  * This function pauses a domain. A paused domain still exists in memory
547  * however it does not receive any timeslices from the hypervisor.
548  *
549  * @parm xch a handle to an open hypervisor interface
550  * @parm domid the domain id to pause
551  * @return 0 on success, -1 on failure.
552  */
553 int xc_domain_pause(xc_interface *xch,
554                     uint32_t domid);
555 /**
556  * This function unpauses a domain.  The domain should have been previously
557  * paused.
558  *
559  * @parm xch a handle to an open hypervisor interface
560  * @parm domid the domain id to unpause
561  * return 0 on success, -1 on failure
562  */
563 int xc_domain_unpause(xc_interface *xch,
564                       uint32_t domid);
565 
566 /**
567  * This function will destroy a domain.  Destroying a domain removes the domain
568  * completely from memory.  This function should be called after sending the
569  * domain a SHUTDOWN control message to free up the domain resources.
570  *
571  * @parm xch a handle to an open hypervisor interface
572  * @parm domid the domain id to destroy
573  * @return 0 on success, -1 on failure
574  */
575 int xc_domain_destroy(xc_interface *xch,
576                       uint32_t domid);
577 
578 
579 /**
580  * This function resumes a suspended domain. The domain should have
581  * been previously suspended.
582  *
583  * Note that there are 'xc_domain_suspend' as suspending a domain
584  * is quite the endeavour.
585  *
586  * For the purpose of this explanation there are three guests:
587  * PV (using hypercalls for privilgied operations), HVM
588  * (fully hardware virtualized guests using emulated devices for everything),
589  * and PVHVM (PV aware with hardware virtualisation).
590  *
591  * HVM guest are the simplest - they suspend via S3 / S4 and resume from
592  * S3 / S4. Upon resume they have to re-negotiate with the emulated devices.
593  *
594  * PV and PVHVM communicate via hypercalls for suspend (and resume).
595  * For suspend the toolstack initiates the process by writing an value
596  * in XenBus "control/shutdown" with the string "suspend".
597  *
598  * The PV guest stashes anything it deems neccessary in 'struct
599  * start_info' in case of failure (PVHVM may ignore this) and calls
600  * the SCHEDOP_shutdown::SHUTDOWN_suspend hypercall (for PV as
601  * argument it passes the MFN to 'struct start_info').
602  *
603  * And then the guest is suspended.
604  *
605  * The checkpointing or notifying a guest that the suspend failed or
606  * cancelled (in case of checkpoint) is by having the
607  * SCHEDOP_shutdown::SHUTDOWN_suspend hypercall return a non-zero
608  * value.
609  *
610  * The PV and PVHVM resume path are similar. For PV it would be
611  * similar to bootup - figure out where the 'struct start_info' is (or
612  * if the suspend was cancelled aka checkpointed - reuse the saved
613  * values).
614  *
615  * From here on they differ depending whether the guest is PV or PVHVM
616  * in specifics but follow overall the same path:
617  *  - PV: Bringing up the vCPUS,
618  *  - PVHVM: Setup vector callback,
619  *  - Bring up vCPU runstates,
620  *  - Remap the grant tables if checkpointing or setup from scratch,
621  *
622  *
623  * If the resume was not checkpointing (or if suspend was succesful) we would
624  * setup the PV timers and the different PV events. Lastly the PV drivers
625  * re-negotiate with the backend.
626  *
627  * This function would return before the guest started resuming. That is
628  * the guest would be in non-running state and its vCPU context would be
629  * in the the SCHEDOP_shutdown::SHUTDOWN_suspend hypercall return path
630  * (for PV and PVHVM). For HVM it would be in would be in QEMU emulated
631  * BIOS handling S3 suspend.
632  *
633  * @parm xch a handle to an open hypervisor interface
634  * @parm domid the domain id to resume
635  * @parm fast use cooperative resume (guest must support this)
636  * return 0 on success, -1 on failure
637  */
638 int xc_domain_resume(xc_interface *xch,
639 		     uint32_t domid,
640 		     int fast);
641 
642 /**
643  * This function will shutdown a domain. This is intended for use in
644  * fully-virtualized domains where this operation is analogous to the
645  * sched_op operations in a paravirtualized domain. The caller is
646  * expected to give the reason for the shutdown.
647  *
648  * @parm xch a handle to an open hypervisor interface
649  * @parm domid the domain id to destroy
650  * @parm reason is the reason (SHUTDOWN_xxx) for the shutdown
651  * @return 0 on success, -1 on failure
652  */
653 int xc_domain_shutdown(xc_interface *xch,
654                        uint32_t domid,
655                        int reason);
656 
657 int xc_watchdog(xc_interface *xch,
658 		uint32_t id,
659 		uint32_t timeout);
660 
661 /**
662  * This function explicitly sets the host NUMA nodes the domain will
663  * have affinity with.
664  *
665  * @parm xch a handle to an open hypervisor interface.
666  * @parm domid the domain id one wants to set the affinity of.
667  * @parm nodemap the map of the affine nodes.
668  * @return 0 on success, -1 on failure.
669  */
670 int xc_domain_node_setaffinity(xc_interface *xch,
671                                uint32_t domind,
672                                xc_nodemap_t nodemap);
673 
674 /**
675  * This function retrieves the host NUMA nodes the domain has
676  * affinity with.
677  *
678  * @parm xch a handle to an open hypervisor interface.
679  * @parm domid the domain id one wants to get the node affinity of.
680  * @parm nodemap the map of the affine nodes.
681  * @return 0 on success, -1 on failure.
682  */
683 int xc_domain_node_getaffinity(xc_interface *xch,
684                                uint32_t domind,
685                                xc_nodemap_t nodemap);
686 
687 /**
688  * This function specifies the CPU affinity for a vcpu.
689  *
690  * There are two kinds of affinity. Soft affinity is on what CPUs a vcpu
691  * prefers to run. Hard affinity is on what CPUs a vcpu is allowed to run.
692  * If flags contains XEN_VCPUAFFINITY_SOFT, the soft affinity it is set to
693  * what cpumap_soft_inout contains. If flags contains XEN_VCPUAFFINITY_HARD,
694  * the hard affinity is set to what cpumap_hard_inout contains. Both flags
695  * can be set at the same time, in which case both soft and hard affinity are
696  * set to what the respective parameter contains.
697  *
698  * The function also returns the effective hard or/and soft affinity, still
699  * via the cpumap_soft_inout and cpumap_hard_inout parameters. Effective
700  * affinity is, in case of soft affinity, the intersection of soft affinity,
701  * hard affinity and the cpupool's online CPUs for the domain, and is returned
702  * in cpumap_soft_inout, if XEN_VCPUAFFINITY_SOFT is set in flags. In case of
703  * hard affinity, it is the intersection between hard affinity and the
704  * cpupool's online CPUs, and is returned in cpumap_hard_inout, if
705  * XEN_VCPUAFFINITY_HARD is set in flags. If both flags are set, both soft
706  * and hard affinity are returned in the respective parameter.
707  *
708  * We do report it back as effective affinity is what the Xen scheduler will
709  * actually use, and we thus allow checking whether or not that matches with,
710  * or at least is good enough for, the caller's purposes.
711  *
712  * @param xch a handle to an open hypervisor interface.
713  * @param domid the id of the domain to which the vcpu belongs
714  * @param vcpu the vcpu id wihin the domain
715  * @param cpumap_hard_inout specifies(/returns) the (effective) hard affinity
716  * @param cpumap_soft_inout specifies(/returns) the (effective) soft affinity
717  * @param flags what we want to set
718  */
719 int xc_vcpu_setaffinity(xc_interface *xch,
720                         uint32_t domid,
721                         int vcpu,
722                         xc_cpumap_t cpumap_hard_inout,
723                         xc_cpumap_t cpumap_soft_inout,
724                         uint32_t flags);
725 
726 /**
727  * This function retrieves hard and soft CPU affinity of a vcpu,
728  * depending on what flags are set.
729  *
730  * Soft affinity is returned in cpumap_soft if XEN_VCPUAFFINITY_SOFT is set.
731  * Hard affinity is returned in cpumap_hard if XEN_VCPUAFFINITY_HARD is set.
732  *
733  * @param xch a handle to an open hypervisor interface.
734  * @param domid the id of the domain to which the vcpu belongs
735  * @param vcpu the vcpu id wihin the domain
736  * @param cpumap_hard is where hard affinity is returned
737  * @param cpumap_soft is where soft affinity is returned
738  * @param flags what we want get
739  */
740 int xc_vcpu_getaffinity(xc_interface *xch,
741                         uint32_t domid,
742                         int vcpu,
743                         xc_cpumap_t cpumap_hard,
744                         xc_cpumap_t cpumap_soft,
745                         uint32_t flags);
746 
747 
748 /**
749  * This function will return the guest_width (in bytes) for the
750  * specified domain.
751  *
752  * @param xch a handle to an open hypervisor interface.
753  * @param domid the domain id one wants the address size width of.
754  * @param addr_size the address size.
755  */
756 int xc_domain_get_guest_width(xc_interface *xch, uint32_t domid,
757                               unsigned int *guest_width);
758 
759 
760 /**
761  * This function will return information about one or more domains. It is
762  * designed to iterate over the list of domains. If a single domain is
763  * requested, this function will return the next domain in the list - if
764  * one exists. It is, therefore, important in this case to make sure the
765  * domain requested was the one returned.
766  *
767  * @parm xch a handle to an open hypervisor interface
768  * @parm first_domid the first domain to enumerate information from.  Domains
769  *                   are currently enumerate in order of creation.
770  * @parm max_doms the number of elements in info
771  * @parm info an array of max_doms size that will contain the information for
772  *            the enumerated domains.
773  * @return the number of domains enumerated or -1 on error
774  */
775 int xc_domain_getinfo(xc_interface *xch,
776                       uint32_t first_domid,
777                       unsigned int max_doms,
778                       xc_dominfo_t *info);
779 
780 
781 /**
782  * This function will set the execution context for the specified vcpu.
783  *
784  * @parm xch a handle to an open hypervisor interface
785  * @parm domid the domain to set the vcpu context for
786  * @parm vcpu the vcpu number for the context
787  * @parm ctxt pointer to the the cpu context with the values to set
788  * @return the number of domains enumerated or -1 on error
789  */
790 int xc_vcpu_setcontext(xc_interface *xch,
791                        uint32_t domid,
792                        uint32_t vcpu,
793                        vcpu_guest_context_any_t *ctxt);
794 /**
795  * This function will return information about one or more domains, using a
796  * single hypercall.  The domain information will be stored into the supplied
797  * array of xc_domaininfo_t structures.
798  *
799  * @parm xch a handle to an open hypervisor interface
800  * @parm first_domain the first domain to enumerate information from.
801  *                    Domains are currently enumerate in order of creation.
802  * @parm max_domains the number of elements in info
803  * @parm info an array of max_doms size that will contain the information for
804  *            the enumerated domains.
805  * @return the number of domains enumerated or -1 on error
806  */
807 int xc_domain_getinfolist(xc_interface *xch,
808                           uint32_t first_domain,
809                           unsigned int max_domains,
810                           xc_domaininfo_t *info);
811 
812 /**
813  * This function set p2m for broken page
814  * &parm xch a handle to an open hypervisor interface
815  * @parm domid the domain id which broken page belong to
816  * @parm pfn the pfn number of the broken page
817  * @return 0 on success, -1 on failure
818  */
819 int xc_set_broken_page_p2m(xc_interface *xch,
820                            uint32_t domid,
821                            unsigned long pfn);
822 
823 /**
824  * This function returns information about the context of a hvm domain
825  * @parm xch a handle to an open hypervisor interface
826  * @parm domid the domain to get information from
827  * @parm ctxt_buf a pointer to a structure to store the execution context of
828  *            the hvm domain
829  * @parm size the size of ctxt_buf in bytes
830  * @return 0 on success, -1 on failure
831  */
832 int xc_domain_hvm_getcontext(xc_interface *xch,
833                              uint32_t domid,
834                              uint8_t *ctxt_buf,
835                              uint32_t size);
836 
837 
838 /**
839  * This function returns one element of the context of a hvm domain
840  * @parm xch a handle to an open hypervisor interface
841  * @parm domid the domain to get information from
842  * @parm typecode which type of elemnt required
843  * @parm instance which instance of the type
844  * @parm ctxt_buf a pointer to a structure to store the execution context of
845  *            the hvm domain
846  * @parm size the size of ctxt_buf (must be >= HVM_SAVE_LENGTH(typecode))
847  * @return 0 on success, -1 on failure
848  */
849 int xc_domain_hvm_getcontext_partial(xc_interface *xch,
850                                      uint32_t domid,
851                                      uint16_t typecode,
852                                      uint16_t instance,
853                                      void *ctxt_buf,
854                                      uint32_t size);
855 
856 /**
857  * This function will set the context for hvm domain
858  *
859  * @parm xch a handle to an open hypervisor interface
860  * @parm domid the domain to set the hvm domain context for
861  * @parm hvm_ctxt pointer to the the hvm context with the values to set
862  * @parm size the size of hvm_ctxt in bytes
863  * @return 0 on success, -1 on failure
864  */
865 int xc_domain_hvm_setcontext(xc_interface *xch,
866                              uint32_t domid,
867                              uint8_t *hvm_ctxt,
868                              uint32_t size);
869 
870 /**
871  * This function will return guest IO ABI protocol
872  *
873  * @parm xch a handle to an open hypervisor interface
874  * @parm domid the domain to get IO ABI protocol for
875  * @return guest protocol on success, NULL on failure
876  */
877 const char *xc_domain_get_native_protocol(xc_interface *xch,
878                                           uint32_t domid);
879 
880 /**
881  * This function returns information about the execution context of a
882  * particular vcpu of a domain.
883  *
884  * @parm xch a handle to an open hypervisor interface
885  * @parm domid the domain to get information from
886  * @parm vcpu the vcpu number
887  * @parm ctxt a pointer to a structure to store the execution context of the
888  *            domain
889  * @return 0 on success, -1 on failure
890  */
891 int xc_vcpu_getcontext(xc_interface *xch,
892                        uint32_t domid,
893                        uint32_t vcpu,
894                        vcpu_guest_context_any_t *ctxt);
895 
896 /**
897  * This function initializes the vuart emulation and returns
898  * the event to be used by the backend for communicating with
899  * the emulation code.
900  *
901  * @parm xch a handle to an open hypervisor interface
902  * #parm type type of vuart
903  * @parm domid the domain to get information from
904  * @parm console_domid the domid of the backend console
905  * @parm gfn the guest pfn to be used as the ring buffer
906  * @parm evtchn the event channel to be used for events
907  * @return 0 on success, negative error on failure
908  */
909 int xc_dom_vuart_init(xc_interface *xch,
910                       uint32_t type,
911                       uint32_t domid,
912                       uint32_t console_domid,
913                       xen_pfn_t gfn,
914                       evtchn_port_t *evtchn);
915 
916 /**
917  * This function returns information about the XSAVE state of a particular
918  * vcpu of a domain. If extstate->size and extstate->xfeature_mask are 0,
919  * the call is considered a query to retrieve them and the buffer is not
920  * filled.
921  *
922  * @parm xch a handle to an open hypervisor interface
923  * @parm domid the domain to get information from
924  * @parm vcpu the vcpu number
925  * @parm extstate a pointer to a structure to store the XSAVE state of the
926  *                domain
927  * @return 0 on success, negative error code on failure
928  */
929 int xc_vcpu_get_extstate(xc_interface *xch,
930                          uint32_t domid,
931                          uint32_t vcpu,
932                          xc_vcpu_extstate_t *extstate);
933 
934 typedef struct xen_domctl_getvcpuinfo xc_vcpuinfo_t;
935 int xc_vcpu_getinfo(xc_interface *xch,
936                     uint32_t domid,
937                     uint32_t vcpu,
938                     xc_vcpuinfo_t *info);
939 
940 long long xc_domain_get_cpu_usage(xc_interface *xch,
941                                   uint32_t domid,
942                                   int vcpu);
943 
944 int xc_domain_sethandle(xc_interface *xch, uint32_t domid,
945                         xen_domain_handle_t handle);
946 
947 typedef struct xen_domctl_shadow_op_stats xc_shadow_op_stats_t;
948 int xc_shadow_control(xc_interface *xch,
949                       uint32_t domid,
950                       unsigned int sop,
951                       xc_hypercall_buffer_t *dirty_bitmap,
952                       unsigned long pages,
953                       unsigned long *mb,
954                       uint32_t mode,
955                       xc_shadow_op_stats_t *stats);
956 
957 int xc_sched_credit_domain_set(xc_interface *xch,
958                                uint32_t domid,
959                                struct xen_domctl_sched_credit *sdom);
960 
961 int xc_sched_credit_domain_get(xc_interface *xch,
962                                uint32_t domid,
963                                struct xen_domctl_sched_credit *sdom);
964 int xc_sched_credit_params_set(xc_interface *xch,
965                                uint32_t cpupool_id,
966                                struct xen_sysctl_credit_schedule *schedule);
967 int xc_sched_credit_params_get(xc_interface *xch,
968                                uint32_t cpupool_id,
969                                struct xen_sysctl_credit_schedule *schedule);
970 
971 int xc_sched_credit2_params_set(xc_interface *xch,
972                                 uint32_t cpupool_id,
973                                 struct xen_sysctl_credit2_schedule *schedule);
974 int xc_sched_credit2_params_get(xc_interface *xch,
975                                 uint32_t cpupool_id,
976                                 struct xen_sysctl_credit2_schedule *schedule);
977 int xc_sched_credit2_domain_set(xc_interface *xch,
978                                 uint32_t domid,
979                                 struct xen_domctl_sched_credit2 *sdom);
980 int xc_sched_credit2_domain_get(xc_interface *xch,
981                                 uint32_t domid,
982                                 struct xen_domctl_sched_credit2 *sdom);
983 
984 int xc_sched_rtds_domain_set(xc_interface *xch,
985                              uint32_t domid,
986                              struct xen_domctl_sched_rtds *sdom);
987 int xc_sched_rtds_domain_get(xc_interface *xch,
988                              uint32_t domid,
989                              struct xen_domctl_sched_rtds *sdom);
990 int xc_sched_rtds_vcpu_set(xc_interface *xch,
991                            uint32_t domid,
992                            struct xen_domctl_schedparam_vcpu *vcpus,
993                            uint32_t num_vcpus);
994 int xc_sched_rtds_vcpu_get(xc_interface *xch,
995                            uint32_t domid,
996                            struct xen_domctl_schedparam_vcpu *vcpus,
997                            uint32_t num_vcpus);
998 
999 int
1000 xc_sched_arinc653_schedule_set(
1001     xc_interface *xch,
1002     uint32_t cpupool_id,
1003     struct xen_sysctl_arinc653_schedule *schedule);
1004 
1005 int
1006 xc_sched_arinc653_schedule_get(
1007     xc_interface *xch,
1008     uint32_t cpupool_id,
1009     struct xen_sysctl_arinc653_schedule *schedule);
1010 
1011 /**
1012  * This function sends a trigger to a domain.
1013  *
1014  * @parm xch a handle to an open hypervisor interface
1015  * @parm domid the domain id to send trigger
1016  * @parm trigger the trigger type
1017  * @parm vcpu the vcpu number to send trigger
1018  * return 0 on success, -1 on failure
1019  */
1020 int xc_domain_send_trigger(xc_interface *xch,
1021                            uint32_t domid,
1022                            uint32_t trigger,
1023                            uint32_t vcpu);
1024 
1025 /**
1026  * This function enables or disable debugging of a domain.
1027  *
1028  * @parm xch a handle to an open hypervisor interface
1029  * @parm domid the domain id to send trigger
1030  * @parm enable true to enable debugging
1031  * return 0 on success, -1 on failure
1032  */
1033 int xc_domain_setdebugging(xc_interface *xch,
1034                            uint32_t domid,
1035                            unsigned int enable);
1036 
1037 /**
1038  * This function audits the (top level) p2m of a domain
1039  * and returns the different error counts, if any.
1040  *
1041  * @parm xch a handle to an open hypervisor interface
1042  * @parm domid the domain id whose top level p2m we
1043  *       want to audit
1044  * @parm orphans count of m2p entries for valid
1045  *       domain pages containing an invalid value
1046  * @parm m2p_bad count of m2p entries mismatching the
1047  *       associated p2m entry for this domain
1048  * @parm p2m_bad count of p2m entries for this domain
1049  *       mismatching the associated m2p entry
1050  * return 0 on success, -1 on failure
1051  * errno values on failure include:
1052  *          -ENOSYS: not implemented
1053  *          -EFAULT: could not copy results back to guest
1054  */
1055 int xc_domain_p2m_audit(xc_interface *xch,
1056                         uint32_t domid,
1057                         uint64_t *orphans,
1058                         uint64_t *m2p_bad,
1059                         uint64_t *p2m_bad);
1060 
1061 /**
1062  * This function sets or clears the requirement that an access memory
1063  * event listener is required on the domain.
1064  *
1065  * @parm xch a handle to an open hypervisor interface
1066  * @parm domid the domain id to send trigger
1067  * @parm enable true to require a listener
1068  * return 0 on success, -1 on failure
1069  */
1070 int xc_domain_set_access_required(xc_interface *xch,
1071 				  uint32_t domid,
1072 				  unsigned int required);
1073 /**
1074  * This function sets the handler of global VIRQs sent by the hypervisor
1075  *
1076  * @parm xch a handle to an open hypervisor interface
1077  * @parm domid the domain id which will handle the VIRQ
1078  * @parm virq the virq number (VIRQ_*)
1079  * return 0 on success, -1 on failure
1080  */
1081 int xc_domain_set_virq_handler(xc_interface *xch, uint32_t domid, int virq);
1082 
1083 /*
1084  * CPUPOOL MANAGEMENT FUNCTIONS
1085  */
1086 
1087 typedef struct xc_cpupoolinfo {
1088     uint32_t cpupool_id;
1089     uint32_t sched_id;
1090     uint32_t n_dom;
1091     xc_cpumap_t cpumap;
1092 } xc_cpupoolinfo_t;
1093 
1094 #define XC_CPUPOOL_POOLID_ANY 0xFFFFFFFF
1095 
1096 /**
1097  * Create a new cpupool.
1098  *
1099  * @parm xc_handle a handle to an open hypervisor interface
1100  * @parm ppoolid pointer to the new cpupool id (in/out)
1101  * @parm sched_id id of scheduler to use for pool
1102  * return 0 on success, -1 on failure
1103  */
1104 int xc_cpupool_create(xc_interface *xch,
1105                       uint32_t *ppoolid,
1106                       uint32_t sched_id);
1107 
1108 /**
1109  * Destroy a cpupool. Pool must be unused and have no cpu assigned.
1110  *
1111  * @parm xc_handle a handle to an open hypervisor interface
1112  * @parm poolid id of the cpupool to destroy
1113  * return 0 on success, -1 on failure
1114  */
1115 int xc_cpupool_destroy(xc_interface *xch,
1116                        uint32_t poolid);
1117 
1118 /**
1119  * Get cpupool info. Returns info for up to the specified number of cpupools
1120  * starting at the given id.
1121  * @parm xc_handle a handle to an open hypervisor interface
1122  * @parm poolid lowest id for which info is returned
1123  * return cpupool info ptr (to be freed via xc_cpupool_infofree)
1124  */
1125 xc_cpupoolinfo_t *xc_cpupool_getinfo(xc_interface *xch,
1126                        uint32_t poolid);
1127 
1128 /**
1129  * Free cpupool info. Used to free info obtained via xc_cpupool_getinfo.
1130  * @parm xc_handle a handle to an open hypervisor interface
1131  * @parm info area to free
1132  */
1133 void xc_cpupool_infofree(xc_interface *xch,
1134                          xc_cpupoolinfo_t *info);
1135 
1136 /**
1137  * Add cpu to a cpupool. cpu may be -1 indicating the first unassigned.
1138  *
1139  * @parm xc_handle a handle to an open hypervisor interface
1140  * @parm poolid id of the cpupool
1141  * @parm cpu cpu number to add
1142  * return 0 on success, -1 on failure
1143  */
1144 int xc_cpupool_addcpu(xc_interface *xch,
1145                       uint32_t poolid,
1146                       int cpu);
1147 
1148 /**
1149  * Remove cpu from cpupool. cpu may be -1 indicating the last cpu of the pool.
1150  *
1151  * @parm xc_handle a handle to an open hypervisor interface
1152  * @parm poolid id of the cpupool
1153  * @parm cpu cpu number to remove
1154  * return 0 on success, -1 on failure
1155  */
1156 int xc_cpupool_removecpu(xc_interface *xch,
1157                          uint32_t poolid,
1158                          int cpu);
1159 
1160 /**
1161  * Move domain to another cpupool.
1162  *
1163  * @parm xc_handle a handle to an open hypervisor interface
1164  * @parm poolid id of the destination cpupool
1165  * @parm domid id of the domain to move
1166  * return 0 on success, -1 on failure
1167  */
1168 int xc_cpupool_movedomain(xc_interface *xch,
1169                           uint32_t poolid,
1170                           uint32_t domid);
1171 
1172 /**
1173  * Return map of cpus not in any cpupool.
1174  *
1175  * @parm xc_handle a handle to an open hypervisor interface
1176  * return cpumap array on success, NULL else
1177  */
1178 xc_cpumap_t xc_cpupool_freeinfo(xc_interface *xch);
1179 
1180 /*
1181  * EVENT CHANNEL FUNCTIONS
1182  *
1183  * None of these do any logging.
1184  */
1185 
1186 /* A port identifier is guaranteed to fit in 31 bits. */
1187 typedef int xc_evtchn_port_or_error_t;
1188 
1189 /**
1190  * This function allocates an unbound port.  Ports are named endpoints used for
1191  * interdomain communication.  This function is most useful in opening a
1192  * well-known port within a domain to receive events on.
1193  *
1194  * NOTE: If you are allocating a *local* unbound port, you probably want to
1195  * use xc_evtchn_bind_unbound_port(). This function is intended for allocating
1196  * ports *only* during domain creation.
1197  *
1198  * @parm xch a handle to an open hypervisor interface
1199  * @parm dom the ID of the local domain (the 'allocatee')
1200  * @parm remote_dom the ID of the domain who will later bind
1201  * @return allocated port (in @dom) on success, -1 on failure
1202  */
1203 xc_evtchn_port_or_error_t
1204 xc_evtchn_alloc_unbound(xc_interface *xch,
1205                         uint32_t dom,
1206                         uint32_t remote_dom);
1207 
1208 int xc_evtchn_reset(xc_interface *xch,
1209                     uint32_t dom);
1210 
1211 typedef struct evtchn_status xc_evtchn_status_t;
1212 int xc_evtchn_status(xc_interface *xch, xc_evtchn_status_t *status);
1213 
1214 
1215 
1216 int xc_physdev_pci_access_modify(xc_interface *xch,
1217                                  uint32_t domid,
1218                                  int bus,
1219                                  int dev,
1220                                  int func,
1221                                  int enable);
1222 
1223 int xc_readconsolering(xc_interface *xch,
1224                        char *buffer,
1225                        unsigned int *pnr_chars,
1226                        int clear, int incremental, uint32_t *pindex);
1227 
1228 int xc_send_debug_keys(xc_interface *xch, const char *keys);
1229 
1230 typedef struct xen_sysctl_physinfo xc_physinfo_t;
1231 typedef struct xen_sysctl_cputopo xc_cputopo_t;
1232 typedef struct xen_sysctl_numainfo xc_numainfo_t;
1233 typedef struct xen_sysctl_meminfo xc_meminfo_t;
1234 typedef struct xen_sysctl_pcitopoinfo xc_pcitopoinfo_t;
1235 
1236 typedef uint32_t xc_cpu_to_node_t;
1237 typedef uint32_t xc_cpu_to_socket_t;
1238 typedef uint32_t xc_cpu_to_core_t;
1239 typedef uint64_t xc_node_to_memsize_t;
1240 typedef uint64_t xc_node_to_memfree_t;
1241 typedef uint32_t xc_node_to_node_dist_t;
1242 
1243 int xc_physinfo(xc_interface *xch, xc_physinfo_t *info);
1244 int xc_cputopoinfo(xc_interface *xch, unsigned *max_cpus,
1245                    xc_cputopo_t *cputopo);
1246 int xc_microcode_update(xc_interface *xch, const void *buf, size_t len);
1247 int xc_numainfo(xc_interface *xch, unsigned *max_nodes,
1248                 xc_meminfo_t *meminfo, uint32_t *distance);
1249 int xc_pcitopoinfo(xc_interface *xch, unsigned num_devs,
1250                    physdev_pci_device_t *devs, uint32_t *nodes);
1251 
1252 int xc_sched_id(xc_interface *xch,
1253                 int *sched_id);
1254 
1255 int xc_machphys_mfn_list(xc_interface *xch,
1256                          unsigned long max_extents,
1257                          xen_pfn_t *extent_start);
1258 
1259 typedef struct xen_sysctl_cpuinfo xc_cpuinfo_t;
1260 int xc_getcpuinfo(xc_interface *xch, int max_cpus,
1261                   xc_cpuinfo_t *info, int *nr_cpus);
1262 
1263 int xc_domain_setmaxmem(xc_interface *xch,
1264                         uint32_t domid,
1265                         uint64_t max_memkb);
1266 
1267 int xc_domain_set_memmap_limit(xc_interface *xch,
1268                                uint32_t domid,
1269                                unsigned long map_limitkb);
1270 
1271 int xc_domain_setvnuma(xc_interface *xch,
1272                         uint32_t domid,
1273                         uint32_t nr_vnodes,
1274                         uint32_t nr_regions,
1275                         uint32_t nr_vcpus,
1276                         xen_vmemrange_t *vmemrange,
1277                         unsigned int *vdistance,
1278                         unsigned int *vcpu_to_vnode,
1279                         unsigned int *vnode_to_pnode);
1280 /*
1281  * Retrieve vnuma configuration
1282  * domid: IN, target domid
1283  * nr_vnodes: IN/OUT, number of vnodes, not NULL
1284  * nr_vmemranges: IN/OUT, number of vmemranges, not NULL
1285  * nr_vcpus: IN/OUT, number of vcpus, not NULL
1286  * vmemranges: OUT, an array which has length of nr_vmemranges
1287  * vdistance: OUT, an array which has length of nr_vnodes * nr_vnodes
1288  * vcpu_to_vnode: OUT, an array which has length of nr_vcpus
1289  */
1290 int xc_domain_getvnuma(xc_interface *xch,
1291                        uint32_t domid,
1292                        uint32_t *nr_vnodes,
1293                        uint32_t *nr_vmemranges,
1294                        uint32_t *nr_vcpus,
1295                        xen_vmemrange_t *vmemrange,
1296                        unsigned int *vdistance,
1297                        unsigned int *vcpu_to_vnode);
1298 
1299 int xc_domain_soft_reset(xc_interface *xch,
1300                          uint32_t domid);
1301 
1302 #if defined(__i386__) || defined(__x86_64__)
1303 /*
1304  * PC BIOS standard E820 types and structure.
1305  */
1306 #define E820_RAM          1
1307 #define E820_RESERVED     2
1308 #define E820_ACPI         3
1309 #define E820_NVS          4
1310 #define E820_UNUSABLE     5
1311 
1312 #define E820MAX           (128)
1313 
1314 struct e820entry {
1315     uint64_t addr;
1316     uint64_t size;
1317     uint32_t type;
1318 } __attribute__((packed));
1319 int xc_domain_set_memory_map(xc_interface *xch,
1320                                uint32_t domid,
1321                                struct e820entry entries[],
1322                                uint32_t nr_entries);
1323 
1324 int xc_get_machine_memory_map(xc_interface *xch,
1325                               struct e820entry entries[],
1326                               uint32_t max_entries);
1327 #endif
1328 
1329 int xc_reserved_device_memory_map(xc_interface *xch,
1330                                   uint32_t flags,
1331                                   uint16_t seg,
1332                                   uint8_t bus,
1333                                   uint8_t devfn,
1334                                   struct xen_reserved_device_memory entries[],
1335                                   uint32_t *max_entries);
1336 int xc_domain_set_time_offset(xc_interface *xch,
1337                               uint32_t domid,
1338                               int32_t time_offset_seconds);
1339 
1340 int xc_domain_set_tsc_info(xc_interface *xch,
1341                            uint32_t domid,
1342                            uint32_t tsc_mode,
1343                            uint64_t elapsed_nsec,
1344                            uint32_t gtsc_khz,
1345                            uint32_t incarnation);
1346 
1347 int xc_domain_get_tsc_info(xc_interface *xch,
1348                            uint32_t domid,
1349                            uint32_t *tsc_mode,
1350                            uint64_t *elapsed_nsec,
1351                            uint32_t *gtsc_khz,
1352                            uint32_t *incarnation);
1353 
1354 int xc_domain_disable_migrate(xc_interface *xch, uint32_t domid);
1355 
1356 int xc_domain_maximum_gpfn(xc_interface *xch, uint32_t domid, xen_pfn_t *gpfns);
1357 
1358 int xc_domain_nr_gpfns(xc_interface *xch, uint32_t domid, xen_pfn_t *gpfns);
1359 
1360 int xc_domain_increase_reservation(xc_interface *xch,
1361                                    uint32_t domid,
1362                                    unsigned long nr_extents,
1363                                    unsigned int extent_order,
1364                                    unsigned int mem_flags,
1365                                    xen_pfn_t *extent_start);
1366 
1367 int xc_domain_increase_reservation_exact(xc_interface *xch,
1368                                          uint32_t domid,
1369                                          unsigned long nr_extents,
1370                                          unsigned int extent_order,
1371                                          unsigned int mem_flags,
1372                                          xen_pfn_t *extent_start);
1373 
1374 int xc_domain_decrease_reservation(xc_interface *xch,
1375                                    uint32_t domid,
1376                                    unsigned long nr_extents,
1377                                    unsigned int extent_order,
1378                                    xen_pfn_t *extent_start);
1379 
1380 int xc_domain_decrease_reservation_exact(xc_interface *xch,
1381                                          uint32_t domid,
1382                                          unsigned long nr_extents,
1383                                          unsigned int extent_order,
1384                                          xen_pfn_t *extent_start);
1385 
1386 int xc_domain_add_to_physmap(xc_interface *xch,
1387                              uint32_t domid,
1388                              unsigned int space,
1389                              unsigned long idx,
1390                              xen_pfn_t gpfn);
1391 
1392 int xc_domain_add_to_physmap_batch(xc_interface *xch,
1393                                    uint32_t domid,
1394                                    uint32_t foreign_domid,
1395                                    unsigned int space,
1396                                    unsigned int size,
1397                                    xen_ulong_t *idxs,
1398                                    xen_pfn_t *gfpns,
1399                                    int *errs);
1400 
1401 int xc_domain_remove_from_physmap(xc_interface *xch,
1402                                   uint32_t domid,
1403                                   xen_pfn_t gpfn);
1404 
1405 int xc_domain_populate_physmap(xc_interface *xch,
1406                                uint32_t domid,
1407                                unsigned long nr_extents,
1408                                unsigned int extent_order,
1409                                unsigned int mem_flags,
1410                                xen_pfn_t *extent_start);
1411 
1412 int xc_domain_populate_physmap_exact(xc_interface *xch,
1413                                      uint32_t domid,
1414                                      unsigned long nr_extents,
1415                                      unsigned int extent_order,
1416                                      unsigned int mem_flags,
1417                                      xen_pfn_t *extent_start);
1418 
1419 int xc_domain_claim_pages(xc_interface *xch,
1420                                uint32_t domid,
1421                                unsigned long nr_pages);
1422 
1423 int xc_domain_memory_exchange_pages(xc_interface *xch,
1424                                     uint32_t domid,
1425                                     unsigned long nr_in_extents,
1426                                     unsigned int in_order,
1427                                     xen_pfn_t *in_extents,
1428                                     unsigned long nr_out_extents,
1429                                     unsigned int out_order,
1430                                     xen_pfn_t *out_extents);
1431 
1432 int xc_domain_set_pod_target(xc_interface *xch,
1433                              uint32_t domid,
1434                              uint64_t target_pages,
1435                              uint64_t *tot_pages,
1436                              uint64_t *pod_cache_pages,
1437                              uint64_t *pod_entries);
1438 
1439 int xc_domain_get_pod_target(xc_interface *xch,
1440                              uint32_t domid,
1441                              uint64_t *tot_pages,
1442                              uint64_t *pod_cache_pages,
1443                              uint64_t *pod_entries);
1444 
1445 int xc_domain_ioport_permission(xc_interface *xch,
1446                                 uint32_t domid,
1447                                 uint32_t first_port,
1448                                 uint32_t nr_ports,
1449                                 uint32_t allow_access);
1450 
1451 int xc_domain_irq_permission(xc_interface *xch,
1452                              uint32_t domid,
1453                              uint8_t pirq,
1454                              uint8_t allow_access);
1455 
1456 int xc_domain_iomem_permission(xc_interface *xch,
1457                                uint32_t domid,
1458                                unsigned long first_mfn,
1459                                unsigned long nr_mfns,
1460                                uint8_t allow_access);
1461 
1462 unsigned long xc_make_page_below_4G(xc_interface *xch, uint32_t domid,
1463                                     unsigned long mfn);
1464 
1465 typedef xen_sysctl_perfc_desc_t xc_perfc_desc_t;
1466 typedef xen_sysctl_perfc_val_t xc_perfc_val_t;
1467 int xc_perfc_reset(xc_interface *xch);
1468 int xc_perfc_query_number(xc_interface *xch,
1469                           int *nbr_desc,
1470                           int *nbr_val);
1471 int xc_perfc_query(xc_interface *xch,
1472                    xc_hypercall_buffer_t *desc,
1473                    xc_hypercall_buffer_t *val);
1474 
1475 typedef xen_sysctl_lockprof_data_t xc_lockprof_data_t;
1476 int xc_lockprof_reset(xc_interface *xch);
1477 int xc_lockprof_query_number(xc_interface *xch,
1478                              uint32_t *n_elems);
1479 int xc_lockprof_query(xc_interface *xch,
1480                       uint32_t *n_elems,
1481                       uint64_t *time,
1482                       xc_hypercall_buffer_t *data);
1483 
1484 void *xc_memalign(xc_interface *xch, size_t alignment, size_t size);
1485 
1486 /**
1487  * Avoid using this function, as it does not work for all cases (such
1488  * as 4M superpages, or guests using PSE36). Only used for debugging.
1489  *
1490  * Translates a virtual address in the context of a given domain and
1491  * vcpu returning the GFN containing the address (that is, an MFN for
1492  * PV guests, a PFN for HVM guests).  Returns 0 for failure.
1493  *
1494  * @parm xch a handle on an open hypervisor interface
1495  * @parm dom the domain to perform the translation in
1496  * @parm vcpu the vcpu to perform the translation on
1497  * @parm virt the virtual address to translate
1498  */
1499 unsigned long xc_translate_foreign_address(xc_interface *xch, uint32_t dom,
1500                                            int vcpu, unsigned long long virt);
1501 
1502 
1503 int xc_copy_to_domain_page(xc_interface *xch, uint32_t domid,
1504                            unsigned long dst_pfn, const char *src_page);
1505 
1506 int xc_clear_domain_pages(xc_interface *xch, uint32_t domid,
1507                           unsigned long dst_pfn, int num);
1508 
xc_clear_domain_page(xc_interface * xch,uint32_t domid,unsigned long dst_pfn)1509 static inline int xc_clear_domain_page(xc_interface *xch, uint32_t domid,
1510                                        unsigned long dst_pfn)
1511 {
1512     return xc_clear_domain_pages(xch, domid, dst_pfn, 1);
1513 }
1514 
1515 int xc_mmuext_op(xc_interface *xch, struct mmuext_op *op, unsigned int nr_ops,
1516                  uint32_t dom);
1517 
1518 /* System wide memory properties */
1519 int xc_maximum_ram_page(xc_interface *xch, unsigned long *max_mfn);
1520 
1521 /* Get current total pages allocated to a domain. */
1522 long xc_get_tot_pages(xc_interface *xch, uint32_t domid);
1523 
1524 /**
1525  * This function retrieves the the number of bytes available
1526  * in the heap in a specific range of address-widths and nodes.
1527  *
1528  * @parm xch a handle to an open hypervisor interface
1529  * @parm domid the domain to query
1530  * @parm min_width the smallest address width to query (0 if don't care)
1531  * @parm max_width the largest address width to query (0 if don't care)
1532  * @parm node the node to query (-1 for all)
1533  * @parm *bytes caller variable to put total bytes counted
1534  * @return 0 on success, <0 on failure.
1535  */
1536 int xc_availheap(xc_interface *xch, int min_width, int max_width, int node,
1537                  uint64_t *bytes);
1538 
1539 /*
1540  * Trace Buffer Operations
1541  */
1542 
1543 /**
1544  * xc_tbuf_enable - enable tracing buffers
1545  *
1546  * @parm xch a handle to an open hypervisor interface
1547  * @parm cnt size of tracing buffers to create (in pages)
1548  * @parm mfn location to store mfn of the trace buffers to
1549  * @parm size location to store the size (in bytes) of a trace buffer to
1550  *
1551  * Gets the machine address of the trace pointer area and the size of the
1552  * per CPU buffers.
1553  */
1554 int xc_tbuf_enable(xc_interface *xch, unsigned long pages,
1555                    unsigned long *mfn, unsigned long *size);
1556 
1557 /*
1558  * Disable tracing buffers.
1559  */
1560 int xc_tbuf_disable(xc_interface *xch);
1561 
1562 /**
1563  * This function sets the size of the trace buffers. Setting the size
1564  * is currently a one-shot operation that may be performed either at boot
1565  * time or via this interface, not both. The buffer size must be set before
1566  * enabling tracing.
1567  *
1568  * @parm xch a handle to an open hypervisor interface
1569  * @parm size the size in pages per cpu for the trace buffers
1570  * @return 0 on success, -1 on failure.
1571  */
1572 int xc_tbuf_set_size(xc_interface *xch, unsigned long size);
1573 
1574 /**
1575  * This function retrieves the current size of the trace buffers.
1576  * Note that the size returned is in terms of bytes, not pages.
1577 
1578  * @parm xch a handle to an open hypervisor interface
1579  * @parm size will contain the size in bytes for the trace buffers
1580  * @return 0 on success, -1 on failure.
1581  */
1582 int xc_tbuf_get_size(xc_interface *xch, unsigned long *size);
1583 
1584 int xc_tbuf_set_cpu_mask(xc_interface *xch, xc_cpumap_t mask);
1585 
1586 int xc_tbuf_set_evt_mask(xc_interface *xch, uint32_t mask);
1587 
1588 int xc_domctl(xc_interface *xch, struct xen_domctl *domctl);
1589 int xc_sysctl(xc_interface *xch, struct xen_sysctl *sysctl);
1590 
1591 int xc_version(xc_interface *xch, int cmd, void *arg);
1592 
1593 int xc_flask_op(xc_interface *xch, xen_flask_op_t *op);
1594 
1595 /*
1596  * Subscribe to domain suspend via evtchn.
1597  * Returns -1 on failure, in which case errno will be set appropriately.
1598  * Just calls XEN_DOMCTL_subscribe - see the caveats for that domctl
1599  * (in its doc comment in domctl.h).
1600  */
1601 int xc_domain_subscribe_for_suspend(
1602     xc_interface *xch, uint32_t domid, evtchn_port_t port);
1603 
1604 /**************************
1605  * GRANT TABLE OPERATIONS *
1606  **************************/
1607 
1608 /*
1609  * These functions sometimes log messages as above, but not always.
1610  */
1611 
1612 
1613 int xc_gnttab_op(xc_interface *xch, int cmd,
1614                  void * op, int op_size, int count);
1615 /* Logs iff hypercall bounce fails, otherwise doesn't. */
1616 
1617 int xc_gnttab_query_size(xc_interface *xch, struct gnttab_query_size *query);
1618 int xc_gnttab_get_version(xc_interface *xch, uint32_t domid); /* Never logs */
1619 grant_entry_v1_t *xc_gnttab_map_table_v1(xc_interface *xch, uint32_t domid, int *gnt_num);
1620 grant_entry_v2_t *xc_gnttab_map_table_v2(xc_interface *xch, uint32_t domid, int *gnt_num);
1621 /* Sometimes these don't set errno [fixme], and sometimes they don't log. */
1622 
1623 int xc_physdev_map_pirq(xc_interface *xch,
1624                         uint32_t domid,
1625                         int index,
1626                         int *pirq);
1627 
1628 int xc_physdev_map_pirq_msi(xc_interface *xch,
1629                             uint32_t domid,
1630                             int index,
1631                             int *pirq,
1632                             int devfn,
1633                             int bus,
1634                             int entry_nr,
1635                             uint64_t table_base);
1636 
1637 int xc_physdev_unmap_pirq(xc_interface *xch,
1638                           uint32_t domid,
1639                           int pirq);
1640 
1641 /*
1642  *  LOGGING AND ERROR REPORTING
1643  */
1644 
1645 
1646 #define XC_MAX_ERROR_MSG_LEN 1024
1647 typedef struct xc_error {
1648   enum xc_error_code code;
1649   char message[XC_MAX_ERROR_MSG_LEN];
1650 } xc_error;
1651 
1652 
1653 /*
1654  * Convert an error code or level into a text description.  Return values
1655  * are pointers to fixed strings and do not need to be freed.
1656  * Do not fail, but return pointers to generic strings if fed bogus input.
1657  */
1658 const char *xc_error_code_to_desc(int code);
1659 
1660 /*
1661  * Convert an errno value to a text description.
1662  */
1663 const char *xc_strerror(xc_interface *xch, int errcode);
1664 
1665 
1666 /*
1667  * Return a pointer to the last error with level XC_REPORT_ERROR. This
1668  * pointer and the data pointed to are only valid until the next call
1669  * to libxc in the same thread.
1670  */
1671 const xc_error *xc_get_last_error(xc_interface *handle);
1672 
1673 /*
1674  * Clear the last error
1675  */
1676 void xc_clear_last_error(xc_interface *xch);
1677 
1678 int xc_hvm_param_set(xc_interface *handle, uint32_t dom, uint32_t param, uint64_t value);
1679 int xc_hvm_param_get(xc_interface *handle, uint32_t dom, uint32_t param, uint64_t *value);
1680 
1681 /* Deprecated: use xc_hvm_param_set/get() instead. */
1682 int xc_set_hvm_param(xc_interface *handle, uint32_t dom, int param, unsigned long value);
1683 int xc_get_hvm_param(xc_interface *handle, uint32_t dom, int param, unsigned long *value);
1684 
1685 /* HVM guest pass-through */
1686 int xc_assign_device(xc_interface *xch,
1687                      uint32_t domid,
1688                      uint32_t machine_sbdf,
1689                      uint32_t flag);
1690 
1691 int xc_get_device_group(xc_interface *xch,
1692                      uint32_t domid,
1693                      uint32_t machine_sbdf,
1694                      uint32_t max_sdevs,
1695                      uint32_t *num_sdevs,
1696                      uint32_t *sdev_array);
1697 
1698 int xc_test_assign_device(xc_interface *xch,
1699                           uint32_t domid,
1700                           uint32_t machine_sbdf);
1701 
1702 int xc_deassign_device(xc_interface *xch,
1703                      uint32_t domid,
1704                      uint32_t machine_sbdf);
1705 
1706 int xc_assign_dt_device(xc_interface *xch,
1707                         uint32_t domid,
1708                         char *path);
1709 int xc_test_assign_dt_device(xc_interface *xch,
1710                              uint32_t domid,
1711                              char *path);
1712 int xc_deassign_dt_device(xc_interface *xch,
1713                           uint32_t domid,
1714                           char *path);
1715 
1716 int xc_domain_memory_mapping(xc_interface *xch,
1717                              uint32_t domid,
1718                              unsigned long first_gfn,
1719                              unsigned long first_mfn,
1720                              unsigned long nr_mfns,
1721                              uint32_t add_mapping);
1722 
1723 int xc_domain_ioport_mapping(xc_interface *xch,
1724                              uint32_t domid,
1725                              uint32_t first_gport,
1726                              uint32_t first_mport,
1727                              uint32_t nr_ports,
1728                              uint32_t add_mapping);
1729 
1730 int xc_domain_update_msi_irq(
1731     xc_interface *xch,
1732     uint32_t domid,
1733     uint32_t gvec,
1734     uint32_t pirq,
1735     uint32_t gflags,
1736     uint64_t gtable);
1737 
1738 int xc_domain_unbind_msi_irq(xc_interface *xch,
1739                              uint32_t domid,
1740                              uint32_t gvec,
1741                              uint32_t pirq,
1742                              uint32_t gflags);
1743 
1744 int xc_domain_bind_pt_irq(xc_interface *xch,
1745                           uint32_t domid,
1746                           uint8_t machine_irq,
1747                           uint8_t irq_type,
1748                           uint8_t bus,
1749                           uint8_t device,
1750                           uint8_t intx,
1751                           uint8_t isa_irq);
1752 
1753 int xc_domain_unbind_pt_irq(xc_interface *xch,
1754                           uint32_t domid,
1755                           uint8_t machine_irq,
1756                           uint8_t irq_type,
1757                           uint8_t bus,
1758                           uint8_t device,
1759                           uint8_t intx,
1760                           uint8_t isa_irq);
1761 
1762 int xc_domain_bind_pt_pci_irq(xc_interface *xch,
1763                               uint32_t domid,
1764                               uint8_t machine_irq,
1765                               uint8_t bus,
1766                               uint8_t device,
1767                               uint8_t intx);
1768 
1769 int xc_domain_bind_pt_isa_irq(xc_interface *xch,
1770                               uint32_t domid,
1771                               uint8_t machine_irq);
1772 
1773 int xc_domain_bind_pt_spi_irq(xc_interface *xch,
1774                               uint32_t domid,
1775                               uint16_t vspi,
1776                               uint16_t spi);
1777 
1778 int xc_domain_unbind_pt_spi_irq(xc_interface *xch,
1779                                 uint32_t domid,
1780                                 uint16_t vspi,
1781                                 uint16_t spi);
1782 
1783 /* Set the target domain */
1784 int xc_domain_set_target(xc_interface *xch,
1785                          uint32_t domid,
1786                          uint32_t target);
1787 
1788 /* Control the domain for debug */
1789 int xc_domain_debug_control(xc_interface *xch,
1790                             uint32_t domid,
1791                             uint32_t sop,
1792                             uint32_t vcpu);
1793 
1794 #if defined(__i386__) || defined(__x86_64__)
1795 
1796 /*
1797  * CPUID policy data, expressed in the legacy XEND format.
1798  *
1799  * Policy is an array of strings, 32 chars long:
1800  *   policy[0] = eax
1801  *   policy[1] = ebx
1802  *   policy[2] = ecx
1803  *   policy[3] = edx
1804  *
1805  * The format of the string is the following:
1806  *   '1' -> force to 1
1807  *   '0' -> force to 0
1808  *   'x' -> we don't care (use default)
1809  *   'k' -> pass through host value
1810  *   's' -> legacy alias for 'k'
1811  */
1812 struct xc_xend_cpuid {
1813     union {
1814         struct {
1815             uint32_t leaf, subleaf;
1816         };
1817         uint32_t input[2];
1818     };
1819     char *policy[4];
1820 };
1821 
1822 /*
1823  * Make adjustments to the CPUID settings for a domain.
1824  *
1825  * This path is used in two cases.  First, for fresh boots of the domain, and
1826  * secondly for migrate-in/restore of pre-4.14 guests (where CPUID data was
1827  * missing from the stream).  The @restore parameter distinguishes these
1828  * cases, and the generated policy must be compatible with a 4.13.
1829  *
1830  * Either pass a full new @featureset (and @nr_features), or adjust individual
1831  * features (@pae).
1832  *
1833  * Then (optionally) apply legacy XEND overrides (@xend) to the result.
1834  */
1835 int xc_cpuid_apply_policy(xc_interface *xch,
1836                           uint32_t domid, bool restore,
1837                           const uint32_t *featureset,
1838                           unsigned int nr_features, bool pae,
1839                           const struct xc_xend_cpuid *xend);
1840 int xc_mca_op(xc_interface *xch, struct xen_mc *mc);
1841 int xc_mca_op_inject_v2(xc_interface *xch, unsigned int flags,
1842                         xc_cpumap_t cpumap, unsigned int nr_cpus);
1843 #endif
1844 
1845 struct xc_px_val {
1846     uint64_t freq;        /* Px core frequency */
1847     uint64_t residency;   /* Px residency time */
1848     uint64_t count;       /* Px transition count */
1849 };
1850 
1851 struct xc_px_stat {
1852     uint8_t total;        /* total Px states */
1853     uint8_t usable;       /* usable Px states */
1854     uint8_t last;         /* last Px state */
1855     uint8_t cur;          /* current Px state */
1856     uint64_t *trans_pt;   /* Px transition table */
1857     struct xc_px_val *pt;
1858 };
1859 
1860 int xc_pm_get_max_px(xc_interface *xch, int cpuid, int *max_px);
1861 int xc_pm_get_pxstat(xc_interface *xch, int cpuid, struct xc_px_stat *pxpt);
1862 int xc_pm_reset_pxstat(xc_interface *xch, int cpuid);
1863 
1864 struct xc_cx_stat {
1865     uint32_t nr;           /* entry nr in triggers[]/residencies[], incl C0 */
1866     uint32_t last;         /* last Cx state */
1867     uint64_t idle_time;    /* idle time from boot */
1868     uint64_t *triggers;    /* Cx trigger counts */
1869     uint64_t *residencies; /* Cx residencies */
1870     uint32_t nr_pc;        /* entry nr in pc[] */
1871     uint32_t nr_cc;        /* entry nr in cc[] */
1872     uint64_t *pc;          /* 1-biased indexing (i.e. excl C0) */
1873     uint64_t *cc;          /* 1-biased indexing (i.e. excl C0) */
1874 };
1875 typedef struct xc_cx_stat xc_cx_stat_t;
1876 
1877 int xc_pm_get_max_cx(xc_interface *xch, int cpuid, int *max_cx);
1878 int xc_pm_get_cxstat(xc_interface *xch, int cpuid, struct xc_cx_stat *cxpt);
1879 int xc_pm_reset_cxstat(xc_interface *xch, int cpuid);
1880 
1881 int xc_cpu_online(xc_interface *xch, int cpu);
1882 int xc_cpu_offline(xc_interface *xch, int cpu);
1883 int xc_smt_enable(xc_interface *xch);
1884 int xc_smt_disable(xc_interface *xch);
1885 
1886 /*
1887  * cpufreq para name of this structure named
1888  * same as sysfs file name of native linux
1889  */
1890 typedef struct xen_userspace xc_userspace_t;
1891 typedef struct xen_ondemand xc_ondemand_t;
1892 
1893 struct xc_get_cpufreq_para {
1894     /* IN/OUT variable */
1895     uint32_t cpu_num;
1896     uint32_t freq_num;
1897     uint32_t gov_num;
1898 
1899     /* for all governors */
1900     /* OUT variable */
1901     uint32_t *affected_cpus;
1902     uint32_t *scaling_available_frequencies;
1903     char     *scaling_available_governors;
1904     char scaling_driver[CPUFREQ_NAME_LEN];
1905 
1906     uint32_t cpuinfo_cur_freq;
1907     uint32_t cpuinfo_max_freq;
1908     uint32_t cpuinfo_min_freq;
1909     uint32_t scaling_cur_freq;
1910 
1911     char scaling_governor[CPUFREQ_NAME_LEN];
1912     uint32_t scaling_max_freq;
1913     uint32_t scaling_min_freq;
1914 
1915     /* for specific governor */
1916     union {
1917         xc_userspace_t userspace;
1918         xc_ondemand_t ondemand;
1919     } u;
1920 
1921     int32_t turbo_enabled;
1922 };
1923 
1924 int xc_get_cpufreq_para(xc_interface *xch, int cpuid,
1925                         struct xc_get_cpufreq_para *user_para);
1926 int xc_set_cpufreq_gov(xc_interface *xch, int cpuid, char *govname);
1927 int xc_set_cpufreq_para(xc_interface *xch, int cpuid,
1928                         int ctrl_type, int ctrl_value);
1929 int xc_get_cpufreq_avgfreq(xc_interface *xch, int cpuid, int *avg_freq);
1930 
1931 int xc_set_sched_opt_smt(xc_interface *xch, uint32_t value);
1932 
1933 int xc_get_cpuidle_max_cstate(xc_interface *xch, uint32_t *value);
1934 int xc_set_cpuidle_max_cstate(xc_interface *xch, uint32_t value);
1935 
1936 int xc_get_cpuidle_max_csubstate(xc_interface *xch, uint32_t *value);
1937 int xc_set_cpuidle_max_csubstate(xc_interface *xch, uint32_t value);
1938 
1939 int xc_enable_turbo(xc_interface *xch, int cpuid);
1940 int xc_disable_turbo(xc_interface *xch, int cpuid);
1941 
1942 /**
1943  * altp2m operations
1944  */
1945 
1946 int xc_altp2m_get_domain_state(xc_interface *handle, uint32_t dom, bool *state);
1947 int xc_altp2m_set_domain_state(xc_interface *handle, uint32_t dom, bool state);
1948 int xc_altp2m_set_vcpu_enable_notify(xc_interface *handle, uint32_t domid,
1949                                      uint32_t vcpuid, xen_pfn_t gfn);
1950 int xc_altp2m_set_vcpu_disable_notify(xc_interface *handle, uint32_t domid,
1951                                       uint32_t vcpuid);
1952 int xc_altp2m_create_view(xc_interface *handle, uint32_t domid,
1953                           xenmem_access_t default_access, uint16_t *view_id);
1954 int xc_altp2m_destroy_view(xc_interface *handle, uint32_t domid,
1955                            uint16_t view_id);
1956 /* Switch all vCPUs of the domain to the specified altp2m view */
1957 int xc_altp2m_switch_to_view(xc_interface *handle, uint32_t domid,
1958                              uint16_t view_id);
1959 int xc_altp2m_set_suppress_ve(xc_interface *handle, uint32_t domid,
1960                               uint16_t view_id, xen_pfn_t gfn, bool sve);
1961 int xc_altp2m_set_supress_ve_multi(xc_interface *handle, uint32_t domid,
1962                                    uint16_t view_id, xen_pfn_t first_gfn,
1963                                    xen_pfn_t last_gfn, bool sve,
1964                                    xen_pfn_t *error_gfn, int32_t *error_code);
1965 int xc_altp2m_get_suppress_ve(xc_interface *handle, uint32_t domid,
1966                               uint16_t view_id, xen_pfn_t gfn, bool *sve);
1967 int xc_altp2m_set_mem_access(xc_interface *handle, uint32_t domid,
1968                              uint16_t view_id, xen_pfn_t gfn,
1969                              xenmem_access_t access);
1970 int xc_altp2m_set_mem_access_multi(xc_interface *handle, uint32_t domid,
1971                                    uint16_t view_id, uint8_t *access,
1972                                    uint64_t *gfns, uint32_t nr);
1973 int xc_altp2m_get_mem_access(xc_interface *handle, uint32_t domid,
1974                              uint16_t view_id, xen_pfn_t gfn,
1975                              xenmem_access_t *access);
1976 int xc_altp2m_change_gfn(xc_interface *handle, uint32_t domid,
1977                          uint16_t view_id, xen_pfn_t old_gfn,
1978                          xen_pfn_t new_gfn);
1979 int xc_altp2m_get_vcpu_p2m_idx(xc_interface *handle, uint32_t domid,
1980                                uint32_t vcpuid, uint16_t *p2midx);
1981 /*
1982  * Set view visibility for xc_altp2m_switch_to_view and vmfunc.
1983  * Note: If altp2m mode is set to mixed the guest is able to change the view
1984  * visibility and then call vmfunc.
1985  */
1986 int xc_altp2m_set_visibility(xc_interface *handle, uint32_t domid,
1987                              uint16_t view_id, bool visible);
1988 
1989 /**
1990  * Mem paging operations.
1991  * Paging is supported only on the x86 architecture in 64 bit mode, with
1992  * Hardware-Assisted Paging (i.e. Intel EPT, AMD NPT). Moreover, AMD NPT
1993  * support is considered experimental.
1994  */
1995 int xc_mem_paging_enable(xc_interface *xch, uint32_t domain_id, uint32_t *port);
1996 int xc_mem_paging_disable(xc_interface *xch, uint32_t domain_id);
1997 int xc_mem_paging_resume(xc_interface *xch, uint32_t domain_id);
1998 int xc_mem_paging_nominate(xc_interface *xch, uint32_t domain_id,
1999                            uint64_t gfn);
2000 int xc_mem_paging_evict(xc_interface *xch, uint32_t domain_id, uint64_t gfn);
2001 int xc_mem_paging_prep(xc_interface *xch, uint32_t domain_id, uint64_t gfn);
2002 int xc_mem_paging_load(xc_interface *xch, uint32_t domain_id,
2003                        uint64_t gfn, void *buffer);
2004 
2005 /**
2006  * Access tracking operations.
2007  * Supported only on Intel EPT 64 bit processors.
2008  */
2009 
2010 /*
2011  * Set a range of memory to a specific access.
2012  * Allowed types are XENMEM_access_default, XENMEM_access_n, any combination of
2013  * XENMEM_access_ + (rwx), and XENMEM_access_rx2rw
2014  */
2015 int xc_set_mem_access(xc_interface *xch, uint32_t domain_id,
2016                       xenmem_access_t access, uint64_t first_pfn,
2017                       uint32_t nr);
2018 
2019 /*
2020  * Set an array of pages to their respective access in the access array.
2021  * The nr parameter specifies the size of the pages and access arrays.
2022  * The same allowed access types as for xc_set_mem_access() apply.
2023  */
2024 int xc_set_mem_access_multi(xc_interface *xch, uint32_t domain_id,
2025                             uint8_t *access, uint64_t *pages,
2026                             uint32_t nr);
2027 
2028 /*
2029  * Gets the mem access for the given page (returned in access on success)
2030  */
2031 int xc_get_mem_access(xc_interface *xch, uint32_t domain_id,
2032                       uint64_t pfn, xenmem_access_t *access);
2033 
2034 /*
2035  * Returns the VM_EVENT_INTERFACE version.
2036  */
2037 int xc_vm_event_get_version(xc_interface *xch);
2038 
2039 /***
2040  * Monitor control operations.
2041  *
2042  * Enables the VM event monitor ring and returns the mapped ring page.
2043  * This ring is used to deliver mem_access events, as well a set of additional
2044  * events that can be enabled with the xc_monitor_* functions.
2045  *
2046  * Will return NULL on error.
2047  * Caller has to unmap this page when done.
2048  */
2049 void *xc_monitor_enable(xc_interface *xch, uint32_t domain_id, uint32_t *port);
2050 int xc_monitor_disable(xc_interface *xch, uint32_t domain_id);
2051 int xc_monitor_resume(xc_interface *xch, uint32_t domain_id);
2052 /*
2053  * Get a bitmap of supported monitor events in the form
2054  * (1 << XEN_DOMCTL_MONITOR_EVENT_*).
2055  */
2056 int xc_monitor_get_capabilities(xc_interface *xch, uint32_t domain_id,
2057                                 uint32_t *capabilities);
2058 int xc_monitor_write_ctrlreg(xc_interface *xch, uint32_t domain_id,
2059                              uint16_t index, bool enable, bool sync,
2060                              uint64_t bitmask, bool onchangeonly);
2061 /*
2062  * A list of MSR indices can usually be found in /usr/include/asm/msr-index.h.
2063  * Please consult the Intel/AMD manuals for more information on
2064  * non-architectural indices.
2065  */
2066 int xc_monitor_mov_to_msr(xc_interface *xch, uint32_t domain_id, uint32_t msr,
2067                           bool enable, bool onchangeonly);
2068 int xc_monitor_singlestep(xc_interface *xch, uint32_t domain_id, bool enable);
2069 int xc_monitor_software_breakpoint(xc_interface *xch, uint32_t domain_id,
2070                                    bool enable);
2071 int xc_monitor_descriptor_access(xc_interface *xch, uint32_t domain_id,
2072                                  bool enable);
2073 int xc_monitor_guest_request(xc_interface *xch, uint32_t domain_id,
2074                              bool enable, bool sync, bool allow_userspace);
2075 /*
2076  * Disables page-walk mem_access events by emulating. If the
2077  * emulation can not be performed then a VM_EVENT_REASON_EMUL_UNIMPLEMENTED
2078  * event will be issued.
2079  */
2080 int xc_monitor_inguest_pagefault(xc_interface *xch, uint32_t domain_id,
2081                                  bool disable);
2082 int xc_monitor_debug_exceptions(xc_interface *xch, uint32_t domain_id,
2083                                 bool enable, bool sync);
2084 int xc_monitor_cpuid(xc_interface *xch, uint32_t domain_id, bool enable);
2085 int xc_monitor_privileged_call(xc_interface *xch, uint32_t domain_id,
2086                                bool enable);
2087 int xc_monitor_emul_unimplemented(xc_interface *xch, uint32_t domain_id,
2088                                   bool enable);
2089 /**
2090  * This function enables / disables emulation for each REP for a
2091  * REP-compatible instruction.
2092  *
2093  * @parm xch a handle to an open hypervisor interface.
2094  * @parm domain_id the domain id one wants to get the node affinity of.
2095  * @parm enable if 0 optimize when possible, else emulate each REP.
2096  * @return 0 on success, -1 on failure.
2097  */
2098 int xc_monitor_emulate_each_rep(xc_interface *xch, uint32_t domain_id,
2099                                 bool enable);
2100 
2101 /***
2102  * Memory sharing operations.
2103  *
2104  * Unles otherwise noted, these calls return 0 on succes, -1 and errno on
2105  * failure.
2106  *
2107  * Sharing is supported only on the x86 architecture in 64 bit mode, with
2108  * Hardware-Assisted Paging (i.e. Intel EPT, AMD NPT). Moreover, AMD NPT
2109  * support is considered experimental.
2110 
2111  * Calls below return ENOSYS if not in the x86_64 architecture.
2112  * Calls below return ENODEV if the domain does not support HAP.
2113  * Calls below return ESRCH if the specified domain does not exist.
2114  * Calls below return EPERM if the caller is unprivileged for this domain.
2115  */
2116 
2117 /* Turn on/off sharing for the domid, depending on the enable flag.
2118  *
2119  * Returns EXDEV if trying to enable and the domain has had a PCI device
2120  * assigned for passthrough (these two features are mutually exclusive).
2121  *
2122  * When sharing for a domain is turned off, the domain may still reference
2123  * shared pages. Unsharing happens lazily. */
2124 int xc_memshr_control(xc_interface *xch,
2125                       uint32_t domid,
2126                       int enable);
2127 
2128 /* Create a communication ring in which the hypervisor will place ENOMEM
2129  * notifications.
2130  *
2131  * ENOMEM happens when unsharing pages: a Copy-on-Write duplicate needs to be
2132  * allocated, and thus the out-of-memory error occurr.
2133  *
2134  * For complete examples on how to plumb a notification ring, look into
2135  * xenpaging or xen-access.
2136  *
2137  * On receipt of a notification, the helper should ensure there is memory
2138  * available to the domain before retrying.
2139  *
2140  * If a domain encounters an ENOMEM condition when sharing and this ring
2141  * has not been set up, the hypervisor will crash the domain.
2142  *
2143  * Fails with:
2144  *  EINVAL if port is NULL
2145  *  EINVAL if the sharing ring has already been enabled
2146  *  ENOSYS if no guest gfn has been specified to host the ring via an hvm param
2147  *  EINVAL if the gfn for the ring has not been populated
2148  *  ENOENT if the gfn for the ring is paged out, or cannot be unshared
2149  *  EINVAL if the gfn for the ring cannot be written to
2150  *  EINVAL if the domain is dying
2151  *  ENOSPC if an event channel cannot be allocated for the ring
2152  *  ENOMEM if memory cannot be allocated for internal data structures
2153  *  EINVAL or EACCESS if the request is denied by the security policy
2154  */
2155 
2156 int xc_memshr_ring_enable(xc_interface *xch,
2157                           uint32_t domid,
2158                           uint32_t *port);
2159 /* Disable the ring for ENOMEM communication.
2160  * May fail with EINVAL if the ring was not enabled in the first place.
2161  */
2162 int xc_memshr_ring_disable(xc_interface *xch,
2163                            uint32_t domid);
2164 
2165 /*
2166  * Calls below return EINVAL if sharing has not been enabled for the domain
2167  * Calls below return EINVAL if the domain is dying
2168  */
2169 /* Once a reponse to an ENOMEM notification is prepared, the tool can
2170  * notify the hypervisor to re-schedule the faulting vcpu of the domain with an
2171  * event channel kick and/or this call. */
2172 int xc_memshr_domain_resume(xc_interface *xch,
2173                             uint32_t domid);
2174 
2175 /* Select a page for sharing.
2176  *
2177  * A 64 bit opaque handle will be stored in handle.  The hypervisor ensures
2178  * that if the page is modified, the handle will be invalidated, and future
2179  * users of it will fail. If the page has already been selected and is still
2180  * associated to a valid handle, the existing handle will be returned.
2181  *
2182  * May fail with:
2183  *  EINVAL if the gfn is not populated or not sharable (mmio, etc)
2184  *  ENOMEM if internal data structures cannot be allocated
2185  *  E2BIG if the page is being referenced by other subsytems (e.g. qemu)
2186  *  ENOENT or EEXIST if there are internal hypervisor errors.
2187  */
2188 int xc_memshr_nominate_gfn(xc_interface *xch,
2189                            uint32_t domid,
2190                            unsigned long gfn,
2191                            uint64_t *handle);
2192 /* Same as above, but instead of a guest frame number, the input is a grant
2193  * reference provided by the guest.
2194  *
2195  * May fail with EINVAL if the grant reference is invalid.
2196  */
2197 int xc_memshr_nominate_gref(xc_interface *xch,
2198                             uint32_t domid,
2199                             grant_ref_t gref,
2200                             uint64_t *handle);
2201 
2202 /* The three calls below may fail with
2203  * 10 (or -XENMEM_SHARING_OP_S_HANDLE_INVALID) if the handle passed as source
2204  * is invalid.
2205  * 9 (or -XENMEM_SHARING_OP_C_HANDLE_INVALID) if the handle passed as client is
2206  * invalid.
2207  */
2208 /* Share two nominated guest pages.
2209  *
2210  * If the call succeeds, both pages will point to the same backing frame (or
2211  * mfn). The hypervisor will verify the handles are still valid, but it will
2212  * not perform any sanity checking on the contens of the pages (the selection
2213  * mechanism for sharing candidates is entirely up to the user-space tool).
2214  *
2215  * After successful sharing, the client handle becomes invalid. Both <domain,
2216  * gfn> tuples point to the same mfn with the same handle, the one specified as
2217  * source. Either 3-tuple can be specified later for further re-sharing.
2218  */
2219 int xc_memshr_share_gfns(xc_interface *xch,
2220                     uint32_t source_domain,
2221                     unsigned long source_gfn,
2222                     uint64_t source_handle,
2223                     uint32_t client_domain,
2224                     unsigned long client_gfn,
2225                     uint64_t client_handle);
2226 
2227 /* Same as above, but share two grant references instead.
2228  *
2229  * May fail with EINVAL if either grant reference is invalid.
2230  */
2231 int xc_memshr_share_grefs(xc_interface *xch,
2232                     uint32_t source_domain,
2233                     grant_ref_t source_gref,
2234                     uint64_t source_handle,
2235                     uint32_t client_domain,
2236                     grant_ref_t client_gref,
2237                     uint64_t client_handle);
2238 
2239 /* Allows to add to the guest physmap of the client domain a shared frame
2240  * directly.
2241  *
2242  * May additionally fail with
2243  *  9 (-XENMEM_SHARING_OP_C_HANDLE_INVALID) if the physmap entry for the gfn is
2244  *  not suitable.
2245  *  ENOMEM if internal data structures cannot be allocated.
2246  *  ENOENT if there is an internal hypervisor error.
2247  */
2248 int xc_memshr_add_to_physmap(xc_interface *xch,
2249                     uint32_t source_domain,
2250                     unsigned long source_gfn,
2251                     uint64_t source_handle,
2252                     uint32_t client_domain,
2253                     unsigned long client_gfn);
2254 
2255 /* Allows to deduplicate a range of memory of a client domain. Using
2256  * this function is equivalent of calling xc_memshr_nominate_gfn for each gfn
2257  * in the two domains followed by xc_memshr_share_gfns.
2258  *
2259  * May fail with -EINVAL if the source and client domain have different
2260  * memory size or if memory sharing is not enabled on either of the domains.
2261  * May also fail with -ENOMEM if there isn't enough memory available to store
2262  * the sharing metadata before deduplication can happen.
2263  */
2264 int xc_memshr_range_share(xc_interface *xch,
2265                           uint32_t source_domain,
2266                           uint32_t client_domain,
2267                           uint64_t first_gfn,
2268                           uint64_t last_gfn);
2269 
2270 int xc_memshr_fork(xc_interface *xch,
2271                    uint32_t source_domain,
2272                    uint32_t client_domain,
2273                    bool allow_with_iommu,
2274                    bool block_interrupts);
2275 
2276 /*
2277  * Note: this function is only intended to be used on short-lived forks that
2278  * haven't yet aquired a lot of memory. In case the fork has a lot of memory
2279  * it is likely more performant to create a new fork with xc_memshr_fork.
2280  *
2281  * With VMs that have a lot of memory this call may block for a long time.
2282  */
2283 int xc_memshr_fork_reset(xc_interface *xch, uint32_t forked_domain);
2284 
2285 /* Debug calls: return the number of pages referencing the shared frame backing
2286  * the input argument. Should be one or greater.
2287  *
2288  * May fail with EINVAL if there is no backing shared frame for the input
2289  * argument.
2290  */
2291 int xc_memshr_debug_gfn(xc_interface *xch,
2292                         uint32_t domid,
2293                         unsigned long gfn);
2294 /* May additionally fail with EINVAL if the grant reference is invalid. */
2295 int xc_memshr_debug_gref(xc_interface *xch,
2296                          uint32_t domid,
2297                          grant_ref_t gref);
2298 
2299 /* Audits the share subsystem.
2300  *
2301  * Returns ENOSYS if not supported (may not be compiled into the hypervisor).
2302  *
2303  * Returns the number of errors found during auditing otherwise. May be (should
2304  * be!) zero.
2305  *
2306  * If debugtrace support has been compiled into the hypervisor and is enabled,
2307  * verbose descriptions for the errors are available in the hypervisor console.
2308  */
2309 int xc_memshr_audit(xc_interface *xch);
2310 
2311 /* Stats reporting.
2312  *
2313  * At any point in time, the following equality should hold for a host:
2314  *
2315  *  Let dominfo(d) be the xc_dominfo_t struct filled by a call to
2316  *  xc_domain_getinfo(d)
2317  *
2318  *  The summation of dominfo(d)->shr_pages for all domains in the system
2319  *      should be equal to
2320  *  xc_sharing_freed_pages + xc_sharing_used_frames
2321  */
2322 /*
2323  * This function returns the total number of pages freed by using sharing
2324  * on the system.  For example, if two domains contain a single entry in
2325  * their p2m table that points to the same shared page (and no other pages
2326  * in the system are shared), then this function should return 1.
2327  */
2328 long xc_sharing_freed_pages(xc_interface *xch);
2329 
2330 /*
2331  * This function returns the total number of frames occupied by shared
2332  * pages on the system.  This is independent of the number of domains
2333  * pointing at these frames.  For example, in the above scenario this
2334  * should return 1. (And dominfo(d) for each of the two domains should return 1
2335  * as well).
2336  *
2337  * Note that some of these sharing_used_frames may be referenced by
2338  * a single domain page, and thus not realize any savings. The same
2339  * applies to some of the pages counted in dominfo(d)->shr_pages.
2340  */
2341 long xc_sharing_used_frames(xc_interface *xch);
2342 /*** End sharing interface ***/
2343 
2344 int xc_flask_load(xc_interface *xc_handle, char *buf, uint32_t size);
2345 int xc_flask_context_to_sid(xc_interface *xc_handle, char *buf, uint32_t size, uint32_t *sid);
2346 int xc_flask_sid_to_context(xc_interface *xc_handle, int sid, char *buf, uint32_t size);
2347 int xc_flask_getenforce(xc_interface *xc_handle);
2348 int xc_flask_setenforce(xc_interface *xc_handle, int mode);
2349 int xc_flask_getbool_byid(xc_interface *xc_handle, int id, char *name, uint32_t size, int *curr, int *pend);
2350 int xc_flask_getbool_byname(xc_interface *xc_handle, char *name, int *curr, int *pend);
2351 int xc_flask_setbool(xc_interface *xc_handle, char *name, int value, int commit);
2352 int xc_flask_add_pirq(xc_interface *xc_handle, unsigned int pirq, char *scontext);
2353 int xc_flask_add_ioport(xc_interface *xc_handle, unsigned long low, unsigned long high,
2354                       char *scontext);
2355 int xc_flask_add_iomem(xc_interface *xc_handle, unsigned long low, unsigned long high,
2356                      char *scontext);
2357 int xc_flask_add_device(xc_interface *xc_handle, unsigned long device, char *scontext);
2358 int xc_flask_del_pirq(xc_interface *xc_handle, unsigned int pirq);
2359 int xc_flask_del_ioport(xc_interface *xc_handle, unsigned long low, unsigned long high);
2360 int xc_flask_del_iomem(xc_interface *xc_handle, unsigned long low, unsigned long high);
2361 int xc_flask_del_device(xc_interface *xc_handle, unsigned long device);
2362 int xc_flask_access(xc_interface *xc_handle, const char *scon, const char *tcon,
2363                   uint16_t tclass, uint32_t req,
2364                   uint32_t *allowed, uint32_t *decided,
2365                   uint32_t *auditallow, uint32_t *auditdeny,
2366                   uint32_t *seqno);
2367 int xc_flask_avc_cachestats(xc_interface *xc_handle, char *buf, int size);
2368 int xc_flask_policyvers(xc_interface *xc_handle);
2369 int xc_flask_avc_hashstats(xc_interface *xc_handle, char *buf, int size);
2370 int xc_flask_getavc_threshold(xc_interface *xc_handle);
2371 int xc_flask_setavc_threshold(xc_interface *xc_handle, int threshold);
2372 int xc_flask_relabel_domain(xc_interface *xch, uint32_t domid, uint32_t sid);
2373 
2374 struct elf_binary;
2375 void xc_elf_set_logfile(xc_interface *xch, struct elf_binary *elf,
2376                         int verbose);
2377 /* Useful for callers who also use libelf. */
2378 
2379 /*
2380  * Execute an image previously loaded with xc_kexec_load().
2381  *
2382  * Does not return on success.
2383  *
2384  * Fails with:
2385  *   ENOENT if the specified image has not been loaded.
2386  */
2387 int xc_kexec_exec(xc_interface *xch, int type);
2388 
2389 /*
2390  * Find the machine address and size of certain memory areas.
2391  *
2392  *   KEXEC_RANGE_MA_CRASH       crash area
2393  *   KEXEC_RANGE_MA_XEN         Xen itself
2394  *   KEXEC_RANGE_MA_CPU         CPU note for CPU number 'nr'
2395  *   KEXEC_RANGE_MA_XENHEAP     xenheap
2396  *   KEXEC_RANGE_MA_EFI_MEMMAP  EFI Memory Map
2397  *   KEXEC_RANGE_MA_VMCOREINFO  vmcoreinfo
2398  *
2399  * Fails with:
2400  *   EINVAL if the range or CPU number isn't valid.
2401  */
2402 int xc_kexec_get_range(xc_interface *xch, int range,  int nr,
2403                        uint64_t *size, uint64_t *start);
2404 
2405 /*
2406  * Load a kexec image into memory.
2407  *
2408  * The image may be of type KEXEC_TYPE_DEFAULT (executed on request)
2409  * or KEXEC_TYPE_CRASH (executed on a crash).
2410  *
2411  * The image architecture may be a 32-bit variant of the hypervisor
2412  * architecture (e.g, EM_386 on a x86-64 hypervisor).
2413  *
2414  * Fails with:
2415  *   ENOMEM if there is insufficient memory for the new image.
2416  *   EINVAL if the image does not fit into the crash area or the entry
2417  *          point isn't within one of segments.
2418  *   EBUSY  if another image is being executed.
2419  */
2420 int xc_kexec_load(xc_interface *xch, uint8_t type, uint16_t arch,
2421                   uint64_t entry_maddr,
2422                   uint32_t nr_segments, xen_kexec_segment_t *segments);
2423 
2424 /*
2425  * Unload a kexec image.
2426  *
2427  * This prevents a KEXEC_TYPE_DEFAULT or KEXEC_TYPE_CRASH image from
2428  * being executed.  The crash images are not cleared from the crash
2429  * region.
2430  */
2431 int xc_kexec_unload(xc_interface *xch, int type);
2432 
2433 /*
2434  * Find out whether the image has been succesfully loaded.
2435  *
2436  * The type can be either KEXEC_TYPE_DEFAULT or KEXEC_TYPE_CRASH.
2437  * If zero is returned, that means no image is loaded for the type.
2438  * If one is returned, that means an image is loaded for the type.
2439  * Otherwise, negative return value indicates error.
2440  */
2441 int xc_kexec_status(xc_interface *xch, int type);
2442 
2443 typedef xenpf_resource_entry_t xc_resource_entry_t;
2444 
2445 /*
2446  * Generic resource operation which contains multiple non-preemptible
2447  * resource access entries that passed to xc_resource_op().
2448  */
2449 struct xc_resource_op {
2450     uint64_t result;        /* on return, check this field first */
2451     uint32_t cpu;           /* which cpu to run */
2452     uint32_t nr_entries;    /* number of resource entries */
2453     xc_resource_entry_t *entries;
2454 };
2455 
2456 typedef struct xc_resource_op xc_resource_op_t;
2457 int xc_resource_op(xc_interface *xch, uint32_t nr_ops, xc_resource_op_t *ops);
2458 
2459 #if defined(__i386__) || defined(__x86_64__)
2460 enum xc_psr_cmt_type {
2461     XC_PSR_CMT_L3_OCCUPANCY,
2462     XC_PSR_CMT_TOTAL_MEM_COUNT,
2463     XC_PSR_CMT_LOCAL_MEM_COUNT,
2464 };
2465 typedef enum xc_psr_cmt_type xc_psr_cmt_type;
2466 
2467 enum xc_psr_type {
2468     XC_PSR_CAT_L3_CBM      = 1,
2469     XC_PSR_CAT_L3_CBM_CODE = 2,
2470     XC_PSR_CAT_L3_CBM_DATA = 3,
2471     XC_PSR_CAT_L2_CBM      = 4,
2472     XC_PSR_MBA_THRTL       = 5,
2473 };
2474 typedef enum xc_psr_type xc_psr_type;
2475 
2476 enum xc_psr_feat_type {
2477     XC_PSR_CAT_L3,
2478     XC_PSR_CAT_L2,
2479     XC_PSR_MBA,
2480 };
2481 typedef enum xc_psr_feat_type xc_psr_feat_type;
2482 
2483 union xc_psr_hw_info {
2484     struct {
2485         uint32_t cos_max;
2486         uint32_t cbm_len;
2487         bool     cdp_enabled;
2488     } cat;
2489 
2490     struct {
2491         uint32_t cos_max;
2492         uint32_t thrtl_max;
2493         bool     linear;
2494     } mba;
2495 };
2496 typedef union xc_psr_hw_info xc_psr_hw_info;
2497 
2498 int xc_psr_cmt_attach(xc_interface *xch, uint32_t domid);
2499 int xc_psr_cmt_detach(xc_interface *xch, uint32_t domid);
2500 int xc_psr_cmt_get_domain_rmid(xc_interface *xch, uint32_t domid,
2501                                uint32_t *rmid);
2502 int xc_psr_cmt_get_total_rmid(xc_interface *xch, uint32_t *total_rmid);
2503 int xc_psr_cmt_get_l3_upscaling_factor(xc_interface *xch,
2504                                        uint32_t *upscaling_factor);
2505 int xc_psr_cmt_get_l3_event_mask(xc_interface *xch, uint32_t *event_mask);
2506 int xc_psr_cmt_get_l3_cache_size(xc_interface *xch, uint32_t cpu,
2507                                  uint32_t *l3_cache_size);
2508 int xc_psr_cmt_get_data(xc_interface *xch, uint32_t rmid, uint32_t cpu,
2509                         uint32_t psr_cmt_type, uint64_t *monitor_data,
2510                         uint64_t *tsc);
2511 int xc_psr_cmt_enabled(xc_interface *xch);
2512 
2513 int xc_psr_set_domain_data(xc_interface *xch, uint32_t domid,
2514                            xc_psr_type type, uint32_t target,
2515                            uint64_t data);
2516 int xc_psr_get_domain_data(xc_interface *xch, uint32_t domid,
2517                            xc_psr_type type, uint32_t target,
2518                            uint64_t *data);
2519 int xc_psr_get_hw_info(xc_interface *xch, uint32_t socket,
2520                        xc_psr_feat_type type, xc_psr_hw_info *hw_info);
2521 
2522 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
2523 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
2524                           uint32_t *nr_features, uint32_t *featureset);
2525 
2526 int xc_get_cpu_policy_size(xc_interface *xch, uint32_t *nr_leaves,
2527                            uint32_t *nr_msrs);
2528 int xc_get_system_cpu_policy(xc_interface *xch, uint32_t index,
2529                              uint32_t *nr_leaves, xen_cpuid_leaf_t *leaves,
2530                              uint32_t *nr_msrs, xen_msr_entry_t *msrs);
2531 int xc_get_domain_cpu_policy(xc_interface *xch, uint32_t domid,
2532                              uint32_t *nr_leaves, xen_cpuid_leaf_t *leaves,
2533                              uint32_t *nr_msrs, xen_msr_entry_t *msrs);
2534 int xc_set_domain_cpu_policy(xc_interface *xch, uint32_t domid,
2535                              uint32_t nr_leaves, xen_cpuid_leaf_t *leaves,
2536                              uint32_t nr_msrs, xen_msr_entry_t *msrs,
2537                              uint32_t *err_leaf_p, uint32_t *err_subleaf_p,
2538                              uint32_t *err_msr_p);
2539 
2540 uint32_t xc_get_cpu_featureset_size(void);
2541 
2542 enum xc_static_cpu_featuremask {
2543     XC_FEATUREMASK_KNOWN,
2544     XC_FEATUREMASK_SPECIAL,
2545     XC_FEATUREMASK_PV_MAX,
2546     XC_FEATUREMASK_PV_DEF,
2547     XC_FEATUREMASK_HVM_SHADOW_MAX,
2548     XC_FEATUREMASK_HVM_SHADOW_DEF,
2549     XC_FEATUREMASK_HVM_HAP_MAX,
2550     XC_FEATUREMASK_HVM_HAP_DEF,
2551 };
2552 const uint32_t *xc_get_static_cpu_featuremask(enum xc_static_cpu_featuremask);
2553 
2554 #endif
2555 
2556 int xc_livepatch_upload(xc_interface *xch,
2557                         char *name, unsigned char *payload, uint32_t size);
2558 
2559 int xc_livepatch_get(xc_interface *xch,
2560                      char *name,
2561                      xen_livepatch_status_t *status);
2562 
2563 /*
2564  * Get a number of available payloads and get actual total size of
2565  * the payloads' name and metadata arrays.
2566  *
2567  * This functions is typically executed first before the xc_livepatch_list()
2568  * to obtain the sizes and correctly allocate all necessary data resources.
2569  *
2570  * The return value is zero if the hypercall completed successfully.
2571  *
2572  * If there was an error performing the sysctl operation, the return value
2573  * will contain the hypercall error code value.
2574  */
2575 int xc_livepatch_list_get_sizes(xc_interface *xch, unsigned int *nr,
2576                                 uint32_t *name_total_size,
2577                                 uint32_t *metadata_total_size);
2578 
2579 /*
2580  * The heart of this function is to get an array of the following objects:
2581  *   - xen_livepatch_status_t: states and return codes of payloads
2582  *   - name: names of payloads
2583  *   - len: lengths of corresponding payloads' names
2584  *   - metadata: payloads' metadata
2585  *   - metadata_len: lengths of corresponding payloads' metadata
2586  *
2587  * However it is complex because it has to deal with the hypervisor
2588  * returning some of the requested data or data being stale
2589  * (another hypercall might alter the list).
2590  *
2591  * The parameters that the function expects to contain data from
2592  * the hypervisor are: 'info', 'name', and 'len'. The 'done' and
2593  * 'left' are also updated with the number of entries filled out
2594  * and respectively the number of entries left to get from hypervisor.
2595  *
2596  * It is expected that the caller of this function will first issue the
2597  * xc_livepatch_list_get_sizes() in order to obtain total sizes of names
2598  * and all metadata as well as the current number of payload entries.
2599  * The total sizes are required and supplied via the 'name_total_size' and
2600  * 'metadata_total_size' parameters.
2601  *
2602  * The 'max' is to be provided by the caller with the maximum number of
2603  * entries that 'info', 'name', 'len', 'metadata' and 'metadata_len' arrays
2604  * can be filled up with.
2605  *
2606  * Each entry in the 'info' array is expected to be of xen_livepatch_status_t
2607  * structure size.
2608  *
2609  * Each entry in the 'name' array may have an arbitrary size.
2610  *
2611  * Each entry in the 'len' array is expected to be of uint32_t size.
2612  *
2613  * Each entry in the 'metadata' array may have an arbitrary size.
2614  *
2615  * Each entry in the 'metadata_len' array is expected to be of uint32_t size.
2616  *
2617  * The return value is zero if the hypercall completed successfully.
2618  * Note that the return value is _not_ the amount of entries filled
2619  * out - that is saved in 'done'.
2620  *
2621  * If there was an error performing the operation, the return value
2622  * will contain an negative -EXX type value. The 'done' and 'left'
2623  * will contain the number of entries that had been succesfully
2624  * retrieved (if any).
2625  */
2626 int xc_livepatch_list(xc_interface *xch, const unsigned int max,
2627                       const unsigned int start,
2628                       struct xen_livepatch_status *info,
2629                       char *name, uint32_t *len,
2630                       const uint32_t name_total_size,
2631                       char *metadata, uint32_t *metadata_len,
2632                       const uint32_t metadata_total_size,
2633                       unsigned int *done, unsigned int *left);
2634 
2635 /*
2636  * The operations are asynchronous and the hypervisor may take a while
2637  * to complete them. The `timeout` offers an option to expire the
2638  * operation if it could not be completed within the specified time
2639  * (in ns). Value of 0 means let hypervisor decide the best timeout.
2640  * The `flags` allows to pass extra parameters to the actions.
2641  */
2642 int xc_livepatch_apply(xc_interface *xch, char *name, uint32_t timeout, uint32_t flags);
2643 int xc_livepatch_revert(xc_interface *xch, char *name, uint32_t timeout, uint32_t flags);
2644 int xc_livepatch_unload(xc_interface *xch, char *name, uint32_t timeout, uint32_t flags);
2645 int xc_livepatch_replace(xc_interface *xch, char *name, uint32_t timeout, uint32_t flags);
2646 
2647 /*
2648  * Ensure cache coherency after memory modifications. A call to this function
2649  * is only required on ARM as the x86 architecture provides cache coherency
2650  * guarantees. Calling this function on x86 is allowed but has no effect.
2651  */
2652 int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,
2653                          xen_pfn_t start_pfn, xen_pfn_t nr_pfns);
2654 
2655 /* Compat shims */
2656 #include "xenctrl_compat.h"
2657 
2658 #endif /* XENCTRL_H */
2659 
2660 /*
2661  * Local variables:
2662  * mode: C
2663  * c-file-style: "BSD"
2664  * c-basic-offset: 4
2665  * tab-width: 4
2666  * indent-tabs-mode: nil
2667  * End:
2668  */
2669