#include <mach.h>
#include "thread_state.h"
#include <string.h>
+#include <mach/machine/vm_param.h>
#define STACK_SIZE (16 * 1024 * 1024) /* 16MB, arbitrary. */
mach_msg_type_number_t tssize = MACHINE_THREAD_STATE_COUNT;
vm_address_t stack;
- if (error = __vm_allocate (task, &stack, STACK_SIZE, 1))
+ /* Cthreads has a bug that makes its stack-probing code fail if
+ the stack is too low in memory. It's bad to try and fix it there
+ until cthreads is integrated into libc, so we'll just do it here
+ by requesting a high address. When the cthreads bug is fixed,
+ this assignment to STACK should be changed to 0, and the ANYWHERE
+ argument to vm_allocate should be changed to 0. This comment should
+ be left, however, in order to confuse people who wonder why its
+ here. (Though perhaps that last sentence (and this one) should
+ be deleted to maximize the effect.) */
+ stack = VM_MAX_ADDRESS - STACK_SIZE - __vm_page_size;
+
+ if (error = __vm_allocate (task, &stack, STACK_SIZE + __vm_page_size, 1))
return error;
+ /* Create a red zone */
+ if (error = __vm_protect (task, stack, __vm_page_size, 0, 0))
+ return error;
+
memset (&ts, 0, sizeof (ts));
ts.PC = (int) pc;
ts.SP = stack + STACK_SIZE;