Blame sysdeps/mach/thread_state.h

Packit 6c4009
/* Generic definitions for dealing with Mach thread states.
Packit 6c4009
   Copyright (C) 1994-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Everything else is called `thread_state', but CMU's header file is
Packit 6c4009
   called `thread_status'.  Oh boy.  */
Packit 6c4009
#include <mach/thread_status.h>
Packit 6c4009
Packit 6c4009
/* The machine-dependent thread_state.h file can either define these
Packit 6c4009
   macros, or just define PC and SP to the register names.  */
Packit 6c4009
Packit 6c4009
#ifndef MACHINE_THREAD_STATE_SET_PC
Packit 6c4009
#define MACHINE_THREAD_STATE_SET_PC(ts, pc) \
Packit 6c4009
  ((ts)->PC = (unsigned long int) (pc))
Packit 6c4009
#endif
Packit 6c4009
#ifndef MACHINE_THREAD_STATE_SET_SP
Packit 6c4009
#ifdef STACK_GROWTH_UP
Packit 6c4009
#define MACHINE_THREAD_STATE_SET_SP(ts, stack, size) \
Packit 6c4009
  ((ts)->SP = (unsigned long int) (stack))
Packit 6c4009
#else
Packit 6c4009
#define MACHINE_THREAD_STATE_SET_SP(ts, stack, size) \
Packit 6c4009
  ((ts)->SP = (unsigned long int) (stack) + (size))
Packit 6c4009
#endif
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* This copies architecture-specific bits from the current thread to the new
Packit 6c4009
   thread state.  */
Packit 6c4009
#ifndef MACHINE_THREAD_STATE_FIX_NEW
Packit 6c4009
# define MACHINE_THREAD_STATE_FIX_NEW(ts)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* These functions are of use in machine-dependent signal trampoline
Packit 6c4009
   implementations.  */
Packit 6c4009
Packit 6c4009
#include <string.h>		/* size_t, memcpy */
Packit 6c4009
#include <mach/mach_interface.h> /* __thread_get_state */
Packit 6c4009
Packit 6c4009
static inline int
Packit 6c4009
machine_get_state (thread_t thread, struct machine_thread_all_state *state,
Packit 6c4009
		   int flavor, void *stateptr, void *scpptr, size_t size)
Packit 6c4009
{
Packit 6c4009
  if (state->set & (1 << flavor))
Packit 6c4009
    {
Packit 6c4009
      /* Copy the saved state.  */
Packit 6c4009
      memcpy (scpptr, stateptr, size);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* No one asked about this flavor of state before; fetch the state
Packit 6c4009
	 directly from the kernel into the sigcontext.  */
Packit 6c4009
      mach_msg_type_number_t got = (size / sizeof (int));
Packit 6c4009
      return (! __thread_get_state (thread, flavor, scpptr, &got)
Packit 6c4009
	      && got == (size / sizeof (int)));
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static inline int
Packit 6c4009
machine_get_basic_state (thread_t thread,
Packit 6c4009
			 struct machine_thread_all_state *state)
Packit 6c4009
{
Packit 6c4009
  mach_msg_type_number_t count;
Packit 6c4009
Packit 6c4009
  if (state->set & (1 << MACHINE_THREAD_STATE_FLAVOR))
Packit 6c4009
    return 1;
Packit 6c4009
Packit 6c4009
  count = MACHINE_THREAD_STATE_COUNT;
Packit 6c4009
  if (__thread_get_state (thread, MACHINE_THREAD_STATE_FLAVOR,
Packit 6c4009
			  (natural_t *) &state->basic,
Packit 6c4009
			  &count) != KERN_SUCCESS ||
Packit 6c4009
      count != MACHINE_THREAD_STATE_COUNT)
Packit 6c4009
    /* What kind of thread?? */
Packit 6c4009
    return 0;			/* XXX */
Packit 6c4009
Packit 6c4009
  state->set |= 1 << MACHINE_THREAD_STATE_FLAVOR;
Packit 6c4009
  return 1;
Packit 6c4009
}