db32437d58f58c2ac23cecf07bd69620031c7632
[gem5.git] / src / arch / x86 / process.cc
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/system.hh"
102
103 using namespace std;
104 using namespace X86ISA;
105
106 M5_64_auxv_t::M5_64_auxv_t(int64_t type, int64_t val)
107 {
108 a_type = TheISA::htog(type);
109 a_val = TheISA::htog(val);
110 }
111
112 X86LiveProcess::X86LiveProcess(LiveProcessParams * params,
113 ObjectFile *objFile)
114 : LiveProcess(params, objFile)
115 {
116 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
117 brk_point = roundUp(brk_point, VMPageSize);
118
119 // Set pointer for next thread stack. Reserve 8M for main stack.
120 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
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 up region for mmaps. This was determined empirically and may not
128 // always be correct.
129 mmap_start = mmap_end = 0x2aaaaaaab000;
130 }
131
132 void X86LiveProcess::handleTrap(int trapNum, ThreadContext *tc)
133 {
134 switch(trapNum)
135 {
136 default:
137 panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum);
138 }
139 }
140
141 void
142 X86LiveProcess::startup()
143 {
144 argsInit(sizeof(IntReg), VMPageSize);
145
146 for (int i = 0; i < threadContexts.size(); i++) {
147 ThreadContext * tc = threadContexts[i];
148
149 SegAttr dataAttr = 0;
150 dataAttr.writable = 1;
151 dataAttr.readable = 1;
152 dataAttr.expandDown = 0;
153 dataAttr.dpl = 3;
154 dataAttr.defaultSize = 0;
155 dataAttr.longMode = 1;
156
157 //Initialize the segment registers.
158 for(int seg = 0; seg < NUM_SEGMENTREGS; seg++) {
159 tc->setMiscRegNoEffect(MISCREG_SEG_BASE(seg), 0);
160 tc->setMiscRegNoEffect(MISCREG_SEG_ATTR(seg), dataAttr);
161 }
162
163 SegAttr csAttr = 0;
164 csAttr.writable = 0;
165 csAttr.readable = 1;
166 csAttr.expandDown = 0;
167 csAttr.dpl = 3;
168 csAttr.defaultSize = 0;
169 csAttr.longMode = 1;
170
171 tc->setMiscRegNoEffect(MISCREG_CS_ATTR, csAttr);
172
173 //Set up the registers that describe the operating mode.
174 CR0 cr0 = 0;
175 cr0.pg = 1; // Turn on paging.
176 cr0.cd = 0; // Don't disable caching.
177 cr0.nw = 0; // This is bit is defined to be ignored.
178 cr0.am = 0; // No alignment checking
179 cr0.wp = 0; // Supervisor mode can write read only pages
180 cr0.ne = 1;
181 cr0.et = 1; // This should always be 1
182 cr0.ts = 0; // We don't do task switching, so causing fp exceptions
183 // would be pointless.
184 cr0.em = 0; // Allow x87 instructions to execute natively.
185 cr0.mp = 1; // This doesn't really matter, but the manual suggests
186 // setting it to one.
187 cr0.pe = 1; // We're definitely in protected mode.
188 tc->setMiscReg(MISCREG_CR0, cr0);
189
190 Efer efer = 0;
191 efer.sce = 1; // Enable system call extensions.
192 efer.lme = 1; // Enable long mode.
193 efer.lma = 1; // Activate long mode.
194 efer.nxe = 1; // Enable nx support.
195 efer.svme = 0; // Disable svm support for now. It isn't implemented.
196 efer.ffxsr = 1; // Turn on fast fxsave and fxrstor.
197 tc->setMiscReg(MISCREG_EFER, efer);
198 }
199 }
200
201 void
202 X86LiveProcess::argsInit(int intSize, int pageSize)
203 {
204 typedef M5_64_auxv_t auxv_t;
205 Process::startup();
206
207 string filename;
208 if(argv.size() < 1)
209 filename = "";
210 else
211 filename = argv[0];
212
213 //We want 16 byte alignment
214 uint64_t align = 16;
215
216 // load object file into target memory
217 objFile->loadSections(initVirtMem);
218
219 enum X86CpuFeature {
220 X86_OnboardFPU = 1 << 0,
221 X86_VirtualModeExtensions = 1 << 1,
222 X86_DebuggingExtensions = 1 << 2,
223 X86_PageSizeExtensions = 1 << 3,
224
225 X86_TimeStampCounter = 1 << 4,
226 X86_ModelSpecificRegisters = 1 << 5,
227 X86_PhysicalAddressExtensions = 1 << 6,
228 X86_MachineCheckExtensions = 1 << 7,
229
230 X86_CMPXCHG8Instruction = 1 << 8,
231 X86_OnboardAPIC = 1 << 9,
232 X86_SYSENTER_SYSEXIT = 1 << 11,
233
234 X86_MemoryTypeRangeRegisters = 1 << 12,
235 X86_PageGlobalEnable = 1 << 13,
236 X86_MachineCheckArchitecture = 1 << 14,
237 X86_CMOVInstruction = 1 << 15,
238
239 X86_PageAttributeTable = 1 << 16,
240 X86_36BitPSEs = 1 << 17,
241 X86_ProcessorSerialNumber = 1 << 18,
242 X86_CLFLUSHInstruction = 1 << 19,
243
244 X86_DebugTraceStore = 1 << 21,
245 X86_ACPIViaMSR = 1 << 22,
246 X86_MultimediaExtensions = 1 << 23,
247
248 X86_FXSAVE_FXRSTOR = 1 << 24,
249 X86_StreamingSIMDExtensions = 1 << 25,
250 X86_StreamingSIMDExtensions2 = 1 << 26,
251 X86_CPUSelfSnoop = 1 << 27,
252
253 X86_HyperThreading = 1 << 28,
254 X86_AutomaticClockControl = 1 << 29,
255 X86_IA64Processor = 1 << 30
256 };
257
258 //Setup the auxilliary vectors. These will already have endian conversion.
259 //Auxilliary vectors are loaded only for elf formatted executables.
260 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
261 if(elfObject)
262 {
263 uint64_t features =
264 X86_OnboardFPU |
265 X86_VirtualModeExtensions |
266 X86_DebuggingExtensions |
267 X86_PageSizeExtensions |
268 X86_TimeStampCounter |
269 X86_ModelSpecificRegisters |
270 X86_PhysicalAddressExtensions |
271 X86_MachineCheckExtensions |
272 X86_CMPXCHG8Instruction |
273 X86_OnboardAPIC |
274 X86_SYSENTER_SYSEXIT |
275 X86_MemoryTypeRangeRegisters |
276 X86_PageGlobalEnable |
277 X86_MachineCheckArchitecture |
278 X86_CMOVInstruction |
279 X86_PageAttributeTable |
280 X86_36BitPSEs |
281 // X86_ProcessorSerialNumber |
282 X86_CLFLUSHInstruction |
283 // X86_DebugTraceStore |
284 // X86_ACPIViaMSR |
285 X86_MultimediaExtensions |
286 X86_FXSAVE_FXRSTOR |
287 X86_StreamingSIMDExtensions |
288 X86_StreamingSIMDExtensions2 |
289 // X86_CPUSelfSnoop |
290 // X86_HyperThreading |
291 // X86_AutomaticClockControl |
292 // X86_IA64Processor |
293 0;
294
295 //Bits which describe the system hardware capabilities
296 //XXX Figure out what these should be
297 auxv.push_back(auxv_t(M5_AT_HWCAP, features));
298 //The system page size
299 auxv.push_back(auxv_t(M5_AT_PAGESZ, X86ISA::VMPageSize));
300 //Frequency at which times() increments
301 auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
302 // For statically linked executables, this is the virtual address of the
303 // program header tables if they appear in the executable image
304 auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
305 // This is the size of a program header entry from the elf file.
306 auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
307 // This is the number of program headers from the original elf file.
308 auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
309 //Defined to be 100 in the kernel source.
310 //This is the address of the elf "interpreter", It should be set
311 //to 0 for regular executables. It should be something else
312 //(not sure what) for dynamic libraries.
313 auxv.push_back(auxv_t(M5_AT_BASE, 0));
314
315 //XXX Figure out what this should be.
316 auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
317 //The entry point to the program
318 auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
319 //Different user and group IDs
320 auxv.push_back(auxv_t(M5_AT_UID, uid()));
321 auxv.push_back(auxv_t(M5_AT_EUID, euid()));
322 auxv.push_back(auxv_t(M5_AT_GID, gid()));
323 auxv.push_back(auxv_t(M5_AT_EGID, egid()));
324 //Whether to enable "secure mode" in the executable
325 auxv.push_back(auxv_t(M5_AT_SECURE, 0));
326 //The string "x86_64" with unknown meaning
327 auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
328 }
329
330 //Figure out how big the initial stack needs to be
331
332 // A sentry NULL void pointer at the top of the stack.
333 int sentry_size = intSize;
334
335 //This is the name of the file which is present on the initial stack
336 //It's purpose is to let the user space linker examine the original file.
337 int file_name_size = filename.size() + 1;
338
339 string platform = "x86_64";
340 int aux_data_size = platform.size() + 1;
341
342 int env_data_size = 0;
343 for (int i = 0; i < envp.size(); ++i) {
344 env_data_size += envp[i].size() + 1;
345 }
346 int arg_data_size = 0;
347 for (int i = 0; i < argv.size(); ++i) {
348 arg_data_size += argv[i].size() + 1;
349 }
350
351 //The info_block needs to be padded so it's size is a multiple of the
352 //alignment mask. Also, it appears that there needs to be at least some
353 //padding, so if the size is already a multiple, we need to increase it
354 //anyway.
355 int base_info_block_size =
356 sentry_size + file_name_size + env_data_size + arg_data_size;
357
358 int info_block_size = roundUp(base_info_block_size, align);
359
360 int info_block_padding = info_block_size - base_info_block_size;
361
362 //Each auxilliary vector is two 8 byte words
363 int aux_array_size = intSize * 2 * (auxv.size() + 1);
364
365 int envp_array_size = intSize * (envp.size() + 1);
366 int argv_array_size = intSize * (argv.size() + 1);
367
368 int argc_size = intSize;
369
370 //Figure out the size of the contents of the actual initial frame
371 int frame_size =
372 aux_array_size +
373 envp_array_size +
374 argv_array_size +
375 argc_size;
376
377 //There needs to be padding after the auxiliary vector data so that the
378 //very bottom of the stack is aligned properly.
379 int partial_size = frame_size + aux_data_size;
380 int aligned_partial_size = roundUp(partial_size, align);
381 int aux_padding = aligned_partial_size - partial_size;
382
383 int space_needed =
384 info_block_size +
385 aux_data_size +
386 aux_padding +
387 frame_size;
388
389 stack_min = stack_base - space_needed;
390 stack_min = roundDown(stack_min, align);
391 stack_size = stack_base - stack_min;
392
393 // map memory
394 pTable->allocate(roundDown(stack_min, pageSize),
395 roundUp(stack_size, pageSize));
396
397 // map out initial stack contents
398 Addr sentry_base = stack_base - sentry_size;
399 Addr file_name_base = sentry_base - file_name_size;
400 Addr env_data_base = file_name_base - env_data_size;
401 Addr arg_data_base = env_data_base - arg_data_size;
402 Addr aux_data_base = arg_data_base - info_block_padding - aux_data_size;
403 Addr auxv_array_base = aux_data_base - aux_array_size - aux_padding;
404 Addr envp_array_base = auxv_array_base - envp_array_size;
405 Addr argv_array_base = envp_array_base - argv_array_size;
406 Addr argc_base = argv_array_base - argc_size;
407
408 DPRINTF(X86, "The addresses of items on the initial stack:\n");
409 DPRINTF(X86, "0x%x - file name\n", file_name_base);
410 DPRINTF(X86, "0x%x - env data\n", env_data_base);
411 DPRINTF(X86, "0x%x - arg data\n", arg_data_base);
412 DPRINTF(X86, "0x%x - aux data\n", aux_data_base);
413 DPRINTF(X86, "0x%x - auxv array\n", auxv_array_base);
414 DPRINTF(X86, "0x%x - envp array\n", envp_array_base);
415 DPRINTF(X86, "0x%x - argv array\n", argv_array_base);
416 DPRINTF(X86, "0x%x - argc \n", argc_base);
417 DPRINTF(X86, "0x%x - stack min\n", stack_min);
418
419 // write contents to stack
420
421 // figure out argc
422 uint64_t argc = argv.size();
423 uint64_t guestArgc = TheISA::htog(argc);
424
425 //Write out the sentry void *
426 uint64_t sentry_NULL = 0;
427 initVirtMem->writeBlob(sentry_base,
428 (uint8_t*)&sentry_NULL, sentry_size);
429
430 //Write the file name
431 initVirtMem->writeString(file_name_base, filename.c_str());
432
433 //Fix up the aux vector which points to the "platform" string
434 assert(auxv[auxv.size() - 1].a_type = M5_AT_PLATFORM);
435 auxv[auxv.size() - 1].a_val = aux_data_base;
436
437 //Copy the aux stuff
438 for(int x = 0; x < auxv.size(); x++)
439 {
440 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
441 (uint8_t*)&(auxv[x].a_type), intSize);
442 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
443 (uint8_t*)&(auxv[x].a_val), intSize);
444 }
445 //Write out the terminating zeroed auxilliary vector
446 const uint64_t zero = 0;
447 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
448 (uint8_t*)&zero, 2 * intSize);
449
450 initVirtMem->writeString(aux_data_base, platform.c_str());
451
452 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
453 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
454
455 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
456
457 //Set the stack pointer register
458 threadContexts[0]->setIntReg(StackPointerReg, stack_min);
459
460 Addr prog_entry = objFile->entryPoint();
461 threadContexts[0]->setPC(prog_entry);
462 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
463
464 //Align the "stack_min" to a page boundary.
465 stack_min = roundDown(stack_min, pageSize);
466
467 // num_processes++;
468 }