Skip to contentSkip to conversation

Bypass tcg memory functions -v1.0-2009

PATCH In review Glauber Costa 7 participants 17 messages Jan 20, 2009 – Jan 26, 2009

lore
Share & tools
lore
#0 lore raw

cover Bypass tcg memory functions -v1.0-2009

This series is not very different from the last one that did it. Just bringing it back from my vacations. Idea is to replace tcg memory functions with kvm's, selectable at runtime. Right now we use a conditional selection. In the future, we might use function pointers to allow for easy coupling of any hypervisor.

#1 lore raw

PATCH 1/6 remove smaller slots if registering a bigger one

It's like a shark eating a bunch of small fishes:
in some situations (vga linear frame buffer mapping, for example), we need to register a new slot in place of older, smaller ones. This patch handles this case

---
 kvm-all.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
kvm-all.c +10 -0
index 9fb295c..53aca0a 100644
@@ -582,6 +582,16 @@ void kvm_set_phys_mem(target_phys_addr_t start_addr,
                 kvm_set_phys_mem(mem_start, mem_size, mem_offset);
 
             return;
+        } else if (start_addr <= mem->start_addr &&
+                   (start_addr + size) >= (mem->start_addr +
+                                           mem->memory_size)) {
+            KVMSlot slot;
+            /* unregister whole slot */
+            memcpy(&slot, mem, sizeof(slot));
+            mem->memory_size = 0;
+            kvm_set_user_memory_region(s, mem);
+
+            kvm_set_phys_mem(start_addr, size, phys_offset);
         } else {
             printf("Registering overlapping slot\n");
             abort();
Signature · 1 line
1.5.6.5
#2 lore raw

PATCH 2/6 re-register whole area upon lfb unmap.

set phys_offset correctly for the whole vga area when unmapping linear vram (for vga optimization). We first register the old pieces as unassigned memory, to make things easier for kvm (and possibly other slot based implementations in the future). Replacing the region directly would make the slot management significantly more complex.

---
 hw/cirrus_vga.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
hw/cirrus_vga.c +2 -5
index ef939ae..75faafc 100644
@@ -2646,11 +2646,8 @@ static void map_linear_vram(CirrusVGAState *s)
         s->lfb_vram_mapped = 1;
         vga_dirty_log_start((VGAState *)s);
     }
-    else {
-        cpu_register_physical_memory(isa_mem_base + 0xa0000, 0x8000, s->vga_io_memory);
-        cpu_register_physical_memory(isa_mem_base + 0xa8000, 0x8000, s->vga_io_memory);
-    }
-
+    else
+        cpu_register_physical_memory(isa_mem_base + 0xa0000, 0x20000, s->vga_io_memory);
 }
 
 static void unmap_linear_vram(CirrusVGAState *s)
Signature · 1 line
1.5.6.5
#3 lore raw

PATCH 3/6 isolate io handling routine

introduce cpu_physical_memory_do_io, which handles the mmio part of cpu_physical_memory_rw. KVM can use it to do mmio, since mmio is essentially the same for both KVM and tcg.

---
 cpu-all.h |    2 +
 exec.c    |   88 ++++++++++++++++++++++++++++++++++---------------------------
 2 files changed, 51 insertions(+), 39 deletions(-)
cpu-all.h +2 -0
index ee0a6e3..0c4432a 100644
@@ -911,6 +911,8 @@ int cpu_register_io_memory(int io_index,
 CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index);
 CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index);
 
+int cpu_physical_memory_do_io(target_phys_addr_t addr, uint8_t *buf, int l,
+                            int is_write, unsigned long pd);
 void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
                             int len, int is_write);
 static inline void cpu_physical_memory_read(target_phys_addr_t addr,
exec.c +49 -39

114 lines not on this page · raw message

Signature · 1 line
1.5.6.5
#4 lore raw

PATCH 4/6 replace cpu_physical_memory_rw

This patch introduces a kvm version of cpu_physical_memory_rw. The main motivation is to bypass tcg version, which contains tcg-specific code, as well as data structures not used by kvm, such as l1_phys_map.

In this patch, I'm using a runtime selection of which function to call, but the mid-term goal is to use function pointers in a way very close to which QEMUAccel used to be.

---
 exec.c    |   13 +++++++++++--
 kvm-all.c |   44 ++++++++++++++++++++++++++++++++++++++++----
 kvm.h     |    2 ++
 3 files changed, 53 insertions(+), 6 deletions(-)
exec.c +11 -2
index f72fb96..20ac972 100644
@@ -2957,8 +2957,8 @@ int cpu_physical_memory_do_io(target_phys_addr_t addr, uint8_t *buf, int l, int
     return l;
 }
 
-void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
-                            int len, int is_write)
+static void default_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
+                                   int len, int is_write)
 {
     int l;
     uint8_t *ptr;
@@ -3016,6 +3016,15 @@ void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
     }
 }
 
+void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
+                            int len, int is_write)
+{
+    if (kvm_enabled())
+        kvm_cpu_physical_memory_rw(addr, buf, len, is_write);
+    else
+        default_physical_memory_rw(addr, buf, len, is_write);
+}
+
 /* used for ROM loading : can write in RAM and ROM */
 void cpu_physical_memory_write_rom(target_phys_addr_t addr,
                                    const uint8_t *buf, int len)
kvm-all.c +40 -4

72 lines not on this page · raw message

kvm.h +2 -0
index efce145..e3e9ca0 100644
@@ -49,6 +49,8 @@ int kvm_has_sync_mmu(void);
 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
 
+void kvm_cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
+                                int len, int is_write);
 /* internal API */
 
 struct KVMState;
Signature · 1 line
1.5.6.5
#5 lore raw

PATCH 5/6 hook cpu_register_physical_mem

Since now we have our own memory read/write function, we don't depend on all of tcg data structures anymore. So, instead of filling them up, bypass it altogether by using kvm_set_phys mem alone.

To do that, we now have to provide our own way to get page information given the address. (kvm_get_physical_page_desc)

---
 exec.c    |   44 +++++++++++++++++++++++++++++---------------
 kvm-all.c |   13 +++++++++++++
 kvm.h     |    2 ++
 3 files changed, 44 insertions(+), 15 deletions(-)
exec.c +29 -15
index 20ac972..f009e41 100644
@@ -2260,14 +2260,8 @@ static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
         }                                                               \
     } while (0)
 
-/* register physical memory. 'size' must be a multiple of the target
-   page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
-   io memory page.  The address used when calling the IO function is
-   the offset from the start of the region, plus region_offset.  Both
-   start_region and regon_offset are rounded down to a page boundary
-   before calculating this offset.  This should not be a problem unless
-   the low bits of start_addr and region_offset differ.  */
-void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,
+
+static void default_register_physical_memory_offset(target_phys_addr_t start_addr,
                                          ram_addr_t size,
                                          ram_addr_t phys_offset,
                                          ram_addr_t region_offset)
@@ -2285,8 +2279,6 @@ void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,
         kqemu_set_phys_mem(start_addr, size, phys_offset);
     }
 #endif
-    if (kvm_enabled())
-        kvm_set_phys_mem(start_addr, size, phys_offset);
 
     region_offset &= TARGET_PAGE_MASK;
     size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
@@ -2353,15 +2345,37 @@ void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,
     }
 }
 
+/* register physical memory. 'size' must be a multiple of the target
+   page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
+   io memory page.  The address used when calling the IO function is
+   the offset from the start of the region, plus region_offset.  Both
+   start_region and regon_offset are rounded down to a page boundary
+   before calculating this offset.  This should not be a problem unless
+   the low bits of start_addr and region_offset differ.  */
+void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,
+                                         ram_addr_t size,
+                                         ram_addr_t phys_offset,
+                                         ram_addr_t region_offset)
+{
+    if (kvm_enabled())
+        kvm_set_phys_mem(start_addr, size, phys_offset);
+    else
+        default_register_physical_memory_offset(start_addr, size, phys_offset, region_offset);
+}
+
 /* XXX: temporary until new memory mapping API */
 ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
 {
-    PhysPageDesc *p;
+    if (kvm_enabled()) {
+        return kvm_get_physical_page_desc(addr);
+    } else {
+        PhysPageDesc *p;
 
-    p = phys_page_find(addr >> TARGET_PAGE_BITS);
-    if (!p)
-        return IO_MEM_UNASSIGNED;
-    return p->phys_offset;
+        p = phys_page_find(addr >> TARGET_PAGE_BITS);
+        if (!p)
+            return IO_MEM_UNASSIGNED;
+        return p->phys_offset;
+    }
 }
 
 void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
kvm-all.c +13 -0
index 9cbb2f9..85f2922 100644
@@ -709,3 +709,16 @@ void kvm_cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
             kvm_cpu_physical_memory_rw(addr + l, buf + l, len - l, is_write);
     }
 }
+
+ram_addr_t kvm_get_physical_page_desc(target_phys_addr_t addr)
+{
+
+    KVMSlot *mem;
+    KVMState *s = kvm_state;
+    mem = kvm_lookup_slot(s, addr);
+
+    if (!mem)
+        return IO_MEM_UNASSIGNED;
+    else
+        return (addr - mem->start_addr) + mem->phys_offset;
+}
kvm.h +2 -0
index e3e9ca0..776cfcf 100644
@@ -51,6 +51,8 @@ int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
 
 void kvm_cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
                                 int len, int is_write);
+
+ram_addr_t kvm_get_physical_page_desc(target_phys_addr_t addr);
 /* internal API */
 
 struct KVMState;
Signature · 1 line
1.5.6.5
#6 lore raw

PATCH 6/6 cache slot lookup

record slot used in last lookup. For the common mmio case, we'll usually access the same memory slot repeatedly.

---
 kvm-all.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
kvm-all.c +10 -1
index 85f2922..6a5a98c 100644
@@ -75,16 +75,25 @@ static KVMSlot *kvm_alloc_slot(KVMState *s)
     return NULL;
 }
 
+static KVMSlot *last_slot = NULL;
+
 static KVMSlot *kvm_lookup_slot(KVMState *s, target_phys_addr_t start_addr)
 {
     int i;
 
+
+    if (last_slot && (start_addr >= last_slot->start_addr &&
+            start_addr < (last_slot->start_addr + last_slot->memory_size)))
+        return last_slot;
+
     for (i = 0; i < ARRAY_SIZE(s->slots); i++) {
         KVMSlot *mem = &s->slots[i];
 
         if (start_addr >= mem->start_addr &&
-            start_addr < (mem->start_addr + mem->memory_size))
+            start_addr < (mem->start_addr + mem->memory_size)) {
+            last_slot = mem;
             return mem;
+        }
     }
 
     return NULL;
Signature · 1 line
1.5.6.5
#7 lore raw

Re: [Qemu-devel] [PATCH 4/6] replace cpu_physical_memory_rw

In reply to Glauber Costa
Quoted 9 lines · Glauber Costa

This patch introduces a kvm version of cpu_physical_memory_rw. The main motivation is to bypass tcg version, which contains tcg-specific code, as well as data structures not used by kvm, such as l1_phys_map.

In this patch, I'm using a runtime selection of which function to call, but the mid-term goal is to use function pointers in a way very close to which QEMUAccel used to be.

You will need to trivially adjust this for cpu_physical_memory_map().

You're still allocating the page descriptors, right?

My feeling is that qemu could long term drop phys_ram_base and move to a slot based structure; most lookups will hit on the first access (the largest slot). The main motivation is to enable memory hotplug. Of course this shouldn't interfere with this patchset going in.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#8 lore raw

Re: [Qemu-devel] [PATCH 4/6] replace cpu_physical_memory_rw

Quoted 7 lines · Avi Kivity
Quoted 6 lines · Glauber Costa

This patch introduces a kvm version of cpu_physical_memory_rw. The main motivation is to bypass tcg version, which contains tcg-specific code, as well as data structures not used by kvm, such as l1_phys_map.

Quoted 6 lines
Quoted 4 lines

In this patch, I'm using a runtime selection of which function to call, but the mid-term goal is to use function pointers in a way very close to which QEMUAccel used to be.

You will need to trivially adjust this for cpu_physical_memory_map().

Was it merged yet?

Quoted 2 lines

You're still allocating the page descriptors, right?

By page descriptors you mean the l1_phys_map? If so, yes, because of the way cpu_exec works. We can
live without them right now. With other patches I have queued up, we get closer to this goal.

Quoted 6 lines

My feeling is that qemu could long term drop phys_ram_base and move to a slot based structure; most lookups will hit on the first access (the largest slot). The main motivation is to enable memory hotplug. Of course this shouldn't interfere with this patchset going in.

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#9 lore raw
In reply to Glauber Costa
Quoted 5 lines · Glauber Costa

This series is not very different from the last one that did it. Just bringing it back from my vacations. Idea is to replace tcg memory functions with kvm's, selectable at runtime. Right now we use a conditional selection. In the future, we might use function pointers to allow for easy coupling of any hypervisor.

I dislike that you're introducing two different ways or doing the same thing. Duplicating all the memory region tracking code seems like a bad way to solve this problem.

Paul

#10 lore raw
Quoted 10 lines · Paul Brook
Quoted 5 lines · Glauber Costa

This series is not very different from the last one that did it. Just bringing it back from my vacations. Idea is to replace tcg memory functions with kvm's, selectable at runtime. Right now we use a conditional selection. In the future, we might use function pointers to allow for easy coupling of any hypervisor.

I dislike that you're introducing two different ways or doing the same thing. Duplicating all the memory region tracking code seems like a bad way to solve this problem.

That's because I think I'm introducing two different ways of doing two different things that just happens to fall under the same name of "memory tracking".

tcg has a lot of stuff that hypervisors won't need, such as keeping track of a tlb, invalidating memory for the code generator, etc. It is all heavily coupled, and attempts to make it less like it, just led to a bigger number of hooks, most of them which were empty hooks. (you might remember the old QEMUAccel approach we took).

Before this patchset, memory registration for KVM is:
* kvm_set_phys_mem
* tcg stuff.

After this patchset, it is:
* kvm_set_phys_mem
* end of story.

So right now, tcg memory handling is pure overhead for us. Keeping things separated gives opportunity for better specific optimization when possible, not to mention the fact that the possibility that we break tcg while inoccently hacking KVM drops significantly in this area.

In all my laziness I agree that we should not be duplicating things. Hence why, for example, I tried to commonize the I/O functions: which are the same. (and I see no benefit in changing the way KVM keeps track of I/O regions in the near future)

#11 lore raw

Glauber Costa writes ("Re: [Qemu-devel] [PATCH 0/6] Bypass tcg memory functions -v1.0-2009"):

Quoted 8 lines · Paul Brook
Quoted 3 lines

I dislike that you're introducing two different ways or doing the same thing. Duplicating all the memory region tracking code seems like a bad way to solve this problem.

That's because I think I'm introducing two different ways of doing two different things that just happens to fall under the same name of "memory tracking".

For what it's worth I can definitely see where Glauber is coming from. The Xen tree also has a replacement implementations of things like cpu_register_io_memory, for very similar reasons.

Quoted 4 lines

In all my laziness I agree that we should not be duplicating things. Hence why, for example, I tried to commonize the I/O
functions: which are the same. (and I see no benefit in changing the way KVM keeps track of I/O regions in the near future)

I think the KVM and Xen approaches are probably similar enough that we can share this code. I'll have to look at it in detail at some point, which I don't have time to do right now or necessarily even soon.

But in the meantime I support in principle the approach that KVM are taking here.

Ian.

#12 lore raw
Quoted 2 lines

For what it's worth I can definitely see where Glauber is coming from.

I'm coming from Brazil. How did you figure it out? ;-)

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#13 lore raw
In reply to Ian Jackson
Quoted 1 line · Ian Jackson

Glauber Costa writes ("Re: [Qemu-devel] [PATCH 0/6] Bypass tcg memory

functions -v1.0-2009"):

Quoted 13 lines
Quoted 8 lines · Paul Brook
Quoted 3 lines

I dislike that you're introducing two different ways or doing the same thing. Duplicating all the memory region tracking code seems like a bad way to solve this problem.

That's because I think I'm introducing two different ways of doing two different things that just happens to fall under the same name of "memory tracking".

For what it's worth I can definitely see where Glauber is coming from. The Xen tree also has a replacement implementations of things like cpu_register_io_memory, for very similar reasons.

Quoted 8 lines
Quoted 4 lines

In all my laziness I agree that we should not be duplicating things. Hence why, for example, I tried to commonize the I/O
functions: which are the same. (and I see no benefit in changing the way KVM keeps track of I/O regions in the near future)

I think the KVM and Xen approaches are probably similar enough that we can share this code. I'll have to look at it in detail at some point, which I don't have time to do right now or necessarily even soon.

I don't see a reason why these need to be different. They're all doing basically the same thing. The low level implementation details ara a bit different, but in principle kvm, xen and tcg all need to to exactly the same
thing: Figure out what a particular physical address is mapped to.

As discussed previously, l1_phys_map is not a good solution, and needs to go away. Anypatch that involves independent code paths for kvm and tcg because "the tcg code doesn't work for kvm" sounds a lot like lazyness. The real solution is to fix the current implementation rather than adding a new one.

Paul

#14 lore raw
Quoted 4 lines · Paul Brook

I don't see a reason why these need to be different. They're all doing basically the same thing. The low level implementation details ara a bit different, but in principle kvm, xen and tcg all need to to exactly the same
thing: Figure out what a particular physical address is mapped to.

I'll try to work something out today. I agree that if we do can have something similar, let's have it.

However, as anthony poses it, we'll differ _somewhere_, and the whole question is where to hook.
In a level lower than that, we can probably share more code, true, but we'll end up with an API
that means a lot less, and much more difficult to get right.

The idea of sharing I have in mind right now is close to what I did wrt to io handling.

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#15 lore raw
In reply to Paul Brook
Quoted 7 lines · Paul Brook
Quoted 5 lines · Ian Jackson
Quoted 5 lines

In all my laziness I agree that we should not be duplicating things. Hence why, for example, I tried to commonize the I/O
functions: which are the same. (and I see no benefit in changing the way KVM keeps track of I/O regions in the near future)

Quoted 7 lines
Quoted 4 lines

I think the KVM and Xen approaches are probably similar enough that we can share this code. I'll have to look at it in detail at some point, which I don't have time to do right now or necessarily even soon.

I don't see a reason why these need to be different. They're all doing basically the same thing. The low level implementation details ara a bit different, but in principle kvm, xen and tcg all need to to exactly the same thing: Figure out what a particular physical address is mapped to.

That's a bit of a simplification. TCG needs a fair bit more than what KVM and Xen needs. TCG needs to keep track of any write operations to memory that could possibly be translated to flush the translation caches. TCG also maintains a software TLB and therefore is going to have a different set of sensitivities to the performance characteristics of RAM lookup since the performance overhead occurs on every software TLB miss.

Conversely, KVM needs to only keep track of MMIO regions since the kernel tracks RAM regions. There is no software TLB so the cost of a TLB miss is neglible. There is no concern about icache flushing because the hardware will take care of this.

That said, it may be possible to develop a unified implementation. However, it has been expressed previously (by Fabrice, among others) that there is a desire to decouple the CPU execution code (be it TCG or KVM) from the rest of QEMU so that you could even plug-in new CPU emulators rather easily. That is what this patch series is doing primarily, establishing that interface.

Quoted 2 lines

As discussed previously, l1_phys_map is not a good solution, and needs to go away. Anypatch that involves independent code paths for kvm and tcg because "the tcg code doesn't work for kvm" sounds a lot like lazyness. The real solution is to fix the current implementation rather than adding a new one.

Laziness is distinctly different from incremental development. We know this code will do well for KVM, so let's use it for KVM and iron out any problems. After someone has done the work of doing performance measurement on the TCG code and adjusting the slot code so that it can be hooked appropriate for TCG, we can convert TCG to using it, if appropriate.

At the end of the day, we're talking about 200-300 lines of code. I'm not hugely concerned that we're duplicating code within QEMU.

Regards,

Anthony Liguori

Quoted 4 lines

Paul

#16 lore raw
In reply to Paul Brook
Quoted 11 lines · Paul Brook
Quoted 6 lines · Glauber Costa

This series is not very different from the last one that did it. Just bringing it back from my vacations. Idea is to replace tcg memory functions with kvm's, selectable at runtime. Right now we use a conditional selection. In the future, we might use function pointers to allow for easy coupling of any hypervisor.

I dislike that you're introducing two different ways or doing the same thing. Duplicating all the memory region tracking code seems like a bad way to solve this problem.

To me, this patch set is really about defining the memory API for QEMU's CPU emulation/virtualization core. It can be made a lot prettier by introducing function pointers and such but doing the separation and having two implementations is the first step in that direction.

I believe this is the right level of abstraction. Trying to make shared code with a lower level interface is much more complicated because TCG has some need for deep hooks in the memory code. If TCG ever wants to use memory slots instead of a tree, I'd rather see common code abstracted into a set of libs than making the slot code the main code with hooks for TCG.

A big part of the reason for these patches is to make it possible to build QEMU without TCG in an architecturally clean way.

Regards,

Anthony Liguori

Quoted 2 lines

Paul