2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 1982, 1986, 1993
31 * The Regents of the University of California. All rights reserved.
35 * Kernel Debugging Protocol UDP implementation.
38 #include <mach/boolean.h>
39 #include <mach/mach_types.h>
40 #include <mach/exception_types.h>
41 #include <kern/cpu_data.h>
42 #include <kern/debug.h>
43 #include <kern/clock.h>
45 #include <kdp/kdp_core.h>
46 #include <kdp/kdp_internal.h>
47 #if (MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING)
48 #include <kdp/kdp_en_debugger.h>
50 #include <kdp/kdp_callout.h>
51 #include <kdp/kdp_udp.h>
52 #include <kdp/kdp_core.h>
54 #include <kdp/kdp_serial.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_protos.h>
59 #include <vm/vm_kern.h> /* kernel_map */
61 #include <mach/memory_object_types.h>
62 #include <machine/pal_routines.h>
64 #include <sys/msgbuf.h>
66 /* we just want the link status flags, so undef KERNEL_PRIVATE for this
69 #include <net/if_media.h>
70 #define KERNEL_PRIVATE
74 #include <IOKit/IOPlatformExpert.h>
75 #include <libkern/version.h>
79 extern unsigned int not_in_kdp
;
80 extern int kdp_snapshot
;
82 #ifdef CONFIG_KDP_INTERACTIVE_DEBUGGING
84 extern int inet_aton(const char *, struct kdp_in_addr
*); /* in libkern */
85 extern char *inet_ntoa_r(struct kdp_in_addr ina
, char *buf
,
86 size_t buflen
); /* in libkern */
88 #define DO_ALIGN 1 /* align all packet data accesses */
89 #define KDP_SERIAL_IPADDR 0xABADBABE /* IP address used for serial KDP */
90 #define LINK_UP_STATUS (IFM_AVALID | IFM_ACTIVE)
92 extern int kdp_getc(void);
93 extern int reattach_wait
;
95 static u_short ip_id
; /* ip packet ctr, for ids */
97 /* @(#)udp_usrreq.c 2.2 88/05/23 4.0NFSSRC SMI; from UCB 7.1 6/5/86 */
100 * UDP protocol implementation.
101 * Per RFC 768, August, 1980.
103 #define UDP_TTL 60 /* deflt time to live for UDP packets */
104 static int udp_ttl
= UDP_TTL
;
105 static unsigned char exception_seq
;
108 uint32_t ih_next
, ih_prev
; /* for protocol sequence q's */
109 u_char ih_x1
; /* (unused) */
110 u_char ih_pr
; /* protocol */
111 short ih_len
; /* protocol length */
112 struct kdp_in_addr ih_src
; /* source internet address */
113 struct kdp_in_addr ih_dst
; /* destination internet address */
117 u_short uh_sport
; /* source port */
118 u_short uh_dport
; /* destination port */
119 short uh_ulen
; /* udp length */
120 u_short uh_sum
; /* udp checksum */
123 struct kdp_udpiphdr
{
124 struct kdp_ipovly ui_i
; /* overlaid ip structure */
125 struct kdp_udphdr ui_u
; /* udp header */
127 #define ui_next ui_i.ih_next
128 #define ui_prev ui_i.ih_prev
129 #define ui_x1 ui_i.ih_x1
130 #define ui_pr ui_i.ih_pr
131 #define ui_len ui_i.ih_len
132 #define ui_src ui_i.ih_src
133 #define ui_dst ui_i.ih_dst
134 #define ui_sport ui_u.uh_sport
135 #define ui_dport ui_u.uh_dport
136 #define ui_ulen ui_u.uh_ulen
137 #define ui_sum ui_u.uh_sum
144 #ifdef __LITTLE_ENDIAN__
145 ip_xhl
:4, /* header length */
146 ip_xv
:4, /* version */
147 ip_xtos
:8, /* type of service */
148 ip_xlen
:16; /* total length */
150 #ifdef __BIG_ENDIAN__
151 ip_xv
:4, /* version */
152 ip_xhl
:4, /* header length */
153 ip_xtos
:8, /* type of service */
154 ip_xlen
:16; /* total length */
158 u_short ip_id
; /* identification */
159 short ip_off
; /* fragment offset field */
160 #define IP_DF 0x4000 /* dont fragment flag */
161 #define IP_MF 0x2000 /* more fragments flag */
162 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
163 u_char ip_ttl
; /* time to live */
164 u_char ip_p
; /* protocol */
165 u_short ip_sum
; /* checksum */
166 struct kdp_in_addr ip_src
, ip_dst
; /* source and dest address */
168 #define ip_v ip_vhltl.ip_x.ip_xv
169 #define ip_hl ip_vhltl.ip_x.ip_xhl
170 #define ip_tos ip_vhltl.ip_x.ip_xtos
171 #define ip_len ip_vhltl.ip_x.ip_xlen
173 #define IPPROTO_UDP 17
176 #define ETHERTYPE_IP 0x0800 /* IP protocol */
179 * Ethernet Address Resolution Protocol.
181 * See RFC 826 for protocol description. Structure below is adapted
182 * to resolving internet addresses. Field names used correspond to
186 #define ETHERTYPE_ARP 0x0806 /* Addr. resolution protocol */
189 u_short ar_hrd
; /* format of hardware address */
190 #define ARPHRD_ETHER 1 /* ethernet hardware format */
191 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
192 u_short ar_pro
; /* format of protocol address */
193 u_char ar_hln
; /* length of hardware address */
194 u_char ar_pln
; /* length of protocol address */
195 u_short ar_op
; /* one of: */
196 #define ARPOP_REQUEST 1 /* request to resolve address */
197 #define ARPOP_REPLY 2 /* response to previous request */
198 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
199 #define ARPOP_REVREPLY 4 /* response giving protocol address */
200 #define ARPOP_INVREQUEST 8 /* request to identify peer */
201 #define ARPOP_INVREPLY 9 /* response identifying peer */
204 struct kdp_ether_arp
{
205 struct kdp_arphdr ea_hdr
; /* fixed-size header */
206 u_char arp_sha
[ETHER_ADDR_LEN
]; /* sender hardware address */
207 u_char arp_spa
[4]; /* sender protocol address */
208 u_char arp_tha
[ETHER_ADDR_LEN
]; /* target hardware address */
209 u_char arp_tpa
[4]; /* target protocol address */
211 #define arp_hrd ea_hdr.ar_hrd
212 #define arp_pro ea_hdr.ar_pro
213 #define arp_hln ea_hdr.ar_hln
214 #define arp_pln ea_hdr.ar_pln
215 #define arp_op ea_hdr.ar_op
217 #define ETHERMTU 1500
218 #define ETHERHDRSIZE 14
220 #define KDP_MAXPACKET (ETHERHDRSIZE + ETHERMTU + ETHERCRC)
223 unsigned char data
[KDP_MAXPACKET
];
224 unsigned int off
, len
;
228 struct kdp_manual_pkt manual_pkt
;
232 struct kdp_in_addr in
;
233 struct kdp_ether_addr ea
;
236 struct kdp_in_addr in
;
237 struct kdp_ether_addr ea
;
242 *exception_message
[] = {
244 "Memory access", /* EXC_BAD_ACCESS */
245 "Failed instruction", /* EXC_BAD_INSTRUCTION */
246 "Arithmetic", /* EXC_ARITHMETIC */
247 "Emulation", /* EXC_EMULATION */
248 "Software", /* EXC_SOFTWARE */
249 "Breakpoint" /* EXC_BREAKPOINT */
252 volatile int kdp_flag
= 0;
253 boolean_t kdp_corezip_disabled
= 0;
255 kdp_send_t kdp_en_send_pkt
;
256 static kdp_receive_t kdp_en_recv_pkt
;
257 static kdp_link_t kdp_en_linkstatus
;
258 static kdp_mode_t kdp_en_setmode
;
260 #if CONFIG_SERIAL_KDP
261 static void kdp_serial_send(void *rpkt
, unsigned int rpkt_len
);
262 #define KDP_SERIAL_ENABLED() (kdp_en_send_pkt == kdp_serial_send)
264 #define KDP_SERIAL_ENABLED() (0)
267 static uint32_t kdp_current_ip_address
= 0;
268 static struct kdp_ether_addr kdp_current_mac_address
= {.ether_addr_octet
= {0, 0, 0, 0, 0, 0}};
269 static void *kdp_current_ifp
;
271 static void kdp_handler( void *);
273 static uint32_t panic_server_ip
= 0;
274 static uint32_t parsed_router_ip
= 0;
275 static uint32_t router_ip
= 0;
276 static uint32_t target_ip
= 0;
278 static boolean_t save_ip_in_nvram
= FALSE
;
280 static volatile boolean_t panicd_specified
= FALSE
;
281 static boolean_t router_specified
= FALSE
;
282 static boolean_t corename_specified
= FALSE
;
283 static unsigned int panicd_port
= CORE_REMOTE_PORT
;
285 static struct kdp_ether_addr etherbroadcastaddr
= {.ether_addr_octet
= {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
287 static struct kdp_ether_addr router_mac
= {.ether_addr_octet
= {0, 0, 0, 0, 0, 0}};
288 static struct kdp_ether_addr destination_mac
= {.ether_addr_octet
= {0, 0, 0, 0, 0, 0}};
289 static struct kdp_ether_addr temp_mac
= {.ether_addr_octet
= {0, 0, 0, 0, 0, 0}};
290 static struct kdp_ether_addr current_resolved_MAC
= {.ether_addr_octet
= {0, 0, 0, 0, 0, 0}};
292 static boolean_t flag_panic_dump_in_progress
= FALSE
;
293 static boolean_t flag_router_mac_initialized
= FALSE
;
294 static boolean_t flag_dont_abort_panic_dump
= FALSE
;
296 static boolean_t flag_arp_resolved
= FALSE
;
298 static unsigned int panic_timeout
= 100000;
299 static unsigned int last_panic_port
= CORE_REMOTE_PORT
;
301 #define KDP_THROTTLE_VALUE (10ULL * NSEC_PER_SEC)
303 uint32_t kdp_crashdump_pkt_size
= 512;
304 #define KDP_LARGE_CRASHDUMP_PKT_SIZE (1440 - 6 - sizeof(struct kdp_udpiphdr))
305 static char panicd_ip_str
[20];
306 static char router_ip_str
[20];
307 static char corename_str
[100];
309 static unsigned int panic_block
= 0;
310 volatile unsigned int kdp_trigger_core_dump
= 0;
311 __private_extern__
volatile unsigned int flag_kdp_trigger_reboot
= 0;
314 extern unsigned int disableConsoleOutput
;
316 extern void kdp_call(void);
318 void * kdp_get_interface(void);
319 void kdp_set_gateway_mac(void *gatewaymac
);
320 void kdp_set_ip_and_mac_addresses(struct kdp_in_addr
*ipaddr
, struct kdp_ether_addr
*);
321 void kdp_set_interface(void *interface
, const struct kdp_ether_addr
*macaddr
);
323 void kdp_disable_arp(void);
324 static void kdp_arp_reply(struct kdp_ether_arp
*);
325 static void kdp_process_arp_reply(struct kdp_ether_arp
*);
326 static boolean_t
kdp_arp_resolve(uint32_t, struct kdp_ether_addr
*);
328 static volatile unsigned kdp_reentry_deadline
;
330 static uint32_t kdp_crashdump_feature_mask
= KDP_FEATURE_LARGE_CRASHDUMPS
| KDP_FEATURE_LARGE_PKT_SIZE
;
331 uint32_t kdp_feature_large_crashdumps
, kdp_feature_large_pkt_size
;
333 char kdp_kernelversion_string
[256];
335 static boolean_t gKDPDebug
= FALSE
;
337 #define KDP_DEBUG(...) if (gKDPDebug) printf(__VA_ARGS__);
339 #define SBLOCKSZ (2048)
340 uint64_t kdp_dump_start_time
= 0;
341 uint64_t kdp_min_superblock_dump_time
= ~1ULL;
342 uint64_t kdp_max_superblock_dump_time
= 0;
343 uint64_t kdp_superblock_dump_time
= 0;
344 uint64_t kdp_superblock_dump_start_time
= 0;
349 kdp_ml_enter_debugger_wrapper(__unused
void *param0
, __unused
void *param1
)
351 kdp_ml_enter_debugger();
355 kdp_timer_callout_init(void)
357 kdp_timer_call
= thread_call_allocate(kdp_ml_enter_debugger_wrapper
, NULL
);
361 /* only send/receive data if the link is up */
365 static int first
= 0;
367 if (!kdp_en_linkstatus
) {
371 while (((*kdp_en_linkstatus
)() & LINK_UP_STATUS
) != LINK_UP_STATUS
) {
377 printf("Waiting for link to become available.\n");
378 kprintf("Waiting for link to become available.\n");
384 kdp_send_data(void *packet
, unsigned int len
)
387 (*kdp_en_send_pkt
)(packet
, len
);
392 kdp_receive_data(void *packet
, unsigned int *len
, unsigned int timeout
)
395 (*kdp_en_recv_pkt
)(packet
, len
, timeout
);
400 kdp_register_link(kdp_link_t link
, kdp_mode_t mode
)
402 kdp_en_linkstatus
= link
;
403 kdp_en_setmode
= mode
;
407 kdp_unregister_link(__unused kdp_link_t link
, __unused kdp_mode_t mode
)
409 kdp_en_linkstatus
= NULL
;
410 kdp_en_setmode
= NULL
;
414 kdp_register_send_receive(
416 kdp_receive_t receive
)
418 unsigned int debug
= 0;
420 PE_parse_boot_argn("debug", &debug
, sizeof(debug
));
422 #if defined(__arm__) || defined(__arm64__)
424 uint32_t debug_flags
;
426 if (!PE_i_can_has_debugger(&debug_flags
)) {
436 kdp_en_send_pkt
= send
;
437 kdp_en_recv_pkt
= receive
;
439 if (debug
& DB_KDP_BP_DIS
) {
440 kdp_flag
|= KDP_BP_DIS
;
442 if (debug
& DB_KDP_GETC_ENA
) {
443 kdp_flag
|= KDP_GETC_ENA
;
445 if (debug
& DB_ARP
) {
449 if (debug
& DB_KERN_DUMP_ON_PANIC
) {
450 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
452 if (debug
& DB_KERN_DUMP_ON_NMI
) {
453 kdp_flag
|= PANIC_CORE_ON_NMI
;
456 if (debug
& DB_DBG_POST_CORE
) {
457 kdp_flag
|= DBG_POST_CORE
;
460 if (debug
& DB_PANICLOG_DUMP
) {
461 kdp_flag
|= PANIC_LOG_DUMP
;
464 kdp_corezip_disabled
= (0 != (debug
& DB_DISABLE_GZIP_CORE
));
466 if (PE_parse_boot_argn("_panicd_ip", panicd_ip_str
, sizeof(panicd_ip_str
))) {
467 panicd_specified
= TRUE
;
470 if ((debug
& DB_REBOOT_POST_CORE
) && (panicd_specified
== TRUE
)) {
471 kdp_flag
|= REBOOT_POST_CORE
;
474 if (PE_parse_boot_argn("_router_ip", router_ip_str
, sizeof(router_ip_str
))) {
475 router_specified
= TRUE
;
478 if (!PE_parse_boot_argn("panicd_port", &panicd_port
, sizeof(panicd_port
))) {
479 panicd_port
= CORE_REMOTE_PORT
;
482 if (PE_parse_boot_argn("_panicd_corename", &corename_str
, sizeof(corename_str
))) {
483 corename_specified
= TRUE
;
486 kdp_flag
|= KDP_READY
;
488 current_debugger
= KDP_CUR_DB
;
489 if ((kdp_current_ip_address
!= 0) && halt_in_debugger
) {
491 halt_in_debugger
= 0;
496 kdp_unregister_send_receive(
497 __unused kdp_send_t send
,
498 __unused kdp_receive_t receive
)
500 if (current_debugger
== KDP_CUR_DB
) {
501 current_debugger
= NO_CUR_DB
;
503 kdp_flag
&= ~KDP_READY
;
504 kdp_en_send_pkt
= NULL
;
505 kdp_en_recv_pkt
= NULL
;
509 kdp_schedule_debugger_reentry(unsigned interval
)
513 clock_interval_to_deadline(interval
, 1000 * 1000, &deadline
);
514 thread_call_enter_delayed(kdp_timer_call
, deadline
);
523 bcopy((char *)src
, (char *)dst
, sizeof(struct kdp_ether_addr
));
526 static unsigned short
532 unsigned int high
, low
, sum
;
542 sum
= (high
<< 8) + low
;
543 sum
= (sum
>> 16) + (sum
& 65535);
545 return sum
> 65535 ? sum
- 65535 : sum
;
550 unsigned short reply_port
,
551 const boolean_t sideband
554 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
555 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
556 struct kdp_in_addr tmp_ipaddr
;
557 struct kdp_ether_addr tmp_enaddr
;
558 struct kdp_ether_header
*eh
= NULL
;
561 kdp_panic("kdp_reply");
564 pkt
.off
-= (unsigned int)sizeof(struct kdp_udpiphdr
);
567 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
569 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
571 ui
->ui_next
= ui
->ui_prev
= 0;
573 ui
->ui_pr
= IPPROTO_UDP
;
574 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof(struct kdp_udphdr
));
575 tmp_ipaddr
= ui
->ui_src
;
576 ui
->ui_src
= ui
->ui_dst
;
577 ui
->ui_dst
= tmp_ipaddr
;
578 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
579 ui
->ui_dport
= reply_port
;
580 ui
->ui_ulen
= ui
->ui_len
;
583 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
584 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
586 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
588 ip
->ip_len
= htons(sizeof(struct kdp_udpiphdr
) + pkt
.len
);
589 ip
->ip_v
= IPVERSION
;
590 ip
->ip_id
= htons(ip_id
++);
591 ip
->ip_hl
= sizeof(struct kdp_ip
) >> 2;
592 ip
->ip_ttl
= udp_ttl
;
594 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
596 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
599 pkt
.len
+= (unsigned int)sizeof(struct kdp_udpiphdr
);
601 pkt
.off
-= (unsigned int)sizeof(struct kdp_ether_header
);
603 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
604 enaddr_copy(eh
->ether_shost
, &tmp_enaddr
);
605 enaddr_copy(eh
->ether_dhost
, eh
->ether_shost
);
606 enaddr_copy(&tmp_enaddr
, eh
->ether_dhost
);
607 eh
->ether_type
= htons(ETHERTYPE_IP
);
609 pkt
.len
+= (unsigned int)sizeof(struct kdp_ether_header
);
611 // save reply for possible retransmission
612 assert(pkt
.len
<= KDP_MAXPACKET
);
614 bcopy((char *)&pkt
, (char *)&saved_reply
, sizeof(saved_reply
));
617 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
619 // increment expected sequence number
627 unsigned short remote_port
630 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
631 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
632 struct kdp_ether_header
*eh
;
635 kdp_panic("kdp_send");
638 pkt
.off
-= (unsigned int)sizeof(struct kdp_udpiphdr
);
641 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
643 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
645 ui
->ui_next
= ui
->ui_prev
= 0;
647 ui
->ui_pr
= IPPROTO_UDP
;
648 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof(struct kdp_udphdr
));
649 ui
->ui_src
= adr
.loc
.in
;
650 ui
->ui_dst
= adr
.rmt
.in
;
651 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
652 ui
->ui_dport
= remote_port
;
653 ui
->ui_ulen
= ui
->ui_len
;
656 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
657 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
659 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
661 ip
->ip_len
= htons(sizeof(struct kdp_udpiphdr
) + pkt
.len
);
662 ip
->ip_v
= IPVERSION
;
663 ip
->ip_id
= htons(ip_id
++);
664 ip
->ip_hl
= sizeof(struct kdp_ip
) >> 2;
665 ip
->ip_ttl
= udp_ttl
;
667 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
669 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
672 pkt
.len
+= (unsigned int)sizeof(struct kdp_udpiphdr
);
674 pkt
.off
-= (unsigned int)sizeof(struct kdp_ether_header
);
676 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
677 enaddr_copy(&adr
.loc
.ea
, eh
->ether_shost
);
678 enaddr_copy(&adr
.rmt
.ea
, eh
->ether_dhost
);
679 eh
->ether_type
= htons(ETHERTYPE_IP
);
681 pkt
.len
+= (unsigned int)sizeof(struct kdp_ether_header
);
682 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
687 debugger_if_necessary(void)
689 if ((current_debugger
== KDP_CUR_DB
) && halt_in_debugger
) {
691 halt_in_debugger
= 0;
696 /* We don't interpret this pointer, we just give it to the bsd stack
697 * so it can decide when to set the MAC and IP info. We'll
698 * early initialize the MAC/IP info if we can so that we can use
699 * KDP early in boot. These values may subsequently get over-written
700 * when the interface gets initialized for real.
703 kdp_set_interface(void *ifp
, const struct kdp_ether_addr
*macaddr
)
706 struct kdp_in_addr addr
= { .s_addr
= 0 };
709 kdp_current_ifp
= ifp
;
711 if (PE_parse_boot_argn("kdp_ip_addr", kdpstr
, sizeof(kdpstr
))) {
712 /* look for a static ip address */
713 if (inet_aton(kdpstr
, &addr
) == FALSE
) {
720 /* use saved ip address */
721 save_ip_in_nvram
= TRUE
;
723 len
= sizeof(kdpstr
);
724 if (PEReadNVRAMProperty("_kdp_ipstr", kdpstr
, &len
) == FALSE
) {
728 kdpstr
[len
< sizeof(kdpstr
) ? len
: sizeof(kdpstr
) - 1] = '\0';
729 if (inet_aton(kdpstr
, &addr
) == FALSE
) {
734 kdp_current_ip_address
= addr
.s_addr
;
736 kdp_current_mac_address
= *macaddr
;
739 /* we can't drop into the debugger at this point because the
740 * link will likely not be up. when getDebuggerLinkStatus() support gets
741 * added to the appropriate network drivers, adding the
742 * following will enable this capability:
743 * debugger_if_necessary();
750 kdp_get_interface(void)
752 return kdp_current_ifp
;
756 kdp_set_ip_and_mac_addresses(
757 struct kdp_in_addr
*ipaddr
,
758 struct kdp_ether_addr
*macaddr
)
760 static uint64_t last_time
= (uint64_t) -1;
761 static uint64_t throttle_val
= 0;
765 if (kdp_current_ip_address
== ipaddr
->s_addr
) {
769 /* don't replace if serial debugging is configured */
770 if (!KDP_SERIAL_ENABLED() ||
771 (kdp_current_ip_address
!= KDP_SERIAL_IPADDR
)) {
772 kdp_current_mac_address
= *macaddr
;
773 kdp_current_ip_address
= ipaddr
->s_addr
;
776 if (save_ip_in_nvram
== FALSE
) {
780 if (inet_ntoa_r(*ipaddr
, addr
, sizeof(addr
)) == NULL
) {
784 /* throttle writes if needed */
786 nanoseconds_to_absolutetime(KDP_THROTTLE_VALUE
, &throttle_val
);
789 cur_time
= mach_absolute_time();
790 if (last_time
== (uint64_t) -1 ||
791 ((cur_time
- last_time
) > throttle_val
)) {
792 PEWriteNVRAMProperty("_kdp_ipstr", addr
,
793 (const unsigned int) strlen(addr
));
795 last_time
= cur_time
;
798 debugger_if_necessary();
802 kdp_set_gateway_mac(void *gatewaymac
)
804 router_mac
= *(struct kdp_ether_addr
*)gatewaymac
;
805 flag_router_mac_initialized
= TRUE
;
808 struct kdp_ether_addr
809 kdp_get_mac_addr(void)
811 return kdp_current_mac_address
;
815 kdp_get_ip_address(void)
817 return (unsigned int)kdp_current_ip_address
;
821 kdp_disable_arp(void)
823 kdp_flag
&= ~(DB_ARP
);
827 kdp_arp_dispatch(void)
829 struct kdp_ether_arp aligned_ea
, *ea
= &aligned_ea
;
830 unsigned arp_header_offset
;
832 arp_header_offset
= (unsigned)sizeof(struct kdp_ether_header
) + pkt
.off
;
833 memcpy((void *)ea
, (void *)&pkt
.data
[arp_header_offset
], sizeof(*ea
));
835 switch (ntohs(ea
->arp_op
)) {
840 kdp_process_arp_reply(ea
);
848 kdp_process_arp_reply(struct kdp_ether_arp
*ea
)
850 /* Are we interested in ARP replies? */
851 if (flag_arp_resolved
== TRUE
) {
855 /* Did we receive a reply from the right source? */
856 if (((struct kdp_in_addr
*)(ea
->arp_spa
))->s_addr
!= target_ip
) {
860 flag_arp_resolved
= TRUE
;
861 current_resolved_MAC
= *(struct kdp_ether_addr
*) (ea
->arp_sha
);
866 /* ARP responses are enabled when the DB_ARP bit of the debug boot arg
871 kdp_arp_reply(struct kdp_ether_arp
*ea
)
873 struct kdp_ether_header
*eh
;
875 struct kdp_in_addr isaddr
, itaddr
, myaddr
;
876 struct kdp_ether_addr my_enaddr
;
878 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
879 pkt
.off
+= (unsigned int)sizeof(struct kdp_ether_header
);
881 if (ntohs(ea
->arp_op
) != ARPOP_REQUEST
) {
885 myaddr
.s_addr
= kdp_get_ip_address();
886 my_enaddr
= kdp_get_mac_addr();
888 if ((ntohl(myaddr
.s_addr
) == 0) ||
889 ((my_enaddr
.ether_addr_octet
[0] & 0xff) == 0
890 && (my_enaddr
.ether_addr_octet
[1] & 0xff) == 0
891 && (my_enaddr
.ether_addr_octet
[2] & 0xff) == 0
892 && (my_enaddr
.ether_addr_octet
[3] & 0xff) == 0
893 && (my_enaddr
.ether_addr_octet
[4] & 0xff) == 0
894 && (my_enaddr
.ether_addr_octet
[5] & 0xff) == 0
899 (void)memcpy((void *)&isaddr
, (void *)ea
->arp_spa
, sizeof(isaddr
));
900 (void)memcpy((void *)&itaddr
, (void *)ea
->arp_tpa
, sizeof(itaddr
));
902 if (itaddr
.s_addr
== myaddr
.s_addr
) {
903 (void)memcpy((void *)ea
->arp_tha
, (void *)ea
->arp_sha
, sizeof(ea
->arp_sha
));
904 (void)memcpy((void *)ea
->arp_sha
, (void *)&my_enaddr
, sizeof(ea
->arp_sha
));
906 (void)memcpy((void *)ea
->arp_tpa
, (void *) ea
->arp_spa
, sizeof(ea
->arp_spa
));
907 (void)memcpy((void *)ea
->arp_spa
, (void *) &itaddr
, sizeof(ea
->arp_spa
));
909 ea
->arp_op
= htons(ARPOP_REPLY
);
910 ea
->arp_pro
= htons(ETHERTYPE_IP
);
911 (void)memcpy(eh
->ether_dhost
, ea
->arp_tha
, sizeof(eh
->ether_dhost
));
912 (void)memcpy(eh
->ether_shost
, &my_enaddr
, sizeof(eh
->ether_shost
));
913 eh
->ether_type
= htons(ETHERTYPE_ARP
);
914 (void)memcpy(&pkt
.data
[pkt
.off
], ea
, sizeof(*ea
));
915 pkt
.off
-= (unsigned int)sizeof(struct kdp_ether_header
);
916 /* pkt.len is still the length we want, ether_header+ether_arp */
917 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
924 struct kdp_ether_header
*eh
= NULL
;
925 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
926 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
927 static int msg_printed
;
930 kdp_panic("kdp_poll");
933 if (!kdp_en_recv_pkt
|| !kdp_en_send_pkt
) {
934 if (msg_printed
== 0) {
936 printf("kdp_poll: no debugger device\n");
941 pkt
.off
= pkt
.len
= 0;
942 kdp_receive_data(pkt
.data
, &pkt
.len
, 3 /* ms */);
948 if (pkt
.len
>= sizeof(struct kdp_ether_header
)) {
949 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
951 if (kdp_flag
& KDP_ARP
) {
952 if (ntohs(eh
->ether_type
) == ETHERTYPE_ARP
) {
959 if (pkt
.len
< (sizeof(struct kdp_ether_header
) + sizeof(struct kdp_udpiphdr
))) {
963 pkt
.off
+= (unsigned int)sizeof(struct kdp_ether_header
);
964 if (ntohs(eh
->ether_type
) != ETHERTYPE_IP
) {
969 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
970 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
972 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
973 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
976 pkt
.off
+= (unsigned int)sizeof(struct kdp_udpiphdr
);
977 if (ui
->ui_pr
!= IPPROTO_UDP
) {
981 if (ip
->ip_hl
> (sizeof(struct kdp_ip
) >> 2)) {
985 if (ntohs(ui
->ui_dport
) != KDP_REMOTE_PORT
) {
986 if (panicd_port
== (ntohs(ui
->ui_dport
)) &&
987 flag_panic_dump_in_progress
) {
988 last_panic_port
= ui
->ui_sport
;
993 /* If we receive a kernel debugging packet whilst a
994 * core dump is in progress, abort the transfer and
995 * enter the debugger if not told otherwise.
997 else if (flag_panic_dump_in_progress
) {
998 if (!flag_dont_abort_panic_dump
) {
999 abort_panic_transfer();
1004 if (!kdp
.is_conn
&& !flag_panic_dump_in_progress
) {
1005 enaddr_copy(eh
->ether_dhost
, &adr
.loc
.ea
);
1006 adr
.loc
.in
= ui
->ui_dst
;
1008 enaddr_copy(eh
->ether_shost
, &adr
.rmt
.ea
);
1009 adr
.rmt
.in
= ui
->ui_src
;
1013 * Calculate kdp packet length.
1015 pkt
.len
= ntohs((u_short
)ui
->ui_ulen
) - (unsigned int)sizeof(struct kdp_udphdr
);
1020 /* Create and transmit an ARP resolution request for the target IP address.
1021 * This is modeled on ether_inet_arp()/RFC 826.
1025 transmit_ARP_request(uint32_t ip_addr
)
1027 struct kdp_ether_header
*eh
= (struct kdp_ether_header
*) &pkt
.data
[0];
1028 struct kdp_ether_arp
*ea
= (struct kdp_ether_arp
*) &pkt
.data
[sizeof(struct kdp_ether_header
)];
1030 KDP_DEBUG("Transmitting ARP request\n");
1031 /* Populate the ether_header */
1032 eh
->ether_type
= htons(ETHERTYPE_ARP
);
1033 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
1034 enaddr_copy(ðerbroadcastaddr
, eh
->ether_dhost
);
1036 /* Populate the ARP header */
1037 ea
->arp_pro
= htons(ETHERTYPE_IP
);
1038 ea
->arp_hln
= sizeof(ea
->arp_sha
);
1039 ea
->arp_pln
= sizeof(ea
->arp_spa
);
1040 ea
->arp_hrd
= htons(ARPHRD_ETHER
);
1041 ea
->arp_op
= htons(ARPOP_REQUEST
);
1044 enaddr_copy(ðerbroadcastaddr
, ea
->arp_tha
);
1045 memcpy(ea
->arp_tpa
, (void *) &ip_addr
, sizeof(ip_addr
));
1048 enaddr_copy(&kdp_current_mac_address
, ea
->arp_sha
);
1049 memcpy(ea
->arp_spa
, (void *) &kdp_current_ip_address
, sizeof(kdp_current_ip_address
));
1052 pkt
.len
= sizeof(struct kdp_ether_header
) + sizeof(struct kdp_ether_arp
);
1054 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
1058 kdp_arp_resolve(uint32_t arp_target_ip
, struct kdp_ether_addr
*resolved_MAC
)
1060 int poll_count
= 256; /* ~770 ms modulo broadcast/delayed traffic? */
1063 #define NUM_ARP_TX_RETRIES 5
1065 target_ip
= arp_target_ip
;
1066 flag_arp_resolved
= FALSE
;
1069 pkt
.off
= pkt
.len
= 0;
1073 if (tretries
>= NUM_ARP_TX_RETRIES
) {
1077 KDP_DEBUG("ARP TX attempt #%d \n", tretries
);
1079 transmit_ARP_request(arp_target_ip
);
1081 while (!pkt
.input
&& !flag_arp_resolved
&& flag_panic_dump_in_progress
&& --poll_count
) {
1085 if (flag_arp_resolved
) {
1086 *resolved_MAC
= current_resolved_MAC
;
1090 if (!flag_panic_dump_in_progress
|| pkt
.input
) { /* we received a debugging packet, bail*/
1091 printf("Received a debugger packet,transferring control to debugger\n");
1092 /* Indicate that we should wait in the debugger when we return */
1093 kdp_flag
|= DBG_POST_CORE
;
1096 } else { /* We timed out */
1097 if (0 == poll_count
) {
1099 goto TRANSMIT_RETRY
;
1110 unsigned short reply_port
;
1111 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
1113 kdp
.saved_state
= saved_state
; // see comment in kdp_raise_exception
1116 while (!pkt
.input
) {
1121 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
1123 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
1126 // ignore replies -- we're not expecting them anyway.
1127 if (hdr
->is_reply
) {
1131 if (hdr
->request
== KDP_REATTACH
) {
1132 exception_seq
= hdr
->seq
;
1135 // check for retransmitted request
1136 if (hdr
->seq
== (exception_seq
- 1)) {
1137 /* retransmit last reply */
1138 kdp_send_data(&saved_reply
.data
[saved_reply
.off
],
1141 } else if ((hdr
->seq
!= exception_seq
) &&
1142 (hdr
->request
!= KDP_CONNECT
)) {
1143 printf("kdp: bad sequence %d (want %d)\n",
1144 hdr
->seq
, exception_seq
);
1148 /* This is a manual side-channel to the main KDP protocol.
1149 * A client like GDB/kgmacros can manually construct
1150 * a request, set the input flag, issue a dummy KDP request,
1151 * and then manually collect the result
1153 if (manual_pkt
.input
) {
1154 kdp_hdr_t
*manual_hdr
= (kdp_hdr_t
*)&manual_pkt
.data
;
1155 unsigned short manual_port_unused
= 0;
1156 if (!manual_hdr
->is_reply
) {
1158 int packet_length
= manual_pkt
.len
;
1159 kdp_packet((unsigned char *)&manual_pkt
.data
,
1161 &manual_port_unused
);
1162 manual_pkt
.len
= packet_length
;
1164 manual_pkt
.input
= 0;
1167 if (kdp_packet((unsigned char*)&pkt
.data
[pkt
.off
],
1169 (unsigned short *)&reply_port
)) {
1170 boolean_t sideband
= FALSE
;
1172 /* if it's an already connected error message,
1173 * send a sideband reply for that. for successful connects,
1174 * make sure the sequence number is correct. */
1175 if (hdr
->request
== KDP_CONNECT
) {
1176 kdp_connect_reply_t
*rp
=
1177 (kdp_connect_reply_t
*) &pkt
.data
[pkt
.off
];
1178 kdp_error_t err
= rp
->error
;
1180 if (err
== KDPERR_NO_ERROR
) {
1181 exception_seq
= hdr
->seq
;
1182 } else if (err
== KDPERR_ALREADY_CONNECTED
) {
1187 kdp_reply(reply_port
, sideband
);
1192 } while (kdp
.is_halted
);
1196 kdp_connection_wait(void)
1198 unsigned short reply_port
;
1199 struct kdp_ether_addr kdp_mac_addr
= kdp_get_mac_addr();
1200 unsigned int ip_addr
= ntohl(kdp_get_ip_address());
1203 * Do both a printf() and a kprintf() of the MAC and IP so that
1204 * they will print out on headless machines but not be added to
1208 if (KDP_SERIAL_ENABLED()) {
1209 printf("Using serial KDP.\n");
1210 kprintf("Using serial KDP.\n");
1212 printf("ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1213 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
1214 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
1215 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
1216 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
1217 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
1218 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
1220 kprintf("ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1221 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
1222 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
1223 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
1224 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
1225 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
1226 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
1228 printf("ip address: %d.%d.%d.%d\n",
1229 (ip_addr
& 0xff000000) >> 24,
1230 (ip_addr
& 0xff0000) >> 16,
1231 (ip_addr
& 0xff00) >> 8,
1234 kprintf("ip address: %d.%d.%d.%d\n",
1235 (ip_addr
& 0xff000000) >> 24,
1236 (ip_addr
& 0xff0000) >> 16,
1237 (ip_addr
& 0xff00) >> 8,
1241 printf("\nWaiting for remote debugger connection.\n");
1242 kprintf("\nWaiting for remote debugger connection.\n");
1245 printf("\nPlease go to https://panic.apple.com to report this panic\n");
1248 if (reattach_wait
== 0) {
1249 if ((kdp_flag
& KDP_GETC_ENA
) && (0 != kdp_getc())) {
1250 printf("Options..... Type\n");
1251 printf("------------ ----\n");
1252 printf("continue.... 'c'\n");
1253 printf("reboot...... 'r'\n");
1262 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
1264 while (!pkt
.input
) {
1265 if (kdp_flag
& KDP_GETC_ENA
) {
1266 switch (kdp_getc()) {
1268 printf("Continuing...\n");
1271 printf("Rebooting...\n");
1272 kdp_machine_reboot();
1282 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
1284 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
1286 if (hdr
->request
== KDP_HOSTREBOOT
) {
1287 kdp_machine_reboot();
1288 /* should not return! */
1290 if (((hdr
->request
== KDP_CONNECT
) || (hdr
->request
== KDP_REATTACH
)) &&
1291 !hdr
->is_reply
&& (hdr
->seq
== exception_seq
)) {
1292 if (kdp_packet((unsigned char *)&pkt
.data
[pkt
.off
],
1294 (unsigned short *)&reply_port
)) {
1295 kdp_reply(reply_port
, FALSE
);
1297 if (hdr
->request
== KDP_REATTACH
) {
1299 hdr
->request
= KDP_DISCONNECT
;
1305 } while (!kdp
.is_conn
);
1307 if (current_debugger
== KDP_CUR_DB
) {
1308 active_debugger
= 1;
1310 printf("Connected to remote debugger.\n");
1311 kprintf("Connected to remote debugger.\n");
1316 unsigned int exception
,
1318 unsigned int subcode
1321 unsigned short remote_port
;
1322 unsigned int timeout_count
= 100;
1323 unsigned int poll_timeout
;
1326 pkt
.off
= sizeof(struct kdp_ether_header
) + sizeof(struct kdp_udpiphdr
);
1327 kdp_exception((unsigned char *)&pkt
.data
[pkt
.off
],
1329 (unsigned short *)&remote_port
,
1330 (unsigned int)exception
,
1332 (unsigned int)subcode
);
1334 kdp_send(remote_port
);
1337 while (!pkt
.input
&& poll_timeout
) {
1343 if (!kdp_exception_ack(&pkt
.data
[pkt
.off
], pkt
.len
)) {
1350 if (kdp
.exception_ack_needed
) {
1351 kdp_us_spin(250000);
1353 } while (kdp
.exception_ack_needed
&& timeout_count
--);
1355 if (kdp
.exception_ack_needed
) {
1356 // give up & disconnect
1357 printf("kdp: exception ack timeout\n");
1358 if (current_debugger
== KDP_CUR_DB
) {
1359 active_debugger
= 0;
1367 unsigned int exception
,
1369 unsigned int subcode
,
1374 if (saved_state
== 0) {
1375 printf("kdp_raise_exception with NULL state\n");
1379 if (exception
!= EXC_BREAKPOINT
) {
1380 if (exception
> EXC_BREAKPOINT
|| exception
< EXC_BAD_ACCESS
) {
1383 printf("%s exception (%x,%x,%x)\n",
1384 exception_message
[index
],
1385 exception
, code
, subcode
);
1390 /* XXX WMG it seems that sometimes it doesn't work to let kdp_handler
1391 * do this. I think the client and the host can get out of sync.
1393 kdp
.saved_state
= saved_state
;
1394 kdp
.kdp_cpu
= cpu_number();
1395 kdp
.kdp_thread
= current_thread();
1397 if (kdp_en_setmode
) {
1398 (*kdp_en_setmode
)(TRUE
); /* enabling link mode */
1401 kdp_panic("kdp_raise_exception");
1404 if (((kdp_flag
& KDP_PANIC_DUMP_ENABLED
)
1405 || (kdp_flag
& PANIC_LOG_DUMP
))
1406 && panic_active()) {
1408 if (kdp_flag
& REBOOT_POST_CORE
&& dumped_kernel_core()) {
1409 kdp_machine_reboot();
1412 if ((kdp_flag
& PANIC_CORE_ON_NMI
) && !panic_active()
1414 disableConsoleOutput
= FALSE
;
1416 if (kdp_flag
& REBOOT_POST_CORE
&& dumped_kernel_core()) {
1417 kdp_machine_reboot();
1420 if (!(kdp_flag
& DBG_POST_CORE
)) {
1421 goto exit_debugger_loop
;
1428 kdp_connection_wait();
1430 kdp_send_exception(exception
, code
, subcode
);
1431 if (kdp
.exception_ack_needed
) {
1432 kdp
.exception_ack_needed
= FALSE
;
1433 kdp_remove_all_breakpoints();
1434 printf("Remote debugger disconnected.\n");
1439 kdp
.is_halted
= TRUE
; /* XXX */
1440 kdp_handler(saved_state
);
1442 kdp_remove_all_breakpoints();
1443 printf("Remote debugger disconnected.\n");
1446 /* Allow triggering a panic core dump when connected to the machine
1447 * Continuing after setting kdp_trigger_core_dump should do the
1451 if (1 == kdp_trigger_core_dump
) {
1452 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
1454 if (kdp_flag
& REBOOT_POST_CORE
&& dumped_kernel_core()) {
1455 kdp_machine_reboot();
1457 kdp_trigger_core_dump
= 0;
1460 /* Trigger a reboot if the user has set this flag through the
1461 * debugger.Ideally, this would be done through the HOSTREBOOT packet
1462 * in the protocol,but that will need gdb support,and when it's
1463 * available, it should work automatically.
1465 if (1 == flag_kdp_trigger_reboot
) {
1466 kdp_machine_reboot();
1467 /* If we're still around, reset the flag */
1468 flag_kdp_trigger_reboot
= 0;
1471 if (kdp_reentry_deadline
) {
1472 kdp_schedule_debugger_reentry(kdp_reentry_deadline
);
1473 printf("Debugger re-entry scheduled in %d milliseconds\n", kdp_reentry_deadline
);
1474 kdp_reentry_deadline
= 0;
1479 if (reattach_wait
== 1) {
1484 if (kdp_en_setmode
) {
1485 (*kdp_en_setmode
)(FALSE
); /* link cleanup */
1492 kdp
.reply_port
= kdp
.exception_port
= 0;
1493 kdp
.is_halted
= kdp
.is_conn
= FALSE
;
1494 kdp
.exception_seq
= kdp
.conn_seq
= 0;
1495 kdp
.session_key
= 0;
1496 pkt
.input
= manual_pkt
.input
= FALSE
;
1497 pkt
.len
= pkt
.off
= manual_pkt
.len
= 0;
1501 create_panic_header(unsigned int request
, const char *corename
,
1502 unsigned length
, unsigned int block
)
1504 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
1505 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
1506 struct kdp_ether_header
*eh
;
1507 struct corehdr
*coreh
;
1508 const char *mode
= "octet";
1509 char modelen
= strlen(mode
) + 1;
1511 size_t fmask_size
= sizeof(KDP_FEATURE_MASK_STRING
) + sizeof(kdp_crashdump_feature_mask
);
1513 pkt
.off
= sizeof(struct kdp_ether_header
);
1514 pkt
.len
= (unsigned int)(length
+ ((request
== KDP_WRQ
) ? modelen
+ fmask_size
: 0) +
1515 (corename
? (strlen(corename
) + 1): 0) + sizeof(struct corehdr
));
1518 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
1520 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
1522 ui
->ui_next
= ui
->ui_prev
= 0;
1524 ui
->ui_pr
= IPPROTO_UDP
;
1525 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof(struct kdp_udphdr
));
1526 ui
->ui_src
.s_addr
= (uint32_t)kdp_current_ip_address
;
1527 /* Already in network byte order via inet_aton() */
1528 ui
->ui_dst
.s_addr
= panic_server_ip
;
1529 ui
->ui_sport
= htons(panicd_port
);
1530 ui
->ui_dport
= ((request
== KDP_WRQ
) ? htons(panicd_port
) : last_panic_port
);
1531 ui
->ui_ulen
= ui
->ui_len
;
1534 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
1535 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
1537 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
1539 ip
->ip_len
= htons(sizeof(struct kdp_udpiphdr
) + pkt
.len
);
1540 ip
->ip_v
= IPVERSION
;
1541 ip
->ip_id
= htons(ip_id
++);
1542 ip
->ip_hl
= sizeof(struct kdp_ip
) >> 2;
1543 ip
->ip_ttl
= udp_ttl
;
1545 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
1547 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
1550 pkt
.len
+= (unsigned int)sizeof(struct kdp_udpiphdr
);
1552 pkt
.off
+= (unsigned int)sizeof(struct kdp_udpiphdr
);
1554 coreh
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1555 coreh
->th_opcode
= htons((u_short
)request
);
1557 if (request
== KDP_WRQ
) {
1559 size_t length_remaining
= (sizeof(pkt
.data
) - pkt
.off
), bytes_filled
= 0;
1561 cp
= coreh
->th_u
.tu_rpl
;
1562 bytes_filled
= strlcpy(cp
, corename
, length_remaining
);
1565 /* account for the extra NULL character that has been added historically */
1566 length_remaining
-= (bytes_filled
+ 1);
1568 bytes_filled
= strlcpy(cp
, mode
, length_remaining
);
1571 /* account for the extra NULL character that has been added historically */
1572 length_remaining
-= (bytes_filled
+ 1);
1574 bytes_filled
= strlcpy(cp
, KDP_FEATURE_MASK_STRING
, length_remaining
);
1577 /* account for the extra NULL character that has been added historically */
1578 length_remaining
-= (bytes_filled
+ 1);
1580 bcopy(&kdp_crashdump_feature_mask
, cp
, sizeof(kdp_crashdump_feature_mask
));
1581 kdp_crashdump_pkt_size
= KDP_LARGE_CRASHDUMP_PKT_SIZE
;
1582 cp
+= sizeof(kdp_crashdump_feature_mask
);
1583 length_remaining
-= sizeof(kdp_crashdump_feature_mask
);
1585 PE_parse_boot_argn("kdp_crashdump_pkt_size", &kdp_crashdump_pkt_size
, sizeof(kdp_crashdump_pkt_size
));
1586 *(uint32_t *)cp
= htonl(kdp_crashdump_pkt_size
);
1588 coreh
->th_block
= htonl((unsigned int) block
);
1591 pkt
.off
-= (unsigned int)sizeof(struct kdp_udpiphdr
);
1592 pkt
.off
-= (unsigned int)sizeof(struct kdp_ether_header
);
1594 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
1595 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
1596 enaddr_copy(&destination_mac
, eh
->ether_dhost
);
1597 eh
->ether_type
= htons(ETHERTYPE_IP
);
1599 pkt
.len
+= (unsigned int)sizeof(struct kdp_ether_header
);
1604 kdp_send_crashdump_seek(char *corename
, uint64_t seek_off
)
1608 if (kdp_feature_large_crashdumps
) {
1609 panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, corename
,
1613 uint32_t off
= (uint32_t) seek_off
;
1614 panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, corename
,
1618 if (panic_error
< 0) {
1619 printf("kdp_send_crashdump_pkt failed with error %d\n",
1624 return KERN_SUCCESS
;
1628 kdp_send_crashdump_data(unsigned int request
, char *corename
,
1629 uint64_t length
, void * txstart
)
1631 int panic_error
= 0;
1633 while ((length
> 0) || !txstart
) {
1634 uint64_t chunk
= MIN(kdp_crashdump_pkt_size
, length
);
1636 panic_error
= kdp_send_crashdump_pkt(request
, corename
, chunk
,
1638 if (panic_error
< 0) {
1639 printf("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
1645 txstart
= (void *)(((uintptr_t) txstart
) + chunk
);
1648 return KERN_SUCCESS
;
1651 uint32_t kdp_crashdump_short_pkt
;
1654 kdp_send_crashdump_pkt(unsigned int request
, char *corename
,
1655 uint64_t length
, void *panic_data
)
1658 struct corehdr
*th
= NULL
;
1659 char rretries
, tretries
;
1661 if (kdp_dump_start_time
== 0) {
1662 kdp_dump_start_time
= mach_absolute_time();
1663 kdp_superblock_dump_start_time
= kdp_dump_start_time
;
1666 tretries
= rretries
= 0;
1667 poll_count
= KDP_CRASHDUMP_POLL_COUNT
;
1668 pkt
.off
= pkt
.len
= 0;
1669 if (request
== KDP_WRQ
) { /* longer timeout for initial request */
1676 if (tretries
>= 15) {
1677 /* The crashdump server is unreachable for some reason. This could be a network
1678 * issue or, if we've been especially unfortunate, we've hit Radar 2760413,
1679 * which is a long standing problem with the IOKit polled mode network driver
1680 * shim which can prevent transmits/receives completely.
1682 printf("Cannot contact panic server, timing out.\n");
1687 printf("TX retry #%d ", tretries
);
1690 th
= create_panic_header(request
, corename
, (unsigned)length
, panic_block
);
1692 if (request
== KDP_DATA
) {
1693 /* as all packets are kdp_crashdump_pkt_size in length, the last packet
1694 * may end up with trailing bits. make sure that those
1695 * bits aren't confusing. */
1696 if (length
< kdp_crashdump_pkt_size
) {
1697 kdp_crashdump_short_pkt
++;
1698 memset(th
->th_data
+ length
, 'Y',
1699 kdp_crashdump_pkt_size
- (uint32_t) length
);
1702 if (!kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)panic_data
, (caddr_t
) th
->th_data
, length
)) {
1703 uintptr_t next_page
= round_page((uintptr_t)panic_data
);
1704 memset((caddr_t
) th
->th_data
, 'X', (size_t)length
);
1705 if ((next_page
- ((uintptr_t) panic_data
)) < length
) {
1706 uint64_t resid
= length
- (next_page
- (intptr_t) panic_data
);
1707 if (!kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)next_page
, (caddr_t
) th
->th_data
+ (length
- resid
), resid
)) {
1708 memset((caddr_t
) th
->th_data
+ (length
- resid
), 'X', (size_t)resid
);
1712 } else if (request
== KDP_SEEK
) {
1713 if (kdp_feature_large_crashdumps
) {
1714 *(uint64_t *) th
->th_data
= OSSwapHostToBigInt64((*(uint64_t *) panic_data
));
1716 *(unsigned int *) th
->th_data
= htonl(*(unsigned int *) panic_data
);
1720 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
1722 /* Listen for the ACK */
1724 while (!pkt
.input
&& flag_panic_dump_in_progress
&& poll_count
) {
1732 th
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1733 if (request
== KDP_WRQ
) {
1734 uint16_t opcode64
= ntohs(th
->th_opcode
);
1735 uint16_t features64
= (opcode64
& 0xFF00) >> 8;
1736 if ((opcode64
& 0xFF) == KDP_ACK
) {
1737 kdp_feature_large_crashdumps
= features64
& KDP_FEATURE_LARGE_CRASHDUMPS
;
1738 if (features64
& KDP_FEATURE_LARGE_PKT_SIZE
) {
1739 kdp_feature_large_pkt_size
= 1;
1741 kdp_feature_large_pkt_size
= 0;
1742 kdp_crashdump_pkt_size
= 512;
1744 printf("Protocol features: 0x%x\n", (uint32_t) features64
);
1745 th
->th_opcode
= htons(KDP_ACK
);
1748 if (ntohs(th
->th_opcode
) == KDP_ACK
&& ntohl(th
->th_block
) == panic_block
) {
1750 if (ntohs(th
->th_opcode
) == KDP_ERROR
) {
1751 printf("Panic server returned error %d, retrying\n", ntohl(th
->th_code
));
1753 goto TRANSMIT_RETRY
;
1754 } else if (ntohl(th
->th_block
) == (panic_block
- 1)) {
1755 printf("RX retry ");
1756 if (++rretries
> 1) {
1757 goto TRANSMIT_RETRY
;
1763 } else if (!flag_panic_dump_in_progress
) { /* we received a debugging packet, bail*/
1764 printf("Received a debugger packet,transferring control to debugger\n");
1765 /* Configure that if not set ..*/
1766 kdp_flag
|= DBG_POST_CORE
;
1768 } else { /* We timed out */
1769 if (0 == poll_count
) {
1771 kdp_us_spin((tretries
% 4) * panic_timeout
); /* capped linear backoff */
1772 goto TRANSMIT_RETRY
;
1776 if (!(++panic_block
% SBLOCKSZ
)) {
1778 kdb_printf_unbuffered(".");
1779 ctime
= mach_absolute_time();
1780 kdp_superblock_dump_time
= ctime
- kdp_superblock_dump_start_time
;
1781 kdp_superblock_dump_start_time
= ctime
;
1782 if (kdp_superblock_dump_time
> kdp_max_superblock_dump_time
) {
1783 kdp_max_superblock_dump_time
= kdp_superblock_dump_time
;
1785 if (kdp_superblock_dump_time
< kdp_min_superblock_dump_time
) {
1786 kdp_min_superblock_dump_time
= kdp_superblock_dump_time
;
1790 if (request
== KDP_EOF
) {
1791 printf("\nTotal number of packets transmitted: %d\n", panic_block
);
1792 printf("Avg. superblock transfer abstime 0x%llx\n", ((mach_absolute_time() - kdp_dump_start_time
) / panic_block
) * SBLOCKSZ
);
1793 printf("Minimum superblock transfer abstime: 0x%llx\n", kdp_min_superblock_dump_time
);
1794 printf("Maximum superblock transfer abstime: 0x%llx\n", kdp_max_superblock_dump_time
);
1796 return KERN_SUCCESS
;
1802 return (c
> 47) && (c
< 58);
1805 /* Horrid hack to extract xnu version if possible - a much cleaner approach
1806 * would be to have the integrator run a script which would copy the
1807 * xnu version into a string or an int somewhere at project submission
1808 * time - makes assumptions about sizeof(version), but will not fail if
1809 * it changes, but may be incorrect.
1811 /* 2006: Incorporated a change from Darwin user P. Lovell to extract
1812 * the minor kernel version numbers from the version string.
1815 kdp_get_xnu_version(char *versionbuf
)
1821 size_t length_remaining
= (sizeof(pkt
.data
) - pkt
.off
);
1823 strlcpy(vstr
, "custom", 10);
1824 if (kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)version
, versionbuf
, 128)) {
1825 versionbuf
[127] = '\0';
1826 versionpos
= strnstr(versionbuf
, "xnu-", 115);
1828 strncpy(vstr
, versionpos
, sizeof(vstr
));
1829 vstr
[sizeof(vstr
) - 1] = '\0';
1830 vptr
= vstr
+ 4; /* Begin after "xnu-" */
1831 while (*vptr
&& (isdigit(*vptr
) || *vptr
== '.')) {
1835 /* Remove trailing period, if any */
1836 if (*(--vptr
) == '.') {
1842 strlcpy(versionbuf
, vstr
, length_remaining
);
1847 kdp_set_dump_info(const uint32_t flags
, const char *filename
,
1848 const char *destipstr
, const char *routeripstr
,
1849 const uint32_t port
)
1853 if (destipstr
&& (destipstr
[0] != '\0')) {
1854 strlcpy(panicd_ip_str
, destipstr
, sizeof(panicd_ip_str
));
1855 panicd_specified
= 1;
1858 if (routeripstr
&& (routeripstr
[0] != '\0')) {
1859 strlcpy(router_ip_str
, routeripstr
, sizeof(router_ip_str
));
1860 router_specified
= 1;
1863 if (filename
&& (filename
[0] != '\0')) {
1864 strlcpy(corename_str
, filename
, sizeof(corename_str
));
1865 corename_specified
= TRUE
;
1867 corename_specified
= FALSE
;
1874 /* on a disconnect, should we stay in KDP or not? */
1875 noresume_on_disconnect
= (flags
& KDP_DUMPINFO_NORESUME
) ? 1 : 0;
1877 if ((flags
& KDP_DUMPINFO_DUMP
) == 0) {
1881 /* the rest of the commands can modify kdp_flags */
1882 cmd
= flags
& KDP_DUMPINFO_MASK
;
1883 if (cmd
== KDP_DUMPINFO_DISABLE
) {
1884 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
1885 panicd_specified
= 0;
1886 kdp_trigger_core_dump
= 0;
1890 kdp_flag
&= ~REBOOT_POST_CORE
;
1891 if (flags
& KDP_DUMPINFO_REBOOT
) {
1892 kdp_flag
|= REBOOT_POST_CORE
;
1895 kdp_flag
&= ~PANIC_LOG_DUMP
;
1896 if (cmd
== KDP_DUMPINFO_PANICLOG
) {
1897 kdp_flag
|= PANIC_LOG_DUMP
;
1900 kdp_flag
&= ~SYSTEM_LOG_DUMP
;
1901 if (cmd
== KDP_DUMPINFO_SYSTEMLOG
) {
1902 kdp_flag
|= SYSTEM_LOG_DUMP
;
1905 /* trigger a dump */
1906 kdp_flag
|= DBG_POST_CORE
;
1908 flag_dont_abort_panic_dump
= (flags
& KDP_DUMPINFO_NOINTR
) ?
1912 disableConsoleOutput
= 0;
1913 kdp_trigger_core_dump
= 1;
1917 kdp_get_dump_info(kdp_dumpinfo_reply_t
*rp
)
1919 if (panicd_specified
) {
1920 strlcpy(rp
->destip
, panicd_ip_str
,
1921 sizeof(rp
->destip
));
1923 rp
->destip
[0] = '\0';
1926 if (router_specified
) {
1927 strlcpy(rp
->routerip
, router_ip_str
,
1928 sizeof(rp
->routerip
));
1930 rp
->routerip
[0] = '\0';
1933 if (corename_specified
) {
1934 strlcpy(rp
->name
, corename_str
,
1940 rp
->port
= panicd_port
;
1943 if (!panicd_specified
) {
1944 rp
->type
|= KDP_DUMPINFO_DISABLE
;
1945 } else if (kdp_flag
& PANIC_LOG_DUMP
) {
1946 rp
->type
|= KDP_DUMPINFO_PANICLOG
;
1948 rp
->type
|= KDP_DUMPINFO_CORE
;
1951 if (noresume_on_disconnect
) {
1952 rp
->type
|= KDP_DUMPINFO_NORESUME
;
1957 /* Primary dispatch routine for the system dump */
1959 kdp_panic_dump(void)
1961 char coreprefix
[10];
1966 uint32_t current_ip
= ntohl((uint32_t)kdp_current_ip_address
);
1968 if (flag_panic_dump_in_progress
) {
1969 kdb_printf("System dump aborted.\n");
1970 goto panic_dump_exit
;
1973 printf("Entering system dump routine\n");
1975 if (!kdp_en_recv_pkt
|| !kdp_en_send_pkt
) {
1976 kdb_printf("Error: No transport device registered for kernel crashdump\n");
1980 if (!panicd_specified
) {
1981 kdb_printf("A dump server was not specified in the boot-args, terminating kernel core dump.\n");
1982 goto panic_dump_exit
;
1985 flag_panic_dump_in_progress
= TRUE
;
1988 kdp_panic("kdp_panic_dump: unexpected pending input packet");
1991 kdp_get_xnu_version((char *) &pkt
.data
[0]);
1993 if (!corename_specified
) {
1995 /* Panic log bit takes precedence over core dump bit */
1996 if ((debugger_panic_str
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
)) {
1997 strlcpy(coreprefix
, "paniclog", sizeof(coreprefix
));
1998 } else if (kdp_flag
& SYSTEM_LOG_DUMP
) {
1999 strlcpy(coreprefix
, "systemlog", sizeof(coreprefix
));
2001 strlcpy(coreprefix
, "core", sizeof(coreprefix
));
2002 if (!kdp_corezip_disabled
) {
2003 strlcpy(coresuffix
, ".gz", sizeof(coresuffix
));
2007 abstime
= mach_absolute_time();
2008 pkt
.data
[20] = '\0';
2009 snprintf(corename_str
,
2010 sizeof(corename_str
),
2011 "%s-%s-%d.%d.%d.%d-%x%s",
2012 coreprefix
, &pkt
.data
[0],
2013 (current_ip
& 0xff000000) >> 24,
2014 (current_ip
& 0xff0000) >> 16,
2015 (current_ip
& 0xff00) >> 8,
2016 (current_ip
& 0xff),
2017 (unsigned int) (abstime
& 0xffffffff),
2021 if (0 == inet_aton(panicd_ip_str
, (struct kdp_in_addr
*) &panic_server_ip
)) {
2022 kdb_printf("inet_aton() failed interpreting %s as a panic server IP\n", panicd_ip_str
);
2024 kdb_printf("Attempting connection to panic server configured at IP %s, port %d\n", panicd_ip_str
, panicd_port
);
2027 destination_mac
= router_mac
;
2029 if (kdp_arp_resolve(panic_server_ip
, &temp_mac
)) {
2030 kdb_printf("Resolved %s's (or proxy's) link level address\n", panicd_ip_str
);
2031 destination_mac
= temp_mac
;
2033 if (!flag_panic_dump_in_progress
) {
2034 goto panic_dump_exit
;
2036 if (router_specified
) {
2037 if (0 == inet_aton(router_ip_str
, (struct kdp_in_addr
*) &parsed_router_ip
)) {
2038 kdb_printf("inet_aton() failed interpreting %s as an IP\n", router_ip_str
);
2040 router_ip
= parsed_router_ip
;
2041 if (kdp_arp_resolve(router_ip
, &temp_mac
)) {
2042 destination_mac
= temp_mac
;
2043 kdb_printf("Routing through specified router IP %s (%d)\n", router_ip_str
, router_ip
);
2049 if (!flag_panic_dump_in_progress
) {
2050 goto panic_dump_exit
;
2053 kdb_printf("Transmitting packets to link level address: %02x:%02x:%02x:%02x:%02x:%02x\n",
2054 destination_mac
.ether_addr_octet
[0] & 0xff,
2055 destination_mac
.ether_addr_octet
[1] & 0xff,
2056 destination_mac
.ether_addr_octet
[2] & 0xff,
2057 destination_mac
.ether_addr_octet
[3] & 0xff,
2058 destination_mac
.ether_addr_octet
[4] & 0xff,
2059 destination_mac
.ether_addr_octet
[5] & 0xff);
2061 kdb_printf("Kernel map size is %llu\n", (unsigned long long) get_vmmap_size(kernel_map
));
2062 kdb_printf("Sending write request for %s\n", corename_str
);
2064 if ((panic_error
= kdp_send_crashdump_pkt(KDP_WRQ
, corename_str
, 0, NULL
)) < 0) {
2065 kdb_printf("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
2066 goto panic_dump_exit
;
2069 /* Just the panic log requested */
2070 if ((debugger_panic_str
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
)) {
2071 kdb_printf_unbuffered("Transmitting panic log, please wait: ");
2072 kdp_send_crashdump_data(KDP_DATA
, corename_str
,
2073 debug_buf_ptr
- debug_buf_base
,
2075 kdp_send_crashdump_pkt(KDP_EOF
, NULL
, 0, ((void *) 0));
2076 printf("Please file a bug report on this panic, if possible.\n");
2077 goto panic_dump_exit
;
2080 /* maybe we wanted the systemlog */
2081 if (kdp_flag
& SYSTEM_LOG_DUMP
) {
2082 long start_off
= msgbufp
->msg_bufx
;
2085 kdb_printf_unbuffered("Transmitting system log, please wait: ");
2086 if (start_off
>= msgbufp
->msg_bufr
) {
2087 len
= msgbufp
->msg_size
- start_off
;
2088 kdp_send_crashdump_data(KDP_DATA
, corename_str
, len
,
2089 msgbufp
->msg_bufc
+ start_off
);
2090 /* seek to remove trailing bytes */
2091 kdp_send_crashdump_seek(corename_str
, len
);
2095 if (start_off
!= msgbufp
->msg_bufr
) {
2096 len
= msgbufp
->msg_bufr
- start_off
;
2097 kdp_send_crashdump_data(KDP_DATA
, corename_str
, len
,
2098 msgbufp
->msg_bufc
+ start_off
);
2101 kdp_send_crashdump_pkt(KDP_EOF
, NULL
, 0, ((void *) 0));
2102 goto panic_dump_exit
;
2105 /* We want a core dump if we're here */
2106 kern_dump(KERN_DUMP_NET
);
2109 abort_panic_transfer();
2115 begin_panic_transfer(void)
2117 flag_panic_dump_in_progress
= TRUE
;
2121 abort_panic_transfer(void)
2123 flag_panic_dump_in_progress
= FALSE
;
2124 flag_dont_abort_panic_dump
= FALSE
;
2128 #if CONFIG_SERIAL_KDP
2130 static boolean_t needs_serial_init
= TRUE
;
2133 kdp_serial_send(void *rpkt
, unsigned int rpkt_len
)
2136 kdp_serialize_packet((unsigned char *)rpkt
, rpkt_len
, pal_serial_putc_nocr
);
2140 kdp_serial_receive(void *rpkt
, unsigned int *rpkt_len
, unsigned int timeout
)
2143 uint64_t now
, deadline
;
2145 clock_interval_to_deadline(timeout
, 1000 * 1000 /* milliseconds */, &deadline
);
2148 for (clock_get_uptime(&now
); now
< deadline
; clock_get_uptime(&now
)) {
2149 readkar
= pal_serial_getc();
2151 unsigned char *packet
;
2152 // printf("got char %02x\n", readkar);
2153 if ((packet
= kdp_unserialize_packet(readkar
, rpkt_len
))) {
2154 memcpy(rpkt
, packet
, *rpkt_len
);
2163 kdp_serial_setmode(boolean_t active
)
2165 if (active
== FALSE
) { /* leaving KDP */
2169 if (!needs_serial_init
) {
2174 needs_serial_init
= FALSE
;
2180 kdp_serial_callout(__unused
void *arg
, kdp_event_t event
)
2183 * When we stop KDP, set the bit to re-initialize the console serial
2184 * port the next time we send/receive a KDP packet. We don't do it on
2185 * KDP_EVENT_ENTER directly because it also gets called when we trap to
2186 * KDP for non-external debugging, i.e., stackshot or core dumps.
2188 * Set needs_serial_init on exit (and initialization, see above) and not
2189 * enter because enter is sent multiple times and causes excess
2194 case KDP_EVENT_PANICLOG
:
2195 case KDP_EVENT_ENTER
:
2197 case KDP_EVENT_EXIT
:
2198 needs_serial_init
= TRUE
;
2203 #endif /* CONFIG_SERIAL_KDP */
2208 strlcpy(kdp_kernelversion_string
, version
, sizeof(kdp_kernelversion_string
));
2210 /* Relies on platform layer calling panic_init() before kdp_init() */
2211 if (kernel_uuid_string
[0] != '\0') {
2213 * Update kdp_kernelversion_string with our UUID
2214 * generated at link time.
2217 strlcat(kdp_kernelversion_string
, "; UUID=", sizeof(kdp_kernelversion_string
));
2218 strlcat(kdp_kernelversion_string
, kernel_uuid_string
, sizeof(kdp_kernelversion_string
));
2223 #if defined(__x86_64__) || defined(__arm__) || defined(__arm64__)
2224 if (vm_kernel_slide
) {
2225 char KASLR_stext
[19];
2226 strlcat(kdp_kernelversion_string
, "; stext=", sizeof(kdp_kernelversion_string
));
2227 snprintf(KASLR_stext
, sizeof(KASLR_stext
), "%p", (void *) vm_kernel_stext
);
2228 strlcat(kdp_kernelversion_string
, KASLR_stext
, sizeof(kdp_kernelversion_string
));
2232 if (debug_boot_arg
& DB_REBOOT_POST_CORE
) {
2233 kdp_flag
|= REBOOT_POST_CORE
;
2235 #if defined(__x86_64__)
2239 kdp_timer_callout_init();
2240 kdp_crashdump_feature_mask
= htonl(kdp_crashdump_feature_mask
);
2243 #if CONFIG_SERIAL_KDP
2245 struct kdp_in_addr ipaddr
;
2246 struct kdp_ether_addr macaddr
;
2248 boolean_t kdp_match_name_found
= PE_parse_boot_argn("kdp_match_name", kdpname
, sizeof(kdpname
));
2249 boolean_t kdp_not_serial
= kdp_match_name_found
? (strncmp(kdpname
, "serial", sizeof(kdpname
))) : TRUE
;
2252 //respect any custom debugger boot-args
2253 if (kdp_match_name_found
&& kdp_not_serial
) {
2256 #else /* CONFIG_EMBEDDED */
2257 // serial must be explicitly requested
2258 if (!kdp_match_name_found
|| kdp_not_serial
) {
2261 #endif /* CONFIG_EMBEDDED */
2264 if (kdp_not_serial
&& PE_consistent_debug_enabled() && debug_boot_arg
) {
2267 printf("Serial requested, consistent debug disabled or debug boot arg not present, configuring debugging over serial\n");
2269 #endif /* CONFIG_EMBEDDED */
2271 kprintf("Initializing serial KDP\n");
2273 kdp_register_callout(kdp_serial_callout
, NULL
);
2274 kdp_register_link(NULL
, kdp_serial_setmode
);
2275 kdp_register_send_receive(kdp_serial_send
, kdp_serial_receive
);
2277 /* fake up an ip and mac for early serial debugging */
2278 macaddr
.ether_addr_octet
[0] = 's';
2279 macaddr
.ether_addr_octet
[1] = 'e';
2280 macaddr
.ether_addr_octet
[2] = 'r';
2281 macaddr
.ether_addr_octet
[3] = 'i';
2282 macaddr
.ether_addr_octet
[4] = 'a';
2283 macaddr
.ether_addr_octet
[5] = 'l';
2284 ipaddr
.s_addr
= KDP_SERIAL_IPADDR
;
2285 kdp_set_ip_and_mac_addresses(&ipaddr
, &macaddr
);
2287 #endif /* CONFIG_SERIAL_KDP */
2290 #else /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
2295 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
2297 #if !(MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING)
2298 static struct kdp_ether_addr kdp_current_mac_address
= {.ether_addr_octet
= {0, 0, 0, 0, 0, 0}};
2300 /* XXX ugly forward declares to stop warnings */
2301 void *kdp_get_interface(void);
2302 void kdp_set_ip_and_mac_addresses(struct kdp_in_addr
*, struct kdp_ether_addr
*);
2303 void kdp_set_gateway_mac(void *);
2304 void kdp_set_interface(void *);
2305 void kdp_register_send_receive(void *, void *);
2306 void kdp_unregister_send_receive(void *, void *);
2308 uint32_t kdp_stack_snapshot_bytes_traced(void);
2311 kdp_register_send_receive(__unused
void *send
, __unused
void *receive
)
2316 kdp_unregister_send_receive(__unused
void *send
, __unused
void *receive
)
2321 kdp_get_interface( void)
2327 kdp_get_ip_address(void )
2332 struct kdp_ether_addr
2333 kdp_get_mac_addr(void)
2335 return kdp_current_mac_address
;
2339 kdp_set_ip_and_mac_addresses(
2340 __unused
struct kdp_in_addr
*ipaddr
,
2341 __unused
struct kdp_ether_addr
*macaddr
)
2346 kdp_set_gateway_mac(__unused
void *gatewaymac
)
2351 kdp_set_interface(__unused
void *ifp
)
2356 kdp_register_link(__unused kdp_link_t link
, __unused kdp_mode_t mode
)
2361 kdp_unregister_link(__unused kdp_link_t link
, __unused kdp_mode_t mode
)
2365 #endif /* !(MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING) */
2367 #if !CONFIG_KDP_INTERACTIVE_DEBUGGING
2368 extern __attribute__((noreturn
)) void panic_spin_forever(void);
2370 __attribute__((noreturn
))
2372 kdp_raise_exception(
2373 __unused
unsigned int exception
,
2374 __unused
unsigned int code
,
2375 __unused
unsigned int subcode
,
2376 __unused
void *saved_state
2380 kdp_raise_exception(
2381 unsigned int exception
,
2383 unsigned int subcode
,
2389 assert(PE_i_can_has_debugger(NULL
));
2392 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
2394 kdp_debugger_loop(exception
, code
, subcode
, saved_state
);
2395 #else /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
2396 assert(current_debugger
!= KDP_CUR_DB
);
2398 panic_spin_forever();
2399 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */