hid: Stub HidP_TranslateUsagesToI8042ScanCodes.
[wine.git] / dlls / ntdll / signal_arm.c
blobcb5f67db75bf066e85b9acfbe382fecab71d9807
1 /*
2 * ARM signal handling routines
4 * Copyright 2002 Marcus Meissner, SuSE Linux AG
5 * Copyright 2010-2013, 2015 André Hentschel
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #ifdef __arm__
24 #include "config.h"
25 #include "wine/port.h"
27 #include <assert.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
37 #endif
38 #ifdef HAVE_SYSCALL_H
39 # include <syscall.h>
40 #else
41 # ifdef HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
43 # endif
44 #endif
45 #ifdef HAVE_SYS_SIGNAL_H
46 # include <sys/signal.h>
47 #endif
48 #ifdef HAVE_SYS_UCONTEXT_H
49 # include <sys/ucontext.h>
50 #endif
52 #define NONAMELESSUNION
53 #define NONAMELESSSTRUCT
54 #include "ntstatus.h"
55 #define WIN32_NO_STATUS
56 #include "windef.h"
57 #include "winternl.h"
58 #include "wine/library.h"
59 #include "wine/exception.h"
60 #include "ntdll_misc.h"
61 #include "wine/debug.h"
62 #include "winnt.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(seh);
66 static pthread_key_t teb_key;
68 /***********************************************************************
69 * signal context platform-specific definitions
71 #ifdef linux
73 #if defined(__ANDROID__) && !defined(HAVE_SYS_UCONTEXT_H)
74 typedef struct ucontext
76 unsigned long uc_flags;
77 struct ucontext *uc_link;
78 stack_t uc_stack;
79 struct sigcontext uc_mcontext;
80 sigset_t uc_sigmask;
81 unsigned long uc_regspace[128] __attribute__((__aligned__(8)));
82 } ucontext_t;
83 #endif
85 /* All Registers access - only for local access */
86 # define REG_sig(reg_name, context) ((context)->uc_mcontext.reg_name)
87 # define REGn_sig(reg_num, context) ((context)->uc_mcontext.arm_r##reg_num)
89 /* Special Registers access */
90 # define SP_sig(context) REG_sig(arm_sp, context) /* Stack pointer */
91 # define LR_sig(context) REG_sig(arm_lr, context) /* Link register */
92 # define PC_sig(context) REG_sig(arm_pc, context) /* Program counter */
93 # define CPSR_sig(context) REG_sig(arm_cpsr, context) /* Current State Register */
94 # define IP_sig(context) REG_sig(arm_ip, context) /* Intra-Procedure-call scratch register */
95 # define FP_sig(context) REG_sig(arm_fp, context) /* Frame pointer */
97 /* Exceptions */
98 # define ERROR_sig(context) REG_sig(error_code, context)
99 # define TRAP_sig(context) REG_sig(trap_no, context)
101 #elif defined(__FreeBSD__)
103 /* All Registers access - only for local access */
104 # define REGn_sig(reg_num, context) ((context)->uc_mcontext.__gregs[reg_num])
106 /* Special Registers access */
107 # define SP_sig(context) REGn_sig(_REG_SP, context) /* Stack pointer */
108 # define LR_sig(context) REGn_sig(_REG_LR, context) /* Link register */
109 # define PC_sig(context) REGn_sig(_REG_PC, context) /* Program counter */
110 # define CPSR_sig(context) REGn_sig(_REG_CPSR, context) /* Current State Register */
111 # define IP_sig(context) REGn_sig(_REG_R12, context) /* Intra-Procedure-call scratch register */
112 # define FP_sig(context) REGn_sig(_REG_FP, context) /* Frame pointer */
114 #endif /* linux */
116 enum arm_trap_code
118 TRAP_ARM_UNKNOWN = -1, /* Unknown fault (TRAP_sig not defined) */
119 TRAP_ARM_PRIVINFLT = 6, /* Invalid opcode exception */
120 TRAP_ARM_PAGEFLT = 14, /* Page fault */
121 TRAP_ARM_ALIGNFLT = 17, /* Alignment check exception */
124 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
125 typedef int (*wine_signal_handler)(unsigned int sig);
127 static wine_signal_handler handlers[256];
130 struct UNWIND_INFO
132 WORD function_length;
133 WORD unknown1 : 7;
134 WORD count : 5;
135 WORD unknown2 : 4;
139 /***********************************************************************
140 * get_trap_code
142 * Get the trap code for a signal.
144 static inline enum arm_trap_code get_trap_code( const ucontext_t *sigcontext )
146 #ifdef TRAP_sig
147 return TRAP_sig(sigcontext);
148 #else
149 return TRAP_ARM_UNKNOWN; /* unknown trap code */
150 #endif
153 /***********************************************************************
154 * get_error_code
156 * Get the error code for a signal.
158 static inline WORD get_error_code( const ucontext_t *sigcontext )
160 #ifdef ERROR_sig
161 return ERROR_sig(sigcontext);
162 #else
163 return 0;
164 #endif
167 /***********************************************************************
168 * dispatch_signal
170 static inline int dispatch_signal(unsigned int sig)
172 if (handlers[sig] == NULL) return 0;
173 return handlers[sig](sig);
176 /*******************************************************************
177 * is_valid_frame
179 static inline BOOL is_valid_frame( void *frame )
181 if ((ULONG_PTR)frame & 3) return FALSE;
182 return (frame >= NtCurrentTeb()->Tib.StackLimit &&
183 (void **)frame < (void **)NtCurrentTeb()->Tib.StackBase - 1);
186 /***********************************************************************
187 * save_context
189 * Set the register values from a sigcontext.
191 static void save_context( CONTEXT *context, const ucontext_t *sigcontext )
193 #define C(x) context->R##x = REGn_sig(x,sigcontext)
194 /* Save normal registers */
195 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
196 #undef C
198 context->ContextFlags = CONTEXT_FULL;
199 context->Sp = SP_sig(sigcontext); /* Stack pointer */
200 context->Lr = LR_sig(sigcontext); /* Link register */
201 context->Pc = PC_sig(sigcontext); /* Program Counter */
202 context->Cpsr = CPSR_sig(sigcontext); /* Current State Register */
203 context->Ip = IP_sig(sigcontext); /* Intra-Procedure-call scratch register */
204 context->Fp = FP_sig(sigcontext); /* Frame pointer */
208 /***********************************************************************
209 * restore_context
211 * Build a sigcontext from the register values.
213 static void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
215 #define C(x) REGn_sig(x,sigcontext) = context->R##x
216 /* Restore normal registers */
217 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9); C(10);
218 #undef C
220 SP_sig(sigcontext) = context->Sp; /* Stack pointer */
221 LR_sig(sigcontext) = context->Lr ; /* Link register */
222 PC_sig(sigcontext) = context->Pc; /* Program Counter */
223 CPSR_sig(sigcontext) = context->Cpsr; /* Current State Register */
224 IP_sig(sigcontext) = context->Ip; /* Intra-Procedure-call scratch register */
225 FP_sig(sigcontext) = context->Fp; /* Frame pointer */
229 /***********************************************************************
230 * save_fpu
232 * Set the FPU context from a sigcontext.
234 static inline void save_fpu( CONTEXT *context, const ucontext_t *sigcontext )
236 FIXME("not implemented\n");
240 /***********************************************************************
241 * restore_fpu
243 * Restore the FPU context to a sigcontext.
245 static inline void restore_fpu( CONTEXT *context, const ucontext_t *sigcontext )
247 FIXME("not implemented\n");
250 /**************************************************************************
251 * __chkstk (NTDLL.@)
253 * Incoming r4 contains words to allocate, converting to bytes then return
255 __ASM_GLOBAL_FUNC( __chkstk, "lsl r4, r4, #2\n\t"
256 "bx lr" )
258 /***********************************************************************
259 * RtlCaptureContext (NTDLL.@)
261 /* FIXME: Use the Stack instead of the actual register values */
262 __ASM_STDCALL_FUNC( RtlCaptureContext, 4,
263 ".arm\n\t"
264 "stmfd SP!, {r1}\n\t"
265 "mov r1, #0x0200000\n\t"/* CONTEXT_ARM */
266 "add r1, r1, #0x3\n\t" /* CONTEXT_FULL */
267 "str r1, [r0]\n\t" /* context->ContextFlags */
268 "ldmfd SP!, {r1}\n\t"
269 "str r0, [r0, #0x4]\n\t" /* context->R0 */
270 "str r1, [r0, #0x8]\n\t" /* context->R1 */
271 "str r2, [r0, #0xc]\n\t" /* context->R2 */
272 "str r3, [r0, #0x10]\n\t" /* context->R3 */
273 "str r4, [r0, #0x14]\n\t" /* context->R4 */
274 "str r5, [r0, #0x18]\n\t" /* context->R5 */
275 "str r6, [r0, #0x1c]\n\t" /* context->R6 */
276 "str r7, [r0, #0x20]\n\t" /* context->R7 */
277 "str r8, [r0, #0x24]\n\t" /* context->R8 */
278 "str r9, [r0, #0x28]\n\t" /* context->R9 */
279 "str r10, [r0, #0x2c]\n\t" /* context->R10 */
280 "str r11, [r0, #0x30]\n\t" /* context->Fp */
281 "str IP, [r0, #0x34]\n\t" /* context->Ip */
282 "str SP, [r0, #0x38]\n\t" /* context->Sp */
283 "str LR, [r0, #0x3c]\n\t" /* context->Lr */
284 "str PC, [r0, #0x40]\n\t" /* context->Pc */
285 "mrs r1, CPSR\n\t"
286 "str r1, [r0, #0x44]\n\t" /* context->Cpsr */
287 "bx lr"
291 /***********************************************************************
292 * set_cpu_context
294 * Set the new CPU context.
296 /* FIXME: What about the CPSR? */
297 __ASM_GLOBAL_FUNC( set_cpu_context,
298 "mov IP, r0\n\t"
299 "ldr r0, [IP, #0x4]\n\t" /* context->R0 */
300 "ldr r1, [IP, #0x8]\n\t" /* context->R1 */
301 "ldr r2, [IP, #0xc]\n\t" /* context->R2 */
302 "ldr r3, [IP, #0x10]\n\t" /* context->R3 */
303 "ldr r4, [IP, #0x14]\n\t" /* context->R4 */
304 "ldr r5, [IP, #0x18]\n\t" /* context->R5 */
305 "ldr r6, [IP, #0x1c]\n\t" /* context->R6 */
306 "ldr r7, [IP, #0x20]\n\t" /* context->R7 */
307 "ldr r8, [IP, #0x24]\n\t" /* context->R8 */
308 "ldr r9, [IP, #0x28]\n\t" /* context->R9 */
309 "ldr r10, [IP, #0x2c]\n\t" /* context->R10 */
310 "ldr r11, [IP, #0x30]\n\t" /* context->Fp */
311 "ldr SP, [IP, #0x38]\n\t" /* context->Sp */
312 "ldr LR, [IP, #0x3c]\n\t" /* context->Lr */
313 "ldr PC, [IP, #0x40]\n\t" /* context->Pc */
317 /***********************************************************************
318 * copy_context
320 * Copy a register context according to the flags.
322 void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
324 flags &= ~CONTEXT_ARM; /* get rid of CPU id */
325 if (flags & CONTEXT_CONTROL)
327 to->Sp = from->Sp;
328 to->Lr = from->Lr;
329 to->Pc = from->Pc;
330 to->Cpsr = from->Cpsr;
332 if (flags & CONTEXT_INTEGER)
334 to->R0 = from->R0;
335 to->R1 = from->R1;
336 to->R2 = from->R2;
337 to->R3 = from->R3;
338 to->R4 = from->R4;
339 to->R5 = from->R5;
340 to->R6 = from->R6;
341 to->R7 = from->R7;
342 to->R8 = from->R8;
343 to->R9 = from->R9;
344 to->R10 = from->R10;
345 to->Ip = from->Ip;
346 to->Fp = from->Fp;
351 /***********************************************************************
352 * context_to_server
354 * Convert a register context to the server format.
356 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
358 DWORD flags = from->ContextFlags & ~CONTEXT_ARM; /* get rid of CPU id */
360 memset( to, 0, sizeof(*to) );
361 to->cpu = CPU_ARM;
363 if (flags & CONTEXT_CONTROL)
365 to->flags |= SERVER_CTX_CONTROL;
366 to->ctl.arm_regs.sp = from->Sp;
367 to->ctl.arm_regs.lr = from->Lr;
368 to->ctl.arm_regs.pc = from->Pc;
369 to->ctl.arm_regs.cpsr = from->Cpsr;
371 if (flags & CONTEXT_INTEGER)
373 to->flags |= SERVER_CTX_INTEGER;
374 to->integer.arm_regs.r[0] = from->R0;
375 to->integer.arm_regs.r[1] = from->R1;
376 to->integer.arm_regs.r[2] = from->R2;
377 to->integer.arm_regs.r[3] = from->R3;
378 to->integer.arm_regs.r[4] = from->R4;
379 to->integer.arm_regs.r[5] = from->R5;
380 to->integer.arm_regs.r[6] = from->R6;
381 to->integer.arm_regs.r[7] = from->R7;
382 to->integer.arm_regs.r[8] = from->R8;
383 to->integer.arm_regs.r[9] = from->R9;
384 to->integer.arm_regs.r[10] = from->R10;
385 to->integer.arm_regs.r[11] = from->Fp;
386 to->integer.arm_regs.r[12] = from->Ip;
388 return STATUS_SUCCESS;
392 /***********************************************************************
393 * context_from_server
395 * Convert a register context from the server format.
397 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
399 if (from->cpu != CPU_ARM) return STATUS_INVALID_PARAMETER;
401 to->ContextFlags = CONTEXT_ARM;
402 if (from->flags & SERVER_CTX_CONTROL)
404 to->ContextFlags |= CONTEXT_CONTROL;
405 to->Sp = from->ctl.arm_regs.sp;
406 to->Lr = from->ctl.arm_regs.lr;
407 to->Pc = from->ctl.arm_regs.pc;
408 to->Cpsr = from->ctl.arm_regs.cpsr;
410 if (from->flags & SERVER_CTX_INTEGER)
412 to->ContextFlags |= CONTEXT_INTEGER;
413 to->R0 = from->integer.arm_regs.r[0];
414 to->R1 = from->integer.arm_regs.r[1];
415 to->R2 = from->integer.arm_regs.r[2];
416 to->R3 = from->integer.arm_regs.r[3];
417 to->R4 = from->integer.arm_regs.r[4];
418 to->R5 = from->integer.arm_regs.r[5];
419 to->R6 = from->integer.arm_regs.r[6];
420 to->R7 = from->integer.arm_regs.r[7];
421 to->R8 = from->integer.arm_regs.r[8];
422 to->R9 = from->integer.arm_regs.r[9];
423 to->R10 = from->integer.arm_regs.r[10];
424 to->Fp = from->integer.arm_regs.r[11];
425 to->Ip = from->integer.arm_regs.r[12];
427 return STATUS_SUCCESS;
430 extern void raise_func_trampoline_thumb( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
431 __ASM_GLOBAL_FUNC( raise_func_trampoline_thumb,
432 ".thumb\n\t"
433 "blx r2\n\t"
434 "bkpt")
436 extern void raise_func_trampoline_arm( EXCEPTION_RECORD *rec, CONTEXT *context, raise_func func );
437 __ASM_GLOBAL_FUNC( raise_func_trampoline_arm,
438 ".arm\n\t"
439 "blx r2\n\t"
440 "bkpt")
442 /***********************************************************************
443 * setup_exception_record
445 * Setup the exception record and context on the thread stack.
447 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
449 struct stack_layout
451 CONTEXT context;
452 EXCEPTION_RECORD rec;
453 } *stack;
454 DWORD exception_code = 0;
456 stack = (struct stack_layout *)(SP_sig(sigcontext) & ~3);
457 stack--; /* push the stack_layout structure */
459 stack->rec.ExceptionRecord = NULL;
460 stack->rec.ExceptionCode = exception_code;
461 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
462 stack->rec.ExceptionAddress = (LPVOID)PC_sig(sigcontext);
463 stack->rec.NumberParameters = 0;
465 save_context( &stack->context, sigcontext );
467 /* now modify the sigcontext to return to the raise function */
468 SP_sig(sigcontext) = (DWORD)stack;
469 if (CPSR_sig(sigcontext) & 0x20)
470 PC_sig(sigcontext) = (DWORD)raise_func_trampoline_thumb;
471 else
472 PC_sig(sigcontext) = (DWORD)raise_func_trampoline_arm;
473 REGn_sig(0, sigcontext) = (DWORD)&stack->rec; /* first arg for raise_func */
474 REGn_sig(1, sigcontext) = (DWORD)&stack->context; /* second arg for raise_func */
475 REGn_sig(2, sigcontext) = (DWORD)func; /* the raise_func as third arg for the trampoline */
478 return &stack->rec;
481 /**********************************************************************
482 * raise_segv_exception
484 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
486 NTSTATUS status;
488 switch(rec->ExceptionCode)
490 case EXCEPTION_ACCESS_VIOLATION:
491 if (rec->NumberParameters == 2)
493 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
494 rec->ExceptionInformation[0], FALSE )))
495 goto done;
497 break;
499 status = NtRaiseException( rec, context, TRUE );
500 if (status) raise_status( status, rec );
501 done:
502 set_cpu_context( context );
505 /**********************************************************************
506 * call_stack_handlers
508 * Call the stack handlers chain.
510 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
512 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
513 DWORD res;
515 frame = NtCurrentTeb()->Tib.ExceptionList;
516 nested_frame = NULL;
517 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
519 /* Check frame address */
520 if (!is_valid_frame( frame ))
522 rec->ExceptionFlags |= EH_STACK_INVALID;
523 break;
526 /* Call handler */
527 TRACE( "calling handler at %p code=%x flags=%x\n",
528 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
529 res = frame->Handler( rec, frame, context, &dispatch );
530 TRACE( "handler at %p returned %x\n", frame->Handler, res );
532 if (frame == nested_frame)
534 /* no longer nested */
535 nested_frame = NULL;
536 rec->ExceptionFlags &= ~EH_NESTED_CALL;
539 switch(res)
541 case ExceptionContinueExecution:
542 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
543 return STATUS_NONCONTINUABLE_EXCEPTION;
544 case ExceptionContinueSearch:
545 break;
546 case ExceptionNestedException:
547 if (nested_frame < dispatch) nested_frame = dispatch;
548 rec->ExceptionFlags |= EH_NESTED_CALL;
549 break;
550 default:
551 return STATUS_INVALID_DISPOSITION;
553 frame = frame->Prev;
555 return STATUS_UNHANDLED_EXCEPTION;
559 /*******************************************************************
560 * raise_exception
562 * Implementation of NtRaiseException.
564 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
566 NTSTATUS status;
568 if (first_chance)
570 DWORD c;
572 TRACE( "code=%x flags=%x addr=%p pc=%08x tid=%04x\n",
573 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
574 context->Pc, GetCurrentThreadId() );
575 for (c = 0; c < rec->NumberParameters; c++)
576 TRACE( " info[%d]=%08lx\n", c, rec->ExceptionInformation[c] );
577 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
579 if (rec->ExceptionInformation[1] >> 16)
580 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
581 rec->ExceptionAddress,
582 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
583 else
584 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
585 rec->ExceptionAddress,
586 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
588 else
590 TRACE( " r0=%08x r1=%08x r2=%08x r3=%08x r4=%08x r5=%08x\n",
591 context->R0, context->R1, context->R2, context->R3, context->R4, context->R5 );
592 TRACE( " r6=%08x r7=%08x r8=%08x r9=%08x r10=%08x fp=%08x\n",
593 context->R6, context->R7, context->R8, context->R9, context->R10, context->Fp );
594 TRACE( " ip=%08x sp=%08x lr=%08x pc=%08x cpsr=%08x\n",
595 context->Ip, context->Sp, context->Lr, context->Pc, context->Cpsr );
598 status = send_debug_event( rec, TRUE, context );
599 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
600 return STATUS_SUCCESS;
602 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
603 return STATUS_SUCCESS;
605 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
606 return status;
609 /* last chance exception */
611 status = send_debug_event( rec, FALSE, context );
612 if (status != DBG_CONTINUE)
614 if (rec->ExceptionFlags & EH_STACK_INVALID)
615 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
616 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
617 ERR("Process attempted to continue execution after noncontinuable exception.\n");
618 else
619 ERR("Unhandled exception code %x flags %x addr %p\n",
620 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
621 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
623 return STATUS_SUCCESS;
627 /**********************************************************************
628 * segv_handler
630 * Handler for SIGSEGV and related errors.
632 static void segv_handler( int signal, siginfo_t *info, void *ucontext )
634 EXCEPTION_RECORD *rec;
635 ucontext_t *context = ucontext;
637 /* check for page fault inside the thread stack */
638 if (get_trap_code(context) == TRAP_ARM_PAGEFLT &&
639 (char *)info->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
640 (char *)info->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
641 virtual_handle_stack_fault( info->si_addr ))
643 /* check if this was the last guard page */
644 if ((char *)info->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
646 rec = setup_exception( context, raise_segv_exception );
647 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
649 return;
652 rec = setup_exception( context, raise_segv_exception );
653 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
655 switch(get_trap_code(context))
657 case TRAP_ARM_PRIVINFLT: /* Invalid opcode exception */
658 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
659 break;
660 case TRAP_ARM_PAGEFLT: /* Page fault */
661 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
662 rec->NumberParameters = 2;
663 rec->ExceptionInformation[0] = (get_error_code(context) & 0x800) != 0;
664 rec->ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
665 break;
666 case TRAP_ARM_ALIGNFLT: /* Alignment check exception */
667 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
668 break;
669 case TRAP_ARM_UNKNOWN: /* Unknown fault code */
670 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
671 rec->NumberParameters = 2;
672 rec->ExceptionInformation[0] = 0;
673 rec->ExceptionInformation[1] = 0xffffffff;
674 break;
675 default:
676 ERR("Got unexpected trap %d\n", get_trap_code(context));
677 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
678 break;
682 /**********************************************************************
683 * trap_handler
685 * Handler for SIGTRAP.
687 static void trap_handler( int signal, siginfo_t *info, void *ucontext )
689 EXCEPTION_RECORD rec;
690 CONTEXT context;
691 NTSTATUS status;
693 switch ( info->si_code )
695 case TRAP_TRACE:
696 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
697 break;
698 case TRAP_BRKPT:
699 default:
700 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
701 break;
704 save_context( &context, ucontext );
705 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
706 rec.ExceptionRecord = NULL;
707 rec.ExceptionAddress = (LPVOID)context.Pc;
708 rec.NumberParameters = 0;
709 status = raise_exception( &rec, &context, TRUE );
710 if (status) raise_status( status, &rec );
711 restore_context( &context, ucontext );
714 /**********************************************************************
715 * fpe_handler
717 * Handler for SIGFPE.
719 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
721 EXCEPTION_RECORD rec;
722 CONTEXT context;
723 NTSTATUS status;
725 save_fpu( &context, sigcontext );
726 save_context( &context, sigcontext );
728 switch (siginfo->si_code & 0xffff )
730 #ifdef FPE_FLTSUB
731 case FPE_FLTSUB:
732 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
733 break;
734 #endif
735 #ifdef FPE_INTDIV
736 case FPE_INTDIV:
737 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
738 break;
739 #endif
740 #ifdef FPE_INTOVF
741 case FPE_INTOVF:
742 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
743 break;
744 #endif
745 #ifdef FPE_FLTDIV
746 case FPE_FLTDIV:
747 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
748 break;
749 #endif
750 #ifdef FPE_FLTOVF
751 case FPE_FLTOVF:
752 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
753 break;
754 #endif
755 #ifdef FPE_FLTUND
756 case FPE_FLTUND:
757 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
758 break;
759 #endif
760 #ifdef FPE_FLTRES
761 case FPE_FLTRES:
762 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
763 break;
764 #endif
765 #ifdef FPE_FLTINV
766 case FPE_FLTINV:
767 #endif
768 default:
769 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
770 break;
772 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
773 rec.ExceptionRecord = NULL;
774 rec.ExceptionAddress = (LPVOID)context.Pc;
775 rec.NumberParameters = 0;
776 status = raise_exception( &rec, &context, TRUE );
777 if (status) raise_status( status, &rec );
779 restore_context( &context, sigcontext );
780 restore_fpu( &context, sigcontext );
783 /**********************************************************************
784 * int_handler
786 * Handler for SIGINT.
788 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
790 if (!dispatch_signal(SIGINT))
792 EXCEPTION_RECORD rec;
793 CONTEXT context;
794 NTSTATUS status;
796 save_context( &context, sigcontext );
797 rec.ExceptionCode = CONTROL_C_EXIT;
798 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
799 rec.ExceptionRecord = NULL;
800 rec.ExceptionAddress = (LPVOID)context.Pc;
801 rec.NumberParameters = 0;
802 status = raise_exception( &rec, &context, TRUE );
803 if (status) raise_status( status, &rec );
804 restore_context( &context, sigcontext );
809 /**********************************************************************
810 * abrt_handler
812 * Handler for SIGABRT.
814 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
816 EXCEPTION_RECORD rec;
817 CONTEXT context;
818 NTSTATUS status;
820 save_context( &context, sigcontext );
821 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
822 rec.ExceptionFlags = EH_NONCONTINUABLE;
823 rec.ExceptionRecord = NULL;
824 rec.ExceptionAddress = (LPVOID)context.Pc;
825 rec.NumberParameters = 0;
826 status = raise_exception( &rec, &context, TRUE );
827 if (status) raise_status( status, &rec );
828 restore_context( &context, sigcontext );
832 /**********************************************************************
833 * quit_handler
835 * Handler for SIGQUIT.
837 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
839 abort_thread(0);
843 /**********************************************************************
844 * usr1_handler
846 * Handler for SIGUSR1, used to signal a thread that it got suspended.
848 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
850 CONTEXT context;
852 save_context( &context, sigcontext );
853 wait_suspend( &context );
854 restore_context( &context, sigcontext );
858 /***********************************************************************
859 * __wine_set_signal_handler (NTDLL.@)
861 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
863 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
864 if (handlers[sig] != NULL) return -2;
865 handlers[sig] = wsh;
866 return 0;
870 /**********************************************************************
871 * signal_alloc_thread
873 NTSTATUS signal_alloc_thread( TEB **teb )
875 static size_t sigstack_zero_bits;
876 SIZE_T size;
877 NTSTATUS status;
879 if (!sigstack_zero_bits)
881 size_t min_size = page_size;
882 /* find the first power of two not smaller than min_size */
883 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
884 assert( sizeof(TEB) <= min_size );
887 size = 1 << sigstack_zero_bits;
888 *teb = NULL;
889 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
890 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
892 (*teb)->Tib.Self = &(*teb)->Tib;
893 (*teb)->Tib.ExceptionList = (void *)~0UL;
895 return status;
899 /**********************************************************************
900 * signal_free_thread
902 void signal_free_thread( TEB *teb )
904 SIZE_T size;
906 if (teb->DeallocationStack)
908 size = 0;
909 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
911 size = 0;
912 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
916 /**********************************************************************
917 * signal_init_thread
919 void signal_init_thread( TEB *teb )
921 static BOOL init_done;
923 if (!init_done)
925 pthread_key_create( &teb_key, NULL );
926 init_done = TRUE;
929 #if defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_8A__)
930 /* Win32/ARM applications expect the TEB pointer to be in the TPIDRURW register. */
931 __asm__ __volatile__( "mcr p15, 0, %0, c13, c0, 2" : : "r" (teb) );
932 #endif
934 pthread_setspecific( teb_key, teb );
938 /**********************************************************************
939 * signal_init_process
941 void signal_init_process(void)
943 struct sigaction sig_act;
945 sig_act.sa_mask = server_block_set;
946 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
948 sig_act.sa_sigaction = int_handler;
949 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
950 sig_act.sa_sigaction = fpe_handler;
951 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
952 sig_act.sa_sigaction = abrt_handler;
953 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
954 sig_act.sa_sigaction = quit_handler;
955 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
956 sig_act.sa_sigaction = usr1_handler;
957 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
959 sig_act.sa_sigaction = segv_handler;
960 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
961 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
962 #ifdef SIGBUS
963 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
964 #endif
966 #ifdef SIGTRAP
967 sig_act.sa_sigaction = trap_handler;
968 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
969 #endif
970 return;
972 error:
973 perror("sigaction");
974 exit(1);
978 /**********************************************************************
979 * __wine_enter_vm86 (NTDLL.@)
981 void __wine_enter_vm86( CONTEXT *context )
983 MESSAGE("vm86 mode not supported on this platform\n");
987 /**********************************************************************
988 * RtlAddFunctionTable (NTDLL.@)
990 BOOLEAN CDECL RtlAddFunctionTable( RUNTIME_FUNCTION *table, DWORD count, DWORD addr )
992 FIXME( "%p %u %x: stub\n", table, count, addr );
993 return TRUE;
997 /**********************************************************************
998 * RtlDeleteFunctionTable (NTDLL.@)
1000 BOOLEAN CDECL RtlDeleteFunctionTable( RUNTIME_FUNCTION *table )
1002 FIXME( "%p: stub\n", table );
1003 return TRUE;
1006 /**********************************************************************
1007 * find_function_info
1009 static RUNTIME_FUNCTION *find_function_info( ULONG_PTR pc, HMODULE module,
1010 RUNTIME_FUNCTION *func, ULONG size )
1012 int min = 0;
1013 int max = size/sizeof(*func) - 1;
1015 while (min <= max)
1017 int pos = (min + max) / 2;
1018 DWORD begin = (func[pos].BeginAddress & ~1), end;
1019 if (func[pos].u.s.Flag)
1020 end = begin + func[pos].u.s.FunctionLength * 2;
1021 else
1023 struct UNWIND_INFO *info;
1024 info = (struct UNWIND_INFO *)((char *)module + func[pos].u.UnwindData);
1025 end = begin + info->function_length * 2;
1028 if ((char *)pc < (char *)module + begin) max = pos - 1;
1029 else if ((char *)pc >= (char *)module + end) min = pos + 1;
1030 else return func + pos;
1032 return NULL;
1035 /**********************************************************************
1036 * RtlLookupFunctionEntry (NTDLL.@)
1038 PRUNTIME_FUNCTION WINAPI RtlLookupFunctionEntry( ULONG_PTR pc, DWORD *base,
1039 UNWIND_HISTORY_TABLE *table )
1041 LDR_MODULE *module;
1042 RUNTIME_FUNCTION *func;
1043 ULONG size;
1045 /* FIXME: should use the history table to make things faster */
1047 if (LdrFindEntryForAddress( (void *)pc, &module ))
1049 WARN( "module not found for %lx\n", pc );
1050 return NULL;
1052 if (!(func = RtlImageDirectoryEntryToData( module->BaseAddress, TRUE,
1053 IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size )))
1055 WARN( "no exception table found in module %p pc %lx\n", module->BaseAddress, pc );
1056 return NULL;
1058 func = find_function_info( pc, module->BaseAddress, func, size );
1059 if (func) *base = (DWORD)module->BaseAddress;
1060 return func;
1063 /***********************************************************************
1064 * RtlUnwind (NTDLL.@)
1066 void WINAPI RtlUnwind( void *endframe, void *target_ip, EXCEPTION_RECORD *rec, void *retval )
1068 CONTEXT context;
1069 EXCEPTION_RECORD record;
1070 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
1071 DWORD res;
1073 RtlCaptureContext( &context );
1074 context.R0 = (DWORD)retval;
1076 /* build an exception record, if we do not have one */
1077 if (!rec)
1079 record.ExceptionCode = STATUS_UNWIND;
1080 record.ExceptionFlags = 0;
1081 record.ExceptionRecord = NULL;
1082 record.ExceptionAddress = (void *)context.Pc;
1083 record.NumberParameters = 0;
1084 rec = &record;
1087 rec->ExceptionFlags |= EH_UNWINDING | (endframe ? 0 : EH_EXIT_UNWIND);
1089 TRACE( "code=%x flags=%x\n", rec->ExceptionCode, rec->ExceptionFlags );
1091 /* get chain of exception frames */
1092 frame = NtCurrentTeb()->Tib.ExceptionList;
1093 while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != endframe))
1095 /* Check frame address */
1096 if (endframe && ((void*)frame > endframe))
1097 raise_status( STATUS_INVALID_UNWIND_TARGET, rec );
1099 if (!is_valid_frame( frame )) raise_status( STATUS_BAD_STACK, rec );
1101 /* Call handler */
1102 TRACE( "calling handler at %p code=%x flags=%x\n",
1103 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
1104 res = frame->Handler(rec, frame, &context, &dispatch);
1105 TRACE( "handler at %p returned %x\n", frame->Handler, res );
1107 switch(res)
1109 case ExceptionContinueSearch:
1110 break;
1111 case ExceptionCollidedUnwind:
1112 frame = dispatch;
1113 break;
1114 default:
1115 raise_status( STATUS_INVALID_DISPOSITION, rec );
1116 break;
1118 frame = __wine_pop_frame( frame );
1122 /*******************************************************************
1123 * NtRaiseException (NTDLL.@)
1125 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
1127 NTSTATUS status = raise_exception( rec, context, first_chance );
1128 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
1129 return status;
1132 /***********************************************************************
1133 * RtlRaiseException (NTDLL.@)
1135 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
1137 CONTEXT context;
1138 NTSTATUS status;
1140 RtlCaptureContext( &context );
1141 rec->ExceptionAddress = (LPVOID)context.Pc;
1142 status = raise_exception( rec, &context, TRUE );
1143 if (status) raise_status( status, rec );
1146 /*************************************************************************
1147 * RtlCaptureStackBackTrace (NTDLL.@)
1149 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
1151 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
1152 return 0;
1155 /***********************************************************************
1156 * call_thread_entry_point
1158 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
1160 __TRY
1162 exit_thread( entry( arg ));
1164 __EXCEPT(unhandled_exception_filter)
1166 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
1168 __ENDTRY
1169 abort(); /* should not be reached */
1172 /***********************************************************************
1173 * RtlExitUserThread (NTDLL.@)
1175 void WINAPI RtlExitUserThread( ULONG status )
1177 exit_thread( status );
1180 /***********************************************************************
1181 * abort_thread
1183 void abort_thread( int status )
1185 terminate_thread( status );
1188 /**********************************************************************
1189 * DbgBreakPoint (NTDLL.@)
1191 void WINAPI DbgBreakPoint(void)
1193 kill(getpid(), SIGTRAP);
1196 /**********************************************************************
1197 * DbgUserBreakPoint (NTDLL.@)
1199 void WINAPI DbgUserBreakPoint(void)
1201 kill(getpid(), SIGTRAP);
1204 /**********************************************************************
1205 * NtCurrentTeb (NTDLL.@)
1207 TEB * WINAPI NtCurrentTeb(void)
1209 return pthread_getspecific( teb_key );
1212 #endif /* __arm__ */