ia64/xen-unstable

view tools/python/xen/lowlevel/xc/xc.c @ 15910:c36292e83f07

Fix two memory leaks in xend.
Signed-off-by: Kouya Shimura <kouya@jp.fujitsu.com>
author kfraser@localhost.localdomain
date Fri Sep 14 16:04:33 2007 +0100 (2007-09-14)
parents 5ee379a1f0d0
children 1f893d055c6f
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 unsigned int mem_mb;
417 unsigned long store_mfn = 0;
418 unsigned long console_mfn = 0;
419 PyObject* elfnote_dict;
420 PyObject* elfnote = NULL;
421 PyObject* ret;
422 int i;
424 static char *kwd_list[] = { "domid", "store_evtchn", "memsize",
425 "console_evtchn", "image",
426 /* optional */
427 "ramdisk", "cmdline", "flags",
428 "features", NULL };
430 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiis|ssis", kwd_list,
431 &domid, &store_evtchn, &mem_mb,
432 &console_evtchn, &image,
433 /* optional */
434 &ramdisk, &cmdline, &flags,
435 &features) )
436 return NULL;
438 xc_dom_loginit();
439 if (!(dom = xc_dom_allocate(cmdline, features)))
440 return pyxc_error_to_exception();
442 if ( xc_dom_linux_build(self->xc_handle, dom, domid, mem_mb, image,
443 ramdisk, flags, store_evtchn, &store_mfn,
444 console_evtchn, &console_mfn) != 0 ) {
445 goto out;
446 }
448 if ( !(elfnote_dict = PyDict_New()) )
449 goto out;
451 for ( i = 0; i < ARRAY_SIZE(dom->parms.elf_notes); i++ )
452 {
453 switch ( dom->parms.elf_notes[i].type )
454 {
455 case XEN_ENT_NONE:
456 continue;
457 case XEN_ENT_LONG:
458 elfnote = Py_BuildValue("k", dom->parms.elf_notes[i].data.num);
459 break;
460 case XEN_ENT_STR:
461 elfnote = Py_BuildValue("s", dom->parms.elf_notes[i].data.str);
462 break;
463 }
464 PyDict_SetItemString(elfnote_dict,
465 dom->parms.elf_notes[i].name,
466 elfnote);
467 Py_DECREF(elfnote);
468 }
470 ret = Py_BuildValue("{s:i,s:i,s:N}",
471 "store_mfn", store_mfn,
472 "console_mfn", console_mfn,
473 "notes", elfnote_dict);
475 if ( dom->arch_hooks->native_protocol )
476 {
477 PyObject *native_protocol =
478 Py_BuildValue("s", dom->arch_hooks->native_protocol);
479 PyDict_SetItemString(ret, "native_protocol", native_protocol);
480 Py_DECREF(native_protocol);
481 }
483 xc_dom_release(dom);
485 return ret;
487 out:
488 xc_dom_release(dom);
489 return pyxc_error_to_exception();
490 }
492 static PyObject *pyxc_get_hvm_param(XcObject *self,
493 PyObject *args,
494 PyObject *kwds)
495 {
496 uint32_t dom;
497 int param;
498 unsigned long value;
500 static char *kwd_list[] = { "domid", "param", NULL };
501 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
502 &dom, &param) )
503 return NULL;
505 if ( xc_get_hvm_param(self->xc_handle, dom, param, &value) != 0 )
506 return pyxc_error_to_exception();
508 return Py_BuildValue("i", value);
510 }
512 #ifdef __ia64__
513 static PyObject *pyxc_nvram_init(XcObject *self,
514 PyObject *args)
515 {
516 char *dom_name;
517 uint32_t dom;
519 if ( !PyArg_ParseTuple(args, "si", &dom_name, &dom) )
520 return NULL;
522 xc_ia64_nvram_init(self->xc_handle, dom_name, dom);
524 Py_INCREF(zero);
525 return zero;
526 }
527 #endif /* __ia64__ */
529 static PyObject *pyxc_hvm_build(XcObject *self,
530 PyObject *args,
531 PyObject *kwds)
532 {
533 uint32_t dom;
534 #if !defined(__ia64__)
535 struct hvm_info_table *va_hvm;
536 uint8_t *va_map, sum;
537 int i;
538 #endif
539 char *image;
540 int store_evtchn, memsize, vcpus = 1, pae = 0, acpi = 0, apic = 1;
541 unsigned long store_mfn;
543 static char *kwd_list[] = { "domid", "store_evtchn",
544 "memsize", "image", "vcpus", "pae", "acpi",
545 "apic", NULL };
546 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiis|iiii", kwd_list,
547 &dom, &store_evtchn, &memsize,
548 &image, &vcpus, &pae, &acpi, &apic) )
549 return NULL;
551 if ( xc_hvm_build(self->xc_handle, dom, memsize, image) != 0 )
552 return pyxc_error_to_exception();
554 #if !defined(__ia64__)
555 /* Set up the HVM info table. */
556 va_map = xc_map_foreign_range(self->xc_handle, dom, XC_PAGE_SIZE,
557 PROT_READ | PROT_WRITE,
558 HVM_INFO_PFN);
559 if ( va_map == NULL )
560 return PyErr_SetFromErrno(xc_error_obj);
561 va_hvm = (struct hvm_info_table *)(va_map + HVM_INFO_OFFSET);
562 memset(va_hvm, 0, sizeof(*va_hvm));
563 strncpy(va_hvm->signature, "HVM INFO", 8);
564 va_hvm->length = sizeof(struct hvm_info_table);
565 va_hvm->acpi_enabled = acpi;
566 va_hvm->apic_mode = apic;
567 va_hvm->nr_vcpus = vcpus;
568 for ( i = 0, sum = 0; i < va_hvm->length; i++ )
569 sum += ((uint8_t *)va_hvm)[i];
570 va_hvm->checksum = -sum;
571 munmap(va_map, XC_PAGE_SIZE);
572 #endif
574 xc_get_hvm_param(self->xc_handle, dom, HVM_PARAM_STORE_PFN, &store_mfn);
575 #if !defined(__ia64__)
576 xc_set_hvm_param(self->xc_handle, dom, HVM_PARAM_PAE_ENABLED, pae);
577 #endif
578 xc_set_hvm_param(self->xc_handle, dom, HVM_PARAM_STORE_EVTCHN,
579 store_evtchn);
581 return Py_BuildValue("{s:i}", "store_mfn", store_mfn);
582 }
584 static PyObject *pyxc_evtchn_alloc_unbound(XcObject *self,
585 PyObject *args,
586 PyObject *kwds)
587 {
588 uint32_t dom, remote_dom;
589 int port;
591 static char *kwd_list[] = { "domid", "remote_dom", NULL };
593 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwd_list,
594 &dom, &remote_dom) )
595 return NULL;
597 if ( (port = xc_evtchn_alloc_unbound(self->xc_handle, dom, remote_dom)) < 0 )
598 return pyxc_error_to_exception();
600 return PyInt_FromLong(port);
601 }
603 static PyObject *pyxc_evtchn_reset(XcObject *self,
604 PyObject *args,
605 PyObject *kwds)
606 {
607 uint32_t dom;
609 static char *kwd_list[] = { "dom", NULL };
611 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &dom) )
612 return NULL;
614 if ( xc_evtchn_reset(self->xc_handle, dom) < 0 )
615 return pyxc_error_to_exception();
617 Py_INCREF(zero);
618 return zero;
619 }
621 static PyObject *pyxc_physdev_pci_access_modify(XcObject *self,
622 PyObject *args,
623 PyObject *kwds)
624 {
625 uint32_t dom;
626 int bus, dev, func, enable, ret;
628 static char *kwd_list[] = { "domid", "bus", "dev", "func", "enable", NULL };
630 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiiii", kwd_list,
631 &dom, &bus, &dev, &func, &enable) )
632 return NULL;
634 ret = xc_physdev_pci_access_modify(
635 self->xc_handle, dom, bus, dev, func, enable);
636 if ( ret != 0 )
637 return pyxc_error_to_exception();
639 Py_INCREF(zero);
640 return zero;
641 }
643 static PyObject *pyxc_readconsolering(XcObject *self,
644 PyObject *args,
645 PyObject *kwds)
646 {
647 unsigned int clear = 0;
648 char _str[32768], *str = _str;
649 unsigned int count = 32768;
650 int ret;
652 static char *kwd_list[] = { "clear", NULL };
654 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwd_list, &clear) )
655 return NULL;
657 ret = xc_readconsolering(self->xc_handle, &str, &count, clear);
658 if ( ret < 0 )
659 return pyxc_error_to_exception();
661 return PyString_FromStringAndSize(str, count);
662 }
665 static unsigned long pages_to_kib(unsigned long pages)
666 {
667 return pages * (XC_PAGE_SIZE / 1024);
668 }
671 static PyObject *pyxc_pages_to_kib(XcObject *self, PyObject *args)
672 {
673 unsigned long pages;
675 if (!PyArg_ParseTuple(args, "l", &pages))
676 return NULL;
678 return PyLong_FromUnsignedLong(pages_to_kib(pages));
679 }
682 static PyObject *pyxc_physinfo(XcObject *self)
683 {
684 #define MAX_CPU_ID 255
685 xc_physinfo_t info;
686 char cpu_cap[128], *p=cpu_cap, *q=cpu_cap;
687 int i, j, max_cpu_id;
688 PyObject *ret_obj, *node_to_cpu_obj;
689 xc_cpu_to_node_t map[MAX_CPU_ID + 1];
691 set_xen_guest_handle(info.cpu_to_node, map);
692 info.max_cpu_id = MAX_CPU_ID;
694 if ( xc_physinfo(self->xc_handle, &info) != 0 )
695 return pyxc_error_to_exception();
697 *q = 0;
698 for ( i = 0; i < sizeof(info.hw_cap)/4; i++ )
699 {
700 p += sprintf(p, "%08x:", info.hw_cap[i]);
701 if ( info.hw_cap[i] )
702 q = p;
703 }
704 if ( q > cpu_cap )
705 *(q-1) = 0;
707 ret_obj = Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:l,s:l,s:l,s:i,s:s}",
708 "nr_nodes", info.nr_nodes,
709 "max_cpu_id", info.max_cpu_id,
710 "threads_per_core", info.threads_per_core,
711 "cores_per_socket", info.cores_per_socket,
712 "sockets_per_node", info.sockets_per_node,
713 "total_memory", pages_to_kib(info.total_pages),
714 "free_memory", pages_to_kib(info.free_pages),
715 "scrub_memory", pages_to_kib(info.scrub_pages),
716 "cpu_khz", info.cpu_khz,
717 "hw_caps", cpu_cap);
719 max_cpu_id = info.max_cpu_id;
720 if ( max_cpu_id > MAX_CPU_ID )
721 max_cpu_id = MAX_CPU_ID;
723 /* Construct node-to-cpu lists. */
724 node_to_cpu_obj = PyList_New(0);
726 /* Make a list for each node. */
727 for ( i = 0; i < info.nr_nodes; i++ )
728 {
729 PyObject *cpus = PyList_New(0);
730 for ( j = 0; j <= max_cpu_id; j++ )
731 if ( i == map[j])
732 PyList_Append(cpus, PyInt_FromLong(j));
733 PyList_Append(node_to_cpu_obj, cpus);
734 }
736 PyDict_SetItemString(ret_obj, "node_to_cpu", node_to_cpu_obj);
738 return ret_obj;
739 #undef MAX_CPU_ID
740 }
742 static PyObject *pyxc_xeninfo(XcObject *self)
743 {
744 xen_extraversion_t xen_extra;
745 xen_compile_info_t xen_cc;
746 xen_changeset_info_t xen_chgset;
747 xen_capabilities_info_t xen_caps;
748 xen_platform_parameters_t p_parms;
749 long xen_version;
750 long xen_pagesize;
751 char str[128];
753 xen_version = xc_version(self->xc_handle, XENVER_version, NULL);
755 if ( xc_version(self->xc_handle, XENVER_extraversion, &xen_extra) != 0 )
756 return pyxc_error_to_exception();
758 if ( xc_version(self->xc_handle, XENVER_compile_info, &xen_cc) != 0 )
759 return pyxc_error_to_exception();
761 if ( xc_version(self->xc_handle, XENVER_changeset, &xen_chgset) != 0 )
762 return pyxc_error_to_exception();
764 if ( xc_version(self->xc_handle, XENVER_capabilities, &xen_caps) != 0 )
765 return pyxc_error_to_exception();
767 if ( xc_version(self->xc_handle, XENVER_platform_parameters, &p_parms) != 0 )
768 return pyxc_error_to_exception();
770 sprintf(str, "virt_start=0x%lx", p_parms.virt_start);
772 xen_pagesize = xc_version(self->xc_handle, XENVER_pagesize, NULL);
773 if (xen_pagesize < 0 )
774 return pyxc_error_to_exception();
776 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}",
777 "xen_major", xen_version >> 16,
778 "xen_minor", (xen_version & 0xffff),
779 "xen_extra", xen_extra,
780 "xen_caps", xen_caps,
781 "xen_pagesize", xen_pagesize,
782 "platform_params", str,
783 "xen_changeset", xen_chgset,
784 "cc_compiler", xen_cc.compiler,
785 "cc_compile_by", xen_cc.compile_by,
786 "cc_compile_domain", xen_cc.compile_domain,
787 "cc_compile_date", xen_cc.compile_date);
788 }
791 static PyObject *pyxc_sedf_domain_set(XcObject *self,
792 PyObject *args,
793 PyObject *kwds)
794 {
795 uint32_t domid;
796 uint64_t period, slice, latency;
797 uint16_t extratime, weight;
798 static char *kwd_list[] = { "domid", "period", "slice",
799 "latency", "extratime", "weight",NULL };
801 if( !PyArg_ParseTupleAndKeywords(args, kwds, "iLLLhh", kwd_list,
802 &domid, &period, &slice,
803 &latency, &extratime, &weight) )
804 return NULL;
805 if ( xc_sedf_domain_set(self->xc_handle, domid, period,
806 slice, latency, extratime,weight) != 0 )
807 return pyxc_error_to_exception();
809 Py_INCREF(zero);
810 return zero;
811 }
813 static PyObject *pyxc_sedf_domain_get(XcObject *self, PyObject *args)
814 {
815 uint32_t domid;
816 uint64_t period, slice,latency;
817 uint16_t weight, extratime;
819 if(!PyArg_ParseTuple(args, "i", &domid))
820 return NULL;
822 if (xc_sedf_domain_get(self->xc_handle, domid, &period,
823 &slice,&latency,&extratime,&weight))
824 return pyxc_error_to_exception();
826 return Py_BuildValue("{s:i,s:L,s:L,s:L,s:i,s:i}",
827 "domid", domid,
828 "period", period,
829 "slice", slice,
830 "latency", latency,
831 "extratime", extratime,
832 "weight", weight);
833 }
835 static PyObject *pyxc_shadow_control(PyObject *self,
836 PyObject *args,
837 PyObject *kwds)
838 {
839 XcObject *xc = (XcObject *)self;
841 uint32_t dom;
842 int op=0;
844 static char *kwd_list[] = { "dom", "op", NULL };
846 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
847 &dom, &op) )
848 return NULL;
850 if ( xc_shadow_control(xc->xc_handle, dom, op, NULL, 0, NULL, 0, NULL)
851 < 0 )
852 return pyxc_error_to_exception();
854 Py_INCREF(zero);
855 return zero;
856 }
858 static PyObject *pyxc_shadow_mem_control(PyObject *self,
859 PyObject *args,
860 PyObject *kwds)
861 {
862 XcObject *xc = (XcObject *)self;
863 int op;
864 uint32_t dom;
865 int mbarg = -1;
866 unsigned long mb;
868 static char *kwd_list[] = { "dom", "mb", NULL };
870 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|i", kwd_list,
871 &dom, &mbarg) )
872 return NULL;
874 if ( mbarg < 0 )
875 op = XEN_DOMCTL_SHADOW_OP_GET_ALLOCATION;
876 else
877 {
878 mb = mbarg;
879 op = XEN_DOMCTL_SHADOW_OP_SET_ALLOCATION;
880 }
881 if ( xc_shadow_control(xc->xc_handle, dom, op, NULL, 0, &mb, 0, NULL) < 0 )
882 return pyxc_error_to_exception();
884 mbarg = mb;
885 return Py_BuildValue("i", mbarg);
886 }
888 static PyObject *pyxc_sched_id_get(XcObject *self) {
890 int sched_id;
891 if (xc_sched_id(self->xc_handle, &sched_id) != 0)
892 return PyErr_SetFromErrno(xc_error_obj);
894 return Py_BuildValue("i", sched_id);
895 }
897 static PyObject *pyxc_sched_credit_domain_set(XcObject *self,
898 PyObject *args,
899 PyObject *kwds)
900 {
901 uint32_t domid;
902 uint16_t weight;
903 uint16_t cap;
904 static char *kwd_list[] = { "domid", "weight", "cap", NULL };
905 static char kwd_type[] = "I|HH";
906 struct xen_domctl_sched_credit sdom;
908 weight = 0;
909 cap = (uint16_t)~0U;
910 if( !PyArg_ParseTupleAndKeywords(args, kwds, kwd_type, kwd_list,
911 &domid, &weight, &cap) )
912 return NULL;
914 sdom.weight = weight;
915 sdom.cap = cap;
917 if ( xc_sched_credit_domain_set(self->xc_handle, domid, &sdom) != 0 )
918 return pyxc_error_to_exception();
920 Py_INCREF(zero);
921 return zero;
922 }
924 static PyObject *pyxc_sched_credit_domain_get(XcObject *self, PyObject *args)
925 {
926 uint32_t domid;
927 struct xen_domctl_sched_credit sdom;
929 if( !PyArg_ParseTuple(args, "I", &domid) )
930 return NULL;
932 if ( xc_sched_credit_domain_get(self->xc_handle, domid, &sdom) != 0 )
933 return pyxc_error_to_exception();
935 return Py_BuildValue("{s:H,s:H}",
936 "weight", sdom.weight,
937 "cap", sdom.cap);
938 }
940 static PyObject *pyxc_domain_setmaxmem(XcObject *self, PyObject *args)
941 {
942 uint32_t dom;
943 unsigned int maxmem_kb;
945 if (!PyArg_ParseTuple(args, "ii", &dom, &maxmem_kb))
946 return NULL;
948 if (xc_domain_setmaxmem(self->xc_handle, dom, maxmem_kb) != 0)
949 return pyxc_error_to_exception();
951 Py_INCREF(zero);
952 return zero;
953 }
955 static PyObject *pyxc_domain_set_memmap_limit(XcObject *self, PyObject *args)
956 {
957 uint32_t dom;
958 unsigned int maplimit_kb;
960 if ( !PyArg_ParseTuple(args, "ii", &dom, &maplimit_kb) )
961 return NULL;
963 if ( xc_domain_set_memmap_limit(self->xc_handle, dom, maplimit_kb) != 0 )
964 return pyxc_error_to_exception();
966 Py_INCREF(zero);
967 return zero;
968 }
970 static PyObject *pyxc_domain_memory_increase_reservation(XcObject *self,
971 PyObject *args,
972 PyObject *kwds)
973 {
974 uint32_t dom;
975 unsigned long mem_kb;
976 unsigned int extent_order = 0 , address_bits = 0;
977 unsigned long nr_extents;
979 static char *kwd_list[] = { "domid", "mem_kb", "extent_order", "address_bits", NULL };
981 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "il|ii", kwd_list,
982 &dom, &mem_kb, &extent_order, &address_bits) )
983 return NULL;
985 /* round down to nearest power of 2. Assume callers using extent_order>0
986 know what they are doing */
987 nr_extents = (mem_kb / (XC_PAGE_SIZE/1024)) >> extent_order;
988 if ( xc_domain_memory_increase_reservation(self->xc_handle, dom,
989 nr_extents, extent_order,
990 address_bits, NULL) )
991 return pyxc_error_to_exception();
993 Py_INCREF(zero);
994 return zero;
995 }
997 static PyObject *pyxc_domain_ioport_permission(XcObject *self,
998 PyObject *args,
999 PyObject *kwds)
1001 uint32_t dom;
1002 int first_port, nr_ports, allow_access, ret;
1004 static char *kwd_list[] = { "domid", "first_port", "nr_ports", "allow_access", NULL };
1006 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iiii", kwd_list,
1007 &dom, &first_port, &nr_ports, &allow_access) )
1008 return NULL;
1010 ret = xc_domain_ioport_permission(
1011 self->xc_handle, dom, first_port, nr_ports, allow_access);
1012 if ( ret != 0 )
1013 return pyxc_error_to_exception();
1015 Py_INCREF(zero);
1016 return zero;
1019 static PyObject *pyxc_domain_irq_permission(PyObject *self,
1020 PyObject *args,
1021 PyObject *kwds)
1023 XcObject *xc = (XcObject *)self;
1024 uint32_t dom;
1025 int pirq, allow_access, ret;
1027 static char *kwd_list[] = { "domid", "pirq", "allow_access", NULL };
1029 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "iii", kwd_list,
1030 &dom, &pirq, &allow_access) )
1031 return NULL;
1033 ret = xc_domain_irq_permission(
1034 xc->xc_handle, dom, pirq, allow_access);
1035 if ( ret != 0 )
1036 return pyxc_error_to_exception();
1038 Py_INCREF(zero);
1039 return zero;
1042 static PyObject *pyxc_domain_iomem_permission(PyObject *self,
1043 PyObject *args,
1044 PyObject *kwds)
1046 XcObject *xc = (XcObject *)self;
1047 uint32_t dom;
1048 unsigned long first_pfn, nr_pfns, allow_access, ret;
1050 static char *kwd_list[] = { "domid", "first_pfn", "nr_pfns", "allow_access", NULL };
1052 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "illi", kwd_list,
1053 &dom, &first_pfn, &nr_pfns, &allow_access) )
1054 return NULL;
1056 ret = xc_domain_iomem_permission(
1057 xc->xc_handle, dom, first_pfn, nr_pfns, allow_access);
1058 if ( ret != 0 )
1059 return pyxc_error_to_exception();
1061 Py_INCREF(zero);
1062 return zero;
1065 static PyObject *pyxc_domain_set_time_offset(XcObject *self, PyObject *args)
1067 uint32_t dom;
1068 int32_t time_offset_seconds;
1069 time_t calendar_time;
1070 struct tm local_time;
1071 struct tm utc_time;
1073 if (!PyArg_ParseTuple(args, "i", &dom))
1074 return NULL;
1076 calendar_time = time(NULL);
1077 localtime_r(&calendar_time, &local_time);
1078 gmtime_r(&calendar_time, &utc_time);
1079 /* set up to get calendar time based on utc_time, with local dst setting */
1080 utc_time.tm_isdst = local_time.tm_isdst;
1081 time_offset_seconds = (int32_t)difftime(calendar_time, mktime(&utc_time));
1083 if (xc_domain_set_time_offset(self->xc_handle, dom, time_offset_seconds) != 0)
1084 return NULL;
1086 Py_INCREF(zero);
1087 return zero;
1090 static PyObject *pyxc_domain_send_trigger(XcObject *self,
1091 PyObject *args,
1092 PyObject *kwds)
1094 uint32_t dom;
1095 int trigger, vcpu = 0;
1097 static char *kwd_list[] = { "domid", "trigger", "vcpu", NULL };
1099 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "ii|i", kwd_list,
1100 &dom, &trigger, &vcpu) )
1101 return NULL;
1103 if (xc_domain_send_trigger(self->xc_handle, dom, trigger, vcpu) != 0)
1104 return pyxc_error_to_exception();
1106 Py_INCREF(zero);
1107 return zero;
1110 static PyObject *pyxc_send_debug_keys(XcObject *self,
1111 PyObject *args,
1112 PyObject *kwds)
1114 char *keys;
1116 static char *kwd_list[] = { "keys", NULL };
1118 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "s", kwd_list, &keys) )
1119 return NULL;
1121 if ( xc_send_debug_keys(self->xc_handle, keys) != 0 )
1122 return pyxc_error_to_exception();
1124 Py_INCREF(zero);
1125 return zero;
1128 static PyObject *dom_op(XcObject *self, PyObject *args,
1129 int (*fn)(int, uint32_t))
1131 uint32_t dom;
1133 if (!PyArg_ParseTuple(args, "i", &dom))
1134 return NULL;
1136 if (fn(self->xc_handle, dom) != 0)
1137 return pyxc_error_to_exception();
1139 Py_INCREF(zero);
1140 return zero;
1143 #ifdef __powerpc__
1144 static PyObject *pyxc_alloc_real_mode_area(XcObject *self,
1145 PyObject *args,
1146 PyObject *kwds)
1148 uint32_t dom;
1149 unsigned int log;
1151 static char *kwd_list[] = { "dom", "log", NULL };
1153 if ( !PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwd_list,
1154 &dom, &log) )
1155 return NULL;
1157 if ( xc_alloc_real_mode_area(self->xc_handle, dom, log) )
1158 return pyxc_error_to_exception();
1160 Py_INCREF(zero);
1161 return zero;
1163 #endif /* powerpc */
1165 static PyMethodDef pyxc_methods[] = {
1166 { "handle",
1167 (PyCFunction)pyxc_handle,
1168 METH_NOARGS, "\n"
1169 "Query the xc control interface file descriptor.\n\n"
1170 "Returns: [int] file descriptor\n" },
1172 { "domain_create",
1173 (PyCFunction)pyxc_domain_create,
1174 METH_VARARGS | METH_KEYWORDS, "\n"
1175 "Create a new domain.\n"
1176 " dom [int, 0]: Domain identifier to use (allocated if zero).\n"
1177 "Returns: [int] new domain identifier; -1 on error.\n" },
1179 { "domain_max_vcpus",
1180 (PyCFunction)pyxc_domain_max_vcpus,
1181 METH_VARARGS, "\n"
1182 "Set the maximum number of VCPUs a domain may create.\n"
1183 " dom [int, 0]: Domain identifier to use.\n"
1184 " max [int, 0]: New maximum number of VCPUs in domain.\n"
1185 "Returns: [int] 0 on success; -1 on error.\n" },
1187 { "domain_dumpcore",
1188 (PyCFunction)pyxc_domain_dumpcore,
1189 METH_VARARGS, "\n"
1190 "Dump core of a domain.\n"
1191 " dom [int]: Identifier of domain to dump core of.\n"
1192 " corefile [string]: Name of corefile to be created.\n\n"
1193 "Returns: [int] 0 on success; -1 on error.\n" },
1195 { "domain_pause",
1196 (PyCFunction)pyxc_domain_pause,
1197 METH_VARARGS, "\n"
1198 "Temporarily pause execution of a domain.\n"
1199 " dom [int]: Identifier of domain to be paused.\n\n"
1200 "Returns: [int] 0 on success; -1 on error.\n" },
1202 { "domain_unpause",
1203 (PyCFunction)pyxc_domain_unpause,
1204 METH_VARARGS, "\n"
1205 "(Re)start execution of a domain.\n"
1206 " dom [int]: Identifier of domain to be unpaused.\n\n"
1207 "Returns: [int] 0 on success; -1 on error.\n" },
1209 { "domain_destroy",
1210 (PyCFunction)pyxc_domain_destroy,
1211 METH_VARARGS, "\n"
1212 "Destroy a domain.\n"
1213 " dom [int]: Identifier of domain to be destroyed.\n\n"
1214 "Returns: [int] 0 on success; -1 on error.\n" },
1216 { "domain_destroy_hook",
1217 (PyCFunction)pyxc_domain_destroy_hook,
1218 METH_VARARGS, "\n"
1219 "Add a hook for arch stuff before destroy a domain.\n"
1220 " dom [int]: Identifier of domain to be destroyed.\n\n"
1221 "Returns: [int] 0 on success; -1 on error.\n" },
1223 { "domain_resume",
1224 (PyCFunction)pyxc_domain_resume,
1225 METH_VARARGS, "\n"
1226 "Resume execution of a suspended domain.\n"
1227 " dom [int]: Identifier of domain to be resumed.\n"
1228 " fast [int]: Use cooperative resume.\n\n"
1229 "Returns: [int] 0 on success; -1 on error.\n" },
1231 { "domain_shutdown",
1232 (PyCFunction)pyxc_domain_shutdown,
1233 METH_VARARGS, "\n"
1234 "Shutdown a domain.\n"
1235 " dom [int, 0]: Domain identifier to use.\n"
1236 " reason [int, 0]: Reason for shutdown.\n"
1237 "Returns: [int] 0 on success; -1 on error.\n" },
1239 { "vcpu_setaffinity",
1240 (PyCFunction)pyxc_vcpu_setaffinity,
1241 METH_VARARGS | METH_KEYWORDS, "\n"
1242 "Pin a VCPU to a specified set CPUs.\n"
1243 " dom [int]: Identifier of domain to which VCPU belongs.\n"
1244 " vcpu [int, 0]: VCPU being pinned.\n"
1245 " cpumap [list, []]: list of usable CPUs.\n\n"
1246 "Returns: [int] 0 on success; -1 on error.\n" },
1248 { "domain_setcpuweight",
1249 (PyCFunction)pyxc_domain_setcpuweight,
1250 METH_VARARGS | METH_KEYWORDS, "\n"
1251 "Set cpuweight scheduler parameter for domain.\n"
1252 " dom [int]: Identifier of domain to be changed.\n"
1253 " cpuweight [float, 1]: VCPU being pinned.\n"
1254 "Returns: [int] 0 on success; -1 on error.\n" },
1256 { "domain_sethandle",
1257 (PyCFunction)pyxc_domain_sethandle,
1258 METH_VARARGS, "\n"
1259 "Set domain's opaque handle.\n"
1260 " dom [int]: Identifier of domain.\n"
1261 " handle [list of 16 ints]: New opaque handle.\n"
1262 "Returns: [int] 0 on success; -1 on error.\n" },
1264 { "domain_getinfo",
1265 (PyCFunction)pyxc_domain_getinfo,
1266 METH_VARARGS | METH_KEYWORDS, "\n"
1267 "Get information regarding a set of domains, in increasing id order.\n"
1268 " first_dom [int, 0]: First domain to retrieve info about.\n"
1269 " max_doms [int, 1024]: Maximum number of domains to retrieve info"
1270 " about.\n\n"
1271 "Returns: [list of dicts] if list length is less than 'max_doms'\n"
1272 " parameter then there was an error, or the end of the\n"
1273 " domain-id space was reached.\n"
1274 " dom [int]: Identifier of domain to which this info pertains\n"
1275 " cpu [int]: CPU to which this domain is bound\n"
1276 " vcpus [int]: Number of Virtual CPUS in this domain\n"
1277 " dying [int]: Bool - is the domain dying?\n"
1278 " crashed [int]: Bool - has the domain crashed?\n"
1279 " shutdown [int]: Bool - has the domain shut itself down?\n"
1280 " paused [int]: Bool - is the domain paused by control software?\n"
1281 " blocked [int]: Bool - is the domain blocked waiting for an event?\n"
1282 " running [int]: Bool - is the domain currently running?\n"
1283 " mem_kb [int]: Memory reservation, in kilobytes\n"
1284 " maxmem_kb [int]: Maximum memory limit, in kilobytes\n"
1285 " cpu_time [long]: CPU time consumed, in nanoseconds\n"
1286 " shutdown_reason [int]: Numeric code from guest OS, explaining "
1287 "reason why it shut itself down.\n" },
1289 { "vcpu_getinfo",
1290 (PyCFunction)pyxc_vcpu_getinfo,
1291 METH_VARARGS | METH_KEYWORDS, "\n"
1292 "Get information regarding a VCPU.\n"
1293 " dom [int]: Domain to retrieve info about.\n"
1294 " vcpu [int, 0]: VCPU to retrieve info about.\n\n"
1295 "Returns: [dict]\n"
1296 " online [int]: Bool - Is this VCPU currently online?\n"
1297 " blocked [int]: Bool - Is this VCPU blocked waiting for an event?\n"
1298 " running [int]: Bool - Is this VCPU currently running on a CPU?\n"
1299 " cpu_time [long]: CPU time consumed, in nanoseconds\n"
1300 " cpumap [int]: Bitmap of CPUs this VCPU can run on\n"
1301 " cpu [int]: CPU that this VCPU is currently bound to\n" },
1303 { "linux_build",
1304 (PyCFunction)pyxc_linux_build,
1305 METH_VARARGS | METH_KEYWORDS, "\n"
1306 "Build a new Linux guest OS.\n"
1307 " dom [int]: Identifier of domain to build into.\n"
1308 " image [str]: Name of kernel image file. May be gzipped.\n"
1309 " ramdisk [str, n/a]: Name of ramdisk file, if any.\n"
1310 " cmdline [str, n/a]: Kernel parameters, if any.\n\n"
1311 " vcpus [int, 1]: Number of Virtual CPUS in domain.\n\n"
1312 "Returns: [int] 0 on success; -1 on error.\n" },
1314 { "hvm_build",
1315 (PyCFunction)pyxc_hvm_build,
1316 METH_VARARGS | METH_KEYWORDS, "\n"
1317 "Build a new HVM guest OS.\n"
1318 " dom [int]: Identifier of domain to build into.\n"
1319 " image [str]: Name of HVM loader image file.\n"
1320 " vcpus [int, 1]: Number of Virtual CPUS in domain.\n\n"
1321 "Returns: [int] 0 on success; -1 on error.\n" },
1323 { "hvm_get_param",
1324 (PyCFunction)pyxc_get_hvm_param,
1325 METH_VARARGS | METH_KEYWORDS, "\n"
1326 "get a parameter of HVM guest OS.\n"
1327 " dom [int]: Identifier of domain to build into.\n"
1328 " param [int]: No. of HVM param.\n"
1329 "Returns: [int] value of the param.\n" },
1331 { "sched_id_get",
1332 (PyCFunction)pyxc_sched_id_get,
1333 METH_NOARGS, "\n"
1334 "Get the current scheduler type in use.\n"
1335 "Returns: [int] sched_id.\n" },
1337 { "sedf_domain_set",
1338 (PyCFunction)pyxc_sedf_domain_set,
1339 METH_KEYWORDS, "\n"
1340 "Set the scheduling parameters for a domain when running with Atropos.\n"
1341 " dom [int]: domain to set\n"
1342 " period [long]: domain's scheduling period\n"
1343 " slice [long]: domain's slice per period\n"
1344 " latency [long]: domain's wakeup latency hint\n"
1345 " extratime [int]: domain aware of extratime?\n"
1346 "Returns: [int] 0 on success; -1 on error.\n" },
1348 { "sedf_domain_get",
1349 (PyCFunction)pyxc_sedf_domain_get,
1350 METH_VARARGS, "\n"
1351 "Get the current scheduling parameters for a domain when running with\n"
1352 "the Atropos scheduler."
1353 " dom [int]: domain to query\n"
1354 "Returns: [dict]\n"
1355 " domain [int]: domain ID\n"
1356 " period [long]: scheduler period\n"
1357 " slice [long]: CPU reservation per period\n"
1358 " latency [long]: domain's wakeup latency hint\n"
1359 " extratime [int]: domain aware of extratime?\n"},
1361 { "sched_credit_domain_set",
1362 (PyCFunction)pyxc_sched_credit_domain_set,
1363 METH_KEYWORDS, "\n"
1364 "Set the scheduling parameters for a domain when running with the\n"
1365 "SMP credit scheduler.\n"
1366 " domid [int]: domain id to set\n"
1367 " weight [short]: domain's scheduling weight\n"
1368 "Returns: [int] 0 on success; -1 on error.\n" },
1370 { "sched_credit_domain_get",
1371 (PyCFunction)pyxc_sched_credit_domain_get,
1372 METH_VARARGS, "\n"
1373 "Get the scheduling parameters for a domain when running with the\n"
1374 "SMP credit scheduler.\n"
1375 " domid [int]: domain id to get\n"
1376 "Returns: [dict]\n"
1377 " weight [short]: domain's scheduling weight\n"},
1379 { "evtchn_alloc_unbound",
1380 (PyCFunction)pyxc_evtchn_alloc_unbound,
1381 METH_VARARGS | METH_KEYWORDS, "\n"
1382 "Allocate an unbound port that will await a remote connection.\n"
1383 " dom [int]: Domain whose port space to allocate from.\n"
1384 " remote_dom [int]: Remote domain to accept connections from.\n\n"
1385 "Returns: [int] Unbound event-channel port.\n" },
1387 { "evtchn_reset",
1388 (PyCFunction)pyxc_evtchn_reset,
1389 METH_VARARGS | METH_KEYWORDS, "\n"
1390 "Reset all connections.\n"
1391 " dom [int]: Domain to reset.\n" },
1393 { "physdev_pci_access_modify",
1394 (PyCFunction)pyxc_physdev_pci_access_modify,
1395 METH_VARARGS | METH_KEYWORDS, "\n"
1396 "Allow a domain access to a PCI device\n"
1397 " dom [int]: Identifier of domain to be allowed access.\n"
1398 " bus [int]: PCI bus\n"
1399 " dev [int]: PCI slot\n"
1400 " func [int]: PCI function\n"
1401 " enable [int]: Non-zero means enable access; else disable access\n\n"
1402 "Returns: [int] 0 on success; -1 on error.\n" },
1404 { "readconsolering",
1405 (PyCFunction)pyxc_readconsolering,
1406 METH_VARARGS | METH_KEYWORDS, "\n"
1407 "Read Xen's console ring.\n"
1408 " clear [int, 0]: Bool - clear the ring after reading from it?\n\n"
1409 "Returns: [str] string is empty on failure.\n" },
1411 { "physinfo",
1412 (PyCFunction)pyxc_physinfo,
1413 METH_NOARGS, "\n"
1414 "Get information about the physical host machine\n"
1415 "Returns [dict]: information about the hardware"
1416 " [None]: on failure.\n" },
1418 { "xeninfo",
1419 (PyCFunction)pyxc_xeninfo,
1420 METH_NOARGS, "\n"
1421 "Get information about the Xen host\n"
1422 "Returns [dict]: information about Xen"
1423 " [None]: on failure.\n" },
1425 { "shadow_control",
1426 (PyCFunction)pyxc_shadow_control,
1427 METH_VARARGS | METH_KEYWORDS, "\n"
1428 "Set parameter for shadow pagetable interface\n"
1429 " dom [int]: Identifier of domain.\n"
1430 " op [int, 0]: operation\n\n"
1431 "Returns: [int] 0 on success; -1 on error.\n" },
1433 { "shadow_mem_control",
1434 (PyCFunction)pyxc_shadow_mem_control,
1435 METH_VARARGS | METH_KEYWORDS, "\n"
1436 "Set or read shadow pagetable memory use\n"
1437 " dom [int]: Identifier of domain.\n"
1438 " mb [int, -1]: MB of shadow memory this domain should have.\n\n"
1439 "Returns: [int] MB of shadow memory in use by this domain.\n" },
1441 { "domain_setmaxmem",
1442 (PyCFunction)pyxc_domain_setmaxmem,
1443 METH_VARARGS, "\n"
1444 "Set a domain's memory limit\n"
1445 " dom [int]: Identifier of domain.\n"
1446 " maxmem_kb [int]: .\n"
1447 "Returns: [int] 0 on success; -1 on error.\n" },
1449 { "domain_set_memmap_limit",
1450 (PyCFunction)pyxc_domain_set_memmap_limit,
1451 METH_VARARGS, "\n"
1452 "Set a domain's physical memory mappping limit\n"
1453 " dom [int]: Identifier of domain.\n"
1454 " map_limitkb [int]: .\n"
1455 "Returns: [int] 0 on success; -1 on error.\n" },
1457 { "domain_memory_increase_reservation",
1458 (PyCFunction)pyxc_domain_memory_increase_reservation,
1459 METH_VARARGS | METH_KEYWORDS, "\n"
1460 "Increase a domain's memory reservation\n"
1461 " dom [int]: Identifier of domain.\n"
1462 " mem_kb [long]: .\n"
1463 "Returns: [int] 0 on success; -1 on error.\n" },
1464 #ifdef __ia64__
1465 { "nvram_init",
1466 (PyCFunction)pyxc_nvram_init,
1467 METH_VARARGS, "\n"
1468 "Init nvram in IA64 platform\n"
1469 "Returns: [int] 0 on success; -1 on error.\n" },
1470 #endif /* __ia64__ */
1471 { "domain_ioport_permission",
1472 (PyCFunction)pyxc_domain_ioport_permission,
1473 METH_VARARGS | METH_KEYWORDS, "\n"
1474 "Allow a domain access to a range of IO ports\n"
1475 " dom [int]: Identifier of domain to be allowed access.\n"
1476 " first_port [int]: First IO port\n"
1477 " nr_ports [int]: Number of IO ports\n"
1478 " allow_access [int]: Non-zero means enable access; else disable access\n\n"
1479 "Returns: [int] 0 on success; -1 on error.\n" },
1481 { "domain_irq_permission",
1482 (PyCFunction)pyxc_domain_irq_permission,
1483 METH_VARARGS | METH_KEYWORDS, "\n"
1484 "Allow a domain access to a physical IRQ\n"
1485 " dom [int]: Identifier of domain to be allowed access.\n"
1486 " pirq [int]: The Physical IRQ\n"
1487 " allow_access [int]: Non-zero means enable access; else disable access\n\n"
1488 "Returns: [int] 0 on success; -1 on error.\n" },
1490 { "domain_iomem_permission",
1491 (PyCFunction)pyxc_domain_iomem_permission,
1492 METH_VARARGS | METH_KEYWORDS, "\n"
1493 "Allow a domain access to a range of IO memory pages\n"
1494 " dom [int]: Identifier of domain to be allowed access.\n"
1495 " first_pfn [long]: First page of I/O Memory\n"
1496 " nr_pfns [long]: Number of pages of I/O Memory (>0)\n"
1497 " allow_access [int]: Non-zero means enable access; else disable access\n\n"
1498 "Returns: [int] 0 on success; -1 on error.\n" },
1500 { "pages_to_kib",
1501 (PyCFunction)pyxc_pages_to_kib,
1502 METH_VARARGS, "\n"
1503 "Returns: [int]: The size in KiB of memory spanning the given number "
1504 "of pages.\n" },
1506 { "domain_set_time_offset",
1507 (PyCFunction)pyxc_domain_set_time_offset,
1508 METH_VARARGS, "\n"
1509 "Set a domain's time offset to Dom0's localtime\n"
1510 " dom [int]: Domain whose time offset is being set.\n"
1511 "Returns: [int] 0 on success; -1 on error.\n" },
1513 { "domain_send_trigger",
1514 (PyCFunction)pyxc_domain_send_trigger,
1515 METH_VARARGS | METH_KEYWORDS, "\n"
1516 "Send trigger to a domain.\n"
1517 " dom [int]: Identifier of domain to be sent trigger.\n"
1518 " trigger [int]: Trigger type number.\n"
1519 " vcpu [int]: VCPU to be sent trigger.\n"
1520 "Returns: [int] 0 on success; -1 on error.\n" },
1522 { "send_debug_keys",
1523 (PyCFunction)pyxc_send_debug_keys,
1524 METH_VARARGS | METH_KEYWORDS, "\n"
1525 "Inject debug keys into Xen.\n"
1526 " keys [str]: String of keys to inject.\n" },
1528 #ifdef __powerpc__
1529 { "arch_alloc_real_mode_area",
1530 (PyCFunction)pyxc_alloc_real_mode_area,
1531 METH_VARARGS | METH_KEYWORDS, "\n"
1532 "Allocate a domain's real mode area.\n"
1533 " dom [int]: Identifier of domain.\n"
1534 " log [int]: Specifies the area's size.\n"
1535 "Returns: [int] 0 on success; -1 on error.\n" },
1536 #endif /* __powerpc */
1538 { NULL, NULL, 0, NULL }
1539 };
1542 static PyObject *PyXc_getattr(PyObject *obj, char *name)
1544 return Py_FindMethod(pyxc_methods, obj, name);
1547 static PyObject *PyXc_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1549 XcObject *self = (XcObject *)type->tp_alloc(type, 0);
1551 if (self == NULL)
1552 return NULL;
1554 self->xc_handle = -1;
1556 return (PyObject *)self;
1559 static int
1560 PyXc_init(XcObject *self, PyObject *args, PyObject *kwds)
1562 if ((self->xc_handle = xc_interface_open()) == -1) {
1563 pyxc_error_to_exception();
1564 return -1;
1567 return 0;
1570 static void PyXc_dealloc(XcObject *self)
1572 if (self->xc_handle != -1) {
1573 xc_interface_close(self->xc_handle);
1574 self->xc_handle = -1;
1577 self->ob_type->tp_free((PyObject *)self);
1580 static PyTypeObject PyXcType = {
1581 PyObject_HEAD_INIT(NULL)
1582 0,
1583 PKG "." CLS,
1584 sizeof(XcObject),
1585 0,
1586 (destructor)PyXc_dealloc, /* tp_dealloc */
1587 NULL, /* tp_print */
1588 PyXc_getattr, /* tp_getattr */
1589 NULL, /* tp_setattr */
1590 NULL, /* tp_compare */
1591 NULL, /* tp_repr */
1592 NULL, /* tp_as_number */
1593 NULL, /* tp_as_sequence */
1594 NULL, /* tp_as_mapping */
1595 NULL, /* tp_hash */
1596 NULL, /* tp_call */
1597 NULL, /* tp_str */
1598 NULL, /* tp_getattro */
1599 NULL, /* tp_setattro */
1600 NULL, /* tp_as_buffer */
1601 Py_TPFLAGS_DEFAULT, /* tp_flags */
1602 "Xen client connections", /* tp_doc */
1603 NULL, /* tp_traverse */
1604 NULL, /* tp_clear */
1605 NULL, /* tp_richcompare */
1606 0, /* tp_weaklistoffset */
1607 NULL, /* tp_iter */
1608 NULL, /* tp_iternext */
1609 pyxc_methods, /* tp_methods */
1610 NULL, /* tp_members */
1611 NULL, /* tp_getset */
1612 NULL, /* tp_base */
1613 NULL, /* tp_dict */
1614 NULL, /* tp_descr_get */
1615 NULL, /* tp_descr_set */
1616 0, /* tp_dictoffset */
1617 (initproc)PyXc_init, /* tp_init */
1618 NULL, /* tp_alloc */
1619 PyXc_new, /* tp_new */
1620 };
1622 static PyMethodDef xc_methods[] = { { NULL } };
1624 PyMODINIT_FUNC initxc(void)
1626 PyObject *m;
1628 if (PyType_Ready(&PyXcType) < 0)
1629 return;
1631 m = Py_InitModule(PKG, xc_methods);
1633 if (m == NULL)
1634 return;
1636 xc_error_obj = PyErr_NewException(PKG ".Error", PyExc_RuntimeError, NULL);
1637 zero = PyInt_FromLong(0);
1639 /* KAF: This ensures that we get debug output in a timely manner. */
1640 setbuf(stdout, NULL);
1641 setbuf(stderr, NULL);
1643 Py_INCREF(&PyXcType);
1644 PyModule_AddObject(m, CLS, (PyObject *)&PyXcType);
1646 Py_INCREF(xc_error_obj);
1647 PyModule_AddObject(m, "Error", xc_error_obj);
1649 /* Expose some libxc constants to Python */
1650 PyModule_AddIntConstant(m, "XEN_SCHEDULER_SEDF", XEN_SCHEDULER_SEDF);
1651 PyModule_AddIntConstant(m, "XEN_SCHEDULER_CREDIT", XEN_SCHEDULER_CREDIT);
1656 /*
1657 * Local variables:
1658 * c-indent-level: 4
1659 * c-basic-offset: 4
1660 * End:
1661 */