Release 1.5.29.
[wine] / server / mach.c
1 /*
2  * Server-side debugger support using Mach primitives
3  *
4  * Copyright (C) 1999, 2006 Alexandre Julliard
5  * Copyright (C) 2006 Ken Thomases for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <signal.h>
29 #include <stdarg.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #ifdef HAVE_SYS_SYSCALL_H
33 #include <sys/syscall.h>
34 #endif
35
36 #include "ntstatus.h"
37 #define WIN32_NO_STATUS
38 #include "winternl.h"
39
40 #include "file.h"
41 #include "process.h"
42 #include "thread.h"
43 #include "request.h"
44 #include "wine/library.h"
45
46 #ifdef USE_MACH
47
48 #include <mach/mach.h>
49 #include <mach/mach_error.h>
50 #include <mach/thread_act.h>
51 #include <servers/bootstrap.h>
52
53 static mach_port_t server_mach_port;
54
55 void sigchld_callback(void)
56 {
57     assert(0);  /* should never be called on MacOS */
58 }
59
60 static void mach_set_error(kern_return_t mach_error)
61 {
62     switch (mach_error)
63     {
64         case KERN_SUCCESS:              break;
65         case KERN_INVALID_ARGUMENT:     set_error(STATUS_INVALID_PARAMETER); break;
66         case KERN_NO_SPACE:             set_error(STATUS_NO_MEMORY); break;
67         case KERN_PROTECTION_FAILURE:   set_error(STATUS_ACCESS_DENIED); break;
68         case KERN_INVALID_ADDRESS:      set_error(STATUS_ACCESS_VIOLATION); break;
69         default:                        set_error(STATUS_UNSUCCESSFUL); break;
70     }
71 }
72
73 static mach_port_t get_process_port( struct process *process )
74 {
75     return process->trace_data;
76 }
77
78 /* initialize the process control mechanism */
79 void init_tracing_mechanism(void)
80 {
81     mach_port_t bp;
82
83     if (task_get_bootstrap_port(mach_task_self(), &bp) != KERN_SUCCESS)
84         fatal_error("Can't find bootstrap port\n");
85     if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &server_mach_port) != KERN_SUCCESS)
86         fatal_error("Can't allocate port\n");
87     if  (mach_port_insert_right( mach_task_self(),
88                                  server_mach_port,
89                                  server_mach_port,
90                                  MACH_MSG_TYPE_MAKE_SEND ) != KERN_SUCCESS)
91             fatal_error("Error inserting rights\n");
92     if (bootstrap_register(bp, (char*)wine_get_server_dir(), server_mach_port) != KERN_SUCCESS)
93         fatal_error("Can't check in server_mach_port\n");
94     mach_port_deallocate(mach_task_self(), bp);
95 }
96
97 /* initialize the per-process tracing mechanism */
98 void init_process_tracing( struct process *process )
99 {
100     int pid, ret;
101     struct
102     {
103         mach_msg_header_t           header;
104         mach_msg_body_t             body;
105         mach_msg_port_descriptor_t  task_port;
106         mach_msg_trailer_t          trailer; /* only present on receive */
107     } msg;
108
109     for (;;)
110     {
111         ret = mach_msg( &msg.header, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof(msg),
112                         server_mach_port, 0, 0 );
113         if (ret)
114         {
115             if (ret != MACH_RCV_TIMED_OUT && debug_level)
116                 fprintf( stderr, "warning: mach port receive failed with %x\n", ret );
117             return;
118         }
119
120         /* if anything in the message is invalid, ignore it */
121         if (msg.header.msgh_size != offsetof(typeof(msg), trailer)) continue;
122         if (msg.body.msgh_descriptor_count != 1) continue;
123         if (msg.task_port.type != MACH_MSG_PORT_DESCRIPTOR) continue;
124         if (msg.task_port.disposition != MACH_MSG_TYPE_PORT_SEND) continue;
125         if (msg.task_port.name == MACH_PORT_NULL) continue;
126         if (msg.task_port.name == MACH_PORT_DEAD) continue;
127
128         if (!pid_for_task( msg.task_port.name, &pid ))
129         {
130             struct thread *thread = get_thread_from_pid( pid );
131
132             if (thread && !thread->process->trace_data)
133                 thread->process->trace_data = msg.task_port.name;
134             else
135                 mach_port_deallocate( mach_task_self(), msg.task_port.name );
136         }
137     }
138 }
139
140 /* terminate the per-process tracing mechanism */
141 void finish_process_tracing( struct process *process )
142 {
143     if (process->trace_data)
144     {
145         mach_port_deallocate( mach_task_self(), process->trace_data );
146         process->trace_data = 0;
147     }
148 }
149
150 /* retrieve the thread x86 registers */
151 void get_thread_context( struct thread *thread, context_t *context, unsigned int flags )
152 {
153 #ifdef __i386__
154     x86_debug_state32_t state;
155     mach_msg_type_number_t count = sizeof(state) / sizeof(int);
156     mach_msg_type_name_t type;
157     mach_port_t port, process_port = get_process_port( thread->process );
158
159     /* all other regs are handled on the client side */
160     assert( flags == SERVER_CTX_DEBUG_REGISTERS );
161
162     if (thread->unix_pid == -1 || !process_port ||
163         mach_port_extract_right( process_port, thread->unix_tid,
164                                  MACH_MSG_TYPE_COPY_SEND, &port, &type ))
165     {
166         set_error( STATUS_ACCESS_DENIED );
167         return;
168     }
169
170     if (!thread_get_state( port, x86_DEBUG_STATE32, (thread_state_t)&state, &count ))
171     {
172 /* work around silly renaming of struct members in OS X 10.5 */
173 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_DEBUG_STATE32)
174         context->debug.i386_regs.dr0 = state.__dr0;
175         context->debug.i386_regs.dr1 = state.__dr1;
176         context->debug.i386_regs.dr2 = state.__dr2;
177         context->debug.i386_regs.dr3 = state.__dr3;
178         context->debug.i386_regs.dr6 = state.__dr6;
179         context->debug.i386_regs.dr7 = state.__dr7;
180 #else
181         context->debug.i386_regs.dr0 = state.dr0;
182         context->debug.i386_regs.dr1 = state.dr1;
183         context->debug.i386_regs.dr2 = state.dr2;
184         context->debug.i386_regs.dr3 = state.dr3;
185         context->debug.i386_regs.dr6 = state.dr6;
186         context->debug.i386_regs.dr7 = state.dr7;
187 #endif
188         context->flags |= SERVER_CTX_DEBUG_REGISTERS;
189     }
190     mach_port_deallocate( mach_task_self(), port );
191 #endif
192 }
193
194 /* set the thread x86 registers */
195 void set_thread_context( struct thread *thread, const context_t *context, unsigned int flags )
196 {
197 #ifdef __i386__
198     x86_debug_state32_t state;
199     mach_msg_type_number_t count = sizeof(state) / sizeof(int);
200     mach_msg_type_name_t type;
201     mach_port_t port, process_port = get_process_port( thread->process );
202     unsigned int dr7;
203
204     /* all other regs are handled on the client side */
205     assert( flags == SERVER_CTX_DEBUG_REGISTERS );
206
207     if (thread->unix_pid == -1 || !process_port ||
208         mach_port_extract_right( process_port, thread->unix_tid,
209                                  MACH_MSG_TYPE_COPY_SEND, &port, &type ))
210     {
211         set_error( STATUS_ACCESS_DENIED );
212         return;
213     }
214
215     /* Mac OS doesn't allow setting the global breakpoint flags */
216     dr7 = (context->debug.i386_regs.dr7 & ~0xaa) | ((context->debug.i386_regs.dr7 & 0xaa) >> 1);
217
218 #if __DARWIN_UNIX03 && defined(_STRUCT_X86_DEBUG_STATE32)
219     state.__dr0 = context->debug.i386_regs.dr0;
220     state.__dr1 = context->debug.i386_regs.dr1;
221     state.__dr2 = context->debug.i386_regs.dr2;
222     state.__dr3 = context->debug.i386_regs.dr3;
223     state.__dr4 = 0;
224     state.__dr5 = 0;
225     state.__dr6 = context->debug.i386_regs.dr6;
226     state.__dr7 = dr7;
227 #else
228     state.dr0 = context->debug.i386_regs.dr0;
229     state.dr1 = context->debug.i386_regs.dr1;
230     state.dr2 = context->debug.i386_regs.dr2;
231     state.dr3 = context->debug.i386_regs.dr3;
232     state.dr4 = 0;
233     state.dr5 = 0;
234     state.dr6 = context->debug.i386_regs.dr6;
235     state.dr7 = dr7;
236 #endif
237     if (!thread_set_state( port, x86_DEBUG_STATE32, (thread_state_t)&state, count ))
238     {
239         if (thread->context)  /* update the cached values */
240             thread->context->debug.i386_regs = context->debug.i386_regs;
241     }
242     mach_port_deallocate( mach_task_self(), port );
243 #endif
244 }
245
246 int send_thread_signal( struct thread *thread, int sig )
247 {
248     int ret = -1;
249     mach_port_t process_port = get_process_port( thread->process );
250
251     if (thread->unix_pid != -1 && process_port)
252     {
253         mach_msg_type_name_t type;
254         mach_port_t port;
255
256         if (!mach_port_extract_right( process_port, thread->unix_tid,
257                                       MACH_MSG_TYPE_COPY_SEND, &port, &type ))
258         {
259             ret = syscall( SYS___pthread_kill, port, sig );
260             mach_port_deallocate( mach_task_self(), port );
261         }
262         else errno = ESRCH;
263
264         if (ret == -1 && errno == ESRCH) /* thread got killed */
265         {
266             thread->unix_pid = -1;
267             thread->unix_tid = -1;
268         }
269     }
270     if (debug_level && ret != -1)
271         fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
272     return (ret != -1);
273 }
274
275 /* read data from a process memory space */
276 int read_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, char *dest )
277 {
278     kern_return_t ret;
279     mach_msg_type_number_t bytes_read;
280     vm_offset_t offset, data;
281     vm_address_t aligned_address;
282     vm_size_t aligned_size;
283     unsigned int page_size = get_page_size();
284     mach_port_t process_port = get_process_port( process );
285
286     if (!process_port)
287     {
288         set_error( STATUS_ACCESS_DENIED );
289         return 0;
290     }
291     if ((vm_address_t)ptr != ptr)
292     {
293         set_error( STATUS_ACCESS_DENIED );
294         return 0;
295     }
296
297     if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
298     {
299         mach_set_error( ret );
300         return 0;
301     }
302
303     offset = ptr % page_size;
304     aligned_address = (vm_address_t)(ptr - offset);
305     aligned_size = (size + offset + page_size - 1) / page_size * page_size;
306
307     ret = vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
308     if (ret != KERN_SUCCESS) mach_set_error( ret );
309     else
310     {
311         memcpy( dest, (char *)data + offset, size );
312         vm_deallocate( mach_task_self(), data, bytes_read );
313     }
314     task_resume( process_port );
315     return (ret == KERN_SUCCESS);
316 }
317
318 /* write data to a process memory space */
319 int write_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, const char *src )
320 {
321     kern_return_t ret;
322     vm_address_t aligned_address, region_address;
323     vm_size_t aligned_size, region_size;
324     mach_msg_type_number_t info_size, bytes_read;
325     vm_offset_t offset, task_mem = 0;
326     struct vm_region_basic_info info;
327     mach_port_t dummy;
328     unsigned int page_size = get_page_size();
329     mach_port_t process_port = get_process_port( process );
330
331     if (!process_port)
332     {
333         set_error( STATUS_ACCESS_DENIED );
334         return 0;
335     }
336     if ((vm_address_t)ptr != ptr)
337     {
338         set_error( STATUS_ACCESS_DENIED );
339         return 0;
340     }
341
342     offset = ptr % page_size;
343     aligned_address = (vm_address_t)(ptr - offset);
344     aligned_size = (size + offset + page_size - 1) / page_size * page_size;
345
346     if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
347     {
348         mach_set_error( ret );
349         return 0;
350     }
351
352     ret = vm_read( process_port, aligned_address, aligned_size, &task_mem, &bytes_read );
353     if (ret != KERN_SUCCESS)
354     {
355         mach_set_error( ret );
356         goto failed;
357     }
358     region_address = aligned_address;
359     info_size = sizeof(info);
360     ret = vm_region( process_port, &region_address, &region_size, VM_REGION_BASIC_INFO,
361                      (vm_region_info_t)&info, &info_size, &dummy );
362     if (ret != KERN_SUCCESS)
363     {
364         mach_set_error( ret );
365         goto failed;
366     }
367     if (region_address > aligned_address ||
368         region_address + region_size < aligned_address + aligned_size)
369     {
370         /* FIXME: should support multiple regions */
371         set_error( ERROR_ACCESS_DENIED );
372         goto failed;
373     }
374     ret = vm_protect( process_port, aligned_address, aligned_size, 0, VM_PROT_READ | VM_PROT_WRITE );
375     if (ret != KERN_SUCCESS)
376     {
377         mach_set_error( ret );
378         goto failed;
379     }
380
381     /* FIXME: there's an optimization that can be made: check first and last */
382     /* pages for writability; read first and last pages; write interior */
383     /* pages to task without ever reading&modifying them; if that succeeds, */
384     /* modify first and last pages and write them. */
385
386     memcpy( (char*)task_mem + offset, src, size );
387
388     ret = vm_write( process_port, aligned_address, task_mem, bytes_read );
389     if (ret != KERN_SUCCESS) mach_set_error( ret );
390     else
391     {
392         vm_deallocate( mach_task_self(), task_mem, bytes_read );
393         /* restore protection */
394         vm_protect( process_port, aligned_address, aligned_size, 0, info.protection );
395         task_resume( process_port );
396         return 1;
397     }
398
399 failed:
400     if (task_mem) vm_deallocate( mach_task_self(), task_mem, bytes_read );
401     task_resume( process_port );
402     return 0;
403 }
404
405 /* retrieve an LDT selector entry */
406 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
407                          unsigned int *limit, unsigned char *flags )
408 {
409     const unsigned int total_size = (2 * sizeof(int) + 1) * 8192;
410     struct process *process = thread->process;
411     unsigned int page_size = get_page_size();
412     vm_offset_t data;
413     kern_return_t ret;
414     mach_msg_type_number_t bytes_read;
415     mach_port_t process_port = get_process_port( thread->process );
416
417     if (!process->ldt_copy || !process_port)
418     {
419         set_error( STATUS_ACCESS_DENIED );
420         return;
421     }
422     if (entry >= 8192)
423     {
424         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
425         return;
426     }
427
428     if ((ret = task_suspend( process_port )) == KERN_SUCCESS)
429     {
430         vm_offset_t offset = process->ldt_copy % page_size;
431         vm_address_t aligned_address = (vm_address_t)(process->ldt_copy - offset);
432         vm_size_t aligned_size = (total_size + offset + page_size - 1) / page_size * page_size;
433
434         ret = vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
435         if (ret != KERN_SUCCESS) mach_set_error( ret );
436         else
437         {
438             const int *ldt = (const int *)((char *)data + offset);
439             memcpy( base, ldt + entry, sizeof(int) );
440             memcpy( limit, ldt + entry + 8192, sizeof(int) );
441             memcpy( flags, (char *)(ldt + 2 * 8192) + entry, 1 );
442             vm_deallocate( mach_task_self(), data, bytes_read );
443         }
444         task_resume( process_port );
445     }
446     else mach_set_error( ret );
447 }
448
449 #endif  /* USE_MACH */