2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
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.
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.
29 #include "arch/mips/process.hh"
31 #include "arch/mips/isa_traits.hh"
32 #include "base/loader/elf_object.hh"
33 #include "base/loader/object_file.hh"
34 #include "base/logging.hh"
35 #include "cpu/thread_context.hh"
36 #include "debug/Loader.hh"
37 #include "mem/page_table.hh"
38 #include "params/Process.hh"
39 #include "sim/aux_vector.hh"
40 #include "sim/process.hh"
41 #include "sim/process_impl.hh"
42 #include "sim/syscall_return.hh"
43 #include "sim/system.hh"
46 using namespace MipsISA
;
48 MipsProcess::MipsProcess(ProcessParams
*params
, ObjectFile
*objFile
)
50 new EmulationPageTable(params
->name
, params
->pid
, PageBytes
),
53 fatal_if(params
->useArchPT
, "Arch page tables not implemented.");
54 // Set up stack. On MIPS, stack starts at the top of kuseg
55 // user address space. MIPS stack grows down from here
56 Addr stack_base
= 0x7FFFFFFF;
58 Addr max_stack_size
= 8 * 1024 * 1024;
60 // Set pointer for next thread stack. Reserve 8M for main stack.
61 Addr next_thread_stack_base
= stack_base
- max_stack_size
;
63 // Set up break point (Top of Heap)
64 Addr brk_point
= image
.maxAddr();
65 brk_point
= roundUp(brk_point
, PageBytes
);
67 // Set up region for mmaps. Start it 1GB above the top of the heap.
68 Addr mmap_end
= brk_point
+ 0x40000000L
;
70 memState
= make_shared
<MemState
>(brk_point
, stack_base
, max_stack_size
,
71 next_thread_stack_base
, mmap_end
);
75 MipsProcess::initState()
79 argsInit
<uint32_t>(PageBytes
);
82 template<class IntType
>
84 MipsProcess::argsInit(int pageSize
)
86 int intSize
= sizeof(IntType
);
88 std::vector
<AuxVector
<IntType
>> auxv
;
90 ElfObject
* elfObject
= dynamic_cast<ElfObject
*>(objFile
);
93 // Set the system page size
94 auxv
.emplace_back(M5_AT_PAGESZ
, MipsISA::PageBytes
);
95 // Set the frequency at which time() increments
96 auxv
.emplace_back(M5_AT_CLKTCK
, 100);
97 // For statically linked executables, this is the virtual
98 // address of the program header tables if they appear in the
100 auxv
.emplace_back(M5_AT_PHDR
, elfObject
->programHeaderTable());
101 DPRINTF(Loader
, "auxv at PHDR %08p\n",
102 elfObject
->programHeaderTable());
103 // This is the size of a program header entry from the elf file.
104 auxv
.emplace_back(M5_AT_PHENT
, elfObject
->programHeaderSize());
105 // This is the number of program headers from the original elf file.
106 auxv
.emplace_back(M5_AT_PHNUM
, elfObject
->programHeaderCount());
107 // This is the base address of the ELF interpreter; it should be
108 // zero for static executables or contain the base address for
109 // dynamic executables.
110 auxv
.emplace_back(M5_AT_BASE
, getBias());
111 //The entry point to the program
112 auxv
.emplace_back(M5_AT_ENTRY
, objFile
->entryPoint());
113 //Different user and group IDs
114 auxv
.emplace_back(M5_AT_UID
, uid());
115 auxv
.emplace_back(M5_AT_EUID
, euid());
116 auxv
.emplace_back(M5_AT_GID
, gid());
117 auxv
.emplace_back(M5_AT_EGID
, egid());
120 // Calculate how much space we need for arg & env & auxv arrays.
121 int argv_array_size
= intSize
* (argv
.size() + 1);
122 int envp_array_size
= intSize
* (envp
.size() + 1);
123 int auxv_array_size
= intSize
* 2 * (auxv
.size() + 1);
125 int arg_data_size
= 0;
126 for (vector
<string
>::size_type i
= 0; i
< argv
.size(); ++i
) {
127 arg_data_size
+= argv
[i
].size() + 1;
129 int env_data_size
= 0;
130 for (vector
<string
>::size_type i
= 0; i
< envp
.size(); ++i
) {
131 env_data_size
+= envp
[i
].size() + 1;
141 // set bottom of stack
142 memState
->setStackMin(memState
->getStackBase() - space_needed
);
144 memState
->setStackMin(roundDown(memState
->getStackMin(), pageSize
));
145 memState
->setStackSize(memState
->getStackBase() - memState
->getStackMin());
147 allocateMem(memState
->getStackMin(), roundUp(memState
->getStackSize(),
150 // map out initial stack contents; leave room for argc
151 IntType argv_array_base
= memState
->getStackMin() + intSize
;
152 IntType envp_array_base
= argv_array_base
+ argv_array_size
;
153 IntType auxv_array_base
= envp_array_base
+ envp_array_size
;
154 IntType arg_data_base
= auxv_array_base
+ auxv_array_size
;
155 IntType env_data_base
= arg_data_base
+ arg_data_size
;
157 // write contents to stack
158 IntType argc
= argv
.size();
160 argc
= htole((IntType
)argc
);
162 initVirtMem
.writeBlob(memState
->getStackMin(), &argc
, intSize
);
164 copyStringArray(argv
, argv_array_base
, arg_data_base
,
165 LittleEndianByteOrder
, initVirtMem
);
167 copyStringArray(envp
, envp_array_base
, env_data_base
,
168 LittleEndianByteOrder
, initVirtMem
);
170 // Copy the aux vector
171 Addr auxv_array_end
= auxv_array_base
;
172 for (const auto &aux
: auxv
) {
173 initVirtMem
.write(auxv_array_end
, aux
, GuestByteOrder
);
174 auxv_array_end
+= sizeof(aux
);
177 // Write out the terminating zeroed auxilliary vector
178 const AuxVector
<IntType
> zero(0, 0);
179 initVirtMem
.write(auxv_array_end
, zero
);
180 auxv_array_end
+= sizeof(zero
);
182 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
184 tc
->setIntReg(FirstArgumentReg
, argc
);
185 tc
->setIntReg(FirstArgumentReg
+ 1, argv_array_base
);
186 tc
->setIntReg(StackPointerReg
, memState
->getStackMin());
188 tc
->pcState(getStartPC());
193 MipsProcess::getSyscallArg(ThreadContext
*tc
, int &i
)
196 return tc
->readIntReg(FirstArgumentReg
+ i
++);
200 MipsProcess::setSyscallReturn(ThreadContext
*tc
, SyscallReturn sysret
)
202 if (sysret
.successful()) {
204 tc
->setIntReg(SyscallSuccessReg
, 0);
205 tc
->setIntReg(ReturnValueReg
, sysret
.returnValue());
207 // got an error, return details
208 tc
->setIntReg(SyscallSuccessReg
, (uint32_t)(-1));
209 tc
->setIntReg(ReturnValueReg
, sysret
.errnoValue());