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>
51 #define NONAMELESSUNION
52 #define NONAMELESSSTRUCT
54 #define WIN32_NO_STATUS
57 #include "wine/library.h"
58 #include "wine/exception.h"
59 #include "ntdll_misc.h"
60 #include "wine/debug.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
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 */
85 # define FAULT_sig(context) REG_sig(fault_address, context)
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 /***********************************************************************
100 static inline int dispatch_signal(unsigned int sig
)
102 if (handlers
[sig
] == NULL
) return 0;
103 return handlers
[sig
](sig
);
106 /*******************************************************************
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 /***********************************************************************
119 * Set the register values from a sigcontext.
121 static void save_context( CONTEXT
*context
, const ucontext_t
*sigcontext
)
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 /***********************************************************************
138 * Build a sigcontext from the register values.
140 static void restore_context( const CONTEXT
*context
, ucontext_t
*sigcontext
)
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 /***********************************************************************
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 /***********************************************************************
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 */
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 */
218 "str x1, [x0, #0x100]\n\t" /* context->Sp */
220 "1: str x1, [x0, #0x108]\n\t" /* context->Pc */
224 /***********************************************************************
227 * Set the new CPU context.
229 static void set_cpu_context( const CONTEXT
*context
)
231 FIXME( "Not implemented on ARM64\n" );
234 /***********************************************************************
235 * get_server_context_flags
237 * Convert CPU-specific flags to generic server flags
239 static unsigned int get_server_context_flags( DWORD flags
)
241 unsigned int ret
= 0;
243 flags
&= ~CONTEXT_ARM64
; /* get rid of CPU id */
244 if (flags
& CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
245 if (flags
& CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
246 if (flags
& CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
247 if (flags
& CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
251 /***********************************************************************
254 * Copy a register context according to the flags.
256 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
258 flags
&= ~CONTEXT_ARM64
; /* get rid of CPU id */
259 if (flags
& CONTEXT_CONTROL
)
261 to
->u
.s
.Fp
= from
->u
.s
.Fp
;
262 to
->u
.s
.Lr
= from
->u
.s
.Lr
;
265 to
->Cpsr
= from
->Cpsr
;
267 if (flags
& CONTEXT_INTEGER
)
269 memcpy( to
->u
.X
, from
->u
.X
, sizeof(to
->u
.X
) );
271 if (flags
& CONTEXT_FLOATING_POINT
)
273 memcpy( to
->V
, from
->V
, sizeof(to
->V
) );
274 to
->Fpcr
= from
->Fpcr
;
275 to
->Fpsr
= from
->Fpsr
;
277 if (flags
& CONTEXT_DEBUG_REGISTERS
)
279 memcpy( to
->Bcr
, from
->Bcr
, sizeof(to
->Bcr
) );
280 memcpy( to
->Bvr
, from
->Bvr
, sizeof(to
->Bvr
) );
281 memcpy( to
->Wcr
, from
->Wcr
, sizeof(to
->Wcr
) );
282 memcpy( to
->Wvr
, from
->Wvr
, sizeof(to
->Wvr
) );
286 /***********************************************************************
289 * Convert a register context to the server format.
291 NTSTATUS
context_to_server( context_t
*to
, const CONTEXT
*from
)
293 DWORD i
, flags
= from
->ContextFlags
& ~CONTEXT_ARM64
; /* get rid of CPU id */
295 memset( to
, 0, sizeof(*to
) );
298 if (flags
& CONTEXT_CONTROL
)
300 to
->flags
|= SERVER_CTX_CONTROL
;
301 to
->integer
.arm64_regs
.x
[29] = from
->u
.s
.Fp
;
302 to
->integer
.arm64_regs
.x
[30] = from
->u
.s
.Lr
;
303 to
->ctl
.arm64_regs
.sp
= from
->Sp
;
304 to
->ctl
.arm64_regs
.pc
= from
->Pc
;
305 to
->ctl
.arm64_regs
.pstate
= from
->Cpsr
;
307 if (flags
& CONTEXT_INTEGER
)
309 to
->flags
|= SERVER_CTX_INTEGER
;
310 for (i
= 0; i
<= 28; i
++) to
->integer
.arm64_regs
.x
[i
] = from
->u
.X
[i
];
312 if (flags
& CONTEXT_FLOATING_POINT
)
314 to
->flags
|= SERVER_CTX_FLOATING_POINT
;
315 for (i
= 0; i
< 64; i
++) to
->fp
.arm64_regs
.d
[i
] = from
->V
[i
/ 2].D
[i
% 2];
316 to
->fp
.arm64_regs
.fpcr
= from
->Fpcr
;
317 to
->fp
.arm64_regs
.fpsr
= from
->Fpsr
;
319 if (flags
& CONTEXT_DEBUG_REGISTERS
)
321 to
->flags
|= SERVER_CTX_DEBUG_REGISTERS
;
322 for (i
= 0; i
< ARM64_MAX_BREAKPOINTS
; i
++) to
->debug
.arm64_regs
.bcr
[i
] = from
->Bcr
[i
];
323 for (i
= 0; i
< ARM64_MAX_BREAKPOINTS
; i
++) to
->debug
.arm64_regs
.bvr
[i
] = from
->Bvr
[i
];
324 for (i
= 0; i
< ARM64_MAX_WATCHPOINTS
; i
++) to
->debug
.arm64_regs
.wcr
[i
] = from
->Wcr
[i
];
325 for (i
= 0; i
< ARM64_MAX_WATCHPOINTS
; i
++) to
->debug
.arm64_regs
.wvr
[i
] = from
->Wvr
[i
];
327 return STATUS_SUCCESS
;
331 /***********************************************************************
332 * context_from_server
334 * Convert a register context from the server format.
336 NTSTATUS
context_from_server( CONTEXT
*to
, const context_t
*from
)
340 if (from
->cpu
!= CPU_ARM64
) return STATUS_INVALID_PARAMETER
;
342 to
->ContextFlags
= CONTEXT_ARM64
;
343 if (from
->flags
& SERVER_CTX_CONTROL
)
345 to
->ContextFlags
|= CONTEXT_CONTROL
;
346 to
->u
.s
.Fp
= from
->integer
.arm64_regs
.x
[29];
347 to
->u
.s
.Lr
= from
->integer
.arm64_regs
.x
[30];
348 to
->Sp
= from
->ctl
.arm64_regs
.sp
;
349 to
->Pc
= from
->ctl
.arm64_regs
.pc
;
350 to
->Cpsr
= from
->ctl
.arm64_regs
.pstate
;
352 if (from
->flags
& SERVER_CTX_INTEGER
)
354 to
->ContextFlags
|= CONTEXT_INTEGER
;
355 for (i
= 0; i
<= 28; i
++) to
->u
.X
[i
] = from
->integer
.arm64_regs
.x
[i
];
357 if (from
->flags
& SERVER_CTX_FLOATING_POINT
)
359 to
->ContextFlags
|= CONTEXT_FLOATING_POINT
;
360 for (i
= 0; i
< 64; i
++) to
->V
[i
/ 2].D
[i
% 2] = from
->fp
.arm64_regs
.d
[i
];
361 to
->Fpcr
= from
->fp
.arm64_regs
.fpcr
;
362 to
->Fpsr
= from
->fp
.arm64_regs
.fpsr
;
364 if (from
->flags
& SERVER_CTX_DEBUG_REGISTERS
)
366 to
->ContextFlags
|= CONTEXT_DEBUG_REGISTERS
;
367 for (i
= 0; i
< ARM64_MAX_BREAKPOINTS
; i
++) to
->Bcr
[i
] = from
->debug
.arm64_regs
.bcr
[i
];
368 for (i
= 0; i
< ARM64_MAX_BREAKPOINTS
; i
++) to
->Bvr
[i
] = from
->debug
.arm64_regs
.bvr
[i
];
369 for (i
= 0; i
< ARM64_MAX_WATCHPOINTS
; i
++) to
->Wcr
[i
] = from
->debug
.arm64_regs
.wcr
[i
];
370 for (i
= 0; i
< ARM64_MAX_WATCHPOINTS
; i
++) to
->Wvr
[i
] = from
->debug
.arm64_regs
.wvr
[i
];
372 return STATUS_SUCCESS
;
375 /***********************************************************************
376 * NtSetContextThread (NTDLL.@)
377 * ZwSetContextThread (NTDLL.@)
379 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
383 context_t server_context
;
385 context_to_server( &server_context
, context
);
386 ret
= set_thread_context( handle
, &server_context
, &self
);
387 if (self
&& ret
== STATUS_SUCCESS
) set_cpu_context( context
);
392 /***********************************************************************
393 * NtGetContextThread (NTDLL.@)
394 * ZwGetContextThread (NTDLL.@)
396 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
399 DWORD needed_flags
= context
->ContextFlags
;
400 BOOL self
= (handle
== GetCurrentThread());
404 context_t server_context
;
405 unsigned int server_flags
= get_server_context_flags( context
->ContextFlags
);
407 if ((ret
= get_thread_context( handle
, &server_context
, server_flags
, &self
))) return ret
;
408 if ((ret
= context_from_server( context
, &server_context
))) return ret
;
409 needed_flags
&= ~context
->ContextFlags
;
412 if (self
&& needed_flags
)
415 RtlCaptureContext( &ctx
);
416 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
417 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
419 return STATUS_SUCCESS
;
423 /***********************************************************************
424 * setup_exception_record
426 * Setup the exception record and context on the thread stack.
428 static EXCEPTION_RECORD
*setup_exception( ucontext_t
*sigcontext
, raise_func func
)
433 EXCEPTION_RECORD rec
;
435 DWORD exception_code
= 0;
437 /* push the stack_layout structure */
438 stack
= (struct stack_layout
*)((SP_sig(sigcontext
) - sizeof(*stack
)) & ~15);
440 stack
->rec
.ExceptionRecord
= NULL
;
441 stack
->rec
.ExceptionCode
= exception_code
;
442 stack
->rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
443 stack
->rec
.ExceptionAddress
= (LPVOID
)PC_sig(sigcontext
);
444 stack
->rec
.NumberParameters
= 0;
446 save_context( &stack
->context
, sigcontext
);
448 /* now modify the sigcontext to return to the raise function */
449 SP_sig(sigcontext
) = (ULONG_PTR
)stack
;
450 PC_sig(sigcontext
) = (ULONG_PTR
)func
;
451 REGn_sig(0, sigcontext
) = (ULONG_PTR
)&stack
->rec
; /* first arg for raise_func */
452 REGn_sig(1, sigcontext
) = (ULONG_PTR
)&stack
->context
; /* second arg for raise_func */
457 /**********************************************************************
458 * raise_segv_exception
460 static void WINAPI
raise_segv_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
464 switch(rec
->ExceptionCode
)
466 case EXCEPTION_ACCESS_VIOLATION
:
467 if (rec
->NumberParameters
== 2)
469 if (!(rec
->ExceptionCode
= virtual_handle_fault( (void *)rec
->ExceptionInformation
[1],
470 rec
->ExceptionInformation
[0], FALSE
)))
475 status
= NtRaiseException( rec
, context
, TRUE
);
476 if (status
) raise_status( status
, rec
);
478 set_cpu_context( context
);
481 /**********************************************************************
482 * call_stack_handlers
484 * Call the stack handlers chain.
486 static NTSTATUS
call_stack_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
488 EXCEPTION_REGISTRATION_RECORD
*frame
, *dispatch
, *nested_frame
;
491 frame
= NtCurrentTeb()->Tib
.ExceptionList
;
493 while (frame
!= (EXCEPTION_REGISTRATION_RECORD
*)~0UL)
495 /* Check frame address */
496 if (!is_valid_frame( frame
))
498 rec
->ExceptionFlags
|= EH_STACK_INVALID
;
503 TRACE( "calling handler at %p code=%x flags=%x\n",
504 frame
->Handler
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
505 res
= frame
->Handler( rec
, frame
, context
, &dispatch
);
506 TRACE( "handler at %p returned %x\n", frame
->Handler
, res
);
508 if (frame
== nested_frame
)
510 /* no longer nested */
512 rec
->ExceptionFlags
&= ~EH_NESTED_CALL
;
517 case ExceptionContinueExecution
:
518 if (!(rec
->ExceptionFlags
& EH_NONCONTINUABLE
)) return STATUS_SUCCESS
;
519 return STATUS_NONCONTINUABLE_EXCEPTION
;
520 case ExceptionContinueSearch
:
522 case ExceptionNestedException
:
523 if (nested_frame
< dispatch
) nested_frame
= dispatch
;
524 rec
->ExceptionFlags
|= EH_NESTED_CALL
;
527 return STATUS_INVALID_DISPOSITION
;
531 return STATUS_UNHANDLED_EXCEPTION
;
535 /*******************************************************************
538 * Implementation of NtRaiseException.
540 static NTSTATUS
raise_exception( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
548 TRACE( "code=%x flags=%x addr=%p pc=%lx tid=%04x\n",
549 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
550 context
->Pc
, GetCurrentThreadId() );
551 for (c
= 0; c
< rec
->NumberParameters
; c
++)
552 TRACE( " info[%d]=%016lx\n", c
, rec
->ExceptionInformation
[c
] );
553 if (rec
->ExceptionCode
== EXCEPTION_WINE_STUB
)
555 if (rec
->ExceptionInformation
[1] >> 16)
556 MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
557 rec
->ExceptionAddress
,
558 (char*)rec
->ExceptionInformation
[0], (char*)rec
->ExceptionInformation
[1] );
560 MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
561 rec
->ExceptionAddress
,
562 (char*)rec
->ExceptionInformation
[0], rec
->ExceptionInformation
[1] );
566 TRACE(" x0=%016lx x1=%016lx x2=%016lx x3=%016lx\n",
567 context
->u
.s
.X0
, context
->u
.s
.X1
, context
->u
.s
.X2
, context
->u
.s
.X3
);
568 TRACE(" x4=%016lx x5=%016lx x6=%016lx x7=%016lx\n",
569 context
->u
.s
.X4
, context
->u
.s
.X5
, context
->u
.s
.X6
, context
->u
.s
.X7
);
570 TRACE(" x8=%016lx x9=%016lx x10=%016lx x11=%016lx\n",
571 context
->u
.s
.X8
, context
->u
.s
.X9
, context
->u
.s
.X10
, context
->u
.s
.X11
);
572 TRACE(" x12=%016lx x13=%016lx x14=%016lx x15=%016lx\n",
573 context
->u
.s
.X12
, context
->u
.s
.X13
, context
->u
.s
.X14
, context
->u
.s
.X15
);
574 TRACE(" x16=%016lx x17=%016lx x18=%016lx x19=%016lx\n",
575 context
->u
.s
.X16
, context
->u
.s
.X17
, context
->u
.s
.X18
, context
->u
.s
.X19
);
576 TRACE(" x20=%016lx x21=%016lx x22=%016lx x23=%016lx\n",
577 context
->u
.s
.X20
, context
->u
.s
.X21
, context
->u
.s
.X22
, context
->u
.s
.X23
);
578 TRACE(" x24=%016lx x25=%016lx x26=%016lx x27=%016lx\n",
579 context
->u
.s
.X24
, context
->u
.s
.X25
, context
->u
.s
.X26
, context
->u
.s
.X27
);
580 TRACE(" x28=%016lx fp=%016lx lr=%016lx sp=%016lx\n",
581 context
->u
.s
.X28
, context
->u
.s
.Fp
, context
->u
.s
.Lr
, context
->Sp
);
582 TRACE(" pc=%016lx\n",
586 status
= send_debug_event( rec
, TRUE
, context
);
587 if (status
== DBG_CONTINUE
|| status
== DBG_EXCEPTION_HANDLED
)
588 return STATUS_SUCCESS
;
590 if (call_vectored_handlers( rec
, context
) == EXCEPTION_CONTINUE_EXECUTION
)
591 return STATUS_SUCCESS
;
593 if ((status
= call_stack_handlers( rec
, context
)) != STATUS_UNHANDLED_EXCEPTION
)
597 /* last chance exception */
599 status
= send_debug_event( rec
, FALSE
, context
);
600 if (status
!= DBG_CONTINUE
)
602 if (rec
->ExceptionFlags
& EH_STACK_INVALID
)
603 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
604 else if (rec
->ExceptionCode
== STATUS_NONCONTINUABLE_EXCEPTION
)
605 ERR("Process attempted to continue execution after noncontinuable exception.\n");
607 ERR("Unhandled exception code %x flags %x addr %p\n",
608 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
609 NtTerminateProcess( NtCurrentProcess(), rec
->ExceptionCode
);
611 return STATUS_SUCCESS
;
614 static inline DWORD
is_write_fault( ucontext_t
*context
)
616 DWORD inst
= *(DWORD
*)PC_sig(context
);
617 if ((inst
& 0xbfff0000) == 0x0c000000 /* C3.3.1 */ ||
618 (inst
& 0xbfe00000) == 0x0c800000 /* C3.3.2 */ ||
619 (inst
& 0xbfdf0000) == 0x0d000000 /* C3.3.3 */ ||
620 (inst
& 0xbfc00000) == 0x0d800000 /* C3.3.4 */ ||
621 (inst
& 0x3f400000) == 0x08000000 /* C3.3.6 */ ||
622 (inst
& 0x3bc00000) == 0x38000000 /* C3.3.8-12 */ ||
623 (inst
& 0x3fe00000) == 0x3c800000 /* C3.3.8-12 128bit */ ||
624 (inst
& 0x3bc00000) == 0x39000000 /* C3.3.13 */ ||
625 (inst
& 0x3fc00000) == 0x3d800000 /* C3.3.13 128bit */ ||
626 (inst
& 0x3a400000) == 0x28000000) /* C3.3.7,14-16 */
627 return EXCEPTION_WRITE_FAULT
;
628 return EXCEPTION_READ_FAULT
;
631 /**********************************************************************
634 * Handler for SIGSEGV and related errors.
636 static void segv_handler( int signal
, siginfo_t
*info
, void *ucontext
)
638 EXCEPTION_RECORD
*rec
;
639 ucontext_t
*context
= ucontext
;
641 /* check for page fault inside the thread stack */
642 if (signal
== SIGSEGV
&&
643 (char *)info
->si_addr
>= (char *)NtCurrentTeb()->DeallocationStack
&&
644 (char *)info
->si_addr
< (char *)NtCurrentTeb()->Tib
.StackBase
&&
645 virtual_handle_stack_fault( info
->si_addr
))
647 /* check if this was the last guard page */
648 if ((char *)info
->si_addr
< (char *)NtCurrentTeb()->DeallocationStack
+ 2*4096)
650 rec
= setup_exception( context
, raise_segv_exception
);
651 rec
->ExceptionCode
= EXCEPTION_STACK_OVERFLOW
;
656 rec
= setup_exception( context
, raise_segv_exception
);
657 if (rec
->ExceptionCode
== EXCEPTION_STACK_OVERFLOW
) return;
661 case SIGILL
: /* Invalid opcode exception */
662 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
664 case SIGSEGV
: /* Segmentation fault */
665 rec
->ExceptionCode
= EXCEPTION_ACCESS_VIOLATION
;
666 rec
->NumberParameters
= 2;
667 rec
->ExceptionInformation
[0] = is_write_fault(context
);
668 rec
->ExceptionInformation
[1] = (ULONG_PTR
)info
->si_addr
;
670 case SIGBUS
: /* Alignment check exception */
671 rec
->ExceptionCode
= EXCEPTION_DATATYPE_MISALIGNMENT
;
674 ERR("Got unexpected signal %i\n", signal
);
675 rec
->ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
;
680 /**********************************************************************
683 * Handler for SIGTRAP.
685 static void trap_handler( int signal
, siginfo_t
*info
, void *ucontext
)
687 EXCEPTION_RECORD rec
;
691 switch ( info
->si_code
)
694 rec
.ExceptionCode
= EXCEPTION_SINGLE_STEP
;
698 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
702 save_context( &context
, ucontext
);
703 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
704 rec
.ExceptionRecord
= NULL
;
705 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
706 rec
.NumberParameters
= 0;
707 status
= raise_exception( &rec
, &context
, TRUE
);
708 if (status
) raise_status( status
, &rec
);
709 restore_context( &context
, ucontext
);
712 /**********************************************************************
715 * Handler for SIGFPE.
717 static void fpe_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
719 EXCEPTION_RECORD rec
;
723 save_fpu( &context
, sigcontext
);
724 save_context( &context
, sigcontext
);
726 switch (siginfo
->si_code
& 0xffff )
730 rec
.ExceptionCode
= EXCEPTION_ARRAY_BOUNDS_EXCEEDED
;
735 rec
.ExceptionCode
= EXCEPTION_INT_DIVIDE_BY_ZERO
;
740 rec
.ExceptionCode
= EXCEPTION_INT_OVERFLOW
;
745 rec
.ExceptionCode
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
750 rec
.ExceptionCode
= EXCEPTION_FLT_OVERFLOW
;
755 rec
.ExceptionCode
= EXCEPTION_FLT_UNDERFLOW
;
760 rec
.ExceptionCode
= EXCEPTION_FLT_INEXACT_RESULT
;
767 rec
.ExceptionCode
= EXCEPTION_FLT_INVALID_OPERATION
;
770 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
771 rec
.ExceptionRecord
= NULL
;
772 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
773 rec
.NumberParameters
= 0;
774 status
= raise_exception( &rec
, &context
, TRUE
);
775 if (status
) raise_status( status
, &rec
);
777 restore_context( &context
, sigcontext
);
778 restore_fpu( &context
, sigcontext
);
781 /**********************************************************************
784 * Handler for SIGINT.
786 static void int_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
788 if (!dispatch_signal(SIGINT
))
790 EXCEPTION_RECORD rec
;
794 save_context( &context
, sigcontext
);
795 rec
.ExceptionCode
= CONTROL_C_EXIT
;
796 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
797 rec
.ExceptionRecord
= NULL
;
798 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
799 rec
.NumberParameters
= 0;
800 status
= raise_exception( &rec
, &context
, TRUE
);
801 if (status
) raise_status( status
, &rec
);
802 restore_context( &context
, sigcontext
);
807 /**********************************************************************
810 * Handler for SIGABRT.
812 static void abrt_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
814 EXCEPTION_RECORD rec
;
818 save_context( &context
, sigcontext
);
819 rec
.ExceptionCode
= EXCEPTION_WINE_ASSERTION
;
820 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
821 rec
.ExceptionRecord
= NULL
;
822 rec
.ExceptionAddress
= (LPVOID
)context
.Pc
;
823 rec
.NumberParameters
= 0;
824 status
= raise_exception( &rec
, &context
, TRUE
);
825 if (status
) raise_status( status
, &rec
);
826 restore_context( &context
, sigcontext
);
830 /**********************************************************************
833 * Handler for SIGQUIT.
835 static void quit_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
841 /**********************************************************************
844 * Handler for SIGUSR1, used to signal a thread that it got suspended.
846 static void usr1_handler( int signal
, siginfo_t
*siginfo
, void *sigcontext
)
850 save_context( &context
, sigcontext
);
851 wait_suspend( &context
);
852 restore_context( &context
, sigcontext
);
856 /***********************************************************************
857 * __wine_set_signal_handler (NTDLL.@)
859 int CDECL
__wine_set_signal_handler(unsigned int sig
, wine_signal_handler wsh
)
861 if (sig
>= ARRAY_SIZE(handlers
)) return -1;
862 if (handlers
[sig
] != NULL
) return -2;
868 /**********************************************************************
869 * signal_alloc_thread
871 NTSTATUS
signal_alloc_thread( TEB
**teb
)
873 static size_t sigstack_zero_bits
;
877 if (!sigstack_zero_bits
)
879 size_t min_size
= teb_size
+ max( MINSIGSTKSZ
, 8192 );
880 /* find the first power of two not smaller than min_size */
881 sigstack_zero_bits
= 12;
882 while ((1u << sigstack_zero_bits
) < min_size
) sigstack_zero_bits
++;
883 signal_stack_size
= (1 << sigstack_zero_bits
) - teb_size
;
884 assert( sizeof(TEB
) <= teb_size
);
887 size
= 1 << sigstack_zero_bits
;
889 if (!(status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)teb
, sigstack_zero_bits
,
890 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
892 (*teb
)->Tib
.Self
= &(*teb
)->Tib
;
893 (*teb
)->Tib
.ExceptionList
= (void *)~0UL;
899 /**********************************************************************
902 void signal_free_thread( TEB
*teb
)
906 NtFreeVirtualMemory( NtCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
910 /**********************************************************************
913 void signal_init_thread( TEB
*teb
)
915 static BOOL init_done
;
919 pthread_key_create( &teb_key
, NULL
);
923 /* Win64/ARM applications expect the TEB pointer to be in the x18 platform register. */
924 __asm__
__volatile__( "mov x18, %0" : : "r" (teb
) );
926 pthread_setspecific( teb_key
, teb
);
930 /**********************************************************************
931 * signal_init_process
933 void signal_init_process(void)
935 struct sigaction sig_act
;
937 sig_act
.sa_mask
= server_block_set
;
938 sig_act
.sa_flags
= SA_RESTART
| SA_SIGINFO
;
940 sig_act
.sa_sigaction
= int_handler
;
941 if (sigaction( SIGINT
, &sig_act
, NULL
) == -1) goto error
;
942 sig_act
.sa_sigaction
= fpe_handler
;
943 if (sigaction( SIGFPE
, &sig_act
, NULL
) == -1) goto error
;
944 sig_act
.sa_sigaction
= abrt_handler
;
945 if (sigaction( SIGABRT
, &sig_act
, NULL
) == -1) goto error
;
946 sig_act
.sa_sigaction
= quit_handler
;
947 if (sigaction( SIGQUIT
, &sig_act
, NULL
) == -1) goto error
;
948 sig_act
.sa_sigaction
= usr1_handler
;
949 if (sigaction( SIGUSR1
, &sig_act
, NULL
) == -1) goto error
;
951 sig_act
.sa_sigaction
= segv_handler
;
952 if (sigaction( SIGSEGV
, &sig_act
, NULL
) == -1) goto error
;
953 if (sigaction( SIGILL
, &sig_act
, NULL
) == -1) goto error
;
955 if (sigaction( SIGBUS
, &sig_act
, NULL
) == -1) goto error
;
959 sig_act
.sa_sigaction
= trap_handler
;
960 if (sigaction( SIGTRAP
, &sig_act
, NULL
) == -1) goto error
;
970 /**********************************************************************
971 * RtlAddFunctionTable (NTDLL.@)
973 BOOLEAN CDECL
RtlAddFunctionTable( RUNTIME_FUNCTION
*table
, DWORD count
, ULONG_PTR addr
)
975 FIXME( "%p %u %lx: stub\n", table
, count
, addr
);
980 /*************************************************************************
981 * RtlAddGrowableFunctionTable (NTDLL.@)
983 DWORD WINAPI
RtlAddGrowableFunctionTable( void **table
, RUNTIME_FUNCTION
*functions
, DWORD count
, DWORD max_count
,
984 ULONG_PTR base
, ULONG_PTR end
)
986 FIXME( "(%p, %p, %d, %d, %ld, %ld) stub!\n", table
, functions
, count
, max_count
, base
, end
);
987 if (table
) *table
= NULL
;
992 /**********************************************************************
993 * RtlDeleteFunctionTable (NTDLL.@)
995 BOOLEAN CDECL
RtlDeleteFunctionTable( RUNTIME_FUNCTION
*table
)
997 FIXME( "%p: stub\n", table
);
1001 /***********************************************************************
1002 * RtlUnwind (NTDLL.@)
1004 void WINAPI
RtlUnwind( PVOID pEndFrame
, PVOID targetIp
, PEXCEPTION_RECORD pRecord
, PVOID retval
)
1006 FIXME( "Not implemented on ARM64\n" );
1009 /*******************************************************************
1010 * NtRaiseException (NTDLL.@)
1012 NTSTATUS WINAPI
NtRaiseException( EXCEPTION_RECORD
*rec
, CONTEXT
*context
, BOOL first_chance
)
1014 NTSTATUS status
= raise_exception( rec
, context
, first_chance
);
1015 if (status
== STATUS_SUCCESS
) NtSetContextThread( GetCurrentThread(), context
);
1019 /***********************************************************************
1020 * RtlRaiseException (NTDLL.@)
1022 void WINAPI
RtlRaiseException( EXCEPTION_RECORD
*rec
)
1027 RtlCaptureContext( &context
);
1028 rec
->ExceptionAddress
= (LPVOID
)context
.Pc
;
1029 status
= raise_exception( rec
, &context
, TRUE
);
1030 if (status
) raise_status( status
, rec
);
1033 /*************************************************************************
1034 * RtlCaptureStackBackTrace (NTDLL.@)
1036 USHORT WINAPI
RtlCaptureStackBackTrace( ULONG skip
, ULONG count
, PVOID
*buffer
, ULONG
*hash
)
1038 FIXME( "(%d, %d, %p, %p) stub!\n", skip
, count
, buffer
, hash
);
1042 /***********************************************************************
1043 * call_thread_entry_point
1045 static void WINAPI
call_thread_entry_point( LPTHREAD_START_ROUTINE entry
, void *arg
)
1049 TRACE_(relay
)( "\1Starting thread proc %p (arg=%p)\n", entry
, arg
);
1050 RtlExitUserThread( entry( arg
));
1052 __EXCEPT(call_unhandled_exception_filter
)
1054 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
1057 abort(); /* should not be reached */
1060 typedef void (WINAPI
*thread_start_func
)(LPTHREAD_START_ROUTINE
,void *);
1064 thread_start_func start
;
1065 LPTHREAD_START_ROUTINE entry
;
1070 /***********************************************************************
1073 static void thread_startup( void *param
)
1075 CONTEXT context
= { 0 };
1076 struct startup_info
*info
= param
;
1078 /* build the initial context */
1079 context
.ContextFlags
= CONTEXT_FULL
;
1080 context
.u
.s
.X0
= (DWORD_PTR
)info
->entry
;
1081 context
.u
.s
.X1
= (DWORD_PTR
)info
->arg
;
1082 context
.Sp
= (DWORD_PTR
)NtCurrentTeb()->Tib
.StackBase
;
1083 context
.Pc
= (DWORD_PTR
)info
->start
;
1085 if (info
->suspend
) wait_suspend( &context
);
1086 attach_dlls( &context
, (void **)&context
.u
.s
.X0
);
1088 ((thread_start_func
)context
.Pc
)( (LPTHREAD_START_ROUTINE
)context
.u
.s
.X0
, (void *)context
.u
.s
.X1
);
1091 /***********************************************************************
1092 * signal_start_thread
1094 * Thread startup sequence:
1095 * signal_start_thread()
1096 * -> thread_startup()
1097 * -> call_thread_entry_point()
1099 void signal_start_thread( LPTHREAD_START_ROUTINE entry
, void *arg
, BOOL suspend
)
1101 struct startup_info info
= { call_thread_entry_point
, entry
, arg
, suspend
};
1102 wine_switch_to_stack( thread_startup
, &info
, NtCurrentTeb()->Tib
.StackBase
);
1105 /**********************************************************************
1106 * signal_start_process
1108 * Process startup sequence:
1109 * signal_start_process()
1110 * -> thread_startup()
1111 * -> kernel32_start_process()
1113 void signal_start_process( LPTHREAD_START_ROUTINE entry
, BOOL suspend
)
1115 struct startup_info info
= { kernel32_start_process
, entry
, NtCurrentTeb()->Peb
, suspend
};
1116 wine_switch_to_stack( thread_startup
, &info
, NtCurrentTeb()->Tib
.StackBase
);
1119 /***********************************************************************
1120 * signal_exit_thread
1122 void signal_exit_thread( int status
)
1124 exit_thread( status
);
1127 /***********************************************************************
1128 * signal_exit_process
1130 void signal_exit_process( int status
)
1135 /**********************************************************************
1136 * DbgBreakPoint (NTDLL.@)
1138 void WINAPI
DbgBreakPoint(void)
1140 kill(getpid(), SIGTRAP
);
1143 /**********************************************************************
1144 * DbgUserBreakPoint (NTDLL.@)
1146 void WINAPI
DbgUserBreakPoint(void)
1148 kill(getpid(), SIGTRAP
);
1151 /**********************************************************************
1152 * NtCurrentTeb (NTDLL.@)
1154 TEB
* WINAPI
NtCurrentTeb(void)
1156 return pthread_getspecific( teb_key
);
1159 #endif /* __aarch64__ */