storage.dll16: Fix get_nth_next_small_blocknr.
[wine.git] / dlls / ntdll / signal_arm64.c
blob14c5260c1488119487a97793542e382a6b18edd9
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, DAIF\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 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 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 * setup_exception_record
336 * Setup the exception record and context on the thread stack.
338 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
340 struct stack_layout
342 CONTEXT context;
343 EXCEPTION_RECORD rec;
344 } *stack;
345 DWORD exception_code = 0;
347 stack = (struct stack_layout *)(SP_sig(sigcontext) & ~15);
348 stack--; /* push the stack_layout structure */
350 stack->rec.ExceptionRecord = NULL;
351 stack->rec.ExceptionCode = exception_code;
352 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
353 stack->rec.ExceptionAddress = (LPVOID)PC_sig(sigcontext);
354 stack->rec.NumberParameters = 0;
356 save_context( &stack->context, sigcontext );
358 /* now modify the sigcontext to return to the raise function */
359 SP_sig(sigcontext) = (ULONG_PTR)stack;
360 PC_sig(sigcontext) = (ULONG_PTR)func;
361 REGn_sig(0, sigcontext) = (ULONG_PTR)&stack->rec; /* first arg for raise_func */
362 REGn_sig(1, sigcontext) = (ULONG_PTR)&stack->context; /* second arg for raise_func */
364 return &stack->rec;
367 /**********************************************************************
368 * raise_segv_exception
370 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
372 NTSTATUS status;
374 switch(rec->ExceptionCode)
376 case EXCEPTION_ACCESS_VIOLATION:
377 if (rec->NumberParameters == 2)
379 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
380 rec->ExceptionInformation[0], FALSE )))
381 goto done;
383 break;
385 status = NtRaiseException( rec, context, TRUE );
386 if (status) raise_status( status, rec );
387 done:
388 set_cpu_context( context );
391 /**********************************************************************
392 * call_stack_handlers
394 * Call the stack handlers chain.
396 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
398 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
399 DWORD res;
401 frame = NtCurrentTeb()->Tib.ExceptionList;
402 nested_frame = NULL;
403 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
405 /* Check frame address */
406 if (!is_valid_frame( frame ))
408 rec->ExceptionFlags |= EH_STACK_INVALID;
409 break;
412 /* Call handler */
413 TRACE( "calling handler at %p code=%x flags=%x\n",
414 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
415 res = frame->Handler( rec, frame, context, &dispatch );
416 TRACE( "handler at %p returned %x\n", frame->Handler, res );
418 if (frame == nested_frame)
420 /* no longer nested */
421 nested_frame = NULL;
422 rec->ExceptionFlags &= ~EH_NESTED_CALL;
425 switch(res)
427 case ExceptionContinueExecution:
428 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
429 return STATUS_NONCONTINUABLE_EXCEPTION;
430 case ExceptionContinueSearch:
431 break;
432 case ExceptionNestedException:
433 if (nested_frame < dispatch) nested_frame = dispatch;
434 rec->ExceptionFlags |= EH_NESTED_CALL;
435 break;
436 default:
437 return STATUS_INVALID_DISPOSITION;
439 frame = frame->Prev;
441 return STATUS_UNHANDLED_EXCEPTION;
445 /*******************************************************************
446 * raise_exception
448 * Implementation of NtRaiseException.
450 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
452 NTSTATUS status;
454 if (first_chance)
456 DWORD c;
458 for (c = 0; c < rec->NumberParameters; c++)
459 TRACE( " info[%d]=%016lx\n", c, rec->ExceptionInformation[c] );
460 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
462 if (rec->ExceptionInformation[1] >> 16)
463 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
464 rec->ExceptionAddress,
465 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
466 else
467 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
468 rec->ExceptionAddress,
469 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
471 else
473 /* FIXME: dump context */
476 status = send_debug_event( rec, TRUE, context );
477 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
478 return STATUS_SUCCESS;
480 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
481 return STATUS_SUCCESS;
483 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
484 return status;
487 /* last chance exception */
489 status = send_debug_event( rec, FALSE, context );
490 if (status != DBG_CONTINUE)
492 if (rec->ExceptionFlags & EH_STACK_INVALID)
493 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
494 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
495 ERR("Process attempted to continue execution after noncontinuable exception.\n");
496 else
497 ERR("Unhandled exception code %x flags %x addr %p\n",
498 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
499 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
501 return STATUS_SUCCESS;
504 /**********************************************************************
505 * segv_handler
507 * Handler for SIGSEGV and related errors.
509 static void segv_handler( int signal, siginfo_t *info, void *ucontext )
511 EXCEPTION_RECORD *rec;
512 ucontext_t *context = ucontext;
514 /* check for page fault inside the thread stack */
515 if (signal == SIGSEGV &&
516 (char *)info->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
517 (char *)info->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
518 virtual_handle_stack_fault( info->si_addr ))
520 /* check if this was the last guard page */
521 if ((char *)info->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
523 rec = setup_exception( context, raise_segv_exception );
524 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
526 return;
529 rec = setup_exception( context, raise_segv_exception );
530 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
532 switch(signal)
534 case SIGILL: /* Invalid opcode exception */
535 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
536 break;
537 case SIGSEGV: /* Segmentation fault */
538 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
539 rec->NumberParameters = 2;
540 /* FIXME: Currently the kernel provides no way to determine if it's read or write */
541 rec->ExceptionInformation[0] = 0;
542 rec->ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
543 break;
544 case SIGBUS: /* Alignment check exception */
545 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
546 break;
547 default:
548 ERR("Got unexpected signal %i\n", signal);
549 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
550 break;
554 /**********************************************************************
555 * trap_handler
557 * Handler for SIGTRAP.
559 static void trap_handler( int signal, siginfo_t *info, void *ucontext )
561 EXCEPTION_RECORD rec;
562 CONTEXT context;
563 NTSTATUS status;
565 switch ( info->si_code )
567 case TRAP_TRACE:
568 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
569 break;
570 case TRAP_BRKPT:
571 default:
572 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
573 break;
576 save_context( &context, ucontext );
577 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
578 rec.ExceptionRecord = NULL;
579 rec.ExceptionAddress = (LPVOID)context.Pc;
580 rec.NumberParameters = 0;
581 status = raise_exception( &rec, &context, TRUE );
582 if (status) raise_status( status, &rec );
583 restore_context( &context, ucontext );
586 /**********************************************************************
587 * fpe_handler
589 * Handler for SIGFPE.
591 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
593 EXCEPTION_RECORD rec;
594 CONTEXT context;
595 NTSTATUS status;
597 save_fpu( &context, sigcontext );
598 save_context( &context, sigcontext );
600 switch (siginfo->si_code & 0xffff )
602 #ifdef FPE_FLTSUB
603 case FPE_FLTSUB:
604 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
605 break;
606 #endif
607 #ifdef FPE_INTDIV
608 case FPE_INTDIV:
609 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
610 break;
611 #endif
612 #ifdef FPE_INTOVF
613 case FPE_INTOVF:
614 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
615 break;
616 #endif
617 #ifdef FPE_FLTDIV
618 case FPE_FLTDIV:
619 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
620 break;
621 #endif
622 #ifdef FPE_FLTOVF
623 case FPE_FLTOVF:
624 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
625 break;
626 #endif
627 #ifdef FPE_FLTUND
628 case FPE_FLTUND:
629 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
630 break;
631 #endif
632 #ifdef FPE_FLTRES
633 case FPE_FLTRES:
634 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
635 break;
636 #endif
637 #ifdef FPE_FLTINV
638 case FPE_FLTINV:
639 #endif
640 default:
641 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
642 break;
644 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
645 rec.ExceptionRecord = NULL;
646 rec.ExceptionAddress = (LPVOID)context.Pc;
647 rec.NumberParameters = 0;
648 status = raise_exception( &rec, &context, TRUE );
649 if (status) raise_status( status, &rec );
651 restore_context( &context, sigcontext );
652 restore_fpu( &context, sigcontext );
655 /**********************************************************************
656 * int_handler
658 * Handler for SIGINT.
660 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
662 if (!dispatch_signal(SIGINT))
664 EXCEPTION_RECORD rec;
665 CONTEXT context;
666 NTSTATUS status;
668 save_context( &context, sigcontext );
669 rec.ExceptionCode = CONTROL_C_EXIT;
670 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
671 rec.ExceptionRecord = NULL;
672 rec.ExceptionAddress = (LPVOID)context.Pc;
673 rec.NumberParameters = 0;
674 status = raise_exception( &rec, &context, TRUE );
675 if (status) raise_status( status, &rec );
676 restore_context( &context, sigcontext );
681 /**********************************************************************
682 * abrt_handler
684 * Handler for SIGABRT.
686 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
688 EXCEPTION_RECORD rec;
689 CONTEXT context;
690 NTSTATUS status;
692 save_context( &context, sigcontext );
693 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
694 rec.ExceptionFlags = EH_NONCONTINUABLE;
695 rec.ExceptionRecord = NULL;
696 rec.ExceptionAddress = (LPVOID)context.Pc;
697 rec.NumberParameters = 0;
698 status = raise_exception( &rec, &context, TRUE );
699 if (status) raise_status( status, &rec );
700 restore_context( &context, sigcontext );
704 /**********************************************************************
705 * quit_handler
707 * Handler for SIGQUIT.
709 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
711 abort_thread(0);
715 /**********************************************************************
716 * usr1_handler
718 * Handler for SIGUSR1, used to signal a thread that it got suspended.
720 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
722 CONTEXT context;
724 save_context( &context, sigcontext );
725 wait_suspend( &context );
726 restore_context( &context, sigcontext );
730 /***********************************************************************
731 * __wine_set_signal_handler (NTDLL.@)
733 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
735 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
736 if (handlers[sig] != NULL) return -2;
737 handlers[sig] = wsh;
738 return 0;
742 /**********************************************************************
743 * signal_alloc_thread
745 NTSTATUS signal_alloc_thread( TEB **teb )
747 static size_t sigstack_zero_bits;
748 SIZE_T size;
749 NTSTATUS status;
751 if (!sigstack_zero_bits)
753 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
754 /* find the first power of two not smaller than min_size */
755 sigstack_zero_bits = 12;
756 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
757 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
758 assert( sizeof(TEB) <= teb_size );
761 size = 1 << sigstack_zero_bits;
762 *teb = NULL;
763 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
764 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
766 (*teb)->Tib.Self = &(*teb)->Tib;
767 (*teb)->Tib.ExceptionList = (void *)~0UL;
769 return status;
773 /**********************************************************************
774 * signal_free_thread
776 void signal_free_thread( TEB *teb )
778 SIZE_T size;
780 if (teb->DeallocationStack)
782 size = 0;
783 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
785 size = 0;
786 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
790 /**********************************************************************
791 * signal_init_thread
793 void signal_init_thread( TEB *teb )
795 static BOOL init_done;
797 if (!init_done)
799 pthread_key_create( &teb_key, NULL );
800 init_done = TRUE;
803 /* Win64/ARM applications expect the TEB pointer to be in the x18 platform register. */
804 __asm__ __volatile__( "mov x18, %0" : : "r" (teb) );
806 pthread_setspecific( teb_key, teb );
810 /**********************************************************************
811 * signal_init_process
813 void signal_init_process(void)
815 struct sigaction sig_act;
817 sig_act.sa_mask = server_block_set;
818 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
820 sig_act.sa_sigaction = int_handler;
821 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
822 sig_act.sa_sigaction = fpe_handler;
823 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
824 sig_act.sa_sigaction = abrt_handler;
825 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
826 sig_act.sa_sigaction = quit_handler;
827 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
828 sig_act.sa_sigaction = usr1_handler;
829 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
831 sig_act.sa_sigaction = segv_handler;
832 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
833 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
834 #ifdef SIGBUS
835 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
836 #endif
838 #ifdef SIGTRAP
839 sig_act.sa_sigaction = trap_handler;
840 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
841 #endif
842 return;
844 error:
845 perror("sigaction");
846 exit(1);
850 /**********************************************************************
851 * __wine_enter_vm86 (NTDLL.@)
853 void __wine_enter_vm86( CONTEXT *context )
855 MESSAGE("vm86 mode not supported on this platform\n");
858 /***********************************************************************
859 * RtlUnwind (NTDLL.@)
861 void WINAPI RtlUnwind( PVOID pEndFrame, PVOID targetIp, PEXCEPTION_RECORD pRecord, PVOID retval )
863 FIXME( "Not implemented on ARM64\n" );
866 /*******************************************************************
867 * NtRaiseException (NTDLL.@)
869 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
871 NTSTATUS status = raise_exception( rec, context, first_chance );
872 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
873 return status;
876 /***********************************************************************
877 * RtlRaiseException (NTDLL.@)
879 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
881 CONTEXT context;
882 NTSTATUS status;
884 RtlCaptureContext( &context );
885 rec->ExceptionAddress = (LPVOID)context.Pc;
886 status = raise_exception( rec, &context, TRUE );
887 if (status) raise_status( status, rec );
890 /*************************************************************************
891 * RtlCaptureStackBackTrace (NTDLL.@)
893 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
895 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
896 return 0;
899 /***********************************************************************
900 * call_thread_entry_point
902 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
904 __TRY
906 exit_thread( entry( arg ));
908 __EXCEPT(unhandled_exception_filter)
910 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
912 __ENDTRY
913 abort(); /* should not be reached */
916 /***********************************************************************
917 * RtlExitUserThread (NTDLL.@)
919 void WINAPI RtlExitUserThread( ULONG status )
921 exit_thread( status );
924 /***********************************************************************
925 * abort_thread
927 void abort_thread( int status )
929 terminate_thread( status );
932 /**********************************************************************
933 * DbgBreakPoint (NTDLL.@)
935 void WINAPI DbgBreakPoint(void)
937 kill(getpid(), SIGTRAP);
940 /**********************************************************************
941 * DbgUserBreakPoint (NTDLL.@)
943 void WINAPI DbgUserBreakPoint(void)
945 kill(getpid(), SIGTRAP);
948 /**********************************************************************
949 * NtCurrentTeb (NTDLL.@)
951 TEB * WINAPI NtCurrentTeb(void)
953 return pthread_getspecific( teb_key );
956 #endif /* __aarch64__ */