sparc,sim: Remove special handling of SPARC in the clone system call.
[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
42 #include "sim/process.hh"
43
44 #include <fcntl.h>
45 #include <unistd.h>
46
47 #include <array>
48 #include <climits>
49 #include <csignal>
50 #include <map>
51 #include <string>
52 #include <vector>
53
54 #include "base/intmath.hh"
55 #include "base/loader/object_file.hh"
56 #include "base/loader/symtab.hh"
57 #include "base/statistics.hh"
58 #include "config/the_isa.hh"
59 #include "cpu/thread_context.hh"
60 #include "mem/page_table.hh"
61 #include "mem/se_translating_port_proxy.hh"
62 #include "params/Process.hh"
63 #include "sim/emul_driver.hh"
64 #include "sim/fd_array.hh"
65 #include "sim/fd_entry.hh"
66 #include "sim/redirect_path.hh"
67 #include "sim/syscall_desc.hh"
68 #include "sim/system.hh"
69
70 using namespace std;
71 using namespace TheISA;
72
73 namespace
74 {
75
76 typedef std::vector<Process::Loader *> LoaderList;
77
78 LoaderList &
79 process_loaders()
80 {
81 static LoaderList loaders;
82 return loaders;
83 }
84
85 } // anonymous namespace
86
87 Process::Loader::Loader()
88 {
89 process_loaders().emplace_back(this);
90 }
91
92 Process *
93 Process::tryLoaders(ProcessParams *params, ::Loader::ObjectFile *obj_file)
94 {
95 for (auto &loader: process_loaders()) {
96 Process *p = loader->load(params, obj_file);
97 if (p)
98 return p;
99 }
100
101 return nullptr;
102 }
103
104 static std::string
105 normalize(std::string& directory)
106 {
107 if (directory.back() != '/')
108 directory += '/';
109 return directory;
110 }
111
112 Process::Process(ProcessParams *params, EmulationPageTable *pTable,
113 ::Loader::ObjectFile *obj_file)
114 : SimObject(params), system(params->system),
115 useArchPT(params->useArchPT),
116 kvmInSE(params->kvmInSE),
117 useForClone(false),
118 pTable(pTable),
119 objFile(obj_file),
120 argv(params->cmd), envp(params->env),
121 executable(params->executable),
122 tgtCwd(normalize(params->cwd)),
123 hostCwd(checkPathRedirect(tgtCwd)),
124 release(params->release),
125 _uid(params->uid), _euid(params->euid),
126 _gid(params->gid), _egid(params->egid),
127 _pid(params->pid), _ppid(params->ppid),
128 _pgid(params->pgid), drivers(params->drivers),
129 fds(make_shared<FDArray>(params->input, params->output, params->errout)),
130 childClearTID(0)
131 {
132 if (_pid >= System::maxPID)
133 fatal("_pid is too large: %d", _pid);
134
135 auto ret_pair = system->PIDs.emplace(_pid);
136 if (!ret_pair.second)
137 fatal("_pid %d is already used", _pid);
138
139 /**
140 * Linux bundles together processes into this concept called a thread
141 * group. The thread group is responsible for recording which processes
142 * behave as threads within a process context. The thread group leader
143 * is the process who's tgid is equal to its pid. Other processes which
144 * belong to the thread group, but do not lead the thread group, are
145 * treated as child threads. These threads are created by the clone system
146 * call with options specified to create threads (differing from the
147 * options used to implement a fork). By default, set up the tgid/pid
148 * with a new, equivalent value. If CLONE_THREAD is specified, patch
149 * the tgid value with the old process' value.
150 */
151 _tgid = params->pid;
152
153 exitGroup = new bool();
154 sigchld = new bool();
155
156 image = objFile->buildImage();
157
158 if (::Loader::debugSymbolTable.empty())
159 ::Loader::debugSymbolTable = objFile->symtab();
160 }
161
162 void
163 Process::clone(ThreadContext *otc, ThreadContext *ntc,
164 Process *np, RegVal flags)
165 {
166 #ifndef CLONE_VM
167 #define CLONE_VM 0
168 #endif
169 #ifndef CLONE_FILES
170 #define CLONE_FILES 0
171 #endif
172 #ifndef CLONE_THREAD
173 #define CLONE_THREAD 0
174 #endif
175 if (CLONE_VM & flags) {
176 /**
177 * Share the process memory address space between the new process
178 * and the old process. Changes in one will be visible in the other
179 * due to the pointer use.
180 */
181 delete np->pTable;
182 np->pTable = pTable;
183
184 np->memState = memState;
185 } else {
186 /**
187 * Duplicate the process memory address space. The state needs to be
188 * copied over (rather than using pointers to share everything).
189 */
190 typedef std::vector<pair<Addr,Addr>> MapVec;
191 MapVec mappings;
192 pTable->getMappings(&mappings);
193
194 for (auto map : mappings) {
195 Addr paddr, vaddr = map.first;
196 bool alloc_page = !(np->pTable->translate(vaddr, paddr));
197 np->replicatePage(vaddr, paddr, otc, ntc, alloc_page);
198 }
199
200 *np->memState = *memState;
201 }
202
203 if (CLONE_FILES & flags) {
204 /**
205 * The parent and child file descriptors are shared because the
206 * two FDArray pointers are pointing to the same FDArray. Opening
207 * and closing file descriptors will be visible to both processes.
208 */
209 np->fds = fds;
210 } else {
211 /**
212 * Copy the file descriptors from the old process into the new
213 * child process. The file descriptors entry can be opened and
214 * closed independently of the other process being considered. The
215 * host file descriptors are also dup'd so that the flags for the
216 * host file descriptor is independent of the other process.
217 */
218 std::shared_ptr<FDArray> nfds = np->fds;
219 for (int tgt_fd = 0; tgt_fd < fds->getSize(); tgt_fd++) {
220 std::shared_ptr<FDEntry> this_fde = (*fds)[tgt_fd];
221 if (!this_fde) {
222 nfds->setFDEntry(tgt_fd, nullptr);
223 continue;
224 }
225 nfds->setFDEntry(tgt_fd, this_fde->clone());
226
227 auto this_hbfd = std::dynamic_pointer_cast<HBFDEntry>(this_fde);
228 if (!this_hbfd)
229 continue;
230
231 int this_sim_fd = this_hbfd->getSimFD();
232 if (this_sim_fd <= 2)
233 continue;
234
235 int np_sim_fd = dup(this_sim_fd);
236 assert(np_sim_fd != -1);
237
238 auto nhbfd = std::dynamic_pointer_cast<HBFDEntry>((*nfds)[tgt_fd]);
239 nhbfd->setSimFD(np_sim_fd);
240 }
241 }
242
243 if (CLONE_THREAD & flags) {
244 np->_tgid = _tgid;
245 delete np->exitGroup;
246 np->exitGroup = exitGroup;
247 }
248
249 np->argv.insert(np->argv.end(), argv.begin(), argv.end());
250 np->envp.insert(np->envp.end(), envp.begin(), envp.end());
251 }
252
253 void
254 Process::regStats()
255 {
256 SimObject::regStats();
257
258 using namespace Stats;
259
260 numSyscalls
261 .name(name() + ".numSyscalls")
262 .desc("Number of system calls")
263 ;
264 }
265
266 void
267 Process::revokeThreadContext(int context_id)
268 {
269 std::vector<ContextID>::iterator it;
270 for (it = contextIds.begin(); it != contextIds.end(); it++) {
271 if (*it == context_id) {
272 contextIds.erase(it);
273 return;
274 }
275 }
276 warn("Unable to find thread context to revoke");
277 }
278
279 void
280 Process::init()
281 {
282 // Patch the ld_bias for dynamic executables.
283 updateBias();
284
285 if (objFile->getInterpreter())
286 interpImage = objFile->getInterpreter()->buildImage();
287 }
288
289 void
290 Process::initState()
291 {
292 if (contextIds.empty())
293 fatal("Process %s is not associated with any HW contexts!\n", name());
294
295 // first thread context for this process... initialize & enable
296 ThreadContext *tc = system->threads[contextIds[0]];
297
298 // mark this context as active so it will start ticking.
299 tc->activate();
300
301 pTable->initState();
302
303 initVirtMem.reset(new SETranslatingPortProxy(
304 tc, SETranslatingPortProxy::Always));
305
306 // load object file into target memory
307 image.write(*initVirtMem);
308 interpImage.write(*initVirtMem);
309 }
310
311 DrainState
312 Process::drain()
313 {
314 fds->updateFileOffsets();
315 return DrainState::Drained;
316 }
317
318 void
319 Process::allocateMem(Addr vaddr, int64_t size, bool clobber)
320 {
321 int npages = divCeil(size, (int64_t)system->getPageBytes());
322 Addr paddr = system->allocPhysPages(npages);
323 pTable->map(vaddr, paddr, size,
324 clobber ? EmulationPageTable::Clobber :
325 EmulationPageTable::MappingFlags(0));
326 }
327
328 void
329 Process::replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
330 ThreadContext *new_tc, bool allocate_page)
331 {
332 if (allocate_page)
333 new_paddr = system->allocPhysPages(1);
334
335 // Read from old physical page.
336 uint8_t *buf_p = new uint8_t[system->getPageBytes()];
337 old_tc->getVirtProxy().readBlob(vaddr, buf_p, system->getPageBytes());
338
339 // Create new mapping in process address space by clobbering existing
340 // mapping (if any existed) and then write to the new physical page.
341 bool clobber = true;
342 pTable->map(vaddr, new_paddr, system->getPageBytes(), clobber);
343 new_tc->getVirtProxy().writeBlob(vaddr, buf_p, system->getPageBytes());
344 delete[] buf_p;
345 }
346
347 bool
348 Process::fixupFault(Addr vaddr)
349 {
350 return memState->fixupFault(vaddr);
351 }
352
353 void
354 Process::serialize(CheckpointOut &cp) const
355 {
356 memState->serialize(cp);
357 pTable->serialize(cp);
358 /**
359 * Checkpoints for file descriptors currently do not work. Need to
360 * come back and fix them at a later date.
361 */
362
363 warn("Checkpoints for file descriptors currently do not work.");
364 }
365
366 void
367 Process::unserialize(CheckpointIn &cp)
368 {
369 memState->unserialize(cp);
370 pTable->unserialize(cp);
371 /**
372 * Checkpoints for file descriptors currently do not work. Need to
373 * come back and fix them at a later date.
374 */
375 warn("Checkpoints for file descriptors currently do not work.");
376 // The above returns a bool so that you could do something if you don't
377 // find the param in the checkpoint if you wanted to, like set a default
378 // but in this case we'll just stick with the instantiated value if not
379 // found.
380 }
381
382 bool
383 Process::map(Addr vaddr, Addr paddr, int size, bool cacheable)
384 {
385 pTable->map(vaddr, paddr, size,
386 cacheable ? EmulationPageTable::MappingFlags(0) :
387 EmulationPageTable::Uncacheable);
388 return true;
389 }
390
391 EmulatedDriver *
392 Process::findDriver(std::string filename)
393 {
394 for (EmulatedDriver *d : drivers) {
395 if (d->match(filename))
396 return d;
397 }
398
399 return nullptr;
400 }
401
402 std::string
403 Process::checkPathRedirect(const std::string &filename)
404 {
405 // If the input parameter contains a relative path, convert it.
406 // The target version of the current working directory is fine since
407 // we immediately convert it using redirect paths into a host version.
408 auto abs_path = absolutePath(filename, false);
409
410 for (auto path : system->redirectPaths) {
411 // Search through the redirect paths to see if a starting substring of
412 // our path falls into any buckets which need to redirected.
413 if (startswith(abs_path, path->appPath())) {
414 std::string tail = abs_path.substr(path->appPath().size());
415
416 // If this path needs to be redirected, search through a list
417 // of targets to see if we can match a valid file (or directory).
418 for (auto host_path : path->hostPaths()) {
419 if (access((host_path + tail).c_str(), R_OK) == 0) {
420 // Return the valid match.
421 return host_path + tail;
422 }
423 }
424 // The path needs to be redirected, but the file or directory
425 // does not exist on the host filesystem. Return the first
426 // host path as a default.
427 return path->hostPaths()[0] + tail;
428 }
429 }
430
431 // The path does not need to be redirected.
432 return abs_path;
433 }
434
435 void
436 Process::updateBias()
437 {
438 auto *interp = objFile->getInterpreter();
439
440 if (!interp || !interp->relocatable())
441 return;
442
443 // Determine how large the interpreters footprint will be in the process
444 // address space.
445 Addr interp_mapsize = roundUp(interp->mapSize(), system->getPageBytes());
446
447 // We are allocating the memory area; set the bias to the lowest address
448 // in the allocated memory region.
449 Addr mmap_end = memState->getMmapEnd();
450 Addr ld_bias = mmapGrowsDown() ? mmap_end - interp_mapsize : mmap_end;
451
452 // Adjust the process mmap area to give the interpreter room; the real
453 // execve system call would just invoke the kernel's internal mmap
454 // functions to make these adjustments.
455 mmap_end = mmapGrowsDown() ? ld_bias : mmap_end + interp_mapsize;
456 memState->setMmapEnd(mmap_end);
457
458 interp->updateBias(ld_bias);
459 }
460
461 Loader::ObjectFile *
462 Process::getInterpreter()
463 {
464 return objFile->getInterpreter();
465 }
466
467 Addr
468 Process::getBias()
469 {
470 auto *interp = getInterpreter();
471
472 return interp ? interp->bias() : objFile->bias();
473 }
474
475 Addr
476 Process::getStartPC()
477 {
478 auto *interp = getInterpreter();
479
480 return interp ? interp->entryPoint() : objFile->entryPoint();
481 }
482
483 std::string
484 Process::absolutePath(const std::string &filename, bool host_filesystem)
485 {
486 if (filename.empty() || startswith(filename, "/"))
487 return filename;
488
489 // Construct the absolute path given the current working directory for
490 // either the host filesystem or target filesystem. The distinction only
491 // matters if filesystem redirection is utilized in the simulation.
492 auto path_base = std::string();
493 if (host_filesystem) {
494 path_base = hostCwd;
495 assert(!hostCwd.empty());
496 } else {
497 path_base = tgtCwd;
498 assert(!tgtCwd.empty());
499 }
500
501 // Add a trailing '/' if the current working directory did not have one.
502 normalize(path_base);
503
504 // Append the filename onto the current working path.
505 auto absolute_path = path_base + filename;
506
507 return absolute_path;
508 }
509
510 Process *
511 ProcessParams::create()
512 {
513 // If not specified, set the executable parameter equal to the
514 // simulated system's zeroth command line parameter
515 if (executable == "") {
516 executable = cmd[0];
517 }
518
519 auto *obj_file = Loader::createObjectFile(executable);
520 fatal_if(!obj_file, "Cannot load object file %s.", executable);
521
522 Process *process = Process::tryLoaders(this, obj_file);
523 fatal_if(!process, "Unknown error creating process object.");
524
525 return process;
526 }