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>
32 #ifdef HAVE_SYS_PTRACE_H
33 # include <sys/ptrace.h>
36 #ifndef PTRACE_PEEKUSER
38 # define PTRACE_PEEKUSER PT_READ_D
40 #endif /* PTRACE_PEEKUSER */
42 #ifndef PTRACE_POKEUSER
44 # define PTRACE_POKEUSER PT_WRITE_D
46 #endif /* PTRACE_POKEUSER */
54 /* retrieve a thread context */
55 static void get_thread_context( struct thread
*thread
, unsigned int flags
, CONTEXT
*context
)
57 int pid
= get_ptrace_pid(thread
);
58 if (flags
& CONTEXT_FULL
)
60 if (flags
& CONTEXT_INTEGER
)
62 #define XREG(x,y) if (ptrace( PTRACE_PEEKUSER, pid, (void*)(x<<2), &context->y) == -1) goto error;
63 #define IREG(x) if (ptrace( PTRACE_PEEKUSER, pid, (void*)(x<<2), &context->Gpr##x) == -1) goto error;
64 IREG(0); IREG(1); IREG(2); IREG(3); IREG(4); IREG(5); IREG(6);
65 IREG(7); IREG(8); IREG(9); IREG(10); IREG(11); IREG(12); IREG(13);
66 IREG(14); IREG(15); IREG(16); IREG(17); IREG(18); IREG(19);
67 IREG(20); IREG(21); IREG(22); IREG(23); IREG(24); IREG(25);
68 IREG(26); IREG(27); IREG(28); IREG(29); IREG(30); IREG(31);
73 if (flags
& CONTEXT_CONTROL
)
78 XREG(36,Lr
); /* 36 is LNK ... probably Lr ? */
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
);
126 #define XREG(x,y) if (ptrace( PTRACE_POKEUSER, pid, (void*)(x<<2), &context->y) == -1) goto error;
127 #define IREG(x) if (ptrace( PTRACE_POKEUSER, pid, (void*)(x<<2), &context->Gpr##x) == -1) goto error;
128 #define FREG(x) if (ptrace( PTRACE_POKEUSER, pid, (void*)((48+x*2)<<2), &context->Fpr##x) == -1) goto error;
129 /* set a thread context */
130 static void set_thread_context( struct thread
*thread
, unsigned int flags
, const CONTEXT
*context
)
132 int pid
= get_ptrace_pid(thread
);
133 if (flags
& CONTEXT_FULL
)
135 if (flags
& CONTEXT_INTEGER
)
137 IREG(0); IREG(1); IREG(2); IREG(3); IREG(4); IREG(5); IREG(6);
138 IREG(7); IREG(8); IREG(9); IREG(10); IREG(11); IREG(12); IREG(13);
139 IREG(14); IREG(15); IREG(16); IREG(17); IREG(18); IREG(19);
140 IREG(20); IREG(21); IREG(22); IREG(23); IREG(24); IREG(25);
141 IREG(26); IREG(27); IREG(28); IREG(29); IREG(30); IREG(31);
146 if (flags
& CONTEXT_CONTROL
)
154 if (flags
& CONTEXT_FLOATING_POINT
)
189 XREG((48+32*2),Fpscr
);
199 #define IREG(x) to->Gpr##x = from->Gpr##x;
200 #define FREG(x) to->Fpr##x = from->Fpr##x;
201 #define CREG(x) to->x = from->x;
202 /* copy a context structure according to the flags */
203 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, int flags
)
205 if (flags
& CONTEXT_CONTROL
)
211 if (flags
& CONTEXT_INTEGER
)
213 IREG(0); IREG(1); IREG(2); IREG(3); IREG(4); IREG(5); IREG(6);
214 IREG(7); IREG(8); IREG(9); IREG(10); IREG(11); IREG(12); IREG(13);
215 IREG(14); IREG(15); IREG(16); IREG(17); IREG(18); IREG(19);
216 IREG(20); IREG(21); IREG(22); IREG(23); IREG(24); IREG(25);
217 IREG(26); IREG(27); IREG(28); IREG(29); IREG(30); IREG(31);
221 if (flags
& CONTEXT_FLOATING_POINT
)
259 /* retrieve the current instruction pointer of a thread */
260 void *get_thread_ip( struct thread
*thread
)
264 if (suspend_for_ptrace( thread
))
266 get_thread_context( thread
, CONTEXT_CONTROL
, &context
);
267 resume_after_ptrace( thread
);
269 return (void *)context
.Iar
;
272 /* determine if we should continue the thread in single-step mode */
273 int get_thread_single_step( struct thread
*thread
)
276 if (thread
->context
) return 0;
277 get_thread_context( thread
, CONTEXT_CONTROL
, &context
);
279 # define MSR_SE (1<<10)
281 return (context
.Msr
& MSR_SE
) != 0;
284 /* send a signal to a specific thread */
285 int tkill( int pid
, int sig
)
287 /* FIXME: should do something here */
292 /* retrieve the current context of a thread */
293 DECL_HANDLER(get_thread_context
)
295 struct thread
*thread
;
297 int flags
= req
->flags
;
299 if (get_reply_max_size() < sizeof(CONTEXT
))
301 set_error( STATUS_INVALID_PARAMETER
);
304 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
306 if ((data
= set_reply_data_size( sizeof(CONTEXT
) )))
308 if (thread
->context
) /* thread is inside an exception event */
310 copy_context( data
, thread
->context
, flags
);
313 if (flags
&& suspend_for_ptrace( thread
))
315 get_thread_context( thread
, flags
, data
);
316 resume_after_ptrace( thread
);
319 release_object( thread
);
323 /* set the current context of a thread */
324 DECL_HANDLER(set_thread_context
)
326 struct thread
*thread
;
327 int flags
= req
->flags
;
329 if (get_req_data_size() < sizeof(CONTEXT
))
331 set_error( STATUS_INVALID_PARAMETER
);
334 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
336 if (thread
->context
) /* thread is inside an exception event */
338 copy_context( thread
->context
, get_req_data(), flags
);
341 if (flags
&& suspend_for_ptrace( thread
))
343 set_thread_context( thread
, flags
, get_req_data() );
344 resume_after_ptrace( thread
);
346 release_object( thread
);
350 #endif /* __powerpc__ */