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