11 static void segfault(int sig)
16 static int page_ok(unsigned long page)
18 unsigned long *address = (unsigned long *) (page << UM_KERN_PAGE_SHIFT);
19 unsigned long n = ~0UL;
24 * First see if the page is readable. If it is, it may still
25 * be a VDSO, so we go on to see if it's writable. If not
26 * then try mapping memory there. If that fails, then we're
27 * still in the kernel area. As a sanity check, we'll fail if
28 * the mmap succeeds, but gives us an address different from
34 mapped = mmap(address, UM_KERN_PAGE_SIZE,
35 PROT_READ | PROT_WRITE,
36 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
37 if (mapped == MAP_FAILED)
39 if (mapped != address)
44 * Now, is it writeable? If so, then we're in user address
45 * space. If not, then try mprotecting it and try the write
48 if (setjmp(buf) == 0) {
52 } else if (mprotect(address, UM_KERN_PAGE_SIZE,
53 PROT_READ | PROT_WRITE) != 0)
56 if (setjmp(buf) == 0) {
63 munmap(mapped, UM_KERN_PAGE_SIZE);
67 unsigned long os_get_top_address(void)
69 struct sigaction sa, old;
70 unsigned long bottom = 0;
72 * A 32-bit UML on a 64-bit host gets confused about the VDSO at
73 * 0xffffe000. It is mapped, is readable, can be reprotected writeable
74 * and written. However, exec discovers later that it can't be
75 * unmapped. So, just set the highest address to be checked to just
76 * below it. This might waste some address space on 4G/4G 32-bit
77 * hosts, but shouldn't hurt otherwise.
79 unsigned long top = 0xffffd000 >> UM_KERN_PAGE_SHIFT;
80 unsigned long test, original;
82 printf("Locating the bottom of the address space ... ");
86 * We're going to be longjmping out of the signal handler, so
87 * SA_DEFER needs to be set.
89 sa.sa_handler = segfault;
90 sigemptyset(&sa.sa_mask);
91 sa.sa_flags = SA_NODEFER;
92 if (sigaction(SIGSEGV, &sa, &old)) {
93 perror("os_get_top_address");
97 /* Manually scan the address space, bottom-up, until we find
98 * the first valid page (or run out of them).
100 for (bottom = 0; bottom < top; bottom++) {
105 /* If we've got this far, we ran out of pages. */
107 fprintf(stderr, "Unable to determine bottom of address "
112 printf("0x%x\n", bottom << UM_KERN_PAGE_SHIFT);
113 printf("Locating the top of the address space ... ");
118 /* This could happen with a 4G/4G split */
123 test = bottom + (top - bottom) / 2;
128 } while (top - bottom > 1);
131 /* Restore the old SIGSEGV handling */
132 if (sigaction(SIGSEGV, &old, NULL)) {
133 perror("os_get_top_address");
136 top <<= UM_KERN_PAGE_SHIFT;
137 printf("0x%x\n", top);
144 unsigned long os_get_top_address(void)
146 /* The old value of CONFIG_TOP_ADDR */