configure: Allow specifying custom CFLAGS for LDAP.
[wine.git] / dlls / ntdll / signal_arm64.c
blob3a5156787835c97b48d141078b77604ec40f51ab
1 /*
2 * ARM64 signal handling routines
4 * Copyright 2010-2013 André Hentschel
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifdef __aarch64__
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34 #ifdef HAVE_SYS_PARAM_H
35 # include <sys/param.h>
36 #endif
37 #ifdef HAVE_SYSCALL_H
38 # include <syscall.h>
39 #else
40 # ifdef HAVE_SYS_SYSCALL_H
41 # include <sys/syscall.h>
42 # endif
43 #endif
44 #ifdef HAVE_SYS_SIGNAL_H
45 # include <sys/signal.h>
46 #endif
47 #ifdef HAVE_SYS_UCONTEXT_H
48 # include <sys/ucontext.h>
49 #endif
51 #include "ntstatus.h"
52 #define WIN32_NO_STATUS
53 #include "windef.h"
54 #include "winternl.h"
55 #include "wine/library.h"
56 #include "wine/exception.h"
57 #include "ntdll_misc.h"
58 #include "wine/debug.h"
59 #include "winnt.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(seh);
63 static pthread_key_t teb_key;
65 /***********************************************************************
66 * signal context platform-specific definitions
68 #ifdef linux
70 /* All Registers access - only for local access */
71 # define REG_sig(reg_name, context) ((context)->uc_mcontext.reg_name)
72 # define REGn_sig(reg_num, context) ((context)->uc_mcontext.regs[reg_num])
74 /* Special Registers access */
75 # define SP_sig(context) REG_sig(sp, context) /* Stack pointer */
76 # define PC_sig(context) REG_sig(pc, context) /* Program counter */
77 # define PSTATE_sig(context) REG_sig(pstate, context) /* Current State Register */
78 # define FP_sig(context) REGn_sig(29, context) /* Frame pointer */
79 # define LR_sig(context) REGn_sig(30, context) /* Link Register */
81 /* Exceptions */
82 # define FAULT_sig(context) REG_sig(fault_address, context)
84 #endif /* linux */
86 static const size_t teb_size = 0x2000; /* we reserve two pages for the TEB */
87 static size_t signal_stack_size;
89 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
90 typedef int (*wine_signal_handler)(unsigned int sig);
92 static wine_signal_handler handlers[256];
94 /***********************************************************************
95 * dispatch_signal
97 static inline int dispatch_signal(unsigned int sig)
99 if (handlers[sig] == NULL) return 0;
100 return handlers[sig](sig);
103 /*******************************************************************
104 * is_valid_frame
106 static inline BOOL is_valid_frame( void *frame )
108 if ((ULONG_PTR)frame & 3) return FALSE;
109 return (frame >= NtCurrentTeb()->Tib.StackLimit &&
110 (void **)frame < (void **)NtCurrentTeb()->Tib.StackBase - 1);
113 /***********************************************************************
114 * save_context
116 * Set the register values from a sigcontext.
118 static void save_context( CONTEXT *context, const ucontext_t *sigcontext )
120 #define C(n) context->X##n = REGn_sig(n,sigcontext)
121 /* Save normal registers */
122 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9);
123 C(10); C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19);
124 C(20); C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28);
125 #undef C
127 context->ContextFlags = CONTEXT_FULL;
128 context->Fp = FP_sig(sigcontext); /* Frame pointer */
129 context->Lr = LR_sig(sigcontext); /* Link register */
130 context->Sp = SP_sig(sigcontext); /* Stack pointer */
131 context->Pc = PC_sig(sigcontext); /* Program Counter */
132 context->Cpsr = PSTATE_sig(sigcontext); /* Current State Register */
136 /***********************************************************************
137 * restore_context
139 * Build a sigcontext from the register values.
141 static void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
143 #define C(n) REGn_sig(n,sigcontext) = context->X##n
144 /* Restore normal registers */
145 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9);
146 C(10); C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19);
147 C(20); C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28);
148 #undef C
150 FP_sig(sigcontext) = context->Fp; /* Frame pointer */
151 LR_sig(sigcontext) = context->Lr; /* Link register */
152 SP_sig(sigcontext) = context->Sp; /* Stack pointer */
153 PC_sig(sigcontext) = context->Pc; /* Program Counter */
154 PSTATE_sig(sigcontext) = context->Cpsr; /* Current State Register */
158 /***********************************************************************
159 * save_fpu
161 * Set the FPU context from a sigcontext.
163 static inline void save_fpu( CONTEXT *context, const ucontext_t *sigcontext )
165 FIXME( "Not implemented on ARM64\n" );
169 /***********************************************************************
170 * restore_fpu
172 * Restore the FPU context to a sigcontext.
174 static inline void restore_fpu( CONTEXT *context, const ucontext_t *sigcontext )
176 FIXME( "Not implemented on ARM64\n" );
179 /***********************************************************************
180 * RtlCaptureContext (NTDLL.@)
182 /* FIXME: Use the Stack instead of the actual register values? */
183 __ASM_STDCALL_FUNC( RtlCaptureContext, 8,
184 "stp x0, x1, [sp, #-32]!\n\t"
185 "mov w1, #0x400000\n\t" /* CONTEXT_ARM64 */
186 "add w1, w1, #0x3\n\t" /* CONTEXT_FULL */
187 "str w1, [x0]\n\t" /* context->ContextFlags */ /* 32-bit, look at cpsr */
188 "mrs x1, NZCV\n\t"
189 "str w1, [x0, #0x4]\n\t" /* context->Cpsr */
190 "ldp x0, x1, [sp], #32\n\t"
191 "str x0, [x0, #0x8]\n\t" /* context->X0 */
192 "str x1, [x0, #0x10]\n\t" /* context->X1 */
193 "str x2, [x0, #0x18]\n\t" /* context->X2 */
194 "str x3, [x0, #0x20]\n\t" /* context->X3 */
195 "str x4, [x0, #0x28]\n\t" /* context->X4 */
196 "str x5, [x0, #0x30]\n\t" /* context->X5 */
197 "str x6, [x0, #0x38]\n\t" /* context->X6 */
198 "str x7, [x0, #0x40]\n\t" /* context->X7 */
199 "str x8, [x0, #0x48]\n\t" /* context->X8 */
200 "str x9, [x0, #0x50]\n\t" /* context->X9 */
201 "str x10, [x0, #0x58]\n\t" /* context->X10 */
202 "str x11, [x0, #0x60]\n\t" /* context->X11 */
203 "str x12, [x0, #0x68]\n\t" /* context->X12 */
204 "str x13, [x0, #0x70]\n\t" /* context->X13 */
205 "str x14, [x0, #0x78]\n\t" /* context->X14 */
206 "str x15, [x0, #0x80]\n\t" /* context->X15 */
207 "str x16, [x0, #0x88]\n\t" /* context->X16 */
208 "str x17, [x0, #0x90]\n\t" /* context->X17 */
209 "str x18, [x0, #0x98]\n\t" /* context->X18 */
210 "str x19, [x0, #0xa0]\n\t" /* context->X19 */
211 "str x20, [x0, #0xa8]\n\t" /* context->X20 */
212 "str x21, [x0, #0xb0]\n\t" /* context->X21 */
213 "str x22, [x0, #0xb8]\n\t" /* context->X22 */
214 "str x23, [x0, #0xc0]\n\t" /* context->X23 */
215 "str x24, [x0, #0xc8]\n\t" /* context->X24 */
216 "str x25, [x0, #0xd0]\n\t" /* context->X25 */
217 "str x26, [x0, #0xd8]\n\t" /* context->X26 */
218 "str x27, [x0, #0xe0]\n\t" /* context->X27 */
219 "str x28, [x0, #0xe8]\n\t" /* context->X28 */
220 "str x29, [x0, #0xf0]\n\t" /* context->Fp */
221 "str x30, [x0, #0xf8]\n\t" /* context->Lr */
222 "mov x1, sp\n\t"
223 "str x1, [x0, #0x100]\n\t" /* context->Sp */
224 "adr x1, 1f\n\t"
225 "1: str x1, [x0, #0x108]\n\t" /* context->Pc */
226 "ret"
229 /***********************************************************************
230 * set_cpu_context
232 * Set the new CPU context.
234 static void set_cpu_context( const CONTEXT *context )
236 FIXME( "Not implemented on ARM64\n" );
239 /***********************************************************************
240 * copy_context
242 * Copy a register context according to the flags.
244 static void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
246 flags &= ~CONTEXT_ARM64; /* get rid of CPU id */
247 if (flags & CONTEXT_CONTROL)
249 to->Fp = from->Fp;
250 to->Lr = from->Lr;
251 to->Sp = from->Sp;
252 to->Pc = from->Pc;
253 to->Cpsr = from->Cpsr;
255 if (flags & CONTEXT_INTEGER)
257 #define C(n) to->X##n = from->X##n
258 /* Restore normal registers */
259 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9);
260 C(10); C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19);
261 C(20); C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28);
262 #undef C
266 /***********************************************************************
267 * context_to_server
269 * Convert a register context to the server format.
271 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
273 DWORD flags = from->ContextFlags & ~CONTEXT_ARM64; /* get rid of CPU id */
275 memset( to, 0, sizeof(*to) );
276 to->cpu = CPU_ARM64;
278 if (flags & CONTEXT_CONTROL)
280 to->flags |= SERVER_CTX_CONTROL;
281 to->integer.arm64_regs.x[29] = from->Fp;
282 to->integer.arm64_regs.x[30] = from->Lr;
283 to->ctl.arm64_regs.sp = from->Sp;
284 to->ctl.arm64_regs.pc = from->Pc;
285 to->ctl.arm64_regs.pstate = from->Cpsr;
287 if (flags & CONTEXT_INTEGER)
289 to->flags |= SERVER_CTX_INTEGER;
290 #define C(n) to->integer.arm64_regs.x[n] = from->X##n
291 /* Restore normal registers */
292 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9);
293 C(10); C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19);
294 C(20); C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28);
295 #undef C
297 return STATUS_SUCCESS;
301 /***********************************************************************
302 * context_from_server
304 * Convert a register context from the server format.
306 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
308 if (from->cpu != CPU_ARM64) return STATUS_INVALID_PARAMETER;
310 to->ContextFlags = CONTEXT_ARM64;
311 if (from->flags & SERVER_CTX_CONTROL)
313 to->ContextFlags |= CONTEXT_CONTROL;
314 to->Fp = from->integer.arm64_regs.x[29];
315 to->Lr = from->integer.arm64_regs.x[30];
316 to->Sp = from->ctl.arm64_regs.sp;
317 to->Pc = from->ctl.arm64_regs.pc;
318 to->Cpsr = from->ctl.arm64_regs.pstate;
320 if (from->flags & SERVER_CTX_INTEGER)
322 to->ContextFlags |= CONTEXT_INTEGER;
323 #define C(n) to->X##n = from->integer.arm64_regs.x[n]
324 /* Restore normal registers */
325 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9);
326 C(10); C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19);
327 C(20); C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28);
328 #undef C
330 return STATUS_SUCCESS;
333 /***********************************************************************
334 * NtSetContextThread (NTDLL.@)
335 * ZwSetContextThread (NTDLL.@)
337 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
339 NTSTATUS ret;
340 BOOL self;
342 ret = set_thread_context( handle, context, &self );
343 if (self && ret == STATUS_SUCCESS) set_cpu_context( context );
344 return ret;
348 /***********************************************************************
349 * NtGetContextThread (NTDLL.@)
350 * ZwGetContextThread (NTDLL.@)
352 NTSTATUS WINAPI NtGetContextThread( HANDLE handle, CONTEXT *context )
354 NTSTATUS ret;
355 DWORD needed_flags = context->ContextFlags;
356 BOOL self = (handle == GetCurrentThread());
358 if (!self)
360 if ((ret = get_thread_context( handle, context, &self ))) return ret;
361 needed_flags &= ~context->ContextFlags;
364 if (self && needed_flags)
366 CONTEXT ctx;
367 RtlCaptureContext( &ctx );
368 copy_context( context, &ctx, ctx.ContextFlags & needed_flags );
369 context->ContextFlags |= ctx.ContextFlags & needed_flags;
371 return STATUS_SUCCESS;
375 /***********************************************************************
376 * setup_exception_record
378 * Setup the exception record and context on the thread stack.
380 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
382 struct stack_layout
384 CONTEXT context;
385 EXCEPTION_RECORD rec;
386 } *stack;
387 DWORD exception_code = 0;
389 stack = (struct stack_layout *)(SP_sig(sigcontext) & ~15);
390 stack--; /* push the stack_layout structure */
392 stack->rec.ExceptionRecord = NULL;
393 stack->rec.ExceptionCode = exception_code;
394 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
395 stack->rec.ExceptionAddress = (LPVOID)PC_sig(sigcontext);
396 stack->rec.NumberParameters = 0;
398 save_context( &stack->context, sigcontext );
400 /* now modify the sigcontext to return to the raise function */
401 SP_sig(sigcontext) = (ULONG_PTR)stack;
402 PC_sig(sigcontext) = (ULONG_PTR)func;
403 REGn_sig(0, sigcontext) = (ULONG_PTR)&stack->rec; /* first arg for raise_func */
404 REGn_sig(1, sigcontext) = (ULONG_PTR)&stack->context; /* second arg for raise_func */
406 return &stack->rec;
409 /**********************************************************************
410 * raise_segv_exception
412 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
414 NTSTATUS status;
416 switch(rec->ExceptionCode)
418 case EXCEPTION_ACCESS_VIOLATION:
419 if (rec->NumberParameters == 2)
421 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
422 rec->ExceptionInformation[0], FALSE )))
423 goto done;
425 break;
427 status = NtRaiseException( rec, context, TRUE );
428 if (status) raise_status( status, rec );
429 done:
430 set_cpu_context( context );
433 /**********************************************************************
434 * call_stack_handlers
436 * Call the stack handlers chain.
438 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
440 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
441 DWORD res;
443 frame = NtCurrentTeb()->Tib.ExceptionList;
444 nested_frame = NULL;
445 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
447 /* Check frame address */
448 if (!is_valid_frame( frame ))
450 rec->ExceptionFlags |= EH_STACK_INVALID;
451 break;
454 /* Call handler */
455 TRACE( "calling handler at %p code=%x flags=%x\n",
456 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
457 res = frame->Handler( rec, frame, context, &dispatch );
458 TRACE( "handler at %p returned %x\n", frame->Handler, res );
460 if (frame == nested_frame)
462 /* no longer nested */
463 nested_frame = NULL;
464 rec->ExceptionFlags &= ~EH_NESTED_CALL;
467 switch(res)
469 case ExceptionContinueExecution:
470 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
471 return STATUS_NONCONTINUABLE_EXCEPTION;
472 case ExceptionContinueSearch:
473 break;
474 case ExceptionNestedException:
475 if (nested_frame < dispatch) nested_frame = dispatch;
476 rec->ExceptionFlags |= EH_NESTED_CALL;
477 break;
478 default:
479 return STATUS_INVALID_DISPOSITION;
481 frame = frame->Prev;
483 return STATUS_UNHANDLED_EXCEPTION;
487 /*******************************************************************
488 * raise_exception
490 * Implementation of NtRaiseException.
492 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
494 NTSTATUS status;
496 if (first_chance)
498 DWORD c;
500 for (c = 0; c < rec->NumberParameters; c++)
501 TRACE( " info[%d]=%016lx\n", c, rec->ExceptionInformation[c] );
502 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
504 if (rec->ExceptionInformation[1] >> 16)
505 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
506 rec->ExceptionAddress,
507 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
508 else
509 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
510 rec->ExceptionAddress,
511 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
513 else
515 /* FIXME: dump context */
518 status = send_debug_event( rec, TRUE, context );
519 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
520 return STATUS_SUCCESS;
522 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
523 return STATUS_SUCCESS;
525 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
526 return status;
529 /* last chance exception */
531 status = send_debug_event( rec, FALSE, context );
532 if (status != DBG_CONTINUE)
534 if (rec->ExceptionFlags & EH_STACK_INVALID)
535 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
536 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
537 ERR("Process attempted to continue execution after noncontinuable exception.\n");
538 else
539 ERR("Unhandled exception code %x flags %x addr %p\n",
540 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
541 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
543 return STATUS_SUCCESS;
546 /**********************************************************************
547 * segv_handler
549 * Handler for SIGSEGV and related errors.
551 static void segv_handler( int signal, siginfo_t *info, void *ucontext )
553 EXCEPTION_RECORD *rec;
554 ucontext_t *context = ucontext;
556 /* check for page fault inside the thread stack */
557 if (signal == SIGSEGV &&
558 (char *)info->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
559 (char *)info->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
560 virtual_handle_stack_fault( info->si_addr ))
562 /* check if this was the last guard page */
563 if ((char *)info->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
565 rec = setup_exception( context, raise_segv_exception );
566 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
568 return;
571 rec = setup_exception( context, raise_segv_exception );
572 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
574 switch(signal)
576 case SIGILL: /* Invalid opcode exception */
577 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
578 break;
579 case SIGSEGV: /* Segmentation fault */
580 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
581 rec->NumberParameters = 2;
582 /* FIXME: Currently the kernel provides no way to determine if it's read or write */
583 rec->ExceptionInformation[0] = 0;
584 rec->ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
585 break;
586 case SIGBUS: /* Alignment check exception */
587 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
588 break;
589 default:
590 ERR("Got unexpected signal %i\n", signal);
591 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
592 break;
596 /**********************************************************************
597 * trap_handler
599 * Handler for SIGTRAP.
601 static void trap_handler( int signal, siginfo_t *info, void *ucontext )
603 EXCEPTION_RECORD rec;
604 CONTEXT context;
605 NTSTATUS status;
607 switch ( info->si_code )
609 case TRAP_TRACE:
610 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
611 break;
612 case TRAP_BRKPT:
613 default:
614 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
615 break;
618 save_context( &context, ucontext );
619 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
620 rec.ExceptionRecord = NULL;
621 rec.ExceptionAddress = (LPVOID)context.Pc;
622 rec.NumberParameters = 0;
623 status = raise_exception( &rec, &context, TRUE );
624 if (status) raise_status( status, &rec );
625 restore_context( &context, ucontext );
628 /**********************************************************************
629 * fpe_handler
631 * Handler for SIGFPE.
633 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
635 EXCEPTION_RECORD rec;
636 CONTEXT context;
637 NTSTATUS status;
639 save_fpu( &context, sigcontext );
640 save_context( &context, sigcontext );
642 switch (siginfo->si_code & 0xffff )
644 #ifdef FPE_FLTSUB
645 case FPE_FLTSUB:
646 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
647 break;
648 #endif
649 #ifdef FPE_INTDIV
650 case FPE_INTDIV:
651 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
652 break;
653 #endif
654 #ifdef FPE_INTOVF
655 case FPE_INTOVF:
656 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
657 break;
658 #endif
659 #ifdef FPE_FLTDIV
660 case FPE_FLTDIV:
661 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
662 break;
663 #endif
664 #ifdef FPE_FLTOVF
665 case FPE_FLTOVF:
666 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
667 break;
668 #endif
669 #ifdef FPE_FLTUND
670 case FPE_FLTUND:
671 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
672 break;
673 #endif
674 #ifdef FPE_FLTRES
675 case FPE_FLTRES:
676 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
677 break;
678 #endif
679 #ifdef FPE_FLTINV
680 case FPE_FLTINV:
681 #endif
682 default:
683 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
684 break;
686 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
687 rec.ExceptionRecord = NULL;
688 rec.ExceptionAddress = (LPVOID)context.Pc;
689 rec.NumberParameters = 0;
690 status = raise_exception( &rec, &context, TRUE );
691 if (status) raise_status( status, &rec );
693 restore_context( &context, sigcontext );
694 restore_fpu( &context, sigcontext );
697 /**********************************************************************
698 * int_handler
700 * Handler for SIGINT.
702 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
704 if (!dispatch_signal(SIGINT))
706 EXCEPTION_RECORD rec;
707 CONTEXT context;
708 NTSTATUS status;
710 save_context( &context, sigcontext );
711 rec.ExceptionCode = CONTROL_C_EXIT;
712 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
713 rec.ExceptionRecord = NULL;
714 rec.ExceptionAddress = (LPVOID)context.Pc;
715 rec.NumberParameters = 0;
716 status = raise_exception( &rec, &context, TRUE );
717 if (status) raise_status( status, &rec );
718 restore_context( &context, sigcontext );
723 /**********************************************************************
724 * abrt_handler
726 * Handler for SIGABRT.
728 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
730 EXCEPTION_RECORD rec;
731 CONTEXT context;
732 NTSTATUS status;
734 save_context( &context, sigcontext );
735 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
736 rec.ExceptionFlags = EH_NONCONTINUABLE;
737 rec.ExceptionRecord = NULL;
738 rec.ExceptionAddress = (LPVOID)context.Pc;
739 rec.NumberParameters = 0;
740 status = raise_exception( &rec, &context, TRUE );
741 if (status) raise_status( status, &rec );
742 restore_context( &context, sigcontext );
746 /**********************************************************************
747 * quit_handler
749 * Handler for SIGQUIT.
751 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
753 abort_thread(0);
757 /**********************************************************************
758 * usr1_handler
760 * Handler for SIGUSR1, used to signal a thread that it got suspended.
762 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
764 CONTEXT context;
766 save_context( &context, sigcontext );
767 wait_suspend( &context );
768 restore_context( &context, sigcontext );
772 /***********************************************************************
773 * __wine_set_signal_handler (NTDLL.@)
775 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
777 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
778 if (handlers[sig] != NULL) return -2;
779 handlers[sig] = wsh;
780 return 0;
784 /**********************************************************************
785 * signal_alloc_thread
787 NTSTATUS signal_alloc_thread( TEB **teb )
789 static size_t sigstack_zero_bits;
790 SIZE_T size;
791 NTSTATUS status;
793 if (!sigstack_zero_bits)
795 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
796 /* find the first power of two not smaller than min_size */
797 sigstack_zero_bits = 12;
798 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
799 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
800 assert( sizeof(TEB) <= teb_size );
803 size = 1 << sigstack_zero_bits;
804 *teb = NULL;
805 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
806 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
808 (*teb)->Tib.Self = &(*teb)->Tib;
809 (*teb)->Tib.ExceptionList = (void *)~0UL;
811 return status;
815 /**********************************************************************
816 * signal_free_thread
818 void signal_free_thread( TEB *teb )
820 SIZE_T size;
822 if (teb->DeallocationStack)
824 size = 0;
825 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
827 size = 0;
828 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
832 /**********************************************************************
833 * signal_init_thread
835 void signal_init_thread( TEB *teb )
837 static BOOL init_done;
839 if (!init_done)
841 pthread_key_create( &teb_key, NULL );
842 init_done = TRUE;
845 /* Win64/ARM applications expect the TEB pointer to be in the x18 platform register. */
846 __asm__ __volatile__( "mov x18, %0" : : "r" (teb) );
848 pthread_setspecific( teb_key, teb );
852 /**********************************************************************
853 * signal_init_process
855 void signal_init_process(void)
857 struct sigaction sig_act;
859 sig_act.sa_mask = server_block_set;
860 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
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;
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
880 #ifdef SIGTRAP
881 sig_act.sa_sigaction = trap_handler;
882 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
883 #endif
884 return;
886 error:
887 perror("sigaction");
888 exit(1);
892 /**********************************************************************
893 * __wine_enter_vm86 (NTDLL.@)
895 void __wine_enter_vm86( CONTEXT *context )
897 MESSAGE("vm86 mode not supported on this platform\n");
900 /***********************************************************************
901 * RtlUnwind (NTDLL.@)
903 void WINAPI RtlUnwind( PVOID pEndFrame, PVOID targetIp, PEXCEPTION_RECORD pRecord, PVOID retval )
905 FIXME( "Not implemented on ARM64\n" );
908 /*******************************************************************
909 * NtRaiseException (NTDLL.@)
911 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
913 NTSTATUS status = raise_exception( rec, context, first_chance );
914 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
915 return status;
918 /***********************************************************************
919 * RtlRaiseException (NTDLL.@)
921 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
923 CONTEXT context;
924 NTSTATUS status;
926 RtlCaptureContext( &context );
927 rec->ExceptionAddress = (LPVOID)context.Pc;
928 status = raise_exception( rec, &context, TRUE );
929 if (status) raise_status( status, rec );
932 /*************************************************************************
933 * RtlCaptureStackBackTrace (NTDLL.@)
935 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
937 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
938 return 0;
941 /***********************************************************************
942 * call_thread_entry_point
944 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
946 __TRY
948 exit_thread( entry( arg ));
950 __EXCEPT(unhandled_exception_filter)
952 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
954 __ENDTRY
955 abort(); /* should not be reached */
958 /***********************************************************************
959 * RtlExitUserThread (NTDLL.@)
961 void WINAPI RtlExitUserThread( ULONG status )
963 exit_thread( status );
966 /***********************************************************************
967 * abort_thread
969 void abort_thread( int status )
971 terminate_thread( status );
974 /**********************************************************************
975 * DbgBreakPoint (NTDLL.@)
977 void WINAPI DbgBreakPoint(void)
979 kill(getpid(), SIGTRAP);
982 /**********************************************************************
983 * DbgUserBreakPoint (NTDLL.@)
985 void WINAPI DbgUserBreakPoint(void)
987 kill(getpid(), SIGTRAP);
990 /**********************************************************************
991 * NtCurrentTeb (NTDLL.@)
993 TEB * WINAPI NtCurrentTeb(void)
995 return pthread_getspecific( teb_key );
998 #endif /* __aarch64__ */