xref: /xnu-11215/osfmk/kdp/kdp_core.h (revision 8d741a5d)
1 /*
2  * Copyright (c) 2003-2019 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /* HISTORY
30  * 8 Aug. 2003 - Created (Derek Kumar)
31  */
32 
33 /* Various protocol definitions
34  * for the core transfer protocol, which is a variant of TFTP
35  */
36 #ifndef __KDP_CORE_H
37 #define __KDP_CORE_H
38 
39 #include <kern/thread.h>
40 #include <kdp/kdp_protocol.h>
41 #include <string.h>
42 #include <IOKit/IOBSD.h>
43 
44 /*
45  * Packet types.
46  */
47 #define KDP_RRQ   1                     /* read request */
48 #define KDP_WRQ   2                     /* write request */
49 #define KDP_DATA  3                     /* data packet */
50 #define KDP_ACK   4                     /* acknowledgement */
51 #define KDP_ERROR 5                     /* error code */
52 #define KDP_SEEK  6                     /* Seek to specified offset */
53 #define KDP_EOF   7                     /* signal end of file */
54 #define KDP_FLUSH 8                     /* flush outstanding data */
55 #define KDP_FEATURE_MASK_STRING         "features"
56 
57 enum    {KDP_FEATURE_LARGE_CRASHDUMPS = 1, KDP_FEATURE_LARGE_PKT_SIZE = 2};
58 extern  uint32_t        kdp_feature_large_crashdumps, kdp_feature_large_pkt_size;
59 
60 struct  corehdr {
61 	short   th_opcode;              /* packet type */
62 	union {
63 		unsigned int    tu_block;       /* block # */
64 		unsigned int    tu_code;        /* error code */
65 		char    tu_rpl[1];      /* request packet payload */
66 	} th_u;
67 	char    th_data[0];             /* data or error string */
68 }__attribute__((packed));
69 
70 #define th_block        th_u.tu_block
71 #define th_code         th_u.tu_code
72 #define th_stuff        th_u.tu_rpl
73 #define th_msg          th_data
74 
75 /*
76  * Error codes.
77  */
78 #define EUNDEF          0               /* not defined */
79 #define ENOTFOUND       1               /* file not found */
80 #define EACCESS         2               /* access violation */
81 #define ENOSPACE        3               /* disk full or allocation exceeded */
82 #define EBADOP          4               /* illegal TFTP operation */
83 #define EBADID          5               /* unknown transfer ID */
84 #define EEXISTS         6               /* file already exists */
85 #define ENOUSER         7               /* no such user */
86 
87 #define CORE_REMOTE_PORT 1069 /* hardwired, we can't really query the services file */
88 
89 #if defined(__arm64__)
90 
91 void panic_spin_shmcon(void);
92 void shmem_mark_as_busy(void);
93 void shmem_unmark_as_busy(void);
94 
95 #endif /* defined(__arm64__) */
96 
97 void kdp_panic_dump(void);
98 void begin_panic_transfer(void);
99 void abort_panic_transfer(void);
100 void kdp_set_dump_info(const uint32_t flags, const char *file, const char *destip,
101     const char *routerip, const uint32_t port);
102 void kdp_get_dump_info(kdp_dumpinfo_reply_t *rp);
103 
104 enum kern_dump_type {
105 	KERN_DUMP_DISK, /* local, on device core dump */
106 	KERN_DUMP_NET, /* kdp network core dump */
107 #if defined(__arm64__)
108 	KERN_DUMP_HW_SHMEM_DBG, /* coordinated hardware shared memory debugger core dump */
109 #endif
110 	KERN_DUMP_STACKSHOT_DISK, /* local, stackshot on device coredump */
111 };
112 
113 int kern_dump(enum kern_dump_type kd_variant);
114 
115 boolean_t dumped_kernel_core(void);
116 
117 struct corehdr *create_panic_header(unsigned int request, const char *corename, unsigned length, unsigned block);
118 
119 int     kdp_send_crashdump_pkt(unsigned int request, char *corename,
120     uint64_t length, void *panic_data);
121 
122 int     kdp_send_crashdump_data(unsigned int request, char *corename,
123     uint64_t length, void * txstart);
124 
125 void kern_collectth_state_size(uint64_t * tstate_count, uint64_t * tstate_size);
126 
127 void kern_collectth_state(thread_t thread, void *buffer, uint64_t size, void **iter);
128 void kern_collect_userth_state_size(task_t task, uint64_t * tstate_count, uint64_t * tstate_size);
129 void kern_collect_userth_state(task_t task, thread_t thread, void *buffer, uint64_t size);
130 
131 boolean_t kdp_has_polled_corefile(void);
132 kern_return_t kdp_polled_corefile_error(void);
133 IOPolledCoreFileMode_t kdp_polled_corefile_mode(void);
134 
135 #ifdef CONFIG_KDP_COREDUMP_ENCRYPTION
136 bool kern_dump_should_enforce_encryption(void);
137 #endif /* CONFIG_KDP_COREDUMP_ENCRYPTION */
138 
139 void kdp_core_init(void);
140 
141 extern boolean_t kdp_corezip_disabled;
142 
143 #define KDP_CRASHDUMP_POLL_COUNT (2500)
144 
145 #if PRIVATE
146 kern_return_t kdp_core_output(void *kdp_core_out_vars, uint64_t length, void * data);
147 
148 /*
149  * Resets the coredump output vars such that they're ready to start writing out coredump data.
150  * Note that the 'encrypt_core' parameter instructs the output vars to encrypt the coredump data (if possible)
151  * The 'out_should_skip_coredump' parameter will be set to true if the calling code should skip this coredump (for reasons).
152  */
153 kern_return_t kdp_reset_output_vars(void *kdp_core_out_vars, uint64_t totalbytes, bool encrypt_core, bool *out_should_skip_coredump);
154 
155 kern_return_t kern_dump_record_file(void *kdp_core_out_vars, const char *filename, uint64_t file_offset, uint64_t *out_file_length, uint64_t details_flags);
156 
157 kern_return_t kern_dump_seek_to_next_file(void *kdp_core_out_varss, uint64_t next_file_offset);
158 
159 extern boolean_t bootloader_valid_page(ppnum_t ppn);
160 
161 /*
162  * Called whenever the encryption functionality becomes available (e.g. when an encryption Kext is loaded
163  * and registers its interface with libkern). It is expected that once encryption support is available,
164  * it will stay available for the remainder of the kernel lifetime.
165  */
166 kern_return_t kdp_core_handle_encryption_available(void);
167 
168 /*
169  * Called whenever the LZ4 functionality becomes available (e.g. when the Compression kext is loaded
170  * and registers its interface with libkern). It is expected that once LZ4 support is available,
171  * it will stay available for the remainder of the kernel lifetime.
172  */
173 kern_return_t kdp_core_handle_lz4_available(void);
174 
175 #endif /* PRIVATE */
176 
177 #endif /* __KDP_CORE_H */
178