thread_state.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Generic definitions for dealing with Mach thread states.
  2. Copyright (C) 1994-2026 Free Software Foundation, Inc.
  3. 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 Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* Everything else is called `thread_state', but CMU's header file is
  16. called `thread_status'. Oh boy. */
  17. #include <mach/thread_status.h>
  18. /* The machine-dependent thread_state.h file can either define these
  19. macros, or just define PC and SP to the register names. */
  20. #ifndef MACHINE_THREAD_STATE_SET_PC
  21. #define MACHINE_THREAD_STATE_SET_PC(ts, pc) \
  22. ((ts)->PC = (unsigned long int) (pc))
  23. #endif
  24. #ifndef MACHINE_THREAD_STATE_SET_SP
  25. #ifdef STACK_GROWTH_UP
  26. #define MACHINE_THREAD_STATE_SET_SP(ts, stack, size) \
  27. ((ts)->SP = (unsigned long int) (stack))
  28. #else
  29. #define MACHINE_THREAD_STATE_SET_SP(ts, stack, size) \
  30. ((ts)->SP = (unsigned long int) (stack) + (size))
  31. #endif
  32. #endif
  33. /* Set up the thread state to call the given function on the given state.
  34. Dependning on architecture, this may imply more than just setting PC
  35. and SP. */
  36. #ifndef MACHINE_THREAD_STATE_SETUP_CALL
  37. #define MACHINE_THREAD_STATE_SETUP_CALL(ts, stack, size, func) \
  38. (MACHINE_THREAD_STATE_SET_PC (ts, func), \
  39. MACHINE_THREAD_STATE_SET_SP (ts, stack, size))
  40. #endif
  41. /* This copies architecture-specific bits from the current thread to the new
  42. thread state. */
  43. #ifndef MACHINE_THREAD_STATE_FIX_NEW
  44. # define MACHINE_THREAD_STATE_FIX_NEW(ts)
  45. #endif
  46. /* These functions are of use in machine-dependent signal trampoline
  47. implementations. */
  48. #include <string.h> /* size_t, memcpy */
  49. #include <mach/mach_interface.h> /* __thread_get_state */
  50. static __inline int
  51. machine_get_state (thread_t thread, struct machine_thread_all_state *state,
  52. int flavor, void *stateptr, void *scpptr, size_t size)
  53. {
  54. if (state->set & (1 << flavor))
  55. {
  56. /* Copy the saved state. */
  57. memcpy (scpptr, stateptr, size);
  58. return 1;
  59. }
  60. else
  61. {
  62. /* No one asked about this flavor of state before; fetch the state
  63. directly from the kernel into the sigcontext. */
  64. mach_msg_type_number_t got = (size / sizeof (int));
  65. return (! __thread_get_state (thread, flavor, (thread_state_t) scpptr, &got)
  66. && got == (size / sizeof (int)));
  67. }
  68. }
  69. static __inline int
  70. machine_get_basic_state (thread_t thread,
  71. struct machine_thread_all_state *state)
  72. {
  73. mach_msg_type_number_t count;
  74. if (state->set & (1 << MACHINE_THREAD_STATE_FLAVOR))
  75. return 1;
  76. count = MACHINE_THREAD_STATE_COUNT;
  77. if (__thread_get_state (thread, MACHINE_THREAD_STATE_FLAVOR,
  78. (natural_t *) &state->basic,
  79. &count) != KERN_SUCCESS
  80. || count != MACHINE_THREAD_STATE_COUNT)
  81. /* What kind of thread?? */
  82. return 0; /* XXX */
  83. state->set |= 1 << MACHINE_THREAD_STATE_FLAVOR;
  84. return 1;
  85. }