Debian Codegraph @

tools/ocaml/libs/xc/xenctrl_stubs.c

raw
1 /*
2 * Copyright (C) 2006-2007 XenSource Ltd.
3 * Copyright (C) 2008 Citrix Ltd.
4 * Author Vincent Hanquez <[email protected]>
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 #define _XOPEN_SOURCE 600
18 #include <stdlib.h>
19 #include <errno.h>
20
21 #define CAML_NAME_SPACE
22 #include <caml/alloc.h>
23 #include <caml/memory.h>
24 #include <caml/signals.h>
25 #include <caml/fail.h>
26 #include <caml/callback.h>
27
28 #include "xen-caml-compat.h"
29
30 #include <sys/mman.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <inttypes.h>
34
35 #define XC_WANT_COMPAT_MAP_FOREIGN_API
36 #include <xenctrl.h>
37 #include <xenguest.h>
38 #include <xen-tools/common-macros.h>
39
40 #include "mmap_stubs.h"
41
42 static inline xc_interface *xch_of_val(value v)
43 {
44 xc_interface *xch = *(xc_interface **)Data_custom_val(v);
45
46 return xch;
47 }
48
49 static void stub_xenctrl_finalize(value v)
50 {
51 xc_interface *xch = xch_of_val(v);
52
53 xc_interface_close(xch);
54 }
55
56 static struct custom_operations xenctrl_ops = {
57 .identifier = "xenctrl",
58 .finalize = stub_xenctrl_finalize,
59 .compare = custom_compare_default, /* Can't compare */
60 .hash = custom_hash_default, /* Can't hash */
61 .serialize = custom_serialize_default, /* Can't serialize */
62 .deserialize = custom_deserialize_default, /* Can't deserialize */
63 .compare_ext = custom_compare_ext_default, /* Can't compare */
64 };
65
66 #define string_of_option_array(array, index) \
67 ((Field(array, index) == Val_none) ? NULL : String_val(Field(Field(array, index), 0)))
68
69 static void Noreturn failwith_xc(xc_interface *xch)
70 {
71 char error_str[XC_MAX_ERROR_MSG_LEN + 6];
72 if (xch) {
73 const xc_error *error = xc_get_last_error(xch);
74 if (error->code == XC_ERROR_NONE)
75 snprintf(error_str, sizeof(error_str),
76 "%d: %s", errno, strerror(errno));
77 else
78 snprintf(error_str, sizeof(error_str),
79 "%d: %s: %s", error->code,
80 xc_error_code_to_desc(error->code),
81 error->message);
82 } else {
83 snprintf(error_str, sizeof(error_str),
84 "Unable to open XC interface");
85 }
86 caml_raise_with_string(*caml_named_value("xc.error"), error_str);
87 }
88
89 CAMLprim value stub_xc_interface_open(value unit)
90 {
91 CAMLparam1(unit);
92 CAMLlocal1(result);
93 xc_interface *xch;
94
95 result = caml_alloc_custom(&xenctrl_ops, sizeof(xch), 0, 1);
96
97 caml_enter_blocking_section();
98 xch = xc_interface_open(NULL, NULL, 0);
99 caml_leave_blocking_section();
100
101 if ( !xch )
102 failwith_xc(xch);
103
104 *(xc_interface **)Data_custom_val(result) = xch;
105
106 CAMLreturn(result);
107 }
108
109 static void domain_handle_of_uuid_string(xen_domain_handle_t h,
110 const char *uuid)
111 {
112 #define X "%02"SCNx8
113 #define UUID_FMT (X X X X "-" X X "-" X X "-" X X "-" X X X X X X)
114
115 if ( sscanf(uuid, UUID_FMT, &h[0], &h[1], &h[2], &h[3], &h[4],
116 &h[5], &h[6], &h[7], &h[8], &h[9], &h[10], &h[11],
117 &h[12], &h[13], &h[14], &h[15]) != 16 )
118 {
119 char buf[128];
120
121 snprintf(buf, sizeof(buf),
122 "Xc.int_array_of_uuid_string: %s", uuid);
123
124 caml_invalid_argument(buf);
125 }
126
127 #undef X
128 }
129
130 /*
131 * Various fields which are a bitmap in the C ABI are converted to lists of
132 * integers in the Ocaml ABI for more idiomatic handling.
133 */
134 static value c_bitmap_to_ocaml_list
135 /* ! */
136 /*
137 * All calls to this function must be in a form suitable
138 * for xenctrl_abi_check. The parsing there is ad-hoc.
139 */
140 (unsigned int bitmap)
141 {
142 CAMLparam0();
143 CAMLlocal2(list, tmp);
144
145 #if defined(__i386__) || defined(__x86_64__)
146 /*
147 * This check file contains a mixture of stuff, because it is
148 * generated from the whole of this xenctrl_stubs.c file (without
149 * regard to arch ifdefs) and the whole of xenctrl.ml (which does not
150 * have any arch ifdeffery). Currently, there is only x86 and
151 * arch-independent stuff, and there is no facility in the abi-check
152 * script for arch conditionals. So for now we make the checks
153 * effective on x86 only; this will suffice to defend even ARM
154 * because breaking changes to common code will break the build
155 * on x86 and not make it to master. This is a bit of a bodge.
156 */
157 #include "xenctrl_abi_check.h"
158 #endif
159
160 list = tmp = Val_emptylist;
161
162 for ( unsigned int i = 0; bitmap; i++, bitmap >>= 1 )
163 {
164 if ( !(bitmap & 1) )
165 continue;
166
167 tmp = caml_alloc_small(2, Tag_cons);
168 Field(tmp, 0) = Val_int(i);
169 Field(tmp, 1) = list;
170 list = tmp;
171 }
172
173 CAMLreturn(list);
174 }
175
176 static unsigned int ocaml_list_to_c_bitmap(value l)
177 /* ! */
178 /*
179 * All calls to this function must be in a form suitable
180 * for xenctrl_abi_check. The parsing there is ad-hoc.
181 */
182 {
183 unsigned int val = 0;
184
185 for ( ; l != Val_none; l = Field(l, 1) )
186 val |= 1u << Int_val(Field(l, 0));
187
188 return val;
189 }
190
191 CAMLprim value stub_xc_domain_create(value xch_val, value wanted_domid, value config)
192 {
193 CAMLparam3(xch_val, wanted_domid, config);
194 CAMLlocal2(l, arch_domconfig);
195 xc_interface *xch = xch_of_val(xch_val);
196
197 /* Mnemonics for the named fields inside domctl_create_config */
198 #define VAL_SSIDREF Field(config, 0)
199 #define VAL_HANDLE Field(config, 1)
200 #define VAL_FLAGS Field(config, 2)
201 #define VAL_IOMMU_OPTS Field(config, 3)
202 #define VAL_MAX_VCPUS Field(config, 4)
203 #define VAL_MAX_EVTCHN_PORT Field(config, 5)
204 #define VAL_MAX_GRANT_FRAMES Field(config, 6)
205 #define VAL_MAX_MAPTRACK_FRAMES Field(config, 7)
206 #define VAL_MAX_GRANT_VERSION Field(config, 8)
207 #define VAL_ALTP2M_OPTS Field(config, 9)
208 #define VAL_VMTRACE_BUF_KB Field(config, 10)
209 #define VAL_CPUPOOL_ID Field(config, 11)
210 #define VAL_ARCH Field(config, 12)
211
212 uint32_t domid = Int_val(wanted_domid);
213 uint64_t vmtrace_size = Int32_val(VAL_VMTRACE_BUF_KB);
214
215 vmtrace_size = ROUNDUP(vmtrace_size << 10, XC_PAGE_SHIFT);
216 if ( vmtrace_size != (uint32_t)vmtrace_size )
217 caml_invalid_argument("vmtrace_buf_kb");
218
219 int result;
220 struct xen_domctl_createdomain cfg = {
221 .ssidref = Int32_val(VAL_SSIDREF),
222 .max_vcpus = Int_val(VAL_MAX_VCPUS),
223 .max_evtchn_port = Int_val(VAL_MAX_EVTCHN_PORT),
224 .max_grant_frames = Int_val(VAL_MAX_GRANT_FRAMES),
225 .max_maptrack_frames = Int_val(VAL_MAX_MAPTRACK_FRAMES),
226 .grant_opts =
227 XEN_DOMCTL_GRANT_version(Int_val(VAL_MAX_GRANT_VERSION)),
228 .altp2m_opts = Int32_val(VAL_ALTP2M_OPTS),
229 .vmtrace_size = vmtrace_size,
230 .cpupool_id = Int32_val(VAL_CPUPOOL_ID),
231 };
232
233 domain_handle_of_uuid_string(cfg.handle, String_val(VAL_HANDLE));
234
235 cfg.flags = ocaml_list_to_c_bitmap
236 /* ! domain_create_flag CDF_ lc */
237 /* ! XEN_DOMCTL_CDF_ XEN_DOMCTL_CDF_MAX max */
238 (VAL_FLAGS);
239
240 cfg.iommu_opts = ocaml_list_to_c_bitmap
241 /* ! domain_create_iommu_opts IOMMU_ lc */
242 /* ! XEN_DOMCTL_IOMMU_ XEN_DOMCTL_IOMMU_MAX max */
243 (VAL_IOMMU_OPTS);
244
245 arch_domconfig = Field(VAL_ARCH, 0);
246 switch ( Tag_val(VAL_ARCH) )
247 {
248 case 0: /* ARM - nothing to do */
249 caml_failwith("Unhandled: ARM");
250 break;
251
252 case 1: /* X86 - emulation flags in the block */
253 #if defined(__i386__) || defined(__x86_64__)
254
255 /* Quick & dirty check for ABI changes. */
256 BUILD_BUG_ON(sizeof(cfg) != 68);
257
258 /* Mnemonics for the named fields inside xen_x86_arch_domainconfig */
259 #define VAL_EMUL_FLAGS Field(arch_domconfig, 0)
260 #define VAL_MISC_FLAGS Field(arch_domconfig, 1)
261
262 cfg.arch.emulation_flags = ocaml_list_to_c_bitmap
263 /* ! x86_arch_emulation_flags X86_EMU_ none */
264 /* ! XEN_X86_EMU_ XEN_X86_EMU_ALL all */
265 (VAL_EMUL_FLAGS);
266
267 cfg.arch.misc_flags = ocaml_list_to_c_bitmap
268 /* ! x86_arch_misc_flags X86_ none */
269 /* ! XEN_X86_ XEN_X86_MSR_RELAXED all */
270 (VAL_MISC_FLAGS);
271
272 #undef VAL_MISC_FLAGS
273 #undef VAL_EMUL_FLAGS
274
275 #else
276 caml_failwith("Unhandled: x86");
277 #endif
278 break;
279
280 default:
281 caml_failwith("Unhandled domconfig type");
282 }
283
284 #undef VAL_ARCH
285 #undef VAL_CPUPOOL_ID
286 #undef VAL_VMTRACE_BUF_KB
287 #undef VAL_ALTP2M_OPTS
288 #undef VAL_MAX_GRANT_VERSION
289 #undef VAL_MAX_MAPTRACK_FRAMES
290 #undef VAL_MAX_GRANT_FRAMES
291 #undef VAL_MAX_EVTCHN_PORT
292 #undef VAL_MAX_VCPUS
293 #undef VAL_IOMMU_OPTS
294 #undef VAL_FLAGS
295 #undef VAL_HANDLE
296 #undef VAL_SSIDREF
297
298 caml_enter_blocking_section();
299 result = xc_domain_create(xch, &domid, &cfg);
300 caml_leave_blocking_section();
301
302 if (result < 0)
303 failwith_xc(xch);
304
305 CAMLreturn(Val_int(domid));
306 }
307
308 CAMLprim value stub_xc_domain_max_vcpus(value xch_val, value domid,
309 value max_vcpus)
310 {
311 CAMLparam3(xch_val, domid, max_vcpus);
312 xc_interface *xch = xch_of_val(xch_val);
313 int r;
314
315 r = xc_domain_max_vcpus(xch, Int_val(domid), Int_val(max_vcpus));
316 if (r)
317 failwith_xc(xch);
318
319 CAMLreturn(Val_unit);
320 }
321
322
323 value stub_xc_domain_sethandle(value xch_val, value domid, value handle)
324 {
325 CAMLparam3(xch_val, domid, handle);
326 xc_interface *xch = xch_of_val(xch_val);
327 xen_domain_handle_t h;
328 int i;
329
330 domain_handle_of_uuid_string(h, String_val(handle));
331
332 i = xc_domain_sethandle(xch, Int_val(domid), h);
333 if (i)
334 failwith_xc(xch);
335
336 CAMLreturn(Val_unit);
337 }
338
339 static value dom_op(value xch_val, value domid,
340 int (*fn)(xc_interface *, uint32_t))
341 {
342 CAMLparam2(xch_val, domid);
343 xc_interface *xch = xch_of_val(xch_val);
344 int result;
345
346 uint32_t c_domid = Int_val(domid);
347
348 caml_enter_blocking_section();
349 result = fn(xch, c_domid);
350 caml_leave_blocking_section();
351 if (result)
352 failwith_xc(xch);
353 CAMLreturn(Val_unit);
354 }
355
356 CAMLprim value stub_xc_domain_pause(value xch_val, value domid)
357 {
358 return dom_op(xch_val, domid, xc_domain_pause);
359 }
360
361
362 CAMLprim value stub_xc_domain_unpause(value xch_val, value domid)
363 {
364 return dom_op(xch_val, domid, xc_domain_unpause);
365 }
366
367 CAMLprim value stub_xc_domain_destroy(value xch_val, value domid)
368 {
369 return dom_op(xch_val, domid, xc_domain_destroy);
370 }
371
372 CAMLprim value stub_xc_domain_resume_fast(value xch_val, value domid)
373 {
374 CAMLparam2(xch_val, domid);
375 xc_interface *xch = xch_of_val(xch_val);
376 int result;
377
378 uint32_t c_domid = Int_val(domid);
379
380 caml_enter_blocking_section();
381 result = xc_domain_resume(xch, c_domid, 1);
382 caml_leave_blocking_section();
383 if (result)
384 failwith_xc(xch);
385 CAMLreturn(Val_unit);
386 }
387
388 CAMLprim value stub_xc_domain_shutdown(value xch_val, value domid, value reason)
389 {
390 CAMLparam3(xch_val, domid, reason);
391 xc_interface *xch = xch_of_val(xch_val);
392 int ret;
393
394 ret = xc_domain_shutdown(xch, Int_val(domid), Int_val(reason));
395 if (ret < 0)
396 failwith_xc(xch);
397
398 CAMLreturn(Val_unit);
399 }
400
401 static value alloc_domaininfo(xc_domaininfo_t * info)
402 {
403 CAMLparam0();
404 CAMLlocal5(result, tmp, arch_config, x86_arch_config, emul_list);
405 int i;
406
407 result = caml_alloc_tuple(17);
408
409 Store_field(result, 0, Val_int(info->domain));
410 Store_field(result, 1, Val_bool(info->flags & XEN_DOMINF_dying));
411 Store_field(result, 2, Val_bool(info->flags & XEN_DOMINF_shutdown));
412 Store_field(result, 3, Val_bool(info->flags & XEN_DOMINF_paused));
413 Store_field(result, 4, Val_bool(info->flags & XEN_DOMINF_blocked));
414 Store_field(result, 5, Val_bool(info->flags & XEN_DOMINF_running));
415 Store_field(result, 6, Val_bool(info->flags & XEN_DOMINF_hvm_guest));
416 Store_field(result, 7, Val_int((info->flags >> XEN_DOMINF_shutdownshift)
417 & XEN_DOMINF_shutdownmask));
418 Store_field(result, 8, caml_copy_nativeint(info->tot_pages));
419 Store_field(result, 9, caml_copy_nativeint(info->max_pages));
420 Store_field(result, 10, caml_copy_int64(info->shared_info_frame));
421 Store_field(result, 11, caml_copy_int64(info->cpu_time));
422 Store_field(result, 12, Val_int(info->nr_online_vcpus));
423 Store_field(result, 13, Val_int(info->max_vcpu_id));
424 Store_field(result, 14, caml_copy_int32(info->ssidref));
425
426 tmp = caml_alloc_small(16, 0);
427 for (i = 0; i < 16; i++) {
428 Field(tmp, i) = Val_int(info->handle[i]);
429 }
430
431 Store_field(result, 15, tmp);
432
433 #if defined(__i386__) || defined(__x86_64__)
434 /*
435 * emulation_flags: x86_arch_emulation_flags list;
436 */
437 emul_list = c_bitmap_to_ocaml_list
438 /* ! x86_arch_emulation_flags */
439 (info->arch_config.emulation_flags);
440
441 /* xen_x86_arch_domainconfig */
442 x86_arch_config = caml_alloc_tuple(1);
443 Store_field(x86_arch_config, 0, emul_list);
444
445 /* arch_config: arch_domainconfig */
446 arch_config = caml_alloc_small(1, 1);
447
448 Store_field(arch_config, 0, x86_arch_config);
449
450 Store_field(result, 16, arch_config);
451 #endif
452
453 CAMLreturn(result);
454 }
455
456 CAMLprim value stub_xc_domain_getinfolist(value xch_val, value first_domain, value nb)
457 {
458 CAMLparam3(xch_val, first_domain, nb);
459 CAMLlocal2(result, temp);
460 xc_interface *xch = xch_of_val(xch_val);
461 xc_domaininfo_t * info;
462 int i, ret, toalloc, retval;
463 unsigned int c_max_domains;
464 uint32_t c_first_domain;
465
466 /* get the minimum number of allocate byte we need and bump it up to page boundary */
467 toalloc = (sizeof(xc_domaininfo_t) * Int_val(nb)) | 0xfff;
468 ret = posix_memalign((void **) ((void *) &info), 4096, toalloc);
469 if (ret)
470 caml_raise_out_of_memory();
471
472 result = temp = Val_emptylist;
473
474 c_first_domain = Int_val(first_domain);
475 c_max_domains = Int_val(nb);
476 caml_enter_blocking_section();
477 retval = xc_domain_getinfolist(xch, c_first_domain,
478 c_max_domains, info);
479 caml_leave_blocking_section();
480
481 if (retval < 0) {
482 free(info);
483 failwith_xc(xch);
484 }
485 for (i = 0; i < retval; i++) {
486 result = caml_alloc_small(2, Tag_cons);
487 Field(result, 0) = Val_int(0);
488 Field(result, 1) = temp;
489 temp = result;
490
491 Store_field(result, 0, alloc_domaininfo(info + i));
492 }
493
494 free(info);
495 CAMLreturn(result);
496 }
497
498 CAMLprim value stub_xc_domain_getinfo(value xch_val, value domid)
499 {
500 CAMLparam2(xch_val, domid);
501 CAMLlocal1(result);
502 xc_interface *xch = xch_of_val(xch_val);
503 xc_domaininfo_t info;
504 int ret;
505
506 ret = xc_domain_getinfo_single(xch, Int_val(domid), &info);
507 if (ret < 0)
508 failwith_xc(xch);
509
510 result = alloc_domaininfo(&info);
511 CAMLreturn(result);
512 }
513
514 CAMLprim value stub_xc_vcpu_getinfo(value xch_val, value domid, value vcpu)
515 {
516 CAMLparam3(xch_val, domid, vcpu);
517 CAMLlocal1(result);
518 xc_interface *xch = xch_of_val(xch_val);
519 xc_vcpuinfo_t info;
520 int retval;
521
522 uint32_t c_domid = Int_val(domid);
523 uint32_t c_vcpu = Int_val(vcpu);
524 caml_enter_blocking_section();
525 retval = xc_vcpu_getinfo(xch, c_domid, c_vcpu, &info);
526 caml_leave_blocking_section();
527 if (retval < 0)
528 failwith_xc(xch);
529
530 result = caml_alloc_tuple(5);
531 Store_field(result, 0, Val_bool(info.online));
532 Store_field(result, 1, Val_bool(info.blocked));
533 Store_field(result, 2, Val_bool(info.running));
534 Store_field(result, 3, caml_copy_int64(info.cpu_time));
535 Store_field(result, 4, caml_copy_int32(info.cpu));
536
537 CAMLreturn(result);
538 }
539
540 CAMLprim value stub_xc_vcpu_context_get(value xch_val, value domid,
541 value cpu)
542 {
543 CAMLparam3(xch_val, domid, cpu);
544 xc_interface *xch = xch_of_val(xch_val);
545 CAMLlocal1(context);
546 int ret;
547 vcpu_guest_context_any_t ctxt;
548
549 ret = xc_vcpu_getcontext(xch, Int_val(domid), Int_val(cpu), &ctxt);
550 if ( ret < 0 )
551 failwith_xc(xch);
552
553 context = caml_alloc_string(sizeof(ctxt));
554 memcpy((char *) String_val(context), &ctxt.c, sizeof(ctxt.c));
555
556 CAMLreturn(context);
557 }
558
559 static int get_cpumap_len(xc_interface *xch, value cpumap)
560 {
561 int ml_len = Wosize_val(cpumap);
562 int xc_len = xc_get_max_cpus(xch);
563
564 if (ml_len < xc_len)
565 return ml_len;
566 else
567 return xc_len;
568 }
569
570 CAMLprim value stub_xc_vcpu_setaffinity(value xch_val, value domid,
571 value vcpu, value cpumap)
572 {
573 CAMLparam4(xch_val, domid, vcpu, cpumap);
574 xc_interface *xch = xch_of_val(xch_val);
575 int i, len = get_cpumap_len(xch, cpumap);
576 xc_cpumap_t c_cpumap;
577 int retval;
578
579 c_cpumap = xc_cpumap_alloc(xch);
580 if (c_cpumap == NULL)
581 failwith_xc(xch);
582
583 for (i=0; i<len; i++) {
584 if (Bool_val(Field(cpumap, i)))
585 c_cpumap[i/8] |= 1 << (i&7);
586 }
587 retval = xc_vcpu_setaffinity(xch, Int_val(domid),
588 Int_val(vcpu),
589 c_cpumap, NULL,
590 XEN_VCPUAFFINITY_HARD);
591 free(c_cpumap);
592
593 if (retval < 0)
594 failwith_xc(xch);
595 CAMLreturn(Val_unit);
596 }
597
598 CAMLprim value stub_xc_vcpu_getaffinity(value xch_val, value domid,
599 value vcpu)
600 {
601 CAMLparam3(xch_val, domid, vcpu);
602 CAMLlocal1(ret);
603 xc_interface *xch = xch_of_val(xch_val);
604 xc_cpumap_t c_cpumap;
605 int i, len = xc_get_max_cpus(xch);
606 int retval;
607
608 if (len < 1)
609 failwith_xc(xch);
610
611 c_cpumap = xc_cpumap_alloc(xch);
612 if (c_cpumap == NULL)
613 failwith_xc(xch);
614
615 retval = xc_vcpu_getaffinity(xch, Int_val(domid),
616 Int_val(vcpu),
617 c_cpumap, NULL,
618 XEN_VCPUAFFINITY_HARD);
619 if (retval < 0) {
620 free(c_cpumap);
621 failwith_xc(xch);
622 }
623
624 ret = caml_alloc(len, 0);
625
626 for (i=0; i<len; i++) {
627 if (c_cpumap[i/8] & 1 << (i&7))
628 Store_field(ret, i, Val_true);
629 else
630 Store_field(ret, i, Val_false);
631 }
632
633 free(c_cpumap);
634
635 CAMLreturn(ret);
636 }
637
638 CAMLprim value stub_xc_sched_id(value xch_val)
639 {
640 CAMLparam1(xch_val);
641 xc_interface *xch = xch_of_val(xch_val);
642 int sched_id;
643
644 if (xc_sched_id(xch, &sched_id))
645 failwith_xc(xch);
646
647 CAMLreturn(Val_int(sched_id));
648 }
649
650 CAMLprim value stub_xc_evtchn_alloc_unbound(value xch_val,
651 value local_domid,
652 value remote_domid)
653 {
654 CAMLparam3(xch_val, local_domid, remote_domid);
655 xc_interface *xch = xch_of_val(xch_val);
656 int result;
657
658 uint32_t c_local_domid = Int_val(local_domid);
659 uint32_t c_remote_domid = Int_val(remote_domid);
660
661 caml_enter_blocking_section();
662 result = xc_evtchn_alloc_unbound(xch, c_local_domid,
663 c_remote_domid);
664 caml_leave_blocking_section();
665
666 if (result < 0)
667 failwith_xc(xch);
668 CAMLreturn(Val_int(result));
669 }
670
671 CAMLprim value stub_xc_evtchn_reset(value xch_val, value domid)
672 {
673 CAMLparam2(xch_val, domid);
674 xc_interface *xch = xch_of_val(xch_val);
675 int r;
676
677 r = xc_evtchn_reset(xch, Int_val(domid));
678 if (r < 0)
679 failwith_xc(xch);
680 CAMLreturn(Val_unit);
681 }
682
683 CAMLprim value stub_xc_evtchn_status(value xch_val, value domid, value port)
684 {
685 CAMLparam3(xch_val, domid, port);
686 CAMLlocal4(result, result_status, stat, interdomain);
687 xc_interface *xch = xch_of_val(xch_val);
688 xc_evtchn_status_t status = {
689 .dom = Int_val(domid),
690 .port = Int_val(port),
691 };
692 int rc;
693
694 caml_enter_blocking_section();
695 rc = xc_evtchn_status(xch, &status);
696 caml_leave_blocking_section();
697
698 if ( rc < 0 )
699 failwith_xc(xch);
700
701 switch ( status.status )
702 {
703 case EVTCHNSTAT_closed:
704 CAMLreturn(Val_none); /* Early exit, no allocations needed */
705
706 case EVTCHNSTAT_unbound:
707 stat = caml_alloc(1, 0); /* 1st non-constant constructor */
708 Store_field(stat, 0, Val_int(status.u.unbound.dom));
709 break;
710
711 case EVTCHNSTAT_interdomain:
712 interdomain = caml_alloc_tuple(2);
713 Store_field(interdomain, 0, Val_int(status.u.interdomain.dom));
714 Store_field(interdomain, 1, Val_int(status.u.interdomain.port));
715 stat = caml_alloc(1, 1); /* 2nd non-constant constructor */
716 Store_field(stat, 0, interdomain);
717 break;
718
719 case EVTCHNSTAT_pirq:
720 stat = caml_alloc(1, 2); /* 3rd non-constant constructor */
721 Store_field(stat, 0, Val_int(status.u.pirq));
722 break;
723
724 case EVTCHNSTAT_virq:
725 stat = caml_alloc(1, 3); /* 4th non-constant constructor */
726 Store_field(stat, 0, Val_int(status.u.virq));
727 break;
728
729 case EVTCHNSTAT_ipi:
730 stat = Val_int(0); /* 1st constant constructor */
731 break;
732
733 default:
734 caml_failwith("Unknown evtchn status");
735 }
736
737 result_status = caml_alloc_tuple(2);
738 Store_field(result_status, 0, Val_int(status.vcpu));
739 Store_field(result_status, 1, stat);
740
741 result = caml_alloc_some(result_status);
742
743 CAMLreturn(result);
744 }
745
746 CAMLprim value stub_xc_readconsolering(value xch_val)
747 {
748 /* Safe to use outside of blocking sections because of Ocaml GC lock. */
749 static unsigned int conring_size = 16384 + 1;
750
751 unsigned int count = conring_size, size = count, index = 0;
752 char *str = NULL, *ptr;
753 int ret;
754
755 CAMLparam1(xch_val);
756 CAMLlocal1(ring);
757 xc_interface *xch = xch_of_val(xch_val);
758
759 str = malloc(size);
760 if (!str)
761 caml_raise_out_of_memory();
762
763 /* Hopefully our conring_size guess is sufficient */
764 caml_enter_blocking_section();
765 ret = xc_readconsolering(xch, str, &count, 0, 0, &index);
766 caml_leave_blocking_section();
767
768 if (ret < 0) {
769 free(str);
770 failwith_xc(xch);
771 }
772
773 while (count == size && ret >= 0) {
774 size += count - 1;
775 if (size < count)
776 break;
777
778 ptr = realloc(str, size);
779 if (!ptr)
780 break;
781
782 str = ptr + count;
783 count = size - count;
784
785 caml_enter_blocking_section();
786 ret = xc_readconsolering(xch, str, &count, 0, 1, &index);
787 caml_leave_blocking_section();
788
789 count += str - ptr;
790 str = ptr;
791 }
792
793 /*
794 * If we didn't break because of an overflow with size, and we have
795 * needed to realloc() ourself more space, update our tracking of the
796 * real console ring size.
797 */
798 if (size > conring_size)
799 conring_size = size;
800
801 ring = caml_alloc_string(count);
802 memcpy((char *) String_val(ring), str, count);
803 free(str);
804
805 CAMLreturn(ring);
806 }
807
808 CAMLprim value stub_xc_send_debug_keys(value xch_val, value keys)
809 {
810 CAMLparam2(xch_val, keys);
811 xc_interface *xch = xch_of_val(xch_val);
812 int r;
813
814 r = xc_send_debug_keys(xch, String_val(keys));
815 if (r)
816 failwith_xc(xch);
817 CAMLreturn(Val_unit);
818 }
819
820 CAMLprim value physinfo_arch_caps(const xc_physinfo_t *info)
821 {
822 CAMLparam0();
823 CAMLlocal2(arch_cap_flags, arch_obj);
824 int tag = -1;
825
826 #if defined(__arm__) || defined(__aarch64__)
827
828 tag = 0; /* tag ARM */
829
830 arch_obj = caml_alloc_tuple(1);
831
832 Store_field(arch_obj, 0,
833 Val_int(MASK_EXTR(info->arch_capabilities,
834 XEN_SYSCTL_PHYSCAP_ARM_SVE_MASK) * 128));
835
836 #elif defined(__i386__) || defined(__x86_64__)
837
838 tag = 1; /* tag x86 */
839
840 arch_obj = Val_emptylist;
841
842 #endif
843
844 if ( tag < 0 )
845 caml_failwith("Unhandled architecture");
846
847 arch_cap_flags = caml_alloc_small(1, tag);
848 Store_field(arch_cap_flags, 0, arch_obj);
849
850 CAMLreturn(arch_cap_flags);
851 }
852
853 CAMLprim value stub_xc_physinfo(value xch_val)
854 {
855 CAMLparam1(xch_val);
856 CAMLlocal2(physinfo, cap_list);
857 xc_interface *xch = xch_of_val(xch_val);
858 xc_physinfo_t c_physinfo;
859 int r;
860
861 caml_enter_blocking_section();
862 r = xc_physinfo(xch, &c_physinfo);
863 caml_leave_blocking_section();
864
865 if (r)
866 failwith_xc(xch);
867
868 /*
869 * capabilities: physinfo_cap_flag list;
870 */
871 cap_list = c_bitmap_to_ocaml_list
872 /* ! physinfo_cap_flag CAP_ lc */
873 /* ! XEN_SYSCTL_PHYSCAP_ XEN_SYSCTL_PHYSCAP_MAX max */
874 (c_physinfo.capabilities);
875
876 physinfo = caml_alloc_tuple(11);
877 Store_field(physinfo, 0, Val_int(c_physinfo.threads_per_core));
878 Store_field(physinfo, 1, Val_int(c_physinfo.cores_per_socket));
879 Store_field(physinfo, 2, Val_int(c_physinfo.nr_cpus));
880 Store_field(physinfo, 3, Val_int(c_physinfo.max_node_id));
881 Store_field(physinfo, 4, Val_int(c_physinfo.cpu_khz));
882 Store_field(physinfo, 5, caml_copy_nativeint(c_physinfo.total_pages));
883 Store_field(physinfo, 6, caml_copy_nativeint(c_physinfo.free_pages));
884 Store_field(physinfo, 7, caml_copy_nativeint(c_physinfo.scrub_pages));
885 Store_field(physinfo, 8, cap_list);
886 Store_field(physinfo, 9, Val_int(c_physinfo.max_cpu_id + 1));
887 Store_field(physinfo, 10, physinfo_arch_caps(&c_physinfo));
888
889 CAMLreturn(physinfo);
890 }
891
892 CAMLprim value stub_xc_pcpu_info(value xch_val, value nr_cpus)
893 {
894 CAMLparam2(xch_val, nr_cpus);
895 CAMLlocal2(pcpus, v);
896 xc_interface *xch = xch_of_val(xch_val);
897 xc_cpuinfo_t *info;
898 int r, size;
899
900 if (Int_val(nr_cpus) < 1)
901 caml_invalid_argument("nr_cpus");
902
903 info = calloc(Int_val(nr_cpus) + 1, sizeof(*info));
904 if (!info)
905 caml_raise_out_of_memory();
906
907 caml_enter_blocking_section();
908 r = xc_getcpuinfo(xch, Int_val(nr_cpus), info, &size);
909 caml_leave_blocking_section();
910
911 if (r) {
912 free(info);
913 failwith_xc(xch);
914 }
915
916 if (size > 0) {
917 int i;
918 pcpus = caml_alloc(size, 0);
919 for (i = 0; i < size; i++) {
920 v = caml_copy_int64(info[i].idletime);
921 caml_modify(&Field(pcpus, i), v);
922 }
923 } else
924 pcpus = Atom(0);
925 free(info);
926 CAMLreturn(pcpus);
927 }
928
929 CAMLprim value stub_xc_domain_setmaxmem(value xch_val, value domid,
930 value max_memkb)
931 {
932 CAMLparam3(xch_val, domid, max_memkb);
933 xc_interface *xch = xch_of_val(xch_val);
934 int retval;
935
936 uint32_t c_domid = Int_val(domid);
937 unsigned int c_max_memkb = Int64_val(max_memkb);
938 caml_enter_blocking_section();
939 retval = xc_domain_setmaxmem(xch, c_domid, c_max_memkb);
940 caml_leave_blocking_section();
941 if (retval)
942 failwith_xc(xch);
943 CAMLreturn(Val_unit);
944 }
945
946 CAMLprim value stub_xc_domain_set_memmap_limit(value xch_val, value domid,
947 value map_limitkb)
948 {
949 CAMLparam3(xch_val, domid, map_limitkb);
950 xc_interface *xch = xch_of_val(xch_val);
951 unsigned long v;
952 int retval;
953
954 v = Int64_val(map_limitkb);
955 retval = xc_domain_set_memmap_limit(xch, Int_val(domid), v);
956 if (retval)
957 failwith_xc(xch);
958
959 CAMLreturn(Val_unit);
960 }
961
962 CAMLprim value stub_xc_domain_memory_increase_reservation(value xch_val,
963 value domid,
964 value mem_kb)
965 {
966 CAMLparam3(xch_val, domid, mem_kb);
967 xc_interface *xch = xch_of_val(xch_val);
968 int retval;
969
970 unsigned long nr_extents = ((unsigned long)(Int64_val(mem_kb))) >> (XC_PAGE_SHIFT - 10);
971
972 uint32_t c_domid = Int_val(domid);
973 caml_enter_blocking_section();
974 retval = xc_domain_increase_reservation_exact(xch, c_domid,
975 nr_extents, 0, 0, NULL);
976 caml_leave_blocking_section();
977
978 if (retval)
979 failwith_xc(xch);
980 CAMLreturn(Val_unit);
981 }
982
983 CAMLprim value stub_xc_version_version(value xch_val)
984 {
985 CAMLparam1(xch_val);
986 CAMLlocal1(result);
987 xc_interface *xch = xch_of_val(xch_val);
988 char *extra;
989 long packed;
990
991 caml_enter_blocking_section();
992 packed = xc_version(xch, XENVER_version, NULL);
993 caml_leave_blocking_section();
994
995 if (packed < 0)
996 failwith_xc(xch);
997
998 caml_enter_blocking_section();
999 extra = xc_xenver_extraversion(xch);
1000 caml_leave_blocking_section();
1001
1002 if (!extra)
1003 failwith_xc(xch);
1004
1005 result = caml_alloc_tuple(3);
1006
1007 Store_field(result, 0, Val_int(packed >> 16));
1008 Store_field(result, 1, Val_int(packed & 0xffff));
1009 Store_field(result, 2, caml_copy_string(extra));
1010
1011 free(extra);
1012
1013 CAMLreturn(result);
1014 }
1015
1016
1017 CAMLprim value stub_xc_version_compile_info(value xch_val)
1018 {
1019 CAMLparam1(xch_val);
1020 CAMLlocal1(result);
1021 xc_interface *xch = xch_of_val(xch_val);
1022 xen_compile_info_t ci;
1023 int retval;
1024
1025 caml_enter_blocking_section();
1026 retval = xc_version(xch, XENVER_compile_info, &ci);
1027 caml_leave_blocking_section();
1028
1029 if (retval)
1030 failwith_xc(xch);
1031
1032 result = caml_alloc_tuple(4);
1033
1034 Store_field(result, 0, caml_copy_string(ci.compiler));
1035 Store_field(result, 1, caml_copy_string(ci.compile_by));
1036 Store_field(result, 2, caml_copy_string(ci.compile_domain));
1037 Store_field(result, 3, caml_copy_string(ci.compile_date));
1038
1039 CAMLreturn(result);
1040 }
1041
1042
1043 CAMLprim value stub_xc_version_changeset(value xch_val)
1044 {
1045 CAMLparam1(xch_val);
1046 CAMLlocal1(result);
1047 xc_interface *xch = xch_of_val(xch_val);
1048 char *changeset;
1049
1050 caml_enter_blocking_section();
1051 changeset = xc_xenver_changeset(xch);
1052 caml_leave_blocking_section();
1053
1054 if (!changeset)
1055 failwith_xc(xch);
1056
1057 result = caml_copy_string(changeset);
1058
1059 free(changeset);
1060
1061 CAMLreturn(result);
1062 }
1063
1064
1065 CAMLprim value stub_xc_version_capabilities(value xch_val)
1066 {
1067 CAMLparam1(xch_val);
1068 CAMLlocal1(result);
1069 xc_interface *xch = xch_of_val(xch_val);
1070 char *capabilities;
1071
1072 caml_enter_blocking_section();
1073 capabilities = xc_xenver_capabilities(xch);
1074 caml_leave_blocking_section();
1075
1076 if (!capabilities)
1077 failwith_xc(xch);
1078
1079 result = caml_copy_string(capabilities);
1080
1081 free(capabilities);
1082
1083 CAMLreturn(result);
1084 }
1085
1086
1087 CAMLprim value stub_pages_to_kib(value pages)
1088 {
1089 CAMLparam1(pages);
1090
1091 CAMLreturn(caml_copy_int64(Int64_val(pages) << (XC_PAGE_SHIFT - 10)));
1092 }
1093
1094
1095 CAMLprim value stub_map_foreign_range(value xch_val, value dom,
1096 value size, value mfn)
1097 {
1098 CAMLparam4(xch_val, dom, size, mfn);
1099 CAMLlocal1(result);
1100 xc_interface *xch = xch_of_val(xch_val);
1101 struct mmap_interface *intf;
1102 unsigned long c_mfn = Nativeint_val(mfn);
1103 int len = Int_val(size);
1104 void *ptr;
1105
1106 BUILD_BUG_ON((sizeof(struct mmap_interface) % sizeof(value)) != 0);
1107 result = caml_alloc(Wsize_bsize(sizeof(struct mmap_interface)),
1108 Abstract_tag);
1109
1110 caml_enter_blocking_section();
1111 ptr = xc_map_foreign_range(xch, Int_val(dom), len,
1112 PROT_READ|PROT_WRITE, c_mfn);
1113 caml_leave_blocking_section();
1114
1115 if (!ptr)
1116 caml_failwith("xc_map_foreign_range error");
1117
1118 intf = Data_abstract_val(result);
1119 *intf = (struct mmap_interface){ ptr, len };
1120
1121 CAMLreturn(result);
1122 }
1123
1124 CAMLprim value stub_sched_credit_domain_get(value xch_val, value domid)
1125 {
1126 CAMLparam2(xch_val, domid);
1127 CAMLlocal1(sdom);
1128 xc_interface *xch = xch_of_val(xch_val);
1129 struct xen_domctl_sched_credit c_sdom;
1130 int ret;
1131
1132 caml_enter_blocking_section();
1133 ret = xc_sched_credit_domain_get(xch, Int_val(domid), &c_sdom);
1134 caml_leave_blocking_section();
1135 if (ret != 0)
1136 failwith_xc(xch);
1137
1138 sdom = caml_alloc_tuple(2);
1139 Store_field(sdom, 0, Val_int(c_sdom.weight));
1140 Store_field(sdom, 1, Val_int(c_sdom.cap));
1141
1142 CAMLreturn(sdom);
1143 }
1144
1145 CAMLprim value stub_sched_credit_domain_set(value xch_val, value domid,
1146 value sdom)
1147 {
1148 CAMLparam3(xch_val, domid, sdom);
1149 xc_interface *xch = xch_of_val(xch_val);
1150 struct xen_domctl_sched_credit c_sdom;
1151 int ret;
1152
1153 c_sdom.weight = Int_val(Field(sdom, 0));
1154 c_sdom.cap = Int_val(Field(sdom, 1));
1155 caml_enter_blocking_section();
1156 ret = xc_sched_credit_domain_set(xch, Int_val(domid), &c_sdom);
1157 caml_leave_blocking_section();
1158 if (ret != 0)
1159 failwith_xc(xch);
1160
1161 CAMLreturn(Val_unit);
1162 }
1163
1164 CAMLprim value stub_shadow_allocation_get(value xch_val, value domid)
1165 {
1166 CAMLparam2(xch_val, domid);
1167 CAMLlocal1(mb);
1168 xc_interface *xch = xch_of_val(xch_val);
1169 unsigned int c_mb;
1170 int ret;
1171
1172 caml_enter_blocking_section();
1173 ret = xc_shadow_control(xch, Int_val(domid),
1174 XEN_DOMCTL_SHADOW_OP_GET_ALLOCATION,
1175 &c_mb, 0);
1176 caml_leave_blocking_section();
1177 if (ret != 0)
1178 failwith_xc(xch);
1179
1180 mb = Val_int(c_mb);
1181 CAMLreturn(mb);
1182 }
1183
1184 CAMLprim value stub_shadow_allocation_set(value xch_val, value domid,
1185 value mb)
1186 {
1187 CAMLparam3(xch_val, domid, mb);
1188 xc_interface *xch = xch_of_val(xch_val);
1189 unsigned int c_mb;
1190 int ret;
1191
1192 c_mb = Int_val(mb);
1193 caml_enter_blocking_section();
1194 ret = xc_shadow_control(xch, Int_val(domid),
1195 XEN_DOMCTL_SHADOW_OP_SET_ALLOCATION,
1196 &c_mb, 0);
1197 caml_leave_blocking_section();
1198 if (ret != 0)
1199 failwith_xc(xch);
1200
1201 CAMLreturn(Val_unit);
1202 }
1203
1204 CAMLprim value stub_xc_domain_ioport_permission(value xch_val, value domid,
1205 value start_port, value nr_ports,
1206 value allow)
1207 {
1208 CAMLparam5(xch_val, domid, start_port, nr_ports, allow);
1209 xc_interface *xch = xch_of_val(xch_val);
1210 uint32_t c_start_port, c_nr_ports;
1211 uint8_t c_allow;
1212 int ret;
1213
1214 c_start_port = Int_val(start_port);
1215 c_nr_ports = Int_val(nr_ports);
1216 c_allow = Bool_val(allow);
1217
1218 ret = xc_domain_ioport_permission(xch, Int_val(domid),
1219 c_start_port, c_nr_ports, c_allow);
1220 if (ret < 0)
1221 failwith_xc(xch);
1222
1223 CAMLreturn(Val_unit);
1224 }
1225
1226 CAMLprim value stub_xc_domain_iomem_permission(value xch_val, value domid,
1227 value start_pfn, value nr_pfns,
1228 value allow)
1229 {
1230 CAMLparam5(xch_val, domid, start_pfn, nr_pfns, allow);
1231 xc_interface *xch = xch_of_val(xch_val);
1232 unsigned long c_start_pfn, c_nr_pfns;
1233 uint8_t c_allow;
1234 int ret;
1235
1236 c_start_pfn = Nativeint_val(start_pfn);
1237 c_nr_pfns = Nativeint_val(nr_pfns);
1238 c_allow = Bool_val(allow);
1239
1240 ret = xc_domain_iomem_permission(xch, Int_val(domid),
1241 c_start_pfn, c_nr_pfns, c_allow);
1242 if (ret < 0)
1243 failwith_xc(xch);
1244
1245 CAMLreturn(Val_unit);
1246 }
1247
1248 CAMLprim value stub_xc_domain_irq_permission(value xch_val, value domid,
1249 value pirq, value allow)
1250 {
1251 CAMLparam4(xch_val, domid, pirq, allow);
1252 xc_interface *xch = xch_of_val(xch_val);
1253 uint32_t c_pirq;
1254 bool c_allow;
1255 int ret;
1256
1257 c_pirq = Int_val(pirq);
1258 c_allow = Bool_val(allow);
1259
1260 ret = xc_domain_irq_permission(xch, Int_val(domid),
1261 c_pirq, c_allow);
1262 if (ret < 0)
1263 failwith_xc(xch);
1264
1265 CAMLreturn(Val_unit);
1266 }
1267
1268 CAMLprim value stub_xc_hvm_param_get(value xch_val, value domid, value param)
1269 {
1270 CAMLparam3(xch_val, domid, param);
1271 xc_interface *xch = xch_of_val(xch_val);
1272 uint64_t val;
1273 int ret;
1274
1275 caml_enter_blocking_section();
1276 ret = xc_hvm_param_get(xch, Int_val(domid), Int_val(param), &val);
1277 caml_leave_blocking_section();
1278
1279 if ( ret )
1280 failwith_xc(xch);
1281
1282 CAMLreturn(caml_copy_int64(val));
1283 }
1284
1285 CAMLprim value stub_xc_hvm_param_set(value xch_val, value domid, value param, value val)
1286 {
1287 CAMLparam4(xch_val, domid, param, val);
1288 xc_interface *xch = xch_of_val(xch_val);
1289 uint64_t val64 = Int64_val(val);
1290 int ret;
1291
1292 caml_enter_blocking_section();
1293 ret = xc_hvm_param_set(xch, Int_val(domid), Int_val(param), val64);
1294 caml_leave_blocking_section();
1295
1296 if ( ret )
1297 failwith_xc(xch);
1298
1299 CAMLreturn(Val_unit);
1300 }
1301
1302 static uint32_t encode_sbdf(int domain, int bus, int dev, int func)
1303 {
1304 return ((uint32_t)domain & 0xffff) << 16 |
1305 ((uint32_t)bus & 0xff) << 8 |
1306 ((uint32_t)dev & 0x1f) << 3 |
1307 ((uint32_t)func & 0x7);
1308 }
1309
1310 CAMLprim value stub_xc_domain_test_assign_device(value xch_val, value domid, value desc)
1311 {
1312 CAMLparam3(xch_val, domid, desc);
1313 xc_interface *xch = xch_of_val(xch_val);
1314 int ret;
1315 int domain, bus, dev, func;
1316 uint32_t sbdf;
1317
1318 domain = Int_val(Field(desc, 0));
1319 bus = Int_val(Field(desc, 1));
1320 dev = Int_val(Field(desc, 2));
1321 func = Int_val(Field(desc, 3));
1322 sbdf = encode_sbdf(domain, bus, dev, func);
1323
1324 ret = xc_test_assign_device(xch, Int_val(domid), sbdf);
1325
1326 CAMLreturn(Val_bool(ret == 0));
1327 }
1328
1329 CAMLprim value stub_xc_domain_assign_device(value xch_val, value domid, value desc)
1330 {
1331 CAMLparam3(xch_val, domid, desc);
1332 xc_interface *xch = xch_of_val(xch_val);
1333 int ret;
1334 int domain, bus, dev, func;
1335 uint32_t sbdf;
1336
1337 domain = Int_val(Field(desc, 0));
1338 bus = Int_val(Field(desc, 1));
1339 dev = Int_val(Field(desc, 2));
1340 func = Int_val(Field(desc, 3));
1341 sbdf = encode_sbdf(domain, bus, dev, func);
1342
1343 ret = xc_assign_device(xch, Int_val(domid), sbdf,
1344 XEN_DOMCTL_DEV_RDM_RELAXED);
1345
1346 if (ret < 0)
1347 failwith_xc(xch);
1348 CAMLreturn(Val_unit);
1349 }
1350
1351 CAMLprim value stub_xc_domain_deassign_device(value xch_val, value domid, value desc)
1352 {
1353 CAMLparam3(xch_val, domid, desc);
1354 xc_interface *xch = xch_of_val(xch_val);
1355 int ret;
1356 int domain, bus, dev, func;
1357 uint32_t sbdf;
1358
1359 domain = Int_val(Field(desc, 0));
1360 bus = Int_val(Field(desc, 1));
1361 dev = Int_val(Field(desc, 2));
1362 func = Int_val(Field(desc, 3));
1363 sbdf = encode_sbdf(domain, bus, dev, func);
1364
1365 ret = xc_deassign_device(xch, Int_val(domid), sbdf);
1366
1367 if (ret < 0)
1368 failwith_xc(xch);
1369 CAMLreturn(Val_unit);
1370 }
1371
1372 CAMLprim value stub_xc_get_cpu_featureset(value xch_val, value idx)
1373 {
1374 CAMLparam2(xch_val, idx);
1375 CAMLlocal1(bitmap_val);
1376 #if defined(__i386__) || defined(__x86_64__)
1377 xc_interface *xch = xch_of_val(xch_val);
1378
1379 /* Safe, because of the global ocaml lock. */
1380 static uint32_t fs_len;
1381
1382 if (fs_len == 0)
1383 {
1384 int ret = xc_get_cpu_featureset(xch, 0, &fs_len, NULL);
1385
1386 if (ret || (fs_len == 0))
1387 failwith_xc(xch);
1388 }
1389
1390 {
1391 /* To/from hypervisor to retrieve actual featureset */
1392 uint32_t fs[fs_len], len = fs_len;
1393 unsigned int i;
1394
1395 int ret = xc_get_cpu_featureset(xch, Int_val(idx), &len, fs);
1396
1397 if (ret)
1398 failwith_xc(xch);
1399
1400 bitmap_val = caml_alloc(len, 0);
1401
1402 for (i = 0; i < len; ++i)
1403 Store_field(bitmap_val, i, caml_copy_int64(fs[i]));
1404 }
1405 #else
1406 caml_failwith("xc_get_cpu_featureset: not implemented");
1407 #endif
1408 CAMLreturn(bitmap_val);
1409 }
1410
1411 CAMLprim value stub_xc_watchdog(value xch_val, value domid, value timeout)
1412 {
1413 CAMLparam3(xch_val, domid, timeout);
1414 xc_interface *xch = xch_of_val(xch_val);
1415 int ret;
1416 unsigned int c_timeout = Int32_val(timeout);
1417
1418 ret = xc_watchdog(xch, Int_val(domid), c_timeout);
1419 if (ret < 0)
1420 failwith_xc(xch);
1421
1422 CAMLreturn(Val_int(ret));
1423 }
1424
1425 /*
1426 * Local variables:
1427 * indent-tabs-mode: t
1428 * c-basic-offset: 8
1429 * tab-width: 8
1430 * End:
1431 */