4c44f408625b0481d32f14575b1ac86ef70395ac
2 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
3 * Copyright (c) 2012 ARM Limited
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.
15 * Copyright (c) 2001-2005 The Regents of The University of Michigan
16 * All rights reserved.
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.
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.
41 * Authors: Nathan Binkert
47 #include "sim/process.hh"
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"
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"
89 #error "THE_ISA not set"
94 using namespace TheISA
;
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
),
104 static_cast<PageTableBase
*>(new ArchPageTable(name(), params
->pid
,
106 static_cast<PageTableBase
*>(new FuncPageTable(name(), params
->pid
))),
107 initVirtMem(system
->getSystemPort(), this,
108 SETranslatingPortProxy::Always
),
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
))
120 // load up symbols, if any... these may be used for debugging or
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
;
137 SimObject::regStats();
139 using namespace Stats
;
142 .name(name() + ".num_syscalls")
143 .desc("Number of system calls")
148 Process::findFreeContext()
150 for (int id
: contextIds
) {
151 ThreadContext
*tc
= system
->getThreadContext(id
);
152 if (tc
->status() == ThreadContext::Halted
)
161 if (contextIds
.empty())
162 fatal("Process %s is not associated with any HW contexts!\n", name());
164 // first thread context for this process... initialize & enable
165 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
167 // mark this context as active so it will start ticking.
170 pTable
->initState(tc
);
176 fds
->updateFileOffsets();
177 return DrainState::Drained
;
181 Process::allocateMem(Addr vaddr
, int64_t size
, bool clobber
)
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
);
190 Process::fixupStackFault(Addr vaddr
)
192 // Check if this is already on the stack and there's just no page there
194 if (vaddr
>= stack_min
&& vaddr
< stack_base
) {
195 allocateMem(roundDown(vaddr
, PageBytes
), PageBytes
);
199 // We've accessed the next page of the stack, so extend it to include
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.");
215 Process::serialize(CheckpointOut
&cp
) const
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
);
225 * Checkpoints for file descriptors currently do not work. Need to
226 * come back and fix them at a later date.
229 warn("Checkpoints for file descriptors currently do not work.");
231 for (int x
= 0; x
< fds
->getSize(); x
++)
232 (*fds
)[x
].serializeSection(cp
, csprintf("FDEntry%d", x
));
238 Process::unserialize(CheckpointIn
&cp
)
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
);
248 * Checkpoints for file descriptors currently do not work. Need to
249 * come back and fix them at a later date.
251 warn("Checkpoints for file descriptors currently do not work.");
253 for (int x
= 0; x
< fds
->getSize(); x
++)
254 (*fds
)[x
]->unserializeSection(cp
, csprintf("FDEntry%d", x
));
255 fds
->restoreFileOffsets();
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
264 Process::map(Addr vaddr
, Addr paddr
, int size
, bool cacheable
)
266 pTable
->map(vaddr
, paddr
, size
,
267 cacheable
? PageTableBase::Zero
: PageTableBase::Uncacheable
);
272 Process::syscall(int64_t callnum
, ThreadContext
*tc
)
276 SyscallDesc
*desc
= getDesc(callnum
);
278 fatal("Syscall %d out of range", callnum
);
280 desc
->doSyscall(callnum
, this, tc
);
284 Process::getSyscallArg(ThreadContext
*tc
, int &i
, int width
)
286 return getSyscallArg(tc
, i
);
290 Process::findDriver(std::string filename
)
292 for (EmulatedDriver
*d
: drivers
) {
293 if (d
->match(filename
))
301 Process::updateBias()
303 ObjectFile
*interp
= objFile
->getInterpreter();
305 if (!interp
|| !interp
->relocatable())
308 // Determine how large the interpreters footprint will be in the process
310 Addr interp_mapsize
= roundUp(interp
->mapSize(), TheISA::PageBytes
);
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
;
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
;
321 interp
->updateBias(ld_bias
);
325 Process::getInterpreter()
327 return objFile
->getInterpreter();
333 ObjectFile
*interp
= getInterpreter();
335 return interp
? interp
->bias() : objFile
->bias();
339 Process::getStartPC()
341 ObjectFile
*interp
= getInterpreter();
343 return interp
? interp
->entryPoint() : objFile
->entryPoint();
347 ProcessParams::create()
349 Process
*process
= NULL
;
351 // If not specified, set the executable parameter equal to the
352 // simulated system's zeroth command line parameter
353 if (executable
== "") {
357 ObjectFile
*obj_file
= createObjectFile(executable
);
358 if (obj_file
== NULL
) {
359 fatal("Can't load object file %s", executable
);
362 #if THE_ISA == ALPHA_ISA
363 if (obj_file
->getArch() != ObjectFile::Alpha
)
364 fatal("Object file architecture does not match compiled ISA (Alpha).");
366 switch (obj_file
->getOpSys()) {
367 case ObjectFile::UnknownOpSys
:
368 warn("Unknown operating system; assuming Linux.");
370 case ObjectFile::Linux
:
371 process
= new AlphaLinuxProcess(this, obj_file
);
375 fatal("Unknown/unsupported operating system.");
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.");
385 case ObjectFile::Linux
:
386 if (obj_file
->getArch() == ObjectFile::SPARC64
) {
387 process
= new Sparc64LinuxProcess(this, obj_file
);
389 process
= new Sparc32LinuxProcess(this, obj_file
);
393 case ObjectFile::Solaris
:
394 process
= new SparcSolarisProcess(this, obj_file
);
398 fatal("Unknown/unsupported operating system.");
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.");
408 case ObjectFile::Linux
:
409 if (obj_file
->getArch() == ObjectFile::X86_64
) {
410 process
= new X86_64LinuxProcess(this, obj_file
);
412 process
= new I386LinuxProcess(this, obj_file
);
417 fatal("Unknown/unsupported operating system.");
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.");
426 case ObjectFile::Linux
:
427 process
= new MipsLinuxProcess(this, obj_file
);
431 fatal("Unknown/unsupported operating system.");
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.");
442 case ObjectFile::Linux
:
443 if (arch
== ObjectFile::Arm64
) {
444 process
= new ArmLinuxProcess64(this, obj_file
,
445 obj_file
->getArch());
447 process
= new ArmLinuxProcess32(this, obj_file
,
448 obj_file
->getArch());
451 case ObjectFile::FreeBSD
:
452 if (arch
== ObjectFile::Arm64
) {
453 process
= new ArmFreebsdProcess64(this, obj_file
,
454 obj_file
->getArch());
456 process
= new ArmFreebsdProcess32(this, obj_file
,
457 obj_file
->getArch());
460 case ObjectFile::LinuxArmOABI
:
461 fatal("M5 does not support ARM OABI binaries. Please recompile with an"
464 fatal("Unknown/unsupported operating system.");
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.");
473 case ObjectFile::Linux
:
474 process
= new PowerLinuxProcess(this, obj_file
);
478 fatal("Unknown/unsupported operating system.");
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.");
487 case ObjectFile::Linux
:
488 process
= new RiscvLinuxProcess(this, obj_file
);
491 fatal("Unknown/unsupported operating system.");
494 #error "THE_ISA not set"
498 fatal("Unknown error creating process object.");
503 Process::fullPath(const std::string
&file_name
)
505 if (file_name
[0] == '/' || cwd
.empty())
508 std::string full
= cwd
;
510 if (cwd
[cwd
.size() - 1] != '/')
513 return full
+ file_name
;