Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(332)

Side by Side Diff: crosstest/stack_hack.x8664.c

Issue 1273153002: Subzero. Native 64-bit int arithmetic on x86-64. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Removes the x8664-specific xtest target. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 //===- subzero/crosstest/stack_hack.x8664.c.h - X8664 stack hack ----------===//
Jim Stichnoth 2015/08/10 16:08:04 no .h
John 2015/08/10 20:41:16 Done.
2 //
3 // The Subzero Code Generator
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implements main() for crosstests in x86-64.
11 //
12 //===----------------------------------------------------------------------===//
13 #include <assert.h>
14 #include <stdint.h>
15
16 #include <sys/mman.h>
17
18 // X8664_STACK_HACK needs to be defined before xdefs.h is included.
19 #define X8664_STACK_HACK
20 #include "xdefs.h"
21
22 /// xSetStack is used to set %rsp to NewRsp. OldRsp is a pointer that will be
23 /// used to save the old %rsp value.
24 #define xSetStack(NewRsp, OldRsp) \
25 do { \
26 __asm__ volatile("xchgq %1, %%rsp\n\t" \
27 "xchgq %1, %0" \
28 : "=r"(*(OldRsp)) \
29 : "r"(NewRsp)); \
30 } while (0)
31
32 extern int wrapped_main(int argc, char *argv[]);
33
34 unsigned char *xStackStart(uint32 StackEnd, uint32 Size) {
35 const uint32 PageBoundary = 4 << 20; // 4 MB.
Jim Stichnoth 2015/08/10 16:08:04 May want to "(void)PageBoundary;" in case we ever
John 2015/08/10 20:41:16 Done.
36 const uint64 StackStart = StackEnd - Size;
37 assert(StackStart + (PageBoundary - 1) & ~(PageBoundary - 1) &&
38 "StackStart not aligned to page boundary.");
39 assert((StackStart & 0xFFFFFFFF00000000ull) == 0 && "StackStart wraps.");
40 return (unsigned char *)StackStart;
41 }
42
43 unsigned char *xAllocStack(uint64 StackEnd, uint32 Size) {
44 assert((StackEnd & 0xFFFFFFFF00000000ull) == 0 && "Invalid StackEnd.");
45 void *Foo = xStackStart(StackEnd, Size);
Jim Stichnoth 2015/08/10 16:08:04 Foo?
John 2015/08/10 20:41:16 Bar instead?
Jim Stichnoth 2015/08/11 16:01:36 Made me look, well played sir.
46 void *Stack =
47 mmap(xStackStart(StackEnd, Size), Size, PROT_READ | PROT_WRITE,
48 MAP_FIXED | MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS, -1, 0);
49 assert(Stack != MAP_FAILED && "mmap failed. no stack.");
50 return Stack;
51 }
52
53 void xDeallocStack(uint64 StackEnd, uint32 Size) {
54 assert((StackEnd & 0xFFFFFFFF00000000ull) == 0 && "Invalid StackEnd.");
55 munmap(xStackStart(StackEnd, Size), Size);
56 }
57
58 int main(int argc, char *argv[]) {
59 // These "locals" need to live **NOT** in the stack.
60 static int Argc;
61 static char **Argv;
62 static const uint32_t StackEnd = 0x80000000;
63 static const uint32_t StackSize = 40 * 1024 * 1024;
64 static unsigned char *new_rsp;
65 static unsigned char *old_rsp;
66 static unsigned char *dummy_rsp;
67 static int Failures;
68 Argc = argc;
69 Argv = argv;
70 new_rsp = xAllocStack(StackEnd, StackSize) + StackSize;
71 xSetStack(new_rsp, &old_rsp);
72 Failures = wrapped_main(Argc, Argv);
73 xSetStack(old_rsp, &new_rsp);
74 xDeallocStack(StackEnd, StackSize);
75 return Failures;
76 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698