ia64/xen-unstable

view tools/python/xen/lowlevel/xc/xc.c @ 16187:dc2ff26bbdf6

ia64: Fix ia64 PV domain creation with new vhpt-size parameter.
Signed-off-by: Kouya Shimura <kouya@jp.fujitsu.com>
author Keir Fraser <keir@xensource.com>
date Mon Oct 22 14:21:13 2007 +0100 (2007-10-22)
parents 7231d971f78c
children 24c2745f939f
line source
1 /******************************************************************************
2 * Xc.c
3 *
4 * Copyright (c) 2003-2004, K A Fraser (University of Cambridge)
5 */
7 #include <Python.h>
8 #include <xenctrl.h>
9 #include <xenguest.h>
10 #include <zlib.h>
11 #include <fcntl.h>
12 #include <netinet/in.h>
13 #include <netinet/tcp.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <sys/mman.h>
17 #include <netdb.h>
18 #include <arpa/inet.h>
20 #include "xenctrl.h"
21 #include <xen/elfnote.h>
22 #include "xc_dom.h"
23 #include <xen/hvm/hvm_info_table.h>
24 #include <xen/hvm/params.h>
26 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
28 /* Needed for Python versions earlier than 2.3. */
29 #ifndef PyMODINIT_FUNC
30 #define PyMODINIT_FUNC DL_EXPORT(void)
31 #endif
33 #define PKG "xen.lowlevel.xc"
34 #define CLS "xc"
36 static PyObject *xc_error_obj, *zero;
38 typedef struct {
39 PyObject_HEAD;
40 int xc_handle;
41 } XcObject;
44 static PyObject *dom_op(XcObject *self, PyObject *args,
45 int (*fn)(int, uint32_t));
47 static PyObject *pyxc_error_to_exception(void)
48 {
49 PyObject *pyerr;
50 const xc_error *err = xc_get_last_error();
51 const char *desc = xc_error_code_to_desc(err->code);
53 if ( err->code == XC_ERROR_NONE )
54 return PyErr_SetFromErrno(xc_error_obj);
56 if ( err->message[0] != '\0' )
57 pyerr = Py_BuildValue("(iss)", err->code, desc, err->message);
58 else
59 pyerr = Py_BuildValue("(is)", err->code, desc);
61 xc_clear_last_error();
63 if ( pyerr != NULL )
64 {
65 PyErr_SetObject(xc_error_obj, pyerr);
66 Py_DECREF(pyerr);
67 }
69 return NULL;
70 }
72 static PyObject *pyxc_domain_dumpcore(XcObject *self, PyObject *args)
73 {
74 uint32_t dom;
75 char *corefile;
77 if ( !PyArg_ParseTuple(args, "is", &dom, &corefile) )
78 return NULL;
80 if ( (corefile == NULL) || (corefile[0] == '\0') )
81 return NULL;
83 if ( xc_domain_dumpcore(self->xc_handle, dom, corefile) != 0 )
84 return pyxc_error_to_exception();
86 Py_INCREF(zero);
87 return zero;
88 }
90 static PyObject *pyxc_handle(XcObject *self)
91 {
92 return PyInt_FromLong(self->xc_handle);
93 }
95 static PyObject *pyxc_domain_create(XcObject *self,
96 PyObject *args,
97 PyObject *kwds)
98 {
99 uint32_t dom = 0, ssidref = 0, flags = 0;
100 int ret, i, hvm = 0;
101 PyObject *pyhandle = NULL;
102 xen_domain_handle_t handle = {
103 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
104 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef };
106 static char *kwd_list[] = { "domid", "ssidref", "handle", "hvm", NULL };
108 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|iiOi", kwd_list,
109 &dom, &ssidref, &pyhandle, &hvm))
110 return NULL;
112 if ( pyhandle != NULL )
113 {
114 if ( !PyList_Check(pyhandle) ||
115 (PyList_Size(pyhandle) != sizeof(xen_domain_handle_t)) )
116 goto out_exception;
118 for ( i = 0; i < sizeof(xen_domain_handle_t); i++ )
119 {
120 PyObject *p = PyList_GetItem(pyhandle, i);
121 if ( !PyInt_Check(p) )
122 goto out_exception;
123 handle[i] = (uint8_t)PyInt_AsLong(p);
124 }
125 }
127 if ( hvm )
128 flags |= XEN_DOMCTL_CDF_hvm_guest;
130 if ( (ret = xc_domain_create(self->xc_handle, ssidref,
131 handle, flags, &dom)) < 0 )
132 return pyxc_error_to_exception();
134 return PyInt_FromLong(dom);
136 out_exception:
137 errno = EINVAL;
138 PyErr_SetFromErrno(xc_error_obj);
139 return NULL;
140 }
142 static PyObject *pyxc_domain_max_vcpus(XcObject *self, PyObject *args)
143 {
144 uint32_t dom, max;
146 if (!PyArg_ParseTuple(args, "ii", &dom, &max))
147 return NULL;
149 if (xc_domain_max_vcpus(self->xc_handle, dom, max) != 0)
150 return pyxc_error_to_exception();
152 Py_INCREF(zero);
153 return zero;
154 }
156 static PyObject *pyxc_domain_pause(XcObject *self, PyObject *args)
157 {
158 return dom_op(self, args, xc_domain_pause);
159 }
161 static PyObject *pyxc_domain_unpause(XcObject *self, PyObject *args)
162 {
163 return dom_op(self, args, xc_domain_unpause);
164 }
166 static PyObject *pyxc_domain_destroy_hook(XcObject *self, PyObject *args)
167 {
168 #ifdef __ia64__
169 dom_op(self, args, xc_ia64_save_to_nvram);
170 #endif
172 Py_INCREF(zero);
173 return zero;
174 }
176 static PyObject *pyxc_domain_destroy(XcObject *self, PyObject *args)
177 {
178 return dom_op(self, args, xc_domain_destroy);
179 }
181 static PyObject *pyxc_domain_shutdown(XcObject *self, PyObject *args)
182 {
183 uint32_t dom, reason;
185 if ( !PyArg_ParseTuple(args, "ii", &dom, &reason) )
186 return NULL;
188 if ( xc_domain_shutdown(self->xc_handle, dom, reason) != 0 )
189 return pyxc_error_to_exception();
191 Py_INCREF(zero);
192 return zero;
193 }
195 static PyObject *pyxc_domain_resume(XcObject *self, PyObject *args)
196 {
197 uint32_t dom;
198 int fast;
200 if ( !PyArg_ParseTuple(args, "ii", &dom, &fast) )
201 return NULL;
203 if ( xc_domain_resume(self->xc_handle, dom, fast) != 0 )
204 return pyxc_error_to_exception();
206 Py_INCREF(zero);
207 return zero;
208 }
210 static PyObject *pyxc_vcpu_setaffinity(XcObject *self,
211 PyObject *args,
212 PyObject *kwds)
213 {
214 uint32_t dom;
215 int vcpu = 0, i;
216 uint64_t cpumap = ~0ULL;
217 PyObject *cpulist = NULL;
219 static char *kwd_list[] = { "domid", "vcpu", "cpumap", NULL };
221 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|iO", kwd_list,
222 &dom, &vcpu, &cpulist) )
223 return NULL;
225 if ( (cpulist != NULL) && PyList_Check(cpulist) )
226 {
227 cpumap = 0ULL;
228 for ( i = 0; i < PyList_Size(cpulist); i++ )
229 cpumap |= (uint64_t)1 << PyInt_AsLong(PyList_GetItem(cpulist, i));
230 }
232 if ( xc_vcpu_setaffinity(self->xc_handle, dom, vcpu, cpumap) != 0 )
233 return pyxc_error_to_exception();
235 Py_INCREF(zero);
236 return zero;
237 }
239 static PyObject *pyxc_domain_setcpuweight(XcObject *self,
240 PyObject *args,
241 PyObject *kwds)
242 {
243 uint32_t dom;
244 float cpuweight = 1;
246 static char *kwd_list[] = { "domid", "cpuweight", NULL };
248 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|f", kwd_list,
249 &dom, &cpuweight) )
250 return NULL;
252 if ( xc_domain_setcpuweight(self->xc_handle, dom, cpuweight) != 0 )
253 return pyxc_error_to_exception();
255 Py_INCREF(zero);
256 return zero;
257 }
259 static PyObject *pyxc_domain_sethandle(XcObject *self, PyObject *args)
260 {
261 int i;
262 uint32_t dom;
263 PyObject *pyhandle;
264 xen_domain_handle_t handle;
266 if (!PyArg_ParseTuple(args, "iO", &dom, &pyhandle))
267 return NULL;
269 if ( !PyList_Check(pyhandle) ||
270 (PyList_Size(pyhandle) != sizeof(xen_domain_handle_t)) )
271 {
272 goto out_exception;
273 }
275 for ( i = 0; i < sizeof(xen_domain_handle_t); i++ )
276 {
277 PyObject *p = PyList_GetItem(pyhandle, i);
278 if ( !PyInt_Check(p) )
279 goto out_exception;
280 handle[i] = (uint8_t)PyInt_AsLong(p);
281 }
283 if (xc_domain_sethandle(self->xc_handle, dom, handle) < 0)
284 return pyxc_error_to_exception();
286 Py_INCREF(zero);
287 return zero;
289 out_exception:
290 PyErr_SetFromErrno(xc_error_obj);
291 return NULL;
292 }
295 static PyObject *pyxc_domain_getinfo(XcObject *self,
296 PyObject *args,
297 PyObject *kwds)
298 {
299 PyObject *list, *info_dict, *pyhandle;
301 uint32_t first_dom = 0;
302 int max_doms = 1024, nr_doms, i, j;
303 xc_dominfo_t *info;
305 static char *kwd_list[] = { "first_dom", "max_doms", NULL };
307 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|ii", kwd_list,
308 &first_dom, &max_doms) )
309 return NULL;
311 if ( (info = malloc(max_doms * sizeof(xc_dominfo_t))) == NULL )
312 return PyErr_NoMemory();
314 nr_doms = xc_domain_getinfo(self->xc_handle, first_dom, max_doms, info);
316 if (nr_doms < 0)
317 {
318 free(info);
319 return pyxc_error_to_exception();
320 }
322 list = PyList_New(nr_doms);
323 for ( i = 0 ; i < nr_doms; i++ )
324 {
325 info_dict = Py_BuildValue(
326 "{s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i"
327 ",s:L,s:L,s:L,s:i,s:i}",
328 "domid", (int)info[i].domid,
329 "online_vcpus", info[i].nr_online_vcpus,
330 "max_vcpu_id", info[i].max_vcpu_id,
331 "hvm", info[i].hvm,
332 "dying", info[i].dying,
333 "crashed", info[i].crashed,
334 "shutdown", info[i].shutdown,
335 "paused", info[i].paused,
336 "blocked", info[i].blocked,
337 "running", info[i].running,
338 "mem_kb", (long long)info[i].nr_pages*(XC_PAGE_SIZE/1024),
339 "cpu_time", (long long)info[i].cpu_time,
340 "maxmem_kb", (long long)info[i].max_memkb,
341 "ssidref", (int)info[i].ssidref,
342 "shutdown_reason", info[i].shutdown_reason);
343 pyhandle = PyList_New(sizeof(xen_domain_handle_t));
344 if ( (pyhandle == NULL) || (info_dict == NULL) )
345 {
346 Py_DECREF(list);
347 if ( pyhandle != NULL ) { Py_DECREF(pyhandle); }
348 if ( info_dict != NULL ) { Py_DECREF(info_dict); }
349 free(info);
350 return NULL;
351 }
352 for ( j = 0; j < sizeof(xen_domain_handle_t); j++ )
353 PyList_SetItem(pyhandle, j, PyInt_FromLong(info[i].handle[j]));
354 PyDict_SetItemString(info_dict, "handle", pyhandle);
355 Py_DECREF(pyhandle);
356 PyList_SetItem(list, i, info_dict);
357 }
359 free(info);
361 return list;
362 }
364 static PyObject *pyxc_vcpu_getinfo(XcObject *self,
365 PyObject *args,
366 PyObject *kwds)
367 {
368 PyObject *info_dict, *cpulist;
370 uint32_t dom, vcpu = 0;
371 xc_vcpuinfo_t info;
372 int rc, i;
373 uint64_t cpumap;
375 static char *kwd_list[] = { "domid", "vcpu", NULL };
377 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
378 &dom, &vcpu) )
379 return NULL;
381 rc = xc_vcpu_getinfo(self->xc_handle, dom, vcpu, &info);
382 if ( rc < 0 )
383 return pyxc_error_to_exception();
384 rc = xc_vcpu_getaffinity(self->xc_handle, dom, vcpu, &cpumap);
385 if ( rc < 0 )
386 return pyxc_error_to_exception();
388 info_dict = Py_BuildValue("{s:i,s:i,s:i,s:L,s:i}",
389 "online", info.online,
390 "blocked", info.blocked,
391 "running", info.running,
392 "cpu_time", info.cpu_time,
393 "cpu", info.cpu);
395 cpulist = PyList_New(0);
396 for ( i = 0; cpumap != 0; i++ )
397 {
398 if ( cpumap & 1 )
399 PyList_Append(cpulist, PyInt_FromLong(i));
400 cpumap >>= 1;
401 }
402 PyDict_SetItemString(info_dict, "cpumap", cpulist);
403 Py_DECREF(cpulist);
404 return info_dict;
405 }
407 static PyObject *pyxc_linux_build(XcObject *self,
408 PyObject *args,
409 PyObject *kwds)
410 {
411 uint32_t domid;
412 struct xc_dom_image *dom;
413 char *image, *ramdisk = NULL, *cmdline = "", *features = NULL;
414 int flags = 0;
415 int store_evtchn, console_evtchn;
416 int vhpt = 0;
417 unsigned int mem_mb;
418 unsigned long store_mfn = 0;
419 unsigned long console_mfn = 0;
420 PyObject* elfnote_dict;
421 PyObject* elfnote = NULL;
422 PyObject* ret;
423 int i;
425 static char *kwd_list[] = { "domid", "store_evtchn", "memsize",
426 "console_evtchn", "image",
427 /* optional */
428 "ramdisk", "cmdline", "flags",
429 "features", "vhpt", NULL };
431 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiis|ssisi", kwd_list,
432 &domid, &store_evtchn, &mem_mb,
433 &console_evtchn, &image,
434 /* optional */
435 &ramdisk, &cmdline, &flags,
436 &features, &vhpt) )
437 return NULL;
439 xc_dom_loginit();
440 if (!(dom = xc_dom_allocate(cmdline, features)))
441 return pyxc_error_to_exception();
443 /* for IA64 */
444 dom->vhpt_size_log2 = vhpt;
446 if ( xc_dom_linux_build(self->xc_handle, dom, domid, mem_mb, image,
447 ramdisk, flags, store_evtchn, &store_mfn,
448 console_evtchn, &console_mfn) != 0 ) {
449 goto out;
450 }
452 if ( !(elfnote_dict = PyDict_New()) )
453 goto out;
455 for ( i = 0; i < ARRAY_SIZE(dom->parms.elf_notes); i++ )
456 {
457 switch ( dom->parms.elf_notes[i].type )
458 {
459 case XEN_ENT_NONE:
460 continue;
461 case XEN_ENT_LONG:
462 elfnote = Py_BuildValue("k", dom->parms.elf_notes[i].data.num);
463 break;
464 case XEN_ENT_STR:
465 elfnote = Py_BuildValue("s", dom->parms.elf_notes[i].data.str);
466 break;
467 }
468 PyDict_SetItemString(elfnote_dict,
469 dom->parms.elf_notes[i].name,
470 elfnote);
471 Py_DECREF(elfnote);
472 }
474 ret = Py_BuildValue("{s:i,s:i,s:N}",
475 "store_mfn", store_mfn,
476 "console_mfn", console_mfn,
477 "notes", elfnote_dict);
479 if ( dom->arch_hooks->native_protocol )
480 {
481 PyObject *native_protocol =
482 Py_BuildValue("s", dom->arch_hooks->native_protocol);
483 PyDict_SetItemString(ret, "native_protocol", native_protocol);
484 Py_DECREF(native_protocol);
485 }
487 xc_dom_release(dom);
489 return ret;
491 out:
492 xc_dom_release(dom);
493 return pyxc_error_to_exception();
494 }
496 static PyObject *pyxc_get_hvm_param(XcObject *self,
497 PyObject *args,
498 PyObject *kwds)
499 {
500 uint32_t dom;
501 int param;
502 unsigned long value;
504 static char *kwd_list[] = { "domid", "param", NULL };
505 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwd_list,
506 &dom, &param) )
507 return NULL;
509 if ( xc_get_hvm_param(self->xc_handle, dom, param, &value) != 0 )
510 return pyxc_error_to_exception();
512 return PyLong_FromUnsignedLong(value);
514 }
516 static PyObject *pyxc_set_hvm_param(XcObject *self,
517 PyObject *args,
518 PyObject *kwds)
519 {
520 uint32_t dom;
521 int param;
522 uint64_t value;
524 static char *kwd_list[] = { "domid", "param", "value", NULL };
525 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiL", kwd_list,
526 &dom, &param, &value) )
527 return NULL;
529 if ( xc_set_hvm_param(self->xc_handle, dom, param, value) != 0 )
530 return pyxc_error_to_exception();
532 Py_INCREF(zero);
533 return zero;
534 }
536 static int token_value(char *token)
537 {
538 token = strchr(token, 'x') + 1;
539 return strtol(token, NULL, 16);
540 }
542 static int next_bdf(char **str, int *seg, int *bus, int *dev, int *func)
543 {
544 char *token;
546 token = strchr(*str, ',');
547 if ( !token )
548 return 0;
549 token++;
551 *seg = token_value(token);
552 token = strchr(token, ',') + 1;
553 *bus = token_value(token);
554 token = strchr(token, ',') + 1;
555 *dev = token_value(token);
556 token = strchr(token, ',') + 1;
557 *func = token_value(token);
559 *str = token;
560 return 1;
561 }
563 static PyObject *pyxc_assign_device(XcObject *self,
564 PyObject *args,
565 PyObject *kwds)
566 {
567 uint32_t dom;
568 char *pci_str;
569 uint32_t bdf = 0;
570 int seg, bus, dev, func;
572 static char *kwd_list[] = { "domid", "pci", NULL };
573 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "is", kwd_list,
574 &dom, &pci_str) )
575 return NULL;
577 while ( next_bdf(&pci_str, &seg, &bus, &dev, &func) )
578 {
579 bdf |= (bus & 0xff) << 16;
580 bdf |= (dev & 0x1f) << 11;
581 bdf |= (func & 0x7) << 8;
583 if ( xc_assign_device(self->xc_handle, dom, bdf) != 0 )
584 break;
586 bdf = 0;
587 }
589 return Py_BuildValue("i", bdf);
590 }
592 #ifdef __ia64__
593 static PyObject *pyxc_nvram_init(XcObject *self,
594 PyObject *args)
595 {
596 char *dom_name;
597 uint32_t dom;
599 if ( !PyArg_ParseTuple(args, "si", &dom_name, &dom) )
600 return NULL;
602 xc_ia64_nvram_init(self->xc_handle, dom_name, dom);
604 Py_INCREF(zero);
605 return zero;
606 }
607 #endif /* __ia64__ */
609 static PyObject *pyxc_hvm_build(XcObject *self,
610 PyObject *args,
611 PyObject *kwds)
612 {
613 uint32_t dom;
614 #if !defined(__ia64__)
615 struct hvm_info_table *va_hvm;
616 uint8_t *va_map, sum;
617 int i;
618 #endif
619 char *image;
620 int memsize, vcpus = 1, acpi = 0, apic = 1;
622 static char *kwd_list[] = { "domid",
623 "memsize", "image", "vcpus", "acpi",
624 "apic", NULL };
625 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iis|iii", kwd_list,
626 &dom, &memsize,
627 &image, &vcpus, &acpi, &apic) )
628 return NULL;
630 if ( xc_hvm_build(self->xc_handle, dom, memsize, image) != 0 )
631 return pyxc_error_to_exception();
633 #if !defined(__ia64__)
634 /* Set up the HVM info table. */
635 va_map = xc_map_foreign_range(self->xc_handle, dom, XC_PAGE_SIZE,
636 PROT_READ | PROT_WRITE,
637 HVM_INFO_PFN);
638 if ( va_map == NULL )
639 return PyErr_SetFromErrno(xc_error_obj);
640 va_hvm = (struct hvm_info_table *)(va_map + HVM_INFO_OFFSET);
641 memset(va_hvm, 0, sizeof(*va_hvm));
642 strncpy(va_hvm->signature, "HVM INFO", 8);
643 va_hvm->length = sizeof(struct hvm_info_table);
644 va_hvm->acpi_enabled = acpi;
645 va_hvm->apic_mode = apic;
646 va_hvm->nr_vcpus = vcpus;
647 for ( i = 0, sum = 0; i < va_hvm->length; i++ )
648 sum += ((uint8_t *)va_hvm)[i];
649 va_hvm->checksum = -sum;
650 munmap(va_map, XC_PAGE_SIZE);
651 #endif
653 return Py_BuildValue("{}");
654 }
656 static PyObject *pyxc_evtchn_alloc_unbound(XcObject *self,
657 PyObject *args,
658 PyObject *kwds)
659 {
660 uint32_t dom, remote_dom;
661 int port;
663 static char *kwd_list[] = { "domid", "remote_dom", NULL };
665 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwd_list,
666 &dom, &remote_dom) )
667 return NULL;
669 if ( (port = xc_evtchn_alloc_unbound(self->xc_handle, dom, remote_dom)) < 0 )
670 return pyxc_error_to_exception();
672 return PyInt_FromLong(port);
673 }
675 static PyObject *pyxc_evtchn_reset(XcObject *self,
676 PyObject *args,
677 PyObject *kwds)
678 {
679 uint32_t dom;
681 static char *kwd_list[] = { "dom", NULL };
683 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &dom) )
684 return NULL;
686 if ( xc_evtchn_reset(self->xc_handle, dom) < 0 )
687 return pyxc_error_to_exception();
689 Py_INCREF(zero);
690 return zero;
691 }
693 static PyObject *pyxc_physdev_pci_access_modify(XcObject *self,
694 PyObject *args,
695 PyObject *kwds)
696 {
697 uint32_t dom;
698 int bus, dev, func, enable, ret;
700 static char *kwd_list[] = { "domid", "bus", "dev", "func", "enable", NULL };
702 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiii", kwd_list,
703 &dom, &bus, &dev, &func, &enable) )
704 return NULL;
706 ret = xc_physdev_pci_access_modify(
707 self->xc_handle, dom, bus, dev, func, enable);
708 if ( ret != 0 )
709 return pyxc_error_to_exception();
711 Py_INCREF(zero);
712 return zero;
713 }
715 static PyObject *pyxc_readconsolering(XcObject *self,
716 PyObject *args,
717 PyObject *kwds)
718 {
719 unsigned int clear = 0;
720 char _str[32768], *str = _str;
721 unsigned int count = 32768;
722 int ret;
724 static char *kwd_list[] = { "clear", NULL };
726 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwd_list, &clear) )
727 return NULL;
729 ret = xc_readconsolering(self->xc_handle, &str, &count, clear);
730 if ( ret < 0 )
731 return pyxc_error_to_exception();
733 return PyString_FromStringAndSize(str, count);
734 }
737 static unsigned long pages_to_kib(unsigned long pages)
738 {
739 return pages * (XC_PAGE_SIZE / 1024);
740 }
743 static PyObject *pyxc_pages_to_kib(XcObject *self, PyObject *args)
744 {
745 unsigned long pages;
747 if (!PyArg_ParseTuple(args, "l", &pages))
748 return NULL;
750 return PyLong_FromUnsignedLong(pages_to_kib(pages));
751 }
754 static PyObject *pyxc_physinfo(XcObject *self)
755 {
756 #define MAX_CPU_ID 255
757 xc_physinfo_t info;
758 char cpu_cap[128], *p=cpu_cap, *q=cpu_cap;
759 int i, j, max_cpu_id;
760 PyObject *ret_obj, *node_to_cpu_obj;
761 xc_cpu_to_node_t map[MAX_CPU_ID + 1];
763 set_xen_guest_handle(info.cpu_to_node, map);
764 info.max_cpu_id = MAX_CPU_ID;
766 if ( xc_physinfo(self->xc_handle, &info) != 0 )
767 return pyxc_error_to_exception();
769 *q = 0;
770 for ( i = 0; i < sizeof(info.hw_cap)/4; i++ )
771 {
772 p += sprintf(p, "%08x:", info.hw_cap[i]);
773 if ( info.hw_cap[i] )
774 q = p;
775 }
776 if ( q > cpu_cap )
777 *(q-1) = 0;
779 ret_obj = Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:l,s:l,s:l,s:i,s:s}",
780 "nr_nodes", info.nr_nodes,
781 "max_cpu_id", info.max_cpu_id,
782 "threads_per_core", info.threads_per_core,
783 "cores_per_socket", info.cores_per_socket,
784 "nr_cpus", info.nr_cpus,
785 "total_memory", pages_to_kib(info.total_pages),
786 "free_memory", pages_to_kib(info.free_pages),
787 "scrub_memory", pages_to_kib(info.scrub_pages),
788 "cpu_khz", info.cpu_khz,
789 "hw_caps", cpu_cap);
791 max_cpu_id = info.max_cpu_id;
792 if ( max_cpu_id > MAX_CPU_ID )
793 max_cpu_id = MAX_CPU_ID;
795 /* Construct node-to-cpu lists. */
796 node_to_cpu_obj = PyList_New(0);
798 /* Make a list for each node. */
799 for ( i = 0; i < info.nr_nodes; i++ )
800 {
801 PyObject *cpus = PyList_New(0);
802 for ( j = 0; j <= max_cpu_id; j++ )
803 if ( i == map[j])
804 PyList_Append(cpus, PyInt_FromLong(j));
805 PyList_Append(node_to_cpu_obj, cpus);
806 }
808 PyDict_SetItemString(ret_obj, "node_to_cpu", node_to_cpu_obj);
810 return ret_obj;
811 #undef MAX_CPU_ID
812 }
814 static PyObject *pyxc_xeninfo(XcObject *self)
815 {
816 xen_extraversion_t xen_extra;
817 xen_compile_info_t xen_cc;
818 xen_changeset_info_t xen_chgset;
819 xen_capabilities_info_t xen_caps;
820 xen_platform_parameters_t p_parms;
821 long xen_version;
822 long xen_pagesize;
823 char str[128];
825 xen_version = xc_version(self->xc_handle, XENVER_version, NULL);
827 if ( xc_version(self->xc_handle, XENVER_extraversion, &xen_extra) != 0 )
828 return pyxc_error_to_exception();
830 if ( xc_version(self->xc_handle, XENVER_compile_info, &xen_cc) != 0 )
831 return pyxc_error_to_exception();
833 if ( xc_version(self->xc_handle, XENVER_changeset, &xen_chgset) != 0 )
834 return pyxc_error_to_exception();
836 if ( xc_version(self->xc_handle, XENVER_capabilities, &xen_caps) != 0 )
837 return pyxc_error_to_exception();
839 if ( xc_version(self->xc_handle, XENVER_platform_parameters, &p_parms) != 0 )
840 return pyxc_error_to_exception();
842 sprintf(str, "virt_start=0x%lx", p_parms.virt_start);
844 xen_pagesize = xc_version(self->xc_handle, XENVER_pagesize, NULL);
845 if (xen_pagesize < 0 )
846 return pyxc_error_to_exception();
848 return Py_BuildValue("{s:i,s:i,s:s,s:s,s:i,s:s,s:s,s:s,s:s,s:s,s:s}",
849 "xen_major", xen_version >> 16,
850 "xen_minor", (xen_version & 0xffff),
851 "xen_extra", xen_extra,
852 "xen_caps", xen_caps,
853 "xen_pagesize", xen_pagesize,
854 "platform_params", str,
855 "xen_changeset", xen_chgset,
856 "cc_compiler", xen_cc.compiler,
857 "cc_compile_by", xen_cc.compile_by,
858 "cc_compile_domain", xen_cc.compile_domain,
859 "cc_compile_date", xen_cc.compile_date);
860 }
863 static PyObject *pyxc_sedf_domain_set(XcObject *self,
864 PyObject *args,
865 PyObject *kwds)
866 {
867 uint32_t domid;
868 uint64_t period, slice, latency;
869 uint16_t extratime, weight;
870 static char *kwd_list[] = { "domid", "period", "slice",
871 "latency", "extratime", "weight",NULL };
873 if( !PyArg_ParseTupleAndKeywords(args, kwds, "iLLLhh", kwd_list,
874 &domid, &period, &slice,
875 &latency, &extratime, &weight) )
876 return NULL;
877 if ( xc_sedf_domain_set(self->xc_handle, domid, period,
878 slice, latency, extratime,weight) != 0 )
879 return pyxc_error_to_exception();
881 Py_INCREF(zero);
882 return zero;
883 }
885 static PyObject *pyxc_sedf_domain_get(XcObject *self, PyObject *args)
886 {
887 uint32_t domid;
888 uint64_t period, slice,latency;
889 uint16_t weight, extratime;
891 if(!PyArg_ParseTuple(args, "i", &domid))
892 return NULL;
894 if (xc_sedf_domain_get(self->xc_handle, domid, &period,
895 &slice,&latency,&extratime,&weight))
896 return pyxc_error_to_exception();
898 return Py_BuildValue("{s:i,s:L,s:L,s:L,s:i,s:i}",
899 "domid", domid,
900 "period", period,
901 "slice", slice,
902 "latency", latency,
903 "extratime", extratime,
904 "weight", weight);
905 }
907 static PyObject *pyxc_shadow_control(PyObject *self,
908 PyObject *args,
909 PyObject *kwds)
910 {
911 XcObject *xc = (XcObject *)self;
913 uint32_t dom;
914 int op=0;
916 static char *kwd_list[] = { "dom", "op", NULL };
918 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
919 &dom, &op) )
920 return NULL;
922 if ( xc_shadow_control(xc->xc_handle, dom, op, NULL, 0, NULL, 0, NULL)
923 < 0 )
924 return pyxc_error_to_exception();
926 Py_INCREF(zero);
927 return zero;
928 }
930 static PyObject *pyxc_shadow_mem_control(PyObject *self,
931 PyObject *args,
932 PyObject *kwds)
933 {
934 XcObject *xc = (XcObject *)self;
935 int op;
936 uint32_t dom;
937 int mbarg = -1;
938 unsigned long mb;
940 static char *kwd_list[] = { "dom", "mb", NULL };
942 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
943 &dom, &mbarg) )
944 return NULL;
946 if ( mbarg < 0 )
947 op = XEN_DOMCTL_SHADOW_OP_GET_ALLOCATION;
948 else
949 {
950 mb = mbarg;
951 op = XEN_DOMCTL_SHADOW_OP_SET_ALLOCATION;
952 }
953 if ( xc_shadow_control(xc->xc_handle, dom, op, NULL, 0, &mb, 0, NULL) < 0 )
954 return pyxc_error_to_exception();
956 mbarg = mb;
957 return Py_BuildValue("i", mbarg);
958 }
960 static PyObject *pyxc_sched_id_get(XcObject *self) {
962 int sched_id;
963 if (xc_sched_id(self->xc_handle, &sched_id) != 0)
964 return PyErr_SetFromErrno(xc_error_obj);
966 return Py_BuildValue("i", sched_id);
967 }
969 static PyObject *pyxc_sched_credit_domain_set(XcObject *self,
970 PyObject *args,
971 PyObject *kwds)
972 {
973 uint32_t domid;
974 uint16_t weight;
975 uint16_t cap;
976 static char *kwd_list[] = { "domid", "weight", "cap", NULL };
977 static char kwd_type[] = "I|HH";
978 struct xen_domctl_sched_credit sdom;
980 weight = 0;
981 cap = (uint16_t)~0U;
982 if( !PyArg_ParseTupleAndKeywords(args, kwds, kwd_type, kwd_list,
983 &domid, &weight, &cap) )
984 return NULL;
986 sdom.weight = weight;
987 sdom.cap = cap;
989 if ( xc_sched_credit_domain_set(self->xc_handle, domid, &sdom) != 0 )
990 return pyxc_error_to_exception();
992 Py_INCREF(zero);
993 return zero;
994 }
996 static PyObject *pyxc_sched_credit_domain_get(XcObject *self, PyObject *args)
997 {
998 uint32_t domid;
999 struct xen_domctl_sched_credit sdom;
1001 if( !PyArg_ParseTuple(args, "I", &domid) )
1002 return NULL;
1004 if ( xc_sched_credit_domain_get(self->xc_handle, domid, &sdom) != 0 )
1005 return pyxc_error_to_exception();
1007 return Py_BuildValue("{s:H,s:H}",
1008 "weight", sdom.weight,
1009 "cap", sdom.cap);
1012 static PyObject *pyxc_domain_setmaxmem(XcObject *self, PyObject *args)
1014 uint32_t dom;
1015 unsigned int maxmem_kb;
1017 if (!PyArg_ParseTuple(args, "ii", &dom, &maxmem_kb))
1018 return NULL;
1020 if (xc_domain_setmaxmem(self->xc_handle, dom, maxmem_kb) != 0)
1021 return pyxc_error_to_exception();
1023 Py_INCREF(zero);
1024 return zero;
1027 static PyObject *pyxc_domain_set_memmap_limit(XcObject *self, PyObject *args)
1029 uint32_t dom;
1030 unsigned int maplimit_kb;
1032 if ( !PyArg_ParseTuple(args, "ii", &dom, &maplimit_kb) )
1033 return NULL;
1035 if ( xc_domain_set_memmap_limit(self->xc_handle, dom, maplimit_kb) != 0 )
1036 return pyxc_error_to_exception();
1038 Py_INCREF(zero);
1039 return zero;
1042 static PyObject *pyxc_domain_memory_increase_reservation(XcObject *self,
1043 PyObject *args,
1044 PyObject *kwds)
1046 uint32_t dom;
1047 unsigned long mem_kb;
1048 unsigned int extent_order = 0 , address_bits = 0;
1049 unsigned long nr_extents;
1051 static char *kwd_list[] = { "domid", "mem_kb", "extent_order", "address_bits", NULL };
1053 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "il|ii", kwd_list,
1054 &dom, &mem_kb, &extent_order, &address_bits) )
1055 return NULL;
1057 /* round down to nearest power of 2. Assume callers using extent_order>0
1058 know what they are doing */
1059 nr_extents = (mem_kb / (XC_PAGE_SIZE/1024)) >> extent_order;
1060 if ( xc_domain_memory_increase_reservation(self->xc_handle, dom,
1061 nr_extents, extent_order,
1062 address_bits, NULL) )
1063 return pyxc_error_to_exception();
1065 Py_INCREF(zero);
1066 return zero;
1069 static PyObject *pyxc_domain_ioport_permission(XcObject *self,
1070 PyObject *args,
1071 PyObject *kwds)
1073 uint32_t dom;
1074 int first_port, nr_ports, allow_access, ret;
1076 static char *kwd_list[] = { "domid", "first_port", "nr_ports", "allow_access", NULL };
1078 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiii", kwd_list,
1079 &dom, &first_port, &nr_ports, &allow_access) )
1080 return NULL;
1082 ret = xc_domain_ioport_permission(
1083 self->xc_handle, dom, first_port, nr_ports, allow_access);
1084 if ( ret != 0 )
1085 return pyxc_error_to_exception();
1087 Py_INCREF(zero);
1088 return zero;
1091 static PyObject *pyxc_domain_irq_permission(PyObject *self,
1092 PyObject *args,
1093 PyObject *kwds)
1095 XcObject *xc = (XcObject *)self;
1096 uint32_t dom;
1097 int pirq, allow_access, ret;
1099 static char *kwd_list[] = { "domid", "pirq", "allow_access", NULL };
1101 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iii", kwd_list,
1102 &dom, &pirq, &allow_access) )
1103 return NULL;
1105 ret = xc_domain_irq_permission(
1106 xc->xc_handle, dom, pirq, allow_access);
1107 if ( ret != 0 )
1108 return pyxc_error_to_exception();
1110 Py_INCREF(zero);
1111 return zero;
1114 static PyObject *pyxc_domain_iomem_permission(PyObject *self,
1115 PyObject *args,
1116 PyObject *kwds)
1118 XcObject *xc = (XcObject *)self;
1119 uint32_t dom;
1120 unsigned long first_pfn, nr_pfns, allow_access, ret;
1122 static char *kwd_list[] = { "domid", "first_pfn", "nr_pfns", "allow_access", NULL };
1124 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "illi", kwd_list,
1125 &dom, &first_pfn, &nr_pfns, &allow_access) )
1126 return NULL;
1128 ret = xc_domain_iomem_permission(
1129 xc->xc_handle, dom, first_pfn, nr_pfns, allow_access);
1130 if ( ret != 0 )
1131 return pyxc_error_to_exception();
1133 Py_INCREF(zero);
1134 return zero;
1137 static PyObject *pyxc_domain_set_time_offset(XcObject *self, PyObject *args)
1139 uint32_t dom;
1140 int32_t time_offset_seconds;
1141 time_t calendar_time;
1142 struct tm local_time;
1143 struct tm utc_time;
1145 if (!PyArg_ParseTuple(args, "i", &dom))
1146 return NULL;
1148 calendar_time = time(NULL);
1149 localtime_r(&calendar_time, &local_time);
1150 gmtime_r(&calendar_time, &utc_time);
1151 /* set up to get calendar time based on utc_time, with local dst setting */
1152 utc_time.tm_isdst = local_time.tm_isdst;
1153 time_offset_seconds = (int32_t)difftime(calendar_time, mktime(&utc_time));
1155 if (xc_domain_set_time_offset(self->xc_handle, dom, time_offset_seconds) != 0)
1156 return NULL;
1158 Py_INCREF(zero);
1159 return zero;
1162 static PyObject *pyxc_domain_send_trigger(XcObject *self,
1163 PyObject *args,
1164 PyObject *kwds)
1166 uint32_t dom;
1167 int trigger, vcpu = 0;
1169 static char *kwd_list[] = { "domid", "trigger", "vcpu", NULL };
1171 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "ii|i", kwd_list,
1172 &dom, &trigger, &vcpu) )
1173 return NULL;
1175 if (xc_domain_send_trigger(self->xc_handle, dom, trigger, vcpu) != 0)
1176 return pyxc_error_to_exception();
1178 Py_INCREF(zero);
1179 return zero;
1182 static PyObject *pyxc_send_debug_keys(XcObject *self,
1183 PyObject *args,
1184 PyObject *kwds)
1186 char *keys;
1188 static char *kwd_list[] = { "keys", NULL };
1190 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "s", kwd_list, &keys) )
1191 return NULL;
1193 if ( xc_send_debug_keys(self->xc_handle, keys) != 0 )
1194 return pyxc_error_to_exception();
1196 Py_INCREF(zero);
1197 return zero;
1200 static PyObject *dom_op(XcObject *self, PyObject *args,
1201 int (*fn)(int, uint32_t))
1203 uint32_t dom;
1205 if (!PyArg_ParseTuple(args, "i", &dom))
1206 return NULL;
1208 if (fn(self->xc_handle, dom) != 0)
1209 return pyxc_error_to_exception();
1211 Py_INCREF(zero);
1212 return zero;
1215 #ifdef __powerpc__
1216 static PyObject *pyxc_alloc_real_mode_area(XcObject *self,
1217 PyObject *args,
1218 PyObject *kwds)
1220 uint32_t dom;
1221 unsigned int log;
1223 static char *kwd_list[] = { "dom", "log", NULL };
1225 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwd_list,
1226 &dom, &log) )
1227 return NULL;
1229 if ( xc_alloc_real_mode_area(self->xc_handle, dom, log) )
1230 return pyxc_error_to_exception();
1232 Py_INCREF(zero);
1233 return zero;
1235 #endif /* powerpc */
1237 static PyMethodDef pyxc_methods[] = {
1238 { "handle",
1239 (PyCFunction)pyxc_handle,
1240 METH_NOARGS, "\n"
1241 "Query the xc control interface file descriptor.\n\n"
1242 "Returns: [int] file descriptor\n" },
1244 { "domain_create",
1245 (PyCFunction)pyxc_domain_create,
1246 METH_VARARGS | METH_KEYWORDS, "\n"
1247 "Create a new domain.\n"
1248 " dom [int, 0]: Domain identifier to use (allocated if zero).\n"
1249 "Returns: [int] new domain identifier; -1 on error.\n" },
1251 { "domain_max_vcpus",
1252 (PyCFunction)pyxc_domain_max_vcpus,
1253 METH_VARARGS, "\n"
1254 "Set the maximum number of VCPUs a domain may create.\n"
1255 " dom [int, 0]: Domain identifier to use.\n"
1256 " max [int, 0]: New maximum number of VCPUs in domain.\n"
1257 "Returns: [int] 0 on success; -1 on error.\n" },
1259 { "domain_dumpcore",
1260 (PyCFunction)pyxc_domain_dumpcore,
1261 METH_VARARGS, "\n"
1262 "Dump core of a domain.\n"
1263 " dom [int]: Identifier of domain to dump core of.\n"
1264 " corefile [string]: Name of corefile to be created.\n\n"
1265 "Returns: [int] 0 on success; -1 on error.\n" },
1267 { "domain_pause",
1268 (PyCFunction)pyxc_domain_pause,
1269 METH_VARARGS, "\n"
1270 "Temporarily pause execution of a domain.\n"
1271 " dom [int]: Identifier of domain to be paused.\n\n"
1272 "Returns: [int] 0 on success; -1 on error.\n" },
1274 { "domain_unpause",
1275 (PyCFunction)pyxc_domain_unpause,
1276 METH_VARARGS, "\n"
1277 "(Re)start execution of a domain.\n"
1278 " dom [int]: Identifier of domain to be unpaused.\n\n"
1279 "Returns: [int] 0 on success; -1 on error.\n" },
1281 { "domain_destroy",
1282 (PyCFunction)pyxc_domain_destroy,
1283 METH_VARARGS, "\n"
1284 "Destroy a domain.\n"
1285 " dom [int]: Identifier of domain to be destroyed.\n\n"
1286 "Returns: [int] 0 on success; -1 on error.\n" },
1288 { "domain_destroy_hook",
1289 (PyCFunction)pyxc_domain_destroy_hook,
1290 METH_VARARGS, "\n"
1291 "Add a hook for arch stuff before destroy a domain.\n"
1292 " dom [int]: Identifier of domain to be destroyed.\n\n"
1293 "Returns: [int] 0 on success; -1 on error.\n" },
1295 { "domain_resume",
1296 (PyCFunction)pyxc_domain_resume,
1297 METH_VARARGS, "\n"
1298 "Resume execution of a suspended domain.\n"
1299 " dom [int]: Identifier of domain to be resumed.\n"
1300 " fast [int]: Use cooperative resume.\n\n"
1301 "Returns: [int] 0 on success; -1 on error.\n" },
1303 { "domain_shutdown",
1304 (PyCFunction)pyxc_domain_shutdown,
1305 METH_VARARGS, "\n"
1306 "Shutdown a domain.\n"
1307 " dom [int, 0]: Domain identifier to use.\n"
1308 " reason [int, 0]: Reason for shutdown.\n"
1309 "Returns: [int] 0 on success; -1 on error.\n" },
1311 { "vcpu_setaffinity",
1312 (PyCFunction)pyxc_vcpu_setaffinity,
1313 METH_VARARGS | METH_KEYWORDS, "\n"
1314 "Pin a VCPU to a specified set CPUs.\n"
1315 " dom [int]: Identifier of domain to which VCPU belongs.\n"
1316 " vcpu [int, 0]: VCPU being pinned.\n"
1317 " cpumap [list, []]: list of usable CPUs.\n\n"
1318 "Returns: [int] 0 on success; -1 on error.\n" },
1320 { "domain_setcpuweight",
1321 (PyCFunction)pyxc_domain_setcpuweight,
1322 METH_VARARGS | METH_KEYWORDS, "\n"
1323 "Set cpuweight scheduler parameter for domain.\n"
1324 " dom [int]: Identifier of domain to be changed.\n"
1325 " cpuweight [float, 1]: VCPU being pinned.\n"
1326 "Returns: [int] 0 on success; -1 on error.\n" },
1328 { "domain_sethandle",
1329 (PyCFunction)pyxc_domain_sethandle,
1330 METH_VARARGS, "\n"
1331 "Set domain's opaque handle.\n"
1332 " dom [int]: Identifier of domain.\n"
1333 " handle [list of 16 ints]: New opaque handle.\n"
1334 "Returns: [int] 0 on success; -1 on error.\n" },
1336 { "domain_getinfo",
1337 (PyCFunction)pyxc_domain_getinfo,
1338 METH_VARARGS | METH_KEYWORDS, "\n"
1339 "Get information regarding a set of domains, in increasing id order.\n"
1340 " first_dom [int, 0]: First domain to retrieve info about.\n"
1341 " max_doms [int, 1024]: Maximum number of domains to retrieve info"
1342 " about.\n\n"
1343 "Returns: [list of dicts] if list length is less than 'max_doms'\n"
1344 " parameter then there was an error, or the end of the\n"
1345 " domain-id space was reached.\n"
1346 " dom [int]: Identifier of domain to which this info pertains\n"
1347 " cpu [int]: CPU to which this domain is bound\n"
1348 " vcpus [int]: Number of Virtual CPUS in this domain\n"
1349 " dying [int]: Bool - is the domain dying?\n"
1350 " crashed [int]: Bool - has the domain crashed?\n"
1351 " shutdown [int]: Bool - has the domain shut itself down?\n"
1352 " paused [int]: Bool - is the domain paused by control software?\n"
1353 " blocked [int]: Bool - is the domain blocked waiting for an event?\n"
1354 " running [int]: Bool - is the domain currently running?\n"
1355 " mem_kb [int]: Memory reservation, in kilobytes\n"
1356 " maxmem_kb [int]: Maximum memory limit, in kilobytes\n"
1357 " cpu_time [long]: CPU time consumed, in nanoseconds\n"
1358 " shutdown_reason [int]: Numeric code from guest OS, explaining "
1359 "reason why it shut itself down.\n" },
1361 { "vcpu_getinfo",
1362 (PyCFunction)pyxc_vcpu_getinfo,
1363 METH_VARARGS | METH_KEYWORDS, "\n"
1364 "Get information regarding a VCPU.\n"
1365 " dom [int]: Domain to retrieve info about.\n"
1366 " vcpu [int, 0]: VCPU to retrieve info about.\n\n"
1367 "Returns: [dict]\n"
1368 " online [int]: Bool - Is this VCPU currently online?\n"
1369 " blocked [int]: Bool - Is this VCPU blocked waiting for an event?\n"
1370 " running [int]: Bool - Is this VCPU currently running on a CPU?\n"
1371 " cpu_time [long]: CPU time consumed, in nanoseconds\n"
1372 " cpumap [int]: Bitmap of CPUs this VCPU can run on\n"
1373 " cpu [int]: CPU that this VCPU is currently bound to\n" },
1375 { "linux_build",
1376 (PyCFunction)pyxc_linux_build,
1377 METH_VARARGS | METH_KEYWORDS, "\n"
1378 "Build a new Linux guest OS.\n"
1379 " dom [int]: Identifier of domain to build into.\n"
1380 " image [str]: Name of kernel image file. May be gzipped.\n"
1381 " ramdisk [str, n/a]: Name of ramdisk file, if any.\n"
1382 " cmdline [str, n/a]: Kernel parameters, if any.\n\n"
1383 " vcpus [int, 1]: Number of Virtual CPUS in domain.\n\n"
1384 "Returns: [int] 0 on success; -1 on error.\n" },
1386 { "hvm_build",
1387 (PyCFunction)pyxc_hvm_build,
1388 METH_VARARGS | METH_KEYWORDS, "\n"
1389 "Build a new HVM guest OS.\n"
1390 " dom [int]: Identifier of domain to build into.\n"
1391 " image [str]: Name of HVM loader image file.\n"
1392 " vcpus [int, 1]: Number of Virtual CPUS in domain.\n\n"
1393 "Returns: [int] 0 on success; -1 on error.\n" },
1395 { "hvm_get_param",
1396 (PyCFunction)pyxc_get_hvm_param,
1397 METH_VARARGS | METH_KEYWORDS, "\n"
1398 "get a parameter of HVM guest OS.\n"
1399 " dom [int]: Identifier of domain to build into.\n"
1400 " param [int]: No. of HVM param.\n"
1401 "Returns: [long] value of the param.\n" },
1403 { "hvm_set_param",
1404 (PyCFunction)pyxc_set_hvm_param,
1405 METH_VARARGS | METH_KEYWORDS, "\n"
1406 "set a parameter of HVM guest OS.\n"
1407 " dom [int]: Identifier of domain to build into.\n"
1408 " param [int]: No. of HVM param.\n"
1409 " value [long]: Value of param.\n"
1410 "Returns: [int] 0 on success.\n" },
1412 { "assign_device",
1413 (PyCFunction)pyxc_assign_device,
1414 METH_VARARGS | METH_KEYWORDS, "\n"
1415 "assign device with VT-d.\n"
1416 " dom [int]: Identifier of domain to build into.\n"
1417 " pci_str [str]: PCI devices.\n"
1418 "Returns: [int] 0 on success, or device bdf that can't be assigned.\n" },
1420 { "sched_id_get",
1421 (PyCFunction)pyxc_sched_id_get,
1422 METH_NOARGS, "\n"
1423 "Get the current scheduler type in use.\n"
1424 "Returns: [int] sched_id.\n" },
1426 { "sedf_domain_set",
1427 (PyCFunction)pyxc_sedf_domain_set,
1428 METH_KEYWORDS, "\n"
1429 "Set the scheduling parameters for a domain when running with Atropos.\n"
1430 " dom [int]: domain to set\n"
1431 " period [long]: domain's scheduling period\n"
1432 " slice [long]: domain's slice per period\n"
1433 " latency [long]: domain's wakeup latency hint\n"
1434 " extratime [int]: domain aware of extratime?\n"
1435 "Returns: [int] 0 on success; -1 on error.\n" },
1437 { "sedf_domain_get",
1438 (PyCFunction)pyxc_sedf_domain_get,
1439 METH_VARARGS, "\n"
1440 "Get the current scheduling parameters for a domain when running with\n"
1441 "the Atropos scheduler."
1442 " dom [int]: domain to query\n"
1443 "Returns: [dict]\n"
1444 " domain [int]: domain ID\n"
1445 " period [long]: scheduler period\n"
1446 " slice [long]: CPU reservation per period\n"
1447 " latency [long]: domain's wakeup latency hint\n"
1448 " extratime [int]: domain aware of extratime?\n"},
1450 { "sched_credit_domain_set",
1451 (PyCFunction)pyxc_sched_credit_domain_set,
1452 METH_KEYWORDS, "\n"
1453 "Set the scheduling parameters for a domain when running with the\n"
1454 "SMP credit scheduler.\n"
1455 " domid [int]: domain id to set\n"
1456 " weight [short]: domain's scheduling weight\n"
1457 "Returns: [int] 0 on success; -1 on error.\n" },
1459 { "sched_credit_domain_get",
1460 (PyCFunction)pyxc_sched_credit_domain_get,
1461 METH_VARARGS, "\n"
1462 "Get the scheduling parameters for a domain when running with the\n"
1463 "SMP credit scheduler.\n"
1464 " domid [int]: domain id to get\n"
1465 "Returns: [dict]\n"
1466 " weight [short]: domain's scheduling weight\n"},
1468 { "evtchn_alloc_unbound",
1469 (PyCFunction)pyxc_evtchn_alloc_unbound,
1470 METH_VARARGS | METH_KEYWORDS, "\n"
1471 "Allocate an unbound port that will await a remote connection.\n"
1472 " dom [int]: Domain whose port space to allocate from.\n"
1473 " remote_dom [int]: Remote domain to accept connections from.\n\n"
1474 "Returns: [int] Unbound event-channel port.\n" },
1476 { "evtchn_reset",
1477 (PyCFunction)pyxc_evtchn_reset,
1478 METH_VARARGS | METH_KEYWORDS, "\n"
1479 "Reset all connections.\n"
1480 " dom [int]: Domain to reset.\n" },
1482 { "physdev_pci_access_modify",
1483 (PyCFunction)pyxc_physdev_pci_access_modify,
1484 METH_VARARGS | METH_KEYWORDS, "\n"
1485 "Allow a domain access to a PCI device\n"
1486 " dom [int]: Identifier of domain to be allowed access.\n"
1487 " bus [int]: PCI bus\n"
1488 " dev [int]: PCI slot\n"
1489 " func [int]: PCI function\n"
1490 " enable [int]: Non-zero means enable access; else disable access\n\n"
1491 "Returns: [int] 0 on success; -1 on error.\n" },
1493 { "readconsolering",
1494 (PyCFunction)pyxc_readconsolering,
1495 METH_VARARGS | METH_KEYWORDS, "\n"
1496 "Read Xen's console ring.\n"
1497 " clear [int, 0]: Bool - clear the ring after reading from it?\n\n"
1498 "Returns: [str] string is empty on failure.\n" },
1500 { "physinfo",
1501 (PyCFunction)pyxc_physinfo,
1502 METH_NOARGS, "\n"
1503 "Get information about the physical host machine\n"
1504 "Returns [dict]: information about the hardware"
1505 " [None]: on failure.\n" },
1507 { "xeninfo",
1508 (PyCFunction)pyxc_xeninfo,
1509 METH_NOARGS, "\n"
1510 "Get information about the Xen host\n"
1511 "Returns [dict]: information about Xen"
1512 " [None]: on failure.\n" },
1514 { "shadow_control",
1515 (PyCFunction)pyxc_shadow_control,
1516 METH_VARARGS | METH_KEYWORDS, "\n"
1517 "Set parameter for shadow pagetable interface\n"
1518 " dom [int]: Identifier of domain.\n"
1519 " op [int, 0]: operation\n\n"
1520 "Returns: [int] 0 on success; -1 on error.\n" },
1522 { "shadow_mem_control",
1523 (PyCFunction)pyxc_shadow_mem_control,
1524 METH_VARARGS | METH_KEYWORDS, "\n"
1525 "Set or read shadow pagetable memory use\n"
1526 " dom [int]: Identifier of domain.\n"
1527 " mb [int, -1]: MB of shadow memory this domain should have.\n\n"
1528 "Returns: [int] MB of shadow memory in use by this domain.\n" },
1530 { "domain_setmaxmem",
1531 (PyCFunction)pyxc_domain_setmaxmem,
1532 METH_VARARGS, "\n"
1533 "Set a domain's memory limit\n"
1534 " dom [int]: Identifier of domain.\n"
1535 " maxmem_kb [int]: .\n"
1536 "Returns: [int] 0 on success; -1 on error.\n" },
1538 { "domain_set_memmap_limit",
1539 (PyCFunction)pyxc_domain_set_memmap_limit,
1540 METH_VARARGS, "\n"
1541 "Set a domain's physical memory mappping limit\n"
1542 " dom [int]: Identifier of domain.\n"
1543 " map_limitkb [int]: .\n"
1544 "Returns: [int] 0 on success; -1 on error.\n" },
1546 { "domain_memory_increase_reservation",
1547 (PyCFunction)pyxc_domain_memory_increase_reservation,
1548 METH_VARARGS | METH_KEYWORDS, "\n"
1549 "Increase a domain's memory reservation\n"
1550 " dom [int]: Identifier of domain.\n"
1551 " mem_kb [long]: .\n"
1552 "Returns: [int] 0 on success; -1 on error.\n" },
1553 #ifdef __ia64__
1554 { "nvram_init",
1555 (PyCFunction)pyxc_nvram_init,
1556 METH_VARARGS, "\n"
1557 "Init nvram in IA64 platform\n"
1558 "Returns: [int] 0 on success; -1 on error.\n" },
1559 #endif /* __ia64__ */
1560 { "domain_ioport_permission",
1561 (PyCFunction)pyxc_domain_ioport_permission,
1562 METH_VARARGS | METH_KEYWORDS, "\n"
1563 "Allow a domain access to a range of IO ports\n"
1564 " dom [int]: Identifier of domain to be allowed access.\n"
1565 " first_port [int]: First IO port\n"
1566 " nr_ports [int]: Number of IO ports\n"
1567 " allow_access [int]: Non-zero means enable access; else disable access\n\n"
1568 "Returns: [int] 0 on success; -1 on error.\n" },
1570 { "domain_irq_permission",
1571 (PyCFunction)pyxc_domain_irq_permission,
1572 METH_VARARGS | METH_KEYWORDS, "\n"
1573 "Allow a domain access to a physical IRQ\n"
1574 " dom [int]: Identifier of domain to be allowed access.\n"
1575 " pirq [int]: The Physical IRQ\n"
1576 " allow_access [int]: Non-zero means enable access; else disable access\n\n"
1577 "Returns: [int] 0 on success; -1 on error.\n" },
1579 { "domain_iomem_permission",
1580 (PyCFunction)pyxc_domain_iomem_permission,
1581 METH_VARARGS | METH_KEYWORDS, "\n"
1582 "Allow a domain access to a range of IO memory pages\n"
1583 " dom [int]: Identifier of domain to be allowed access.\n"
1584 " first_pfn [long]: First page of I/O Memory\n"
1585 " nr_pfns [long]: Number of pages of I/O Memory (>0)\n"
1586 " allow_access [int]: Non-zero means enable access; else disable access\n\n"
1587 "Returns: [int] 0 on success; -1 on error.\n" },
1589 { "pages_to_kib",
1590 (PyCFunction)pyxc_pages_to_kib,
1591 METH_VARARGS, "\n"
1592 "Returns: [int]: The size in KiB of memory spanning the given number "
1593 "of pages.\n" },
1595 { "domain_set_time_offset",
1596 (PyCFunction)pyxc_domain_set_time_offset,
1597 METH_VARARGS, "\n"
1598 "Set a domain's time offset to Dom0's localtime\n"
1599 " dom [int]: Domain whose time offset is being set.\n"
1600 "Returns: [int] 0 on success; -1 on error.\n" },
1602 { "domain_send_trigger",
1603 (PyCFunction)pyxc_domain_send_trigger,
1604 METH_VARARGS | METH_KEYWORDS, "\n"
1605 "Send trigger to a domain.\n"
1606 " dom [int]: Identifier of domain to be sent trigger.\n"
1607 " trigger [int]: Trigger type number.\n"
1608 " vcpu [int]: VCPU to be sent trigger.\n"
1609 "Returns: [int] 0 on success; -1 on error.\n" },
1611 { "send_debug_keys",
1612 (PyCFunction)pyxc_send_debug_keys,
1613 METH_VARARGS | METH_KEYWORDS, "\n"
1614 "Inject debug keys into Xen.\n"
1615 " keys [str]: String of keys to inject.\n" },
1617 #ifdef __powerpc__
1618 { "arch_alloc_real_mode_area",
1619 (PyCFunction)pyxc_alloc_real_mode_area,
1620 METH_VARARGS | METH_KEYWORDS, "\n"
1621 "Allocate a domain's real mode area.\n"
1622 " dom [int]: Identifier of domain.\n"
1623 " log [int]: Specifies the area's size.\n"
1624 "Returns: [int] 0 on success; -1 on error.\n" },
1625 #endif /* __powerpc */
1627 { NULL, NULL, 0, NULL }
1628 };
1631 static PyObject *PyXc_getattr(PyObject *obj, char *name)
1633 return Py_FindMethod(pyxc_methods, obj, name);
1636 static PyObject *PyXc_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1638 XcObject *self = (XcObject *)type->tp_alloc(type, 0);
1640 if (self == NULL)
1641 return NULL;
1643 self->xc_handle = -1;
1645 return (PyObject *)self;
1648 static int
1649 PyXc_init(XcObject *self, PyObject *args, PyObject *kwds)
1651 if ((self->xc_handle = xc_interface_open()) == -1) {
1652 pyxc_error_to_exception();
1653 return -1;
1656 return 0;
1659 static void PyXc_dealloc(XcObject *self)
1661 if (self->xc_handle != -1) {
1662 xc_interface_close(self->xc_handle);
1663 self->xc_handle = -1;
1666 self->ob_type->tp_free((PyObject *)self);
1669 static PyTypeObject PyXcType = {
1670 PyObject_HEAD_INIT(NULL)
1671 0,
1672 PKG "." CLS,
1673 sizeof(XcObject),
1674 0,
1675 (destructor)PyXc_dealloc, /* tp_dealloc */
1676 NULL, /* tp_print */
1677 PyXc_getattr, /* tp_getattr */
1678 NULL, /* tp_setattr */
1679 NULL, /* tp_compare */
1680 NULL, /* tp_repr */
1681 NULL, /* tp_as_number */
1682 NULL, /* tp_as_sequence */
1683 NULL, /* tp_as_mapping */
1684 NULL, /* tp_hash */
1685 NULL, /* tp_call */
1686 NULL, /* tp_str */
1687 NULL, /* tp_getattro */
1688 NULL, /* tp_setattro */
1689 NULL, /* tp_as_buffer */
1690 Py_TPFLAGS_DEFAULT, /* tp_flags */
1691 "Xen client connections", /* tp_doc */
1692 NULL, /* tp_traverse */
1693 NULL, /* tp_clear */
1694 NULL, /* tp_richcompare */
1695 0, /* tp_weaklistoffset */
1696 NULL, /* tp_iter */
1697 NULL, /* tp_iternext */
1698 pyxc_methods, /* tp_methods */
1699 NULL, /* tp_members */
1700 NULL, /* tp_getset */
1701 NULL, /* tp_base */
1702 NULL, /* tp_dict */
1703 NULL, /* tp_descr_get */
1704 NULL, /* tp_descr_set */
1705 0, /* tp_dictoffset */
1706 (initproc)PyXc_init, /* tp_init */
1707 NULL, /* tp_alloc */
1708 PyXc_new, /* tp_new */
1709 };
1711 static PyMethodDef xc_methods[] = { { NULL } };
1713 PyMODINIT_FUNC initxc(void)
1715 PyObject *m;
1717 if (PyType_Ready(&PyXcType) < 0)
1718 return;
1720 m = Py_InitModule(PKG, xc_methods);
1722 if (m == NULL)
1723 return;
1725 xc_error_obj = PyErr_NewException(PKG ".Error", PyExc_RuntimeError, NULL);
1726 zero = PyInt_FromLong(0);
1728 /* KAF: This ensures that we get debug output in a timely manner. */
1729 setbuf(stdout, NULL);
1730 setbuf(stderr, NULL);
1732 Py_INCREF(&PyXcType);
1733 PyModule_AddObject(m, CLS, (PyObject *)&PyXcType);
1735 Py_INCREF(xc_error_obj);
1736 PyModule_AddObject(m, "Error", xc_error_obj);
1738 /* Expose some libxc constants to Python */
1739 PyModule_AddIntConstant(m, "XEN_SCHEDULER_SEDF", XEN_SCHEDULER_SEDF);
1740 PyModule_AddIntConstant(m, "XEN_SCHEDULER_CREDIT", XEN_SCHEDULER_CREDIT);
1745 /*
1746 * Local variables:
1747 * c-indent-level: 4
1748 * c-basic-offset: 4
1749 * End:
1750 */