summaryrefslogtreecommitdiff
path: root/libgcc/config/rs6000/aix-unwind.h
blob: 9f529d7f824adf380fc64b09f665e46ab5efe847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* DWARF2 EH unwinding support for AIX.
   Copyright (C) 2011-2020 Free Software Foundation, Inc.

   This file is part of GCC.

   GCC is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GCC is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.

   Under Section 7 of GPL version 3, you are granted additional
   permissions described in the GCC Runtime Library Exception, version
   3.1, as published by the Free Software Foundation.

   You should have received a copy of the GNU General Public License and
   a copy of the GCC Runtime Library Exception along with this program;
   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   <http://www.gnu.org/licenses/>.  */

/* Useful register numbers.  */

#define R_LR             65
#define R_CR2            70
#define R_XER            76
#define R_FIRST_ALTIVEC  77
#define R_VRSAVE        109
#define R_VSCR          110

/* If the current unwind info (FS) does not contain explicit info
   saving R2, then we have to do a minor amount of code reading to
   figure out if it was saved.  The big problem here is that the
   code that does the save/restore is generated by the linker, so
   we have no good way to determine at compile time what to do.  */

#ifdef __64BIT__
#define MD_FROB_UPDATE_CONTEXT(CTX, FS)					\
  do {									\
    if ((FS)->regs.reg[2].how == REG_UNSAVED)				\
      {									\
	unsigned int *insn						\
	  = (unsigned int *)						\
	    _Unwind_GetGR ((CTX), R_LR);				\
	if (*insn == 0xE8410028)					\
	  _Unwind_SetGRPtr ((CTX), 2, (CTX)->cfa + 40);			\
      }									\
  } while (0)
#else
#define MD_FROB_UPDATE_CONTEXT(CTX, FS)					\
  do {									\
    if ((FS)->regs.reg[2].how == REG_UNSAVED)				\
      {									\
	unsigned int *insn						\
	  = (unsigned int *)						\
	    _Unwind_GetGR ((CTX), R_LR);				\
	if (*insn == 0x80410014)					\
	  _Unwind_SetGRPtr ((CTX), 2, (CTX)->cfa + 20);			\
      }									\
  } while (0)
#endif

/* Now on to MD_FALLBACK_FRAME_STATE_FOR.
   32bit AIX 5.2, 5.3, 6.1, 7.X and
   64bit AIX 6.1, 7.X only at this stage.  */

#include <stdlib.h>
#include <stddef.h>
#include <signal.h>
#include <sys/machine.h>

#ifdef __64BIT__

typedef struct __context64 mstate_t;

#else

typedef struct mstsave mstate_t;

#endif

#define MD_FALLBACK_FRAME_STATE_FOR ppc_aix_fallback_frame_state

/* If we are compiling on AIX < 5.3, the VMX related datastructs are not
   defined and we take measures to obtain proper runtime behavior if the
   compiled code happens to run on a later version with VMX enabled.  */

#ifndef MSR_VMX
#define MSR_VMX 0x2000000
#endif

typedef unsigned int uint;
typedef struct { uint v[4]; } vreg_t;
typedef struct {
  vreg_t regs[32];
  uint   pad1 [3];
  uint   vscr;
  uint   vrsave;
  uint   pad2 [3];
} vstate_t;

#define EXT_CONTEXT_MARK 0x45435458
#define EXT_CONTEXT_SIZE 4096
#define BUMPER_SIZE (EXT_CONTEXT_SIZE - sizeof(vstate_t) - (5 * sizeof(int)))

typedef struct {
  uint     pad1 [4];
  vstate_t vstate;
  char     bumper [BUMPER_SIZE];
  int      mark; 
} extended_context_t;

typedef struct {
  char bumper [offsetof (ucontext_t, uc_stack) + sizeof (stack_t)];
  extended_context_t * ectx;
  int mark;
} vmx_ucontext_t;

/* Determine whether CONTEXT designates a signal handler, and return the
   associated ucontext_t address if so.  Return NULL otherwise.  */

static ucontext_t *
ucontext_for (struct _Unwind_Context *context)
{
  const unsigned int * ra = context->ra;

  /* AIX 5.2, 5.3, 6.1 and 7.X, threaded or not, share common patterns
     and feature variants depending on the configured kernel (unix_mp
     or unix_64).  */

#ifdef __64BIT__
  if (*(ra - 5) == 0x4c00012c     /* isync             */
      && *(ra - 4) == 0xe8ec0000  /* ld      r7,0(r12) */
      && *(ra - 3) == 0xe84c0008  /* ld      r2,8(r12) */
      && *(ra - 2) == 0x7ce903a6  /* mtctr   r7        */
      && *(ra - 1) == 0x4e800421  /* bctrl             */
      && *(ra - 0) == 0x7de27b78) /* mr      r2,r15   <-- context->ra */
    {
      /* unix_64 */
      if (*(ra - 6) == 0x7d000164)  /* mtmsrd  r8 */
	{
	  /* AIX 6.1, 7.1 and 7.2 */
	  return (ucontext_t *)(context->cfa + 0x70);
	}
    }
#else
  if (*(ra - 5) == 0x4c00012c     /* isync             */
      && *(ra - 4) == 0x80ec0000  /* lwz     r7,0(r12) */
      && *(ra - 3) == 0x804c0004  /* lwz     r2,4(r12) */
      && *(ra - 2) == 0x7ce903a6  /* mtctr   r7        */
      && *(ra - 1) == 0x4e800421  /* bctrl             */
      && *(ra - 0) == 0x7dc37378) /* mr      r3,r14   <-- context->ra */
    {
      /* unix_64 */
      if (*(ra - 6) == 0x7d000164)  /* mtmsrd  r8 */
	{
	  switch (*(ra + 18))
	    {
	      /* AIX 5.2 */
	    case 0x835a0520: /* lwz r26,1312(r26) */ 
	      return (ucontext_t *)(context->cfa + 0x70);

	      /* AIX 5.3 */
	    case 0x835a0570:  /* lwz r26,1392(r26) */
	      return (ucontext_t *)(context->cfa + 0x40);

	      /* AIX 6.1 and 7.1 */
	    case 0x2c1a0000:  /* cmpwi   r26,0 */
	      return (ucontext_t *)(context->cfa + 0x40);

	      /* AIX 7.2 */
	    case 0x3800000a:  /* li   r0,A */
	      return (ucontext_t *)(context->cfa + 0x40);

	    default:
	      return 0;
	    }
	}

      /* unix_mp */
      if (*(ra - 6) == 0x7d000124)  /* mtmsr  r8 */
	{
	  typedef struct {
	    char pad[56];
	    ucontext_t ucontext;
	    siginfo_t siginfo;
	  } aix52_stack_t;

	  aix52_stack_t * frame = (aix52_stack_t *) context->cfa;
	  return &frame->ucontext;
	}
    }
#endif
  return 0;
}

/* The fallback proper.  */

#ifdef __LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__
#define RETURN_COLUMN __LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__
#else
#define RETURN_COLUMN ARG_POINTER_REGNUM
#endif

#define REGISTER_CFA_OFFSET_FOR(FS,REGNO,ADDR,CFA)\
do { \
(FS)->regs.reg[REGNO].how = REG_SAVED_OFFSET; \
(FS)->regs.reg[REGNO].loc.offset = (long) (ADDR) - (CFA); \
} while (0)

static _Unwind_Reason_Code
ppc_aix_fallback_frame_state (struct _Unwind_Context *context,
			      _Unwind_FrameState *fs)
{
  ucontext_t * uctx = ucontext_for (context);
  mstate_t * mctx;

  long new_cfa;
  int i;

  if (uctx == NULL)
    return _URC_END_OF_STACK;

  mctx = &uctx->uc_mcontext.jmp_context;

  /* The "kernel" frame cfa is the stack pointer at the signal occurrence
     point.  */
  new_cfa = mctx->gpr[__LIBGCC_STACK_POINTER_REGNUM__];

  fs->regs.cfa_how = CFA_REG_OFFSET;
  fs->regs.cfa_reg = __LIBGCC_STACK_POINTER_REGNUM__;
  fs->regs.cfa_offset = new_cfa - (long) context->cfa;

  /* And we state how to find the various registers it has saved with
     relative offset rules from there.  */

  for (i = 0; i < 32; i++)
    if (i != __LIBGCC_STACK_POINTER_REGNUM__)
      REGISTER_CFA_OFFSET_FOR (fs, i, &mctx->gpr[i], new_cfa);

  REGISTER_CFA_OFFSET_FOR (fs, R_CR2, &mctx->cr, new_cfa);
  REGISTER_CFA_OFFSET_FOR (fs, R_XER, &mctx->xer, new_cfa);
  REGISTER_CFA_OFFSET_FOR (fs, R_LR, &mctx->lr, new_cfa);

  fs->retaddr_column = RETURN_COLUMN;
  REGISTER_CFA_OFFSET_FOR (fs, RETURN_COLUMN, &mctx->iar, new_cfa);
  fs->signal_frame = 1;

  /* Honor FP Ever Used ...   */
  if (mctx->fpeu)
    {
      for (i = 0; i < 32; i++)
	REGISTER_CFA_OFFSET_FOR (fs, i+32, &mctx->fpr[i], new_cfa);
    }

  /* Honor VMX context, if any.  We expect the msr bit never to be set in
     environments where there is no VMX support, e.g. on AIX < 5.3.  */
  if (mctx->msr & MSR_VMX)
    {
      vmx_ucontext_t * uc = (vmx_ucontext_t *) uctx;

      if (uc->mark == EXT_CONTEXT_MARK && uc->ectx->mark == EXT_CONTEXT_MARK)
	{
	  vstate_t * vstate = &uc->ectx->vstate;

	  for (i = 0; i < 32; i++)
	    REGISTER_CFA_OFFSET_FOR
	    (fs, i+R_FIRST_ALTIVEC, &vstate->regs[i], new_cfa);

	  REGISTER_CFA_OFFSET_FOR (fs, R_VSCR, &vstate->vscr, new_cfa);
	  REGISTER_CFA_OFFSET_FOR (fs, R_VRSAVE, &vstate->vrsave, new_cfa);
	}
    }

  return _URC_NO_REASON;
}