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
24 #include "wine/port.h"
34 #ifdef HAVE_SYS_PARAM_H
35 # include <sys/param.h>
40 # ifdef HAVE_SYS_SYSCALL_H
41 # include <sys/syscall.h>
44 #ifdef HAVE_SYS_SIGNAL_H
45 # include <sys/signal.h>
47 #ifdef HAVE_SYS_UCONTEXT_H
48 # include <sys/ucontext.h>
52 #define WIN32_NO_STATUS
55 #include "wine/library.h"
56 #include "wine/exception.h"
57 #include "ntdll_misc.h"
58 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
62 WINE_DECLARE_DEBUG_CHANNEL(relay
);
64 static pthread_key_t teb_key
;
66 /***********************************************************************
67 * signal context platform-specific definitions
71 /* All Registers access - only for local access */
72 # define REG_sig(reg_name, context) ((context)->uc_mcontext.reg_name)
73 # define REGn_sig(reg_num, context) ((context)->uc_mcontext.regs[reg_num])
75 /* Special Registers access */
76 # define SP_sig(context) REG_sig(sp, context) /* Stack pointer */
77 # define PC_sig(context) REG_sig(pc, context) /* Program counter */
78 # define PSTATE_sig(context) REG_sig(pstate, context) /* Current State Register */
79 # define FP_sig(context) REGn_sig(29, context) /* Frame pointer */
80 # define LR_sig(context) REGn_sig(30, context) /* Link Register */
83 # define FAULT_sig(context) REG_sig(fault_address, context)
87 static const size_t teb_size
= 0x2000; /* we reserve two pages for the TEB */
88 static size_t signal_stack_size
;
90 typedef void (WINAPI
*raise_func
)( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
91 typedef int (*wine_signal_handler
)(unsigned int sig
);
93 static wine_signal_handler handlers
[256];
95 /***********************************************************************
98 static inline int dispatch_signal(unsigned int sig
)
100 if (handlers
[sig
] == NULL
) return 0;
101 return handlers
[sig
](sig
);
104 /*******************************************************************
107 static inline BOOL
is_valid_frame( void *frame
)
109 if ((ULONG_PTR
)frame
& 3) return FALSE
;
110 return (frame
>= NtCurrentTeb()->Tib
.StackLimit
&&
111 (void **)frame
< (void **)NtCurrentTeb()->Tib
.StackBase
- 1);
114 /***********************************************************************
117 * Set the register values from a sigcontext.
119 static void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
)
121 #define C(n) context->X##n = REGn_sig(n,sigcontext)
122 /* Save normal registers */
123 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9);
124 C(10); C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19);
125 C(20); C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28);
128 context
->ContextFlags
= CONTEXT_FULL
;
129 context
->Fp
= FP_sig(sigcontext
); /* Frame pointer */
130 context
->Lr
= LR_sig(sigcontext
); /* Link register */
131 context
->Sp
= SP_sig(sigcontext
); /* Stack pointer */
132 context
->Pc
= PC_sig(sigcontext
); /* Program Counter */
133 context
->Cpsr
= PSTATE_sig(sigcontext
); /* Current State Register */
137 /***********************************************************************
140 * Build a sigcontext from the register values.
142 static void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
144 #define C(n) REGn_sig(n,sigcontext) = context->X##n
145 /* Restore normal registers */
146 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9);
147 C(10); C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19);
148 C(20); C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28);
151 FP_sig(sigcontext
) = context
->Fp
; /* Frame pointer */
152 LR_sig(sigcontext
) = context
->Lr
; /* Link register */
153 SP_sig(sigcontext
) = context
->Sp
; /* Stack pointer */
154 PC_sig(sigcontext
) = context
->Pc
; /* Program Counter */
155 PSTATE_sig(sigcontext
) = context
->Cpsr
; /* Current State Register */
159 /***********************************************************************
162 * Set the FPU context from a sigcontext.
164 static inline void save_fpu( CONTEXT
*context
, const ucontext_t
*sigcontext
)
166 FIXME( "Not implemented on ARM64\n" );
170 /***********************************************************************
173 * Restore the FPU context to a sigcontext.
175 static inline void restore_fpu( CONTEXT
*context
, const ucontext_t
*sigcontext
)
177 FIXME( "Not implemented on ARM64\n" );
180 /***********************************************************************
181 * RtlCaptureContext (NTDLL.@)
183 /* FIXME: Use the Stack instead of the actual register values? */
184 __ASM_STDCALL_FUNC( RtlCaptureContext
, 8,
185 "stp x0, x1, [sp, #-32]!\n\t"
186 "mov w1, #0x400000\n\t" /* CONTEXT_ARM64 */
187 "add w1, w1, #0x3\n\t" /* CONTEXT_FULL */
188 "str w1, [x0]\n\t" /* context->ContextFlags */ /* 32-bit, look at cpsr */
190 "str w1, [x0, #0x4]\n\t" /* context->Cpsr */
191 "ldp x0, x1, [sp], #32\n\t"
192 "str x0, [x0, #0x8]\n\t" /* context->X0 */
193 "str x1, [x0, #0x10]\n\t" /* context->X1 */
194 "str x2, [x0, #0x18]\n\t" /* context->X2 */
195 "str x3, [x0, #0x20]\n\t" /* context->X3 */
196 "str x4, [x0, #0x28]\n\t" /* context->X4 */
197 "str x5, [x0, #0x30]\n\t" /* context->X5 */
198 "str x6, [x0, #0x38]\n\t" /* context->X6 */
199 "str x7, [x0, #0x40]\n\t" /* context->X7 */
200 "str x8, [x0, #0x48]\n\t" /* context->X8 */
201 "str x9, [x0, #0x50]\n\t" /* context->X9 */
202 "str x10, [x0, #0x58]\n\t" /* context->X10 */
203 "str x11, [x0, #0x60]\n\t" /* context->X11 */
204 "str x12, [x0, #0x68]\n\t" /* context->X12 */
205 "str x13, [x0, #0x70]\n\t" /* context->X13 */
206 "str x14, [x0, #0x78]\n\t" /* context->X14 */
207 "str x15, [x0, #0x80]\n\t" /* context->X15 */
208 "str x16, [x0, #0x88]\n\t" /* context->X16 */
209 "str x17, [x0, #0x90]\n\t" /* context->X17 */
210 "str x18, [x0, #0x98]\n\t" /* context->X18 */
211 "str x19, [x0, #0xa0]\n\t" /* context->X19 */
212 "str x20, [x0, #0xa8]\n\t" /* context->X20 */
213 "str x21, [x0, #0xb0]\n\t" /* context->X21 */
214 "str x22, [x0, #0xb8]\n\t" /* context->X22 */
215 "str x23, [x0, #0xc0]\n\t" /* context->X23 */
216 "str x24, [x0, #0xc8]\n\t" /* context->X24 */
217 "str x25, [x0, #0xd0]\n\t" /* context->X25 */
218 "str x26, [x0, #0xd8]\n\t" /* context->X26 */
219 "str x27, [x0, #0xe0]\n\t" /* context->X27 */
220 "str x28, [x0, #0xe8]\n\t" /* context->X28 */
221 "str x29, [x0, #0xf0]\n\t" /* context->Fp */
222 "str x30, [x0, #0xf8]\n\t" /* context->Lr */
224 "str x1, [x0, #0x100]\n\t" /* context->Sp */
226 "1: str x1, [x0, #0x108]\n\t" /* context->Pc */
230 /***********************************************************************
233 * Set the new CPU context.
235 static void set_cpu_context( const CONTEXT
*context
)
237 FIXME( "Not implemented on ARM64\n" );
240 /***********************************************************************
243 * Copy a register context according to the flags.
245 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
247 flags
&= ~CONTEXT_ARM64
; /* get rid of CPU id */
248 if (flags
& CONTEXT_CONTROL
)
254 to
->Cpsr
= from
->Cpsr
;
256 if (flags
& CONTEXT_INTEGER
)
258 #define C(n) to->X##n = from->X##n
259 /* Restore normal registers */
260 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9);
261 C(10); C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19);
262 C(20); C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28);
267 /***********************************************************************
270 * Convert a register context to the server format.
272 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
274 DWORD flags
= from
->ContextFlags
& ~CONTEXT_ARM64
; /* get rid of CPU id */
276 memset( to
, 0, sizeof(*to
) );
279 if (flags
& CONTEXT_CONTROL
)
281 to
->flags
|= SERVER_CTX_CONTROL
;
282 to
->integer
.arm64_regs
.x
[29] = from
->Fp
;
283 to
->integer
.arm64_regs
.x
[30] = from
->Lr
;
284 to
->ctl
.arm64_regs
.sp
= from
->Sp
;
285 to
->ctl
.arm64_regs
.pc
= from
->Pc
;
286 to
->ctl
.arm64_regs
.pstate
= from
->Cpsr
;
288 if (flags
& CONTEXT_INTEGER
)
290 to
->flags
|= SERVER_CTX_INTEGER
;
291 #define C(n) to->integer.arm64_regs.x[n] = from->X##n
292 /* Restore normal registers */
293 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9);
294 C(10); C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19);
295 C(20); C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28);
298 return STATUS_SUCCESS
;
302 /***********************************************************************
303 * context_from_server
305 * Convert a register context from the server format.
307 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
309 if (from
->cpu
!= CPU_ARM64
) return STATUS_INVALID_PARAMETER
;
311 to
->ContextFlags
= CONTEXT_ARM64
;
312 if (from
->flags
& SERVER_CTX_CONTROL
)
314 to
->ContextFlags
|= CONTEXT_CONTROL
;
315 to
->Fp
= from
->integer
.arm64_regs
.x
[29];
316 to
->Lr
= from
->integer
.arm64_regs
.x
[30];
317 to
->Sp
= from
->ctl
.arm64_regs
.sp
;
318 to
->Pc
= from
->ctl
.arm64_regs
.pc
;
319 to
->Cpsr
= from
->ctl
.arm64_regs
.pstate
;
321 if (from
->flags
& SERVER_CTX_INTEGER
)
323 to
->ContextFlags
|= CONTEXT_INTEGER
;
324 #define C(n) to->X##n = from->integer.arm64_regs.x[n]
325 /* Restore normal registers */
326 C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9);
327 C(10); C(11); C(12); C(13); C(14); C(15); C(16); C(17); C(18); C(19);
328 C(20); C(21); C(22); C(23); C(24); C(25); C(26); C(27); C(28);
331 return STATUS_SUCCESS
;
334 /***********************************************************************
335 * NtSetContextThread (NTDLL.@)
336 * ZwSetContextThread (NTDLL.@)
338 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
343 ret
= set_thread_context( handle
, context
, &self
);
344 if (self
&& ret
== STATUS_SUCCESS
) set_cpu_context( context
);
349 /***********************************************************************
350 * NtGetContextThread (NTDLL.@)
351 * ZwGetContextThread (NTDLL.@)
353 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
356 DWORD needed_flags
= context
->ContextFlags
;
357 BOOL self
= (handle
== GetCurrentThread());
361 if ((ret
= get_thread_context( handle
, context
, &self
))) return ret
;
362 needed_flags
&= ~context
->ContextFlags
;
365 if (self
&& needed_flags
)
368 RtlCaptureContext( &ctx
);
369 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
370 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
372 return STATUS_SUCCESS
;
376 /***********************************************************************
377 * setup_exception_record
379 * Setup the exception record and context on the thread stack.
381 static EXCEPTION_RECORD
*setup_exception( ucontext_t
*sigcontext
, raise_func func
)
386 EXCEPTION_RECORD rec
;
388 DWORD exception_code
= 0;
390 /* push the stack_layout structure */
391 stack
= (struct stack_layout
*)((SP_sig(sigcontext
) - sizeof(*stack
)) & ~15);
393 stack
->rec
.ExceptionRecord
= NULL
;
394 stack
->rec
.ExceptionCode
= exception_code
;
395 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
396 stack
->rec
.ExceptionAddress
= (LPVOID
)PC_sig(sigcontext
);
397 stack
->rec
.NumberParameters
= 0;
399 save_context( &stack
->context
, sigcontext
);
401 /* now modify the sigcontext to return to the raise function */
402 SP_sig(sigcontext
) = (ULONG_PTR
)stack
;
403 PC_sig(sigcontext
) = (ULONG_PTR
)func
;
404 REGn_sig(0, sigcontext
) = (ULONG_PTR
)&stack
->rec
; /* first arg for raise_func */
405 REGn_sig(1, sigcontext
) = (ULONG_PTR
)&stack
->context
; /* second arg for raise_func */
410 /**********************************************************************
411 * raise_segv_exception
413 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
417 switch(rec
->ExceptionCode
)
419 case EXCEPTION_ACCESS_VIOLATION
:
420 if (rec
->NumberParameters
== 2)
422 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
423 rec
->ExceptionInformation
[0], FALSE
)))
428 status
= NtRaiseException( rec
, context
, TRUE
);
429 if (status
) raise_status( status
, rec
);
431 set_cpu_context( context
);
434 /**********************************************************************
435 * call_stack_handlers
437 * Call the stack handlers chain.
439 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
441 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
444 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
446 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
448 /* Check frame address */
449 if (!is_valid_frame( frame
))
451 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
456 TRACE( "calling handler at %p code=%x flags=%x\n",
457 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
458 res
= frame
->Handler( rec
, frame
, context
, &dispatch
);
459 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
461 if (frame
== nested_frame
)
463 /* no longer nested */
465 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
470 case ExceptionContinueExecution
:
471 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
472 return STATUS_NONCONTINUABLE_EXCEPTION
;
473 case ExceptionContinueSearch
:
475 case ExceptionNestedException
:
476 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
477 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
480 return STATUS_INVALID_DISPOSITION
;
484 return STATUS_UNHANDLED_EXCEPTION
;
488 /*******************************************************************
491 * Implementation of NtRaiseException.
493 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
501 for (c
= 0; c
< rec
->NumberParameters
; c
++)
502 TRACE( " info[%d]=%016lx\n", c
, rec
->ExceptionInformation
[c
] );
503 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
505 if (rec
->ExceptionInformation
[1] >> 16)
506 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
507 rec
->ExceptionAddress
,
508 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
510 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
511 rec
->ExceptionAddress
,
512 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
516 /* FIXME: dump context */
519 status
= send_debug_event( rec
, TRUE
, context
);
520 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
521 return STATUS_SUCCESS
;
523 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
524 return STATUS_SUCCESS
;
526 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
530 /* last chance exception */
532 status
= send_debug_event( rec
, FALSE
, context
);
533 if (status
!= DBG_CONTINUE
)
535 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
536 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
537 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
538 ERR("Process attempted to continue execution after noncontinuable exception.\n");
540 ERR("Unhandled exception code %x flags %x addr %p\n",
541 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
542 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
544 return STATUS_SUCCESS
;
547 /**********************************************************************
550 * Handler for SIGSEGV and related errors.
552 static void segv_handler( int signal
, siginfo_t
*info
, void *ucontext
)
554 EXCEPTION_RECORD
*rec
;
555 ucontext_t
*context
= ucontext
;
557 /* check for page fault inside the thread stack */
558 if (signal
== SIGSEGV
&&
559 (char *)info
->si_addr
>= (char *)NtCurrentTeb()->DeallocationStack
&&
560 (char *)info
->si_addr
< (char *)NtCurrentTeb()->Tib
.StackBase
&&
561 virtual_handle_stack_fault( info
->si_addr
))
563 /* check if this was the last guard page */
564 if ((char *)info
->si_addr
< (char *)NtCurrentTeb()->DeallocationStack
+ 2*4096)
566 rec
= setup_exception( context
, raise_segv_exception
);
567 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
572 rec
= setup_exception( context
, raise_segv_exception
);
573 if (rec
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) return;
577 case SIGILL
: /* Invalid opcode exception */
578 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
580 case SIGSEGV
: /* Segmentation fault */
581 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
582 rec
->NumberParameters
= 2;
583 /* FIXME: Currently the kernel provides no way to determine if it's read or write */
584 rec
->ExceptionInformation
[0] = 0;
585 rec
->ExceptionInformation
[1] = (ULONG_PTR
)info
->si_addr
;
587 case SIGBUS
: /* Alignment check exception */
588 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
591 ERR("Got unexpected signal %i\n", signal
);
592 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
597 /**********************************************************************
600 * Handler for SIGTRAP.
602 static void trap_handler( int signal
, siginfo_t
*info
, void *ucontext
)
604 EXCEPTION_RECORD rec
;
608 switch ( info
->si_code
)
611 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
615 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
619 save_context( &context
, ucontext
);
620 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
621 rec
.ExceptionRecord
= NULL
;
622 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
623 rec
.NumberParameters
= 0;
624 status
= raise_exception( &rec
, &context
, TRUE
);
625 if (status
) raise_status( status
, &rec
);
626 restore_context( &context
, ucontext
);
629 /**********************************************************************
632 * Handler for SIGFPE.
634 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
636 EXCEPTION_RECORD rec
;
640 save_fpu( &context
, sigcontext
);
641 save_context( &context
, sigcontext
);
643 switch (siginfo
->si_code
& 0xffff )
647 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
652 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
657 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
662 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
667 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
672 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
677 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
684 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
687 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
688 rec
.ExceptionRecord
= NULL
;
689 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
690 rec
.NumberParameters
= 0;
691 status
= raise_exception( &rec
, &context
, TRUE
);
692 if (status
) raise_status( status
, &rec
);
694 restore_context( &context
, sigcontext
);
695 restore_fpu( &context
, sigcontext
);
698 /**********************************************************************
701 * Handler for SIGINT.
703 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
705 if (!dispatch_signal(SIGINT
))
707 EXCEPTION_RECORD rec
;
711 save_context( &context
, sigcontext
);
712 rec
.ExceptionCode
= CONTROL_C_EXIT
;
713 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
714 rec
.ExceptionRecord
= NULL
;
715 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
716 rec
.NumberParameters
= 0;
717 status
= raise_exception( &rec
, &context
, TRUE
);
718 if (status
) raise_status( status
, &rec
);
719 restore_context( &context
, sigcontext
);
724 /**********************************************************************
727 * Handler for SIGABRT.
729 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
731 EXCEPTION_RECORD rec
;
735 save_context( &context
, sigcontext
);
736 rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
737 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
738 rec
.ExceptionRecord
= NULL
;
739 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
740 rec
.NumberParameters
= 0;
741 status
= raise_exception( &rec
, &context
, TRUE
);
742 if (status
) raise_status( status
, &rec
);
743 restore_context( &context
, sigcontext
);
747 /**********************************************************************
750 * Handler for SIGQUIT.
752 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
758 /**********************************************************************
761 * Handler for SIGUSR1, used to signal a thread that it got suspended.
763 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
767 save_context( &context
, sigcontext
);
768 wait_suspend( &context
);
769 restore_context( &context
, sigcontext
);
773 /***********************************************************************
774 * __wine_set_signal_handler (NTDLL.@)
776 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
778 if (sig
>= sizeof(handlers
) / sizeof(handlers
[0])) return -1;
779 if (handlers
[sig
] != NULL
) return -2;
785 /**********************************************************************
786 * signal_alloc_thread
788 NTSTATUS
signal_alloc_thread( TEB
**teb
)
790 static size_t sigstack_zero_bits
;
794 if (!sigstack_zero_bits
)
796 size_t min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
797 /* find the first power of two not smaller than min_size */
798 sigstack_zero_bits
= 12;
799 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
800 signal_stack_size
= (1 << sigstack_zero_bits
) - teb_size
;
801 assert( sizeof(TEB
) <= teb_size
);
804 size
= 1 << sigstack_zero_bits
;
806 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb
, sigstack_zero_bits
,
807 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
809 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
810 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
816 /**********************************************************************
819 void signal_free_thread( TEB
*teb
)
823 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
827 /**********************************************************************
830 void signal_init_thread( TEB
*teb
)
832 static BOOL init_done
;
836 pthread_key_create( &teb_key
, NULL
);
840 /* Win64/ARM applications expect the TEB pointer to be in the x18 platform register. */
841 __asm__
__volatile__( "mov x18, %0" : : "r" (teb
) );
843 pthread_setspecific( teb_key
, teb
);
847 /**********************************************************************
848 * signal_init_process
850 void signal_init_process(void)
852 struct sigaction sig_act
;
854 sig_act
.sa_mask
= server_block_set
;
855 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
;
857 sig_act
.sa_sigaction
= int_handler
;
858 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
859 sig_act
.sa_sigaction
= fpe_handler
;
860 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
861 sig_act
.sa_sigaction
= abrt_handler
;
862 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
863 sig_act
.sa_sigaction
= quit_handler
;
864 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
865 sig_act
.sa_sigaction
= usr1_handler
;
866 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
868 sig_act
.sa_sigaction
= segv_handler
;
869 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
870 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
872 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
876 sig_act
.sa_sigaction
= trap_handler
;
877 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
887 /**********************************************************************
888 * __wine_enter_vm86 (NTDLL.@)
890 void __wine_enter_vm86( CONTEXT
*context
)
892 MESSAGE("vm86 mode not supported on this platform\n");
895 /***********************************************************************
896 * RtlUnwind (NTDLL.@)
898 void WINAPI
RtlUnwind( PVOID pEndFrame
, PVOID targetIp
, PEXCEPTION_RECORD pRecord
, PVOID retval
)
900 FIXME( "Not implemented on ARM64\n" );
903 /*******************************************************************
904 * NtRaiseException (NTDLL.@)
906 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
908 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
909 if (status
== STATUS_SUCCESS
) NtSetContextThread( GetCurrentThread(), context
);
913 /***********************************************************************
914 * RtlRaiseException (NTDLL.@)
916 void WINAPI
RtlRaiseException( EXCEPTION_RECORD
*rec
)
921 RtlCaptureContext( &context
);
922 rec
->ExceptionAddress
= (LPVOID
)context
.Pc
;
923 status
= raise_exception( rec
, &context
, TRUE
);
924 if (status
) raise_status( status
, rec
);
927 /*************************************************************************
928 * RtlCaptureStackBackTrace (NTDLL.@)
930 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
932 FIXME( "(%d, %d, %p, %p) stub!\n", skip
, count
, buffer
, hash
);
936 /***********************************************************************
937 * call_thread_entry_point
939 static void call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
)
943 TRACE_(relay
)( "\1Starting thread proc %p (arg=%p)\n", entry
, arg
);
944 exit_thread( entry( arg
));
946 __EXCEPT(unhandled_exception_filter
)
948 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
951 abort(); /* should not be reached */
956 LPTHREAD_START_ROUTINE entry
;
961 /***********************************************************************
964 static void thread_startup( void *param
)
966 CONTEXT context
= { 0 };
967 struct startup_info
*info
= param
;
969 /* build the initial context */
970 context
.ContextFlags
= CONTEXT_FULL
;
971 context
.X0
= (DWORD_PTR
)info
->entry
;
972 context
.X1
= (DWORD_PTR
)info
->arg
;
973 context
.Sp
= (DWORD_PTR
)NtCurrentTeb()->Tib
.StackBase
;
974 context
.Pc
= (DWORD_PTR
)call_thread_entry_point
;
976 if (info
->suspend
) wait_suspend( &context
);
977 attach_dlls( &context
);
979 call_thread_entry_point( (LPTHREAD_START_ROUTINE
)context
.X0
, (void *)context
.X1
);
982 /***********************************************************************
983 * signal_start_thread
985 * Thread startup sequence:
986 * signal_start_thread()
987 * -> thread_startup()
988 * -> call_thread_entry_point()
990 void signal_start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
)
992 struct startup_info info
= { entry
, arg
, suspend
};
993 wine_switch_to_stack( thread_startup
, &info
, NtCurrentTeb()->Tib
.StackBase
);
996 /**********************************************************************
997 * signal_start_process
999 * Process startup sequence:
1000 * signal_start_process()
1001 * -> thread_startup()
1002 * -> call_thread_entry_point()
1003 * -> kernel32_start_process()
1005 void signal_start_process( LPTHREAD_START_ROUTINE entry
, BOOL suspend
)
1007 struct startup_info info
= { kernel32_start_process
, entry
, suspend
};
1008 wine_switch_to_stack( thread_startup
, &info
, NtCurrentTeb()->Tib
.StackBase
);
1011 /***********************************************************************
1012 * RtlExitUserThread (NTDLL.@)
1014 void WINAPI
RtlExitUserThread( ULONG status
)
1016 exit_thread( status
);
1019 /***********************************************************************
1022 void abort_thread( int status
)
1024 terminate_thread( status
);
1027 /**********************************************************************
1028 * DbgBreakPoint (NTDLL.@)
1030 void WINAPI
DbgBreakPoint(void)
1032 kill(getpid(), SIGTRAP
);
1035 /**********************************************************************
1036 * DbgUserBreakPoint (NTDLL.@)
1038 void WINAPI
DbgUserBreakPoint(void)
1040 kill(getpid(), SIGTRAP
);
1043 /**********************************************************************
1044 * NtCurrentTeb (NTDLL.@)
1046 TEB
* WINAPI
NtCurrentTeb(void)
1048 return pthread_getspecific( teb_key
);
1051 #endif /* __aarch64__ */