All of lore.kernel.org
 help / color / mirror / Atom feed
From: Taylor Simpson <tsimpson@quicinc.com>
To: qemu-devel@nongnu.org
Cc: ale@rev.ng, bcain@quicinc.com, philmd@redhat.com,
	richard.henderson@linaro.org, laurent@vivier.eu,
	tsimpson@quicinc.com, alex.bennee@linaro.org
Subject: [PATCH v8 02/35] Hexagon (target/hexagon) README
Date: Sun,  7 Feb 2021 23:45:52 -0600	[thread overview]
Message-ID: <1612763186-18161-3-git-send-email-tsimpson@quicinc.com> (raw)
In-Reply-To: <1612763186-18161-1-git-send-email-tsimpson@quicinc.com>

Gives an introduction and overview to the Hexagon target

Signed-off-by: Taylor Simpson <tsimpson@quicinc.com>
---
 target/hexagon/README | 235 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 235 insertions(+)
 create mode 100644 target/hexagon/README

diff --git a/target/hexagon/README b/target/hexagon/README
new file mode 100644
index 0000000..b0b2435
--- /dev/null
+++ b/target/hexagon/README
@@ -0,0 +1,235 @@
+Hexagon is Qualcomm's very long instruction word (VLIW) digital signal
+processor(DSP).
+
+The following versions of the Hexagon core are supported
+    Scalar core: v67
+    https://developer.qualcomm.com/downloads/qualcomm-hexagon-v67-programmer-s-reference-manual
+
+We presented an overview of the project at the 2019 KVM Forum.
+    https://kvmforum2019.sched.com/event/Tmwc/qemu-hexagon-automatic-translation-of-the-isa-manual-pseudcode-to-tiny-code-instructions-of-a-vliw-architecture-niccolo-izzo-revng-taylor-simpson-qualcomm-innovation-center
+
+*** Tour of the code ***
+
+The qemu-hexagon implementation is a combination of qemu and the Hexagon
+architecture library (aka archlib).  The three primary directories with
+Hexagon-specific code are
+
+    qemu/target/hexagon
+        This has all the instruction and packet semantics
+    qemu/target/hexagon/imported
+        These files are imported with very little modification from archlib
+        *.idef                  Instruction semantics definition
+        macros.def              Mapping of macros to instruction attributes
+        encode*.def             Encoding patterns for each instruction
+        iclass.def              Instruction class definitions used to determine
+                                legal VLIW slots for each instruction
+    qemu/linux-user/hexagon
+        Helpers for loading the ELF file and making Linux system calls,
+        signals, etc
+
+We start with scripts that generate a bunch of include files.  This
+is a two step process.  The first step is to use the C preprocessor to expand
+macros inside the architecture definition files.  This is done in
+target/hexagon/gen_semantics.c.  This step produces
+    <BUILD_DIR>/target/hexagon/semantics_generated.pyinc.
+That file is consumed by the following python scripts to produce the indicated
+header files in <BUILD_DIR>/target/hexagon
+        gen_opcodes_def.py              -> opcodes_def_generated.h.inc
+        gen_op_regs.py                  -> op_regs_generated.h.inc
+        gen_printinsn.py                -> printinsn_generated.h.inc
+        gen_op_attribs.py               -> op_attribs_generated.h.inc
+        gen_helper_protos.py            -> helper_protos_generated.h.inc
+        gen_shortcode.py                -> shortcode_generated.h.inc
+        gen_tcg_funcs.py                -> tcg_funcs_generated.c.inc
+        gen_tcg_func_table.py           -> tcg_func_table_generated.c.inc
+        gen_helper_funcs.py             -> helper_funcs_generated.c.inc
+
+Qemu helper functions have 3 parts
+    DEF_HELPER declaration indicates the signature of the helper
+    gen_helper_<NAME> will generate a TCG call to the helper function
+    The helper implementation
+
+Here's an example of the A2_add instruction.
+    Instruction tag        A2_add
+    Assembly syntax        "Rd32=add(Rs32,Rt32)"
+    Instruction semantics  "{ RdV=RsV+RtV;}"
+
+By convention, the operands are identified by letter
+    RdV is the destination register
+    RsV, RtV are source registers
+
+The generator uses the operand naming conventions (see large comment in
+hex_common.py) to determine the signature of the helper function.  Here are the
+results for A2_add
+
+helper_protos_generated.h.inc
+    DEF_HELPER_3(A2_add, s32, env, s32, s32)
+
+tcg_funcs_generated.c.inc
+    static void generate_A2_add(
+                    CPUHexagonState *env,
+                    DisasContext *ctx,
+                    Insn *insn,
+                    Packet *pkt)
+    {
+        TCGv RdV = tcg_temp_local_new();
+        const int RdN = insn->regno[0];
+        TCGv RsV = hex_gpr[insn->regno[1]];
+        TCGv RtV = hex_gpr[insn->regno[2]];
+        gen_helper_A2_add(RdV, cpu_env, RsV, RtV);
+        gen_log_reg_write(RdN, RdV);
+        ctx_log_reg_write(ctx, RdN);
+        tcg_temp_free(RdV);
+    }
+
+helper_funcs_generated.c.inc
+    int32_t HELPER(A2_add)(CPUHexagonState *env, int32_t RsV, int32_t RtV)
+    {
+        uint32_t slot __attribute__((unused)) = 4;
+        int32_t RdV = 0;
+        { RdV=RsV+RtV;}
+        return RdV;
+    }
+
+Note that generate_A2_add updates the disassembly context to be processed
+when the packet commits (see "Packet Semantics" below).
+
+The generator checks for fGEN_TCG_<tag> macro.  This allows us to generate
+TCG code instead of a call to the helper.  If defined, the macro takes 1
+argument.
+    C semantics (aka short code)
+
+This allows the code generator to override the auto-generated code.  In some
+cases this is necessary for correct execution.  We can also override for
+faster emulation.  For example, calling a helper for add is more expensive
+than generating a TCG add operation.
+
+The gen_tcg.h file has any overrides. For example, we could write
+    #define fGEN_TCG_A2_add(GENHLPR, SHORTCODE) \
+        tcg_gen_add_tl(RdV, RsV, RtV)
+
+The instruction semantics C code relies heavily on macros.  In cases where the
+C semantics are specified only with macros, we can override the default with
+the short semantics option and #define the macros to generate TCG code.  One
+example is L2_loadw_locked:
+    Instruction tag        L2_loadw_locked
+    Assembly syntax        "Rd32=memw_locked(Rs32)"
+    Instruction semantics  "{ fEA_REG(RsV); fLOAD_LOCKED(1,4,u,EA,RdV) }"
+
+In gen_tcg.h, we use the shortcode
+#define fGEN_TCG_L2_loadw_locked(SHORTCODE) \
+    SHORTCODE
+
+There are also cases where we brute force the TCG code generation.
+Instructions with multiple definitions are examples.  These require special
+handling because qemu helpers can only return a single value.
+
+In addition to instruction semantics, we use a generator to create the decode
+tree.  This generation is also a two step process.  The first step is to run
+target/hexagon/gen_dectree_import.c to produce
+    <BUILD_DIR>/target/hexagon/iset.py
+This file is imported by target/hexagon/dectree.py to produce
+    <BUILD_DIR>/target/hexagon/dectree_generated.h.inc
+
+*** Key Files ***
+
+cpu.h
+
+This file contains the definition of the CPUHexagonState struct.  It is the
+runtime information for each thread and contains stuff like the GPR and
+predicate registers.
+
+macros.h
+
+The Hexagon arch lib relies heavily on macros for the instruction semantics.
+This is a great advantage for qemu because we can override them for different
+purposes.  You will also notice there are sometimes two definitions of a macro.
+The QEMU_GENERATE variable determines whether we want the macro to generate TCG
+code.  If QEMU_GENERATE is not defined, we want the macro to generate vanilla
+C code that will work in the helper implementation.
+
+translate.c
+
+The functions in this file generate TCG code for a translation block.  Some
+important functions in this file are
+
+    gen_start_packet - initialize the data structures for packet semantics
+    gen_commit_packet - commit the register writes, stores, etc for a packet
+    decode_and_translate_packet - disassemble a packet and generate code
+
+genptr.c
+gen_tcg.h
+
+These files create a function for each instruction.  It is mostly composed of
+fGEN_TCG_<tag> definitions followed by including tcg_funcs_generated.c.inc.
+
+op_helper.c
+
+This file contains the implementations of all the helpers.  There are a few
+general purpose helpers, but most of them are generated by including
+helper_funcs_generated.c.inc.  There are also several helpers used for debugging.
+
+
+*** Packet Semantics ***
+
+VLIW packet semantics differ from serial semantics in that all input operands
+are read, then the operations are performed, then all the results are written.
+For exmaple, this packet performs a swap of registers r0 and r1
+    { r0 = r1; r1 = r0 }
+Note that the result is different if the instructions are executed serially.
+
+Packet semantics dictate that we defer any changes of state until the entire
+packet is committed.  We record the results of each instruction in a side data
+structure, and update the visible processor state when we commit the packet.
+
+The data structures are divided between the runtime state and the translation
+context.
+
+During the TCG generation (see translate.[ch]), we use the DisasContext to
+track what needs to be done during packet commit.  Here are the relevant
+fields
+
+    reg_log            list of registers written
+    reg_log_idx        index into ctx_reg_log
+    pred_log           list of predicates written
+    pred_log_idx       index into ctx_pred_log
+    store_width        width of stores (indexed by slot)
+
+During runtime, the following fields in CPUHexagonState (see cpu.h) are used
+
+    new_value             new value of a given register
+    reg_written           boolean indicating if register was written
+    new_pred_value        new value of a predicate register
+    pred_written          boolean indicating if predicate was written
+    mem_log_stores        record of the stores (indexed by slot)
+
+*** Debugging ***
+
+You can turn on a lot of debugging by changing the HEX_DEBUG macro to 1 in
+internal.h.  This will stream a lot of information as it generates TCG and
+executes the code.
+
+To track down nasty issues with Hexagon->TCG generation, we compare the
+execution results with actual hardware running on a Hexagon Linux target.
+Run qemu with the "-d cpu" option.  Then, we can diff the results and figure
+out where qemu and hardware behave differently.
+
+The stacks are located at different locations.  We handle this by changing
+env->stack_adjust in translate.c.  First, set this to zero and run qemu.
+Then, change env->stack_adjust to the difference between the two stack
+locations.  Then rebuild qemu and run again. That will produce a very
+clean diff.
+
+Here are some handy places to set breakpoints
+
+    At the call to gen_start_packet for a given PC (note that the line number
+        might change in the future)
+        br translate.c:602 if ctx->base.pc_next == 0xdeadbeef
+    The helper function for each instruction is named helper_<TAG>, so here's
+        an example that will set a breakpoint at the start
+        br helper_A2_add
+    If you have the HEX_DEBUG macro set, the following will be useful
+        At the start of execution of a packet for a given PC
+            br helper_debug_start_packet if env->gpr[41] == 0xdeadbeef
+        At the end of execution of a packet for a given PC
+            br helper_debug_commit_end if env->this_PC == 0xdeadbeef
-- 
2.7.4


  parent reply	other threads:[~2021-02-08  5:49 UTC|newest]

Thread overview: 101+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-08  5:45 [PATCH v8 00/35] Hexagon patch series Taylor Simpson
2021-02-08  5:45 ` [PATCH v8 01/35] Hexagon Update MAINTAINERS file Taylor Simpson
2021-02-08  5:45 ` Taylor Simpson [this message]
2021-02-08  5:45 ` [PATCH v8 03/35] Hexagon (include/elf.h) ELF machine definition Taylor Simpson
2021-02-08  5:45 ` [PATCH v8 04/35] Hexagon (target/hexagon) scalar core definition Taylor Simpson
2021-02-08  5:45 ` [PATCH v8 05/35] Hexagon (disas) disassembler Taylor Simpson
2021-02-08  5:45 ` [PATCH v8 06/35] Hexagon (target/hexagon) register names Taylor Simpson
2021-02-08  5:45 ` [PATCH v8 07/35] Hexagon (target/hexagon) scalar core helpers Taylor Simpson
2021-02-08  5:45 ` [PATCH v8 08/35] Hexagon (target/hexagon) GDB Stub Taylor Simpson
2021-02-08  5:45 ` [PATCH v8 09/35] Hexagon (target/hexagon) architecture types Taylor Simpson
2021-02-12  0:32   ` Philippe Mathieu-Daudé
2021-02-14 17:16   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 10/35] Hexagon (target/hexagon) instruction and packet types Taylor Simpson
2021-02-14 17:21   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 11/35] Hexagon (target/hexagon) register fields Taylor Simpson
2021-02-14 17:25   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 12/35] Hexagon (target/hexagon) instruction attributes Taylor Simpson
2021-02-14 18:00   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 13/35] Hexagon (target/hexagon) instruction/packet decode Taylor Simpson
2021-02-14 18:31   ` Richard Henderson
2021-03-14 17:23     ` Taylor Simpson
2021-02-08  5:46 ` [PATCH v8 14/35] Hexagon (target/hexagon) instruction printing Taylor Simpson
2021-02-14 18:36   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 15/35] Hexagon (target/hexagon/arch.[ch]) utility functions Taylor Simpson
2021-02-14 20:13   ` Richard Henderson
2021-03-18  3:57     ` Taylor Simpson
2021-02-08  5:46 ` [PATCH v8 16/35] Hexagon (target/hexagon/conv_emu.[ch]) " Taylor Simpson
2021-02-14 20:57   ` Richard Henderson
2021-03-18  3:57     ` Taylor Simpson
2021-03-18 13:30       ` Richard Henderson
2021-03-18 14:11         ` Taylor Simpson
2021-03-18 15:35           ` Richard Henderson
2021-03-18 18:03             ` Taylor Simpson
2021-03-18 18:32               ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 17/35] Hexagon (target/hexagon/fma_emu.[ch]) " Taylor Simpson
2021-02-12  0:31   ` Philippe Mathieu-Daudé
2021-02-14 23:14   ` Richard Henderson
2021-03-18  3:57     ` Taylor Simpson
2021-02-08  5:46 ` [PATCH v8 18/35] Hexagon (target/hexagon/imported) arch import Taylor Simpson
2021-02-08  5:46 ` [PATCH v8 19/35] Hexagon (target/hexagon) generator phase 1 - C preprocessor for semantics Taylor Simpson
2021-02-14 23:18   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 20/35] Hexagon (target/hexagon) generator phase 2 - generate header files Taylor Simpson
2021-02-14 23:20   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 21/35] Hexagon (target/hexagon) generator phase 3 - C preprocessor for decode tree Taylor Simpson
2021-02-14 23:24   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 22/35] Hexagon (target/hexagon) generater phase 4 - " Taylor Simpson
2021-02-14 23:26   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 23/35] Hexagon (target/hexagon) opcode data structures Taylor Simpson
2021-02-12  0:26   ` Philippe Mathieu-Daudé
2021-02-14 23:27   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 24/35] Hexagon (target/hexagon) macros Taylor Simpson
2021-02-14 23:36   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 25/35] Hexagon (target/hexagon) instruction classes Taylor Simpson
2021-02-14 23:41   ` Richard Henderson
2021-03-14  0:39     ` Taylor Simpson
2021-02-08  5:46 ` [PATCH v8 26/35] Hexagon (target/hexagon) TCG generation Taylor Simpson
2021-02-12  0:22   ` Philippe Mathieu-Daudé
2021-04-05 23:03     ` Taylor Simpson
2021-04-06  9:20       ` Alex Bennée
2021-04-06 15:44         ` Taylor Simpson
2021-02-15  0:06   ` Richard Henderson
2021-03-14  0:39     ` Taylor Simpson
2021-03-14  1:39       ` Richard Henderson
2021-03-15  3:09         ` Taylor Simpson
2021-02-08  5:46 ` [PATCH v8 27/35] Hexagon (target/hexagon) TCG for instructions with multiple definitions Taylor Simpson
2021-02-15  0:33   ` Richard Henderson
2021-03-14  0:41     ` Taylor Simpson
2021-03-14 18:02       ` Richard Henderson
2021-03-15  2:59         ` Taylor Simpson
2021-02-08  5:46 ` [PATCH v8 28/35] Hexagon (target/hexagon) TCG for floating point instructions Taylor Simpson
2021-02-15  0:34   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 29/35] Hexagon (target/hexagon) translation Taylor Simpson
2021-02-15  1:03   ` Richard Henderson
2021-03-14  0:40     ` Taylor Simpson
2021-03-14  1:44       ` Richard Henderson
2021-03-15  3:06         ` Taylor Simpson
2021-03-15 13:31           ` Richard Henderson
2021-03-15 22:19             ` Taylor Simpson
2021-02-08  5:46 ` [PATCH v8 30/35] Hexagon (linux-user/hexagon) Linux user emulation Taylor Simpson
2021-02-08  5:46 ` [PATCH v8 31/35] Hexagon (tests/tcg/hexagon) TCG tests - multiarch Taylor Simpson
2021-02-14 18:14   ` Philippe Mathieu-Daudé
2021-02-15  1:05   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 32/35] Hexagon (tests/tcg/hexagon) TCG tests - atomics/load/store/misc Taylor Simpson
2021-02-15  1:09   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 33/35] Hexagon (tests/tcg/hexagon) TCG tests - floating point Taylor Simpson
2021-02-15  1:10   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 34/35] Hexagon build infrastructure Taylor Simpson
2021-02-15  1:11   ` Richard Henderson
2021-02-08  5:46 ` [PATCH v8 35/35] Add Dockerfile for hexagon Taylor Simpson
2021-02-14 18:50   ` Philippe Mathieu-Daudé
2021-02-17 17:23     ` Alessandro Di Federico via
2021-02-17 17:33       ` Philippe Mathieu-Daudé
2021-02-27 14:10   ` Philippe Mathieu-Daudé
2021-02-27 14:32     ` Brian Cain
2021-02-08  6:25 ` [PATCH v8 00/35] Hexagon patch series no-reply
2021-02-15  1:23 ` Richard Henderson
2021-02-16 20:59   ` Taylor Simpson
2021-02-16 21:17     ` Peter Maydell
2021-02-17 14:23       ` Philippe Mathieu-Daudé
2021-02-17 17:05         ` Richard Henderson
2021-02-17 16:58     ` Richard Henderson

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=1612763186-18161-3-git-send-email-tsimpson@quicinc.com \
    --to=tsimpson@quicinc.com \
    --cc=ale@rev.ng \
    --cc=alex.bennee@linaro.org \
    --cc=bcain@quicinc.com \
    --cc=laurent@vivier.eu \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.