qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stuart Brady <sdb@zubnet.me.uk>
To: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 5/8] tcg: Add interpreter for bytecode
Date: Mon, 19 Sep 2011 21:24:47 +0100	[thread overview]
Message-ID: <20110919202446.GA19005@zubnet.me.uk> (raw)
In-Reply-To: <1316289634-18786-5-git-send-email-weil@mail.berlios.de>

On Sat, Sep 17, 2011 at 10:00:31PM +0200, Stefan Weil wrote:

> +#if MAX_OPC_PARAM_IARGS != 4
> +# error Fix needed, number of supported input arguments changed!
> +#endif
> +#if TCG_TARGET_REG_BITS == 32
> +typedef uint64_t (*helper_function)(tcg_target_ulong, tcg_target_ulong,
> +                                    tcg_target_ulong, tcg_target_ulong,
> +                                    tcg_target_ulong, tcg_target_ulong,
> +                                    tcg_target_ulong, tcg_target_ulong);
> +#else
> +typedef uint64_t (*helper_function)(tcg_target_ulong, tcg_target_ulong,
> +                                    tcg_target_ulong, tcg_target_ulong);
> +#endif

[...]

> +        case INDEX_op_call:
> +            t0 = tci_read_ri(&tb_ptr);
> +#if TCG_TARGET_REG_BITS == 32
> +            u64 = ((helper_function)t0)(tci_read_reg(TCG_REG_R0),
> +                                        tci_read_reg(TCG_REG_R1),
> +                                        tci_read_reg(TCG_REG_R2),
> +                                        tci_read_reg(TCG_REG_R3),
> +                                        tci_read_reg(TCG_REG_R5),
> +                                        tci_read_reg(TCG_REG_R6),
> +                                        tci_read_reg(TCG_REG_R7),
> +                                        tci_read_reg(TCG_REG_R8));
> +            tci_write_reg(TCG_REG_R0, u64);
> +            tci_write_reg(TCG_REG_R1, u64 >> 32);
> +#else
> +            u64 = ((helper_function)t0)(tci_read_reg(TCG_REG_R0),
> +                                        tci_read_reg(TCG_REG_R1),
> +                                        tci_read_reg(TCG_REG_R2),
> +                                        tci_read_reg(TCG_REG_R3));
> +            tci_write_reg(TCG_REG_R0, u64);
> +#endif
> +            break;

Unfortunately, this won't work on all architectures.

C99 6.5.2.2 states:

   9. If the function is defined with a type that is not compatible with
      the type (of the expression) pointed to by the expression that
      denotes the called function, the behavior is undefined.

We could perhaps get away with this on certain architectures (and on
those architectures, doing it this way might be the most efficient
option), although I'm not sure which those architectures are.

The real problem is relates to alignment of parameters in registers
when passing 64-bit ints as arguments on 32-bit architectures.

Some ABIs have the situation where:

    void foo(uint32_t a, uint64_t b);

results in:

    register  contents
          p0  a
          p1  [padding]
          p2  b & 0xffffffff
          p3  b >> 32

An ABI may require this regardless of whether 64-bit integers are
typically aligned to 64-bit addresses in memory (or it may even do
this without such alignment for memory addresses).  The ordering of
the upper and lower 32-bits of a 64-bit parameter may have nothing at
all to do with the architecture's endianness.  The alignment rules
when passing arguments via registers might not even be consistent
with those when passing via the stack.

In QEMU, tcg_gen_callN() handles alignment of registers for the
architectures that currently have TCG backends.  If any new backend
were to require features not already supported by tcg_gen_callN(),
then those features would simply have to be added.

When using TCI, we could define TCG_TARGET_CALL_ALIGN_ARGS (and be
careful to handle the REGPARM case under x86), and simply rely on
tcg_gen_callN(), but this isn't guaranteed to work for all ABIs.

Since TCI is intended to be portable, I feel that we should provide
a means of calling helper functions that doesn't rely upon any
ABI-specific definitions, at least as a fallback.  It would probably
make sense to get the generic code working first, and then think about
optimising for specific ABIs later, IMO.

So, this leaves the question of how to do this in a generic manner.
Do we:

 1) Include a pointer to a wrapper function in the bytecode, which
    would call the helper with the correct type.  Each wrapper could
    just read from and write to the TCI registers itself without
    accepting/returning values, or the values of the TCI registers
    could be passed in as arguments to each of the wrapper functions.

 2) Encode the type of the function into the bytecode, such that a
    huge switch() statement can be used to cast the function pointer
    to the appropriate type, allowing the helper to be invoked in a
    defined manner.  My guess is that this would be slower when
    executing bytecode than 1), although it would be quicker for
    compilation of the bytecode.

 3) Modify the helpers themselves to accept uint32_t arguments when
    using TCI.  This would require quite a lot of work but would
    likely yield the best performance.  However, it would prevent
    us from ever being able to choose between architecture-specific
    backends and TCI using a command line option.

 4) Go with some other option that I've not considered?

To me, option 1) seems like the simplest, although the macros needed
to do this are likely to be a little hairy...

I'm also concerned that we should not clobber R1 when storing a
32-bit return value in R0 on 32-bit architectures.

Cheers,
-- 
Stuart Brady

  parent reply	other threads:[~2011-09-19 21:44 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-17 19:59 [Qemu-devel] [PATCH 0/8] tcg/interpreter: Add TCG + interpreter for bytecode (virtual machine) Stefan Weil
2011-09-17 20:00 ` [Qemu-devel] [PATCH 1/8] tcg: Declare TCG_TARGET_REG_BITS in tcg.h Stefan Weil
2011-09-17 20:00 ` [Qemu-devel] [PATCH 2/8] tcg: Don't declare TCG_TARGET_REG_BITS in tcg-target.h Stefan Weil
2011-09-17 20:00 ` [Qemu-devel] [PATCH 3/8] tcg: Add forward declarations for local functions Stefan Weil
2011-09-17 21:40   ` Peter Maydell
2011-09-17 20:00 ` [Qemu-devel] [PATCH 4/8] tcg: Add some assertions Stefan Weil
2011-09-17 20:00 ` [Qemu-devel] [PATCH 5/8] tcg: Add interpreter for bytecode Stefan Weil
2011-09-18  4:03   ` Andi Kleen
2011-09-18  5:49     ` Stefan Weil
2011-09-18  7:22       ` Paolo Bonzini
2011-09-18 17:54         ` Avi Kivity
2011-09-19  6:52           ` Andi Kleen
2011-09-19 11:56             ` Avi Kivity
2011-09-19 14:48               ` Andi Kleen
2011-09-18 10:18   ` Blue Swirl
2011-09-19 16:43   ` Richard Henderson
2011-09-19 20:24   ` Stuart Brady [this message]
2011-10-16 21:54     ` Stuart Brady
2011-09-17 20:00 ` [Qemu-devel] [PATCH 6/8] tcg: Add bytecode generator for tcg interpreter Stefan Weil
2011-09-18 10:03   ` Blue Swirl
2011-09-19 22:28     ` Stuart Brady
2011-10-01 16:54   ` Andreas Färber
2011-10-01 21:25     ` Stefan Weil
2011-10-09 16:19       ` Andreas Färber
2011-09-17 20:00 ` [Qemu-devel] [PATCH 7/8] tcg: Add tcg interpreter to configure / make Stefan Weil
2011-09-18  9:37   ` Blue Swirl
2011-09-18 10:14     ` Stefan Weil
2011-09-17 20:00 ` [Qemu-devel] [PATCH 8/8] ppc: Support tcg interpreter on ppc hosts Stefan Weil
2011-09-17 21:31   ` Peter Maydell
2011-09-17 21:33     ` Stefan Weil
2011-09-18 10:26 ` [Qemu-devel] [PATCH 0/8] tcg/interpreter: Add TCG + interpreter for bytecode (virtual machine) Blue Swirl
2011-09-18 10:49   ` malc
2011-09-18 12:12     ` Blue Swirl
2011-09-18 12:46       ` malc
2011-09-18 13:00         ` Blue Swirl
2011-09-18 13:13           ` malc
2011-09-18 13:26             ` Blue Swirl
2011-09-25 20:37           ` Stefan Weil
2011-10-01 12:02             ` Blue Swirl
2011-09-18 15:02 ` Mulyadi Santosa
2011-09-18 15:13   ` Stefan Weil
2011-09-18 16:39     ` Mulyadi Santosa
2011-09-18 20:15       ` Stefan Weil
2011-09-19 15:14         ` Mulyadi Santosa
2011-09-19  8:40     ` David Gilbert
2011-09-19 10:20       ` Stefan Hajnoczi
2011-09-19 10:27         ` David Gilbert
2011-09-18 18:02 ` Avi Kivity

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=20110919202446.GA19005@zubnet.me.uk \
    --to=sdb@zubnet.me.uk \
    --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).