2 * Copyright (c) 2000-2013 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>
75 extern unsigned int not_in_kdp
;
76 extern int kdp_snapshot
;
77 extern void do_stackshot(void);
79 #ifdef CONFIG_KDP_INTERACTIVE_DEBUGGING
81 extern int inet_aton(const char *, struct kdp_in_addr
*); /* in libkern */
82 extern char *inet_ntoa_r(struct kdp_in_addr ina
, char *buf
,
83 size_t buflen
); /* in libkern */
85 #define DO_ALIGN 1 /* align all packet data accesses */
86 #define KDP_SERIAL_IPADDR 0xABADBABE /* IP address used for serial KDP */
87 #define LINK_UP_STATUS (IFM_AVALID | IFM_ACTIVE)
89 extern int kdp_getc(void);
90 extern int reattach_wait
;
92 static u_short ip_id
; /* ip packet ctr, for ids */
94 /* @(#)udp_usrreq.c 2.2 88/05/23 4.0NFSSRC SMI; from UCB 7.1 6/5/86 */
97 * UDP protocol implementation.
98 * Per RFC 768, August, 1980.
100 #define UDP_TTL 60 /* deflt time to live for UDP packets */
101 static int udp_ttl
= UDP_TTL
;
102 static unsigned char exception_seq
;
105 uint32_t ih_next
, ih_prev
; /* for protocol sequence q's */
106 u_char ih_x1
; /* (unused) */
107 u_char ih_pr
; /* protocol */
108 short ih_len
; /* protocol length */
109 struct kdp_in_addr ih_src
; /* source internet address */
110 struct kdp_in_addr ih_dst
; /* destination internet address */
114 u_short uh_sport
; /* source port */
115 u_short uh_dport
; /* destination port */
116 short uh_ulen
; /* udp length */
117 u_short uh_sum
; /* udp checksum */
120 struct kdp_udpiphdr
{
121 struct kdp_ipovly ui_i
; /* overlaid ip structure */
122 struct kdp_udphdr ui_u
; /* udp header */
124 #define ui_next ui_i.ih_next
125 #define ui_prev ui_i.ih_prev
126 #define ui_x1 ui_i.ih_x1
127 #define ui_pr ui_i.ih_pr
128 #define ui_len ui_i.ih_len
129 #define ui_src ui_i.ih_src
130 #define ui_dst ui_i.ih_dst
131 #define ui_sport ui_u.uh_sport
132 #define ui_dport ui_u.uh_dport
133 #define ui_ulen ui_u.uh_ulen
134 #define ui_sum ui_u.uh_sum
141 #ifdef __LITTLE_ENDIAN__
142 ip_xhl
:4, /* header length */
143 ip_xv
:4, /* version */
144 ip_xtos
:8, /* type of service */
145 ip_xlen
:16; /* total length */
147 #ifdef __BIG_ENDIAN__
148 ip_xv
:4, /* version */
149 ip_xhl
:4, /* header length */
150 ip_xtos
:8, /* type of service */
151 ip_xlen
:16; /* total length */
155 u_short ip_id
; /* identification */
156 short ip_off
; /* fragment offset field */
157 #define IP_DF 0x4000 /* dont fragment flag */
158 #define IP_MF 0x2000 /* more fragments flag */
159 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
160 u_char ip_ttl
; /* time to live */
161 u_char ip_p
; /* protocol */
162 u_short ip_sum
; /* checksum */
163 struct kdp_in_addr ip_src
,ip_dst
; /* source and dest address */
165 #define ip_v ip_vhltl.ip_x.ip_xv
166 #define ip_hl ip_vhltl.ip_x.ip_xhl
167 #define ip_tos ip_vhltl.ip_x.ip_xtos
168 #define ip_len ip_vhltl.ip_x.ip_xlen
170 #define IPPROTO_UDP 17
173 #define ETHERTYPE_IP 0x0800 /* IP protocol */
176 * Ethernet Address Resolution Protocol.
178 * See RFC 826 for protocol description. Structure below is adapted
179 * to resolving internet addresses. Field names used correspond to
183 #define ETHERTYPE_ARP 0x0806 /* Addr. resolution protocol */
186 u_short ar_hrd
; /* format of hardware address */
187 #define ARPHRD_ETHER 1 /* ethernet hardware format */
188 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
189 u_short ar_pro
; /* format of protocol address */
190 u_char ar_hln
; /* length of hardware address */
191 u_char ar_pln
; /* length of protocol address */
192 u_short ar_op
; /* one of: */
193 #define ARPOP_REQUEST 1 /* request to resolve address */
194 #define ARPOP_REPLY 2 /* response to previous request */
195 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
196 #define ARPOP_REVREPLY 4 /* response giving protocol address */
197 #define ARPOP_INVREQUEST 8 /* request to identify peer */
198 #define ARPOP_INVREPLY 9 /* response identifying peer */
201 struct kdp_ether_arp
{
202 struct kdp_arphdr ea_hdr
; /* fixed-size header */
203 u_char arp_sha
[ETHER_ADDR_LEN
]; /* sender hardware address */
204 u_char arp_spa
[4]; /* sender protocol address */
205 u_char arp_tha
[ETHER_ADDR_LEN
]; /* target hardware address */
206 u_char arp_tpa
[4]; /* target protocol address */
208 #define arp_hrd ea_hdr.ar_hrd
209 #define arp_pro ea_hdr.ar_pro
210 #define arp_hln ea_hdr.ar_hln
211 #define arp_pln ea_hdr.ar_pln
212 #define arp_op ea_hdr.ar_op
214 #define ETHERMTU 1500
215 #define ETHERHDRSIZE 14
217 #define KDP_MAXPACKET (ETHERHDRSIZE + ETHERMTU + ETHERCRC)
220 unsigned char data
[KDP_MAXPACKET
];
221 unsigned int off
, len
;
225 struct kdp_manual_pkt manual_pkt
;
229 struct kdp_in_addr in
;
230 struct kdp_ether_addr ea
;
233 struct kdp_in_addr in
;
234 struct kdp_ether_addr ea
;
239 *exception_message
[] = {
241 "Memory access", /* EXC_BAD_ACCESS */
242 "Failed instruction", /* EXC_BAD_INSTRUCTION */
243 "Arithmetic", /* EXC_ARITHMETIC */
244 "Emulation", /* EXC_EMULATION */
245 "Software", /* EXC_SOFTWARE */
246 "Breakpoint" /* EXC_BREAKPOINT */
249 volatile int kdp_flag
= 0;
251 kdp_send_t kdp_en_send_pkt
;
252 static kdp_receive_t kdp_en_recv_pkt
;
253 static kdp_link_t kdp_en_linkstatus
;
254 static kdp_mode_t kdp_en_setmode
;
256 #if CONFIG_SERIAL_KDP
257 static void kdp_serial_send(void *rpkt
, unsigned int rpkt_len
);
258 #define KDP_SERIAL_ENABLED() (kdp_en_send_pkt == kdp_serial_send)
260 #define KDP_SERIAL_ENABLED() (0)
263 static uint32_t kdp_current_ip_address
= 0;
264 static struct kdp_ether_addr kdp_current_mac_address
= {{0, 0, 0, 0, 0, 0}};
265 static void *kdp_current_ifp
;
267 static void kdp_handler( void *);
269 static uint32_t panic_server_ip
= 0;
270 static uint32_t parsed_router_ip
= 0;
271 static uint32_t router_ip
= 0;
272 static uint32_t target_ip
= 0;
274 static boolean_t save_ip_in_nvram
= FALSE
;
276 static volatile boolean_t panicd_specified
= FALSE
;
277 static boolean_t router_specified
= FALSE
;
278 static boolean_t corename_specified
= FALSE
;
279 static unsigned int panicd_port
= CORE_REMOTE_PORT
;
281 static struct kdp_ether_addr etherbroadcastaddr
= {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
283 static struct kdp_ether_addr router_mac
= {{0, 0, 0 , 0, 0, 0}};
284 static struct kdp_ether_addr destination_mac
= {{0, 0, 0 , 0, 0, 0}};
285 static struct kdp_ether_addr temp_mac
= {{0, 0, 0 , 0, 0, 0}};
286 static struct kdp_ether_addr current_resolved_MAC
= {{0, 0, 0 , 0, 0, 0}};
288 static boolean_t flag_panic_dump_in_progress
= FALSE
;
289 static boolean_t flag_router_mac_initialized
= FALSE
;
290 static boolean_t flag_dont_abort_panic_dump
= FALSE
;
292 static boolean_t flag_arp_resolved
= FALSE
;
294 static unsigned int panic_timeout
= 100000;
295 static unsigned int last_panic_port
= CORE_REMOTE_PORT
;
297 #define KDP_THROTTLE_VALUE (10ULL * NSEC_PER_SEC)
299 uint32_t kdp_crashdump_pkt_size
= 512;
300 #define KDP_LARGE_CRASHDUMP_PKT_SIZE (1440 - 6 - sizeof(struct kdp_udpiphdr))
301 static char panicd_ip_str
[20];
302 static char router_ip_str
[20];
303 static char corename_str
[50];
305 static unsigned int panic_block
= 0;
306 volatile unsigned int kdp_trigger_core_dump
= 0;
307 __private_extern__
volatile unsigned int flag_kdp_trigger_reboot
= 0;
310 extern unsigned int disableConsoleOutput
;
312 extern void kdp_call(void);
313 extern boolean_t
kdp_call_kdb(void);
315 void * kdp_get_interface(void);
316 void kdp_set_gateway_mac(void *gatewaymac
);
317 void kdp_set_ip_and_mac_addresses(struct kdp_in_addr
*ipaddr
, struct kdp_ether_addr
*);
318 void kdp_set_interface(void *interface
, const struct kdp_ether_addr
*macaddr
);
320 void kdp_disable_arp(void);
321 static void kdp_arp_reply(struct kdp_ether_arp
*);
322 static void kdp_process_arp_reply(struct kdp_ether_arp
*);
323 static boolean_t
kdp_arp_resolve(uint32_t, struct kdp_ether_addr
*);
325 static volatile unsigned kdp_reentry_deadline
;
327 static uint32_t kdp_crashdump_feature_mask
= KDP_FEATURE_LARGE_CRASHDUMPS
| KDP_FEATURE_LARGE_PKT_SIZE
;
328 uint32_t kdp_feature_large_crashdumps
, kdp_feature_large_pkt_size
;
330 char kdp_kernelversion_string
[256];
332 static boolean_t gKDPDebug
= FALSE
;
333 #define KDP_DEBUG(...) if (gKDPDebug) printf(__VA_ARGS__);
335 #define SBLOCKSZ (2048)
336 uint64_t kdp_dump_start_time
= 0;
337 uint64_t kdp_min_superblock_dump_time
= ~1ULL;
338 uint64_t kdp_max_superblock_dump_time
= 0;
339 uint64_t kdp_superblock_dump_time
= 0;
340 uint64_t kdp_superblock_dump_start_time
= 0;
345 kdp_ml_enter_debugger_wrapper(__unused
void *param0
, __unused
void *param1
) {
346 kdp_ml_enter_debugger();
350 kdp_timer_callout_init(void) {
351 kdp_timer_call
= thread_call_allocate(kdp_ml_enter_debugger_wrapper
, NULL
);
355 /* only send/receive data if the link is up */
356 inline static void wait_for_link(void)
358 static int first
= 0;
360 if (!kdp_en_linkstatus
)
363 while (((*kdp_en_linkstatus
)() & LINK_UP_STATUS
) != LINK_UP_STATUS
) {
368 printf("Waiting for link to become available.\n");
369 kprintf("Waiting for link to become available.\n");
374 inline static void kdp_send_data(void *packet
, unsigned int len
)
377 (*kdp_en_send_pkt
)(packet
, len
);
381 inline static void kdp_receive_data(void *packet
, unsigned int *len
,
382 unsigned int timeout
)
385 (*kdp_en_recv_pkt
)(packet
, len
, timeout
);
389 void kdp_register_link(kdp_link_t link
, kdp_mode_t mode
)
391 kdp_en_linkstatus
= link
;
392 kdp_en_setmode
= mode
;
395 void kdp_unregister_link(__unused kdp_link_t link
, __unused kdp_mode_t mode
)
397 kdp_en_linkstatus
= NULL
;
398 kdp_en_setmode
= NULL
;
402 kdp_register_send_receive(
404 kdp_receive_t receive
)
406 unsigned int debug
= 0;
408 PE_parse_boot_argn("debug", &debug
, sizeof (debug
));
414 kdp_en_send_pkt
= send
;
415 kdp_en_recv_pkt
= receive
;
417 if (debug
& DB_KDP_BP_DIS
)
418 kdp_flag
|= KDP_BP_DIS
;
419 if (debug
& DB_KDP_GETC_ENA
)
420 kdp_flag
|= KDP_GETC_ENA
;
424 if (debug
& DB_KERN_DUMP_ON_PANIC
)
425 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
426 if (debug
& DB_KERN_DUMP_ON_NMI
)
427 kdp_flag
|= PANIC_CORE_ON_NMI
;
429 if (debug
& DB_DBG_POST_CORE
)
430 kdp_flag
|= DBG_POST_CORE
;
432 if (debug
& DB_PANICLOG_DUMP
)
433 kdp_flag
|= PANIC_LOG_DUMP
;
435 if (PE_parse_boot_argn("_panicd_ip", panicd_ip_str
, sizeof (panicd_ip_str
)))
436 panicd_specified
= TRUE
;
438 if ((debug
& DB_REBOOT_POST_CORE
) && (panicd_specified
== TRUE
))
439 kdp_flag
|= REBOOT_POST_CORE
;
441 if (PE_parse_boot_argn("_router_ip", router_ip_str
, sizeof (router_ip_str
)))
442 router_specified
= TRUE
;
444 if (!PE_parse_boot_argn("panicd_port", &panicd_port
, sizeof (panicd_port
)))
445 panicd_port
= CORE_REMOTE_PORT
;
447 if (PE_parse_boot_argn("_panicd_corename", &corename_str
, sizeof (corename_str
)))
448 corename_specified
= TRUE
;
450 kdp_flag
|= KDP_READY
;
452 if (current_debugger
== NO_CUR_DB
)
453 current_debugger
= KDP_CUR_DB
;
454 if ((kdp_current_ip_address
!= 0) && halt_in_debugger
) {
461 kdp_unregister_send_receive(
462 __unused kdp_send_t send
,
463 __unused kdp_receive_t receive
)
465 if (current_debugger
== KDP_CUR_DB
)
466 current_debugger
= NO_CUR_DB
;
467 kdp_flag
&= ~KDP_READY
;
468 kdp_en_send_pkt
= NULL
;
469 kdp_en_recv_pkt
= NULL
;
473 kdp_schedule_debugger_reentry(unsigned interval
) {
476 clock_interval_to_deadline(interval
, 1000 * 1000, &deadline
);
477 thread_call_enter_delayed(kdp_timer_call
, deadline
);
486 bcopy((char *)src
, (char *)dst
, sizeof (struct kdp_ether_addr
));
489 static unsigned short
495 unsigned int high
, low
, sum
;
505 sum
= (high
<< 8) + low
;
506 sum
= (sum
>> 16) + (sum
& 65535);
508 return (sum
> 65535 ? sum
- 65535 : sum
);
513 unsigned short reply_port
,
514 const boolean_t sideband
517 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
518 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
519 struct kdp_in_addr tmp_ipaddr
;
520 struct kdp_ether_addr tmp_enaddr
;
521 struct kdp_ether_header
*eh
= NULL
;
524 kdp_panic("kdp_reply");
526 pkt
.off
-= (unsigned int)sizeof (struct kdp_udpiphdr
);
529 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
531 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
533 ui
->ui_next
= ui
->ui_prev
= 0;
535 ui
->ui_pr
= IPPROTO_UDP
;
536 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct kdp_udphdr
));
537 tmp_ipaddr
= ui
->ui_src
;
538 ui
->ui_src
= ui
->ui_dst
;
539 ui
->ui_dst
= tmp_ipaddr
;
540 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
541 ui
->ui_dport
= reply_port
;
542 ui
->ui_ulen
= ui
->ui_len
;
545 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
546 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
548 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
550 ip
->ip_len
= htons(sizeof (struct kdp_udpiphdr
) + pkt
.len
);
551 ip
->ip_v
= IPVERSION
;
552 ip
->ip_id
= htons(ip_id
++);
553 ip
->ip_hl
= sizeof (struct kdp_ip
) >> 2;
554 ip
->ip_ttl
= udp_ttl
;
556 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
558 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
561 pkt
.len
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
563 pkt
.off
-= (unsigned int)sizeof (struct kdp_ether_header
);
565 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
566 enaddr_copy(eh
->ether_shost
, &tmp_enaddr
);
567 enaddr_copy(eh
->ether_dhost
, eh
->ether_shost
);
568 enaddr_copy(&tmp_enaddr
, eh
->ether_dhost
);
569 eh
->ether_type
= htons(ETHERTYPE_IP
);
571 pkt
.len
+= (unsigned int)sizeof (struct kdp_ether_header
);
573 // save reply for possible retransmission
574 assert(pkt
.len
<= KDP_MAXPACKET
);
576 bcopy((char *)&pkt
, (char *)&saved_reply
, sizeof(saved_reply
));
578 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
580 // increment expected sequence number
587 unsigned short remote_port
590 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
591 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
592 struct kdp_ether_header
*eh
;
595 kdp_panic("kdp_send");
597 pkt
.off
-= (unsigned int)sizeof (struct kdp_udpiphdr
);
600 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
602 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
604 ui
->ui_next
= ui
->ui_prev
= 0;
606 ui
->ui_pr
= IPPROTO_UDP
;
607 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct kdp_udphdr
));
608 ui
->ui_src
= adr
.loc
.in
;
609 ui
->ui_dst
= adr
.rmt
.in
;
610 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
611 ui
->ui_dport
= remote_port
;
612 ui
->ui_ulen
= ui
->ui_len
;
615 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
616 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
618 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
620 ip
->ip_len
= htons(sizeof (struct kdp_udpiphdr
) + pkt
.len
);
621 ip
->ip_v
= IPVERSION
;
622 ip
->ip_id
= htons(ip_id
++);
623 ip
->ip_hl
= sizeof (struct kdp_ip
) >> 2;
624 ip
->ip_ttl
= udp_ttl
;
626 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
628 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
631 pkt
.len
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
633 pkt
.off
-= (unsigned int)sizeof (struct kdp_ether_header
);
635 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
636 enaddr_copy(&adr
.loc
.ea
, eh
->ether_shost
);
637 enaddr_copy(&adr
.rmt
.ea
, eh
->ether_dhost
);
638 eh
->ether_type
= htons(ETHERTYPE_IP
);
640 pkt
.len
+= (unsigned int)sizeof (struct kdp_ether_header
);
641 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
645 inline static void debugger_if_necessary(void)
647 if ((current_debugger
== KDP_CUR_DB
) && halt_in_debugger
) {
654 /* We don't interpret this pointer, we just give it to the bsd stack
655 so it can decide when to set the MAC and IP info. We'll
656 early initialize the MAC/IP info if we can so that we can use
657 KDP early in boot. These values may subsequently get over-written
658 when the interface gets initialized for real.
661 kdp_set_interface(void *ifp
, const struct kdp_ether_addr
*macaddr
)
664 struct kdp_in_addr addr
= { 0 };
667 kdp_current_ifp
= ifp
;
669 if (PE_parse_boot_argn("kdp_ip_addr", kdpstr
, sizeof(kdpstr
))) {
670 /* look for a static ip address */
671 if (inet_aton(kdpstr
, &addr
) == FALSE
)
677 /* use saved ip address */
678 save_ip_in_nvram
= TRUE
;
680 len
= sizeof(kdpstr
);
681 if (PEReadNVRAMProperty("_kdp_ipstr", kdpstr
, &len
) == FALSE
)
684 kdpstr
[len
< sizeof(kdpstr
) ? len
: sizeof(kdpstr
) - 1] = '\0';
685 if (inet_aton(kdpstr
, &addr
) == FALSE
)
689 kdp_current_ip_address
= addr
.s_addr
;
691 kdp_current_mac_address
= *macaddr
;
693 /* we can't drop into the debugger at this point because the
694 link will likely not be up. when getDebuggerLinkStatus() support gets
695 added to the appropriate network drivers, adding the
696 following will enable this capability:
697 debugger_if_necessary();
704 kdp_get_interface(void)
706 return kdp_current_ifp
;
710 kdp_set_ip_and_mac_addresses(
711 struct kdp_in_addr
*ipaddr
,
712 struct kdp_ether_addr
*macaddr
)
714 static uint64_t last_time
= (uint64_t) -1;
715 static uint64_t throttle_val
= 0;
719 if (kdp_current_ip_address
== ipaddr
->s_addr
)
722 /* don't replace if serial debugging is configured */
723 if (!KDP_SERIAL_ENABLED() ||
724 (kdp_current_ip_address
!= KDP_SERIAL_IPADDR
)) {
725 kdp_current_mac_address
= *macaddr
;
726 kdp_current_ip_address
= ipaddr
->s_addr
;
729 if (save_ip_in_nvram
== FALSE
)
732 if (inet_ntoa_r(*ipaddr
, addr
, sizeof(addr
)) == NULL
)
735 /* throttle writes if needed */
737 nanoseconds_to_absolutetime(KDP_THROTTLE_VALUE
, &throttle_val
);
739 cur_time
= mach_absolute_time();
740 if (last_time
== (uint64_t) -1 ||
741 ((cur_time
- last_time
) > throttle_val
)) {
742 PEWriteNVRAMProperty("_kdp_ipstr", addr
,
743 (const unsigned int) strlen(addr
));
745 last_time
= cur_time
;
748 debugger_if_necessary();
752 kdp_set_gateway_mac(void *gatewaymac
)
754 router_mac
= *(struct kdp_ether_addr
*)gatewaymac
;
755 flag_router_mac_initialized
= TRUE
;
758 struct kdp_ether_addr
759 kdp_get_mac_addr(void)
761 return kdp_current_mac_address
;
765 kdp_get_ip_address(void)
767 return (unsigned int)kdp_current_ip_address
;
771 kdp_disable_arp(void)
773 kdp_flag
&= ~(DB_ARP
);
777 kdp_arp_dispatch(void)
779 struct kdp_ether_arp aligned_ea
, *ea
= &aligned_ea
;
780 unsigned arp_header_offset
;
782 arp_header_offset
= (unsigned)sizeof(struct kdp_ether_header
) + pkt
.off
;
783 memcpy((void *)ea
, (void *)&pkt
.data
[arp_header_offset
], sizeof(*ea
));
785 switch(ntohs(ea
->arp_op
)) {
790 kdp_process_arp_reply(ea
);
798 kdp_process_arp_reply(struct kdp_ether_arp
*ea
)
800 /* Are we interested in ARP replies? */
801 if (flag_arp_resolved
== TRUE
)
804 /* Did we receive a reply from the right source? */
805 if (((struct kdp_in_addr
*)(ea
->arp_spa
))->s_addr
!= target_ip
)
808 flag_arp_resolved
= TRUE
;
809 current_resolved_MAC
= *(struct kdp_ether_addr
*) (ea
->arp_sha
);
814 /* ARP responses are enabled when the DB_ARP bit of the debug boot arg
819 kdp_arp_reply(struct kdp_ether_arp
*ea
)
821 struct kdp_ether_header
*eh
;
823 struct kdp_in_addr isaddr
, itaddr
, myaddr
;
824 struct kdp_ether_addr my_enaddr
;
826 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
827 pkt
.off
+= (unsigned int)sizeof(struct kdp_ether_header
);
829 if(ntohs(ea
->arp_op
) != ARPOP_REQUEST
)
832 myaddr
.s_addr
= kdp_get_ip_address();
833 my_enaddr
= kdp_get_mac_addr();
835 if ((ntohl(myaddr
.s_addr
) == 0) ||
836 ((my_enaddr
.ether_addr_octet
[0] & 0xff) == 0
837 && (my_enaddr
.ether_addr_octet
[1] & 0xff) == 0
838 && (my_enaddr
.ether_addr_octet
[2] & 0xff) == 0
839 && (my_enaddr
.ether_addr_octet
[3] & 0xff) == 0
840 && (my_enaddr
.ether_addr_octet
[4] & 0xff) == 0
841 && (my_enaddr
.ether_addr_octet
[5] & 0xff) == 0
845 (void)memcpy((void *)&isaddr
, (void *)ea
->arp_spa
, sizeof (isaddr
));
846 (void)memcpy((void *)&itaddr
, (void *)ea
->arp_tpa
, sizeof (itaddr
));
848 if (itaddr
.s_addr
== myaddr
.s_addr
) {
849 (void)memcpy((void *)ea
->arp_tha
, (void *)ea
->arp_sha
, sizeof(ea
->arp_sha
));
850 (void)memcpy((void *)ea
->arp_sha
, (void *)&my_enaddr
, sizeof(ea
->arp_sha
));
852 (void)memcpy((void *)ea
->arp_tpa
, (void *) ea
->arp_spa
, sizeof(ea
->arp_spa
));
853 (void)memcpy((void *)ea
->arp_spa
, (void *) &itaddr
, sizeof(ea
->arp_spa
));
855 ea
->arp_op
= htons(ARPOP_REPLY
);
856 ea
->arp_pro
= htons(ETHERTYPE_IP
);
857 (void)memcpy(eh
->ether_dhost
, ea
->arp_tha
, sizeof(eh
->ether_dhost
));
858 (void)memcpy(eh
->ether_shost
, &my_enaddr
, sizeof(eh
->ether_shost
));
859 eh
->ether_type
= htons(ETHERTYPE_ARP
);
860 (void)memcpy(&pkt
.data
[pkt
.off
], ea
, sizeof(*ea
));
861 pkt
.off
-= (unsigned int)sizeof (struct kdp_ether_header
);
862 /* pkt.len is still the length we want, ether_header+ether_arp */
863 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
870 struct kdp_ether_header
*eh
= NULL
;
871 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
872 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
873 static int msg_printed
;
876 kdp_panic("kdp_poll");
878 if (!kdp_en_recv_pkt
|| !kdp_en_send_pkt
) {
879 if( msg_printed
== 0) {
881 printf("kdp_poll: no debugger device\n");
886 pkt
.off
= pkt
.len
= 0;
887 kdp_receive_data(pkt
.data
, &pkt
.len
, 3/* ms */);
892 if (pkt
.len
>= sizeof(struct kdp_ether_header
))
894 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
896 if (kdp_flag
& KDP_ARP
)
898 if (ntohs(eh
->ether_type
) == ETHERTYPE_ARP
)
906 if (pkt
.len
< (sizeof (struct kdp_ether_header
) + sizeof (struct kdp_udpiphdr
)))
909 pkt
.off
+= (unsigned int)sizeof (struct kdp_ether_header
);
910 if (ntohs(eh
->ether_type
) != ETHERTYPE_IP
) {
915 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
916 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
918 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
919 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
922 pkt
.off
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
923 if (ui
->ui_pr
!= IPPROTO_UDP
) {
927 if (ip
->ip_hl
> (sizeof (struct kdp_ip
) >> 2)) {
931 if (ntohs(ui
->ui_dport
) != KDP_REMOTE_PORT
) {
932 if (panicd_port
== (ntohs(ui
->ui_dport
)) &&
933 flag_panic_dump_in_progress
) {
934 last_panic_port
= ui
->ui_sport
;
939 /* If we receive a kernel debugging packet whilst a
940 * core dump is in progress, abort the transfer and
941 * enter the debugger if not told otherwise.
944 if (flag_panic_dump_in_progress
)
946 if (!flag_dont_abort_panic_dump
) {
947 abort_panic_transfer();
952 if (!kdp
.is_conn
&& !flag_panic_dump_in_progress
) {
953 enaddr_copy(eh
->ether_dhost
, &adr
.loc
.ea
);
954 adr
.loc
.in
= ui
->ui_dst
;
956 enaddr_copy(eh
->ether_shost
, &adr
.rmt
.ea
);
957 adr
.rmt
.in
= ui
->ui_src
;
961 * Calculate kdp packet length.
963 pkt
.len
= ntohs((u_short
)ui
->ui_ulen
) - (unsigned int)sizeof (struct kdp_udphdr
);
967 /* Create and transmit an ARP resolution request for the target IP address.
968 * This is modeled on ether_inet_arp()/RFC 826.
972 transmit_ARP_request(uint32_t ip_addr
)
974 struct kdp_ether_header
*eh
= (struct kdp_ether_header
*) &pkt
.data
[0];
975 struct kdp_ether_arp
*ea
= (struct kdp_ether_arp
*) &pkt
.data
[sizeof(struct kdp_ether_header
)];
977 KDP_DEBUG("Transmitting ARP request\n");
978 /* Populate the ether_header */
979 eh
->ether_type
= htons(ETHERTYPE_ARP
);
980 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
981 enaddr_copy(ðerbroadcastaddr
, eh
->ether_dhost
);
983 /* Populate the ARP header */
984 ea
->arp_pro
= htons(ETHERTYPE_IP
);
985 ea
->arp_hln
= sizeof(ea
->arp_sha
);
986 ea
->arp_pln
= sizeof(ea
->arp_spa
);
987 ea
->arp_hrd
= htons(ARPHRD_ETHER
);
988 ea
->arp_op
= htons(ARPOP_REQUEST
);
991 enaddr_copy(ðerbroadcastaddr
, ea
->arp_tha
);
992 memcpy(ea
->arp_tpa
, (void *) &ip_addr
, sizeof(ip_addr
));
995 enaddr_copy(&kdp_current_mac_address
, ea
->arp_sha
);
996 memcpy(ea
->arp_spa
, (void *) &kdp_current_ip_address
, sizeof(kdp_current_ip_address
));
999 pkt
.len
= sizeof(struct kdp_ether_header
) + sizeof(struct kdp_ether_arp
);
1001 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
1005 kdp_arp_resolve(uint32_t arp_target_ip
, struct kdp_ether_addr
*resolved_MAC
)
1007 int poll_count
= 256; /* ~770 ms modulo broadcast/delayed traffic? */
1010 #define NUM_ARP_TX_RETRIES 5
1012 target_ip
= arp_target_ip
;
1013 flag_arp_resolved
= FALSE
;
1016 pkt
.off
= pkt
.len
= 0;
1020 if (tretries
>= NUM_ARP_TX_RETRIES
) {
1024 KDP_DEBUG("ARP TX attempt #%d \n", tretries
);
1026 transmit_ARP_request(arp_target_ip
);
1028 while (!pkt
.input
&& !flag_arp_resolved
&& flag_panic_dump_in_progress
&& --poll_count
) {
1032 if (flag_arp_resolved
) {
1033 *resolved_MAC
= current_resolved_MAC
;
1037 if (!flag_panic_dump_in_progress
|| pkt
.input
) /* we received a debugging packet, bail*/
1039 printf("Received a debugger packet,transferring control to debugger\n");
1040 /* Indicate that we should wait in the debugger when we return */
1041 kdp_flag
|= DBG_POST_CORE
;
1045 else /* We timed out */
1046 if (0 == poll_count
) {
1048 goto TRANSMIT_RETRY
;
1058 unsigned short reply_port
;
1059 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
1061 kdp
.saved_state
= saved_state
; // see comment in kdp_raise_exception
1068 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
1070 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
1073 // ignore replies -- we're not expecting them anyway.
1074 if (hdr
->is_reply
) {
1078 if (hdr
->request
== KDP_REATTACH
)
1079 exception_seq
= hdr
->seq
;
1081 // check for retransmitted request
1082 if (hdr
->seq
== (exception_seq
- 1)) {
1083 /* retransmit last reply */
1084 kdp_send_data(&saved_reply
.data
[saved_reply
.off
],
1087 } else if ((hdr
->seq
!= exception_seq
) &&
1088 (hdr
->request
!= KDP_CONNECT
)) {
1089 printf("kdp: bad sequence %d (want %d)\n",
1090 hdr
->seq
, exception_seq
);
1094 /* This is a manual side-channel to the main KDP protocol.
1095 * A client like GDB/kgmacros can manually construct
1096 * a request, set the input flag, issue a dummy KDP request,
1097 * and then manually collect the result
1099 if (manual_pkt
.input
) {
1100 kdp_hdr_t
*manual_hdr
= (kdp_hdr_t
*)&manual_pkt
.data
;
1101 unsigned short manual_port_unused
= 0;
1102 if (!manual_hdr
->is_reply
) {
1104 kdp_packet((unsigned char *)&manual_pkt
.data
,
1105 (int *)&manual_pkt
.len
,
1106 &manual_port_unused
);
1108 manual_pkt
.input
= 0;
1111 if (kdp_packet((unsigned char*)&pkt
.data
[pkt
.off
],
1113 (unsigned short *)&reply_port
)) {
1114 boolean_t sideband
= FALSE
;
1116 /* if it's an already connected error message,
1117 * send a sideband reply for that. for successful connects,
1118 * make sure the sequence number is correct. */
1119 if (hdr
->request
== KDP_CONNECT
) {
1120 kdp_connect_reply_t
*rp
=
1121 (kdp_connect_reply_t
*) &pkt
.data
[pkt
.off
];
1122 kdp_error_t err
= rp
->error
;
1124 if (err
== KDPERR_NO_ERROR
) {
1125 exception_seq
= hdr
->seq
;
1126 } else if (err
== KDPERR_ALREADY_CONNECTED
) {
1131 kdp_reply(reply_port
, sideband
);
1136 } while (kdp
.is_halted
);
1140 kdp_connection_wait(void)
1142 unsigned short reply_port
;
1143 struct kdp_ether_addr kdp_mac_addr
= kdp_get_mac_addr();
1144 unsigned int ip_addr
= ntohl(kdp_get_ip_address());
1147 * Do both a printf() and a kprintf() of the MAC and IP so that
1148 * they will print out on headless machines but not be added to
1152 if (KDP_SERIAL_ENABLED()) {
1153 printf("Using serial KDP.\n");
1154 kprintf("Using serial KDP.\n");
1156 printf( "ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1157 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
1158 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
1159 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
1160 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
1161 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
1162 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
1164 kprintf( "ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1165 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
1166 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
1167 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
1168 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
1169 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
1170 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
1172 printf( "ip address: %d.%d.%d.%d\n",
1173 (ip_addr
& 0xff000000) >> 24,
1174 (ip_addr
& 0xff0000) >> 16,
1175 (ip_addr
& 0xff00) >> 8,
1178 kprintf( "ip address: %d.%d.%d.%d\n",
1179 (ip_addr
& 0xff000000) >> 24,
1180 (ip_addr
& 0xff0000) >> 16,
1181 (ip_addr
& 0xff00) >> 8,
1185 printf("\nWaiting for remote debugger connection.\n");
1186 kprintf("\nWaiting for remote debugger connection.\n");
1189 if (reattach_wait
== 0) {
1190 if((kdp_flag
& KDP_GETC_ENA
) && (0 != kdp_getc()))
1192 printf("Options..... Type\n");
1193 printf("------------ ----\n");
1194 printf("continue.... 'c'\n");
1195 printf("reboot...... 'r'\n");
1203 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
1205 while (!pkt
.input
) {
1206 if (kdp_flag
& KDP_GETC_ENA
) {
1207 switch(kdp_getc()) {
1209 printf("Continuing...\n");
1212 printf("Rebooting...\n");
1213 kdp_machine_reboot();
1223 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
1225 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
1227 if (hdr
->request
== KDP_HOSTREBOOT
) {
1228 kdp_machine_reboot();
1229 /* should not return! */
1231 if (((hdr
->request
== KDP_CONNECT
) || (hdr
->request
== KDP_REATTACH
)) &&
1232 !hdr
->is_reply
&& (hdr
->seq
== exception_seq
)) {
1233 if (kdp_packet((unsigned char *)&pkt
.data
[pkt
.off
],
1235 (unsigned short *)&reply_port
))
1236 kdp_reply(reply_port
, FALSE
);
1237 if (hdr
->request
== KDP_REATTACH
) {
1239 hdr
->request
=KDP_DISCONNECT
;
1245 } while (!kdp
.is_conn
);
1247 if (current_debugger
== KDP_CUR_DB
)
1249 printf("Connected to remote debugger.\n");
1250 kprintf("Connected to remote debugger.\n");
1255 unsigned int exception
,
1257 unsigned int subcode
1260 unsigned short remote_port
;
1261 unsigned int timeout_count
= 100;
1262 unsigned int poll_timeout
;
1265 pkt
.off
= sizeof (struct kdp_ether_header
) + sizeof (struct kdp_udpiphdr
);
1266 kdp_exception((unsigned char *)&pkt
.data
[pkt
.off
],
1268 (unsigned short *)&remote_port
,
1269 (unsigned int)exception
,
1271 (unsigned int)subcode
);
1273 kdp_send(remote_port
);
1276 while(!pkt
.input
&& poll_timeout
)
1283 if (!kdp_exception_ack(&pkt
.data
[pkt
.off
], pkt
.len
)) {
1290 if (kdp
.exception_ack_needed
)
1291 kdp_us_spin(250000);
1293 } while (kdp
.exception_ack_needed
&& timeout_count
--);
1295 if (kdp
.exception_ack_needed
) {
1296 // give up & disconnect
1297 printf("kdp: exception ack timeout\n");
1298 if (current_debugger
== KDP_CUR_DB
)
1306 unsigned int exception
,
1308 unsigned int subcode
,
1313 if (saved_state
== 0)
1314 printf("kdp_raise_exception with NULL state\n");
1317 if (exception
!= EXC_BREAKPOINT
) {
1318 if (exception
> EXC_BREAKPOINT
|| exception
< EXC_BAD_ACCESS
) {
1321 printf("%s exception (%x,%x,%x)\n",
1322 exception_message
[index
],
1323 exception
, code
, subcode
);
1328 /* XXX WMG it seems that sometimes it doesn't work to let kdp_handler
1329 * do this. I think the client and the host can get out of sync.
1331 kdp
.saved_state
= saved_state
;
1332 kdp
.kdp_cpu
= cpu_number();
1333 kdp
.kdp_thread
= current_thread();
1336 (*kdp_en_setmode
)(TRUE
); /* enabling link mode */
1339 kdp_panic("kdp_raise_exception");
1341 if (((kdp_flag
& KDP_PANIC_DUMP_ENABLED
) || (kdp_flag
& PANIC_LOG_DUMP
) || kdp_has_polled_corefile())
1342 && (panicstr
!= (char *) 0)) {
1344 if (kdp_flag
& REBOOT_POST_CORE
)
1345 kdp_machine_reboot();
1348 if ((kdp_flag
& PANIC_CORE_ON_NMI
) && (panicstr
== (char *) 0) &&
1351 disable_debug_output
= disableConsoleOutput
= FALSE
;
1354 if (!(kdp_flag
& DBG_POST_CORE
))
1355 goto exit_debugger_loop
;
1360 kdp_connection_wait();
1362 kdp_send_exception(exception
, code
, subcode
);
1363 if (kdp
.exception_ack_needed
) {
1364 kdp
.exception_ack_needed
= FALSE
;
1365 kdp_remove_all_breakpoints();
1366 printf("Remote debugger disconnected.\n");
1371 kdp
.is_halted
= TRUE
; /* XXX */
1372 kdp_handler(saved_state
);
1375 kdp_remove_all_breakpoints();
1376 printf("Remote debugger disconnected.\n");
1379 /* Allow triggering a panic core dump when connected to the machine
1380 * Continuing after setting kdp_trigger_core_dump should do the
1384 if (1 == kdp_trigger_core_dump
) {
1385 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
1387 if (kdp_flag
& REBOOT_POST_CORE
)
1388 kdp_machine_reboot();
1389 kdp_trigger_core_dump
= 0;
1392 /* Trigger a reboot if the user has set this flag through the
1393 * debugger.Ideally, this would be done through the HOSTREBOOT packet
1394 * in the protocol,but that will need gdb support,and when it's
1395 * available, it should work automatically.
1397 if (1 == flag_kdp_trigger_reboot
) {
1398 kdp_machine_reboot();
1399 /* If we're still around, reset the flag */
1400 flag_kdp_trigger_reboot
= 0;
1403 if (kdp_reentry_deadline
) {
1404 kdp_schedule_debugger_reentry(kdp_reentry_deadline
);
1405 printf("Debugger re-entry scheduled in %d milliseconds\n", kdp_reentry_deadline
);
1406 kdp_reentry_deadline
= 0;
1411 if (reattach_wait
== 1)
1416 (*kdp_en_setmode
)(FALSE
); /* link cleanup */
1422 kdp
.reply_port
= kdp
.exception_port
= 0;
1423 kdp
.is_halted
= kdp
.is_conn
= FALSE
;
1424 kdp
.exception_seq
= kdp
.conn_seq
= 0;
1425 kdp
.session_key
= 0;
1426 pkt
.input
= manual_pkt
.input
= FALSE
;
1427 pkt
.len
= pkt
.off
= manual_pkt
.len
= 0;
1431 create_panic_header(unsigned int request
, const char *corename
,
1432 unsigned length
, unsigned int block
)
1434 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
1435 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
1436 struct kdp_ether_header
*eh
;
1437 struct corehdr
*coreh
;
1438 const char *mode
= "octet";
1439 char modelen
= strlen(mode
) + 1;
1441 size_t fmask_size
= sizeof(KDP_FEATURE_MASK_STRING
) + sizeof(kdp_crashdump_feature_mask
);
1443 pkt
.off
= sizeof (struct kdp_ether_header
);
1444 pkt
.len
= (unsigned int)(length
+ ((request
== KDP_WRQ
) ? modelen
+ fmask_size
: 0) +
1445 (corename
? (strlen(corename
) + 1 ): 0) + sizeof(struct corehdr
));
1448 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
1450 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
1452 ui
->ui_next
= ui
->ui_prev
= 0;
1454 ui
->ui_pr
= IPPROTO_UDP
;
1455 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct kdp_udphdr
));
1456 ui
->ui_src
.s_addr
= (uint32_t)kdp_current_ip_address
;
1457 /* Already in network byte order via inet_aton() */
1458 ui
->ui_dst
.s_addr
= panic_server_ip
;
1459 ui
->ui_sport
= htons(panicd_port
);
1460 ui
->ui_dport
= ((request
== KDP_WRQ
) ? htons(panicd_port
) : last_panic_port
);
1461 ui
->ui_ulen
= ui
->ui_len
;
1464 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
1465 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
1467 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
1469 ip
->ip_len
= htons(sizeof (struct kdp_udpiphdr
) + pkt
.len
);
1470 ip
->ip_v
= IPVERSION
;
1471 ip
->ip_id
= htons(ip_id
++);
1472 ip
->ip_hl
= sizeof (struct kdp_ip
) >> 2;
1473 ip
->ip_ttl
= udp_ttl
;
1475 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
1477 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
1480 pkt
.len
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
1482 pkt
.off
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
1484 coreh
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1485 coreh
->th_opcode
= htons((u_short
)request
);
1487 if (request
== KDP_WRQ
)
1491 cp
= coreh
->th_u
.tu_rpl
;
1492 cp
+= strlcpy (cp
, corename
, KDP_MAXPACKET
);
1494 cp
+= strlcpy (cp
, mode
, KDP_MAXPACKET
- strlen(corename
));
1496 cp
+= strlcpy(cp
, KDP_FEATURE_MASK_STRING
, sizeof(KDP_FEATURE_MASK_STRING
));
1497 *cp
++ = '\0'; /* Redundant */
1498 bcopy(&kdp_crashdump_feature_mask
, cp
, sizeof(kdp_crashdump_feature_mask
));
1499 kdp_crashdump_pkt_size
= KDP_LARGE_CRASHDUMP_PKT_SIZE
;
1500 PE_parse_boot_argn("kdp_crashdump_pkt_size", &kdp_crashdump_pkt_size
, sizeof(kdp_crashdump_pkt_size
));
1501 cp
+= sizeof(kdp_crashdump_feature_mask
);
1502 *(uint32_t *)cp
= htonl(kdp_crashdump_pkt_size
);
1506 coreh
->th_block
= htonl((unsigned int) block
);
1509 pkt
.off
-= (unsigned int)sizeof (struct kdp_udpiphdr
);
1510 pkt
.off
-= (unsigned int)sizeof (struct kdp_ether_header
);
1512 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
1513 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
1514 enaddr_copy(&destination_mac
, eh
->ether_dhost
);
1515 eh
->ether_type
= htons(ETHERTYPE_IP
);
1517 pkt
.len
+= (unsigned int)sizeof (struct kdp_ether_header
);
1521 static int kdp_send_crashdump_seek(char *corename
, uint64_t seek_off
)
1525 if (kdp_feature_large_crashdumps
) {
1526 panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, corename
,
1530 uint32_t off
= (uint32_t) seek_off
;
1531 panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, corename
,
1535 if (panic_error
< 0) {
1536 printf ("kdp_send_crashdump_pkt failed with error %d\n",
1541 return KERN_SUCCESS
;
1544 int kdp_send_crashdump_data(unsigned int request
, char *corename
,
1545 uint64_t length
, void * txstart
)
1547 int panic_error
= 0;
1549 while ((length
> 0) || !txstart
) {
1550 uint64_t chunk
= MIN(kdp_crashdump_pkt_size
, length
);
1552 panic_error
= kdp_send_crashdump_pkt(request
, corename
, chunk
,
1554 if (panic_error
< 0) {
1555 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
1558 if (!txstart
) break;
1559 txstart
= (void *)(((uintptr_t) txstart
) + chunk
);
1562 return KERN_SUCCESS
;
1565 uint32_t kdp_crashdump_short_pkt
;
1568 kdp_send_crashdump_pkt(unsigned int request
, char *corename
,
1569 uint64_t length
, void *panic_data
)
1572 struct corehdr
*th
= NULL
;
1573 char rretries
, tretries
;
1575 if (kdp_dump_start_time
== 0) {
1576 kdp_dump_start_time
= mach_absolute_time();
1577 kdp_superblock_dump_start_time
= kdp_dump_start_time
;
1580 tretries
= rretries
= 0;
1581 poll_count
= KDP_CRASHDUMP_POLL_COUNT
;
1582 pkt
.off
= pkt
.len
= 0;
1583 if (request
== KDP_WRQ
) /* longer timeout for initial request */
1589 if (tretries
>=15) {
1590 /* The crashdump server is unreachable for some reason. This could be a network
1591 * issue or, if we've been especially unfortunate, we've hit Radar 2760413,
1592 * which is a long standing problem with the IOKit polled mode network driver
1593 * shim which can prevent transmits/receives completely.
1595 printf ("Cannot contact panic server, timing out.\n");
1600 printf("TX retry #%d ", tretries
);
1602 th
= create_panic_header(request
, corename
, (unsigned)length
, panic_block
);
1604 if (request
== KDP_DATA
) {
1605 /* as all packets are kdp_crashdump_pkt_size in length, the last packet
1606 * may end up with trailing bits. make sure that those
1607 * bits aren't confusing. */
1608 if (length
< kdp_crashdump_pkt_size
) {
1609 kdp_crashdump_short_pkt
++;
1610 memset(th
->th_data
+ length
, 'Y',
1611 kdp_crashdump_pkt_size
- (uint32_t) length
);
1614 if (!kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)panic_data
, (caddr_t
) th
->th_data
, length
)) {
1615 uintptr_t next_page
= round_page((uintptr_t)panic_data
);
1616 memset((caddr_t
) th
->th_data
, 'X', (size_t)length
);
1617 if ((next_page
- ((uintptr_t) panic_data
)) < length
) {
1618 uint64_t resid
= length
- (next_page
- (intptr_t) panic_data
);
1619 if (!kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)next_page
, (caddr_t
) th
->th_data
+ (length
- resid
), resid
)) {
1620 memset((caddr_t
) th
->th_data
+ (length
- resid
), 'X', (size_t)resid
);
1625 else if (request
== KDP_SEEK
) {
1626 if (kdp_feature_large_crashdumps
)
1627 *(uint64_t *) th
->th_data
= OSSwapHostToBigInt64((*(uint64_t *) panic_data
));
1629 *(unsigned int *) th
->th_data
= htonl(*(unsigned int *) panic_data
);
1632 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
1634 /* Listen for the ACK */
1636 while (!pkt
.input
&& flag_panic_dump_in_progress
&& poll_count
) {
1645 th
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1646 if (request
== KDP_WRQ
) {
1647 uint16_t opcode64
= ntohs(th
->th_opcode
);
1648 uint16_t features64
= (opcode64
& 0xFF00)>>8;
1649 if ((opcode64
& 0xFF) == KDP_ACK
) {
1650 kdp_feature_large_crashdumps
= features64
& KDP_FEATURE_LARGE_CRASHDUMPS
;
1651 if (features64
& KDP_FEATURE_LARGE_PKT_SIZE
) {
1652 kdp_feature_large_pkt_size
= 1;
1655 kdp_feature_large_pkt_size
= 0;
1656 kdp_crashdump_pkt_size
= 512;
1658 printf("Protocol features: 0x%x\n", (uint32_t) features64
);
1659 th
->th_opcode
= htons(KDP_ACK
);
1662 if (ntohs(th
->th_opcode
) == KDP_ACK
&& ntohl(th
->th_block
) == panic_block
) {
1665 if (ntohs(th
->th_opcode
) == KDP_ERROR
) {
1666 printf("Panic server returned error %d, retrying\n", ntohl(th
->th_code
));
1668 goto TRANSMIT_RETRY
;
1671 if (ntohl(th
->th_block
) == (panic_block
- 1)) {
1672 printf("RX retry ");
1674 goto TRANSMIT_RETRY
;
1680 if (!flag_panic_dump_in_progress
) /* we received a debugging packet, bail*/
1682 printf("Received a debugger packet,transferring control to debugger\n");
1683 /* Configure that if not set ..*/
1684 kdp_flag
|= DBG_POST_CORE
;
1687 else /* We timed out */
1688 if (0 == poll_count
) {
1690 kdp_us_spin ((tretries%4
) * panic_timeout
); /* capped linear backoff */
1691 goto TRANSMIT_RETRY
;
1694 if (!(++panic_block
% SBLOCKSZ
)) {
1696 kdb_printf_unbuffered(".");
1697 ctime
= mach_absolute_time();
1698 kdp_superblock_dump_time
= ctime
- kdp_superblock_dump_start_time
;
1699 kdp_superblock_dump_start_time
= ctime
;
1700 if (kdp_superblock_dump_time
> kdp_max_superblock_dump_time
)
1701 kdp_max_superblock_dump_time
= kdp_superblock_dump_time
;
1702 if (kdp_superblock_dump_time
< kdp_min_superblock_dump_time
)
1703 kdp_min_superblock_dump_time
= kdp_superblock_dump_time
;
1706 if (request
== KDP_EOF
) {
1707 printf("\nTotal number of packets transmitted: %d\n", panic_block
);
1708 printf("Avg. superblock transfer abstime 0x%llx\n", ((mach_absolute_time() - kdp_dump_start_time
) / panic_block
) * SBLOCKSZ
);
1709 printf("Minimum superblock transfer abstime: 0x%llx\n", kdp_min_superblock_dump_time
);
1710 printf("Maximum superblock transfer abstime: 0x%llx\n", kdp_max_superblock_dump_time
);
1712 return KERN_SUCCESS
;
1718 return ((c
> 47) && (c
< 58));
1721 /* Horrid hack to extract xnu version if possible - a much cleaner approach
1722 * would be to have the integrator run a script which would copy the
1723 * xnu version into a string or an int somewhere at project submission
1724 * time - makes assumptions about sizeof(version), but will not fail if
1725 * it changes, but may be incorrect.
1727 /* 2006: Incorporated a change from Darwin user P. Lovell to extract
1728 * the minor kernel version numbers from the version string.
1731 kdp_get_xnu_version(char *versionbuf
)
1738 strlcpy(vstr
, "custom", 10);
1739 if (kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)version
, versionbuf
, 128)) {
1740 versionbuf
[127] = '\0';
1741 versionpos
= strnstr(versionbuf
, "xnu-", 115);
1743 strncpy(vstr
, versionpos
, sizeof(vstr
));
1744 vstr
[sizeof(vstr
)-1] = '\0';
1745 vptr
= vstr
+ 4; /* Begin after "xnu-" */
1746 while (*vptr
&& (isdigit(*vptr
) || *vptr
== '.'))
1749 /* Remove trailing period, if any */
1750 if (*(--vptr
) == '.')
1755 strlcpy(versionbuf
, vstr
, KDP_MAXPACKET
);
1760 kdp_set_dump_info(const uint32_t flags
, const char *filename
,
1761 const char *destipstr
, const char *routeripstr
,
1762 const uint32_t port
)
1766 if (destipstr
&& (destipstr
[0] != '\0')) {
1767 strlcpy(panicd_ip_str
, destipstr
, sizeof(panicd_ip_str
));
1768 panicd_specified
= 1;
1771 if (routeripstr
&& (routeripstr
[0] != '\0')) {
1772 strlcpy(router_ip_str
, routeripstr
, sizeof(router_ip_str
));
1773 router_specified
= 1;
1776 if (filename
&& (filename
[0] != '\0')) {
1777 strlcpy(corename_str
, filename
, sizeof(corename_str
));
1778 corename_specified
= TRUE
;
1780 corename_specified
= FALSE
;
1786 /* on a disconnect, should we stay in KDP or not? */
1787 noresume_on_disconnect
= (flags
& KDP_DUMPINFO_NORESUME
) ? 1 : 0;
1789 if ((flags
& KDP_DUMPINFO_DUMP
) == 0)
1792 /* the rest of the commands can modify kdp_flags */
1793 cmd
= flags
& KDP_DUMPINFO_MASK
;
1794 if (cmd
== KDP_DUMPINFO_DISABLE
) {
1795 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
1796 panicd_specified
= 0;
1797 kdp_trigger_core_dump
= 0;
1801 kdp_flag
&= ~REBOOT_POST_CORE
;
1802 if (flags
& KDP_DUMPINFO_REBOOT
)
1803 kdp_flag
|= REBOOT_POST_CORE
;
1805 kdp_flag
&= ~PANIC_LOG_DUMP
;
1806 if (cmd
== KDP_DUMPINFO_PANICLOG
)
1807 kdp_flag
|= PANIC_LOG_DUMP
;
1809 kdp_flag
&= ~SYSTEM_LOG_DUMP
;
1810 if (cmd
== KDP_DUMPINFO_SYSTEMLOG
)
1811 kdp_flag
|= SYSTEM_LOG_DUMP
;
1813 /* trigger a dump */
1814 kdp_flag
|= DBG_POST_CORE
;
1816 flag_dont_abort_panic_dump
= (flags
& KDP_DUMPINFO_NOINTR
) ?
1820 logPanicDataToScreen
= 1;
1821 disableConsoleOutput
= 0;
1822 disable_debug_output
= 0;
1823 kdp_trigger_core_dump
= 1;
1827 kdp_get_dump_info(uint32_t *flags
, char *filename
, char *destipstr
,
1828 char *routeripstr
, uint32_t *port
)
1831 if (panicd_specified
)
1832 strlcpy(destipstr
, panicd_ip_str
,
1833 sizeof(panicd_ip_str
));
1835 destipstr
[0] = '\0';
1839 if (router_specified
)
1840 strlcpy(routeripstr
, router_ip_str
,
1841 sizeof(router_ip_str
));
1843 routeripstr
[0] = '\0';
1847 if (corename_specified
)
1848 strlcpy(filename
, corename_str
,
1849 sizeof(corename_str
));
1856 *port
= panicd_port
;
1860 if (!panicd_specified
)
1861 *flags
|= KDP_DUMPINFO_DISABLE
;
1862 else if (kdp_flag
& PANIC_LOG_DUMP
)
1863 *flags
|= KDP_DUMPINFO_PANICLOG
;
1865 *flags
|= KDP_DUMPINFO_CORE
;
1867 if (noresume_on_disconnect
)
1868 *flags
|= KDP_DUMPINFO_NORESUME
;
1873 /* Primary dispatch routine for the system dump */
1875 kdp_panic_dump(void)
1877 char coreprefix
[10];
1882 uint32_t current_ip
= ntohl((uint32_t)kdp_current_ip_address
);
1884 if (flag_panic_dump_in_progress
) {
1885 kdb_printf("System dump aborted.\n");
1886 goto panic_dump_exit
;
1889 printf("Entering system dump routine\n");
1891 /* try a local disk dump */
1892 if (kdp_has_polled_corefile()) {
1893 flag_panic_dump_in_progress
= TRUE
;
1895 abort_panic_transfer();
1898 if (!strcmp("local", panicd_ip_str
)) return; /* disk only request */
1900 if (!kdp_en_recv_pkt
|| !kdp_en_send_pkt
) {
1901 if (!kdp_has_polled_corefile()) {
1902 kdb_printf("Error: No transport device registered for kernel crashdump\n");
1907 if (!panicd_specified
) {
1908 if (!kdp_has_polled_corefile()) {
1909 kdb_printf("A dump server was not specified in the boot-args, terminating kernel core dump.\n");
1911 goto panic_dump_exit
;
1914 flag_panic_dump_in_progress
= TRUE
;
1917 kdp_panic("kdp_panic_dump: unexpected pending input packet");
1919 kdp_get_xnu_version((char *) &pkt
.data
[0]);
1921 if (!corename_specified
) {
1923 /* Panic log bit takes precedence over core dump bit */
1924 if ((panicstr
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
))
1925 strlcpy(coreprefix
, "paniclog", sizeof(coreprefix
));
1926 else if (kdp_flag
& SYSTEM_LOG_DUMP
)
1927 strlcpy(coreprefix
, "systemlog", sizeof(coreprefix
));
1929 strlcpy(coreprefix
, "core", sizeof(coreprefix
));
1930 strlcpy(coresuffix
, ".gz", sizeof(coresuffix
));
1933 abstime
= mach_absolute_time();
1934 pkt
.data
[20] = '\0';
1935 snprintf (corename_str
, sizeof(corename_str
), "%s-%s-%d.%d.%d.%d-%x%s",
1936 coreprefix
, &pkt
.data
[0],
1937 (current_ip
& 0xff000000) >> 24,
1938 (current_ip
& 0xff0000) >> 16,
1939 (current_ip
& 0xff00) >> 8,
1940 (current_ip
& 0xff),
1941 (unsigned int) (abstime
& 0xffffffff),
1945 if (0 == inet_aton(panicd_ip_str
, (struct kdp_in_addr
*) &panic_server_ip
)) {
1946 kdb_printf("inet_aton() failed interpreting %s as a panic server IP\n", panicd_ip_str
);
1949 kdb_printf("Attempting connection to panic server configured at IP %s, port %d\n", panicd_ip_str
, panicd_port
);
1951 destination_mac
= router_mac
;
1953 if (kdp_arp_resolve(panic_server_ip
, &temp_mac
)) {
1954 kdb_printf("Resolved %s's (or proxy's) link level address\n", panicd_ip_str
);
1955 destination_mac
= temp_mac
;
1958 if (!flag_panic_dump_in_progress
) goto panic_dump_exit
;
1959 if (router_specified
) {
1960 if (0 == inet_aton(router_ip_str
, (struct kdp_in_addr
*) &parsed_router_ip
))
1961 kdb_printf("inet_aton() failed interpreting %s as an IP\n", router_ip_str
);
1963 router_ip
= parsed_router_ip
;
1964 if (kdp_arp_resolve(router_ip
, &temp_mac
)) {
1965 destination_mac
= temp_mac
;
1966 kdb_printf("Routing through specified router IP %s (%d)\n", router_ip_str
, router_ip
);
1972 if (!flag_panic_dump_in_progress
) goto panic_dump_exit
;
1974 kdb_printf("Transmitting packets to link level address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1975 destination_mac
.ether_addr_octet
[0] & 0xff,
1976 destination_mac
.ether_addr_octet
[1] & 0xff,
1977 destination_mac
.ether_addr_octet
[2] & 0xff,
1978 destination_mac
.ether_addr_octet
[3] & 0xff,
1979 destination_mac
.ether_addr_octet
[4] & 0xff,
1980 destination_mac
.ether_addr_octet
[5] & 0xff);
1982 kdb_printf("Kernel map size is %llu\n", (unsigned long long) get_vmmap_size(kernel_map
));
1983 kdb_printf("Sending write request for %s\n", corename_str
);
1985 if ((panic_error
= kdp_send_crashdump_pkt(KDP_WRQ
, corename_str
, 0 , NULL
)) < 0) {
1986 kdb_printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
1987 goto panic_dump_exit
;
1990 /* Just the panic log requested */
1991 if ((panicstr
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
)) {
1992 kdb_printf_unbuffered("Transmitting panic log, please wait: ");
1993 kdp_send_crashdump_data(KDP_DATA
, corename_str
,
1994 debug_buf_ptr
- debug_buf_addr
,
1996 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
1997 printf("Please file a bug report on this panic, if possible.\n");
1998 goto panic_dump_exit
;
2001 /* maybe we wanted the systemlog */
2002 if (kdp_flag
& SYSTEM_LOG_DUMP
) {
2003 long start_off
= msgbufp
->msg_bufx
;
2006 kdb_printf_unbuffered("Transmitting system log, please wait: ");
2007 if (start_off
>= msgbufp
->msg_bufr
) {
2008 len
= msgbufp
->msg_size
- start_off
;
2009 kdp_send_crashdump_data(KDP_DATA
, corename_str
, len
,
2010 msgbufp
->msg_bufc
+ start_off
);
2011 /* seek to remove trailing bytes */
2012 kdp_send_crashdump_seek(corename_str
, len
);
2016 if (start_off
!= msgbufp
->msg_bufr
) {
2017 len
= msgbufp
->msg_bufr
- start_off
;
2018 kdp_send_crashdump_data(KDP_DATA
, corename_str
, len
,
2019 msgbufp
->msg_bufc
+ start_off
);
2022 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
2023 goto panic_dump_exit
;
2026 /* We want a core dump if we're here */
2030 abort_panic_transfer();
2036 abort_panic_transfer(void)
2038 flag_panic_dump_in_progress
= FALSE
;
2039 flag_dont_abort_panic_dump
= FALSE
;
2043 #if CONFIG_SERIAL_KDP
2045 static boolean_t needs_serial_init
= TRUE
;
2048 kdp_serial_send(void *rpkt
, unsigned int rpkt_len
)
2051 kdp_serialize_packet((unsigned char *)rpkt
, rpkt_len
, pal_serial_putc
);
2055 kdp_serial_receive(void *rpkt
, unsigned int *rpkt_len
, unsigned int timeout
)
2058 uint64_t now
, deadline
;
2060 clock_interval_to_deadline(timeout
, 1000 * 1000 /* milliseconds */, &deadline
);
2063 for(clock_get_uptime(&now
); now
< deadline
; clock_get_uptime(&now
))
2065 readkar
= pal_serial_getc();
2068 unsigned char *packet
;
2069 // printf("got char %02x\n", readkar);
2070 if((packet
= kdp_unserialize_packet(readkar
,rpkt_len
)))
2072 memcpy(rpkt
, packet
, *rpkt_len
);
2081 kdp_serial_setmode(boolean_t active
)
2083 if (active
== FALSE
) /* leaving KDP */
2086 if (!needs_serial_init
)
2090 needs_serial_init
= FALSE
;
2096 kdp_serial_callout(__unused
void *arg
, kdp_event_t event
)
2098 /* When we stop KDP, set the bit to re-initialize the console serial port
2099 * the next time we send/receive a KDP packet. We don't do it on
2100 * KDP_EVENT_ENTER directly because it also gets called when we trap to KDP
2101 * for non-external debugging, i.e., stackshot or core dumps.
2103 * Set needs_serial_init on exit (and initialization, see above) and not
2104 * enter because enter is sent multiple times and causes excess reinitialization.
2109 case KDP_EVENT_PANICLOG
:
2110 case KDP_EVENT_ENTER
:
2112 case KDP_EVENT_EXIT
:
2113 needs_serial_init
= TRUE
;
2118 #endif /* CONFIG_SERIAL_KDP */
2123 strlcpy(kdp_kernelversion_string
, version
, sizeof(kdp_kernelversion_string
));
2125 /* Relies on platform layer calling panic_init() before kdp_init() */
2126 if (kernel_uuid_string
[0] != '\0') {
2128 * Update kdp_kernelversion_string with our UUID
2129 * generated at link time.
2132 strlcat(kdp_kernelversion_string
, "; UUID=", sizeof(kdp_kernelversion_string
));
2133 strlcat(kdp_kernelversion_string
, kernel_uuid_string
, sizeof(kdp_kernelversion_string
));
2138 #if defined(__x86_64__) || defined(__arm__) || defined(__arm64__)
2139 if (vm_kernel_slide
) {
2140 char KASLR_stext
[19];
2141 strlcat(kdp_kernelversion_string
, "; stext=", sizeof(kdp_kernelversion_string
));
2142 snprintf(KASLR_stext
, sizeof(KASLR_stext
), "%p", (void *) vm_kernel_stext
);
2143 strlcat(kdp_kernelversion_string
, KASLR_stext
, sizeof(kdp_kernelversion_string
));
2147 if (debug_boot_arg
& DB_REBOOT_POST_CORE
)
2148 kdp_flag
|= REBOOT_POST_CORE
;
2149 #if defined(__x86_64__)
2153 kdp_timer_callout_init();
2154 kdp_crashdump_feature_mask
= htonl(kdp_crashdump_feature_mask
);
2157 #if CONFIG_SERIAL_KDP
2159 struct kdp_in_addr ipaddr
;
2160 struct kdp_ether_addr macaddr
;
2163 // serial must be explicitly requested
2164 if(!PE_parse_boot_argn("kdp_match_name", kdpname
, sizeof(kdpname
)) || strncmp(kdpname
, "serial", sizeof(kdpname
)) != 0)
2167 kprintf("Initializing serial KDP\n");
2169 kdp_register_callout(kdp_serial_callout
, NULL
);
2170 kdp_register_link(NULL
, kdp_serial_setmode
);
2171 kdp_register_send_receive(kdp_serial_send
, kdp_serial_receive
);
2173 /* fake up an ip and mac for early serial debugging */
2174 macaddr
.ether_addr_octet
[0] = 's';
2175 macaddr
.ether_addr_octet
[1] = 'e';
2176 macaddr
.ether_addr_octet
[2] = 'r';
2177 macaddr
.ether_addr_octet
[3] = 'i';
2178 macaddr
.ether_addr_octet
[4] = 'a';
2179 macaddr
.ether_addr_octet
[5] = 'l';
2180 ipaddr
.s_addr
= KDP_SERIAL_IPADDR
;
2181 kdp_set_ip_and_mac_addresses(&ipaddr
, &macaddr
);
2183 #endif /* CONFIG_SERIAL_KDP */
2186 #else /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
2191 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
2193 #if defined(__arm64__) || !CONFIG_KDP_INTERACTIVE_DEBUGGING
2195 panic_spin_forever()
2197 kdb_printf("\nPlease go to https://panic.apple.com to report this panic\n");
2203 kdp_raise_exception(
2204 unsigned int exception
,
2206 unsigned int subcode
,
2210 unsigned int initial_not_in_kdp
= not_in_kdp
;
2213 /* Was a system trace requested ? */
2214 if (kdp_snapshot
&& (!panic_active()) && (panic_caller
== 0)) {
2216 not_in_kdp
= initial_not_in_kdp
;
2221 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
2223 disable_preemption();
2225 * On ARM64, KDP debugging is disabled by default.
2226 * It is compiled into the kernel for DEVELOPMENT and DEBUG,
2227 * but still hidden behind a boot arg (thus PE_i_can_has_kdp()).
2228 * For RELEASE, it is not compiled.
2231 (current_debugger
!= KDP_CUR_DB
)
2234 /* try a local disk dump */
2235 if (kdp_has_polled_corefile()) {
2236 flag_panic_dump_in_progress
= TRUE
;
2238 abort_panic_transfer();
2242 if (current_debugger
!= KDP_CUR_DB
) {
2243 kdb_printf("\nDebugger not configured. Hanging.\n");
2247 kdp_debugger_loop(exception
, code
, subcode
, saved_state
);
2248 not_in_kdp
= initial_not_in_kdp
;
2249 enable_preemption();
2250 #else /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
2251 assert(current_debugger
!= KDP_CUR_DB
);
2254 * If kernel debugging is enabled via boot-args, but KDP debugging
2255 * is not compiled into the kernel, spin here waiting for debugging
2256 * via another method. Why here? Because we want to have watchdog
2257 * disabled (via KDP callout) while sitting waiting to be debugged.
2259 panic_spin_forever();
2265 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */