process.cc revision 4434
1/*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 *          Steve Reinhardt
30 *          Ali Saidi
31 */
32
33#include <unistd.h>
34#include <fcntl.h>
35
36#include <string>
37
38#include "arch/remote_gdb.hh"
39#include "base/intmath.hh"
40#include "base/loader/object_file.hh"
41#include "base/loader/symtab.hh"
42#include "base/statistics.hh"
43#include "config/full_system.hh"
44#include "cpu/thread_context.hh"
45#include "mem/page_table.hh"
46#include "mem/physical.hh"
47#include "mem/translating_port.hh"
48#include "sim/builder.hh"
49#include "sim/process.hh"
50#include "sim/process_impl.hh"
51#include "sim/stats.hh"
52#include "sim/syscall_emul.hh"
53#include "sim/system.hh"
54
55#include "arch/isa_specific.hh"
56#if THE_ISA == ALPHA_ISA
57#include "arch/alpha/linux/process.hh"
58#include "arch/alpha/tru64/process.hh"
59#elif THE_ISA == SPARC_ISA
60#include "arch/sparc/linux/process.hh"
61#include "arch/sparc/solaris/process.hh"
62#elif THE_ISA == MIPS_ISA
63#include "arch/mips/linux/process.hh"
64#elif THE_ISA == X86_ISA
65#include "arch/x86/linux/process.hh"
66#else
67#error "THE_ISA not set"
68#endif
69
70
71using namespace std;
72using namespace TheISA;
73
74//
75// The purpose of this code is to fake the loader & syscall mechanism
76// when there's no OS: thus there's no resone to use it in FULL_SYSTEM
77// mode when we do have an OS
78//
79#if FULL_SYSTEM
80#error "process.cc not compatible with FULL_SYSTEM"
81#endif
82
83// current number of allocated processes
84int num_processes = 0;
85
86Process::Process(const string &nm,
87                 System *_system,
88                 int stdin_fd, 	// initial I/O descriptors
89                 int stdout_fd,
90                 int stderr_fd)
91    : SimObject(nm), system(_system)
92{
93    // initialize first 3 fds (stdin, stdout, stderr)
94    fd_map[STDIN_FILENO] = stdin_fd;
95    fd_map[STDOUT_FILENO] = stdout_fd;
96    fd_map[STDERR_FILENO] = stderr_fd;
97
98    // mark remaining fds as free
99    for (int i = 3; i <= MAX_FD; ++i) {
100        fd_map[i] = -1;
101    }
102
103    mmap_start = mmap_end = 0;
104    nxm_start = nxm_end = 0;
105    pTable = new PageTable(system);
106    // other parameters will be initialized when the program is loaded
107}
108
109
110void
111Process::regStats()
112{
113    using namespace Stats;
114
115    num_syscalls
116        .name(name() + ".PROG:num_syscalls")
117        .desc("Number of system calls")
118        ;
119}
120
121//
122// static helper functions
123//
124int
125Process::openInputFile(const string &filename)
126{
127    int fd = open(filename.c_str(), O_RDONLY);
128
129    if (fd == -1) {
130        perror(NULL);
131        cerr << "unable to open \"" << filename << "\" for reading\n";
132        fatal("can't open input file");
133    }
134
135    return fd;
136}
137
138
139int
140Process::openOutputFile(const string &filename)
141{
142    int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774);
143
144    if (fd == -1) {
145        perror(NULL);
146        cerr << "unable to open \"" << filename << "\" for writing\n";
147        fatal("can't open output file");
148    }
149
150    return fd;
151}
152
153
154int
155Process::registerThreadContext(ThreadContext *tc)
156{
157    // add to list
158    int myIndex = threadContexts.size();
159    threadContexts.push_back(tc);
160
161//    RemoteGDB *rgdb = new RemoteGDB(system, tc);
162//    GDBListener *gdbl = new GDBListener(rgdb, 7000 + myIndex);
163//    gdbl->listen();
164    //gdbl->accept();
165
166//    remoteGDB.push_back(rgdb);
167
168    // return CPU number to caller
169    return myIndex;
170}
171
172void
173Process::startup()
174{
175    if (threadContexts.empty())
176        fatal("Process %s is not associated with any CPUs!\n", name());
177
178    // first thread context for this process... initialize & enable
179    ThreadContext *tc = threadContexts[0];
180
181    // mark this context as active so it will start ticking.
182    tc->activate(0);
183
184    Port *mem_port;
185    mem_port = system->physmem->getPort("functional");
186    initVirtMem = new TranslatingPort("process init port", this,
187            TranslatingPort::Always);
188    mem_port->setPeer(initVirtMem);
189    initVirtMem->setPeer(mem_port);
190}
191
192void
193Process::replaceThreadContext(ThreadContext *tc, int tcIndex)
194{
195    if (tcIndex >= threadContexts.size()) {
196        panic("replaceThreadContext: bad tcIndex, %d >= %d\n",
197              tcIndex, threadContexts.size());
198    }
199
200    threadContexts[tcIndex] = tc;
201}
202
203// map simulator fd sim_fd to target fd tgt_fd
204void
205Process::dup_fd(int sim_fd, int tgt_fd)
206{
207    if (tgt_fd < 0 || tgt_fd > MAX_FD)
208        panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd);
209
210    fd_map[tgt_fd] = sim_fd;
211}
212
213
214// generate new target fd for sim_fd
215int
216Process::alloc_fd(int sim_fd)
217{
218    // in case open() returns an error, don't allocate a new fd
219    if (sim_fd == -1)
220        return -1;
221
222    // find first free target fd
223    for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) {
224        if (fd_map[free_fd] == -1) {
225            fd_map[free_fd] = sim_fd;
226            return free_fd;
227        }
228    }
229
230    panic("Process::alloc_fd: out of file descriptors!");
231}
232
233
234// free target fd (e.g., after close)
235void
236Process::free_fd(int tgt_fd)
237{
238    if (fd_map[tgt_fd] == -1)
239        warn("Process::free_fd: request to free unused fd %d", tgt_fd);
240
241    fd_map[tgt_fd] = -1;
242}
243
244
245// look up simulator fd for given target fd
246int
247Process::sim_fd(int tgt_fd)
248{
249    if (tgt_fd > MAX_FD)
250        return -1;
251
252    return fd_map[tgt_fd];
253}
254
255bool
256Process::checkAndAllocNextPage(Addr vaddr)
257{
258    // if this is an initial write we might not have
259    if (vaddr >= stack_min && vaddr < stack_base) {
260        pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize);
261        return true;
262    }
263
264    // We've accessed the next page of the stack, so extend the stack
265    // to cover it.
266    if(vaddr < stack_min && vaddr >= stack_min - TheISA::PageBytes)
267    {
268        stack_min -= TheISA::PageBytes;
269        if(stack_base - stack_min > 8*1024*1024)
270            fatal("Over max stack size for one thread\n");
271        pTable->allocate(stack_min, TheISA::PageBytes);
272        warn("Increasing stack size by one page.");
273        return true;
274    }
275    return false;
276}
277
278void
279Process::serialize(std::ostream &os)
280{
281    SERIALIZE_SCALAR(initialContextLoaded);
282    SERIALIZE_SCALAR(brk_point);
283    SERIALIZE_SCALAR(stack_base);
284    SERIALIZE_SCALAR(stack_size);
285    SERIALIZE_SCALAR(stack_min);
286    SERIALIZE_SCALAR(next_thread_stack_base);
287    SERIALIZE_SCALAR(mmap_start);
288    SERIALIZE_SCALAR(mmap_end);
289    SERIALIZE_SCALAR(nxm_start);
290    SERIALIZE_SCALAR(nxm_end);
291    SERIALIZE_ARRAY(fd_map, MAX_FD);
292
293    pTable->serialize(os);
294}
295
296void
297Process::unserialize(Checkpoint *cp, const std::string &section)
298{
299    UNSERIALIZE_SCALAR(initialContextLoaded);
300    UNSERIALIZE_SCALAR(brk_point);
301    UNSERIALIZE_SCALAR(stack_base);
302    UNSERIALIZE_SCALAR(stack_size);
303    UNSERIALIZE_SCALAR(stack_min);
304    UNSERIALIZE_SCALAR(next_thread_stack_base);
305    UNSERIALIZE_SCALAR(mmap_start);
306    UNSERIALIZE_SCALAR(mmap_end);
307    UNSERIALIZE_SCALAR(nxm_start);
308    UNSERIALIZE_SCALAR(nxm_end);
309    UNSERIALIZE_ARRAY(fd_map, MAX_FD);
310
311    pTable->unserialize(cp, section);
312}
313
314
315//
316// need to declare these here since there is no concrete Process type
317// that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call,
318// which is where these get declared for concrete types).
319//
320DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process)
321
322
323////////////////////////////////////////////////////////////////////////
324//
325// LiveProcess member definitions
326//
327////////////////////////////////////////////////////////////////////////
328
329
330LiveProcess::LiveProcess(const string &nm, ObjectFile *_objFile,
331                         System *_system,
332                         int stdin_fd, int stdout_fd, int stderr_fd,
333                         vector<string> &_argv, vector<string> &_envp,
334                         const string &_cwd,
335                         uint64_t _uid, uint64_t _euid,
336                         uint64_t _gid, uint64_t _egid,
337                         uint64_t _pid, uint64_t _ppid)
338    : Process(nm, _system, stdin_fd, stdout_fd, stderr_fd),
339      objFile(_objFile), argv(_argv), envp(_envp), cwd(_cwd)
340{
341    __uid = _uid;
342    __euid = _euid;
343    __gid = _gid;
344    __egid = _egid;
345    __pid = _pid;
346    __ppid = _ppid;
347
348    prog_fname = argv[0];
349
350    // load up symbols, if any... these may be used for debugging or
351    // profiling.
352    if (!debugSymbolTable) {
353        debugSymbolTable = new SymbolTable();
354        if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
355            !objFile->loadLocalSymbols(debugSymbolTable)) {
356            // didn't load any symbols
357            delete debugSymbolTable;
358            debugSymbolTable = NULL;
359        }
360    }
361}
362
363void
364LiveProcess::argsInit(int intSize, int pageSize)
365{
366    Process::startup();
367
368    // load object file into target memory
369    objFile->loadSections(initVirtMem);
370
371    // Calculate how much space we need for arg & env arrays.
372    int argv_array_size = intSize * (argv.size() + 1);
373    int envp_array_size = intSize * (envp.size() + 1);
374    int arg_data_size = 0;
375    for (int i = 0; i < argv.size(); ++i) {
376        arg_data_size += argv[i].size() + 1;
377    }
378    int env_data_size = 0;
379    for (int i = 0; i < envp.size(); ++i) {
380        env_data_size += envp[i].size() + 1;
381    }
382
383    int space_needed =
384        argv_array_size + envp_array_size + arg_data_size + env_data_size;
385    if (space_needed < 32*1024)
386        space_needed = 32*1024;
387
388    // set bottom of stack
389    stack_min = stack_base - space_needed;
390    // align it
391    stack_min = roundDown(stack_min, pageSize);
392    stack_size = stack_base - stack_min;
393    // map memory
394    pTable->allocate(stack_min, roundUp(stack_size, pageSize));
395
396    // map out initial stack contents
397    Addr argv_array_base = stack_min + intSize; // room for argc
398    Addr envp_array_base = argv_array_base + argv_array_size;
399    Addr arg_data_base = envp_array_base + envp_array_size;
400    Addr env_data_base = arg_data_base + arg_data_size;
401
402    // write contents to stack
403    uint64_t argc = argv.size();
404    if (intSize == 8)
405        argc = htog((uint64_t)argc);
406    else if (intSize == 4)
407        argc = htog((uint32_t)argc);
408    else
409        panic("Unknown int size");
410
411    initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize);
412
413    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
414    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
415
416    threadContexts[0]->setIntReg(ArgumentReg0, argc);
417    threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
418    threadContexts[0]->setIntReg(StackPointerReg, stack_min);
419
420    Addr prog_entry = objFile->entryPoint();
421    threadContexts[0]->setPC(prog_entry);
422    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
423
424#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc
425    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
426#endif
427
428    num_processes++;
429}
430
431void
432LiveProcess::syscall(int64_t callnum, ThreadContext *tc)
433{
434    num_syscalls++;
435
436    SyscallDesc *desc = getDesc(callnum);
437    if (desc == NULL)
438        fatal("Syscall %d out of range", callnum);
439
440    desc->doSyscall(callnum, this, tc);
441}
442
443LiveProcess *
444LiveProcess::create(const std::string &nm, System *system, int stdin_fd,
445                    int stdout_fd, int stderr_fd, std::string executable,
446                    std::vector<std::string> &argv,
447                    std::vector<std::string> &envp,
448                    const std::string &cwd,
449                    uint64_t _uid, uint64_t _euid,
450                    uint64_t _gid, uint64_t _egid,
451                    uint64_t _pid, uint64_t _ppid)
452{
453    LiveProcess *process = NULL;
454
455    ObjectFile *objFile = createObjectFile(executable);
456    if (objFile == NULL) {
457        fatal("Can't load object file %s", executable);
458    }
459
460    if (objFile->isDynamic())
461       fatal("Object file is a dynamic executable however only static "
462             "executables are supported!\n        Please recompile your "
463             "executable as a static binary and try again.\n");
464
465#if THE_ISA == ALPHA_ISA
466    if (objFile->getArch() != ObjectFile::Alpha)
467        fatal("Object file architecture does not match compiled ISA (Alpha).");
468    switch (objFile->getOpSys()) {
469      case ObjectFile::Tru64:
470        process = new AlphaTru64Process(nm, objFile, system,
471                                        stdin_fd, stdout_fd, stderr_fd,
472                                        argv, envp, cwd,
473                                        _uid, _euid, _gid, _egid, _pid, _ppid);
474        break;
475
476      case ObjectFile::Linux:
477        process = new AlphaLinuxProcess(nm, objFile, system,
478                                        stdin_fd, stdout_fd, stderr_fd,
479                                        argv, envp, cwd,
480                                        _uid, _euid, _gid, _egid, _pid, _ppid);
481        break;
482
483      default:
484        fatal("Unknown/unsupported operating system.");
485    }
486#elif THE_ISA == SPARC_ISA
487    if (objFile->getArch() != ObjectFile::SPARC64 && objFile->getArch() != ObjectFile::SPARC32)
488        fatal("Object file architecture does not match compiled ISA (SPARC).");
489    switch (objFile->getOpSys()) {
490      case ObjectFile::Linux:
491        if (objFile->getArch() == ObjectFile::SPARC64) {
492            process = new Sparc64LinuxProcess(nm, objFile, system,
493                                              stdin_fd, stdout_fd, stderr_fd,
494                                              argv, envp, cwd,
495                                              _uid, _euid, _gid,
496                                              _egid, _pid, _ppid);
497        } else {
498            process = new Sparc32LinuxProcess(nm, objFile, system,
499                                              stdin_fd, stdout_fd, stderr_fd,
500                                              argv, envp, cwd,
501                                              _uid, _euid, _gid,
502                                              _egid, _pid, _ppid);
503        }
504        break;
505
506
507      case ObjectFile::Solaris:
508        process = new SparcSolarisProcess(nm, objFile, system,
509                                        stdin_fd, stdout_fd, stderr_fd,
510                                        argv, envp, cwd,
511                                        _uid, _euid, _gid, _egid, _pid, _ppid);
512        break;
513      default:
514        fatal("Unknown/unsupported operating system.");
515    }
516#elif THE_ISA == X86_ISA
517    if (objFile->getArch() != ObjectFile::X86)
518        fatal("Object file architecture does not match compiled ISA (x86).");
519    switch (objFile->getOpSys()) {
520      case ObjectFile::Linux:
521        process = new X86LinuxProcess(nm, objFile, system,
522                                          stdin_fd, stdout_fd, stderr_fd,
523                                          argv, envp, cwd,
524                                          _uid, _euid, _gid,
525                                          _egid, _pid, _ppid);
526        break;
527      default:
528        fatal("Unknown/unsupported operating system.");
529    }
530#elif THE_ISA == MIPS_ISA
531    if (objFile->getArch() != ObjectFile::Mips)
532        fatal("Object file architecture does not match compiled ISA (MIPS).");
533    switch (objFile->getOpSys()) {
534      case ObjectFile::Linux:
535        process = new MipsLinuxProcess(nm, objFile, system,
536                                        stdin_fd, stdout_fd, stderr_fd,
537                                        argv, envp, cwd,
538                                        _uid, _euid, _gid, _egid, _pid, _ppid);
539        break;
540
541      default:
542        fatal("Unknown/unsupported operating system.");
543    }
544#else
545#error "THE_ISA not set"
546#endif
547
548
549    if (process == NULL)
550        fatal("Unknown error creating process object.");
551    return process;
552}
553
554
555BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess)
556
557    VectorParam<string> cmd;
558    Param<string> executable;
559    Param<string> input;
560    Param<string> output;
561    VectorParam<string> env;
562    Param<string> cwd;
563    SimObjectParam<System *> system;
564    Param<uint64_t> uid;
565    Param<uint64_t> euid;
566    Param<uint64_t> gid;
567    Param<uint64_t> egid;
568    Param<uint64_t> pid;
569    Param<uint64_t> ppid;
570
571END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess)
572
573
574BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess)
575
576    INIT_PARAM(cmd, "command line (executable plus arguments)"),
577    INIT_PARAM(executable, "executable (overrides cmd[0] if set)"),
578    INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"),
579    INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"),
580    INIT_PARAM(env, "environment settings"),
581    INIT_PARAM(cwd, "current working directory"),
582    INIT_PARAM(system, "system"),
583    INIT_PARAM(uid, "user id"),
584    INIT_PARAM(euid, "effective user id"),
585    INIT_PARAM(gid, "group id"),
586    INIT_PARAM(egid, "effective group id"),
587    INIT_PARAM(pid, "process id"),
588    INIT_PARAM(ppid, "parent process id")
589
590END_INIT_SIM_OBJECT_PARAMS(LiveProcess)
591
592
593CREATE_SIM_OBJECT(LiveProcess)
594{
595    string in = input;
596    string out = output;
597
598    // initialize file descriptors to default: same as simulator
599    int stdin_fd, stdout_fd, stderr_fd;
600
601    if (in == "stdin" || in == "cin")
602        stdin_fd = STDIN_FILENO;
603    else
604        stdin_fd = Process::openInputFile(input);
605
606    if (out == "stdout" || out == "cout")
607        stdout_fd = STDOUT_FILENO;
608    else if (out == "stderr" || out == "cerr")
609        stdout_fd = STDERR_FILENO;
610    else
611        stdout_fd = Process::openOutputFile(out);
612
613    stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO;
614
615    return LiveProcess::create(getInstanceName(), system,
616                               stdin_fd, stdout_fd, stderr_fd,
617                               (string)executable == "" ? cmd[0] : executable,
618                               cmd, env, cwd,
619                               uid, euid, gid, egid, pid, ppid);
620}
621
622
623REGISTER_SIM_OBJECT("LiveProcess", LiveProcess)
624