4c44f408625b0481d32f14575b1ac86ef70395ac
[gem5.git] / src / sim / process.cc
1 /*
2 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
3 * Copyright (c) 2012 ARM Limited
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2001-2005 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Nathan Binkert
42 * Steve Reinhardt
43 * Ali Saidi
44 * Brandon Potter
45 */
46
47 #include "sim/process.hh"
48
49 #include <fcntl.h>
50 #include <unistd.h>
51
52 #include <array>
53 #include <map>
54 #include <string>
55 #include <vector>
56
57 #include "base/intmath.hh"
58 #include "base/loader/object_file.hh"
59 #include "base/loader/symtab.hh"
60 #include "base/statistics.hh"
61 #include "config/the_isa.hh"
62 #include "cpu/thread_context.hh"
63 #include "mem/page_table.hh"
64 #include "mem/se_translating_port_proxy.hh"
65 #include "params/Process.hh"
66 #include "sim/emul_driver.hh"
67 #include "sim/fd_array.hh"
68 #include "sim/fd_entry.hh"
69 #include "sim/syscall_desc.hh"
70 #include "sim/system.hh"
71
72 #if THE_ISA == ALPHA_ISA
73 #include "arch/alpha/linux/process.hh"
74 #elif THE_ISA == SPARC_ISA
75 #include "arch/sparc/linux/process.hh"
76 #include "arch/sparc/solaris/process.hh"
77 #elif THE_ISA == MIPS_ISA
78 #include "arch/mips/linux/process.hh"
79 #elif THE_ISA == ARM_ISA
80 #include "arch/arm/linux/process.hh"
81 #include "arch/arm/freebsd/process.hh"
82 #elif THE_ISA == X86_ISA
83 #include "arch/x86/linux/process.hh"
84 #elif THE_ISA == POWER_ISA
85 #include "arch/power/linux/process.hh"
86 #elif THE_ISA == RISCV_ISA
87 #include "arch/riscv/linux/process.hh"
88 #else
89 #error "THE_ISA not set"
90 #endif
91
92
93 using namespace std;
94 using namespace TheISA;
95
96 Process::Process(ProcessParams * params, ObjectFile * obj_file)
97 : SimObject(params), system(params->system),
98 brk_point(0), stack_base(0), stack_size(0), stack_min(0),
99 max_stack_size(params->max_stack_size),
100 next_thread_stack_base(0),
101 useArchPT(params->useArchPT),
102 kvmInSE(params->kvmInSE),
103 pTable(useArchPT ?
104 static_cast<PageTableBase *>(new ArchPageTable(name(), params->pid,
105 system)) :
106 static_cast<PageTableBase *>(new FuncPageTable(name(), params->pid))),
107 initVirtMem(system->getSystemPort(), this,
108 SETranslatingPortProxy::Always),
109 objFile(obj_file),
110 argv(params->cmd), envp(params->env), cwd(params->cwd),
111 executable(params->executable),
112 _uid(params->uid), _euid(params->euid),
113 _gid(params->gid), _egid(params->egid),
114 _pid(params->pid), _ppid(params->ppid),
115 drivers(params->drivers),
116 fds(make_shared<FDArray>(params->input, params->output, params->errout))
117 {
118 mmap_end = 0;
119
120 // load up symbols, if any... these may be used for debugging or
121 // profiling.
122 if (!debugSymbolTable) {
123 debugSymbolTable = new SymbolTable();
124 if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
125 !objFile->loadLocalSymbols(debugSymbolTable) ||
126 !objFile->loadWeakSymbols(debugSymbolTable)) {
127 // didn't load any symbols
128 delete debugSymbolTable;
129 debugSymbolTable = NULL;
130 }
131 }
132 }
133
134 void
135 Process::regStats()
136 {
137 SimObject::regStats();
138
139 using namespace Stats;
140
141 num_syscalls
142 .name(name() + ".num_syscalls")
143 .desc("Number of system calls")
144 ;
145 }
146
147 ThreadContext *
148 Process::findFreeContext()
149 {
150 for (int id : contextIds) {
151 ThreadContext *tc = system->getThreadContext(id);
152 if (tc->status() == ThreadContext::Halted)
153 return tc;
154 }
155 return NULL;
156 }
157
158 void
159 Process::initState()
160 {
161 if (contextIds.empty())
162 fatal("Process %s is not associated with any HW contexts!\n", name());
163
164 // first thread context for this process... initialize & enable
165 ThreadContext *tc = system->getThreadContext(contextIds[0]);
166
167 // mark this context as active so it will start ticking.
168 tc->activate();
169
170 pTable->initState(tc);
171 }
172
173 DrainState
174 Process::drain()
175 {
176 fds->updateFileOffsets();
177 return DrainState::Drained;
178 }
179
180 void
181 Process::allocateMem(Addr vaddr, int64_t size, bool clobber)
182 {
183 int npages = divCeil(size, (int64_t)PageBytes);
184 Addr paddr = system->allocPhysPages(npages);
185 pTable->map(vaddr, paddr, size,
186 clobber ? PageTableBase::Clobber : PageTableBase::Zero);
187 }
188
189 bool
190 Process::fixupStackFault(Addr vaddr)
191 {
192 // Check if this is already on the stack and there's just no page there
193 // yet.
194 if (vaddr >= stack_min && vaddr < stack_base) {
195 allocateMem(roundDown(vaddr, PageBytes), PageBytes);
196 return true;
197 }
198
199 // We've accessed the next page of the stack, so extend it to include
200 // this address.
201 if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
202 while (vaddr < stack_min) {
203 stack_min -= TheISA::PageBytes;
204 if (stack_base - stack_min > max_stack_size)
205 fatal("Maximum stack size exceeded\n");
206 allocateMem(stack_min, TheISA::PageBytes);
207 inform("Increasing stack size by one page.");
208 };
209 return true;
210 }
211 return false;
212 }
213
214 void
215 Process::serialize(CheckpointOut &cp) const
216 {
217 SERIALIZE_SCALAR(brk_point);
218 SERIALIZE_SCALAR(stack_base);
219 SERIALIZE_SCALAR(stack_size);
220 SERIALIZE_SCALAR(stack_min);
221 SERIALIZE_SCALAR(next_thread_stack_base);
222 SERIALIZE_SCALAR(mmap_end);
223 pTable->serialize(cp);
224 /**
225 * Checkpoints for file descriptors currently do not work. Need to
226 * come back and fix them at a later date.
227 */
228
229 warn("Checkpoints for file descriptors currently do not work.");
230 #if 0
231 for (int x = 0; x < fds->getSize(); x++)
232 (*fds)[x].serializeSection(cp, csprintf("FDEntry%d", x));
233 #endif
234
235 }
236
237 void
238 Process::unserialize(CheckpointIn &cp)
239 {
240 UNSERIALIZE_SCALAR(brk_point);
241 UNSERIALIZE_SCALAR(stack_base);
242 UNSERIALIZE_SCALAR(stack_size);
243 UNSERIALIZE_SCALAR(stack_min);
244 UNSERIALIZE_SCALAR(next_thread_stack_base);
245 UNSERIALIZE_SCALAR(mmap_end);
246 pTable->unserialize(cp);
247 /**
248 * Checkpoints for file descriptors currently do not work. Need to
249 * come back and fix them at a later date.
250 */
251 warn("Checkpoints for file descriptors currently do not work.");
252 #if 0
253 for (int x = 0; x < fds->getSize(); x++)
254 (*fds)[x]->unserializeSection(cp, csprintf("FDEntry%d", x));
255 fds->restoreFileOffsets();
256 #endif
257 // The above returns a bool so that you could do something if you don't
258 // find the param in the checkpoint if you wanted to, like set a default
259 // but in this case we'll just stick with the instantiated value if not
260 // found.
261 }
262
263 bool
264 Process::map(Addr vaddr, Addr paddr, int size, bool cacheable)
265 {
266 pTable->map(vaddr, paddr, size,
267 cacheable ? PageTableBase::Zero : PageTableBase::Uncacheable);
268 return true;
269 }
270
271 void
272 Process::syscall(int64_t callnum, ThreadContext *tc)
273 {
274 num_syscalls++;
275
276 SyscallDesc *desc = getDesc(callnum);
277 if (desc == NULL)
278 fatal("Syscall %d out of range", callnum);
279
280 desc->doSyscall(callnum, this, tc);
281 }
282
283 IntReg
284 Process::getSyscallArg(ThreadContext *tc, int &i, int width)
285 {
286 return getSyscallArg(tc, i);
287 }
288
289 EmulatedDriver *
290 Process::findDriver(std::string filename)
291 {
292 for (EmulatedDriver *d : drivers) {
293 if (d->match(filename))
294 return d;
295 }
296
297 return NULL;
298 }
299
300 void
301 Process::updateBias()
302 {
303 ObjectFile *interp = objFile->getInterpreter();
304
305 if (!interp || !interp->relocatable())
306 return;
307
308 // Determine how large the interpreters footprint will be in the process
309 // address space.
310 Addr interp_mapsize = roundUp(interp->mapSize(), TheISA::PageBytes);
311
312 // We are allocating the memory area; set the bias to the lowest address
313 // in the allocated memory region.
314 Addr ld_bias = mmapGrowsDown() ? mmap_end - interp_mapsize : mmap_end;
315
316 // Adjust the process mmap area to give the interpreter room; the real
317 // execve system call would just invoke the kernel's internal mmap
318 // functions to make these adjustments.
319 mmap_end = mmapGrowsDown() ? ld_bias : mmap_end + interp_mapsize;
320
321 interp->updateBias(ld_bias);
322 }
323
324 ObjectFile *
325 Process::getInterpreter()
326 {
327 return objFile->getInterpreter();
328 }
329
330 Addr
331 Process::getBias()
332 {
333 ObjectFile *interp = getInterpreter();
334
335 return interp ? interp->bias() : objFile->bias();
336 }
337
338 Addr
339 Process::getStartPC()
340 {
341 ObjectFile *interp = getInterpreter();
342
343 return interp ? interp->entryPoint() : objFile->entryPoint();
344 }
345
346 Process *
347 ProcessParams::create()
348 {
349 Process *process = NULL;
350
351 // If not specified, set the executable parameter equal to the
352 // simulated system's zeroth command line parameter
353 if (executable == "") {
354 executable = cmd[0];
355 }
356
357 ObjectFile *obj_file = createObjectFile(executable);
358 if (obj_file == NULL) {
359 fatal("Can't load object file %s", executable);
360 }
361
362 #if THE_ISA == ALPHA_ISA
363 if (obj_file->getArch() != ObjectFile::Alpha)
364 fatal("Object file architecture does not match compiled ISA (Alpha).");
365
366 switch (obj_file->getOpSys()) {
367 case ObjectFile::UnknownOpSys:
368 warn("Unknown operating system; assuming Linux.");
369 // fall through
370 case ObjectFile::Linux:
371 process = new AlphaLinuxProcess(this, obj_file);
372 break;
373
374 default:
375 fatal("Unknown/unsupported operating system.");
376 }
377 #elif THE_ISA == SPARC_ISA
378 if (obj_file->getArch() != ObjectFile::SPARC64 &&
379 obj_file->getArch() != ObjectFile::SPARC32)
380 fatal("Object file architecture does not match compiled ISA (SPARC).");
381 switch (obj_file->getOpSys()) {
382 case ObjectFile::UnknownOpSys:
383 warn("Unknown operating system; assuming Linux.");
384 // fall through
385 case ObjectFile::Linux:
386 if (obj_file->getArch() == ObjectFile::SPARC64) {
387 process = new Sparc64LinuxProcess(this, obj_file);
388 } else {
389 process = new Sparc32LinuxProcess(this, obj_file);
390 }
391 break;
392
393 case ObjectFile::Solaris:
394 process = new SparcSolarisProcess(this, obj_file);
395 break;
396
397 default:
398 fatal("Unknown/unsupported operating system.");
399 }
400 #elif THE_ISA == X86_ISA
401 if (obj_file->getArch() != ObjectFile::X86_64 &&
402 obj_file->getArch() != ObjectFile::I386)
403 fatal("Object file architecture does not match compiled ISA (x86).");
404 switch (obj_file->getOpSys()) {
405 case ObjectFile::UnknownOpSys:
406 warn("Unknown operating system; assuming Linux.");
407 // fall through
408 case ObjectFile::Linux:
409 if (obj_file->getArch() == ObjectFile::X86_64) {
410 process = new X86_64LinuxProcess(this, obj_file);
411 } else {
412 process = new I386LinuxProcess(this, obj_file);
413 }
414 break;
415
416 default:
417 fatal("Unknown/unsupported operating system.");
418 }
419 #elif THE_ISA == MIPS_ISA
420 if (obj_file->getArch() != ObjectFile::Mips)
421 fatal("Object file architecture does not match compiled ISA (MIPS).");
422 switch (obj_file->getOpSys()) {
423 case ObjectFile::UnknownOpSys:
424 warn("Unknown operating system; assuming Linux.");
425 // fall through
426 case ObjectFile::Linux:
427 process = new MipsLinuxProcess(this, obj_file);
428 break;
429
430 default:
431 fatal("Unknown/unsupported operating system.");
432 }
433 #elif THE_ISA == ARM_ISA
434 ObjectFile::Arch arch = obj_file->getArch();
435 if (arch != ObjectFile::Arm && arch != ObjectFile::Thumb &&
436 arch != ObjectFile::Arm64)
437 fatal("Object file architecture does not match compiled ISA (ARM).");
438 switch (obj_file->getOpSys()) {
439 case ObjectFile::UnknownOpSys:
440 warn("Unknown operating system; assuming Linux.");
441 // fall through
442 case ObjectFile::Linux:
443 if (arch == ObjectFile::Arm64) {
444 process = new ArmLinuxProcess64(this, obj_file,
445 obj_file->getArch());
446 } else {
447 process = new ArmLinuxProcess32(this, obj_file,
448 obj_file->getArch());
449 }
450 break;
451 case ObjectFile::FreeBSD:
452 if (arch == ObjectFile::Arm64) {
453 process = new ArmFreebsdProcess64(this, obj_file,
454 obj_file->getArch());
455 } else {
456 process = new ArmFreebsdProcess32(this, obj_file,
457 obj_file->getArch());
458 }
459 break;
460 case ObjectFile::LinuxArmOABI:
461 fatal("M5 does not support ARM OABI binaries. Please recompile with an"
462 " EABI compiler.");
463 default:
464 fatal("Unknown/unsupported operating system.");
465 }
466 #elif THE_ISA == POWER_ISA
467 if (obj_file->getArch() != ObjectFile::Power)
468 fatal("Object file architecture does not match compiled ISA (Power).");
469 switch (obj_file->getOpSys()) {
470 case ObjectFile::UnknownOpSys:
471 warn("Unknown operating system; assuming Linux.");
472 // fall through
473 case ObjectFile::Linux:
474 process = new PowerLinuxProcess(this, obj_file);
475 break;
476
477 default:
478 fatal("Unknown/unsupported operating system.");
479 }
480 #elif THE_ISA == RISCV_ISA
481 if (obj_file->getArch() != ObjectFile::Riscv)
482 fatal("Object file architecture does not match compiled ISA (RISCV).");
483 switch (obj_file->getOpSys()) {
484 case ObjectFile::UnknownOpSys:
485 warn("Unknown operating system; assuming Linux.");
486 // fall through
487 case ObjectFile::Linux:
488 process = new RiscvLinuxProcess(this, obj_file);
489 break;
490 default:
491 fatal("Unknown/unsupported operating system.");
492 }
493 #else
494 #error "THE_ISA not set"
495 #endif
496
497 if (process == NULL)
498 fatal("Unknown error creating process object.");
499 return process;
500 }
501
502 std::string
503 Process::fullPath(const std::string &file_name)
504 {
505 if (file_name[0] == '/' || cwd.empty())
506 return file_name;
507
508 std::string full = cwd;
509
510 if (cwd[cwd.size() - 1] != '/')
511 full += '/';
512
513 return full + file_name;
514 }