e2aff08fdb750d64ec1c2746293d66f9fa8e8897
[gem5.git] / src / sim / kernel_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_KERNEL_WORKLOAD_HH__
29 #define __SIM_KERNEL_WORKLOAD_HH__
30
31 #include <string>
32 #include <vector>
33
34 #include "base/loader/object_file.hh"
35 #include "base/loader/symtab.hh"
36 #include "base/types.hh"
37 #include "params/KernelWorkload.hh"
38 #include "sim/workload.hh"
39
40 class System;
41
42 class KernelWorkload : public Workload
43 {
44 public:
45 using Params = KernelWorkloadParams;
46
47 protected:
48 const Params &_params;
49
50 Loader::MemoryImage image;
51
52 /** Mask that should be anded for binary/symbol loading.
53 * This allows one two different OS requirements for the same ISA to be
54 * handled. Some OSes are compiled for a virtual address and need to be
55 * loaded into physical memory that starts at address 0, while other
56 * bare metal tools generate images that start at address 0.
57 */
58 Addr _loadAddrMask;
59
60 /** Offset that should be used for binary/symbol loading.
61 * This further allows more flexibility than the loadAddrMask allows alone
62 * in loading kernels and similar. The loadAddrOffset is applied after the
63 * loadAddrMask.
64 */
65 Addr _loadAddrOffset;
66
67 Addr _start, _end;
68
69 std::vector<Loader::ObjectFile *> extras;
70
71 Loader::ObjectFile *kernelObj = nullptr;
72 // Keep a separate copy of the kernel's symbol table so we can add things
73 // to it.
74 Loader::SymbolTable kernelSymtab;
75
76 const std::string commandLine;
77
78 public:
79 const Params &params() const { return _params; }
80
81 Addr start() const { return _start; }
82 Addr end() const { return _end; }
83 Addr loadAddrMask() const { return _loadAddrMask; }
84 Addr loadAddrOffset() const { return _loadAddrOffset; }
85
86 KernelWorkload(const Params &p);
87
88 Addr getEntry() const override { return kernelObj->entryPoint(); }
89 Loader::Arch
90 getArch() const override
91 {
92 return kernelObj->getArch();
93 }
94
95 const Loader::SymbolTable &
96 symtab(ThreadContext *tc) override
97 {
98 return kernelSymtab;
99 }
100
101 bool
102 insertSymbol(const Loader::Symbol &symbol) override
103 {
104 return kernelSymtab.insert(symbol);
105 }
106
107 void initState() override;
108
109 void serialize(CheckpointOut &cp) const override;
110 void unserialize(CheckpointIn &cp) override;
111
112 /** @{ */
113 /**
114 * Add a function-based event to a kernel symbol.
115 *
116 * These functions work like their addFuncEvent() and
117 * addFuncEventOrPanic() counterparts. The only difference is that
118 * they automatically use the kernel symbol table. All arguments
119 * are forwarded to the underlying method.
120 *
121 * @see addFuncEvent()
122 * @see addFuncEventOrPanic()
123 *
124 * @param lbl Function to hook the event to.
125 * @param args Arguments to be passed to addFuncEvent
126 */
127 template <class T, typename... Args>
128 T *
129 addKernelFuncEvent(const char *lbl, Args... args)
130 {
131 return addFuncEvent<T>(kernelSymtab, lbl, std::forward<Args>(args)...);
132 }
133
134 template <class T, typename... Args>
135 T *
136 addKernelFuncEventOrPanic(const char *lbl, Args... args)
137 {
138 T *e = addFuncEvent<T>(kernelSymtab, lbl, std::forward<Args>(args)...);
139 panic_if(!e, "Failed to find kernel symbol '%s'", lbl);
140 return e;
141 }
142 /** @} */
143 };
144
145 #endif // __SIM_KERNEL_WORKLOAD_HH__