1 # Simulator main loop for eBPF. -*- C -*-
3 # Copyright (C) 2020-2021 Free Software Foundation, Inc.
5 # This file is part of the GNU Simulators.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # /bin/sh mloop.in command
27 # extract-{simple,scache,pbb}
28 # {full,fast}-exec-{simple,scache,pbb}
30 # A target need only provide a "full" version of one of simple,scache,pbb.
31 # If the target wants it can also provide a fast version of same, or if
32 # the slow (full featured) version is `simple', then the fast version can be
34 # A target can't provide more than this.
35 # However for illustration's sake this file provides examples of all.
37 # ??? After a few more ports are done, revisit.
38 # Will eventually need to machine generate a lot of this.
46 static INLINE const IDESC *
47 extract (SIM_CPU *current_cpu, PCADDR pc, CGEN_INSN_WORD insn,
48 ARGBUF *abuf, int fast_p)
50 const IDESC *id = @prefix@_decode (current_cpu, pc, insn, abuf);
51 @prefix@_fill_argbuf (current_cpu, abuf, id, pc, fast_p);
54 int trace_p = PC_IN_TRACE_RANGE_P (current_cpu, pc);
55 int profile_p = PC_IN_PROFILE_RANGE_P (current_cpu, pc);
56 @prefix@_fill_argbuf_tp (current_cpu, abuf, trace_p, profile_p);
62 execute (SIM_CPU *current_cpu, SCACHE *sc, int fast_p)
67 vpc = (*sc->argbuf.semantic.sem_fast) (current_cpu, sc);
70 ARGBUF *abuf = &sc->argbuf;
71 const IDESC *idesc = abuf->idesc;
72 const CGEN_INSN *idata = idesc->idata;
77 /* FIXME: call x-before */
78 if (ARGBUF_PROFILE_P (abuf))
79 PROFILE_COUNT_INSN (current_cpu, abuf->addr, idesc->num);
80 /* FIXME: Later make cover macros: PROFILE_INSN_{INIT,FINI}. */
81 if (PROFILE_MODEL_P (current_cpu)
82 && ARGBUF_PROFILE_P (abuf))
83 @cpu@_model_insn_before (current_cpu, 1 /*first_p*/);
84 CGEN_TRACE_INSN_INIT (current_cpu, abuf, 1);
85 CGEN_TRACE_INSN (current_cpu, idata,
86 (const struct argbuf *) abuf, abuf->addr);
88 vpc = (*sc->argbuf.semantic.sem_full) (current_cpu, sc);
91 /* FIXME: call x-after */
92 if (PROFILE_MODEL_P (current_cpu)
93 && ARGBUF_PROFILE_P (abuf))
97 cycles = (*idesc->timing->model_fn) (current_cpu, sc);
98 @cpu@_model_insn_after (current_cpu, 1 /*last_p*/, cycles);
100 CGEN_TRACE_INSN_FINI (current_cpu, abuf, 1);
122 UDI insn = GETIMEMUDI (current_cpu, vpc);
124 if (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
129 /* eBPF instructions are little-endian, but GETIMEMUDI reads according
130 to target byte order. Swap to little-endian. */
131 insn = SWAP_8 (insn);
133 /* But, the imm32 and offset16 fields within instructions follow target
134 byte order. Swap those fields back. */
135 off16 = (UHI) ((insn & 0x00000000ffff0000) >> 16);
136 imm32 = (USI) ((insn & 0xffffffff00000000) >> 32);
137 off16 = SWAP_2 (off16);
138 imm32 = SWAP_4 (imm32);
140 insn = (((UDI) imm32) << 32) | (((UDI) off16) << 16) | (insn & 0xffff);
143 extract (current_cpu, vpc, insn, SEM_ARGBUF (sc), FAST_P);
145 //XXX SEM_SKIP_COMPILE (current_cpu, sc, 1);
151 xfull-exec-* | xfast-exec-*)
153 # Inputs: current_cpu, vpc, sc, FAST_P
155 # vpc is the virtual program counter.
158 vpc = execute (current_cpu, sc, FAST_P);
164 echo "Invalid argument to mainloop.in: $1" >&2