2 * PowerPC register context support
4 * Copyright (C) 2002 Marcus Meissner, SuSE Linux AG.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/types.h>
33 #ifdef HAVE_SYS_PTRACE_H
34 # include <sys/ptrace.h>
37 #ifndef PTRACE_PEEKUSER
39 # define PTRACE_PEEKUSER PT_READ_D
41 #endif /* PTRACE_PEEKUSER */
43 #ifndef PTRACE_POKEUSER
45 # define PTRACE_POKEUSER PT_WRITE_D
47 #endif /* PTRACE_POKEUSER */
55 /* retrieve a thread context */
56 static void get_thread_context_ptrace( struct thread
*thread
, unsigned int flags
, CONTEXT
*context
)
58 int pid
= get_ptrace_pid(thread
);
59 if (flags
& CONTEXT_INTEGER
)
61 #define XREG(x,y) if (ptrace( PTRACE_PEEKUSER, pid, (void*)(x<<2), &context->y) == -1) goto error;
62 #define IREG(x) if (ptrace( PTRACE_PEEKUSER, pid, (void*)(x<<2), &context->Gpr##x) == -1) goto error;
63 IREG(0); IREG(1); IREG(2); IREG(3); IREG(4); IREG(5); IREG(6);
64 IREG(7); IREG(8); IREG(9); IREG(10); IREG(11); IREG(12); IREG(13);
65 IREG(14); IREG(15); IREG(16); IREG(17); IREG(18); IREG(19);
66 IREG(20); IREG(21); IREG(22); IREG(23); IREG(24); IREG(25);
67 IREG(26); IREG(27); IREG(28); IREG(29); IREG(30); IREG(31);
71 context
->ContextFlags
|= CONTEXT_INTEGER
;
73 if (flags
& CONTEXT_CONTROL
)
78 XREG(36,Lr
); /* 36 is LNK ... probably Lr ? */
79 context
->ContextFlags
|= CONTEXT_CONTROL
;
81 if (flags
& CONTEXT_FLOATING_POINT
)
83 #define FREG(x) if (ptrace( PTRACE_PEEKUSER, pid, (void*)((48+x*2)<<2), &context->Fpr##x) == -1) goto error;
116 XREG((48+32*2),Fpscr
);
117 context
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
127 #define XREG(x,y) if (ptrace( PTRACE_POKEUSER, pid, (void*)(x<<2), &context->y) == -1) goto error;
128 #define IREG(x) if (ptrace( PTRACE_POKEUSER, pid, (void*)(x<<2), &context->Gpr##x) == -1) goto error;
129 #define FREG(x) if (ptrace( PTRACE_POKEUSER, pid, (void*)((48+x*2)<<2), &context->Fpr##x) == -1) goto error;
130 /* set a thread context */
131 static void set_thread_context_ptrace( struct thread
*thread
, unsigned int flags
, const CONTEXT
*context
)
133 int pid
= get_ptrace_pid(thread
);
134 if (flags
& CONTEXT_FULL
)
136 if (flags
& CONTEXT_INTEGER
)
138 IREG(0); IREG(1); IREG(2); IREG(3); IREG(4); IREG(5); IREG(6);
139 IREG(7); IREG(8); IREG(9); IREG(10); IREG(11); IREG(12); IREG(13);
140 IREG(14); IREG(15); IREG(16); IREG(17); IREG(18); IREG(19);
141 IREG(20); IREG(21); IREG(22); IREG(23); IREG(24); IREG(25);
142 IREG(26); IREG(27); IREG(28); IREG(29); IREG(30); IREG(31);
147 if (flags
& CONTEXT_CONTROL
)
155 if (flags
& CONTEXT_FLOATING_POINT
)
190 XREG((48+32*2),Fpscr
);
200 #define IREG(x) to->Gpr##x = from->Gpr##x;
201 #define FREG(x) to->Fpr##x = from->Fpr##x;
202 #define CREG(x) to->x = from->x;
203 /* copy a context structure according to the flags */
204 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, unsigned int flags
)
206 if (flags
& CONTEXT_CONTROL
)
211 to
->ContextFlags
|= CONTEXT_CONTROL
;
213 if (flags
& CONTEXT_INTEGER
)
215 IREG(0); IREG(1); IREG(2); IREG(3); IREG(4); IREG(5); IREG(6);
216 IREG(7); IREG(8); IREG(9); IREG(10); IREG(11); IREG(12); IREG(13);
217 IREG(14); IREG(15); IREG(16); IREG(17); IREG(18); IREG(19);
218 IREG(20); IREG(21); IREG(22); IREG(23); IREG(24); IREG(25);
219 IREG(26); IREG(27); IREG(28); IREG(29); IREG(30); IREG(31);
222 to
->ContextFlags
|= CONTEXT_INTEGER
;
224 if (flags
& CONTEXT_FLOATING_POINT
)
259 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
263 /* retrieve the current instruction pointer of a context */
264 void *get_context_ip( const CONTEXT
*context
)
266 return (void *)context
->Iar
;
269 /* retrieve the thread context */
270 void get_thread_context( struct thread
*thread
, CONTEXT
*context
, unsigned int flags
)
272 if (thread
->context
) /* thread is inside an exception event or suspended */
274 copy_context( context
, thread
->context
, flags
);
276 else if (flags
&& suspend_for_ptrace( thread
))
278 get_thread_context_ptrace( thread
, flags
, context
);
279 resume_after_ptrace( thread
);
283 /* set the thread context */
284 void set_thread_context( struct thread
*thread
, const CONTEXT
*context
, unsigned int flags
)
286 if (thread
->context
) /* thread is inside an exception event or suspended */
288 copy_context( thread
->context
, context
, flags
);
290 else if (flags
&& suspend_for_ptrace( thread
))
292 set_thread_context_ptrace( thread
, flags
, context
);
293 resume_after_ptrace( thread
);
297 #endif /* __powerpc__ */