1 /*
2  * Copyright (C) 2009      Citrix Ltd.
3  * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
4  * Author Stefano Stabellini <stefano.stabellini@eu.citrix.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; version 2.1 only. with the special
9  * exception on linking described in file LICENSE.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  */
16 
17 #ifndef LIBXL_INTERNAL_H
18 #define LIBXL_INTERNAL_H
19 
20 #include "libxl_osdeps.h" /* must come before any other headers */
21 
22 #include "xentoolcore_internal.h"
23 
24 #include "libxl_sr_stream_format.h"
25 
26 #include <assert.h>
27 #include <dirent.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <inttypes.h>
31 #include <pthread.h>
32 #include <signal.h>
33 #include <stdarg.h>
34 #include <stddef.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <ctype.h>
41 
42 #include <sys/mman.h>
43 #include <poll.h>
44 #include <sys/select.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <sys/types.h>
48 #include <sys/wait.h>
49 #include <sys/socket.h>
50 #include <sys/file.h>
51 #include <sys/ioctl.h>
52 
53 #include <xenevtchn.h>
54 #include <xenstore.h>
55 #define XC_WANT_COMPAT_MAP_FOREIGN_API
56 #include <xenctrl.h>
57 #include <xenguest.h>
58 #include <xc_dom.h>
59 
60 #include "xentoollog.h"
61 
62 #include <xen/io/xenbus.h>
63 
64 #ifdef LIBXL_H
65 # error libxl.h should be included via libxl_internal.h, not separately
66 #endif
67 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
68 # define LIBXL_EXTERNAL_CALLERS_ONLY \
69     __attribute__((warning("may not be called from within libxl")))
70 #endif
71 
72 #include "libxl.h"
73 #include "_paths.h"
74 #include "_libxl_save_msgs_callout.h"
75 
76 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
77 #define _hidden __attribute__((visibility("hidden")))
78 #define _protected __attribute__((visibility("protected")))
79 #else
80 #define _hidden
81 #define _protected
82 #endif
83 
84 #include "flexarray.h"
85 #include "libxl_utils.h"
86 
87 #include "libxl_json.h"
88 
89 #include "_libxl_types_internal.h"
90 #include "_libxl_types_internal_json.h"
91 
92 #define LIBXL_INIT_TIMEOUT 10
93 #define LIBXL_DESTROY_TIMEOUT 10
94 #define LIBXL_HOTPLUG_TIMEOUT 40
95 /* QEMU may be slow to load and start due to a bug in Linux where the I/O
96  * subsystem sometime produce high latency under load. */
97 #define LIBXL_DEVICE_MODEL_START_TIMEOUT 60
98 #define LIBXL_DEVICE_MODEL_SAVE_FILE XEN_LIB_DIR "/qemu-save" /* .$domid */
99 #define LIBXL_DEVICE_MODEL_RESTORE_FILE XEN_LIB_DIR "/qemu-resume" /* .$domid */
100 #define LIBXL_STUBDOM_START_TIMEOUT 30
101 #define LIBXL_QEMU_BODGE_TIMEOUT 2
102 #define LIBXL_XENCONSOLE_LIMIT 1048576
103 #define LIBXL_XENCONSOLE_PROTOCOL "vt100"
104 #define LIBXL_MAXMEM_CONSTANT 1024
105 #define LIBXL_PV_EXTRA_MEMORY 1024
106 #define LIBXL_HVM_EXTRA_MEMORY 2048
107 #define LIBXL_MIN_DOM0_MEM (128*1024)
108 #define LIBXL_INVALID_GFN (~(uint64_t)0)
109 #define LIBXL_VGA_HOLE_SIZE 0x20
110 /* use 0 as the domid of the toolstack domain for now */
111 #define LIBXL_TOOLSTACK_DOMID 0
112 #define QEMU_SIGNATURE "DeviceModelRecord0002"
113 #define STUBDOM_CONSOLE_LOGGING 0
114 #define STUBDOM_CONSOLE_SAVE 1
115 #define STUBDOM_CONSOLE_RESTORE 2
116 #define STUBDOM_CONSOLE_SERIAL 3
117 #define STUBDOM_SPECIAL_CONSOLES 3
118 #define TAP_DEVICE_SUFFIX "-emu"
119 #define DOMID_XS_PATH "domid"
120 #define INVALID_DOMID ~0
121 #define PVSHIM_BASENAME "xen-shim"
122 #define PVSHIM_CMDLINE "pv-shim console=xen,pv sched=null"
123 
124 /* Size macros. */
125 #define __AC(X,Y)   (X##Y)
126 #define _AC(X,Y)    __AC(X,Y)
127 #define MB(_mb)     (_AC(_mb, ULL) << 20)
128 #define GB(_gb)     (_AC(_gb, ULL) << 30)
129 
130 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
131 
132 #define ROUNDUP(_val, _order)                                           \
133     (((unsigned long)(_val)+(1UL<<(_order))-1) & ~((1UL<<(_order))-1))
134 
135 #define DIV_ROUNDUP(n, d) (((n) + (d) - 1) / (d))
136 
137 #define MASK_EXTR(v, m) (((v) & (m)) / ((m) & -(m)))
138 #define MASK_INSR(v, m) (((v) * ((m) & -(m))) & (m))
139 
140 #define min(X, Y) ({                             \
141             const typeof (X) _x = (X);           \
142             const typeof (Y) _y = (Y);           \
143             (void) (&_x == &_y);                 \
144             (_x < _y) ? _x : _y; })
145 #define max(X, Y) ({                             \
146             const typeof (X) _x = (X);           \
147             const typeof (Y) _y = (Y);           \
148             (void) (&_x == &_y);                 \
149             (_x > _y) ? _x : _y; })
150 
151 #define min_t(type, x, y)                                               \
152     ({ const type _x = (x); const type _y = (y); _x < _y ? _x: _y; })
153 #define max_t(type, x, y)                                               \
154     ({ const type _x = (x); const type _y = (y); _x > _y ? _x: _y; })
155 
156 #define LIBXL__LOGGING_ENABLED
157 
158 #ifdef LIBXL__LOGGING_ENABLED
159 #define LIBXL__LOG(ctx, loglevel, _f, _a...)   libxl__log(ctx, loglevel, -1, __FILE__, __LINE__, __func__, INVALID_DOMID, _f, ##_a)
160 #define LIBXL__LOG_ERRNO(ctx, loglevel, _f, _a...)   libxl__log(ctx, loglevel, errno, __FILE__, __LINE__, __func__, INVALID_DOMID, _f, ##_a)
161 #define LIBXL__LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...)   libxl__log(ctx, loglevel, errnoval, __FILE__, __LINE__, __func__, INVALID_DOMID, _f, ##_a)
162 
163 /* Same log functions as above, but with _d being a domain id. */
164 #define LIBXL__LOGD(ctx, loglevel, _d, _f, _a...)   libxl__log(ctx, loglevel, -1, __FILE__, __LINE__, __func__, _d, _f, ##_a)
165 #define LIBXL__LOGD_ERRNO(ctx, loglevel, _d, _f, _a...)   libxl__log(ctx, loglevel, errno, __FILE__, __LINE__, __func__, _d, _f, ##_a)
166 #define LIBXL__LOGD_ERRNOVAL(ctx, loglevel, errnoval, _d, _f, _a...)   libxl__log(ctx, loglevel, errnoval, __FILE__, __LINE__, __func__, _d, _f, ##_a)
167 #else
168 #define LIBXL__LOG(ctx, loglevel, _f, _a...)
169 #define LIBXL__LOG_ERRNO(ctx, loglevel, _f, _a...)
170 #define LIBXL__LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...)
171 
172 #define LIBXLD__LOG(ctx, loglevel, _d, _f, _a...)
173 #define LIBXLD__LOG_ERRNO(ctx, loglevel, _d, _f, _a...)
174 #define LIBXLD__LOG_ERRNOVAL(ctx, loglevel, errnoval, _d, _f, _a...)
175 #endif
176   /* all of these macros preserve errno (saving and restoring) */
177 
178 /* Convert pfn to physical address space. */
179 #define pfn_to_paddr(x) ((uint64_t)(x) << XC_PAGE_SHIFT)
180 
181 /* logging */
182 _hidden void libxl__logv(libxl_ctx *ctx, xentoollog_level msglevel, int errnoval,
183              const char *file /* may be 0 */, int line /* ignored if !file */,
184              const char *func /* may be 0 */,
185              uint32_t domid /* may be INVALID_DOMID */,
186              const char *fmt, va_list al)
187      __attribute__((format(printf,8,0)));
188 
189 _hidden void libxl__log(libxl_ctx *ctx, xentoollog_level msglevel, int errnoval,
190             const char *file /* may be 0 */, int line /* ignored if !file */,
191             const char *func /* may be 0 */,
192             uint32_t domid /* may be INVALID_DOMID */,
193             const char *fmt, ...)
194      __attribute__((format(printf,8,9)));
195 
196      /* these functions preserve errno (saving and restoring) */
197 
198 typedef struct libxl__gc libxl__gc;
199 typedef struct libxl__egc libxl__egc;
200 typedef struct libxl__ao libxl__ao;
201 typedef struct libxl__aop_occurred libxl__aop_occurred;
202 typedef struct libxl__osevent_hook_nexus libxl__osevent_hook_nexus;
203 typedef struct libxl__osevent_hook_nexi libxl__osevent_hook_nexi;
204 
205 typedef struct libxl__domain_create_state libxl__domain_create_state;
206 typedef void libxl__domain_create_cb(struct libxl__egc *egc,
207                                      libxl__domain_create_state *dcs,
208                                      int rc, uint32_t domid);
209 
210 typedef struct libxl__colo_device_nic libxl__colo_device_nic;
211 typedef struct libxl__colo_qdisk libxl__colo_qdisk;
212 typedef struct libxl__colo_proxy_state libxl__colo_proxy_state;
213 typedef struct libxl__colo_save_state libxl__colo_save_state;
214 typedef struct libxl__colo_restore_state libxl__colo_restore_state;
215 
216 _hidden void libxl__alloc_failed(libxl_ctx *, const char *func,
217                          size_t nmemb, size_t size) __attribute__((noreturn));
218   /* func, size and nmemb are used only in the log message.
219    * You may pass size==0 if size and nmemb are not meaningful
220    * and should not be printed. */
221 
222 typedef struct libxl__ev_fd libxl__ev_fd;
223 typedef void libxl__ev_fd_callback(libxl__egc *egc, libxl__ev_fd *ev,
224                                    int fd, short events, short revents);
225   /* Note that revents may contain POLLERR or POLLHUP regardless of
226    * events; otherwise revents contains only bits in events.  Contrary
227    * to the documentation for poll(2), POLLERR and POLLHUP can occur
228    * even if only POLLIN was set in events.  (POLLNVAL is a fatal
229    * error and will cause libxl event machinery to fail an assertion.)
230    *
231    * It is not permitted to listen for the same or overlapping events
232    * on the same fd using multiple different libxl__ev_fd's.
233    *
234    * (Spurious wakeups, and spurious bits set in revents, are
235    * suppressed by the libxl event core.)
236    */
237 struct libxl__ev_fd {
238     /* caller should include this in their own struct */
239     /* read-only for caller, who may read only when registered: */
240     int fd;
241     short events;
242     libxl__ev_fd_callback *func;
243     /* remainder is private for libxl__ev_fd... */
244     LIBXL_LIST_ENTRY(libxl__ev_fd) entry;
245     libxl__osevent_hook_nexus *nexus;
246 };
247 
248 
249 typedef struct libxl__ao_abortable libxl__ao_abortable;
250 typedef void libxl__ao_abortable_callback(libxl__egc *egc,
251                   libxl__ao_abortable *ao_abortable, int rc /* ABORTED */);
252 
253 struct libxl__ao_abortable {
254     /* caller must fill this in and it must remain valid */
255     libxl__ao *ao;
256     libxl__ao_abortable_callback *callback;
257     /* remainder is private for abort machinery */
258     bool registered;
259     LIBXL_LIST_ENTRY(libxl__ao_abortable) entry;
260     /*
261      * For nested aos:
262      *  Semantically, abort affects the whole tree of aos,
263      *    not just the parent.
264      *  libxl__ao_abortable.ao refers to the child, so
265      *    that the child callback sees the right ao.  (After all,
266      *    it was code dealing with the child that set .ao.)
267      *  But, the abortable is recorded on the "abortables" list
268      *    for the ultimate root ao, so that every possible child
269      *    abort occurs as a result of the abort of the parent.
270      *  We set ao->aborting only in the root.
271      */
272 };
273 
274 _hidden int libxl__ao_abortable_register(libxl__ao_abortable*);
275 _hidden void libxl__ao_abortable_deregister(libxl__ao_abortable*);
276 
libxl__ao_abortable_init(libxl__ao_abortable * c)277 static inline void libxl__ao_abortable_init
278   (libxl__ao_abortable *c) { c->registered = 0; }
libxl__ao_abortable_isregistered(const libxl__ao_abortable * c)279 static inline bool libxl__ao_abortable_isregistered
280   (const libxl__ao_abortable *c) { return c->registered; }
281 
282 int libxl__ao_aborting(libxl__ao *ao); /* -> 0 or ERROR_ABORTED */
283 
284 
285 typedef struct libxl__ev_time libxl__ev_time;
286 typedef void libxl__ev_time_callback(libxl__egc *egc, libxl__ev_time *ev,
287                                      const struct timeval *requested_abs,
288                                      int rc); /* TIMEDOUT or ABORTED */
289 struct libxl__ev_time {
290     /* caller should include this in their own struct */
291     /* read-only for caller, who may read only when registered: */
292     libxl__ev_time_callback *func;
293     /* remainder is private for libxl__ev_time... */
294     int infinite; /* not registered in list or with app if infinite */
295     LIBXL_TAILQ_ENTRY(libxl__ev_time) entry;
296     struct timeval abs;
297     libxl__osevent_hook_nexus *nexus;
298     libxl__ao_abortable abrt;
299 };
300 
301 typedef struct libxl__ev_xswatch libxl__ev_xswatch;
302 typedef void libxl__ev_xswatch_callback(libxl__egc *egc, libxl__ev_xswatch*,
303                             const char *watch_path, const char *event_path);
304 struct libxl__ev_xswatch {
305     /* caller should include this in their own struct */
306     /* read-only for caller, who may read only when registered: */
307     char *path;
308     libxl__ev_xswatch_callback *callback;
309     /* remainder is private for libxl__ev_xswatch... */
310     int slotnum; /* registered iff slotnum >= 0 */
311     uint32_t counterval;
312 };
313 
314 typedef struct libxl__ev_evtchn libxl__ev_evtchn;
315 typedef void libxl__ev_evtchn_callback(libxl__egc *egc, libxl__ev_evtchn*);
316 struct libxl__ev_evtchn {
317     /* caller must fill these in, and they must all remain valid */
318     libxl__ev_evtchn_callback *callback;
319     int port;
320     /* remainder is private for libxl__ev_evtchn_... */
321     bool waiting;
322     LIBXL_LIST_ENTRY(libxl__ev_evtchn) entry;
323 };
324 
325 /*
326  * An entry in the watch_slots table is either:
327  *  1. an entry in the free list, ie NULL or pointer to next free list entry
328  *  2. an pointer to a libxl__ev_xswatch
329  *
330  * But we don't want to use unions or type-punning because the
331  * compiler might "prove" that our code is wrong and misoptimise it.
332  *
333  * The rules say that all struct pointers have identical
334  * representation and alignment requirements (C99+TC1+TC2 6.2.5p26) so
335  * what we do is simply declare our array as containing only the free
336  * list pointers, and explicitly convert from and to our actual
337  * xswatch pointers when we store and retrieve them.
338  */
339 typedef struct libxl__ev_watch_slot {
340     LIBXL_SLIST_ENTRY(struct libxl__ev_watch_slot) empty;
341 } libxl__ev_watch_slot;
342 
343 _hidden libxl__ev_xswatch *libxl__watch_slot_contents(libxl__gc *gc,
344                                                       int slotnum);
345 
346 
347 typedef struct libxl__ev_child libxl__ev_child;
348 typedef void libxl__ev_child_callback(libxl__egc *egc, libxl__ev_child*,
349                                       pid_t pid, int status);
350 struct libxl__ev_child {
351     /* caller should include this in their own struct */
352     /* read-only for caller: */
353     pid_t pid; /* -1 means unused ("unregistered", ie Idle) */
354     libxl__ev_child_callback *callback;
355     /* remainder is private for libxl__ev_... */
356     LIBXL_LIST_ENTRY(struct libxl__ev_child) entry;
357 };
358 
359 
360 /*
361  * evgen structures, which are the state we use for generating
362  * events for the caller.
363  *
364  * In general in each case there's an internal and an external
365  * version of the _evdisable_FOO function; the internal one is
366  * used during cleanup.
367  */
368 struct libxl__evgen_domain_death {
369     uint32_t domid;
370     unsigned shutdown_reported:1, death_reported:1;
371     LIBXL_TAILQ_ENTRY(libxl_evgen_domain_death) entry;
372         /* on list .death_reported ? CTX->death_list : CTX->death_reported */
373     libxl_ev_user user;
374 };
375 _hidden void
376 libxl__evdisable_domain_death(libxl__gc*, libxl_evgen_domain_death*);
377 
378 struct libxl__evgen_disk_eject {
379     libxl__ev_xswatch watch;
380     uint32_t domid;
381     LIBXL_LIST_ENTRY(libxl_evgen_disk_eject) entry;
382     libxl_ev_user user;
383     char *vdev, *be_ptr_path;
384 };
385 _hidden void
386 libxl__evdisable_disk_eject(libxl__gc*, libxl_evgen_disk_eject*);
387 
388 typedef struct libxl__poller libxl__poller;
389 struct libxl__poller {
390     /*
391      * These are used to allow other threads to wake up a thread which
392      * may be stuck in poll, because whatever it was waiting for
393      * hadn't happened yet.  Threads which generate events will write
394      * a byte to each pipe.  A thread which is waiting will empty its
395      * own pipe, and put its poller on the pollers_event list, before
396      * releasing the ctx lock and going into poll; when it comes out
397      * of poll it will take the poller off the pollers_event list.
398      *
399      * A thread which is waiting for completion of a synchronous ao
400      * will allocate a poller and record it in the ao, so that other
401      * threads can wake it up.
402      *
403      * When a thread is done with a poller it should put it onto
404      * pollers_idle, where it can be reused later.
405      *
406      * The "poller_app" is never idle, but is sometimes on
407      * pollers_event.
408      */
409     LIBXL_LIST_ENTRY(libxl__poller) entry;
410 
411     struct pollfd *fd_polls;
412     int fd_polls_allocd;
413 
414     int fd_rindices_allocd;
415     int (*fd_rindices)[3]; /* see libxl_event.c:beforepoll_internal */
416 
417     int wakeup_pipe[2]; /* 0 means no fd allocated */
418 
419     /*
420      * We also use the poller to record whether any fds have been
421      * deregistered since we entered poll.  Each poller which is not
422      * idle is on the list pollers_fds_changed.  fds_changed is
423      * cleared by beforepoll, and tested by afterpoll.  Whenever an fd
424      * event is deregistered, we set the fds_changed of all non-idle
425      * pollers.  So afterpoll can tell whether any POLLNVAL is
426      * plausibly due to an fd being closed and reopened.
427      */
428     LIBXL_LIST_ENTRY(libxl__poller) fds_changed_entry;
429     bool fds_changed;
430 };
431 
432 struct libxl__gc {
433     /* mini-GC */
434     int alloc_maxsize; /* -1 means this is the dummy non-gc gc */
435     void **alloc_ptrs;
436     libxl_ctx *owner;
437 };
438 
439 struct libxl__ctx {
440     xentoollog_logger *lg;
441     xc_interface *xch;
442     struct xs_handle *xsh;
443     libxl__gc nogc_gc;
444 
445     const libxl_event_hooks *event_hooks;
446     void *event_hooks_user;
447 
448     pthread_mutex_t lock; /* protects data structures hanging off the ctx */
449       /* Always use libxl__ctx_lock and _unlock (or the convenience
450        * macors CTX_LOCK and CTX_UNLOCK) to manipulate this.
451        *
452        * You may acquire this mutex recursively if it is convenient to
453        * do so.  You may not acquire this lock at the same time as any
454        * other lock.  If you need to call application code outside
455        * libxl (ie, a callback) with this lock held then it is
456        * necessaray to impose restrictions on the caller to maintain a
457        * proper lock hierarchy, and these restrictions must then be
458        * documented in the libxl public interface.
459        */
460 
461     LIBXL_TAILQ_HEAD(libxl__event_list, libxl_event) occurred;
462 
463     int osevent_in_hook;
464     const libxl_osevent_hooks *osevent_hooks;
465     void *osevent_user;
466       /* See the comment for OSEVENT_HOOK_INTERN in libxl_event.c
467        * for restrictions on the use of the osevent fields. */
468 
469     libxl__poller *poller_app; /* libxl_osevent_beforepoll and _afterpoll */
470     LIBXL_LIST_HEAD(, libxl__poller) pollers_event, pollers_idle;
471     LIBXL_LIST_HEAD(, libxl__poller) pollers_fds_changed;
472 
473     LIBXL_SLIST_HEAD(libxl__osevent_hook_nexi, libxl__osevent_hook_nexus)
474         hook_fd_nexi_idle, hook_timeout_nexi_idle;
475     LIBXL_LIST_HEAD(, libxl__ev_fd) efds;
476     LIBXL_TAILQ_HEAD(, libxl__ev_time) etimes;
477 
478     libxl__ev_watch_slot *watch_slots;
479     int watch_nslots, nwatches;
480     LIBXL_SLIST_HEAD(, libxl__ev_watch_slot) watch_freeslots;
481     uint32_t watch_counter; /* helps disambiguate slot reuse */
482     libxl__ev_fd watch_efd;
483 
484     xenevtchn_handle *xce; /* waiting must be done only with libxl__ev_evtchn* */
485     LIBXL_LIST_HEAD(, libxl__ev_evtchn) evtchns_waiting;
486     libxl__ev_fd evtchn_efd;
487 
488     LIBXL_LIST_HEAD(, libxl__ao) aos_inprogress;
489 
490     LIBXL_TAILQ_HEAD(libxl__evgen_domain_death_list, libxl_evgen_domain_death)
491         death_list /* sorted by domid */,
492         death_reported;
493     libxl__ev_xswatch death_watch;
494 
495     LIBXL_LIST_HEAD(, libxl_evgen_disk_eject) disk_eject_evgens;
496 
497     const libxl_childproc_hooks *childproc_hooks;
498     void *childproc_user;
499     int sigchld_selfpipe[2]; /* [0]==-1 means handler not installed */
500     libxl__ev_fd sigchld_selfpipe_efd;
501     LIBXL_LIST_HEAD(, libxl__ev_child) children;
502     bool sigchld_user_registered;
503     LIBXL_LIST_ENTRY(libxl_ctx) sigchld_users_entry;
504 
505     libxl_version_info version_info;
506 };
507 
508 /*
509  * libxl__device is a transparent structure that doesn't contain private fields
510  * or external memory references, and as such can be copied by assignment.
511  */
512 typedef struct {
513     uint32_t backend_devid;
514     uint32_t backend_domid;
515     uint32_t devid;
516     uint32_t domid;
517     libxl__device_kind backend_kind;
518     libxl__device_kind kind;
519 } libxl__device;
520 
521 /* Used to know if backend of given device is QEMU */
522 #define QEMU_BACKEND(dev) (\
523     (dev)->backend_kind == LIBXL__DEVICE_KIND_QDISK || \
524     (dev)->backend_kind == LIBXL__DEVICE_KIND_VFB || \
525     (dev)->backend_kind == LIBXL__DEVICE_KIND_QUSB || \
526     (dev)->backend_kind == LIBXL__DEVICE_KIND_VKBD)
527 
528 #define XC_PCI_BDF             "0x%x, 0x%x, 0x%x, 0x%x"
529 #define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))
530 #define PCI_SLOT(devfn)         (((devfn) >> 3) & 0x1f)
531 #define PCI_FUNC(devfn)         ((devfn) & 0x07)
532 #define AUTO_PHP_SLOT          0x100
533 
534 #define PROC_PCI_NUM_RESOURCES 7
535 #define PCI_BAR_IO             0x01
536 
537 #define PRINTF_ATTRIBUTE(x, y) __attribute__((format(printf, x, y)))
538 
539 struct libxl__egc {
540     /* For event-generating functions only.
541      * The egc and its gc may be accessed only on the creating thread. */
542     struct libxl__gc gc;
543     struct libxl__event_list occurred_for_callback;
544     LIBXL_TAILQ_HEAD(, libxl__ao) aos_for_callback;
545     LIBXL_TAILQ_HEAD(, libxl__aop_occurred) aops_for_callback;
546 };
547 
548 struct libxl__aop_occurred {
549     /*
550      * An aop belongs to, and may be accessed only on, the thread
551      * which created it.  It normally lives in that thread's egc.
552      *
553      * While an aop exists, it corresponds to one refcount in
554      * ao->progress_reports_outstanding, preventing ao destruction.
555      */
556     LIBXL_TAILQ_ENTRY(libxl__aop_occurred) entry;
557     libxl__ao *ao;
558     libxl_event *ev;
559     const libxl_asyncprogress_how *how;
560 };
561 
562 #define LIBXL__AO_MAGIC              0xA0FACE00ul
563 #define LIBXL__AO_MAGIC_DESTROYED    0xA0DEAD00ul
564 
565 struct libxl__ao {
566     /*
567      * An ao and its gc may be accessed only with the ctx lock held.
568      *
569      * Special exception: If an ao has been added to
570      * egc->aos_for_callback, the thread owning the egc may remove the
571      * ao from that list and make the callback without holding the
572      * lock.
573      *
574      * Corresponding restriction: An ao may be added only to one
575      * egc->aos_for_callback, once; rc and how must already have been
576      * set and may not be subsequently modified.  (This restriction is
577      * easily and obviously met since the ao is queued for callback
578      * only in libxl__ao_complete.)
579      */
580     uint32_t magic;
581     unsigned constructing:1, in_initiator:1, complete:1, notified:1,
582         aborting:1;
583     int manip_refcnt;
584     libxl__ao *nested_root;
585     int nested_progeny;
586     int progress_reports_outstanding;
587     int rc;
588     LIBXL_LIST_HEAD(, libxl__ao_abortable) abortables;
589     LIBXL_LIST_ENTRY(libxl__ao) inprogress_entry;
590     libxl__gc gc;
591     libxl_asyncop_how how;
592     libxl__poller *poller;
593     uint32_t domid;
594     LIBXL_TAILQ_ENTRY(libxl__ao) entry_for_callback;
595 };
596 
597 #define LIBXL_INIT_GC(gc,ctx) do{               \
598         (gc).alloc_maxsize = 0;                 \
599         (gc).alloc_ptrs = 0;                    \
600         (gc).owner = (ctx);                     \
601     } while(0)
602     /* NB, also, a gc struct ctx->nogc_gc is initialised in libxl_ctx_alloc */
603 
libxl__gc_owner(libxl__gc * gc)604 static inline libxl_ctx *libxl__gc_owner(libxl__gc *gc)
605 {
606     return gc->owner;
607 }
608 
libxl__gc_is_real(const libxl__gc * gc)609 static inline int libxl__gc_is_real(const libxl__gc *gc)
610 {
611     return gc->alloc_maxsize >= 0;
612 }
613 
614 /*
615  * Memory allocation tracking/helpers
616  *
617  * See comment "libxl memory management" in libxl.h for a description
618  * of the framework which these calls belong to.
619  *
620  * These functions deal with memory allocations of type (a) and (d) in
621  * that description.
622  *
623  * All pointers returned by these functions are registered for garbage
624  * collection on exit from the outermost libxl callframe.
625  *
626  * However, where the argument is stated to be "gc_opt", &ctx->nogc_gc
627  * may be passed instead, in which case no garbage collection will
628  * occur; the pointer must later be freed with free().  (Passing NULL
629  * for gc_opt is not permitted.)  This is for memory allocations of
630  * types (b) and (c).  The convenience macro NOGC should be used where
631  * possible.
632  *
633  * NOGC (and ctx->nogc_gc) may ONLY be used with functions which
634  * explicitly declare that it's OK.  Use with nonconsenting functions
635  * may result in leaks of those functions' internal allocations on the
636  * psuedo-gc.
637  */
638 /* register ptr in gc for free on exit from outermost libxl callframe. */
639 
640 #define NN(...) __attribute__((nonnull(__VA_ARGS__)))
641 #define NN1 __attribute__((nonnull(1)))
642  /* It used to be legal to pass NULL for gc_opt.  Get the compiler to
643   * warn about this if any slip through. */
644 
645 _hidden void libxl__ptr_add(libxl__gc *gc_opt, void *ptr /* may be NULL */) NN1;
646 /* if this is the outermost libxl callframe then free all pointers in @gc */
647 _hidden void libxl__free_all(libxl__gc *gc);
648 /* allocate @size bytes. (a gc'd malloc(3)) */
649 _hidden void *libxl__malloc(libxl__gc *gc_opt, size_t size) NN1;
650 /* allocate and zero @size. (similar to a gc'd malloc(3)+memzero()) */
651 _hidden void *libxl__zalloc(libxl__gc *gc_opt, size_t size) NN1;
652 /* allocate and zero memory for an array of @nmemb members of @size each.
653  * (similar to a gc'd calloc(3)). */
654 _hidden void *libxl__calloc(libxl__gc *gc_opt, size_t nmemb, size_t size) NN1;
655 /* change the size of the memory block pointed to by @ptr to @new_size bytes.
656  * unlike other allocation functions here any additional space between the
657  * oldsize and @new_size is not initialised (similar to a gc'd realloc(3)).
658  * if @ptr is non-NULL and @gc_opt is not nogc_gc then @ptr must have been
659  * registered with @gc_opt previously. */
660 _hidden void *libxl__realloc(libxl__gc *gc_opt, void *ptr, size_t new_size) NN1;
661 /* print @fmt into an allocated string large enoughto contain the result.
662  * (similar to gc'd asprintf(3)). */
663 _hidden char *libxl__sprintf(libxl__gc *gc_opt, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3) NN1;
664 _hidden char *libxl__vsprintf(libxl__gc *gc, const char *format, va_list ap) PRINTF_ATTRIBUTE(2, 0);
665 /* duplicate the string @c (similar to a gc'd strdup(3)). */
666 _hidden char *libxl__strdup(libxl__gc *gc_opt,
667                             const char *c /* may be NULL */) NN1;
668 /* duplicate at most @n bytes of string @c (similar to a gc'd strndup(3)). */
669 _hidden char *libxl__strndup(libxl__gc *gc_opt,
670                              const char *c /* may be NULL */,
671                              size_t n) NN1;
672 /* strip the last path component from @s and return as a newly allocated
673  * string. (similar to a gc'd dirname(3)). */
674 _hidden char *libxl__dirname(libxl__gc *gc_opt, const char *s) NN1;
675 
676 /* Make a pipe and set both ends nonblocking.  On error, nothing
677  * is left open and both fds[]==-1, and a message is logged.
678  * Useful for self-pipes. */
679 _hidden int libxl__pipe_nonblock(libxl_ctx *ctx, int fds[2]);
680 /* Closes the pipe fd(s).  Either or both of fds[] may be -1 meaning
681  * `not open'.  Ignores any errors.  Sets fds[] to -1. */
682 _hidden void libxl__pipe_close(int fds[2]);
683 
684 /* Change the flags for the file description associated with fd to
685  *    (flags & mask) | val.
686  * If r_oldflags != NULL then sets *r_oldflags to the original set of
687  * flags.
688  */
689 _hidden int libxl__fd_flags_modify_save(libxl__gc *gc, int fd,
690                                         int mask, int val, int *r_oldflags);
691 /* Restores the flags for the file description associated with fd to
692  * to the previous value (returned by libxl__fd_flags_modify_save)
693  */
694 _hidden int libxl__fd_flags_restore(libxl__gc *gc, int fd, int old_flags);
695 
696 /* Each of these logs errors and returns a libxl error code.
697  * They do not mind if path is already removed.
698  * For _file, path must not be a directory; for _directory it must be. */
699 _hidden int libxl__remove_file(libxl__gc *gc, const char *path);
700 _hidden int libxl__remove_directory(libxl__gc *gc, const char *path);
701 _hidden int libxl__remove_file_or_directory(libxl__gc *gc, const char *path);
702 
703 
704 _hidden char **libxl__xs_kvs_of_flexarray(libxl__gc *gc, flexarray_t *array);
705 
706 /* treats kvs as pairs of keys and values and writes each to dir. */
707 _hidden int libxl__xs_writev(libxl__gc *gc, xs_transaction_t t,
708                              const char *dir, char **kvs);
709 /* as writev but also sets the permissions on each path */
710 _hidden int libxl__xs_writev_perms(libxl__gc *gc, xs_transaction_t t,
711                                    const char *dir, char *kvs[],
712                                    struct xs_permissions *perms,
713                                    unsigned int num_perms);
714 /* _atonce creates a transaction and writes all keys at once */
715 _hidden int libxl__xs_writev_atonce(libxl__gc *gc,
716                              const char *dir, char **kvs);
717    /* Each fn returns 0 on success.
718     * On error: returns -1, sets errno (no logging) */
719 
720 _hidden char *libxl__xs_get_dompath(libxl__gc *gc, uint32_t domid);
721    /* On error: logs, returns NULL, sets errno. */
722 
723 _hidden char *libxl__xs_read(libxl__gc *gc, xs_transaction_t t,
724                              const char *path);
725 _hidden char **libxl__xs_directory(libxl__gc *gc, xs_transaction_t t,
726                                    const char *path, unsigned int *nb);
727    /* On error: returns NULL, sets errno (no logging) */
728 _hidden char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid);
729 
730 _hidden int libxl__backendpath_parse_domid(libxl__gc *gc, const char *be_path,
731                                            libxl_domid *domid_out);
732 
733 /*----- "checked" xenstore access functions -----*/
734 /* Each of these functions will check that it succeeded; if it
735  * fails it logs and returns ERROR_FAIL.
736  */
737 
738 int libxl__xs_vprintf(libxl__gc *gc, xs_transaction_t t,
739                       const char *path, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(4, 0);
740 int libxl__xs_printf(libxl__gc *gc, xs_transaction_t t,
741                      const char *path, const char *fmt, ...) PRINTF_ATTRIBUTE(4, 5);
742 
743 /* On success, path will exist and will have an empty value */
744 int libxl__xs_mknod(libxl__gc *gc, xs_transaction_t t,
745                     const char *path, struct xs_permissions *perms,
746                     unsigned int num_perms);
747 
748 /* On success, *result_out came from the gc.
749  * On error, *result_out is undefined.
750  * ENOENT is regarded as error.
751  */
752 int libxl__xs_read_mandatory(libxl__gc *gc, xs_transaction_t t,
753                              const char *path, const char **result_out);
754 
755 /* On success, *result_out came from the gc.
756  * On error, *result_out is undefined.
757  * ENOENT counts as success but sets *result_out=0
758  */
759 int libxl__xs_read_checked(libxl__gc *gc, xs_transaction_t t,
760                            const char *path, const char **result_out);
761 
762 /* Does not include a trailing null.
763  * May usefully be combined with GCSPRINTF if the format string
764  * behaviour of libxl__xs_printf is desirable. */
765 int libxl__xs_write_checked(libxl__gc *gc, xs_transaction_t t,
766                             const char *path, const char *string);
767 
768 /* ENOENT is not an error (even if the parent directories don't exist) */
769 int libxl__xs_rm_checked(libxl__gc *gc, xs_transaction_t t, const char *path);
770 
771 /* Transaction functions, best used together.
772  * The caller should initialise *t to 0 (XBT_NULL) before calling start.
773  * Each function leaves *t!=0 iff the transaction needs cleaning up.
774  *
775  * libxl__xs_transaction_commit returns:
776  *   <0  failure - a libxl error code
777  *   +1  commit conflict; transaction has been destroyed and caller
778  *        must go round again (call _start again and retry)
779  *    0  committed successfully
780  *
781  * The intended usage pattern looks like this:
782  *    int some_function()
783  *    {
784  *        int rc;
785  *        xs_transaction_t t = 0;
786  *        // other initialisations
787  *
788  *        // do whatever you need to do before the xenstore stuff
789  *        // errors?  set rc and goto out.
790  *
791  *        for (;;) {
792  *            rc = libxl__xs_transaction_start(gc, &t);
793  *            if (rc) goto out;
794  *
795  *            // do your work here, including all xenstore reads and writes
796  *            // libxl__xs_*_checked are useful; pass them t.
797  *            // errors?  set rc and goto out.
798  *
799  *            rc = libxl__xs_transaction_commit(gc, &t);
800  *            if (!rc) break;
801  *            if (rc<0) goto out;
802  *        }
803  *
804  *        // now the xenstore transaction succeeded
805  *        // do whatever else you need to do
806  *        // errors?  set rc and goto out.
807  *
808  *        return something;
809  *
810  *     out:
811  *        // other cleanups
812  *        libxl__xs_transaction_abort(gc, &t);
813  *        // other cleanups
814  *        return rc;
815  *    }
816  *
817  * Formally the states of *t are:
818  *
819  *  name     value of *t  description
820  *   Idle         0         no transaction exists
821  *   Ready        non-0     ready for work, nothing done yet
822  *   Busy         non-0     writes have been made but we are not finished
823  *   Uncommitted  non-0     writes have been made and should be committed
824  *
825  * libxl__xs_transaction_start:  Idle -> Ready (on error: Idle)
826  *
827  * The transaction goes from Ready to Busy, and from Busy to
828  * Uncommitted, by the use of xenstore read and write operations
829  * (libxl__xs_..., xs_...) made by libxl__xs_transaction's caller.
830  *
831  * libxl__xs_transaction_commit:  Ready/Uncommitted -> Idle
832  *     on success (returns 0): xenstore has been updated
833  *     on error (<0) or conflict (+1): updates discarded
834  *
835  * libxl__xs_transaction_abort:  Any -> Idle  (any updates discarded)
836  */
837 int libxl__xs_transaction_start(libxl__gc *gc, xs_transaction_t *t);
838 int libxl__xs_transaction_commit(libxl__gc *gc, xs_transaction_t *t);
839 void libxl__xs_transaction_abort(libxl__gc *gc, xs_transaction_t *t);
840 
841 
842 
843 /*
844  * This is a recursive delete, from top to bottom. What this function does
845  * is remove empty folders that contained the deleted entry.
846  *
847  * It mimics xenstore-rm -t behaviour.
848  */
849 _hidden int libxl__xs_path_cleanup(libxl__gc *gc, xs_transaction_t t,
850                                    const char *user_path);
851 
852 /*
853  * Event generation functions provided by the libxl event core to the
854  * rest of libxl.  Implemented in terms of _beforepoll/_afterpoll
855  * and/or the fd registration machinery, as provided by the
856  * application.
857  *
858  * Semantics are similar to those of the fd and timeout registration
859  * functions provided to libxl_osevent_register_hooks.
860  *
861  * Non-0 returns from libxl__ev_{modify,deregister} have already been
862  * logged by the core and should be returned unmodified to libxl's
863  * caller; NB that they may be valid libxl error codes but they may
864  * also be positive numbers supplied by the caller.
865  *
866  * In each case, there is a libxl__ev_FOO structure which can be in
867  * one of three states:
868  *
869  *   Undefined   - Might contain anything.  All-bits-zero is
870  *                 an undefined state.
871  *
872  *   Idle        - Struct contents are defined enough to pass to any
873  *                 libxl__ev_FOO function but not registered and
874  *                 callback will not be called.  The struct does not
875  *                 contain references to any allocated resources so
876  *                 can be thrown away.
877  *
878  *   Active      - Request for events has been registered and events
879  *                 may be generated.  _deregister must be called to
880  *                 reclaim resources.
881  *
882  * These functions are provided for each kind of event KIND:
883  *
884  *   int libxl__ev_KIND_register(libxl__gc *gc, libxl__ev_KIND *GEN,
885  *                              libxl__ev_KIND_callback *FUNC,
886  *                              DETAILS);
887  *      On entry *GEN must be in state Undefined or Idle.
888  *      Returns a libxl error code; on error return *GEN is Idle.
889  *      On successful return *GEN is Active and FUNC wil be
890  *      called by the event machinery in future.  FUNC will
891  *      not be called from within the call to _register.
892  *      FUNC will be called with the context locked (with CTX_LOCK).
893  *
894  *  void libxl__ev_KIND_deregister(libxl__gc *gc, libxl__ev_KIND *GEN_upd);
895  *      On entry *GEN must be in state Active or Idle.
896  *      On return it is Idle.  (Idempotent.)
897  *
898  *  void libxl__ev_KIND_init(libxl__ev_KIND *GEN);
899  *      Provided for initialising an Undefined KIND.
900  *      On entry *GEN must be in state Idle or Undefined.
901  *      On return it is Idle.  (Idempotent.)
902  *
903  *  int libxl__ev_KIND_isregistered(const libxl__ev_KIND *GEN);
904  *      On entry *GEN must be Idle or Active.
905  *      Returns nonzero if it is Active, zero otherwise.
906  *      Cannot fail.
907  *
908  *  int libxl__ev_KIND_modify(libxl__gc*, libxl__ev_KIND *GEN,
909  *                            DETAILS);
910  *      Only provided for some kinds of generator.
911  *      On entry *GEN must be Active and on return, whether successful
912  *      or not, it will be Active.
913  *      Returns a libxl error code; on error the modification
914  *      is not effective.
915  *
916  * All of these functions are fully threadsafe and may be called by
917  * general code in libxl even from within event callback FUNCs.
918  * The ctx will be locked on entry to each FUNC and FUNC should not
919  * unlock it.
920  *
921  * Callers of libxl__ev_KIND_register must ensure that the
922  * registration is undone, with _deregister, in libxl_ctx_free.
923  * This means that normally each kind of libxl__evgen (ie each
924  * application-requested event source) needs to be on a list so that
925  * it can be automatically deregistered as promised in libxl_event.h.
926  */
927 
928 
929 _hidden int libxl__ev_fd_register(libxl__gc*, libxl__ev_fd *ev_out,
930                                   libxl__ev_fd_callback*,
931                                   int fd, short events /* as for poll(2) */);
932 _hidden int libxl__ev_fd_modify(libxl__gc*, libxl__ev_fd *ev,
933                                 short events);
934 _hidden void libxl__ev_fd_deregister(libxl__gc*, libxl__ev_fd *ev);
libxl__ev_fd_init(libxl__ev_fd * efd)935 static inline void libxl__ev_fd_init(libxl__ev_fd *efd)
936                     { efd->fd = -1; }
libxl__ev_fd_isregistered(const libxl__ev_fd * efd)937 static inline int libxl__ev_fd_isregistered(const libxl__ev_fd *efd)
938                     { return efd->fd >= 0; }
939 
940 _hidden int libxl__ev_time_register_rel(libxl__ao*, libxl__ev_time *ev_out,
941                                         libxl__ev_time_callback*,
942                                         int milliseconds /* as for poll(2) */);
943 _hidden int libxl__ev_time_register_abs(libxl__ao*, libxl__ev_time *ev_out,
944                                         libxl__ev_time_callback*,
945                                         struct timeval);
946 _hidden int libxl__ev_time_modify_rel(libxl__gc*, libxl__ev_time *ev,
947                                       int milliseconds /* as for poll(2) */);
948 _hidden int libxl__ev_time_modify_abs(libxl__gc*, libxl__ev_time *ev,
949                                       struct timeval);
950 _hidden void libxl__ev_time_deregister(libxl__gc*, libxl__ev_time *ev);
libxl__ev_time_init(libxl__ev_time * ev)951 static inline void libxl__ev_time_init(libxl__ev_time *ev)
952                 { ev->func = 0; libxl__ao_abortable_init(&ev->abrt); }
libxl__ev_time_isregistered(const libxl__ev_time * ev)953 static inline int libxl__ev_time_isregistered(const libxl__ev_time *ev)
954                 { return !!ev->func; }
955 
956 
957 _hidden int libxl__ev_xswatch_register(libxl__gc*, libxl__ev_xswatch *xsw_out,
958                                        libxl__ev_xswatch_callback*,
959                                        const char *path /* copied */);
960 _hidden void libxl__ev_xswatch_deregister(libxl__gc *gc, libxl__ev_xswatch*);
961 
libxl__ev_xswatch_init(libxl__ev_xswatch * xswatch_out)962 static inline void libxl__ev_xswatch_init(libxl__ev_xswatch *xswatch_out)
963                 { xswatch_out->slotnum = -1; }
libxl__ev_xswatch_isregistered(const libxl__ev_xswatch * xw)964 static inline int libxl__ev_xswatch_isregistered(const libxl__ev_xswatch *xw)
965                 { return xw->slotnum >= 0; }
966 
967 
968 /*
969  * The evtchn facility is one-shot per call to libxl__ev_evtchn_wait.
970  * You should:
971  *   Use libxl__ctx_evtchn_init to make sure CTX->xce is valid;
972  *   Call some suitable xc bind function on (or to obtain) the port;
973  *   Then call libxl__ev_evtchn_wait.
974  *
975  * When the event is signaled then the callback will be made, once.
976  * Then you must call libxl__ev_evtchn_wait again, if desired.
977  *
978  * You must NOT call xenevtchn_unmask.  wait will do that for you.
979  *
980  * Calling libxl__ev_evtchn_cancel will arrange for libxl to disregard
981  * future occurrences of event.  Both libxl__ev_evtchn_wait and
982  * libxl__ev_evtchn_cancel are idempotent.
983  *
984  * (Note of course that an event channel becomes signaled when it is
985  * first bound, so you will get one call to libxl__ev_evtchn_wait
986  * "right away"; unless you have won a very fast race, the condition
987  * you were waiting for won't exist yet so when you check for it
988  * you'll find you need to call wait again.)
989  *
990  * You must not wait on the same port twice at once (that is, with
991  * two separate libxl__ev_evtchn's).
992  */
993 _hidden int libxl__ev_evtchn_wait(libxl__gc*, libxl__ev_evtchn *evev);
994 _hidden void libxl__ev_evtchn_cancel(libxl__gc *gc, libxl__ev_evtchn *evev);
995 
libxl__ev_evtchn_init(libxl__ev_evtchn * evev)996 static inline void libxl__ev_evtchn_init(libxl__ev_evtchn *evev)
997                 { evev->waiting = 0; }
libxl__ev_evtchn_iswaiting(const libxl__ev_evtchn * evev)998 static inline bool libxl__ev_evtchn_iswaiting(const libxl__ev_evtchn *evev)
999                 { return evev->waiting; }
1000 
1001 _hidden int libxl__ctx_evtchn_init(libxl__gc *gc); /* for libxl_ctx_alloc */
1002 
1003 /*
1004  * For making subprocesses.  This is the only permitted mechanism for
1005  * code in libxl to do so.
1006  *
1007  * In the parent, returns the pid, filling in childw_out.
1008  * In the child, returns 0.
1009  * If it fails, returns a libxl error (all of which are -ve).
1010  *
1011  * The child should go on to exec (or exit) soon.  The child may not
1012  * make any further calls to libxl infrastructure, except for memory
1013  * allocation and logging.  If the child needs to use xenstore it
1014  * must open its own xs handle and use it directly, rather than via
1015  * the libxl event machinery.
1016  *
1017  * The parent may signal the child but it must not reap it.  That will
1018  * be done by the event machinery.
1019  *
1020  * It is not possible to "deregister" the child death event source.
1021  * It will generate exactly one event callback; until then the childw
1022  * is Active and may not be reused.
1023  */
1024 _hidden pid_t libxl__ev_child_fork(libxl__gc *gc, libxl__ev_child *childw_out,
1025                                  libxl__ev_child_callback *death);
libxl__ev_child_init(libxl__ev_child * childw_out)1026 static inline void libxl__ev_child_init(libxl__ev_child *childw_out)
1027                 { childw_out->pid = -1; }
libxl__ev_child_inuse(const libxl__ev_child * childw_out)1028 static inline int libxl__ev_child_inuse(const libxl__ev_child *childw_out)
1029                 { return childw_out->pid >= 0; }
1030 
1031 /* Useable (only) in the child to once more make the ctx useable for
1032  * xenstore operations.  logs failure in the form "what: <error
1033  * message>". */
1034 _hidden int libxl__ev_child_xenstore_reopen(libxl__gc *gc, const char *what);
1035 
1036 
1037 /*
1038  * Other event-handling support provided by the libxl event core to
1039  * the rest of libxl.
1040  */
1041 
1042 _hidden void libxl__event_occurred(libxl__egc*, libxl_event *event);
1043   /* Arranges to notify the application that the event has occurred.
1044    * event should be suitable for passing to libxl_event_free. */
1045 
1046 _hidden libxl_event *libxl__event_new(libxl__egc*, libxl_event_type,
1047                                       uint32_t domid,
1048                                       libxl_ev_user for_user);
1049   /* Convenience function.
1050    * Allocates a new libxl_event, fills in domid and type.
1051    * Cannot fail. */
1052 
1053 #define NEW_EVENT(egc, type, domid, user)                        \
1054     libxl__event_new((egc), LIBXL_EVENT_TYPE_##type, (domid), (user))
1055     /* Convenience macro. */
1056 
1057 /*
1058  * In general, call this via the macro LIBXL__EVENT_DISASTER.
1059  *
1060  * Event-generating functions may call this if they might have wanted
1061  * to generate an event (either an internal one ie a
1062  * libxl__ev_FOO_callback or an application event), but are prevented
1063  * from doing so due to eg lack of memory.
1064  *
1065  * NB that this function may return and the caller isn't supposed to
1066  * then crash, although it may fail (and henceforth leave things in a
1067  * state where many or all calls fail).
1068  */
1069 _hidden void libxl__event_disaster(libxl__egc*, const char *msg, int errnoval,
1070                                    libxl_event_type type /* may be 0 */,
1071                                    const char *file, int line,
1072                                    const char *func);
1073 #define LIBXL__EVENT_DISASTER(egc, msg, errnoval, type) \
1074     libxl__event_disaster(egc, msg, errnoval, type, __FILE__,__LINE__,__func__)
1075 
1076 
1077 /* Fills in, or disposes of, the resources held by, a poller whose
1078  * space the caller has allocated.  ctx must be locked. */
1079 _hidden int libxl__poller_init(libxl__gc *gc, libxl__poller *p);
1080 _hidden void libxl__poller_dispose(libxl__poller *p);
1081 
1082 /* Obtain a fresh poller from malloc or the idle list, and put it
1083  * away again afterwards.  _get can fail, returning NULL.
1084  * ctx must be locked. */
1085 _hidden libxl__poller *libxl__poller_get(libxl__gc *gc);
1086 _hidden void libxl__poller_put(libxl_ctx*, libxl__poller *p /* may be NULL */);
1087 
1088 /* Notifies whoever is polling using p that they should wake up.
1089  * ctx must be locked. */
1090 _hidden void libxl__poller_wakeup(libxl__egc *egc, libxl__poller *p);
1091 
1092 /* Internal to fork and child reaping machinery */
1093 extern const libxl_childproc_hooks libxl__childproc_default_hooks;
1094 int libxl__sigchld_needed(libxl__gc*); /* non-reentrant idempotent, logs errs */
1095 void libxl__sigchld_notneeded(libxl__gc*); /* non-reentrant idempotent */
1096 void libxl__sigchld_check_stale_handler(void);
1097 int libxl__self_pipe_wakeup(int fd); /* returns 0 or -1 setting errno */
1098 int libxl__self_pipe_eatall(int fd); /* returns 0 or -1 setting errno */
1099 
1100 
1101 _hidden int libxl__atfork_init(libxl_ctx *ctx);
1102 
1103 
1104 /* File references */
1105 typedef struct {
1106     /*
1107      * Path is always set if the file reference is valid. However if
1108      * mapped is true then the actual file may already be unlinked.
1109      */
1110     const char * path;
1111     int mapped;
1112     void * data;
1113     size_t size;
1114 } libxl__file_reference;
1115 _hidden int libxl__file_reference_map(libxl__file_reference *f);
1116 _hidden int libxl__file_reference_unmap(libxl__file_reference *f);
1117 
1118 /* from xl_dom */
1119 _hidden libxl_domain_type libxl__domain_type(libxl__gc *gc, uint32_t domid);
1120 _hidden int libxl__domain_cpupool(libxl__gc *gc, uint32_t domid);
1121 _hidden libxl_scheduler libxl__domain_scheduler(libxl__gc *gc, uint32_t domid);
1122 _hidden int libxl__sched_set_params(libxl__gc *gc, uint32_t domid,
1123                                     libxl_domain_sched_params *scparams);
1124 _hidden int libxl__grant_vga_iomem_permission(libxl__gc *gc, const uint32_t domid,
1125                                               libxl_domain_config *const d_config);
1126 
1127 typedef struct {
1128     uint32_t store_port;
1129     uint32_t store_domid;
1130     unsigned long store_mfn;
1131 
1132     uint32_t console_port;
1133     uint32_t console_domid;
1134     unsigned long console_mfn;
1135     char *console_tty;
1136 
1137     char *saved_state;
1138 
1139     libxl__file_reference pv_kernel;
1140     libxl__file_reference pv_ramdisk;
1141     const char * shim_path;
1142     const char * shim_cmdline;
1143     const char * pv_cmdline;
1144 
1145     xen_vmemrange_t *vmemranges;
1146     uint32_t num_vmemranges;
1147 
1148     xc_domain_configuration_t config;
1149 
1150     xen_pfn_t vuart_gfn;
1151     evtchn_port_t vuart_port;
1152 } libxl__domain_build_state;
1153 
1154 _hidden int libxl__build_pre(libxl__gc *gc, uint32_t domid,
1155               libxl_domain_config * const d_config,
1156               libxl__domain_build_state *state);
1157 _hidden int libxl__build_post(libxl__gc *gc, uint32_t domid,
1158                libxl_domain_build_info *info, libxl__domain_build_state *state,
1159                char **vms_ents, char **local_ents);
1160 
1161 _hidden int libxl__build_pv(libxl__gc *gc, uint32_t domid,
1162              libxl_domain_build_info *info, libxl__domain_build_state *state);
1163 _hidden int libxl__build_hvm(libxl__gc *gc, uint32_t domid,
1164               libxl_domain_config *d_config,
1165               libxl__domain_build_state *state);
1166 
1167 _hidden int libxl__qemu_traditional_cmd(libxl__gc *gc, uint32_t domid,
1168                                         const char *cmd);
1169 _hidden int libxl__domain_rename(libxl__gc *gc, uint32_t domid,
1170                                  const char *old_name, const char *new_name,
1171                                  xs_transaction_t trans);
1172 
1173 _hidden int libxl__domain_resume_device_model(libxl__gc *gc, uint32_t domid);
1174 
1175 _hidden const char *libxl__userdata_path(libxl__gc *gc, uint32_t domid,
1176                                          const char *userdata_userid,
1177                                          const char *wh);
1178 _hidden void libxl__userdata_destroyall(libxl__gc *gc, uint32_t domid);
1179 /* Caller must hold userdata store lock before calling
1180  * libxl__userdata_{retrieve,store}
1181  * See libxl__{un,}lock_domain_userdata.
1182  */
1183 _hidden int libxl__userdata_retrieve(libxl__gc *gc, uint32_t domid,
1184                                      const char *userdata_userid,
1185                                      uint8_t **data_r, int *datalen_r);
1186 _hidden int libxl__userdata_store(libxl__gc *gc, uint32_t domid,
1187                                   const char *userdata_userid,
1188                                   const uint8_t *data, int datalen);
1189 
1190 _hidden int libxl__domain_resume(libxl__gc *gc, uint32_t domid,
1191                                  int suspend_cancel);
1192 
1193 /* returns 0 or 1, or a libxl error code */
1194 _hidden int libxl__domain_pvcontrol_available(libxl__gc *gc, uint32_t domid);
1195 
1196 _hidden const char *libxl__domain_pvcontrol_xspath(libxl__gc*, uint32_t domid);
1197 _hidden char * libxl__domain_pvcontrol_read(libxl__gc *gc,
1198                                             xs_transaction_t t, uint32_t domid);
1199 _hidden int libxl__domain_pvcontrol_write(libxl__gc *gc, xs_transaction_t t,
1200                                           uint32_t domid, const char *cmd);
1201 
1202 /* from xl_device */
1203 _hidden char *libxl__device_disk_string_of_backend(libxl_disk_backend backend);
1204 _hidden char *libxl__device_disk_string_of_format(libxl_disk_format format);
1205 _hidden int libxl__device_disk_set_backend(libxl__gc*, libxl_device_disk*);
1206 
1207 _hidden int libxl__device_physdisk_major_minor(const char *physpath, int *major, int *minor);
1208 _hidden int libxl__device_disk_dev_number(const char *virtpath,
1209                                           int *pdisk, int *ppartition);
1210 _hidden char *libxl__devid_to_vdev(libxl__gc *gc, int devid);
1211 
1212 _hidden int libxl__device_console_add(libxl__gc *gc, uint32_t domid,
1213                                       libxl__device_console *console,
1214                                       libxl__domain_build_state *state,
1215                                       libxl__device *device);
1216 _hidden int libxl__device_vuart_add(libxl__gc *gc, uint32_t domid,
1217                                     libxl__device_console *console,
1218                                     libxl__domain_build_state *state);
1219 
1220 /* Returns 1 if device exists, 0 if not, ERROR_* (<0) on error. */
1221 _hidden int libxl__device_exists(libxl__gc *gc, xs_transaction_t t,
1222                                  libxl__device *device);
1223 _hidden int libxl__device_generic_add(libxl__gc *gc, xs_transaction_t t,
1224         libxl__device *device, char **bents, char **fents, char **ro_fents);
1225 _hidden char *libxl__device_backend_path(libxl__gc *gc, libxl__device *device);
1226 _hidden char *libxl__device_libxl_path(libxl__gc *gc, libxl__device *device);
1227 _hidden int libxl__parse_backend_path(libxl__gc *gc, const char *path,
1228                                       libxl__device *dev);
1229 _hidden int libxl__device_destroy(libxl__gc *gc, libxl__device *dev);
1230 _hidden int libxl__wait_for_backend(libxl__gc *gc, const char *be_path,
1231                                     const char *state);
1232 _hidden int libxl__nic_type(libxl__gc *gc, libxl__device *dev,
1233                             libxl_nic_type *nictype);
1234 _hidden int libxl__init_console_from_channel(libxl__gc *gc,
1235                                              libxl__device_console *console,
1236                                              int dev_num,
1237                                              libxl_device_channel *channel);
1238 _hidden int libxl__device_nextid(libxl__gc *gc, uint32_t domid, char *device);
1239 _hidden int libxl__resolve_domid(libxl__gc *gc, const char *name,
1240                                  uint32_t *domid);
1241 
1242 /*
1243  * For each aggregate type which can be used as an input we provide:
1244  *
1245  * int libxl__<type>_setdefault(gc, <type> *p):
1246  *
1247  *     Idempotently sets any members of "p" which is currently set to
1248  *     a special value indicating that the defaults should be used
1249  *     (per libxl_<type>_init) to a specific value.
1250  *
1251  *     All libxl API functions are expected to have arranged for this
1252  *     to be called before using any values within these structures.
1253  */
1254 _hidden int libxl__domain_create_info_setdefault(libxl__gc *gc,
1255                                         libxl_domain_create_info *c_info);
1256 _hidden int libxl__domain_build_info_setdefault(libxl__gc *gc,
1257                                         libxl_domain_build_info *b_info);
1258 _hidden void libxl__rdm_setdefault(libxl__gc *gc,
1259                                    libxl_domain_build_info *b_info);
1260 
1261 _hidden const char *libxl__device_nic_devname(libxl__gc *gc,
1262                                               uint32_t domid,
1263                                               uint32_t devid,
1264                                               libxl_nic_type type);
1265 
1266 _hidden int libxl__get_domid(libxl__gc *gc, uint32_t *domid);
1267 
1268 /*----- xswait: wait for a xenstore node to be suitable -----*/
1269 
1270 typedef struct libxl__xswait_state libxl__xswait_state;
1271 
1272 /*
1273  * rc describes the circumstances of this callback:
1274  *
1275  * rc==0
1276  *
1277  *     The xenstore path (may have) changed.  It has been read for
1278  *     you.  The result is in data (allocated from the ao gc).
1279  *     data may be NULL, which means that the xenstore read gave
1280  *     ENOENT.
1281  *
1282  *     If you are satisfied, you MUST call libxl__xswait_stop.
1283  *     Otherwise, xswait will continue waiting and watching and
1284  *     will call you back later.
1285  *
1286  * rc==ERROR_TIMEDOUT, rc==ERROR_ABORTED
1287  *
1288  *     The specified timeout was reached.
1289  *     This has NOT been logged (except to the debug log).
1290  *     xswait will not continue (but calling libxl__xswait_stop is OK).
1291  *
1292  * rc!=0, !=ERROR_TIMEDOUT, !=ERROR_ABORTED
1293  *
1294  *     Some other error occurred.
1295  *     This HAS been logged.
1296  *     xswait will not continue (but calling libxl__xswait_stop is OK).
1297  *
1298  * xswait.path may start with with '@', in which case no read is done
1299  * and the callback will always get data==0.
1300  */
1301 typedef void libxl__xswait_callback(libxl__egc *egc,
1302       libxl__xswait_state *xswa, int rc, const char *data);
1303 
1304 struct libxl__xswait_state {
1305     /* caller must fill these in, and they must all remain valid */
1306     libxl__ao *ao;
1307     const char *what; /* for error msgs: noun phrase, what we're waiting for */
1308     const char *path;
1309     int timeout_ms; /* as for poll(2) */
1310     libxl__xswait_callback *callback;
1311     /* remaining fields are private to xswait */
1312     libxl__ev_time time_ev;
1313     libxl__ev_xswatch watch_ev;
1314 };
1315 
1316 void libxl__xswait_init(libxl__xswait_state*);
1317 void libxl__xswait_stop(libxl__gc*, libxl__xswait_state*); /*idempotent*/
1318 bool libxl__xswait_inuse(const libxl__xswait_state *ss);
1319 
1320 int libxl__xswait_start(libxl__gc*, libxl__xswait_state*);
1321 
1322 /*
1323  * libxl__ev_devstate - waits a given time for a device to
1324  * reach a given state.  Follows the libxl_ev_* conventions.
1325  * Will generate only one event, and after that is automatically
1326  * cancelled.
1327  */
1328 typedef struct libxl__ev_devstate libxl__ev_devstate;
1329 typedef void libxl__ev_devstate_callback(libxl__egc *egc, libxl__ev_devstate*,
1330                                          int rc);
1331   /* rc will be 0, ERROR_TIMEDOUT, ERROR_ABORTED, ERROR_INVAL
1332    * (meaning path was removed), or ERROR_FAIL if other stuff went
1333    * wrong (in which latter case, logged) */
1334 
1335 struct libxl__ev_devstate {
1336     /* read-only for caller, who may read only when waiting: */
1337     int wanted;
1338     libxl__ev_devstate_callback *callback;
1339     /* as for the remainder, read-only public parts may also be
1340      * read by the caller (notably, watch.path), but only when waiting: */
1341     libxl__xswait_state w;
1342 };
1343 
libxl__ev_devstate_init(libxl__ev_devstate * ds)1344 static inline void libxl__ev_devstate_init(libxl__ev_devstate *ds)
1345 {
1346     libxl__xswait_init(&ds->w);
1347 }
1348 
libxl__ev_devstate_cancel(libxl__gc * gc,libxl__ev_devstate * ds)1349 static inline void libxl__ev_devstate_cancel(libxl__gc *gc,
1350                                              libxl__ev_devstate *ds)
1351 {
1352     libxl__xswait_stop(gc,&ds->w);
1353 }
1354 
1355 _hidden int libxl__ev_devstate_wait(libxl__ao *ao, libxl__ev_devstate *ds,
1356                                     libxl__ev_devstate_callback cb,
1357                                     const char *state_path,
1358                                     int state, int milliseconds);
1359 
1360 /*
1361  * libxl__ev_domaindeathcheck_register - arranges to call back (once)
1362  * if the domain is destroyed.  If the domain dies, we log a message
1363  * of the form "<what>: <explanation of the situation, including the domid>".
1364  */
1365 
1366 typedef struct libxl__domaindeathcheck libxl__domaindeathcheck;
1367 typedef void libxl___domaindeathcheck_callback(libxl__egc *egc,
1368         libxl__domaindeathcheck*,
1369         int rc /* DESTROYED or ABORTED */);
1370 
1371 struct libxl__domaindeathcheck {
1372     /* must be filled in by caller, and remain valid: */
1373     const char *what;
1374     uint32_t domid;
1375     libxl___domaindeathcheck_callback *callback;
1376     /* private */
1377     libxl__ao_abortable abrt;
1378     libxl__ev_xswatch watch;
1379 };
1380 
1381 _hidden int libxl__domaindeathcheck_start(libxl__ao *ao,
1382                                           libxl__domaindeathcheck *dc);
1383 
1384 void libxl__domaindeathcheck_init(libxl__domaindeathcheck *dc);
1385 void libxl__domaindeathcheck_stop(libxl__gc *gc, libxl__domaindeathcheck *dc);
1386 
1387 
1388 /*
1389  * libxl__try_phy_backend - Check if there's support for the passed
1390  * type of file using the PHY backend
1391  * st_mode: mode_t of the file, as returned by stat function
1392  *
1393  * Returns 1 on success, and 0 if not suitable for phy backend.
1394  */
1395 _hidden int libxl__try_phy_backend(mode_t st_mode);
1396 
1397 
1398 _hidden char *libxl__devid_to_localdev(libxl__gc *gc, int devid);
1399 
1400 _hidden int libxl__pci_numdevs(libxl__gc *gc);
1401 _hidden int libxl__pci_topology_init(libxl__gc *gc,
1402                                      physdev_pci_device_t *devs,
1403                                      int num_devs);
1404 
1405 /* from libxl_pci */
1406 
1407 _hidden int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev, int starting);
1408 _hidden int libxl__create_pci_backend(libxl__gc *gc, uint32_t domid,
1409                                       libxl_device_pci *pcidev, int num);
1410 _hidden int libxl__device_pci_destroy_all(libxl__gc *gc, uint32_t domid);
1411 _hidden bool libxl__is_igd_vga_passthru(libxl__gc *gc,
1412                                         const libxl_domain_config *d_config);
1413 
1414 /* from libxl_dtdev */
1415 
1416 _hidden int libxl__device_dt_add(libxl__gc *gc, uint32_t domid,
1417                                  const libxl_device_dtdev *dtdev);
1418 
1419 /*
1420  *----- spawn -----
1421  *
1422  * Higher-level double-fork and separate detach eg as for device models
1423  *
1424  * Each libxl__spawn_state is in one of these states
1425  *    Undefined, Idle, Attached, Detaching
1426  */
1427 
1428 typedef struct libxl__obsolete_spawn_starting libxl__spawn_starting;
1429 /* this type is never defined, so no objects of this type exist
1430  * fixme-ao  This should go away completely.  */
1431 
1432 typedef struct libxl__spawn_state libxl__spawn_state;
1433 
1434 /* Clears out a spawn state; idempotent. */
1435 _hidden void libxl__spawn_init(libxl__spawn_state*);
1436 
1437 /*
1438  * libxl__spawn_spawn - Create a new process which will become daemonic
1439  * Forks twice, to allow the child to detach entirely from the parent.
1440  *
1441  * We call the two generated processes the "middle child" (result of
1442  * the first fork) and the "inner child" (result of the second fork
1443  * which takes place in the middle child).
1444  *
1445  * The inner child must soon exit or exec.  It must also soon exit or
1446  * notify the parent of its successful startup by writing to the
1447  * xenstore path xspath.
1448  *
1449  * The user (in the parent) will be called back (confirm_cb) every
1450  * time that xenstore path is modified.
1451  *
1452  * In both children, the ctx is not fully usable: gc and logging
1453  * operations are OK, but operations on Xen and xenstore are not.
1454  * (The restrictions are the same as those which apply to children
1455  * made with libxl__ev_child_fork.)
1456  *
1457  * midproc_cb will be called in the middle child, with the pid of the
1458  * inner child; this could for example record the pid.  midproc_cb
1459  * should be fast, and should return.  It will be called (reentrantly)
1460  * within libxl__spawn_init.
1461  *
1462  * failure_cb will be called in the parent on failure of the
1463  * intermediate or final child; an error message will have been
1464  * logged.
1465  *
1466  * confirm_cb, failure_cb and detached_cb will not be called
1467  * reentrantly from within libxl__spawn_spawn.
1468  *
1469  * what: string describing the spawned process, used for logging
1470  *
1471  * Logs errors.  A copy of "what" is taken.
1472  * Return values:
1473  *  < 0   error, *spawn is now Idle and need not be detached
1474  *   +1   caller is the parent, *spawn is Attached and must be detached
1475  *    0   caller is now the inner child, should probably call libxl__exec
1476  *
1477  * The spawn state must be Undefined or Idle on entry.
1478  */
1479 _hidden int libxl__spawn_spawn(libxl__egc *egc, libxl__spawn_state *spawn);
1480 
1481 /*
1482  * libxl__spawn_request_detach - Detaches the daemonic child.
1483  *
1484  * Works by killing the intermediate process from spawn_spawn.
1485  * After this function returns, failures of either child are no
1486  * longer reported via failure_cb.
1487  *
1488  * This is not synchronous: there will be a further callback when
1489  * the detach is complete.
1490  *
1491  * If called before the inner child has been created, this may prevent
1492  * it from running at all.  Thus this should be called only when the
1493  * inner child has notified that it is ready.  Normally it will be
1494  * called from within confirm_cb.
1495  *
1496  * Logs errors.
1497  *
1498  * The spawn state must be Attached entry and will be Detaching
1499  * on return.
1500  */
1501 _hidden void libxl__spawn_initiate_detach(libxl__gc *gc, libxl__spawn_state*);
1502 
1503 /*
1504  * If successful, this should return 0.
1505  *
1506  * Otherwise it should return a signal number, which will be
1507  * sent to the inner child; the overall spawn will then fail.
1508  */
1509 typedef int /* signal number */
1510 libxl__spawn_midproc_cb(libxl__gc*, libxl__spawn_state*, pid_t inner);
1511 
1512 /*
1513  * Called if the spawn failed.  The reason will have been logged.
1514  * The spawn state will be Idle on entry to the callback (and
1515  * it may be reused immediately if desired).
1516  */
1517 typedef void libxl__spawn_failure_cb(libxl__egc*, libxl__spawn_state*,
1518                                      int rc);
1519 
1520 /*
1521  * Called when the xspath watch triggers.  xspath will have been read
1522  * and the result placed in xsdata; if that failed because the key
1523  * didn't exist, xspath==0.  (If it failed for some other reason,
1524  * the spawn machinery calls failure_cb instead.)
1525  *
1526  * If the child has indicated its successful startup, or a failure
1527  * has occurred, this should call libxl__spawn_detach.
1528  *
1529  * If the child is still starting up, should simply return, doing
1530  * nothing.
1531  *
1532  * The spawn state will be Active on entry to the callback; there
1533  * are no restrictions on the state on return; it may even have
1534  * been detached and reused.
1535  */
1536 typedef void libxl__spawn_confirm_cb(libxl__egc*, libxl__spawn_state*,
1537                                      const char *xsdata);
1538 
1539 /*
1540  * Called when the detach (requested by libxl__spawn_initiate_detach) has
1541  * completed.  On entry to the callback the spawn state is Idle.
1542  */
1543 typedef void libxl__spawn_detached_cb(libxl__egc*, libxl__spawn_state*);
1544 
1545 struct libxl__spawn_state {
1546     /* must be filled in by user and remain valid */
1547     libxl__ao *ao;
1548     const char *what;
1549     const char *xspath;
1550     const char *pidpath; /* only used by libxl__spawn_midproc_record_pid */
1551     int timeout_ms; /* -1 means forever */
1552     libxl__spawn_midproc_cb *midproc_cb;
1553     libxl__spawn_failure_cb *failure_cb;
1554     libxl__spawn_confirm_cb *confirm_cb;
1555     libxl__spawn_detached_cb *detached_cb;
1556 
1557     /* remaining fields are private to libxl_spawn_... */
1558     int detaching; /* we are in Detaching */
1559     int rc; /* might be non-0 whenever we are not Idle */
1560     libxl__ev_child mid; /* always in use whenever we are not Idle */
1561     libxl__xswait_state xswait;
1562 };
1563 
libxl__spawn_inuse(const libxl__spawn_state * ss)1564 static inline int libxl__spawn_inuse(const libxl__spawn_state *ss)
1565     { return libxl__ev_child_inuse(&ss->mid); }
1566 
1567 /*
1568  * libxl_spawner_record_pid - Record given pid in xenstore
1569  *
1570  * This function can be passed directly as an intermediate_hook to
1571  * libxl__spawn_spawn.  On failure, returns the value SIGTERM.
1572  */
1573 _hidden int libxl__spawn_record_pid(libxl__gc*, libxl__spawn_state*,
1574                                     pid_t innerchild);
1575 
1576 /*
1577  * libxl__xenstore_child_wait_deprecated - Wait for daemonic child IPC
1578  *
1579  * This is a NOT function for waiting for ordinary child processes.
1580  * If you want to run (fork/exec/wait) subprocesses from libxl:
1581  *  - Make your libxl entrypoint use the ao machinery
1582  *  - Use libxl__ev_child_fork, and use the callback programming style
1583  *
1584  * This function is intended for interprocess communication with a
1585  * service process.  If the service process does not respond quickly,
1586  * the whole caller may be blocked.  Therefore this function is
1587  * deprecated.  This function is currently used only by
1588  * libxl__wait_for_device_model_deprecated.
1589  *
1590  * gc: allocation pool
1591  * domid: guest to work with
1592  * timeout: how many seconds to wait for the state to appear
1593  * what: string describing the spawned process
1594  * path: path to the state file in xenstore
1595  * state: expected string to wait for in path (optional)
1596  * spawning: malloc'd pointer to libxl__spawn_starting (optional)
1597  * check_callback: (optional)
1598  * check_callback_userdata: data to pass to the callback function
1599  *
1600  * Returns 0 on success, and < 0 on error.
1601  *
1602  * This function waits the given timeout for the given path to appear
1603  * in xenstore, and optionally for state in path.
1604  * If path appears and state matches, check_callback is called.
1605  * If check_callback returns > 0, waiting for path or state continues.
1606  * Otherwise libxl__xenstore_child_wait_deprecated returns.
1607  */
1608 _hidden int libxl__xenstore_child_wait_deprecated(libxl__gc *gc,
1609                                  uint32_t domid,
1610                                  uint32_t timeout, char *what,
1611                                  char *path, char *state,
1612                                  libxl__spawn_starting *spawning,
1613                                  int (*check_callback)(libxl__gc *gc,
1614                                                        uint32_t domid,
1615                                                        const char *state,
1616                                                        void *userdata),
1617                                  void *check_callback_userdata);
1618 
1619 
1620  /* low-level stuff, for synchronous subprocesses etc. */
1621 
1622 /*
1623  * env should be passed using the following format,
1624  *
1625  * env[0]: name of env variable
1626  * env[1]: value of env variable
1627  * env[n]: ...
1628  *
1629  * So it efectively becomes something like:
1630  * export env[n]=env[n+1]
1631  * (where n%2 = 0)
1632  *
1633  * The last entry of the array always has to be NULL.
1634  *
1635  * stdinfd, stdoutfd, stderrfd will be dup2'd onto the corresponding
1636  * fd in the child, if they are not -1.  The original copy of the
1637  * descriptor will be closed in the child (unless it's 0, 1 or 2
1638  * ie the source descriptor is itself stdin, stdout or stderr).
1639  *
1640  * Logs errors, never returns.
1641  */
1642 _hidden  void libxl__exec(libxl__gc *gc, int stdinfd, int stdoutfd,
1643                           int stderrfd, const char *arg0, char *const args[],
1644                           char *const env[]) __attribute__((noreturn));
1645 
1646 /* from xl_create */
1647 
1648  /* on entry, libxl_domid_valid_guest(domid) must be false;
1649   * on exit (even error exit), domid may be valid and refer to a domain */
1650 _hidden int libxl__domain_make(libxl__gc *gc,
1651                                libxl_domain_config *d_config,
1652                                uint32_t *domid,
1653                                xc_domain_configuration_t *xc_config);
1654 
1655 _hidden int libxl__domain_build(libxl__gc *gc,
1656                                 libxl_domain_config *d_config,
1657                                 uint32_t domid,
1658                                 libxl__domain_build_state *state);
1659 
1660 /* for device model creation */
1661 _hidden const char *libxl__domain_device_model(libxl__gc *gc,
1662                                         const libxl_domain_build_info *info);
1663 _hidden int libxl__need_xenpv_qemu(libxl__gc *gc,
1664                                    libxl_domain_config *d_config);
1665 _hidden bool libxl__query_qemu_backend(libxl__gc *gc,
1666                                        uint32_t domid,
1667                                        uint32_t backend_id,
1668                                        const char *type,
1669                                        bool def);
1670 _hidden int libxl__dm_active(libxl__gc *gc, uint32_t domid);
1671 _hidden int libxl__dm_check_start(libxl__gc *gc,
1672                                   libxl_domain_config *d_config,
1673                                   uint32_t domid);
1674 
1675 /*
1676  * This function will fix reserved device memory conflict
1677  * according to user's configuration.
1678  */
1679 _hidden int libxl__domain_device_construct_rdm(libxl__gc *gc,
1680                                    libxl_domain_config *d_config,
1681                                    uint64_t rdm_mem_guard,
1682                                    struct xc_dom_image *dom);
1683 
1684 /*
1685  * This function will cause the whole libxl process to hang
1686  * if the device model does not respond.  It is deprecated.
1687  *
1688  * Instead of calling this function:
1689  *  - Make your libxl entrypoint use the ao machinery
1690  *  - Use libxl__ev_xswatch_register, and use the callback programming
1691  *    style
1692  */
1693 _hidden int libxl__wait_for_device_model_deprecated(libxl__gc *gc,
1694                                 uint32_t domid, char *state,
1695                                 libxl__spawn_starting *spawning
1696                                                     /* NULL allowed */,
1697                                 int (*check_callback)(libxl__gc *gc,
1698                                                       uint32_t domid,
1699                                                       const char *state,
1700                                                       void *userdata),
1701                                 void *check_callback_userdata);
1702 
1703 _hidden int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid);
1704 
1705 _hidden const libxl_vnc_info *libxl__dm_vnc(const libxl_domain_config *g_cfg);
1706 
1707 _hidden char *libxl__abs_path(libxl__gc *gc, const char *s, const char *path);
1708 
1709 #define LIBXL__LOG_DEBUG   XTL_DEBUG
1710 #define LIBXL__LOG_VERBOSE XTL_VERBOSE
1711 #define LIBXL__LOG_INFO    XTL_INFO
1712 #define LIBXL__LOG_WARNING XTL_WARN
1713 #define LIBXL__LOG_ERROR   XTL_ERROR
1714 
1715 _hidden char *libxl__domid_to_name(libxl__gc *gc, uint32_t domid);
1716 _hidden char *libxl__cpupoolid_to_name(libxl__gc *gc, uint32_t poolid);
1717 
1718 _hidden int libxl__enum_from_string(const libxl_enum_string_table *t,
1719                                     const char *s, int *e) NN(2);
1720 
1721 _hidden yajl_gen_status libxl__yajl_gen_asciiz(yajl_gen hand, const char *str);
1722 
1723 _hidden yajl_gen_status libxl__string_gen_json(yajl_gen hand, const char *p);
1724 
1725 typedef yajl_gen_status (*libxl__gen_json_callback)(yajl_gen hand, void *);
1726 _hidden char *libxl__object_to_json(libxl_ctx *ctx, const char *type,
1727                                     libxl__gen_json_callback gen, void *p);
1728 
1729   /* holds the CPUID response for a single CPUID leaf
1730    * input contains the value of the EAX and ECX register,
1731    * and each policy string contains a filter to apply to
1732    * the host given values for that particular leaf.
1733    */
1734 struct libxl__cpuid_policy {
1735     uint32_t input[2];
1736     char *policy[4];
1737 };
1738 
1739 /*
1740  * blktap2 support
1741  */
1742 
1743 /* libxl__blktap_enabled:
1744  *    return true if blktap/blktap2 support is available.
1745  */
1746 _hidden int libxl__blktap_enabled(libxl__gc *gc);
1747 
1748 /* libxl__blktap_devpath:
1749  *    Argument: path and disk image as specified in config file.
1750  *      The type specifies whether this is aio, qcow, qcow2, etc.
1751  *    returns device path xenstore wants to have. returns NULL
1752  *      if no device corresponds to the disk.
1753  */
1754 _hidden char *libxl__blktap_devpath(libxl__gc *gc,
1755                                     const char *disk,
1756                                     libxl_disk_format format);
1757 
1758 /* libxl__device_destroy_tapdisk:
1759  *   Destroys any tapdisk process associated with the backend represented
1760  *   by be_path.
1761  *   Always logs on failure.
1762  */
1763 _hidden int libxl__device_destroy_tapdisk(libxl__gc *gc, const char *params);
1764 
1765 /* Calls poll() again - useful to check whether a signaled condition
1766  * is still true.  Cannot fail.  Returns currently-true revents. */
1767 _hidden short libxl__fd_poll_recheck(libxl__egc *egc, int fd, short events);
1768 
1769 _hidden char *libxl__uuid2string(libxl__gc *gc, const libxl_uuid uuid);
1770 
1771 struct libxl__xen_console_reader {
1772     char *buffer;
1773     unsigned int size;
1774     unsigned int count;
1775     unsigned int clear;
1776     unsigned int incremental;
1777     unsigned int index;
1778 };
1779 
1780 /* parse the string @s as a sequence of 6 colon separated bytes in to @mac */
1781 _hidden int libxl__parse_mac(const char *s, libxl_mac mac);
1782 /* compare mac address @a and @b. 0 if the same, -ve if a<b and +ve if a>b */
1783 _hidden int libxl__compare_macs(libxl_mac *a, libxl_mac *b);
1784 /* return true if mac address is all zero (the default value) */
1785 _hidden int libxl__mac_is_default(libxl_mac *mac);
1786 /* init a recursive mutex */
1787 _hidden int libxl__init_recursive_mutex(libxl_ctx *ctx, pthread_mutex_t *lock);
1788 
1789 _hidden int libxl__gettimeofday(libxl__gc *gc, struct timeval *now_r);
1790 
1791 #define STRINGIFY(x) #x
1792 #define TOSTRING(x) STRINGIFY(x)
1793 
1794 /* from libxl_qmp */
1795 typedef struct libxl__qmp_handler libxl__qmp_handler;
1796 
1797 /* Initialise and connect to the QMP socket.
1798  *   Return an handler or NULL if there is an error
1799  */
1800 _hidden libxl__qmp_handler *libxl__qmp_initialize(libxl__gc *gc,
1801                                                   uint32_t domid);
1802 _hidden int libxl__qmp_run_command_flexarray(libxl__gc *gc, int domid,
1803                                              const char *cmd,
1804                                              flexarray_t *array);
1805 /* ask to QEMU the serial port information and store it in xenstore. */
1806 _hidden int libxl__qmp_query_serial(libxl__qmp_handler *qmp);
1807 _hidden int libxl__qmp_pci_add(libxl__gc *gc, int d, libxl_device_pci *pcidev);
1808 _hidden int libxl__qmp_pci_del(libxl__gc *gc, int domid,
1809                                libxl_device_pci *pcidev);
1810 /* Resume hvm domain */
1811 _hidden int libxl__qmp_system_wakeup(libxl__gc *gc, int domid);
1812 /* Suspend QEMU. */
1813 _hidden int libxl__qmp_stop(libxl__gc *gc, int domid);
1814 /* Resume QEMU. */
1815 _hidden int libxl__qmp_resume(libxl__gc *gc, int domid);
1816 /* Save current QEMU state into fd. */
1817 _hidden int libxl__qmp_save(libxl__gc *gc, int domid, const char *filename);
1818 /* Load current QEMU state from file. */
1819 _hidden int libxl__qmp_restore(libxl__gc *gc, int domid, const char *filename);
1820 /* Set dirty bitmap logging status */
1821 _hidden int libxl__qmp_set_global_dirty_log(libxl__gc *gc, int domid, bool enable);
1822 _hidden int libxl__qmp_insert_cdrom(libxl__gc *gc, int domid, const libxl_device_disk *disk);
1823 /* Add a virtual CPU */
1824 _hidden int libxl__qmp_cpu_add(libxl__gc *gc, int domid, int index);
1825 /* Query the bitmap of CPUs */
1826 _hidden int libxl__qmp_query_cpus(libxl__gc *gc, int domid,
1827                                   libxl_bitmap *map);
1828 /* Start NBD server */
1829 _hidden int libxl__qmp_nbd_server_start(libxl__gc *gc, int domid,
1830                                         const char *host, const char *port);
1831 /* Add a disk to NBD server */
1832 _hidden int libxl__qmp_nbd_server_add(libxl__gc *gc, int domid,
1833                                       const char *disk);
1834 /* Start replication */
1835 _hidden int libxl__qmp_start_replication(libxl__gc *gc, int domid,
1836                                          bool primary);
1837 /* Get replication error that occurs when the vm is running */
1838 _hidden int libxl__qmp_query_xen_replication_status(libxl__gc *gc, int domid);
1839 /* Do checkpoint */
1840 _hidden int libxl__qmp_colo_do_checkpoint(libxl__gc *gc, int domid);
1841 /* Stop replication */
1842 _hidden int libxl__qmp_stop_replication(libxl__gc *gc, int domid,
1843                                         bool primary);
1844 /* Stop NBD server */
1845 _hidden int libxl__qmp_nbd_server_stop(libxl__gc *gc, int domid);
1846 /* Add or remove a child to/from quorum */
1847 _hidden int libxl__qmp_x_blockdev_change(libxl__gc *gc, int domid,
1848                                          const char *parant,
1849                                          const char *child, const char *node);
1850 /* run a hmp command in qmp mode */
1851 _hidden int libxl__qmp_hmp(libxl__gc *gc, int domid, const char *command_line,
1852                            char **out);
1853 /* close and free the QMP handler */
1854 _hidden void libxl__qmp_close(libxl__qmp_handler *qmp);
1855 /* remove the socket file, if the file has already been removed,
1856  * nothing happen */
1857 _hidden void libxl__qmp_cleanup(libxl__gc *gc, uint32_t domid);
1858 
1859 /* this helper calls qmp_initialize, query_serial and qmp_close */
1860 _hidden int libxl__qmp_initializations(libxl__gc *gc, uint32_t domid,
1861                                        const libxl_domain_config *guest_config);
1862 
1863 /* on failure, logs */
1864 int libxl__sendmsg_fds(libxl__gc *gc, int carrier,
1865                        const void *data, size_t datalen,
1866                        int nfds, const int fds[], const char *what);
1867 
1868 /* Insists on receiving exactly nfds and datalen.  On failure, logs
1869  * and leaves *fds untouched. */
1870 int libxl__recvmsg_fds(libxl__gc *gc, int carrier,
1871                        void *databuf, size_t datalen,
1872                        int nfds, int fds[], const char *what);
1873 
1874 /* from libxl_json */
1875 #include <yajl/yajl_gen.h>
1876 
1877 _hidden yajl_gen_status libxl__yajl_gen_asciiz(yajl_gen hand, const char *str);
1878 _hidden yajl_gen_status libxl__yajl_gen_enum(yajl_gen hand, const char *str);
1879 
1880 typedef enum {
1881     JSON_NULL    = (1 << 0),
1882     JSON_BOOL    = (1 << 1),
1883     JSON_INTEGER = (1 << 2),
1884     JSON_DOUBLE  = (1 << 3),
1885     /* number is store in string, it's too big to be a long long or a double */
1886     JSON_NUMBER  = (1 << 4),
1887     JSON_STRING  = (1 << 5),
1888     JSON_MAP     = (1 << 6),
1889     JSON_ARRAY   = (1 << 7),
1890     JSON_ANY     = 255 /* this is a mask of all values above, adjust as needed */
1891 } libxl__json_node_type;
1892 
1893 typedef struct libxl__json_object {
1894     libxl__json_node_type type;
1895     union {
1896         bool b;
1897         long long i;
1898         double d;
1899         char *string;
1900         /* List of libxl__json_object */
1901         flexarray_t *array;
1902         /* List of libxl__json_map_node */
1903         flexarray_t *map;
1904     } u;
1905     struct libxl__json_object *parent;
1906 } libxl__json_object;
1907 
1908 typedef int (*libxl__json_parse_callback)(libxl__gc *gc,
1909                                           libxl__json_object *o,
1910                                           void *p);
1911 _hidden int libxl__object_from_json(libxl_ctx *ctx, const char *type,
1912                                     libxl__json_parse_callback parse,
1913                                     void *p,
1914                                     const char *s);
1915 
1916 typedef struct {
1917     char *map_key;
1918     libxl__json_object *obj;
1919 } libxl__json_map_node;
1920 
1921 typedef struct libxl__yajl_ctx libxl__yajl_ctx;
1922 
libxl__json_object_is_null(const libxl__json_object * o)1923 static inline bool libxl__json_object_is_null(const libxl__json_object *o)
1924 {
1925     return o != NULL && o->type == JSON_NULL;
1926 }
libxl__json_object_is_bool(const libxl__json_object * o)1927 static inline bool libxl__json_object_is_bool(const libxl__json_object *o)
1928 {
1929     return o != NULL && o->type == JSON_BOOL;
1930 }
libxl__json_object_is_string(const libxl__json_object * o)1931 static inline bool libxl__json_object_is_string(const libxl__json_object *o)
1932 {
1933     return o != NULL && o->type == JSON_STRING;
1934 }
libxl__json_object_is_integer(const libxl__json_object * o)1935 static inline bool libxl__json_object_is_integer(const libxl__json_object *o)
1936 {
1937     return o != NULL && o->type == JSON_INTEGER;
1938 }
libxl__json_object_is_double(const libxl__json_object * o)1939 static inline bool libxl__json_object_is_double(const libxl__json_object *o)
1940 {
1941     return o != NULL && o->type == JSON_DOUBLE;
1942 }
libxl__json_object_is_number(const libxl__json_object * o)1943 static inline bool libxl__json_object_is_number(const libxl__json_object *o)
1944 {
1945     return o != NULL && o->type == JSON_NUMBER;
1946 }
libxl__json_object_is_map(const libxl__json_object * o)1947 static inline bool libxl__json_object_is_map(const libxl__json_object *o)
1948 {
1949     return o != NULL && o->type == JSON_MAP;
1950 }
libxl__json_object_is_array(const libxl__json_object * o)1951 static inline bool libxl__json_object_is_array(const libxl__json_object *o)
1952 {
1953     return o != NULL && o->type == JSON_ARRAY;
1954 }
1955 
libxl__json_object_get_bool(const libxl__json_object * o)1956 static inline bool libxl__json_object_get_bool(const libxl__json_object *o)
1957 {
1958     if (libxl__json_object_is_bool(o))
1959         return o->u.b;
1960     else
1961         return false;
1962 }
1963 static inline
libxl__json_object_get_string(const libxl__json_object * o)1964 const char *libxl__json_object_get_string(const libxl__json_object *o)
1965 {
1966     if (libxl__json_object_is_string(o))
1967         return o->u.string;
1968     else
1969         return NULL;
1970 }
1971 static inline
libxl__json_object_get_number(const libxl__json_object * o)1972 const char *libxl__json_object_get_number(const libxl__json_object *o)
1973 {
1974     if (libxl__json_object_is_number(o))
1975         return o->u.string;
1976     else
1977         return NULL;
1978 }
1979 static inline
libxl__json_object_get_map(const libxl__json_object * o)1980 flexarray_t *libxl__json_object_get_map(const libxl__json_object *o)
1981 {
1982     if (libxl__json_object_is_map(o))
1983         return o->u.map;
1984     else
1985         return NULL;
1986 }
1987 static inline
libxl__json_object_get_array(const libxl__json_object * o)1988 flexarray_t *libxl__json_object_get_array(const libxl__json_object *o)
1989 {
1990     if (libxl__json_object_is_array(o))
1991         return o->u.array;
1992     else
1993         return NULL;
1994 }
libxl__json_object_get_integer(const libxl__json_object * o)1995 static inline long long libxl__json_object_get_integer(const libxl__json_object *o)
1996 {
1997     if (libxl__json_object_is_integer(o))
1998         return o->u.i;
1999     else
2000         return -1;
2001 }
2002 
2003 /*
2004  * NOGC can be used with those json_object functions, but the
2005  * libxl__json_object* will need to be freed with libxl__json_object_free.
2006  */
2007 _hidden libxl__json_object *libxl__json_object_alloc(libxl__gc *gc_opt,
2008                                                      libxl__json_node_type type);
2009 _hidden int libxl__json_object_append_to(libxl__gc *gc_opt,
2010                                          libxl__json_object *obj,
2011                                          libxl__yajl_ctx *ctx);
2012 _hidden libxl__json_object *libxl__json_array_get(const libxl__json_object *o,
2013                                                   int i);
2014 _hidden
2015 libxl__json_map_node *libxl__json_map_node_get(const libxl__json_object *o,
2016                                                int i);
2017 _hidden const libxl__json_object *libxl__json_map_get(const char *key,
2018                                           const libxl__json_object *o,
2019                                           libxl__json_node_type expected_type);
2020 _hidden yajl_status libxl__json_object_to_yajl_gen(libxl__gc *gc_opt,
2021                                                    yajl_gen hand,
2022                                                    libxl__json_object *param);
2023 _hidden void libxl__json_object_free(libxl__gc *gc_opt,
2024                                      libxl__json_object *obj);
2025 
2026 _hidden libxl__json_object *libxl__json_parse(libxl__gc *gc_opt, const char *s);
2027 
2028   /* Based on /local/domain/$domid/dm-version xenstore key
2029    * default is qemu xen traditional */
2030 _hidden int libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
2031   /* Return the system-wide default device model */
2032 _hidden libxl_device_model_version libxl__default_device_model(libxl__gc *gc);
2033 
2034 #define DEVICE_MODEL_XS_PATH(gc, dm_domid, domid, fmt, _a...)              \
2035     libxl__sprintf(gc, "/local/domain/%u/device-model/%u" fmt, dm_domid,   \
2036                    domid, ##_a)
2037 
2038 /*
2039  * Calling context and GC for event-generating functions:
2040  *
2041  * These are for use by parts of libxl which directly or indirectly
2042  * call libxl__event_occurred.  These contain a gc but also a list of
2043  * deferred events.
2044  *
2045  * You should never need to initialise an egc unless you are part of
2046  * the event machinery itself.  Otherwise you will always be given an
2047  * egc if you need one.  Even functions which generate specific kinds
2048  * of events don't need to - rather, they will be passed an egc into
2049  * their own callback function and should just use the one they're
2050  * given.
2051  *
2052  * Functions using LIBXL_INIT_EGC may *not* generally be called from
2053  * within libxl, because libxl__egc_cleanup may call back into the
2054  * application.  This should be enforced by declaring all such
2055  * functions in libxl.h or libxl_event.h with
2056  * LIBXL_EXTERNAL_CALLERS_ONLY.  You should in any case not find it
2057  * necessary to call egc-creators from within libxl.
2058  *
2059  * The callbacks must all take place with the ctx unlocked because
2060  * the application is entitled to reenter libxl from them.  This
2061  * would be bad not because the lock is not recursive (it is) but
2062  * because the application might make blocking libxl calls which
2063  * would hold the lock unreasonably long.
2064  *
2065  * For the same reason libxl__egc_cleanup (or EGC_FREE) must be called
2066  * with the ctx *unlocked*.  So the right pattern has the EGC_...
2067  * macro calls on the outside of the CTX_... ones.
2068  */
2069 
2070 /* useful for all functions which take an egc: */
2071 
2072 #define EGC_GC                                  \
2073     libxl__gc *const gc __attribute__((unused)) = &egc->gc
2074 
2075 /* egc initialisation and destruction: */
2076 
2077 #define LIBXL_INIT_EGC(egc,ctx) do{                     \
2078         LIBXL_INIT_GC((egc).gc,ctx);                    \
2079         LIBXL_TAILQ_INIT(&(egc).occurred_for_callback); \
2080         LIBXL_TAILQ_INIT(&(egc).aos_for_callback);      \
2081         LIBXL_TAILQ_INIT(&(egc).aops_for_callback);     \
2082     } while(0)
2083 
2084 _hidden void libxl__egc_cleanup(libxl__egc *egc);
2085   /* Frees memory allocated within this egc's gc, and and report all
2086    * occurred events via callback, if applicable.  May reenter the
2087    * application; see restrictions above.  The ctx must be UNLOCKED. */
2088 
2089 /* convenience macros: */
2090 
2091 #define EGC_INIT(ctx)                       \
2092     libxl__egc egc[1]; LIBXL_INIT_EGC(egc[0],ctx);      \
2093     EGC_GC
2094 
2095 #define EGC_FREE           libxl__egc_cleanup(egc)
2096 
2097 
2098 /*
2099  * Machinery for asynchronous operations ("ao")
2100  *
2101  * All "slow" functions (see below for the exact definition) need to
2102  * use the asynchronous operation ("ao") machinery.  The function
2103  * should take a parameter const libxl_asyncop_how *ao_how and must
2104  * start with a call to AO_CREATE or equivalent.  These functions MAY
2105  * NOT be called from inside libxl (regardless of what is passed for
2106  * ao_how), because they can cause reentrancy hazards due to
2107  * callbacks.
2108  *
2109  * For the same reason functions taking an ao_how may make themselves
2110  * an egc with EGC_INIT (and they will generally want to, to be able
2111  * to immediately complete an ao during its setup).
2112  *
2113  *
2114  * "Slow" functions includes any that might block on a guest or an
2115  * external script.  More broadly, it includes any operations which
2116  * are sufficiently slow that an application might reasonably want to
2117  * initiate them, and then carry on doing something else, while the
2118  * operation completes.  That is, a "fast" function must be fast
2119  * enough that we do not mind blocking all other management operations
2120  * on the same host while it completes.
2121  *
2122  * There are certain primitive functions which make a libxl operation
2123  * necessarily "slow" for API reasons.  These are:
2124  *  - awaiting xenstore watches (although read-modify-write xenstore
2125  *    transactions are OK for fast functions)
2126  *  - spawning subprocesses
2127  *  - anything with a timeout
2128  *
2129  *
2130  * Lifecycle of an ao:
2131  *
2132  * - Created by libxl__ao_create (or the AO_CREATE convenience macro).
2133  *
2134  * - After creation, can be used by code which implements
2135  *   the operation as follows:
2136  *      - the ao's gc, for allocating memory for the lifetime
2137  *        of the operation (possibly with the help of the AO_GC
2138  *        macro to introduce the gc into scope)
2139  *      - the ao itself may be passed about to sub-functions
2140  *        so that they can stash it away etc.
2141  *      - in particular, the ao pointer must be stashed in some
2142  *        per-operation structure which is also passed as a user
2143  *        pointer to the internal event generation request routines
2144  *        libxl__evgen_FOO, so that at some point a CALLBACK will be
2145  *        made when the operation is complete.
2146  *      - if the operation provides progress reports, the aop_how(s)
2147  *        must be copied into the per-operation structure using
2148  *        libxl__ao_progress_gethow.
2149  *
2150  * - If the initiation is unsuccessful, the initiating function must
2151  *   call libxl__ao_create_fail before unlocking and returning whatever
2152  *   error code is appropriate (AO_CREATE_FAIL macro).
2153  *
2154  * If initiation is successful:
2155  *
2156  * - The initiating function must run libxl__ao_inprogress right
2157  *   before unlocking and returning, and return whatever it returns.
2158  *   This is best achieved with the AO_INPROGRESS macro.
2159  *
2160  * - If the operation supports progress reports, it may generate
2161  *   suitable events with NEW_EVENT and report them with
2162  *   libxl__ao_progress_report (with the ctx locked).
2163  *
2164  * - Eventually, some callback function, whose callback has been
2165  *   requested directly or indirectly, should call libxl__ao_complete
2166  *   (with the ctx locked, as it will generally already be in any
2167  *   event callback function).  This must happen exactly once for each
2168  *   ao, as the last that happens with that ao.
2169  *
2170  * - However, it is permissible for the initiating function to call
2171  *   libxl__ao_inprogress and/or libxl__ao_complete (directly or
2172  *   indirectly), before it uses AO_INPROGRESS to return.  (The ao
2173  *   infrastructure will arrange to defer destruction of the ao, etc.,
2174  *   until the proper time.)  An initiating function should do this
2175  *   if it takes a codepath which completes synchronously.
2176  *
2177  * - Conversely it is forbidden to call libxl__ao_complete in the
2178  *   initiating function _after_ AO_INPROGRESS, because
2179  *   libxl__ao_complete requires the ctx to be locked.
2180  *
2181  * - Note that during callback functions, two gcs are available:
2182  *    - The one in egc, whose lifetime is only this callback
2183  *    - The one in ao, whose lifetime is the asynchronous operation
2184  *   Usually a callback function should use CONTAINER_OF to obtain its
2185  *   own state structure, containing a pointer to the ao.  It should
2186  *   then obtain the ao and use the ao's gc; this is most easily done
2187  *   using the convenience macro STATE_AO_GC.
2188  */
2189 
2190 #define AO_CREATE(ctx, domid, ao_how)                           \
2191     libxl__ctx_lock(ctx);                                       \
2192     libxl__ao *ao = libxl__ao_create(ctx, domid, ao_how,        \
2193                                __FILE__, __LINE__, __func__);   \
2194     if (!ao) { libxl__ctx_unlock(ctx); return ERROR_NOMEM; }    \
2195     libxl__egc egc[1]; LIBXL_INIT_EGC(egc[0],ctx);              \
2196     AO_GC;
2197 
2198 #define AO_INPROGRESS ({                                        \
2199         libxl_ctx *ao__ctx = libxl__gc_owner(&ao->gc);          \
2200         CTX_UNLOCK;                                             \
2201         EGC_FREE;                                               \
2202         CTX_LOCK;                                               \
2203         int ao__rc = libxl__ao_inprogress(ao,                   \
2204                                __FILE__, __LINE__, __func__);   \
2205         libxl__ctx_unlock(ao__ctx); /* gc is now invalid */     \
2206         (ao__rc);                                               \
2207    })
2208 
2209 #define AO_CREATE_FAIL(rc) ({                                   \
2210         libxl_ctx *ao__ctx = libxl__gc_owner(&ao->gc);          \
2211         assert(rc);                                             \
2212         libxl__ao_create_fail(ao);                              \
2213         libxl__ctx_unlock(ao__ctx); /* gc is now invalid */     \
2214         EGC_FREE;                                               \
2215         (rc);                                                   \
2216     })
2217 
2218 
2219 /*
2220  * Given, in scope,
2221  *   libxl__ao *ao;
2222  * produces, in scope,
2223  *   libxl__gc *gc;
2224  */
2225 #define AO_GC                                   \
2226     libxl__gc *const gc __attribute__((unused)) = &ao->gc
2227 
2228 /*
2229  * void STATE_AO_GC(libxl__ao *ao_spec);
2230  * // Produces, in scope:
2231  *   libxl__ao *ao;  // set from ao_spec
2232  *   libxl__gc *gc;
2233  */
2234 #define STATE_AO_GC(op_ao)                      \
2235     libxl__ao *const ao = (op_ao);              \
2236     libxl__gc *const gc __attribute__((unused)) = libxl__ao_inprogress_gc(ao)
2237 
2238 
2239 /* All of these MUST be called with the ctx locked.
2240  * libxl__ao_inprogress MUST be called with the ctx locked exactly once. */
2241 _hidden libxl__ao *libxl__ao_create(libxl_ctx*, uint32_t domid,
2242                                     const libxl_asyncop_how*,
2243        const char *file, int line, const char *func);
2244 _hidden int libxl__ao_inprogress(libxl__ao *ao,
2245        const char *file, int line, const char *func); /* temporarily unlocks */
2246 _hidden void libxl__ao_create_fail(libxl__ao *ao);
2247 _hidden void libxl__ao_complete(libxl__egc *egc, libxl__ao *ao, int rc);
2248 _hidden libxl__gc *libxl__ao_inprogress_gc(libxl__ao *ao);
2249 
2250 /* Can be called at any time.  Use is essential for any aop user. */
2251 _hidden void libxl__ao_progress_gethow(libxl_asyncprogress_how *in_state,
2252                                        const libxl_asyncprogress_how *from_app);
2253 
2254 /* Must be called with the ctx locked.  Will fill in ev->for_user,
2255  * so caller need not do that. */
2256 _hidden void libxl__ao_progress_report(libxl__egc *egc, libxl__ao *ao,
2257    const libxl_asyncprogress_how *how, libxl_event *ev /* consumed */);
2258 
2259 /* For use by ao machinery ONLY */
2260 _hidden void libxl__ao__destroy(libxl_ctx*, libxl__ao *ao);
2261 _hidden void libxl__ao_complete_check_progress_reports(libxl__egc*, libxl__ao*);
2262 
2263 
2264 /*
2265  * Short-lived sub-ao, aka "nested ao".
2266  *
2267  * Some asynchronous operations are very long-running.  Generally,
2268  * since an ao has a gc, any allocations made in that ao will live
2269  * until the ao is completed.  When this is not desirable, these
2270  * functions may be used to manage a "sub-ao".
2271  *
2272  * The returned sub-ao is suitable for passing to gc-related functions
2273  * and macros such as libxl__ao_inprogress_gc, AO_GC, and STATE_AO_GC.
2274  *
2275  * It MUST NOT be used with AO_INPROGRESS, AO_CREATE_FAIL,
2276  * libxl__ao_complete, libxl__ao_progress_report, and so on.
2277  *
2278  * The caller must ensure that all of the sub-ao's are freed before
2279  * the parent is.  Multiple levels of nesting are OK (although
2280  * hopefully they won't be necessary).
2281  */
2282 
2283 _hidden libxl__ao *libxl__nested_ao_create(libxl__ao *parent); /* cannot fail */
2284 _hidden void libxl__nested_ao_free(libxl__ao *child);
2285 
2286 
2287 /*
2288  * File descriptors and CLOEXEC
2289  */
2290 
2291 /*
2292  * For libxl functions which create file descriptors, at least one
2293  * of the following must be true:
2294  *  (a) libxl does not care if copies of this open-file are inherited
2295  *      by random children and might remain open indefinitely
2296  *  (b) libxl must take extra care for the fd (the actual descriptor,
2297  *      not the open-file) as below.  We call this a "carefd".
2298  *
2299  * The rules for opening a carefd are:
2300  *  (i)   Before bringing any carefds into existence,
2301  *        libxl code must call libxl__carefd_begin.
2302  *  (ii)  Then for each carefd brought into existence,
2303  *        libxl code must call libxl__carefd_record
2304  *        and remember the libxl__carefd_record*.
2305  *  (iii) Then it must call libxl__carefd_unlock.
2306  *  (iv)  When in a child process the fd is to be passed across
2307  *        exec by libxl, the libxl code must unset FD_CLOEXEC
2308  *        on the fd eg by using libxl_fd_set_cloexec.
2309  *  (v)   Later, when the fd is to be closed in the same process,
2310  *        libxl code must not call close.  Instead, it must call
2311  *        libxl__carefd_close.
2312  * Steps (ii) and (iii) can be combined by calling the convenience
2313  * function libxl__carefd_opened.
2314  */
2315 /* libxl__carefd_begin and _unlock (or _opened) must be called always
2316  * in pairs.  They may be called with the CTX lock held.  In between
2317  * _begin and _unlock, the following are prohibited:
2318  *   - anything which might block
2319  *   - any callbacks to the application
2320  *   - nested calls to libxl__carefd_begin
2321  *   - fork (libxl__fork)
2322  * In general nothing should be done before _unlock that could be done
2323  * afterwards.
2324  */
2325 typedef struct libxl__carefd libxl__carefd;
2326 
2327 _hidden void libxl__carefd_begin(void);
2328 _hidden void libxl__carefd_unlock(void);
2329 
2330 /* fd may be -1, in which case this returns a dummy libxl__fd_record
2331  * on which it _carefd_close is a no-op.  Cannot fail. */
2332 _hidden libxl__carefd *libxl__carefd_record(libxl_ctx *ctx, int fd);
2333 
2334 /* Combines _record and _unlock in a single call.  If fd==-1,
2335  * still does the unlock, but returns 0. */
2336 _hidden libxl__carefd *libxl__carefd_opened(libxl_ctx *ctx, int fd);
2337 
2338 /* Works just like close(2).  You may pass NULL, in which case it's
2339  * a successful no-op. */
2340 _hidden int libxl__carefd_close(libxl__carefd*);
2341 
2342 /* You may pass NULL in which case the answer is -1. */
2343 _hidden int libxl__carefd_fd(const libxl__carefd*);
2344 
2345 /* common paths */
2346 _hidden const char *libxl__private_bindir_path(void);
2347 _hidden const char *libxl__xenfirmwaredir_path(void);
2348 _hidden const char *libxl__xen_config_dir_path(void);
2349 _hidden const char *libxl__xen_script_dir_path(void);
2350 _hidden const char *libxl__lock_dir_path(void);
2351 _hidden const char *libxl__run_dir_path(void);
2352 _hidden const char *libxl__seabios_path(void);
2353 _hidden const char *libxl__ovmf_path(void);
2354 
2355 /*----- subprocess execution with timeout -----*/
2356 
2357 typedef struct libxl__async_exec_state libxl__async_exec_state;
2358 
2359 typedef void libxl__async_exec_callback(libxl__egc *egc,
2360                         libxl__async_exec_state *aes, int rc, int status);
2361 /*
2362  * Meaning of status and rc:
2363  *  rc==0, status==0    all went well
2364  *  rc==0, status!=0    everything OK except child exited nonzero (logged)
2365  *  rc!=0               something else went wrong (status is real
2366  *                       exit status; maybe reflecting SIGKILL, and
2367  *                       therefore not very interesting, if aes code
2368  *                       killed the child).  Logged unless ABORTED.
2369  */
2370 
2371 struct libxl__async_exec_state {
2372     /* caller must fill these in */
2373     libxl__ao *ao;
2374     const char *what; /* for error msgs, what we're executing */
2375     int timeout_ms;
2376     libxl__async_exec_callback *callback;
2377     /* caller must fill in; as for libxl__exec */
2378     int stdfds[3];
2379     char **args; /* execution arguments */
2380     char **env; /* execution environment */
2381 
2382     /* private */
2383     libxl__ev_time time;
2384     libxl__ev_child child;
2385     int rc;
2386 };
2387 
2388 void libxl__async_exec_init(libxl__async_exec_state *aes);
2389 int libxl__async_exec_start(libxl__async_exec_state *aes);
2390 bool libxl__async_exec_inuse(const libxl__async_exec_state *aes);
2391 
2392 _hidden void libxl__kill(libxl__gc *gc, pid_t pid, int sig, const char *what);
2393 
2394 /*----- device addition/removal -----*/
2395 
2396 typedef struct libxl__ao_device libxl__ao_device;
2397 typedef struct libxl__multidev libxl__multidev;
2398 typedef void libxl__device_callback(libxl__egc*, libxl__ao_device*);
2399 
2400 /* This functions sets the necessary libxl__ao_device struct values to use
2401  * safely inside functions. It marks the operation as "active"
2402  * since we need to be sure that all device status structs are set
2403  * to active before start queueing events, or we might call
2404  * ao_complete before all devices had finished
2405  *
2406  * libxl__initiate_device_{remove/addition} should not be called without
2407  * calling libxl__prepare_ao_device first, since it initializes the private
2408  * fields of the struct libxl__ao_device to what this functions expect.
2409  *
2410  * Once _prepare has been called on a libxl__ao_device, it is safe to just
2411  * discard this struct, there's no need to call any destroy function.
2412  * _prepare can also be called multiple times with the same libxl__ao_device.
2413  */
2414 _hidden void libxl__prepare_ao_device(libxl__ao *ao, libxl__ao_device *aodev);
2415 
2416 /* generic callback for devices that only need to set ao_complete */
2417 _hidden void device_addrm_aocomplete(libxl__egc *egc, libxl__ao_device *aodev);
2418 
2419 struct libxl__ao_device {
2420     /* filled in by user */
2421     libxl__ao *ao;
2422     libxl__device_action action;
2423     libxl__device *dev;
2424     int force;
2425     libxl__device_callback *callback;
2426     /* return value, zeroed by user on entry, is valid on callback */
2427     int rc;
2428     /* private for multidev */
2429     int active;
2430     libxl__multidev *multidev; /* reference to the containing multidev */
2431     /* private for add/remove implementation */
2432     libxl__ev_devstate backend_ds;
2433     /* Bodge for Qemu devices */
2434     libxl__ev_time timeout;
2435     /* xenstore watch for backend path of driver domains */
2436     libxl__xswait_state xswait;
2437     int num_exec;
2438     /* for calling hotplug scripts */
2439     libxl__async_exec_state aes;
2440     /* If we need to update JSON config */
2441     bool update_json;
2442     /* for asynchronous execution of synchronous-only syscalls etc. */
2443     libxl__ev_child child;
2444 };
2445 
2446 /*
2447  * Multiple devices "multidev" handling.
2448  *
2449  * Firstly, you should
2450  *    libxl__multidev_begin
2451  *    multidev->callback = ...
2452  * Then zero or more times
2453  *    libxl__multidev_prepare
2454  *    libxl__initiate_device_{remove/addition}
2455  *       (or some other thing which will eventually call
2456  *        aodev->callback or libxl__multidev_one_callback)
2457  * Finally, once
2458  *    libxl__multidev_prepared
2459  * which will result (perhaps reentrantly) in one call to
2460  * multidev->callback().
2461  */
2462 
2463 /* Starts preparing to add/remove a bunch of devices. */
2464 _hidden void libxl__multidev_begin(libxl__ao *ao, libxl__multidev*);
2465 
2466 /* Prepares to add/remove one of many devices.
2467  * Calls libxl__prepare_ao_device on libxl__ao_device argument provided and
2468  * also sets the aodev->callback (to libxl__multidev_one_callback)
2469  * The user should not mess with aodev->callback.
2470  */
2471 _hidden void libxl__multidev_prepare_with_aodev(libxl__multidev*,
2472                                                 libxl__ao_device*);
2473 
2474 /* A wrapper function around libxl__multidev_prepare_with_aodev.
2475  * Allocates a libxl__ao_device and prepares it for addition/removal.
2476  * Returns the newly allocated libxl__ao_dev.
2477  */
2478 _hidden libxl__ao_device *libxl__multidev_prepare(libxl__multidev*);
2479 
2480 /* Indicates to multidev that this one device has been processed.
2481  * Normally the multidev user does not need to touch this function, as
2482  * multidev_prepare will name it in aodev->callback.  However, if you
2483  * want to do something more complicated you can set aodev->callback
2484  * yourself to something else, so long as you eventually call
2485  * libxl__multidev_one_callback.
2486  */
2487 _hidden void libxl__multidev_one_callback(libxl__egc *egc,
2488                                           libxl__ao_device *aodev);
2489 
2490 /* Notifies the multidev machinery that we have now finished preparing
2491  * and initiating devices.  multidev->callback may then be called as
2492  * soon as there are no prepared but not completed operations
2493  * outstanding, perhaps reentrantly.  If rc!=0 (error should have been
2494  * logged) multidev->callback will get a non-zero rc.
2495  * callback may be set by the user at any point before prepared. */
2496 _hidden void libxl__multidev_prepared(libxl__egc*, libxl__multidev*, int rc);
2497 
2498 typedef void libxl__devices_callback(libxl__egc*, libxl__multidev*, int rc);
2499 struct libxl__multidev {
2500     /* set by user: */
2501     libxl__devices_callback *callback;
2502     /* for private use by libxl__...ao_devices... machinery: */
2503     libxl__ao *ao;
2504     libxl__ao_device **array;
2505     int used, allocd;
2506     libxl__ao_device *preparation;
2507 };
2508 
2509 /*
2510  * Algorithm for handling device removal (including domain
2511  * destruction).  This is somewhat subtle because we may already have
2512  * killed the domain and caused the death of qemu.
2513  *
2514  * In current versions of qemu there is no mechanism for ensuring that
2515  * the resources used by its devices (both emulated and any PV devices
2516  * provided by qemu) are freed (eg, fds closed) before it shuts down,
2517  * and no confirmation from a terminating qemu back to the toolstack.
2518  *
2519  * This will need to be fixed in future Xen versions. In the meantime
2520  * (Xen 4.2) we implement a bodge.
2521  *
2522  *      WE WANT TO UNPLUG         WE WANT TO SHUT DOWN OR DESTROY
2523  *                    |                           |
2524  *                    |             LIBXL SENDS SIGHUP TO QEMU
2525  *                    |      .....................|........................
2526  *                    |      : XEN 4.3+ PLANNED   |                       :
2527  *                    |      :      QEMU TEARS DOWN ALL DEVICES           :
2528  *                    |      :      FREES RESOURCES (closing fds)         :
2529  *                    |      :      SETS PV BACKENDS TO STATE 5,          :
2530  *                    |      :       waits for PV frontends to shut down  :
2531  *                    |      :       SETS PV BACKENDS TO STATE 6          :
2532  *                    |      :                    |                       :
2533  *                    |      :      QEMU NOTIFIES TOOLSTACK (via          :
2534  *                    |      :       xenstore) that it is exiting         :
2535  *                    |      :      QEMU EXITS (parent may be init)       :
2536  *                    |      :                    |                       :
2537  *                    |      :        TOOLSTACK WAITS FOR QEMU            :
2538  *                    |      :        notices qemu has finished           :
2539  *                    |      :....................|.......................:
2540  *                    |      .--------------------'
2541  *                    V      V
2542  *                  for each device
2543  *                 we want to unplug/remove
2544  *       ..................|...........................................
2545  *       :                 V                       XEN 4.2 RACY BODGE :
2546  *       :      device is provided by    qemu                         :
2547  *       :            |            `-----------.                      :
2548  *       :   something|                        V                      :
2549  *       :    else, eg|             domain (that is domain for which  :
2550  *       :     blkback|              this PV device is the backend,   :
2551  *       :            |              which might be the stub dm)      :
2552  *       :            |                is still alive?                :
2553  *       :            |                  |        |                   :
2554  *       :            |                  |alive   |dead               :
2555  *       :            |<-----------------'        |                   :
2556  *       :            |    hopefully qemu is      |                   :
2557  *       :            |       still running       |                   :
2558  *       :............|.................          |                   :
2559  *             ,----->|                :     we may be racing         :
2560  *             |    backend state?     :      with qemu's death       :
2561  *             ^      |         |      :          |                   :
2562  *     xenstore|      |other    |6     :      WAIT 2.0s               :
2563  *     conflict|      |         |      :       TIMEOUT                :
2564  *             |   WRITE B.E.   |      :          |                   :
2565  *             |    STATE:=5    |      :     hopefully qemu has       :
2566  *             `---'  |         |      :      gone by now and         :
2567  *                    |ok       |      :      freed its resources     :
2568  *                    |         |      :          |                   :
2569  *              WAIT FOR        |      :     SET B.E.                 :
2570  *              STATE==6        |      :      STATE:=6                :
2571  *              /     |         |      :..........|...................:
2572  *      timeout/    ok|         |                 |
2573  *            /       |         |                 |
2574  *           |    RUN HOTPLUG <-'<----------------'
2575  *           |      SCRIPT
2576  *           |        |
2577  *           `---> NUKE
2578  *                  BACKEND
2579  *                    |
2580  *                   DONE.
2581  */
2582 
2583 /* AO operation to connect a disk device, called by
2584  * libxl_device_disk_add and libxl__add_disks. This function calls
2585  * libxl__wait_device_connection to wait for the device to
2586  * finish the connection (might involve executing hotplug scripts).
2587  *
2588  * Once finished, aodev->callback will be executed.
2589  */
2590 /*
2591  * As of Xen 4.5 we maintain various infomation, including hotplug
2592  * device information, in JSON files, so that we can use this JSON
2593  * file as a template to reconstruct domain configuration.
2594  *
2595  * In essense there are now two views of device state, one is xenstore,
2596  * the other is JSON file. We use xenstore as primary reference.
2597  *
2598  * Here we maintain one invariant: every device in xenstore must have
2599  * an entry in JSON file.
2600  *
2601  * All device hotplug routines should comply to following pattern:
2602  *   lock json config (json_lock)
2603  *       read json config
2604  *       update in-memory json config with new entry, replacing
2605  *          any stale entry
2606  *       for loop -- xs transaction
2607  *           open xs transaction
2608  *           check device existence, bail if it exists
2609  *           write in-memory json config to disk
2610  *           commit xs transaction
2611  *       end for loop
2612  *   unlock json config
2613  *
2614  * Device removal routines are not touched.
2615  *
2616  * Here is the proof that we always maintain that invariant and we
2617  * don't leak files during interaction of hotplug thread and other
2618  * threads / processes.
2619  *
2620  * # Safe against parallel add
2621  *
2622  * When another thread / process tries to add same device, it's
2623  * blocked by json_lock. The loser of two threads will bail at
2624  * existence check, so that we don't overwrite anything.
2625  *
2626  * # Safe against domain destruction
2627  *
2628  * If the thread / process trying to destroy domain loses the race, it's
2629  * blocked by json_lock. If the hotplug thread is loser, it bails at
2630  * acquiring lock because lock acquisition function checks existence of
2631  * the domain.
2632  *
2633  * # Safe against parallel removal
2634  *
2635  * When another thread / process tries to remove a device, it's _NOT_
2636  * blocked by json_lock, but xenstore transaction can help maintain
2637  * invariant. The removal threads either a) sees that device in
2638  * xenstore, b) doesn't see that device in xenstore.
2639  *
2640  * In a), it sees that device in xenstore. At that point hotplug is
2641  * already finished (both JSON and xenstore changes committed). So that
2642  * device can be safely removed. JSON entry is left untouched and
2643  * becomes stale, but this is a valid state -- next time when a
2644  * device with same identifier gets added, the stale entry gets
2645  * overwritten.
2646  *
2647  * In b), it doesn't see that device in xenstore, but it will commence
2648  * anyway. Eventually a forcibly removal is initiated, which will forcely
2649  * remove xenstore entry.
2650  *
2651  * If hotplug threads creates xenstore entry (therefore JSON entry as
2652  * well) before force removal, that xenstore entry is removed. We're
2653  * left with JSON stale entry but not xenstore entry, which is a valid
2654  * state.
2655  *
2656  * If hotplug thread has not created xenstore entry when the removal
2657  * is committed, we're obviously safe. Hotplug thread will add in
2658  * xenstore entry afterwards. We have both JSON and xenstore entry,
2659  * it's a valid state.
2660  */
2661 
2662 /* Waits for the passed device to reach state XenbusStateInitWait.
2663  * This is not really useful by itself, but is important when executing
2664  * hotplug scripts, since we need to be sure the device is in the correct
2665  * state before executing them.
2666  *
2667  * Once finished, aodev->callback will be executed.
2668  */
2669 _hidden void libxl__wait_device_connection(libxl__egc*,
2670                                            libxl__ao_device *aodev);
2671 
2672 /* Arranges that dev will be removed to the guest, and the
2673  * hotplug scripts will be executed (if necessary). When
2674  * this is done (or an error happens), the callback in
2675  * aodev->callback will be called.
2676  *
2677  * The libxl__ao_device passed to this function should be
2678  * prepared using libxl__prepare_ao_device prior to calling
2679  * this function.
2680  *
2681  * Once finished, aodev->callback will be executed.
2682  */
2683 _hidden void libxl__initiate_device_generic_remove(libxl__egc *egc,
2684                                                    libxl__ao_device *aodev);
2685 
2686 _hidden void libxl__initiate_device_usbctrl_remove(libxl__egc *egc,
2687                                                    libxl__ao_device *aodev);
2688 
2689 /*
2690  * libxl__get_hotplug_script_info returns the args and env that should
2691  * be passed to the hotplug script for the requested device.
2692  *
2693  * Since a device might not need to execute any hotplug script, this function
2694  * can return the following values:
2695  * < 0: Error
2696  * 0: No need to execute hotplug script
2697  * 1: Execute hotplug script
2698  *
2699  * The last parameter, "num_exec" refeers to the number of times hotplug
2700  * scripts have been called for this device.
2701  *
2702  * The main body of libxl will, for each device, keep calling
2703  * libxl__get_hotplug_script_info, with incrementing values of
2704  * num_exec, and executing the resulting script accordingly,
2705  * until libxl__get_hotplug_script_info returns<=0.
2706  */
2707 _hidden int libxl__get_hotplug_script_info(libxl__gc *gc, libxl__device *dev,
2708                                            char ***args, char ***env,
2709                                            libxl__device_action action,
2710                                            int num_exec);
2711 
2712 /*----- local disk attach: attach a disk locally to run the bootloader -----*/
2713 
2714 typedef struct libxl__disk_local_state libxl__disk_local_state;
2715 typedef void libxl__disk_local_state_callback(libxl__egc*,
2716                                               libxl__disk_local_state*,
2717                                               int rc);
2718 
2719 /* A libxl__disk_local_state may be in the following states:
2720  * Undefined, Idle, Attaching, Attached, Detaching.
2721  */
2722 struct libxl__disk_local_state {
2723     /* filled by the user */
2724     libxl__ao *ao;
2725     const libxl_device_disk *in_disk;
2726     libxl_device_disk disk;
2727     const char *blkdev_start;
2728     libxl__disk_local_state_callback *callback;
2729     /* filled by libxl__device_disk_local_initiate_attach */
2730     char *diskpath;
2731     /* private for implementation of local detach */
2732     libxl__ao_device aodev;
2733     int rc;
2734 };
2735 
2736 /*
2737  * Prepares a dls for use.
2738  * State Undefined -> Idle
2739  */
libxl__device_disk_local_init(libxl__disk_local_state * dls)2740 static inline void libxl__device_disk_local_init(libxl__disk_local_state *dls)
2741 {
2742     dls->rc = 0;
2743 }
2744 
2745 /*
2746  * See if we can find a way to access a disk locally
2747  */
2748 _hidden char * libxl__device_disk_find_local_path(libxl__gc *gc,
2749                                                   libxl_domid guest_domid,
2750                                                   const libxl_device_disk *disk,
2751                                                   bool qdisk_direct);
2752 
2753 
2754 /* Make a disk available in this (the control) domain. Always calls
2755  * dls->callback when finished.
2756  * State Idle -> Attaching
2757  *
2758  * The state of dls on entry to the callback depends on the value
2759  * of rc passed to the callback:
2760  *     rc == 0: Attached if rc == 0
2761  *     rc != 0: Idle
2762  */
2763 _hidden void libxl__device_disk_local_initiate_attach(libxl__egc *egc,
2764                                                 libxl__disk_local_state *dls);
2765 
2766 /* Disconnects a disk device form the control domain. If the passed
2767  * dls is not attached (or has already been detached),
2768  * libxl__device_disk_local_initiate_detach will just call the callback
2769  * directly.
2770  * State Idle/Attached -> Detaching
2771  *
2772  * The state of dls on entry to the callback is Idle.
2773  */
2774 _hidden void libxl__device_disk_local_initiate_detach(libxl__egc *egc,
2775                                                 libxl__disk_local_state *dls);
2776 
2777 /*----- datacopier: copies data from one fd to another -----*/
2778 
2779 typedef struct libxl__datacopier_state libxl__datacopier_state;
2780 typedef struct libxl__datacopier_buf libxl__datacopier_buf;
2781 
2782 /* onwrite==1 means problem happened when writing
2783  *     rc==FAIL    errnoval >0    we had a write error, logged
2784  * onwrite==0 means problem happened when reading
2785  *     rc==0       errnoval==0    we got eof and all data was written
2786  *     rc==FAIL    errnoval >0    we had a read error, logged
2787  * onwrite==-1 means some other internal problem
2788  *     rc==FAIL    errnoval==EIO  some other internal failure, logged
2789  *     rc==ABORTED errnoval==0    abort requested, not logged
2790  * If we get POLLHUP, we call callback_pollhup with
2791  *     rc==FAIL    errnoval==-1   POLLHUP signalled
2792  * or if callback_pollhup==0 this is treated as eof (if POLLIN|POLLHUP
2793  * on the reading fd) or an internal failure (otherwise), as above.
2794  * In all cases copier is killed before calling this callback */
2795 typedef void libxl__datacopier_callback(libxl__egc *egc,
2796      libxl__datacopier_state *dc, int rc, int onwrite, int errnoval);
2797 
2798 struct libxl__datacopier_buf {
2799     /* private to datacopier */
2800     LIBXL_TAILQ_ENTRY(libxl__datacopier_buf) entry;
2801     int used;
2802     char buf[1000];
2803 };
2804 
2805 struct libxl__datacopier_state {
2806     /* caller must fill these in, and they must all remain valid */
2807     libxl__ao *ao;
2808     int readfd, writefd;
2809     ssize_t maxsz;
2810     ssize_t bytes_to_read; /* set to -1 to read until EOF */
2811     const char *copywhat, *readwhat, *writewhat; /* for error msgs */
2812     FILE *log; /* gets a copy of everything */
2813     libxl__datacopier_callback *callback;
2814     libxl__datacopier_callback *callback_pollhup;
2815     void *readbuf; /* Set this to read data into it without writing to an
2816                       fd. The buffer should be at least as large as the
2817                       bytes_to_read parameter, which should not be -1. */
2818     /* remaining fields are private to datacopier */
2819     libxl__ao_abortable abrt;
2820     libxl__ev_fd toread, towrite;
2821     ssize_t used;
2822     LIBXL_TAILQ_HEAD(libxl__datacopier_bufs, libxl__datacopier_buf) bufs;
2823 };
2824 
2825 _hidden void libxl__datacopier_init(libxl__datacopier_state *dc);
2826 _hidden void libxl__datacopier_kill(libxl__datacopier_state *dc);
2827 _hidden int libxl__datacopier_start(libxl__datacopier_state *dc);
2828 
2829 /* Inserts literal data into the output stream.  The data is copied.
2830  * May safely be used only immediately after libxl__datacopier_start
2831  * (before the ctx is unlocked).  But may be called multiple times.
2832  * NB exceeding maxsz will fail an assertion! */
2833 _hidden void libxl__datacopier_prefixdata(libxl__egc*, libxl__datacopier_state*,
2834                                           const void *data, size_t len);
2835 
2836 /*----- Save/restore helper (used by creation and suspend) -----*/
2837 
2838 typedef struct libxl__srm_save_callbacks {
2839     libxl__srm_save_autogen_callbacks a;
2840 } libxl__srm_save_callbacks;
2841 
2842 typedef struct libxl__srm_restore_callbacks {
2843     libxl__srm_restore_autogen_callbacks a;
2844 } libxl__srm_restore_callbacks;
2845 
2846 /* a pointer to this struct is also passed as "user" to the
2847  * save callout helper callback functions */
2848 typedef struct libxl__save_helper_state {
2849     /* public, caller of run_helper initialises */
2850     libxl__ao *ao;
2851     uint32_t domid;
2852     union {
2853         libxl__srm_save_callbacks save;
2854         libxl__srm_restore_callbacks restore;
2855     } callbacks;
2856     int (*recv_callback)(const unsigned char *msg, uint32_t len, void *user);
2857     void (*completion_callback)(libxl__egc *egc, void *caller_state,
2858                                 int rc, int retval, int errnoval);
2859     void *caller_state;
2860     int need_results; /* set to 0 or 1 by caller of run_helper;
2861                        * if set to 1 then the ultimate caller's
2862                        * results function must set it to 0 */
2863     /* private */
2864     int rc;
2865     int completed; /* retval/errnoval valid iff completed */
2866     int retval, errnoval; /* from xc_domain_save / xc_domain_restore */
2867     libxl__ao_abortable abrt;
2868     libxl__carefd *pipes[2]; /* 0 = helper's stdin, 1 = helper's stdout */
2869     libxl__ev_fd readable;
2870     libxl__ev_child child;
2871     const char *stdin_what, *stdout_what;
2872 
2873     libxl__egc *egc; /* valid only for duration of each event callback;
2874                       * is here in this struct for the benefit of the
2875                       * marshalling and xc callback functions */
2876 } libxl__save_helper_state;
2877 
2878 /*----- checkpoint device related state structure -----*/
2879 /*
2880  * The abstract checkpoint device layer exposes a common
2881  * set of API to [external] libxl for manipulating devices attached to
2882  * a guest protected by Remus/COLO. The device layer also exposes a set of
2883  * [internal] interfaces that every device type must implement.
2884  *
2885  * The following API are exposed to libxl:
2886  *
2887  * One-time configuration operations:
2888  *  +libxl__checkpoint_devices_setup
2889  *    > Enable output buffering for NICs, setup disk replication, etc.
2890  *  +libxl__checkpoint_devices_teardown
2891  *    > Disable output buffering and disk replication; teardown any
2892  *       associated external setups like qdiscs for NICs.
2893  *
2894  * Operations executed every checkpoint (in order of invocation):
2895  *  +libxl__checkpoint_devices_postsuspend
2896  *  +libxl__checkpoint_devices_preresume
2897  *  +libxl__checkpoint_devices_commit
2898  *
2899  * Each device type needs to implement the interfaces specified in
2900  * the libxl__checkpoint_device_instance_ops if it wishes to support Remus/COLO.
2901  *
2902  * The high-level control flow through the checkpoint device layer is shown
2903  * below:
2904  *
2905  * xl remus
2906  *  |->  libxl_domain_remus_start
2907  *    |-> libxl__checkpoint_devices_setup
2908  *      |-> Per-checkpoint libxl__checkpoint_devices_[postsuspend,preresume,commit]
2909  *        ...
2910  *        |-> On backup failure, network error or other internal errors:
2911  *            libxl__checkpoint_devices_teardown
2912  */
2913 
2914 typedef struct libxl__checkpoint_device libxl__checkpoint_device;
2915 typedef struct libxl__checkpoint_devices_state libxl__checkpoint_devices_state;
2916 typedef struct libxl__checkpoint_device_instance_ops libxl__checkpoint_device_instance_ops;
2917 
2918 /*
2919  * Interfaces to be implemented by every device subkind that wishes to
2920  * support Remus/COLO. Functions must be implemented unless otherwise
2921  * stated. Many of these functions are asynchronous. They call
2922  * dev->aodev.callback when done.  The actual implementations may be
2923  * synchronous and call dev->aodev.callback directly (as the last
2924  * thing they do).
2925  */
2926 struct libxl__checkpoint_device_instance_ops {
2927     /* the device kind this ops belongs to... */
2928     libxl__device_kind kind;
2929 
2930     /*
2931      * Checkpoint operations. May be NULL, meaning the op is not
2932      * implemented and the caller should treat them as a no-op (and do
2933      * nothing when checkpointing).
2934      * Asynchronous.
2935      */
2936 
2937     void (*postsuspend)(libxl__egc *egc, libxl__checkpoint_device *dev);
2938     void (*preresume)(libxl__egc *egc, libxl__checkpoint_device *dev);
2939     void (*commit)(libxl__egc *egc, libxl__checkpoint_device *dev);
2940 
2941     /*
2942      * setup() and teardown() are refer to the actual checkpoint device.
2943      * Asynchronous.
2944      * teardown is called even if setup fails.
2945      */
2946     /*
2947      * setup() should first determines whether the subkind matches the specific
2948      * device. If matched, the device will then be managed with this set of
2949      * subkind operations.
2950      * Yields 0 if the device successfully set up.
2951      * CHECKPOINT_DEVOPS_DOES_NOT_MATCH if the ops does not match the device.
2952      * any other rc indicates failure.
2953      */
2954     void (*setup)(libxl__egc *egc, libxl__checkpoint_device *dev);
2955     void (*teardown)(libxl__egc *egc, libxl__checkpoint_device *dev);
2956 };
2957 
2958 int init_subkind_nic(libxl__checkpoint_devices_state *cds);
2959 void cleanup_subkind_nic(libxl__checkpoint_devices_state *cds);
2960 int init_subkind_drbd_disk(libxl__checkpoint_devices_state *cds);
2961 void cleanup_subkind_drbd_disk(libxl__checkpoint_devices_state *cds);
2962 
2963 typedef void libxl__checkpoint_callback(libxl__egc *,
2964                                         libxl__checkpoint_devices_state *,
2965                                         int rc);
2966 
2967 /*
2968  * State associated with a checkpoint invocation, including parameters
2969  * passed to the checkpoint abstract device layer by the remus
2970  * save/restore machinery.
2971  */
2972 struct libxl__checkpoint_devices_state {
2973     /*-- must be set by caller of libxl__checkpoint_device_(setup|teardown) --*/
2974 
2975     libxl__ao *ao;
2976     uint32_t domid;
2977     libxl__checkpoint_callback *callback;
2978     void *concrete_data;
2979     int device_kind_flags;
2980     /* The ops must be pointer array, and the last ops must be NULL. */
2981     const libxl__checkpoint_device_instance_ops **ops;
2982 
2983     /*----- private for abstract layer only -----*/
2984 
2985     int num_devices;
2986     /*
2987      * this array is allocated before setup the checkpoint devices by the
2988      * checkpoint abstract layer.
2989      * devs may be NULL, means there's no checkpoint devices that has been
2990      * set up.
2991      * the size of this array is 'num_devices', which is the total number
2992      * of libxl nic devices and disk devices(num_nics + num_disks).
2993      */
2994     libxl__checkpoint_device **devs;
2995 
2996     libxl_device_nic *nics;
2997     int num_nics;
2998     libxl_device_disk *disks;
2999     int num_disks;
3000 
3001     libxl__multidev multidev;
3002 };
3003 
3004 /*
3005  * Information about a single device being handled by remus.
3006  * Allocated by the checkpoint abstract layer.
3007  */
3008 struct libxl__checkpoint_device {
3009     /*----- shared between abstract and concrete layers -----*/
3010     /*
3011      * if this is true, that means the subkind ops match the device
3012      */
3013     bool matched;
3014 
3015     /*----- set by checkpoint device abstruct layer -----*/
3016     /* libxl__device_* which this checkpoint device related to */
3017     const void *backend_dev;
3018     libxl__device_kind kind;
3019     libxl__checkpoint_devices_state *cds;
3020     libxl__ao_device aodev;
3021 
3022     /*----- private for abstract layer only -----*/
3023 
3024     /*
3025      * Control and state variables for the asynchronous callback
3026      * based loops which iterate over device subkinds, and over
3027      * individual devices.
3028      */
3029     int ops_index;
3030     const libxl__checkpoint_device_instance_ops *ops;
3031 
3032     /*----- private for concrete (device-specific) layer -----*/
3033 
3034     /* concrete device's private data */
3035     void *concrete_data;
3036 };
3037 
3038 /* the following 5 APIs are async ops, call cds->callback when done */
3039 _hidden void libxl__checkpoint_devices_setup(libxl__egc *egc,
3040                                         libxl__checkpoint_devices_state *cds);
3041 _hidden void libxl__checkpoint_devices_teardown(libxl__egc *egc,
3042                                         libxl__checkpoint_devices_state *cds);
3043 _hidden void libxl__checkpoint_devices_postsuspend(libxl__egc *egc,
3044                                         libxl__checkpoint_devices_state *cds);
3045 _hidden void libxl__checkpoint_devices_preresume(libxl__egc *egc,
3046                                         libxl__checkpoint_devices_state *cds);
3047 _hidden void libxl__checkpoint_devices_commit(libxl__egc *egc,
3048                                         libxl__checkpoint_devices_state *cds);
3049 
3050 /*----- Remus related state structure -----*/
3051 typedef struct libxl__remus_state libxl__remus_state;
3052 struct libxl__remus_state {
3053     /* private */
3054     libxl__ev_time checkpoint_timeout; /* used for Remus checkpoint */
3055     int interval; /* checkpoint interval */
3056 
3057     /*----- private for concrete (device-specific) layer only -----*/
3058     /* private for nic device subkind ops */
3059     char *netbufscript;
3060     struct nl_sock *nlsock;
3061     struct nl_cache *qdisc_cache;
3062 
3063     /* private for drbd disk subkind ops */
3064     char *drbd_probe_script;
3065 };
3066 _hidden int libxl__netbuffer_enabled(libxl__gc *gc);
3067 
3068 /*----- Legacy conversion helper -----*/
3069 typedef struct libxl__conversion_helper_state libxl__conversion_helper_state;
3070 
3071 struct libxl__conversion_helper_state {
3072     /* Public - Must be filled by caller unless noted. */
3073     libxl__ao *ao;
3074     int legacy_fd;             /* fd to read the legacy stream from. */
3075     bool hvm;                  /* pv or hvm domain? */
3076     libxl__carefd *v2_carefd;  /* Filled by successful call to
3077                                 * libxl__convert_legacy_stream().  Caller
3078                                 * assumes ownership of the fd. */
3079     void (*completion_callback)(
3080         libxl__egc *egc, libxl__conversion_helper_state *chs, int rc);
3081     /* private */
3082     int rc;
3083     libxl__ao_abortable abrt;
3084     libxl__ev_child child;
3085 };
3086 
3087 _hidden void libxl__conversion_helper_init
3088                     (libxl__conversion_helper_state *chs);
3089 _hidden int libxl__convert_legacy_stream(libxl__egc *egc,
3090                     libxl__conversion_helper_state *chs);
3091 _hidden void libxl__conversion_helper_abort(libxl__egc *egc,
3092                     libxl__conversion_helper_state *chs, int rc);
libxl__conversion_helper_inuse(const libxl__conversion_helper_state * chs)3093 static inline bool libxl__conversion_helper_inuse
3094                     (const libxl__conversion_helper_state *chs)
3095 { return libxl__ev_child_inuse(&chs->child); }
3096 
3097 /* State for reading a libxl migration v2 stream */
3098 typedef struct libxl__stream_read_state libxl__stream_read_state;
3099 
3100 typedef struct libxl__sr_record_buf {
3101     /* private to stream read helper */
3102     LIBXL_STAILQ_ENTRY(struct libxl__sr_record_buf) entry;
3103     libxl__sr_rec_hdr hdr;
3104     void *body; /* iff hdr.length != 0 */
3105 } libxl__sr_record_buf;
3106 
3107 struct libxl__stream_read_state {
3108     /* filled by the user */
3109     libxl__ao *ao;
3110     libxl__domain_create_state *dcs;
3111     int fd;
3112     bool legacy;
3113     bool back_channel;
3114     void (*completion_callback)(libxl__egc *egc,
3115                                 libxl__stream_read_state *srs,
3116                                 int rc);
3117     void (*checkpoint_callback)(libxl__egc *egc,
3118                                 libxl__stream_read_state *srs,
3119                                 int rc);
3120     /* Private */
3121     int rc;
3122     bool running;
3123     bool in_checkpoint;
3124     bool sync_teardown; /* Only used to coordinate shutdown on error path. */
3125     bool in_checkpoint_state;
3126     libxl__save_helper_state shs;
3127     libxl__conversion_helper_state chs;
3128 
3129     /* Main stream-reading data. */
3130     libxl__datacopier_state dc; /* Only used when reading a record */
3131     libxl__sr_hdr hdr;
3132     LIBXL_STAILQ_HEAD(, libxl__sr_record_buf) record_queue; /* NOGC */
3133     enum {
3134         SRS_PHASE_NORMAL,
3135         SRS_PHASE_BUFFERING,
3136         SRS_PHASE_UNBUFFERING,
3137     } phase;
3138     bool recursion_guard;
3139 
3140     /* Only used while actively reading a record from the stream. */
3141     libxl__sr_record_buf *incoming_record; /* NOGC */
3142 
3143     /* Both only used when processing an EMULATOR record. */
3144     libxl__datacopier_state emu_dc;
3145     libxl__carefd *emu_carefd;
3146 };
3147 
3148 _hidden void libxl__stream_read_init(libxl__stream_read_state *stream);
3149 _hidden void libxl__stream_read_start(libxl__egc *egc,
3150                                       libxl__stream_read_state *stream);
3151 _hidden void libxl__stream_read_start_checkpoint(libxl__egc *egc,
3152                                                  libxl__stream_read_state *stream);
3153 _hidden void libxl__stream_read_checkpoint_state(libxl__egc *egc,
3154                                                  libxl__stream_read_state *stream);
3155 _hidden void libxl__stream_read_abort(libxl__egc *egc,
3156                                       libxl__stream_read_state *stream, int rc);
3157 static inline bool
libxl__stream_read_inuse(const libxl__stream_read_state * stream)3158 libxl__stream_read_inuse(const libxl__stream_read_state *stream)
3159 {
3160     return stream->running;
3161 }
3162 
3163 #include "libxl_colo.h"
3164 
3165 /*----- Domain suspend (save) state structure -----*/
3166 /*
3167  * "suspend" refers to quiescing the VM, so pausing qemu, making a
3168  * remote_shutdown(SHUTDOWN_suspend) hypercall etc.
3169  *
3170  * "save" refers to the actions involved in actually shuffling the
3171  * state of the VM, so xc_domain_save() etc.
3172  */
3173 
3174 typedef struct libxl__domain_suspend_state libxl__domain_suspend_state;
3175 typedef struct libxl__domain_save_state libxl__domain_save_state;
3176 
3177 typedef void libxl__domain_save_cb(libxl__egc*,
3178                                    libxl__domain_save_state*, int rc);
3179 typedef void libxl__save_device_model_cb(libxl__egc*,
3180                                          libxl__domain_save_state*, int rc);
3181 
3182 /* State for writing a libxl migration v2 stream */
3183 typedef struct libxl__stream_write_state libxl__stream_write_state;
3184 typedef void (*sws_record_done_cb)(libxl__egc *egc,
3185                                    libxl__stream_write_state *sws);
3186 struct libxl__stream_write_state {
3187     /* filled by the user */
3188     libxl__ao *ao;
3189     libxl__domain_save_state *dss;
3190     int fd;
3191     bool back_channel;
3192     void (*completion_callback)(libxl__egc *egc,
3193                                 libxl__stream_write_state *sws,
3194                                 int rc);
3195     void (*checkpoint_callback)(libxl__egc *egc,
3196                                 libxl__stream_write_state *sws,
3197                                 int rc);
3198     /* Private */
3199     int rc;
3200     bool running;
3201     bool in_checkpoint;
3202     bool sync_teardown;  /* Only used to coordinate shutdown on error path. */
3203     bool in_checkpoint_state;
3204     libxl__save_helper_state shs;
3205 
3206     /* Main stream-writing data. */
3207     libxl__datacopier_state dc;
3208     sws_record_done_cb record_done_callback;
3209 
3210     /* Cache device model version. */
3211     libxl_device_model_version device_model_version;
3212 
3213     /* Only used when constructing EMULATOR records. */
3214     libxl__datacopier_state emu_dc;
3215     libxl__carefd *emu_carefd;
3216     libxl__sr_rec_hdr emu_rec_hdr;
3217     libxl__sr_emulator_hdr emu_sub_hdr;
3218     void *emu_body;
3219 };
3220 
3221 _hidden void libxl__stream_write_init(libxl__stream_write_state *stream);
3222 _hidden void libxl__stream_write_start(libxl__egc *egc,
3223                                        libxl__stream_write_state *stream);
3224 _hidden void
3225 libxl__stream_write_start_checkpoint(libxl__egc *egc,
3226                                      libxl__stream_write_state *stream);
3227 _hidden void
3228 libxl__stream_write_checkpoint_state(libxl__egc *egc,
3229                                      libxl__stream_write_state *stream,
3230                                      libxl_sr_checkpoint_state *srcs);
3231 _hidden void libxl__stream_write_abort(libxl__egc *egc,
3232                                        libxl__stream_write_state *stream,
3233                                        int rc);
3234 static inline bool
libxl__stream_write_inuse(const libxl__stream_write_state * stream)3235 libxl__stream_write_inuse(const libxl__stream_write_state *stream)
3236 {
3237     return stream->running;
3238 }
3239 
3240 typedef struct libxl__logdirty_switch {
3241     /* Set by caller of libxl__domain_common_switch_qemu_logdirty */
3242     libxl__ao *ao;
3243     void (*callback)(libxl__egc *egc, struct libxl__logdirty_switch *lds,
3244                      int rc);
3245 
3246     const char *cmd;
3247     const char *cmd_path;
3248     const char *ret_path;
3249     libxl__ev_xswatch watch;
3250     libxl__ev_time timeout;
3251 } libxl__logdirty_switch;
3252 
3253 _hidden void libxl__logdirty_init(libxl__logdirty_switch *lds);
3254 
3255 struct libxl__domain_suspend_state {
3256     /* set by caller of libxl__domain_suspend_init */
3257     libxl__ao *ao;
3258     uint32_t domid;
3259 
3260     /* private */
3261     libxl_domain_type type;
3262 
3263     libxl__ev_evtchn guest_evtchn;
3264     int guest_evtchn_lockfd;
3265     int guest_responded;
3266 
3267     libxl__xswait_state pvcontrol;
3268     libxl__ev_xswatch guest_watch;
3269     libxl__ev_time guest_timeout;
3270 
3271     const char *dm_savefile;
3272     void (*callback_common_done)(libxl__egc*,
3273                                  struct libxl__domain_suspend_state*, int ok);
3274 };
3275 int libxl__domain_suspend_init(libxl__egc *egc,
3276                                libxl__domain_suspend_state *dsps,
3277                                libxl_domain_type type);
3278 
3279 struct libxl__domain_save_state {
3280     /* set by caller of libxl__domain_save */
3281     libxl__ao *ao;
3282     libxl__domain_save_cb *callback;
3283 
3284     uint32_t domid;
3285     int fd;
3286     int fdfl; /* original flags on fd */
3287     int recv_fd;
3288     libxl_domain_type type;
3289     int live;
3290     int debug;
3291     int checkpointed_stream;
3292     const libxl_domain_remus_info *remus;
3293     /* private */
3294     int rc;
3295     int hvm;
3296     int xcflags;
3297     libxl__domain_suspend_state dsps;
3298     union {
3299         /* for Remus */
3300         libxl__remus_state rs;
3301         /* for COLO */
3302         libxl__colo_save_state css;
3303     };
3304     libxl__checkpoint_devices_state cds;
3305     libxl__stream_write_state sws;
3306     libxl__logdirty_switch logdirty;
3307 };
3308 
3309 
3310 /*----- openpty -----*/
3311 
3312 /*
3313  * opens count (>0) ptys like count calls to openpty, and then
3314  * calls back.  On entry, all op[].master and op[].slave must be
3315  * 0.  On callback, either rc==0 and master and slave are non-0,
3316  * or rc is a libxl error and they are both 0.  If libxl__openpty
3317  * returns non-0 no callback will happen and everything is left
3318  * cleaned up.
3319  */
3320 
3321 typedef struct libxl__openpty_state libxl__openpty_state;
3322 typedef struct libxl__openpty_result libxl__openpty_result;
3323 typedef void libxl__openpty_callback(libxl__egc *egc, libxl__openpty_state *op);
3324 
3325 struct libxl__openpty_state {
3326     /* caller must fill these in, and they must all remain valid */
3327     libxl__ao *ao;
3328     libxl__openpty_callback *callback;
3329     int count;
3330     libxl__openpty_result *results; /* actual size is count, out parameter */
3331     /* public, result, caller may only read in callback */
3332     int rc;
3333     /* private for implementation */
3334     libxl__ev_child child;
3335 };
3336 
3337 struct libxl__openpty_result {
3338     libxl__carefd *master, *slave;
3339 };
3340 
3341 int libxl__openptys(libxl__openpty_state *op,
3342                     struct termios *termp,
3343                     struct winsize *winp);
3344 
3345 
3346 /*----- bootloader -----*/
3347 
3348 typedef struct libxl__bootloader_state libxl__bootloader_state;
3349 typedef void libxl__run_bootloader_callback(libxl__egc*,
3350                                 libxl__bootloader_state*, int rc);
3351 typedef void libxl__bootloader_console_callback(libxl__egc*,
3352                                 libxl__bootloader_state*);
3353 
3354 struct libxl__bootloader_state {
3355     /* caller must fill these in, and they must all remain valid */
3356     libxl__ao *ao;
3357     libxl__run_bootloader_callback *callback;
3358     libxl__bootloader_console_callback *console_available;
3359     const libxl_domain_build_info *info;
3360     libxl_device_disk *disk;
3361     /* Should be zeroed by caller on entry.  Will be filled in by
3362      * bootloader machinery; represents the local attachment of the
3363      * disk for the benefit of the bootloader.  Must be detached by
3364      * the caller using libxl__device_disk_local_initiate_detach.
3365      * (This is safe to do after ->callback() has happened since
3366      * the domain's kernel and initramfs will have been copied
3367      * out of the guest's disk into a temporary directory, mapped
3368      * as file references, and deleted. */
3369     libxl__disk_local_state dls;
3370     uint32_t domid;
3371     /* outputs:
3372      *  - caller must initialise kernel and ramdisk to point to file
3373      *    references, these will be updated and mapped;
3374      *  - caller must initialise cmdline to NULL, it will be updated with a
3375      *    string allocated from the gc;
3376      */
3377     libxl__file_reference *kernel, *ramdisk;
3378     const char *cmdline;
3379     /* private to libxl__run_bootloader */
3380     char *outputpath, *outputdir, *logfile;
3381     libxl__openpty_state openpty;
3382     libxl__openpty_result ptys[2];  /* [0] is for bootloader */
3383     libxl__ev_child child;
3384     libxl__domaindeathcheck deathcheck;
3385     int nargs, argsspace;
3386     const char **args;
3387     libxl__datacopier_state keystrokes, display;
3388     int rc, got_pollhup;
3389 };
3390 
3391 _hidden void libxl__bootloader_init(libxl__bootloader_state *bl);
3392 
3393 /* Will definitely call st->callback, perhaps reentrantly.
3394  * If callback is passed rc==0, will have updated st->info appropriately */
3395 _hidden void libxl__bootloader_run(libxl__egc*, libxl__bootloader_state *st);
3396 
3397 /*----- Generic Device Handling -----*/
3398 #define LIBXL_DEFINE_DEVICE_ADD(type)                                   \
3399     int libxl_device_##type##_add(libxl_ctx *ctx,                       \
3400         uint32_t domid, libxl_device_##type *type,                      \
3401         const libxl_asyncop_how *ao_how)                                \
3402     {                                                                   \
3403         AO_CREATE(ctx, domid, ao_how);                                  \
3404         libxl__ao_device *aodev;                                        \
3405                                                                         \
3406         GCNEW(aodev);                                                   \
3407         libxl__prepare_ao_device(ao, aodev);                            \
3408         aodev->action = LIBXL__DEVICE_ACTION_ADD;                       \
3409         aodev->callback = device_addrm_aocomplete;                      \
3410         aodev->update_json = true;                                      \
3411         libxl__device_##type##_add(egc, domid, type, aodev);            \
3412                                                                         \
3413         return AO_INPROGRESS;                                           \
3414     }
3415 
3416 #define LIBXL_DEFINE_DEVICES_ADD(type)                                  \
3417     void libxl__add_##type##s(libxl__egc *egc, libxl__ao *ao, uint32_t domid, \
3418                               libxl_domain_config *d_config,            \
3419                               libxl__multidev *multidev)                \
3420     {                                                                   \
3421         AO_GC;                                                          \
3422         int i;                                                          \
3423         for (i = 0; i < d_config->num_##type##s; i++) {                 \
3424             libxl__ao_device *aodev = libxl__multidev_prepare(multidev);  \
3425             libxl__device_##type##_add(egc, domid, &d_config->type##s[i], \
3426                                        aodev);                          \
3427         }                                                               \
3428     }
3429 
3430 #define LIBXL_DEFINE_DEVICE_REMOVE_EXT(type, remtype, removedestroy, f) \
3431     int libxl_device_##type##_##removedestroy(libxl_ctx *ctx,           \
3432         uint32_t domid, libxl_device_##type *type,                      \
3433         const libxl_asyncop_how *ao_how)                                \
3434     {                                                                   \
3435         AO_CREATE(ctx, domid, ao_how);                                  \
3436         libxl__device *device;                                          \
3437         libxl__ao_device *aodev;                                        \
3438         int rc;                                                         \
3439                                                                         \
3440         GCNEW(device);                                                  \
3441         rc = libxl__device_from_##type(gc, domid, type, device);        \
3442         if (rc != 0) goto out;                                          \
3443                                                                         \
3444         GCNEW(aodev);                                                   \
3445         libxl__prepare_ao_device(ao, aodev);                            \
3446         aodev->action = LIBXL__DEVICE_ACTION_REMOVE;                    \
3447         aodev->dev = device;                                            \
3448         aodev->callback = device_addrm_aocomplete;                      \
3449         aodev->force = f;                                               \
3450         libxl__initiate_device_##remtype##_remove(egc, aodev);          \
3451                                                                         \
3452     out:                                                                \
3453         if (rc) return AO_CREATE_FAIL(rc);                              \
3454         return AO_INPROGRESS;                                           \
3455     }
3456 
3457 #define LIBXL_DEFINE_UPDATE_DEVID(type, name)                           \
3458     int libxl__device_##type##_update_devid(libxl__gc *gc,              \
3459                                             uint32_t domid,             \
3460                                             libxl_device_##type *type)  \
3461     {                                                                   \
3462         if (type->devid == -1)                                          \
3463             type->devid = libxl__device_nextid(gc, domid, name);        \
3464         if (type->devid < 0)                                            \
3465             return ERROR_FAIL;                                          \
3466         return 0;                                                       \
3467     }
3468 
3469 #define LIBXL_DEFINE_DEVICE_REMOVE(type)                                \
3470     LIBXL_DEFINE_DEVICE_REMOVE_EXT(type, generic, remove, 0)            \
3471     LIBXL_DEFINE_DEVICE_REMOVE_EXT(type, generic, destroy, 1)
3472 
3473 #define LIBXL_DEFINE_DEVICE_REMOVE_CUSTOM(type)                         \
3474     LIBXL_DEFINE_DEVICE_REMOVE_EXT(type, type, remove, 0)               \
3475     LIBXL_DEFINE_DEVICE_REMOVE_EXT(type, type, destroy, 1)
3476 
3477 #define LIBXL_DEFINE_DEVICE_LIST(type)                                  \
3478     libxl_device_##type *libxl_device_##type##_list(libxl_ctx *ctx,     \
3479                                                     uint32_t domid,     \
3480                                                     int *num)           \
3481     {                                                                   \
3482         libxl_device_##type *r;                                         \
3483         GC_INIT(ctx);                                                   \
3484         r = libxl__device_list(gc, &libxl__##type##_devtype,            \
3485                                domid, num);                             \
3486         GC_FREE;                                                        \
3487         return r;                                                       \
3488     }                                                                   \
3489                                                                         \
3490     void libxl_device_##type##_list_free(libxl_device_##type *list,     \
3491                                          int num)                       \
3492     {                                                                   \
3493         libxl__device_list_free(&libxl__##type##_devtype, list, num);   \
3494     }
3495 
3496 typedef void (*device_add_fn_t)(libxl__egc *, libxl__ao *, uint32_t,
3497                                 libxl_domain_config *, libxl__multidev *);
3498 typedef int (*device_set_default_fn_t)(libxl__gc *, uint32_t, void *, bool);
3499 typedef int (*device_to_device_fn_t)(libxl__gc *, uint32_t, void *,
3500                                      libxl__device *);
3501 typedef void (*device_init_fn_t)(void *);
3502 typedef void (*device_copy_fn_t)(libxl_ctx *, void *, void *);
3503 typedef void (*device_dispose_fn_t)(void *);
3504 typedef int (*device_compare_fn_t)(void *, void *);
3505 typedef void (*device_merge_fn_t)(libxl_ctx *, void *, void *);
3506 typedef int (*device_dm_needed_fn_t)(void *, unsigned);
3507 typedef void (*device_update_config_fn_t)(libxl__gc *, void *, void *);
3508 typedef int (*device_update_devid_fn_t)(libxl__gc *, uint32_t, void *);
3509 typedef int (*device_from_xenstore_fn_t)(libxl__gc *, const char *,
3510                                          libxl_devid, void *);
3511 typedef int (*device_set_xenstore_config_fn_t)(libxl__gc *, uint32_t, void *,
3512                                                flexarray_t *, flexarray_t *,
3513                                                flexarray_t *);
3514 
3515 struct libxl_device_type {
3516     char *type;
3517     char *entry;
3518     int skip_attach;   /* Skip entry in domcreate_attach_devices() if 1 */
3519     int ptr_offset;    /* Offset of device array ptr in libxl_domain_config */
3520     int num_offset;    /* Offset of # of devices in libxl_domain_config */
3521     int dev_elem_size; /* Size of one device element in array */
3522     device_add_fn_t                 add;
3523     device_set_default_fn_t         set_default;
3524     device_to_device_fn_t           to_device;
3525     device_init_fn_t                init;
3526     device_copy_fn_t                copy;
3527     device_dispose_fn_t             dispose;
3528     device_compare_fn_t             compare;
3529     device_merge_fn_t               merge;
3530     device_dm_needed_fn_t           dm_needed;
3531     device_update_config_fn_t       update_config;
3532     device_update_devid_fn_t        update_devid;
3533     device_from_xenstore_fn_t       from_xenstore;
3534     device_set_xenstore_config_fn_t set_xenstore_config;
3535 };
3536 
3537 #define DEFINE_DEVICE_TYPE_STRUCT_X(name, sname, sentry, ...)                  \
3538     const struct libxl_device_type libxl__ ## name ## _devtype = {             \
3539         .type          = #sname,                                               \
3540         .entry         = #sentry,                                              \
3541         .ptr_offset    = offsetof(libxl_domain_config, name ## s),             \
3542         .num_offset    = offsetof(libxl_domain_config, num_ ## name ## s),     \
3543         .dev_elem_size = sizeof(libxl_device_ ## sname),                       \
3544         .add           = libxl__add_ ## name ## s,                             \
3545         .set_default   = (device_set_default_fn_t)                             \
3546                          libxl__device_ ## sname ## _setdefault,               \
3547         .to_device     = (device_to_device_fn_t)libxl__device_from_ ## name,   \
3548         .init          = (device_init_fn_t)libxl_device_ ## sname ## _init,    \
3549         .copy          = (device_copy_fn_t)libxl_device_ ## sname ## _copy,    \
3550         .dispose       = (device_dispose_fn_t)                                 \
3551                          libxl_device_ ## sname ## _dispose,                   \
3552         .compare       = (device_compare_fn_t)                                 \
3553                          libxl_device_ ## sname ## _compare,                   \
3554         .update_devid  = (device_update_devid_fn_t)                            \
3555                          libxl__device_ ## sname ## _update_devid,             \
3556         __VA_ARGS__                                                            \
3557     }
3558 
3559 #define DEFINE_DEVICE_TYPE_STRUCT(name, ...)                                   \
3560     DEFINE_DEVICE_TYPE_STRUCT_X(name, name, name, __VA_ARGS__)
3561 
libxl__device_type_get_ptr(const struct libxl_device_type * dt,const libxl_domain_config * d_config)3562 static inline void **libxl__device_type_get_ptr(
3563     const struct libxl_device_type *dt, const libxl_domain_config *d_config)
3564 {
3565     return (void **)((void *)d_config + dt->ptr_offset);
3566 }
3567 
libxl__device_type_get_elem(const struct libxl_device_type * dt,const libxl_domain_config * d_config,int e)3568 static inline void *libxl__device_type_get_elem(
3569     const struct libxl_device_type *dt, const libxl_domain_config *d_config,
3570     int e)
3571 {
3572     return *libxl__device_type_get_ptr(dt, d_config) + dt->dev_elem_size * e;
3573 }
3574 
libxl__device_type_get_num(const struct libxl_device_type * dt,const libxl_domain_config * d_config)3575 static inline int *libxl__device_type_get_num(
3576     const struct libxl_device_type *dt, const libxl_domain_config *d_config)
3577 {
3578     return (int *)((void *)d_config + dt->num_offset);
3579 }
3580 
3581 extern const struct libxl_device_type libxl__vfb_devtype;
3582 extern const struct libxl_device_type libxl__vkb_devtype;
3583 extern const struct libxl_device_type libxl__disk_devtype;
3584 extern const struct libxl_device_type libxl__nic_devtype;
3585 extern const struct libxl_device_type libxl__vtpm_devtype;
3586 extern const struct libxl_device_type libxl__usbctrl_devtype;
3587 extern const struct libxl_device_type libxl__usbdev_devtype;
3588 extern const struct libxl_device_type libxl__pcidev_devtype;
3589 extern const struct libxl_device_type libxl__vdispl_devtype;
3590 extern const struct libxl_device_type libxl__p9_devtype;
3591 
3592 extern const struct libxl_device_type *device_type_tbl[];
3593 
3594 /*----- Domain destruction -----*/
3595 
3596 /* Domain destruction has been split into two functions:
3597  *
3598  * libxl__domain_destroy is the main destroy function, which detects
3599  * stubdoms and calls libxl__destroy_domid on the domain and its
3600  * stubdom if present, creating a different libxl__destroy_domid_state
3601  * for each one of them.
3602  *
3603  * libxl__destroy_domid actually destroys the domain, but it
3604  * doesn't check for stubdomains, since that would involve
3605  * recursion, which we want to avoid.
3606  */
3607 
3608 typedef struct libxl__domain_destroy_state libxl__domain_destroy_state;
3609 typedef struct libxl__destroy_domid_state libxl__destroy_domid_state;
3610 typedef struct libxl__devices_remove_state libxl__devices_remove_state;
3611 
3612 typedef void libxl__domain_destroy_cb(libxl__egc *egc,
3613                                       libxl__domain_destroy_state *dds,
3614                                       int rc);
3615 
3616 typedef void libxl__domid_destroy_cb(libxl__egc *egc,
3617                                      libxl__destroy_domid_state *dis,
3618                                      int rc);
3619 
3620 typedef void libxl__devices_remove_callback(libxl__egc *egc,
3621                                             libxl__devices_remove_state *drs,
3622                                             int rc);
3623 
3624 struct libxl__devices_remove_state {
3625     /* filled in by user */
3626     libxl__ao *ao;
3627     uint32_t domid;
3628     libxl__devices_remove_callback *callback;
3629     int force; /* libxl_device_TYPE_destroy rather than _remove */
3630     /* private */
3631     libxl__multidev multidev;
3632     int num_devices;
3633 };
3634 
3635 struct libxl__destroy_domid_state {
3636     /* filled in by user */
3637     libxl__ao *ao;
3638     uint32_t domid;
3639     libxl__domid_destroy_cb *callback;
3640     /* private to implementation */
3641     libxl__devices_remove_state drs;
3642     libxl__ev_child destroyer;
3643     bool soft_reset;
3644 };
3645 
3646 struct libxl__domain_destroy_state {
3647     /* filled by the user */
3648     libxl__ao *ao;
3649     uint32_t domid;
3650     libxl__domain_destroy_cb *callback;
3651     /* Private */
3652     int rc;
3653     uint32_t stubdomid;
3654     libxl__destroy_domid_state stubdom;
3655     int stubdom_finished;
3656     libxl__destroy_domid_state domain;
3657     int domain_finished;
3658     bool soft_reset;
3659 };
3660 
3661 /*
3662  * Entry point for domain destruction
3663  * This function checks for stubdom presence and then calls
3664  * libxl__destroy_domid on the passed domain and its stubdom if found.
3665  */
3666 _hidden void libxl__domain_destroy(libxl__egc *egc,
3667                                    libxl__domain_destroy_state *dds);
3668 
3669 /* Used to destroy a domain with the passed id (it doesn't check for stubs) */
3670 _hidden void libxl__destroy_domid(libxl__egc *egc,
3671                                   libxl__destroy_domid_state *dis);
3672 
3673 /* Entry point for devices destruction */
3674 _hidden void libxl__devices_destroy(libxl__egc *egc,
3675                                     libxl__devices_remove_state *drs);
3676 
3677 /* Helper function to add a bunch of disks. This should be used when
3678  * the caller is inside an async op. "multidev" will NOT be prepared by
3679  * this function, so the caller must make sure to call
3680  * libxl__multidev_begin before calling this function.
3681  *
3682  * The "callback" will be called for each device, and the user is responsible
3683  * for calling libxl__ao_device_check_last on the callback.
3684  */
3685 _hidden void libxl__add_disks(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
3686                               libxl_domain_config *d_config,
3687                               libxl__multidev *multidev);
3688 
3689 _hidden void libxl__add_nics(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
3690                              libxl_domain_config *d_config,
3691                              libxl__multidev *multidev);
3692 
3693 /*----- device model creation -----*/
3694 
3695 /* First layer; wraps libxl__spawn_spawn. */
3696 
3697 typedef struct libxl__dm_spawn_state libxl__dm_spawn_state;
3698 
3699 typedef void libxl__dm_spawn_cb(libxl__egc *egc, libxl__dm_spawn_state*,
3700                                 int rc /* if !0, error was logged */);
3701 
3702 struct libxl__dm_spawn_state {
3703     /* mixed - spawn.ao must be initialised by user; rest is private: */
3704     libxl__spawn_state spawn;
3705     /* filled in by user, must remain valid: */
3706     uint32_t guest_domid; /* domain being served */
3707     libxl_domain_config *guest_config;
3708     libxl__domain_build_state *build_state; /* relates to guest_domid */
3709     libxl__dm_spawn_cb *callback;
3710 };
3711 
3712 _hidden void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state*);
3713 
3714 /* Stubdom device models. */
3715 
3716 typedef struct {
3717     /* Mixed - user must fill in public parts EXCEPT callback,
3718      * which may be undefined on entry.  (See above for details) */
3719     libxl__dm_spawn_state dm; /* the stub domain device model */
3720     /* filled in by user, must remain valid: */
3721     libxl__dm_spawn_cb *callback; /* called as callback(,&sdss->dm,) */
3722     /* private to libxl__spawn_stub_dm: */
3723     libxl_domain_config dm_config;
3724     libxl__domain_build_state dm_state;
3725     libxl__dm_spawn_state pvqemu;
3726     libxl__destroy_domid_state dis;
3727     libxl__multidev multidev;
3728     libxl__xswait_state xswait;
3729 } libxl__stub_dm_spawn_state;
3730 
3731 _hidden void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state*);
3732 
3733 _hidden char *libxl__stub_dm_name(libxl__gc *gc, const char * guest_name);
3734 
3735 /* Qdisk backend launch helpers */
3736 
3737 _hidden void libxl__spawn_qdisk_backend(libxl__egc *egc,
3738                                         libxl__dm_spawn_state *dmss);
3739 _hidden int libxl__destroy_qdisk_backend(libxl__gc *gc, uint32_t domid);
3740 
3741 /*----- Domain creation -----*/
3742 
3743 
3744 struct libxl__domain_create_state {
3745     /* filled in by user */
3746     libxl__ao *ao;
3747     libxl_domain_config *guest_config;
3748     libxl_domain_config guest_config_saved; /* vanilla config */
3749     int restore_fd, libxc_fd;
3750     int restore_fdfl; /* original flags of restore_fd */
3751     int send_back_fd;
3752     libxl_domain_restore_params restore_params;
3753     uint32_t domid_soft_reset;
3754     libxl__domain_create_cb *callback;
3755     libxl_asyncprogress_how aop_console_how;
3756     /* private to domain_create */
3757     int guest_domid;
3758     int device_type_idx;
3759     const char *colo_proxy_script;
3760     libxl__domain_build_state build_state;
3761     libxl__colo_restore_state crs;
3762     libxl__checkpoint_devices_state cds;
3763     libxl__bootloader_state bl;
3764     libxl__stub_dm_spawn_state sdss;
3765         /* If we're not doing stubdom, we use only dmss.dm,
3766          * for the non-stubdom device model. */
3767     libxl__stream_read_state srs;
3768     /* necessary if the domain creation failed and we have to destroy it */
3769     libxl__domain_destroy_state dds;
3770     libxl__multidev multidev;
3771 };
3772 
3773 _hidden int libxl__device_nic_set_devids(libxl__gc *gc,
3774                                          libxl_domain_config *d_config,
3775                                          uint32_t domid);
3776 
3777 /*----- Domain suspend (save) functions -----*/
3778 
3779 /* calls dss->callback when done */
3780 _hidden void libxl__domain_save(libxl__egc *egc,
3781                                 libxl__domain_save_state *dss);
3782 
3783 
3784 /* calls libxl__xc_domain_suspend_done when done */
3785 _hidden void libxl__xc_domain_save(libxl__egc *egc,
3786                                    libxl__domain_save_state *dss,
3787                                    libxl__save_helper_state *shs);
3788 /* If rc==0 then retval is the return value from xc_domain_save
3789  * and errnoval is the errno value it provided.
3790  * If rc!=0, retval and errnoval are undefined. */
3791 _hidden void libxl__xc_domain_save_done(libxl__egc*, void *dss_void,
3792                                         int rc, int retval, int errnoval);
3793 
3794 /* Used by asynchronous callbacks: ie ones which xc regards as
3795  * returning a value, but which we want to handle asynchronously.
3796  * Such functions' actual callback function return void in libxl
3797  * When they are ready to indicate completion, they call this. */
3798 void libxl__xc_domain_saverestore_async_callback_done(libxl__egc *egc,
3799                            libxl__save_helper_state *shs, int return_value);
3800 
3801 
3802 _hidden void libxl__domain_suspend_common_switch_qemu_logdirty
3803                                (uint32_t domid, unsigned int enable, void *data);
3804 _hidden void libxl__domain_common_switch_qemu_logdirty(libxl__egc *egc,
3805                                                int domid, unsigned enable,
3806                                                libxl__logdirty_switch *lds);
3807 _hidden int libxl__save_emulator_xenstore_data(libxl__domain_save_state *dss,
3808                                                char **buf, uint32_t *len);
3809 _hidden int libxl__restore_emulator_xenstore_data
3810     (libxl__domain_create_state *dcs, const char *ptr, uint32_t size);
3811 
3812 
3813 /* calls libxl__xc_domain_restore_done when done */
3814 _hidden void libxl__xc_domain_restore(libxl__egc *egc,
3815                                       libxl__domain_create_state *dcs,
3816                                       libxl__save_helper_state *shs,
3817                                       int hvm, int pae);
3818 /* If rc==0 then retval is the return value from xc_domain_save
3819  * and errnoval is the errno value it provided.
3820  * If rc!=0, retval and errnoval are undefined. */
3821 _hidden void libxl__xc_domain_restore_done(libxl__egc *egc, void *dcs_void,
3822                                            int rc, int retval, int errnoval);
3823 
3824 _hidden void libxl__save_helper_init(libxl__save_helper_state *shs);
3825 _hidden void libxl__save_helper_abort(libxl__egc *egc,
3826                                       libxl__save_helper_state *shs);
3827 
libxl__save_helper_inuse(const libxl__save_helper_state * shs)3828 static inline bool libxl__save_helper_inuse(const libxl__save_helper_state *shs)
3829 {
3830     return libxl__ev_child_inuse(&shs->child);
3831 }
3832 
3833 /* Each time the dm needs to be saved, we must call suspend and then save */
3834 _hidden int libxl__domain_suspend_device_model(libxl__gc *gc,
3835                                            libxl__domain_suspend_state *dsps);
3836 
3837 _hidden const char *libxl__device_model_savefile(libxl__gc *gc, uint32_t domid);
3838 
3839 /* calls dsps->callback_common_done when done */
3840 _hidden void libxl__domain_suspend(libxl__egc *egc,
3841                                    libxl__domain_suspend_state *dsps);
3842 /* used by libxc to suspend the guest during migration */
3843 _hidden void libxl__domain_suspend_callback(void *data);
3844 
3845 /* Remus setup and teardown */
3846 _hidden void libxl__remus_setup(libxl__egc *egc,
3847                                 libxl__remus_state *rs);
3848 _hidden void libxl__remus_teardown(libxl__egc *egc,
3849                                    libxl__remus_state *rs,
3850                                    int rc);
3851 _hidden void libxl__remus_restore_setup(libxl__egc *egc,
3852                                         libxl__domain_create_state *dcs);
3853 
3854 
3855 /*
3856  * Convenience macros.
3857  */
3858 
3859 #define FILLZERO LIBXL_FILLZERO
3860 
3861 
3862 /*
3863  * All of these assume (or define)
3864  *    libxl__gc *gc;
3865  * as a local variable.
3866  */
3867 
3868 #define GC_INIT(ctx)  libxl__gc gc[1]; LIBXL_INIT_GC(gc[0],ctx)
3869 #define GC_FREE       libxl__free_all(gc)
3870 #define CTX           libxl__gc_owner(gc)
3871 #define NOGC          (&CTX->nogc_gc) /* pass only to consenting functions */
3872 
3873 /* Allocation macros all of which use the gc. */
3874 
3875 #define ARRAY_SIZE_OK(ptr, nmemb) ((nmemb) < INT_MAX / (sizeof(*(ptr)) * 2))
3876 
3877 /*
3878  * Expression statement  <type> *GCNEW(<type> *var);
3879  * Uses                  libxl__gc *gc;
3880  *
3881  * Allocates a new object of type <type> from the gc and zeroes it
3882  * with memset.  Sets var to point to the new object or zero (setting
3883  * errno).  Returns the new value of var.
3884  */
3885 #define GCNEW(var)                                      \
3886     (((var) = libxl__zalloc((gc),sizeof(*(var)))))
3887 
3888 /*
3889  * Expression statement  <type> *GCNEW_ARRAY(<type> *var, ssize_t nmemb);
3890  * Uses                  libxl__gc *gc;
3891  *
3892  * Like GCNEW but allocates an array of nmemb elements, as if from
3893  * calloc.  Does check for integer overflow due to large nmemb.  If
3894  * nmemb is 0 may succeed by returning 0.
3895  */
3896 #define GCNEW_ARRAY(var, nmemb)                                 \
3897     ((var) = libxl__calloc((gc), (nmemb), sizeof(*(var))))
3898 
3899 /*
3900  * Expression statement  <type> *GCREALLOC_ARRAY(<type> *var, size_t nmemb);
3901  * Uses                  libxl__gc *gc;
3902  *
3903  * Reallocates the array var to be of size nmemb elements.  Updates
3904  * var and returns the new value of var.  Does check for integer
3905  * overflow due to large nmemb.
3906  *
3907  * Do not pass nmemb==0.  old may be 0 on entry.
3908  */
3909 #define GCREALLOC_ARRAY(var, nmemb)                                     \
3910     (assert(nmemb > 0),                                                 \
3911      assert(ARRAY_SIZE_OK((var), (nmemb))),                             \
3912      (var) = libxl__realloc((gc), (var), (nmemb)*sizeof(*(var))))
3913 
3914 
3915 /*
3916  * Expression            char *GCSPRINTF(const char *fmt, ...);
3917  * Uses                  libxl__gc *gc;
3918  *
3919  * Trivial convenience wrapper for libxl__sprintf.
3920  */
3921 #define GCSPRINTF(fmt, ...) (libxl__sprintf((gc), (fmt), __VA_ARGS__))
3922 
3923 
3924 /*
3925  * Expression statements
3926  *    void LOG(<xtl_level_suffix>, const char *fmt, ...);
3927  *    void LOGE(<xtl_level_suffix>, const char *fmt, ...);
3928  *    void LOGEV(<xtl_level_suffix>, int errnoval, const char *fmt, ...);
3929  *
3930  *    void LOGD(<xtl_level_suffix>, uint32_t domid, const char *fmt, ...);
3931  *    void LOGED(<xtl_level_suffix>, uint32_t domid, const char *fmt, ...);
3932  *    void LOGEVD(<xtl_level_suffix>, int errnoval, uint32_t domid, const char *fmt, ...);
3933  * Use
3934  *    libxl__gc *gc;
3935  *
3936  * Trivial convenience wrappers for LIBXL__LOG, LIBXL__LOG_ERRNO,
3937  * LIBXL__LOG_ERRNOVAL, LIBXL__LOGD, LIBXL__LOGD_ERRNO and
3938  * LIBXL__LOGD_ERRNOVAL respectively (and thus for libxl__log).
3939  *
3940  * XTL_<xtl_level_suffix> should exist and be an xentoollog.h log level
3941  * So <xtl_level_suffix> should be one of
3942  *   DEBUG VERBOSE DETAIL PROGRESS INFO NOTICE WARN ERROR ERROR CRITICAL
3943  * Of these, most of libxl uses
3944  *   DEBUG INFO WARN ERROR
3945  *
3946  * The LOG*D family will preprend the log message with a string formatted
3947  * as follows: 'Domain %PRIu32:'. This should help better automatic sorting
3948  * of log messages per domain.
3949  */
3950 #define LOG(l,f, ...)     LIBXL__LOG(CTX,XTL_##l,(f),##__VA_ARGS__)
3951 #define LOGE(l,f, ...)    LIBXL__LOG_ERRNO(CTX,XTL_##l,(f),##__VA_ARGS__)
3952 #define LOGEV(l,e,f, ...) LIBXL__LOG_ERRNOVAL(CTX,XTL_##l,(e),(f),##__VA_ARGS__)
3953 
3954 #define LOGD(l,d,f, ...)     LIBXL__LOGD(CTX,XTL_##l,(d),(f),##__VA_ARGS__)
3955 #define LOGED(l,d,f, ...)    LIBXL__LOGD_ERRNO(CTX,XTL_##l,(d),(f),##__VA_ARGS__)
3956 #define LOGEVD(l,e,d,f, ...) LIBXL__LOGD_ERRNOVAL(CTX,XTL_##l,(e),(d),(f),##__VA_ARGS__)
3957 
3958 
3959 /* Locking functions.  See comment for "lock" member of libxl__ctx. */
3960 
libxl__ctx_lock(libxl_ctx * ctx)3961 static inline void libxl__ctx_lock(libxl_ctx *ctx) {
3962     int r = pthread_mutex_lock(&ctx->lock);
3963     assert(!r);
3964 }
3965 
libxl__ctx_unlock(libxl_ctx * ctx)3966 static inline void libxl__ctx_unlock(libxl_ctx *ctx) {
3967     int r = pthread_mutex_unlock(&ctx->lock);
3968     assert(!r);
3969 }
3970 
3971 #define CTX_LOCK (libxl__ctx_lock(CTX))
3972 #define CTX_UNLOCK (libxl__ctx_unlock(CTX))
3973 
3974 /*
3975  * Automatic NUMA placement
3976  *
3977  * These functions and data structures deal with the initial placement of a
3978  * domain onto the host NUMA nodes.
3979  *
3980  * The key concept here is the one of "NUMA placement candidate", which is
3981  * basically a set of nodes whose characteristics have been successfully
3982  * checked against some specific requirements. More precisely, a candidate
3983  * is the nodemap associated with one of the possible subset of the host
3984  * NUMA nodes providing a certain amount of free memory, or a given number
3985  * of cpus, or even both (depending in what the caller wants). For
3986  * convenience of use, some of this information are stored within the
3987  * candidate itself, instead of always being dynamically computed. A single
3988  * node can be valid placement candidate, as well as it is possible for a
3989  * candidate to contain all the nodes of the host. The fewer nodes there
3990  * are in a candidate, the better performance a domain placed onto it
3991  * should get (at least from a NUMA point of view). For instance, looking
3992  * for a numa candidates with 2GB of free memory means we want the subsets
3993  * of the host NUMA nodes with, cumulatively, at least 2GB of free memory.
3994  * This condition can be satisfied by just one particular node, or it may
3995  * require more nodes, depending on the characteristics of the host, on how
3996  * many domains have been created already, on how big they are, etc.
3997  *
3998  * The intended usage is as follows:
3999  *  1. first of all, call libxl__get_numa_candidates(), and specify the
4000  *     proper constraints to it (e.g., the amount of memory a domain need
4001  *     as the minimum amount of free memory for the candidates). If a
4002  *     candidate comparison function is provided, the candidate with fewer
4003  *     nodes that is found to be best according to what such fucntion says
4004  *     is returned. If no comparison function is passed, the very first
4005  *     candidate is.
4006  *  2. The chosen candidate's nodemap should be utilized for computing the
4007  *     actual affinity of the domain which, given the current NUMA support
4008  *     in the hypervisor, is what determines the placement of the domain's
4009  *     vcpus and memory.
4010  */
4011 
4012 typedef struct {
4013     int nr_cpus, nr_nodes;
4014     int nr_vcpus;
4015     uint64_t free_memkb;
4016     libxl_bitmap nodemap;
4017 } libxl__numa_candidate;
4018 
4019 /* Signature for the comparison function between two candidates */
4020 typedef int (*libxl__numa_candidate_cmpf)(const libxl__numa_candidate *c1,
4021                                           const libxl__numa_candidate *c2);
4022 
4023 /*
4024  * This looks for the best NUMA placement candidate satisfying some
4025  * specific conditions. If min_nodes and/or max_nodes are not 0, their
4026  * value is used to determine the minimum and maximum number of nodes the
4027  * candidate can have. If they are 0, it means the candidate can contain
4028  * from 1 node (min_nodes=0) to the total number of nodes of the host
4029  * (max_ndoes=0). If min_free_memkb and/or min_cpus are not 0, the caller
4030  * only wants candidates with at least the amount of free memory and the
4031  * number of cpus they specify, respectively. If they are 0, the
4032  * candidates' free memory and/or number of cpus won't be checked at all.
4033  *
4034  * Candidates are compared among each others by calling numa_cmpf(), which
4035  * is where the heuristics for determining which candidate is the best
4036  * one is actually implemented. The only bit of it that is hardcoded in
4037  * this function is the fact that candidates with fewer nodes are always
4038  * preferrable.
4039  *
4040  * If at least one suitable candidate is found, it is returned in cndt_out,
4041  * cndt_found is set to one, and the function returns successfully. On the
4042  * other hand, if not even one single candidate can be found, the function
4043  * still returns successfully but cndt_found will be zero.
4044  *
4045  * Finally, suitable_cpumap is useful for telling that only the cpus in that
4046  * mask should be considered when generating placement candidates (for
4047  * example because of cpupools).
4048  *
4049  * It is up to the function to properly allocate cndt_out (by calling
4050  * libxl__numa_candidate_alloc()), while it is the caller that should init
4051  * (libxl__numa_candidate_init()) and free (libxl__numa_candidate_dispose())
4052  * it.
4053  */
4054 _hidden int libxl__get_numa_candidate(libxl__gc *gc,
4055                                       uint64_t min_free_memkb, int min_cpus,
4056                                       int min_nodes, int max_nodes,
4057                                       const libxl_bitmap *suitable_cpumap,
4058                                       libxl__numa_candidate_cmpf numa_cmpf,
4059                                       libxl__numa_candidate *cndt_out,
4060                                       int *cndt_found);
4061 
4062 /* Initialization, allocation and deallocation for placement candidates */
libxl__numa_candidate_init(libxl__numa_candidate * cndt)4063 static inline void libxl__numa_candidate_init(libxl__numa_candidate *cndt)
4064 {
4065     cndt->free_memkb = 0;
4066     cndt->nr_cpus = cndt->nr_nodes = cndt->nr_vcpus = 0;
4067     libxl_bitmap_init(&cndt->nodemap);
4068 }
4069 
libxl__numa_candidate_alloc(libxl__gc * gc,libxl__numa_candidate * cndt)4070 static inline int libxl__numa_candidate_alloc(libxl__gc *gc,
4071                                               libxl__numa_candidate *cndt)
4072 {
4073     return libxl_node_bitmap_alloc(CTX, &cndt->nodemap, 0);
4074 }
libxl__numa_candidate_dispose(libxl__numa_candidate * cndt)4075 static inline void libxl__numa_candidate_dispose(libxl__numa_candidate *cndt)
4076 {
4077     libxl_bitmap_dispose(&cndt->nodemap);
4078 }
4079 
4080 /* Retrieve (in nodemap) the node map associated to placement candidate cndt */
4081 static inline
libxl__numa_candidate_get_nodemap(libxl__gc * gc,const libxl__numa_candidate * cndt,libxl_bitmap * nodemap)4082 void libxl__numa_candidate_get_nodemap(libxl__gc *gc,
4083                                        const libxl__numa_candidate *cndt,
4084                                        libxl_bitmap *nodemap)
4085 {
4086     libxl_bitmap_copy(CTX, nodemap, &cndt->nodemap);
4087 }
4088 /* Set the node map of placement candidate cndt to match nodemap */
4089 static inline
libxl__numa_candidate_put_nodemap(libxl__gc * gc,libxl__numa_candidate * cndt,const libxl_bitmap * nodemap)4090 void libxl__numa_candidate_put_nodemap(libxl__gc *gc,
4091                                        libxl__numa_candidate *cndt,
4092                                        const libxl_bitmap *nodemap)
4093 {
4094     libxl_bitmap_copy(CTX, &cndt->nodemap, nodemap);
4095 }
4096 
4097 /* Check if vNUMA config is valid. Returns 0 if valid,
4098  * ERROR_VNUMA_CONFIG_INVALID otherwise.
4099  */
4100 int libxl__vnuma_config_check(libxl__gc *gc,
4101                               const libxl_domain_build_info *b_info,
4102                               const libxl__domain_build_state *state);
4103 int libxl__vnuma_build_vmemrange_pv_generic(libxl__gc *gc,
4104                                             uint32_t domid,
4105                                             libxl_domain_build_info *b_info,
4106                                             libxl__domain_build_state *state);
4107 int libxl__vnuma_build_vmemrange_pv(libxl__gc *gc,
4108                                     uint32_t domid,
4109                                     libxl_domain_build_info *b_info,
4110                                     libxl__domain_build_state *state);
4111 int libxl__vnuma_build_vmemrange_hvm(libxl__gc *gc,
4112                                      uint32_t domid,
4113                                      libxl_domain_build_info *b_info,
4114                                      libxl__domain_build_state *state,
4115                                      struct xc_dom_image *dom);
4116 bool libxl__vnuma_configured(const libxl_domain_build_info *b_info);
4117 
4118 _hidden int libxl__ms_vm_genid_set(libxl__gc *gc, uint32_t domid,
4119                                    const libxl_ms_vm_genid *id);
4120 
4121 
4122 /* Som handy macros for defbool type. */
4123 #define LIBXL__DEFBOOL_DEFAULT     (0)
4124 #define LIBXL__DEFBOOL_FALSE       (-1)
4125 #define LIBXL__DEFBOOL_TRUE        (1)
4126 #define LIBXL__DEFBOOL_STR_DEFAULT "<default>"
4127 #define LIBXL__DEFBOOL_STR_FALSE   "False"
4128 #define LIBXL__DEFBOOL_STR_TRUE    "True"
libxl__defbool_is_default(libxl_defbool * db)4129 static inline int libxl__defbool_is_default(libxl_defbool *db)
4130 {
4131     return !db->val;
4132 }
4133 
4134 /*
4135  * Inserts "elm_new" into the sorted list "head".
4136  *
4137  * "elm_search" must be a loop search variable of the same type as
4138  * "elm_new".  "new_after_search_p" must be an expression which is
4139  * true iff the element "elm_new" sorts after the element
4140  * "elm_search".
4141  *
4142  * "search_body" can be empty, or some declaration(s) and statement(s)
4143  * needed for "new_after_search_p".
4144  */
4145 #define LIBXL_TAILQ_INSERT_SORTED(head, entry, elm_new, elm_search,     \
4146                                   search_body, new_after_search_p)      \
4147     do {                                                                \
4148         for ((elm_search) = LIBXL_TAILQ_FIRST((head));                  \
4149              (elm_search);                                              \
4150              (elm_search) = LIBXL_TAILQ_NEXT((elm_search), entry)) {    \
4151             search_body;                                                \
4152             if (!(new_after_search_p))                                  \
4153                 break;                                                  \
4154         }                                                               \
4155         /* now elm_search is either the element before which we want    \
4156          * to place elm_new, or NULL meaning we want to put elm_new at  \
4157          * the end */                                                   \
4158         if ((elm_search))                                               \
4159             LIBXL_TAILQ_INSERT_BEFORE((elm_search), (elm_new), entry);  \
4160         else                                                            \
4161             LIBXL_TAILQ_INSERT_TAIL((head), (elm_new), entry);          \
4162     } while(0)
4163 
4164 
4165 /*
4166  * int CTYPE(ISFOO, char c);
4167  * int CTYPE(toupper, char c);
4168  * int CTYPE(tolower, char c);
4169  *
4170  * This is necessary because passing a simple char to a ctype.h
4171  * is forbidden.  ctype.h macros take ints derived from _unsigned_ chars.
4172  *
4173  * If you have a char which might be EOF then you should already have
4174  * it in an int representing an unsigned char, and you can use the
4175  * <ctype.h> macros directly.  This generally happens only with values
4176  * from fgetc et al.
4177  *
4178  * For any value known to be a character (eg, anything that came from
4179  * a char[]), use CTYPE.
4180  */
4181 #define CTYPE(isfoo,c) (isfoo((unsigned char)(c)))
4182 
4183 int libxl__defbool_parse_json(libxl__gc *gc, const libxl__json_object *o,
4184                               libxl_defbool *p);
4185 int libxl__bool_parse_json(libxl__gc *gc, const libxl__json_object *o,
4186                            bool *p);
4187 int libxl__mac_parse_json(libxl__gc *gc, const libxl__json_object *o,
4188                           libxl_mac *p);
4189 int libxl__bitmap_parse_json(libxl__gc *gc, const libxl__json_object *o,
4190                              libxl_bitmap *p);
4191 int libxl__uuid_parse_json(libxl__gc *gc, const libxl__json_object *o,
4192                            libxl_uuid *p);
4193 int libxl__cpuid_policy_list_parse_json(libxl__gc *gc,
4194                                         const libxl__json_object *o,
4195                                         libxl_cpuid_policy_list *p);
4196 int libxl__string_list_parse_json(libxl__gc *gc, const libxl__json_object *o,
4197                                   libxl_string_list *p);
4198 int libxl__key_value_list_parse_json(libxl__gc *gc,
4199                                      const libxl__json_object *o,
4200                                      libxl_key_value_list *p);
4201 int libxl__hwcap_parse_json(libxl__gc *gc, const libxl__json_object *o,
4202                             libxl_hwcap *p);
4203 int libxl__ms_vm_genid_parse_json(libxl__gc *gc, const libxl__json_object *o,
4204                                   libxl_ms_vm_genid *p);
4205 int libxl__int_parse_json(libxl__gc *gc, const libxl__json_object *o,
4206                           void *p);
4207 int libxl__uint8_parse_json(libxl__gc *gc, const libxl__json_object *o,
4208                             void *p);
4209 int libxl__uint16_parse_json(libxl__gc *gc, const libxl__json_object *o,
4210                              void *p);
4211 int libxl__uint32_parse_json(libxl__gc *gc, const libxl__json_object *o,
4212                              void *p);
4213 int libxl__uint64_parse_json(libxl__gc *gc, const libxl__json_object *o,
4214                              void *p);
4215 int libxl__string_parse_json(libxl__gc *gc, const libxl__json_object *o,
4216                              char **p);
4217 
4218 int libxl__random_bytes(libxl__gc *gc, uint8_t *buf, size_t len);
4219 
4220 #include "_libxl_types_private.h"
4221 #include "_libxl_types_internal_private.h"
4222 
4223 /* This always return false, there's no "default value" for hw cap */
libxl__hwcap_is_default(libxl_hwcap * hwcap)4224 static inline int libxl__hwcap_is_default(libxl_hwcap *hwcap)
4225 {
4226     return 0;
4227 }
4228 
libxl__string_list_is_empty(libxl_string_list * psl)4229 static inline int libxl__string_list_is_empty(libxl_string_list *psl)
4230 {
4231     return !libxl_string_list_length(psl);
4232 }
4233 
libxl__key_value_list_is_empty(libxl_key_value_list * pkvl)4234 static inline int libxl__key_value_list_is_empty(libxl_key_value_list *pkvl)
4235 {
4236     return !libxl_key_value_list_length(pkvl);
4237 }
4238 
4239 int libxl__cpuid_policy_is_empty(libxl_cpuid_policy_list *pl);
4240 
4241 /* Portability note: a proper flock(2) implementation is required */
4242 typedef struct {
4243     libxl__carefd *carefd;
4244     char *path; /* path of the lock file itself */
4245 } libxl__domain_userdata_lock;
4246 /* The CTX_LOCK must be held around uses of this lock */
4247 libxl__domain_userdata_lock *libxl__lock_domain_userdata(libxl__gc *gc,
4248                                                          uint32_t domid);
4249 void libxl__unlock_domain_userdata(libxl__domain_userdata_lock *lock);
4250 
4251 /*
4252  * Retrieve / store domain configuration from / to libxl private
4253  * data store. The registry entry in libxl private data store
4254  * is "libxl-json".
4255  * Caller must hold user data lock.
4256  */
4257 int libxl__get_domain_configuration(libxl__gc *gc, uint32_t domid,
4258                                     libxl_domain_config *d_config);
4259 int libxl__set_domain_configuration(libxl__gc *gc, uint32_t domid,
4260                                     libxl_domain_config *d_config);
4261 
4262 /* ------ Things related to updating domain configurations ----- */
4263 void libxl__update_domain_configuration(libxl__gc *gc,
4264                                         libxl_domain_config *dst,
4265                                         const libxl_domain_config *src);
4266 
4267 /* Target memory in xenstore is different from what user has
4268  * asked for. The difference is video_memkb + (possible) fudge.
4269  * See libxl_set_memory_target.
4270  */
4271 static inline
libxl__get_targetmem_fudge(libxl__gc * gc,const libxl_domain_build_info * info)4272 uint64_t libxl__get_targetmem_fudge(libxl__gc *gc,
4273                                     const libxl_domain_build_info *info)
4274 {
4275     int64_t mem_target_fudge = (info->type == LIBXL_DOMAIN_TYPE_HVM &&
4276                                 info->max_memkb > info->target_memkb)
4277                                 ? LIBXL_MAXMEM_CONSTANT : 0;
4278 
4279     return info->video_memkb + mem_target_fudge;
4280 }
4281 
4282 int libxl__get_memory_target(libxl__gc *gc, uint32_t domid,
4283                              uint64_t *out_target_memkb,
4284                              uint64_t *out_max_memkb);
4285 void libxl__xcinfo2xlinfo(libxl_ctx *ctx,
4286                           const xc_domaininfo_t *xcinfo,
4287                           libxl_dominfo *xlinfo);
4288 
4289 /* Macros used to compare device identifier. Returns true if the two
4290  * devices have same identifier. */
4291 #define COMPARE_DEVID(a, b) ((a)->devid == (b)->devid)
4292 #define COMPARE_DISK(a, b) (!strcmp((a)->vdev, (b)->vdev))
4293 #define COMPARE_PCI(a, b) ((a)->func == (b)->func &&    \
4294                            (a)->bus == (b)->bus &&      \
4295                            (a)->dev == (b)->dev)
4296 #define COMPARE_USB(a, b) ((a)->ctrl == (b)->ctrl && \
4297                            (a)->port == (b)->port)
4298 #define COMPARE_USBCTRL(a, b) ((a)->devid == (b)->devid)
4299 
4300 /* This function copies X bytes from source to destination bitmap,
4301  * where X is the smaller of the two sizes.
4302  *
4303  * If destination's size is larger than source, the extra bytes are
4304  * untouched.
4305  *
4306  * XXX This is introduced to fix a regression for 4.5. It shall
4307  * be revisited in 4.6 time frame.
4308  */
4309 void libxl__bitmap_copy_best_effort(libxl__gc *gc, libxl_bitmap *dptr,
4310                                     const libxl_bitmap *sptr);
4311 
4312 int libxl__count_physical_sockets(libxl__gc *gc, int *sockets);
4313 
4314 _hidden int libxl__read_sysfs_file_contents(libxl__gc *gc,
4315                                             const char *filename,
4316                                             void **data_r,
4317                                             int *datalen_r);
4318 
4319 #define LIBXL_QEMU_USER_PREFIX "xen-qemuuser"
4320 #define LIBXL_QEMU_USER_BASE   LIBXL_QEMU_USER_PREFIX"-domid"
4321 #define LIBXL_QEMU_USER_SHARED LIBXL_QEMU_USER_PREFIX"-shared"
4322 #define LIBXL_QEMU_USER_RANGE_BASE LIBXL_QEMU_USER_PREFIX"-range-base"
4323 
libxl__acpi_defbool_val(const libxl_domain_build_info * b_info)4324 static inline bool libxl__acpi_defbool_val(const libxl_domain_build_info *b_info)
4325 {
4326     return libxl_defbool_val(b_info->acpi) &&
4327            libxl_defbool_val(b_info->u.hvm.acpi);
4328 }
4329 
4330 void device_add_domain_config(libxl__gc *gc, libxl_domain_config *d_config,
4331                               const struct libxl_device_type *dt, void *type);
4332 
4333 void libxl__device_add_async(libxl__egc *egc, uint32_t domid,
4334                              const struct libxl_device_type *dt, void *type,
4335                              libxl__ao_device *aodev);
4336 int libxl__device_add(libxl__gc *gc, uint32_t domid,
4337                       const struct libxl_device_type *dt, void *type);
4338 
4339 /* Caller is responsible for freeing the memory by calling
4340  * libxl__device_list_free
4341  */
4342 void* libxl__device_list(libxl__gc *gc, const struct libxl_device_type *dt,
4343                          uint32_t domid, int *num);
4344 void libxl__device_list_free(const struct libxl_device_type *dt,
4345                              void *list, int num);
4346 
libxl__timer_mode_is_default(libxl_timer_mode * tm)4347 static inline bool libxl__timer_mode_is_default(libxl_timer_mode *tm)
4348 {
4349     return *tm == LIBXL_TIMER_MODE_DEFAULT;
4350 }
4351 
libxl__string_is_default(char ** s)4352 static inline bool libxl__string_is_default(char **s)
4353 {
4354     return *s == NULL;
4355 }
4356 #endif
4357 
4358 /*
4359  * Local variables:
4360  * mode: C
4361  * c-basic-offset: 4
4362  * indent-tabs-mode: nil
4363  * End:
4364  */
4365