libxenguest

Introduction

libxenguest is a library written in C provided for the Xen Hypervisor in Dom0.

For example, it used as the low-level interface building Xen Guest domains.

Its source is located in the folder tools/libs/guest of the Xen repository.

Responsibilities

Allocating the boot memory for new & migrated VMs

One important responsibility of libxenguest is creating the memory layout of new and migrated VMs.

The boot memory setup of xenguest and libxl (used by the xl CLI command) call xc_dom_boot_mem_init() which dispatches the call to meminit_hvm() and meminit_pv() which layout, allocate and populate the boot memory of domains.

Functions

Subsections of libxenguest

xc_dom_boot_mem_init()

VM boot memory setup

xenguest’s hvm_build_setup_mem() and libxl and the xl CLI call xc_dom_boot_mem_init() to allocate and populate the domain’s system memory for booting it:

flowchart LR

subgraph libxl / xl CLI
    libxl__build_dom("libxl__build_dom()")
end

subgraph xenguest
    hvm_build_setup_mem("hvm_build_setup_mem()")
end

subgraph libxenctrl
    xc_domain_populate_physmap("One call for each memory range (extent):
    xc_domain_populate_physmap()
    xc_domain_populate_physmap()
    xc_domain_populate_physmap()")
end

subgraph libxenguest

    hvm_build_setup_mem & libxl__build_dom
        --> xc_dom_boot_mem_init("xc_dom_boot_mem_init()")

    xc_dom_boot_mem_init
        --> meminit_hvm("meminit_hvm()") & meminit_pv("meminit_pv()")
            --> xc_domain_populate_physmap
end

click xc_dom_boot_mem_init
"https://github.com/xen-project/xen/blob/39c45c/tools/libs/guest/xg_dom_boot.c#L110-L126
" _blank

click meminit_hvm
"https://github.com/xen-project/xen/blob/39c45c/tools/libs/guest/xg_dom_x86.c#L1348-L1648
" _blank

click meminit_pv
"https://github.com/xen-project/xen/blob/de0254b9/tools/libs/guest/xg_dom_x86.c#L1183-L1333
" _blank

The allocation strategies of them called functions are:

Strategy of the libxenguest meminit functions

  • Attempt to allocate 1GB superpages when possible
  • Fall back to 2MB pages when 1GB allocation failed
  • Fall back to 4k pages when both failed

They use xc_domain_populate_physmap() to perform memory allocation and to map the allocated memory to the system RAM ranges of the domain.

Strategy of xc_domain_populate_physmap()

xc_domain_populate_physmap() calls the XENMEM_populate_physmap command of the Xen memory hypercall.

For a more detailed walk-through of the inner workings of this hypercall, see the reference on xc_domain_populate_physmap().

For more details on the VM build step involving xenguest and Xen side see: https://wiki.xenproject.org/wiki/Walkthrough:_VM_build_using_xenguest