1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef ZIRCON_SYSCALLS_EXCEPTION_H_
6 #define ZIRCON_SYSCALLS_EXCEPTION_H_
7 
8 #include <zircon/compiler.h>
9 #include <zircon/syscalls/port.h>
10 #include <zircon/types.h>
11 
12 __BEGIN_CDECLS
13 
14 // ask clang format not to mess up the indentation:
15 // clang-format off
16 
17 // This bit is set for synthetic exceptions to distinguish them from
18 // architectural exceptions.
19 // Note: Port packet types provide 8 bits to distinguish the exception type.
20 // See zircon/port.h.
21 #define ZX_EXCP_SYNTH ((uint8_t)0x80)
22 
23 // The kind of an exception.
24 // Exception types are a subset of port packet types. See zircon/port.h.
25 
26 // These are architectural exceptions.
27 // Depending on the exception, further information can be found in
28 // |report.context.arch|.
29 
30 // General exception not covered by another value.
31 #define ZX_EXCP_GENERAL ZX_PKT_TYPE_EXCEPTION(0)
32 #define ZX_EXCP_FATAL_PAGE_FAULT ZX_PKT_TYPE_EXCEPTION(1)
33 #define ZX_EXCP_UNDEFINED_INSTRUCTION ZX_PKT_TYPE_EXCEPTION(2)
34 #define ZX_EXCP_SW_BREAKPOINT ZX_PKT_TYPE_EXCEPTION(3)
35 #define ZX_EXCP_HW_BREAKPOINT ZX_PKT_TYPE_EXCEPTION(4)
36 #define ZX_EXCP_UNALIGNED_ACCESS ZX_PKT_TYPE_EXCEPTION(5)
37 
38 // Synthetic exceptions.
39 
40 // A thread is starting.
41 // This exception is sent to debuggers only (ZX_EXCEPTION_PORT_TYPE_DEBUGGER).
42 // The thread is paused until it is resumed by the debugger
43 // with zx_task_resume_from_exception.
44 #define ZX_EXCP_THREAD_STARTING ZX_PKT_TYPE_EXCEPTION(ZX_EXCP_SYNTH | 0)
45 
46 // A thread is exiting.
47 // This exception is sent to debuggers only (ZX_EXCEPTION_PORT_TYPE_DEBUGGER).
48 // This exception is different from ZX_EXCP_GONE in that a debugger can
49 // still examine thread state.
50 // The thread is paused until it is resumed by the debugger
51 // with zx_task_resume_from_exception.
52 #define ZX_EXCP_THREAD_EXITING ZX_PKT_TYPE_EXCEPTION(ZX_EXCP_SYNTH | 1)
53 
54 // This exception is generated when a syscall fails with a job policy
55 // error (for example, an invalid handle argument is passed to the
56 // syscall when the ZX_POL_BAD_HANDLE policy is enabled) and
57 // ZX_POL_ACTION_EXCEPTION is set for the policy.  The thread that
58 // invoked the syscall may be resumed with zx_task_resume_from_exception.
59 #define ZX_EXCP_POLICY_ERROR ZX_PKT_TYPE_EXCEPTION(ZX_EXCP_SYNTH | 2)
60 
61 // A process is starting.
62 // This exception is sent to job debuggers only
63 // (ZX_EXCEPTION_PORT_TYPE_JOB_DEBUGGER).
64 // The initial thread is paused until it is resumed by the debugger with
65 // zx_task_resume_from_exception.
66 #define ZX_EXCP_PROCESS_STARTING ZX_PKT_TYPE_EXCEPTION(ZX_EXCP_SYNTH | 3)
67 
68 typedef uint32_t zx_excp_type_t;
69 
70 // Assuming |excp| is an exception type, return non-zero if it is an
71 // architectural exception.
72 #define ZX_EXCP_IS_ARCH(excp) \
73   (((excp) & (ZX_PKT_TYPE_EXCEPTION(ZX_EXCP_SYNTH) & ~ZX_PKT_TYPE_MASK)) == 0)
74 
75 typedef struct zx_x86_64_exc_data {
76     uint64_t vector;
77     uint64_t err_code;
78     uint64_t cr2;
79 } zx_x86_64_exc_data_t;
80 
81 typedef struct zx_arm64_exc_data {
82     uint32_t esr;
83     uint64_t far;
84 } zx_arm64_exc_data_t;
85 
86 // data associated with an exception (siginfo in linux parlance)
87 // Things available from regsets (e.g., pc) are not included here.
88 // For an example list of things one might add, see linux siginfo.
89 typedef struct zx_exception_context {
90     struct {
91         union {
92             zx_x86_64_exc_data_t x86_64;
93             zx_arm64_exc_data_t  arm_64;
94         } u;
95     } arch;
96 } zx_exception_context_t;
97 
98 // The common header of all exception reports.
99 typedef struct zx_exception_header {
100     // The actual size, in bytes, of the report (including this field).
101     uint32_t size;
102 
103     zx_excp_type_t type;
104 } zx_exception_header_t;
105 
106 // Data reported to an exception handler for most exceptions.
107 typedef struct zx_exception_report {
108     zx_exception_header_t header;
109     // The remainder of the report is exception-specific.
110     zx_exception_context_t context;
111 } zx_exception_report_t;
112 
113 // Options for zx_task_resume_from_exception()
114 #define ZX_RESUME_TRY_NEXT ((uint32_t)2)
115 // Indicates that instead of resuming from the faulting instruction we instead
116 // let the next exception handler in the search order, if any, process the
117 // exception. If there are no more then the entire process is killed.
118 
119 // Options for zx_task_bind_exception_port.
120 #define ZX_EXCEPTION_PORT_DEBUGGER ((uint32_t)1)
121 // When binding an exception port to a process, set the process's debugger
122 // exception port.
123 
124 // The type of exception port a thread may be waiting for a response from.
125 // These values are reported in zx_info_thread_t.wait_exception_port_type.
126 #define ZX_EXCEPTION_PORT_TYPE_NONE         ((uint32_t)0u)
127 #define ZX_EXCEPTION_PORT_TYPE_DEBUGGER     ((uint32_t)1u)
128 #define ZX_EXCEPTION_PORT_TYPE_THREAD       ((uint32_t)2u)
129 #define ZX_EXCEPTION_PORT_TYPE_PROCESS      ((uint32_t)3u)
130 #define ZX_EXCEPTION_PORT_TYPE_JOB          ((uint32_t)4u)
131 #define ZX_EXCEPTION_PORT_TYPE_JOB_DEBUGGER ((uint32_t)5u)
132 
133 __END_CDECLS
134 
135 #endif // ZIRCON_SYSCALLS_EXCEPTION_H_
136