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.
33 #include "arch/mips/isa_traits.hh"
34 #include "arch/mips/process.hh"
36 #include "base/loader/object_file.hh"
37 #include "base/misc.hh"
38 #include "cpu/thread_context.hh"
40 #include "mem/page_table.hh"
42 #include "sim/process.hh"
43 #include "sim/process_impl.hh"
44 #include "sim/system.hh"
47 using namespace MipsISA
;
49 MipsLiveProcess::MipsLiveProcess(LiveProcessParams
* params
,
51 : LiveProcess(params
, objFile
)
53 // Set up stack. On MIPS, stack starts at the top of kuseg
54 // user address space. MIPS stack grows down from here
55 stack_base
= 0x7FFFFFFF;
57 // Set pointer for next thread stack. Reserve 8M for main stack.
58 next_thread_stack_base
= stack_base
- (8 * 1024 * 1024);
60 // Set up break point (Top of Heap)
61 brk_point
= objFile
->dataBase() + objFile
->dataSize() + objFile
->bssSize();
62 brk_point
= roundUp(brk_point
, VMPageSize
);
64 // Set up region for mmaps. For now, start at bottom of kuseg space.
65 mmap_start
= mmap_end
= 0x10000;
69 MipsLiveProcess::startup()
73 argsInit(MachineBytes
, VMPageSize
);
77 MipsLiveProcess::argsInit(int intSize
, int pageSize
)
79 // load object file into target memory
80 objFile
->loadSections(initVirtMem
);
82 // Calculate how much space we need for arg & env arrays.
83 int argv_array_size
= intSize
* (argv
.size() + 1);
84 int envp_array_size
= intSize
* (envp
.size() + 1);
85 int arg_data_size
= 0;
86 for (vector
<string
>::size_type i
= 0; i
< argv
.size(); ++i
) {
87 arg_data_size
+= argv
[i
].size() + 1;
89 int env_data_size
= 0;
90 for (vector
<string
>::size_type i
= 0; i
< envp
.size(); ++i
) {
91 env_data_size
+= envp
[i
].size() + 1;
95 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
96 if (space_needed
< 32*1024)
97 space_needed
= 32*1024;
99 // set bottom of stack
100 stack_min
= stack_base
- space_needed
;
102 stack_min
= roundDown(stack_min
, pageSize
);
103 stack_size
= stack_base
- stack_min
;
105 pTable
->allocate(stack_min
, roundUp(stack_size
, pageSize
));
107 // map out initial stack contents
109 // NOTE: Using uint32_t hardcodes MIPS32 and not MIPS64
110 // even if MIPS64 was intended. This is because the
111 // copyStringArray function templates on the parameters.
112 // Elegant way to check intSize and vary between 32/64?
114 uint32_t argv_array_base
= stack_min
+ intSize
; // room for argc
115 uint32_t envp_array_base
= argv_array_base
+ argv_array_size
;
116 uint32_t arg_data_base
= envp_array_base
+ envp_array_size
;
117 uint32_t env_data_base
= arg_data_base
+ arg_data_size
;
119 // write contents to stack
120 uint32_t argc
= argv
.size();
123 argc
= htog((uint64_t)argc
);
124 else if (intSize
== 4)
125 argc
= htog((uint32_t)argc
);
127 panic("Unknown int size");
130 initVirtMem
->writeBlob(stack_min
, (uint8_t*)&argc
, intSize
);
132 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
134 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
136 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
138 setSyscallArg(tc
, 0, argc
);
139 setSyscallArg(tc
, 1, argv_array_base
);
140 tc
->setIntReg(StackPointerReg
, stack_min
);
142 Addr prog_entry
= objFile
->entryPoint();
143 tc
->setPC(prog_entry
);
144 tc
->setNextPC(prog_entry
+ sizeof(MachInst
));
145 tc
->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
150 MipsLiveProcess::getSyscallArg(ThreadContext
*tc
, int &i
)
153 return tc
->readIntReg(FirstArgumentReg
+ i
++);
157 MipsLiveProcess::setSyscallArg(ThreadContext
*tc
,
158 int i
, MipsISA::IntReg val
)
161 tc
->setIntReg(FirstArgumentReg
+ i
, val
);
165 MipsLiveProcess::setSyscallReturn(ThreadContext
*tc
,
166 SyscallReturn return_value
)
168 if (return_value
.successful()) {
170 tc
->setIntReg(SyscallSuccessReg
, 0);
171 tc
->setIntReg(ReturnValueReg
, return_value
.value());
173 // got an error, return details
174 tc
->setIntReg(SyscallSuccessReg
, (IntReg
) -1);
175 tc
->setIntReg(ReturnValueReg
, -return_value
.value());