arch-power: Stop "using namespace std"
[gem5.git] / src / arch / power / process.cc
1 /*
2 * Copyright (c) 2007-2008 The Florida State University
3 * Copyright (c) 2009 The University of Edinburgh
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "arch/power/process.hh"
31
32 #include "arch/power/isa_traits.hh"
33 #include "arch/power/types.hh"
34 #include "base/loader/elf_object.hh"
35 #include "base/loader/object_file.hh"
36 #include "base/logging.hh"
37 #include "cpu/thread_context.hh"
38 #include "debug/Stack.hh"
39 #include "mem/page_table.hh"
40 #include "params/Process.hh"
41 #include "sim/aux_vector.hh"
42 #include "sim/process_impl.hh"
43 #include "sim/syscall_return.hh"
44 #include "sim/system.hh"
45
46 using namespace PowerISA;
47
48 PowerProcess::PowerProcess(
49 const ProcessParams &params, ::Loader::ObjectFile *objFile)
50 : Process(params,
51 new EmulationPageTable(params.name, params.pid, PageBytes),
52 objFile)
53 {
54 fatal_if(params.useArchPT, "Arch page tables not implemented.");
55 // Set up break point (Top of Heap)
56 Addr brk_point = image.maxAddr();
57 brk_point = roundUp(brk_point, PageBytes);
58
59 Addr stack_base = 0xbf000000L;
60
61 Addr max_stack_size = 8 * 1024 * 1024;
62
63 // Set pointer for next thread stack. Reserve 8M for main stack.
64 Addr next_thread_stack_base = stack_base - max_stack_size;
65
66 // Set up region for mmaps. For now, start at bottom of kuseg space.
67 Addr mmap_end = 0x70000000L;
68
69 memState = std::make_shared<MemState>(
70 this, brk_point, stack_base, max_stack_size,
71 next_thread_stack_base, mmap_end);
72 }
73
74 void
75 PowerProcess::initState()
76 {
77 Process::initState();
78
79 argsInit(sizeof(uint32_t), PageBytes);
80 }
81
82 void
83 PowerProcess::argsInit(int intSize, int pageSize)
84 {
85 std::vector<AuxVector<uint32_t>> auxv;
86
87 std::string filename;
88 if (argv.size() < 1)
89 filename = "";
90 else
91 filename = argv[0];
92
93 //We want 16 byte alignment
94 uint64_t align = 16;
95
96 // load object file into target memory
97 image.write(*initVirtMem);
98 interpImage.write(*initVirtMem);
99
100 //Setup the auxilliary vectors. These will already have endian conversion.
101 //Auxilliary vectors are loaded only for elf formatted executables.
102 auto *elfObject = dynamic_cast<::Loader::ElfObject *>(objFile);
103 if (elfObject) {
104 uint32_t features = 0;
105
106 //Bits which describe the system hardware capabilities
107 //XXX Figure out what these should be
108 auxv.emplace_back(M5_AT_HWCAP, features);
109 //The system page size
110 auxv.emplace_back(M5_AT_PAGESZ, PowerISA::PageBytes);
111 //Frequency at which times() increments
112 auxv.emplace_back(M5_AT_CLKTCK, 0x64);
113 // For statically linked executables, this is the virtual address of
114 // the program header tables if they appear in the executable image
115 auxv.emplace_back(M5_AT_PHDR, elfObject->programHeaderTable());
116 // This is the size of a program header entry from the elf file.
117 auxv.emplace_back(M5_AT_PHENT, elfObject->programHeaderSize());
118 // This is the number of program headers from the original elf file.
119 auxv.emplace_back(M5_AT_PHNUM, elfObject->programHeaderCount());
120 // This is the base address of the ELF interpreter; it should be
121 // zero for static executables or contain the base address for
122 // dynamic executables.
123 auxv.emplace_back(M5_AT_BASE, getBias());
124 //XXX Figure out what this should be.
125 auxv.emplace_back(M5_AT_FLAGS, 0);
126 //The entry point to the program
127 auxv.emplace_back(M5_AT_ENTRY, objFile->entryPoint());
128 //Different user and group IDs
129 auxv.emplace_back(M5_AT_UID, uid());
130 auxv.emplace_back(M5_AT_EUID, euid());
131 auxv.emplace_back(M5_AT_GID, gid());
132 auxv.emplace_back(M5_AT_EGID, egid());
133 //Whether to enable "secure mode" in the executable
134 auxv.emplace_back(M5_AT_SECURE, 0);
135 //The address of 16 "random" bytes
136 auxv.emplace_back(M5_AT_RANDOM, 0);
137 //The filename of the program
138 auxv.emplace_back(M5_AT_EXECFN, 0);
139 //The string "v51" with unknown meaning
140 auxv.emplace_back(M5_AT_PLATFORM, 0);
141 }
142
143 //Figure out how big the initial stack nedes to be
144
145 // A sentry NULL void pointer at the top of the stack.
146 int sentry_size = intSize;
147
148 std::string platform = "v51";
149 int platform_size = platform.size() + 1;
150
151 // The aux vectors are put on the stack in two groups. The first group are
152 // the vectors that are generated as the elf is loaded. The second group
153 // are the ones that were computed ahead of time and include the platform
154 // string.
155 int aux_data_size = filename.size() + 1;
156
157 const int numRandomBytes = 16;
158 aux_data_size += numRandomBytes;
159
160 int env_data_size = 0;
161 for (int i = 0; i < envp.size(); ++i) {
162 env_data_size += envp[i].size() + 1;
163 }
164 int arg_data_size = 0;
165 for (int i = 0; i < argv.size(); ++i) {
166 arg_data_size += argv[i].size() + 1;
167 }
168
169 int info_block_size =
170 sentry_size + env_data_size + arg_data_size +
171 aux_data_size + platform_size;
172
173 //Each auxilliary vector is two 4 byte words
174 int aux_array_size = intSize * 2 * (auxv.size() + 1);
175
176 int envp_array_size = intSize * (envp.size() + 1);
177 int argv_array_size = intSize * (argv.size() + 1);
178
179 int argc_size = intSize;
180
181 //Figure out the size of the contents of the actual initial frame
182 int frame_size =
183 info_block_size +
184 aux_array_size +
185 envp_array_size +
186 argv_array_size +
187 argc_size;
188
189 //There needs to be padding after the auxiliary vector data so that the
190 //very bottom of the stack is aligned properly.
191 int partial_size = frame_size;
192 int aligned_partial_size = roundUp(partial_size, align);
193 int aux_padding = aligned_partial_size - partial_size;
194
195 int space_needed = frame_size + aux_padding;
196
197 Addr stack_min = memState->getStackBase() - space_needed;
198 stack_min = roundDown(stack_min, align);
199
200 memState->setStackSize(memState->getStackBase() - stack_min);
201
202 // map memory
203 memState->mapRegion(roundDown(stack_min, pageSize),
204 roundUp(memState->getStackSize(), pageSize), "stack");
205
206 // map out initial stack contents
207 uint32_t sentry_base = memState->getStackBase() - sentry_size;
208 uint32_t aux_data_base = sentry_base - aux_data_size;
209 uint32_t env_data_base = aux_data_base - env_data_size;
210 uint32_t arg_data_base = env_data_base - arg_data_size;
211 uint32_t platform_base = arg_data_base - platform_size;
212 uint32_t auxv_array_base = platform_base - aux_array_size - aux_padding;
213 uint32_t envp_array_base = auxv_array_base - envp_array_size;
214 uint32_t argv_array_base = envp_array_base - argv_array_size;
215 uint32_t argc_base = argv_array_base - argc_size;
216
217 DPRINTF(Stack, "The addresses of items on the initial stack:\n");
218 DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
219 DPRINTF(Stack, "0x%x - env data\n", env_data_base);
220 DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
221 DPRINTF(Stack, "0x%x - platform base\n", platform_base);
222 DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
223 DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
224 DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
225 DPRINTF(Stack, "0x%x - argc \n", argc_base);
226 DPRINTF(Stack, "0x%x - stack min\n", stack_min);
227
228 // write contents to stack
229
230 // figure out argc
231 uint32_t argc = argv.size();
232 uint32_t guestArgc = htobe(argc);
233
234 //Write out the sentry void *
235 uint32_t sentry_NULL = 0;
236 initVirtMem->writeBlob(sentry_base, &sentry_NULL, sentry_size);
237
238 //Fix up the aux vectors which point to other data
239 for (int i = auxv.size() - 1; i >= 0; i--) {
240 if (auxv[i].type == M5_AT_PLATFORM) {
241 auxv[i].val = platform_base;
242 initVirtMem->writeString(platform_base, platform.c_str());
243 } else if (auxv[i].type == M5_AT_EXECFN) {
244 auxv[i].val = aux_data_base + numRandomBytes;
245 initVirtMem->writeString(aux_data_base, filename.c_str());
246 } else if (auxv[i].type == M5_AT_RANDOM) {
247 auxv[i].val = aux_data_base;
248 }
249 }
250
251 //Copy the aux stuff
252 Addr auxv_array_end = auxv_array_base;
253 for (const auto &aux: auxv) {
254 initVirtMem->write(auxv_array_end, aux, GuestByteOrder);
255 auxv_array_end += sizeof(aux);
256 }
257 //Write out the terminating zeroed auxilliary vector
258 const AuxVector<uint64_t> zero(0, 0);
259 initVirtMem->write(auxv_array_end, zero);
260 auxv_array_end += sizeof(zero);
261
262 copyStringArray(envp, envp_array_base, env_data_base,
263 ByteOrder::big, *initVirtMem);
264 copyStringArray(argv, argv_array_base, arg_data_base,
265 ByteOrder::big, *initVirtMem);
266
267 initVirtMem->writeBlob(argc_base, &guestArgc, intSize);
268
269 ThreadContext *tc = system->threads[contextIds[0]];
270
271 //Set the stack pointer register
272 tc->setIntReg(StackPointerReg, stack_min);
273
274 tc->pcState(getStartPC());
275
276 //Align the "stack_min" to a page boundary.
277 memState->setStackMin(roundDown(stack_min, pageSize));
278 }