2 * This file is part of ltrace.
3 * Copyright (C) 2012 Petr Machata, Red Hat Inc.
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.
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.
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
21 #include <asm/ptrace.h>
22 #include <sys/ptrace.h>
23 #include <sys/ucontext.h>
37 struct fetch_context {
38 struct user_regs_struct regs;
39 arch_addr_t stack_pointer;
45 s390x(struct fetch_context *ctx)
47 /* +--------+--------+--------+
48 * | PSW.31 | PSW.32 | mode |
49 * +--------+--------+--------+
50 * | 0 | 0 | 24-bit | Not supported in Linux
51 * | 0 | 1 | 31-bit | s390 compatible mode
52 * | 1 | 1 | 64-bit | z/Architecture, "s390x"
53 * +--------+--------+--------+
54 * (Note: The leftmost bit is PSW.0, rightmost PSW.63.)
58 if ((ctx->regs.psw.mask & 0x180000000UL) == 0x180000000UL)
65 fetch_register_banks(struct process *proc, struct fetch_context *ctx,
69 parea.len = sizeof(ctx->regs);
70 parea.process_addr = (uintptr_t)&ctx->regs;
71 parea.kernel_addr = 0;
72 if (ptrace(PTRACE_PEEKUSR_AREA, proc->pid, &parea, NULL) < 0) {
73 fprintf(stderr, "fetch_register_banks GPR: %s\n",
79 ctx->regs.gprs[2] = ctx->regs.orig_gpr2;
85 fetch_context_init(struct process *proc, struct fetch_context *context,
90 return fetch_register_banks(proc, context, syscall_enter);
93 struct fetch_context *
94 arch_fetch_arg_init(enum tof type, struct process *proc,
95 struct arg_type_info *ret_info)
97 struct fetch_context *context = malloc(sizeof(*context));
99 || fetch_context_init(proc, context, type == LT_TOF_SYSCALL) < 0) {
100 fprintf(stderr, "arch_fetch_arg_init: %s\n",
106 context->stack_pointer = get_stack_pointer(proc)
107 + (s390x(context) ? 160 : 96);
108 if (ret_info->type == ARGTYPE_STRUCT)
114 struct fetch_context *
115 arch_fetch_arg_clone(struct process *proc,
116 struct fetch_context *context)
118 struct fetch_context *clone = malloc(sizeof(*context));
126 allocate_stack_slot(struct fetch_context *ctx, struct process *proc,
127 struct arg_type_info *info, struct value *valuep,
130 /* Note: here we shouldn't see large composite types, those
131 * are passed by reference, which is handled below. Here we
132 * only deal with integers, floats, small structs, etc. */
139 /* Note: double is 8 bytes. */
144 size_t off = sz < a ? a - sz : 0;
145 value_in_inferior(valuep, ctx->stack_pointer + off);
147 ctx->stack_pointer += sz > a ? sz : a;
152 copy_gpr(struct fetch_context *ctx, struct value *valuep, int regno)
154 value_set_word(valuep, ctx->regs.gprs[regno]);
158 allocate_gpr(struct fetch_context *ctx, struct process *proc,
159 struct arg_type_info *info, struct value *valuep,
163 return allocate_stack_slot(ctx, proc, info, valuep, sz);
165 copy_gpr(ctx, valuep, ctx->greg++);
170 allocate_gpr_pair(struct fetch_context *ctx, struct process *proc,
171 struct arg_type_info *info, struct value *valuep,
179 return allocate_stack_slot(ctx, proc, info, valuep, sz);
182 if (value_reserve(valuep, sz) == NULL)
185 unsigned char *ptr = value_get_raw_data(valuep);
191 unsigned char buf[8];
193 u.a = ctx->regs.gprs[ctx->greg++];
194 u.b = ctx->regs.gprs[ctx->greg++];
195 memcpy(ptr, u.buf, sz);
201 allocate_fpr(struct fetch_context *ctx, struct process *proc,
202 struct arg_type_info *info, struct value *valuep,
205 int pool = s390x(ctx) ? 6 : 2;
207 if (ctx->freg > pool)
208 return allocate_stack_slot(ctx, proc, info, valuep, sz);
210 if (value_reserve(valuep, sz) == NULL)
213 memcpy(value_get_raw_data(valuep),
214 &ctx->regs.fp_regs.fprs[ctx->freg], sz);
221 arch_fetch_arg_next(struct fetch_context *ctx, enum tof type,
222 struct process *proc,
223 struct arg_type_info *info, struct value *valuep)
225 size_t sz = type_sizeof(proc, info);
226 if (sz == (size_t)-1)
229 switch (info->type) {
231 value_set_word(valuep, 0);
235 if (type_get_fp_equivalent(info) != NULL)
239 return allocate_fpr(ctx, proc, info, valuep, sz);
241 /* Structures<4 bytes on s390 and structures<8 bytes
242 * on s390x are passed in register. On s390, long
243 * long and structures<8 bytes are passed in two
244 * consecutive registers (if two are available). */
246 if (sz <= (s390x(ctx) ? 8 : 4))
247 return allocate_gpr(ctx, proc, info, valuep, sz);
249 return allocate_gpr_pair(ctx, proc, info, valuep, sz);
254 if (value_pass_by_reference(valuep) < 0)
265 case ARGTYPE_POINTER:
266 return allocate_gpr(ctx, proc, info, valuep, sz);
269 assert(info->type != info->type);
276 arch_fetch_retval(struct fetch_context *ctx, enum tof type,
277 struct process *proc, struct arg_type_info *info,
278 struct value *valuep)
280 if (info->type == ARGTYPE_STRUCT) {
281 if (value_pass_by_reference(valuep) < 0)
283 copy_gpr(ctx, valuep, 2);
287 if (fetch_context_init(proc, ctx, false) < 0)
289 return arch_fetch_arg_next(ctx, type, proc, info, valuep);
293 arch_fetch_arg_done(struct fetch_context *context)