6 #include "kern_constants.h"
10 static void segfault(int sig)
15 static int page_ok(unsigned long page)
17 unsigned long *address = (unsigned long *) (page << UM_KERN_PAGE_SHIFT);
18 unsigned long n = ~0UL;
23 * First see if the page is readable. If it is, it may still
24 * be a VDSO, so we go on to see if it's writable. If not
25 * then try mapping memory there. If that fails, then we're
26 * still in the kernel area. As a sanity check, we'll fail if
27 * the mmap succeeds, but gives us an address different from
33 mapped = mmap(address, UM_KERN_PAGE_SIZE,
34 PROT_READ | PROT_WRITE,
35 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
36 if (mapped == MAP_FAILED)
38 if (mapped != address)
43 * Now, is it writeable? If so, then we're in user address
44 * space. If not, then try mprotecting it and try the write
47 if (setjmp(buf) == 0) {
51 } else if (mprotect(address, UM_KERN_PAGE_SIZE,
52 PROT_READ | PROT_WRITE) != 0)
55 if (setjmp(buf) == 0) {
62 munmap(mapped, UM_KERN_PAGE_SIZE);
66 unsigned long os_get_task_size(void)
68 struct sigaction sa, old;
69 unsigned long bottom = 0;
71 * A 32-bit UML on a 64-bit host gets confused about the VDSO at
72 * 0xffffe000. It is mapped, is readable, can be reprotected writeable
73 * and written. However, exec discovers later that it can't be
74 * unmapped. So, just set the highest address to be checked to just
75 * below it. This might waste some address space on 4G/4G 32-bit
76 * hosts, but shouldn't hurt otherwise.
78 unsigned long top = 0xffffd000 >> UM_KERN_PAGE_SHIFT;
81 printf("Locating the top of the address space ... ");
85 * We're going to be longjmping out of the signal handler, so
86 * SA_DEFER needs to be set.
88 sa.sa_handler = segfault;
89 sigemptyset(&sa.sa_mask);
90 sa.sa_flags = SA_NODEFER;
91 sigaction(SIGSEGV, &sa, &old);
93 if (!page_ok(bottom)) {
94 fprintf(stderr, "Address 0x%x no good?\n",
95 bottom << UM_KERN_PAGE_SHIFT);
99 /* This could happen with a 4G/4G split */
104 test = bottom + (top - bottom) / 2;
109 } while (top - bottom > 1);
112 /* Restore the old SIGSEGV handling */
113 sigaction(SIGSEGV, &old, NULL);
115 top <<= UM_KERN_PAGE_SHIFT;
116 printf("0x%x\n", top);