ppc-linux.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*Copyright (C) 2015-2022 Free Software Foundation, Inc.
  2. This file is part of GDB.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program 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
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include "gdbsupport/common-defs.h"
  14. #include "ppc-linux.h"
  15. #include "nat/gdb_ptrace.h"
  16. #include <elf.h>
  17. #ifdef HAVE_GETAUXVAL
  18. #include <sys/auxv.h>
  19. #endif
  20. #ifdef __powerpc64__
  21. /* Get the HWCAP from the process of GDB or GDBserver. If success,
  22. save it in *VALP. */
  23. static void
  24. ppc64_host_hwcap (unsigned long *valp)
  25. {
  26. #ifdef HAVE_GETAUXVAL
  27. *valp = getauxval (AT_HWCAP);
  28. #else
  29. unsigned long data[2];
  30. FILE *f = fopen ("/proc/self/auxv", "r");
  31. if (f == NULL)
  32. return;
  33. while (fread (data, sizeof (data), 1, f) > 0)
  34. {
  35. if (data[0] == AT_HWCAP)
  36. {
  37. *valp = data[1];
  38. break;
  39. }
  40. }
  41. fclose (f);
  42. #endif /* HAVE_GETAUXVAL */
  43. }
  44. static inline int
  45. ppc64_64bit_inferior_p (long msr)
  46. {
  47. unsigned long ppc_host_hwcap = 0;
  48. /* Get host's HWCAP to check whether the machine is Book E. */
  49. ppc64_host_hwcap (&ppc_host_hwcap);
  50. /* We actually have a 64-bit inferior only if the certain bit of the
  51. MSR is set. The PowerISA Book III-S MSR is different from the
  52. PowerISA Book III-E MSR. The Book III-S MSR is 64 bits wide, and
  53. its MSR[SF] is the bit 0 of a 64-bit value. Book III-E MSR is 32
  54. bits wide, and its MSR[CM] is the bit 0 of a 32-bit value. */
  55. if (ppc_host_hwcap & PPC_FEATURE_BOOKE)
  56. return msr & 0x80000000;
  57. else
  58. return msr < 0;
  59. }
  60. #endif
  61. int
  62. ppc_linux_target_wordsize (int tid)
  63. {
  64. int wordsize = 4;
  65. /* Check for 64-bit inferior process. This is the case when the host is
  66. 64-bit, and in addition the top bit of the MSR register is set. */
  67. #ifdef __powerpc64__
  68. long msr;
  69. errno = 0;
  70. msr = (long) ptrace (PTRACE_PEEKUSER, tid, PT_MSR * 8, 0);
  71. if (errno == 0 && ppc64_64bit_inferior_p (msr))
  72. wordsize = 8;
  73. #endif
  74. return wordsize;
  75. }