ntdll: Move NtSetContextThread implementation to the platform-specific files.
[wine.git] / dlls / ntdll / signal_arm64.c
blob77fc8dc6136d38953c4fdfac5049bcb58a4d5ac0
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 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 * setup_exception_record
351 * Setup the exception record and context on the thread stack.
353 static EXCEPTION_RECORD *setup_exception( ucontext_t *sigcontext, raise_func func )
355 struct stack_layout
357 CONTEXT context;
358 EXCEPTION_RECORD rec;
359 } *stack;
360 DWORD exception_code = 0;
362 stack = (struct stack_layout *)(SP_sig(sigcontext) & ~15);
363 stack--; /* push the stack_layout structure */
365 stack->rec.ExceptionRecord = NULL;
366 stack->rec.ExceptionCode = exception_code;
367 stack->rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
368 stack->rec.ExceptionAddress = (LPVOID)PC_sig(sigcontext);
369 stack->rec.NumberParameters = 0;
371 save_context( &stack->context, sigcontext );
373 /* now modify the sigcontext to return to the raise function */
374 SP_sig(sigcontext) = (ULONG_PTR)stack;
375 PC_sig(sigcontext) = (ULONG_PTR)func;
376 REGn_sig(0, sigcontext) = (ULONG_PTR)&stack->rec; /* first arg for raise_func */
377 REGn_sig(1, sigcontext) = (ULONG_PTR)&stack->context; /* second arg for raise_func */
379 return &stack->rec;
382 /**********************************************************************
383 * raise_segv_exception
385 static void WINAPI raise_segv_exception( EXCEPTION_RECORD *rec, CONTEXT *context )
387 NTSTATUS status;
389 switch(rec->ExceptionCode)
391 case EXCEPTION_ACCESS_VIOLATION:
392 if (rec->NumberParameters == 2)
394 if (!(rec->ExceptionCode = virtual_handle_fault( (void *)rec->ExceptionInformation[1],
395 rec->ExceptionInformation[0], FALSE )))
396 goto done;
398 break;
400 status = NtRaiseException( rec, context, TRUE );
401 if (status) raise_status( status, rec );
402 done:
403 set_cpu_context( context );
406 /**********************************************************************
407 * call_stack_handlers
409 * Call the stack handlers chain.
411 static NTSTATUS call_stack_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
413 EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
414 DWORD res;
416 frame = NtCurrentTeb()->Tib.ExceptionList;
417 nested_frame = NULL;
418 while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
420 /* Check frame address */
421 if (!is_valid_frame( frame ))
423 rec->ExceptionFlags |= EH_STACK_INVALID;
424 break;
427 /* Call handler */
428 TRACE( "calling handler at %p code=%x flags=%x\n",
429 frame->Handler, rec->ExceptionCode, rec->ExceptionFlags );
430 res = frame->Handler( rec, frame, context, &dispatch );
431 TRACE( "handler at %p returned %x\n", frame->Handler, res );
433 if (frame == nested_frame)
435 /* no longer nested */
436 nested_frame = NULL;
437 rec->ExceptionFlags &= ~EH_NESTED_CALL;
440 switch(res)
442 case ExceptionContinueExecution:
443 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return STATUS_SUCCESS;
444 return STATUS_NONCONTINUABLE_EXCEPTION;
445 case ExceptionContinueSearch:
446 break;
447 case ExceptionNestedException:
448 if (nested_frame < dispatch) nested_frame = dispatch;
449 rec->ExceptionFlags |= EH_NESTED_CALL;
450 break;
451 default:
452 return STATUS_INVALID_DISPOSITION;
454 frame = frame->Prev;
456 return STATUS_UNHANDLED_EXCEPTION;
460 /*******************************************************************
461 * raise_exception
463 * Implementation of NtRaiseException.
465 static NTSTATUS raise_exception( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
467 NTSTATUS status;
469 if (first_chance)
471 DWORD c;
473 for (c = 0; c < rec->NumberParameters; c++)
474 TRACE( " info[%d]=%016lx\n", c, rec->ExceptionInformation[c] );
475 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
477 if (rec->ExceptionInformation[1] >> 16)
478 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
479 rec->ExceptionAddress,
480 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
481 else
482 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
483 rec->ExceptionAddress,
484 (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
486 else
488 /* FIXME: dump context */
491 status = send_debug_event( rec, TRUE, context );
492 if (status == DBG_CONTINUE || status == DBG_EXCEPTION_HANDLED)
493 return STATUS_SUCCESS;
495 if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION)
496 return STATUS_SUCCESS;
498 if ((status = call_stack_handlers( rec, context )) != STATUS_UNHANDLED_EXCEPTION)
499 return status;
502 /* last chance exception */
504 status = send_debug_event( rec, FALSE, context );
505 if (status != DBG_CONTINUE)
507 if (rec->ExceptionFlags & EH_STACK_INVALID)
508 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
509 else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
510 ERR("Process attempted to continue execution after noncontinuable exception.\n");
511 else
512 ERR("Unhandled exception code %x flags %x addr %p\n",
513 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
514 NtTerminateProcess( NtCurrentProcess(), rec->ExceptionCode );
516 return STATUS_SUCCESS;
519 /**********************************************************************
520 * segv_handler
522 * Handler for SIGSEGV and related errors.
524 static void segv_handler( int signal, siginfo_t *info, void *ucontext )
526 EXCEPTION_RECORD *rec;
527 ucontext_t *context = ucontext;
529 /* check for page fault inside the thread stack */
530 if (signal == SIGSEGV &&
531 (char *)info->si_addr >= (char *)NtCurrentTeb()->DeallocationStack &&
532 (char *)info->si_addr < (char *)NtCurrentTeb()->Tib.StackBase &&
533 virtual_handle_stack_fault( info->si_addr ))
535 /* check if this was the last guard page */
536 if ((char *)info->si_addr < (char *)NtCurrentTeb()->DeallocationStack + 2*4096)
538 rec = setup_exception( context, raise_segv_exception );
539 rec->ExceptionCode = EXCEPTION_STACK_OVERFLOW;
541 return;
544 rec = setup_exception( context, raise_segv_exception );
545 if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) return;
547 switch(signal)
549 case SIGILL: /* Invalid opcode exception */
550 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
551 break;
552 case SIGSEGV: /* Segmentation fault */
553 rec->ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
554 rec->NumberParameters = 2;
555 /* FIXME: Currently the kernel provides no way to determine if it's read or write */
556 rec->ExceptionInformation[0] = 0;
557 rec->ExceptionInformation[1] = (ULONG_PTR)info->si_addr;
558 break;
559 case SIGBUS: /* Alignment check exception */
560 rec->ExceptionCode = EXCEPTION_DATATYPE_MISALIGNMENT;
561 break;
562 default:
563 ERR("Got unexpected signal %i\n", signal);
564 rec->ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
565 break;
569 /**********************************************************************
570 * trap_handler
572 * Handler for SIGTRAP.
574 static void trap_handler( int signal, siginfo_t *info, void *ucontext )
576 EXCEPTION_RECORD rec;
577 CONTEXT context;
578 NTSTATUS status;
580 switch ( info->si_code )
582 case TRAP_TRACE:
583 rec.ExceptionCode = EXCEPTION_SINGLE_STEP;
584 break;
585 case TRAP_BRKPT:
586 default:
587 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
588 break;
591 save_context( &context, ucontext );
592 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
593 rec.ExceptionRecord = NULL;
594 rec.ExceptionAddress = (LPVOID)context.Pc;
595 rec.NumberParameters = 0;
596 status = raise_exception( &rec, &context, TRUE );
597 if (status) raise_status( status, &rec );
598 restore_context( &context, ucontext );
601 /**********************************************************************
602 * fpe_handler
604 * Handler for SIGFPE.
606 static void fpe_handler( int signal, siginfo_t *siginfo, void *sigcontext )
608 EXCEPTION_RECORD rec;
609 CONTEXT context;
610 NTSTATUS status;
612 save_fpu( &context, sigcontext );
613 save_context( &context, sigcontext );
615 switch (siginfo->si_code & 0xffff )
617 #ifdef FPE_FLTSUB
618 case FPE_FLTSUB:
619 rec.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
620 break;
621 #endif
622 #ifdef FPE_INTDIV
623 case FPE_INTDIV:
624 rec.ExceptionCode = EXCEPTION_INT_DIVIDE_BY_ZERO;
625 break;
626 #endif
627 #ifdef FPE_INTOVF
628 case FPE_INTOVF:
629 rec.ExceptionCode = EXCEPTION_INT_OVERFLOW;
630 break;
631 #endif
632 #ifdef FPE_FLTDIV
633 case FPE_FLTDIV:
634 rec.ExceptionCode = EXCEPTION_FLT_DIVIDE_BY_ZERO;
635 break;
636 #endif
637 #ifdef FPE_FLTOVF
638 case FPE_FLTOVF:
639 rec.ExceptionCode = EXCEPTION_FLT_OVERFLOW;
640 break;
641 #endif
642 #ifdef FPE_FLTUND
643 case FPE_FLTUND:
644 rec.ExceptionCode = EXCEPTION_FLT_UNDERFLOW;
645 break;
646 #endif
647 #ifdef FPE_FLTRES
648 case FPE_FLTRES:
649 rec.ExceptionCode = EXCEPTION_FLT_INEXACT_RESULT;
650 break;
651 #endif
652 #ifdef FPE_FLTINV
653 case FPE_FLTINV:
654 #endif
655 default:
656 rec.ExceptionCode = EXCEPTION_FLT_INVALID_OPERATION;
657 break;
659 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
660 rec.ExceptionRecord = NULL;
661 rec.ExceptionAddress = (LPVOID)context.Pc;
662 rec.NumberParameters = 0;
663 status = raise_exception( &rec, &context, TRUE );
664 if (status) raise_status( status, &rec );
666 restore_context( &context, sigcontext );
667 restore_fpu( &context, sigcontext );
670 /**********************************************************************
671 * int_handler
673 * Handler for SIGINT.
675 static void int_handler( int signal, siginfo_t *siginfo, void *sigcontext )
677 if (!dispatch_signal(SIGINT))
679 EXCEPTION_RECORD rec;
680 CONTEXT context;
681 NTSTATUS status;
683 save_context( &context, sigcontext );
684 rec.ExceptionCode = CONTROL_C_EXIT;
685 rec.ExceptionFlags = EXCEPTION_CONTINUABLE;
686 rec.ExceptionRecord = NULL;
687 rec.ExceptionAddress = (LPVOID)context.Pc;
688 rec.NumberParameters = 0;
689 status = raise_exception( &rec, &context, TRUE );
690 if (status) raise_status( status, &rec );
691 restore_context( &context, sigcontext );
696 /**********************************************************************
697 * abrt_handler
699 * Handler for SIGABRT.
701 static void abrt_handler( int signal, siginfo_t *siginfo, void *sigcontext )
703 EXCEPTION_RECORD rec;
704 CONTEXT context;
705 NTSTATUS status;
707 save_context( &context, sigcontext );
708 rec.ExceptionCode = EXCEPTION_WINE_ASSERTION;
709 rec.ExceptionFlags = EH_NONCONTINUABLE;
710 rec.ExceptionRecord = NULL;
711 rec.ExceptionAddress = (LPVOID)context.Pc;
712 rec.NumberParameters = 0;
713 status = raise_exception( &rec, &context, TRUE );
714 if (status) raise_status( status, &rec );
715 restore_context( &context, sigcontext );
719 /**********************************************************************
720 * quit_handler
722 * Handler for SIGQUIT.
724 static void quit_handler( int signal, siginfo_t *siginfo, void *sigcontext )
726 abort_thread(0);
730 /**********************************************************************
731 * usr1_handler
733 * Handler for SIGUSR1, used to signal a thread that it got suspended.
735 static void usr1_handler( int signal, siginfo_t *siginfo, void *sigcontext )
737 CONTEXT context;
739 save_context( &context, sigcontext );
740 wait_suspend( &context );
741 restore_context( &context, sigcontext );
745 /***********************************************************************
746 * __wine_set_signal_handler (NTDLL.@)
748 int CDECL __wine_set_signal_handler(unsigned int sig, wine_signal_handler wsh)
750 if (sig >= sizeof(handlers) / sizeof(handlers[0])) return -1;
751 if (handlers[sig] != NULL) return -2;
752 handlers[sig] = wsh;
753 return 0;
757 /**********************************************************************
758 * signal_alloc_thread
760 NTSTATUS signal_alloc_thread( TEB **teb )
762 static size_t sigstack_zero_bits;
763 SIZE_T size;
764 NTSTATUS status;
766 if (!sigstack_zero_bits)
768 size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
769 /* find the first power of two not smaller than min_size */
770 sigstack_zero_bits = 12;
771 while ((1u << sigstack_zero_bits) < min_size) sigstack_zero_bits++;
772 signal_stack_size = (1 << sigstack_zero_bits) - teb_size;
773 assert( sizeof(TEB) <= teb_size );
776 size = 1 << sigstack_zero_bits;
777 *teb = NULL;
778 if (!(status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb, sigstack_zero_bits,
779 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
781 (*teb)->Tib.Self = &(*teb)->Tib;
782 (*teb)->Tib.ExceptionList = (void *)~0UL;
784 return status;
788 /**********************************************************************
789 * signal_free_thread
791 void signal_free_thread( TEB *teb )
793 SIZE_T size;
795 if (teb->DeallocationStack)
797 size = 0;
798 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
800 size = 0;
801 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
805 /**********************************************************************
806 * signal_init_thread
808 void signal_init_thread( TEB *teb )
810 static BOOL init_done;
812 if (!init_done)
814 pthread_key_create( &teb_key, NULL );
815 init_done = TRUE;
818 /* Win64/ARM applications expect the TEB pointer to be in the x18 platform register. */
819 __asm__ __volatile__( "mov x18, %0" : : "r" (teb) );
821 pthread_setspecific( teb_key, teb );
825 /**********************************************************************
826 * signal_init_process
828 void signal_init_process(void)
830 struct sigaction sig_act;
832 sig_act.sa_mask = server_block_set;
833 sig_act.sa_flags = SA_RESTART | SA_SIGINFO;
835 sig_act.sa_sigaction = int_handler;
836 if (sigaction( SIGINT, &sig_act, NULL ) == -1) goto error;
837 sig_act.sa_sigaction = fpe_handler;
838 if (sigaction( SIGFPE, &sig_act, NULL ) == -1) goto error;
839 sig_act.sa_sigaction = abrt_handler;
840 if (sigaction( SIGABRT, &sig_act, NULL ) == -1) goto error;
841 sig_act.sa_sigaction = quit_handler;
842 if (sigaction( SIGQUIT, &sig_act, NULL ) == -1) goto error;
843 sig_act.sa_sigaction = usr1_handler;
844 if (sigaction( SIGUSR1, &sig_act, NULL ) == -1) goto error;
846 sig_act.sa_sigaction = segv_handler;
847 if (sigaction( SIGSEGV, &sig_act, NULL ) == -1) goto error;
848 if (sigaction( SIGILL, &sig_act, NULL ) == -1) goto error;
849 #ifdef SIGBUS
850 if (sigaction( SIGBUS, &sig_act, NULL ) == -1) goto error;
851 #endif
853 #ifdef SIGTRAP
854 sig_act.sa_sigaction = trap_handler;
855 if (sigaction( SIGTRAP, &sig_act, NULL ) == -1) goto error;
856 #endif
857 return;
859 error:
860 perror("sigaction");
861 exit(1);
865 /**********************************************************************
866 * __wine_enter_vm86 (NTDLL.@)
868 void __wine_enter_vm86( CONTEXT *context )
870 MESSAGE("vm86 mode not supported on this platform\n");
873 /***********************************************************************
874 * RtlUnwind (NTDLL.@)
876 void WINAPI RtlUnwind( PVOID pEndFrame, PVOID targetIp, PEXCEPTION_RECORD pRecord, PVOID retval )
878 FIXME( "Not implemented on ARM64\n" );
881 /*******************************************************************
882 * NtRaiseException (NTDLL.@)
884 NTSTATUS WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context, BOOL first_chance )
886 NTSTATUS status = raise_exception( rec, context, first_chance );
887 if (status == STATUS_SUCCESS) NtSetContextThread( GetCurrentThread(), context );
888 return status;
891 /***********************************************************************
892 * RtlRaiseException (NTDLL.@)
894 void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
896 CONTEXT context;
897 NTSTATUS status;
899 RtlCaptureContext( &context );
900 rec->ExceptionAddress = (LPVOID)context.Pc;
901 status = raise_exception( rec, &context, TRUE );
902 if (status) raise_status( status, rec );
905 /*************************************************************************
906 * RtlCaptureStackBackTrace (NTDLL.@)
908 USHORT WINAPI RtlCaptureStackBackTrace( ULONG skip, ULONG count, PVOID *buffer, ULONG *hash )
910 FIXME( "(%d, %d, %p, %p) stub!\n", skip, count, buffer, hash );
911 return 0;
914 /***********************************************************************
915 * call_thread_entry_point
917 void call_thread_entry_point( LPTHREAD_START_ROUTINE entry, void *arg )
919 __TRY
921 exit_thread( entry( arg ));
923 __EXCEPT(unhandled_exception_filter)
925 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
927 __ENDTRY
928 abort(); /* should not be reached */
931 /***********************************************************************
932 * RtlExitUserThread (NTDLL.@)
934 void WINAPI RtlExitUserThread( ULONG status )
936 exit_thread( status );
939 /***********************************************************************
940 * abort_thread
942 void abort_thread( int status )
944 terminate_thread( status );
947 /**********************************************************************
948 * DbgBreakPoint (NTDLL.@)
950 void WINAPI DbgBreakPoint(void)
952 kill(getpid(), SIGTRAP);
955 /**********************************************************************
956 * DbgUserBreakPoint (NTDLL.@)
958 void WINAPI DbgUserBreakPoint(void)
960 kill(getpid(), SIGTRAP);
963 /**********************************************************************
964 * NtCurrentTeb (NTDLL.@)
966 TEB * WINAPI NtCurrentTeb(void)
968 return pthread_getspecific( teb_key );
971 #endif /* __aarch64__ */