Multi-level page tables and userland mapping fixes.
PATCH 3/6 Fix last page errors in page_set_flags and page_check_range.
In reply to Richard HendersonThe addr < end comparison prevents the last page from being iterated; an iteration based on length avoids this problem.
--- exec.c | 54 +++++++++++++++++++++++++++--------------------------- 1 files changed, 27 insertions(+), 27 deletions(-)
exec.c +27 -27
@@ -2222,35 +2222,31 @@ void page_dump(FILE *f)int page_get_flags (target_ulong address) { -PageDesc *p; - - p =page_find (address >>TARGET_PAGE_BITS ); -if (!p) -return 0 ; -return p->flags; +PageDesc *p =page_find (address >>TARGET_PAGE_BITS ); +return p ? p->flags :0 ; } -/* modify the flags of a page and invalidate the code if -necessary. The flag PAGE_WRITE_ORG is positioned automatically -depending on PAGE_WRITE */ +/* Modify the flags of a page and invalidate the code if necessary. +The flag PAGE_WRITE_ORG is positioned automatically depending +on PAGE_WRITE. The mmap_lock should already be held. */ void page_set_flags (target_ulong start,target_ulong end,int flags) { -PageDesc *p; -target_ulong addr; +target_ulong addr, len; -/* mmap_lock should already be held. */ start = start &TARGET_PAGE_MASK ; end =TARGET_PAGE_ALIGN (end); -if (flags &PAGE_WRITE ) + +if (flags &PAGE_WRITE ) { flags |=PAGE_WRITE_ORG ; -for (addr = start; addr < end; addr +=TARGET_PAGE_SIZE ) { - p =page_find_alloc (addr >>TARGET_PAGE_BITS ); -/* We may be called for host regions that are outside guest -address space. */ -if (!p) -return ; -/* if the write protection is set, then we invalidate the code -inside */ + } + +for (addr = start, len = end - start; + len !=0 ; + len -=TARGET_PAGE_SIZE , addr +=TARGET_PAGE_SIZE ) { +PageDesc *p =page_find_alloc (addr >>TARGET_PAGE_BITS ,1 ); + +/* If the write protection bit is set, then we invalidate +the code inside. */ if (!(p->flags &PAGE_WRITE ) && (flags &PAGE_WRITE ) && p->first_tb) {
@@ -2266,18 +2262,22 @@ int page_check_range(target_ulong start, target_ulong len, int flags)target_ulong end;target_ulong addr; -if (start + len < start) -/* we've wrapped around */ +if (start + len -1 < start) { +/* We've wrapped around. */ return -1 ; + } - end =TARGET_PAGE_ALIGN (start+len);/* must do before we loose bits in the next step */ +/* END must be computed before we drop bits from START. */ + end =TARGET_PAGE_ALIGN (start + len); start = start &TARGET_PAGE_MASK ; -for (addr = start; addr < end; addr +=TARGET_PAGE_SIZE ) { +for (addr = start, len = end - start; + len !=0 ; + len -=TARGET_PAGE_SIZE , addr +=TARGET_PAGE_SIZE ) { p =page_find (addr >>TARGET_PAGE_BITS ); -if ( !p ) +if (!p)return -1 ; -if ( !(p->flags &PAGE_VALID ) ) +if (!(p->flags &PAGE_VALID ))return -1 ;if ((flags &PAGE_READ ) && !(p->flags &PAGE_READ ))
Signature · 1 line
1.6.6
PATCH 4/6 Implement multi-level page tables.
In reply to Richard HendersonUse TARGET_VIRT_ADDR_SPACE_BITS for the virtual memory map based off of l1_map. This rewrites page_find_alloc, page_flush_tb, and walk_memory_regions.
Use TARGET_PHYS_ADDR_SPACE_BITS for the physical memory map based off of l1_phys_map. This rewrites page_phys_find_alloc and phys_page_for_each.
--- cpu-all.h | 7 +- exec.c | 442 +++++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 271 insertions(+), 178 deletions(-)
cpu-all.h +5 -2
@@ -745,8 +745,11 @@ extern unsigned long qemu_host_page_mask;#define PAGE_RESERVED 0x0020void page_dump (FILE *f); -int walk_memory_regions (void *, -int (*fn)(void *,unsigned long ,unsigned long ,unsigned long )); + +typedef int (*walk_memory_regions_fn )(void *,unsigned long , +unsigned long ,unsigned long ); +int walk_memory_regions (void *,walk_memory_regions_fn ); +int page_get_flags (target_ulong address);void page_set_flags (target_ulong start,target_ulong end,int flags);int page_check_range (target_ulong start,target_ulong len,int flags);
exec.c +263 -173
545 lines not on this page · raw message
Signature · 1 line
1.6.6
cover Multi-level page tables and userland mapping fixes.
I have previously posted a variant of part 6, to address the problem of the host returning mmap results that are not page aligned for the guest. That, however, led me to the fact that we could also return addresses that are outside the guest's virtual address space.
Which raises the question of what *is* the guest's virtual address space? For a 32-bit guest, clearly we cannot return anything outside GUEST_BASE through GUEST_BASE+4G. For a 64-bit guest, the question is less clear. One thing is certain: the guest's virtual address space had better not be anything outside what page_find can support.
Which brings us to the problem of exec.c and the address spaces therein. First, there was the fact that TARGET_PHYS_ADDR_SPACE_BITS was constrained to be no larger than 32 (with a partial hack for Alpha to extend this to 42 bits). Second, that this physical address space value was applied to virtual addresses via page_find.
This patch series untangles this somewhat.
First, define separate physical and virtual address spaces for each cpu. This allows the page tables used to be no deeper than necessary in order to support what the native hardware does. E.g. 3 level page tables for Alpha's 43-bit virtual address space, rather than the 5 levels required for a full 64-bit space. I've looked up proper values for x86_64 and ppc64; I couldn't find the correct values for mips64 and sparc64, so I guessed. Certainly the guess is no worse than what is supported by the current exec.c values.
Second, implement the multi-level search within exec.c. The form of this multi-level search is taken from Tristan Gingold's es40 patches. However, he only addressed the physical address space and ignored the virtual; this patch handles both. I tried to arrange things as readably as possible here; getting too clever here is a sure-fire recipe for confusion.
Third, re-apply the mmap address fixes. This time, as promised, with a clear division between host and guest address space -- the last variant that I posted could return addresses below GUEST_BASE.
r~
Richard Henderson (6):
Move TARGET_PHYS_ADDR_SPACE_BITS to target-*/cpu.h. Use TARGET_VIRT_ADDR_SPACE_BITS in h2g_valid.
Fix last page errors in page_set_flags and page_check_range. Implement multi-level page tables.
linux-user: Use h2g_valid in qemu_vmalloc.
linux-user: Fix mmap_find_vma returning invalid addresses.
cpu-all.h | 23 ++- exec.c | 513 +++++++++++++++++++++++++++-------------------- linux-user/main.c | 7 +- linux-user/mmap.c | 111 ++++++++--- target-alpha/cpu.h | 4 +- target-arm/cpu.h | 3 + target-cris/cpu.h | 3 + target-i386/cpu.h | 11 + target-m68k/cpu.h | 3 + target-microblaze/cpu.h | 3 + target-mips/mips-defs.h | 4 + target-ppc/cpu.h | 17 ++ target-s390x/cpu.h | 5 + target-sh4/cpu.h | 3 + target-sparc/cpu.h | 8 + 15 files changed, 456 insertions(+), 262 deletions(-)
PATCH 2/6 Use TARGET_VIRT_ADDR_SPACE_BITS in h2g_valid.
Previously, only 32-bit guests had a proper check for the validity of the virtual address. Extend that check to 64-bit guests with a restricted virtual address space.
--- cpu-all.h | 16 +++++++++++----- 1 files changed, 11 insertions(+), 5 deletions(-)
cpu-all.h +11 -5
@@ -634,16 +634,22 @@ extern int have_guest_base;/* All direct uses of g2h and h2g need to go away for usermode softmmu. */ #define g2h (x) ((void *)((unsigned long)(x) + GUEST_BASE)) + +#if HOST_LONG_BITS ==TARGET_VIRT_ADDR_SPACE_BITS +#define h2g_valid (x) 1 +#else +#define h2g_valid (x) ({ \ + unsigned long __guest = (unsigned long)(x) - GUEST_BASE; \ + __guest < (1ul << TARGET_VIRT_ADDR_SPACE_BITS); \ +}) +#endif +#define h2g (x) ({ \ unsigned long __ret = (unsigned long)(x) - GUEST_BASE; \/* Check if given address fits target address space */ \ -assert (__ret == (abi_ulong )__ret); \ +assert (h2g_valid (x)); \ (abi_ulong )__ret; \ }) -#define h2g_valid (x) ({ \ - unsigned long __guest = (unsigned long)(x) - GUEST_BASE; \ - (__guest == (abi_ulong)__guest); \ -})#define saddr (x) g2h(x)#define laddr (x) g2h(x)
Signature · 1 line
1.6.6
PATCH 1/6 Move TARGET_PHYS_ADDR_SPACE_BITS to target-*/cpu.h.
In reply to Richard HendersonRemoves a set of ifdefs from exec.c.
Introduce TARGET_VIRT_ADDR_SPACE_BITS for all targets other than Alpha. This will be used for page_find_alloc, which is supposed to be using virtual addresses in the first place.
--- exec.c | 17 ----------------- target-alpha/cpu.h | 4 +++- target-arm/cpu.h | 3 +++ target-cris/cpu.h | 3 +++ target-i386/cpu.h | 11 +++++++++++ target-m68k/cpu.h | 3 +++ target-microblaze/cpu.h | 3 +++ target-mips/mips-defs.h | 4 ++++ target-ppc/cpu.h | 17 +++++++++++++++++ target-s390x/cpu.h | 5 +++++ target-sh4/cpu.h | 3 +++ target-sparc/cpu.h | 8 ++++++++ 12 files changed, 63 insertions(+), 18 deletions(-)
exec.c +0 -17
@@ -62,23 +62,6 @@#define SMC_BITMAP_USE_THRESHOLD 10 -#if defined(TARGET_SPARC64 ) -#define TARGET_PHYS_ADDR_SPACE_BITS 41 -#elif defined(TARGET_SPARC ) -#define TARGET_PHYS_ADDR_SPACE_BITS 36 -#elif defined(TARGET_ALPHA ) -#define TARGET_PHYS_ADDR_SPACE_BITS 42 -#define TARGET_VIRT_ADDR_SPACE_BITS 42 -#elif defined(TARGET_PPC64 ) -#define TARGET_PHYS_ADDR_SPACE_BITS 42 -#elif defined(TARGET_X86_64 ) -#define TARGET_PHYS_ADDR_SPACE_BITS 42 -#elif defined(TARGET_I386 ) -#define TARGET_PHYS_ADDR_SPACE_BITS 36 -#else -#define TARGET_PHYS_ADDR_SPACE_BITS 32 -#endif -static TranslationBlock *tbs;int code_gen_max_blocks;TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE ];
target-alpha/cpu.h +3 -1
@@ -41,7 +41,9 @@#define TARGET_PAGE_BITS 13 -#define VA_BITS 43 +/* ??? EV4 has 34 phys addr bits, EV5 has 40, EV6 has 44. */ +#define TARGET_PHYS_ADDR_SPACE_BITS 44 +#define TARGET_VIRT_ADDR_SPACE_BITS (30 + TARGET_PAGE_BITS)/* Alpha major type */ enum {
target-arm/cpu.h +3 -0
@@ -405,6 +405,9 @@ void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,#define TARGET_PAGE_BITS 10#endif +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#define cpu_init cpu_arm_init#define cpu_exec cpu_arm_exec#define cpu_gen_code cpu_arm_gen_code
target-cris/cpu.h +3 -0
@@ -195,6 +195,9 @@ enum {#define TARGET_PAGE_BITS 13#define MMAP_SHIFT TARGET_PAGE_BITS +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#define cpu_init cpu_cris_init#define cpu_exec cpu_cris_exec#define cpu_gen_code cpu_cris_gen_code
target-i386/cpu.h +11 -0
@@ -872,6 +872,17 @@ uint64_t cpu_get_tsc(CPUX86State *env);#define TARGET_PAGE_BITS 12 +#ifdef TARGET_X86_64 +#define TARGET_PHYS_ADDR_SPACE_BITS 52 +/* ??? This is really 48 bits, sign-extended, but the only thing +accessible to userland with bit 48 set is the VSYSCALL, and that +is handled via other mechanisms. */ +#define TARGET_VIRT_ADDR_SPACE_BITS 47 +#else +#define TARGET_PHYS_ADDR_SPACE_BITS 36 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#endif +#define cpu_init cpu_x86_init#define cpu_exec cpu_x86_exec#define cpu_gen_code cpu_x86_gen_code
target-m68k/cpu.h +3 -0
@@ -210,6 +210,9 @@ void register_m68k_insns (CPUM68KState *env);#define TARGET_PAGE_BITS 10#endif +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#define cpu_init cpu_m68k_init#define cpu_exec cpu_m68k_exec#define cpu_gen_code cpu_m68k_gen_code
target-microblaze/cpu.h +3 -0
@@ -253,6 +253,9 @@ enum {#define TARGET_PAGE_BITS 12#define MMAP_SHIFT TARGET_PAGE_BITS +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#define cpu_init cpu_mb_init#define cpu_exec cpu_mb_exec#define cpu_gen_code cpu_mb_gen_code
target-mips/mips-defs.h +4 -0
@@ -8,6 +8,10 @@#define TARGET_PAGE_BITS 12#define MIPS_TLB_MAX 128 +/* ??? MIPS64 no doubt has a larger address space. */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#if defined(TARGET_MIPS64 )#define TARGET_LONG_BITS 64#else
target-ppc/cpu.h +17 -0
@@ -29,6 +29,20 @@#define TARGET_LONG_BITS 64#define TARGET_PAGE_BITS 12 +/* Note that the official physical address space bits is 62-M where M +is implementation dependent. I've not looked up M for the set of +cpus we emulate at the system level. */ +#define TARGET_PHYS_ADDR_SPACE_BITS 62 + +/* Note that the PPC environment architecture talks about 80 bit virtual +addresses, with segmentation. Obviously that's not all visible to a +single process, which is all we're concerned with here. */ +#ifdef TARGET_ABI32 +# define TARGET_VIRT_ADDR_SPACE_BITS 32 +#else +# define TARGET_VIRT_ADDR_SPACE_BITS 64 +#endif +#else /* defined (TARGET_PPC64) */ /* PowerPC 32 definitions */ #define TARGET_LONG_BITS 32
@@ -50,6 +64,9 @@#define TARGET_PAGE_BITS 12#endif /* defined(TARGET_PPCEMB) */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#endif /* defined (TARGET_PPC64) */ #define CPUState struct CPUPPCState
target-s390x/cpu.h +5 -0
@@ -100,6 +100,11 @@ int cpu_s390x_handle_mmu_fault (CPUS390XState *env, target_ulong address, int rw#define TARGET_PAGE_BITS 12 +/* ??? This is certainly wrong for 64-bit s390x, but given that only KVM +emulation actually works, this is good enough for a placeholder. */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#ifndef CONFIG_USER_ONLY extern int s390_virtio_hypercall (CPUState *env);extern void kvm_s390_virtio_irq (CPUState *env,int config_change,uint64_t token);
target-sh4/cpu.h +3 -0
@@ -44,6 +44,9 @@#define TARGET_PAGE_BITS 12/* 4k XXXXX */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#define SR_MD (1 << 30)#define SR_RB (1 << 29)#define SR_BL (1 << 28)
target-sparc/cpu.h +8 -0
@@ -7,10 +7,18 @@#define TARGET_LONG_BITS 32#define TARGET_FPREGS 32#define TARGET_PAGE_BITS 12/* 4k */ + +#define TARGET_PHYS_ADDR_SPACE_BITS 41 + +/* ??? This is a guess based on PAGE_OFFSET in the Linux kernel. */ +#define TARGET_VIRT_ADDR_SPACE_BITS 41 +#else #define TARGET_LONG_BITS 64#define TARGET_FPREGS 64#define TARGET_PAGE_BITS 13/* 8k */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32#endif #define CPUState struct CPUSPARCState
Signature · 1 line
1.6.6
PATCH 6/6 linux-user: Fix mmap_find_vma returning invalid addresses.
In reply to Richard HendersonDon't return addresses that aren't properly aligned for the guest, e.g. when the guest has a larger page size than the host. Don't return addresses that are outside the virtual address space for the target, by paying proper attention to the h2g/g2h macros.
At the same time, place the default mapping base for 64-bit guests (on 64-bit hosts) outside the low 4G. Consistently interpret mmap_next_start in the guest address space.
--- linux-user/main.c | 7 +--- linux-user/mmap.c | 102 ++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 80 insertions(+), 29 deletions(-)
linux-user/main.c +2 -5
@@ -2725,12 +2725,9 @@ int main(int argc, char **argv, char **envp)/* * Read in mmap_min_addr kernel parameter. This value is used * When loading the ELF image to determine whether guest_base -* is needed. -* -* When user has explicitly set the quest base, we skip this -* test. +* is needed. It is also used in mmap_find_vma. */ -if (!have_guest_base) { + {FILE *fp;if ((fp =fopen ("/proc/sys/vm/mmap_min_addr" ,"r" )) !=NULL ) {
linux-user/mmap.c +78 -24
140 lines not on this page · raw message
Signature · 1 line
1.6.6
PATCH 5/6 linux-user: Use h2g_valid in qemu_vmalloc.
In reply to Richard Henderson--- linux-user/mmap.c | 9 ++++----- 1 files changed, 4 insertions(+), 5 deletions(-)
linux-user/mmap.c +4 -5
@@ -80,16 +80,15 @@ void mmap_unlock(void)void *qemu_vmalloc (size_t size) {void *p; -unsigned long addr; +mmap_lock ();/* Use map and mark the pages as used. */ p =mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); - addr = (unsigned long )p; -if (addr == (target_ulong ) addr) { -/* Allocated region overlaps guest address space. -This may recurse. */ +if (h2g_valid (p)) { +/* Allocated region overlaps guest address space. This may recurse. */ +unsigned long addr =h2g (p);page_set_flags (addr &TARGET_PAGE_MASK ,TARGET_PAGE_ALIGN (addr + size),PAGE_RESERVED ); }
Signature · 1 line
1.6.6
Re: [Qemu-devel] [PATCH 3/6] Fix last page errors in page_set_flags and page_check_range.
In reply to Richard HendersonQuoted 2 lines · Richard Henderson
The addr < end comparison prevents the last page from being iterated; an iteration based on length avoids this problem.
Please make separate patches for unrelated changes. Now the essence of the patch is very hard to see. Also pure formatting changes are not very useful.
Quoted 11 lines
---
exec.c | 54 +++++++++++++++++++++++++++---------------------------1 files changed, 27 insertions(+), 27 deletions(-)exec.c +0
-0@@ -2222,35 +2222,31 @@ void page_dump(FILE *f) int page_get_flags(target_ulong address)
exec.c void page_dump(FILE *f) · line 2227
Quoted 10 lines
exec.c +2
-6{ - PageDesc *p; - - p = page_find(address >> TARGET_PAGE_BITS); - if (!p) - return 0; - return p->flags; + PageDesc *p = page_find(address >> TARGET_PAGE_BITS); + return p ? p->flags : 0; }
For example this change does not make any difference but just add confusion.
Quoted 14 lines
exec.c +4
-6-/* modify the flags of a page and invalidate the code if -necessary. The flag PAGE_WRITE_ORG is positioned automatically -depending on PAGE_WRITE */ +/* Modify the flags of a page and invalidate the code if necessary. +The flag PAGE_WRITE_ORG is positioned automatically depending +on PAGE_WRITE. The mmap_lock should already be held. */ void page_set_flags(target_ulong start, target_ulong end, int flags) { - PageDesc *p; - target_ulong addr; + target_ulong addr, len; - /* mmap_lock should already be held. */
exec.c void page_dump(FILE *f) · line 2249
Quoted 10 lines
exec.c +2
-5start = start & TARGET_PAGE_MASK; end = TARGET_PAGE_ALIGN(end); - if (flags & PAGE_WRITE) + + if (flags & PAGE_WRITE) { flags |= PAGE_WRITE_ORG; - for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) { - p = page_find_alloc(addr >> TARGET_PAGE_BITS); - /* We may be called for host regions that are outside guest -address space. */
Why remove the comment, is it no longer true?
Quoted 51 lines
exec.c +9
-4- if (!p) - return; - /* if the write protection is set, then we invalidate the code -inside */ + } + + for (addr = start, len = end - start; + len != 0; + len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { + PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1); + + /* If the write protection bit is set, then we invalidate +the code inside. */ if (!(p->flags &
PAGE_WRITE) &&
(flags &PAGE_WRITE) &&
p->first_tb) {exec.c +10
-7@@ -2266,18 +2262,22 @@ int page_check_range(target_ulong start, target_ulong len, int flags) target_ulong end; target_ulong addr; - if (start + len < start) - /* we've wrapped around */ + if (start + len - 1 < start) { + /* We've wrapped around. */ return -1; + } - end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */ + /* END must be computed before we drop bits from START. */ + end = TARGET_PAGE_ALIGN(start + len); start = start & TARGET_PAGE_MASK; - for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) { + for (addr = start, len = end - start; + len != 0; + len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { p = page_find(addr >> TARGET_PAGE_BITS); - if( !p ) + if (!p) return -1; - if( !(p->flags & PAGE_VALID) ) + if (!(p->flags & PAGE_VALID)) return -1; if ((flags & PAGE_READ) && !(p->flags & PAGE_READ)) --1.6.6
Re: [Qemu-devel] [PATCH 1/6] Move TARGET_PHYS_ADDR_SPACE_BITS to target-*/cpu.h.
In reply to Richard HendersonQuoted 167 lines · Richard Henderson
Removes a set of ifdefs from
exec.c.Introduce
TARGET_VIRT_ADDR_SPACE_BITSfor all targets other than Alpha. This will be used forpage_find_alloc, which is supposed to be using virtual addresses in the first place.
---
exec.c | 17 -----------------target-alpha/cpu.h| 4 +++-target-arm/cpu.h| 3 +++target-cris/cpu.h | 3 +++ target-i386/cpu.h | 11 +++++++++++ target-m68k/cpu.h | 3 +++target-microblaze/cpu.h | 3 +++
target-mips/mips-defs.h | 4 ++++
target-ppc/cpu.h | 17 +++++++++++++++++
target-s390x/cpu.h | 5 +++++
target-sh4/cpu.h | 3 +++
target-sparc/cpu.h | 8 ++++++++12 files changed, 63 insertions(+), 18 deletions(-)exec.c +0
-17@@ -62,23 +62,6 @@ #define SMC_BITMAP_USE_THRESHOLD 10 -#if defined(TARGET_SPARC64 ) -#define TARGET_PHYS_ADDR_SPACE_BITS 41 -#elif defined(TARGET_SPARC ) -#define TARGET_PHYS_ADDR_SPACE_BITS 36 -#elif defined(TARGET_ALPHA ) -#define TARGET_PHYS_ADDR_SPACE_BITS 42 -#define TARGET_VIRT_ADDR_SPACE_BITS 42 -#elif defined(TARGET_PPC64 ) -#define TARGET_PHYS_ADDR_SPACE_BITS 42 -#elif defined(TARGET_X86_64 ) -#define TARGET_PHYS_ADDR_SPACE_BITS 42 -#elif defined(TARGET_I386 ) -#define TARGET_PHYS_ADDR_SPACE_BITS 36 -#else -#define TARGET_PHYS_ADDR_SPACE_BITS 32 -#endif - static TranslationBlock *tbs; int code_gen_max_blocks; TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];target-alpha/cpu.h +3
-1@@ -41,7 +41,9 @@ #define TARGET_PAGE_BITS 13 -#define VA_BITS 43 +/* ??? EV4 has 34 phys addr bits, EV5 has 40, EV6 has 44. */ +#define TARGET_PHYS_ADDR_SPACE_BITS 44 +#define TARGET_VIRT_ADDR_SPACE_BITS (30 + TARGET_PAGE_BITS) /* Alpha major type */ enum {target-arm/cpu.h +3
-0@@ -405,6 +405,9 @@ void cpu_arm_set_cp_io(CPUARMState *env, int cpnum, #define TARGET_PAGE_BITS 10 #endif +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 + #define cpu_init cpu_arm_init #define cpu_exec cpu_arm_exec #define cpu_gen_code cpu_arm_gen_codetarget-cris/cpu.h +3
-0@@ -195,6 +195,9 @@ enum { #define TARGET_PAGE_BITS 13 #define MMAP_SHIFT TARGET_PAGE_BITS +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 + #define cpu_init cpu_cris_init #define cpu_exec cpu_cris_exec #define cpu_gen_code cpu_cris_gen_codetarget-i386/cpu.h +11
-0@@ -872,6 +872,17 @@ uint64_t cpu_get_tsc(CPUX86State *env); #define TARGET_PAGE_BITS 12 +#ifdef TARGET_X86_64 +#define TARGET_PHYS_ADDR_SPACE_BITS 52 +/* ??? This is really 48 bits, sign-extended, but the only thing +accessible to userland with bit 48 set is the VSYSCALL, and that +is handled via other mechanisms. */ +#define TARGET_VIRT_ADDR_SPACE_BITS 47 +#else +#define TARGET_PHYS_ADDR_SPACE_BITS 36 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#endif + #define cpu_init cpu_x86_init #define cpu_exec cpu_x86_exec #define cpu_gen_code cpu_x86_gen_codetarget-m68k/cpu.h +3
-0@@ -210,6 +210,9 @@ void register_m68k_insns (CPUM68KState *env); #define TARGET_PAGE_BITS 10 #endif +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 + #define cpu_init cpu_m68k_init #define cpu_exec cpu_m68k_exec #define cpu_gen_code cpu_m68k_gen_codetarget-microblaze/cpu.h +3
-0@@ -253,6 +253,9 @@ enum { #define TARGET_PAGE_BITS 12 #define MMAP_SHIFT TARGET_PAGE_BITS +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 + #define cpu_init cpu_mb_init #define cpu_exec cpu_mb_exec #define cpu_gen_code cpu_mb_gen_codetarget-mips/mips-defs.h +4
-0@@ -8,6 +8,10 @@ #define TARGET_PAGE_BITS 12 #define MIPS_TLB_MAX 128 +/* ??? MIPS64 no doubt has a larger address space. */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 + #if defined(TARGET_MIPS64) #define TARGET_LONG_BITS 64 #elsetarget-ppc/cpu.h +3
-0@@ -29,6 +29,20 @@ #define TARGET_LONG_BITS 64 #define TARGET_PAGE_BITS 12 +/* Note that the official physical address space bits is 62-M where M +is implementation dependent. I've not looked up M for the set of +cpus we emulate at the system level. */
target-ppc/cpu.h line 44
Quoted 10 lines
target-ppc/cpu.h +10
-0+#define TARGET_PHYS_ADDR_SPACE_BITS 62 + +/* Note that the PPC environment architecture talks about 80 bit virtual +addresses, with segmentation. Obviously that's not all visible to a +single process, which is all we're concerned with here. */ +#ifdef TARGET_ABI32 +# define TARGET_VIRT_ADDR_SPACE_BITS 32 +#else +# define TARGET_VIRT_ADDR_SPACE_BITS 64 +#endif
I'd suppose this change applies to all targets with ABI32, not just PPC.
Quoted 49 lines
target-ppc/cpu.h +4
-0+ #else /* defined (TARGET_PPC64) */ /* PowerPC 32 definitions */ #define TARGET_LONG_BITS 32@@ -50,6 +64,9 @@ #define TARGET_PAGE_BITS 12 #endif /* defined(TARGET_PPCEMB) */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 + #endif /* defined (TARGET_PPC64) */ #define CPUState struct CPUPPCStatetarget-s390x/cpu.h +5
-0@@ -100,6 +100,11 @@ int cpu_s390x_handle_mmu_fault (CPUS390XState *env, target_ulong address, int rw #define TARGET_PAGE_BITS 12 +/* ??? This is certainly wrong for 64-bit s390x, but given that only KVM +emulation actually works, this is good enough for a placeholder. */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 + #ifndef CONFIG_USER_ONLY extern int s390_virtio_hypercall(CPUState *env); extern void kvm_s390_virtio_irq(CPUState *env, int config_change, uint64_t token);target-sh4/cpu.h +3
-0@@ -44,6 +44,9 @@ #define TARGET_PAGE_BITS 12 /* 4k XXXXX */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 + #define SR_MD (1 << 30) #define SR_RB (1 << 29) #define SR_BL (1 << 28)target-sparc/cpu.h +0
-0
target-sparc/cpu.h line 14
Quoted 8 lines
target-sparc/cpu.h +5
-0@@ -7,10 +7,18 @@ #define TARGET_LONG_BITS 32 #define TARGET_FPREGS 32 #define TARGET_PAGE_BITS 12 /* 4k */ + +#define TARGET_PHYS_ADDR_SPACE_BITS 41 + +/* ??? This is a guess based on PAGE_OFFSET in the Linux kernel. */ +#define TARGET_VIRT_ADDR_SPACE_BITS 41
No need to guess, the value for UltraSparc IIi is 44.
target-sparc/cpu.h line 20
Quoted 6 lines
target-sparc/cpu.h +2
-0+ #else #define TARGET_LONG_BITS 64 #define TARGET_FPREGS 64 #define TARGET_PAGE_BITS 13 /* 8k */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32
No. Please use the original value.
Quoted 10 lines
target-sparc/cpu.h +1
-1+#define TARGET_VIRT_ADDR_SPACE_BITS 32 #endif #define CPUState struct CPUSPARCState --1.6.6
Re: [Qemu-devel] [PATCH 3/6] Fix last page errors in page_set_flags and page_check_range.
In reply to Blue SwirlQuoted 3 lines · Blue Swirl
Please make separate patches for unrelated changes. Now the essence of the patch is very hard to see. Also pure formatting changes are not very useful.
Is this just about page_get_flags, or was there some other pure formatting change to which you object? For instance:
Quoted 6 lines
exec.c int page_check_range(target_ulong start, target_ulong len, int flags) · line 2268
Quoted 6 lines
exec.c +3
-2-if (start + len < start) -/* we've wrapped around */ +if (start + len -1 < start) { +/* We've wrapped around. */ return -1 ; + }
Only the first line is required to fix an off-by-one error. But if I don't add the braces, generally the patch will be bounced for that.
Quoted 4 lines
exec.c void page_dump(FILE *f) · line 2249
Quoted 2 lines
exec.c +0
-2-/* We may be called for host regions that are outside guest -address space. */ Why remove the comment, is it no longer true?
Yes, after the entire patch series is applied. Indeed, I believe that many of the addresses rejected here were *not* in fact outside the guest address space, merely that page_find_alloc did not accurately know what the guest address space was.
I think it was a mistake to ever consider usage of this function with out-of-band addresses as valid -- it adds a great deal of confusion.
I could add an assertion here to make sure, if you like.
If this were C++, it might have been interesting to try to use the type system to keep the host and guest address spaces forcibly separate. But since this is plain C, I think wrapping everything in structures and access macros would just be too ugly.
r~
Re: [Qemu-devel] [PATCH 1/6] Move TARGET_PHYS_ADDR_SPACE_BITS to target-*/cpu.h.
In reply to Blue SwirlQuoted 7 lines · Blue Swirl
target-ppc/cpu.h line 44
Quoted 5 lines
target-ppc/cpu.h +5
-0+#ifdef TARGET_ABI32 +# define TARGET_VIRT_ADDR_SPACE_BITS 32 +#else +# define TARGET_VIRT_ADDR_SPACE_BITS 64 +#endif I'd suppose this change applies to all targets with ABI32, not just PPC.
Indeed. Odd that sparc32plus didn't yield errors building, while ppc did -- that's how I noticed the funny config macro in the first place.
Quoted 3 lines
Quoted 1 line
+#define
TARGET_PHYS_ADDR_SPACE_BITS32No. Please use the original value.
Oops. Thanks for the catch.
r~
Re: [Qemu-devel] [PATCH 3/6] Fix last page errors in page_set_flags and page_check_range.
In reply to Richard HendersonQuoted 8 lines · Richard Henderson
Quoted 4 lines · Blue Swirl
Please make separate patches for unrelated changes. Now the essence of the patch is very hard to see. Also pure formatting changes are not very useful.
Is this just about
page_get_flags, or was there some other pure formatting change to which you object? For instance:
Also this one:
- end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose
bits in the next step */
+ /* END must be computed before we drop bits from START. */
+ end = TARGET_PAGE_ALIGN(start + len);
start = start & TARGET_PAGE_MASK;and these:
- if( !p )
+ if (!p)
return -1;
- if( !(p->flags & PAGE_VALID) )
+ if (!(p->flags & PAGE_VALID))
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
- if (start + len < start)
- /* we've wrapped around */
+ if (start + len - 1 < start) {
+ /* We've wrapped around. */
return -1;
+ }Only the first line is required to fix an off-by-one error. But if I don't add the braces, generally the patch will be bounced for that.
That change is OK.
Quoted 23 lines
Quoted 4 lines
Quoted 2 lines
- /* We may be called for host regions that are outside guest
- address space. */Why remove the comment, is it no longer true?
Yes, after the entire patch series is applied. Indeed, I believe that many of the addresses rejected here were *not* in fact outside the guest address space, merely that
page_find_allocdid not accurately know what the guest address space was.I think it was a mistake to ever consider usage of this function with out-of-band addresses as valid -- it adds a great deal of confusion.
I could add an assertion here to make sure, if you like.
If this were C++, it might have been interesting to try to use the type system to keep the host and guest address spaces forcibly separate. But since this is plain C, I think wrapping everything in structures and access macros would just be too ugly.
r~
cover Multi-level page tables and userland mapping fixes, v2
In reply to Richard HendersonChanges since v1:
* Sparc virt and phys address range corrections.
* Unrelated changes removed.
* Assertions added for guest address space.
r~
Richard Henderson (7):
Move TARGET_PHYS_ADDR_SPACE_BITS to target-*/cpu.h. Use TARGET_VIRT_ADDR_SPACE_BITS in h2g_valid.
Fix last page errors in page_set_flags and page_check_range. Implement multi-level page tables.
linux-user: Use h2g_valid in qemu_vmalloc.
linux-user: Fix mmap_find_vma returning invalid addresses. Assert arguments in range for guest address space.
cpu-all.h | 23 ++- exec.c | 513 ++++++++++++++++++++++++++++------------------- linux-user/main.c | 7 +- linux-user/mmap.c | 111 ++++++++--- target-alpha/cpu.h | 4 +- target-arm/cpu.h | 3 + target-cris/cpu.h | 3 + target-i386/cpu.h | 11 + target-m68k/cpu.h | 3 + target-microblaze/cpu.h | 3 + target-mips/mips-defs.h | 4 + target-ppc/cpu.h | 17 ++ target-s390x/cpu.h | 5 + target-sh4/cpu.h | 3 + target-sparc/cpu.h | 8 + 15 files changed, 465 insertions(+), 253 deletions(-)
PATCH 1/7 Move TARGET_PHYS_ADDR_SPACE_BITS to target-*/cpu.h.
Removes a set of ifdefs from exec.c.
Introduce TARGET_VIRT_ADDR_SPACE_BITS for all targets other than Alpha. This will be used for page_find_alloc, which is supposed to be using virtual addresses in the first place.
--- exec.c | 17 ----------------- target-alpha/cpu.h | 4 +++- target-arm/cpu.h | 3 +++ target-cris/cpu.h | 3 +++ target-i386/cpu.h | 11 +++++++++++ target-m68k/cpu.h | 3 +++ target-microblaze/cpu.h | 3 +++ target-mips/mips-defs.h | 4 ++++ target-ppc/cpu.h | 17 +++++++++++++++++ target-s390x/cpu.h | 5 +++++ target-sh4/cpu.h | 3 +++ target-sparc/cpu.h | 8 ++++++++ 12 files changed, 63 insertions(+), 18 deletions(-)
exec.c +0 -17
@@ -62,23 +62,6 @@#define SMC_BITMAP_USE_THRESHOLD 10 -#if defined(TARGET_SPARC64 ) -#define TARGET_PHYS_ADDR_SPACE_BITS 41 -#elif defined(TARGET_SPARC ) -#define TARGET_PHYS_ADDR_SPACE_BITS 36 -#elif defined(TARGET_ALPHA ) -#define TARGET_PHYS_ADDR_SPACE_BITS 42 -#define TARGET_VIRT_ADDR_SPACE_BITS 42 -#elif defined(TARGET_PPC64 ) -#define TARGET_PHYS_ADDR_SPACE_BITS 42 -#elif defined(TARGET_X86_64 ) -#define TARGET_PHYS_ADDR_SPACE_BITS 42 -#elif defined(TARGET_I386 ) -#define TARGET_PHYS_ADDR_SPACE_BITS 36 -#else -#define TARGET_PHYS_ADDR_SPACE_BITS 32 -#endif -static TranslationBlock *tbs;int code_gen_max_blocks;TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE ];
target-alpha/cpu.h +3 -1
@@ -41,7 +41,9 @@#define TARGET_PAGE_BITS 13 -#define VA_BITS 43 +/* ??? EV4 has 34 phys addr bits, EV5 has 40, EV6 has 44. */ +#define TARGET_PHYS_ADDR_SPACE_BITS 44 +#define TARGET_VIRT_ADDR_SPACE_BITS (30 + TARGET_PAGE_BITS)/* Alpha major type */ enum {
target-arm/cpu.h +3 -0
@@ -405,6 +405,9 @@ void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,#define TARGET_PAGE_BITS 10#endif +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#define cpu_init cpu_arm_init#define cpu_exec cpu_arm_exec#define cpu_gen_code cpu_arm_gen_code
target-cris/cpu.h +3 -0
@@ -200,6 +200,9 @@ enum {#define TARGET_PAGE_BITS 13#define MMAP_SHIFT TARGET_PAGE_BITS +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#define cpu_init cpu_cris_init#define cpu_exec cpu_cris_exec#define cpu_gen_code cpu_cris_gen_code
target-i386/cpu.h +11 -0
@@ -872,6 +872,17 @@ uint64_t cpu_get_tsc(CPUX86State *env);#define TARGET_PAGE_BITS 12 +#ifdef TARGET_X86_64 +#define TARGET_PHYS_ADDR_SPACE_BITS 52 +/* ??? This is really 48 bits, sign-extended, but the only thing +accessible to userland with bit 48 set is the VSYSCALL, and that +is handled via other mechanisms. */ +#define TARGET_VIRT_ADDR_SPACE_BITS 47 +#else +#define TARGET_PHYS_ADDR_SPACE_BITS 36 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#endif +#define cpu_init cpu_x86_init#define cpu_exec cpu_x86_exec#define cpu_gen_code cpu_x86_gen_code
target-m68k/cpu.h +3 -0
@@ -210,6 +210,9 @@ void register_m68k_insns (CPUM68KState *env);#define TARGET_PAGE_BITS 10#endif +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#define cpu_init cpu_m68k_init#define cpu_exec cpu_m68k_exec#define cpu_gen_code cpu_m68k_gen_code
target-microblaze/cpu.h +3 -0
@@ -253,6 +253,9 @@ enum {#define TARGET_PAGE_BITS 12#define MMAP_SHIFT TARGET_PAGE_BITS +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#define cpu_init cpu_mb_init#define cpu_exec cpu_mb_exec#define cpu_gen_code cpu_mb_gen_code
target-mips/mips-defs.h +4 -0
@@ -8,6 +8,10 @@#define TARGET_PAGE_BITS 12#define MIPS_TLB_MAX 128 +/* ??? MIPS64 no doubt has a larger address space. */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#if defined(TARGET_MIPS64 )#define TARGET_LONG_BITS 64#else
target-ppc/cpu.h +17 -0
@@ -29,6 +29,20 @@#define TARGET_LONG_BITS 64#define TARGET_PAGE_BITS 12 +/* Note that the official physical address space bits is 62-M where M +is implementation dependent. I've not looked up M for the set of +cpus we emulate at the system level. */ +#define TARGET_PHYS_ADDR_SPACE_BITS 62 + +/* Note that the PPC environment architecture talks about 80 bit virtual +addresses, with segmentation. Obviously that's not all visible to a +single process, which is all we're concerned with here. */ +#ifdef TARGET_ABI32 +# define TARGET_VIRT_ADDR_SPACE_BITS 32 +#else +# define TARGET_VIRT_ADDR_SPACE_BITS 64 +#endif +#else /* defined (TARGET_PPC64) */ /* PowerPC 32 definitions */ #define TARGET_LONG_BITS 32
@@ -50,6 +64,9 @@#define TARGET_PAGE_BITS 12#endif /* defined(TARGET_PPCEMB) */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#endif /* defined (TARGET_PPC64) */ #define CPUState struct CPUPPCState
target-s390x/cpu.h +5 -0
@@ -100,6 +100,11 @@ int cpu_s390x_handle_mmu_fault (CPUS390XState *env, target_ulong address, int rw#define TARGET_PAGE_BITS 12 +/* ??? This is certainly wrong for 64-bit s390x, but given that only KVM +emulation actually works, this is good enough for a placeholder. */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#ifndef CONFIG_USER_ONLY extern int s390_virtio_hypercall (CPUState *env);extern void kvm_s390_virtio_irq (CPUState *env,int config_change,uint64_t token);
target-sh4/cpu.h +3 -0
@@ -44,6 +44,9 @@#define TARGET_PAGE_BITS 12/* 4k XXXXX */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#define SR_MD (1 << 30)#define SR_RB (1 << 29)#define SR_BL (1 << 28)
target-sparc/cpu.h +8 -0
@@ -7,10 +7,18 @@#define TARGET_LONG_BITS 32#define TARGET_FPREGS 32#define TARGET_PAGE_BITS 12/* 4k */ +#define TARGET_PHYS_ADDR_SPACE_BITS 41 +# ifdef TARGET_ABI32 +# define TARGET_VIRT_ADDR_SPACE_BITS 32 +# else +# define TARGET_VIRT_ADDR_SPACE_BITS 44 +# endif #else #define TARGET_LONG_BITS 64#define TARGET_FPREGS 64#define TARGET_PAGE_BITS 13/* 8k */ +#define TARGET_PHYS_ADDR_SPACE_BITS 36 +#define TARGET_VIRT_ADDR_SPACE_BITS 32#endif #define CPUState struct CPUSPARCState
Signature · 1 line
1.6.6
PATCH 2/7 Use TARGET_VIRT_ADDR_SPACE_BITS in h2g_valid.
In reply to Richard HendersonPreviously, only 32-bit guests had a proper check for the validity of the virtual address. Extend that check to 64-bit guests with a restricted virtual address space.
--- cpu-all.h | 16 +++++++++++----- 1 files changed, 11 insertions(+), 5 deletions(-)
cpu-all.h +11 -5
@@ -634,16 +634,22 @@ extern int have_guest_base;/* All direct uses of g2h and h2g need to go away for usermode softmmu. */ #define g2h (x) ((void *)((unsigned long)(x) + GUEST_BASE)) + +#if HOST_LONG_BITS ==TARGET_VIRT_ADDR_SPACE_BITS +#define h2g_valid (x) 1 +#else +#define h2g_valid (x) ({ \ + unsigned long __guest = (unsigned long)(x) - GUEST_BASE; \ + __guest < (1ul << TARGET_VIRT_ADDR_SPACE_BITS); \ +}) +#endif +#define h2g (x) ({ \ unsigned long __ret = (unsigned long)(x) - GUEST_BASE; \/* Check if given address fits target address space */ \ -assert (__ret == (abi_ulong )__ret); \ +assert (h2g_valid (x)); \ (abi_ulong )__ret; \ }) -#define h2g_valid (x) ({ \ - unsigned long __guest = (unsigned long)(x) - GUEST_BASE; \ - (__guest == (abi_ulong)__guest); \ -})#define saddr (x) g2h(x)#define laddr (x) g2h(x)
Signature · 1 line
1.6.6
PATCH 3/7 Fix last page errors in page_set_flags and page_check_range.
In reply to Richard HendersonThe addr < end comparison prevents the last page from being iterated; an iteration based on length avoids this problem.
--- exec.c | 39 +++++++++++++++++++++------------------ 1 files changed, 21 insertions(+), 18 deletions(-)
exec.c +21 -18
@@ -2230,27 +2230,27 @@ int page_get_flags(target_ulong address)return p->flags; } -/* modify the flags of a page and invalidate the code if -necessary. The flag PAGE_WRITE_ORG is positioned automatically -depending on PAGE_WRITE */ +/* Modify the flags of a page and invalidate the code if necessary. +The flag PAGE_WRITE_ORG is positioned automatically depending +on PAGE_WRITE. The mmap_lock should already be held. */ void page_set_flags (target_ulong start,target_ulong end,int flags) { -PageDesc *p; -target_ulong addr; +target_ulong addr, len; -/* mmap_lock should already be held. */ start = start &TARGET_PAGE_MASK ; end =TARGET_PAGE_ALIGN (end); -if (flags &PAGE_WRITE ) + +if (flags &PAGE_WRITE ) { flags |=PAGE_WRITE_ORG ; -for (addr = start; addr < end; addr +=TARGET_PAGE_SIZE ) { - p =page_find_alloc (addr >>TARGET_PAGE_BITS ); -/* We may be called for host regions that are outside guest -address space. */ -if (!p) -return ; -/* if the write protection is set, then we invalidate the code -inside */ + } + +for (addr = start, len = end - start; + len !=0 ; + len -=TARGET_PAGE_SIZE , addr +=TARGET_PAGE_SIZE ) { +PageDesc *p =page_find_alloc (addr >>TARGET_PAGE_BITS ,1 ); + +/* If the write protection bit is set, then we invalidate +the code inside. */ if (!(p->flags &PAGE_WRITE ) && (flags &PAGE_WRITE ) && p->first_tb) {
@@ -2266,14 +2266,17 @@ int page_check_range(target_ulong start, target_ulong len, int flags)target_ulong end;target_ulong addr; -if (start + len < start) -/* we've wrapped around */ +if (start + len -1 < start) { +/* We've wrapped around. */ return -1 ; + } end =TARGET_PAGE_ALIGN (start+len);/* must do before we loose bits in the next step */ start = start &TARGET_PAGE_MASK ; -for (addr = start; addr < end; addr +=TARGET_PAGE_SIZE ) { +for (addr = start, len = end - start; + len !=0 ; + len -=TARGET_PAGE_SIZE , addr +=TARGET_PAGE_SIZE ) { p =page_find (addr >>TARGET_PAGE_BITS );if ( !p )return -1 ;
Signature · 1 line
1.6.6
PATCH 5/7 linux-user: Use h2g_valid in qemu_vmalloc.
In reply to Richard Henderson--- linux-user/mmap.c | 9 ++++----- 1 files changed, 4 insertions(+), 5 deletions(-)
linux-user/mmap.c +4 -5
@@ -80,16 +80,15 @@ void mmap_unlock(void)void *qemu_vmalloc (size_t size) {void *p; -unsigned long addr; +mmap_lock ();/* Use map and mark the pages as used. */ p =mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); - addr = (unsigned long )p; -if (addr == (target_ulong ) addr) { -/* Allocated region overlaps guest address space. -This may recurse. */ +if (h2g_valid (p)) { +/* Allocated region overlaps guest address space. This may recurse. */ +unsigned long addr =h2g (p);page_set_flags (addr &TARGET_PAGE_MASK ,TARGET_PAGE_ALIGN (addr + size),PAGE_RESERVED ); }
Signature · 1 line
1.6.6
PATCH 7/7 Assert arguments in range for guest address space.
In reply to Richard HendersonWe don't expect host addresses within page_set_flags or page_check_range.
--- exec.c | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-)
exec.c +15 -0
@@ -2327,6 +2327,14 @@ void page_set_flags(target_ulong start, target_ulong end, int flags) {target_ulong addr, len; +/* This function should never be called with addresses outside the +guest address space. If this assert fires, it probably indicates +a missing call to h2g_valid. */ +#if HOST_LONG_BITS >TARGET_VIRT_ADDR_SPACE_BITS +assert (end < (1ul <<TARGET_VIRT_ADDR_SPACE_BITS )); +#endif +assert (start < end); + start = start &TARGET_PAGE_MASK ; end =TARGET_PAGE_ALIGN (end);
@@ -2356,6 +2364,13 @@ int page_check_range(target_ulong start, target_ulong len, int flags)target_ulong end;target_ulong addr; +/* This function should never be called with addresses outside the +guest address space. If this assert fires, it probably indicates +a missing call to h2g_valid. */ +#if HOST_LONG_BITS >TARGET_VIRT_ADDR_SPACE_BITS +assert (start < (1ul <<TARGET_VIRT_ADDR_SPACE_BITS )); +#endif +if (start + len -1 < start) {/* We've wrapped around. */ return -1 ;
Signature · 1 line
1.6.6
PATCH 6/7 linux-user: Fix mmap_find_vma returning invalid addresses.
In reply to Richard HendersonDon't return addresses that aren't properly aligned for the guest, e.g. when the guest has a larger page size than the host. Don't return addresses that are outside the virtual address space for the target, by paying proper attention to the h2g/g2h macros.
At the same time, place the default mapping base for 64-bit guests (on 64-bit hosts) outside the low 4G. Consistently interpret mmap_next_start in the guest address space.
--- linux-user/main.c | 7 +--- linux-user/mmap.c | 102 ++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 80 insertions(+), 29 deletions(-)
linux-user/main.c +2 -5
@@ -2725,12 +2725,9 @@ int main(int argc, char **argv, char **envp)/* * Read in mmap_min_addr kernel parameter. This value is used * When loading the ELF image to determine whether guest_base -* is needed. -* -* When user has explicitly set the quest base, we skip this -* test. +* is needed. It is also used in mmap_find_vma. */ -if (!have_guest_base) { + {FILE *fp;if ((fp =fopen ("/proc/sys/vm/mmap_min_addr" ,"r" )) !=NULL ) {
linux-user/mmap.c +78 -24
140 lines not on this page · raw message
Signature · 1 line
1.6.6
PATCH 4/7 Implement multi-level page tables.
In reply to Richard HendersonUse TARGET_VIRT_ADDR_SPACE_BITS for the virtual memory map based off of l1_map. This rewrites page_find_alloc, page_flush_tb, and walk_memory_regions.
Use TARGET_PHYS_ADDR_SPACE_BITS for the physical memory map based off of l1_phys_map. This rewrites page_phys_find_alloc and phys_page_for_each.
--- cpu-all.h | 7 +- exec.c | 442 +++++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 271 insertions(+), 178 deletions(-)
cpu-all.h +5 -2
@@ -745,8 +745,11 @@ extern unsigned long qemu_host_page_mask;#define PAGE_RESERVED 0x0020void page_dump (FILE *f); -int walk_memory_regions (void *, -int (*fn)(void *,unsigned long ,unsigned long ,unsigned long )); + +typedef int (*walk_memory_regions_fn )(void *,unsigned long , +unsigned long ,unsigned long ); +int walk_memory_regions (void *,walk_memory_regions_fn ); +int page_get_flags (target_ulong address);void page_set_flags (target_ulong start,target_ulong end,int flags);int page_check_range (target_ulong start,target_ulong len,int flags);
exec.c +263 -173
545 lines not on this page · raw message
Signature · 1 line
1.6.6
Re: [Qemu-devel] [PATCH 2/7] Use TARGET_VIRT_ADDR_SPACE_BITS in h2g_valid.
In reply to Richard Hendersoncpu-all.h extern int have_guest_base; · line 638
Quoted 5 lines
/* All direct uses of g2h and h2g need to go away for usermode softmmu. */ #define g2h(x) ((void *)((unsigned long)(x) +
GUEST_BASE))cpu-all.h +2
-0+ +#if HOST_LONG_BITS ==TARGET_VIRT_ADDR_SPACE_BITS
Shouldn't this be <= ?
1ul << T_V_A_S_B is undefined for 64-bit guests on 32-bit hosts.
cpu-all.h extern int have_guest_base; · line 643
Quoted 5 lines
cpu-all.h +5
-0+#define h2g_valid (x) 1 +#else +#define h2g_valid (x) ({ \ + unsigned long __guest = (unsigned long)(x) - GUEST_BASE; \ + __guest < (1ul << TARGET_VIRT_ADDR_SPACE_BITS); \
Paul
Quoted 5 lines
Which brings us to the problem of
exec.cand the address spaces therein. First, there was the fact thatTARGET_PHYS_ADDR_SPACE_BITSwas constrained to be no larger than 32 (with a partial hack for Alpha to extend this to 42 bits). Second, that this physical address space value was applied to virtual addresses viapage_find.
On further investigation (I missed this the first time round) it gets worse than that :-( While userspace emulation uses PageDesc to describe virtual pages, system emulation uses it to track physical pages. This probably helps explain why the existing code is so confused.
I'm not sure why we have separate l1_map and l1_phys_map. My guess is it's an attempt to save memory, on the theory that typically only a small faction of ram will be used to hold code.
Quoted 1 line
This patch series untangles this somewhat.
Looks like a fairly good start, however they're missing a Signed-off-by.
Paul