process.cc revision 11294:a368064a2ab5
1/*
2 * Copyright (c) 2014 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 */
45
46#include <fcntl.h>
47#include <unistd.h>
48
49#include <cstdio>
50#include <map>
51#include <string>
52
53#include "base/loader/object_file.hh"
54#include "base/loader/symtab.hh"
55#include "base/intmath.hh"
56#include "base/statistics.hh"
57#include "config/the_isa.hh"
58#include "cpu/thread_context.hh"
59#include "mem/page_table.hh"
60#include "mem/multi_level_page_table.hh"
61#include "mem/se_translating_port_proxy.hh"
62#include "params/LiveProcess.hh"
63#include "params/Process.hh"
64#include "sim/debug.hh"
65#include "sim/process.hh"
66#include "sim/process_impl.hh"
67#include "sim/stats.hh"
68#include "sim/syscall_emul.hh"
69#include "sim/system.hh"
70
71#if THE_ISA == ALPHA_ISA
72#include "arch/alpha/linux/process.hh"
73#include "arch/alpha/tru64/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#else
87#error "THE_ISA not set"
88#endif
89
90
91using namespace std;
92using namespace TheISA;
93
94// current number of allocated processes
95int num_processes = 0;
96
97template<class IntType>
98
99AuxVector<IntType>::AuxVector(IntType type, IntType val)
100{
101    a_type = TheISA::htog(type);
102    a_val = TheISA::htog(val);
103}
104
105template struct AuxVector<uint32_t>;
106template struct AuxVector<uint64_t>;
107
108static int
109openFile(const string& filename, int flags, mode_t mode)
110{
111    int sim_fd = open(filename.c_str(), flags, mode);
112    if (sim_fd != -1)
113        return sim_fd;
114    fatal("Unable to open %s with mode %O", filename, mode);
115}
116
117static int
118openInputFile(const string &filename)
119{
120    return openFile(filename, O_RDONLY, 0);
121}
122
123static int
124openOutputFile(const string &filename)
125{
126    return openFile(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);
127}
128
129Process::Process(ProcessParams * params)
130    : SimObject(params), system(params->system),
131      brk_point(0), stack_base(0), stack_size(0), stack_min(0),
132      max_stack_size(params->max_stack_size),
133      next_thread_stack_base(0),
134      M5_pid(system->allocatePID()),
135      useArchPT(params->useArchPT),
136      kvmInSE(params->kvmInSE),
137      pTable(useArchPT ?
138        static_cast<PageTableBase *>(new ArchPageTable(name(), M5_pid, system)) :
139        static_cast<PageTableBase *>(new FuncPageTable(name(), M5_pid)) ),
140      initVirtMem(system->getSystemPort(), this,
141                  SETranslatingPortProxy::Always),
142      fd_array(make_shared<array<FDEntry, NUM_FDS>>()),
143      imap {{"",       -1},
144            {"cin",    STDIN_FILENO},
145            {"stdin",  STDIN_FILENO}},
146      oemap{{"",       -1},
147            {"cout",   STDOUT_FILENO},
148            {"stdout", STDOUT_FILENO},
149            {"cerr",   STDERR_FILENO},
150            {"stderr", STDERR_FILENO}}
151{
152    int sim_fd;
153    std::map<string,int>::iterator it;
154
155    // Search through the input options and set fd if match is found;
156    // otherwise, open an input file and seek to location.
157    FDEntry *fde_stdin = getFDEntry(STDIN_FILENO);
158    if ((it = imap.find(params->input)) != imap.end())
159        sim_fd = it->second;
160    else
161        sim_fd = openInputFile(params->input);
162    fde_stdin->set(sim_fd, params->input, O_RDONLY, -1, false);
163
164    // Search through the output/error options and set fd if match is found;
165    // otherwise, open an output file and seek to location.
166    FDEntry *fde_stdout = getFDEntry(STDOUT_FILENO);
167    if ((it = oemap.find(params->output)) != oemap.end())
168        sim_fd = it->second;
169    else
170        sim_fd = openOutputFile(params->output);
171    fde_stdout->set(sim_fd, params->output, O_WRONLY | O_CREAT | O_TRUNC,
172                    0664, false);
173
174    FDEntry *fde_stderr = getFDEntry(STDERR_FILENO);
175    if (params->output == params->errout)
176        // Reuse the same file descriptor if these match.
177        sim_fd = fde_stdout->fd;
178    else if ((it = oemap.find(params->errout)) != oemap.end())
179        sim_fd = it->second;
180    else
181        sim_fd = openOutputFile(params->errout);
182    fde_stderr->set(sim_fd, params->errout, O_WRONLY | O_CREAT | O_TRUNC,
183                    0664, false);
184
185    mmap_start = mmap_end = 0;
186    nxm_start = nxm_end = 0;
187    // other parameters will be initialized when the program is loaded
188}
189
190
191void
192Process::regStats()
193{
194    using namespace Stats;
195
196    num_syscalls
197        .name(name() + ".num_syscalls")
198        .desc("Number of system calls")
199        ;
200}
201
202void
203Process::inheritFDArray(Process *p)
204{
205    fd_array = p->fd_array;
206}
207
208ThreadContext *
209Process::findFreeContext()
210{
211    for (int id : contextIds) {
212        ThreadContext *tc = system->getThreadContext(id);
213        if (tc->status() == ThreadContext::Halted)
214            return tc;
215    }
216    return NULL;
217}
218
219void
220Process::initState()
221{
222    if (contextIds.empty())
223        fatal("Process %s is not associated with any HW contexts!\n", name());
224
225    // first thread context for this process... initialize & enable
226    ThreadContext *tc = system->getThreadContext(contextIds[0]);
227
228    // mark this context as active so it will start ticking.
229    tc->activate();
230
231    pTable->initState(tc);
232}
233
234DrainState
235Process::drain()
236{
237    findFileOffsets();
238    return DrainState::Drained;
239}
240
241int
242Process::allocFD(int sim_fd, const string& filename, int flags, int mode,
243                 bool pipe)
244{
245    for (int free_fd = 0; free_fd < fd_array->size(); free_fd++) {
246        FDEntry *fde = getFDEntry(free_fd);
247        if (fde->isFree()) {
248            fde->set(sim_fd, filename, flags, mode, pipe);
249            return free_fd;
250        }
251    }
252
253    fatal("Out of target file descriptors");
254}
255
256void
257Process::resetFDEntry(int tgt_fd)
258{
259    FDEntry *fde = getFDEntry(tgt_fd);
260    assert(fde->fd > -1);
261
262    fde->reset();
263}
264
265int
266Process::getSimFD(int tgt_fd)
267{
268    FDEntry *entry = getFDEntry(tgt_fd);
269    return entry ? entry->fd : -1;
270}
271
272FDEntry *
273Process::getFDEntry(int tgt_fd)
274{
275    assert(0 <= tgt_fd && tgt_fd < fd_array->size());
276    return &(*fd_array)[tgt_fd];
277}
278
279int
280Process::getTgtFD(int sim_fd)
281{
282    for (int index = 0; index < fd_array->size(); index++)
283        if ((*fd_array)[index].fd == sim_fd)
284            return index;
285    return -1;
286}
287
288void
289Process::allocateMem(Addr vaddr, int64_t size, bool clobber)
290{
291    int npages = divCeil(size, (int64_t)PageBytes);
292    Addr paddr = system->allocPhysPages(npages);
293    pTable->map(vaddr, paddr, size,
294                clobber ? PageTableBase::Clobber : PageTableBase::Zero);
295}
296
297bool
298Process::fixupStackFault(Addr vaddr)
299{
300    // Check if this is already on the stack and there's just no page there
301    // yet.
302    if (vaddr >= stack_min && vaddr < stack_base) {
303        allocateMem(roundDown(vaddr, PageBytes), PageBytes);
304        return true;
305    }
306
307    // We've accessed the next page of the stack, so extend it to include
308    // this address.
309    if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
310        while (vaddr < stack_min) {
311            stack_min -= TheISA::PageBytes;
312            if (stack_base - stack_min > max_stack_size)
313                fatal("Maximum stack size exceeded\n");
314            allocateMem(stack_min, TheISA::PageBytes);
315            inform("Increasing stack size by one page.");
316        };
317        return true;
318    }
319    return false;
320}
321
322void
323Process::fixFileOffsets()
324{
325    auto seek = [] (FDEntry *fde)
326    {
327        if (lseek(fde->fd, fde->fileOffset, SEEK_SET) < 0)
328            fatal("Unable to see to location in %s", fde->filename);
329    };
330
331    std::map<string,int>::iterator it;
332
333    // Search through the input options and set fd if match is found;
334    // otherwise, open an input file and seek to location.
335    FDEntry *fde_stdin = getFDEntry(STDIN_FILENO);
336    if ((it = imap.find(fde_stdin->filename)) != imap.end()) {
337        fde_stdin->fd = it->second;
338    } else {
339        fde_stdin->fd = openInputFile(fde_stdin->filename);
340        seek(fde_stdin);
341    }
342
343    // Search through the output/error options and set fd if match is found;
344    // otherwise, open an output file and seek to location.
345    FDEntry *fde_stdout = getFDEntry(STDOUT_FILENO);
346    if ((it = oemap.find(fde_stdout->filename)) != oemap.end()) {
347        fde_stdout->fd = it->second;
348    } else {
349        fde_stdout->fd = openOutputFile(fde_stdout->filename);
350        seek(fde_stdout);
351    }
352
353    FDEntry *fde_stderr = getFDEntry(STDERR_FILENO);
354    if (fde_stdout->filename == fde_stderr->filename) {
355        // Reuse the same file descriptor if these match.
356        fde_stderr->fd = fde_stdout->fd;
357    } else if ((it = oemap.find(fde_stderr->filename)) != oemap.end()) {
358        fde_stderr->fd = it->second;
359    } else {
360        fde_stderr->fd = openOutputFile(fde_stderr->filename);
361        seek(fde_stderr);
362    }
363
364    for (int tgt_fd = 3; tgt_fd < fd_array->size(); tgt_fd++) {
365        FDEntry *fde = getFDEntry(tgt_fd);
366        if (fde->fd == -1)
367            continue;
368
369        if (fde->isPipe) {
370            if (fde->filename == "PIPE-WRITE")
371                continue;
372            assert(fde->filename == "PIPE-READ");
373
374            int fds[2];
375            if (pipe(fds) < 0)
376                fatal("Unable to create new pipe");
377
378            fde->fd = fds[0];
379
380            FDEntry *fde_write = getFDEntry(fde->readPipeSource);
381            assert(
382                    fde_write->filename == "PIPE-WRITE");
383            fde_write->fd = fds[1];
384        } else {
385            fde->fd = openFile(fde->filename.c_str(), fde->flags, fde->mode);
386            seek(fde);
387        }
388    }
389}
390
391void
392Process::findFileOffsets()
393{
394    for (auto& fde : *fd_array) {
395        if (fde.fd != -1)
396            fde.fileOffset = lseek(fde.fd, 0, SEEK_CUR);
397    }
398}
399
400void
401Process::setReadPipeSource(int read_pipe_fd, int source_fd)
402{
403    FDEntry *fde = getFDEntry(read_pipe_fd);
404    assert(source_fd >= -1);
405    fde->readPipeSource = source_fd;
406}
407
408void
409Process::serialize(CheckpointOut &cp) const
410{
411    SERIALIZE_SCALAR(brk_point);
412    SERIALIZE_SCALAR(stack_base);
413    SERIALIZE_SCALAR(stack_size);
414    SERIALIZE_SCALAR(stack_min);
415    SERIALIZE_SCALAR(next_thread_stack_base);
416    SERIALIZE_SCALAR(mmap_start);
417    SERIALIZE_SCALAR(mmap_end);
418    SERIALIZE_SCALAR(nxm_start);
419    SERIALIZE_SCALAR(nxm_end);
420    pTable->serialize(cp);
421    for (int x = 0; x < fd_array->size(); x++) {
422        (*fd_array)[x].serializeSection(cp, csprintf("FDEntry%d", x));
423    }
424    SERIALIZE_SCALAR(M5_pid);
425
426}
427
428void
429Process::unserialize(CheckpointIn &cp)
430{
431    UNSERIALIZE_SCALAR(brk_point);
432    UNSERIALIZE_SCALAR(stack_base);
433    UNSERIALIZE_SCALAR(stack_size);
434    UNSERIALIZE_SCALAR(stack_min);
435    UNSERIALIZE_SCALAR(next_thread_stack_base);
436    UNSERIALIZE_SCALAR(mmap_start);
437    UNSERIALIZE_SCALAR(mmap_end);
438    UNSERIALIZE_SCALAR(nxm_start);
439    UNSERIALIZE_SCALAR(nxm_end);
440    pTable->unserialize(cp);
441    for (int x = 0; x < fd_array->size(); x++) {
442        FDEntry *fde = getFDEntry(x);
443        fde->unserializeSection(cp, csprintf("FDEntry%d", x));
444    }
445    fixFileOffsets();
446    UNSERIALIZE_OPT_SCALAR(M5_pid);
447    // The above returns a bool so that you could do something if you don't
448    // find the param in the checkpoint if you wanted to, like set a default
449    // but in this case we'll just stick with the instantiated value if not
450    // found.
451}
452
453
454bool
455Process::map(Addr vaddr, Addr paddr, int size, bool cacheable)
456{
457    pTable->map(vaddr, paddr, size,
458                cacheable ? PageTableBase::Zero : PageTableBase::Uncacheable);
459    return true;
460}
461
462
463////////////////////////////////////////////////////////////////////////
464//
465// LiveProcess member definitions
466//
467////////////////////////////////////////////////////////////////////////
468
469
470LiveProcess::LiveProcess(LiveProcessParams *params, ObjectFile *_objFile)
471    : Process(params), objFile(_objFile),
472      argv(params->cmd), envp(params->env), cwd(params->cwd),
473      executable(params->executable),
474      __uid(params->uid), __euid(params->euid),
475      __gid(params->gid), __egid(params->egid),
476      __pid(params->pid), __ppid(params->ppid),
477      drivers(params->drivers)
478{
479
480    // load up symbols, if any... these may be used for debugging or
481    // profiling.
482    if (!debugSymbolTable) {
483        debugSymbolTable = new SymbolTable();
484        if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
485            !objFile->loadLocalSymbols(debugSymbolTable) ||
486            !objFile->loadWeakSymbols(debugSymbolTable)) {
487            // didn't load any symbols
488            delete debugSymbolTable;
489            debugSymbolTable = NULL;
490        }
491    }
492}
493
494void
495LiveProcess::syscall(int64_t callnum, ThreadContext *tc)
496{
497    num_syscalls++;
498
499    SyscallDesc *desc = getDesc(callnum);
500    if (desc == NULL)
501        fatal("Syscall %d out of range", callnum);
502
503    desc->doSyscall(callnum, this, tc);
504}
505
506IntReg
507LiveProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
508{
509    return getSyscallArg(tc, i);
510}
511
512
513EmulatedDriver *
514LiveProcess::findDriver(std::string filename)
515{
516    for (EmulatedDriver *d : drivers) {
517        if (d->match(filename))
518            return d;
519    }
520
521    return NULL;
522}
523
524
525LiveProcess *
526LiveProcess::create(LiveProcessParams * params)
527{
528    LiveProcess *process = NULL;
529
530    // If not specified, set the executable parameter equal to the
531    // simulated system's zeroth command line parameter
532    if (params->executable == "") {
533        params->executable = params->cmd[0];
534    }
535
536    ObjectFile *objFile = createObjectFile(params->executable);
537    if (objFile == NULL) {
538        fatal("Can't load object file %s", params->executable);
539    }
540
541    if (objFile->isDynamic())
542       fatal("Object file is a dynamic executable however only static "
543             "executables are supported!\n       Please recompile your "
544             "executable as a static binary and try again.\n");
545
546#if THE_ISA == ALPHA_ISA
547    if (objFile->getArch() != ObjectFile::Alpha)
548        fatal("Object file architecture does not match compiled ISA (Alpha).");
549
550    switch (objFile->getOpSys()) {
551      case ObjectFile::Tru64:
552        process = new AlphaTru64Process(params, objFile);
553        break;
554
555      case ObjectFile::UnknownOpSys:
556        warn("Unknown operating system; assuming Linux.");
557        // fall through
558      case ObjectFile::Linux:
559        process = new AlphaLinuxProcess(params, objFile);
560        break;
561
562      default:
563        fatal("Unknown/unsupported operating system.");
564    }
565#elif THE_ISA == SPARC_ISA
566    if (objFile->getArch() != ObjectFile::SPARC64 &&
567        objFile->getArch() != ObjectFile::SPARC32)
568        fatal("Object file architecture does not match compiled ISA (SPARC).");
569    switch (objFile->getOpSys()) {
570      case ObjectFile::UnknownOpSys:
571        warn("Unknown operating system; assuming Linux.");
572        // fall through
573      case ObjectFile::Linux:
574        if (objFile->getArch() == ObjectFile::SPARC64) {
575            process = new Sparc64LinuxProcess(params, objFile);
576        } else {
577            process = new Sparc32LinuxProcess(params, objFile);
578        }
579        break;
580
581
582      case ObjectFile::Solaris:
583        process = new SparcSolarisProcess(params, objFile);
584        break;
585
586      default:
587        fatal("Unknown/unsupported operating system.");
588    }
589#elif THE_ISA == X86_ISA
590    if (objFile->getArch() != ObjectFile::X86_64 &&
591        objFile->getArch() != ObjectFile::I386)
592        fatal("Object file architecture does not match compiled ISA (x86).");
593    switch (objFile->getOpSys()) {
594      case ObjectFile::UnknownOpSys:
595        warn("Unknown operating system; assuming Linux.");
596        // fall through
597      case ObjectFile::Linux:
598        if (objFile->getArch() == ObjectFile::X86_64) {
599            process = new X86_64LinuxProcess(params, objFile);
600        } else {
601            process = new I386LinuxProcess(params, objFile);
602        }
603        break;
604
605      default:
606        fatal("Unknown/unsupported operating system.");
607    }
608#elif THE_ISA == MIPS_ISA
609    if (objFile->getArch() != ObjectFile::Mips)
610        fatal("Object file architecture does not match compiled ISA (MIPS).");
611    switch (objFile->getOpSys()) {
612      case ObjectFile::UnknownOpSys:
613        warn("Unknown operating system; assuming Linux.");
614        // fall through
615      case ObjectFile::Linux:
616        process = new MipsLinuxProcess(params, objFile);
617        break;
618
619      default:
620        fatal("Unknown/unsupported operating system.");
621    }
622#elif THE_ISA == ARM_ISA
623    ObjectFile::Arch arch = objFile->getArch();
624    if (arch != ObjectFile::Arm && arch != ObjectFile::Thumb &&
625        arch != ObjectFile::Arm64)
626        fatal("Object file architecture does not match compiled ISA (ARM).");
627    switch (objFile->getOpSys()) {
628      case ObjectFile::UnknownOpSys:
629        warn("Unknown operating system; assuming Linux.");
630        // fall through
631      case ObjectFile::Linux:
632        if (arch == ObjectFile::Arm64) {
633            process = new ArmLinuxProcess64(params, objFile,
634                                            objFile->getArch());
635        } else {
636            process = new ArmLinuxProcess32(params, objFile,
637                                            objFile->getArch());
638        }
639        break;
640      case ObjectFile::FreeBSD:
641        if (arch == ObjectFile::Arm64) {
642            process = new ArmFreebsdProcess64(params, objFile,
643                                              objFile->getArch());
644        } else {
645            process = new ArmFreebsdProcess32(params, objFile,
646                                              objFile->getArch());
647        }
648        break;
649      case ObjectFile::LinuxArmOABI:
650        fatal("M5 does not support ARM OABI binaries. Please recompile with an"
651              " EABI compiler.");
652      default:
653        fatal("Unknown/unsupported operating system.");
654    }
655#elif THE_ISA == POWER_ISA
656    if (objFile->getArch() != ObjectFile::Power)
657        fatal("Object file architecture does not match compiled ISA (Power).");
658    switch (objFile->getOpSys()) {
659      case ObjectFile::UnknownOpSys:
660        warn("Unknown operating system; assuming Linux.");
661        // fall through
662      case ObjectFile::Linux:
663        process = new PowerLinuxProcess(params, objFile);
664        break;
665
666      default:
667        fatal("Unknown/unsupported operating system.");
668    }
669#else
670#error "THE_ISA not set"
671#endif
672
673    if (process == NULL)
674        fatal("Unknown error creating process object.");
675    return process;
676}
677
678LiveProcess *
679LiveProcessParams::create()
680{
681    return LiveProcess::create(this);
682}
683