Reimplemented DebugBreakProcess.
[wine] / server / context_sparc.c
1 /*
2  * Sparc register context support
3  *
4  * Copyright (C) 2000 Ulrich Weigand
5  */
6
7 #include "config.h"
8
9 #ifdef __sparc__
10
11 #include <assert.h>
12 #include <errno.h>
13 #include <sys/types.h>
14 #ifdef HAVE_SYS_REG_H
15 # include <sys/reg.h>
16 #endif
17 #include <unistd.h>
18 #ifdef HAVE_SYS_PTRACE_H
19 # include <sys/ptrace.h>
20 #endif
21
22 #include "winbase.h"
23
24 #include "thread.h"
25 #include "request.h"
26
27
28 #if defined(__sun) || defined(__sun__)
29
30 /* retrieve a thread context */
31 static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
32 {
33     int pid = thread->unix_pid;
34     if (flags & CONTEXT_FULL)
35     {
36         struct regs regs;
37         if (ptrace( PTRACE_GETREGS, pid, 0, (int) &regs ) == -1) goto error;
38         if (flags & CONTEXT_INTEGER)
39         {
40             context->g0 = 0;
41             context->g1 = regs.r_g1;
42             context->g2 = regs.r_g2;
43             context->g3 = regs.r_g3;
44             context->g4 = regs.r_g4;
45             context->g5 = regs.r_g5;
46             context->g6 = regs.r_g6;
47             context->g7 = regs.r_g7;
48
49             context->o0 = regs.r_o0;
50             context->o1 = regs.r_o1;
51             context->o2 = regs.r_o2;
52             context->o3 = regs.r_o3;
53             context->o4 = regs.r_o4;
54             context->o5 = regs.r_o5;
55             context->o6 = regs.r_o6;
56             context->o7 = regs.r_o7;
57
58             /* FIXME: local and in registers */
59         }
60         if (flags & CONTEXT_CONTROL)
61         {
62             context->psr = regs.r_psr;
63             context->pc  = regs.r_pc;
64             context->npc = regs.r_npc;
65             context->y   = regs.r_y;
66             context->wim = 0;  /* FIXME */
67             context->tbr = 0;  /* FIXME */
68         }
69     }
70     if (flags & CONTEXT_FLOATING_POINT)
71     {
72         /* FIXME */
73     }
74     return;
75  error:
76     file_set_error();
77 }
78
79
80 /* set a thread context */
81 static void set_thread_context( struct thread *thread, unsigned int flags, const CONTEXT *context )
82 {
83     /* FIXME */
84 }
85
86 #else  /* __sun__ */
87 #error You must implement get/set_thread_context for your platform
88 #endif  /* __sun__ */
89
90
91 /* copy a context structure according to the flags */
92 static void copy_context( CONTEXT *to, const CONTEXT *from, int flags )
93 {
94     if (flags & CONTEXT_CONTROL)
95     {
96         to->psr    = from->psr;
97         to->pc     = from->pc;
98         to->npc    = from->npc;
99         to->y      = from->y;
100         to->wim    = from->wim;
101         to->tbr    = from->tbr;
102     }
103     if (flags & CONTEXT_INTEGER)
104     {
105         to->g0 = from->g0;
106         to->g1 = from->g1;
107         to->g2 = from->g2;
108         to->g3 = from->g3;
109         to->g4 = from->g4;
110         to->g5 = from->g5;
111         to->g6 = from->g6;
112         to->g7 = from->g7;
113         to->o0 = from->o0;
114         to->o1 = from->o1;
115         to->o2 = from->o2;
116         to->o3 = from->o3;
117         to->o4 = from->o4;
118         to->o5 = from->o5;
119         to->o6 = from->o6;
120         to->o7 = from->o7;
121         to->l0 = from->l0;
122         to->l1 = from->l1;
123         to->l2 = from->l2;
124         to->l3 = from->l3;
125         to->l4 = from->l4;
126         to->l5 = from->l5;
127         to->l6 = from->l6;
128         to->l7 = from->l7;
129         to->i0 = from->i0;
130         to->i1 = from->i1;
131         to->i2 = from->i2;
132         to->i3 = from->i3;
133         to->i4 = from->i4;
134         to->i5 = from->i5;
135         to->i6 = from->i6;
136         to->i7 = from->i7;
137     }
138     if (flags & CONTEXT_FLOATING_POINT)
139     {
140         /* FIXME */
141     }
142 }
143
144 /* retrieve the current instruction pointer of a thread */
145 void *get_thread_ip( struct thread *thread )
146 {
147     CONTEXT context;
148     context.pc = 0;
149     if (suspend_for_ptrace( thread ))
150     {
151         get_thread_context( thread, CONTEXT_CONTROL, &context );
152         resume_thread( thread );
153     }
154     return (void *)context.pc;
155 }
156
157 /* determine if we should continue the thread in single-step mode */
158 int get_thread_single_step( struct thread *thread )
159 {
160     return 0;  /* FIXME */
161 }
162
163 /* retrieve the current context of a thread */
164 DECL_HANDLER(get_thread_context)
165 {
166     struct thread *thread;
167     void *data;
168     int flags = req->flags & ~CONTEXT_SPARC;  /* get rid of CPU id */
169
170     if (get_reply_max_size() < sizeof(CONTEXT))
171     {
172         set_error( STATUS_INVALID_PARAMETER );
173         return;
174     }
175     if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
176
177     if ((data = set_reply_data_size( sizeof(CONTEXT) )))
178     {
179         if (thread->context)  /* thread is inside an exception event */
180         {
181             copy_context( data, thread->context, flags );
182             flags = 0;
183         }
184         if (flags && suspend_for_ptrace( thread ))
185         {
186             get_thread_context( thread, flags, data );
187             resume_thread( thread );
188         }
189     }
190     release_object( thread );
191 }
192
193
194 /* set the current context of a thread */
195 DECL_HANDLER(set_thread_context)
196 {
197     struct thread *thread;
198     int flags = req->flags & ~CONTEXT_SPARC;  /* get rid of CPU id */
199
200     if (get_req_data_size() < sizeof(CONTEXT))
201     {
202         set_error( STATUS_INVALID_PARAMETER );
203         return;
204     }
205     if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
206     {
207         if (thread->context)  /* thread is inside an exception event */
208         {
209             copy_context( thread->context, get_req_data(), flags );
210             flags = 0;
211         }
212         if (flags && suspend_for_ptrace( thread ))
213         {
214             set_thread_context( thread, flags, get_req_data() );
215             resume_thread( thread );
216         }
217         release_object( thread );
218     }
219 }
220
221 #endif  /* __sparc__ */