tcg patch queue
v2 cover tcg patch queue
Changes from v1:
* Patch 10 is new, avoiding an overflow in probe_guest_base, visible with aarch64 host, --static --disable-pie, exposed by the placement of the host binary in the address space.
r~
Emilio Cota (2):
util: import GTree as QTree
tcg: use QTree instead of GTree
Richard Henderson (10):
linux-user: Diagnose misaligned -R size
accel/tcg: Pass last not end to page_set_flags
accel/tcg: Pass last not end to page_reset_target_data accel/tcg: Pass last not end to PAGE_FOR_EACH_TB accel/tcg: Pass last not end to page_collection_lock accel/tcg: Pass last not end to tb_invalidate_phys_page_range__locked accel/tcg: Pass last not end to tb_invalidate_phys_range
linux-user: Pass last not end to probe_guest_base include/exec: Change reserved_va semantics to last byte linux-user/arm: Take more care allocating commpage
configure | 15 + meson.build | 4 + include/exec/cpu-all.h | 15 +- include/exec/exec-all.h | 2 +- include/qemu/qtree.h | 201 +++++ linux-user/arm/target_cpu.h | 2 +- accel/tcg/tb-maint.c | 112 +-- accel/tcg/translate-all.c | 2 +- accel/tcg/user-exec.c | 25 +- bsd-user/main.c | 10 +- bsd-user/mmap.c | 10 +- linux-user/elfload.c | 72 +- linux-user/flatload.c | 2 +- linux-user/main.c | 31 +- linux-user/mmap.c | 22 +- linux-user/syscall.c | 4 +- softmmu/physmem.c | 2 +- tcg/region.c | 19 +- tests/bench/qtree-bench.c | 286 +++++++ tests/unit/test-qtree.c | 333 +++++++++ util/qtree.c | 1390 +++++++++++++++++++++++++++++++++++ tests/bench/meson.build | 4 + tests/unit/meson.build | 1 + util/meson.build | 1 + 24 files changed, 2415 insertions(+), 150 deletions(-) create mode 100644 include/qemu/qtree.h create mode 100644 tests/bench/qtree-bench.c create mode 100644 tests/unit/test-qtree.c create mode 100644 util/qtree.c
Signature · 1 line
2.34.1
PATCH v2 1/12 util: import GTree as QTree
From: Emilio Cota <[email protected]>
The only reason to add this implementation is to control the memory allocator used. Some users (e.g. TCG) cannot work reliably in multi-threaded environments (e.g. forking in user-mode) with GTree's allocator, GSlice. See https://gitlab.com/qemu-project/qemu/-/issues/285 for details.
Importing GTree is a temporary workaround until GTree migrates away from GSlice.
This implementation is identical to that in glib v2.75.0, except that we don't import recent additions to the API nor deprecated API calls, none of which are used in QEMU.
I've imported tests from glib and added a benchmark just to make sure that performance is similar. Note: it cannot be identical because (1) we are not using GSlice, (2) we use different compilation flags (e.g. -fPIC) and (3) we're linking statically.
$ cat /proc/cpuinfo| grep 'model name' | head -1 model name : AMD Ryzen 7 PRO 5850U with Radeon Graphics $ echo '0' | sudo tee /sys/devices/system/cpu/cpufreq/boost $ tests/bench/qtree-bench
Tree Op 32 1024 4096 131072 1048576
------------------------------------------------------------------------------------------------
GTree Lookup 83.23 43.08 25.31 19.40 16.22 QTree Lookup 113.42 (1.36x) 53.83 (1.25x) 28.38 (1.12x) 17.64 (0.91x) 13.04 (0.80x) GTree Insert 44.23 29.37 25.83 19.49 17.03 QTree Insert 46.87 (1.06x) 25.62 (0.87x) 24.29 (0.94x) 16.83 (0.86x) 12.97 (0.76x) GTree Remove 53.27 35.15 31.43 24.64 16.70 QTree Remove 57.32 (1.08x) 41.76 (1.19x) 38.37 (1.22x) 29.30 (1.19x) 15.07 (0.90x) GTree RemoveAll 135.44 127.52 126.72 120.11 64.34 QTree RemoveAll 127.15 (0.94x) 110.37 (0.87x) 107.97 (0.85x) 97.13 (0.81x) 55.10 (0.86x) GTree Traverse 277.71 276.09 272.78 246.72 98.47 QTree Traverse 370.33 (1.33x) 411.97 (1.49x) 400.23 (1.47x) 262.82 (1.07x) 78.52 (0.80x)
------------------------------------------------------------------------------------------------
As a sanity check, the same benchmark when Glib's version is >= $glib_dropped_gslice_version (i.e. QTree == GTree):
Tree Op 32 1024 4096 131072 1048576
------------------------------------------------------------------------------------------------
GTree Lookup 82.72 43.09 24.18 19.73 16.09 QTree Lookup 81.82 (0.99x) 43.10 (1.00x) 24.20 (1.00x) 19.76 (1.00x) 16.26 (1.01x) GTree Insert 45.07 29.62 26.34 19.90 17.18 QTree Insert 45.72 (1.01x) 29.60 (1.00x) 26.38 (1.00x) 19.71 (0.99x) 17.20 (1.00x) GTree Remove 54.48 35.36 31.77 24.97 16.95 QTree Remove 54.46 (1.00x) 35.32 (1.00x) 31.77 (1.00x) 24.91 (1.00x) 17.15 (1.01x) GTree RemoveAll 140.68 127.36 125.43 121.45 68.20 QTree RemoveAll 140.65 (1.00x) 127.64 (1.00x) 125.01 (1.00x) 121.73 (1.00x) 67.06 (0.98x) GTree Traverse 278.68 276.05 266.75 251.65 104.93 QTree Traverse 278.31 (1.00x) 275.78 (1.00x) 266.42 (1.00x) 247.89 (0.99x) 104.58 (1.00x)
------------------------------------------------------------------------------------------------
Message-Id: <[email protected]>
--- configure | 15 + meson.build | 4 + include/qemu/qtree.h | 201 ++++++ tests/bench/qtree-bench.c | 286 ++++++++ tests/unit/test-qtree.c | 333 +++++++++ util/qtree.c | 1390 +++++++++++++++++++++++++++++++++++++ tests/bench/meson.build | 4 + tests/unit/meson.build | 1 + util/meson.build | 1 + 9 files changed, 2235 insertions(+) create mode 100644 include/qemu/qtree.h create mode 100644 tests/bench/qtree-bench.c create mode 100644 tests/unit/test-qtree.c create mode 100644 util/qtree.c
configure +15 -0
@@ -231,6 +231,7 @@ safe_stack="" use_containers="yes" gdb_bin=$(command -v "gdb-multiarch" || command -v "gdb") gdb_arches="" +glib_has_gslice="no" if test -e "$source_path/.git" then
@@ -1494,6 +1495,17 @@ for i in $glib_modules; do fi done +# Check whether glib has gslice, which we have to avoid for correctness. +# TODO: remove this check and the corresponding workaround (qtree) when +# the minimum supported glib is >= $glib_dropped_gslice_version. +glib_dropped_gslice_version=2.75.3 +for i in $glib_modules; do + if ! $pkg_config --atleast-version=$glib_dropped_gslice_version $i; then + glib_has_gslice="yes" + break + fi +done + glib_bindir="$($pkg_config --variable=bindir glib-2.0)" if test -z "$glib_bindir" ; then glib_bindir="$($pkg_config --variable=prefix glib-2.0)"/bin
@@ -2420,6 +2432,9 @@ echo "GLIB_CFLAGS=$glib_cflags" >> $config_host_mak echo "GLIB_LIBS=$glib_libs" >> $config_host_mak echo "GLIB_BINDIR=$glib_bindir" >> $config_host_mak echo "GLIB_VERSION=$($pkg_config --modversion glib-2.0)" >> $config_host_mak +if test "$glib_has_gslice" = "yes" ; then + echo "HAVE_GLIB_WITH_SLICE_ALLOCATOR=y" >> $config_host_mak +fi echo "QEMU_LDFLAGS=$QEMU_LDFLAGS" >> $config_host_mak echo "EXESUF=$EXESUF" >> $config_host_mak
meson.build +4 -0
@@ -508,6 +508,10 @@ glib = declare_dependency(compile_args: config_host['GLIB_CFLAGS'].split(), }) # override glib dep with the configure results (for subprojects) meson.override_dependency('glib-2.0', glib) +# pass down whether Glib has the slice allocator +if config_host.has_key('HAVE_GLIB_WITH_SLICE_ALLOCATOR') + config_host_data.set('HAVE_GLIB_WITH_SLICE_ALLOCATOR', true) +endif gio = not_found gdbus_codegen = not_found
include/qemu/qtree.h new file +201 -0
201 lines not on this page · raw message
tests/bench/qtree-bench.c new file +286 -0
286 lines not on this page · raw message
tests/unit/test-qtree.c new file +333 -0
333 lines not on this page · raw message
util/qtree.c new file +1390 -0
1,390 lines not on this page · raw message
tests/bench/meson.build +4 -0
@@ -9,6 +9,10 @@ xbzrle_bench = executable('xbzrle-bench', dependencies: [qemuutil,migration]) endif +qtree_bench = executable('qtree-bench', + sources: 'qtree-bench.c', + dependencies: [qemuutil]) + executable('atomic_add-bench', sources: files('atomic_add-bench.c'), dependencies: [qemuutil],
tests/unit/meson.build +1 -0
@@ -36,6 +36,7 @@ tests = { 'test-rcu-slist': [], 'test-qdist': [], 'test-qht': [], + 'test-qtree': [], 'test-bitops': [], 'test-bitcnt': [], 'test-qgraph': ['../qtest/libqos/qgraph.c'],
util/meson.build +1 -0
@@ -26,6 +26,7 @@ util_ss.add(when: 'CONFIG_WIN32', if_true: files('oslib-win32.c')) util_ss.add(when: 'CONFIG_WIN32', if_true: files('qemu-thread-win32.c')) util_ss.add(when: 'CONFIG_WIN32', if_true: winmm) util_ss.add(when: 'CONFIG_WIN32', if_true: pathcch) +util_ss.add(when: 'HAVE_GLIB_WITH_SLICE_ALLOCATOR', if_true: files('qtree.c')) util_ss.add(files('envlist.c', 'path.c', 'module.c')) util_ss.add(files('host-utils.c')) util_ss.add(files('bitmap.c', 'bitops.c'))
Signature · 1 line
2.34.1
PATCH v2 2/12 tcg: use QTree instead of GTree
In reply to Richard HendersonFrom: Emilio Cota <[email protected]>
qemu-user can hang in a multi-threaded fork. One common reason is that when creating a TB, between fork and exec we manipulate a GTree whose memory allocator (GSlice) is not fork-safe.
Although POSIX does not mandate it, the system's allocator (e.g. tcmalloc, libc malloc) is probably fork-safe.
Fix some of these hangs by using QTree, which uses the system's allocator regardless of the Glib version that we used at configuration time.
Tested with the test program in the original bug report, i.e.:
```
void garble() {
int pid = fork();
if (pid == 0) {
exit(0);
} else {
int wstatus;
waitpid(pid, &wstatus, 0);
}
}
void supragarble(unsigned depth) {
if (depth == 0)
return ;
std::thread a(supragarble, depth-1);
std::thread b(supragarble, depth-1);
garble();
a.join();
b.join();
}
int main() {
supragarble(10);
}
```Resolves: https://gitlab.com/qemu-project/qemu/-/issues/285
Message-Id: <[email protected]>
--- accel/tcg/tb-maint.c | 17 +++++++++-------- tcg/region.c | 19 ++++++++++--------- util/qtree.c | 8 ++++---- 3 files changed, 23 insertions(+), 21 deletions(-)
accel/tcg/tb-maint.c +9 -8
@@ -19,6 +19,7 @@#include "qemu/osdep.h" #include "qemu/interval-tree.h" +#include "qemu/qtree.h" #include "exec/cputlb.h" #include "exec/log.h" #include "exec/exec-all.h"
@@ -314,7 +315,7 @@ struct page_entry {* See also: page_collection_lock(). */ struct page_collection { -GTree *tree; +QTree *tree;struct page_entry *max; };
@@ -467,7 +468,7 @@ static bool page_trylock_add(struct page_collection *set, tb_page_addr_t addr)struct page_entry *pe;PageDesc *pd; - pe =g_tree_lookup (set->tree, &index); + pe =q_tree_lookup (set->tree, &index);if (pe) {return false; }
@@ -478,7 +479,7 @@ static bool page_trylock_add(struct page_collection *set, tb_page_addr_t addr) } pe =page_entry_new (pd, index); -g_tree_insert (set->tree, &pe->index, pe); +q_tree_insert (set->tree, &pe->index, pe);/* * If this is either (1) the first insertion or (2) a page whose index
@@ -525,13 +526,13 @@ static struct page_collection *page_collection_lock(tb_page_addr_t start, end >>=TARGET_PAGE_BITS ;g_assert (start <= end); - set->tree =g_tree_new_full (tb_page_addr_cmp,NULL ,NULL , + set->tree =q_tree_new_full (tb_page_addr_cmp,NULL ,NULL , page_entry_destroy); set->max =NULL ;assert_no_pages_locked (); retry: -g_tree_foreach (set->tree, page_entry_lock,NULL ); +q_tree_foreach (set->tree, page_entry_lock,NULL );for (index = start; index <= end; index++) {TranslationBlock *tb;
@@ -542,7 +543,7 @@ static struct page_collection *page_collection_lock(tb_page_addr_t start,continue ; }if (page_trylock_add (set, index <<TARGET_PAGE_BITS )) { -g_tree_foreach (set->tree, page_entry_unlock,NULL ); +q_tree_foreach (set->tree, page_entry_unlock,NULL ); goto retry; }assert_page_locked (pd);
@@ -551,7 +552,7 @@ static struct page_collection *page_collection_lock(tb_page_addr_t start, (tb_page_addr1(tb) != -1 && page_trylock_add(set, tb_page_addr1(tb)))) {/* drop all locks, and reacquire in order */ -g_tree_foreach (set->tree, page_entry_unlock,NULL ); +q_tree_foreach (set->tree, page_entry_unlock,NULL ); goto retry; } }
@@ -562,7 +563,7 @@ static struct page_collection *page_collection_lock(tb_page_addr_t start,static void page_collection_unlock (struct page_collection *set) {/* entries are unlocked and freed via page_entry_destroy */ -g_tree_destroy (set->tree); +q_tree_destroy (set->tree);g_free (set); }
tcg/region.c +10 -9
@@ -28,6 +28,7 @@#include "qemu/mprotect.h" #include "qemu/memalign.h" #include "qemu/cacheinfo.h" +#include "qemu/qtree.h" #include "qapi/error.h" #include "exec/exec-all.h" #include "tcg/tcg.h"
@@ -36,7 +37,7 @@struct tcg_region_tree {QemuMutex lock; -GTree *tree; +QTree *tree;/* padding to avoid false sharing is computed at run-time */ };
@@ -163,7 +164,7 @@ static void tcg_region_trees_init(void)struct tcg_region_tree *rt = region_trees + i * tree_size;qemu_mutex_init (&rt->lock); - rt->tree =g_tree_new_full (tb_tc_cmp,NULL ,NULL , tb_destroy); + rt->tree =q_tree_new_full (tb_tc_cmp,NULL ,NULL , tb_destroy); } }
@@ -202,7 +203,7 @@ void tcg_tb_insert(TranslationBlock *tb)g_assert (rt !=NULL );qemu_mutex_lock (&rt->lock); -g_tree_insert (rt->tree, &tb->tc, tb); +q_tree_insert (rt->tree, &tb->tc, tb);qemu_mutex_unlock (&rt->lock); }
@@ -212,7 +213,7 @@ void tcg_tb_remove(TranslationBlock *tb)g_assert (rt !=NULL );qemu_mutex_lock (&rt->lock); -g_tree_remove (rt->tree, &tb->tc); +q_tree_remove (rt->tree, &tb->tc);qemu_mutex_unlock (&rt->lock); }
@@ -232,7 +233,7 @@ TranslationBlock *tcg_tb_lookup(uintptr_t tc_ptr) }qemu_mutex_lock (&rt->lock); - tb =g_tree_lookup (rt->tree, &s); + tb =q_tree_lookup (rt->tree, &s);qemu_mutex_unlock (&rt->lock);return tb; }
@@ -267,7 +268,7 @@ void tcg_tb_foreach(GTraverseFunc func, gpointer user_data)for (i =0 ; i < region.n; i++) {struct tcg_region_tree *rt = region_trees + i * tree_size; -g_tree_foreach (rt->tree, func, user_data); +q_tree_foreach (rt->tree, func, user_data); }tcg_region_tree_unlock_all (); }
@@ -281,7 +282,7 @@ size_t tcg_nb_tbs(void)for (i =0 ; i < region.n; i++) {struct tcg_region_tree *rt = region_trees + i * tree_size; - nb_tbs +=g_tree_nnodes (rt->tree); + nb_tbs +=q_tree_nnodes (rt->tree); }tcg_region_tree_unlock_all ();return nb_tbs;
@@ -296,8 +297,8 @@ static void tcg_region_tree_reset_all(void)struct tcg_region_tree *rt = region_trees + i * tree_size;/* Increment the refcount first so that destroy acts as a reset */ -g_tree_ref (rt->tree); -g_tree_destroy (rt->tree); +q_tree_ref (rt->tree); +q_tree_destroy (rt->tree); }tcg_region_tree_unlock_all (); }
util/qtree.c +4 -4
@@ -310,7 +310,7 @@ q_tree_node_next(QTreeNode *node)* * Since: 2.70 in GLib. Internal in Qtree, i.e. not in the public API. */ -static void +static void QEMU_DISABLE_CFIq_tree_remove_all (QTree *tree) {QTreeNode *node;
@@ -532,7 +532,7 @@ q_tree_replace(QTree *tree, }/* internal insert routine */ -static QTreeNode * +static QTreeNode * QEMU_DISABLE_CFI q_tree_insert_internal(QTree *tree, gpointer key, gpointer value,
@@ -721,7 +721,7 @@ q_tree_steal(QTree *tree, }/* internal remove routine */ -static gboolean +static gboolean QEMU_DISABLE_CFIq_tree_remove_internal (QTree *tree,gconstpointer key,gboolean steal)
@@ -1182,7 +1182,7 @@ q_tree_node_balance(QTreeNode *node)return node; } -static QTreeNode * +static QTreeNode * QEMU_DISABLE_CFIq_tree_find_node (QTree *tree,gconstpointer key) {
Signature · 1 line
2.34.1
PATCH v2 3/12 linux-user: Diagnose misaligned -R size
In reply to Richard HendersonWe have been enforcing host page alignment for the non-R fallback of MAX_RESERVED_VA, but failing to enforce for -R.
--- linux-user/main.c | 6 ++++++ 1 file changed, 6 insertions(+)
linux-user/main.c +6 -0
@@ -793,6 +793,12 @@ int main(int argc, char **argv, char **envp)*/ max_reserved_va =MAX_RESERVED_VA (cpu);if (reserved_va !=0 ) { +if (reserved_va % qemu_host_page_size) { +char *s =size_to_str (qemu_host_page_size); +fprintf (stderr,"Reserved virtual address not aligned mod %s\n" , s); +g_free (s); +exit (EXIT_FAILURE ); + }if (max_reserved_va && reserved_va > max_reserved_va) {fprintf (stderr,"Reserved virtual address too big\n" );exit (EXIT_FAILURE );
Signature · 1 line
2.34.1
PATCH v2 4/12 accel/tcg: Pass last not end to page_set_flags
In reply to Richard HendersonPass the address of the last byte to be changed, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1528
--- include/exec/cpu-all.h | 2 +- accel/tcg/user-exec.c | 16 +++++++--------- bsd-user/mmap.c | 6 +++--- linux-user/elfload.c | 11 ++++++----- linux-user/mmap.c | 16 ++++++++-------- linux-user/syscall.c | 4 ++-- 6 files changed, 27 insertions(+), 28 deletions(-)
include/exec/cpu-all.h +1 -1
@@ -276,7 +276,7 @@ typedef int (*walk_memory_regions_fn)(void *, target_ulong,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); +void page_set_flags (target_ulong start,target_ulong last,int flags);void page_reset_target_data (target_ulong start,target_ulong end);int page_check_range (target_ulong start,target_ulong len,int flags);
accel/tcg/user-exec.c +7 -9
@@ -480,24 +480,22 @@ static bool pageflags_set_clear(target_ulong start, target_ulong last,* 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) +void page_set_flags (target_ulong start,target_ulong last,int flags) { -target_ulong last;bool reset = false;bool inval_tb = false;/* 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. */ -assert (start < end); -assert (end -1 <=GUEST_ADDR_MAX ); +assert (start <= last); +assert (last <=GUEST_ADDR_MAX );/* Only set PAGE_ANON with new mappings. */ assert (!(flags &PAGE_ANON ) || (flags &PAGE_RESET ));assert_memory_lock (); - start = start &TARGET_PAGE_MASK ; - end =TARGET_PAGE_ALIGN (end); - last = end -1 ; + start &=TARGET_PAGE_MASK ; + last |= ~TARGET_PAGE_MASK ;if (!(flags &PAGE_VALID )) { flags =0 ;
@@ -510,7 +508,7 @@ void page_set_flags(target_ulong start, target_ulong end, int flags) }if (!flags || reset) { -page_reset_target_data (start, end); +page_reset_target_data (start, last +1 ); inval_tb |=pageflags_unset (start, last); }if (flags) {
@@ -518,7 +516,7 @@ void page_set_flags(target_ulong start, target_ulong end, int flags) ~(reset ? 0 : PAGE_STICKY)); }if (inval_tb) { -tb_invalidate_phys_range (start, end); +tb_invalidate_phys_range (start, last +1 ); } }
bsd-user/mmap.c +3 -3
@@ -118,7 +118,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)if (ret !=0 ) goto error; } -page_set_flags (start, start + len, prot |PAGE_VALID ); +page_set_flags (start, start + len -1 , prot |PAGE_VALID );mmap_unlock ();return 0 ; error:
@@ -656,7 +656,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, } } the_end1: -page_set_flags (start, start + len, prot |PAGE_VALID ); +page_set_flags (start, start + len -1 , prot |PAGE_VALID ); the_end:#ifdef DEBUG_MMAP printf ("ret=0x" TARGET_ABI_FMT_lx"\n" , start);
@@ -767,7 +767,7 @@ int target_munmap(abi_ulong start, abi_ulong len) }if (ret ==0 ) { -page_set_flags (start, start + len,0 ); +page_set_flags (start, start + len -1 ,0 ); }mmap_unlock ();return ret;
linux-user/elfload.c +6 -5
@@ -213,7 +213,7 @@ static bool init_guest_commpage(void)exit (EXIT_FAILURE ); }page_set_flags (TARGET_VSYSCALL_PAGE , -TARGET_VSYSCALL_PAGE +TARGET_PAGE_SIZE , +TARGET_VSYSCALL_PAGE | ~TARGET_PAGE_MASK ,PAGE_EXEC |PAGE_VALID );return true; }
@@ -444,7 +444,7 @@ static bool init_guest_commpage(void)exit (EXIT_FAILURE ); } -page_set_flags (commpage, commpage + qemu_host_page_size, +page_set_flags (commpage, commpage | ~qemu_host_page_mask,PAGE_READ |PAGE_EXEC |PAGE_VALID );return true; }
@@ -1316,7 +1316,7 @@ static bool init_guest_commpage(void)exit (EXIT_FAILURE ); } -page_set_flags (LO_COMMPAGE ,LO_COMMPAGE +TARGET_PAGE_SIZE , +page_set_flags (LO_COMMPAGE ,LO_COMMPAGE | ~TARGET_PAGE_MASK ,PAGE_READ |PAGE_EXEC |PAGE_VALID );return true; }
@@ -1728,7 +1728,7 @@ static bool init_guest_commpage(void)* and implement syscalls. Here, simply mark the page executable. * Special case the entry points during translation (see do_page_zero). */ -page_set_flags (LO_COMMPAGE ,LO_COMMPAGE +TARGET_PAGE_SIZE , +page_set_flags (LO_COMMPAGE ,LO_COMMPAGE | ~TARGET_PAGE_MASK ,PAGE_EXEC |PAGE_VALID );return true; }
@@ -2209,7 +2209,8 @@ static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)/* Ensure that the bss page(s) are valid */ if ((page_get_flags (last_bss-1 ) & prot) != prot) { -page_set_flags (elf_bss &TARGET_PAGE_MASK , last_bss, prot |PAGE_VALID ); +page_set_flags (elf_bss &TARGET_PAGE_MASK , last_bss -1 , + prot |PAGE_VALID ); }if (host_start < host_map_start) {
linux-user/mmap.c +8 -8
@@ -181,7 +181,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int target_prot) } } -page_set_flags (start, start + len, page_flags); +page_set_flags (start, start + len -1 , page_flags); ret =0 ; error:
@@ -640,15 +640,15 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, } page_flags |=PAGE_RESET ;if (passthrough_start == passthrough_end) { -page_set_flags (start, start + len, page_flags); +page_set_flags (start, start + len -1 , page_flags); }else {if (start < passthrough_start) { -page_set_flags (start, passthrough_start, page_flags); +page_set_flags (start, passthrough_start -1 , page_flags); } -page_set_flags (passthrough_start, passthrough_end, +page_set_flags (passthrough_start, passthrough_end -1 , page_flags |PAGE_PASSTHROUGH );if (passthrough_end < start + len) { -page_set_flags (passthrough_end, start + len, page_flags); +page_set_flags (passthrough_end, start + len -1 , page_flags); } } the_end:
@@ -763,7 +763,7 @@ int target_munmap(abi_ulong start, abi_ulong len) }if (ret ==0 ) { -page_set_flags (start, start + len,0 ); +page_set_flags (start, start + len -1 ,0 ); }mmap_unlock ();return ret;
@@ -849,8 +849,8 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, }else { new_addr =h2g (host_addr); prot =page_get_flags (old_addr); -page_set_flags (old_addr, old_addr + old_size,0 ); -page_set_flags (new_addr, new_addr + new_size, +page_set_flags (old_addr, old_addr + old_size -1 ,0 ); +page_set_flags (new_addr, new_addr + new_size -1 , prot |PAGE_VALID |PAGE_RESET ); }mmap_unlock ();
linux-user/syscall.c +2 -2
@@ -4595,7 +4595,7 @@ static inline abi_ulong do_shmat(CPUArchState *cpu_env, } raddr=h2g ((unsigned long )host_raddr); -page_set_flags (raddr, raddr + shm_info.shm_segsz, +page_set_flags (raddr, raddr + shm_info.shm_segsz -1 ,PAGE_VALID |PAGE_RESET |PAGE_READ | (shmflg &SHM_RDONLY ?0 :PAGE_WRITE ));
@@ -4625,7 +4625,7 @@ static inline abi_long do_shmdt(abi_ulong shmaddr)for (i =0 ; i <N_SHM_REGIONS ; ++i) {if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) { shm_regions[i].in_use = false; -page_set_flags (shmaddr, shmaddr + shm_regions[i].size,0 ); +page_set_flags (shmaddr, shmaddr + shm_regions[i].size -1 ,0 );break ; } }
Signature · 1 line
2.34.1
PATCH v2 5/12 accel/tcg: Pass last not end to page_reset_target_data
In reply to Richard HendersonPass the address of the last byte to be changed, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved.
--- include/exec/cpu-all.h | 2 +- accel/tcg/user-exec.c | 11 +++++------ linux-user/mmap.c | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-)
include/exec/cpu-all.h +1 -1
@@ -277,7 +277,7 @@ 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 last,int flags); -void page_reset_target_data (target_ulong start,target_ulong end); +void page_reset_target_data (target_ulong start,target_ulong last);int page_check_range (target_ulong start,target_ulong len,int flags);/**
accel/tcg/user-exec.c +5 -6
@@ -508,7 +508,7 @@ void page_set_flags(target_ulong start, target_ulong last, int flags) }if (!flags || reset) { -page_reset_target_data (start, last +1 ); +page_reset_target_data (start, last); inval_tb |=pageflags_unset (start, last); }if (flags) {
@@ -814,15 +814,14 @@ typedef struct TargetPageDataNode {static IntervalTreeRoot targetdata_root; -void page_reset_target_data (target_ulong start,target_ulong end) +void page_reset_target_data (target_ulong start,target_ulong last) {IntervalTreeNode *n, *next; -target_ulong last;assert_memory_lock (); - start = start &TARGET_PAGE_MASK ; - last =TARGET_PAGE_ALIGN (end) -1 ; + start &=TARGET_PAGE_MASK ; + last |= ~TARGET_PAGE_MASK ; for (n = interval_tree_iter_first(&targetdata_root, start, last), next = n ? interval_tree_iter_next(n, start, last) : NULL;
@@ -885,7 +884,7 @@ void *page_get_target_data(target_ulong address) return t->data[(page - region) >> TARGET_PAGE_BITS]; }#else -void page_reset_target_data (target_ulong start,target_ulong end) { } +void page_reset_target_data (target_ulong start,target_ulong last) { }#endif /* TARGET_PAGE_DATA_SIZE */ /* The softmmu versions of these helpers are in cputlb.c. */
linux-user/mmap.c +1 -1
@@ -946,7 +946,7 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)if (can_passthrough_madvise (start, end)) { ret =get_errno (madvise (g2h_untagged (start), len, advice));if ((advice ==MADV_DONTNEED ) && (ret ==0 )) { -page_reset_target_data (start, start + len); +page_reset_target_data (start, start + len -1 ); } } }
Signature · 1 line
2.34.1
PATCH v2 6/12 accel/tcg: Pass last not end to PAGE_FOR_EACH_TB
In reply to Richard HendersonPass the address of the last byte to be changed, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved.
--- accel/tcg/tb-maint.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-)
accel/tcg/tb-maint.c +16 -12
@@ -127,29 +127,29 @@ static void tb_remove(TranslationBlock *tb) }/* TODO: For now, still shared with translate-all.c for system mode. */ -#define PAGE_FOR_EACH_TB (start, end, pagedesc,T ,N ) \ - for (T = foreach_tb_first(start, end), \ - N = foreach_tb_next(T, start, end); \ +#define PAGE_FOR_EACH_TB (start, last, pagedesc,T ,N ) \ + for (T = foreach_tb_first(start, last), \ + N = foreach_tb_next(T, start, last); \ T != NULL; \ - T = N, N = foreach_tb_next(N, start, end)) + T = N, N = foreach_tb_next(N, start, last))typedef TranslationBlock *PageForEachNext ;static PageForEachNext foreach_tb_first (tb_page_addr_t start, -tb_page_addr_t end) +tb_page_addr_t last) { -IntervalTreeNode *n =interval_tree_iter_first (&tb_root, start, end -1 ); +IntervalTreeNode *n =interval_tree_iter_first (&tb_root, start, last);return n ?container_of (n, TranslationBlock, itree) :NULL ; }static PageForEachNext foreach_tb_next (PageForEachNext tb,tb_page_addr_t start, -tb_page_addr_t end) +tb_page_addr_t last) {IntervalTreeNode *n;if (tb) { - n =interval_tree_iter_next (&tb->itree, start, end -1 ); + n =interval_tree_iter_next (&tb->itree, start, last);if (n) {return container_of (n, TranslationBlock, itree); }
@@ -320,7 +320,7 @@ struct page_collection { };typedef int PageForEachNext ; -#define PAGE_FOR_EACH_TB (start, end, pagedesc, tb, n) \ +#define PAGE_FOR_EACH_TB (start, last, pagedesc, tb, n) \ TB_FOR_EACH_TAGGED((pagedesc)->first_tb, tb, n, page_next)#ifdef CONFIG_DEBUG_TCG
@@ -995,10 +995,11 @@ void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end) {TranslationBlock *tb;PageForEachNext n; +tb_page_addr_t last = end -1 ;assert_memory_lock (); - PAGE_FOR_EACH_TB(start, end, unused, tb, n) { + PAGE_FOR_EACH_TB(start, last, unused, tb, n) {tb_phys_invalidate__locked (tb); } }
@@ -1030,6 +1031,7 @@ bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr, uintptr_t pc)bool current_tb_modified;TranslationBlock *tb;PageForEachNext n; +tb_page_addr_t last;/* * Without precise smc semantics, or when outside of a TB,
@@ -1046,10 +1048,11 @@ bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr, uintptr_t pc)assert_memory_lock (); current_tb =tcg_tb_lookup (pc); + last = addr | ~TARGET_PAGE_MASK ; addr &=TARGET_PAGE_MASK ; current_tb_modified = false; - PAGE_FOR_EACH_TB(addr, addr + TARGET_PAGE_SIZE, unused, tb, n) { + PAGE_FOR_EACH_TB(addr, last, unused, tb, n) {if (current_tb == tb && (tb_cflags (current_tb) &CF_COUNT_MASK ) !=1 ) {/*
@@ -1091,12 +1094,13 @@ tb_invalidate_phys_page_range__locked(struct page_collection *pages,bool current_tb_modified = false;TranslationBlock *current_tb = retaddr ?tcg_tb_lookup (retaddr) :NULL ;#endif /* TARGET_HAS_PRECISE_SMC */ + tb_page_addr_t last G_GNUC_UNUSED = end - 1;/* * We remove all the TBs in the range [start, end[. * XXX: see if in some cases it could be faster to invalidate all the code */ - PAGE_FOR_EACH_TB(start, end, p, tb, n) { + PAGE_FOR_EACH_TB(start, last, p, tb, n) {/* NOTE: this is subtle as a TB may span two physical pages */ if (n ==0 ) {/* NOTE: tb_end may be after the end of the page, but
Signature · 1 line
2.34.1
PATCH v2 7/12 accel/tcg: Pass last not end to page_collection_lock
In reply to Richard HendersonPass the address of the last byte to be changed, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved.
Fixes a bug in the loop comparision where "<= end" would lock one more page than required.
--- accel/tcg/tb-maint.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
accel/tcg/tb-maint.c +11 -11
@@ -511,20 +511,20 @@ static gint tb_page_addr_cmp(gconstpointer ap, gconstpointer bp, gpointer udata) }/* -* Lock a range of pages ([@start,@end[) as well as the pages of all +* Lock a range of pages ([@start,@last]) as well as the pages of all * intersecting TBs. * Locking order: acquire locks in ascending order of page index. */ static struct page_collection *page_collection_lock (tb_page_addr_t start, -tb_page_addr_t end) +tb_page_addr_t last) {struct page_collection *set =g_malloc (sizeof (*set));tb_page_addr_t index;PageDesc *pd; start >>=TARGET_PAGE_BITS ; - end >>=TARGET_PAGE_BITS ; -g_assert (start <= end); + last >>=TARGET_PAGE_BITS ; +g_assert (start <= last); set->tree =q_tree_new_full (tb_page_addr_cmp,NULL ,NULL , page_entry_destroy);
@@ -534,7 +534,7 @@ static struct page_collection *page_collection_lock(tb_page_addr_t start, retry:q_tree_foreach (set->tree, page_entry_lock,NULL ); -for (index = start; index <= end; index++) { +for (index = start; index <= last; index++) {TranslationBlock *tb;PageForEachNext n;
@@ -1154,7 +1154,7 @@ tb_invalidate_phys_page_range__locked(struct page_collection *pages,void tb_invalidate_phys_page (tb_page_addr_t addr) {struct page_collection *pages; -tb_page_addr_t start, end; +tb_page_addr_t start, last;PageDesc *p; p =page_find (addr >>TARGET_PAGE_BITS );
@@ -1163,9 +1163,9 @@ void tb_invalidate_phys_page(tb_page_addr_t addr) } start = addr &TARGET_PAGE_MASK ; - end = start +TARGET_PAGE_SIZE ; - pages =page_collection_lock (start, end); -tb_invalidate_phys_page_range__locked (pages, p, start, end,0 ); + last = addr | ~TARGET_PAGE_MASK ; + pages =page_collection_lock (start, last); +tb_invalidate_phys_page_range__locked (pages, p, start, last +1 ,0 );page_collection_unlock (pages); }
@@ -1181,7 +1181,7 @@ void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end)struct page_collection *pages;tb_page_addr_t next; - pages =page_collection_lock (start, end); + pages =page_collection_lock (start, end -1 );for (next = (start &TARGET_PAGE_MASK ) +TARGET_PAGE_SIZE ; start < end; start = next, next +=TARGET_PAGE_SIZE ) {
@@ -1226,7 +1226,7 @@ void tb_invalidate_phys_range_fast(ram_addr_t ram_addr, {struct page_collection *pages; - pages =page_collection_lock (ram_addr, ram_addr + size); + pages =page_collection_lock (ram_addr, ram_addr + size -1 );tb_invalidate_phys_page_fast__locked (pages, ram_addr, size, retaddr);page_collection_unlock (pages); }
Signature · 1 line
2.34.1
PATCH v2 8/12 accel/tcg: Pass last not end to tb_invalidate_phys_page_range__locked
In reply to Richard HendersonPass the address of the last byte to be changed, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved.
Properly truncate tb_last to the end of the page; the comment about tb_end being past the end of the page being ok is not correct, considering overflow.
--- accel/tcg/tb-maint.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-)
accel/tcg/tb-maint.c +12 -14
@@ -1084,35 +1084,33 @@ bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr, uintptr_t pc)static void tb_invalidate_phys_page_range__locked (struct page_collection *pages,PageDesc *p,tb_page_addr_t start, -tb_page_addr_t end, +tb_page_addr_t last,uintptr_t retaddr) {TranslationBlock *tb; -tb_page_addr_t tb_start, tb_end;PageForEachNext n;#ifdef TARGET_HAS_PRECISE_SMC bool current_tb_modified = false;TranslationBlock *current_tb = retaddr ?tcg_tb_lookup (retaddr) :NULL ;#endif /* TARGET_HAS_PRECISE_SMC */ - tb_page_addr_t last G_GNUC_UNUSED = end - 1;/* -* We remove all the TBs in the range [start, end[. +* We remove all the TBs in the range [start, last]. * XXX: see if in some cases it could be faster to invalidate all the code */ PAGE_FOR_EACH_TB(start, last, p, tb, n) { +tb_page_addr_t tb_start, tb_last; +/* NOTE: this is subtle as a TB may span two physical pages */ + tb_start =tb_page_addr0 (tb); + tb_last = tb_start + tb->size -1 ;if (n ==0 ) { -/* NOTE: tb_end may be after the end of the page, but -it is not a problem */ - tb_start =tb_page_addr0 (tb); - tb_end = tb_start + tb->size; + tb_last =MIN (tb_last, tb_start | ~TARGET_PAGE_MASK ); }else { tb_start =tb_page_addr1 (tb); - tb_end = tb_start + ((tb_page_addr0 (tb) + tb->size) - & ~TARGET_PAGE_MASK ); + tb_last = tb_start + (tb_last & ~TARGET_PAGE_MASK ); } -if (!(tb_end <= start || tb_start >= end)) { +if (!(tb_last < start || tb_start > last)) {#ifdef TARGET_HAS_PRECISE_SMC if (current_tb == tb && (tb_cflags (current_tb) &CF_COUNT_MASK ) !=1 ) {
@@ -1165,7 +1163,7 @@ void tb_invalidate_phys_page(tb_page_addr_t addr) start = addr &TARGET_PAGE_MASK ; last = addr | ~TARGET_PAGE_MASK ; pages =page_collection_lock (start, last); -tb_invalidate_phys_page_range__locked (pages, p, start, last +1 ,0 ); +tb_invalidate_phys_page_range__locked (pages, p, start, last,0 );page_collection_unlock (pages); }
@@ -1192,7 +1190,7 @@ void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end)continue ; }assert_page_locked (pd); -tb_invalidate_phys_page_range__locked (pages, pd, start, bound,0 ); +tb_invalidate_phys_page_range__locked (pages, pd, start, bound -1 ,0 ); }page_collection_unlock (pages); }
@@ -1212,7 +1210,7 @@ static void tb_invalidate_phys_page_fast__locked(struct page_collection *pages, }assert_page_locked (p); -tb_invalidate_phys_page_range__locked (pages, p, start, start + len, ra); +tb_invalidate_phys_page_range__locked (pages, p, start, start + len -1 , ra); }/*
Signature · 1 line
2.34.1
PATCH v2 9/12 accel/tcg: Pass last not end to tb_invalidate_phys_range
In reply to Richard HendersonPass the address of the last byte to be changed, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved.
--- include/exec/exec-all.h | 2 +- accel/tcg/tb-maint.c | 31 ++++++++++++++++--------------- accel/tcg/translate-all.c | 2 +- accel/tcg/user-exec.c | 2 +- softmmu/physmem.c | 2 +- 5 files changed, 20 insertions(+), 19 deletions(-)
include/exec/exec-all.h +1 -1
@@ -678,7 +678,7 @@ void tb_invalidate_phys_addr(target_ulong addr);void tb_invalidate_phys_addr (AddressSpace *as,hwaddr addr,MemTxAttrs attrs);#endif void tb_phys_invalidate (TranslationBlock *tb,tb_page_addr_t page_addr); -void tb_invalidate_phys_range (tb_page_addr_t start,tb_page_addr_t end); +void tb_invalidate_phys_range (tb_page_addr_t start,tb_page_addr_t last);void tb_set_jmp_target (TranslationBlock *tb,int n,uintptr_t addr);/* GETPC is the true target of the return instruction that we'll execute. */
accel/tcg/tb-maint.c +16 -15
@@ -991,11 +991,10 @@ TranslationBlock *tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc,* Called with mmap_lock held for user-mode emulation. * NOTE: this function must not be called while a TB is running. */ -void tb_invalidate_phys_range (tb_page_addr_t start,tb_page_addr_t end) +void tb_invalidate_phys_range (tb_page_addr_t start,tb_page_addr_t last) {TranslationBlock *tb;PageForEachNext n; -tb_page_addr_t last = end -1 ;assert_memory_lock ();
@@ -1011,11 +1010,11 @@ void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end)*/ void tb_invalidate_phys_page (tb_page_addr_t addr) { -tb_page_addr_t start, end; +tb_page_addr_t start, last; start = addr &TARGET_PAGE_MASK ; - end = start +TARGET_PAGE_SIZE ; -tb_invalidate_phys_range (start, end); + last = addr | ~TARGET_PAGE_MASK ; +tb_invalidate_phys_range (start, last); }/*
@@ -1169,28 +1168,30 @@ void tb_invalidate_phys_page(tb_page_addr_t addr)/* * Invalidate all TBs which intersect with the target physical address range -* [start;end[. NOTE: start and end may refer to *different* physical pages. +* [start;last]. NOTE: start and end may refer to *different* physical pages. * 'is_cpu_write_access' should be true if called from a real cpu write * access: the virtual CPU will exit the current TB if code is modified inside * this TB. */ -void tb_invalidate_phys_range (tb_page_addr_t start,tb_page_addr_t end) +void tb_invalidate_phys_range (tb_page_addr_t start,tb_page_addr_t last) {struct page_collection *pages; -tb_page_addr_t next; +tb_page_addr_t index, index_last; - pages =page_collection_lock (start, end -1 ); -for (next = (start &TARGET_PAGE_MASK ) +TARGET_PAGE_SIZE ; - start < end; - start = next, next +=TARGET_PAGE_SIZE ) { -PageDesc *pd =page_find (start >>TARGET_PAGE_BITS ); -tb_page_addr_t bound =MIN (next, end); + pages =page_collection_lock (start, last); + + index_last = last >>TARGET_PAGE_BITS ; +for (index = start >>TARGET_PAGE_BITS ; index <= index_last; index++) { +PageDesc *pd =page_find (index); +tb_page_addr_t bound;if (pd ==NULL ) {continue ; }assert_page_locked (pd); -tb_invalidate_phys_page_range__locked (pages, pd, start, bound -1 ,0 ); + bound = (index <<TARGET_PAGE_BITS ) | ~TARGET_PAGE_MASK ; + bound =MIN (bound, last); +tb_invalidate_phys_page_range__locked (pages, pd, start, bound,0 ); }page_collection_unlock (pages); }
accel/tcg/translate-all.c +1 -1
@@ -572,7 +572,7 @@ void tb_check_watchpoint(CPUState *cpu, uintptr_t retaddr)cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags); addr =get_page_addr_code (env, pc);if (addr !=-1 ) { -tb_invalidate_phys_range (addr, addr +1 ); +tb_invalidate_phys_range (addr, addr); } } }
accel/tcg/user-exec.c +1 -1
@@ -516,7 +516,7 @@ void page_set_flags(target_ulong start, target_ulong last, int flags) ~(reset ? 0 : PAGE_STICKY)); }if (inval_tb) { -tb_invalidate_phys_range (start, last +1 ); +tb_invalidate_phys_range (start, last); } }
softmmu/physmem.c +1 -1
@@ -2527,7 +2527,7 @@ static void invalidate_and_set_dirty(MemoryRegion *mr, hwaddr addr, }if (dirty_log_mask & (1 <<DIRTY_MEMORY_CODE )) {assert (tcg_enabled ()); -tb_invalidate_phys_range (addr, addr + length); +tb_invalidate_phys_range (addr, addr + length -1 ); dirty_log_mask &= ~(1 <<DIRTY_MEMORY_CODE ); }cpu_physical_memory_set_dirty_range (addr, length, dirty_log_mask);
Signature · 1 line
2.34.1
PATCH v2 10/12 linux-user: Pass last not end to probe_guest_base
In reply to Richard HendersonPass the address of the last byte of the image, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved.
--- linux-user/elfload.c | 24 ++++++++++++------------ linux-user/flatload.c | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-)
linux-user/elfload.c +12 -12
@@ -2504,7 +2504,7 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,if (guest_hiaddr > reserved_va) {error_report ("%s: requires more than reserved virtual " "address space (0x%" PRIx64" > 0x%lx)" , - image_name, (uint64_t )guest_hiaddr, reserved_va); + image_name, (uint64_t )guest_hiaddr +1 , reserved_va);exit (EXIT_FAILURE ); } }else {
@@ -2512,7 +2512,7 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,if ((guest_hiaddr - guest_base) > ~(uintptr_t )0 ) {error_report ("%s: requires more virtual address space " "than the host can provide (0x%" PRIx64")" , - image_name, (uint64_t )guest_hiaddr - guest_base); + image_name, (uint64_t )guest_hiaddr +1 - guest_base);exit (EXIT_FAILURE ); }#endif
@@ -2525,18 +2525,18 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,if (reserved_va) { guest_loaddr = (guest_base >= mmap_min_addr ?0 : mmap_min_addr - guest_base); - guest_hiaddr = reserved_va; + guest_hiaddr = reserved_va -1 ; }/* Reserve the address space for the binary, or reserved_va. */ test =g2h_untagged (guest_loaddr); - addr =mmap (test, guest_hiaddr - guest_loaddr,PROT_NONE , flags,-1 ,0 ); + addr =mmap (test, guest_hiaddr - guest_loaddr +1 ,PROT_NONE , flags,-1 ,0 );if (test != addr) {pgb_fail_in_use (image_name); }qemu_log_mask (CPU_LOG_PAGE , -"%s: base @ %p for " TARGET_ABI_FMT_ld" bytes\n" , - __func__, addr, guest_hiaddr - guest_loaddr); +"%s: base @ %p for %" PRIu64" bytes\n" , + __func__, addr, (uint64_t )guest_hiaddr - guest_loaddr +1 ); }/**
@@ -2680,7 +2680,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr,if (hiaddr != orig_hiaddr) {error_report ("%s: requires virtual address space that the " "host cannot provide (0x%" PRIx64")" , - image_name, (uint64_t )orig_hiaddr); + image_name, (uint64_t )orig_hiaddr +1 );exit (EXIT_FAILURE ); }
@@ -2694,7 +2694,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr,* arithmetic wraps around. */ if (sizeof (uintptr_t ) ==8 || loaddr >=0x80000000u ) { - hiaddr = (uintptr_t )4 <<30 ; + hiaddr =UINT32_MAX ; }else { offset = -(HI_COMMPAGE & -align); }
@@ -2702,7 +2702,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr, loaddr =MIN (loaddr,LO_COMMPAGE & -align); } - addr =pgb_find_hole (loaddr, hiaddr - loaddr, align, offset); + addr =pgb_find_hole (loaddr, hiaddr - loaddr +1 , align, offset);if (addr ==-1 ) {/* * If HI_COMMPAGE, there *might* be a non-consecutive allocation
@@ -2755,7 +2755,7 @@ static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,if (guest_hiaddr > reserved_va) {error_report ("%s: requires more than reserved virtual " "address space (0x%" PRIx64" > 0x%lx)" , - image_name, (uint64_t )guest_hiaddr, reserved_va); + image_name, (uint64_t )guest_hiaddr +1 , reserved_va);exit (EXIT_FAILURE ); }
@@ -3021,7 +3021,7 @@ static void load_elf_image(const char *image_name, int image_fd,if (a < loaddr) { loaddr = a; } - a = eppnt->p_vaddr + eppnt->p_memsz; + a = eppnt->p_vaddr + eppnt->p_memsz -1 ;if (a > hiaddr) { hiaddr = a; }
@@ -3112,7 +3112,7 @@ static void load_elf_image(const char *image_name, int image_fd,* In both cases, we will overwrite pages in this range with mappings * from the executable. */ - load_addr =target_mmap (loaddr, hiaddr - loaddr,PROT_NONE , + load_addr =target_mmap (loaddr, (size_t )hiaddr - loaddr +1 ,PROT_NONE ,MAP_PRIVATE |MAP_ANON |MAP_NORESERVE | (ehdr->e_type ==ET_EXEC ?MAP_FIXED :0 ),-1 ,0 );
linux-user/flatload.c +1 -1
@@ -448,7 +448,7 @@ static int load_flat_file(struct linux_binprm * bprm,* Allocate the address space. */ probe_guest_base (bprm->filename,0 , - text_len + data_len + extra + indx_len); + text_len + data_len + extra + indx_len -1 );/* * there are a couple of cases here, the separate code/data
Signature · 1 line
2.34.1
Re: [PATCH for-8.0 v2 10/12] linux-user: Pass last not end to probe_guest_base
Hi Richard,
Quoted 114 lines · Richard Henderson
Pass the address of the last byte of the image, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved.
Signed-off-by Richard Henderson <[email protected]>--- linux-user/elfload.c | 24 ++++++++++++------------ linux-user/flatload.c | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-)linux-user/elfload.c +12
-12@@ -2504,7 +2504,7 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,if (guest_hiaddr > reserved_va) {error_report ("%s: requires more than reserved virtual " "address space (0x%" PRIx64" > 0x%lx)" , - image_name, (uint64_t )guest_hiaddr, reserved_va); + image_name, (uint64_t )guest_hiaddr +1 , reserved_va);exit (EXIT_FAILURE ); } }else {@@ -2512,7 +2512,7 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,if ((guest_hiaddr - guest_base) > ~(uintptr_t )0 ) {error_report ("%s: requires more virtual address space " "than the host can provide (0x%" PRIx64")" , - image_name, (uint64_t )guest_hiaddr - guest_base); + image_name, (uint64_t )guest_hiaddr +1 - guest_base);exit (EXIT_FAILURE ); }#endif @@ -2525,18 +2525,18 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,if (reserved_va) { guest_loaddr = (guest_base >= mmap_min_addr ?0 : mmap_min_addr - guest_base); - guest_hiaddr = reserved_va; + guest_hiaddr = reserved_va -1 ; }/* Reserve the address space for the binary, or reserved_va. */ test =g2h_untagged (guest_loaddr); - addr =mmap (test, guest_hiaddr - guest_loaddr,PROT_NONE , flags,-1 ,0 ); + addr =mmap (test, guest_hiaddr - guest_loaddr +1 ,PROT_NONE , flags,-1 ,0 );if (test != addr) {pgb_fail_in_use (image_name); }qemu_log_mask (CPU_LOG_PAGE , -"%s: base @ %p for " TARGET_ABI_FMT_ld" bytes\n" , - __func__, addr, guest_hiaddr - guest_loaddr); +"%s: base @ %p for %" PRIu64" bytes\n" , + __func__, addr, (uint64_t )guest_hiaddr - guest_loaddr +1 ); }/** @@ -2680,7 +2680,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr,if (hiaddr != orig_hiaddr) {error_report ("%s: requires virtual address space that the " "host cannot provide (0x%" PRIx64")" , - image_name, (uint64_t )orig_hiaddr); + image_name, (uint64_t )orig_hiaddr +1 );exit (EXIT_FAILURE ); }@@ -2694,7 +2694,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr,* arithmetic wraps around. */ if (sizeof (uintptr_t ) ==8 || loaddr >=0x80000000u ) { - hiaddr = (uintptr_t )4 <<30 ; + hiaddr =UINT32_MAX ; }else { offset = -(HI_COMMPAGE & -align); }@@ -2702,7 +2702,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr, loaddr =MIN (loaddr,LO_COMMPAGE & -align); } - addr =pgb_find_hole (loaddr, hiaddr - loaddr, align, offset); + addr =pgb_find_hole (loaddr, hiaddr - loaddr +1 , align, offset);if (addr ==-1 ) {/* * If HI_COMMPAGE, there *might* be a non-consecutive allocation @@ -2755,7 +2755,7 @@ static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,if (guest_hiaddr > reserved_va) {error_report ("%s: requires more than reserved virtual " "address space (0x%" PRIx64" > 0x%lx)" , - image_name, (uint64_t )guest_hiaddr, reserved_va); + image_name, (uint64_t )guest_hiaddr +1 , reserved_va);exit (EXIT_FAILURE ); }@@ -3021,7 +3021,7 @@ static void load_elf_image(const char *image_name, int image_fd,if (a < loaddr) { loaddr = a; } - a = eppnt->p_vaddr + eppnt->p_memsz; + a = eppnt->p_vaddr + eppnt->p_memsz -1 ;if (a > hiaddr) { hiaddr = a; }@@ -3112,7 +3112,7 @@ static void load_elf_image(const char *image_name, int image_fd,* In both cases, we will overwrite pages in this range with mappings * from the executable. */ - load_addr =target_mmap (loaddr, hiaddr - loaddr,PROT_NONE , + load_addr =target_mmap (loaddr, (size_t )hiaddr - loaddr +1 ,PROT_NONE ,MAP_PRIVATE |MAP_ANON |MAP_NORESERVE | (ehdr->e_type ==ET_EXEC ?MAP_FIXED :0 ),-1 ,0 );linux-user/flatload.c +0
-0
linux-user/flatload.c static int load_flat_file(struct linux_binprm * bprm, · line 451
Quoted 5 lines
linux-user/flatload.c +1
-1@@ -448,7 +448,7 @@ static int load_flat_file(struct linux_binprm * bprm,* Allocate the address space. */ probe_guest_base (bprm->filename,0 , - text_len + data_len + extra + indx_len); + text_len + data_len + extra + indx_len -1 );
Per linux-user/user-internals.h:
/**
* probe_guest_base:
* @image_name: the executable being loaded
* @loaddr: the lowest fixed address in the executable
* @hiaddr: the highest fixed address in the executable
*
* Creates the initial guest address space in the host memory space. *
* If @loaddr == 0, then no address in the executable is fixed,
* i.e. it is fully relocatable. In that case @hiaddr is the size
* of the executable.
*
* This function will not return if a valid value for guest_base
* cannot be chosen. On return, the executable loader can expect *
* target_mmap(loaddr, hiaddr - loaddr, ...)
*
* to succeed.
*/
Since here @loaddr == 0, "@hiaddr is the size of the executable", not "the first address past the last byte".
So we can not "Pass the address of the last byte of the image" to this API. Maybe the API description is incorrect, in that case your patch is right. Otherwise we might need to tune probe_guest_base().
Quoted 3 lines
linux-user/flatload.c +0
-0/* * there are a couple of cases here, the separate code/data
Re: [PATCH for-8.0 v2 10/12] linux-user: Pass last not end to probe_guest_base
Quoted 144 lines · Philippe Mathieu-Daudé
Hi Richard,
Quoted 113 lines · Richard Henderson
Pass the address of the last byte of the image, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved.
Signed-off-by Richard Henderson <[email protected]>---
linux-user/elfload.c | 24 ++++++++++++------------linux-user/flatload.c| 2 +-2 files changed, 13 insertions(+), 13 deletions(-)linux-user/elfload.c +0
-0@@ -2504,7 +2504,7 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr, if (guest_hiaddr > reserved_va) {error_report("%s: requires more than reserved virtual " "address space (0x%" PRIx64 " > 0x%lx)", -
image_name, (uint64_t)guest_hiaddr,reserved_va); +image_name, (uint64_t)guest_hiaddr + 1,reserved_va);exit(EXIT_FAILURE);
}
} else {linux-user/elfload.c +0
-0@@ -2512,7 +2512,7 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr, if ((guest_hiaddr - guest_base) > ~(uintptr_t)0) {error_report("%s: requires more virtual address space " "than the host can provide (0x%" PRIx64 ")", -
image_name, (uint64_t)guest_hiaddr -guest_base); +image_name, (uint64_t)guest_hiaddr + 1 -guest_base);exit(EXIT_FAILURE);
}
#endiflinux-user/elfload.c +0
-0@@ -2525,18 +2525,18 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr, if (reserved_va) {
guest_loaddr= (guest_base>=mmap_min_addr? 0
:mmap_min_addr-guest_base);
-guest_hiaddr=reserved_va;
+guest_hiaddr=reserved_va- 1;
}
/* Reserve the address space for the binary, orreserved_va. */ test =g2h_untagged(guest_loaddr);
- addr =mmap(test, guest_hiaddr - guest_loaddr, PROT_NONE, flags, -1, 0); + addr =mmap(test, guest_hiaddr - guest_loaddr + 1, PROT_NONE, flags, -1, 0); if (test != addr) {
pgb_fail_in_use(image_name);
}
qemu_log_mask(CPU_LOG_PAGE,
- "%s: base @ %p for "TARGET_ABI_FMT_ld" bytes\n", -__func__, addr,guest_hiaddr-guest_loaddr);
+ "%s: base @ %p for %" PRIu64 " bytes\n",
+__func__, addr, (uint64_t)guest_hiaddr -guest_loaddr+ 1); }
/**linux-user/elfload.c +0
-0@@ -2680,7 +2680,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr, if (hiaddr != orig_hiaddr) {error_report("%s: requires virtual address space that the " "host cannot provide (0x%" PRIx64 ")", -
image_name, (uint64_t)orig_hiaddr); +image_name, (uint64_t)orig_hiaddr + 1);exit(EXIT_FAILURE);
}linux-user/elfload.c +2
-2@@ -2694,7 +2694,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr,* arithmetic wraps around. */ if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) { - hiaddr = (uintptr_t) 4 << 30; + hiaddr = UINT32_MAX; } else { offset = -(HI_COMMPAGE & -align); }@@ -2702,7 +2702,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr, loaddr = MIN(loaddr, LO_COMMPAGE & -align); } - addr = pgb_find_hole(loaddr, hiaddr - loaddr, align, offset); + addr = pgb_find_hole(loaddr, hiaddr - loaddr + 1, align, offset); if (addr == -1) { /** If HI_COMMPAGE, there *might* be a non-consecutive allocation @@ -2755,7 +2755,7 @@ static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr, if (guest_hiaddr > reserved_va) {error_report("%s: requires more than reserved virtual " "address space (0x%" PRIx64 " > 0x%lx)", -
image_name, (uint64_t)guest_hiaddr,reserved_va); +image_name, (uint64_t)guest_hiaddr + 1,reserved_va);exit(EXIT_FAILURE);
}linux-user/elfload.c +2
-2@@ -3021,7 +3021,7 @@ static void load_elf_image(const char *image_name, int image_fd, if (a < loaddr) { loaddr = a; } - a = eppnt->p_vaddr + eppnt->p_memsz; + a = eppnt->p_vaddr + eppnt->p_memsz - 1; if (a > hiaddr) { hiaddr = a; }@@ -3112,7 +3112,7 @@ static void load_elf_image(const char *image_name, int image_fd,* In both cases, we will overwrite pages in this range with mappings * from the executable. */ - load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE, + load_addr = target_mmap(loaddr, (size_t)hiaddr - loaddr + 1, PROT_NONE,
MAP_PRIVATE|MAP_ANON|MAP_NORESERVE| (ehdr->e_type==ET_EXEC?MAP_FIXED: 0), -1, 0);linux-user/flatload.c +0
-0linux-user/flatload.c static int load_flat_file(struct linux_binprm * bprm, · line 451
Quoted 5 lines
linux-user/flatload.c +1
-1@@ -448,7 +448,7 @@ static int load_flat_file(struct linux_binprm * bprm,* Allocate the address space. */ probe_guest_base(bprm->filename, 0, - text_len + data_len + extra + indx_len); + text_len + data_len + extra + indx_len - 1);Per
linux-user/user-internals.h:/**
*probe_guest_base:
* @image_name: the executable being loaded
* @loaddr: the lowest fixed address in the executable
* @hiaddr: the highest fixed address in the executable
*
* Creates the initial guest address space in the host memory space. *
* If @loaddr == 0, then no address in the executable is fixed,
* i.e. it is fully relocatable. In that case @hiaddr is the size
* of the executable.
*
* This function will not return if a valid value forguest_base
* cannot be chosen. On return, the executable loader can expect *
*target_mmap(loaddr, hiaddr - loaddr, ...)
*
* to succeed.
*/
Quoted 7 lines
Since here @loaddr == 0, "@hiaddr is the size of the executable", not "the first address past the last byte".
So we can not "Pass the address of the last byte of the image" to this API. Maybe the API description is incorrect, in that case your patch is right. Otherwise we might need to tune
probe_guest_base().
I'll update the documentation.
r~
Philippe Mathieu-Daudé
Reviewed-byRe: [PATCH for-8.0 v2 10/12] linux-user: Pass last not end to probe_guest_base
Quoted 51 lines · Richard Henderson
Quoted 50 lines · Philippe Mathieu-Daudé
Hi Richard,
Quoted 15 lines · Richard Henderson
Pass the address of the last byte of the image, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved.
Signed-off-by Richard Henderson <[email protected]>---
linux-user/elfload.c | 24 ++++++++++++------------linux-user/flatload.c| 2 +-2 files changed, 13 insertions(+), 13 deletions(-)index
e99570ca18..5efec2630e100644linux-user/flatload.c static int load_flat_file(struct linux_binprm * · line 452
Quoted 6 lines
linux-user/flatload.c +1
-1@@ -448,7 +448,7 @@ static int load_flat_file(struct linux_binprm *bprm, * Allocate the address space. */ probe_guest_base(bprm->filename, 0, - text_len + data_len + extra + indx_len); + text_len + data_len + extra + indx_len - 1);Per
linux-user/user-internals.h:/**
*probe_guest_base:
* @image_name: the executable being loaded
* @loaddr: the lowest fixed address in the executable
* @hiaddr: the highest fixed address in the executable
*
* Creates the initial guest address space in the host memory space. *
* If @loaddr == 0, then no address in the executable is fixed,
* i.e. it is fully relocatable. In that case @hiaddr is the size
* of the executable.
*
* This function will not return if a valid value forguest_base
* cannot be chosen. On return, the executable loader can expect *
*target_mmap(loaddr, hiaddr - loaddr, ...)
*
* to succeed.
*/Since here @loaddr == 0, "@hiaddr is the size of the executable", not "the first address past the last byte".
Quoted 6 lines
Quoted 4 lines
So we can not "Pass the address of the last byte of the image" to this API. Maybe the API description is incorrect, in that case your patch is right. Otherwise we might need to tune
probe_guest_base().I'll update the documentation.
Then:
PATCH v2 11/12 include/exec: Change reserved_va semantics to last byte
In reply to Richard HendersonChange the semantics to be the last byte of the guest va, rather than the following byte. This avoids some overflow conditions.
--- include/exec/cpu-all.h | 11 ++++++++++- linux-user/arm/target_cpu.h | 2 +- bsd-user/main.c | 10 +++------- bsd-user/mmap.c | 4 ++-- linux-user/elfload.c | 14 +++++++------- linux-user/main.c | 27 +++++++++++++-------------- linux-user/mmap.c | 4 ++-- 7 files changed, 38 insertions(+), 34 deletions(-)
include/exec/cpu-all.h +10 -1
@@ -152,6 +152,15 @@ static inline void tswap64s(uint64_t *s)*/ extern uintptr_t guest_base;extern bool have_guest_base; + +/* +* If non-zero, the guest virtual address space is a contiguous subset +* of the host virtual address space, i.e. '-R reserved_va' is in effect +* either from the command-line or by default. The value is the last +* byte of the guest address space e.g. UINT32_MAX. +* +* If zero, the host and guest virtual address spaces are intermingled. +*/ extern unsigned long reserved_va;/*
@@ -171,7 +180,7 @@ extern unsigned long reserved_va;#define GUEST_ADDR_MAX_ \ ((MIN_CONST(TARGET_VIRT_ADDR_SPACE_BITS, TARGET_ABI_BITS) <= 32) ? \ UINT32_MAX : ~0ul) -#define GUEST_ADDR_MAX (reserved_va ? reserved_va - 1 : GUEST_ADDR_MAX_) +#define GUEST_ADDR_MAX (reserved_va ? : GUEST_ADDR_MAX_)#else
linux-user/arm/target_cpu.h +1 -1
@@ -30,7 +30,7 @@ static inline unsigned long arm_max_reserved_va(CPUState *cs)* the high addresses. Restrict linux-user to the * cached write-back RAM in the system map. */ -return 0x80000000ul ; +return 0x7ffffffful ; }else {/* * We need to be able to map the commpage.
bsd-user/main.c +3 -7
@@ -68,13 +68,9 @@ bool have_guest_base;# if HOST_LONG_BITS >TARGET_VIRT_ADDR_SPACE_BITS # if TARGET_VIRT_ADDR_SPACE_BITS ==32 && \ (TARGET_LONG_BITS ==32 || defined(TARGET_ABI32 )) -/* -* There are a number of places where we assign reserved_va to a variable -* of type abi_ulong and expect it to fit. Avoid the last page. -*/ -# define MAX_RESERVED_VA (0xfffffffful & TARGET_PAGE_MASK) +# define MAX_RESERVED_VA 0xfffffffful# else -# define MAX_RESERVED_VA (1ul << TARGET_VIRT_ADDR_SPACE_BITS) +# define MAX_RESERVED_VA ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)# endif # else # define MAX_RESERVED_VA 0
@@ -466,7 +462,7 @@ int main(int argc, char **argv)envlist_free (envlist);if (reserved_va) { - mmap_next_start = reserved_va; + mmap_next_start = reserved_va +1 ; } {
bsd-user/mmap.c +2 -2
@@ -234,7 +234,7 @@ static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size, size =HOST_PAGE_ALIGN (size) + alignment; end_addr = start + size;if (end_addr > reserved_va) { - end_addr = reserved_va; + end_addr = reserved_va +1 ; } addr = end_addr - qemu_host_page_size;
@@ -243,7 +243,7 @@ static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size,if (looped) {return (abi_ulong)-1 ; } - end_addr = reserved_va; + end_addr = reserved_va +1 ; addr = end_addr - qemu_host_page_size; looped =1 ;continue ;
linux-user/elfload.c +7 -7
@@ -208,7 +208,7 @@ static bool init_guest_commpage(void)* has specified -R reserved_va, which would trigger an assert(). */ if (reserved_va !=0 && -TARGET_VSYSCALL_PAGE +TARGET_PAGE_SIZE >= reserved_va) { +TARGET_VSYSCALL_PAGE +TARGET_PAGE_SIZE -1 > reserved_va) {error_report ("Cannot allocate vsyscall page" );exit (EXIT_FAILURE ); }
@@ -2504,7 +2504,7 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,if (guest_hiaddr > reserved_va) {error_report ("%s: requires more than reserved virtual " "address space (0x%" PRIx64" > 0x%lx)" , - image_name, (uint64_t )guest_hiaddr +1 , reserved_va); + image_name, (uint64_t )guest_hiaddr, reserved_va);exit (EXIT_FAILURE ); } }else {
@@ -2525,7 +2525,7 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,if (reserved_va) { guest_loaddr = (guest_base >= mmap_min_addr ?0 : mmap_min_addr - guest_base); - guest_hiaddr = reserved_va -1 ; + guest_hiaddr = reserved_va; }/* Reserve the address space for the binary, or reserved_va. */
@@ -2755,7 +2755,7 @@ static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,if (guest_hiaddr > reserved_va) {error_report ("%s: requires more than reserved virtual " "address space (0x%" PRIx64" > 0x%lx)" , - image_name, (uint64_t )guest_hiaddr +1 , reserved_va); + image_name, (uint64_t )guest_hiaddr, reserved_va);exit (EXIT_FAILURE ); }
@@ -2768,17 +2768,17 @@ static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,/* Reserve the memory on the host. */ assert (guest_base !=0 ); test =g2h_untagged (0 ); - addr =mmap (test, reserved_va,PROT_NONE , flags,-1 ,0 ); + addr =mmap (test, reserved_va +1 ,PROT_NONE , flags,-1 ,0 );if (addr ==MAP_FAILED || addr != test) {error_report ("Unable to reserve 0x%lx bytes of virtual address " "space at %p (%s) for use as guest address space (check your " "virtual memory ulimit setting, min_mmap_addr or reserve less " -"using -R option)" , reserved_va, test,strerror (errno)); +"using -R option)" , reserved_va +1 , test,strerror (errno));exit (EXIT_FAILURE ); }qemu_log_mask (CPU_LOG_PAGE ,"%s: base @ %p for %lu bytes\n" , - __func__, addr, reserved_va); + __func__, addr, reserved_va +1 ); } void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
linux-user/main.c +13 -14
@@ -109,11 +109,9 @@ static const char *last_log_filename;# if HOST_LONG_BITS >TARGET_VIRT_ADDR_SPACE_BITS # if TARGET_VIRT_ADDR_SPACE_BITS ==32 && \ (TARGET_LONG_BITS ==32 || defined(TARGET_ABI32 )) -/* There are a number of places where we assign reserved_va to a variable -of type abi_ulong and expect it to fit. Avoid the last page. */ -# define MAX_RESERVED_VA (CPU ) (0xfffffffful & TARGET_PAGE_MASK) +# define MAX_RESERVED_VA (CPU ) 0xfffffffful# else -# define MAX_RESERVED_VA (CPU ) (1ul << TARGET_VIRT_ADDR_SPACE_BITS) +# define MAX_RESERVED_VA (CPU ) ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)# endif # else # define MAX_RESERVED_VA (CPU ) 0
@@ -379,7 +377,9 @@ static void handle_arg_reserved_va(const char *arg) {char *p;int shift =0 ; - reserved_va =strtoul (arg, &p,0 ); +unsigned long val; + + val =strtoul (arg, &p,0 );switch (*p) {case 'k' :case 'K' :
@@ -393,10 +393,10 @@ static void handle_arg_reserved_va(const char *arg)break ; }if (shift) { -unsigned long unshifted = reserved_va; +unsigned long unshifted = val; p++; - reserved_va <<= shift; -if (reserved_va >> shift != unshifted) { + val <<= shift; +if (val >> shift != unshifted) {fprintf (stderr,"Reserved virtual address too big\n" );exit (EXIT_FAILURE ); }
@@ -405,6 +405,8 @@ static void handle_arg_reserved_va(const char *arg)fprintf (stderr,"Unrecognised -R size suffix '%s'\n" , p);exit (EXIT_FAILURE ); } +/* The representation is size - 1, with 0 remaining "default". */ + reserved_va = val ? val -1 :0 ; }static void handle_arg_singlestep (const char *arg)
@@ -793,7 +795,7 @@ int main(int argc, char **argv, char **envp)*/ max_reserved_va =MAX_RESERVED_VA (cpu);if (reserved_va !=0 ) { -if (reserved_va % qemu_host_page_size) { +if ((reserved_va +1 ) % qemu_host_page_size) {char *s =size_to_str (qemu_host_page_size);fprintf (stderr,"Reserved virtual address not aligned mod %s\n" , s);g_free (s);
@@ -804,11 +806,8 @@ int main(int argc, char **argv, char **envp)exit (EXIT_FAILURE ); } }else if (HOST_LONG_BITS ==64 &&TARGET_VIRT_ADDR_SPACE_BITS <=32 ) { -/* -* reserved_va must be aligned with the host page size -* as it is used with mmap() -*/ - reserved_va = max_reserved_va & qemu_host_page_mask; +/* MAX_RESERVED_VA + 1 is a large power of 2, so is aligned. */ + reserved_va = max_reserved_va; } {
linux-user/mmap.c +2 -2
@@ -283,7 +283,7 @@ static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size, end_addr = start + size;if (start > reserved_va - size) {/* Start at the top of the address space. */ - end_addr = ((reserved_va - size) & -align) + size; + end_addr = ((reserved_va +1 - size) & -align) + size; looped = true; }
@@ -297,7 +297,7 @@ static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size,return (abi_ulong)-1 ; }/* Re-start at the top of the address space. */ - addr = end_addr = ((reserved_va - size) & -align) + size; + addr = end_addr = ((reserved_va +1 - size) & -align) + size; looped = true; }else { prot =page_get_flags (addr);
Signature · 1 line
2.34.1
PATCH v2 12/12 linux-user/arm: Take more care allocating commpage
In reply to Richard HendersonUser setting of -R reserved_va can lead to an assertion failure in page_set_flags. Sanity check the value of reserved_va and print an error message instead. Do not allocate a commpage at all for m-profile cpus.
--- linux-user/elfload.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-)
linux-user/elfload.c +27 -10
@@ -423,12 +423,32 @@ enum {static bool init_guest_commpage (void ) { -abi_ptr commpage =HI_COMMPAGE & -qemu_host_page_size; -void *want =g2h_untagged (commpage); -void *addr =mmap (want, qemu_host_page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE |MAP_FIXED ,-1 ,0 ); +ARMCPU *cpu =ARM_CPU (thread_cpu); +abi_ptr want =HI_COMMPAGE &TARGET_PAGE_MASK ; +abi_ptr addr; -if (addr ==MAP_FAILED ) { +/* +* M-profile allocates maximum of 2GB address space, so can never +* allocate the commpage. Skip it. +*/ +if (arm_feature (&cpu->env,ARM_FEATURE_M )) { +return true; + } + +/* +* If reserved_va does not cover the commpage, we get an assert +* in page_set_flags. Produce an intelligent error instead. +*/ +if (reserved_va !=0 && want +TARGET_PAGE_SIZE -1 > reserved_va) { +error_report ("Allocating guest commpage: -R 0x%" PRIx64" too small" , + (uint64_t )reserved_va +1 ); +exit (EXIT_FAILURE ); + } + + addr =target_mmap (want,TARGET_PAGE_SIZE ,PROT_READ |PROT_WRITE , +MAP_ANONYMOUS |MAP_PRIVATE |MAP_FIXED ,-1 ,0 ); + +if (addr ==-1 ) {perror ("Allocating guest commpage" );exit (EXIT_FAILURE ); }
@@ -437,15 +457,12 @@ static bool init_guest_commpage(void) }/* Set kernel helper versions; rest of page is 0. */ -__put_user (5 , (uint32_t *)g2h_untagged (0xffff0ffcu )); +put_user_u32 (5 ,0xffff0ffcu ); -if (mprotect (addr, qemu_host_page_size,PROT_READ )) { +if (target_mprotect (addr, qemu_host_page_size,PROT_READ |PROT_EXEC )) {perror ("Protecting guest commpage" );exit (EXIT_FAILURE ); } - -page_set_flags (commpage, commpage | ~qemu_host_page_mask, -PAGE_READ |PAGE_EXEC |PAGE_VALID );return true; }
Signature · 1 line
2.34.1
Re: [PATCH for-8.0 v2 12/12] linux-user/arm: Take more care allocating commpage
Quoted 57 lines · Richard Henderson
User setting of -R
reserved_vacan lead to an assertion failure inpage_set_flags. Sanity check the value ofreserved_vaand print an error message instead. Do not allocate a commpage at all for m-profile cpus.Signed-off-by Richard Henderson <[email protected]>--- linux-user/elfload.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-)linux-user/elfload.c +25
-6@@ -423,12 +423,32 @@ enum {static bool init_guest_commpage (void ) { -abi_ptr commpage =HI_COMMPAGE & -qemu_host_page_size; -void *want =g2h_untagged (commpage); -void *addr =mmap (want, qemu_host_page_size,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE |MAP_FIXED ,-1 ,0 ); +ARMCPU *cpu =ARM_CPU (thread_cpu); +abi_ptr want =HI_COMMPAGE &TARGET_PAGE_MASK ; +abi_ptr addr; -if (addr ==MAP_FAILED ) { +/* +* M-profile allocates maximum of 2GB address space, so can never +* allocate the commpage. Skip it. +*/ +if (arm_feature (&cpu->env,ARM_FEATURE_M )) { +return true; + } + +/* +* If reserved_va does not cover the commpage, we get an assert +* in page_set_flags. Produce an intelligent error instead. +*/ +if (reserved_va !=0 && want +TARGET_PAGE_SIZE -1 > reserved_va) { +error_report ("Allocating guest commpage: -R 0x%" PRIx64" too small" , + (uint64_t )reserved_va +1 ); +exit (EXIT_FAILURE ); + } + + addr =target_mmap (want,TARGET_PAGE_SIZE ,PROT_READ |PROT_WRITE , +MAP_ANONYMOUS |MAP_PRIVATE |MAP_FIXED ,-1 ,0 ); + +if (addr ==-1 ) {perror ("Allocating guest commpage" );exit (EXIT_FAILURE ); }@@ -437,15 +457,12 @@ static bool init_guest_commpage(void) }/* Set kernel helper versions; rest of page is 0. */ -__put_user (5 , (uint32_t *)g2h_untagged (0xffff0ffcu ));
linux-user/elfload.c static bool init_guest_commpage(void) · line 448
Quoted 10 lines
linux-user/elfload.c +2
-4+put_user_u32 (5 ,0xffff0ffcu ); -if (mprotect (addr, qemu_host_page_size,PROT_READ )) { +if (target_mprotect (addr, qemu_host_page_size,PROT_READ |PROT_EXEC )) {perror ("Protecting guest commpage" );exit (EXIT_FAILURE ); } - -page_set_flags (commpage, commpage | ~qemu_host_page_mask, -PAGE_READ |PAGE_EXEC |PAGE_VALID );
Included in target_mprotect(PROT_EXEC), OK.
linux-user/elfload.c static bool init_guest_commpage(void) · line 468
Quoted 3 lines
linux-user/elfload.c +0
-0return true; }
LGTM. As a future cleanup, I'd rather see all init_guest_commpage() use the same API: either mmap/mprotect/page_set_flags or the target_XXX equivalent. The latter is preferred, since the logic is simplified.