wined3d: Use application provided fog range when fog is enabled.
[wine/multimedia.git] / server / mach.c
blob111953c773a22c1fbd811b33b829e20f5231c1d0
1 /*
2 * Server-side debugger support using Mach primitives
4 * Copyright (C) 1999, 2006 Alexandre Julliard
5 * Copyright (C) 2006 Ken Thomases for CodeWeavers
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.
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.
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
22 #include "config.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <sys/types.h>
30 #include <unistd.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "winternl.h"
36 #include "file.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "request.h"
40 #include "wine/library.h"
42 #ifdef USE_MACH
44 #include <mach/mach.h>
45 #include <mach/mach_error.h>
46 #include <mach/thread_act.h>
47 #include <servers/bootstrap.h>
49 extern int __pthread_kill(mach_port_t, int);
51 static mach_port_t server_mach_port;
53 void sigchld_callback(void)
55 assert(0); /* should never be called on MacOS */
58 static void mach_set_error(kern_return_t mach_error)
60 switch (mach_error)
62 case KERN_SUCCESS: break;
63 case KERN_INVALID_ARGUMENT: set_error(STATUS_INVALID_PARAMETER); break;
64 case KERN_NO_SPACE: set_error(STATUS_NO_MEMORY); break;
65 case KERN_PROTECTION_FAILURE: set_error(STATUS_ACCESS_DENIED); break;
66 case KERN_INVALID_ADDRESS: set_error(STATUS_ACCESS_VIOLATION); break;
67 default: set_error(STATUS_UNSUCCESSFUL); break;
71 static mach_port_t get_process_port( struct process *process )
73 return process->trace_data;
76 /* initialize the process control mechanism */
77 void init_tracing_mechanism(void)
79 mach_port_t bp;
81 if (task_get_bootstrap_port(mach_task_self(), &bp) != KERN_SUCCESS)
82 fatal_error("Can't find bootstrap port\n");
83 if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &server_mach_port) != KERN_SUCCESS)
84 fatal_error("Can't allocate port\n");
85 if (mach_port_insert_right( mach_task_self(),
86 server_mach_port,
87 server_mach_port,
88 MACH_MSG_TYPE_MAKE_SEND ) != KERN_SUCCESS)
89 fatal_error("Error inserting rights\n");
90 if (bootstrap_register(bp, (char*)wine_get_server_dir(), server_mach_port) != KERN_SUCCESS)
91 fatal_error("Can't check in server_mach_port\n");
92 mach_port_deallocate(mach_task_self(), bp);
95 /* initialize the per-process tracing mechanism */
96 void init_process_tracing( struct process *process )
98 int pid, ret;
99 struct
101 mach_msg_header_t header;
102 mach_msg_body_t body;
103 mach_msg_port_descriptor_t task_port;
104 mach_msg_trailer_t trailer; /* only present on receive */
105 } msg;
107 for (;;)
109 ret = mach_msg( &msg.header, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof(msg),
110 server_mach_port, 0, 0 );
111 if (ret)
113 if (ret != MACH_RCV_TIMED_OUT && debug_level)
114 fprintf( stderr, "warning: mach port receive failed with %x\n", ret );
115 return;
118 /* if anything in the message is invalid, ignore it */
119 if (msg.header.msgh_size != offsetof(typeof(msg), trailer)) continue;
120 if (msg.body.msgh_descriptor_count != 1) continue;
121 if (msg.task_port.type != MACH_MSG_PORT_DESCRIPTOR) continue;
122 if (msg.task_port.disposition != MACH_MSG_TYPE_PORT_SEND) continue;
123 if (msg.task_port.name == MACH_PORT_NULL) continue;
124 if (msg.task_port.name == MACH_PORT_DEAD) continue;
126 if (!pid_for_task( msg.task_port.name, &pid ))
128 struct thread *thread = get_thread_from_pid( pid );
130 if (thread && !thread->process->trace_data)
131 thread->process->trace_data = msg.task_port.name;
132 else
133 mach_port_deallocate( mach_task_self(), msg.task_port.name );
138 /* terminate the per-process tracing mechanism */
139 void finish_process_tracing( struct process *process )
141 if (process->trace_data)
143 mach_port_deallocate( mach_task_self(), process->trace_data );
144 process->trace_data = 0;
148 /* retrieve the thread x86 registers */
149 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
151 struct x86_debug_state32 state;
152 mach_msg_type_number_t count = sizeof(state) / sizeof(int);
153 mach_msg_type_name_t type;
154 mach_port_t port, process_port = get_process_port( thread->process );
156 /* all other regs are handled on the client side */
157 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
159 if (thread->unix_pid == -1 || !process_port ||
160 mach_port_extract_right( process_port, thread->unix_tid,
161 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
163 set_error( STATUS_ACCESS_DENIED );
164 return;
167 if (!thread_get_state( port, x86_DEBUG_STATE32, (thread_state_t)&state, &count ))
169 context->Dr0 = state.dr0;
170 context->Dr1 = state.dr1;
171 context->Dr2 = state.dr2;
172 context->Dr3 = state.dr3;
173 context->Dr6 = state.dr6;
174 context->Dr7 = state.dr7;
175 context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
177 mach_port_deallocate( mach_task_self(), port );
180 /* set the thread x86 registers */
181 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
183 struct x86_debug_state32 state;
184 mach_msg_type_number_t count = sizeof(state) / sizeof(int);
185 mach_msg_type_name_t type;
186 mach_port_t port, process_port = get_process_port( thread->process );
188 /* all other regs are handled on the client side */
189 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
191 if (thread->unix_pid == -1 || !process_port ||
192 mach_port_extract_right( process_port, thread->unix_tid,
193 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
195 set_error( STATUS_ACCESS_DENIED );
196 return;
199 state.dr0 = context->Dr0;
200 state.dr1 = context->Dr1;
201 state.dr2 = context->Dr2;
202 state.dr3 = context->Dr3;
203 state.dr4 = 0;
204 state.dr5 = 0;
205 state.dr6 = context->Dr6;
206 state.dr7 = context->Dr7;
207 if (!thread_set_state( port, x86_DEBUG_STATE32, (thread_state_t)&state, count ))
209 if (thread->context) /* update the cached values */
211 thread->context->Dr0 = context->Dr0;
212 thread->context->Dr1 = context->Dr1;
213 thread->context->Dr2 = context->Dr2;
214 thread->context->Dr3 = context->Dr3;
215 thread->context->Dr6 = context->Dr6;
216 thread->context->Dr7 = context->Dr7;
219 mach_port_deallocate( mach_task_self(), port );
222 int send_thread_signal( struct thread *thread, int sig )
224 int ret = -1;
225 mach_port_t process_port = get_process_port( thread->process );
227 if (thread->unix_pid != -1 && process_port)
229 mach_msg_type_name_t type;
230 mach_port_t port;
232 if (!mach_port_extract_right( process_port, thread->unix_tid,
233 MACH_MSG_TYPE_COPY_SEND, &port, &type ))
235 ret = __pthread_kill( port, sig );
236 mach_port_deallocate( mach_task_self(), port );
238 else errno = ESRCH;
240 if (ret == -1 && errno == ESRCH) /* thread got killed */
242 thread->unix_pid = -1;
243 thread->unix_tid = -1;
246 if (debug_level && ret != -1)
247 fprintf( stderr, "%04x: *sent signal* signal=%d\n", thread->id, sig );
248 return (ret != -1);
251 /* read data from a process memory space */
252 int read_process_memory( struct process *process, const void *ptr, data_size_t size, char *dest )
254 kern_return_t ret;
255 mach_msg_type_number_t bytes_read;
256 vm_offset_t offset, data;
257 vm_address_t aligned_address;
258 vm_size_t aligned_size;
259 unsigned int page_size = get_page_size();
260 mach_port_t process_port = get_process_port( process );
262 if (!process_port)
264 set_error( STATUS_ACCESS_DENIED );
265 return 0;
268 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
270 mach_set_error( ret );
271 return 0;
274 offset = (unsigned long)ptr % page_size;
275 aligned_address = (vm_address_t)((char *)ptr - offset);
276 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
278 ret = vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
279 if (ret != KERN_SUCCESS) mach_set_error( ret );
280 else
282 memcpy( dest, (char *)data + offset, size );
283 vm_deallocate( mach_task_self(), data, bytes_read );
285 task_resume( process_port );
286 return (ret == KERN_SUCCESS);
289 /* write data to a process memory space */
290 int write_process_memory( struct process *process, void *ptr, data_size_t size, const char *src )
292 kern_return_t ret;
293 vm_address_t aligned_address, region_address;
294 vm_size_t aligned_size, region_size;
295 mach_msg_type_number_t info_size, bytes_read;
296 vm_offset_t offset, task_mem = 0;
297 struct vm_region_basic_info info;
298 mach_port_t dummy;
299 unsigned int page_size = get_page_size();
300 mach_port_t process_port = get_process_port( process );
302 if (!process_port)
304 set_error( STATUS_ACCESS_DENIED );
305 return 0;
308 offset = (unsigned long)ptr % page_size;
309 aligned_address = (vm_address_t)((char *)ptr - offset);
310 aligned_size = (size + offset + page_size - 1) / page_size * page_size;
312 if ((ret = task_suspend( process_port )) != KERN_SUCCESS)
314 mach_set_error( ret );
315 return 0;
318 ret = vm_read( process_port, aligned_address, aligned_size, &task_mem, &bytes_read );
319 if (ret != KERN_SUCCESS)
321 mach_set_error( ret );
322 goto failed;
324 region_address = aligned_address;
325 info_size = sizeof(info);
326 ret = vm_region( process_port, &region_address, &region_size, VM_REGION_BASIC_INFO,
327 (vm_region_info_t)&info, &info_size, &dummy );
328 if (ret != KERN_SUCCESS)
330 mach_set_error( ret );
331 goto failed;
333 if (region_address > aligned_address ||
334 region_address + region_size < aligned_address + aligned_size)
336 /* FIXME: should support multiple regions */
337 set_error( ERROR_ACCESS_DENIED );
338 goto failed;
340 ret = vm_protect( process_port, aligned_address, aligned_size, 0, VM_PROT_READ | VM_PROT_WRITE );
341 if (ret != KERN_SUCCESS)
343 mach_set_error( ret );
344 goto failed;
347 /* FIXME: there's an optimization that can be made: check first and last */
348 /* pages for writability; read first and last pages; write interior */
349 /* pages to task without ever reading&modifying them; if that succeeds, */
350 /* modify first and last pages and write them. */
352 memcpy( (char*)task_mem + offset, src, size );
354 ret = vm_write( process_port, aligned_address, task_mem, bytes_read );
355 if (ret != KERN_SUCCESS) mach_set_error( ret );
356 else
358 vm_deallocate( mach_task_self(), task_mem, bytes_read );
359 /* restore protection */
360 vm_protect( process_port, aligned_address, aligned_size, 0, info.protection );
361 task_resume( process_port );
362 return 1;
365 failed:
366 if (task_mem) vm_deallocate( mach_task_self(), task_mem, bytes_read );
367 task_resume( process_port );
368 return 0;
371 /* retrieve an LDT selector entry */
372 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
373 unsigned int *limit, unsigned char *flags )
375 const unsigned int total_size = (2 * sizeof(int) + 1) * 8192;
376 struct process *process = thread->process;
377 unsigned int page_size = get_page_size();
378 vm_offset_t data;
379 kern_return_t ret;
380 mach_msg_type_number_t bytes_read;
381 mach_port_t process_port = get_process_port( thread->process );
383 if (!process->ldt_copy || !process_port)
385 set_error( STATUS_ACCESS_DENIED );
386 return;
388 if (entry >= 8192)
390 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
391 return;
394 if ((ret = task_suspend( process_port )) == KERN_SUCCESS)
396 void *ptr = process->ldt_copy;
397 vm_offset_t offset = (unsigned long)ptr % page_size;
398 vm_address_t aligned_address = (vm_address_t)((char *)ptr - offset);
399 vm_size_t aligned_size = (total_size + offset + page_size - 1) / page_size * page_size;
401 ret = vm_read( process_port, aligned_address, aligned_size, &data, &bytes_read );
402 if (ret != KERN_SUCCESS) mach_set_error( ret );
403 else
405 const int *ldt = (const int *)((char *)data + offset);
406 memcpy( base, ldt + entry, sizeof(int) );
407 memcpy( limit, ldt + entry + 8192, sizeof(int) );
408 memcpy( flags, (char *)(ldt + 2 * 8192) + entry, 1 );
409 vm_deallocate( mach_task_self(), data, bytes_read );
411 task_resume( process_port );
413 else mach_set_error( ret );
416 #endif /* USE_MACH */