Back to index

glibc  2.9
setup-thread.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,94,1995,1997,2002,2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <mach.h>
00020 #include <thread_state.h>
00021 #include <string.h>
00022 #include <mach/machine/vm_param.h>
00023 #include "sysdep.h"         /* Defines stack direction.  */
00024 
00025 #define       STACK_SIZE    (16 * 1024 * 1024) /* 16MB, arbitrary.  */
00026 
00027 /* Give THREAD a stack and set it to run at PC when resumed.
00028    If *STACK_SIZE is nonzero, that size of stack is allocated.
00029    If *STACK_BASE is nonzero, that stack location is used.
00030    If STACK_BASE is not null it is filled in with the chosen stack base.
00031    If STACK_SIZE is not null it is filled in with the chosen stack size.
00032    Regardless, an extra page of red zone is allocated off the end; this
00033    is not included in *STACK_SIZE.  */
00034 
00035 kern_return_t
00036 __mach_setup_thread (task_t task, thread_t thread, void *pc,
00037                    vm_address_t *stack_base, vm_size_t *stack_size)
00038 {
00039   kern_return_t error;
00040   struct machine_thread_state ts;
00041   mach_msg_type_number_t tssize = MACHINE_THREAD_STATE_COUNT;
00042   vm_address_t stack;
00043   vm_size_t size;
00044   int anywhere;
00045 
00046   size = stack_size ? *stack_size ? : STACK_SIZE : STACK_SIZE;
00047   stack = stack_base ? *stack_base ? : 0 : 0;
00048   anywhere = !stack_base || !*stack_base;
00049 
00050   error = __vm_allocate (task, &stack, size + __vm_page_size, anywhere);
00051   if (error)
00052     return error;
00053 
00054   if (stack_size)
00055     *stack_size = size;
00056 
00057   memset (&ts, 0, sizeof (ts));
00058   MACHINE_THREAD_STATE_SET_PC (&ts, pc);
00059 #ifdef STACK_GROWTH_DOWN
00060   if (stack_base)
00061     *stack_base = stack + __vm_page_size;
00062   ts.SP = stack + __vm_page_size + size;
00063 #elif defined (STACK_GROWTH_UP)
00064   if (stack_base)
00065     *stack_base = stack;
00066   ts.SP = stack;
00067   stack += size;
00068 #else
00069   #error stack direction unknown
00070 #endif
00071 
00072   /* Create the red zone.  */
00073   if (error = __vm_protect (task, stack, __vm_page_size, 0, VM_PROT_NONE))
00074     return error;
00075 
00076   return __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
00077                           (natural_t *) &ts, tssize);
00078 }
00079 
00080 weak_alias (__mach_setup_thread, mach_setup_thread)