2 * Copyright (c) 2000-2016 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 #include <kdp/kdp_en_debugger.h>
48 #include <kdp/kdp_callout.h>
49 #include <kdp/kdp_udp.h>
50 #include <kdp/kdp_core.h>
52 #include <kdp/kdp_serial.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_protos.h>
57 #include <vm/vm_kern.h> /* kernel_map */
59 #include <mach/memory_object_types.h>
60 #include <machine/pal_routines.h>
62 #include <sys/msgbuf.h>
64 /* we just want the link status flags, so undef KERNEL_PRIVATE for this
67 #include <net/if_media.h>
68 #define KERNEL_PRIVATE
72 #include <IOKit/IOPlatformExpert.h>
73 #include <libkern/version.h>
77 extern unsigned int not_in_kdp
;
78 extern int kdp_snapshot
;
79 extern void do_stackshot(void);
81 #ifdef CONFIG_KDP_INTERACTIVE_DEBUGGING
83 extern int inet_aton(const char *, struct kdp_in_addr
*); /* in libkern */
84 extern char *inet_ntoa_r(struct kdp_in_addr ina
, char *buf
,
85 size_t buflen
); /* in libkern */
87 #define DO_ALIGN 1 /* align all packet data accesses */
88 #define KDP_SERIAL_IPADDR 0xABADBABE /* IP address used for serial KDP */
89 #define LINK_UP_STATUS (IFM_AVALID | IFM_ACTIVE)
91 extern int kdp_getc(void);
92 extern int reattach_wait
;
94 static u_short ip_id
; /* ip packet ctr, for ids */
96 /* @(#)udp_usrreq.c 2.2 88/05/23 4.0NFSSRC SMI; from UCB 7.1 6/5/86 */
99 * UDP protocol implementation.
100 * Per RFC 768, August, 1980.
102 #define UDP_TTL 60 /* deflt time to live for UDP packets */
103 static int udp_ttl
= UDP_TTL
;
104 static unsigned char exception_seq
;
107 uint32_t ih_next
, ih_prev
; /* for protocol sequence q's */
108 u_char ih_x1
; /* (unused) */
109 u_char ih_pr
; /* protocol */
110 short ih_len
; /* protocol length */
111 struct kdp_in_addr ih_src
; /* source internet address */
112 struct kdp_in_addr ih_dst
; /* destination internet address */
116 u_short uh_sport
; /* source port */
117 u_short uh_dport
; /* destination port */
118 short uh_ulen
; /* udp length */
119 u_short uh_sum
; /* udp checksum */
122 struct kdp_udpiphdr
{
123 struct kdp_ipovly ui_i
; /* overlaid ip structure */
124 struct kdp_udphdr ui_u
; /* udp header */
126 #define ui_next ui_i.ih_next
127 #define ui_prev ui_i.ih_prev
128 #define ui_x1 ui_i.ih_x1
129 #define ui_pr ui_i.ih_pr
130 #define ui_len ui_i.ih_len
131 #define ui_src ui_i.ih_src
132 #define ui_dst ui_i.ih_dst
133 #define ui_sport ui_u.uh_sport
134 #define ui_dport ui_u.uh_dport
135 #define ui_ulen ui_u.uh_ulen
136 #define ui_sum ui_u.uh_sum
143 #ifdef __LITTLE_ENDIAN__
144 ip_xhl
:4, /* header length */
145 ip_xv
:4, /* version */
146 ip_xtos
:8, /* type of service */
147 ip_xlen
:16; /* total length */
149 #ifdef __BIG_ENDIAN__
150 ip_xv
:4, /* version */
151 ip_xhl
:4, /* header length */
152 ip_xtos
:8, /* type of service */
153 ip_xlen
:16; /* total length */
157 u_short ip_id
; /* identification */
158 short ip_off
; /* fragment offset field */
159 #define IP_DF 0x4000 /* dont fragment flag */
160 #define IP_MF 0x2000 /* more fragments flag */
161 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
162 u_char ip_ttl
; /* time to live */
163 u_char ip_p
; /* protocol */
164 u_short ip_sum
; /* checksum */
165 struct kdp_in_addr ip_src
,ip_dst
; /* source and dest address */
167 #define ip_v ip_vhltl.ip_x.ip_xv
168 #define ip_hl ip_vhltl.ip_x.ip_xhl
169 #define ip_tos ip_vhltl.ip_x.ip_xtos
170 #define ip_len ip_vhltl.ip_x.ip_xlen
172 #define IPPROTO_UDP 17
175 #define ETHERTYPE_IP 0x0800 /* IP protocol */
178 * Ethernet Address Resolution Protocol.
180 * See RFC 826 for protocol description. Structure below is adapted
181 * to resolving internet addresses. Field names used correspond to
185 #define ETHERTYPE_ARP 0x0806 /* Addr. resolution protocol */
188 u_short ar_hrd
; /* format of hardware address */
189 #define ARPHRD_ETHER 1 /* ethernet hardware format */
190 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
191 u_short ar_pro
; /* format of protocol address */
192 u_char ar_hln
; /* length of hardware address */
193 u_char ar_pln
; /* length of protocol address */
194 u_short ar_op
; /* one of: */
195 #define ARPOP_REQUEST 1 /* request to resolve address */
196 #define ARPOP_REPLY 2 /* response to previous request */
197 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
198 #define ARPOP_REVREPLY 4 /* response giving protocol address */
199 #define ARPOP_INVREQUEST 8 /* request to identify peer */
200 #define ARPOP_INVREPLY 9 /* response identifying peer */
203 struct kdp_ether_arp
{
204 struct kdp_arphdr ea_hdr
; /* fixed-size header */
205 u_char arp_sha
[ETHER_ADDR_LEN
]; /* sender hardware address */
206 u_char arp_spa
[4]; /* sender protocol address */
207 u_char arp_tha
[ETHER_ADDR_LEN
]; /* target hardware address */
208 u_char arp_tpa
[4]; /* target protocol address */
210 #define arp_hrd ea_hdr.ar_hrd
211 #define arp_pro ea_hdr.ar_pro
212 #define arp_hln ea_hdr.ar_hln
213 #define arp_pln ea_hdr.ar_pln
214 #define arp_op ea_hdr.ar_op
216 #define ETHERMTU 1500
217 #define ETHERHDRSIZE 14
219 #define KDP_MAXPACKET (ETHERHDRSIZE + ETHERMTU + ETHERCRC)
222 unsigned char data
[KDP_MAXPACKET
];
223 unsigned int off
, len
;
227 struct kdp_manual_pkt manual_pkt
;
231 struct kdp_in_addr in
;
232 struct kdp_ether_addr ea
;
235 struct kdp_in_addr in
;
236 struct kdp_ether_addr ea
;
241 *exception_message
[] = {
243 "Memory access", /* EXC_BAD_ACCESS */
244 "Failed instruction", /* EXC_BAD_INSTRUCTION */
245 "Arithmetic", /* EXC_ARITHMETIC */
246 "Emulation", /* EXC_EMULATION */
247 "Software", /* EXC_SOFTWARE */
248 "Breakpoint" /* EXC_BREAKPOINT */
251 volatile int kdp_flag
= 0;
252 boolean_t kdp_corezip_disabled
= 0;
254 kdp_send_t kdp_en_send_pkt
;
255 static kdp_receive_t kdp_en_recv_pkt
;
256 static kdp_link_t kdp_en_linkstatus
;
257 static kdp_mode_t kdp_en_setmode
;
259 #if CONFIG_SERIAL_KDP
260 static void kdp_serial_send(void *rpkt
, unsigned int rpkt_len
);
261 #define KDP_SERIAL_ENABLED() (kdp_en_send_pkt == kdp_serial_send)
263 #define KDP_SERIAL_ENABLED() (0)
266 static uint32_t kdp_current_ip_address
= 0;
267 static struct kdp_ether_addr kdp_current_mac_address
= {{0, 0, 0, 0, 0, 0}};
268 static void *kdp_current_ifp
;
270 static void kdp_handler( void *);
272 static uint32_t panic_server_ip
= 0;
273 static uint32_t parsed_router_ip
= 0;
274 static uint32_t router_ip
= 0;
275 static uint32_t target_ip
= 0;
277 static boolean_t save_ip_in_nvram
= FALSE
;
279 static volatile boolean_t panicd_specified
= FALSE
;
280 static boolean_t router_specified
= FALSE
;
281 static boolean_t corename_specified
= FALSE
;
282 static unsigned int panicd_port
= CORE_REMOTE_PORT
;
284 static struct kdp_ether_addr etherbroadcastaddr
= {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
286 static struct kdp_ether_addr router_mac
= {{0, 0, 0 , 0, 0, 0}};
287 static struct kdp_ether_addr destination_mac
= {{0, 0, 0 , 0, 0, 0}};
288 static struct kdp_ether_addr temp_mac
= {{0, 0, 0 , 0, 0, 0}};
289 static struct kdp_ether_addr current_resolved_MAC
= {{0, 0, 0 , 0, 0, 0}};
291 static boolean_t flag_panic_dump_in_progress
= FALSE
;
292 static boolean_t flag_router_mac_initialized
= FALSE
;
293 static boolean_t flag_dont_abort_panic_dump
= FALSE
;
295 static boolean_t flag_arp_resolved
= FALSE
;
297 static unsigned int panic_timeout
= 100000;
298 static unsigned int last_panic_port
= CORE_REMOTE_PORT
;
300 #define KDP_THROTTLE_VALUE (10ULL * NSEC_PER_SEC)
302 uint32_t kdp_crashdump_pkt_size
= 512;
303 #define KDP_LARGE_CRASHDUMP_PKT_SIZE (1440 - 6 - sizeof(struct kdp_udpiphdr))
304 static char panicd_ip_str
[20];
305 static char router_ip_str
[20];
306 static char corename_str
[100];
308 static unsigned int panic_block
= 0;
309 volatile unsigned int kdp_trigger_core_dump
= 0;
310 __private_extern__
volatile unsigned int flag_kdp_trigger_reboot
= 0;
313 extern unsigned int disableConsoleOutput
;
315 extern void kdp_call(void);
316 extern boolean_t
kdp_call_kdb(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 #if WITH_CONSISTENT_DBG
338 #include <arm/caches_internal.h>
339 extern volatile struct xnu_hw_shmem_dbg_command_info
*hwsd_info
;
342 #define KDP_DEBUG(...) if (gKDPDebug) printf(__VA_ARGS__);
344 #define SBLOCKSZ (2048)
345 uint64_t kdp_dump_start_time
= 0;
346 uint64_t kdp_min_superblock_dump_time
= ~1ULL;
347 uint64_t kdp_max_superblock_dump_time
= 0;
348 uint64_t kdp_superblock_dump_time
= 0;
349 uint64_t kdp_superblock_dump_start_time
= 0;
354 kdp_ml_enter_debugger_wrapper(__unused
void *param0
, __unused
void *param1
) {
355 kdp_ml_enter_debugger();
359 kdp_timer_callout_init(void) {
360 kdp_timer_call
= thread_call_allocate(kdp_ml_enter_debugger_wrapper
, NULL
);
364 /* only send/receive data if the link is up */
368 static int first
= 0;
370 if (!kdp_en_linkstatus
)
373 while (((*kdp_en_linkstatus
)() & LINK_UP_STATUS
) != LINK_UP_STATUS
) {
378 printf("Waiting for link to become available.\n");
379 kprintf("Waiting for link to become available.\n");
385 kdp_send_data(void *packet
, unsigned int len
)
388 (*kdp_en_send_pkt
)(packet
, len
);
393 kdp_receive_data(void *packet
, unsigned int *len
, unsigned int timeout
)
396 (*kdp_en_recv_pkt
)(packet
, len
, timeout
);
401 kdp_register_link(kdp_link_t link
, kdp_mode_t mode
)
403 kdp_en_linkstatus
= link
;
404 kdp_en_setmode
= mode
;
408 kdp_unregister_link(__unused kdp_link_t link
, __unused kdp_mode_t mode
)
410 kdp_en_linkstatus
= NULL
;
411 kdp_en_setmode
= NULL
;
415 kdp_register_send_receive(
417 kdp_receive_t receive
)
419 unsigned int debug
= 0;
421 PE_parse_boot_argn("debug", &debug
, sizeof (debug
));
427 kdp_en_send_pkt
= send
;
428 kdp_en_recv_pkt
= receive
;
430 if (debug
& DB_KDP_BP_DIS
)
431 kdp_flag
|= KDP_BP_DIS
;
432 if (debug
& DB_KDP_GETC_ENA
)
433 kdp_flag
|= KDP_GETC_ENA
;
437 if (debug
& DB_KERN_DUMP_ON_PANIC
)
438 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
439 if (debug
& DB_KERN_DUMP_ON_NMI
)
440 kdp_flag
|= PANIC_CORE_ON_NMI
;
442 if (debug
& DB_DBG_POST_CORE
)
443 kdp_flag
|= DBG_POST_CORE
;
445 if (debug
& DB_PANICLOG_DUMP
)
446 kdp_flag
|= PANIC_LOG_DUMP
;
448 kdp_corezip_disabled
= (0 != (debug
& DB_DISABLE_GZIP_CORE
));
450 if (PE_parse_boot_argn("_panicd_ip", panicd_ip_str
, sizeof (panicd_ip_str
)))
451 panicd_specified
= TRUE
;
453 if ((debug
& DB_REBOOT_POST_CORE
) && (panicd_specified
== TRUE
))
454 kdp_flag
|= REBOOT_POST_CORE
;
456 if (PE_parse_boot_argn("_router_ip", router_ip_str
, sizeof (router_ip_str
)))
457 router_specified
= TRUE
;
459 if (!PE_parse_boot_argn("panicd_port", &panicd_port
, sizeof (panicd_port
)))
460 panicd_port
= CORE_REMOTE_PORT
;
462 if (PE_parse_boot_argn("_panicd_corename", &corename_str
, sizeof (corename_str
)))
463 corename_specified
= TRUE
;
465 kdp_flag
|= KDP_READY
;
467 current_debugger
= KDP_CUR_DB
;
468 if ((kdp_current_ip_address
!= 0) && halt_in_debugger
) {
475 kdp_unregister_send_receive(
476 __unused kdp_send_t send
,
477 __unused kdp_receive_t receive
)
479 if (current_debugger
== KDP_CUR_DB
)
480 current_debugger
= NO_CUR_DB
;
481 kdp_flag
&= ~KDP_READY
;
482 kdp_en_send_pkt
= NULL
;
483 kdp_en_recv_pkt
= NULL
;
487 kdp_schedule_debugger_reentry(unsigned interval
) {
490 clock_interval_to_deadline(interval
, 1000 * 1000, &deadline
);
491 thread_call_enter_delayed(kdp_timer_call
, deadline
);
500 bcopy((char *)src
, (char *)dst
, sizeof (struct kdp_ether_addr
));
503 static unsigned short
509 unsigned int high
, low
, sum
;
519 sum
= (high
<< 8) + low
;
520 sum
= (sum
>> 16) + (sum
& 65535);
522 return (sum
> 65535 ? sum
- 65535 : sum
);
527 unsigned short reply_port
,
528 const boolean_t sideband
531 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
532 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
533 struct kdp_in_addr tmp_ipaddr
;
534 struct kdp_ether_addr tmp_enaddr
;
535 struct kdp_ether_header
*eh
= NULL
;
538 kdp_panic("kdp_reply");
540 pkt
.off
-= (unsigned int)sizeof (struct kdp_udpiphdr
);
543 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
545 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
547 ui
->ui_next
= ui
->ui_prev
= 0;
549 ui
->ui_pr
= IPPROTO_UDP
;
550 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct kdp_udphdr
));
551 tmp_ipaddr
= ui
->ui_src
;
552 ui
->ui_src
= ui
->ui_dst
;
553 ui
->ui_dst
= tmp_ipaddr
;
554 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
555 ui
->ui_dport
= reply_port
;
556 ui
->ui_ulen
= ui
->ui_len
;
559 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
560 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
562 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
564 ip
->ip_len
= htons(sizeof (struct kdp_udpiphdr
) + pkt
.len
);
565 ip
->ip_v
= IPVERSION
;
566 ip
->ip_id
= htons(ip_id
++);
567 ip
->ip_hl
= sizeof (struct kdp_ip
) >> 2;
568 ip
->ip_ttl
= udp_ttl
;
570 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
572 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
575 pkt
.len
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
577 pkt
.off
-= (unsigned int)sizeof (struct kdp_ether_header
);
579 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
580 enaddr_copy(eh
->ether_shost
, &tmp_enaddr
);
581 enaddr_copy(eh
->ether_dhost
, eh
->ether_shost
);
582 enaddr_copy(&tmp_enaddr
, eh
->ether_dhost
);
583 eh
->ether_type
= htons(ETHERTYPE_IP
);
585 pkt
.len
+= (unsigned int)sizeof (struct kdp_ether_header
);
587 // save reply for possible retransmission
588 assert(pkt
.len
<= KDP_MAXPACKET
);
590 bcopy((char *)&pkt
, (char *)&saved_reply
, sizeof(saved_reply
));
592 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
594 // increment expected sequence number
601 unsigned short remote_port
604 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
605 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
606 struct kdp_ether_header
*eh
;
609 kdp_panic("kdp_send");
611 pkt
.off
-= (unsigned int)sizeof (struct kdp_udpiphdr
);
614 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
616 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
618 ui
->ui_next
= ui
->ui_prev
= 0;
620 ui
->ui_pr
= IPPROTO_UDP
;
621 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct kdp_udphdr
));
622 ui
->ui_src
= adr
.loc
.in
;
623 ui
->ui_dst
= adr
.rmt
.in
;
624 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
625 ui
->ui_dport
= remote_port
;
626 ui
->ui_ulen
= ui
->ui_len
;
629 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
630 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
632 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
634 ip
->ip_len
= htons(sizeof (struct kdp_udpiphdr
) + pkt
.len
);
635 ip
->ip_v
= IPVERSION
;
636 ip
->ip_id
= htons(ip_id
++);
637 ip
->ip_hl
= sizeof (struct kdp_ip
) >> 2;
638 ip
->ip_ttl
= udp_ttl
;
640 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
642 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
645 pkt
.len
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
647 pkt
.off
-= (unsigned int)sizeof (struct kdp_ether_header
);
649 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
650 enaddr_copy(&adr
.loc
.ea
, eh
->ether_shost
);
651 enaddr_copy(&adr
.rmt
.ea
, eh
->ether_dhost
);
652 eh
->ether_type
= htons(ETHERTYPE_IP
);
654 pkt
.len
+= (unsigned int)sizeof (struct kdp_ether_header
);
655 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
660 debugger_if_necessary(void)
662 if ((current_debugger
== KDP_CUR_DB
) && halt_in_debugger
) {
669 /* We don't interpret this pointer, we just give it to the bsd stack
670 so it can decide when to set the MAC and IP info. We'll
671 early initialize the MAC/IP info if we can so that we can use
672 KDP early in boot. These values may subsequently get over-written
673 when the interface gets initialized for real.
676 kdp_set_interface(void *ifp
, const struct kdp_ether_addr
*macaddr
)
679 struct kdp_in_addr addr
= { 0 };
682 kdp_current_ifp
= ifp
;
684 if (PE_parse_boot_argn("kdp_ip_addr", kdpstr
, sizeof(kdpstr
))) {
685 /* look for a static ip address */
686 if (inet_aton(kdpstr
, &addr
) == FALSE
)
692 /* use saved ip address */
693 save_ip_in_nvram
= TRUE
;
695 len
= sizeof(kdpstr
);
696 if (PEReadNVRAMProperty("_kdp_ipstr", kdpstr
, &len
) == FALSE
)
699 kdpstr
[len
< sizeof(kdpstr
) ? len
: sizeof(kdpstr
) - 1] = '\0';
700 if (inet_aton(kdpstr
, &addr
) == FALSE
)
704 kdp_current_ip_address
= addr
.s_addr
;
706 kdp_current_mac_address
= *macaddr
;
708 /* we can't drop into the debugger at this point because the
709 link will likely not be up. when getDebuggerLinkStatus() support gets
710 added to the appropriate network drivers, adding the
711 following will enable this capability:
712 debugger_if_necessary();
719 kdp_get_interface(void)
721 return kdp_current_ifp
;
725 kdp_set_ip_and_mac_addresses(
726 struct kdp_in_addr
*ipaddr
,
727 struct kdp_ether_addr
*macaddr
)
729 static uint64_t last_time
= (uint64_t) -1;
730 static uint64_t throttle_val
= 0;
734 if (kdp_current_ip_address
== ipaddr
->s_addr
)
737 /* don't replace if serial debugging is configured */
738 if (!KDP_SERIAL_ENABLED() ||
739 (kdp_current_ip_address
!= KDP_SERIAL_IPADDR
)) {
740 kdp_current_mac_address
= *macaddr
;
741 kdp_current_ip_address
= ipaddr
->s_addr
;
744 if (save_ip_in_nvram
== FALSE
)
747 if (inet_ntoa_r(*ipaddr
, addr
, sizeof(addr
)) == NULL
)
750 /* throttle writes if needed */
752 nanoseconds_to_absolutetime(KDP_THROTTLE_VALUE
, &throttle_val
);
754 cur_time
= mach_absolute_time();
755 if (last_time
== (uint64_t) -1 ||
756 ((cur_time
- last_time
) > throttle_val
)) {
757 PEWriteNVRAMProperty("_kdp_ipstr", addr
,
758 (const unsigned int) strlen(addr
));
760 last_time
= cur_time
;
763 debugger_if_necessary();
767 kdp_set_gateway_mac(void *gatewaymac
)
769 router_mac
= *(struct kdp_ether_addr
*)gatewaymac
;
770 flag_router_mac_initialized
= TRUE
;
773 struct kdp_ether_addr
774 kdp_get_mac_addr(void)
776 return kdp_current_mac_address
;
780 kdp_get_ip_address(void)
782 return (unsigned int)kdp_current_ip_address
;
786 kdp_disable_arp(void)
788 kdp_flag
&= ~(DB_ARP
);
792 kdp_arp_dispatch(void)
794 struct kdp_ether_arp aligned_ea
, *ea
= &aligned_ea
;
795 unsigned arp_header_offset
;
797 arp_header_offset
= (unsigned)sizeof(struct kdp_ether_header
) + pkt
.off
;
798 memcpy((void *)ea
, (void *)&pkt
.data
[arp_header_offset
], sizeof(*ea
));
800 switch(ntohs(ea
->arp_op
)) {
805 kdp_process_arp_reply(ea
);
813 kdp_process_arp_reply(struct kdp_ether_arp
*ea
)
815 /* Are we interested in ARP replies? */
816 if (flag_arp_resolved
== TRUE
)
819 /* Did we receive a reply from the right source? */
820 if (((struct kdp_in_addr
*)(ea
->arp_spa
))->s_addr
!= target_ip
)
823 flag_arp_resolved
= TRUE
;
824 current_resolved_MAC
= *(struct kdp_ether_addr
*) (ea
->arp_sha
);
829 /* ARP responses are enabled when the DB_ARP bit of the debug boot arg
834 kdp_arp_reply(struct kdp_ether_arp
*ea
)
836 struct kdp_ether_header
*eh
;
838 struct kdp_in_addr isaddr
, itaddr
, myaddr
;
839 struct kdp_ether_addr my_enaddr
;
841 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
842 pkt
.off
+= (unsigned int)sizeof(struct kdp_ether_header
);
844 if(ntohs(ea
->arp_op
) != ARPOP_REQUEST
)
847 myaddr
.s_addr
= kdp_get_ip_address();
848 my_enaddr
= kdp_get_mac_addr();
850 if ((ntohl(myaddr
.s_addr
) == 0) ||
851 ((my_enaddr
.ether_addr_octet
[0] & 0xff) == 0
852 && (my_enaddr
.ether_addr_octet
[1] & 0xff) == 0
853 && (my_enaddr
.ether_addr_octet
[2] & 0xff) == 0
854 && (my_enaddr
.ether_addr_octet
[3] & 0xff) == 0
855 && (my_enaddr
.ether_addr_octet
[4] & 0xff) == 0
856 && (my_enaddr
.ether_addr_octet
[5] & 0xff) == 0
860 (void)memcpy((void *)&isaddr
, (void *)ea
->arp_spa
, sizeof (isaddr
));
861 (void)memcpy((void *)&itaddr
, (void *)ea
->arp_tpa
, sizeof (itaddr
));
863 if (itaddr
.s_addr
== myaddr
.s_addr
) {
864 (void)memcpy((void *)ea
->arp_tha
, (void *)ea
->arp_sha
, sizeof(ea
->arp_sha
));
865 (void)memcpy((void *)ea
->arp_sha
, (void *)&my_enaddr
, sizeof(ea
->arp_sha
));
867 (void)memcpy((void *)ea
->arp_tpa
, (void *) ea
->arp_spa
, sizeof(ea
->arp_spa
));
868 (void)memcpy((void *)ea
->arp_spa
, (void *) &itaddr
, sizeof(ea
->arp_spa
));
870 ea
->arp_op
= htons(ARPOP_REPLY
);
871 ea
->arp_pro
= htons(ETHERTYPE_IP
);
872 (void)memcpy(eh
->ether_dhost
, ea
->arp_tha
, sizeof(eh
->ether_dhost
));
873 (void)memcpy(eh
->ether_shost
, &my_enaddr
, sizeof(eh
->ether_shost
));
874 eh
->ether_type
= htons(ETHERTYPE_ARP
);
875 (void)memcpy(&pkt
.data
[pkt
.off
], ea
, sizeof(*ea
));
876 pkt
.off
-= (unsigned int)sizeof (struct kdp_ether_header
);
877 /* pkt.len is still the length we want, ether_header+ether_arp */
878 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
885 struct kdp_ether_header
*eh
= NULL
;
886 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
887 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
888 static int msg_printed
;
891 kdp_panic("kdp_poll");
893 if (!kdp_en_recv_pkt
|| !kdp_en_send_pkt
) {
894 if( msg_printed
== 0) {
896 printf("kdp_poll: no debugger device\n");
901 pkt
.off
= pkt
.len
= 0;
902 kdp_receive_data(pkt
.data
, &pkt
.len
, 3/* ms */);
907 if (pkt
.len
>= sizeof(struct kdp_ether_header
)) {
908 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
910 if (kdp_flag
& KDP_ARP
) {
911 if (ntohs(eh
->ether_type
) == ETHERTYPE_ARP
) {
918 if (pkt
.len
< (sizeof (struct kdp_ether_header
) + sizeof (struct kdp_udpiphdr
)))
921 pkt
.off
+= (unsigned int)sizeof (struct kdp_ether_header
);
922 if (ntohs(eh
->ether_type
) != ETHERTYPE_IP
) {
927 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
928 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
930 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
931 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
934 pkt
.off
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
935 if (ui
->ui_pr
!= IPPROTO_UDP
) {
939 if (ip
->ip_hl
> (sizeof (struct kdp_ip
) >> 2)) {
943 if (ntohs(ui
->ui_dport
) != KDP_REMOTE_PORT
) {
944 if (panicd_port
== (ntohs(ui
->ui_dport
)) &&
945 flag_panic_dump_in_progress
) {
946 last_panic_port
= ui
->ui_sport
;
951 /* If we receive a kernel debugging packet whilst a
952 * core dump is in progress, abort the transfer and
953 * enter the debugger if not told otherwise.
956 if (flag_panic_dump_in_progress
) {
957 if (!flag_dont_abort_panic_dump
) {
958 abort_panic_transfer();
963 if (!kdp
.is_conn
&& !flag_panic_dump_in_progress
) {
964 enaddr_copy(eh
->ether_dhost
, &adr
.loc
.ea
);
965 adr
.loc
.in
= ui
->ui_dst
;
967 enaddr_copy(eh
->ether_shost
, &adr
.rmt
.ea
);
968 adr
.rmt
.in
= ui
->ui_src
;
972 * Calculate kdp packet length.
974 pkt
.len
= ntohs((u_short
)ui
->ui_ulen
) - (unsigned int)sizeof (struct kdp_udphdr
);
979 /* Create and transmit an ARP resolution request for the target IP address.
980 * This is modeled on ether_inet_arp()/RFC 826.
984 transmit_ARP_request(uint32_t ip_addr
)
986 struct kdp_ether_header
*eh
= (struct kdp_ether_header
*) &pkt
.data
[0];
987 struct kdp_ether_arp
*ea
= (struct kdp_ether_arp
*) &pkt
.data
[sizeof(struct kdp_ether_header
)];
989 KDP_DEBUG("Transmitting ARP request\n");
990 /* Populate the ether_header */
991 eh
->ether_type
= htons(ETHERTYPE_ARP
);
992 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
993 enaddr_copy(ðerbroadcastaddr
, eh
->ether_dhost
);
995 /* Populate the ARP header */
996 ea
->arp_pro
= htons(ETHERTYPE_IP
);
997 ea
->arp_hln
= sizeof(ea
->arp_sha
);
998 ea
->arp_pln
= sizeof(ea
->arp_spa
);
999 ea
->arp_hrd
= htons(ARPHRD_ETHER
);
1000 ea
->arp_op
= htons(ARPOP_REQUEST
);
1003 enaddr_copy(ðerbroadcastaddr
, ea
->arp_tha
);
1004 memcpy(ea
->arp_tpa
, (void *) &ip_addr
, sizeof(ip_addr
));
1007 enaddr_copy(&kdp_current_mac_address
, ea
->arp_sha
);
1008 memcpy(ea
->arp_spa
, (void *) &kdp_current_ip_address
, sizeof(kdp_current_ip_address
));
1011 pkt
.len
= sizeof(struct kdp_ether_header
) + sizeof(struct kdp_ether_arp
);
1013 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
1017 kdp_arp_resolve(uint32_t arp_target_ip
, struct kdp_ether_addr
*resolved_MAC
)
1019 int poll_count
= 256; /* ~770 ms modulo broadcast/delayed traffic? */
1022 #define NUM_ARP_TX_RETRIES 5
1024 target_ip
= arp_target_ip
;
1025 flag_arp_resolved
= FALSE
;
1028 pkt
.off
= pkt
.len
= 0;
1032 if (tretries
>= NUM_ARP_TX_RETRIES
) {
1036 KDP_DEBUG("ARP TX attempt #%d \n", tretries
);
1038 transmit_ARP_request(arp_target_ip
);
1040 while (!pkt
.input
&& !flag_arp_resolved
&& flag_panic_dump_in_progress
&& --poll_count
) {
1044 if (flag_arp_resolved
) {
1045 *resolved_MAC
= current_resolved_MAC
;
1049 if (!flag_panic_dump_in_progress
|| pkt
.input
) { /* we received a debugging packet, bail*/
1050 printf("Received a debugger packet,transferring control to debugger\n");
1051 /* Indicate that we should wait in the debugger when we return */
1052 kdp_flag
|= DBG_POST_CORE
;
1055 } else { /* We timed out */
1056 if (0 == poll_count
) {
1058 goto TRANSMIT_RETRY
;
1069 unsigned short reply_port
;
1070 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
1072 kdp
.saved_state
= saved_state
; // see comment in kdp_raise_exception
1079 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
1081 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
1084 // ignore replies -- we're not expecting them anyway.
1085 if (hdr
->is_reply
) {
1089 if (hdr
->request
== KDP_REATTACH
)
1090 exception_seq
= hdr
->seq
;
1092 // check for retransmitted request
1093 if (hdr
->seq
== (exception_seq
- 1)) {
1094 /* retransmit last reply */
1095 kdp_send_data(&saved_reply
.data
[saved_reply
.off
],
1098 } else if ((hdr
->seq
!= exception_seq
) &&
1099 (hdr
->request
!= KDP_CONNECT
)) {
1100 printf("kdp: bad sequence %d (want %d)\n",
1101 hdr
->seq
, exception_seq
);
1105 /* This is a manual side-channel to the main KDP protocol.
1106 * A client like GDB/kgmacros can manually construct
1107 * a request, set the input flag, issue a dummy KDP request,
1108 * and then manually collect the result
1110 if (manual_pkt
.input
) {
1111 kdp_hdr_t
*manual_hdr
= (kdp_hdr_t
*)&manual_pkt
.data
;
1112 unsigned short manual_port_unused
= 0;
1113 if (!manual_hdr
->is_reply
) {
1115 kdp_packet((unsigned char *)&manual_pkt
.data
,
1116 (int *)&manual_pkt
.len
,
1117 &manual_port_unused
);
1119 manual_pkt
.input
= 0;
1122 if (kdp_packet((unsigned char*)&pkt
.data
[pkt
.off
],
1124 (unsigned short *)&reply_port
)) {
1125 boolean_t sideband
= FALSE
;
1127 /* if it's an already connected error message,
1128 * send a sideband reply for that. for successful connects,
1129 * make sure the sequence number is correct. */
1130 if (hdr
->request
== KDP_CONNECT
) {
1131 kdp_connect_reply_t
*rp
=
1132 (kdp_connect_reply_t
*) &pkt
.data
[pkt
.off
];
1133 kdp_error_t err
= rp
->error
;
1135 if (err
== KDPERR_NO_ERROR
) {
1136 exception_seq
= hdr
->seq
;
1137 } else if (err
== KDPERR_ALREADY_CONNECTED
) {
1142 kdp_reply(reply_port
, sideband
);
1147 } while (kdp
.is_halted
);
1151 kdp_connection_wait(void)
1153 unsigned short reply_port
;
1154 struct kdp_ether_addr kdp_mac_addr
= kdp_get_mac_addr();
1155 unsigned int ip_addr
= ntohl(kdp_get_ip_address());
1158 * Do both a printf() and a kprintf() of the MAC and IP so that
1159 * they will print out on headless machines but not be added to
1163 if (KDP_SERIAL_ENABLED()) {
1164 printf("Using serial KDP.\n");
1165 kprintf("Using serial KDP.\n");
1167 printf("ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1168 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
1169 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
1170 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
1171 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
1172 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
1173 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
1175 kprintf("ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1176 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
1177 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
1178 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
1179 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
1180 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
1181 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
1183 printf("ip address: %d.%d.%d.%d\n",
1184 (ip_addr
& 0xff000000) >> 24,
1185 (ip_addr
& 0xff0000) >> 16,
1186 (ip_addr
& 0xff00) >> 8,
1189 kprintf("ip address: %d.%d.%d.%d\n",
1190 (ip_addr
& 0xff000000) >> 24,
1191 (ip_addr
& 0xff0000) >> 16,
1192 (ip_addr
& 0xff00) >> 8,
1196 printf("\nWaiting for remote debugger connection.\n");
1197 kprintf("\nWaiting for remote debugger connection.\n");
1200 if (reattach_wait
== 0) {
1201 if((kdp_flag
& KDP_GETC_ENA
) && (0 != kdp_getc())) {
1202 printf("Options..... Type\n");
1203 printf("------------ ----\n");
1204 printf("continue.... 'c'\n");
1205 printf("reboot...... 'r'\n");
1214 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
1216 while (!pkt
.input
) {
1217 if (kdp_flag
& KDP_GETC_ENA
) {
1218 switch(kdp_getc()) {
1220 printf("Continuing...\n");
1223 printf("Rebooting...\n");
1224 kdp_machine_reboot();
1234 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
1236 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
1238 if (hdr
->request
== KDP_HOSTREBOOT
) {
1239 kdp_machine_reboot();
1240 /* should not return! */
1242 if (((hdr
->request
== KDP_CONNECT
) || (hdr
->request
== KDP_REATTACH
)) &&
1243 !hdr
->is_reply
&& (hdr
->seq
== exception_seq
)) {
1244 if (kdp_packet((unsigned char *)&pkt
.data
[pkt
.off
],
1246 (unsigned short *)&reply_port
))
1247 kdp_reply(reply_port
, FALSE
);
1248 if (hdr
->request
== KDP_REATTACH
) {
1250 hdr
->request
=KDP_DISCONNECT
;
1256 } while (!kdp
.is_conn
);
1258 if (current_debugger
== KDP_CUR_DB
)
1260 printf("Connected to remote debugger.\n");
1261 kprintf("Connected to remote debugger.\n");
1266 unsigned int exception
,
1268 unsigned int subcode
1271 unsigned short remote_port
;
1272 unsigned int timeout_count
= 100;
1273 unsigned int poll_timeout
;
1276 pkt
.off
= sizeof (struct kdp_ether_header
) + sizeof (struct kdp_udpiphdr
);
1277 kdp_exception((unsigned char *)&pkt
.data
[pkt
.off
],
1279 (unsigned short *)&remote_port
,
1280 (unsigned int)exception
,
1282 (unsigned int)subcode
);
1284 kdp_send(remote_port
);
1287 while (!pkt
.input
&& poll_timeout
) {
1293 if (!kdp_exception_ack(&pkt
.data
[pkt
.off
], pkt
.len
)) {
1300 if (kdp
.exception_ack_needed
)
1301 kdp_us_spin(250000);
1303 } while (kdp
.exception_ack_needed
&& timeout_count
--);
1305 if (kdp
.exception_ack_needed
) {
1306 // give up & disconnect
1307 printf("kdp: exception ack timeout\n");
1308 if (current_debugger
== KDP_CUR_DB
)
1316 unsigned int exception
,
1318 unsigned int subcode
,
1323 if (saved_state
== 0)
1324 printf("kdp_raise_exception with NULL state\n");
1327 if (exception
!= EXC_BREAKPOINT
) {
1328 if (exception
> EXC_BREAKPOINT
|| exception
< EXC_BAD_ACCESS
) {
1331 printf("%s exception (%x,%x,%x)\n",
1332 exception_message
[index
],
1333 exception
, code
, subcode
);
1338 /* XXX WMG it seems that sometimes it doesn't work to let kdp_handler
1339 * do this. I think the client and the host can get out of sync.
1341 kdp
.saved_state
= saved_state
;
1342 kdp
.kdp_cpu
= cpu_number();
1343 kdp
.kdp_thread
= current_thread();
1346 (*kdp_en_setmode
)(TRUE
); /* enabling link mode */
1349 kdp_panic("kdp_raise_exception");
1351 if (((kdp_flag
& KDP_PANIC_DUMP_ENABLED
)
1352 || (kdp_flag
& PANIC_LOG_DUMP
)
1353 || kdp_has_polled_corefile())
1354 && (panicstr
!= (char *) 0)) {
1356 if (kdp_flag
& REBOOT_POST_CORE
)
1357 kdp_machine_reboot();
1359 if ((kdp_flag
& PANIC_CORE_ON_NMI
) && (panicstr
== (char *) 0)
1362 disable_debug_output
= disableConsoleOutput
= FALSE
;
1364 if (kdp_flag
& REBOOT_POST_CORE
)
1365 kdp_machine_reboot();
1367 if (!(kdp_flag
& DBG_POST_CORE
))
1368 goto exit_debugger_loop
;
1374 kdp_connection_wait();
1376 kdp_send_exception(exception
, code
, subcode
);
1377 if (kdp
.exception_ack_needed
) {
1378 kdp
.exception_ack_needed
= FALSE
;
1379 kdp_remove_all_breakpoints();
1380 printf("Remote debugger disconnected.\n");
1385 kdp
.is_halted
= TRUE
; /* XXX */
1386 kdp_handler(saved_state
);
1389 kdp_remove_all_breakpoints();
1390 printf("Remote debugger disconnected.\n");
1393 /* Allow triggering a panic core dump when connected to the machine
1394 * Continuing after setting kdp_trigger_core_dump should do the
1398 if (1 == kdp_trigger_core_dump
) {
1399 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
1401 if (kdp_flag
& REBOOT_POST_CORE
)
1402 kdp_machine_reboot();
1403 kdp_trigger_core_dump
= 0;
1406 /* Trigger a reboot if the user has set this flag through the
1407 * debugger.Ideally, this would be done through the HOSTREBOOT packet
1408 * in the protocol,but that will need gdb support,and when it's
1409 * available, it should work automatically.
1411 if (1 == flag_kdp_trigger_reboot
) {
1412 kdp_machine_reboot();
1413 /* If we're still around, reset the flag */
1414 flag_kdp_trigger_reboot
= 0;
1417 if (kdp_reentry_deadline
) {
1418 kdp_schedule_debugger_reentry(kdp_reentry_deadline
);
1419 printf("Debugger re-entry scheduled in %d milliseconds\n", kdp_reentry_deadline
);
1420 kdp_reentry_deadline
= 0;
1425 if (reattach_wait
== 1)
1430 (*kdp_en_setmode
)(FALSE
); /* link cleanup */
1436 kdp
.reply_port
= kdp
.exception_port
= 0;
1437 kdp
.is_halted
= kdp
.is_conn
= FALSE
;
1438 kdp
.exception_seq
= kdp
.conn_seq
= 0;
1439 kdp
.session_key
= 0;
1440 pkt
.input
= manual_pkt
.input
= FALSE
;
1441 pkt
.len
= pkt
.off
= manual_pkt
.len
= 0;
1445 create_panic_header(unsigned int request
, const char *corename
,
1446 unsigned length
, unsigned int block
)
1448 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
1449 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
1450 struct kdp_ether_header
*eh
;
1451 struct corehdr
*coreh
;
1452 const char *mode
= "octet";
1453 char modelen
= strlen(mode
) + 1;
1455 size_t fmask_size
= sizeof(KDP_FEATURE_MASK_STRING
) + sizeof(kdp_crashdump_feature_mask
);
1457 pkt
.off
= sizeof (struct kdp_ether_header
);
1458 pkt
.len
= (unsigned int)(length
+ ((request
== KDP_WRQ
) ? modelen
+ fmask_size
: 0) +
1459 (corename
? (strlen(corename
) + 1 ): 0) + sizeof(struct corehdr
));
1462 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
1464 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
1466 ui
->ui_next
= ui
->ui_prev
= 0;
1468 ui
->ui_pr
= IPPROTO_UDP
;
1469 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct kdp_udphdr
));
1470 ui
->ui_src
.s_addr
= (uint32_t)kdp_current_ip_address
;
1471 /* Already in network byte order via inet_aton() */
1472 ui
->ui_dst
.s_addr
= panic_server_ip
;
1473 ui
->ui_sport
= htons(panicd_port
);
1474 ui
->ui_dport
= ((request
== KDP_WRQ
) ? htons(panicd_port
) : last_panic_port
);
1475 ui
->ui_ulen
= ui
->ui_len
;
1478 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
1479 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
1481 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
1483 ip
->ip_len
= htons(sizeof (struct kdp_udpiphdr
) + pkt
.len
);
1484 ip
->ip_v
= IPVERSION
;
1485 ip
->ip_id
= htons(ip_id
++);
1486 ip
->ip_hl
= sizeof (struct kdp_ip
) >> 2;
1487 ip
->ip_ttl
= udp_ttl
;
1489 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
1491 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
1494 pkt
.len
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
1496 pkt
.off
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
1498 coreh
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1499 coreh
->th_opcode
= htons((u_short
)request
);
1501 if (request
== KDP_WRQ
) {
1504 cp
= coreh
->th_u
.tu_rpl
;
1505 cp
+= strlcpy (cp
, corename
, KDP_MAXPACKET
);
1507 cp
+= strlcpy (cp
, mode
, KDP_MAXPACKET
- strlen(corename
));
1509 cp
+= strlcpy(cp
, KDP_FEATURE_MASK_STRING
, sizeof(KDP_FEATURE_MASK_STRING
));
1510 *cp
++ = '\0'; /* Redundant */
1511 bcopy(&kdp_crashdump_feature_mask
, cp
, sizeof(kdp_crashdump_feature_mask
));
1512 kdp_crashdump_pkt_size
= KDP_LARGE_CRASHDUMP_PKT_SIZE
;
1513 PE_parse_boot_argn("kdp_crashdump_pkt_size", &kdp_crashdump_pkt_size
, sizeof(kdp_crashdump_pkt_size
));
1514 cp
+= sizeof(kdp_crashdump_feature_mask
);
1515 *(uint32_t *)cp
= htonl(kdp_crashdump_pkt_size
);
1517 coreh
->th_block
= htonl((unsigned int) block
);
1520 pkt
.off
-= (unsigned int)sizeof (struct kdp_udpiphdr
);
1521 pkt
.off
-= (unsigned int)sizeof (struct kdp_ether_header
);
1523 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
1524 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
1525 enaddr_copy(&destination_mac
, eh
->ether_dhost
);
1526 eh
->ether_type
= htons(ETHERTYPE_IP
);
1528 pkt
.len
+= (unsigned int)sizeof (struct kdp_ether_header
);
1533 kdp_send_crashdump_seek(char *corename
, uint64_t seek_off
)
1537 if (kdp_feature_large_crashdumps
) {
1538 panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, corename
,
1542 uint32_t off
= (uint32_t) seek_off
;
1543 panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, corename
,
1547 if (panic_error
< 0) {
1548 printf ("kdp_send_crashdump_pkt failed with error %d\n",
1553 return KERN_SUCCESS
;
1557 kdp_send_crashdump_data(unsigned int request
, char *corename
,
1558 uint64_t length
, void * txstart
)
1560 int panic_error
= 0;
1562 while ((length
> 0) || !txstart
) {
1563 uint64_t chunk
= MIN(kdp_crashdump_pkt_size
, length
);
1565 panic_error
= kdp_send_crashdump_pkt(request
, corename
, chunk
,
1567 if (panic_error
< 0) {
1568 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
1571 if (!txstart
) break;
1572 txstart
= (void *)(((uintptr_t) txstart
) + chunk
);
1575 return KERN_SUCCESS
;
1578 uint32_t kdp_crashdump_short_pkt
;
1581 kdp_send_crashdump_pkt(unsigned int request
, char *corename
,
1582 uint64_t length
, void *panic_data
)
1585 struct corehdr
*th
= NULL
;
1586 char rretries
, tretries
;
1588 if (kdp_dump_start_time
== 0) {
1589 kdp_dump_start_time
= mach_absolute_time();
1590 kdp_superblock_dump_start_time
= kdp_dump_start_time
;
1593 tretries
= rretries
= 0;
1594 poll_count
= KDP_CRASHDUMP_POLL_COUNT
;
1595 pkt
.off
= pkt
.len
= 0;
1596 if (request
== KDP_WRQ
) /* longer timeout for initial request */
1602 if (tretries
>=15) {
1603 /* The crashdump server is unreachable for some reason. This could be a network
1604 * issue or, if we've been especially unfortunate, we've hit Radar 2760413,
1605 * which is a long standing problem with the IOKit polled mode network driver
1606 * shim which can prevent transmits/receives completely.
1608 printf ("Cannot contact panic server, timing out.\n");
1613 printf("TX retry #%d ", tretries
);
1615 th
= create_panic_header(request
, corename
, (unsigned)length
, panic_block
);
1617 if (request
== KDP_DATA
) {
1618 /* as all packets are kdp_crashdump_pkt_size in length, the last packet
1619 * may end up with trailing bits. make sure that those
1620 * bits aren't confusing. */
1621 if (length
< kdp_crashdump_pkt_size
) {
1622 kdp_crashdump_short_pkt
++;
1623 memset(th
->th_data
+ length
, 'Y',
1624 kdp_crashdump_pkt_size
- (uint32_t) length
);
1627 if (!kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)panic_data
, (caddr_t
) th
->th_data
, length
)) {
1628 uintptr_t next_page
= round_page((uintptr_t)panic_data
);
1629 memset((caddr_t
) th
->th_data
, 'X', (size_t)length
);
1630 if ((next_page
- ((uintptr_t) panic_data
)) < length
) {
1631 uint64_t resid
= length
- (next_page
- (intptr_t) panic_data
);
1632 if (!kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)next_page
, (caddr_t
) th
->th_data
+ (length
- resid
), resid
)) {
1633 memset((caddr_t
) th
->th_data
+ (length
- resid
), 'X', (size_t)resid
);
1638 else if (request
== KDP_SEEK
) {
1639 if (kdp_feature_large_crashdumps
)
1640 *(uint64_t *) th
->th_data
= OSSwapHostToBigInt64((*(uint64_t *) panic_data
));
1642 *(unsigned int *) th
->th_data
= htonl(*(unsigned int *) panic_data
);
1645 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
1647 /* Listen for the ACK */
1649 while (!pkt
.input
&& flag_panic_dump_in_progress
&& poll_count
) {
1657 th
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1658 if (request
== KDP_WRQ
) {
1659 uint16_t opcode64
= ntohs(th
->th_opcode
);
1660 uint16_t features64
= (opcode64
& 0xFF00)>>8;
1661 if ((opcode64
& 0xFF) == KDP_ACK
) {
1662 kdp_feature_large_crashdumps
= features64
& KDP_FEATURE_LARGE_CRASHDUMPS
;
1663 if (features64
& KDP_FEATURE_LARGE_PKT_SIZE
) {
1664 kdp_feature_large_pkt_size
= 1;
1667 kdp_feature_large_pkt_size
= 0;
1668 kdp_crashdump_pkt_size
= 512;
1670 printf("Protocol features: 0x%x\n", (uint32_t) features64
);
1671 th
->th_opcode
= htons(KDP_ACK
);
1674 if (ntohs(th
->th_opcode
) == KDP_ACK
&& ntohl(th
->th_block
) == panic_block
) {
1676 if (ntohs(th
->th_opcode
) == KDP_ERROR
) {
1677 printf("Panic server returned error %d, retrying\n", ntohl(th
->th_code
));
1679 goto TRANSMIT_RETRY
;
1680 } else if (ntohl(th
->th_block
) == (panic_block
- 1)) {
1681 printf("RX retry ");
1683 goto TRANSMIT_RETRY
;
1688 } else if (!flag_panic_dump_in_progress
) { /* we received a debugging packet, bail*/
1689 printf("Received a debugger packet,transferring control to debugger\n");
1690 /* Configure that if not set ..*/
1691 kdp_flag
|= DBG_POST_CORE
;
1693 } else { /* We timed out */
1694 if (0 == poll_count
) {
1696 kdp_us_spin ((tretries%4
) * panic_timeout
); /* capped linear backoff */
1697 goto TRANSMIT_RETRY
;
1701 if (!(++panic_block
% SBLOCKSZ
)) {
1703 kdb_printf_unbuffered(".");
1704 ctime
= mach_absolute_time();
1705 kdp_superblock_dump_time
= ctime
- kdp_superblock_dump_start_time
;
1706 kdp_superblock_dump_start_time
= ctime
;
1707 if (kdp_superblock_dump_time
> kdp_max_superblock_dump_time
)
1708 kdp_max_superblock_dump_time
= kdp_superblock_dump_time
;
1709 if (kdp_superblock_dump_time
< kdp_min_superblock_dump_time
)
1710 kdp_min_superblock_dump_time
= kdp_superblock_dump_time
;
1713 if (request
== KDP_EOF
) {
1714 printf("\nTotal number of packets transmitted: %d\n", panic_block
);
1715 printf("Avg. superblock transfer abstime 0x%llx\n", ((mach_absolute_time() - kdp_dump_start_time
) / panic_block
) * SBLOCKSZ
);
1716 printf("Minimum superblock transfer abstime: 0x%llx\n", kdp_min_superblock_dump_time
);
1717 printf("Maximum superblock transfer abstime: 0x%llx\n", kdp_max_superblock_dump_time
);
1719 return KERN_SUCCESS
;
1725 return ((c
> 47) && (c
< 58));
1728 /* Horrid hack to extract xnu version if possible - a much cleaner approach
1729 * would be to have the integrator run a script which would copy the
1730 * xnu version into a string or an int somewhere at project submission
1731 * time - makes assumptions about sizeof(version), but will not fail if
1732 * it changes, but may be incorrect.
1734 /* 2006: Incorporated a change from Darwin user P. Lovell to extract
1735 * the minor kernel version numbers from the version string.
1738 kdp_get_xnu_version(char *versionbuf
)
1745 strlcpy(vstr
, "custom", 10);
1746 if (kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)version
, versionbuf
, 128)) {
1747 versionbuf
[127] = '\0';
1748 versionpos
= strnstr(versionbuf
, "xnu-", 115);
1750 strncpy(vstr
, versionpos
, sizeof(vstr
));
1751 vstr
[sizeof(vstr
)-1] = '\0';
1752 vptr
= vstr
+ 4; /* Begin after "xnu-" */
1753 while (*vptr
&& (isdigit(*vptr
) || *vptr
== '.'))
1756 /* Remove trailing period, if any */
1757 if (*(--vptr
) == '.')
1762 strlcpy(versionbuf
, vstr
, KDP_MAXPACKET
);
1767 kdp_set_dump_info(const uint32_t flags
, const char *filename
,
1768 const char *destipstr
, const char *routeripstr
,
1769 const uint32_t port
)
1773 if (destipstr
&& (destipstr
[0] != '\0')) {
1774 strlcpy(panicd_ip_str
, destipstr
, sizeof(panicd_ip_str
));
1775 panicd_specified
= 1;
1778 if (routeripstr
&& (routeripstr
[0] != '\0')) {
1779 strlcpy(router_ip_str
, routeripstr
, sizeof(router_ip_str
));
1780 router_specified
= 1;
1783 if (filename
&& (filename
[0] != '\0')) {
1784 strlcpy(corename_str
, filename
, sizeof(corename_str
));
1785 corename_specified
= TRUE
;
1787 corename_specified
= FALSE
;
1793 /* on a disconnect, should we stay in KDP or not? */
1794 noresume_on_disconnect
= (flags
& KDP_DUMPINFO_NORESUME
) ? 1 : 0;
1796 if ((flags
& KDP_DUMPINFO_DUMP
) == 0)
1799 /* the rest of the commands can modify kdp_flags */
1800 cmd
= flags
& KDP_DUMPINFO_MASK
;
1801 if (cmd
== KDP_DUMPINFO_DISABLE
) {
1802 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
1803 panicd_specified
= 0;
1804 kdp_trigger_core_dump
= 0;
1808 kdp_flag
&= ~REBOOT_POST_CORE
;
1809 if (flags
& KDP_DUMPINFO_REBOOT
)
1810 kdp_flag
|= REBOOT_POST_CORE
;
1812 kdp_flag
&= ~PANIC_LOG_DUMP
;
1813 if (cmd
== KDP_DUMPINFO_PANICLOG
)
1814 kdp_flag
|= PANIC_LOG_DUMP
;
1816 kdp_flag
&= ~SYSTEM_LOG_DUMP
;
1817 if (cmd
== KDP_DUMPINFO_SYSTEMLOG
)
1818 kdp_flag
|= SYSTEM_LOG_DUMP
;
1820 /* trigger a dump */
1821 kdp_flag
|= DBG_POST_CORE
;
1823 flag_dont_abort_panic_dump
= (flags
& KDP_DUMPINFO_NOINTR
) ?
1827 logPanicDataToScreen
= 1;
1828 disableConsoleOutput
= 0;
1829 disable_debug_output
= 0;
1830 kdp_trigger_core_dump
= 1;
1834 kdp_get_dump_info(uint32_t *flags
, char *filename
, char *destipstr
,
1835 char *routeripstr
, uint32_t *port
)
1838 if (panicd_specified
)
1839 strlcpy(destipstr
, panicd_ip_str
,
1840 sizeof(panicd_ip_str
));
1842 destipstr
[0] = '\0';
1846 if (router_specified
)
1847 strlcpy(routeripstr
, router_ip_str
,
1848 sizeof(router_ip_str
));
1850 routeripstr
[0] = '\0';
1854 if (corename_specified
)
1855 strlcpy(filename
, corename_str
,
1856 sizeof(corename_str
));
1863 *port
= panicd_port
;
1867 if (!panicd_specified
)
1868 *flags
|= KDP_DUMPINFO_DISABLE
;
1869 else if (kdp_flag
& PANIC_LOG_DUMP
)
1870 *flags
|= KDP_DUMPINFO_PANICLOG
;
1872 *flags
|= KDP_DUMPINFO_CORE
;
1874 if (noresume_on_disconnect
)
1875 *flags
|= KDP_DUMPINFO_NORESUME
;
1880 /* Primary dispatch routine for the system dump */
1882 kdp_panic_dump(void)
1884 char coreprefix
[10];
1889 uint32_t current_ip
= ntohl((uint32_t)kdp_current_ip_address
);
1891 if (flag_panic_dump_in_progress
) {
1892 kdb_printf("System dump aborted.\n");
1893 goto panic_dump_exit
;
1896 printf("Entering system dump routine\n");
1898 /* try a local disk dump */
1899 if (kdp_has_polled_corefile()) {
1900 flag_panic_dump_in_progress
= TRUE
;
1901 kern_dump(KERN_DUMP_DISK
);
1902 abort_panic_transfer();
1905 if (!strcmp("local", panicd_ip_str
)) return; /* disk only request */
1907 if (!kdp_en_recv_pkt
|| !kdp_en_send_pkt
) {
1908 if (!kdp_has_polled_corefile()) {
1909 kdb_printf("Error: No transport device registered for kernel crashdump\n");
1914 if (!panicd_specified
) {
1915 if (!kdp_has_polled_corefile()) {
1916 kdb_printf("A dump server was not specified in the boot-args, terminating kernel core dump.\n");
1918 goto panic_dump_exit
;
1921 flag_panic_dump_in_progress
= TRUE
;
1924 kdp_panic("kdp_panic_dump: unexpected pending input packet");
1926 kdp_get_xnu_version((char *) &pkt
.data
[0]);
1928 if (!corename_specified
) {
1930 /* Panic log bit takes precedence over core dump bit */
1931 if ((panicstr
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
))
1932 strlcpy(coreprefix
, "paniclog", sizeof(coreprefix
));
1933 else if (kdp_flag
& SYSTEM_LOG_DUMP
)
1934 strlcpy(coreprefix
, "systemlog", sizeof(coreprefix
));
1936 strlcpy(coreprefix
, "core", sizeof(coreprefix
));
1937 if (!kdp_corezip_disabled
) strlcpy(coresuffix
, ".gz", sizeof(coresuffix
));
1940 abstime
= mach_absolute_time();
1941 pkt
.data
[20] = '\0';
1942 snprintf (corename_str
,
1943 sizeof(corename_str
),
1944 "%s-%s-%d.%d.%d.%d-%x%s",
1945 coreprefix
, &pkt
.data
[0],
1946 (current_ip
& 0xff000000) >> 24,
1947 (current_ip
& 0xff0000) >> 16,
1948 (current_ip
& 0xff00) >> 8,
1949 (current_ip
& 0xff),
1950 (unsigned int) (abstime
& 0xffffffff),
1954 if (0 == inet_aton(panicd_ip_str
, (struct kdp_in_addr
*) &panic_server_ip
)) {
1955 kdb_printf("inet_aton() failed interpreting %s as a panic server IP\n", panicd_ip_str
);
1957 kdb_printf("Attempting connection to panic server configured at IP %s, port %d\n", panicd_ip_str
, panicd_port
);
1960 destination_mac
= router_mac
;
1962 if (kdp_arp_resolve(panic_server_ip
, &temp_mac
)) {
1963 kdb_printf("Resolved %s's (or proxy's) link level address\n", panicd_ip_str
);
1964 destination_mac
= temp_mac
;
1966 if (!flag_panic_dump_in_progress
) goto panic_dump_exit
;
1967 if (router_specified
) {
1968 if (0 == inet_aton(router_ip_str
, (struct kdp_in_addr
*) &parsed_router_ip
)) {
1969 kdb_printf("inet_aton() failed interpreting %s as an IP\n", router_ip_str
);
1971 router_ip
= parsed_router_ip
;
1972 if (kdp_arp_resolve(router_ip
, &temp_mac
)) {
1973 destination_mac
= temp_mac
;
1974 kdb_printf("Routing through specified router IP %s (%d)\n", router_ip_str
, router_ip
);
1980 if (!flag_panic_dump_in_progress
) goto panic_dump_exit
;
1982 kdb_printf("Transmitting packets to link level address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1983 destination_mac
.ether_addr_octet
[0] & 0xff,
1984 destination_mac
.ether_addr_octet
[1] & 0xff,
1985 destination_mac
.ether_addr_octet
[2] & 0xff,
1986 destination_mac
.ether_addr_octet
[3] & 0xff,
1987 destination_mac
.ether_addr_octet
[4] & 0xff,
1988 destination_mac
.ether_addr_octet
[5] & 0xff);
1990 kdb_printf("Kernel map size is %llu\n", (unsigned long long) get_vmmap_size(kernel_map
));
1991 kdb_printf("Sending write request for %s\n", corename_str
);
1993 if ((panic_error
= kdp_send_crashdump_pkt(KDP_WRQ
, corename_str
, 0 , NULL
)) < 0) {
1994 kdb_printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
1995 goto panic_dump_exit
;
1998 /* Just the panic log requested */
1999 if ((panicstr
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
)) {
2000 kdb_printf_unbuffered("Transmitting panic log, please wait: ");
2001 kdp_send_crashdump_data(KDP_DATA
, corename_str
,
2002 debug_buf_ptr
- debug_buf_addr
,
2004 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
2005 printf("Please file a bug report on this panic, if possible.\n");
2006 goto panic_dump_exit
;
2009 /* maybe we wanted the systemlog */
2010 if (kdp_flag
& SYSTEM_LOG_DUMP
) {
2011 long start_off
= msgbufp
->msg_bufx
;
2014 kdb_printf_unbuffered("Transmitting system log, please wait: ");
2015 if (start_off
>= msgbufp
->msg_bufr
) {
2016 len
= msgbufp
->msg_size
- start_off
;
2017 kdp_send_crashdump_data(KDP_DATA
, corename_str
, len
,
2018 msgbufp
->msg_bufc
+ start_off
);
2019 /* seek to remove trailing bytes */
2020 kdp_send_crashdump_seek(corename_str
, len
);
2024 if (start_off
!= msgbufp
->msg_bufr
) {
2025 len
= msgbufp
->msg_bufr
- start_off
;
2026 kdp_send_crashdump_data(KDP_DATA
, corename_str
, len
,
2027 msgbufp
->msg_bufc
+ start_off
);
2030 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
2031 goto panic_dump_exit
;
2034 /* We want a core dump if we're here */
2035 kern_dump(KERN_DUMP_NET
);
2038 abort_panic_transfer();
2044 abort_panic_transfer(void)
2046 flag_panic_dump_in_progress
= FALSE
;
2047 flag_dont_abort_panic_dump
= FALSE
;
2051 #if CONFIG_SERIAL_KDP
2053 static boolean_t needs_serial_init
= TRUE
;
2056 kdp_serial_send(void *rpkt
, unsigned int rpkt_len
)
2059 kdp_serialize_packet((unsigned char *)rpkt
, rpkt_len
, pal_serial_putc_nocr
);
2063 kdp_serial_receive(void *rpkt
, unsigned int *rpkt_len
, unsigned int timeout
)
2066 uint64_t now
, deadline
;
2068 clock_interval_to_deadline(timeout
, 1000 * 1000 /* milliseconds */, &deadline
);
2071 for(clock_get_uptime(&now
); now
< deadline
; clock_get_uptime(&now
)) {
2072 readkar
= pal_serial_getc();
2074 unsigned char *packet
;
2075 // printf("got char %02x\n", readkar);
2076 if((packet
= kdp_unserialize_packet(readkar
,rpkt_len
))) {
2077 memcpy(rpkt
, packet
, *rpkt_len
);
2086 kdp_serial_setmode(boolean_t active
)
2088 if (active
== FALSE
) /* leaving KDP */
2091 if (!needs_serial_init
)
2095 needs_serial_init
= FALSE
;
2101 kdp_serial_callout(__unused
void *arg
, kdp_event_t event
)
2104 * When we stop KDP, set the bit to re-initialize the console serial
2105 * port the next time we send/receive a KDP packet. We don't do it on
2106 * KDP_EVENT_ENTER directly because it also gets called when we trap to
2107 * KDP for non-external debugging, i.e., stackshot or core dumps.
2109 * Set needs_serial_init on exit (and initialization, see above) and not
2110 * enter because enter is sent multiple times and causes excess
2116 case KDP_EVENT_PANICLOG
:
2117 case KDP_EVENT_ENTER
:
2119 case KDP_EVENT_EXIT
:
2120 needs_serial_init
= TRUE
;
2125 #endif /* CONFIG_SERIAL_KDP */
2130 strlcpy(kdp_kernelversion_string
, version
, sizeof(kdp_kernelversion_string
));
2132 /* Relies on platform layer calling panic_init() before kdp_init() */
2133 if (kernel_uuid_string
[0] != '\0') {
2135 * Update kdp_kernelversion_string with our UUID
2136 * generated at link time.
2139 strlcat(kdp_kernelversion_string
, "; UUID=", sizeof(kdp_kernelversion_string
));
2140 strlcat(kdp_kernelversion_string
, kernel_uuid_string
, sizeof(kdp_kernelversion_string
));
2145 #if defined(__x86_64__) || defined(__arm__) || defined(__arm64__)
2146 if (vm_kernel_slide
) {
2147 char KASLR_stext
[19];
2148 strlcat(kdp_kernelversion_string
, "; stext=", sizeof(kdp_kernelversion_string
));
2149 snprintf(KASLR_stext
, sizeof(KASLR_stext
), "%p", (void *) vm_kernel_stext
);
2150 strlcat(kdp_kernelversion_string
, KASLR_stext
, sizeof(kdp_kernelversion_string
));
2154 if (debug_boot_arg
& DB_REBOOT_POST_CORE
)
2155 kdp_flag
|= REBOOT_POST_CORE
;
2156 #if defined(__x86_64__)
2160 kdp_timer_callout_init();
2161 kdp_crashdump_feature_mask
= htonl(kdp_crashdump_feature_mask
);
2164 #if CONFIG_SERIAL_KDP
2166 struct kdp_in_addr ipaddr
;
2167 struct kdp_ether_addr macaddr
;
2169 boolean_t kdp_match_name_found
= PE_parse_boot_argn("kdp_match_name", kdpname
, sizeof(kdpname
));
2170 boolean_t kdp_not_serial
= kdp_match_name_found
? (strncmp(kdpname
, "serial", sizeof(kdpname
))) : TRUE
;
2172 // serial must be explicitly requested
2173 if(!kdp_match_name_found
|| kdp_not_serial
)
2176 #if WITH_CONSISTENT_DBG
2177 if (kdp_not_serial
&& PE_consistent_debug_enabled() && debug_boot_arg
) {
2178 current_debugger
= HW_SHM_CUR_DB
;
2181 printf("Serial requested, consistent debug disabled or debug boot arg not present, configuring debugging over serial\n");
2183 #endif /* WITH_CONSISTENT_DBG */
2185 kprintf("Initializing serial KDP\n");
2187 kdp_register_callout(kdp_serial_callout
, NULL
);
2188 kdp_register_link(NULL
, kdp_serial_setmode
);
2189 kdp_register_send_receive(kdp_serial_send
, kdp_serial_receive
);
2191 /* fake up an ip and mac for early serial debugging */
2192 macaddr
.ether_addr_octet
[0] = 's';
2193 macaddr
.ether_addr_octet
[1] = 'e';
2194 macaddr
.ether_addr_octet
[2] = 'r';
2195 macaddr
.ether_addr_octet
[3] = 'i';
2196 macaddr
.ether_addr_octet
[4] = 'a';
2197 macaddr
.ether_addr_octet
[5] = 'l';
2198 ipaddr
.s_addr
= KDP_SERIAL_IPADDR
;
2199 kdp_set_ip_and_mac_addresses(&ipaddr
, &macaddr
);
2201 #endif /* CONFIG_SERIAL_KDP */
2204 #else /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
2209 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
2211 #if !CONFIG_KDP_INTERACTIVE_DEBUGGING
2212 __attribute__((noreturn
))
2214 panic_spin_forever()
2216 kdb_printf("\nPlease go to https://panic.apple.com to report this panic\n");
2222 #if WITH_CONSISTENT_DBG && CONFIG_KDP_INTERACTIVE_DEBUGGING
2223 __attribute__((noreturn
))
2227 kdb_printf("\nPlease go to https://panic.apple.com to report this panic\n");
2228 kdb_printf("Waiting for hardware shared memory debugger, handshake structure is at virt: %p, phys %p\n",
2229 hwsd_info
, (void *)kvtophys((vm_offset_t
)hwsd_info
));
2231 assert(hwsd_info
!= NULL
);
2232 hwsd_info
->xhsdci_status
= XHSDCI_STATUS_KERNEL_READY
;
2233 hwsd_info
->xhsdci_seq_no
= 0;
2234 FlushPoC_DcacheRegion((vm_offset_t
) hwsd_info
, sizeof(*hwsd_info
));
2237 FlushPoC_DcacheRegion((vm_offset_t
) hwsd_info
, sizeof(*hwsd_info
));
2238 if (hwsd_info
->xhsdci_status
== XHSDCI_COREDUMP_BEGIN
) {
2239 kern_dump(KERN_DUMP_HW_SHMEM_DBG
);
2242 if ((hwsd_info
->xhsdci_status
== XHSDCI_COREDUMP_REMOTE_DONE
) ||
2243 (hwsd_info
->xhsdci_status
== XHSDCI_COREDUMP_ERROR
)) {
2244 hwsd_info
->xhsdci_status
= XHSDCI_STATUS_KERNEL_READY
;
2245 hwsd_info
->xhsdci_seq_no
= 0;
2246 FlushPoC_DcacheRegion((vm_offset_t
) hwsd_info
, sizeof(*hwsd_info
));
2250 #endif /* WITH_CONSISTENT_DBG && CONFIG_KDP_INTERACTIVE_DEBUGGING */
2252 #if !CONFIG_KDP_INTERACTIVE_DEBUGGING
2253 __attribute__((noreturn
))
2255 kdp_raise_exception(
2256 __unused
unsigned int exception
,
2257 __unused
unsigned int code
,
2258 __unused
unsigned int subcode
,
2259 __unused
void *saved_state
2263 kdp_raise_exception(
2264 unsigned int exception
,
2266 unsigned int subcode
,
2272 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
2274 unsigned int initial_not_in_kdp
= not_in_kdp
;
2277 disable_preemption();
2279 if (current_debugger
!= KDP_CUR_DB
) {
2280 /* try a local disk dump */
2281 if (kdp_has_polled_corefile()) {
2282 #if WITH_CONSISTENT_DBG
2283 if (current_debugger
== HW_SHM_CUR_DB
) {
2284 hwsd_info
->xhsdci_status
= XHSDCI_STATUS_KERNEL_BUSY
;
2286 #endif /* WITH_CONSISTENT_DBG */
2287 flag_panic_dump_in_progress
= TRUE
;
2288 kern_dump(KERN_DUMP_DISK
);
2289 abort_panic_transfer();
2291 #if WITH_CONSISTENT_DBG
2292 if (current_debugger
== HW_SHM_CUR_DB
) {
2293 panic_spin_shmcon();
2295 #endif /* WITH_CONSISTENT_DBG */
2298 if (!panicDebugging
) {
2299 kdp_machine_reboot();
2303 kdp_debugger_loop(exception
, code
, subcode
, saved_state
);
2304 not_in_kdp
= initial_not_in_kdp
;
2305 enable_preemption();
2306 #else /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
2307 assert(current_debugger
!= KDP_CUR_DB
);
2310 * If kernel debugging is enabled via boot-args, but KDP debugging
2311 * is not compiled into the kernel, spin here waiting for debugging
2312 * via another method. Why here? Because we want to have watchdog
2313 * disabled (via KDP callout) while sitting waiting to be debugged.
2315 panic_spin_forever();
2316 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */