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 */
56 /* retrieve a thread context */
57 static void get_thread_context( struct thread
*thread
, unsigned int flags
, CONTEXT
*context
)
59 int pid
= get_ptrace_pid(thread
);
60 if (flags
& CONTEXT_FULL
)
62 if (flags
& CONTEXT_INTEGER
)
64 #define XREG(x,y) if (ptrace( PTRACE_PEEKUSER, pid, (void*)(x<<2), &context->y) == -1) goto error;
65 #define IREG(x) if (ptrace( PTRACE_PEEKUSER, pid, (void*)(x<<2), &context->Gpr##x) == -1) goto error;
66 IREG(0); IREG(1); IREG(2); IREG(3); IREG(4); IREG(5); IREG(6);
67 IREG(7); IREG(8); IREG(9); IREG(10); IREG(11); IREG(12); IREG(13);
68 IREG(14); IREG(15); IREG(16); IREG(17); IREG(18); IREG(19);
69 IREG(20); IREG(21); IREG(22); IREG(23); IREG(24); IREG(25);
70 IREG(26); IREG(27); IREG(28); IREG(29); IREG(30); IREG(31);
75 if (flags
& CONTEXT_CONTROL
)
80 XREG(36,Lr
); /* 36 is LNK ... probably Lr ? */
83 if (flags
& CONTEXT_FLOATING_POINT
)
85 #define FREG(x) if (ptrace( PTRACE_PEEKUSER, pid, (void*)((48+x*2)<<2), &context->Fpr##x) == -1) goto error;
118 XREG((48+32*2),Fpscr
);
128 #define XREG(x,y) if (ptrace( PTRACE_POKEUSER, pid, (void*)(x<<2), &context->y) == -1) goto error;
129 #define IREG(x) if (ptrace( PTRACE_POKEUSER, pid, (void*)(x<<2), &context->Gpr##x) == -1) goto error;
130 #define FREG(x) if (ptrace( PTRACE_POKEUSER, pid, (void*)((48+x*2)<<2), &context->Fpr##x) == -1) goto error;
131 /* set a thread context */
132 static void set_thread_context( struct thread
*thread
, unsigned int flags
, const CONTEXT
*context
)
134 int pid
= get_ptrace_pid(thread
);
135 if (flags
& CONTEXT_FULL
)
137 if (flags
& CONTEXT_INTEGER
)
139 IREG(0); IREG(1); IREG(2); IREG(3); IREG(4); IREG(5); IREG(6);
140 IREG(7); IREG(8); IREG(9); IREG(10); IREG(11); IREG(12); IREG(13);
141 IREG(14); IREG(15); IREG(16); IREG(17); IREG(18); IREG(19);
142 IREG(20); IREG(21); IREG(22); IREG(23); IREG(24); IREG(25);
143 IREG(26); IREG(27); IREG(28); IREG(29); IREG(30); IREG(31);
148 if (flags
& CONTEXT_CONTROL
)
156 if (flags
& CONTEXT_FLOATING_POINT
)
191 XREG((48+32*2),Fpscr
);
201 #define IREG(x) to->Gpr##x = from->Gpr##x;
202 #define FREG(x) to->Fpr##x = from->Fpr##x;
203 #define CREG(x) to->x = from->x;
204 /* copy a context structure according to the flags */
205 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, int flags
)
207 if (flags
& 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);
223 if (flags
& CONTEXT_FLOATING_POINT
)
261 /* retrieve the current instruction pointer of a thread */
262 void *get_thread_ip( struct thread
*thread
)
266 if (suspend_for_ptrace( thread
))
268 get_thread_context( thread
, CONTEXT_CONTROL
, &context
);
269 resume_after_ptrace( thread
);
271 return (void *)context
.Iar
;
274 /* determine if we should continue the thread in single-step mode */
275 int get_thread_single_step( struct thread
*thread
)
278 if (thread
->context
) return 0;
279 get_thread_context( thread
, CONTEXT_CONTROL
, &context
);
281 # define MSR_SE (1<<10)
283 return (context
.Msr
& MSR_SE
) != 0;
286 /* send a signal to a specific thread */
287 int tkill( int pid
, int sig
)
289 /* FIXME: should do something here */
294 /* retrieve the current context of a thread */
295 DECL_HANDLER(get_thread_context
)
297 struct thread
*thread
;
299 int flags
= req
->flags
;
301 if (get_reply_max_size() < sizeof(CONTEXT
))
303 set_error( STATUS_INVALID_PARAMETER
);
306 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
308 if ((data
= set_reply_data_size( sizeof(CONTEXT
) )))
310 if (thread
->context
) /* thread is inside an exception event */
312 copy_context( data
, thread
->context
, flags
);
315 if (flags
&& suspend_for_ptrace( thread
))
317 get_thread_context( thread
, flags
, data
);
318 resume_after_ptrace( thread
);
321 release_object( thread
);
325 /* set the current context of a thread */
326 DECL_HANDLER(set_thread_context
)
328 struct thread
*thread
;
329 int flags
= req
->flags
;
331 if (get_req_data_size() < sizeof(CONTEXT
))
333 set_error( STATUS_INVALID_PARAMETER
);
336 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
338 if (thread
->context
) /* thread is inside an exception event */
340 copy_context( thread
->context
, get_req_data(), flags
);
343 if (flags
&& suspend_for_ptrace( thread
))
345 set_thread_context( thread
, flags
, get_req_data() );
346 resume_after_ptrace( thread
);
348 release_object( thread
);
352 #endif /* __powerpc__ */