2 * Copyright (c) 2000-2008 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.
39 #include <mach/boolean.h>
40 #include <mach/mach_types.h>
41 #include <mach/exception_types.h>
42 #include <kern/cpu_data.h>
43 #include <kern/debug.h>
44 #include <kern/clock.h>
46 #include <kdp/kdp_core.h>
47 #include <kdp/kdp_internal.h>
48 #include <kdp/kdp_en_debugger.h>
49 #include <kdp/kdp_callout.h>
50 #include <kdp/kdp_udp.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 #define DO_ALIGN 1 /* align all packet data accesses */
76 #define KDP_SERIAL_IPADDR 0xABADBABE /* IP address used for serial KDP */
77 #define LINK_UP_STATUS (IFM_AVALID | IFM_ACTIVE)
79 extern int kdp_getc(void);
80 extern int reattach_wait
;
82 /* only used by IONetworkingFamily */
83 typedef uint32_t (*kdp_link_t
)(void);
84 typedef boolean_t (*kdp_mode_t
)(boolean_t
);
85 void kdp_register_link(kdp_link_t link
, kdp_mode_t mode
);
86 void kdp_unregister_link(kdp_link_t link
, kdp_mode_t mode
);
88 static u_short ip_id
; /* ip packet ctr, for ids */
90 /* @(#)udp_usrreq.c 2.2 88/05/23 4.0NFSSRC SMI; from UCB 7.1 6/5/86 */
93 * UDP protocol implementation.
94 * Per RFC 768, August, 1980.
96 #define UDP_TTL 60 /* deflt time to live for UDP packets */
97 int udp_ttl
= UDP_TTL
;
98 static unsigned char exception_seq
;
101 unsigned char data
[KDP_MAXPACKET
];
102 unsigned int off
, len
;
106 struct kdp_manual_pkt manual_pkt
;
111 struct ether_addr ea
;
115 struct ether_addr ea
;
120 *exception_message
[] = {
122 "Memory access", /* EXC_BAD_ACCESS */
123 "Failed instruction", /* EXC_BAD_INSTRUCTION */
124 "Arithmetic", /* EXC_ARITHMETIC */
125 "Emulation", /* EXC_EMULATION */
126 "Software", /* EXC_SOFTWARE */
127 "Breakpoint" /* EXC_BREAKPOINT */
130 volatile int kdp_flag
= 0;
132 static kdp_send_t kdp_en_send_pkt
;
133 static kdp_receive_t kdp_en_recv_pkt
;
134 static kdp_link_t kdp_en_linkstatus
;
135 static kdp_mode_t kdp_en_setmode
;
137 #if CONFIG_SERIAL_KDP
138 static void kdp_serial_send(void *rpkt
, unsigned int rpkt_len
);
139 #define KDP_SERIAL_ENABLED() (kdp_en_send_pkt == kdp_serial_send)
141 #define KDP_SERIAL_ENABLED() (0)
144 static uint32_t kdp_current_ip_address
= 0;
145 static struct ether_addr kdp_current_mac_address
= {{0, 0, 0, 0, 0, 0}};
146 static void *kdp_current_ifp
;
148 static void kdp_handler( void *);
150 static uint32_t panic_server_ip
= 0;
151 static uint32_t parsed_router_ip
= 0;
152 static uint32_t router_ip
= 0;
153 static uint32_t target_ip
= 0;
155 static boolean_t save_ip_in_nvram
= FALSE
;
157 static volatile boolean_t panicd_specified
= FALSE
;
158 static boolean_t router_specified
= FALSE
;
159 static boolean_t corename_specified
= FALSE
;
160 static unsigned int panicd_port
= CORE_REMOTE_PORT
;
162 static struct ether_addr etherbroadcastaddr
= {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
164 static struct ether_addr router_mac
= {{0, 0, 0 , 0, 0, 0}};
165 static struct ether_addr destination_mac
= {{0, 0, 0 , 0, 0, 0}};
166 static struct ether_addr temp_mac
= {{0, 0, 0 , 0, 0, 0}};
167 static struct ether_addr current_resolved_MAC
= {{0, 0, 0 , 0, 0, 0}};
169 static boolean_t flag_panic_dump_in_progress
= FALSE
;
170 static boolean_t flag_router_mac_initialized
= FALSE
;
171 static boolean_t flag_dont_abort_panic_dump
= FALSE
;
173 static boolean_t flag_arp_resolved
= FALSE
;
175 static unsigned int panic_timeout
= 100000;
176 static unsigned int last_panic_port
= CORE_REMOTE_PORT
;
178 #define KDP_THROTTLE_VALUE (10ULL * NSEC_PER_SEC)
180 uint32_t kdp_crashdump_pkt_size
= 512;
181 #define KDP_LARGE_CRASHDUMP_PKT_SIZE (1440 - 6 - sizeof(struct udpiphdr))
182 static char panicd_ip_str
[20];
183 static char router_ip_str
[20];
184 static char corename_str
[50];
186 static unsigned int panic_block
= 0;
187 volatile unsigned int kdp_trigger_core_dump
= 0;
188 __private_extern__
volatile unsigned int flag_kdp_trigger_reboot
= 0;
190 extern unsigned int not_in_kdp
;
192 extern unsigned int disableConsoleOutput
;
194 extern void kdp_call(void);
195 extern boolean_t
kdp_call_kdb(void);
196 extern int kern_dump(void);
198 extern int inet_aton(const char *cp
, struct in_addr
*pin
);
199 extern int inet_ntoa2(struct in_addr
* pin
, char * cp
, const int len
);
201 void * kdp_get_interface(void);
202 void kdp_set_gateway_mac(void *gatewaymac
);
203 void kdp_set_ip_and_mac_addresses(struct in_addr
*ipaddr
, struct ether_addr
*);
204 void kdp_set_interface(void *interface
, const struct ether_addr
*macaddr
);
206 void kdp_disable_arp(void);
207 static void kdp_arp_reply(struct ether_arp
*);
208 static void kdp_process_arp_reply(struct ether_arp
*);
209 static boolean_t
kdp_arp_resolve(uint32_t, struct ether_addr
*);
211 static volatile unsigned kdp_reentry_deadline
;
213 static uint32_t kdp_crashdump_feature_mask
= KDP_FEATURE_LARGE_CRASHDUMPS
| KDP_FEATURE_LARGE_PKT_SIZE
;
214 uint32_t kdp_feature_large_crashdumps
, kdp_feature_large_pkt_size
;
216 char kdp_kernelversion_string
[256];
218 static boolean_t gKDPDebug
= FALSE
;
219 #define KDP_DEBUG(...) if (gKDPDebug) printf(__VA_ARGS__);
221 int kdp_snapshot
= 0;
222 static int stack_snapshot_ret
= 0;
223 static unsigned stack_snapshot_bytes_traced
= 0;
225 static void *stack_snapshot_buf
;
226 static uint32_t stack_snapshot_bufsize
;
227 static int stack_snapshot_pid
;
228 static uint32_t stack_snapshot_flags
;
229 static uint32_t stack_snapshot_dispatch_offset
;
231 static unsigned int old_debugger
;
233 #define SBLOCKSZ (2048)
234 uint64_t kdp_dump_start_time
= 0;
235 uint64_t kdp_min_superblock_dump_time
= ~1ULL;
236 uint64_t kdp_max_superblock_dump_time
= 0;
237 uint64_t kdp_superblock_dump_time
= 0;
238 uint64_t kdp_superblock_dump_start_time
= 0;
241 kdp_snapshot_preflight(int pid
, void * tracebuf
, uint32_t tracebuf_size
,
242 uint32_t flags
, uint32_t dispatch_offset
);
245 kdp_snapshot_postflight(void);
248 kdp_stackshot(int pid
, void *tracebuf
, uint32_t tracebuf_size
,
249 uint32_t flags
, uint32_t dispatch_offset
, uint32_t *pbytesTraced
);
252 kdp_stack_snapshot_geterror(void);
255 kdp_stack_snapshot_bytes_traced(void);
261 kdp_ml_enter_debugger_wrapper(__unused
void *param0
, __unused
void *param1
) {
262 kdp_ml_enter_debugger();
266 kdp_timer_callout_init(void) {
267 kdp_timer_call
= thread_call_allocate(kdp_ml_enter_debugger_wrapper
, NULL
);
271 /* only send/receive data if the link is up */
272 inline static void wait_for_link(void)
274 static int first
= 0;
276 if (!kdp_en_linkstatus
)
279 while (((*kdp_en_linkstatus
)() & LINK_UP_STATUS
) != LINK_UP_STATUS
) {
284 printf("Waiting for link to become available.\n");
285 kprintf("Waiting for link to become available.\n");
290 inline static void kdp_send_data(void *packet
, unsigned int len
)
293 (*kdp_en_send_pkt
)(packet
, len
);
297 inline static void kdp_receive_data(void *packet
, unsigned int *len
,
298 unsigned int timeout
)
301 (*kdp_en_recv_pkt
)(packet
, len
, timeout
);
306 void kdp_register_link(kdp_link_t link
, kdp_mode_t mode
)
308 kdp_en_linkstatus
= link
;
309 kdp_en_setmode
= mode
;
312 void kdp_unregister_link(__unused kdp_link_t link
, __unused kdp_mode_t mode
)
314 kdp_en_linkstatus
= NULL
;
315 kdp_en_setmode
= NULL
;
319 kdp_register_send_receive(
321 kdp_receive_t receive
)
323 unsigned int debug
= 0;
327 kdp_timer_callout_init();
329 PE_parse_boot_argn("debug", &debug
, sizeof (debug
));
330 kdp_crashdump_feature_mask
= htonl(kdp_crashdump_feature_mask
);
336 kdp_en_send_pkt
= send
;
337 kdp_en_recv_pkt
= receive
;
339 if (debug
& DB_KDP_BP_DIS
)
340 kdp_flag
|= KDP_BP_DIS
;
341 if (debug
& DB_KDP_GETC_ENA
)
342 kdp_flag
|= KDP_GETC_ENA
;
346 if (debug
& DB_KERN_DUMP_ON_PANIC
)
347 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
348 if (debug
& DB_KERN_DUMP_ON_NMI
)
349 kdp_flag
|= PANIC_CORE_ON_NMI
;
351 if (debug
& DB_DBG_POST_CORE
)
352 kdp_flag
|= DBG_POST_CORE
;
354 if (debug
& DB_PANICLOG_DUMP
)
355 kdp_flag
|= PANIC_LOG_DUMP
;
357 if (PE_parse_boot_argn("_panicd_ip", panicd_ip_str
, sizeof (panicd_ip_str
)))
358 panicd_specified
= TRUE
;
360 if ((debug
& DB_REBOOT_POST_CORE
) && (panicd_specified
== TRUE
))
361 kdp_flag
|= REBOOT_POST_CORE
;
363 if (PE_parse_boot_argn("_router_ip", router_ip_str
, sizeof (router_ip_str
)))
364 router_specified
= TRUE
;
366 if (!PE_parse_boot_argn("panicd_port", &panicd_port
, sizeof (panicd_port
)))
367 panicd_port
= CORE_REMOTE_PORT
;
369 if (PE_parse_boot_argn("_panicd_corename", &corename_str
, sizeof (corename_str
)))
370 corename_specified
= TRUE
;
372 kdp_flag
|= KDP_READY
;
373 if (current_debugger
== NO_CUR_DB
)
374 current_debugger
= KDP_CUR_DB
;
375 if ((kdp_current_ip_address
!= 0) && halt_in_debugger
) {
382 kdp_unregister_send_receive(
383 __unused kdp_send_t send
,
384 __unused kdp_receive_t receive
)
386 if (current_debugger
== KDP_CUR_DB
)
387 current_debugger
= NO_CUR_DB
;
388 kdp_flag
&= ~KDP_READY
;
389 kdp_en_send_pkt
= NULL
;
390 kdp_en_recv_pkt
= NULL
;
393 /* Cache stack snapshot parameters in preparation for a trace */
395 kdp_snapshot_preflight(int pid
, void * tracebuf
, uint32_t tracebuf_size
, uint32_t flags
, uint32_t dispatch_offset
)
397 stack_snapshot_pid
= pid
;
398 stack_snapshot_buf
= tracebuf
;
399 stack_snapshot_bufsize
= tracebuf_size
;
400 stack_snapshot_flags
= flags
;
401 stack_snapshot_dispatch_offset
= dispatch_offset
;
403 /* Mark this debugger as active, since the polled mode driver that
404 * ordinarily does this may not be enabled (yet), or since KDB may be
405 * the primary debugger.
407 old_debugger
= current_debugger
;
408 if (old_debugger
!= KDP_CUR_DB
) {
409 current_debugger
= KDP_CUR_DB
;
414 kdp_snapshot_postflight(void)
417 if ((kdp_en_send_pkt
== NULL
) || (old_debugger
== KDB_CUR_DB
))
418 current_debugger
= old_debugger
;
422 kdp_stack_snapshot_geterror(void)
424 return stack_snapshot_ret
;
428 kdp_stack_snapshot_bytes_traced(void)
430 return stack_snapshot_bytes_traced
;
434 kdp_schedule_debugger_reentry(unsigned interval
) {
437 clock_interval_to_deadline(interval
, 1000 * 1000, &deadline
);
438 thread_call_enter_delayed(kdp_timer_call
, deadline
);
447 bcopy((char *)src
, (char *)dst
, sizeof (struct ether_addr
));
450 static unsigned short
456 unsigned int high
, low
, sum
;
466 sum
= (high
<< 8) + low
;
467 sum
= (sum
>> 16) + (sum
& 65535);
469 return (sum
> 65535 ? sum
- 65535 : sum
);
474 unsigned short reply_port
,
475 const boolean_t sideband
478 struct udpiphdr aligned_ui
, *ui
= &aligned_ui
;
479 struct ip aligned_ip
, *ip
= &aligned_ip
;
480 struct in_addr tmp_ipaddr
;
481 struct ether_addr tmp_enaddr
;
482 struct ether_header
*eh
= NULL
;
485 kdp_panic("kdp_reply");
487 pkt
.off
-= (unsigned int)sizeof (struct udpiphdr
);
490 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
492 ui
= (struct udpiphdr
*)&pkt
.data
[pkt
.off
];
494 ui
->ui_next
= ui
->ui_prev
= 0;
496 ui
->ui_pr
= IPPROTO_UDP
;
497 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct udphdr
));
498 tmp_ipaddr
= ui
->ui_src
;
499 ui
->ui_src
= ui
->ui_dst
;
500 ui
->ui_dst
= tmp_ipaddr
;
501 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
502 ui
->ui_dport
= reply_port
;
503 ui
->ui_ulen
= ui
->ui_len
;
506 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
507 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
509 ip
= (struct ip
*)&pkt
.data
[pkt
.off
];
511 ip
->ip_len
= htons(sizeof (struct udpiphdr
) + pkt
.len
);
512 ip
->ip_v
= IPVERSION
;
513 ip
->ip_id
= htons(ip_id
++);
514 ip
->ip_hl
= sizeof (struct ip
) >> 2;
515 ip
->ip_ttl
= udp_ttl
;
517 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
519 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
522 pkt
.len
+= (unsigned int)sizeof (struct udpiphdr
);
524 pkt
.off
-= (unsigned int)sizeof (struct ether_header
);
526 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
527 enaddr_copy(eh
->ether_shost
, &tmp_enaddr
);
528 enaddr_copy(eh
->ether_dhost
, eh
->ether_shost
);
529 enaddr_copy(&tmp_enaddr
, eh
->ether_dhost
);
530 eh
->ether_type
= htons(ETHERTYPE_IP
);
532 pkt
.len
+= (unsigned int)sizeof (struct ether_header
);
534 // save reply for possible retransmission
535 assert(pkt
.len
<= KDP_MAXPACKET
);
537 bcopy((char *)&pkt
, (char *)&saved_reply
, sizeof(saved_reply
));
539 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
541 // increment expected sequence number
548 unsigned short remote_port
551 struct udpiphdr aligned_ui
, *ui
= &aligned_ui
;
552 struct ip aligned_ip
, *ip
= &aligned_ip
;
553 struct ether_header
*eh
;
556 kdp_panic("kdp_send");
558 pkt
.off
-= (unsigned int)sizeof (struct udpiphdr
);
561 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
563 ui
= (struct udpiphdr
*)&pkt
.data
[pkt
.off
];
565 ui
->ui_next
= ui
->ui_prev
= 0;
567 ui
->ui_pr
= IPPROTO_UDP
;
568 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct udphdr
));
569 ui
->ui_src
= adr
.loc
.in
;
570 ui
->ui_dst
= adr
.rmt
.in
;
571 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
572 ui
->ui_dport
= remote_port
;
573 ui
->ui_ulen
= ui
->ui_len
;
576 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
577 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
579 ip
= (struct ip
*)&pkt
.data
[pkt
.off
];
581 ip
->ip_len
= htons(sizeof (struct udpiphdr
) + pkt
.len
);
582 ip
->ip_v
= IPVERSION
;
583 ip
->ip_id
= htons(ip_id
++);
584 ip
->ip_hl
= sizeof (struct ip
) >> 2;
585 ip
->ip_ttl
= udp_ttl
;
587 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
589 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
592 pkt
.len
+= (unsigned int)sizeof (struct udpiphdr
);
594 pkt
.off
-= (unsigned int)sizeof (struct ether_header
);
596 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
597 enaddr_copy(&adr
.loc
.ea
, eh
->ether_shost
);
598 enaddr_copy(&adr
.rmt
.ea
, eh
->ether_dhost
);
599 eh
->ether_type
= htons(ETHERTYPE_IP
);
601 pkt
.len
+= (unsigned int)sizeof (struct ether_header
);
602 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
606 inline static void debugger_if_necessary(void)
608 if ((current_debugger
== KDP_CUR_DB
) && halt_in_debugger
) {
615 /* We don't interpret this pointer, we just give it to the bsd stack
616 so it can decide when to set the MAC and IP info. We'll
617 early initialize the MAC/IP info if we can so that we can use
618 KDP early in boot. These values may subsequently get over-written
619 when the interface gets initialized for real.
622 kdp_set_interface(void *ifp
, const struct ether_addr
*macaddr
)
625 struct in_addr addr
= { 0 };
628 kdp_current_ifp
= ifp
;
630 if (PE_parse_boot_argn("kdp_ip_addr", kdpstr
, sizeof(kdpstr
))) {
631 /* look for a static ip address */
632 if (inet_aton(kdpstr
, &addr
) == FALSE
)
638 /* use saved ip address */
639 save_ip_in_nvram
= TRUE
;
641 len
= sizeof(kdpstr
);
642 if (PEReadNVRAMProperty("_kdp_ipstr", kdpstr
, &len
) == FALSE
)
645 kdpstr
[len
< sizeof(kdpstr
) ? len
: sizeof(kdpstr
) - 1] = '\0';
646 if (inet_aton(kdpstr
, &addr
) == FALSE
)
650 kdp_current_ip_address
= addr
.s_addr
;
652 kdp_current_mac_address
= *macaddr
;
654 /* we can't drop into the debugger at this point because the
655 link will likely not be up. when getDebuggerLinkStatus() support gets
656 added to the appropriate network drivers, adding the
657 following will enable this capability:
658 debugger_if_necessary();
665 kdp_get_interface(void)
667 return kdp_current_ifp
;
671 kdp_set_ip_and_mac_addresses(
672 struct in_addr
*ipaddr
,
673 struct ether_addr
*macaddr
)
675 static uint64_t last_time
= (uint64_t) -1;
676 static uint64_t throttle_val
= 0;
680 if (kdp_current_ip_address
== ipaddr
->s_addr
)
683 /* don't replace if serial debugging is configured */
684 if (!KDP_SERIAL_ENABLED() ||
685 (kdp_current_ip_address
!= KDP_SERIAL_IPADDR
)) {
686 kdp_current_mac_address
= *macaddr
;
687 kdp_current_ip_address
= ipaddr
->s_addr
;
690 if (save_ip_in_nvram
== FALSE
)
693 if (inet_ntoa2(ipaddr
, addr
, sizeof(addr
)) == FALSE
)
696 /* throttle writes if needed */
698 nanoseconds_to_absolutetime(KDP_THROTTLE_VALUE
, &throttle_val
);
700 cur_time
= mach_absolute_time();
701 if (last_time
== (uint64_t) -1 ||
702 ((cur_time
- last_time
) > throttle_val
)) {
703 PEWriteNVRAMProperty("_kdp_ipstr", addr
,
704 (const unsigned int) strlen(addr
));
706 last_time
= cur_time
;
709 debugger_if_necessary();
713 kdp_set_gateway_mac(void *gatewaymac
)
715 router_mac
= *(struct ether_addr
*)gatewaymac
;
716 flag_router_mac_initialized
= TRUE
;
720 kdp_get_mac_addr(void)
722 return kdp_current_mac_address
;
726 kdp_get_ip_address(void)
728 return (unsigned int)kdp_current_ip_address
;
732 kdp_disable_arp(void)
734 kdp_flag
&= ~(DB_ARP
);
738 kdp_arp_dispatch(void)
740 struct ether_arp aligned_ea
, *ea
= &aligned_ea
;
741 unsigned arp_header_offset
;
743 arp_header_offset
= (unsigned)sizeof(struct ether_header
) + pkt
.off
;
744 memcpy((void *)ea
, (void *)&pkt
.data
[arp_header_offset
], sizeof(*ea
));
746 switch(ntohs(ea
->arp_op
)) {
751 kdp_process_arp_reply(ea
);
759 kdp_process_arp_reply(struct ether_arp
*ea
)
761 /* Are we interested in ARP replies? */
762 if (flag_arp_resolved
== TRUE
)
765 /* Did we receive a reply from the right source? */
766 if (((struct in_addr
*)(ea
->arp_spa
))->s_addr
!= target_ip
)
769 flag_arp_resolved
= TRUE
;
770 current_resolved_MAC
= *(struct ether_addr
*) (ea
->arp_sha
);
775 /* ARP responses are enabled when the DB_ARP bit of the debug boot arg
780 kdp_arp_reply(struct ether_arp
*ea
)
782 struct ether_header
*eh
;
784 struct in_addr isaddr
, itaddr
, myaddr
;
785 struct ether_addr my_enaddr
;
787 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
788 pkt
.off
+= (unsigned int)sizeof(struct ether_header
);
790 if(ntohs(ea
->arp_op
) != ARPOP_REQUEST
)
793 myaddr
.s_addr
= kdp_get_ip_address();
794 my_enaddr
= kdp_get_mac_addr();
796 if ((ntohl(myaddr
.s_addr
) == 0) ||
797 ((my_enaddr
.ether_addr_octet
[0] & 0xff) == 0
798 && (my_enaddr
.ether_addr_octet
[1] & 0xff) == 0
799 && (my_enaddr
.ether_addr_octet
[2] & 0xff) == 0
800 && (my_enaddr
.ether_addr_octet
[3] & 0xff) == 0
801 && (my_enaddr
.ether_addr_octet
[4] & 0xff) == 0
802 && (my_enaddr
.ether_addr_octet
[5] & 0xff) == 0
806 (void)memcpy((void *)&isaddr
, (void *)ea
->arp_spa
, sizeof (isaddr
));
807 (void)memcpy((void *)&itaddr
, (void *)ea
->arp_tpa
, sizeof (itaddr
));
809 if (itaddr
.s_addr
== myaddr
.s_addr
) {
810 (void)memcpy((void *)ea
->arp_tha
, (void *)ea
->arp_sha
, sizeof(ea
->arp_sha
));
811 (void)memcpy((void *)ea
->arp_sha
, (void *)&my_enaddr
, sizeof(ea
->arp_sha
));
813 (void)memcpy((void *)ea
->arp_tpa
, (void *) ea
->arp_spa
, sizeof(ea
->arp_spa
));
814 (void)memcpy((void *)ea
->arp_spa
, (void *) &itaddr
, sizeof(ea
->arp_spa
));
816 ea
->arp_op
= htons(ARPOP_REPLY
);
817 ea
->arp_pro
= htons(ETHERTYPE_IP
);
818 (void)memcpy(eh
->ether_dhost
, ea
->arp_tha
, sizeof(eh
->ether_dhost
));
819 (void)memcpy(eh
->ether_shost
, &my_enaddr
, sizeof(eh
->ether_shost
));
820 eh
->ether_type
= htons(ETHERTYPE_ARP
);
821 (void)memcpy(&pkt
.data
[pkt
.off
], ea
, sizeof(*ea
));
822 pkt
.off
-= (unsigned int)sizeof (struct ether_header
);
823 /* pkt.len is still the length we want, ether_header+ether_arp */
824 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
831 struct ether_header
*eh
= NULL
;
832 struct udpiphdr aligned_ui
, *ui
= &aligned_ui
;
833 struct ip aligned_ip
, *ip
= &aligned_ip
;
834 static int msg_printed
;
837 kdp_panic("kdp_poll");
839 if (!kdp_en_recv_pkt
|| !kdp_en_send_pkt
) {
840 if( msg_printed
== 0) {
842 printf("kdp_poll: no debugger device\n");
847 pkt
.off
= pkt
.len
= 0;
848 kdp_receive_data(pkt
.data
, &pkt
.len
, 3/* ms */);
853 if (pkt
.len
>= sizeof(struct ether_header
))
855 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
857 if (kdp_flag
& KDP_ARP
)
859 if (ntohs(eh
->ether_type
) == ETHERTYPE_ARP
)
867 if (pkt
.len
< (sizeof (struct ether_header
) + sizeof (struct udpiphdr
)))
870 pkt
.off
+= (unsigned int)sizeof (struct ether_header
);
871 if (ntohs(eh
->ether_type
) != ETHERTYPE_IP
) {
876 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
877 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
879 ui
= (struct udpiphdr
*)&pkt
.data
[pkt
.off
];
880 ip
= (struct ip
*)&pkt
.data
[pkt
.off
];
883 pkt
.off
+= (unsigned int)sizeof (struct udpiphdr
);
884 if (ui
->ui_pr
!= IPPROTO_UDP
) {
888 if (ip
->ip_hl
> (sizeof (struct ip
) >> 2)) {
892 if (ntohs(ui
->ui_dport
) != KDP_REMOTE_PORT
) {
893 if (panicd_port
== (ntohs(ui
->ui_dport
)) &&
894 flag_panic_dump_in_progress
) {
895 last_panic_port
= ui
->ui_sport
;
900 /* If we receive a kernel debugging packet whilst a
901 * core dump is in progress, abort the transfer and
902 * enter the debugger if not told otherwise.
905 if (flag_panic_dump_in_progress
)
907 if (!flag_dont_abort_panic_dump
) {
908 abort_panic_transfer();
913 if (!kdp
.is_conn
&& !flag_panic_dump_in_progress
) {
914 enaddr_copy(eh
->ether_dhost
, &adr
.loc
.ea
);
915 adr
.loc
.in
= ui
->ui_dst
;
917 enaddr_copy(eh
->ether_shost
, &adr
.rmt
.ea
);
918 adr
.rmt
.in
= ui
->ui_src
;
922 * Calculate kdp packet length.
924 pkt
.len
= ntohs((u_short
)ui
->ui_ulen
) - (unsigned int)sizeof (struct udphdr
);
928 /* Create and transmit an ARP resolution request for the target IP address.
929 * This is modeled on ether_inet_arp()/RFC 826.
933 transmit_ARP_request(uint32_t ip_addr
)
935 struct ether_header
*eh
= (struct ether_header
*) &pkt
.data
[0];
936 struct ether_arp
*ea
= (struct ether_arp
*) &pkt
.data
[sizeof(struct ether_header
)];
938 KDP_DEBUG("Transmitting ARP request\n");
939 /* Populate the ether_header */
940 eh
->ether_type
= htons(ETHERTYPE_ARP
);
941 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
942 enaddr_copy(ðerbroadcastaddr
, eh
->ether_dhost
);
944 /* Populate the ARP header */
945 ea
->arp_pro
= htons(ETHERTYPE_IP
);
946 ea
->arp_hln
= sizeof(ea
->arp_sha
);
947 ea
->arp_pln
= sizeof(ea
->arp_spa
);
948 ea
->arp_hrd
= htons(ARPHRD_ETHER
);
949 ea
->arp_op
= htons(ARPOP_REQUEST
);
952 enaddr_copy(ðerbroadcastaddr
, ea
->arp_tha
);
953 memcpy(ea
->arp_tpa
, (void *) &ip_addr
, sizeof(ip_addr
));
956 enaddr_copy(&kdp_current_mac_address
, ea
->arp_sha
);
957 memcpy(ea
->arp_spa
, (void *) &kdp_current_ip_address
, sizeof(kdp_current_ip_address
));
960 pkt
.len
= sizeof(struct ether_header
) + sizeof(struct ether_arp
);
962 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
966 kdp_arp_resolve(uint32_t arp_target_ip
, struct ether_addr
*resolved_MAC
)
968 int poll_count
= 256; /* ~770 ms modulo broadcast/delayed traffic? */
971 #define NUM_ARP_TX_RETRIES 5
973 target_ip
= arp_target_ip
;
974 flag_arp_resolved
= FALSE
;
977 pkt
.off
= pkt
.len
= 0;
981 if (tretries
>= NUM_ARP_TX_RETRIES
) {
985 KDP_DEBUG("ARP TX attempt #%d \n", tretries
);
987 transmit_ARP_request(arp_target_ip
);
989 while (!pkt
.input
&& !flag_arp_resolved
&& flag_panic_dump_in_progress
&& --poll_count
) {
993 if (flag_arp_resolved
) {
994 *resolved_MAC
= current_resolved_MAC
;
998 if (!flag_panic_dump_in_progress
|| pkt
.input
) /* we received a debugging packet, bail*/
1000 printf("Received a debugger packet,transferring control to debugger\n");
1001 /* Indicate that we should wait in the debugger when we return */
1002 kdp_flag
|= DBG_POST_CORE
;
1006 else /* We timed out */
1007 if (0 == poll_count
) {
1009 goto TRANSMIT_RETRY
;
1019 unsigned short reply_port
;
1020 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
1022 kdp
.saved_state
= saved_state
; // see comment in kdp_raise_exception
1029 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
1031 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
1034 // ignore replies -- we're not expecting them anyway.
1035 if (hdr
->is_reply
) {
1039 if (hdr
->request
== KDP_REATTACH
)
1040 exception_seq
= hdr
->seq
;
1042 // check for retransmitted request
1043 if (hdr
->seq
== (exception_seq
- 1)) {
1044 /* retransmit last reply */
1045 kdp_send_data(&saved_reply
.data
[saved_reply
.off
],
1048 } else if ((hdr
->seq
!= exception_seq
) &&
1049 (hdr
->request
!= KDP_CONNECT
)) {
1050 printf("kdp: bad sequence %d (want %d)\n",
1051 hdr
->seq
, exception_seq
);
1055 /* This is a manual side-channel to the main KDP protocol.
1056 * A client like GDB/kgmacros can manually construct
1057 * a request, set the input flag, issue a dummy KDP request,
1058 * and then manually collect the result
1060 if (manual_pkt
.input
) {
1061 kdp_hdr_t
*manual_hdr
= (kdp_hdr_t
*)&manual_pkt
.data
;
1062 unsigned short manual_port_unused
= 0;
1063 if (!manual_hdr
->is_reply
) {
1065 kdp_packet((unsigned char *)&manual_pkt
.data
,
1066 (int *)&manual_pkt
.len
,
1067 &manual_port_unused
);
1069 manual_pkt
.input
= 0;
1072 if (kdp_packet((unsigned char*)&pkt
.data
[pkt
.off
],
1074 (unsigned short *)&reply_port
)) {
1075 boolean_t sideband
= FALSE
;
1077 /* if it's an already connected error message,
1078 * send a sideband reply for that. for successful connects,
1079 * make sure the sequence number is correct. */
1080 if (hdr
->request
== KDP_CONNECT
) {
1081 kdp_connect_reply_t
*rp
=
1082 (kdp_connect_reply_t
*) &pkt
.data
[pkt
.off
];
1083 kdp_error_t err
= rp
->error
;
1085 if (err
== KDPERR_NO_ERROR
) {
1086 exception_seq
= hdr
->seq
;
1087 } else if (err
== KDPERR_ALREADY_CONNECTED
) {
1092 kdp_reply(reply_port
, sideband
);
1097 } while (kdp
.is_halted
);
1101 kdp_connection_wait(void)
1103 unsigned short reply_port
;
1104 struct ether_addr kdp_mac_addr
= kdp_get_mac_addr();
1105 unsigned int ip_addr
= ntohl(kdp_get_ip_address());
1108 * Do both a printf() and a kprintf() of the MAC and IP so that
1109 * they will print out on headless machines but not be added to
1113 if (KDP_SERIAL_ENABLED()) {
1114 printf("Using serial KDP.\n");
1115 kprintf("Using serial KDP.\n");
1117 printf( "ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1118 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
1119 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
1120 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
1121 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
1122 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
1123 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
1125 kprintf( "ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1126 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
1127 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
1128 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
1129 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
1130 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
1131 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
1133 printf( "ip address: %d.%d.%d.%d\n",
1134 (ip_addr
& 0xff000000) >> 24,
1135 (ip_addr
& 0xff0000) >> 16,
1136 (ip_addr
& 0xff00) >> 8,
1139 kprintf( "ip address: %d.%d.%d.%d\n",
1140 (ip_addr
& 0xff000000) >> 24,
1141 (ip_addr
& 0xff0000) >> 16,
1142 (ip_addr
& 0xff00) >> 8,
1146 printf("\nWaiting for remote debugger connection.\n");
1149 if (reattach_wait
== 0) {
1150 if((kdp_flag
& KDP_GETC_ENA
) && (0 != kdp_getc()))
1152 printf("Options..... Type\n");
1153 printf("------------ ----\n");
1154 printf("continue.... 'c'\n");
1155 printf("reboot...... 'r'\n");
1157 printf("enter kdb... 'k'\n");
1166 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
1168 while (!pkt
.input
) {
1169 if (kdp_flag
& KDP_GETC_ENA
) {
1170 switch(kdp_getc()) {
1172 printf("Continuing...\n");
1175 printf("Rebooting...\n");
1176 kdp_machine_reboot();
1180 printf("calling kdb...\n");
1184 printf("not implemented...\n");
1194 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
1196 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
1198 if (hdr
->request
== KDP_HOSTREBOOT
) {
1199 kdp_machine_reboot();
1200 /* should not return! */
1202 if (((hdr
->request
== KDP_CONNECT
) || (hdr
->request
== KDP_REATTACH
)) &&
1203 !hdr
->is_reply
&& (hdr
->seq
== exception_seq
)) {
1204 if (kdp_packet((unsigned char *)&pkt
.data
[pkt
.off
],
1206 (unsigned short *)&reply_port
))
1207 kdp_reply(reply_port
, FALSE
);
1208 if (hdr
->request
== KDP_REATTACH
) {
1210 hdr
->request
=KDP_DISCONNECT
;
1216 } while (!kdp
.is_conn
);
1218 if (current_debugger
== KDP_CUR_DB
)
1220 printf("Connected to remote debugger.\n");
1225 unsigned int exception
,
1227 unsigned int subcode
1230 unsigned short remote_port
;
1231 unsigned int timeout_count
= 100;
1232 unsigned int poll_timeout
;
1235 pkt
.off
= sizeof (struct ether_header
) + sizeof (struct udpiphdr
);
1236 kdp_exception((unsigned char *)&pkt
.data
[pkt
.off
],
1238 (unsigned short *)&remote_port
,
1239 (unsigned int)exception
,
1241 (unsigned int)subcode
);
1243 kdp_send(remote_port
);
1246 while(!pkt
.input
&& poll_timeout
)
1253 if (!kdp_exception_ack(&pkt
.data
[pkt
.off
], pkt
.len
)) {
1260 if (kdp
.exception_ack_needed
)
1261 kdp_us_spin(250000);
1263 } while (kdp
.exception_ack_needed
&& timeout_count
--);
1265 if (kdp
.exception_ack_needed
) {
1266 // give up & disconnect
1267 printf("kdp: exception ack timeout\n");
1268 if (current_debugger
== KDP_CUR_DB
)
1275 kdp_raise_exception(
1276 unsigned int exception
,
1278 unsigned int subcode
,
1284 /* Was a system trace requested ? */
1285 if (kdp_snapshot
&& (!panic_active()) && (panic_caller
== 0)) {
1286 stack_snapshot_ret
= kdp_stackshot(stack_snapshot_pid
,
1287 stack_snapshot_buf
, stack_snapshot_bufsize
,
1288 stack_snapshot_flags
, stack_snapshot_dispatch_offset
,
1289 &stack_snapshot_bytes_traced
);
1293 disable_preemption();
1295 if (saved_state
== 0)
1296 printf("kdp_raise_exception with NULL state\n");
1299 if (exception
!= EXC_BREAKPOINT
) {
1300 if (exception
> EXC_BREAKPOINT
|| exception
< EXC_BAD_ACCESS
) {
1303 printf("%s exception (%x,%x,%x)\n",
1304 exception_message
[index
],
1305 exception
, code
, subcode
);
1310 /* XXX WMG it seems that sometimes it doesn't work to let kdp_handler
1311 * do this. I think the client and the host can get out of sync.
1313 kdp
.saved_state
= saved_state
;
1314 kdp
.kdp_cpu
= cpu_number();
1315 kdp
.kdp_thread
= current_thread();
1318 (*kdp_en_setmode
)(TRUE
); /* enabling link mode */
1321 kdp_panic("kdp_raise_exception");
1323 if (((kdp_flag
& KDP_PANIC_DUMP_ENABLED
) || (kdp_flag
& PANIC_LOG_DUMP
))
1324 && (panicstr
!= (char *) 0)) {
1326 if (kdp_flag
& REBOOT_POST_CORE
)
1327 kdp_machine_reboot();
1330 if ((kdp_flag
& PANIC_CORE_ON_NMI
) && (panicstr
== (char *) 0) &&
1333 disable_debug_output
= disableConsoleOutput
= FALSE
;
1336 if (!(kdp_flag
& DBG_POST_CORE
))
1337 goto exit_raise_exception
;
1342 kdp_connection_wait();
1344 kdp_send_exception(exception
, code
, subcode
);
1345 if (kdp
.exception_ack_needed
) {
1346 kdp
.exception_ack_needed
= FALSE
;
1347 kdp_remove_all_breakpoints();
1348 printf("Remote debugger disconnected.\n");
1353 kdp
.is_halted
= TRUE
; /* XXX */
1354 kdp_handler(saved_state
);
1357 kdp_remove_all_breakpoints();
1358 printf("Remote debugger disconnected.\n");
1361 /* Allow triggering a panic core dump when connected to the machine
1362 * Continuing after setting kdp_trigger_core_dump should do the
1366 if (1 == kdp_trigger_core_dump
) {
1367 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
1369 if (kdp_flag
& REBOOT_POST_CORE
)
1370 kdp_machine_reboot();
1371 kdp_trigger_core_dump
= 0;
1374 /* Trigger a reboot if the user has set this flag through the
1375 * debugger.Ideally, this would be done through the HOSTREBOOT packet
1376 * in the protocol,but that will need gdb support,and when it's
1377 * available, it should work automatically.
1379 if (1 == flag_kdp_trigger_reboot
) {
1380 kdp_machine_reboot();
1381 /* If we're still around, reset the flag */
1382 flag_kdp_trigger_reboot
= 0;
1385 if (kdp_reentry_deadline
) {
1386 kdp_schedule_debugger_reentry(kdp_reentry_deadline
);
1387 printf("Debugger re-entry scheduled in %d milliseconds\n", kdp_reentry_deadline
);
1388 kdp_reentry_deadline
= 0;
1393 if (reattach_wait
== 1)
1396 exit_raise_exception
:
1398 (*kdp_en_setmode
)(FALSE
); /* link cleanup */
1399 enable_preemption();
1405 kdp
.reply_port
= kdp
.exception_port
= 0;
1406 kdp
.is_halted
= kdp
.is_conn
= FALSE
;
1407 kdp
.exception_seq
= kdp
.conn_seq
= 0;
1408 kdp
.session_key
= 0;
1412 create_panic_header(unsigned int request
, const char *corename
,
1413 unsigned length
, unsigned int block
)
1415 struct udpiphdr aligned_ui
, *ui
= &aligned_ui
;
1416 struct ip aligned_ip
, *ip
= &aligned_ip
;
1417 struct ether_header
*eh
;
1418 struct corehdr
*coreh
;
1419 const char *mode
= "octet";
1420 char modelen
= strlen(mode
);
1422 size_t fmask_size
= sizeof(KDP_FEATURE_MASK_STRING
) + sizeof(kdp_crashdump_feature_mask
);
1424 pkt
.off
= sizeof (struct ether_header
);
1425 pkt
.len
= (unsigned int)(length
+ ((request
== KDP_WRQ
) ? modelen
+ fmask_size
: 0) +
1426 (corename
? strlen(corename
): 0) + sizeof(struct corehdr
));
1429 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
1431 ui
= (struct udpiphdr
*)&pkt
.data
[pkt
.off
];
1433 ui
->ui_next
= ui
->ui_prev
= 0;
1435 ui
->ui_pr
= IPPROTO_UDP
;
1436 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct udphdr
));
1437 ui
->ui_src
.s_addr
= (uint32_t)kdp_current_ip_address
;
1438 /* Already in network byte order via inet_aton() */
1439 ui
->ui_dst
.s_addr
= panic_server_ip
;
1440 ui
->ui_sport
= htons(panicd_port
);
1441 ui
->ui_dport
= ((request
== KDP_WRQ
) ? htons(panicd_port
) : last_panic_port
);
1442 ui
->ui_ulen
= ui
->ui_len
;
1445 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
1446 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
1448 ip
= (struct ip
*)&pkt
.data
[pkt
.off
];
1450 ip
->ip_len
= htons(sizeof (struct udpiphdr
) + pkt
.len
);
1451 ip
->ip_v
= IPVERSION
;
1452 ip
->ip_id
= htons(ip_id
++);
1453 ip
->ip_hl
= sizeof (struct ip
) >> 2;
1454 ip
->ip_ttl
= udp_ttl
;
1456 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
1458 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
1461 pkt
.len
+= (unsigned int)sizeof (struct udpiphdr
);
1463 pkt
.off
+= (unsigned int)sizeof (struct udpiphdr
);
1465 coreh
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1466 coreh
->th_opcode
= htons((u_short
)request
);
1468 if (request
== KDP_WRQ
)
1472 cp
= coreh
->th_u
.tu_rpl
;
1473 cp
+= strlcpy (cp
, corename
, KDP_MAXPACKET
);
1475 cp
+= strlcpy (cp
, mode
, KDP_MAXPACKET
- strlen(corename
));
1477 cp
+= strlcpy(cp
, KDP_FEATURE_MASK_STRING
, sizeof(KDP_FEATURE_MASK_STRING
));
1478 *cp
++ = '\0'; /* Redundant */
1479 bcopy(&kdp_crashdump_feature_mask
, cp
, sizeof(kdp_crashdump_feature_mask
));
1480 kdp_crashdump_pkt_size
= KDP_LARGE_CRASHDUMP_PKT_SIZE
;
1481 PE_parse_boot_argn("kdp_crashdump_pkt_size", &kdp_crashdump_pkt_size
, sizeof(kdp_crashdump_pkt_size
));
1482 cp
+= sizeof(kdp_crashdump_feature_mask
);
1483 *(uint32_t *)cp
= htonl(kdp_crashdump_pkt_size
);
1487 coreh
->th_block
= htonl((unsigned int) block
);
1490 pkt
.off
-= (unsigned int)sizeof (struct udpiphdr
);
1491 pkt
.off
-= (unsigned int)sizeof (struct ether_header
);
1493 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
1494 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
1495 enaddr_copy(&destination_mac
, eh
->ether_dhost
);
1496 eh
->ether_type
= htons(ETHERTYPE_IP
);
1498 pkt
.len
+= (unsigned int)sizeof (struct ether_header
);
1502 static int kdp_send_crashdump_seek(char *corename
, uint64_t seek_off
)
1506 if (kdp_feature_large_crashdumps
) {
1507 panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, corename
,
1511 uint32_t off
= (uint32_t) seek_off
;
1512 panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, corename
,
1516 if (panic_error
< 0) {
1517 printf ("kdp_send_crashdump_pkt failed with error %d\n",
1525 int kdp_send_crashdump_data(unsigned int request
, char *corename
,
1526 int64_t length
, caddr_t txstart
)
1528 int panic_error
= 0;
1530 while (length
> 0) {
1531 uint64_t chunk
= MIN(kdp_crashdump_pkt_size
, length
);
1533 panic_error
= kdp_send_crashdump_pkt(request
, corename
, chunk
,
1535 if (panic_error
< 0) {
1536 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
1546 uint32_t kdp_crashdump_short_pkt
;
1549 kdp_send_crashdump_pkt(unsigned int request
, char *corename
,
1550 uint64_t length
, void *panic_data
)
1553 struct corehdr
*th
= NULL
;
1554 char rretries
, tretries
;
1556 if (kdp_dump_start_time
== 0) {
1557 kdp_dump_start_time
= mach_absolute_time();
1558 kdp_superblock_dump_start_time
= kdp_dump_start_time
;
1561 tretries
= rretries
= 0;
1562 poll_count
= KDP_CRASHDUMP_POLL_COUNT
;
1563 pkt
.off
= pkt
.len
= 0;
1564 if (request
== KDP_WRQ
) /* longer timeout for initial request */
1570 if (tretries
>=15) {
1571 /* The crashdump server is unreachable for some reason. This could be a network
1572 * issue or, if we've been especially unfortunate, we've hit Radar 2760413,
1573 * which is a long standing problem with the IOKit polled mode network driver
1574 * shim which can prevent transmits/receives completely.
1576 printf ("Cannot contact panic server, timing out.\n");
1581 printf("TX retry #%d ", tretries
);
1583 th
= create_panic_header(request
, corename
, (unsigned)length
, panic_block
);
1585 if (request
== KDP_DATA
) {
1586 /* as all packets are kdp_crashdump_pkt_size in length, the last packet
1587 * may end up with trailing bits. make sure that those
1588 * bits aren't confusing. */
1589 if (length
< kdp_crashdump_pkt_size
) {
1590 kdp_crashdump_short_pkt
++;
1591 memset(th
->th_data
+ length
, 'Y',
1592 kdp_crashdump_pkt_size
- (uint32_t) length
);
1595 if (!kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)panic_data
, (caddr_t
) th
->th_data
, length
)) {
1596 uintptr_t next_page
= round_page((uintptr_t)panic_data
);
1597 memset((caddr_t
) th
->th_data
, 'X', (size_t)length
);
1598 if ((next_page
- ((uintptr_t) panic_data
)) < length
) {
1599 uint64_t resid
= length
- (next_page
- (intptr_t) panic_data
);
1600 if (!kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)next_page
, (caddr_t
) th
->th_data
+ (length
- resid
), resid
)) {
1601 memset((caddr_t
) th
->th_data
+ (length
- resid
), 'X', (size_t)resid
);
1606 else if (request
== KDP_SEEK
) {
1607 if (kdp_feature_large_crashdumps
)
1608 *(uint64_t *) th
->th_data
= OSSwapHostToBigInt64((*(uint64_t *) panic_data
));
1610 *(unsigned int *) th
->th_data
= htonl(*(unsigned int *) panic_data
);
1613 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
1615 /* Listen for the ACK */
1617 while (!pkt
.input
&& flag_panic_dump_in_progress
&& poll_count
) {
1626 th
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1627 if (request
== KDP_WRQ
) {
1628 uint16_t opcode64
= ntohs(th
->th_opcode
);
1629 uint16_t features64
= (opcode64
& 0xFF00)>>8;
1630 if ((opcode64
& 0xFF) == KDP_ACK
) {
1631 kdp_feature_large_crashdumps
= features64
& KDP_FEATURE_LARGE_CRASHDUMPS
;
1632 if (features64
& KDP_FEATURE_LARGE_PKT_SIZE
) {
1633 kdp_feature_large_pkt_size
= 1;
1636 kdp_feature_large_pkt_size
= 0;
1637 kdp_crashdump_pkt_size
= 512;
1639 printf("Protocol features: 0x%x\n", (uint32_t) features64
);
1640 th
->th_opcode
= htons(KDP_ACK
);
1643 if (ntohs(th
->th_opcode
) == KDP_ACK
&& ntohl(th
->th_block
) == panic_block
) {
1646 if (ntohs(th
->th_opcode
) == KDP_ERROR
) {
1647 printf("Panic server returned error %d, retrying\n", ntohl(th
->th_code
));
1649 goto TRANSMIT_RETRY
;
1652 if (ntohl(th
->th_block
) == (panic_block
- 1)) {
1653 printf("RX retry ");
1655 goto TRANSMIT_RETRY
;
1661 if (!flag_panic_dump_in_progress
) /* we received a debugging packet, bail*/
1663 printf("Received a debugger packet,transferring control to debugger\n");
1664 /* Configure that if not set ..*/
1665 kdp_flag
|= DBG_POST_CORE
;
1668 else /* We timed out */
1669 if (0 == poll_count
) {
1671 kdp_us_spin ((tretries%4
) * panic_timeout
); /* capped linear backoff */
1672 goto TRANSMIT_RETRY
;
1675 if (!(++panic_block
% SBLOCKSZ
)) {
1677 kdb_printf_unbuffered(".");
1678 ctime
= mach_absolute_time();
1679 kdp_superblock_dump_time
= ctime
- kdp_superblock_dump_start_time
;
1680 kdp_superblock_dump_start_time
= ctime
;
1681 if (kdp_superblock_dump_time
> kdp_max_superblock_dump_time
)
1682 kdp_max_superblock_dump_time
= kdp_superblock_dump_time
;
1683 if (kdp_superblock_dump_time
< kdp_min_superblock_dump_time
)
1684 kdp_min_superblock_dump_time
= kdp_superblock_dump_time
;
1687 if (request
== KDP_EOF
) {
1688 printf("\nTotal number of packets transmitted: %d\n", panic_block
);
1689 printf("Avg. superblock transfer abstime 0x%llx\n", ((mach_absolute_time() - kdp_dump_start_time
) / panic_block
) * SBLOCKSZ
);
1690 printf("Minimum superblock transfer abstime: 0x%llx\n", kdp_min_superblock_dump_time
);
1691 printf("Maximum superblock transfer abstime: 0x%llx\n", kdp_max_superblock_dump_time
);
1699 return ((c
> 47) && (c
< 58));
1701 /* From user mode Libc - this ought to be in a library */
1703 strnstr(char *s
, const char *find
, size_t slen
)
1708 if ((c
= *find
++) != '\0') {
1712 if ((sc
= *s
++) == '\0' || slen
-- < 1)
1717 } while (strncmp(s
, find
, len
) != 0);
1723 /* Horrid hack to extract xnu version if possible - a much cleaner approach
1724 * would be to have the integrator run a script which would copy the
1725 * xnu version into a string or an int somewhere at project submission
1726 * time - makes assumptions about sizeof(version), but will not fail if
1727 * it changes, but may be incorrect.
1729 /* 2006: Incorporated a change from Darwin user P. Lovell to extract
1730 * the minor kernel version numbers from the version string.
1733 kdp_get_xnu_version(char *versionbuf
)
1740 strlcpy(vstr
, "custom", 10);
1741 if (kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)version
, versionbuf
, 128)) {
1742 versionbuf
[127] = '\0';
1743 versionpos
= strnstr(versionbuf
, "xnu-", 115);
1745 strncpy(vstr
, versionpos
, sizeof(vstr
));
1746 vstr
[sizeof(vstr
)-1] = '\0';
1747 vptr
= vstr
+ 4; /* Begin after "xnu-" */
1748 while (*vptr
&& (isdigit(*vptr
) || *vptr
== '.'))
1751 /* Remove trailing period, if any */
1752 if (*(--vptr
) == '.')
1757 strlcpy(versionbuf
, vstr
, KDP_MAXPACKET
);
1762 kdp_set_dump_info(const uint32_t flags
, const char *filename
,
1763 const char *destipstr
, const char *routeripstr
,
1764 const uint32_t port
)
1768 if (destipstr
&& (destipstr
[0] != '\0')) {
1769 strlcpy(panicd_ip_str
, destipstr
, sizeof(panicd_ip_str
));
1770 panicd_specified
= 1;
1773 if (routeripstr
&& (routeripstr
[0] != '\0')) {
1774 strlcpy(router_ip_str
, routeripstr
, sizeof(router_ip_str
));
1775 router_specified
= 1;
1778 if (filename
&& (filename
[0] != '\0')) {
1779 strlcpy(corename_str
, filename
, sizeof(corename_str
));
1780 corename_specified
= TRUE
;
1782 corename_specified
= FALSE
;
1788 /* on a disconnect, should we stay in KDP or not? */
1789 noresume_on_disconnect
= (flags
& KDP_DUMPINFO_NORESUME
) ? 1 : 0;
1791 if ((flags
& KDP_DUMPINFO_DUMP
) == 0)
1794 /* the rest of the commands can modify kdp_flags */
1795 cmd
= flags
& KDP_DUMPINFO_MASK
;
1796 if (cmd
== KDP_DUMPINFO_DISABLE
) {
1797 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
1798 panicd_specified
= 0;
1799 kdp_trigger_core_dump
= 0;
1803 kdp_flag
&= ~REBOOT_POST_CORE
;
1804 if (flags
& KDP_DUMPINFO_REBOOT
)
1805 kdp_flag
|= REBOOT_POST_CORE
;
1807 kdp_flag
&= ~PANIC_LOG_DUMP
;
1808 if (cmd
== KDP_DUMPINFO_PANICLOG
)
1809 kdp_flag
|= PANIC_LOG_DUMP
;
1811 kdp_flag
&= ~SYSTEM_LOG_DUMP
;
1812 if (cmd
== KDP_DUMPINFO_SYSTEMLOG
)
1813 kdp_flag
|= SYSTEM_LOG_DUMP
;
1815 /* trigger a dump */
1816 kdp_flag
|= DBG_POST_CORE
;
1818 flag_dont_abort_panic_dump
= (flags
& KDP_DUMPINFO_NOINTR
) ?
1822 logPanicDataToScreen
= 1;
1823 disableConsoleOutput
= 0;
1824 disable_debug_output
= 0;
1825 kdp_trigger_core_dump
= 1;
1829 kdp_get_dump_info(uint32_t *flags
, char *filename
, char *destipstr
,
1830 char *routeripstr
, uint32_t *port
)
1833 if (panicd_specified
)
1834 strlcpy(destipstr
, panicd_ip_str
,
1835 sizeof(panicd_ip_str
));
1837 destipstr
[0] = '\0';
1841 if (router_specified
)
1842 strlcpy(routeripstr
, router_ip_str
,
1843 sizeof(router_ip_str
));
1845 routeripstr
[0] = '\0';
1849 if (corename_specified
)
1850 strlcpy(filename
, corename_str
,
1851 sizeof(corename_str
));
1858 *port
= panicd_port
;
1862 if (!panicd_specified
)
1863 *flags
|= KDP_DUMPINFO_DISABLE
;
1864 else if (kdp_flag
& PANIC_LOG_DUMP
)
1865 *flags
|= KDP_DUMPINFO_PANICLOG
;
1867 *flags
|= KDP_DUMPINFO_CORE
;
1869 if (noresume_on_disconnect
)
1870 *flags
|= KDP_DUMPINFO_NORESUME
;
1875 /* Primary dispatch routine for the system dump */
1877 kdp_panic_dump(void)
1879 char coreprefix
[10];
1883 uint32_t current_ip
= ntohl((uint32_t)kdp_current_ip_address
);
1885 if (flag_panic_dump_in_progress
) {
1886 kdb_printf("System dump aborted.\n");
1887 goto panic_dump_exit
;
1890 printf("Entering system dump routine\n");
1892 if (!kdp_en_recv_pkt
|| !kdp_en_send_pkt
) {
1893 kdb_printf("Error: No transport device registered for kernel crashdump\n");
1897 if (!panicd_specified
) {
1898 kdb_printf("A dump server was not specified in the boot-args, terminating kernel core dump.\n");
1899 goto panic_dump_exit
;
1902 flag_panic_dump_in_progress
= TRUE
;
1906 kdp_panic("kdp_panic_dump: unexpected pending input packet");
1908 kdp_get_xnu_version((char *) &pkt
.data
[0]);
1910 if (!corename_specified
) {
1911 /* Panic log bit takes precedence over core dump bit */
1912 if ((panicstr
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
))
1913 strlcpy(coreprefix
, "paniclog", sizeof(coreprefix
));
1914 else if (kdp_flag
& SYSTEM_LOG_DUMP
)
1915 strlcpy(coreprefix
, "systemlog", sizeof(coreprefix
));
1917 strlcpy(coreprefix
, "core", sizeof(coreprefix
));
1919 abstime
= mach_absolute_time();
1920 pkt
.data
[20] = '\0';
1921 snprintf (corename_str
, sizeof(corename_str
), "%s-%s-%d.%d.%d.%d-%x",
1922 coreprefix
, &pkt
.data
[0],
1923 (current_ip
& 0xff000000) >> 24,
1924 (current_ip
& 0xff0000) >> 16,
1925 (current_ip
& 0xff00) >> 8,
1926 (current_ip
& 0xff),
1927 (unsigned int) (abstime
& 0xffffffff));
1930 if (0 == inet_aton(panicd_ip_str
, (struct in_addr
*) &panic_server_ip
)) {
1931 kdb_printf("inet_aton() failed interpreting %s as a panic server IP\n", panicd_ip_str
);
1934 kdb_printf("Attempting connection to panic server configured at IP %s, port %d\n", panicd_ip_str
, panicd_port
);
1936 destination_mac
= router_mac
;
1938 if (kdp_arp_resolve(panic_server_ip
, &temp_mac
)) {
1939 kdb_printf("Resolved %s's (or proxy's) link level address\n", panicd_ip_str
);
1940 destination_mac
= temp_mac
;
1943 if (!flag_panic_dump_in_progress
) goto panic_dump_exit
;
1944 if (router_specified
) {
1945 if (0 == inet_aton(router_ip_str
, (struct in_addr
*) &parsed_router_ip
))
1946 kdb_printf("inet_aton() failed interpreting %s as an IP\n", router_ip_str
);
1948 router_ip
= parsed_router_ip
;
1949 if (kdp_arp_resolve(router_ip
, &temp_mac
)) {
1950 destination_mac
= temp_mac
;
1951 kdb_printf("Routing through specified router IP %s (%d)\n", router_ip_str
, router_ip
);
1957 if (!flag_panic_dump_in_progress
) goto panic_dump_exit
;
1959 kdb_printf("Transmitting packets to link level address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1960 destination_mac
.ether_addr_octet
[0] & 0xff,
1961 destination_mac
.ether_addr_octet
[1] & 0xff,
1962 destination_mac
.ether_addr_octet
[2] & 0xff,
1963 destination_mac
.ether_addr_octet
[3] & 0xff,
1964 destination_mac
.ether_addr_octet
[4] & 0xff,
1965 destination_mac
.ether_addr_octet
[5] & 0xff);
1967 kdb_printf("Kernel map size is %llu\n", (unsigned long long) get_vmmap_size(kernel_map
));
1968 kdb_printf("Sending write request for %s\n", corename_str
);
1970 if ((panic_error
= kdp_send_crashdump_pkt(KDP_WRQ
, corename_str
, 0 , NULL
)) < 0) {
1971 kdb_printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
1972 goto panic_dump_exit
;
1975 /* Just the panic log requested */
1976 if ((panicstr
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
)) {
1977 kdb_printf_unbuffered("Transmitting panic log, please wait: ");
1978 kdp_send_crashdump_data(KDP_DATA
, corename_str
,
1979 debug_buf_ptr
- debug_buf
,
1981 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
1982 printf("Please file a bug report on this panic, if possible.\n");
1983 goto panic_dump_exit
;
1986 /* maybe we wanted the systemlog */
1987 if (kdp_flag
& SYSTEM_LOG_DUMP
) {
1988 long start_off
= msgbufp
->msg_bufx
;
1991 kdb_printf_unbuffered("Transmitting system log, please wait: ");
1992 if (start_off
>= msgbufp
->msg_bufr
) {
1993 len
= msgbufp
->msg_size
- start_off
;
1994 kdp_send_crashdump_data(KDP_DATA
, corename_str
, len
,
1995 msgbufp
->msg_bufc
+ start_off
);
1996 /* seek to remove trailing bytes */
1997 kdp_send_crashdump_seek(corename_str
, len
);
2001 if (start_off
!= msgbufp
->msg_bufr
) {
2002 len
= msgbufp
->msg_bufr
- start_off
;
2003 kdp_send_crashdump_data(KDP_DATA
, corename_str
, len
,
2004 msgbufp
->msg_bufc
+ start_off
);
2007 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
2008 goto panic_dump_exit
;
2011 /* We want a core dump if we're here */
2015 abort_panic_transfer();
2023 abort_panic_transfer(void)
2025 flag_panic_dump_in_progress
= FALSE
;
2026 flag_dont_abort_panic_dump
= FALSE
;
2031 #if CONFIG_SERIAL_KDP
2033 static boolean_t needs_serial_init
= TRUE
;
2036 kdp_serial_send(void *rpkt
, unsigned int rpkt_len
)
2039 kdp_serialize_packet((unsigned char *)rpkt
, rpkt_len
, pal_serial_putc
);
2043 kdp_serial_receive(void *rpkt
, unsigned int *rpkt_len
, unsigned int timeout
)
2046 uint64_t now
, deadline
;
2048 clock_interval_to_deadline(timeout
, 1000 * 1000 /* milliseconds */, &deadline
);
2051 for(clock_get_uptime(&now
); now
< deadline
; clock_get_uptime(&now
))
2053 readkar
= pal_serial_getc();
2056 unsigned char *packet
;
2057 // printf("got char %02x\n", readkar);
2058 if((packet
= kdp_unserialize_packet(readkar
,rpkt_len
)))
2060 memcpy(rpkt
, packet
, *rpkt_len
);
2069 kdp_serial_setmode(boolean_t active
)
2071 if (active
== FALSE
) /* leaving KDP */
2074 if (!needs_serial_init
)
2078 needs_serial_init
= FALSE
;
2083 static void kdp_serial_callout(__unused
void *arg
, kdp_event_t event
)
2085 /* When we stop KDP, set the bit to re-initialize the console serial port
2086 * the next time we send/receive a KDP packet. We don't do it on
2087 * KDP_EVENT_ENTER directly because it also gets called when we trap to KDP
2088 * for non-external debugging, i.e., stackshot or core dumps.
2090 * Set needs_serial_init on exit (and initialization, see above) and not
2091 * enter because enter is sent multiple times and causes excess reinitialization.
2096 case KDP_EVENT_PANICLOG
:
2097 case KDP_EVENT_ENTER
:
2099 case KDP_EVENT_EXIT
:
2100 needs_serial_init
= TRUE
;
2105 #endif /* CONFIG_SERIAL_KDP */
2110 strlcpy(kdp_kernelversion_string
, version
, sizeof(kdp_kernelversion_string
));
2112 /* Relies on platform layer calling panic_init() before kdp_init() */
2113 if (kernel_uuid
[0] != '\0') {
2115 * Update kdp_kernelversion_string with our UUID
2116 * generated at link time.
2119 strlcat(kdp_kernelversion_string
, "; UUID=", sizeof(kdp_kernelversion_string
));
2120 strlcat(kdp_kernelversion_string
, kernel_uuid
, sizeof(kdp_kernelversion_string
));
2123 if (debug_boot_arg
& DB_REBOOT_POST_CORE
)
2124 kdp_flag
|= REBOOT_POST_CORE
;
2125 #if CONFIG_SERIAL_KDP
2127 struct in_addr ipaddr
;
2128 struct ether_addr macaddr
;
2132 //serial will be the debugger, unless match name is explicitly provided, and it's not "serial"
2133 if(PE_parse_boot_argn("kdp_match_name", kdpname
, sizeof(kdpname
)) && strncmp(kdpname
, "serial", sizeof(kdpname
)) != 0)
2136 // serial must be explicitly requested
2137 if(!PE_parse_boot_argn("kdp_match_name", kdpname
, sizeof(kdpname
)) || strncmp(kdpname
, "serial", sizeof(kdpname
)) != 0)
2141 kprintf("Initializing serial KDP\n");
2143 kdp_register_callout(kdp_serial_callout
, NULL
);
2144 kdp_register_link(NULL
, kdp_serial_setmode
);
2145 kdp_register_send_receive(kdp_serial_send
, kdp_serial_receive
);
2147 /* fake up an ip and mac for early serial debugging */
2148 macaddr
.ether_addr_octet
[0] = 's';
2149 macaddr
.ether_addr_octet
[1] = 'e';
2150 macaddr
.ether_addr_octet
[2] = 'r';
2151 macaddr
.ether_addr_octet
[3] = 'i';
2152 macaddr
.ether_addr_octet
[4] = 'a';
2153 macaddr
.ether_addr_octet
[5] = 'l';
2154 ipaddr
.s_addr
= KDP_SERIAL_IPADDR
;
2155 kdp_set_ip_and_mac_addresses(&ipaddr
, &macaddr
);
2157 #endif /* CONFIG_SERIAL_KDP */