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