Multi-level page tables and userland mapping fixes, v3
cover Multi-level page tables and userland mapping fixes, v3
Changes since v2:
* Fix two errors pointed out by pbrook: l1_map size in system mode, and h2g_valid definition for 64-on-32-bit.
* Assertions that addresses are not outside the bounds of the guest address space.
* Re-base vs master, after l1_map_phys changes.
r~
Richard Henderson (6):
Move TARGET_PHYS_ADDR_SPACE_BITS to target-*/cpu.h. Use TARGET_VIRT_ADDR_SPACE_BITS in h2g_valid.
linux-user: Use h2g_valid in qemu_vmalloc.
linux-user: Fix mmap_find_vma returning invalid addresses. Implement multi-level page tables.
Fix last page errors in page_check_range and page_set_flags.
cpu-all.h | 23 ++- exec.c | 522 ++++++++++++++++++++++++++++------------------- linux-user/main.c | 11 +- linux-user/mmap.c | 111 ++++++++--- linux-user/qemu.h | 2 - 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 + 16 files changed, 474 insertions(+), 259 deletions(-)
PATCH 1/6 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
@@ -874,6 +874,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
@@ -99,6 +99,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.1
Re: [Qemu-devel] [PATCH 1/6] Move TARGET_PHYS_ADDR_SPACE_BITS to target-*/cpu.h.
Quoted 148 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.Signed-off-by Richard Henderson <[email protected]>--- 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@@ -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_codetarget-i386/cpu.h +11
-0@@ -874,6 +874,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 +0
-0
target-mips/mips-defs.h line 14
Quoted 7 lines
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 +
In 32-bit mode, TARGET_VIRT_ADDR_SPACE_BITS is 32 bits, and TARGET_PHYS_ADDR_SPACE_BITS is 36 bits.
In 64-bit mode, TARGET_VIRT_ADDR_SPACE_BITS is 42 bits, and TARGET_PHYS_ADDR_SPACE_BITS is 36 bits for the CPUs emulated by QEMU, though the architecture specification limits those value to respectively 62 and 59 bits.
Quoted 97 lines
target-mips/mips-defs.h +0
-0#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 CPUPPCStatetarget-s390x/cpu.h +5
-0@@ -99,6 +99,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 CPUSPARCStateSignature · 1 line
1.6.6.1
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
Re: [Qemu-devel] [PATCH 1/6] Move TARGET_PHYS_ADDR_SPACE_BITS to target-*/cpu.h.
Quoted 8 lines · Aurelien Jarno
target-mips/mips-defs.h line 14
Quoted 4 lines
target-mips/mips-defs.h +4
-0+/* ??? MIPS64 no doubt has a larger address space. */ +#define TARGET_PHYS_ADDR_SPACE_BITS 32 +#define TARGET_VIRT_ADDR_SPACE_BITS 32 +In 32-bit mode,
TARGET_VIRT_ADDR_SPACE_BITSis 32 bits, andTARGET_PHYS_ADDR_SPACE_BITSis 36 bits.
Quoted 4 lines
In 64-bit mode,
TARGET_VIRT_ADDR_SPACE_BITSis 42 bits, andTARGET_PHYS_ADDR_SPACE_BITSis 36 bits for the CPUs emulated by QEMU, though the architecture specification limits those value to respectively 62 and 59 bits.
Thanks. I'll queue that either for a subsequent round or a follow-up patch (since the current tree doesn't use anything but the 32 above).
r~
PATCH 2/6 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.1
PATCH 3/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.1
PATCH 4/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 | 11 ++---- linux-user/mmap.c | 102 ++++++++++++++++++++++++++++++++++++++++------------ linux-user/qemu.h | 2 - 3 files changed, 82 insertions(+), 33 deletions(-)
linux-user/main.c +4 -7
@@ -39,8 +39,8 @@char *exec_path;int singlestep; -#if defined(CONFIG_USE_GUEST_BASE )unsigned long mmap_min_addr; +#if defined(CONFIG_USE_GUEST_BASE )unsigned long guest_base;int have_guest_base;#endif
@@ -2812,16 +2812,14 @@ int main(int argc, char **argv, char **envp)* proper page alignment for guest_base. */ guest_base =HOST_PAGE_ALIGN (guest_base); +#endif /* CONFIG_USE_GUEST_BASE */ /* * 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 ) {
@@ -2833,7 +2831,6 @@ int main(int argc, char **argv, char **envp)fclose (fp); } } -#endif /* CONFIG_USE_GUEST_BASE */ /* * Prepare copy of argv vector for target.
linux-user/mmap.c +78 -24
140 lines not on this page · raw message
linux-user/qemu.h +0 -2
@@ -133,9 +133,7 @@ void init_task_state(TaskState *ts);void task_settid (TaskState *);void stop_all_tasks (void );extern const char *qemu_uname_release; -#if defined(CONFIG_USE_GUEST_BASE )extern unsigned long mmap_min_addr; -#endif /* ??? See if we can avoid exposing so much of the loader internals. */ /*
Signature · 1 line
1.6.6.1
PATCH 5/6 Implement multi-level page tables.
In reply to Richard HendersonDefine L1_MAP_ADDR_SPACE_BITS to be either the virtual address size (in user mode) or physical address size (in system mode), and use that to size 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 | 451 +++++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 278 insertions(+), 180 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 +270 -175
557 lines not on this page · raw message
Signature · 1 line
1.6.6.1
Re: [Qemu-devel] [PATCH 5/6] Implement multi-level page tables.
Quoted 9 lines · Richard Henderson
Define
L1_MAP_ADDR_SPACE_BITSto be either the virtual address size (in user mode) or physical address size (in system mode), and use that to sizel1_map. This rewritespage_find_alloc,page_flush_tb, andwalk_memory_regions.Use
TARGET_PHYS_ADDR_SPACE_BITSfor the physical memory map based off ofl1_phys_map. This rewritespage_phys_find_allocandphys_page_for_each.
Quoted 5 lines
Signed-off-by Richard Henderson <[email protected]>--- cpu-all.h | 7 +- exec.c | 451 +++++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 278 insertions(+), 180 deletions(-)
This patch breaks remote debugging (QEMU crash).
Test scenario:
x86_64 debian host, mips malta (32/64 bit, big/little endian) target
start qemu system emulation with a malta kernel and options -s -S, attach remote debugger, start mips kernel with "c" in debugger => qemu crash:
page_flush_tb is filled with zero in page_flush_tb tb_phys_invalidate is called, access fault in tb_remove.
Other targets might be affected, too.
Regards,
Stefan Weil
Re: [Qemu-devel] [PATCH 5/6] Implement multi-level page tables.
Quoted 10 lines
Quoted 9 lines · Richard Henderson
Define
L1_MAP_ADDR_SPACE_BITSto be either the virtual address size (in user mode) or physical address size (in system mode), and use that to sizel1_map. This rewritespage_find_alloc,page_flush_tb, andwalk_memory_regions.Use
TARGET_PHYS_ADDR_SPACE_BITSfor the physical memory map based off ofl1_phys_map. This rewritespage_phys_find_allocandphys_page_for_each.
Quoted 8 lines
Quoted 6 lines
Signed-off-by Richard Henderson <[email protected]>--- cpu-all.h | 7 +- exec.c | 451+++++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 278 insertions(+), 180 deletions(-)
This patch breaks remote debugging (QEMU crash).
Probably nothing to do with debugging. Should be fixed by 7296abaccc98872e28cec50091dbf26d38e4f062
Paul
Re: [Qemu-devel] [PATCH 5/6] Implement multi-level page tables.
Quoted 17 lines · Paul Brook
Quoted 17 lines
Quoted 15 lines · Richard Henderson
Define
L1_MAP_ADDR_SPACE_BITSto be either the virtual address size (in user mode) or physical address size (in system mode), and use that to sizel1_map. This rewritespage_find_alloc,page_flush_tb, andwalk_memory_regions.Use
TARGET_PHYS_ADDR_SPACE_BITSfor the physical memory map based off ofl1_phys_map. This rewritespage_phys_find_allocandphys_page_for_each.Signed-off-by Richard Henderson <[email protected]>--- cpu-all.h | 7 +- exec.c | 451+++++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 278 insertions(+), 180 deletions(-)
Quoted 5 lines
Quoted 1 line
This patch breaks remote debugging (QEMU crash).
Probably nothing to do with debugging. Should be fixed by
7296abaccc98872e28cec50091dbf26d38e4f062
Thanks it also fixes the problem I have reported.
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
PATCH 6/6 Fix last page errors in page_check_range and page_set_flags.
In reply to Richard HendersonThe addr < end comparison prevents iterating over the last page in the guest address space; an iteration based on length avoids this problem.
At the same time, assert that the given address is in the guest address space.
--- exec.c | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 files changed, 36 insertions(+), 18 deletions(-)
exec.c +36 -18
@@ -2315,27 +2315,35 @@ 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; + +/* 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 >L1_MAP_ADDR_SPACE_BITS +assert (end < (1ul <<L1_MAP_ADDR_SPACE_BITS )); +#endif +assert (start < end); -/* 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) {
@@ -2351,14 +2359,24 @@ 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 */ +/* 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 >L1_MAP_ADDR_SPACE_BITS +assert (start < (1ul <<L1_MAP_ADDR_SPACE_BITS )); +#endif + +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.1
Paul Brook
AppliedQuoted 7 lines
Richard Henderson (6):
MoveTARGET_PHYS_ADDR_SPACE_BITStotarget-*/cpu.h. UseTARGET_VIRT_ADDR_SPACE_BITSinh2g_valid.
linux-user: Useh2g_validinqemu_vmalloc.
linux-user: Fixmmap_find_vmareturning invalid addresses. Implement multi-level page tables.
Fix last page errors inpage_check_rangeandpage_set_flags.
Applied, plus a bunch of follow-on patches that isolate the userspace emulation from any concept of physical address space.
Paul
Quoted 11 lines · Paul Brook
Quoted 7 lines
Richard Henderson (6):
MoveTARGET_PHYS_ADDR_SPACE_BITStotarget-*/cpu.h. UseTARGET_VIRT_ADDR_SPACE_BITSinh2g_valid.
linux-user: Useh2g_validinqemu_vmalloc.
linux-user: Fixmmap_find_vmareturning invalid addresses. Implement multi-level page tables.
Fix last page errors inpage_check_rangeandpage_set_flags.Applied, plus a bunch of follow-on patches that isolate the userspace
emulation from any concept of physical address space.
Now I get these warnings on OpenBSD/Sparc64:
CC sparc-bsd-user/exec.o
/src/qemu/exec.c: In function `page_set_flags':
/src/qemu/exec.c:2336: warning: comparison is always true due to limited range of data type
/src/qemu/exec.c: In function `page_check_range':
/src/qemu/exec.c:2373: warning: comparison is always true due to limited range of data type
Quoted 3 lines
/src/qemu/exec.c: In function `page_set_flags':
/src/qemu/exec.c:2336: warning: comparison is always true due to limited range of data type
Should be fixed by b480d9b74dfd1efd29026b7dc6438364a633ee99
Paul
Quoted 11 lines · Paul Brook
Quoted 7 lines
Richard Henderson (6):
MoveTARGET_PHYS_ADDR_SPACE_BITStotarget-*/cpu.h. UseTARGET_VIRT_ADDR_SPACE_BITSinh2g_valid.
linux-user: Useh2g_validinqemu_vmalloc.
linux-user: Fixmmap_find_vmareturning invalid addresses. Implement multi-level page tables.
Fix last page errors inpage_check_rangeandpage_set_flags.Applied, plus a bunch of follow-on patches that isolate the userspace emulation from any concept of physical address space.
The patch "Implement multi-level page tables" breaks i386/x86_64 system emulation. Using the image on [1], qemu now segfaults during the initrd parts. This is not 100% reproducible, a few times it hanged instead of crashing.
[1] http://people.debian.org/~aurel32/qemu/i386/debian_lenny_i386_small.qcow2
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
I am sorry to bring us all down to earth, we all had wished that this stuff gets finally fixed, but it seems that those patches applied to QEMU master have killed QEMU user mode.
I am referring to the user mode fixes after commit 0aef4261ac0ec9089ade0e3a92f986cb4ba7317e
I had QEMU working on PowerPC and ARM in chroots from the Linux versions:
- Fedora 11 / 12
- Ubuntu 9.04, 9.10, 10.04
- Debian 4, 5 and Sid
for ARM and PowerPC. My host OS is openSUSE 11.2 using a 2.6.31 kernel, or openSUSE 11.1. All machines are 64 Bit machines.
Now I have the situation that all the listed target chroots fail, except:
- Fedora 11 / 12 @ ARM
As you might guess (or even know), I am one of the Open Build Service (http://en.opensuse.org/Build_Service/Testing) developers and can automatedly setup and test such a big number of Targets and automatically run testsuites or compile jobs on them. The last fixes in the QEMU user mode memory allocator before your rewrite came from Jan-Simon and me.
I think its long overdue to fix the user mode stuff, so I am happy that now someone that knows more about QEMU than me cares about. SO my offering is to help to track this down and fix it. But I would be happy if you give me a hint about what your changes are really doing to track down the problem.
Quoted 23 lines · Aurelien Jarno
Quoted 8 lines · Paul Brook
Quoted 8 lines
Richard Henderson (6):
MoveTARGET_PHYS_ADDR_SPACE_BITStotarget-*/cpu.h. UseTARGET_VIRT_ADDR_SPACE_BITSinh2g_valid.
linux-user: Useh2g_validinqemu_vmalloc.
linux-user: Fixmmap_find_vmareturning invalid addresses. Implement multi-level page tables.
Fix last page errors inpage_check_rangeandpage_set_flags.Quoted 4 lines
Applied, plus a bunch of follow-on patches that isolate the userspace emulation from any concept of physical address space.
The patch "Implement multi-level page tables" breaks i386/x86_64 system emulation. Using the image on [1], qemu now segfaults during the initrd parts. This is not 100% reproducible, a few times it hanged instead of crashing.
[1] http://people.debian.org/~aurel32/qemu/i386/debian_lenny_i386_small.qcow2
Quoted 1 line · Martin Mohring
I am sorry to bring us all down to earth, we all had wished that this stuff gets finally fixed, but it seems that those patches applied to QEMU master have killed QEMU user mode.
sid and maemo userlands work for me like before (64bit and 32bit hosts).
Quoted 4 lines
I am referring to the user mode fixes after commit
0aef4261ac0ec9089ade0e3a92f986cb4ba7317eI had QEMU working on PowerPC and ARM in chroots from the Linux versions:
Quoted 8 lines
- Fedora 11 / 12
- Ubuntu 9.04, 9.10, 10.04
- Debian 4, 5 and Sidfor ARM and PowerPC. My host OS is openSUSE 11.2 using a 2.6.31 kernel, or openSUSE 11.1. All machines are 64 Bit machines.
Now I have the situation that all the listed target chroots fail, except:
- Fedora 11 / 12 @ ARM
Please try to be more specific. What is killed? what do you mean by "chroots fail". qemu segfault? some binary doesn't work that before did? some syscall doesn't work anymore?
Quoted 9 lines
Quoted 9 lines
I had QEMU working on PowerPC and ARM in chroots from the Linux versions:
- Fedora 11 / 12
- Ubuntu 9.04, 9.10, 10.04
- Debian 4, 5 and Sidfor ARM and PowerPC. My host OS is openSUSE 11.2 using a 2.6.31 kernel, or openSUSE 11.1. All machines are 64 Bit machines.
Quoted 7 lines
Quoted 2 lines
Now I have the situation that all the listed target chroots fail, except:
- Fedora 11 / 12 @ ARMPlease try to be more specific. What is killed? what do you mean by "chroots fail". qemu segfault? some binary doesn't work that before did? some syscall doesn't work anymore?
We're still investigating the topic. So far, we tracked one failure down to ldconfig.real which is a static arm binary executed after the chroot got the basic packages installed. If we setup a chroot with an older qemu-arm and exchange it afterwards with the new qemu and rerun just the build step, it works. Thus it seems to be an issue with static arm binaries atm.
Strace of a call of ldconfig.real with the qemu-arm failing:
Sort version:
root@frodo:/# qemu-arm -strace /sbin/ldconfig.real
16359 uname(0x403fef78) = 0
16359 brk(NULL) = 0x000a9000
16359 brk(0x000a9d08) = 0x000a9d08
16359 open("/dev/urandom",O_RDONLY) = 3
16359 read(3,0x403ff27d,3) = 3
16359 close(3) = 0
[...]
16359 stat64("/usr/lib/libgettextlib.so",0x403fdf28) = 0
16359 stat64("/usr/lib/libgettextpo.so.0",0x403fdec0) = 0
16359 stat64("/usr/lib/libgettextpo.so.0.4.0",0x403fdf28) = 0
16359 stat64("/usr/lib/libpython2.6.so.1.0",0x403fdec0) = 0
16359 stat64("/usr/lib/libpython2.6.so.1.0",0x403fdf28) = 0
16359 open("/etc/ld.so.cache~",O_WRONLY|O_CREAT|O_NOFOLLOW|O_TRUNC,0600) = 3
16359 write(3,0xb03d0,1288) = 1288
16359 write(3,0x403ff0a0,0) = -1 errno=14 (Bad address)
16359 write(2,0x403fca08,21)/sbin/ldconfig.real: = 21
16359 write(2,0x403fc9e8,28)Writing of cache data failed = 28
16359 write(2,0x403fc5b8,13): Bad address = 13
16359 write(2,0x403fc9c0,1)
= 1
16359 exit_group(1)Best,
Jan-Simon
Quoted 12 lines · Jan-Simon Möller
We're still investigating the topic. So far, we tracked one failure down to ldconfig.real which is a static arm binary executed after the chroot got the basic packages installed. If we setup a chroot with an older qemu-arm and exchange it afterwards with the new qemu and rerun just the build step, it works. Thus it seems to be an issue with static arm binaries atm.
Strace of a call of ldconfig.real with the qemu-arm failing:
Sort version:
Quoted 16 lines
root@frodo:/# qemu-arm -strace /sbin/ldconfig.real
16359 uname(0x403fef78) = 0
16359 brk(NULL) = 0x000a9000
16359 brk(0x000a9d08) = 0x000a9d08
16359 open("/dev/urandom",O_RDONLY) = 3
16359 read(3,0x403ff27d,3) = 3
16359 close(3) = 0
[...]
16359 stat64("/usr/lib/libgettextlib.so",0x403fdf28) = 0
16359 stat64("/usr/lib/libgettextpo.so.0",0x403fdec0) = 0
16359 stat64("/usr/lib/libgettextpo.so.0.4.0",0x403fdf28) = 0
16359 stat64("/usr/lib/libpython2.6.so.1.0",0x403fdec0) = 0
16359 stat64("/usr/lib/libpython2.6.so.1.0",0x403fdf28) = 0
16359 open("/etc/ld.so.cache~",O_WRONLY|O_CREAT|O_NOFOLLOW|O_TRUNC,0600) = 3
16359 write(3,0xb03d0,1288) = 1288
16359 write(3,0x403ff0a0,0) = -1 errno=14 (Bad address)A zero sized write. According to manpage ok.
In qemu we do a lock_user to to get the string to write. Richards change changes the access checks the get called by lock_user:
page_check_range:
- if (start + len < start)
- /* we've wrapped around */
...
+ if (start + len - 1 < start) {
+ /* We've wrapped around. */
This now blows up with len = 0;
Quoted 10 lines
16359 write(2,0x403fca08,21)/sbin/ldconfig.real: = 21 16359 write(2,0x403fc9e8,28)Writing of cache data failed = 28 16359
write(2,0x403fc5b8,13): Bad address = 13 16359write(2,0x403fc9c0,1)
= 1
16359exit_group(1)Best,
Jan-Simon
Am Montag, 15. März 2010 15:48:03 schrieb Riku Voipio:
Quoted 44 lines · Jan-Simon Möller
Quoted 13 lines
We're still investigating the topic. So far, we tracked one failure down to ldconfig.real which is a static arm binary executed after the chroot got the basic packages installed. If we setup a chroot with an older qemu-arm and exchange it afterwards with the new qemu and rerun just the build step, it works. Thus it seems to be an issue with static arm binaries atm.
Strace of a call of ldconfig.real with the qemu-arm failing:
Sort version:
Quoted 16 lines
root@frodo:/# qemu-arm -strace /sbin/ldconfig.real 16359 uname(0x403fef78) = 0 16359 brk(NULL) = 0x000a9000 16359 brk(0x000a9d08) = 0x000a9d08 16359 open("/dev/urandom",O_RDONLY) = 3 16359 read(3,0x403ff27d,3) = 3 16359 close(3) = 0 [...] 16359 stat64("/usr/lib/libgettextlib.so",0x403fdf28) = 0 16359 stat64("/usr/lib/libgettextpo.so.0",0x403fdec0) = 0 16359 stat64("/usr/lib/libgettextpo.so.0.4.0",0x403fdf28) = 0 16359 stat64("/usr/lib/libpython2.6.so.1.0",0x403fdec0) = 0 16359 stat64("/usr/lib/libpython2.6.so.1.0",0x403fdf28) = 0 16359 open("/etc/ld.so.cache~",O_WRONLY|O_CREAT|O_NOFOLLOW|O_TRUNC,0600) = 3 16359 write(3,0xb03d0,1288) = 1288 16359 write(3,0x403ff0a0,0) = -1 errno=14 (Bad address)A zero sized write. According to manpage ok.
In qemu we do a
lock_userto to get the string to write. Richards change
changes the access checks the get called bylock_user:
page_check_range:(quoted diff) +2
-2- if (start + len < start) - /* we've wrapped around */ + if (start + len - 1 < start) { + /* We've wrapped around. */
Quoted 1 line
This now blows up with len = 0;
Confirmed. A quick test with if (len > 0) around and ldconfig.real runs.
Best,
Jan-Simon
Quoted 32 lines · Jan-Simon Möller
Am Montag, 15. März 2010 15:48:03 schrieb Riku Voipio:
Quoted 31 lines · Jan-Simon Möller
Quoted 8 lines
root@frodo:/# qemu-arm -strace /sbin/ldconfig.real 16359 uname(0x403fef78) = 0 16359 brk(NULL) = 0x000a9000 16359 brk(0x000a9d08) = 0x000a9d08 16359 open("/dev/urandom",O_RDONLY) = 3 16359 read(3,0x403ff27d,3) = 3 16359 close(3) = 0 [...]Quoted 8 lines
16359 stat64("/usr/lib/libgettextlib.so",0x403fdf28) = 0 16359 stat64("/usr/lib/libgettextpo.so.0",0x403fdec0) = 0 16359 stat64("/usr/lib/libgettextpo.so.0.4.0",0x403fdf28) = 0 16359 stat64("/usr/lib/libpython2.6.so.1.0",0x403fdec0) = 0 16359 stat64("/usr/lib/libpython2.6.so.1.0",0x403fdf28) = 0 16359 open("/etc/ld.so.cache~",O_WRONLY|O_CREAT|O_NOFOLLOW|O_TRUNC,0600) = 3 16359 write(3,0xb03d0,1288) = 1288 16359 write(3,0x403ff0a0,0) = -1 errno=14 (Bad address)A zero sized write. According to manpage ok.
In qemu we do a
lock_userto to get the string to write. Richards change
changes the access checks the get called bylock_user:
page_check_range:(quoted diff) +2
-2- if (start + len < start) - /* we've wrapped around */ + if (start + len - 1 < start) { + /* We've wrapped around. */
Quoted 3 lines
Quoted 1 line
This now blows up with len = 0;
Confirmed. A quick test with if (len > 0) around and ldconfig.real runs.
Richard, would you be ok with that change? Or should we rather have explicit
if (len == 0) return 0;
?
Quoted 8 lines · Riku Voipio
Quoted 8 lines
Quoted 8 lines
page_check_range:(quoted diff) +2
-2- if (start + len < start) - /* we've wrapped around */ + if (start + len - 1 < start) { + /* We've wrapped around. */
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
This now blows up with len = 0;
Confirmed. A quick test with if (len > 0) around and ldconfig.real runs.
Richard, would you be ok with that change? Or should we rather have explicit
if (len == 0) return 0;
My personal preference is
if (len != 0 && start + len - 1 < start)
but don't let that stop anyone checking in any correct variant.
r~