75a869b3399795468a956aa087d1b96d77b9056e
[deliverable/binutils-gdb.git] / sim / bpf / mloop.in
1 # Simulator main loop for eBPF. -*- C -*-
2 #
3 # Copyright (C) 2020-2021 Free Software Foundation, Inc.
4 #
5 # This file is part of the GNU Simulators.
6 #
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.
11 #
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.
16 #
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/>.
19
20 # Syntax:
21 # /bin/sh mloop.in command
22 #
23 # Command is one of:
24 #
25 # init
26 # support
27 # extract-{simple,scache,pbb}
28 # {full,fast}-exec-{simple,scache,pbb}
29 #
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
33 # one of scache/pbb.
34 # A target can't provide more than this.
35 # However for illustration's sake this file provides examples of all.
36
37 # ??? After a few more ports are done, revisit.
38 # Will eventually need to machine generate a lot of this.
39
40 case "x$1" in
41
42 xsupport)
43
44 cat <<EOF
45
46 static INLINE const IDESC *
47 extract (SIM_CPU *current_cpu, PCADDR pc, CGEN_INSN_WORD insn,
48 ARGBUF *abuf, int fast_p)
49 {
50 const IDESC *id = @prefix@_decode (current_cpu, pc, insn, abuf);
51 @prefix@_fill_argbuf (current_cpu, abuf, id, pc, fast_p);
52 if (!fast_p)
53 {
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);
57 }
58 return id;
59 }
60
61 static INLINE SEM_PC
62 execute (SIM_CPU *current_cpu, SCACHE *sc, int fast_p)
63 {
64 SEM_PC vpc;
65
66 if (fast_p)
67 vpc = (*sc->argbuf.semantic.sem_fast) (current_cpu, sc);
68 else
69 {
70 ARGBUF *abuf = &sc->argbuf;
71 const IDESC *idesc = abuf->idesc;
72 const CGEN_INSN *idata = idesc->idata;
73 int virtual_p = 0;
74
75 if (! virtual_p)
76 {
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);
87 }
88 vpc = (*sc->argbuf.semantic.sem_full) (current_cpu, sc);
89 if (! virtual_p)
90 {
91 /* FIXME: call x-after */
92 if (PROFILE_MODEL_P (current_cpu)
93 && ARGBUF_PROFILE_P (abuf))
94 {
95 int cycles;
96
97 cycles = (*idesc->timing->model_fn) (current_cpu, sc);
98 @cpu@_model_insn_after (current_cpu, 1 /*last_p*/, cycles);
99 }
100 CGEN_TRACE_INSN_FINI (current_cpu, abuf, 1);
101 }
102 }
103
104 return vpc;
105 }
106
107 EOF
108
109 ;;
110
111 xinit)
112
113 # Nothing needed.
114
115 ;;
116
117 xextract-scache)
118
119 cat <<EOF
120 {
121
122 UDI insn = GETIMEMUDI (current_cpu, vpc);
123
124 if (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
125 {
126 UHI off16;
127 USI imm32;
128
129 /* eBPF instructions are little-endian, but GETIMEMUDI reads according
130 to target byte order. Swap to little-endian. */
131 insn = SWAP_8 (insn);
132
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);
139
140 insn = (((UDI) imm32) << 32) | (((UDI) off16) << 16) | (insn & 0xffff);
141 }
142
143 extract (current_cpu, vpc, insn, SEM_ARGBUF (sc), FAST_P);
144
145 //XXX SEM_SKIP_COMPILE (current_cpu, sc, 1);
146 }
147 EOF
148
149 ;;
150
151 xfull-exec-* | xfast-exec-*)
152
153 # Inputs: current_cpu, vpc, sc, FAST_P
154 # Outputs: vpc
155 # vpc is the virtual program counter.
156
157 cat <<EOF
158 vpc = execute (current_cpu, sc, FAST_P);
159 EOF
160
161 ;;
162
163 *)
164 echo "Invalid argument to mainloop.in: $1" >&2
165 exit 1
166 ;;
167
168 esac
This page took 0.032617 seconds and 3 git commands to generate.