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