process.cc (5941:e8a1f956d76c) process.cc (5956:a49d9413a9e8)
1/*
2 * Copyright (c) 2003-2006 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: Gabe Black
29 * Ali Saidi
30 */
31
32/*
33 * Copyright (c) 2007 The Hewlett-Packard Development Company
34 * All rights reserved.
35 *
36 * Redistribution and use of this software in source and binary forms,
37 * with or without modification, are permitted provided that the
38 * following conditions are met:
39 *
40 * The software must be used only for Non-Commercial Use which means any
41 * use which is NOT directed to receiving any direct monetary
42 * compensation for, or commercial advantage from such use. Illustrative
43 * examples of non-commercial use are academic research, personal study,
44 * teaching, education and corporate research & development.
45 * Illustrative examples of commercial use are distributing products for
46 * commercial advantage and providing services using the software for
47 * commercial advantage.
48 *
49 * If you wish to use this software or functionality therein that may be
50 * covered by patents for commercial use, please contact:
51 * Director of Intellectual Property Licensing
52 * Office of Strategy and Technology
53 * Hewlett-Packard Company
54 * 1501 Page Mill Road
55 * Palo Alto, California 94304
56 *
57 * Redistributions of source code must retain the above copyright notice,
58 * this list of conditions and the following disclaimer. Redistributions
59 * in binary form must reproduce the above copyright notice, this list of
60 * conditions and the following disclaimer in the documentation and/or
61 * other materials provided with the distribution. Neither the name of
62 * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
63 * contributors may be used to endorse or promote products derived from
64 * this software without specific prior written permission. No right of
65 * sublicense is granted herewith. Derivatives of the software and
66 * output created using the software may be prepared, but only for
67 * Non-Commercial Uses. Derivatives of the software may be shared with
68 * others provided: (i) the others agree to abide by the list of
69 * conditions herein which includes the Non-Commercial Use restrictions;
70 * and (ii) such Derivatives of the software include the above copyright
71 * notice to acknowledge the contribution from this software where
72 * applicable, this list of conditions and the disclaimer below.
73 *
74 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
75 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
76 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
77 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
78 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
81 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
82 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
83 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
84 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
85 *
86 * Authors: Gabe Black
87 */
88
89#include "arch/x86/isa_traits.hh"
90#include "arch/x86/process.hh"
91#include "arch/x86/segmentregs.hh"
92#include "arch/x86/types.hh"
93#include "base/loader/object_file.hh"
94#include "base/loader/elf_object.hh"
95#include "base/misc.hh"
96#include "base/trace.hh"
97#include "cpu/thread_context.hh"
98#include "mem/page_table.hh"
99#include "mem/translating_port.hh"
100#include "sim/process_impl.hh"
1/*
2 * Copyright (c) 2003-2006 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: Gabe Black
29 * Ali Saidi
30 */
31
32/*
33 * Copyright (c) 2007 The Hewlett-Packard Development Company
34 * All rights reserved.
35 *
36 * Redistribution and use of this software in source and binary forms,
37 * with or without modification, are permitted provided that the
38 * following conditions are met:
39 *
40 * The software must be used only for Non-Commercial Use which means any
41 * use which is NOT directed to receiving any direct monetary
42 * compensation for, or commercial advantage from such use. Illustrative
43 * examples of non-commercial use are academic research, personal study,
44 * teaching, education and corporate research & development.
45 * Illustrative examples of commercial use are distributing products for
46 * commercial advantage and providing services using the software for
47 * commercial advantage.
48 *
49 * If you wish to use this software or functionality therein that may be
50 * covered by patents for commercial use, please contact:
51 * Director of Intellectual Property Licensing
52 * Office of Strategy and Technology
53 * Hewlett-Packard Company
54 * 1501 Page Mill Road
55 * Palo Alto, California 94304
56 *
57 * Redistributions of source code must retain the above copyright notice,
58 * this list of conditions and the following disclaimer. Redistributions
59 * in binary form must reproduce the above copyright notice, this list of
60 * conditions and the following disclaimer in the documentation and/or
61 * other materials provided with the distribution. Neither the name of
62 * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
63 * contributors may be used to endorse or promote products derived from
64 * this software without specific prior written permission. No right of
65 * sublicense is granted herewith. Derivatives of the software and
66 * output created using the software may be prepared, but only for
67 * Non-Commercial Uses. Derivatives of the software may be shared with
68 * others provided: (i) the others agree to abide by the list of
69 * conditions herein which includes the Non-Commercial Use restrictions;
70 * and (ii) such Derivatives of the software include the above copyright
71 * notice to acknowledge the contribution from this software where
72 * applicable, this list of conditions and the disclaimer below.
73 *
74 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
75 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
76 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
77 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
78 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
81 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
82 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
83 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
84 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
85 *
86 * Authors: Gabe Black
87 */
88
89#include "arch/x86/isa_traits.hh"
90#include "arch/x86/process.hh"
91#include "arch/x86/segmentregs.hh"
92#include "arch/x86/types.hh"
93#include "base/loader/object_file.hh"
94#include "base/loader/elf_object.hh"
95#include "base/misc.hh"
96#include "base/trace.hh"
97#include "cpu/thread_context.hh"
98#include "mem/page_table.hh"
99#include "mem/translating_port.hh"
100#include "sim/process_impl.hh"
101#include "sim/syscall_emul.hh"
101#include "sim/system.hh"
102
103using namespace std;
104using namespace X86ISA;
105
106
102#include "sim/system.hh"
103
104using namespace std;
105using namespace X86ISA;
106
107
107X86LiveProcess::X86LiveProcess(LiveProcessParams * params,
108 ObjectFile *objFile)
109 : LiveProcess(params, objFile)
108X86LiveProcess::X86LiveProcess(LiveProcessParams * params, ObjectFile *objFile,
109 SyscallDesc *_syscallDescs, int _numSyscallDescs) :
110 LiveProcess(params, objFile), syscallDescs(_syscallDescs),
111 numSyscallDescs(_numSyscallDescs)
110{
111 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
112 brk_point = roundUp(brk_point, VMPageSize);
112{
113 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
114 brk_point = roundUp(brk_point, VMPageSize);
115}
113
116
114 // Set pointer for next thread stack. Reserve 8M for main stack.
115 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
116
117X86_64LiveProcess::X86_64LiveProcess(LiveProcessParams *params,
118 ObjectFile *objFile, SyscallDesc *_syscallDescs,
119 int _numSyscallDescs) :
120 X86LiveProcess(params, objFile, _syscallDescs, _numSyscallDescs)
121{
117 // Set up stack. On X86_64 Linux, stack goes from the top of memory
118 // downward, less the hole for the kernel address space plus one page
119 // for undertermined purposes.
120 stack_base = (Addr)0x7FFFFFFFF000ULL;
121
122 // Set up stack. On X86_64 Linux, stack goes from the top of memory
123 // downward, less the hole for the kernel address space plus one page
124 // for undertermined purposes.
125 stack_base = (Addr)0x7FFFFFFFF000ULL;
126
127 // Set pointer for next thread stack. Reserve 8M for main stack.
128 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
129
122 // Set up region for mmaps. This was determined empirically and may not
123 // always be correct.
124 mmap_start = mmap_end = (Addr)0x2aaaaaaab000ULL;
125}
126
130 // Set up region for mmaps. This was determined empirically and may not
131 // always be correct.
132 mmap_start = mmap_end = (Addr)0x2aaaaaaab000ULL;
133}
134
127void X86LiveProcess::handleTrap(int trapNum, ThreadContext *tc)
135I386LiveProcess::I386LiveProcess(LiveProcessParams *params,
136 ObjectFile *objFile, SyscallDesc *_syscallDescs,
137 int _numSyscallDescs) :
138 X86LiveProcess(params, objFile, _syscallDescs, _numSyscallDescs)
128{
139{
129 switch(trapNum)
130 {
131 default:
132 panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum);
133 }
140 stack_base = (Addr)0xffffe000ULL;
141
142 // Set pointer for next thread stack. Reserve 8M for main stack.
143 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
144
145 // Set up region for mmaps. This was determined empirically and may not
146 // always be correct.
147 mmap_start = mmap_end = (Addr)0xf7ffd000ULL;
134}
135
148}
149
150SyscallDesc*
151X86LiveProcess::getDesc(int callnum)
152{
153 if (callnum < 0 || callnum >= numSyscallDescs)
154 return NULL;
155 return &syscallDescs[callnum];
156}
157
136void
158void
137X86LiveProcess::startup()
159X86_64LiveProcess::startup()
138{
160{
161 LiveProcess::startup();
162
139 if (checkpointRestored)
140 return;
141
163 if (checkpointRestored)
164 return;
165
142 argsInit(sizeof(IntReg), VMPageSize);
166 argsInit(sizeof(uint64_t), VMPageSize);
143
144 for (int i = 0; i < contextIds.size(); i++) {
145 ThreadContext * tc = system->getThreadContext(contextIds[i]);
146
147 SegAttr dataAttr = 0;
148 dataAttr.writable = 1;
149 dataAttr.readable = 1;
150 dataAttr.expandDown = 0;
151 dataAttr.dpl = 3;
152 dataAttr.defaultSize = 0;
153 dataAttr.longMode = 1;
154
155 //Initialize the segment registers.
156 for(int seg = 0; seg < NUM_SEGMENTREGS; seg++) {
157 tc->setMiscRegNoEffect(MISCREG_SEG_BASE(seg), 0);
158 tc->setMiscRegNoEffect(MISCREG_SEG_EFF_BASE(seg), 0);
159 tc->setMiscRegNoEffect(MISCREG_SEG_ATTR(seg), dataAttr);
160 }
161
162 SegAttr csAttr = 0;
163 csAttr.writable = 0;
164 csAttr.readable = 1;
165 csAttr.expandDown = 0;
166 csAttr.dpl = 3;
167 csAttr.defaultSize = 0;
168 csAttr.longMode = 1;
169
170 tc->setMiscRegNoEffect(MISCREG_CS_ATTR, csAttr);
171
172 //Set up the registers that describe the operating mode.
173 CR0 cr0 = 0;
174 cr0.pg = 1; // Turn on paging.
175 cr0.cd = 0; // Don't disable caching.
176 cr0.nw = 0; // This is bit is defined to be ignored.
177 cr0.am = 0; // No alignment checking
178 cr0.wp = 0; // Supervisor mode can write read only pages
179 cr0.ne = 1;
180 cr0.et = 1; // This should always be 1
181 cr0.ts = 0; // We don't do task switching, so causing fp exceptions
182 // would be pointless.
183 cr0.em = 0; // Allow x87 instructions to execute natively.
184 cr0.mp = 1; // This doesn't really matter, but the manual suggests
185 // setting it to one.
186 cr0.pe = 1; // We're definitely in protected mode.
187 tc->setMiscReg(MISCREG_CR0, cr0);
188
189 Efer efer = 0;
190 efer.sce = 1; // Enable system call extensions.
191 efer.lme = 1; // Enable long mode.
192 efer.lma = 1; // Activate long mode.
193 efer.nxe = 1; // Enable nx support.
194 efer.svme = 0; // Disable svm support for now. It isn't implemented.
195 efer.ffxsr = 1; // Turn on fast fxsave and fxrstor.
196 tc->setMiscReg(MISCREG_EFER, efer);
197 }
198}
199
200void
167
168 for (int i = 0; i < contextIds.size(); i++) {
169 ThreadContext * tc = system->getThreadContext(contextIds[i]);
170
171 SegAttr dataAttr = 0;
172 dataAttr.writable = 1;
173 dataAttr.readable = 1;
174 dataAttr.expandDown = 0;
175 dataAttr.dpl = 3;
176 dataAttr.defaultSize = 0;
177 dataAttr.longMode = 1;
178
179 //Initialize the segment registers.
180 for(int seg = 0; seg < NUM_SEGMENTREGS; seg++) {
181 tc->setMiscRegNoEffect(MISCREG_SEG_BASE(seg), 0);
182 tc->setMiscRegNoEffect(MISCREG_SEG_EFF_BASE(seg), 0);
183 tc->setMiscRegNoEffect(MISCREG_SEG_ATTR(seg), dataAttr);
184 }
185
186 SegAttr csAttr = 0;
187 csAttr.writable = 0;
188 csAttr.readable = 1;
189 csAttr.expandDown = 0;
190 csAttr.dpl = 3;
191 csAttr.defaultSize = 0;
192 csAttr.longMode = 1;
193
194 tc->setMiscRegNoEffect(MISCREG_CS_ATTR, csAttr);
195
196 //Set up the registers that describe the operating mode.
197 CR0 cr0 = 0;
198 cr0.pg = 1; // Turn on paging.
199 cr0.cd = 0; // Don't disable caching.
200 cr0.nw = 0; // This is bit is defined to be ignored.
201 cr0.am = 0; // No alignment checking
202 cr0.wp = 0; // Supervisor mode can write read only pages
203 cr0.ne = 1;
204 cr0.et = 1; // This should always be 1
205 cr0.ts = 0; // We don't do task switching, so causing fp exceptions
206 // would be pointless.
207 cr0.em = 0; // Allow x87 instructions to execute natively.
208 cr0.mp = 1; // This doesn't really matter, but the manual suggests
209 // setting it to one.
210 cr0.pe = 1; // We're definitely in protected mode.
211 tc->setMiscReg(MISCREG_CR0, cr0);
212
213 Efer efer = 0;
214 efer.sce = 1; // Enable system call extensions.
215 efer.lme = 1; // Enable long mode.
216 efer.lma = 1; // Activate long mode.
217 efer.nxe = 1; // Enable nx support.
218 efer.svme = 0; // Disable svm support for now. It isn't implemented.
219 efer.ffxsr = 1; // Turn on fast fxsave and fxrstor.
220 tc->setMiscReg(MISCREG_EFER, efer);
221 }
222}
223
224void
201X86LiveProcess::argsInit(int intSize, int pageSize)
225I386LiveProcess::startup()
202{
226{
203 typedef AuxVector<uint64_t> auxv_t;
204 std::vector<auxv_t> auxv;
227 LiveProcess::startup();
205
228
206 Process::startup();
229 if (checkpointRestored)
230 return;
207
231
232 argsInit(sizeof(uint32_t), VMPageSize);
233
234 for (int i = 0; i < contextIds.size(); i++) {
235 ThreadContext * tc = system->getThreadContext(contextIds[i]);
236
237 SegAttr dataAttr = 0;
238 dataAttr.writable = 1;
239 dataAttr.readable = 1;
240 dataAttr.expandDown = 0;
241 dataAttr.dpl = 3;
242 dataAttr.defaultSize = 1;
243 dataAttr.longMode = 0;
244
245 //Initialize the segment registers.
246 for(int seg = 0; seg < NUM_SEGMENTREGS; seg++) {
247 tc->setMiscRegNoEffect(MISCREG_SEG_BASE(seg), 0);
248 tc->setMiscRegNoEffect(MISCREG_SEG_EFF_BASE(seg), 0);
249 tc->setMiscRegNoEffect(MISCREG_SEG_ATTR(seg), dataAttr);
250 tc->setMiscRegNoEffect(MISCREG_SEG_SEL(seg), 0xB);
251 }
252
253 SegAttr csAttr = 0;
254 csAttr.writable = 0;
255 csAttr.readable = 1;
256 csAttr.expandDown = 0;
257 csAttr.dpl = 3;
258 csAttr.defaultSize = 1;
259 csAttr.longMode = 0;
260
261 tc->setMiscRegNoEffect(MISCREG_CS_ATTR, csAttr);
262
263 //Set up the registers that describe the operating mode.
264 CR0 cr0 = 0;
265 cr0.pg = 1; // Turn on paging.
266 cr0.cd = 0; // Don't disable caching.
267 cr0.nw = 0; // This is bit is defined to be ignored.
268 cr0.am = 0; // No alignment checking
269 cr0.wp = 0; // Supervisor mode can write read only pages
270 cr0.ne = 1;
271 cr0.et = 1; // This should always be 1
272 cr0.ts = 0; // We don't do task switching, so causing fp exceptions
273 // would be pointless.
274 cr0.em = 0; // Allow x87 instructions to execute natively.
275 cr0.mp = 1; // This doesn't really matter, but the manual suggests
276 // setting it to one.
277 cr0.pe = 1; // We're definitely in protected mode.
278 tc->setMiscReg(MISCREG_CR0, cr0);
279
280 Efer efer = 0;
281 efer.sce = 1; // Enable system call extensions.
282 efer.lme = 1; // Enable long mode.
283 efer.lma = 0; // Deactivate long mode.
284 efer.nxe = 1; // Enable nx support.
285 efer.svme = 0; // Disable svm support for now. It isn't implemented.
286 efer.ffxsr = 1; // Turn on fast fxsave and fxrstor.
287 tc->setMiscReg(MISCREG_EFER, efer);
288 }
289}
290
291template<class IntType>
292void
293X86LiveProcess::argsInit(int pageSize)
294{
295 int intSize = sizeof(IntType);
296
297 typedef AuxVector<IntType> auxv_t;
298 std::vector<auxv_t> auxv;
299
208 string filename;
209 if(argv.size() < 1)
210 filename = "";
211 else
212 filename = argv[0];
213
214 //We want 16 byte alignment
215 uint64_t align = 16;
216
217 // load object file into target memory
218 objFile->loadSections(initVirtMem);
219
220 enum X86CpuFeature {
221 X86_OnboardFPU = 1 << 0,
222 X86_VirtualModeExtensions = 1 << 1,
223 X86_DebuggingExtensions = 1 << 2,
224 X86_PageSizeExtensions = 1 << 3,
225
226 X86_TimeStampCounter = 1 << 4,
227 X86_ModelSpecificRegisters = 1 << 5,
228 X86_PhysicalAddressExtensions = 1 << 6,
229 X86_MachineCheckExtensions = 1 << 7,
230
231 X86_CMPXCHG8Instruction = 1 << 8,
232 X86_OnboardAPIC = 1 << 9,
233 X86_SYSENTER_SYSEXIT = 1 << 11,
234
235 X86_MemoryTypeRangeRegisters = 1 << 12,
236 X86_PageGlobalEnable = 1 << 13,
237 X86_MachineCheckArchitecture = 1 << 14,
238 X86_CMOVInstruction = 1 << 15,
239
240 X86_PageAttributeTable = 1 << 16,
241 X86_36BitPSEs = 1 << 17,
242 X86_ProcessorSerialNumber = 1 << 18,
243 X86_CLFLUSHInstruction = 1 << 19,
244
245 X86_DebugTraceStore = 1 << 21,
246 X86_ACPIViaMSR = 1 << 22,
247 X86_MultimediaExtensions = 1 << 23,
248
249 X86_FXSAVE_FXRSTOR = 1 << 24,
250 X86_StreamingSIMDExtensions = 1 << 25,
251 X86_StreamingSIMDExtensions2 = 1 << 26,
252 X86_CPUSelfSnoop = 1 << 27,
253
254 X86_HyperThreading = 1 << 28,
255 X86_AutomaticClockControl = 1 << 29,
256 X86_IA64Processor = 1 << 30
257 };
258
259 //Setup the auxilliary vectors. These will already have endian conversion.
260 //Auxilliary vectors are loaded only for elf formatted executables.
261 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
262 if(elfObject)
263 {
264 uint64_t features =
265 X86_OnboardFPU |
266 X86_VirtualModeExtensions |
267 X86_DebuggingExtensions |
268 X86_PageSizeExtensions |
269 X86_TimeStampCounter |
270 X86_ModelSpecificRegisters |
271 X86_PhysicalAddressExtensions |
272 X86_MachineCheckExtensions |
273 X86_CMPXCHG8Instruction |
274 X86_OnboardAPIC |
275 X86_SYSENTER_SYSEXIT |
276 X86_MemoryTypeRangeRegisters |
277 X86_PageGlobalEnable |
278 X86_MachineCheckArchitecture |
279 X86_CMOVInstruction |
280 X86_PageAttributeTable |
281 X86_36BitPSEs |
282// X86_ProcessorSerialNumber |
283 X86_CLFLUSHInstruction |
284// X86_DebugTraceStore |
285// X86_ACPIViaMSR |
286 X86_MultimediaExtensions |
287 X86_FXSAVE_FXRSTOR |
288 X86_StreamingSIMDExtensions |
289 X86_StreamingSIMDExtensions2 |
290// X86_CPUSelfSnoop |
291// X86_HyperThreading |
292// X86_AutomaticClockControl |
293// X86_IA64Processor |
294 0;
295
296 //Bits which describe the system hardware capabilities
297 //XXX Figure out what these should be
298 auxv.push_back(auxv_t(M5_AT_HWCAP, features));
299 //The system page size
300 auxv.push_back(auxv_t(M5_AT_PAGESZ, X86ISA::VMPageSize));
301 //Frequency at which times() increments
302 auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
303 // For statically linked executables, this is the virtual address of the
304 // program header tables if they appear in the executable image
305 auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
306 // This is the size of a program header entry from the elf file.
307 auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
308 // This is the number of program headers from the original elf file.
309 auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
310 //Defined to be 100 in the kernel source.
311 //This is the address of the elf "interpreter", It should be set
312 //to 0 for regular executables. It should be something else
313 //(not sure what) for dynamic libraries.
314 auxv.push_back(auxv_t(M5_AT_BASE, 0));
315
316 //XXX Figure out what this should be.
317 auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
318 //The entry point to the program
319 auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
320 //Different user and group IDs
321 auxv.push_back(auxv_t(M5_AT_UID, uid()));
322 auxv.push_back(auxv_t(M5_AT_EUID, euid()));
323 auxv.push_back(auxv_t(M5_AT_GID, gid()));
324 auxv.push_back(auxv_t(M5_AT_EGID, egid()));
325 //Whether to enable "secure mode" in the executable
326 auxv.push_back(auxv_t(M5_AT_SECURE, 0));
327 //The string "x86_64" with unknown meaning
328 auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
329 }
330
331 //Figure out how big the initial stack needs to be
332
333 // A sentry NULL void pointer at the top of the stack.
334 int sentry_size = intSize;
335
336 //This is the name of the file which is present on the initial stack
337 //It's purpose is to let the user space linker examine the original file.
338 int file_name_size = filename.size() + 1;
339
340 string platform = "x86_64";
341 int aux_data_size = platform.size() + 1;
342
343 int env_data_size = 0;
344 for (int i = 0; i < envp.size(); ++i) {
345 env_data_size += envp[i].size() + 1;
346 }
347 int arg_data_size = 0;
348 for (int i = 0; i < argv.size(); ++i) {
349 arg_data_size += argv[i].size() + 1;
350 }
351
352 //The info_block needs to be padded so it's size is a multiple of the
353 //alignment mask. Also, it appears that there needs to be at least some
354 //padding, so if the size is already a multiple, we need to increase it
355 //anyway.
356 int base_info_block_size =
357 sentry_size + file_name_size + env_data_size + arg_data_size;
358
359 int info_block_size = roundUp(base_info_block_size, align);
360
361 int info_block_padding = info_block_size - base_info_block_size;
362
363 //Each auxilliary vector is two 8 byte words
364 int aux_array_size = intSize * 2 * (auxv.size() + 1);
365
366 int envp_array_size = intSize * (envp.size() + 1);
367 int argv_array_size = intSize * (argv.size() + 1);
368
369 int argc_size = intSize;
370
371 //Figure out the size of the contents of the actual initial frame
372 int frame_size =
373 aux_array_size +
374 envp_array_size +
375 argv_array_size +
376 argc_size;
377
378 //There needs to be padding after the auxiliary vector data so that the
379 //very bottom of the stack is aligned properly.
380 int partial_size = frame_size + aux_data_size;
381 int aligned_partial_size = roundUp(partial_size, align);
382 int aux_padding = aligned_partial_size - partial_size;
383
384 int space_needed =
385 info_block_size +
386 aux_data_size +
387 aux_padding +
388 frame_size;
389
390 stack_min = stack_base - space_needed;
391 stack_min = roundDown(stack_min, align);
392 stack_size = stack_base - stack_min;
393
394 // map memory
395 pTable->allocate(roundDown(stack_min, pageSize),
396 roundUp(stack_size, pageSize));
397
398 // map out initial stack contents
300 string filename;
301 if(argv.size() < 1)
302 filename = "";
303 else
304 filename = argv[0];
305
306 //We want 16 byte alignment
307 uint64_t align = 16;
308
309 // load object file into target memory
310 objFile->loadSections(initVirtMem);
311
312 enum X86CpuFeature {
313 X86_OnboardFPU = 1 << 0,
314 X86_VirtualModeExtensions = 1 << 1,
315 X86_DebuggingExtensions = 1 << 2,
316 X86_PageSizeExtensions = 1 << 3,
317
318 X86_TimeStampCounter = 1 << 4,
319 X86_ModelSpecificRegisters = 1 << 5,
320 X86_PhysicalAddressExtensions = 1 << 6,
321 X86_MachineCheckExtensions = 1 << 7,
322
323 X86_CMPXCHG8Instruction = 1 << 8,
324 X86_OnboardAPIC = 1 << 9,
325 X86_SYSENTER_SYSEXIT = 1 << 11,
326
327 X86_MemoryTypeRangeRegisters = 1 << 12,
328 X86_PageGlobalEnable = 1 << 13,
329 X86_MachineCheckArchitecture = 1 << 14,
330 X86_CMOVInstruction = 1 << 15,
331
332 X86_PageAttributeTable = 1 << 16,
333 X86_36BitPSEs = 1 << 17,
334 X86_ProcessorSerialNumber = 1 << 18,
335 X86_CLFLUSHInstruction = 1 << 19,
336
337 X86_DebugTraceStore = 1 << 21,
338 X86_ACPIViaMSR = 1 << 22,
339 X86_MultimediaExtensions = 1 << 23,
340
341 X86_FXSAVE_FXRSTOR = 1 << 24,
342 X86_StreamingSIMDExtensions = 1 << 25,
343 X86_StreamingSIMDExtensions2 = 1 << 26,
344 X86_CPUSelfSnoop = 1 << 27,
345
346 X86_HyperThreading = 1 << 28,
347 X86_AutomaticClockControl = 1 << 29,
348 X86_IA64Processor = 1 << 30
349 };
350
351 //Setup the auxilliary vectors. These will already have endian conversion.
352 //Auxilliary vectors are loaded only for elf formatted executables.
353 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
354 if(elfObject)
355 {
356 uint64_t features =
357 X86_OnboardFPU |
358 X86_VirtualModeExtensions |
359 X86_DebuggingExtensions |
360 X86_PageSizeExtensions |
361 X86_TimeStampCounter |
362 X86_ModelSpecificRegisters |
363 X86_PhysicalAddressExtensions |
364 X86_MachineCheckExtensions |
365 X86_CMPXCHG8Instruction |
366 X86_OnboardAPIC |
367 X86_SYSENTER_SYSEXIT |
368 X86_MemoryTypeRangeRegisters |
369 X86_PageGlobalEnable |
370 X86_MachineCheckArchitecture |
371 X86_CMOVInstruction |
372 X86_PageAttributeTable |
373 X86_36BitPSEs |
374// X86_ProcessorSerialNumber |
375 X86_CLFLUSHInstruction |
376// X86_DebugTraceStore |
377// X86_ACPIViaMSR |
378 X86_MultimediaExtensions |
379 X86_FXSAVE_FXRSTOR |
380 X86_StreamingSIMDExtensions |
381 X86_StreamingSIMDExtensions2 |
382// X86_CPUSelfSnoop |
383// X86_HyperThreading |
384// X86_AutomaticClockControl |
385// X86_IA64Processor |
386 0;
387
388 //Bits which describe the system hardware capabilities
389 //XXX Figure out what these should be
390 auxv.push_back(auxv_t(M5_AT_HWCAP, features));
391 //The system page size
392 auxv.push_back(auxv_t(M5_AT_PAGESZ, X86ISA::VMPageSize));
393 //Frequency at which times() increments
394 auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
395 // For statically linked executables, this is the virtual address of the
396 // program header tables if they appear in the executable image
397 auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
398 // This is the size of a program header entry from the elf file.
399 auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
400 // This is the number of program headers from the original elf file.
401 auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
402 //Defined to be 100 in the kernel source.
403 //This is the address of the elf "interpreter", It should be set
404 //to 0 for regular executables. It should be something else
405 //(not sure what) for dynamic libraries.
406 auxv.push_back(auxv_t(M5_AT_BASE, 0));
407
408 //XXX Figure out what this should be.
409 auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
410 //The entry point to the program
411 auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
412 //Different user and group IDs
413 auxv.push_back(auxv_t(M5_AT_UID, uid()));
414 auxv.push_back(auxv_t(M5_AT_EUID, euid()));
415 auxv.push_back(auxv_t(M5_AT_GID, gid()));
416 auxv.push_back(auxv_t(M5_AT_EGID, egid()));
417 //Whether to enable "secure mode" in the executable
418 auxv.push_back(auxv_t(M5_AT_SECURE, 0));
419 //The string "x86_64" with unknown meaning
420 auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
421 }
422
423 //Figure out how big the initial stack needs to be
424
425 // A sentry NULL void pointer at the top of the stack.
426 int sentry_size = intSize;
427
428 //This is the name of the file which is present on the initial stack
429 //It's purpose is to let the user space linker examine the original file.
430 int file_name_size = filename.size() + 1;
431
432 string platform = "x86_64";
433 int aux_data_size = platform.size() + 1;
434
435 int env_data_size = 0;
436 for (int i = 0; i < envp.size(); ++i) {
437 env_data_size += envp[i].size() + 1;
438 }
439 int arg_data_size = 0;
440 for (int i = 0; i < argv.size(); ++i) {
441 arg_data_size += argv[i].size() + 1;
442 }
443
444 //The info_block needs to be padded so it's size is a multiple of the
445 //alignment mask. Also, it appears that there needs to be at least some
446 //padding, so if the size is already a multiple, we need to increase it
447 //anyway.
448 int base_info_block_size =
449 sentry_size + file_name_size + env_data_size + arg_data_size;
450
451 int info_block_size = roundUp(base_info_block_size, align);
452
453 int info_block_padding = info_block_size - base_info_block_size;
454
455 //Each auxilliary vector is two 8 byte words
456 int aux_array_size = intSize * 2 * (auxv.size() + 1);
457
458 int envp_array_size = intSize * (envp.size() + 1);
459 int argv_array_size = intSize * (argv.size() + 1);
460
461 int argc_size = intSize;
462
463 //Figure out the size of the contents of the actual initial frame
464 int frame_size =
465 aux_array_size +
466 envp_array_size +
467 argv_array_size +
468 argc_size;
469
470 //There needs to be padding after the auxiliary vector data so that the
471 //very bottom of the stack is aligned properly.
472 int partial_size = frame_size + aux_data_size;
473 int aligned_partial_size = roundUp(partial_size, align);
474 int aux_padding = aligned_partial_size - partial_size;
475
476 int space_needed =
477 info_block_size +
478 aux_data_size +
479 aux_padding +
480 frame_size;
481
482 stack_min = stack_base - space_needed;
483 stack_min = roundDown(stack_min, align);
484 stack_size = stack_base - stack_min;
485
486 // map memory
487 pTable->allocate(roundDown(stack_min, pageSize),
488 roundUp(stack_size, pageSize));
489
490 // map out initial stack contents
399 Addr sentry_base = stack_base - sentry_size;
400 Addr file_name_base = sentry_base - file_name_size;
401 Addr env_data_base = file_name_base - env_data_size;
402 Addr arg_data_base = env_data_base - arg_data_size;
403 Addr aux_data_base = arg_data_base - info_block_padding - aux_data_size;
404 Addr auxv_array_base = aux_data_base - aux_array_size - aux_padding;
405 Addr envp_array_base = auxv_array_base - envp_array_size;
406 Addr argv_array_base = envp_array_base - argv_array_size;
407 Addr argc_base = argv_array_base - argc_size;
491 IntType sentry_base = stack_base - sentry_size;
492 IntType file_name_base = sentry_base - file_name_size;
493 IntType env_data_base = file_name_base - env_data_size;
494 IntType arg_data_base = env_data_base - arg_data_size;
495 IntType aux_data_base = arg_data_base - info_block_padding - aux_data_size;
496 IntType auxv_array_base = aux_data_base - aux_array_size - aux_padding;
497 IntType envp_array_base = auxv_array_base - envp_array_size;
498 IntType argv_array_base = envp_array_base - argv_array_size;
499 IntType argc_base = argv_array_base - argc_size;
408
409 DPRINTF(Stack, "The addresses of items on the initial stack:\n");
410 DPRINTF(Stack, "0x%x - file name\n", file_name_base);
411 DPRINTF(Stack, "0x%x - env data\n", env_data_base);
412 DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
413 DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
414 DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
415 DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
416 DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
417 DPRINTF(Stack, "0x%x - argc \n", argc_base);
418 DPRINTF(Stack, "0x%x - stack min\n", stack_min);
419
420 // write contents to stack
421
422 // figure out argc
500
501 DPRINTF(Stack, "The addresses of items on the initial stack:\n");
502 DPRINTF(Stack, "0x%x - file name\n", file_name_base);
503 DPRINTF(Stack, "0x%x - env data\n", env_data_base);
504 DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
505 DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
506 DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
507 DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
508 DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
509 DPRINTF(Stack, "0x%x - argc \n", argc_base);
510 DPRINTF(Stack, "0x%x - stack min\n", stack_min);
511
512 // write contents to stack
513
514 // figure out argc
423 uint64_t argc = argv.size();
424 uint64_t guestArgc = X86ISA::htog(argc);
515 IntType argc = argv.size();
516 IntType guestArgc = X86ISA::htog(argc);
425
426 //Write out the sentry void *
517
518 //Write out the sentry void *
427 uint64_t sentry_NULL = 0;
519 IntType sentry_NULL = 0;
428 initVirtMem->writeBlob(sentry_base,
429 (uint8_t*)&sentry_NULL, sentry_size);
430
431 //Write the file name
432 initVirtMem->writeString(file_name_base, filename.c_str());
433
434 //Fix up the aux vector which points to the "platform" string
435 assert(auxv[auxv.size() - 1].a_type = M5_AT_PLATFORM);
436 auxv[auxv.size() - 1].a_val = aux_data_base;
437
438 //Copy the aux stuff
439 for(int x = 0; x < auxv.size(); x++)
440 {
441 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
442 (uint8_t*)&(auxv[x].a_type), intSize);
443 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
444 (uint8_t*)&(auxv[x].a_val), intSize);
445 }
446 //Write out the terminating zeroed auxilliary vector
447 const uint64_t zero = 0;
448 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
449 (uint8_t*)&zero, 2 * intSize);
450
451 initVirtMem->writeString(aux_data_base, platform.c_str());
452
453 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
454 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
455
456 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
457
458 ThreadContext *tc = system->getThreadContext(contextIds[0]);
459 //Set the stack pointer register
460 tc->setIntReg(StackPointerReg, stack_min);
461
462 Addr prog_entry = objFile->entryPoint();
463 // There doesn't need to be any segment base added in since we're dealing
464 // with the flat segmentation model.
465 tc->setPC(prog_entry);
466 tc->setNextPC(prog_entry + sizeof(MachInst));
467
468 //Align the "stack_min" to a page boundary.
469 stack_min = roundDown(stack_min, pageSize);
470
471// num_processes++;
472}
520 initVirtMem->writeBlob(sentry_base,
521 (uint8_t*)&sentry_NULL, sentry_size);
522
523 //Write the file name
524 initVirtMem->writeString(file_name_base, filename.c_str());
525
526 //Fix up the aux vector which points to the "platform" string
527 assert(auxv[auxv.size() - 1].a_type = M5_AT_PLATFORM);
528 auxv[auxv.size() - 1].a_val = aux_data_base;
529
530 //Copy the aux stuff
531 for(int x = 0; x < auxv.size(); x++)
532 {
533 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
534 (uint8_t*)&(auxv[x].a_type), intSize);
535 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
536 (uint8_t*)&(auxv[x].a_val), intSize);
537 }
538 //Write out the terminating zeroed auxilliary vector
539 const uint64_t zero = 0;
540 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
541 (uint8_t*)&zero, 2 * intSize);
542
543 initVirtMem->writeString(aux_data_base, platform.c_str());
544
545 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
546 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
547
548 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
549
550 ThreadContext *tc = system->getThreadContext(contextIds[0]);
551 //Set the stack pointer register
552 tc->setIntReg(StackPointerReg, stack_min);
553
554 Addr prog_entry = objFile->entryPoint();
555 // There doesn't need to be any segment base added in since we're dealing
556 // with the flat segmentation model.
557 tc->setPC(prog_entry);
558 tc->setNextPC(prog_entry + sizeof(MachInst));
559
560 //Align the "stack_min" to a page boundary.
561 stack_min = roundDown(stack_min, pageSize);
562
563// num_processes++;
564}
565
566void
567X86_64LiveProcess::argsInit(int intSize, int pageSize)
568{
569 X86LiveProcess::argsInit<uint64_t>(pageSize);
570}
571
572void
573I386LiveProcess::argsInit(int intSize, int pageSize)
574{
575 X86LiveProcess::argsInit<uint32_t>(pageSize);
576}