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