2 * Copyright (c) 2003-2004 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/sparc/isa_traits.hh"
30 #include "arch/sparc/process.hh"
31 #include "arch/sparc/linux/process.hh"
32 #include "arch/sparc/solaris/process.hh"
33 #include "base/loader/object_file.hh"
34 #include "base/misc.hh"
35 #include "cpu/exec_context.hh"
36 #include "mem/page_table.hh"
37 #include "mem/translating_port.hh"
38 #include "sim/builder.hh"
39 #include "sim/system.hh"
42 using namespace SparcISA
;
45 SparcLiveProcess::create(const std::string
&nm
, System
*system
, int stdin_fd
,
46 int stdout_fd
, int stderr_fd
, std::string executable
,
47 std::vector
<std::string
> &argv
, std::vector
<std::string
> &envp
)
49 SparcLiveProcess
*process
= NULL
;
51 ObjectFile
*objFile
= createObjectFile(executable
);
52 if (objFile
== NULL
) {
53 fatal("Can't load object file %s", executable
);
57 if (objFile
->getArch() != ObjectFile::SPARC
)
58 fatal("Object file with arch %x does not match architecture %x.",
59 objFile
->getArch(), ObjectFile::SPARC
);
60 switch (objFile
->getOpSys()) {
61 case ObjectFile::Linux
:
62 process
= new SparcLinuxProcess(nm
, objFile
, system
,
63 stdin_fd
, stdout_fd
, stderr_fd
,
68 case ObjectFile::Solaris
:
69 process
= new SparcSolarisProcess(nm
, objFile
, system
,
70 stdin_fd
, stdout_fd
, stderr_fd
,
74 fatal("Unknown/unsupported operating system.");
78 fatal("Unknown error creating process object.");
82 SparcLiveProcess::SparcLiveProcess(const std::string
&nm
, ObjectFile
*objFile
,
83 System
*_system
, int stdin_fd
, int stdout_fd
, int stderr_fd
,
84 std::vector
<std::string
> &argv
, std::vector
<std::string
> &envp
)
85 : LiveProcess(nm
, objFile
, _system
, stdin_fd
, stdout_fd
, stderr_fd
,
89 // XXX all the below need to be updated for SPARC - Ali
90 brk_point
= objFile
->dataBase() + objFile
->dataSize() + objFile
->bssSize();
91 brk_point
= roundUp(brk_point
, VMPageSize
);
93 // Set up stack. On SPARC Linux, stack goes from the top of memory
94 // downward, less the hole for the kernel address space.
95 stack_base
= ((Addr
)0x80000000000ULL
);
97 // Set up region for mmaps. Tru64 seems to start just above 0 and
98 // grow up from there.
99 mmap_start
= mmap_end
= 0x800000;
101 // Set pointer for next thread stack. Reserve 8M for main stack.
102 next_thread_stack_base
= stack_base
- (8 * 1024 * 1024);
106 SparcLiveProcess::startup()
108 argsInit(MachineBytes
, VMPageSize
);
112 //The process runs in user mode
113 execContexts
[0]->setMiscRegWithEffect(MISCREG_PSTATE_PRIV
, 0);
114 //Interrupts are enabled
115 execContexts
[0]->setMiscRegWithEffect(MISCREG_PSTATE_IE
, 1);
117 execContexts
[0]->setMiscRegWithEffect(MISCREG_FSR_RD
, 0);
118 //Floating point traps are not enabled
119 execContexts
[0]->setMiscRegWithEffect(MISCREG_FSR_TEM
, 0);
120 //Turn non standard mode off
121 execContexts
[0]->setMiscRegWithEffect(MISCREG_FSR_NS
, 0);
122 //The floating point queue is empty
123 execContexts
[0]->setMiscRegWithEffect(MISCREG_FSR_QNE
, 0);
124 //There are no accrued eexecContext[0]eptions
125 execContexts
[0]->setMiscRegWithEffect(MISCREG_FSR_AEXC
, 0);
126 //There are no current eexecContext[0]eptions
127 execContexts
[0]->setMiscRegWithEffect(MISCREG_FSR_CEXC
, 0);
130 * Register window management registers
133 //No windows contain info from other programs
134 execContexts
[0]->setMiscRegWithEffect(MISCREG_OTHERWIN
, 0);
135 //There are no windows to pop
136 execContexts
[0]->setMiscRegWithEffect(MISCREG_CANRESTORE
, 0);
137 //All windows are available to save into
138 execContexts
[0]->setMiscRegWithEffect(MISCREG_CANSAVE
, NWindows
- 2);
139 //All windows are "clean"
140 execContexts
[0]->setMiscRegWithEffect(MISCREG_CLEANWIN
, NWindows
);
141 //Start with register window 0
142 execContexts
[0]->setMiscRegWithEffect(MISCREG_CWP
, 0);
145 m5_auxv_t
buildAuxVect(int64_t type
, int64_t val
)
148 result
.a_type
= TheISA::htog(type
);
149 result
.a_val
= TheISA::htog(val
);
154 SparcLiveProcess::argsInit(int intSize
, int pageSize
)
158 Addr alignmentMask
= ~(intSize
- 1);
160 // load object file into target memory
161 objFile
->loadSections(initVirtMem
);
163 //These are the auxilliary vector types
168 SPARC_AT_CLKTCK
= 17,
183 M5_HWCAP_SPARC_FLUSH
= 1,
184 M5_HWCAP_SPARC_STBAR
= 2,
185 M5_HWCAP_SPARC_SWAP
= 4,
186 M5_HWCAP_SPARC_MULDIV
= 8,
187 M5_HWCAP_SPARC_V9
= 16,
188 //This one should technically only be set
189 //if there is a cheetah or cheetah_plus tlb,
190 //but we'll use it all the time
191 M5_HWCAP_SPARC_ULTRA3
= 32
194 const int64_t hwcap
=
195 M5_HWCAP_SPARC_FLUSH
|
196 M5_HWCAP_SPARC_STBAR
|
197 M5_HWCAP_SPARC_SWAP
|
198 M5_HWCAP_SPARC_MULDIV
|
200 M5_HWCAP_SPARC_ULTRA3
;
202 //Setup the auxilliary vectors. These will already have
204 auxv
.push_back(buildAuxVect(SPARC_AT_EGID
, 100));
205 auxv
.push_back(buildAuxVect(SPARC_AT_GID
, 100));
206 auxv
.push_back(buildAuxVect(SPARC_AT_EUID
, 100));
207 auxv
.push_back(buildAuxVect(SPARC_AT_UID
, 100));
208 //This would work, but the entry point is a protected member
209 //auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entry));
210 auxv
.push_back(buildAuxVect(SPARC_AT_FLAGS
, 0));
211 //This is the address of the elf "interpreter", which I don't
212 //think we currently set up. It should be set to 0 (I think)
213 //auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0));
214 //This is the number of headers which were in the original elf
215 //file. This information isn't avaibale by this point.
216 //auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, 3));
217 //This is the size of a program header entry. This isn't easy
219 //auxv.push_back(buildAuxVect(SPARC_AT_PHENT, blah));
220 //This is should be set to load_addr (whatever that is) +
221 //e_phoff. I think it's a pointer to the program headers.
222 //auxv.push_back(buildAuxVect(SPARC_AT_PHDR, blah));
223 //This should be easy to get right, but I won't set it for now
224 //auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, blah));
225 auxv
.push_back(buildAuxVect(SPARC_AT_PAGESZ
, SparcISA::VMPageSize
));
226 auxv
.push_back(buildAuxVect(SPARC_AT_HWCAP
, hwcap
));
228 //Figure out how big the initial stack needs to be
230 //Each auxilliary vector is two 8 byte words
231 int aux_data_size
= 2 * intSize
* auxv
.size();
232 int env_data_size
= 0;
233 for (int i
= 0; i
< envp
.size(); ++i
) {
234 env_data_size
+= envp
[i
].size() + 1;
236 int arg_data_size
= 0;
237 for (int i
= 0; i
< argv
.size(); ++i
) {
238 arg_data_size
+= argv
[i
].size() + 1;
241 int aux_array_size
= intSize
* 2 * (auxv
.size() + 1);
243 int argv_array_size
= intSize
* (argv
.size() + 1);
244 int envp_array_size
= intSize
* (envp
.size() + 1);
246 int argc_size
= intSize
;
247 int window_save_size
= intSize
* 16;
249 int info_block_size
=
253 ~alignmentMask
) & alignmentMask
;
255 int info_block_padding
=
269 stack_min
= stack_base
- space_needed
;
270 stack_min
&= alignmentMask
;
271 stack_size
= stack_base
- stack_min
;
274 pTable
->allocate(roundDown(stack_min
, pageSize
),
275 roundUp(stack_size
, pageSize
));
277 // map out initial stack contents
278 Addr aux_data_base
= stack_base
- aux_data_size
- info_block_padding
;
279 Addr env_data_base
= aux_data_base
- env_data_size
;
280 Addr arg_data_base
= env_data_base
- arg_data_size
;
281 Addr auxv_array_base
= arg_data_base
- aux_array_size
;
282 Addr envp_array_base
= auxv_array_base
- envp_array_size
;
283 Addr argv_array_base
= envp_array_base
- argv_array_size
;
284 Addr argc_base
= argv_array_base
- argc_size
;
285 Addr window_save_base
= argc_base
- window_save_size
;
287 DPRINTF(Sparc
, "The addresses of items on the initial stack:\n");
288 DPRINTF(Sparc
, "0x%x - aux data\n", aux_data_base
);
289 DPRINTF(Sparc
, "0x%x - env data\n", env_data_base
);
290 DPRINTF(Sparc
, "0x%x - arg data\n", arg_data_base
);
291 DPRINTF(Sparc
, "0x%x - auxv array\n", auxv_array_base
);
292 DPRINTF(Sparc
, "0x%x - envp array\n", envp_array_base
);
293 DPRINTF(Sparc
, "0x%x - argv array\n", argv_array_base
);
294 DPRINTF(Sparc
, "0x%x - argc \n", argc_base
);
295 DPRINTF(Sparc
, "0x%x - window save\n", window_save_base
);
296 DPRINTF(Sparc
, "0x%x - stack min\n", stack_min
);
298 // write contents to stack
299 uint64_t argc
= argv
.size();
300 uint64_t guestArgc
= TheISA::htog(argc
);
303 for(int x
= 0; x
< auxv
.size(); x
++)
305 initVirtMem
->writeBlob(auxv_array_base
+ x
* 2 * intSize
,
306 (uint8_t*)&(auxv
[x
].a_type
), intSize
);
307 initVirtMem
->writeBlob(auxv_array_base
+ (x
* 2 + 1) * intSize
,
308 (uint8_t*)&(auxv
[x
].a_val
), intSize
);
310 //Write out the terminating zeroed auxilliary vector
311 const uint64_t zero
= 0;
312 initVirtMem
->writeBlob(auxv_array_base
+ 2 * intSize
* auxv
.size(),
313 (uint8_t*)&zero
, 2 * intSize
);
315 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
316 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
318 initVirtMem
->writeBlob(argc_base
, (uint8_t*)&guestArgc
, intSize
);
320 execContexts
[0]->setIntReg(ArgumentReg0
, argc
);
321 execContexts
[0]->setIntReg(ArgumentReg1
, argv_array_base
);
322 execContexts
[0]->setIntReg(StackPointerReg
, stack_min
- StackBias
);
324 Addr prog_entry
= objFile
->entryPoint();
325 execContexts
[0]->setPC(prog_entry
);
326 execContexts
[0]->setNextPC(prog_entry
+ sizeof(MachInst
));
327 execContexts
[0]->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
333 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SparcLiveProcess
)
335 VectorParam
<string
> cmd
;
336 Param
<string
> executable
;
338 Param
<string
> output
;
339 VectorParam
<string
> env
;
340 SimObjectParam
<System
*> system
;
342 END_DECLARE_SIM_OBJECT_PARAMS(SparcLiveProcess
)
345 BEGIN_INIT_SIM_OBJECT_PARAMS(SparcLiveProcess
)
347 INIT_PARAM(cmd
, "command line (executable plus arguments)"),
348 INIT_PARAM(executable
, "executable (overrides cmd[0] if set)"),
349 INIT_PARAM(input
, "filename for stdin (dflt: use sim stdin)"),
350 INIT_PARAM(output
, "filename for stdout/stderr (dflt: use sim stdout)"),
351 INIT_PARAM(env
, "environment settings"),
352 INIT_PARAM(system
, "system")
354 END_INIT_SIM_OBJECT_PARAMS(SparcLiveProcess
)
357 CREATE_SIM_OBJECT(SparcLiveProcess
)
362 // initialize file descriptors to default: same as simulator
363 int stdin_fd
, stdout_fd
, stderr_fd
;
365 if (in
== "stdin" || in
== "cin")
366 stdin_fd
= STDIN_FILENO
;
368 stdin_fd
= Process::openInputFile(input
);
370 if (out
== "stdout" || out
== "cout")
371 stdout_fd
= STDOUT_FILENO
;
372 else if (out
== "stderr" || out
== "cerr")
373 stdout_fd
= STDERR_FILENO
;
375 stdout_fd
= Process::openOutputFile(out
);
377 stderr_fd
= (stdout_fd
!= STDOUT_FILENO
) ? stdout_fd
: STDERR_FILENO
;
379 return SparcLiveProcess::create(getInstanceName(), system
,
380 stdin_fd
, stdout_fd
, stderr_fd
,
381 (string
)executable
== "" ? cmd
[0] : executable
,
386 REGISTER_SIM_OBJECT("SparcLiveProcess", SparcLiveProcess
)