Skip to contentSkip to conversation

qemu qemu-doc.texi

Qemu-devel Paul Brook 3 participants 6 messages Jun 11, 2006 – Jun 19, 2006

lore
Share & tools
lore

Paul Brook

author
#0 lore raw

CVSROOT: /sources/qemu
Module name: qemu
Changes by: Paul Brook <pbrook> 06/06/11 16:28:41

Modified files:
. : qemu-doc.texi

Log message:
Document new arm-user features.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/qemu/qemu-doc.texi?cvsroot=qemu&r1=1.90&r2=1.91

Patches:
Index: qemu-doc.texi
===================================================================
RCS file: /sources/qemu/qemu/qemu-doc.texi,v
retrieving revision 1.90
retrieving revision 1.91

qemu-doc.texi +10 -0
diff -u -b -r1.90 -r1.91
@@ -1531,6 +1531,7 @@
 * Quick Start::
 * Wine launch::
 * Command line options::
+* Other binaries::
 @end menu
 
 @node Quick Start
@@ -1637,6 +1638,15 @@
 Act as if the host page size was 'pagesize' bytes
 @end table
 
+@node Other binaries
+@section Other binaries
+
+@command{qemu-arm} is also capable of running ARM "Angel" semihosted ELF
+binaries (as implemented by the arm-elf and arm-eabi Newlib/GDB
+configurations), and arm-uclinux bFLT format binaries.
+
+The binary format is detected automatically.
+
 @node compilation
 @chapter Compilation from the sources
 
#1 lore raw

Hi Paul,

Does this mean that qemu-arm should be able to run the binaries that are produced by RVCT? I am trying to run a simple helloworld, compiled and linked with rvct2.2 (armcc -g -o hello hello.c) into a "ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped" file, but qemu-arm fails with an "error loading ./hello".

On a related note, how do I go about debugging qemu (namely the part that loads executables)? Since qemu appears as a shared object, this is not so straightforward. When I start gdb on arm-user, set a breakpoint at main() and type "run", I get

"cannot insert breakpoint 1.
Error accessing memory address 0x1cfb2: Input/output error. The same program may be running in another process."

(This is with gdb 5.2, but 6.3 is not much different)

- Wolfgang

qemu-devel-bounces+wolfgang.schildbach=codingtechnologies.com@nongnu.org wrote on 11.06.2006 18:28:41:

Quoted 32 lines
CVSROOT:   /sources/qemu
Module name:   qemu
Changes by:   Paul Brook <pbrook>   06/06/11 16:28:41

Modified files:
. : qemu-doc.texi

Log message:
Document new arm-user features.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/qemu/qemu-doc.texi?
cvsroot=qemu&r1=1.90&r2=1.91

Patches:
Index: qemu-doc.texi
===================================================================
RCS file: /sources/qemu/qemu/qemu-doc.texi,v
retrieving revision 1.90
retrieving revision 1.91
diff -u -b -r1.90 -r1.91

qemu-doc.texi 11 Jun 2006 16:28:41 -0000 1.91 renamed from qemu-doc.texi 26 May 2006 00:49:52 -0000 1.90 +1 -0
@@ -1531,6 +1531,7 @@
 * Quick Start::
 * Wine launch::
 * Command line options::
+* Other binaries::
 @end menu
 
 @node Quick Start

qemu-doc.texi 11 Jun 2006 16:28:41 -0000 1.91 line 1644

Quoted 7 lines
qemu-doc.texi 11 Jun 2006 16:28:41 -0000 1.91 renamed from qemu-doc.texi 26 May 2006 00:49:52 -0000 1.90 +4 -0
@@ -1637,6 +1638,15 @@
 Act as if the host page size was 'pagesize' bytes
 @end table
 
+@node Other binaries
+@section Other binaries
+
+@command{qemu-arm} is also capable of running ARM "Angel" semihosted 

ELF

Quoted 10 lines
qemu-doc.texi 11 Jun 2006 16:28:41 -0000 1.91 renamed from qemu-doc.texi 26 May 2006 00:49:52 -0000 1.90 +5 -0
+binaries (as implemented by the arm-elf and arm-eabi Newlib/GDB
+configurations), and arm-uclinux bFLT format binaries.
+
+The binary format is detected automatically.
+
 @node compilation
 @chapter Compilation from the sources
 
Quoted 4 lines

_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel

--
Wolfgang Schildbach, Senior Research Engineer
Coding Technologies GmbH

#2 lore raw
Quoted 7 lines · Wolfgang Schildbach

Hi Paul,

Does this mean that qemu-arm should be able to run the binaries that are produced by RVCT? I am trying to run a simple helloworld, compiled and linked with rvct2.2 (armcc -g -o hello hello.c) into a "ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped" file, but qemu-arm fails with an "error loading ./hello".

Yes it should be able to run RVCT binaries. The problem is that the qemu loader assumes the LMA and file offsets be page aligned (ie. be the same modulo the page size). This is part of the ABI for linux executables so that the binary can be mmapped directly into memory. RVCT does not enforce this alignment by default.

Quoted 4 lines

On a related note, how do I go about debugging qemu (namely the part that loads executables)? Since qemu appears as a shared object, this is not so straightforward. When I start gdb on arm-user, set a breakpoint at main() and type "run", I get

Configure with --static.

Paul

#3 lore raw

Thanks much Paul -- that did the trick! (Well, almost.)

For the benefit of other ADS/RVCT users that try this, here is what I did:

- figured out the file offset of the executable region in the axf file (readelf -l foo.axf|grep LOAD).
For me, the offset appears to always be 0x34, and the executable region
starts at 0x8000.
- linked the application with armlink --ro-base 0x8034. This will move the executable region to a new start which is page-aligned to the actual file offset.
- finally (this took me a little to figure out), I made sure that the executable has execute permissions: chmod u+rx foo.axf.

With these changes, I could execute ARM/RealView Developer Suite generated executables directly. This is excellent -- thanks much to all who contributed to make this possible!

The one remaining issue is that command line parameters are not properly transfered over to the ARM executable. This surprises me a little, since this does work for arm-userspace-linux, and the mechanisms loading the image appear quite similar.

Where would I (start to) look for the reasons behind this? Is this something that needs to be "fixed" on the ARM side (i.e. fix the location where the ARM code looks for the environment)?

- Wolfgang

Paul Brook <[email protected]> wrote on 12.06.2006 16:18:34:

Quoted 4 lines · Wolfgang Schildbach
Quoted 3 lines

Hi Paul,

Does this mean that qemu-arm should be able to run the binaries that

are

Quoted 3 lines
Quoted 3 lines

produced by RVCT? I am trying to run a simple helloworld, compiled and linked with rvct2.2 (armcc -g -o hello hello.c) into a "ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped"

file,

Quoted 7 lines
Quoted 1 line

but qemu-arm fails with an "error loading ./hello".

Yes it should be able to run RVCT binaries. The problem is that the qemu

loader assumes the LMA and file offsets be page aligned (ie. be the same

modulo the page size). This is part of the ABI for linux executables so

that

Quoted 1 line

the binary can be mmapped directly into memory. RVCT does not enforce

this

Quoted 3 lines

alignment by default.

Quoted 1 line

On a related note, how do I go about debugging qemu (namely the part

that

Quoted 1 line
Quoted 1 line

loads executables)? Since qemu appears as a shared object, this is not

so

Quoted 1 line
Quoted 1 line

straightforward. When I start gdb on arm-user, set a breakpoint at

main()

Quoted 5 lines
Quoted 1 line

and type "run", I get

Configure with --static.

Paul

--
Wolfgang Schildbach, Senior Research Engineer
Coding Technologies GmbH

#4 lore raw
Quoted 3 lines

Where would I (start to) look for the reasons behind this? Is this something that needs to be "fixed" on the ARM side (i.e. fix the location where the ARM code looks for the environment)?

Look at the code in load_elf_binary that uses target_mmap to map the loadable segments into memory. The page size I'm referring to below is the target page size (4k for qemu-arm). target_mmap is more-or-less a wrapper around normal mmap that deals with the corner cases and differences in page size when host != target.

There are two issues:
- mmap requires the file offset be a multiple of the page size. This is relatively easy to fix. If the file data is misaligned create an anonymous mapping and pread the data.

- The code assumes the VMA of the segments after roundind to a page boundary do not overlap. ie. a single memory page will contain data from no more than one segment. Fixing this is more complicated and probably involves merging the regions used by sections with "overlapping" pages.

Paul

#5 lore raw

Hi Paul,

Thanks for the explanation. I feel like fixing the alignment issue in the qemu code is a little above my head right now. However, it turns out that the alignment issue can be solved on the arm compiler/linker side, by giving appropriate arguments to the linker (--ro-base 0x8034). The "missing command line issue" can be attributed to the fact that the corresponding ARM semihosting function was not implemented.

The patch below implements the SYS_GET_CMDLINE semihosting call, by keeping a global pointer to the user space commandline arguments, and by re-building a space-separated command line in the guest side supplied buffer. This patch has two shortcomings:

- I am keeping a global pointer in order to have a handle on the command line arguments inside do_arm_semihosting. Is there a better place to keep this?
- The ARM (guest) side supplied buffer is only 256 bytes long; if the user supplied command line is larger, the semihosting call will fail (gracefully). A better strategy may be to build the string on the initial guest stack (similar to loader_build_argptgr()) and then hand that pointer to the guest.

I am not quite sure whether the ARGS[] array should be locked before access (then again, none of the functions in do_arm_semihosting() do this).

Anyhow, with the patch below (and the linker flags above), I can successfully execute ADS/RVCT compiled binaries, together with command line arguments, both little- and bigendian.

All comments welcome.

- Wolfgang

Paul Brook <[email protected]> wrote on 12.06.2006 18:47:12:

Quoted 2 lines
Quoted 2 lines

Where would I (start to) look for the reasons behind this? Is this something that needs to be "fixed" on the ARM side (i.e. fix the

location

Quoted 3 lines
Quoted 1 line

where the ARM code looks for the environment)?

Look at the code in load_elf_binary that uses target_mmap to map

theloadable

Quoted 1 line

segments into memory. The page size I'm referring to below is the target

page

Quoted 1 line

size (4k for qemu-arm). target_mmap is more-or-less a wrapper around

normal

Quoted 7 lines

mmap that deals with the corner cases and differences in page size when host != target.

There are two issues:
- mmap requires the file offset be a multiple of the page size. This is

relatively easy to fix. If the file data is misaligned create an

anonymous

Quoted 3 lines

mapping and pread the data.

- The code assumes the VMA of the segments after roundind to a page

boundary

Quoted 1 line

do not overlap. ie. a single memory page will contain data from no more

than

Quoted 1 line

one segment. Fixing this is more complicated and probably involves

merging

Quoted 3 lines

the regions used by sections with "overlapping" pages.

Paul

--
Wolfgang Schildbach, Senior Research Engineer
Coding Technologies GmbH

Index: linux-user/arm-semi.c
===================================================================
RCS file: /sources/qemu/qemu/linux-user/arm-semi.c,v retrieving revision 1.5
diff -r1.5 arm-semi.c
80c80,81
< #define ARG(n) tget32(args + n * 4)
---

Quoted 2 lines

#define ARG(n) tget32(args + (n) * 4)
#define SET_ARG(n,val) tput32(args + (n) * 4,val)

161,164c162,201

<         /* XXX: Not implemented.  */
<         s = (char *)g2h(ARG(0));
<         *s = 0;
<         return -1;

---

Quoted 32 lines
        /* The ARM semihosting interface requires that the commandline is
           presented with blanks separating the arguments. Thus, we need
           to build a new command line, given the global pointer to the
           command line we received.
           A better way would be to build this command line on the user stack,
           similar to the way it is done in loader_build_argptr(), and then
           just hand that pointer back to the caller */
        {
            extern char **global_userspace_argv ; /* initialized in main() */
            char **av = global_userspace_argv ;   /* work ptr */
            int len = ARG(1); /* amount of RAM that the ARM binary has set
                                 aside for the command line */
            /* lock the buffer on the ARM side */
            char *cmdline_buffer = (char*)lock_user(ARG(0),len,0);
            s = cmdline_buffer ;
            do {
                int n = strlen(*av) ;

                /* is there still space in the supplied buffer, including
                   the terminating zero? */
                if (s - cmdline_buffer + n+1 > len)
                    break ; /* no */

                memcpy(s,*av,n);
                s += n ;
                *s++ = ' ';
                len -= n+1 ;
                av++;
            } while (*av);
            *s++ = 0; /* terminate cmdline string */

            /* unlock the buffer on the ARM side */
Quoted 8 lines
            unlock_user(cmdline_buffer, ARG(0), ARG(1));

            /* adjust the commandline length argument */
            SET_ARG(1, (uint32_t)(s - cmdline_buffer));

            /* successfull return if commandline fit into buffer */
            return *av == 0 ? 0 : -1 ;
        }

Index: linux-user/main.c
===================================================================
RCS file: /sources/qemu/qemu/linux-user/main.c,v
retrieving revision 1.88
diff -r1.88 main.c
312a313,315

Quoted 3 lines

/* XXX: this is an ugly hack, to make argc/argv available to ARM semihosting */ char **global_userspace_argv ;

1531,1532c1534,1536
<
< if (loader_exec(filename, argv+optind, environ, regs, info) != 0) {
---

Quoted 3 lines
    global_userspace_argv = argv + optind;

    if ((loader_exec(filename, global_userspace_argv, environ, regs, info)) != 0) {