e24aa746e8be0589277e1d8a4c3f73571b1e16fb
[gem5.git] / src / sim / workload.hh
1 /*
2 * Copyright 2019 Google Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef __SIM_WORKLOAD_HH__
29 #define __SIM_WORKLOAD_HH__
30
31 #include "base/loader/object_file.hh"
32 #include "base/loader/symtab.hh"
33 #include "params/Workload.hh"
34 #include "sim/sim_object.hh"
35
36 class System;
37 class ThreadContext;
38
39 class Workload : public SimObject
40 {
41 protected:
42 virtual Addr fixFuncEventAddr(Addr addr) const { return addr; }
43
44 public:
45 using SimObject::SimObject;
46
47 System *system = nullptr;
48
49 virtual Addr getEntry() const = 0;
50 virtual Loader::Arch getArch() const = 0;
51
52 virtual const Loader::SymbolTable *symtab(ThreadContext *tc) = 0;
53 virtual bool insertSymbol(const Loader::Symbol &symbol) = 0;
54
55 /** @{ */
56 /**
57 * Add a function-based event to the given function, to be looked
58 * up in the specified symbol table.
59 *
60 * The ...OrPanic flavor of the method causes the simulator to
61 * panic if the symbol can't be found.
62 *
63 * @param symtab Symbol table to use for look up.
64 * @param lbl Function to hook the event to.
65 * @param desc Description to be passed to the event.
66 * @param args Arguments to be forwarded to the event constructor.
67 */
68 template <class T, typename... Args>
69 T *
70 addFuncEvent(const Loader::SymbolTable *symtab, const char *lbl,
71 const std::string &desc, Args... args)
72 {
73 auto it = symtab->find(lbl);
74 if (it == symtab->end())
75 return nullptr;
76
77 return new T(system, desc, fixFuncEventAddr(it->address),
78 std::forward<Args>(args)...);
79 }
80
81 template <class T>
82 T *
83 addFuncEvent(const Loader::SymbolTable *symtab, const char *lbl)
84 {
85 return addFuncEvent<T>(symtab, lbl, lbl);
86 }
87
88 template <class T, typename... Args>
89 T *
90 addFuncEventOrPanic(const Loader::SymbolTable *symtab, const char *lbl,
91 Args... args)
92 {
93 T *e = addFuncEvent<T>(symtab, lbl, std::forward<Args>(args)...);
94 panic_if(!e, "Failed to find symbol '%s'", lbl);
95 return e;
96 }
97 /** @} */
98 };
99
100 #endif // __SIM_WORKLOAD_HH__