qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Alex Bennée" <1880225@bugs.launchpad.net>
To: qemu-devel@nongnu.org
Subject: [Bug 1880225] [PATCH v1 2/3] linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit
Date: Wed, 27 May 2020 10:05:45 -0000	[thread overview]
Message-ID: <20200527100546.29297-3-alex.bennee@linaro.org> (raw)
Message-ID: <20200527100545.02ru6ePQBJsGZJRf8isbSIUqbrl6fO4IqCoNqpj5bVE@z> (raw)
In-Reply-To: 159017301531.7966.9120113243897778171.malonedeb@gac.canonical.com

We rely on the pointer to wrap when accessing the high address of the
COMMPAGE so it lands somewhere reasonable. However on 32 bit hosts we
cannot afford just to map the entire 4gb address range. The old mmap
trial and error code handled this by just checking we could map both
the guest_base and the computed COMMPAGE address.

We can't just manipulate loadaddr to get what we want so we introduce
an offset which pgb_find_hole can apply when looking for a gap for
guest_base that ensures there is space left to map the COMMPAGE
afterwards.

This is arguably a little inefficient for the one 32 bit
value (kuser_helper_version) we need to keep there given all the
actual code entries are picked up during the translation phase.

Fixes: ee94743034b
Bug: https://bugs.launchpad.net/qemu/+bug/1880225
Cc: Bug 1880225 <1880225@bugs.launchpad.net>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Cc: Richard Henderson <richard.henderson@linaro.org>
Cc: Peter Maydell <peter.maydell@linaro.org>
---
 linux-user/elfload.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index d6027867a1a..31defce95b5 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -2145,7 +2145,7 @@ static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk, lon
 
 /* Return value for guest_base, or -1 if no hole found. */
 static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
-                               long align)
+                               long align, uintptr_t offset)
 {
     GSList *maps, *iter;
     uintptr_t this_start, this_end, next_start, brk;
@@ -2171,7 +2171,7 @@ static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
 
         this_end = ((MapInfo *)iter->data)->start;
         next_start = ((MapInfo *)iter->data)->end;
-        align_start = ROUND_UP(this_start, align);
+        align_start = ROUND_UP(this_start + offset, align);
 
         /* Skip holes that are too small. */
         if (align_start >= this_end) {
@@ -2221,6 +2221,7 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr,
 {
     uintptr_t loaddr = orig_loaddr;
     uintptr_t hiaddr = orig_hiaddr;
+    uintptr_t offset = 0;
     uintptr_t addr;
 
     if (hiaddr != orig_hiaddr) {
@@ -2234,18 +2235,19 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr,
     if (ARM_COMMPAGE) {
         /*
          * Extend the allocation to include the commpage.
-         * For a 64-bit host, this is just 4GiB; for a 32-bit host,
-         * the address arithmetic will wrap around, but the difference
-         * will produce the correct allocation size.
+         * For a 64-bit host, this is just 4GiB; for a 32-bit host we
+         * need to ensure there is space bellow the guest_base so we
+         * can map the commpage in the place needed when the address
+         * arithmetic wraps around.
          */
         if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) {
             hiaddr = (uintptr_t)4 << 30;
         } else {
-            loaddr = ARM_COMMPAGE & -align;
+            offset = (128 * KiB);
         }
     }
 
-    addr = pgb_find_hole(loaddr, hiaddr - loaddr, align);
+    addr = pgb_find_hole(loaddr, hiaddr - loaddr, align, offset);
     if (addr == -1) {
         /*
          * If ARM_COMMPAGE, there *might* be a non-consecutive allocation
@@ -2280,7 +2282,7 @@ static void pgb_dynamic(const char *image_name, long align)
          * just above that, and maximises the positive guest addresses.
          */
         commpage = ARM_COMMPAGE & -align;
-        addr = pgb_find_hole(commpage, -commpage, align);
+        addr = pgb_find_hole(commpage, -commpage, align, 0);
         assert(addr != -1);
         guest_base = addr;
     }
-- 
2.20.1


** Changed in: qemu
       Status: Confirmed => In Progress

-- 
You received this bug notification because you are a member of qemu-
devel-ml, which is subscribed to QEMU.
https://bugs.launchpad.net/bugs/1880225

Title:
  Emulation of some arm programs fail with "Assertion `have_guest_base'
  failed."

Status in QEMU:
  In Progress

Bug description:
  This issue is observer with QEMU ToT, checked out around May 15th (but
  I believe it is present in current master too), and wasn't present in
  QEMU v5.0.0.

  I am using 32-bit Intel(R) Pentium(R) M processor 1.73GHz host.

  Arm cross-compiler is a standard cross-compiler that comes with
  Debian-based distributions, and gcc version is:

  $ arm-linux-gnueabi-gcc --version
  arm-linux-gnueabi-gcc (Debian 8.3.0-2) 8.3.0

  Compile this program with cross compiler:

  $ arm-linux-gnueabi-gcc -O2 -static toupper_string.c -o
  toupper_string-arm

  Emulation with QEMU v5.0.0 is correct, and gives expected output:

  $ ~/Build/qemu-5.0.0/build-gcc/arm-linux-user/qemu-arm ./toupper_string-arm
  CONTROL RESULT: (toupper_string)
   nwlrbbmqbhcdarz owkkyhiddqscdxr jmowfrxsjybldbe fsarcbynecdyggx xpklorellnmpapq
   NWLRBBMQBHCDARZ OWKKYHIDDQSCDXR JMOWFRXSJYBLDBE FSARCBYNECDYGGX XPKLORELLNMPAPQ

  While, in case of QEMU master it fails:

  $ ~/Build/qemu-master/build-gcc/arm-linux-user/qemu-arm ./toupper_string-arm
  qemu-arm: /home/rtrk/Build/qemu-master/linux-user/elfload.c:2294: probe_guest_base: Assertion `have_guest_base' failed.
  Aborted

  There are many other programs that exibit the same behavior. The
  failure is arm-sprecific.

  
  -----------------------------------------------------

  source code: (let's call this file toupper_string.c) (similar file is
  also in attachment)

  
  #include <stdlib.h>
  #include <string.h>
  #include <stdio.h>
  #include <unistd.h>

  
  #define MAX_STRING_LENGHT              15
  #define NUMBER_OF_RANDOM_STRINGS       100
  #define DEFAULT_NUMBER_OF_REPETITIONS  30000
  #define MAX_NUMBER_OF_REPETITIONS      1000000000
  #define NUMBER_OF_CONTROL_PRINT_ITEMS  5

  /* Structure for keeping an array of strings */
  struct StringStruct {
      char chars[MAX_STRING_LENGHT + 1];
  };

  /**
   * Sets characters of the given string to random small letters a-z.
   * @param s String to get random characters.
   * @len Length of the input string.
   */
  static void gen_random_string(char *chars, const int len)
  {
      static const char letters[] = "abcdefghijklmnopqrstuvwxyz";

      for (size_t i = 0; i < len; i++) {
          chars[i] = letters[rand() % (sizeof(letters) - 1)];
      }
      chars[len] = 0;
  }

  void main (int argc, char* argv[])
  {
      struct StringStruct random_strings[NUMBER_OF_RANDOM_STRINGS];
      struct StringStruct strings_to_be_uppercased[NUMBER_OF_RANDOM_STRINGS];
      int32_t number_of_repetitions = DEFAULT_NUMBER_OF_REPETITIONS;
      int32_t option;

      /* Parse command line options */
      while ((option = getopt(argc, argv, "n:")) != -1) {
          if (option == 'n') {
              int32_t user_number_of_repetitions = atoi(optarg);
              /* Check if the value is a negative number */
              if (user_number_of_repetitions < 1) {
                  fprintf(stderr, "Error ... Value for option '-n' cannot be a "
                                  "negative number.\n");
                  exit(EXIT_FAILURE);
              }
              /* Check if the value is a string or zero */
              if (user_number_of_repetitions == 0) {
                  fprintf(stderr, "Error ... Invalid value for option '-n'.\n");
                  exit(EXIT_FAILURE);
              }
              /* Check if the value is too large */
              if (user_number_of_repetitions > MAX_NUMBER_OF_REPETITIONS) {
                  fprintf(stderr, "Error ... Value for option '-n' cannot be "
                                  "more than %d.\n", MAX_NUMBER_OF_REPETITIONS);
                  exit(EXIT_FAILURE);
              }
              number_of_repetitions = user_number_of_repetitions;
          } else {
              exit(EXIT_FAILURE);
          }
      }

      /* Create an array of strings with random content */
      srand(1);
      for (size_t i = 0; i < NUMBER_OF_RANDOM_STRINGS; i++) {
          gen_random_string(random_strings[i].chars, MAX_STRING_LENGHT);
      }

      /* Perform uppercasing of a set of random strings multiple times */
      for (size_t j = 0; j < number_of_repetitions; j++) {
          /* Copy initial set of random strings to the set to be uppercased */
          memcpy(strings_to_be_uppercased, random_strings,
                 NUMBER_OF_RANDOM_STRINGS * (MAX_STRING_LENGHT + 1));
          /* Do actual changing case to uppercase */
          for (size_t i = 0; i < NUMBER_OF_RANDOM_STRINGS; i++) {
              int k = 0;
    
              while (strings_to_be_uppercased[i].chars[k]) { 
                  char ch = strings_to_be_uppercased[i].chars[k] - 32; 
                  memcpy((void *)strings_to_be_uppercased[i].chars + k,
                         &ch, 1);
                  k++; 
              } 
          }
      }

      /* Control printing */
      printf("CONTROL RESULT: (toupper_string)\n");
      for (size_t i = 0; i < NUMBER_OF_CONTROL_PRINT_ITEMS; i++) {
          printf(" %s", random_strings[i].chars);
      }
      printf("\n");
      for (size_t i = 0; i < NUMBER_OF_CONTROL_PRINT_ITEMS; i++) {
          printf(" %s", strings_to_be_uppercased[i].chars);
      }
      printf("\n");
  }

To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1880225/+subscriptions


  parent reply	other threads:[~2020-05-27 10:16 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-27 10:05 [PATCH v1 0/3] some linux-user guest_base fixes Alex Bennée
2020-05-27 10:05 ` [PATCH v1 1/3] linux-user: provide fallback pgd_find_hole for bare chroots Alex Bennée
2020-06-02  0:37   ` Richard Henderson
2020-05-27 10:05 ` Alex Bennée [this message]
2020-05-27 10:05   ` [Bug 1880225] [PATCH v1 2/3] linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit Alex Bennée
2020-05-27 12:05   ` Aleksandar Markovic
2020-05-27 12:05     ` [Bug 1880225] " Aleksandar Markovic
2020-05-27 14:47     ` Aleksandar Markovic
2020-05-27 14:47       ` [Bug 1880225] " Aleksandar Markovic
2020-05-27 16:14       ` Alex Bennée
2020-05-27 16:14         ` [Bug 1880225] " Alex Bennée
2020-06-02  0:28   ` Richard Henderson
2020-06-05  9:45     ` Alex Bennée
2020-06-05  9:45       ` [Bug 1880225] " Alex Bennée
2020-06-05 10:24       ` Alex Bennée
2020-06-05 10:24         ` [Bug 1880225] " Alex Bennée
2020-05-27 10:05 ` [PATCH v1 3/3] tests/tcg: add simple commpage test case Alex Bennée
2020-06-02  0:40   ` Richard Henderson
2020-05-27 14:12 ` [PATCH v1 0/3] some linux-user guest_base fixes no-reply
  -- strict thread matches above, loose matches on Subject: below --
2020-06-09 10:37 [PULL 00/17] testing and misc fixes Alex Bennée
2020-06-09 10:37 ` [PULL 01/17] qemu-plugin.h: add missing include <stddef.h> to define size_t Alex Bennée
2020-06-09 10:37 ` [PULL 02/17] scripts/clean-includes: Mark 'qemu/qemu-plugin.h' as special header Alex Bennée
2020-06-09 10:37 ` [PULL 03/17] tests/plugin: correctly honour io_count Alex Bennée
2020-06-09 10:37 ` [PULL 04/17] exec: flush the whole TLB if a watchpoint crosses a page boundary Alex Bennée
2020-06-09 10:37 ` [PULL 05/17] .travis.yml: allow failure for unreliable hosts Alex Bennée
2020-06-09 10:37 ` [PULL 06/17] .shippable: temporaily disable some cross builds Alex Bennée
2020-06-09 10:37 ` [PULL 07/17] tests/docker: fix pre-requisite for debian-tricore-cross Alex Bennée
2020-06-09 10:38 ` [PULL 08/17] docker: update Ubuntu to 20.04 Alex Bennée
2020-06-09 10:38 ` [PULL 09/17] hw/virtio/vhost: re-factor vhost-section and allow DIRTY_MEMORY_CODE Alex Bennée
2020-06-09 10:38 ` [PULL 10/17] linux-user: provide fallback pgd_find_hole for bare chroots Alex Bennée
2020-06-09 10:38 ` [PULL 11/17] linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit Alex Bennée
2020-06-09 10:38   ` [Bug 1880225] " Alex Bennée
2020-06-09 10:38 ` [PULL 12/17] tests/tcg: add simple commpage test case Alex Bennée
2020-06-09 10:38 ` [PULL 13/17] linux-user: detect overflow of MAP_FIXED mmap Alex Bennée
2020-06-09 10:38 ` [PULL 14/17] tests/docker: Remove flex/bison packages Alex Bennée
2020-06-09 10:38 ` [PULL 15/17] tests/vm: " Alex Bennée
2020-06-09 10:53   ` Claudio Fontana
2020-06-09 11:08     ` Alex Bennée
2020-06-09 10:38 ` [PULL 16/17] cirrus-ci: " Alex Bennée
2020-06-09 10:38 ` [PULL 17/17] scripts/coverity-scan: " Alex Bennée
2020-06-11 10:20 ` [PULL 00/17] testing and misc fixes Peter Maydell
2020-06-05 15:49 [PATCH v1 00/14] various fixes for next PR (testing, vhost, guest_base fixes) Alex Bennée
2020-06-05 15:49 ` [PATCH v1 01/14] qemu-plugin.h: add missing include <stddef.h> to define size_t Alex Bennée
2020-06-05 15:49 ` [PATCH v1 02/14] scripts/clean-includes: Mark 'qemu/qemu-plugin.h' as special header Alex Bennée
2020-06-05 15:49 ` [PATCH v1 03/14] tests/plugin: correctly honour io_count Alex Bennée
2020-06-05 15:49 ` [PATCH v1 04/14] exec: flush the whole TLB if a watchpoint crosses a page boundary Alex Bennée
2020-06-05 15:49 ` [PATCH v1 05/14] .travis.yml: allow failure for unreliable hosts Alex Bennée
2020-06-05 15:49 ` [PATCH v1 06/14] .shippable: temporaily disable some cross builds Alex Bennée
2020-06-05 16:12   ` Philippe Mathieu-Daudé
2020-06-05 15:49 ` [PATCH v1 07/14] iotests: 194: wait migration completion on target too Alex Bennée
2020-06-05 15:49 ` [PATCH v1 08/14] tests/docker: fix pre-requisite for debian-tricore-cross Alex Bennée
2020-06-05 15:49 ` [PATCH v1 09/14] docker: update Ubuntu to 20.04 Alex Bennée
2020-06-05 15:49 ` [PATCH v1 10/14] hw/virtio/vhost: re-factor vhost-section and allow DIRTY_MEMORY_CODE Alex Bennée
2020-06-05 15:49 ` [PATCH v1 11/14] linux-user: provide fallback pgd_find_hole for bare chroots Alex Bennée
2020-06-05 15:49 ` [PATCH v1 12/14] linux-user: deal with address wrap for ARM_COMMPAGE on 32 bit Alex Bennée
2020-06-05 15:49   ` [Bug 1880225] " Alex Bennée
2020-06-05 15:49 ` [PATCH v1 13/14] tests/tcg: add simple commpage test case Alex Bennée
2020-06-05 15:49 ` [PATCH v1 14/14] linux-user: detect overflow of MAP_FIXED mmap Alex Bennée
2020-06-05 16:16   ` Philippe Mathieu-Daudé
2020-06-05 15:54 ` [PATCH v1 00/14] various fixes for next PR (testing, vhost, guest_base fixes) Eric Blake
2020-06-05 17:46 ` no-reply
2020-06-07  6:55 ` Thomas Huth
2020-06-08 15:58   ` Alex Bennée
2020-05-22 18:43 [Bug 1880225] [NEW] Emulation of some arm programs fail with "Assertion `have_guest_base' failed." Aleksandar Markovic
2020-05-22 19:18 ` Alex Bennée
2020-05-22 19:18   ` Alex Bennée
2020-05-22 19:27 ` [Bug 1880225] " Alex Bennée
2020-05-23  1:07 ` Aleksandar Markovic
2020-05-23  1:14 ` Aleksandar Markovic
2020-05-23  7:40   ` Alex Bennée
2020-05-23  7:40     ` Alex Bennée
2020-05-23  1:31 ` Aleksandar Markovic
2020-05-23  7:50 ` Alex Bennée
2020-05-23  7:52 ` Aleksandar Markovic
2020-05-23 10:14   ` Alex Bennée
2020-08-20 15:08 ` Thomas Huth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200527100546.29297-3-alex.bennee@linaro.org \
    --to=1880225@bugs.launchpad.net \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).