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