def read(self, path): transaction = lib.xs_transaction_start(self.xsh) contents = ffi.cast('char *', lib.xs_read(self.xsh, transaction, path, ffi.NULL)) lib.xs_transaction_end(self.xsh, transaction, False) if contents == ffi.NULL: raise XenException('failed to read file', path, errno=True) return ffi.string(contents)
def domain_setdebugging(self, domid, enable): err = lib.xc_domain_setdebugging(self.xch, domid, enable) if err != 0: raise XenException('Failed to set debugging to', enable, 'for domid', domid, errno=True)
def cleanup(): err = lib.xenforeignmemory_unmap(self.fmem, mem_page_base, num_pages) if err: raise XenException('failed to unmap xen foreign memory', mem_page_base, 'with err', err, errno=True)
def domain_get_guest_width(self, domid): word_size = ffi.new('unsigned int *') err = lib.xc_domain_get_guest_width(self.xch, domid, word_size) if err != 0: raise XenException('Failed to get word size for domain', self.domid, 'with err =', err, errno=True) return word_size[0]
def domctl(self, domid, cmd, before, after): buf = lib.xencall_alloc_buffer(self.xcall, ffi.sizeof('xen_domctl_t')) domctl = ffi.cast('xen_domctl_t *', buf) domctl.interface_version = lib.XEN_DOMCTL_INTERFACE_VERSION domctl.domain = domid domctl.cmd = cmd before(domctl.u) err = lib.xencall1(self.xcall, lib.FOO__HYPERVISOR_domctl, ffi.cast('uint64_t', buf)) try: if err != 0: raise XenException('Hypercall failed with', -err) else: return after(domctl.u) finally: lib.xencall_free_buffer(self.xcall, buf)
def map_by_mfn(self, domid, base_mfn, offset, size, prot): num_pages = (size + lib.XC_PAGE_SIZE - 1) >> lib.XC_PAGE_SHIFT pages = ffi.new('xen_pfn_t[]', [base_mfn + i for i in range(num_pages)]) mem_page_base = lib.xenforeignmemory_map(self.fmem, domid, prot, num_pages, pages, ffi.NULL) if mem_page_base == ffi.NULL: raise XenException('failed to map xen foreign memory', errno=True) def cleanup(): err = lib.xenforeignmemory_unmap(self.fmem, mem_page_base, num_pages) if err: raise XenException('failed to unmap xen foreign memory', mem_page_base, 'with err', err, errno=True) return mem_page_base + offset, cleanup
def close(self): err = lib.xencall_close(self.xcall) if err != 0: raise XenException('Failed to close xencall handle', errno=True)
def __init__(self): self.xch = lib.xc_interface_open(ffi.NULL, ffi.NULL, 0) if self.xch == ffi.NULL: raise XenException('Failed to open xenctrl handle', errno=True)
def vcpu_getcontext(self, domid, vcpu): ctxt = ffi.new('vcpu_guest_context_any_t *') err = lib.xc_vcpu_getcontext(self.xch, domid, vcpu, ctxt) if err != 0: raise XenException('Failed to get vcpu context', errno=True) return ctxt
def vcpu_setcontext(self, domid, vcpu, ctxt): err = lib.xc_vcpu_setcontext(self.xch, domid, vcpu, ctxt) if err != 0: raise XenException('Failed to set vcpu context', errno=True)
def translate_foreign_address(self, domid, vcpu, virt): mfn = lib.xc_translate_foreign_address(self.xch, domid, vcpu, virt) if mfn == 0: raise XenException('Failed to translate foreign address', errno=True) return mfn
def domain_getinfo(self, domid): dominfo = ffi.new('xc_dominfo_t *') n = lib.xc_domain_getinfo(self.xch, domid, 1, dominfo) if n != 1: raise XenException('Failed to get domain info', errno=True) return dominfo
def close(self): err = lib.xc_interface_close(self.xch) if err != 0: raise XenException('Failed to close xenctrl handle', errno=True)
def __init__(self): self.fmem = lib.xenforeignmemory_open(ffi.NULL, 0) if self.fmem == ffi.NULL: raise XenException('failed to open xen foreign memory handle', errno=True)
def close(self): err = lib.xenforeignmemory_close(self.fmem) if err: raise XenException('failed to close xen foreign memory handle:', err, errno=True)
def __init__(self): self.xsh = lib.xs_open(0) if self.xsh == ffi.NULL: raise XenException('failed to open xen store handle', errno=True)
def __init__(self): self.xcall = lib.xencall_open(ffi.NULL, 0) if self.xcall == ffi.NULL: raise XenException('Failed to open xencall handle', errno=True)
def directory(self, path): num_entries = ffi.new('unsigned int *') entries = lib.xs_directory(self.xsh, lib.XBT_NULL, path, num_entries) if entries == ffi.NULL: raise XenException('failed to read directory', path, errno=True) return [ffi.string(entries[i]) for i in range(num_entries[0])]