xref: /xnu-8020.140.41/osfmk/kdp/kdp_core.h (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
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 
43 /*
44  * Packet types.
45  */
46 #define KDP_RRQ   1                     /* read request */
47 #define KDP_WRQ   2                     /* write request */
48 #define KDP_DATA  3                     /* data packet */
49 #define KDP_ACK   4                     /* acknowledgement */
50 #define KDP_ERROR 5                     /* error code */
51 #define KDP_SEEK  6                     /* Seek to specified offset */
52 #define KDP_EOF   7                     /* signal end of file */
53 #define KDP_FLUSH 8                     /* flush outstanding data */
54 #define KDP_FEATURE_MASK_STRING         "features"
55 
56 enum    {KDP_FEATURE_LARGE_CRASHDUMPS = 1, KDP_FEATURE_LARGE_PKT_SIZE = 2};
57 extern  uint32_t        kdp_feature_large_crashdumps, kdp_feature_large_pkt_size;
58 
59 struct  corehdr {
60 	short   th_opcode;              /* packet type */
61 	union {
62 		unsigned int    tu_block;       /* block # */
63 		unsigned int    tu_code;        /* error code */
64 		char    tu_rpl[1];      /* request packet payload */
65 	} th_u;
66 	char    th_data[0];             /* data or error string */
67 }__attribute__((packed));
68 
69 #define th_block        th_u.tu_block
70 #define th_code         th_u.tu_code
71 #define th_stuff        th_u.tu_rpl
72 #define th_msg          th_data
73 
74 /*
75  * Error codes.
76  */
77 #define EUNDEF          0               /* not defined */
78 #define ENOTFOUND       1               /* file not found */
79 #define EACCESS         2               /* access violation */
80 #define ENOSPACE        3               /* disk full or allocation exceeded */
81 #define EBADOP          4               /* illegal TFTP operation */
82 #define EBADID          5               /* unknown transfer ID */
83 #define EEXISTS         6               /* file already exists */
84 #define ENOUSER         7               /* no such user */
85 
86 #define CORE_REMOTE_PORT 1069 /* hardwired, we can't really query the services file */
87 
88 #if defined(__arm__) || defined(__arm64__)
89 
90 void panic_spin_shmcon(void);
91 
92 #endif /* defined(__arm__) || defined(__arm64__) */
93 
94 void kdp_panic_dump(void);
95 void begin_panic_transfer(void);
96 void abort_panic_transfer(void);
97 void kdp_set_dump_info(const uint32_t flags, const char *file, const char *destip,
98     const char *routerip, const uint32_t port);
99 void kdp_get_dump_info(kdp_dumpinfo_reply_t *rp);
100 
101 enum kern_dump_type {
102 	KERN_DUMP_DISK, /* local, on device core dump */
103 	KERN_DUMP_NET, /* kdp network core dump */
104 #if defined(__arm__) || defined(__arm64__)
105 	KERN_DUMP_HW_SHMEM_DBG, /* coordinated hardware shared memory debugger core dump */
106 #endif
107 	KERN_DUMP_STACKSHOT_DISK, /* local, stackshot on device coredump */
108 };
109 
110 int kern_dump(enum kern_dump_type kd_variant);
111 
112 boolean_t dumped_kernel_core(void);
113 
114 struct corehdr *create_panic_header(unsigned int request, const char *corename, unsigned length, unsigned block);
115 
116 int     kdp_send_crashdump_pkt(unsigned int request, char *corename,
117     uint64_t length, void *panic_data);
118 
119 int     kdp_send_crashdump_data(unsigned int request, char *corename,
120     uint64_t length, void * txstart);
121 
122 void kern_collectth_state_size(uint64_t * tstate_count, uint64_t * tstate_size);
123 
124 void kern_collectth_state(thread_t thread, void *buffer, uint64_t size, void **iter);
125 
126 boolean_t kdp_has_polled_corefile(void);
127 kern_return_t kdp_polled_corefile_error(void);
128 
129 void kdp_core_init(void);
130 
131 extern boolean_t kdp_corezip_disabled;
132 
133 #define KDP_CRASHDUMP_POLL_COUNT (2500)
134 
135 #if PRIVATE
136 kern_return_t kdp_core_output(void *kdp_core_out_vars, uint64_t length, void * data);
137 
138 /*
139  * Resets the coredump output vars such that they're ready to start writing out coredump data.
140  * Note that the 'encrypt_core' parameter instructs the output vars to encrypt the coredump data (if possible)
141  * The 'out_should_skip_coredump' parameter will be set to true if the calling code should skip this coredump (for reasons).
142  */
143 kern_return_t kdp_reset_output_vars(void *kdp_core_out_vars, uint64_t totalbytes, bool encrypt_core, bool *out_should_skip_coredump);
144 
145 kern_return_t kern_dump_record_file(void *kdp_core_out_vars, const char *filename, uint64_t file_offset, uint64_t *out_file_length);
146 
147 kern_return_t kern_dump_seek_to_next_file(void *kdp_core_out_varss, uint64_t next_file_offset);
148 
149 extern boolean_t bootloader_valid_page(ppnum_t ppn);
150 
151 /*
152  * Called whenever the encryption functionality becomes available (e.g. when an encryption Kext is loaded
153  * and registers its interface with libkern). It is expected that once encryption support is available,
154  * it will stay available for the remainder of the kernel lifetime.
155  */
156 kern_return_t kdp_core_handle_encryption_available(void);
157 
158 #endif /* PRIVATE */
159 
160 #endif /* __KDP_CORE_H */
161