Release 1.5.29.
[wine] / dlls / ntdll / signal_arm.c
1 /*
2  * ARM signal handling routines
3  *
4  * Copyright 2002 Marcus Meissner, SuSE Linux AG
5  * Copyright 2010, 2011 AndrĂ© Hentschel
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 #ifdef __arm__
23
24 #include "config.h"
25 #include "wine/port.h"
26
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
35
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
49
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"
61
62 WINE_DEFAULT_DEBUG_CHANNEL(seh);
63
64 static pthread_key_t teb_key;
65
66 /***********************************************************************
67  * signal context platform-specific definitions
68  */
69 #ifdef linux
70
71 typedef ucontext_t SIGCONTEXT;
72
73 /* All Registers access - only for local access */
74 # define REG_sig(reg_name, context) ((context)->uc_mcontext.reg_name)
75 # define REGn_sig(reg_num, context) ((context)->uc_mcontext.arm_r##reg_num)
76
77 /* Special Registers access  */
78 # define SP_sig(context)            REG_sig(arm_sp, context)    /* Stack pointer */
79 # define LR_sig(context)            REG_sig(arm_lr, context)    /* Link register */
80 # define PC_sig(context)            REG_sig(arm_pc, context)    /* Program counter */
81 # define CPSR_sig(context)          REG_sig(arm_cpsr, context)  /* Current State Register */
82 # define IP_sig(context)            REG_sig(arm_ip, context)    /* Intra-Procedure-call scratch register */
83 # define FP_sig(context)            REG_sig(arm_fp, context)    /* Frame pointer */
84
85 /* Exceptions */
86 # define ERROR_sig(context)         REG_sig(error_code, context)
87 # define FAULT_sig(context)         REG_sig(fault_address, context)
88 # define TRAP_sig(context)          REG_sig(trap_no, context)
89
90 #endif /* linux */
91
92 enum arm_trap_code
93 {
94     TRAP_ARM_UNKNOWN    = -1,  /* Unknown fault (TRAP_sig not defined) */
95     TRAP_ARM_PRIVINFLT  =  6,  /* Invalid opcode exception */
96     TRAP_ARM_PAGEFLT    = 14,  /* Page fault */
97     TRAP_ARM_ALIGNFLT   = 17,  /* Alignment check exception */
98 };
99
100 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
101 typedef int (*wine_signal_handler)(unsigned int sig);
102
103 static wine_signal_handler handlers[256];
104
105
106 struct UNWIND_INFO
107 {
108     WORD function_length;
109     WORD unknown1 : 7;
110     WORD count : 5;
111     WORD unknown2 : 4;
112 };
113
114 /***********************************************************************
115  *           dispatch_signal
116  */
117 static inline int dispatch_signal(unsigned int sig)
118 {
119     if (handlers[sig] == NULL) return 0;
120     return handlers[sig](sig);
121 }
122
123 /*******************************************************************
124  *         is_valid_frame
125  */
126 static inline BOOL is_valid_frame( void *frame )
127 {
128     if ((ULONG_PTR)frame & 3) return FALSE;
129     return (frame >= NtCurrentTeb()->Tib.StackLimit &&
130             (void **)frame < (void **)NtCurrentTeb()->Tib.StackBase - 1);
131 }
132
133 /***********************************************************************
134  *           save_context
135  *
136  * Set the register values from a sigcontext.
137  */
138 static void save_context( CONTEXT *context, const SIGCONTEXT *sigcontext )
139 {
140 #define C(x) context->R##x = REGn_sig(x,sigcontext)
141     /* Save normal registers */
142     C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
143 #undef C
144
145     context->ContextFlags = CONTEXT_FULL;
146     context->Sp   = SP_sig(sigcontext);   /* Stack pointer */
147     context->Lr   = LR_sig(sigcontext);   /* Link register */
148     context->Pc   = PC_sig(sigcontext);   /* Program Counter */
149     context->Cpsr = CPSR_sig(sigcontext); /* Current State Register */
150     context->Ip   = IP_sig(sigcontext);   /* Intra-Procedure-call scratch register */
151     context->Fp   = FP_sig(sigcontext);   /* Frame pointer */
152 }
153
154
155 /***********************************************************************
156  *           restore_context
157  *
158  * Build a sigcontext from the register values.
159  */
160 static void restore_context( const CONTEXT *context, SIGCONTEXT *sigcontext )
161 {
162 #define C(x)  REGn_sig(x,sigcontext) = context->R##x
163     /* Restore normal registers */
164     C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
165 #undef C
166
167     SP_sig(sigcontext)   = context->Sp;   /* Stack pointer */
168     LR_sig(sigcontext)   = context->Lr ;  /* Link register */
169     PC_sig(sigcontext)   = context->Pc;   /* Program Counter */
170     CPSR_sig(sigcontext) = context->Cpsr; /* Current State Register */
171     IP_sig(sigcontext)   = context->Ip;   /* Intra-Procedure-call scratch register */
172     FP_sig(sigcontext)   = context->Fp;   /* Frame pointer */
173 }
174
175
176 /***********************************************************************
177  *           save_fpu
178  *
179  * Set the FPU context from a sigcontext.
180  */
181 static inline void save_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
182 {
183     FIXME("not implemented\n");
184 }
185
186
187 /***********************************************************************
188  *           restore_fpu
189  *
190  * Restore the FPU context to a sigcontext.
191  */
192 static inline void restore_fpu( CONTEXT *context, const SIGCONTEXT *sigcontext )
193 {
194     FIXME("not implemented\n");
195 }
196
197
198 /***********************************************************************
199  *              RtlCaptureContext (NTDLL.@)
200  */
201 /* FIXME: Use the Stack instead of the actual register values */
202 __ASM_STDCALL_FUNC( RtlCaptureContext, 4,
203                     ".arm\n\t"
204                     "stmfd SP!, {r1}\n\t"
205                     "mov r1, #0x40\n\t"     /* CONTEXT_ARM */
206                     "add r1, r1, #0x3\n\t"  /* CONTEXT_FULL */
207                     "str r1, [r0]\n\t"      /* context->ContextFlags */
208                     "ldmfd SP!, {r1}\n\t"
209                     "str r0, [r0, #0x4]\n\t"   /* context->R0 */
210                     "str r1, [r0, #0x8]\n\t"   /* context->R1 */
211                     "str r2, [r0, #0xc]\n\t"   /* context->R2 */
212                     "str r3, [r0, #0x10]\n\t"  /* context->R3 */
213                     "str r4, [r0, #0x14]\n\t"  /* context->R4 */
214                     "str r5, [r0, #0x18]\n\t"  /* context->R5 */
215                     "str r6, [r0, #0x1c]\n\t"  /* context->R6 */
216                     "str r7, [r0, #0x20]\n\t"  /* context->R7 */
217                     "str r8, [r0, #0x24]\n\t"  /* context->R8 */
218                     "str r9, [r0, #0x28]\n\t"  /* context->R9 */
219                     "str r10, [r0, #0x2c]\n\t" /* context->R10 */
220                     "str r11, [r0, #0x30]\n\t" /* context->Fp */
221                     "str IP, [r0, #0x34]\n\t"  /* context->Ip */
222                     "str SP, [r0, #0x38]\n\t"  /* context->Sp */
223                     "str LR, [r0, #0x3c]\n\t"  /* context->Lr */
224                     "str LR, [r0, #0x40]\n\t"  /* context->Pc */
225                     "mrs r1, CPSR\n\t"
226                     "str r1, [r0, #0x44]\n\t"  /* context->Cpsr */
227                     "mov PC, LR\n"
228                     )
229
230
231 /***********************************************************************
232  *           set_cpu_context
233  *
234  * Set the new CPU context.
235  */
236 /* FIXME: What about the CPSR? */
237 __ASM_GLOBAL_FUNC( set_cpu_context,
238                    "mov IP, r0\n\t"
239                    "ldr r0,  [IP, #0x4]\n\t"  /* context->R0 */
240                    "ldr r1,  [IP, #0x8]\n\t"  /* context->R1 */
241                    "ldr r2,  [IP, #0xc]\n\t"  /* context->R2 */
242                    "ldr r3,  [IP, #0x10]\n\t" /* context->R3 */
243                    "ldr r4,  [IP, #0x14]\n\t" /* context->R4 */
244                    "ldr r5,  [IP, #0x18]\n\t" /* context->R5 */
245                    "ldr r6,  [IP, #0x1c]\n\t" /* context->R6 */
246                    "ldr r7,  [IP, #0x20]\n\t" /* context->R7 */
247                    "ldr r8,  [IP, #0x24]\n\t" /* context->R8 */
248                    "ldr r9,  [IP, #0x28]\n\t" /* context->R9 */
249                    "ldr r10, [IP, #0x2c]\n\t" /* context->R10 */
250                    "ldr r11, [IP, #0x30]\n\t" /* context->Fp */
251                    "ldr SP,  [IP, #0x38]\n\t" /* context->Sp */
252                    "ldr LR,  [IP, #0x3c]\n\t" /* context->Lr */
253                    "ldr PC,  [IP, #0x40]\n\t" /* context->Pc */
254                    )
255
256
257 /***********************************************************************
258  *           copy_context
259  *
260  * Copy a register context according to the flags.
261  */
262 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
263 {
264     flags &= ~CONTEXT_ARM;  /* get rid of CPU id */
265     if (flags & CONTEXT_CONTROL)
266     {
267         to->Sp      = from->Sp;
268         to->Lr      = from->Lr;
269         to->Pc      = from->Pc;
270         to->Cpsr    = from->Cpsr;
271     }
272     if (flags & CONTEXT_INTEGER)
273     {
274         to->R0  = from->R0;
275         to->R1  = from->R1;
276         to->R2  = from->R2;
277         to->R3  = from->R3;
278         to->R4  = from->R4;
279         to->R5  = from->R5;
280         to->R6  = from->R6;
281         to->R7  = from->R7;
282         to->R8  = from->R8;
283         to->R9  = from->R9;
284         to->R10 = from->R10;
285         to->Ip  = from->Ip;
286         to->Fp  = from->Fp;
287     }
288 }
289
290
291 /***********************************************************************
292  *           context_to_server
293  *
294  * Convert a register context to the server format.
295  */
296 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
297 {
298     DWORD flags = from->ContextFlags & ~CONTEXT_ARM;  /* get rid of CPU id */
299
300     memset( to, 0, sizeof(*to) );
301     to->cpu = CPU_ARM;
302
303     if (flags & CONTEXT_CONTROL)
304     {
305         to->flags |= SERVER_CTX_CONTROL;
306         to->ctl.arm_regs.sp   = from->Sp;
307         to->ctl.arm_regs.lr   = from->Lr;
308         to->ctl.arm_regs.pc   = from->Pc;
309         to->ctl.arm_regs.cpsr = from->Cpsr;
310     }
311     if (flags & CONTEXT_INTEGER)
312     {
313         to->flags |= SERVER_CTX_INTEGER;
314         to->integer.arm_regs.r[0]  = from->R0;
315         to->integer.arm_regs.r[1]  = from->R1;
316         to->integer.arm_regs.r[2]  = from->R2;
317         to->integer.arm_regs.r[3]  = from->R3;
318         to->integer.arm_regs.r[4]  = from->R4;
319         to->integer.arm_regs.r[5]  = from->R5;
320         to->integer.arm_regs.r[6]  = from->R6;
321         to->integer.arm_regs.r[7]  = from->R7;
322         to->integer.arm_regs.r[8]  = from->R8;
323         to->integer.arm_regs.r[9]  = from->R9;
324         to->integer.arm_regs.r[10] = from->R10;
325         to->integer.arm_regs.r[11] = from->Fp;
326         to->integer.arm_regs.r[12] = from->Ip;
327     }
328     return STATUS_SUCCESS;
329 }
330
331
332 /***********************************************************************
333  *           context_from_server
334  *
335  * Convert a register context from the server format.
336  */
337 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
338 {
339     if (from->cpu != CPU_ARM) return STATUS_INVALID_PARAMETER;
340
341     to->ContextFlags = CONTEXT_ARM;
342     if (from->flags & SERVER_CTX_CONTROL)
343     {
344         to->ContextFlags |= CONTEXT_CONTROL;
345         to->Sp   = from->ctl.arm_regs.sp;
346         to->Lr   = from->ctl.arm_regs.lr;
347         to->Pc   = from->ctl.arm_regs.pc;
348         to->Cpsr = from->ctl.arm_regs.cpsr;
349     }
350     if (from->flags & SERVER_CTX_INTEGER)
351     {
352         to->ContextFlags |= CONTEXT_INTEGER;
353         to->R0  = from->integer.arm_regs.r[0];
354         to->R1  = from->integer.arm_regs.r[1];
355         to->R2  = from->integer.arm_regs.r[2];
356         to->R3  = from->integer.arm_regs.r[3];
357         to->R4  = from->integer.arm_regs.r[4];
358         to->R5  = from->integer.arm_regs.r[5];
359         to->R6  = from->integer.arm_regs.r[6];
360         to->R7  = from->integer.arm_regs.r[7];
361         to->R8  = from->integer.arm_regs.r[8];
362         to->R9  = from->integer.arm_regs.r[9];
363         to->R10 = from->integer.arm_regs.r[10];
364         to->Fp  = from->integer.arm_regs.r[11];
365         to->Ip  = from->integer.arm_regs.r[12];
366      }
367     return STATUS_SUCCESS;
368 }
369
370 extern void raise_func_trampoline_thumb( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
371 __ASM_GLOBAL_FUNC( raise_func_trampoline_thumb,
372                    ".thumb\n\t"
373                    "blx r2\n\t"
374                    "bkpt")
375
376 extern void raise_func_trampoline_arm( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
377 __ASM_GLOBAL_FUNC( raise_func_trampoline_arm,
378                    ".arm\n\t"
379                    "blx r2\n\t"
380                    "bkpt")
381
382 /***********************************************************************
383  *           setup_exception_record
384  *
385  * Setup the exception record and context on the thread stack.
386  */
387 static EXCEPTION_RECORD *setup_exception( SIGCONTEXT *sigcontext, raise_func func )
388 {
389     struct stack_layout
390     {
391         CONTEXT           context;
392         EXCEPTION_RECORD  rec;
393     } *stack;
394     DWORD exception_code = 0;
395
396     stack = (struct stack_layout *)(SP_sig(sigcontext) & ~3);
397     stack--;  /* push the stack_layout structure */
398
399     stack->rec.ExceptionRecord  = NULL;
400     stack->rec.ExceptionCode    = exception_code;
401     stack->rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
402     stack->rec.ExceptionAddress = (LPVOID)PC_sig(sigcontext);
403     stack->rec.NumberParameters = 0;
404
405     save_context( &stack->context, sigcontext );
406
407     /* now modify the sigcontext to return to the raise function */
408     SP_sig(sigcontext) = (DWORD)stack;
409     if (CPSR_sig(sigcontext) & 0x20)
410         PC_sig(sigcontext) = (DWORD)raise_func_trampoline_thumb;
411     else
412         PC_sig(sigcontext) = (DWORD)raise_func_trampoline_arm;
413     REGn_sig(0, sigcontext) = (DWORD)&stack->rec;  /* first arg for raise_func */
414     REGn_sig(1, sigcontext) = (DWORD)&stack->context; /* second arg for raise_func */
415     REGn_sig(2, sigcontext) = (DWORD)func; /* the raise_func as third arg for the trampoline */
416
417
418     return &stack->rec;
419 }
420
421 /**********************************************************************
422  *              raise_segv_exception
423  */
424 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
425 {
426     NTSTATUS status;
427
428     switch(rec->ExceptionCode)
429     {
430     case EXCEPTION_ACCESS_VIOLATION:
431         if (rec->NumberParameters == 2)
432         {
433             if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
434                                                              rec->ExceptionInformation[0] )))
435                 goto done;
436         }
437         break;
438     }
439     status = NtRaiseException( rec, context, TRUE );
440     if (status) raise_status( status, rec );
441 done:
442     set_cpu_context( context );
443 }
444
445 /**********************************************************************
446  *           call_stack_handlers
447  *
448  * Call the stack handlers chain.
449  */
450 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
451 {
452     EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
453     DWORD res;
454
455     frame = NtCurrentTeb()->Tib.ExceptionList;
456     nested_frame = NULL;
457     while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
458     {
459         /* Check frame address */
460         if (!is_valid_frame( frame ))
461         {
462             rec->ExceptionFlags |= EH_STACK_INVALID;
463             break;
464         }
465
466         /* Call handler */
467         TRACE( "calling handler at %p code=%x flags=%x\n",
468                frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
469         res = frame->Handler( rec, frame, context, &dispatch );
470         TRACE( "handler at %p returned %x\n", frame->Handler, res );
471
472         if (frame == nested_frame)
473         {
474             /* no longer nested */
475             nested_frame = NULL;
476             rec->ExceptionFlags &= ~EH_NESTED_CALL;
477         }
478
479         switch(res)
480         {
481         case ExceptionContinueExecution:
482             if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
483             return STATUS_NONCONTINUABLE_EXCEPTION;
484         case ExceptionContinueSearch:
485             break;
486         case ExceptionNestedException:
487             if (nested_frame < dispatch) nested_frame = dispatch;
488             rec->ExceptionFlags |= EH_NESTED_CALL;
489             break;
490         default:
491             return STATUS_INVALID_DISPOSITION;
492         }
493         frame = frame->Prev;
494     }
495     return STATUS_UNHANDLED_EXCEPTION;
496 }
497
498
499 /*******************************************************************
500  *              raise_exception
501  *
502  * Implementation of NtRaiseException.
503  */
504 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
505 {
506     NTSTATUS status;
507
508     if (first_chance)
509     {
510         DWORD c;
511
512         for (c = 0; c < rec->NumberParameters; c++)
513             TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
514         if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
515         {
516             if (rec->ExceptionInformation[1] >> 16)
517                 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
518                          rec->ExceptionAddress,
519                          (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
520             else
521                 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
522                          rec->ExceptionAddress,
523                          (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
524         }
525         else
526         {
527             TRACE(" Pc:%04x Sp:%04x Lr:%04x Cpsr:%04x r0:%04x r1:%04x r2:%04x r3:%04x\n",
528                   context->Pc, context->Sp, context->Lr, context->Cpsr,
529                   context->R0, context->R1, context->R2, context->R3);
530             TRACE(" r4:%04x r5:%04x  r6:%04x  r7:%04x r8:%04x r9:%04x r10:%04x Fp:%04x Ip:%04x\n",
531                   context->R4, context->R5, context->R6, context->R7, context->R8,
532                   context->R9, context->R10, context->Fp, context->Ip );
533         }
534
535         status = send_debug_event( rec, TRUE, context );
536         if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
537             return STATUS_SUCCESS;
538
539         if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
540             return STATUS_SUCCESS;
541
542         if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
543             return status;
544     }
545
546     /* last chance exception */
547
548     status = send_debug_event( rec, FALSE, context );
549     if (status != DBG_CONTINUE)
550     {
551         if (rec->ExceptionFlags & EH_STACK_INVALID)
552             ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
553         else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
554             ERR("Process attempted to continue execution after noncontinuable exception.\n");
555         else
556             ERR("Unhandled exception code %x flags %x addr %p\n",
557                 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
558         NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
559     }
560     return STATUS_SUCCESS;
561 }
562
563
564 /**********************************************************************
565  *              segv_handler
566  *
567  * Handler for SIGSEGV and related errors.
568  */
569 static void segv_handler( int signal, siginfo_t *info, void *ucontext )
570 {
571     EXCEPTION_RECORD *rec;
572     SIGCONTEXT *context = ucontext;
573
574     /* check for page fault inside the thread stack */
575     if (TRAP_sig(context) == TRAP_ARM_PAGEFLT &&
576         (char *)info->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
577         (char *)info->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
578         virtual_handle_stack_fault( info->si_addr ))
579     {
580         /* check if this was the last guard page */
581         if ((char *)info->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
582         {
583             rec = setup_exception( context, raise_segv_exception );
584             rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
585         }
586         return;
587     }
588
589     rec = setup_exception( context, raise_segv_exception );
590     if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
591
592     switch(TRAP_sig(context))
593     {
594     case TRAP_ARM_PRIVINFLT:   /* Invalid opcode exception */
595         rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
596         break;
597     case TRAP_ARM_PAGEFLT:  /* Page fault */
598         rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
599         rec->NumberParameters = 2;
600         rec->ExceptionInformation[0] = (ERROR_sig(context) & 0x800) != 0;
601         rec->ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
602         break;
603     case TRAP_ARM_ALIGNFLT:  /* Alignment check exception */
604         rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
605         break;
606     default:
607         ERR("Got unexpected trap %ld\n", TRAP_sig(context));
608         rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
609         break;
610     }
611 }
612
613 /**********************************************************************
614  *              trap_handler
615  *
616  * Handler for SIGTRAP.
617  */
618 static void trap_handler( int signal, siginfo_t *info, void *ucontext )
619 {
620     EXCEPTION_RECORD rec;
621     CONTEXT context;
622     NTSTATUS status;
623
624     switch ( info->si_code )
625     {
626     case TRAP_TRACE:
627         rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
628         break;
629     case TRAP_BRKPT:
630     default:
631         rec.ExceptionCode = EXCEPTION_BREAKPOINT;
632         break;
633     }
634
635     save_context( &context, ucontext );
636     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
637     rec.ExceptionRecord  = NULL;
638     rec.ExceptionAddress = (LPVOID)context.Pc;
639     rec.NumberParameters = 0;
640     status = raise_exception( &rec, &context, TRUE );
641     if (status) raise_status( status, &rec );
642     restore_context( &context, ucontext );
643 }
644
645 /**********************************************************************
646  *              fpe_handler
647  *
648  * Handler for SIGFPE.
649  */
650 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
651 {
652     EXCEPTION_RECORD rec;
653     CONTEXT context;
654     NTSTATUS status;
655
656     save_fpu( &context, sigcontext );
657     save_context( &context, sigcontext );
658
659     switch (siginfo->si_code & 0xffff )
660     {
661 #ifdef FPE_FLTSUB
662     case FPE_FLTSUB:
663         rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
664         break;
665 #endif
666 #ifdef FPE_INTDIV
667     case FPE_INTDIV:
668         rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
669         break;
670 #endif
671 #ifdef FPE_INTOVF
672     case FPE_INTOVF:
673         rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
674         break;
675 #endif
676 #ifdef FPE_FLTDIV
677     case FPE_FLTDIV:
678         rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
679         break;
680 #endif
681 #ifdef FPE_FLTOVF
682     case FPE_FLTOVF:
683         rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
684         break;
685 #endif
686 #ifdef FPE_FLTUND
687     case FPE_FLTUND:
688         rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
689         break;
690 #endif
691 #ifdef FPE_FLTRES
692     case FPE_FLTRES:
693         rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
694         break;
695 #endif
696 #ifdef FPE_FLTINV
697     case FPE_FLTINV:
698 #endif
699     default:
700         rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
701         break;
702     }
703     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
704     rec.ExceptionRecord  = NULL;
705     rec.ExceptionAddress = (LPVOID)context.Pc;
706     rec.NumberParameters = 0;
707     status = raise_exception( &rec, &context, TRUE );
708     if (status) raise_status( status, &rec );
709
710     restore_context( &context, sigcontext );
711     restore_fpu( &context, sigcontext );
712 }
713
714 /**********************************************************************
715  *              int_handler
716  *
717  * Handler for SIGINT.
718  */
719 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
720 {
721     if (!dispatch_signal(SIGINT))
722     {
723         EXCEPTION_RECORD rec;
724         CONTEXT context;
725         NTSTATUS status;
726
727         save_context( &context, sigcontext );
728         rec.ExceptionCode    = CONTROL_C_EXIT;
729         rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
730         rec.ExceptionRecord  = NULL;
731         rec.ExceptionAddress = (LPVOID)context.Pc;
732         rec.NumberParameters = 0;
733         status = raise_exception( &rec, &context, TRUE );
734         if (status) raise_status( status, &rec );
735         restore_context( &context, sigcontext );
736     }
737 }
738
739
740 /**********************************************************************
741  *              abrt_handler
742  *
743  * Handler for SIGABRT.
744  */
745 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
746 {
747     EXCEPTION_RECORD rec;
748     CONTEXT context;
749     NTSTATUS status;
750
751     save_context( &context, sigcontext );
752     rec.ExceptionCode    = EXCEPTION_WINE_ASSERTION;
753     rec.ExceptionFlags   = EH_NONCONTINUABLE;
754     rec.ExceptionRecord  = NULL;
755     rec.ExceptionAddress = (LPVOID)context.Pc;
756     rec.NumberParameters = 0;
757     status = raise_exception( &rec, &context, TRUE );
758     if (status) raise_status( status, &rec );
759     restore_context( &context, sigcontext );
760 }
761
762
763 /**********************************************************************
764  *              quit_handler
765  *
766  * Handler for SIGQUIT.
767  */
768 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
769 {
770     abort_thread(0);
771 }
772
773
774 /**********************************************************************
775  *              usr1_handler
776  *
777  * Handler for SIGUSR1, used to signal a thread that it got suspended.
778  */
779 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
780 {
781     CONTEXT context;
782
783     save_context( &context, sigcontext );
784     wait_suspend( &context );
785     restore_context( &context, sigcontext );
786 }
787
788
789 /***********************************************************************
790  *           __wine_set_signal_handler   (NTDLL.@)
791  */
792 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
793 {
794     if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
795     if (handlers[sig] != NULL) return -2;
796     handlers[sig] = wsh;
797     return 0;
798 }
799
800
801 /**********************************************************************
802  *             signal_alloc_thread
803  */
804 NTSTATUS signal_alloc_thread( TEB **teb )
805 {
806     static size_t sigstack_zero_bits;
807     SIZE_T size;
808     NTSTATUS status;
809
810     if (!sigstack_zero_bits)
811     {
812         size_t min_size = page_size;
813         /* find the first power of two not smaller than min_size */
814         while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
815         assert( sizeof(TEB) <= min_size );
816     }
817
818     size = 1 << sigstack_zero_bits;
819     *teb = NULL;
820     if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
821                                             &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
822     {
823         (*teb)->Tib.Self = &(*teb)->Tib;
824         (*teb)->Tib.ExceptionList = (void *)~0UL;
825     }
826     return status;
827 }
828
829
830 /**********************************************************************
831  *             signal_free_thread
832  */
833 void signal_free_thread( TEB *teb )
834 {
835     SIZE_T size;
836
837     if (teb->DeallocationStack)
838     {
839         size = 0;
840         NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
841     }
842     size = 0;
843     NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
844 }
845
846 /**********************************************************************
847  *      set_tpidrurw
848  *
849  * Win32/ARM applications expect the TEB pointer to be in the TPIDRURW register.
850  */
851 #ifdef __ARM_ARCH_7A__
852 extern void set_tpidrurw( TEB *teb );
853 __ASM_GLOBAL_FUNC( set_tpidrurw,
854                    "mcr p15, 0, r0, c13, c0, 2\n\t" /* TEB -> TPIDRURW */
855                    "blx lr" )
856 #else
857 void set_tpidrurw( TEB *teb )
858 {
859 }
860 #endif
861
862 /**********************************************************************
863  *              signal_init_thread
864  */
865 void signal_init_thread( TEB *teb )
866 {
867     static int init_done;
868
869     if (!init_done)
870     {
871         pthread_key_create( &teb_key, NULL );
872         init_done = 1;
873     }
874     set_tpidrurw( teb );
875     pthread_setspecific( teb_key, teb );
876 }
877
878
879 /**********************************************************************
880  *              signal_init_process
881  */
882 void signal_init_process(void)
883 {
884     struct sigaction sig_act;
885
886     sig_act.sa_mask = server_block_set;
887     sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
888
889     sig_act.sa_sigaction = int_handler;
890     if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
891     sig_act.sa_sigaction = fpe_handler;
892     if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
893     sig_act.sa_sigaction = abrt_handler;
894     if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
895     sig_act.sa_sigaction = quit_handler;
896     if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
897     sig_act.sa_sigaction = usr1_handler;
898     if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
899
900     sig_act.sa_sigaction = segv_handler;
901     if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
902     if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
903 #ifdef SIGBUS
904     if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
905 #endif
906
907 #ifdef SIGTRAP
908     sig_act.sa_sigaction = trap_handler;
909     if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
910 #endif
911     return;
912
913  error:
914     perror("sigaction");
915     exit(1);
916 }
917
918
919 /**********************************************************************
920  *              __wine_enter_vm86   (NTDLL.@)
921  */
922 void __wine_enter_vm86( CONTEXT *context )
923 {
924     MESSAGE("vm86 mode not supported on this platform\n");
925 }
926
927
928 /**********************************************************************
929  *              RtlAddFunctionTable   (NTDLL.@)
930  */
931 BOOLEAN CDECL RtlAddFunctionTable( RUNTIME_FUNCTION *table, DWORD count, DWORD addr )
932 {
933     FIXME( "%p %u %x: stub\n", table, count, addr );
934     return TRUE;
935 }
936
937
938 /**********************************************************************
939  *              RtlDeleteFunctionTable   (NTDLL.@)
940  */
941 BOOLEAN CDECL RtlDeleteFunctionTable( RUNTIME_FUNCTION *table )
942 {
943     FIXME( "%p: stub\n", table );
944     return TRUE;
945 }
946
947 /**********************************************************************
948  *              find_function_info
949  */
950 static RUNTIME_FUNCTION *find_function_info( ULONG_PTR pc, HMODULE module,
951                                              RUNTIME_FUNCTION *func, ULONG size )
952 {
953     int min = 0;
954     int max = size/sizeof(*func) - 1;
955
956     while (min <= max)
957     {
958         int pos = (min + max) / 2;
959         DWORD begin = (func[pos].BeginAddress & ~1), end;
960         if (func[pos].u.s.Flag)
961             end = begin + func[pos].u.s.FunctionLength * 2;
962         else
963         {
964             struct UNWIND_INFO *info;
965             info = (struct UNWIND_INFO *)((char *)module + func[pos].u.UnwindData);
966             end = begin + info->function_length * 2;
967         }
968
969         if ((char *)pc < (char *)module + begin) max = pos - 1;
970         else if ((char *)pc >= (char *)module + end) min = pos + 1;
971         else return func + pos;
972     }
973     return NULL;
974 }
975
976 /**********************************************************************
977  *              RtlLookupFunctionEntry   (NTDLL.@)
978  */
979 PRUNTIME_FUNCTION WINAPI RtlLookupFunctionEntry( ULONG_PTR pc, DWORD *base,
980                                                  UNWIND_HISTORY_TABLE *table )
981 {
982     LDR_MODULE *module;
983     RUNTIME_FUNCTION *func;
984     ULONG size;
985
986     /* FIXME: should use the history table to make things faster */
987
988     if (LdrFindEntryForAddress( (void *)pc, &module ))
989     {
990         WARN( "module not found for %lx\n", pc );
991         return NULL;
992     }
993     if (!(func = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
994                                                IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
995     {
996         WARN( "no exception table found in module %p pc %lx\n", module->BaseAddress, pc );
997         return NULL;
998     }
999     func = find_function_info( pc, module->BaseAddress, func, size );
1000     if (func) *base = (DWORD)module->BaseAddress;
1001     return func;
1002 }
1003
1004 /***********************************************************************
1005  *            RtlUnwind  (NTDLL.@)
1006  */
1007 void WINAPI RtlUnwind( void *endframe, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1008 {
1009     CONTEXT context;
1010     EXCEPTION_RECORD record;
1011     EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
1012     DWORD res;
1013
1014     RtlCaptureContext( &context );
1015     context.R0 = (DWORD)retval;
1016
1017     /* build an exception record, if we do not have one */
1018     if (!rec)
1019     {
1020         record.ExceptionCode    = STATUS_UNWIND;
1021         record.ExceptionFlags   = 0;
1022         record.ExceptionRecord  = NULL;
1023         record.ExceptionAddress = (void *)context.Pc;
1024         record.NumberParameters = 0;
1025         rec = &record;
1026     }
1027
1028     rec->ExceptionFlags |= EH_UNWINDING | (endframe ? 0 : EH_EXIT_UNWIND);
1029
1030     TRACE( "code=%x flags=%x\n", rec->ExceptionCode, rec->ExceptionFlags );
1031
1032     /* get chain of exception frames */
1033     frame = NtCurrentTeb()->Tib.ExceptionList;
1034     while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != endframe))
1035     {
1036         /* Check frame address */
1037         if (endframe && ((void*)frame > endframe))
1038             raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
1039
1040         if (!is_valid_frame( frame )) raise_status( STATUS_BAD_STACK, rec );
1041
1042         /* Call handler */
1043         TRACE( "calling handler at %p code=%x flags=%x\n",
1044                frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
1045         res = frame->Handler(rec, frame, &context, &dispatch);
1046         TRACE( "handler at %p returned %x\n", frame->Handler, res );
1047
1048         switch(res)
1049         {
1050         case ExceptionContinueSearch:
1051             break;
1052         case ExceptionCollidedUnwind:
1053             frame = dispatch;
1054             break;
1055         default:
1056             raise_status( STATUS_INVALID_DISPOSITION, rec );
1057             break;
1058         }
1059         frame = __wine_pop_frame( frame );
1060     }
1061 }
1062
1063 /*******************************************************************
1064  *              NtRaiseException (NTDLL.@)
1065  */
1066 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
1067 {
1068     NTSTATUS status = raise_exception( rec, context, first_chance );
1069     if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
1070     return status;
1071 }
1072
1073 /***********************************************************************
1074  *              RtlRaiseException (NTDLL.@)
1075  */
1076 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
1077 {
1078     CONTEXT context;
1079     NTSTATUS status;
1080
1081     RtlCaptureContext( &context );
1082     rec->ExceptionAddress = (LPVOID)context.Pc;
1083     status = raise_exception( rec, &context, TRUE );
1084     if (status) raise_status( status, rec );
1085 }
1086
1087 /*************************************************************************
1088  *             RtlCaptureStackBackTrace (NTDLL.@)
1089  */
1090 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1091 {
1092     FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
1093     return 0;
1094 }
1095
1096 /***********************************************************************
1097  *           call_thread_entry_point
1098  */
1099 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
1100 {
1101     __TRY
1102     {
1103         exit_thread( entry( arg ));
1104     }
1105     __EXCEPT(unhandled_exception_filter)
1106     {
1107         NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
1108     }
1109     __ENDTRY
1110     abort();  /* should not be reached */
1111 }
1112
1113 /***********************************************************************
1114  *           RtlExitUserThread  (NTDLL.@)
1115  */
1116 void WINAPI RtlExitUserThread( ULONG status )
1117 {
1118     exit_thread( status );
1119 }
1120
1121 /***********************************************************************
1122  *           abort_thread
1123  */
1124 void abort_thread( int status )
1125 {
1126     terminate_thread( status );
1127 }
1128
1129 /**********************************************************************
1130  *              DbgBreakPoint   (NTDLL.@)
1131  */
1132 void WINAPI DbgBreakPoint(void)
1133 {
1134      kill(getpid(), SIGTRAP);
1135 }
1136
1137 /**********************************************************************
1138  *              DbgUserBreakPoint   (NTDLL.@)
1139  */
1140 void WINAPI DbgUserBreakPoint(void)
1141 {
1142      kill(getpid(), SIGTRAP);
1143 }
1144
1145 /**********************************************************************
1146  *           NtCurrentTeb   (NTDLL.@)
1147  */
1148 TEB * WINAPI NtCurrentTeb(void)
1149 {
1150     return pthread_getspecific( teb_key );
1151 }
1152
1153 #endif  /* __arm__ */