</>EmbeddedInterview

Implement a vCPU state save and restore structure.

MediumCoding: Systems, Bitmaps & Virtualization

Short answer: Save the virtual CPU's architectural state before suspension or migration and restore it before resuming. A simplified AArch64 core state includes general registers, stack pointer, PC and PSTATE.

#include <stdint.h>
struct cpu_state {
    uint64_t x[31], sp, pc, pstate;
};

void save_cpu(struct cpu_state *snapshot,
              const struct cpu_state *cpu)
{
    *snapshot = *cpu;
}

void restore_cpu(struct cpu_state *cpu,
                 const struct cpu_state *snapshot)
{
    *cpu = *snapshot;
}

Pause the vCPU before taking a consistent snapshot. In ARM KVM, supported registers are accessed using KVM_GET_ONE_REG and KVM_SET_ONE_REG. Real migration also needs system registers, FP/SIMD, timers, interrupt controller state, pending events, device state and a versioned portable serialization format. A raw C structure is not a portable migration image.