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