inetcomm: Use BOOL type where appropriate.
[wine.git] / dlls / ntdll / signal_arm.c
blob9b0f1ebe19e6a0046c5bab9269bc24e005549148
1 /*
2 * ARM signal handling routines
4 * Copyright 2002 Marcus Meissner, SuSE Linux AG
5 * Copyright 2010, 2011 André Hentschel
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 #ifdef __arm__
24 #include "config.h"
25 #include "wine/port.h"
27 #include <assert.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 #ifdef HAVE_SYSCALL_H
40 # include <syscall.h>
41 #else
42 # ifdef HAVE_SYS_SYSCALL_H
43 # include <sys/syscall.h>
44 # endif
45 #endif
46 #ifdef HAVE_SYS_SIGNAL_H
47 # include <sys/signal.h>
48 #endif
50 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
52 #include "ntstatus.h"
53 #define WIN32_NO_STATUS
54 #include "windef.h"
55 #include "winternl.h"
56 #include "wine/library.h"
57 #include "wine/exception.h"
58 #include "ntdll_misc.h"
59 #include "wine/debug.h"
60 #include "winnt.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(seh);
64 static pthread_key_t teb_key;
66 /***********************************************************************
67 * signal context platform-specific definitions
69 #ifdef linux
71 #ifdef __ANDROID__
72 typedef struct ucontext
74 unsigned long uc_flags;
75 struct ucontext *uc_link;
76 stack_t uc_stack;
77 struct sigcontext uc_mcontext;
78 sigset_t uc_sigmask;
79 unsigned long uc_regspace[128] __attribute__((__aligned__(8)));
80 } ucontext_t;
81 #endif
83 typedef ucontext_t SIGCONTEXT;
85 /* All Registers access - only for local access */
86 # define REG_sig(reg_name, context) ((context)->uc_mcontext.reg_name)
87 # define REGn_sig(reg_num, context) ((context)->uc_mcontext.arm_r##reg_num)
89 /* Special Registers access */
90 # define SP_sig(context) REG_sig(arm_sp, context) /* Stack pointer */
91 # define LR_sig(context) REG_sig(arm_lr, context) /* Link register */
92 # define PC_sig(context) REG_sig(arm_pc, context) /* Program counter */
93 # define CPSR_sig(context) REG_sig(arm_cpsr, context) /* Current State Register */
94 # define IP_sig(context) REG_sig(arm_ip, context) /* Intra-Procedure-call scratch register */
95 # define FP_sig(context) REG_sig(arm_fp, context) /* Frame pointer */
97 /* Exceptions */
98 # define ERROR_sig(context) REG_sig(error_code, context)
99 # define FAULT_sig(context) REG_sig(fault_address, context)
100 # define TRAP_sig(context) REG_sig(trap_no, context)
102 #endif /* linux */
104 enum arm_trap_code
106 TRAP_ARM_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
107 TRAP_ARM_PRIVINFLT = 6, /* Invalid opcode exception */
108 TRAP_ARM_PAGEFLT = 14, /* Page fault */
109 TRAP_ARM_ALIGNFLT = 17, /* Alignment check exception */
112 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
113 typedef int (*wine_signal_handler)(unsigned int sig);
115 static wine_signal_handler handlers[256];
118 struct UNWIND_INFO
120 WORD function_length;
121 WORD unknown1 : 7;
122 WORD count : 5;
123 WORD unknown2 : 4;
126 /***********************************************************************
127 * dispatch_signal
129 static inline int dispatch_signal(unsigned int sig)
131 if (handlers[sig] == NULL) return 0;
132 return handlers[sig](sig);
135 /*******************************************************************
136 * is_valid_frame
138 static inline BOOL is_valid_frame( void *frame )
140 if ((ULONG_PTR)frame & 3) return FALSE;
141 return (frame >= NtCurrentTeb()->Tib.StackLimit &&
142 (void **)frame < (void **)NtCurrentTeb()->Tib.StackBase - 1);
145 /***********************************************************************
146 * save_context
148 * Set the register values from a sigcontext.
150 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
152 #define C(x) context->R##x = REGn_sig(x,sigcontext)
153 /* Save normal registers */
154 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
155 #undef C
157 context->ContextFlags = CONTEXT_FULL;
158 context->Sp = SP_sig(sigcontext); /* Stack pointer */
159 context->Lr = LR_sig(sigcontext); /* Link register */
160 context->Pc = PC_sig(sigcontext); /* Program Counter */
161 context->Cpsr = CPSR_sig(sigcontext); /* Current State Register */
162 context->Ip = IP_sig(sigcontext); /* Intra-Procedure-call scratch register */
163 context->Fp = FP_sig(sigcontext); /* Frame pointer */
167 /***********************************************************************
168 * restore_context
170 * Build a sigcontext from the register values.
172 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
174 #define C(x) REGn_sig(x,sigcontext) = context->R##x
175 /* Restore normal registers */
176 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
177 #undef C
179 SP_sig(sigcontext) = context->Sp; /* Stack pointer */
180 LR_sig(sigcontext) = context->Lr ; /* Link register */
181 PC_sig(sigcontext) = context->Pc; /* Program Counter */
182 CPSR_sig(sigcontext) = context->Cpsr; /* Current State Register */
183 IP_sig(sigcontext) = context->Ip; /* Intra-Procedure-call scratch register */
184 FP_sig(sigcontext) = context->Fp; /* Frame pointer */
188 /***********************************************************************
189 * save_fpu
191 * Set the FPU context from a sigcontext.
193 static inline void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
195 FIXME("not implemented\n");
199 /***********************************************************************
200 * restore_fpu
202 * Restore the FPU context to a sigcontext.
204 static inline void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
206 FIXME("not implemented\n");
210 /***********************************************************************
211 * RtlCaptureContext (NTDLL.@)
213 /* FIXME: Use the Stack instead of the actual register values */
214 __ASM_STDCALL_FUNC( RtlCaptureContext, 4,
215 ".arm\n\t"
216 "stmfd SP!, {r1}\n\t"
217 "mov r1, #0x40\n\t" /* CONTEXT_ARM */
218 "add r1, r1, #0x3\n\t" /* CONTEXT_FULL */
219 "str r1, [r0]\n\t" /* context->ContextFlags */
220 "ldmfd SP!, {r1}\n\t"
221 "str r0, [r0, #0x4]\n\t" /* context->R0 */
222 "str r1, [r0, #0x8]\n\t" /* context->R1 */
223 "str r2, [r0, #0xc]\n\t" /* context->R2 */
224 "str r3, [r0, #0x10]\n\t" /* context->R3 */
225 "str r4, [r0, #0x14]\n\t" /* context->R4 */
226 "str r5, [r0, #0x18]\n\t" /* context->R5 */
227 "str r6, [r0, #0x1c]\n\t" /* context->R6 */
228 "str r7, [r0, #0x20]\n\t" /* context->R7 */
229 "str r8, [r0, #0x24]\n\t" /* context->R8 */
230 "str r9, [r0, #0x28]\n\t" /* context->R9 */
231 "str r10, [r0, #0x2c]\n\t" /* context->R10 */
232 "str r11, [r0, #0x30]\n\t" /* context->Fp */
233 "str IP, [r0, #0x34]\n\t" /* context->Ip */
234 "str SP, [r0, #0x38]\n\t" /* context->Sp */
235 "str LR, [r0, #0x3c]\n\t" /* context->Lr */
236 "str PC, [r0, #0x40]\n\t" /* context->Pc */
237 "mrs r1, CPSR\n\t"
238 "str r1, [r0, #0x44]\n\t" /* context->Cpsr */
239 "mov PC, LR\n"
243 /***********************************************************************
244 * set_cpu_context
246 * Set the new CPU context.
248 /* FIXME: What about the CPSR? */
249 __ASM_GLOBAL_FUNC( set_cpu_context,
250 "mov IP, r0\n\t"
251 "ldr r0, [IP, #0x4]\n\t" /* context->R0 */
252 "ldr r1, [IP, #0x8]\n\t" /* context->R1 */
253 "ldr r2, [IP, #0xc]\n\t" /* context->R2 */
254 "ldr r3, [IP, #0x10]\n\t" /* context->R3 */
255 "ldr r4, [IP, #0x14]\n\t" /* context->R4 */
256 "ldr r5, [IP, #0x18]\n\t" /* context->R5 */
257 "ldr r6, [IP, #0x1c]\n\t" /* context->R6 */
258 "ldr r7, [IP, #0x20]\n\t" /* context->R7 */
259 "ldr r8, [IP, #0x24]\n\t" /* context->R8 */
260 "ldr r9, [IP, #0x28]\n\t" /* context->R9 */
261 "ldr r10, [IP, #0x2c]\n\t" /* context->R10 */
262 "ldr r11, [IP, #0x30]\n\t" /* context->Fp */
263 "ldr SP, [IP, #0x38]\n\t" /* context->Sp */
264 "ldr LR, [IP, #0x3c]\n\t" /* context->Lr */
265 "ldr PC, [IP, #0x40]\n\t" /* context->Pc */
269 /***********************************************************************
270 * copy_context
272 * Copy a register context according to the flags.
274 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
276 flags &= ~CONTEXT_ARM; /* get rid of CPU id */
277 if (flags & CONTEXT_CONTROL)
279 to->Sp = from->Sp;
280 to->Lr = from->Lr;
281 to->Pc = from->Pc;
282 to->Cpsr = from->Cpsr;
284 if (flags & CONTEXT_INTEGER)
286 to->R0 = from->R0;
287 to->R1 = from->R1;
288 to->R2 = from->R2;
289 to->R3 = from->R3;
290 to->R4 = from->R4;
291 to->R5 = from->R5;
292 to->R6 = from->R6;
293 to->R7 = from->R7;
294 to->R8 = from->R8;
295 to->R9 = from->R9;
296 to->R10 = from->R10;
297 to->Ip = from->Ip;
298 to->Fp = from->Fp;
303 /***********************************************************************
304 * context_to_server
306 * Convert a register context to the server format.
308 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
310 DWORD flags = from->ContextFlags & ~CONTEXT_ARM; /* get rid of CPU id */
312 memset( to, 0, sizeof(*to) );
313 to->cpu = CPU_ARM;
315 if (flags & CONTEXT_CONTROL)
317 to->flags |= SERVER_CTX_CONTROL;
318 to->ctl.arm_regs.sp = from->Sp;
319 to->ctl.arm_regs.lr = from->Lr;
320 to->ctl.arm_regs.pc = from->Pc;
321 to->ctl.arm_regs.cpsr = from->Cpsr;
323 if (flags & CONTEXT_INTEGER)
325 to->flags |= SERVER_CTX_INTEGER;
326 to->integer.arm_regs.r[0] = from->R0;
327 to->integer.arm_regs.r[1] = from->R1;
328 to->integer.arm_regs.r[2] = from->R2;
329 to->integer.arm_regs.r[3] = from->R3;
330 to->integer.arm_regs.r[4] = from->R4;
331 to->integer.arm_regs.r[5] = from->R5;
332 to->integer.arm_regs.r[6] = from->R6;
333 to->integer.arm_regs.r[7] = from->R7;
334 to->integer.arm_regs.r[8] = from->R8;
335 to->integer.arm_regs.r[9] = from->R9;
336 to->integer.arm_regs.r[10] = from->R10;
337 to->integer.arm_regs.r[11] = from->Fp;
338 to->integer.arm_regs.r[12] = from->Ip;
340 return STATUS_SUCCESS;
344 /***********************************************************************
345 * context_from_server
347 * Convert a register context from the server format.
349 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
351 if (from->cpu != CPU_ARM) return STATUS_INVALID_PARAMETER;
353 to->ContextFlags = CONTEXT_ARM;
354 if (from->flags & SERVER_CTX_CONTROL)
356 to->ContextFlags |= CONTEXT_CONTROL;
357 to->Sp = from->ctl.arm_regs.sp;
358 to->Lr = from->ctl.arm_regs.lr;
359 to->Pc = from->ctl.arm_regs.pc;
360 to->Cpsr = from->ctl.arm_regs.cpsr;
362 if (from->flags & SERVER_CTX_INTEGER)
364 to->ContextFlags |= CONTEXT_INTEGER;
365 to->R0 = from->integer.arm_regs.r[0];
366 to->R1 = from->integer.arm_regs.r[1];
367 to->R2 = from->integer.arm_regs.r[2];
368 to->R3 = from->integer.arm_regs.r[3];
369 to->R4 = from->integer.arm_regs.r[4];
370 to->R5 = from->integer.arm_regs.r[5];
371 to->R6 = from->integer.arm_regs.r[6];
372 to->R7 = from->integer.arm_regs.r[7];
373 to->R8 = from->integer.arm_regs.r[8];
374 to->R9 = from->integer.arm_regs.r[9];
375 to->R10 = from->integer.arm_regs.r[10];
376 to->Fp = from->integer.arm_regs.r[11];
377 to->Ip = from->integer.arm_regs.r[12];
379 return STATUS_SUCCESS;
382 extern void raise_func_trampoline_thumb( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
383 __ASM_GLOBAL_FUNC( raise_func_trampoline_thumb,
384 ".thumb\n\t"
385 "blx r2\n\t"
386 "bkpt")
388 extern void raise_func_trampoline_arm( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
389 __ASM_GLOBAL_FUNC( raise_func_trampoline_arm,
390 ".arm\n\t"
391 "blx r2\n\t"
392 "bkpt")
394 /***********************************************************************
395 * setup_exception_record
397 * Setup the exception record and context on the thread stack.
399 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
401 struct stack_layout
403 CONTEXT context;
404 EXCEPTION_RECORD rec;
405 } *stack;
406 DWORD exception_code = 0;
408 stack = (struct stack_layout *)(SP_sig(sigcontext) & ~3);
409 stack--; /* push the stack_layout structure */
411 stack->rec.ExceptionRecord = NULL;
412 stack->rec.ExceptionCode = exception_code;
413 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
414 stack->rec.ExceptionAddress = (LPVOID)PC_sig(sigcontext);
415 stack->rec.NumberParameters = 0;
417 save_context( &stack->context, sigcontext );
419 /* now modify the sigcontext to return to the raise function */
420 SP_sig(sigcontext) = (DWORD)stack;
421 if (CPSR_sig(sigcontext) & 0x20)
422 PC_sig(sigcontext) = (DWORD)raise_func_trampoline_thumb;
423 else
424 PC_sig(sigcontext) = (DWORD)raise_func_trampoline_arm;
425 REGn_sig(0, sigcontext) = (DWORD)&stack->rec; /* first arg for raise_func */
426 REGn_sig(1, sigcontext) = (DWORD)&stack->context; /* second arg for raise_func */
427 REGn_sig(2, sigcontext) = (DWORD)func; /* the raise_func as third arg for the trampoline */
430 return &stack->rec;
433 /**********************************************************************
434 * raise_segv_exception
436 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
438 NTSTATUS status;
440 switch(rec->ExceptionCode)
442 case EXCEPTION_ACCESS_VIOLATION:
443 if (rec->NumberParameters == 2)
445 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
446 rec->ExceptionInformation[0] )))
447 goto done;
449 break;
451 status = NtRaiseException( rec, context, TRUE );
452 if (status) raise_status( status, rec );
453 done:
454 set_cpu_context( context );
457 /**********************************************************************
458 * call_stack_handlers
460 * Call the stack handlers chain.
462 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
464 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
465 DWORD res;
467 frame = NtCurrentTeb()->Tib.ExceptionList;
468 nested_frame = NULL;
469 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
471 /* Check frame address */
472 if (!is_valid_frame( frame ))
474 rec->ExceptionFlags |= EH_STACK_INVALID;
475 break;
478 /* Call handler */
479 TRACE( "calling handler at %p code=%x flags=%x\n",
480 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
481 res = frame->Handler( rec, frame, context, &dispatch );
482 TRACE( "handler at %p returned %x\n", frame->Handler, res );
484 if (frame == nested_frame)
486 /* no longer nested */
487 nested_frame = NULL;
488 rec->ExceptionFlags &= ~EH_NESTED_CALL;
491 switch(res)
493 case ExceptionContinueExecution:
494 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
495 return STATUS_NONCONTINUABLE_EXCEPTION;
496 case ExceptionContinueSearch:
497 break;
498 case ExceptionNestedException:
499 if (nested_frame < dispatch) nested_frame = dispatch;
500 rec->ExceptionFlags |= EH_NESTED_CALL;
501 break;
502 default:
503 return STATUS_INVALID_DISPOSITION;
505 frame = frame->Prev;
507 return STATUS_UNHANDLED_EXCEPTION;
511 /*******************************************************************
512 * raise_exception
514 * Implementation of NtRaiseException.
516 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
518 NTSTATUS status;
520 if (first_chance)
522 DWORD c;
524 for (c = 0; c < rec->NumberParameters; c++)
525 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
526 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
528 if (rec->ExceptionInformation[1] >> 16)
529 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
530 rec->ExceptionAddress,
531 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
532 else
533 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
534 rec->ExceptionAddress,
535 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
537 else
539 TRACE(" Pc:%04x Sp:%04x Lr:%04x Cpsr:%04x r0:%04x r1:%04x r2:%04x r3:%04x\n",
540 context->Pc, context->Sp, context->Lr, context->Cpsr,
541 context->R0, context->R1, context->R2, context->R3);
542 TRACE(" r4:%04x r5:%04x r6:%04x r7:%04x r8:%04x r9:%04x r10:%04x Fp:%04x Ip:%04x\n",
543 context->R4, context->R5, context->R6, context->R7, context->R8,
544 context->R9, context->R10, context->Fp, context->Ip );
547 status = send_debug_event( rec, TRUE, context );
548 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
549 return STATUS_SUCCESS;
551 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
552 return STATUS_SUCCESS;
554 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
555 return status;
558 /* last chance exception */
560 status = send_debug_event( rec, FALSE, context );
561 if (status != DBG_CONTINUE)
563 if (rec->ExceptionFlags & EH_STACK_INVALID)
564 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
565 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
566 ERR("Process attempted to continue execution after noncontinuable exception.\n");
567 else
568 ERR("Unhandled exception code %x flags %x addr %p\n",
569 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
570 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
572 return STATUS_SUCCESS;
576 /**********************************************************************
577 * segv_handler
579 * Handler for SIGSEGV and related errors.
581 static void segv_handler( int signal, siginfo_t *info, void *ucontext )
583 EXCEPTION_RECORD *rec;
584 SIGCONTEXT *context = ucontext;
586 /* check for page fault inside the thread stack */
587 if (TRAP_sig(context) == TRAP_ARM_PAGEFLT &&
588 (char *)info->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
589 (char *)info->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
590 virtual_handle_stack_fault( info->si_addr ))
592 /* check if this was the last guard page */
593 if ((char *)info->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
595 rec = setup_exception( context, raise_segv_exception );
596 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
598 return;
601 rec = setup_exception( context, raise_segv_exception );
602 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
604 switch(TRAP_sig(context))
606 case TRAP_ARM_PRIVINFLT: /* Invalid opcode exception */
607 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
608 break;
609 case TRAP_ARM_PAGEFLT: /* Page fault */
610 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
611 rec->NumberParameters = 2;
612 rec->ExceptionInformation[0] = (ERROR_sig(context) & 0x800) != 0;
613 rec->ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
614 break;
615 case TRAP_ARM_ALIGNFLT: /* Alignment check exception */
616 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
617 break;
618 default:
619 ERR("Got unexpected trap %ld\n", TRAP_sig(context));
620 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
621 break;
625 /**********************************************************************
626 * trap_handler
628 * Handler for SIGTRAP.
630 static void trap_handler( int signal, siginfo_t *info, void *ucontext )
632 EXCEPTION_RECORD rec;
633 CONTEXT context;
634 NTSTATUS status;
636 switch ( info->si_code )
638 case TRAP_TRACE:
639 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
640 break;
641 case TRAP_BRKPT:
642 default:
643 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
644 break;
647 save_context( &context, ucontext );
648 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
649 rec.ExceptionRecord = NULL;
650 rec.ExceptionAddress = (LPVOID)context.Pc;
651 rec.NumberParameters = 0;
652 status = raise_exception( &rec, &context, TRUE );
653 if (status) raise_status( status, &rec );
654 restore_context( &context, ucontext );
657 /**********************************************************************
658 * fpe_handler
660 * Handler for SIGFPE.
662 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
664 EXCEPTION_RECORD rec;
665 CONTEXT context;
666 NTSTATUS status;
668 save_fpu( &context, sigcontext );
669 save_context( &context, sigcontext );
671 switch (siginfo->si_code & 0xffff )
673 #ifdef FPE_FLTSUB
674 case FPE_FLTSUB:
675 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
676 break;
677 #endif
678 #ifdef FPE_INTDIV
679 case FPE_INTDIV:
680 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
681 break;
682 #endif
683 #ifdef FPE_INTOVF
684 case FPE_INTOVF:
685 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
686 break;
687 #endif
688 #ifdef FPE_FLTDIV
689 case FPE_FLTDIV:
690 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
691 break;
692 #endif
693 #ifdef FPE_FLTOVF
694 case FPE_FLTOVF:
695 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
696 break;
697 #endif
698 #ifdef FPE_FLTUND
699 case FPE_FLTUND:
700 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
701 break;
702 #endif
703 #ifdef FPE_FLTRES
704 case FPE_FLTRES:
705 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
706 break;
707 #endif
708 #ifdef FPE_FLTINV
709 case FPE_FLTINV:
710 #endif
711 default:
712 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
713 break;
715 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
716 rec.ExceptionRecord = NULL;
717 rec.ExceptionAddress = (LPVOID)context.Pc;
718 rec.NumberParameters = 0;
719 status = raise_exception( &rec, &context, TRUE );
720 if (status) raise_status( status, &rec );
722 restore_context( &context, sigcontext );
723 restore_fpu( &context, sigcontext );
726 /**********************************************************************
727 * int_handler
729 * Handler for SIGINT.
731 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
733 if (!dispatch_signal(SIGINT))
735 EXCEPTION_RECORD rec;
736 CONTEXT context;
737 NTSTATUS status;
739 save_context( &context, sigcontext );
740 rec.ExceptionCode = CONTROL_C_EXIT;
741 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
742 rec.ExceptionRecord = NULL;
743 rec.ExceptionAddress = (LPVOID)context.Pc;
744 rec.NumberParameters = 0;
745 status = raise_exception( &rec, &context, TRUE );
746 if (status) raise_status( status, &rec );
747 restore_context( &context, sigcontext );
752 /**********************************************************************
753 * abrt_handler
755 * Handler for SIGABRT.
757 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
759 EXCEPTION_RECORD rec;
760 CONTEXT context;
761 NTSTATUS status;
763 save_context( &context, sigcontext );
764 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
765 rec.ExceptionFlags = EH_NONCONTINUABLE;
766 rec.ExceptionRecord = NULL;
767 rec.ExceptionAddress = (LPVOID)context.Pc;
768 rec.NumberParameters = 0;
769 status = raise_exception( &rec, &context, TRUE );
770 if (status) raise_status( status, &rec );
771 restore_context( &context, sigcontext );
775 /**********************************************************************
776 * quit_handler
778 * Handler for SIGQUIT.
780 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
782 abort_thread(0);
786 /**********************************************************************
787 * usr1_handler
789 * Handler for SIGUSR1, used to signal a thread that it got suspended.
791 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
793 CONTEXT context;
795 save_context( &context, sigcontext );
796 wait_suspend( &context );
797 restore_context( &context, sigcontext );
801 /***********************************************************************
802 * __wine_set_signal_handler (NTDLL.@)
804 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
806 if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
807 if (handlers[sig] != NULL) return -2;
808 handlers[sig] = wsh;
809 return 0;
813 /**********************************************************************
814 * signal_alloc_thread
816 NTSTATUS signal_alloc_thread( TEB **teb )
818 static size_t sigstack_zero_bits;
819 SIZE_T size;
820 NTSTATUS status;
822 if (!sigstack_zero_bits)
824 size_t min_size = page_size;
825 /* find the first power of two not smaller than min_size */
826 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
827 assert( sizeof(TEB) <= min_size );
830 size = 1 << sigstack_zero_bits;
831 *teb = NULL;
832 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
833 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
835 (*teb)->Tib.Self = &(*teb)->Tib;
836 (*teb)->Tib.ExceptionList = (void *)~0UL;
838 return status;
842 /**********************************************************************
843 * signal_free_thread
845 void signal_free_thread( TEB *teb )
847 SIZE_T size;
849 if (teb->DeallocationStack)
851 size = 0;
852 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
854 size = 0;
855 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
858 /**********************************************************************
859 * set_tpidrurw
861 * Win32/ARM applications expect the TEB pointer to be in the TPIDRURW register.
863 #ifdef __ARM_ARCH_7A__
864 extern void set_tpidrurw( TEB *teb );
865 __ASM_GLOBAL_FUNC( set_tpidrurw,
866 "mcr p15, 0, r0, c13, c0, 2\n\t" /* TEB -> TPIDRURW */
867 "blx lr" )
868 #else
869 void set_tpidrurw( TEB *teb )
872 #endif
874 /**********************************************************************
875 * signal_init_thread
877 void signal_init_thread( TEB *teb )
879 static int init_done;
881 if (!init_done)
883 pthread_key_create( &teb_key, NULL );
884 init_done = 1;
886 set_tpidrurw( teb );
887 pthread_setspecific( teb_key, teb );
891 /**********************************************************************
892 * signal_init_process
894 void signal_init_process(void)
896 struct sigaction sig_act;
898 sig_act.sa_mask = server_block_set;
899 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
901 sig_act.sa_sigaction = int_handler;
902 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
903 sig_act.sa_sigaction = fpe_handler;
904 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
905 sig_act.sa_sigaction = abrt_handler;
906 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
907 sig_act.sa_sigaction = quit_handler;
908 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
909 sig_act.sa_sigaction = usr1_handler;
910 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
912 sig_act.sa_sigaction = segv_handler;
913 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
914 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
915 #ifdef SIGBUS
916 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
917 #endif
919 #ifdef SIGTRAP
920 sig_act.sa_sigaction = trap_handler;
921 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
922 #endif
923 return;
925 error:
926 perror("sigaction");
927 exit(1);
931 /**********************************************************************
932 * __wine_enter_vm86 (NTDLL.@)
934 void __wine_enter_vm86( CONTEXT *context )
936 MESSAGE("vm86 mode not supported on this platform\n");
940 /**********************************************************************
941 * RtlAddFunctionTable (NTDLL.@)
943 BOOLEAN CDECL RtlAddFunctionTable( RUNTIME_FUNCTION *table, DWORD count, DWORD addr )
945 FIXME( "%p %u %x: stub\n", table, count, addr );
946 return TRUE;
950 /**********************************************************************
951 * RtlDeleteFunctionTable (NTDLL.@)
953 BOOLEAN CDECL RtlDeleteFunctionTable( RUNTIME_FUNCTION *table )
955 FIXME( "%p: stub\n", table );
956 return TRUE;
959 /**********************************************************************
960 * find_function_info
962 static RUNTIME_FUNCTION *find_function_info( ULONG_PTR pc, HMODULE module,
963 RUNTIME_FUNCTION *func, ULONG size )
965 int min = 0;
966 int max = size/sizeof(*func) - 1;
968 while (min <= max)
970 int pos = (min + max) / 2;
971 DWORD begin = (func[pos].BeginAddress & ~1), end;
972 if (func[pos].u.s.Flag)
973 end = begin + func[pos].u.s.FunctionLength * 2;
974 else
976 struct UNWIND_INFO *info;
977 info = (struct UNWIND_INFO *)((char *)module + func[pos].u.UnwindData);
978 end = begin + info->function_length * 2;
981 if ((char *)pc < (char *)module + begin) max = pos - 1;
982 else if ((char *)pc >= (char *)module + end) min = pos + 1;
983 else return func + pos;
985 return NULL;
988 /**********************************************************************
989 * RtlLookupFunctionEntry (NTDLL.@)
991 PRUNTIME_FUNCTION WINAPI RtlLookupFunctionEntry( ULONG_PTR pc, DWORD *base,
992 UNWIND_HISTORY_TABLE *table )
994 LDR_MODULE *module;
995 RUNTIME_FUNCTION *func;
996 ULONG size;
998 /* FIXME: should use the history table to make things faster */
1000 if (LdrFindEntryForAddress( (void *)pc, &module ))
1002 WARN( "module not found for %lx\n", pc );
1003 return NULL;
1005 if (!(func = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
1006 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
1008 WARN( "no exception table found in module %p pc %lx\n", module->BaseAddress, pc );
1009 return NULL;
1011 func = find_function_info( pc, module->BaseAddress, func, size );
1012 if (func) *base = (DWORD)module->BaseAddress;
1013 return func;
1016 /***********************************************************************
1017 * RtlUnwind (NTDLL.@)
1019 void WINAPI RtlUnwind( void *endframe, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1021 CONTEXT context;
1022 EXCEPTION_RECORD record;
1023 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
1024 DWORD res;
1026 RtlCaptureContext( &context );
1027 context.R0 = (DWORD)retval;
1029 /* build an exception record, if we do not have one */
1030 if (!rec)
1032 record.ExceptionCode = STATUS_UNWIND;
1033 record.ExceptionFlags = 0;
1034 record.ExceptionRecord = NULL;
1035 record.ExceptionAddress = (void *)context.Pc;
1036 record.NumberParameters = 0;
1037 rec = &record;
1040 rec->ExceptionFlags |= EH_UNWINDING | (endframe ? 0 : EH_EXIT_UNWIND);
1042 TRACE( "code=%x flags=%x\n", rec->ExceptionCode, rec->ExceptionFlags );
1044 /* get chain of exception frames */
1045 frame = NtCurrentTeb()->Tib.ExceptionList;
1046 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != endframe))
1048 /* Check frame address */
1049 if (endframe && ((void*)frame > endframe))
1050 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
1052 if (!is_valid_frame( frame )) raise_status( STATUS_BAD_STACK, rec );
1054 /* Call handler */
1055 TRACE( "calling handler at %p code=%x flags=%x\n",
1056 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
1057 res = frame->Handler(rec, frame, &context, &dispatch);
1058 TRACE( "handler at %p returned %x\n", frame->Handler, res );
1060 switch(res)
1062 case ExceptionContinueSearch:
1063 break;
1064 case ExceptionCollidedUnwind:
1065 frame = dispatch;
1066 break;
1067 default:
1068 raise_status( STATUS_INVALID_DISPOSITION, rec );
1069 break;
1071 frame = __wine_pop_frame( frame );
1075 /*******************************************************************
1076 * NtRaiseException (NTDLL.@)
1078 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
1080 NTSTATUS status = raise_exception( rec, context, first_chance );
1081 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
1082 return status;
1085 /***********************************************************************
1086 * RtlRaiseException (NTDLL.@)
1088 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
1090 CONTEXT context;
1091 NTSTATUS status;
1093 RtlCaptureContext( &context );
1094 rec->ExceptionAddress = (LPVOID)context.Pc;
1095 status = raise_exception( rec, &context, TRUE );
1096 if (status) raise_status( status, rec );
1099 /*************************************************************************
1100 * RtlCaptureStackBackTrace (NTDLL.@)
1102 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1104 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
1105 return 0;
1108 /***********************************************************************
1109 * call_thread_entry_point
1111 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
1113 __TRY
1115 exit_thread( entry( arg ));
1117 __EXCEPT(unhandled_exception_filter)
1119 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
1121 __ENDTRY
1122 abort(); /* should not be reached */
1125 /***********************************************************************
1126 * RtlExitUserThread (NTDLL.@)
1128 void WINAPI RtlExitUserThread( ULONG status )
1130 exit_thread( status );
1133 /***********************************************************************
1134 * abort_thread
1136 void abort_thread( int status )
1138 terminate_thread( status );
1141 /**********************************************************************
1142 * DbgBreakPoint (NTDLL.@)
1144 void WINAPI DbgBreakPoint(void)
1146 kill(getpid(), SIGTRAP);
1149 /**********************************************************************
1150 * DbgUserBreakPoint (NTDLL.@)
1152 void WINAPI DbgUserBreakPoint(void)
1154 kill(getpid(), SIGTRAP);
1157 /**********************************************************************
1158 * NtCurrentTeb (NTDLL.@)
1160 TEB * WINAPI NtCurrentTeb(void)
1162 return pthread_getspecific( teb_key );
1165 #endif /* __arm__ */