2

In the link KVM api I can across the below structure in assigning memory to the guest.

struct kvm_userspace_memory_region region = {
    .slot = 0,
    .guest_phys_addr = 0x1000,
    .memory_size = 0x1000,
    .userspace_addr = (uint64_t)mem,
    };
    ioctl(vmfd, KVM_SET_USER_MEMORY_REGION, &region);

Yes, the above link explained each variable but it is hard for me to understand. The below is my understanding so far, kindly correct me if anything I misunderstand.

  • Guest physical memory is also a virtual memory but it looks like a physical memory to the guest.
  • Userspace address is the address that the user code's starting memory address.

Below is my doubts

  • What is the purpose of the slot, can I assume it as it is similar to actual memory physical slot that we can see if we open the CPU.
  • Is there any max memory limit to a single slot, If not what is the use-case of having multiple slots.
  • Is memory size denotes the size of the userspace code or the size we are requesting for a guest physical address?
  • Why in that example guest physical memory is starting from 4094 (0x1000) why it is not started from 0. If there is any memory layout or procedure, kindly explain or provide a working link, I will go through that link.
3
  • Why did you re-purpose your previous question, instead of keeping this one? Feb 10, 2020 at 10:21
  • @StephenKitt in my previous question no one added any comment or answer so it though modifying that question will not harm anyone. Also i will get a benifit of adding question in bounty. WDYT? Feb 10, 2020 at 11:05
  • You could have added the bounty to your existing question. I was working on answering the previous incarnation of this question (about PTE structure)... Feb 10, 2020 at 12:01

1 Answer 1

3
+100

What is the purpose of the slot, can I assume it as it is similar to actual memory physical slot that we can see if we open the CPU.

It’s one memory mapping. There’s no relationship to physical memory slots.

Is there any max memory limit to a single slot, if not what is the use-case of having multiple slots.

There is no limit apart from what’s available to the host. Multiple slots are useful because each memory “slot” in KVM can have different properties; currently, the mapping associated with a slot can be as-is (transparent), or log writes to it, or be read-only.

In addition, memory slots mapped in this way are intended for “physical” memory from the guest’s perspective; address space for memory-mapped I/O isn’t mapped to a memory slot. This means that a typical address space with discontiguous physical memory and interleaved MMIO areas needs multiple slots to be represented.

Is memory size denotes the size of the userspace code or the size we are requesting for a guest physical address?

It’s the size of the memory mapping: memory_size bytes are mapped into the guest memory, starting at physical address guest_phys_addr (from the guest’s perspective), using memory at linear address userspace_addr (from the host’s perspective).

Why in that example guest physical memory is starting from 4094 (0x1000) why it is not started from 0. If there is any memory layout or procedure, kindly explain or provide a working link, I will go through that link.

This is explained in the example:

For our initial state, we will preload this code into the second page of guest "physical" memory (to avoid conflicting with a non-existent real-mode interrupt descriptor table at address 0).

10
  • Tnx @StephenKitt, will guest use "userspace_addr" like a main memory (RAM) or it is just to map user code to the guest? Feb 10, 2020 at 11:47
  • The guest doesn’t use userspace_addr; that has meaning only in the host. Feb 10, 2020 at 12:02
  • we are mapping outer process userspace_addr with guest's physical address right? If not what is the use of userspace_addr in the kvm_userspace_memory_region structure? I was looking the image in the link events.static.linuxfound.org/slides/2011/linuxcon-japan/… under title "GFN to PFN in KVM" Feb 10, 2020 at 12:50
  • I thought if we write anything into a guest physical address (from a process inside guest) it will reflect in "userspace_addr (address we passed)", am i missing anything? Feb 10, 2020 at 13:02
  • I thought that you meant that the guest would use the value in userspace_addr. Your last two comments are correct: the memory is mapped at guest_phys_addr in the guest, and reads and writes there from the guest read and write the memory mapped at userspace_addr in the host. (The accesses aren’t “reflected”, they’re mapped directly.) Feb 10, 2020 at 13:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .