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