ntdll: Fix endianness of three fields in DVD_LAYER_DESCRIPTOR.
[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 /***********************************************************************
360  *           setup_exception_record
361  *
362  * Setup the exception record and context on the thread stack.
363  */
364 static EXCEPTION_RECORD *setup_exception_record( SIGCONTEXT *sigcontext, void *stack_ptr, raise_func func )
365 {
366     struct stack_layout
367     {
368         CONTEXT           context;
369         EXCEPTION_RECORD  rec;
370     } *stack = stack_ptr;
371     DWORD exception_code = 0;
372
373     stack--;  /* push the stack_layout structure */
374
375     stack->rec.ExceptionRecord  = NULL;
376     stack->rec.ExceptionCode    = exception_code;
377     stack->rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
378     stack->rec.ExceptionAddress = (LPVOID)PC_sig(sigcontext);
379     stack->rec.NumberParameters = 0;
380
381     save_context( &stack->context, sigcontext );
382
383     /* now modify the sigcontext to return to the raise function */
384     SP_sig(sigcontext) = (DWORD)stack;
385     PC_sig(sigcontext) = (DWORD)func;
386     REGn_sig(0, sigcontext) = (DWORD)&stack->rec;  /* first arg for raise_func */
387     REGn_sig(1, sigcontext) = (DWORD)&stack->context; /* second arg for raise_func */
388
389
390     return &stack->rec;
391 }
392
393 /**********************************************************************
394  *              raise_segv_exception
395  */
396 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
397 {
398     NTSTATUS status;
399
400     switch(rec->ExceptionCode)
401     {
402     case EXCEPTION_ACCESS_VIOLATION:
403         if (rec->NumberParameters == 2)
404         {
405             if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
406                                                              rec->ExceptionInformation[0] )))
407                 goto done;
408         }
409         break;
410     }
411     status = NtRaiseException( rec, context, TRUE );
412     if (status) raise_status( status, rec );
413 done:
414     set_cpu_context( context );
415 }
416
417 /**********************************************************************
418  *           call_stack_handlers
419  *
420  * Call the stack handlers chain.
421  */
422 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
423 {
424     EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
425     DWORD res;
426
427     frame = NtCurrentTeb()->Tib.ExceptionList;
428     nested_frame = NULL;
429     while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
430     {
431         /* Check frame address */
432         if (!is_valid_frame( frame ))
433         {
434             rec->ExceptionFlags |= EH_STACK_INVALID;
435             break;
436         }
437
438         /* Call handler */
439         TRACE( "calling handler at %p code=%x flags=%x\n",
440                frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
441         res = frame->Handler( rec, frame, context, &dispatch );
442         TRACE( "handler at %p returned %x\n", frame->Handler, res );
443
444         if (frame == nested_frame)
445         {
446             /* no longer nested */
447             nested_frame = NULL;
448             rec->ExceptionFlags &= ~EH_NESTED_CALL;
449         }
450
451         switch(res)
452         {
453         case ExceptionContinueExecution:
454             if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
455             return STATUS_NONCONTINUABLE_EXCEPTION;
456         case ExceptionContinueSearch:
457             break;
458         case ExceptionNestedException:
459             if (nested_frame < dispatch) nested_frame = dispatch;
460             rec->ExceptionFlags |= EH_NESTED_CALL;
461             break;
462         default:
463             return STATUS_INVALID_DISPOSITION;
464         }
465         frame = frame->Prev;
466     }
467     return STATUS_UNHANDLED_EXCEPTION;
468 }
469
470
471 /*******************************************************************
472  *              raise_exception
473  *
474  * Implementation of NtRaiseException.
475  */
476 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
477 {
478     NTSTATUS status;
479
480     if (first_chance)
481     {
482         DWORD c;
483
484         for (c = 0; c < rec->NumberParameters; c++)
485             TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
486         if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
487         {
488             if (rec->ExceptionInformation[1] >> 16)
489                 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
490                          rec->ExceptionAddress,
491                          (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
492             else
493                 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
494                          rec->ExceptionAddress,
495                          (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
496         }
497         else
498         {
499             TRACE(" Pc:%04x Sp:%04x Lr:%04x Cpsr:%04x r0:%04x r1:%04x r2:%04x r3:%04x\n",
500                   context->Pc, context->Sp, context->Lr, context->Cpsr,
501                   context->R0, context->R1, context->R2, context->R3);
502             TRACE(" r4:%04x r5:%04x  r6:%04x  r7:%04x r8:%04x r9:%04x r10:%04x Fp:%04x Ip:%04x\n",
503                   context->R4, context->R5, context->R6, context->R7, context->R8,
504                   context->R9, context->R10, context->Fp, context->Ip );
505         }
506
507         status = send_debug_event( rec, TRUE, context );
508         if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
509             return STATUS_SUCCESS;
510
511         if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
512             return STATUS_SUCCESS;
513
514         if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
515             return status;
516     }
517
518     /* last chance exception */
519
520     status = send_debug_event( rec, FALSE, context );
521     if (status != DBG_CONTINUE)
522     {
523         if (rec->ExceptionFlags & EH_STACK_INVALID)
524             ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
525         else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
526             ERR("Process attempted to continue execution after noncontinuable exception.\n");
527         else
528             ERR("Unhandled exception code %x flags %x addr %p\n",
529                 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
530         NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
531     }
532     return STATUS_SUCCESS;
533 }
534
535
536 /**********************************************************************
537  *              segv_handler
538  *
539  * Handler for SIGSEGV and related errors.
540  */
541 static void segv_handler( int signal, siginfo_t *info, void *ucontext )
542 {
543     EXCEPTION_RECORD *rec;
544     SIGCONTEXT *context = ucontext;
545     void *stack = (void *) (SP_sig(context) & ~3);
546
547     /* check for page fault inside the thread stack */
548     if (TRAP_sig(context) == TRAP_ARM_PAGEFLT &&
549         (char *)info->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
550         (char *)info->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
551         virtual_handle_stack_fault( info->si_addr ))
552     {
553         /* check if this was the last guard page */
554         if ((char *)info->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
555         {
556             rec = setup_exception_record( context, stack, raise_segv_exception );
557             rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
558         }
559         return;
560     }
561
562     rec = setup_exception_record( context, stack, raise_segv_exception );
563     if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
564
565     switch(TRAP_sig(context))
566     {
567     case TRAP_ARM_PRIVINFLT:   /* Invalid opcode exception */
568         rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
569         break;
570     case TRAP_ARM_PAGEFLT:  /* Page fault */
571         rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
572         rec->NumberParameters = 2;
573         rec->ExceptionInformation[0] = (ERROR_sig(context) & 0x800) != 0;
574         rec->ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
575         break;
576     case TRAP_ARM_ALIGNFLT:  /* Alignment check exception */
577         rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
578         break;
579     default:
580         ERR("Got unexpected trap %ld\n", TRAP_sig(context));
581         rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
582         break;
583     }
584 }
585
586 /**********************************************************************
587  *              trap_handler
588  *
589  * Handler for SIGTRAP.
590  */
591 static void trap_handler( int signal, siginfo_t *info, void *ucontext )
592 {
593     EXCEPTION_RECORD rec;
594     CONTEXT context;
595     NTSTATUS status;
596
597     switch ( info->si_code )
598     {
599     case TRAP_TRACE:
600         rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
601         break;
602     case TRAP_BRKPT:
603     default:
604         rec.ExceptionCode = EXCEPTION_BREAKPOINT;
605         break;
606     }
607
608     save_context( &context, ucontext );
609     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
610     rec.ExceptionRecord  = NULL;
611     rec.ExceptionAddress = (LPVOID)context.Pc;
612     rec.NumberParameters = 0;
613     status = raise_exception( &rec, &context, TRUE );
614     if (status) raise_status( status, &rec );
615     restore_context( &context, ucontext );
616 }
617
618 /**********************************************************************
619  *              fpe_handler
620  *
621  * Handler for SIGFPE.
622  */
623 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
624 {
625     EXCEPTION_RECORD rec;
626     CONTEXT context;
627     NTSTATUS status;
628
629     save_fpu( &context, sigcontext );
630     save_context( &context, sigcontext );
631
632     switch (siginfo->si_code & 0xffff )
633     {
634 #ifdef FPE_FLTSUB
635     case FPE_FLTSUB:
636         rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
637         break;
638 #endif
639 #ifdef FPE_INTDIV
640     case FPE_INTDIV:
641         rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
642         break;
643 #endif
644 #ifdef FPE_INTOVF
645     case FPE_INTOVF:
646         rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
647         break;
648 #endif
649 #ifdef FPE_FLTDIV
650     case FPE_FLTDIV:
651         rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
652         break;
653 #endif
654 #ifdef FPE_FLTOVF
655     case FPE_FLTOVF:
656         rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
657         break;
658 #endif
659 #ifdef FPE_FLTUND
660     case FPE_FLTUND:
661         rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
662         break;
663 #endif
664 #ifdef FPE_FLTRES
665     case FPE_FLTRES:
666         rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
667         break;
668 #endif
669 #ifdef FPE_FLTINV
670     case FPE_FLTINV:
671 #endif
672     default:
673         rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
674         break;
675     }
676     rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
677     rec.ExceptionRecord  = NULL;
678     rec.ExceptionAddress = (LPVOID)context.Pc;
679     rec.NumberParameters = 0;
680     status = raise_exception( &rec, &context, TRUE );
681     if (status) raise_status( status, &rec );
682
683     restore_context( &context, sigcontext );
684     restore_fpu( &context, sigcontext );
685 }
686
687 /**********************************************************************
688  *              int_handler
689  *
690  * Handler for SIGINT.
691  */
692 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
693 {
694     if (!dispatch_signal(SIGINT))
695     {
696         EXCEPTION_RECORD rec;
697         CONTEXT context;
698         NTSTATUS status;
699
700         save_context( &context, sigcontext );
701         rec.ExceptionCode    = CONTROL_C_EXIT;
702         rec.ExceptionFlags   = EXCEPTION_CONTINUABLE;
703         rec.ExceptionRecord  = NULL;
704         rec.ExceptionAddress = (LPVOID)context.Pc;
705         rec.NumberParameters = 0;
706         status = raise_exception( &rec, &context, TRUE );
707         if (status) raise_status( status, &rec );
708         restore_context( &context, sigcontext );
709     }
710 }
711
712
713 /**********************************************************************
714  *              abrt_handler
715  *
716  * Handler for SIGABRT.
717  */
718 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
719 {
720     EXCEPTION_RECORD rec;
721     CONTEXT context;
722     NTSTATUS status;
723
724     save_context( &context, sigcontext );
725     rec.ExceptionCode    = EXCEPTION_WINE_ASSERTION;
726     rec.ExceptionFlags   = EH_NONCONTINUABLE;
727     rec.ExceptionRecord  = NULL;
728     rec.ExceptionAddress = (LPVOID)context.Pc;
729     rec.NumberParameters = 0;
730     status = raise_exception( &rec, &context, TRUE );
731     if (status) raise_status( status, &rec );
732     restore_context( &context, sigcontext );
733 }
734
735
736 /**********************************************************************
737  *              quit_handler
738  *
739  * Handler for SIGQUIT.
740  */
741 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
742 {
743     abort_thread(0);
744 }
745
746
747 /**********************************************************************
748  *              usr1_handler
749  *
750  * Handler for SIGUSR1, used to signal a thread that it got suspended.
751  */
752 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
753 {
754     CONTEXT context;
755
756     save_context( &context, sigcontext );
757     wait_suspend( &context );
758     restore_context( &context, sigcontext );
759 }
760
761
762 /***********************************************************************
763  *           __wine_set_signal_handler   (NTDLL.@)
764  */
765 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
766 {
767     if (sig > sizeof(handlers) / sizeof(handlers[0])) return -1;
768     if (handlers[sig] != NULL) return -2;
769     handlers[sig] = wsh;
770     return 0;
771 }
772
773
774 /**********************************************************************
775  *             signal_alloc_thread
776  */
777 NTSTATUS signal_alloc_thread( TEB **teb )
778 {
779     static size_t sigstack_zero_bits;
780     SIZE_T size;
781     NTSTATUS status;
782
783     if (!sigstack_zero_bits)
784     {
785         size_t min_size = getpagesize();  /* this is just for the TEB, we don't use a signal stack yet */
786         /* find the first power of two not smaller than min_size */
787         while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
788         assert( sizeof(TEB) <= min_size );
789     }
790
791     size = 1 << sigstack_zero_bits;
792     *teb = NULL;
793     if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
794                                             &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
795     {
796         (*teb)->Tib.Self = &(*teb)->Tib;
797         (*teb)->Tib.ExceptionList = (void *)~0UL;
798     }
799     return status;
800 }
801
802
803 /**********************************************************************
804  *             signal_free_thread
805  */
806 void signal_free_thread( TEB *teb )
807 {
808     SIZE_T size;
809
810     if (teb->DeallocationStack)
811     {
812         size = 0;
813         NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
814     }
815     size = 0;
816     NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
817 }
818
819
820 /**********************************************************************
821  *              signal_init_thread
822  */
823 void signal_init_thread( TEB *teb )
824 {
825     static int init_done;
826
827     if (!init_done)
828     {
829         pthread_key_create( &teb_key, NULL );
830         init_done = 1;
831     }
832     pthread_setspecific( teb_key, teb );
833 }
834
835
836 /**********************************************************************
837  *              signal_init_process
838  */
839 void signal_init_process(void)
840 {
841     struct sigaction sig_act;
842
843     sig_act.sa_mask = server_block_set;
844     sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
845
846     sig_act.sa_sigaction = int_handler;
847     if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
848     sig_act.sa_sigaction = fpe_handler;
849     if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
850     sig_act.sa_sigaction = abrt_handler;
851     if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
852     sig_act.sa_sigaction = quit_handler;
853     if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
854     sig_act.sa_sigaction = usr1_handler;
855     if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
856
857     sig_act.sa_sigaction = segv_handler;
858     if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
859     if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
860 #ifdef SIGBUS
861     if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
862 #endif
863
864 #ifdef SIGTRAP
865     sig_act.sa_sigaction = trap_handler;
866     if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
867 #endif
868     return;
869
870  error:
871     perror("sigaction");
872     exit(1);
873 }
874
875
876 /**********************************************************************
877  *              __wine_enter_vm86   (NTDLL.@)
878  */
879 void __wine_enter_vm86( CONTEXT *context )
880 {
881     MESSAGE("vm86 mode not supported on this platform\n");
882 }
883
884 /***********************************************************************
885  *            RtlUnwind  (NTDLL.@)
886  */
887 void WINAPI RtlUnwind( PVOID pEndFrame, PVOID targetIp, PEXCEPTION_RECORD pRecord, PVOID retval )
888 {
889     FIXME( "Not implemented on ARM\n" );
890 }
891
892 /*******************************************************************
893  *              NtRaiseException (NTDLL.@)
894  */
895 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
896 {
897     NTSTATUS status = raise_exception( rec, context, first_chance );
898     if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
899     return status;
900 }
901
902 /***********************************************************************
903  *              RtlRaiseException (NTDLL.@)
904  */
905 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
906 {
907     CONTEXT context;
908     NTSTATUS status;
909
910     RtlCaptureContext( &context );
911     rec->ExceptionAddress = (LPVOID)context.Pc;
912     status = raise_exception( rec, &context, TRUE );
913     if (status) raise_status( status, rec );
914 }
915
916 /*************************************************************************
917  *             RtlCaptureStackBackTrace (NTDLL.@)
918  */
919 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
920 {
921     FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
922     return 0;
923 }
924
925 /***********************************************************************
926  *           call_thread_entry_point
927  */
928 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
929 {
930     __TRY
931     {
932         exit_thread( entry( arg ));
933     }
934     __EXCEPT(unhandled_exception_filter)
935     {
936         NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
937     }
938     __ENDTRY
939     abort();  /* should not be reached */
940 }
941
942 /***********************************************************************
943  *           RtlExitUserThread  (NTDLL.@)
944  */
945 void WINAPI RtlExitUserThread( ULONG status )
946 {
947     exit_thread( status );
948 }
949
950 /***********************************************************************
951  *           abort_thread
952  */
953 void abort_thread( int status )
954 {
955     terminate_thread( status );
956 }
957
958 /**********************************************************************
959  *              DbgBreakPoint   (NTDLL.@)
960  */
961 void WINAPI DbgBreakPoint(void)
962 {
963      kill(getpid(), SIGTRAP);
964 }
965
966 /**********************************************************************
967  *              DbgUserBreakPoint   (NTDLL.@)
968  */
969 void WINAPI DbgUserBreakPoint(void)
970 {
971      kill(getpid(), SIGTRAP);
972 }
973
974 /**********************************************************************
975  *           NtCurrentTeb   (NTDLL.@)
976  */
977 TEB * WINAPI NtCurrentTeb(void)
978 {
979     return pthread_getspecific( teb_key );
980 }
981
982 #endif  /* __arm__ */