setup-thread.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <mach.h>
  15. #include <mach/setup-thread.h>
  16. #include <thread_state.h>
  17. #include <string.h>
  18. #include <ldsodefs.h>
  19. #include "sysdep.h" /* Defines stack direction. */
  20. #define STACK_SIZE (16 * 1024 * 1024) /* 16MB, arbitrary. */
  21. static kern_return_t
  22. mach_setup_thread_impl (task_t task, thread_t thread, int is_call,
  23. void *pc, vm_address_t *stack_base,
  24. vm_size_t *stack_size)
  25. {
  26. kern_return_t error;
  27. struct machine_thread_state ts;
  28. mach_msg_type_number_t tssize = MACHINE_THREAD_STATE_COUNT;
  29. vm_address_t stack, stack_start;
  30. vm_size_t size;
  31. int anywhere;
  32. memset (&ts, 0, sizeof (ts));
  33. size = stack_size ? *stack_size ? : STACK_SIZE : STACK_SIZE;
  34. stack = stack_base ? *stack_base ? : 0 : 0;
  35. anywhere = !stack_base || !*stack_base;
  36. error = __vm_allocate (task, &stack, size + __vm_page_size, anywhere);
  37. if (error)
  38. return error;
  39. if (stack_size)
  40. *stack_size = size;
  41. #ifdef STACK_GROWTH_DOWN
  42. stack_start = stack + __vm_page_size;
  43. #elif defined (STACK_GROWTH_UP)
  44. stack_start = stack;
  45. stack += size;
  46. #else
  47. #error stack direction unknown
  48. #endif
  49. if (stack_base)
  50. *stack_base = stack_start;
  51. if (is_call)
  52. MACHINE_THREAD_STATE_SETUP_CALL (&ts, stack_start, size, pc);
  53. else
  54. {
  55. MACHINE_THREAD_STATE_SET_PC (&ts, pc);
  56. MACHINE_THREAD_STATE_SET_SP (&ts, stack_start, size);
  57. }
  58. /* Create the red zone. */
  59. if (error = __vm_protect (task, stack, __vm_page_size, 0, VM_PROT_NONE))
  60. return error;
  61. return __thread_set_state (thread, MACHINE_NEW_THREAD_STATE_FLAVOR,
  62. (natural_t *) &ts, tssize);
  63. }
  64. /* Give THREAD a stack and set it to run at PC when resumed.
  65. If *STACK_SIZE is nonzero, that size of stack is allocated.
  66. If *STACK_BASE is nonzero, that stack location is used.
  67. If STACK_BASE is not null it is filled in with the chosen stack base.
  68. If STACK_SIZE is not null it is filled in with the chosen stack size.
  69. Regardless, an extra page of red zone is allocated off the end; this
  70. is not included in *STACK_SIZE. */
  71. kern_return_t
  72. __mach_setup_thread (task_t task, thread_t thread, void *pc,
  73. vm_address_t *stack_base, vm_size_t *stack_size)
  74. {
  75. return mach_setup_thread_impl (task, thread, 0, pc, stack_base, stack_size);
  76. }
  77. weak_alias (__mach_setup_thread, mach_setup_thread)
  78. kern_return_t
  79. __mach_setup_thread_call (task_t task, thread_t thread, void *pc,
  80. vm_address_t *stack_base, vm_size_t *stack_size)
  81. {
  82. return mach_setup_thread_impl (task, thread, 1, pc, stack_base, stack_size);
  83. }
  84. /* Give THREAD a TLS area. */
  85. kern_return_t
  86. __mach_setup_tls (thread_t thread)
  87. {
  88. tcbhead_t *tcb = _dl_allocate_tls (NULL);
  89. if (tcb == NULL)
  90. return KERN_RESOURCE_SHORTAGE;
  91. return _hurd_tls_new (thread, tcb);
  92. }
  93. weak_alias (__mach_setup_tls, mach_setup_tls)