update from main archive 970214
[glibc/history.git] / mach / setup-thread.c
bloba00edacdfddff259cb1aea6ea9a265b0b3a3a60d
1 /* Copyright (C) 1991, 1994, 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <mach.h>
20 #include "thread_state.h"
21 #include <string.h>
22 #include <mach/machine/vm_param.h>
23 #include "sysdep.h" /* Defines stack direction. */
25 #define STACK_SIZE (16 * 1024 * 1024) /* 16MB, arbitrary. */
27 /* Give THREAD a stack and set it to run at PC when resumed.
28 If *STACK_SIZE is nonzero, that size of stack is allocated.
29 If *STACK_BASE is nonzero, that stack location is used.
30 If STACK_BASE is not null it is filled in with the chosen stack base.
31 If STACK_SIZE is not null it is filled in with the chosen stack size.
32 Regardless, an extra page of red zone is allocated off the end; this
33 is not included in *STACK_SIZE. */
35 kern_return_t
36 __mach_setup_thread (task_t task, thread_t thread, void *pc,
37 vm_address_t *stack_base, vm_size_t *stack_size)
39 kern_return_t error;
40 struct machine_thread_state ts;
41 mach_msg_type_number_t tssize = MACHINE_THREAD_STATE_COUNT;
42 vm_address_t stack;
43 vm_size_t size;
44 int anywhere = 0;
46 size = stack_size ? *stack_size ? : STACK_SIZE : STACK_SIZE;
48 if (stack_base && *stack_base)
49 stack = *stack_base;
50 else if (size == STACK_SIZE)
52 /* Cthreads has a bug that makes its stack-probing code fail if
53 the stack is too low in memory. It's bad to try and fix it there
54 until cthreads is integrated into libc, so we'll just do it here
55 by requesting a high address. When the cthreads bug is fixed,
56 this assignment to STACK should be changed to 0, and the ANYWHERE
57 argument to vm_allocate should be changed to 0. This comment should
58 be left, however, in order to confuse people who wonder why its
59 here. (Though perhaps that last sentence (and this one) should
60 be deleted to maximize the effect.) */
61 #ifdef STACK_GROWTH_DOWN
62 stack = VM_MAX_ADDRESS - size - __vm_page_size;
63 #else
64 stack = VM_MIN_ADDRESS;
65 #endif
67 else
68 anywhere = 1;
70 if (error = __vm_allocate (task, &stack, size + __vm_page_size, anywhere))
71 return error;
73 if (stack_size)
74 *stack_size = size;
76 memset (&ts, 0, sizeof (ts));
77 MACHINE_THREAD_STATE_SET_PC (&ts, pc);
78 #ifdef STACK_GROWTH_DOWN
79 if (stack_base)
80 *stack_base = stack + __vm_page_size;
81 ts.SP = stack + __vm_page_size + size;
82 #elif defined (STACK_GROWTH_UP)
83 if (stack_base)
84 *stack_base = stack;
85 ts.SP = stack;
86 stack += size;
87 #else
88 #error stack direction unknown
89 #endif
91 /* Create the red zone. */
92 if (error = __vm_protect (task, stack, __vm_page_size, 0, VM_PROT_NONE))
93 return error;
95 return __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
96 (int *) &ts, tssize);
99 weak_alias (__mach_setup_thread, mach_setup_thread)