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