Skip to contentSkip to conversation

bypass tcg memory functions -v2

PATCH In review Glauber Costa 5 participants 13 messages Dec 18, 2008 – Dec 23, 2008 KVM

lore
Share & tools
lore
#0 lore raw

cover bypass tcg memory functions -v2

Same old story, but addressing immediate concerns. I'm not handling the overflow issue avi raised, since it's a separate deal. We can address it in a separate patch

#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 11034df..3c12c37 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 83c5f40..e4f08ef 100644
@@ -2657,11 +2657,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 648264c..d46da05 100644
@@ -910,6 +910,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 25b403e..9a52275 100644
@@ -2937,8 +2937,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 tcg_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
+                                   int len, int is_write)
 {
     int l;
     uint8_t *ptr;
@@ -2996,6 +2996,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
+        tcg_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 9a52275..b058501 100644
@@ -2240,14 +2240,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 tcg_register_physical_memory_offset(target_phys_addr_t start_addr,
                                          ram_addr_t size,
                                          ram_addr_t phys_offset,
                                          ram_addr_t region_offset)
@@ -2265,8 +2259,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;
@@ -2333,15 +2325,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
+        tcg_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 90c1413..a94ee57 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 a94ee57..8896cf8 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 5/6] hook cpu_register_physical_mem

In reply to Glauber Costa
Quoted 10 lines · Glauber Costa

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)

+static void tcg_register_physical_memory_offset(target_phys_addr_t start_addr,

I don't think TCG actually has much to do with the function. Would no_kvm be too ugly?

More philosophically, what is (Qemu /\ ~KVM /\ ~TCG)? I hope I got the math right.

#8 lore raw

Re: [Qemu-devel] [PATCH 5/6] hook cpu_register_physical_mem

Quoted 3 lines · Blue Swirl

More philosophically, what is (Qemu /\ ~KVM /\ ~TCG)? I hope I got the math right.

Your math is right, but I would add ^~translators, which leaves us with devices, and various other utilities which in total represent about 50% of the code base :-)

Laurent

#9 lore raw

Re: [Qemu-devel] [PATCH 5/6] hook cpu_register_physical_mem

In reply to Blue Swirl
Quoted 9 lines · Blue Swirl
Quoted 7 lines · Glauber Costa

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)

Quoted 8 lines
Quoted 6 lines

+static void tcg_register_physical_memory_offset(target_phys_addr_t start_addr,

I don't think TCG actually has much to do with the function.

It really does though. The way physical memory is registered and managed is TCG specific right now. It has deep hooks for invalidating TranslationBlock's, and the table structure is designed to be conducive to the access patterns of TCG.

If you think of a higher level CPU API, I think registering physical memory and reading/writing physical memory would end up being part of that API.

Regards,

Anthony Liguori

Quoted 10 lines

Would
no_kvm be too ugly?

More philosophically, what is (Qemu /\ ~KVM /\ ~TCG)? I hope I got the math right.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw

Re: [Qemu-devel] [PATCH 5/6] hook cpu_register_physical_mem

Quoted 24 lines · Anthony Liguori
Quoted 10 lines · Blue Swirl
Quoted 7 lines · Glauber Costa

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)

Quoted 7 lines
Quoted 7 lines

+static void

tcg_register_physical_memory_offset(target_phys_addr_t start_addr,

Quoted 3 lines
Quoted 2 lines
Quoted 7 lines
Quoted 2 lines

I don't think TCG actually has much to do with the function.

It really does though. The way physical memory is registered and managed
is TCG specific right now. It has deep hooks for invalidating TranslationBlock's, and the table structure is designed to be conducive to the access patterns of TCG.

Yes, but also dyngen stuff used the same structures, so it's a bit more generic than TCG-only.

Quoted 2 lines

If you think of a higher level CPU API, I think registering physical memory
and reading/writing physical memory would end up being part of that API.

Thanks, I was looking for something like this. CPU emulator is more than just TCG or dyngen and it is also ~KVM. So how about cpu_emu_register_physical_memory_offset?

Also noaccel_register_physical_memory_offset would fit.

#11 lore raw

Re: [Qemu-devel] [PATCH 5/6] hook cpu_register_physical_mem

Quoted 7 lines
Quoted 4 lines

It really does though. The way physical memory is registered and managed
is TCG specific right now. It has deep hooks for invalidating TranslationBlock's, and the table structure is designed to be conducive to the access patterns of TCG.

Yes, but also dyngen stuff used the same structures, so it's a bit more generic than TCG-only.

yeah, but dyngen is gone now. So tcg really stands for "whatever qemu does", in here.

Quoted 9 lines
Quoted 3 lines

If you think of a higher level CPU API, I think registering physical memory
and reading/writing physical memory would end up being part of that API.

Thanks, I was looking for something like this. CPU emulator is more than just TCG or dyngen and it is also ~KVM. So how about cpu_emu_register_physical_memory_offset?

Also noaccel_register_physical_memory_offset would fit.

After this revision, I must say I'm less happy with the name "accel". It's really a cpu model. I'm even thinking about changing the name of it to QEMUCPUModel or whatever. (suggestions welcome)

So "default" or "qemu" would be good choices IMHO.

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

"The less confident you are, the more serious you have to act."
#12 lore raw

Re: [Qemu-devel] [PATCH 5/6] hook cpu_register_physical_mem

Quoted 10 lines · Glauber Costa
Quoted 7 lines
Quoted 4 lines

It really does though. The way physical memory is registered and managed
is TCG specific right now. It has deep hooks for invalidating TranslationBlock's, and the table structure is designed to be conducive to the access patterns of TCG.

Yes, but also dyngen stuff used the same structures, so it's a bit more generic than TCG-only.

yeah, but dyngen is gone now. So tcg really stands for "whatever qemu
does", in here.

But something new can still come and replace TCG.

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

If you think of a higher level CPU API, I think registering physical memory
and reading/writing physical memory would end up being part of that API.

Thanks, I was looking for something like this. CPU emulator is more than just TCG or dyngen and it is also ~KVM. So how about cpu_emu_register_physical_memory_offset?

Quoted 7 lines
Quoted 1 line

Also noaccel_register_physical_memory_offset would fit.

After this revision, I must say I'm less happy with the name "accel".
It's really a cpu model. I'm even thinking about changing the name of it to QEMUCPUModel or whatever. (suggestions welcome)

So "default" or "qemu" would be good choices IMHO.

I vote for "default".