ntdll: Update the ARM64 CONTEXT struct to match Win SDK 10.0.16299.0.
[wine.git] / dlls / ntdll / signal_arm64.c
blobdef107130a95a8badf413f71bc6a43dd80d7318d
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 #define NONAMELESSUNION
52 #define NONAMELESSSTRUCT
53 #include "ntstatus.h"
54 #define WIN32_NO_STATUS
55 #include "windef.h"
56 #include "winternl.h"
57 #include "wine/library.h"
58 #include "wine/exception.h"
59 #include "ntdll_misc.h"
60 #include "wine/debug.h"
61 #include "winnt.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(seh);
64 WINE_DECLARE_DEBUG_CHANNEL(relay);
66 static pthread_key_t teb_key;
68 /***********************************************************************
69 * signal context platform-specific definitions
71 #ifdef linux
73 /* All Registers access - only for local access */
74 # define REG_sig(reg_name, context) ((context)->uc_mcontext.reg_name)
75 # define REGn_sig(reg_num, context) ((context)->uc_mcontext.regs[reg_num])
77 /* Special Registers access */
78 # define SP_sig(context) REG_sig(sp, context) /* Stack pointer */
79 # define PC_sig(context) REG_sig(pc, context) /* Program counter */
80 # define PSTATE_sig(context) REG_sig(pstate, context) /* Current State Register */
81 # define FP_sig(context) REGn_sig(29, context) /* Frame pointer */
82 # define LR_sig(context) REGn_sig(30, context) /* Link Register */
84 /* Exceptions */
85 # define FAULT_sig(context) REG_sig(fault_address, context)
87 #endif /* linux */
89 static const size_t teb_size = 0x2000; /* we reserve two pages for the TEB */
90 static size_t signal_stack_size;
92 typedef void (WINAPI *raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
93 typedef int (*wine_signal_handler)(unsigned int sig);
95 static wine_signal_handler handlers[256];
97 /***********************************************************************
98 * dispatch_signal
100 static inline int dispatch_signal(unsigned int sig)
102 if (handlers[sig] == NULL) return 0;
103 return handlers[sig](sig);
106 /*******************************************************************
107 * is_valid_frame
109 static inline BOOL is_valid_frame( void *frame )
111 if ((ULONG_PTR)frame & 3) return FALSE;
112 return (frame >= NtCurrentTeb()->Tib.StackLimit &&
113 (void **)frame < (void **)NtCurrentTeb()->Tib.StackBase - 1);
116 /***********************************************************************
117 * save_context
119 * Set the register values from a sigcontext.
121 static void save_context( CONTEXT *context, const ucontext_t *sigcontext )
123 DWORD i;
125 context->ContextFlags = CONTEXT_FULL;
126 context->u.s.Fp = FP_sig(sigcontext); /* Frame pointer */
127 context->u.s.Lr = LR_sig(sigcontext); /* Link register */
128 context->Sp = SP_sig(sigcontext); /* Stack pointer */
129 context->Pc = PC_sig(sigcontext); /* Program Counter */
130 context->Cpsr = PSTATE_sig(sigcontext); /* Current State Register */
131 for (i = 0; i <= 28; i++) context->u.X[i] = REGn_sig( i, sigcontext );
135 /***********************************************************************
136 * restore_context
138 * Build a sigcontext from the register values.
140 static void restore_context( const CONTEXT *context, ucontext_t *sigcontext )
142 DWORD i;
144 FP_sig(sigcontext) = context->u.s.Fp; /* Frame pointer */
145 LR_sig(sigcontext) = context->u.s.Lr; /* Link register */
146 SP_sig(sigcontext) = context->Sp; /* Stack pointer */
147 PC_sig(sigcontext) = context->Pc; /* Program Counter */
148 PSTATE_sig(sigcontext) = context->Cpsr; /* Current State Register */
149 for (i = 0; i <= 28; i++) REGn_sig( i, sigcontext ) = context->u.X[i];
153 /***********************************************************************
154 * save_fpu
156 * Set the FPU context from a sigcontext.
158 static inline void save_fpu( CONTEXT *context, const ucontext_t *sigcontext )
160 FIXME( "Not implemented on ARM64\n" );
164 /***********************************************************************
165 * restore_fpu
167 * Restore the FPU context to a sigcontext.
169 static inline void restore_fpu( CONTEXT *context, const ucontext_t *sigcontext )
171 FIXME( "Not implemented on ARM64\n" );
174 /***********************************************************************
175 * RtlCaptureContext (NTDLL.@)
177 /* FIXME: Use the Stack instead of the actual register values? */
178 __ASM_STDCALL_FUNC( RtlCaptureContext, 8,
179 "stp x0, x1, [sp, #-32]!\n\t"
180 "mov w1, #0x400000\n\t" /* CONTEXT_ARM64 */
181 "add w1, w1, #0x3\n\t" /* CONTEXT_FULL */
182 "str w1, [x0]\n\t" /* context->ContextFlags */ /* 32-bit, look at cpsr */
183 "mrs x1, NZCV\n\t"
184 "str w1, [x0, #0x4]\n\t" /* context->Cpsr */
185 "ldp x0, x1, [sp], #32\n\t"
186 "str x0, [x0, #0x8]\n\t" /* context->X0 */
187 "str x1, [x0, #0x10]\n\t" /* context->X1 */
188 "str x2, [x0, #0x18]\n\t" /* context->X2 */
189 "str x3, [x0, #0x20]\n\t" /* context->X3 */
190 "str x4, [x0, #0x28]\n\t" /* context->X4 */
191 "str x5, [x0, #0x30]\n\t" /* context->X5 */
192 "str x6, [x0, #0x38]\n\t" /* context->X6 */
193 "str x7, [x0, #0x40]\n\t" /* context->X7 */
194 "str x8, [x0, #0x48]\n\t" /* context->X8 */
195 "str x9, [x0, #0x50]\n\t" /* context->X9 */
196 "str x10, [x0, #0x58]\n\t" /* context->X10 */
197 "str x11, [x0, #0x60]\n\t" /* context->X11 */
198 "str x12, [x0, #0x68]\n\t" /* context->X12 */
199 "str x13, [x0, #0x70]\n\t" /* context->X13 */
200 "str x14, [x0, #0x78]\n\t" /* context->X14 */
201 "str x15, [x0, #0x80]\n\t" /* context->X15 */
202 "str x16, [x0, #0x88]\n\t" /* context->X16 */
203 "str x17, [x0, #0x90]\n\t" /* context->X17 */
204 "str x18, [x0, #0x98]\n\t" /* context->X18 */
205 "str x19, [x0, #0xa0]\n\t" /* context->X19 */
206 "str x20, [x0, #0xa8]\n\t" /* context->X20 */
207 "str x21, [x0, #0xb0]\n\t" /* context->X21 */
208 "str x22, [x0, #0xb8]\n\t" /* context->X22 */
209 "str x23, [x0, #0xc0]\n\t" /* context->X23 */
210 "str x24, [x0, #0xc8]\n\t" /* context->X24 */
211 "str x25, [x0, #0xd0]\n\t" /* context->X25 */
212 "str x26, [x0, #0xd8]\n\t" /* context->X26 */
213 "str x27, [x0, #0xe0]\n\t" /* context->X27 */
214 "str x28, [x0, #0xe8]\n\t" /* context->X28 */
215 "str x29, [x0, #0xf0]\n\t" /* context->Fp */
216 "str x30, [x0, #0xf8]\n\t" /* context->Lr */
217 "mov x1, sp\n\t"
218 "str x1, [x0, #0x100]\n\t" /* context->Sp */
219 "adr x1, 1f\n\t"
220 "1: str x1, [x0, #0x108]\n\t" /* context->Pc */
221 "ret"
224 /***********************************************************************
225 * set_cpu_context
227 * Set the new CPU context.
229 static void set_cpu_context( const CONTEXT *context )
231 FIXME( "Not implemented on ARM64\n" );
234 /***********************************************************************
235 * copy_context
237 * Copy a register context according to the flags.
239 static void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
241 flags &= ~CONTEXT_ARM64; /* get rid of CPU id */
242 if (flags & CONTEXT_CONTROL)
244 to->u.s.Fp = from->u.s.Fp;
245 to->u.s.Lr = from->u.s.Lr;
246 to->Sp = from->Sp;
247 to->Pc = from->Pc;
248 to->Cpsr = from->Cpsr;
250 if (flags & CONTEXT_INTEGER)
252 memcpy( to->u.X, from->u.X, sizeof(to->u.X) );
254 if (flags & CONTEXT_FLOATING_POINT)
256 memcpy( to->V, from->V, sizeof(to->V) );
257 to->Fpcr = from->Fpcr;
258 to->Fpsr = from->Fpsr;
260 if (flags & CONTEXT_DEBUG_REGISTERS)
262 memcpy( to->Bcr, from->Bcr, sizeof(to->Bcr) );
263 memcpy( to->Bvr, from->Bvr, sizeof(to->Bvr) );
264 memcpy( to->Wcr, from->Wcr, sizeof(to->Wcr) );
265 memcpy( to->Wvr, from->Wvr, sizeof(to->Wvr) );
269 /***********************************************************************
270 * context_to_server
272 * Convert a register context to the server format.
274 NTSTATUS context_to_server( context_t *to, const CONTEXT *from )
276 DWORD i, flags = from->ContextFlags & ~CONTEXT_ARM64; /* get rid of CPU id */
278 memset( to, 0, sizeof(*to) );
279 to->cpu = CPU_ARM64;
281 if (flags & CONTEXT_CONTROL)
283 to->flags |= SERVER_CTX_CONTROL;
284 to->integer.arm64_regs.x[29] = from->u.s.Fp;
285 to->integer.arm64_regs.x[30] = from->u.s.Lr;
286 to->ctl.arm64_regs.sp = from->Sp;
287 to->ctl.arm64_regs.pc = from->Pc;
288 to->ctl.arm64_regs.pstate = from->Cpsr;
290 if (flags & CONTEXT_INTEGER)
292 to->flags |= SERVER_CTX_INTEGER;
293 for (i = 0; i <= 28; i++) to->integer.arm64_regs.x[i] = from->u.X[i];
295 if (flags & CONTEXT_FLOATING_POINT)
297 to->flags |= SERVER_CTX_FLOATING_POINT;
298 for (i = 0; i < 64; i++) to->fp.arm64_regs.d[i] = from->V[i / 2].D[i % 2];
299 to->fp.arm64_regs.fpcr = from->Fpcr;
300 to->fp.arm64_regs.fpsr = from->Fpsr;
302 if (flags & CONTEXT_DEBUG_REGISTERS)
304 to->flags |= SERVER_CTX_DEBUG_REGISTERS;
305 for (i = 0; i < ARM64_MAX_BREAKPOINTS; i++) to->debug.arm64_regs.bcr[i] = from->Bcr[i];
306 for (i = 0; i < ARM64_MAX_BREAKPOINTS; i++) to->debug.arm64_regs.bvr[i] = from->Bvr[i];
307 for (i = 0; i < ARM64_MAX_WATCHPOINTS; i++) to->debug.arm64_regs.wcr[i] = from->Wcr[i];
308 for (i = 0; i < ARM64_MAX_WATCHPOINTS; i++) to->debug.arm64_regs.wvr[i] = from->Wvr[i];
310 return STATUS_SUCCESS;
314 /***********************************************************************
315 * context_from_server
317 * Convert a register context from the server format.
319 NTSTATUS context_from_server( CONTEXT *to, const context_t *from )
321 DWORD i;
323 if (from->cpu != CPU_ARM64) return STATUS_INVALID_PARAMETER;
325 to->ContextFlags = CONTEXT_ARM64;
326 if (from->flags & SERVER_CTX_CONTROL)
328 to->ContextFlags |= CONTEXT_CONTROL;
329 to->u.s.Fp = from->integer.arm64_regs.x[29];
330 to->u.s.Lr = from->integer.arm64_regs.x[30];
331 to->Sp = from->ctl.arm64_regs.sp;
332 to->Pc = from->ctl.arm64_regs.pc;
333 to->Cpsr = from->ctl.arm64_regs.pstate;
335 if (from->flags & SERVER_CTX_INTEGER)
337 to->ContextFlags |= CONTEXT_INTEGER;
338 for (i = 0; i <= 28; i++) to->u.X[i] = from->integer.arm64_regs.x[i];
340 if (from->flags & SERVER_CTX_FLOATING_POINT)
342 to->ContextFlags |= CONTEXT_FLOATING_POINT;
343 for (i = 0; i < 64; i++) to->V[i / 2].D[i % 2] = from->fp.arm64_regs.d[i];
344 to->Fpcr = from->fp.arm64_regs.fpcr;
345 to->Fpsr = from->fp.arm64_regs.fpsr;
347 if (from->flags & SERVER_CTX_DEBUG_REGISTERS)
349 to->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
350 for (i = 0; i < ARM64_MAX_BREAKPOINTS; i++) to->Bcr[i] = from->debug.arm64_regs.bcr[i];
351 for (i = 0; i < ARM64_MAX_BREAKPOINTS; i++) to->Bvr[i] = from->debug.arm64_regs.bvr[i];
352 for (i = 0; i < ARM64_MAX_WATCHPOINTS; i++) to->Wcr[i] = from->debug.arm64_regs.wcr[i];
353 for (i = 0; i < ARM64_MAX_WATCHPOINTS; i++) to->Wvr[i] = from->debug.arm64_regs.wvr[i];
355 return STATUS_SUCCESS;
358 /***********************************************************************
359 * NtSetContextThread (NTDLL.@)
360 * ZwSetContextThread (NTDLL.@)
362 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
364 NTSTATUS ret;
365 BOOL self;
367 ret = set_thread_context( handle, context, &self );
368 if (self && ret == STATUS_SUCCESS) set_cpu_context( context );
369 return ret;
373 /***********************************************************************
374 * NtGetContextThread (NTDLL.@)
375 * ZwGetContextThread (NTDLL.@)
377 NTSTATUS WINAPI NtGetContextThread( HANDLE handle, CONTEXT *context )
379 NTSTATUS ret;
380 DWORD needed_flags = context->ContextFlags;
381 BOOL self = (handle == GetCurrentThread());
383 if (!self)
385 if ((ret = get_thread_context( handle, context, &self ))) return ret;
386 needed_flags &= ~context->ContextFlags;
389 if (self && needed_flags)
391 CONTEXT ctx;
392 RtlCaptureContext( &ctx );
393 copy_context( context, &ctx, ctx.ContextFlags & needed_flags );
394 context->ContextFlags |= ctx.ContextFlags & needed_flags;
396 return STATUS_SUCCESS;
400 /***********************************************************************
401 * setup_exception_record
403 * Setup the exception record and context on the thread stack.
405 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
407 struct stack_layout
409 CONTEXT context;
410 EXCEPTION_RECORD rec;
411 } *stack;
412 DWORD exception_code = 0;
414 /* push the stack_layout structure */
415 stack = (struct stack_layout *)((SP_sig(sigcontext) - sizeof(*stack)) & ~15);
417 stack->rec.ExceptionRecord = NULL;
418 stack->rec.ExceptionCode = exception_code;
419 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
420 stack->rec.ExceptionAddress = (LPVOID)PC_sig(sigcontext);
421 stack->rec.NumberParameters = 0;
423 save_context( &stack->context, sigcontext );
425 /* now modify the sigcontext to return to the raise function */
426 SP_sig(sigcontext) = (ULONG_PTR)stack;
427 PC_sig(sigcontext) = (ULONG_PTR)func;
428 REGn_sig(0, sigcontext) = (ULONG_PTR)&stack->rec; /* first arg for raise_func */
429 REGn_sig(1, sigcontext) = (ULONG_PTR)&stack->context; /* second arg for raise_func */
431 return &stack->rec;
434 /**********************************************************************
435 * raise_segv_exception
437 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
439 NTSTATUS status;
441 switch(rec->ExceptionCode)
443 case EXCEPTION_ACCESS_VIOLATION:
444 if (rec->NumberParameters == 2)
446 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
447 rec->ExceptionInformation[0], FALSE )))
448 goto done;
450 break;
452 status = NtRaiseException( rec, context, TRUE );
453 if (status) raise_status( status, rec );
454 done:
455 set_cpu_context( context );
458 /**********************************************************************
459 * call_stack_handlers
461 * Call the stack handlers chain.
463 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
465 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
466 DWORD res;
468 frame = NtCurrentTeb()->Tib.ExceptionList;
469 nested_frame = NULL;
470 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
472 /* Check frame address */
473 if (!is_valid_frame( frame ))
475 rec->ExceptionFlags |= EH_STACK_INVALID;
476 break;
479 /* Call handler */
480 TRACE( "calling handler at %p code=%x flags=%x\n",
481 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
482 res = frame->Handler( rec, frame, context, &dispatch );
483 TRACE( "handler at %p returned %x\n", frame->Handler, res );
485 if (frame == nested_frame)
487 /* no longer nested */
488 nested_frame = NULL;
489 rec->ExceptionFlags &= ~EH_NESTED_CALL;
492 switch(res)
494 case ExceptionContinueExecution:
495 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
496 return STATUS_NONCONTINUABLE_EXCEPTION;
497 case ExceptionContinueSearch:
498 break;
499 case ExceptionNestedException:
500 if (nested_frame < dispatch) nested_frame = dispatch;
501 rec->ExceptionFlags |= EH_NESTED_CALL;
502 break;
503 default:
504 return STATUS_INVALID_DISPOSITION;
506 frame = frame->Prev;
508 return STATUS_UNHANDLED_EXCEPTION;
512 /*******************************************************************
513 * raise_exception
515 * Implementation of NtRaiseException.
517 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
519 NTSTATUS status;
521 if (first_chance)
523 DWORD c;
525 for (c = 0; c < rec->NumberParameters; c++)
526 TRACE( " info[%d]=%016lx\n", c, rec->ExceptionInformation[c] );
527 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
529 if (rec->ExceptionInformation[1] >> 16)
530 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
531 rec->ExceptionAddress,
532 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
533 else
534 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
535 rec->ExceptionAddress,
536 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
538 else
540 /* FIXME: dump context */
543 status = send_debug_event( rec, TRUE, context );
544 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
545 return STATUS_SUCCESS;
547 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
548 return STATUS_SUCCESS;
550 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
551 return status;
554 /* last chance exception */
556 status = send_debug_event( rec, FALSE, context );
557 if (status != DBG_CONTINUE)
559 if (rec->ExceptionFlags & EH_STACK_INVALID)
560 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
561 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
562 ERR("Process attempted to continue execution after noncontinuable exception.\n");
563 else
564 ERR("Unhandled exception code %x flags %x addr %p\n",
565 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
566 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
568 return STATUS_SUCCESS;
571 static inline DWORD is_write_fault( ucontext_t *context )
573 DWORD inst = *(DWORD *)PC_sig(context);
574 if ((inst & 0xbfff0000) == 0x0c000000 /* C3.3.1 */ ||
575 (inst & 0xbfe00000) == 0x0c800000 /* C3.3.2 */ ||
576 (inst & 0xbfdf0000) == 0x0d000000 /* C3.3.3 */ ||
577 (inst & 0xbfc00000) == 0x0d800000 /* C3.3.4 */ ||
578 (inst & 0x3f400000) == 0x08000000 /* C3.3.6 */ ||
579 (inst & 0x3bc00000) == 0x38000000 /* C3.3.8-12 */ ||
580 (inst & 0x3fe00000) == 0x3c800000 /* C3.3.8-12 128bit */ ||
581 (inst & 0x3bc00000) == 0x39000000 /* C3.3.13 */ ||
582 (inst & 0x3fc00000) == 0x3d800000 /* C3.3.13 128bit */ ||
583 (inst & 0x3a400000) == 0x28000000) /* C3.3.7,14-16 */
584 return EXCEPTION_WRITE_FAULT;
585 return EXCEPTION_READ_FAULT;
588 /**********************************************************************
589 * segv_handler
591 * Handler for SIGSEGV and related errors.
593 static void segv_handler( int signal, siginfo_t *info, void *ucontext )
595 EXCEPTION_RECORD *rec;
596 ucontext_t *context = ucontext;
598 /* check for page fault inside the thread stack */
599 if (signal == SIGSEGV &&
600 (char *)info->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
601 (char *)info->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
602 virtual_handle_stack_fault( info->si_addr ))
604 /* check if this was the last guard page */
605 if ((char *)info->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
607 rec = setup_exception( context, raise_segv_exception );
608 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
610 return;
613 rec = setup_exception( context, raise_segv_exception );
614 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
616 switch(signal)
618 case SIGILL: /* Invalid opcode exception */
619 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
620 break;
621 case SIGSEGV: /* Segmentation fault */
622 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
623 rec->NumberParameters = 2;
624 rec->ExceptionInformation[0] = is_write_fault(context);
625 rec->ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
626 break;
627 case SIGBUS: /* Alignment check exception */
628 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
629 break;
630 default:
631 ERR("Got unexpected signal %i\n", signal);
632 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
633 break;
637 /**********************************************************************
638 * trap_handler
640 * Handler for SIGTRAP.
642 static void trap_handler( int signal, siginfo_t *info, void *ucontext )
644 EXCEPTION_RECORD rec;
645 CONTEXT context;
646 NTSTATUS status;
648 switch ( info->si_code )
650 case TRAP_TRACE:
651 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
652 break;
653 case TRAP_BRKPT:
654 default:
655 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
656 break;
659 save_context( &context, ucontext );
660 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
661 rec.ExceptionRecord = NULL;
662 rec.ExceptionAddress = (LPVOID)context.Pc;
663 rec.NumberParameters = 0;
664 status = raise_exception( &rec, &context, TRUE );
665 if (status) raise_status( status, &rec );
666 restore_context( &context, ucontext );
669 /**********************************************************************
670 * fpe_handler
672 * Handler for SIGFPE.
674 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
676 EXCEPTION_RECORD rec;
677 CONTEXT context;
678 NTSTATUS status;
680 save_fpu( &context, sigcontext );
681 save_context( &context, sigcontext );
683 switch (siginfo->si_code & 0xffff )
685 #ifdef FPE_FLTSUB
686 case FPE_FLTSUB:
687 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
688 break;
689 #endif
690 #ifdef FPE_INTDIV
691 case FPE_INTDIV:
692 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
693 break;
694 #endif
695 #ifdef FPE_INTOVF
696 case FPE_INTOVF:
697 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
698 break;
699 #endif
700 #ifdef FPE_FLTDIV
701 case FPE_FLTDIV:
702 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
703 break;
704 #endif
705 #ifdef FPE_FLTOVF
706 case FPE_FLTOVF:
707 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
708 break;
709 #endif
710 #ifdef FPE_FLTUND
711 case FPE_FLTUND:
712 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
713 break;
714 #endif
715 #ifdef FPE_FLTRES
716 case FPE_FLTRES:
717 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
718 break;
719 #endif
720 #ifdef FPE_FLTINV
721 case FPE_FLTINV:
722 #endif
723 default:
724 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
725 break;
727 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
728 rec.ExceptionRecord = NULL;
729 rec.ExceptionAddress = (LPVOID)context.Pc;
730 rec.NumberParameters = 0;
731 status = raise_exception( &rec, &context, TRUE );
732 if (status) raise_status( status, &rec );
734 restore_context( &context, sigcontext );
735 restore_fpu( &context, sigcontext );
738 /**********************************************************************
739 * int_handler
741 * Handler for SIGINT.
743 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
745 if (!dispatch_signal(SIGINT))
747 EXCEPTION_RECORD rec;
748 CONTEXT context;
749 NTSTATUS status;
751 save_context( &context, sigcontext );
752 rec.ExceptionCode = CONTROL_C_EXIT;
753 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
754 rec.ExceptionRecord = NULL;
755 rec.ExceptionAddress = (LPVOID)context.Pc;
756 rec.NumberParameters = 0;
757 status = raise_exception( &rec, &context, TRUE );
758 if (status) raise_status( status, &rec );
759 restore_context( &context, sigcontext );
764 /**********************************************************************
765 * abrt_handler
767 * Handler for SIGABRT.
769 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
771 EXCEPTION_RECORD rec;
772 CONTEXT context;
773 NTSTATUS status;
775 save_context( &context, sigcontext );
776 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
777 rec.ExceptionFlags = EH_NONCONTINUABLE;
778 rec.ExceptionRecord = NULL;
779 rec.ExceptionAddress = (LPVOID)context.Pc;
780 rec.NumberParameters = 0;
781 status = raise_exception( &rec, &context, TRUE );
782 if (status) raise_status( status, &rec );
783 restore_context( &context, sigcontext );
787 /**********************************************************************
788 * quit_handler
790 * Handler for SIGQUIT.
792 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
794 abort_thread(0);
798 /**********************************************************************
799 * usr1_handler
801 * Handler for SIGUSR1, used to signal a thread that it got suspended.
803 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
805 CONTEXT context;
807 save_context( &context, sigcontext );
808 wait_suspend( &context );
809 restore_context( &context, sigcontext );
813 /***********************************************************************
814 * __wine_set_signal_handler (NTDLL.@)
816 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
818 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
819 if (handlers[sig] != NULL) return -2;
820 handlers[sig] = wsh;
821 return 0;
825 /**********************************************************************
826 * signal_alloc_thread
828 NTSTATUS signal_alloc_thread( TEB **teb )
830 static size_t sigstack_zero_bits;
831 SIZE_T size;
832 NTSTATUS status;
834 if (!sigstack_zero_bits)
836 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
837 /* find the first power of two not smaller than min_size */
838 sigstack_zero_bits = 12;
839 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
840 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
841 assert( sizeof(TEB) <= teb_size );
844 size = 1 << sigstack_zero_bits;
845 *teb = NULL;
846 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
847 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
849 (*teb)->Tib.Self = &(*teb)->Tib;
850 (*teb)->Tib.ExceptionList = (void *)~0UL;
852 return status;
856 /**********************************************************************
857 * signal_free_thread
859 void signal_free_thread( TEB *teb )
861 SIZE_T size = 0;
863 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
867 /**********************************************************************
868 * signal_init_thread
870 void signal_init_thread( TEB *teb )
872 static BOOL init_done;
874 if (!init_done)
876 pthread_key_create( &teb_key, NULL );
877 init_done = TRUE;
880 /* Win64/ARM applications expect the TEB pointer to be in the x18 platform register. */
881 __asm__ __volatile__( "mov x18, %0" : : "r" (teb) );
883 pthread_setspecific( teb_key, teb );
887 /**********************************************************************
888 * signal_init_process
890 void signal_init_process(void)
892 struct sigaction sig_act;
894 sig_act.sa_mask = server_block_set;
895 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
897 sig_act.sa_sigaction = int_handler;
898 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
899 sig_act.sa_sigaction = fpe_handler;
900 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
901 sig_act.sa_sigaction = abrt_handler;
902 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
903 sig_act.sa_sigaction = quit_handler;
904 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
905 sig_act.sa_sigaction = usr1_handler;
906 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
908 sig_act.sa_sigaction = segv_handler;
909 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
910 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
911 #ifdef SIGBUS
912 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
913 #endif
915 #ifdef SIGTRAP
916 sig_act.sa_sigaction = trap_handler;
917 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
918 #endif
919 return;
921 error:
922 perror("sigaction");
923 exit(1);
927 /***********************************************************************
928 * RtlUnwind (NTDLL.@)
930 void WINAPI RtlUnwind( PVOID pEndFrame, PVOID targetIp, PEXCEPTION_RECORD pRecord, PVOID retval )
932 FIXME( "Not implemented on ARM64\n" );
935 /*******************************************************************
936 * NtRaiseException (NTDLL.@)
938 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
940 NTSTATUS status = raise_exception( rec, context, first_chance );
941 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
942 return status;
945 /***********************************************************************
946 * RtlRaiseException (NTDLL.@)
948 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
950 CONTEXT context;
951 NTSTATUS status;
953 RtlCaptureContext( &context );
954 rec->ExceptionAddress = (LPVOID)context.Pc;
955 status = raise_exception( rec, &context, TRUE );
956 if (status) raise_status( status, rec );
959 /*************************************************************************
960 * RtlCaptureStackBackTrace (NTDLL.@)
962 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
964 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
965 return 0;
968 /***********************************************************************
969 * call_thread_entry_point
971 static void WINAPI call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
973 __TRY
975 TRACE_(relay)( "\1Starting thread proc %p (arg=%p)\n", entry, arg );
976 RtlExitUserThread( entry( arg ));
978 __EXCEPT(unhandled_exception_filter)
980 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
982 __ENDTRY
983 abort(); /* should not be reached */
986 typedef void (WINAPI *thread_start_func)(LPTHREAD_START_ROUTINE,void *);
988 struct startup_info
990 thread_start_func start;
991 LPTHREAD_START_ROUTINE entry;
992 void *arg;
993 BOOL suspend;
996 /***********************************************************************
997 * thread_startup
999 static void thread_startup( void *param )
1001 CONTEXT context = { 0 };
1002 struct startup_info *info = param;
1004 /* build the initial context */
1005 context.ContextFlags = CONTEXT_FULL;
1006 context.u.s.X0 = (DWORD_PTR)info->entry;
1007 context.u.s.X1 = (DWORD_PTR)info->arg;
1008 context.Sp = (DWORD_PTR)NtCurrentTeb()->Tib.StackBase;
1009 context.Pc = (DWORD_PTR)info->start;
1011 if (info->suspend) wait_suspend( &context );
1012 attach_dlls( &context, (void **)&context.u.s.X0 );
1014 ((thread_start_func)context.Pc)( (LPTHREAD_START_ROUTINE)context.u.s.X0, (void *)context.u.s.X1 );
1017 /***********************************************************************
1018 * signal_start_thread
1020 * Thread startup sequence:
1021 * signal_start_thread()
1022 * -> thread_startup()
1023 * -> call_thread_entry_point()
1025 void signal_start_thread( LPTHREAD_START_ROUTINE entry, void *arg, BOOL suspend )
1027 struct startup_info info = { call_thread_entry_point, entry, arg, suspend };
1028 wine_switch_to_stack( thread_startup, &info, NtCurrentTeb()->Tib.StackBase );
1031 /**********************************************************************
1032 * signal_start_process
1034 * Process startup sequence:
1035 * signal_start_process()
1036 * -> thread_startup()
1037 * -> kernel32_start_process()
1039 void signal_start_process( LPTHREAD_START_ROUTINE entry, BOOL suspend )
1041 struct startup_info info = { kernel32_start_process, entry, NtCurrentTeb()->Peb, suspend };
1042 wine_switch_to_stack( thread_startup, &info, NtCurrentTeb()->Tib.StackBase );
1045 /***********************************************************************
1046 * signal_exit_thread
1048 void signal_exit_thread( int status )
1050 exit_thread( status );
1053 /***********************************************************************
1054 * signal_exit_process
1056 void signal_exit_process( int status )
1058 exit( status );
1061 /**********************************************************************
1062 * DbgBreakPoint (NTDLL.@)
1064 void WINAPI DbgBreakPoint(void)
1066 kill(getpid(), SIGTRAP);
1069 /**********************************************************************
1070 * DbgUserBreakPoint (NTDLL.@)
1072 void WINAPI DbgUserBreakPoint(void)
1074 kill(getpid(), SIGTRAP);
1077 /**********************************************************************
1078 * NtCurrentTeb (NTDLL.@)
1080 TEB * WINAPI NtCurrentTeb(void)
1082 return pthread_getspecific( teb_key );
1085 #endif /* __aarch64__ */