c9381c3d3c4aaccce30e03b41a5c377cf993fe3f
[platform/upstream/ltrace.git] / sysdeps / linux-gnu / ppc / fetch.c
1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 2012 Petr Machata, Red Hat Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  * 02110-1301 USA
19  */
20
21 #include <assert.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/ucontext.h>
26
27 #include "backend.h"
28 #include "fetch.h"
29 #include "type.h"
30 #include "ptrace.h"
31 #include "proc.h"
32 #include "value.h"
33 #include "ltrace-elf.h"
34
35 static int allocate_gpr(struct fetch_context *ctx, struct process *proc,
36                         struct arg_type_info *info, struct value *valuep,
37                         size_t off, bool is_hfa_type);
38
39 /* Floating point registers have the same width on 32-bit as well as
40  * 64-bit PPC, but <ucontext.h> presents a different API depending on
41  * whether ltrace is PPC32 or PPC64.
42  *
43  * This is PPC64 definition.  The PPC32 is simply an array of 33
44  * doubles, and doesn't contain the terminating pad.  Both seem
45  * compatible enough.  */
46 struct fpregs_t
47 {
48         double fpregs[32];
49         double fpscr;
50         unsigned int _pad[2];
51 };
52
53 typedef uint32_t gregs32_t[48];
54 typedef uint64_t gregs64_t[48];
55
56 struct fetch_context {
57         arch_addr_t stack_pointer;
58         int greg;
59         int freg;
60         int ret_struct;
61
62         union {
63                 gregs32_t r32;
64                 gregs64_t r64;
65         } regs;
66         struct fpregs_t fpregs;
67         int vgreg;
68         int struct_size;
69         int struct_hfa_size;
70         int struct_hfa_count;
71 };
72
73 static int
74 fetch_context_init(struct process *proc, struct fetch_context *context)
75 {
76         context->greg = 3;
77         context->freg = 1;
78
79         if (proc->e_machine == EM_PPC)
80                 context->stack_pointer = proc->stack_pointer + 8;
81         else
82                 context->stack_pointer = proc->stack_pointer
83                         + STACK_FRAME_OVERHEAD;
84
85         /* When ltrace is 64-bit, we might use PTRACE_GETREGS to
86          * obtain 64-bit as well as 32-bit registers.  But if we do it
87          * this way, 32-bit ltrace can obtain 64-bit registers.
88          *
89          * XXX this direction is not supported as of this writing, but
90          * should be eventually.  */
91         if (proc->e_machine == EM_PPC64) {
92                 if (ptrace(PTRACE_GETREGS64, proc->pid, 0,
93                            &context->regs.r64) < 0)
94                         return -1;
95         } else {
96 #ifdef __powerpc64__
97                 if (ptrace(PTRACE_GETREGS, proc->pid, 0,
98                           &context->regs.r64) < 0)
99                         return -1;
100                 unsigned i;
101                 for (i = 0; i < sizeof(context->regs.r64)/8; ++i)
102                         context->regs.r32[i] = context->regs.r64[i];
103 #else
104                 if (ptrace(PTRACE_GETREGS, proc->pid, 0,
105                           &context->regs.r32) < 0)
106                         return -1;
107 #endif
108         }
109
110         if (ptrace(PTRACE_GETFPREGS, proc->pid, 0, &context->fpregs) < 0)
111                 return -1;
112
113         return 0;
114 }
115
116 struct fetch_context *
117 arch_fetch_arg_init(enum tof type, struct process *proc,
118                     struct arg_type_info *ret_info)
119 {
120         struct fetch_context *context = malloc(sizeof(*context));
121         if (context == NULL
122             || fetch_context_init(proc, context) < 0) {
123                 free(context);
124                 return NULL;
125         }
126
127         context->vgreg = context->greg;
128         context->struct_size = 0;
129         context->struct_hfa_size = 0;
130         context->struct_hfa_count = 0;
131
132         /* Aggregates or unions of any length, and character strings
133          * of length longer than 8 bytes, will be returned in a
134          * storage buffer allocated by the caller. The caller will
135          * pass the address of this buffer as a hidden first argument
136          * in r3, causing the first explicit argument to be passed in
137          * r4.  */
138         context->ret_struct = ret_info->type == ARGTYPE_STRUCT;
139         if (context->ret_struct) {
140 #if _CALL_ELF == 2
141                 /* if R3 points to stack, parameters will be in R4.  */
142                 uint64_t pstack_end = ptrace(PTRACE_PEEKTEXT, proc->pid,
143                                         proc->stack_pointer, 0);
144                 if (((arch_addr_t)context->regs.r64[3] > proc->stack_pointer)
145                     && (context->regs.r64[3] < pstack_end)) {
146                         context->greg++;
147                         context->stack_pointer += 8;
148                 }
149 #else
150                 context->greg++;
151 #endif
152         }
153
154         return context;
155 }
156
157 struct fetch_context *
158 arch_fetch_arg_clone(struct process *proc,
159                      struct fetch_context *context)
160 {
161         struct fetch_context *clone = malloc(sizeof(*context));
162         if (clone == NULL)
163                 return NULL;
164         *clone = *context;
165         return clone;
166 }
167
168 static int
169 allocate_stack_slot(struct fetch_context *ctx, struct process *proc,
170                     struct arg_type_info *info, struct value *valuep,
171                     bool is_hfa_type)
172 {
173         size_t sz = type_sizeof(proc, info);
174         if (sz == (size_t)-1)
175                 return -1;
176
177         size_t a = type_alignof(proc, info);
178         size_t off = 0;
179         if (proc->e_machine == EM_PPC && a < 4)
180                 a = 4;
181 #if _CALL_ELF == 2
182         else if (proc->e_machine == EM_PPC64 && sz == 4 && is_hfa_type)
183                 a = 4;
184         else
185                 a = 8;
186 #else
187         else if (proc->e_machine == EM_PPC64 && a < 8)
188 #endif
189                 a = 8;
190
191         /* XXX Remove the two double casts when arch_addr_t
192          * becomes integral type.  */
193         uintptr_t tmp = align((uint64_t)(uintptr_t)ctx->stack_pointer, a);
194         ctx->stack_pointer = (arch_addr_t)tmp;
195
196         if (valuep != NULL)
197                 value_in_inferior(valuep, ctx->stack_pointer + off);
198         ctx->stack_pointer += a;
199
200         return 0;
201 }
202
203 static uint64_t
204 read_gpr(struct fetch_context *ctx, struct process *proc, int reg_num)
205 {
206         if (proc->e_machine == EM_PPC)
207                 return ctx->regs.r32[reg_num];
208         else
209                 return ctx->regs.r64[reg_num];
210 }
211
212 /* The support for little endian PowerPC is in upstream Linux and BFD,
213  * and Unix-like Solaris, which we might well support at some point,
214  * runs PowerPC in little endian as well.  This code moves SZ-sized
215  * value to the beginning of W-sized BUF regardless of
216  * endian.  */
217 static void
218 align_small_int(unsigned char *buf, size_t w, size_t sz)
219 {
220         assert(w == 4 || w == 8);
221         union {
222                 uint64_t i64;
223                 uint32_t i32;
224                 uint16_t i16;
225                 uint8_t i8;
226                 char buf[0];
227         } u;
228         memcpy(u.buf, buf, w);
229         if (w == 4)
230                 u.i64 = u.i32;
231
232         switch (sz) {
233         case 1:
234                 u.i8 = u.i64;
235                 break;
236         case 2:
237                 u.i16 = u.i64;
238                 break;
239         case 4:
240                 u.i32 = u.i64;
241         case 8:
242                 break;
243         }
244
245         memcpy(buf, u.buf, sz);
246 }
247
248 static int
249 allocate_gpr(struct fetch_context *ctx, struct process *proc,
250              struct arg_type_info *info, struct value *valuep,
251              size_t off, bool is_hfa_type)
252 {
253         if (ctx->greg > 10)
254                 return allocate_stack_slot(ctx, proc, info, valuep, is_hfa_type);
255
256         int reg_num = ctx->greg;
257
258         size_t sz = type_sizeof(proc, info);
259         if (sz == (size_t)-1)
260                 return -1;
261         assert(sz == 1 || sz == 2 || sz == 4 || sz == 8);
262 #if _CALL_ELF == 2
263         /* Consume the stack slot corresponding to this arg.  */
264         if ((sz + off) >= 8)
265                 ctx->greg++;
266
267         if (is_hfa_type)
268                 ctx->stack_pointer += sz;
269         else
270                 ctx->stack_pointer += 8;
271 #else
272         ctx->greg++;
273 #endif
274
275         if (valuep == NULL)
276                 return 0;
277
278         if (value_reserve(valuep, sz) == NULL)
279                 return -1;
280
281         union {
282                 uint64_t i64;
283                 unsigned char buf[0];
284         } u;
285
286         u.i64 = read_gpr(ctx, proc, reg_num);
287         if (proc->e_machine == EM_PPC)
288                 align_small_int(u.buf, 8, sz);
289         memcpy(value_get_raw_data(valuep), u.buf + off, sz);
290         return 0;
291 }
292
293 static int
294 allocate_float(struct fetch_context *ctx, struct process *proc,
295                struct arg_type_info *info, struct value *valuep,
296                size_t off, bool is_hfa_type)
297 {
298         int pool = proc->e_machine == EM_PPC64 ? 13 : 8;
299         if (ctx->freg <= pool) {
300                 union {
301                         double d;
302                         float f;
303                         char buf[0];
304                 } u = { .d = ctx->fpregs.fpregs[ctx->freg] };
305
306                 ctx->freg++;
307
308                 if (!is_hfa_type)
309                         ctx->vgreg++;
310
311                 if (proc->e_machine == EM_PPC64)
312                         allocate_gpr(ctx, proc, info, NULL, off, is_hfa_type);
313
314                 size_t sz = sizeof(double);
315                 if (info->type == ARGTYPE_FLOAT) {
316                         sz = sizeof(float);
317                         u.f = (float)u.d;
318                 }
319
320                 if (value_reserve(valuep, sz) == NULL)
321                         return -1;
322
323                 memcpy(value_get_raw_data(valuep), u.buf, sz);
324                 return 0;
325         }
326         return allocate_stack_slot(ctx, proc, info, valuep, is_hfa_type);
327 }
328
329 #if _CALL_ELF == 2
330 static int
331 allocate_hfa(struct fetch_context *ctx, struct process *proc,
332              struct arg_type_info *info, struct value *valuep,
333              enum arg_type hfa_type, size_t hfa_count)
334 {
335         size_t sz = type_sizeof(proc, info);
336         if (sz == (size_t)-1)
337                 return -1;
338
339         ctx->struct_hfa_size += sz;
340
341         /* There are two changes regarding structure return types:
342          * * heterogeneous float/vector structs are returned
343          *   in (multiple) FP/vector registers,
344          *   instead of via implicit reference.
345          * * small structs (up to 16 bytes) are return
346          *   in one or two GPRs, instead of via implicit reference.
347          *
348          * Other structures (larger than 16 bytes, not heterogeneous)
349          * are still returned via implicit reference (i.e. a pointer
350          * to memory where to return the struct being passed in r3).
351          * Of course, whether or not an implicit reference pointer
352          * is present will shift the remaining arguments,
353          * so you need to get this right for ELFv2 in order
354          * to get the arguments correct.
355          * If an actual parameter is known to correspond to an HFA
356          * formal parameter, each element is passed in the next
357          * available floating-point argument register starting at fp1
358          * until the fp13. The remaining elements of the aggregate are
359          * passed on the stack.  */
360         size_t slot_off = 0;
361
362         unsigned char *buf = value_reserve(valuep, sz);
363         if (buf == NULL)
364                 return -1;
365
366         struct arg_type_info *hfa_info = type_get_simple(hfa_type);
367         size_t hfa_sz = type_sizeof(proc, hfa_info);
368
369         if (hfa_count > 8)
370                 ctx->struct_hfa_count += hfa_count;
371
372         while (hfa_count > 0 && ctx->freg <= 13) {
373                 int rc;
374                 struct value tmp;
375
376                 value_init(&tmp, proc, NULL, hfa_info, 0);
377
378                 /* Hetereogeneous struct - get value on GPR or stack.  */
379                 if (((hfa_type == ARGTYPE_FLOAT
380                     || hfa_type == ARGTYPE_DOUBLE)
381                       && hfa_count <= 8))
382                         rc = allocate_float(ctx, proc, hfa_info, &tmp,
383                                                 slot_off, true);
384                 else
385                         rc = allocate_gpr(ctx, proc, hfa_info, &tmp,
386                                                 slot_off, true);
387
388                 memcpy(buf, value_get_data(&tmp, NULL), hfa_sz);
389
390                 slot_off += hfa_sz;
391                 buf += hfa_sz;
392                 hfa_count--;
393                 if (slot_off == 8) {
394                         slot_off = 0;
395                         ctx->vgreg++;
396                 }
397
398                 value_destroy(&tmp);
399                 if (rc < 0)
400                         return -1;
401         }
402         if (hfa_count == 0)
403                 return 0;
404
405         /* if no remaining FP, GPR corresponding to slot is used
406         * Mostly it is in part of r10.  */
407         if (ctx->struct_hfa_size <= 64 && ctx->vgreg == 10) {
408                 while (ctx->vgreg <= 10) {
409                         struct value tmp;
410                         value_init(&tmp, proc, NULL, hfa_info, 0);
411                         union {
412                                 uint64_t i64;
413                                 unsigned char buf[0];
414                         } u;
415
416                         u.i64 = read_gpr(ctx, proc, ctx->vgreg);
417
418                         memcpy(buf, u.buf + slot_off, hfa_sz);
419                         slot_off += hfa_sz;
420                         buf += hfa_sz;
421                         hfa_count--;
422                         ctx->stack_pointer += hfa_sz;
423                         if (slot_off >= 8 ) {
424                                 slot_off = 0;
425                                 ctx->vgreg++;
426                         }
427                         value_destroy(&tmp);
428                 }
429         }
430
431         if (hfa_count == 0)
432                 return 0;
433
434         /* Remaining values are on stack */
435         while (hfa_count) {
436                 struct value tmp;
437                 value_init(&tmp, proc, NULL, hfa_info, 0);
438
439                 value_in_inferior(&tmp, ctx->stack_pointer);
440                 memcpy(buf, value_get_data(&tmp, NULL), hfa_sz);
441                 ctx->stack_pointer += hfa_sz;
442                 buf += hfa_sz;
443                 hfa_count--;
444         }
445         return 0;
446 }
447 #endif
448
449 static int
450 allocate_argument(struct fetch_context *ctx, struct process *proc,
451                   struct arg_type_info *info, struct value *valuep)
452 {
453         /* Floating point types and void are handled specially.  */
454         switch (info->type) {
455         case ARGTYPE_VOID:
456                 value_set_word(valuep, 0);
457                 return 0;
458
459         case ARGTYPE_FLOAT:
460         case ARGTYPE_DOUBLE:
461                 return allocate_float(ctx, proc, info, valuep,
462                                         8 - type_sizeof(proc,info), false);
463
464         case ARGTYPE_STRUCT:
465                 if (proc->e_machine == EM_PPC) {
466                         if (value_pass_by_reference(valuep) < 0)
467                                 return -1;
468                 } else {
469 #if _CALL_ELF == 2
470                         struct arg_type_info *hfa_info;
471                         size_t hfa_size;
472                         hfa_info = type_get_hfa_type(info, &hfa_size);
473                         if (hfa_info != NULL ) {
474                                 size_t sz = type_sizeof(proc, info);
475                                 ctx->struct_size += sz;
476                                 return allocate_hfa(ctx, proc, info, valuep,
477                                                 hfa_info->type, hfa_size);
478                         }
479 #endif
480                         /* PPC64: Fixed size aggregates and unions passed by
481                          * value are mapped to as many doublewords of the
482                          * parameter save area as the value uses in memory.
483                          * [...] The first eight doublewords mapped to the
484                          * parameter save area correspond to the registers r3
485                          * through r10.  */
486                 }
487                 /* fall through */
488         case ARGTYPE_CHAR:
489         case ARGTYPE_SHORT:
490         case ARGTYPE_USHORT:
491         case ARGTYPE_INT:
492         case ARGTYPE_UINT:
493         case ARGTYPE_LONG:
494         case ARGTYPE_ULONG:
495         case ARGTYPE_POINTER:
496                 break;
497
498         case ARGTYPE_ARRAY:
499                 /* Arrays decay into pointers.  XXX Fortran?  */
500         default:
501                 assert(info->type != info->type);
502                 abort();
503         }
504
505         unsigned width = proc->e_machine == EM_PPC64 ? 8 : 4;
506
507         /* For other cases (integral types and aggregates), read the
508          * eightbytes comprising the data.  */
509         size_t sz = type_sizeof(proc, valuep->type);
510         if (sz == (size_t)-1)
511                 return -1;
512
513         if (ctx->ret_struct)
514                 ctx->struct_size += sz;
515
516         size_t slots = (sz + width - 1) / width;  /* Round up.  */
517         unsigned char *buf = value_reserve(valuep, slots * width);
518         if (buf == NULL)
519                 return -1;
520         struct arg_type_info *long_info = type_get_simple(ARGTYPE_LONG);
521
522         unsigned char *ptr = buf;
523         while (slots-- > 0) {
524                 struct value val;
525                 value_init(&val, proc, NULL, long_info, 0);
526
527                 /* Floating point registers [...] are used [...] to
528                    pass [...] one member aggregates passed by value
529                    containing a floating point value[.]  Note that for
530                    one member aggregates, "containing" extends to
531                    aggregates within aggregates ad infinitum.  */
532                 int rc;
533                 struct arg_type_info *fp_info
534                         = type_get_fp_equivalent(valuep->type);
535                 if (fp_info != NULL)
536                         rc = allocate_float(ctx, proc, fp_info, &val,
537                                         8-type_sizeof(proc,info), false);
538                 else
539                         rc = allocate_gpr(ctx, proc, long_info, &val,
540                                         0, false);
541
542                 if (rc >= 0) {
543                         memcpy(ptr, value_get_data(&val, NULL), width);
544                         ptr += width;
545                 }
546                 value_destroy(&val);
547
548                 /* Bail out if we failed or if we are dealing with
549                  * FP-equivalent.  Those don't need the adjustments
550                  * made below.  */
551                 if (rc < 0 || fp_info != NULL)
552                         return rc;
553         }
554
555 #ifndef __LITTLE_ENDIAN__
556         /* Small values need post-processing.  */
557         if (sz < width) {
558                 switch (info->type) {
559                 default:
560                         abort();
561
562                 /* Simple integer types (char, short, int, long, enum)
563                  * are mapped to a single doubleword. Values shorter
564                  * than a doubleword are sign or zero extended as
565                  * necessary.  */
566                 case ARGTYPE_CHAR:
567                 case ARGTYPE_SHORT:
568                 case ARGTYPE_INT:
569                 case ARGTYPE_USHORT:
570                 case ARGTYPE_UINT:
571                         align_small_int(buf, width, sz);
572                         break;
573
574                 /* Single precision floating point values are mapped
575                  * to the second word in a single doubleword.
576                  *
577                  * An aggregate or union smaller than one doubleword
578                  * in size is padded so that it appears in the least
579                  * significant bits of the doubleword.  */
580                 case ARGTYPE_FLOAT:
581                 case ARGTYPE_ARRAY:
582                 case ARGTYPE_STRUCT:
583                         memmove(buf, buf + width - sz, sz);
584                         break;
585                 }
586         }
587 #endif
588
589         return 0;
590 }
591
592 int
593 arch_fetch_arg_next(struct fetch_context *ctx, enum tof type,
594                     struct process *proc,
595                     struct arg_type_info *info, struct value *valuep)
596 {
597         return allocate_argument(ctx, proc, info, valuep);
598 }
599
600 int
601 arch_fetch_retval(struct fetch_context *ctx, enum tof type,
602                   struct process *proc, struct arg_type_info *info,
603                   struct value *valuep)
604 {
605         if (fetch_context_init(proc, ctx) < 0)
606                 return -1;
607
608 #if _CALL_ELF == 2
609         void *ptr = (void *)(ctx->regs.r64[1]+32);
610         uint64_t val = ptrace(PTRACE_PEEKTEXT, proc->pid, ptr, 0);
611
612         if (ctx->ret_struct
613            && ((ctx->struct_size > 64
614               || ctx->struct_hfa_count > 8
615               || (ctx->struct_hfa_size == 0 && ctx->struct_size > 56)
616               || (ctx->regs.r64[3] == ctx->regs.r64[1]+32)
617               || (ctx->regs.r64[3] == val )))) {
618 #else
619         if (ctx->ret_struct) {
620 #endif
621                 assert(info->type == ARGTYPE_STRUCT);
622
623                 uint64_t addr = read_gpr(ctx, proc, 3);
624                 value_init(valuep, proc, NULL, info, 0);
625
626                 valuep->where = VAL_LOC_INFERIOR;
627                 /* XXX Remove the double cast when arch_addr_t
628                  * becomes integral type. */
629                 valuep->u.address = (arch_addr_t)(uintptr_t)addr;
630                 return 0;
631         }
632
633         return allocate_argument(ctx, proc, info, valuep);
634 }
635
636 void
637 arch_fetch_arg_done(struct fetch_context *context)
638 {
639         free(context);
640 }