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.
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>
51 #include <kdp/kdp_serial.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_protos.h>
56 #include <vm/vm_kern.h> /* kernel_map */
58 #include <mach/memory_object_types.h>
59 #include <machine/pal_routines.h>
61 #include <sys/msgbuf.h>
63 /* we just want the link status flags, so undef KERNEL_PRIVATE for this
66 #include <net/if_media.h>
67 #define KERNEL_PRIVATE
71 #include <IOKit/IOPlatformExpert.h>
72 #include <libkern/version.h>
74 #define DO_ALIGN 1 /* align all packet data accesses */
75 #define KDP_SERIAL_IPADDR 0xABADBABE /* IP address used for serial KDP */
76 #define LINK_UP_STATUS (IFM_AVALID | IFM_ACTIVE)
78 extern int kdp_getc(void);
79 extern int reattach_wait
;
81 /* only used by IONetworkingFamily */
82 typedef uint32_t (*kdp_link_t
)(void);
83 typedef boolean_t (*kdp_mode_t
)(boolean_t
);
84 void kdp_register_link(kdp_link_t link
, kdp_mode_t mode
);
85 void kdp_unregister_link(kdp_link_t link
, kdp_mode_t mode
);
87 static u_short ip_id
; /* ip packet ctr, for ids */
89 /* @(#)udp_usrreq.c 2.2 88/05/23 4.0NFSSRC SMI; from UCB 7.1 6/5/86 */
92 * UDP protocol implementation.
93 * Per RFC 768, August, 1980.
95 #define UDP_TTL 60 /* deflt time to live for UDP packets */
96 int udp_ttl
= UDP_TTL
;
97 static unsigned char exception_seq
;
100 unsigned char data
[KDP_MAXPACKET
];
101 unsigned int off
, len
;
105 struct kdp_manual_pkt manual_pkt
;
110 struct ether_addr ea
;
114 struct ether_addr ea
;
119 *exception_message
[] = {
121 "Memory access", /* EXC_BAD_ACCESS */
122 "Failed instruction", /* EXC_BAD_INSTRUCTION */
123 "Arithmetic", /* EXC_ARITHMETIC */
124 "Emulation", /* EXC_EMULATION */
125 "Software", /* EXC_SOFTWARE */
126 "Breakpoint" /* EXC_BREAKPOINT */
129 volatile int kdp_flag
= 0;
131 static kdp_send_t kdp_en_send_pkt
;
132 static kdp_receive_t kdp_en_recv_pkt
;
133 static kdp_link_t kdp_en_linkstatus
;
134 static kdp_mode_t kdp_en_setmode
;
136 #if CONFIG_SERIAL_KDP
137 static void kdp_serial_send(void *rpkt
, unsigned int rpkt_len
);
138 #define KDP_SERIAL_ENABLED() (kdp_en_send_pkt == kdp_serial_send)
140 #define KDP_SERIAL_ENABLED() (0)
143 static uint32_t kdp_current_ip_address
= 0;
144 static struct ether_addr kdp_current_mac_address
= {{0, 0, 0, 0, 0, 0}};
145 static void *kdp_current_ifp
;
147 static void kdp_handler( void *);
149 static uint32_t panic_server_ip
= 0;
150 static uint32_t parsed_router_ip
= 0;
151 static uint32_t router_ip
= 0;
152 static uint32_t target_ip
= 0;
154 static boolean_t save_ip_in_nvram
= FALSE
;
156 static volatile boolean_t panicd_specified
= FALSE
;
157 static boolean_t router_specified
= FALSE
;
158 static boolean_t corename_specified
= FALSE
;
159 static unsigned int panicd_port
= CORE_REMOTE_PORT
;
161 static struct ether_addr etherbroadcastaddr
= {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
163 static struct ether_addr router_mac
= {{0, 0, 0 , 0, 0, 0}};
164 static struct ether_addr destination_mac
= {{0, 0, 0 , 0, 0, 0}};
165 static struct ether_addr temp_mac
= {{0, 0, 0 , 0, 0, 0}};
166 static struct ether_addr current_resolved_MAC
= {{0, 0, 0 , 0, 0, 0}};
168 static boolean_t flag_panic_dump_in_progress
= FALSE
;
169 static boolean_t flag_router_mac_initialized
= FALSE
;
170 static boolean_t flag_dont_abort_panic_dump
= FALSE
;
172 static boolean_t flag_arp_resolved
= FALSE
;
174 static unsigned int panic_timeout
= 100000;
175 static unsigned int last_panic_port
= CORE_REMOTE_PORT
;
177 #define KDP_THROTTLE_VALUE (10ULL * NSEC_PER_SEC)
179 uint32_t kdp_crashdump_pkt_size
= 512;
180 #define KDP_LARGE_CRASHDUMP_PKT_SIZE (1440 - 6 - sizeof(struct udpiphdr))
181 static char panicd_ip_str
[20];
182 static char router_ip_str
[20];
183 static char corename_str
[50];
185 static unsigned int panic_block
= 0;
186 volatile unsigned int kdp_trigger_core_dump
= 0;
187 __private_extern__
volatile unsigned int flag_kdp_trigger_reboot
= 0;
189 extern unsigned int not_in_kdp
;
191 extern unsigned int disableConsoleOutput
;
193 extern void kdp_call(void);
194 extern boolean_t
kdp_call_kdb(void);
195 extern int kern_dump(void);
197 extern int inet_aton(const char *cp
, struct in_addr
*pin
);
198 extern int inet_ntoa2(struct in_addr
* pin
, char * cp
, const int len
);
200 void * kdp_get_interface(void);
201 void kdp_set_gateway_mac(void *gatewaymac
);
202 void kdp_set_ip_and_mac_addresses(struct in_addr
*ipaddr
, struct ether_addr
*);
203 void kdp_set_interface(void *interface
, const struct ether_addr
*macaddr
);
205 void kdp_disable_arp(void);
206 static void kdp_arp_reply(struct ether_arp
*);
207 static void kdp_process_arp_reply(struct ether_arp
*);
208 static boolean_t
kdp_arp_resolve(uint32_t, struct ether_addr
*);
210 static volatile unsigned kdp_reentry_deadline
;
212 static uint32_t kdp_crashdump_feature_mask
= KDP_FEATURE_LARGE_CRASHDUMPS
| KDP_FEATURE_LARGE_PKT_SIZE
;
213 uint32_t kdp_feature_large_crashdumps
, kdp_feature_large_pkt_size
;
215 char kdp_kernelversion_string
[256];
217 static boolean_t gKDPDebug
= FALSE
;
218 #define KDP_DEBUG(...) if (gKDPDebug) printf(__VA_ARGS__);
220 int kdp_snapshot
= 0;
221 static int stack_snapshot_ret
= 0;
222 static unsigned stack_snapshot_bytes_traced
= 0;
224 static void *stack_snapshot_buf
;
225 static uint32_t stack_snapshot_bufsize
;
226 static int stack_snapshot_pid
;
227 static uint32_t stack_snapshot_flags
;
228 static uint32_t stack_snapshot_dispatch_offset
;
230 static unsigned int old_debugger
;
232 #define SBLOCKSZ (2048)
233 uint64_t kdp_dump_start_time
= 0;
234 uint64_t kdp_min_superblock_dump_time
= ~1ULL;
235 uint64_t kdp_max_superblock_dump_time
= 0;
236 uint64_t kdp_superblock_dump_time
= 0;
237 uint64_t kdp_superblock_dump_start_time
= 0;
240 kdp_snapshot_preflight(int pid
, void * tracebuf
, uint32_t tracebuf_size
,
241 uint32_t flags
, uint32_t dispatch_offset
);
244 kdp_snapshot_postflight(void);
247 kdp_stackshot(int pid
, void *tracebuf
, uint32_t tracebuf_size
,
248 uint32_t flags
, uint32_t dispatch_offset
, uint32_t *pbytesTraced
);
251 kdp_stack_snapshot_geterror(void);
254 kdp_stack_snapshot_bytes_traced(void);
260 kdp_ml_enter_debugger_wrapper(__unused
void *param0
, __unused
void *param1
) {
261 kdp_ml_enter_debugger();
265 kdp_timer_callout_init(void) {
266 kdp_timer_call
= thread_call_allocate(kdp_ml_enter_debugger_wrapper
, NULL
);
270 /* only send/receive data if the link is up */
271 inline static void wait_for_link(void)
273 static int first
= 0;
275 if (!kdp_en_linkstatus
)
278 while (((*kdp_en_linkstatus
)() & LINK_UP_STATUS
) != LINK_UP_STATUS
) {
283 printf("Waiting for link to become available.\n");
284 kprintf("Waiting for link to become available.\n");
289 inline static void kdp_send_data(void *packet
, unsigned int len
)
292 (*kdp_en_send_pkt
)(packet
, len
);
296 inline static void kdp_receive_data(void *packet
, unsigned int *len
,
297 unsigned int timeout
)
300 (*kdp_en_recv_pkt
)(packet
, len
, timeout
);
304 void kdp_register_link(kdp_link_t link
, kdp_mode_t mode
)
306 kdp_en_linkstatus
= link
;
307 kdp_en_setmode
= mode
;
310 void kdp_unregister_link(__unused kdp_link_t link
, __unused kdp_mode_t mode
)
312 kdp_en_linkstatus
= NULL
;
313 kdp_en_setmode
= NULL
;
317 kdp_register_send_receive(
319 kdp_receive_t receive
)
321 unsigned int debug
= 0;
323 PE_parse_boot_argn("debug", &debug
, sizeof (debug
));
329 kdp_en_send_pkt
= send
;
330 kdp_en_recv_pkt
= receive
;
332 if (debug
& DB_KDP_BP_DIS
)
333 kdp_flag
|= KDP_BP_DIS
;
334 if (debug
& DB_KDP_GETC_ENA
)
335 kdp_flag
|= KDP_GETC_ENA
;
339 if (debug
& DB_KERN_DUMP_ON_PANIC
)
340 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
341 if (debug
& DB_KERN_DUMP_ON_NMI
)
342 kdp_flag
|= PANIC_CORE_ON_NMI
;
344 if (debug
& DB_DBG_POST_CORE
)
345 kdp_flag
|= DBG_POST_CORE
;
347 if (debug
& DB_PANICLOG_DUMP
)
348 kdp_flag
|= PANIC_LOG_DUMP
;
350 if (PE_parse_boot_argn("_panicd_ip", panicd_ip_str
, sizeof (panicd_ip_str
)))
351 panicd_specified
= TRUE
;
353 if ((debug
& DB_REBOOT_POST_CORE
) && (panicd_specified
== TRUE
))
354 kdp_flag
|= REBOOT_POST_CORE
;
356 if (PE_parse_boot_argn("_router_ip", router_ip_str
, sizeof (router_ip_str
)))
357 router_specified
= TRUE
;
359 if (!PE_parse_boot_argn("panicd_port", &panicd_port
, sizeof (panicd_port
)))
360 panicd_port
= CORE_REMOTE_PORT
;
362 if (PE_parse_boot_argn("_panicd_corename", &corename_str
, sizeof (corename_str
)))
363 corename_specified
= TRUE
;
365 kdp_flag
|= KDP_READY
;
366 if (current_debugger
== NO_CUR_DB
)
367 current_debugger
= KDP_CUR_DB
;
368 if ((kdp_current_ip_address
!= 0) && halt_in_debugger
) {
375 kdp_unregister_send_receive(
376 __unused kdp_send_t send
,
377 __unused kdp_receive_t receive
)
379 if (current_debugger
== KDP_CUR_DB
)
380 current_debugger
= NO_CUR_DB
;
381 kdp_flag
&= ~KDP_READY
;
382 kdp_en_send_pkt
= NULL
;
383 kdp_en_recv_pkt
= NULL
;
386 /* Cache stack snapshot parameters in preparation for a trace */
388 kdp_snapshot_preflight(int pid
, void * tracebuf
, uint32_t tracebuf_size
, uint32_t flags
, uint32_t dispatch_offset
)
390 stack_snapshot_pid
= pid
;
391 stack_snapshot_buf
= tracebuf
;
392 stack_snapshot_bufsize
= tracebuf_size
;
393 stack_snapshot_flags
= flags
;
394 stack_snapshot_dispatch_offset
= dispatch_offset
;
396 /* Mark this debugger as active, since the polled mode driver that
397 * ordinarily does this may not be enabled (yet), or since KDB may be
398 * the primary debugger.
400 old_debugger
= current_debugger
;
401 if (old_debugger
!= KDP_CUR_DB
) {
402 current_debugger
= KDP_CUR_DB
;
407 kdp_snapshot_postflight(void)
410 if ((kdp_en_send_pkt
== NULL
) || (old_debugger
== KDB_CUR_DB
))
411 current_debugger
= old_debugger
;
415 kdp_stack_snapshot_geterror(void)
417 return stack_snapshot_ret
;
421 kdp_stack_snapshot_bytes_traced(void)
423 return stack_snapshot_bytes_traced
;
427 kdp_schedule_debugger_reentry(unsigned interval
) {
430 clock_interval_to_deadline(interval
, 1000 * 1000, &deadline
);
431 thread_call_enter_delayed(kdp_timer_call
, deadline
);
440 bcopy((char *)src
, (char *)dst
, sizeof (struct ether_addr
));
443 static unsigned short
449 unsigned int high
, low
, sum
;
459 sum
= (high
<< 8) + low
;
460 sum
= (sum
>> 16) + (sum
& 65535);
462 return (sum
> 65535 ? sum
- 65535 : sum
);
467 unsigned short reply_port
,
468 const boolean_t sideband
471 struct udpiphdr aligned_ui
, *ui
= &aligned_ui
;
472 struct ip aligned_ip
, *ip
= &aligned_ip
;
473 struct in_addr tmp_ipaddr
;
474 struct ether_addr tmp_enaddr
;
475 struct ether_header
*eh
= NULL
;
478 kdp_panic("kdp_reply");
480 pkt
.off
-= (unsigned int)sizeof (struct udpiphdr
);
483 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
485 ui
= (struct udpiphdr
*)&pkt
.data
[pkt
.off
];
487 ui
->ui_next
= ui
->ui_prev
= 0;
489 ui
->ui_pr
= IPPROTO_UDP
;
490 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct udphdr
));
491 tmp_ipaddr
= ui
->ui_src
;
492 ui
->ui_src
= ui
->ui_dst
;
493 ui
->ui_dst
= tmp_ipaddr
;
494 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
495 ui
->ui_dport
= reply_port
;
496 ui
->ui_ulen
= ui
->ui_len
;
499 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
500 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
502 ip
= (struct ip
*)&pkt
.data
[pkt
.off
];
504 ip
->ip_len
= htons(sizeof (struct udpiphdr
) + pkt
.len
);
505 ip
->ip_v
= IPVERSION
;
506 ip
->ip_id
= htons(ip_id
++);
507 ip
->ip_hl
= sizeof (struct ip
) >> 2;
508 ip
->ip_ttl
= udp_ttl
;
510 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
512 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
515 pkt
.len
+= (unsigned int)sizeof (struct udpiphdr
);
517 pkt
.off
-= (unsigned int)sizeof (struct ether_header
);
519 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
520 enaddr_copy(eh
->ether_shost
, &tmp_enaddr
);
521 enaddr_copy(eh
->ether_dhost
, eh
->ether_shost
);
522 enaddr_copy(&tmp_enaddr
, eh
->ether_dhost
);
523 eh
->ether_type
= htons(ETHERTYPE_IP
);
525 pkt
.len
+= (unsigned int)sizeof (struct ether_header
);
527 // save reply for possible retransmission
528 assert(pkt
.len
<= KDP_MAXPACKET
);
530 bcopy((char *)&pkt
, (char *)&saved_reply
, sizeof(saved_reply
));
532 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
534 // increment expected sequence number
541 unsigned short remote_port
544 struct udpiphdr aligned_ui
, *ui
= &aligned_ui
;
545 struct ip aligned_ip
, *ip
= &aligned_ip
;
546 struct ether_header
*eh
;
549 kdp_panic("kdp_send");
551 pkt
.off
-= (unsigned int)sizeof (struct udpiphdr
);
554 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
556 ui
= (struct udpiphdr
*)&pkt
.data
[pkt
.off
];
558 ui
->ui_next
= ui
->ui_prev
= 0;
560 ui
->ui_pr
= IPPROTO_UDP
;
561 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct udphdr
));
562 ui
->ui_src
= adr
.loc
.in
;
563 ui
->ui_dst
= adr
.rmt
.in
;
564 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
565 ui
->ui_dport
= remote_port
;
566 ui
->ui_ulen
= ui
->ui_len
;
569 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
570 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
572 ip
= (struct ip
*)&pkt
.data
[pkt
.off
];
574 ip
->ip_len
= htons(sizeof (struct udpiphdr
) + pkt
.len
);
575 ip
->ip_v
= IPVERSION
;
576 ip
->ip_id
= htons(ip_id
++);
577 ip
->ip_hl
= sizeof (struct ip
) >> 2;
578 ip
->ip_ttl
= udp_ttl
;
580 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
582 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
585 pkt
.len
+= (unsigned int)sizeof (struct udpiphdr
);
587 pkt
.off
-= (unsigned int)sizeof (struct ether_header
);
589 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
590 enaddr_copy(&adr
.loc
.ea
, eh
->ether_shost
);
591 enaddr_copy(&adr
.rmt
.ea
, eh
->ether_dhost
);
592 eh
->ether_type
= htons(ETHERTYPE_IP
);
594 pkt
.len
+= (unsigned int)sizeof (struct ether_header
);
595 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
599 inline static void debugger_if_necessary(void)
601 if ((current_debugger
== KDP_CUR_DB
) && halt_in_debugger
) {
608 /* We don't interpret this pointer, we just give it to the bsd stack
609 so it can decide when to set the MAC and IP info. We'll
610 early initialize the MAC/IP info if we can so that we can use
611 KDP early in boot. These values may subsequently get over-written
612 when the interface gets initialized for real.
615 kdp_set_interface(void *ifp
, const struct ether_addr
*macaddr
)
618 struct in_addr addr
= { 0 };
621 kdp_current_ifp
= ifp
;
623 if (PE_parse_boot_argn("kdp_ip_addr", kdpstr
, sizeof(kdpstr
))) {
624 /* look for a static ip address */
625 if (inet_aton(kdpstr
, &addr
) == FALSE
)
631 /* use saved ip address */
632 save_ip_in_nvram
= TRUE
;
634 len
= sizeof(kdpstr
);
635 if (PEReadNVRAMProperty("_kdp_ipstr", kdpstr
, &len
) == FALSE
)
638 kdpstr
[len
< sizeof(kdpstr
) ? len
: sizeof(kdpstr
) - 1] = '\0';
639 if (inet_aton(kdpstr
, &addr
) == FALSE
)
643 kdp_current_ip_address
= addr
.s_addr
;
645 kdp_current_mac_address
= *macaddr
;
647 /* we can't drop into the debugger at this point because the
648 link will likely not be up. when getDebuggerLinkStatus() support gets
649 added to the appropriate network drivers, adding the
650 following will enable this capability:
651 debugger_if_necessary();
658 kdp_get_interface(void)
660 return kdp_current_ifp
;
664 kdp_set_ip_and_mac_addresses(
665 struct in_addr
*ipaddr
,
666 struct ether_addr
*macaddr
)
668 static uint64_t last_time
= (uint64_t) -1;
669 static uint64_t throttle_val
= 0;
673 if (kdp_current_ip_address
== ipaddr
->s_addr
)
676 /* don't replace if serial debugging is configured */
677 if (!KDP_SERIAL_ENABLED() ||
678 (kdp_current_ip_address
!= KDP_SERIAL_IPADDR
)) {
679 kdp_current_mac_address
= *macaddr
;
680 kdp_current_ip_address
= ipaddr
->s_addr
;
683 if (save_ip_in_nvram
== FALSE
)
686 if (inet_ntoa2(ipaddr
, addr
, sizeof(addr
)) == FALSE
)
689 /* throttle writes if needed */
691 nanoseconds_to_absolutetime(KDP_THROTTLE_VALUE
, &throttle_val
);
693 cur_time
= mach_absolute_time();
694 if (last_time
== (uint64_t) -1 ||
695 ((cur_time
- last_time
) > throttle_val
)) {
696 PEWriteNVRAMProperty("_kdp_ipstr", addr
,
697 (const unsigned int) strlen(addr
));
699 last_time
= cur_time
;
702 debugger_if_necessary();
706 kdp_set_gateway_mac(void *gatewaymac
)
708 router_mac
= *(struct ether_addr
*)gatewaymac
;
709 flag_router_mac_initialized
= TRUE
;
713 kdp_get_mac_addr(void)
715 return kdp_current_mac_address
;
719 kdp_get_ip_address(void)
721 return (unsigned int)kdp_current_ip_address
;
725 kdp_disable_arp(void)
727 kdp_flag
&= ~(DB_ARP
);
731 kdp_arp_dispatch(void)
733 struct ether_arp aligned_ea
, *ea
= &aligned_ea
;
734 unsigned arp_header_offset
;
736 arp_header_offset
= (unsigned)sizeof(struct ether_header
) + pkt
.off
;
737 memcpy((void *)ea
, (void *)&pkt
.data
[arp_header_offset
], sizeof(*ea
));
739 switch(ntohs(ea
->arp_op
)) {
744 kdp_process_arp_reply(ea
);
752 kdp_process_arp_reply(struct ether_arp
*ea
)
754 /* Are we interested in ARP replies? */
755 if (flag_arp_resolved
== TRUE
)
758 /* Did we receive a reply from the right source? */
759 if (((struct in_addr
*)(ea
->arp_spa
))->s_addr
!= target_ip
)
762 flag_arp_resolved
= TRUE
;
763 current_resolved_MAC
= *(struct ether_addr
*) (ea
->arp_sha
);
768 /* ARP responses are enabled when the DB_ARP bit of the debug boot arg
773 kdp_arp_reply(struct ether_arp
*ea
)
775 struct ether_header
*eh
;
777 struct in_addr isaddr
, itaddr
, myaddr
;
778 struct ether_addr my_enaddr
;
780 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
781 pkt
.off
+= (unsigned int)sizeof(struct ether_header
);
783 if(ntohs(ea
->arp_op
) != ARPOP_REQUEST
)
786 myaddr
.s_addr
= kdp_get_ip_address();
787 my_enaddr
= kdp_get_mac_addr();
789 if ((ntohl(myaddr
.s_addr
) == 0) ||
790 ((my_enaddr
.ether_addr_octet
[0] & 0xff) == 0
791 && (my_enaddr
.ether_addr_octet
[1] & 0xff) == 0
792 && (my_enaddr
.ether_addr_octet
[2] & 0xff) == 0
793 && (my_enaddr
.ether_addr_octet
[3] & 0xff) == 0
794 && (my_enaddr
.ether_addr_octet
[4] & 0xff) == 0
795 && (my_enaddr
.ether_addr_octet
[5] & 0xff) == 0
799 (void)memcpy((void *)&isaddr
, (void *)ea
->arp_spa
, sizeof (isaddr
));
800 (void)memcpy((void *)&itaddr
, (void *)ea
->arp_tpa
, sizeof (itaddr
));
802 if (itaddr
.s_addr
== myaddr
.s_addr
) {
803 (void)memcpy((void *)ea
->arp_tha
, (void *)ea
->arp_sha
, sizeof(ea
->arp_sha
));
804 (void)memcpy((void *)ea
->arp_sha
, (void *)&my_enaddr
, sizeof(ea
->arp_sha
));
806 (void)memcpy((void *)ea
->arp_tpa
, (void *) ea
->arp_spa
, sizeof(ea
->arp_spa
));
807 (void)memcpy((void *)ea
->arp_spa
, (void *) &itaddr
, sizeof(ea
->arp_spa
));
809 ea
->arp_op
= htons(ARPOP_REPLY
);
810 ea
->arp_pro
= htons(ETHERTYPE_IP
);
811 (void)memcpy(eh
->ether_dhost
, ea
->arp_tha
, sizeof(eh
->ether_dhost
));
812 (void)memcpy(eh
->ether_shost
, &my_enaddr
, sizeof(eh
->ether_shost
));
813 eh
->ether_type
= htons(ETHERTYPE_ARP
);
814 (void)memcpy(&pkt
.data
[pkt
.off
], ea
, sizeof(*ea
));
815 pkt
.off
-= (unsigned int)sizeof (struct ether_header
);
816 /* pkt.len is still the length we want, ether_header+ether_arp */
817 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
824 struct ether_header
*eh
= NULL
;
825 struct udpiphdr aligned_ui
, *ui
= &aligned_ui
;
826 struct ip aligned_ip
, *ip
= &aligned_ip
;
827 static int msg_printed
;
830 kdp_panic("kdp_poll");
832 if (!kdp_en_recv_pkt
|| !kdp_en_send_pkt
) {
833 if( msg_printed
== 0) {
835 printf("kdp_poll: no debugger device\n");
840 pkt
.off
= pkt
.len
= 0;
841 kdp_receive_data(pkt
.data
, &pkt
.len
, 3/* ms */);
846 if (pkt
.len
>= sizeof(struct ether_header
))
848 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
850 if (kdp_flag
& KDP_ARP
)
852 if (ntohs(eh
->ether_type
) == ETHERTYPE_ARP
)
860 if (pkt
.len
< (sizeof (struct ether_header
) + sizeof (struct udpiphdr
)))
863 pkt
.off
+= (unsigned int)sizeof (struct ether_header
);
864 if (ntohs(eh
->ether_type
) != ETHERTYPE_IP
) {
869 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
870 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
872 ui
= (struct udpiphdr
*)&pkt
.data
[pkt
.off
];
873 ip
= (struct ip
*)&pkt
.data
[pkt
.off
];
876 pkt
.off
+= (unsigned int)sizeof (struct udpiphdr
);
877 if (ui
->ui_pr
!= IPPROTO_UDP
) {
881 if (ip
->ip_hl
> (sizeof (struct ip
) >> 2)) {
885 if (ntohs(ui
->ui_dport
) != KDP_REMOTE_PORT
) {
886 if (panicd_port
== (ntohs(ui
->ui_dport
)) &&
887 flag_panic_dump_in_progress
) {
888 last_panic_port
= ui
->ui_sport
;
893 /* If we receive a kernel debugging packet whilst a
894 * core dump is in progress, abort the transfer and
895 * enter the debugger if not told otherwise.
898 if (flag_panic_dump_in_progress
)
900 if (!flag_dont_abort_panic_dump
) {
901 abort_panic_transfer();
906 if (!kdp
.is_conn
&& !flag_panic_dump_in_progress
) {
907 enaddr_copy(eh
->ether_dhost
, &adr
.loc
.ea
);
908 adr
.loc
.in
= ui
->ui_dst
;
910 enaddr_copy(eh
->ether_shost
, &adr
.rmt
.ea
);
911 adr
.rmt
.in
= ui
->ui_src
;
915 * Calculate kdp packet length.
917 pkt
.len
= ntohs((u_short
)ui
->ui_ulen
) - (unsigned int)sizeof (struct udphdr
);
921 /* Create and transmit an ARP resolution request for the target IP address.
922 * This is modeled on ether_inet_arp()/RFC 826.
926 transmit_ARP_request(uint32_t ip_addr
)
928 struct ether_header
*eh
= (struct ether_header
*) &pkt
.data
[0];
929 struct ether_arp
*ea
= (struct ether_arp
*) &pkt
.data
[sizeof(struct ether_header
)];
931 KDP_DEBUG("Transmitting ARP request\n");
932 /* Populate the ether_header */
933 eh
->ether_type
= htons(ETHERTYPE_ARP
);
934 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
935 enaddr_copy(ðerbroadcastaddr
, eh
->ether_dhost
);
937 /* Populate the ARP header */
938 ea
->arp_pro
= htons(ETHERTYPE_IP
);
939 ea
->arp_hln
= sizeof(ea
->arp_sha
);
940 ea
->arp_pln
= sizeof(ea
->arp_spa
);
941 ea
->arp_hrd
= htons(ARPHRD_ETHER
);
942 ea
->arp_op
= htons(ARPOP_REQUEST
);
945 enaddr_copy(ðerbroadcastaddr
, ea
->arp_tha
);
946 memcpy(ea
->arp_tpa
, (void *) &ip_addr
, sizeof(ip_addr
));
949 enaddr_copy(&kdp_current_mac_address
, ea
->arp_sha
);
950 memcpy(ea
->arp_spa
, (void *) &kdp_current_ip_address
, sizeof(kdp_current_ip_address
));
953 pkt
.len
= sizeof(struct ether_header
) + sizeof(struct ether_arp
);
955 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
959 kdp_arp_resolve(uint32_t arp_target_ip
, struct ether_addr
*resolved_MAC
)
961 int poll_count
= 256; /* ~770 ms modulo broadcast/delayed traffic? */
964 #define NUM_ARP_TX_RETRIES 5
966 target_ip
= arp_target_ip
;
967 flag_arp_resolved
= FALSE
;
970 pkt
.off
= pkt
.len
= 0;
974 if (tretries
>= NUM_ARP_TX_RETRIES
) {
978 KDP_DEBUG("ARP TX attempt #%d \n", tretries
);
980 transmit_ARP_request(arp_target_ip
);
982 while (!pkt
.input
&& !flag_arp_resolved
&& flag_panic_dump_in_progress
&& --poll_count
) {
986 if (flag_arp_resolved
) {
987 *resolved_MAC
= current_resolved_MAC
;
991 if (!flag_panic_dump_in_progress
|| pkt
.input
) /* we received a debugging packet, bail*/
993 printf("Received a debugger packet,transferring control to debugger\n");
994 /* Indicate that we should wait in the debugger when we return */
995 kdp_flag
|= DBG_POST_CORE
;
999 else /* We timed out */
1000 if (0 == poll_count
) {
1002 goto TRANSMIT_RETRY
;
1012 unsigned short reply_port
;
1013 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
1015 kdp
.saved_state
= saved_state
; // see comment in kdp_raise_exception
1022 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
1024 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
1027 // ignore replies -- we're not expecting them anyway.
1028 if (hdr
->is_reply
) {
1032 if (hdr
->request
== KDP_REATTACH
)
1033 exception_seq
= hdr
->seq
;
1035 // check for retransmitted request
1036 if (hdr
->seq
== (exception_seq
- 1)) {
1037 /* retransmit last reply */
1038 kdp_send_data(&saved_reply
.data
[saved_reply
.off
],
1041 } else if ((hdr
->seq
!= exception_seq
) &&
1042 (hdr
->request
!= KDP_CONNECT
)) {
1043 printf("kdp: bad sequence %d (want %d)\n",
1044 hdr
->seq
, exception_seq
);
1048 /* This is a manual side-channel to the main KDP protocol.
1049 * A client like GDB/kgmacros can manually construct
1050 * a request, set the input flag, issue a dummy KDP request,
1051 * and then manually collect the result
1053 if (manual_pkt
.input
) {
1054 kdp_hdr_t
*manual_hdr
= (kdp_hdr_t
*)&manual_pkt
.data
;
1055 unsigned short manual_port_unused
= 0;
1056 if (!manual_hdr
->is_reply
) {
1058 kdp_packet((unsigned char *)&manual_pkt
.data
,
1059 (int *)&manual_pkt
.len
,
1060 &manual_port_unused
);
1062 manual_pkt
.input
= 0;
1065 if (kdp_packet((unsigned char*)&pkt
.data
[pkt
.off
],
1067 (unsigned short *)&reply_port
)) {
1068 boolean_t sideband
= FALSE
;
1070 /* if it's an already connected error message,
1071 * send a sideband reply for that. for successful connects,
1072 * make sure the sequence number is correct. */
1073 if (hdr
->request
== KDP_CONNECT
) {
1074 kdp_connect_reply_t
*rp
=
1075 (kdp_connect_reply_t
*) &pkt
.data
[pkt
.off
];
1076 kdp_error_t err
= rp
->error
;
1078 if (err
== KDPERR_NO_ERROR
) {
1079 exception_seq
= hdr
->seq
;
1080 } else if (err
== KDPERR_ALREADY_CONNECTED
) {
1085 kdp_reply(reply_port
, sideband
);
1090 } while (kdp
.is_halted
);
1094 kdp_connection_wait(void)
1096 unsigned short reply_port
;
1097 struct ether_addr kdp_mac_addr
= kdp_get_mac_addr();
1098 unsigned int ip_addr
= ntohl(kdp_get_ip_address());
1101 * Do both a printf() and a kprintf() of the MAC and IP so that
1102 * they will print out on headless machines but not be added to
1106 if (KDP_SERIAL_ENABLED()) {
1107 printf("Using serial KDP.\n");
1108 kprintf("Using serial KDP.\n");
1110 printf( "ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1111 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
1112 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
1113 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
1114 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
1115 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
1116 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
1118 kprintf( "ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1119 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
1120 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
1121 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
1122 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
1123 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
1124 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
1126 printf( "ip address: %d.%d.%d.%d\n",
1127 (ip_addr
& 0xff000000) >> 24,
1128 (ip_addr
& 0xff0000) >> 16,
1129 (ip_addr
& 0xff00) >> 8,
1132 kprintf( "ip address: %d.%d.%d.%d\n",
1133 (ip_addr
& 0xff000000) >> 24,
1134 (ip_addr
& 0xff0000) >> 16,
1135 (ip_addr
& 0xff00) >> 8,
1139 printf("\nWaiting for remote debugger connection.\n");
1140 kprintf("\nWaiting for remote debugger connection.\n");
1143 if (reattach_wait
== 0) {
1144 if((kdp_flag
& KDP_GETC_ENA
) && (0 != kdp_getc()))
1146 printf("Options..... Type\n");
1147 printf("------------ ----\n");
1148 printf("continue.... 'c'\n");
1149 printf("reboot...... 'r'\n");
1157 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
1159 while (!pkt
.input
) {
1160 if (kdp_flag
& KDP_GETC_ENA
) {
1161 switch(kdp_getc()) {
1163 printf("Continuing...\n");
1166 printf("Rebooting...\n");
1167 kdp_machine_reboot();
1177 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
1179 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
1181 if (hdr
->request
== KDP_HOSTREBOOT
) {
1182 kdp_machine_reboot();
1183 /* should not return! */
1185 if (((hdr
->request
== KDP_CONNECT
) || (hdr
->request
== KDP_REATTACH
)) &&
1186 !hdr
->is_reply
&& (hdr
->seq
== exception_seq
)) {
1187 if (kdp_packet((unsigned char *)&pkt
.data
[pkt
.off
],
1189 (unsigned short *)&reply_port
))
1190 kdp_reply(reply_port
, FALSE
);
1191 if (hdr
->request
== KDP_REATTACH
) {
1193 hdr
->request
=KDP_DISCONNECT
;
1199 } while (!kdp
.is_conn
);
1201 if (current_debugger
== KDP_CUR_DB
)
1203 printf("Connected to remote debugger.\n");
1204 kprintf("Connected to remote debugger.\n");
1209 unsigned int exception
,
1211 unsigned int subcode
1214 unsigned short remote_port
;
1215 unsigned int timeout_count
= 100;
1216 unsigned int poll_timeout
;
1219 pkt
.off
= sizeof (struct ether_header
) + sizeof (struct udpiphdr
);
1220 kdp_exception((unsigned char *)&pkt
.data
[pkt
.off
],
1222 (unsigned short *)&remote_port
,
1223 (unsigned int)exception
,
1225 (unsigned int)subcode
);
1227 kdp_send(remote_port
);
1230 while(!pkt
.input
&& poll_timeout
)
1237 if (!kdp_exception_ack(&pkt
.data
[pkt
.off
], pkt
.len
)) {
1244 if (kdp
.exception_ack_needed
)
1245 kdp_us_spin(250000);
1247 } while (kdp
.exception_ack_needed
&& timeout_count
--);
1249 if (kdp
.exception_ack_needed
) {
1250 // give up & disconnect
1251 printf("kdp: exception ack timeout\n");
1252 if (current_debugger
== KDP_CUR_DB
)
1259 kdp_raise_exception(
1260 unsigned int exception
,
1262 unsigned int subcode
,
1267 unsigned int initial_not_in_kdp
= not_in_kdp
;
1270 /* Was a system trace requested ? */
1271 if (kdp_snapshot
&& (!panic_active()) && (panic_caller
== 0)) {
1272 stack_snapshot_ret
= kdp_stackshot(stack_snapshot_pid
,
1273 stack_snapshot_buf
, stack_snapshot_bufsize
,
1274 stack_snapshot_flags
, stack_snapshot_dispatch_offset
,
1275 &stack_snapshot_bytes_traced
);
1276 not_in_kdp
= initial_not_in_kdp
;
1280 disable_preemption();
1282 if (saved_state
== 0)
1283 printf("kdp_raise_exception with NULL state\n");
1286 if (exception
!= EXC_BREAKPOINT
) {
1287 if (exception
> EXC_BREAKPOINT
|| exception
< EXC_BAD_ACCESS
) {
1290 printf("%s exception (%x,%x,%x)\n",
1291 exception_message
[index
],
1292 exception
, code
, subcode
);
1297 /* XXX WMG it seems that sometimes it doesn't work to let kdp_handler
1298 * do this. I think the client and the host can get out of sync.
1300 kdp
.saved_state
= saved_state
;
1301 kdp
.kdp_cpu
= cpu_number();
1302 kdp
.kdp_thread
= current_thread();
1305 (*kdp_en_setmode
)(TRUE
); /* enabling link mode */
1308 kdp_panic("kdp_raise_exception");
1310 if (((kdp_flag
& KDP_PANIC_DUMP_ENABLED
) || (kdp_flag
& PANIC_LOG_DUMP
))
1311 && (panicstr
!= (char *) 0)) {
1313 if (kdp_flag
& REBOOT_POST_CORE
)
1314 kdp_machine_reboot();
1317 if ((kdp_flag
& PANIC_CORE_ON_NMI
) && (panicstr
== (char *) 0) &&
1320 disable_debug_output
= disableConsoleOutput
= FALSE
;
1323 if (!(kdp_flag
& DBG_POST_CORE
))
1324 goto exit_raise_exception
;
1329 kdp_connection_wait();
1331 kdp_send_exception(exception
, code
, subcode
);
1332 if (kdp
.exception_ack_needed
) {
1333 kdp
.exception_ack_needed
= FALSE
;
1334 kdp_remove_all_breakpoints();
1335 printf("Remote debugger disconnected.\n");
1340 kdp
.is_halted
= TRUE
; /* XXX */
1341 kdp_handler(saved_state
);
1344 kdp_remove_all_breakpoints();
1345 printf("Remote debugger disconnected.\n");
1348 /* Allow triggering a panic core dump when connected to the machine
1349 * Continuing after setting kdp_trigger_core_dump should do the
1353 if (1 == kdp_trigger_core_dump
) {
1354 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
1356 if (kdp_flag
& REBOOT_POST_CORE
)
1357 kdp_machine_reboot();
1358 kdp_trigger_core_dump
= 0;
1361 /* Trigger a reboot if the user has set this flag through the
1362 * debugger.Ideally, this would be done through the HOSTREBOOT packet
1363 * in the protocol,but that will need gdb support,and when it's
1364 * available, it should work automatically.
1366 if (1 == flag_kdp_trigger_reboot
) {
1367 kdp_machine_reboot();
1368 /* If we're still around, reset the flag */
1369 flag_kdp_trigger_reboot
= 0;
1372 if (kdp_reentry_deadline
) {
1373 kdp_schedule_debugger_reentry(kdp_reentry_deadline
);
1374 printf("Debugger re-entry scheduled in %d milliseconds\n", kdp_reentry_deadline
);
1375 kdp_reentry_deadline
= 0;
1380 if (reattach_wait
== 1)
1383 exit_raise_exception
:
1385 (*kdp_en_setmode
)(FALSE
); /* link cleanup */
1387 not_in_kdp
= initial_not_in_kdp
;
1389 enable_preemption();
1395 kdp
.reply_port
= kdp
.exception_port
= 0;
1396 kdp
.is_halted
= kdp
.is_conn
= FALSE
;
1397 kdp
.exception_seq
= kdp
.conn_seq
= 0;
1398 kdp
.session_key
= 0;
1399 pkt
.input
= manual_pkt
.input
= FALSE
;
1400 pkt
.len
= pkt
.off
= manual_pkt
.len
= 0;
1404 create_panic_header(unsigned int request
, const char *corename
,
1405 unsigned length
, unsigned int block
)
1407 struct udpiphdr aligned_ui
, *ui
= &aligned_ui
;
1408 struct ip aligned_ip
, *ip
= &aligned_ip
;
1409 struct ether_header
*eh
;
1410 struct corehdr
*coreh
;
1411 const char *mode
= "octet";
1412 char modelen
= strlen(mode
) + 1;
1414 size_t fmask_size
= sizeof(KDP_FEATURE_MASK_STRING
) + sizeof(kdp_crashdump_feature_mask
);
1416 pkt
.off
= sizeof (struct ether_header
);
1417 pkt
.len
= (unsigned int)(length
+ ((request
== KDP_WRQ
) ? modelen
+ fmask_size
: 0) +
1418 (corename
? (strlen(corename
) + 1 ): 0) + sizeof(struct corehdr
));
1421 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
1423 ui
= (struct udpiphdr
*)&pkt
.data
[pkt
.off
];
1425 ui
->ui_next
= ui
->ui_prev
= 0;
1427 ui
->ui_pr
= IPPROTO_UDP
;
1428 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct udphdr
));
1429 ui
->ui_src
.s_addr
= (uint32_t)kdp_current_ip_address
;
1430 /* Already in network byte order via inet_aton() */
1431 ui
->ui_dst
.s_addr
= panic_server_ip
;
1432 ui
->ui_sport
= htons(panicd_port
);
1433 ui
->ui_dport
= ((request
== KDP_WRQ
) ? htons(panicd_port
) : last_panic_port
);
1434 ui
->ui_ulen
= ui
->ui_len
;
1437 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
1438 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
1440 ip
= (struct ip
*)&pkt
.data
[pkt
.off
];
1442 ip
->ip_len
= htons(sizeof (struct udpiphdr
) + pkt
.len
);
1443 ip
->ip_v
= IPVERSION
;
1444 ip
->ip_id
= htons(ip_id
++);
1445 ip
->ip_hl
= sizeof (struct ip
) >> 2;
1446 ip
->ip_ttl
= udp_ttl
;
1448 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
1450 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
1453 pkt
.len
+= (unsigned int)sizeof (struct udpiphdr
);
1455 pkt
.off
+= (unsigned int)sizeof (struct udpiphdr
);
1457 coreh
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1458 coreh
->th_opcode
= htons((u_short
)request
);
1460 if (request
== KDP_WRQ
)
1464 cp
= coreh
->th_u
.tu_rpl
;
1465 cp
+= strlcpy (cp
, corename
, KDP_MAXPACKET
);
1467 cp
+= strlcpy (cp
, mode
, KDP_MAXPACKET
- strlen(corename
));
1469 cp
+= strlcpy(cp
, KDP_FEATURE_MASK_STRING
, sizeof(KDP_FEATURE_MASK_STRING
));
1470 *cp
++ = '\0'; /* Redundant */
1471 bcopy(&kdp_crashdump_feature_mask
, cp
, sizeof(kdp_crashdump_feature_mask
));
1472 kdp_crashdump_pkt_size
= KDP_LARGE_CRASHDUMP_PKT_SIZE
;
1473 PE_parse_boot_argn("kdp_crashdump_pkt_size", &kdp_crashdump_pkt_size
, sizeof(kdp_crashdump_pkt_size
));
1474 cp
+= sizeof(kdp_crashdump_feature_mask
);
1475 *(uint32_t *)cp
= htonl(kdp_crashdump_pkt_size
);
1479 coreh
->th_block
= htonl((unsigned int) block
);
1482 pkt
.off
-= (unsigned int)sizeof (struct udpiphdr
);
1483 pkt
.off
-= (unsigned int)sizeof (struct ether_header
);
1485 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
1486 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
1487 enaddr_copy(&destination_mac
, eh
->ether_dhost
);
1488 eh
->ether_type
= htons(ETHERTYPE_IP
);
1490 pkt
.len
+= (unsigned int)sizeof (struct ether_header
);
1494 static int kdp_send_crashdump_seek(char *corename
, uint64_t seek_off
)
1498 if (kdp_feature_large_crashdumps
) {
1499 panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, corename
,
1503 uint32_t off
= (uint32_t) seek_off
;
1504 panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, corename
,
1508 if (panic_error
< 0) {
1509 printf ("kdp_send_crashdump_pkt failed with error %d\n",
1517 int kdp_send_crashdump_data(unsigned int request
, char *corename
,
1518 int64_t length
, caddr_t txstart
)
1520 int panic_error
= 0;
1522 while (length
> 0) {
1523 uint64_t chunk
= MIN(kdp_crashdump_pkt_size
, length
);
1525 panic_error
= kdp_send_crashdump_pkt(request
, corename
, chunk
,
1527 if (panic_error
< 0) {
1528 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
1538 uint32_t kdp_crashdump_short_pkt
;
1541 kdp_send_crashdump_pkt(unsigned int request
, char *corename
,
1542 uint64_t length
, void *panic_data
)
1545 struct corehdr
*th
= NULL
;
1546 char rretries
, tretries
;
1548 if (kdp_dump_start_time
== 0) {
1549 kdp_dump_start_time
= mach_absolute_time();
1550 kdp_superblock_dump_start_time
= kdp_dump_start_time
;
1553 tretries
= rretries
= 0;
1554 poll_count
= KDP_CRASHDUMP_POLL_COUNT
;
1555 pkt
.off
= pkt
.len
= 0;
1556 if (request
== KDP_WRQ
) /* longer timeout for initial request */
1562 if (tretries
>=15) {
1563 /* The crashdump server is unreachable for some reason. This could be a network
1564 * issue or, if we've been especially unfortunate, we've hit Radar 2760413,
1565 * which is a long standing problem with the IOKit polled mode network driver
1566 * shim which can prevent transmits/receives completely.
1568 printf ("Cannot contact panic server, timing out.\n");
1573 printf("TX retry #%d ", tretries
);
1575 th
= create_panic_header(request
, corename
, (unsigned)length
, panic_block
);
1577 if (request
== KDP_DATA
) {
1578 /* as all packets are kdp_crashdump_pkt_size in length, the last packet
1579 * may end up with trailing bits. make sure that those
1580 * bits aren't confusing. */
1581 if (length
< kdp_crashdump_pkt_size
) {
1582 kdp_crashdump_short_pkt
++;
1583 memset(th
->th_data
+ length
, 'Y',
1584 kdp_crashdump_pkt_size
- (uint32_t) length
);
1587 if (!kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)panic_data
, (caddr_t
) th
->th_data
, length
)) {
1588 uintptr_t next_page
= round_page((uintptr_t)panic_data
);
1589 memset((caddr_t
) th
->th_data
, 'X', (size_t)length
);
1590 if ((next_page
- ((uintptr_t) panic_data
)) < length
) {
1591 uint64_t resid
= length
- (next_page
- (intptr_t) panic_data
);
1592 if (!kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)next_page
, (caddr_t
) th
->th_data
+ (length
- resid
), resid
)) {
1593 memset((caddr_t
) th
->th_data
+ (length
- resid
), 'X', (size_t)resid
);
1598 else if (request
== KDP_SEEK
) {
1599 if (kdp_feature_large_crashdumps
)
1600 *(uint64_t *) th
->th_data
= OSSwapHostToBigInt64((*(uint64_t *) panic_data
));
1602 *(unsigned int *) th
->th_data
= htonl(*(unsigned int *) panic_data
);
1605 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
1607 /* Listen for the ACK */
1609 while (!pkt
.input
&& flag_panic_dump_in_progress
&& poll_count
) {
1618 th
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1619 if (request
== KDP_WRQ
) {
1620 uint16_t opcode64
= ntohs(th
->th_opcode
);
1621 uint16_t features64
= (opcode64
& 0xFF00)>>8;
1622 if ((opcode64
& 0xFF) == KDP_ACK
) {
1623 kdp_feature_large_crashdumps
= features64
& KDP_FEATURE_LARGE_CRASHDUMPS
;
1624 if (features64
& KDP_FEATURE_LARGE_PKT_SIZE
) {
1625 kdp_feature_large_pkt_size
= 1;
1628 kdp_feature_large_pkt_size
= 0;
1629 kdp_crashdump_pkt_size
= 512;
1631 printf("Protocol features: 0x%x\n", (uint32_t) features64
);
1632 th
->th_opcode
= htons(KDP_ACK
);
1635 if (ntohs(th
->th_opcode
) == KDP_ACK
&& ntohl(th
->th_block
) == panic_block
) {
1638 if (ntohs(th
->th_opcode
) == KDP_ERROR
) {
1639 printf("Panic server returned error %d, retrying\n", ntohl(th
->th_code
));
1641 goto TRANSMIT_RETRY
;
1644 if (ntohl(th
->th_block
) == (panic_block
- 1)) {
1645 printf("RX retry ");
1647 goto TRANSMIT_RETRY
;
1653 if (!flag_panic_dump_in_progress
) /* we received a debugging packet, bail*/
1655 printf("Received a debugger packet,transferring control to debugger\n");
1656 /* Configure that if not set ..*/
1657 kdp_flag
|= DBG_POST_CORE
;
1660 else /* We timed out */
1661 if (0 == poll_count
) {
1663 kdp_us_spin ((tretries%4
) * panic_timeout
); /* capped linear backoff */
1664 goto TRANSMIT_RETRY
;
1667 if (!(++panic_block
% SBLOCKSZ
)) {
1669 kdb_printf_unbuffered(".");
1670 ctime
= mach_absolute_time();
1671 kdp_superblock_dump_time
= ctime
- kdp_superblock_dump_start_time
;
1672 kdp_superblock_dump_start_time
= ctime
;
1673 if (kdp_superblock_dump_time
> kdp_max_superblock_dump_time
)
1674 kdp_max_superblock_dump_time
= kdp_superblock_dump_time
;
1675 if (kdp_superblock_dump_time
< kdp_min_superblock_dump_time
)
1676 kdp_min_superblock_dump_time
= kdp_superblock_dump_time
;
1679 if (request
== KDP_EOF
) {
1680 printf("\nTotal number of packets transmitted: %d\n", panic_block
);
1681 printf("Avg. superblock transfer abstime 0x%llx\n", ((mach_absolute_time() - kdp_dump_start_time
) / panic_block
) * SBLOCKSZ
);
1682 printf("Minimum superblock transfer abstime: 0x%llx\n", kdp_min_superblock_dump_time
);
1683 printf("Maximum superblock transfer abstime: 0x%llx\n", kdp_max_superblock_dump_time
);
1691 return ((c
> 47) && (c
< 58));
1693 /* From user mode Libc - this ought to be in a library */
1695 strnstr(char *s
, const char *find
, size_t slen
)
1700 if ((c
= *find
++) != '\0') {
1704 if ((sc
= *s
++) == '\0' || slen
-- < 1)
1709 } while (strncmp(s
, find
, len
) != 0);
1715 /* Horrid hack to extract xnu version if possible - a much cleaner approach
1716 * would be to have the integrator run a script which would copy the
1717 * xnu version into a string or an int somewhere at project submission
1718 * time - makes assumptions about sizeof(version), but will not fail if
1719 * it changes, but may be incorrect.
1721 /* 2006: Incorporated a change from Darwin user P. Lovell to extract
1722 * the minor kernel version numbers from the version string.
1725 kdp_get_xnu_version(char *versionbuf
)
1732 strlcpy(vstr
, "custom", 10);
1733 if (kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)version
, versionbuf
, 128)) {
1734 versionbuf
[127] = '\0';
1735 versionpos
= strnstr(versionbuf
, "xnu-", 115);
1737 strncpy(vstr
, versionpos
, sizeof(vstr
));
1738 vstr
[sizeof(vstr
)-1] = '\0';
1739 vptr
= vstr
+ 4; /* Begin after "xnu-" */
1740 while (*vptr
&& (isdigit(*vptr
) || *vptr
== '.'))
1743 /* Remove trailing period, if any */
1744 if (*(--vptr
) == '.')
1749 strlcpy(versionbuf
, vstr
, KDP_MAXPACKET
);
1754 kdp_set_dump_info(const uint32_t flags
, const char *filename
,
1755 const char *destipstr
, const char *routeripstr
,
1756 const uint32_t port
)
1760 if (destipstr
&& (destipstr
[0] != '\0')) {
1761 strlcpy(panicd_ip_str
, destipstr
, sizeof(panicd_ip_str
));
1762 panicd_specified
= 1;
1765 if (routeripstr
&& (routeripstr
[0] != '\0')) {
1766 strlcpy(router_ip_str
, routeripstr
, sizeof(router_ip_str
));
1767 router_specified
= 1;
1770 if (filename
&& (filename
[0] != '\0')) {
1771 strlcpy(corename_str
, filename
, sizeof(corename_str
));
1772 corename_specified
= TRUE
;
1774 corename_specified
= FALSE
;
1780 /* on a disconnect, should we stay in KDP or not? */
1781 noresume_on_disconnect
= (flags
& KDP_DUMPINFO_NORESUME
) ? 1 : 0;
1783 if ((flags
& KDP_DUMPINFO_DUMP
) == 0)
1786 /* the rest of the commands can modify kdp_flags */
1787 cmd
= flags
& KDP_DUMPINFO_MASK
;
1788 if (cmd
== KDP_DUMPINFO_DISABLE
) {
1789 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
1790 panicd_specified
= 0;
1791 kdp_trigger_core_dump
= 0;
1795 kdp_flag
&= ~REBOOT_POST_CORE
;
1796 if (flags
& KDP_DUMPINFO_REBOOT
)
1797 kdp_flag
|= REBOOT_POST_CORE
;
1799 kdp_flag
&= ~PANIC_LOG_DUMP
;
1800 if (cmd
== KDP_DUMPINFO_PANICLOG
)
1801 kdp_flag
|= PANIC_LOG_DUMP
;
1803 kdp_flag
&= ~SYSTEM_LOG_DUMP
;
1804 if (cmd
== KDP_DUMPINFO_SYSTEMLOG
)
1805 kdp_flag
|= SYSTEM_LOG_DUMP
;
1807 /* trigger a dump */
1808 kdp_flag
|= DBG_POST_CORE
;
1810 flag_dont_abort_panic_dump
= (flags
& KDP_DUMPINFO_NOINTR
) ?
1814 logPanicDataToScreen
= 1;
1815 disableConsoleOutput
= 0;
1816 disable_debug_output
= 0;
1817 kdp_trigger_core_dump
= 1;
1821 kdp_get_dump_info(uint32_t *flags
, char *filename
, char *destipstr
,
1822 char *routeripstr
, uint32_t *port
)
1825 if (panicd_specified
)
1826 strlcpy(destipstr
, panicd_ip_str
,
1827 sizeof(panicd_ip_str
));
1829 destipstr
[0] = '\0';
1833 if (router_specified
)
1834 strlcpy(routeripstr
, router_ip_str
,
1835 sizeof(router_ip_str
));
1837 routeripstr
[0] = '\0';
1841 if (corename_specified
)
1842 strlcpy(filename
, corename_str
,
1843 sizeof(corename_str
));
1850 *port
= panicd_port
;
1854 if (!panicd_specified
)
1855 *flags
|= KDP_DUMPINFO_DISABLE
;
1856 else if (kdp_flag
& PANIC_LOG_DUMP
)
1857 *flags
|= KDP_DUMPINFO_PANICLOG
;
1859 *flags
|= KDP_DUMPINFO_CORE
;
1861 if (noresume_on_disconnect
)
1862 *flags
|= KDP_DUMPINFO_NORESUME
;
1867 /* Primary dispatch routine for the system dump */
1869 kdp_panic_dump(void)
1871 char coreprefix
[10];
1875 uint32_t current_ip
= ntohl((uint32_t)kdp_current_ip_address
);
1877 if (flag_panic_dump_in_progress
) {
1878 kdb_printf("System dump aborted.\n");
1879 goto panic_dump_exit
;
1882 printf("Entering system dump routine\n");
1884 if (!kdp_en_recv_pkt
|| !kdp_en_send_pkt
) {
1885 kdb_printf("Error: No transport device registered for kernel crashdump\n");
1889 if (!panicd_specified
) {
1890 kdb_printf("A dump server was not specified in the boot-args, terminating kernel core dump.\n");
1891 goto panic_dump_exit
;
1894 flag_panic_dump_in_progress
= TRUE
;
1897 kdp_panic("kdp_panic_dump: unexpected pending input packet");
1899 kdp_get_xnu_version((char *) &pkt
.data
[0]);
1901 if (!corename_specified
) {
1902 /* Panic log bit takes precedence over core dump bit */
1903 if ((panicstr
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
))
1904 strlcpy(coreprefix
, "paniclog", sizeof(coreprefix
));
1905 else if (kdp_flag
& SYSTEM_LOG_DUMP
)
1906 strlcpy(coreprefix
, "systemlog", sizeof(coreprefix
));
1908 strlcpy(coreprefix
, "core", sizeof(coreprefix
));
1910 abstime
= mach_absolute_time();
1911 pkt
.data
[20] = '\0';
1912 snprintf (corename_str
, sizeof(corename_str
), "%s-%s-%d.%d.%d.%d-%x",
1913 coreprefix
, &pkt
.data
[0],
1914 (current_ip
& 0xff000000) >> 24,
1915 (current_ip
& 0xff0000) >> 16,
1916 (current_ip
& 0xff00) >> 8,
1917 (current_ip
& 0xff),
1918 (unsigned int) (abstime
& 0xffffffff));
1921 if (0 == inet_aton(panicd_ip_str
, (struct in_addr
*) &panic_server_ip
)) {
1922 kdb_printf("inet_aton() failed interpreting %s as a panic server IP\n", panicd_ip_str
);
1925 kdb_printf("Attempting connection to panic server configured at IP %s, port %d\n", panicd_ip_str
, panicd_port
);
1927 destination_mac
= router_mac
;
1929 if (kdp_arp_resolve(panic_server_ip
, &temp_mac
)) {
1930 kdb_printf("Resolved %s's (or proxy's) link level address\n", panicd_ip_str
);
1931 destination_mac
= temp_mac
;
1934 if (!flag_panic_dump_in_progress
) goto panic_dump_exit
;
1935 if (router_specified
) {
1936 if (0 == inet_aton(router_ip_str
, (struct in_addr
*) &parsed_router_ip
))
1937 kdb_printf("inet_aton() failed interpreting %s as an IP\n", router_ip_str
);
1939 router_ip
= parsed_router_ip
;
1940 if (kdp_arp_resolve(router_ip
, &temp_mac
)) {
1941 destination_mac
= temp_mac
;
1942 kdb_printf("Routing through specified router IP %s (%d)\n", router_ip_str
, router_ip
);
1948 if (!flag_panic_dump_in_progress
) goto panic_dump_exit
;
1950 kdb_printf("Transmitting packets to link level address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1951 destination_mac
.ether_addr_octet
[0] & 0xff,
1952 destination_mac
.ether_addr_octet
[1] & 0xff,
1953 destination_mac
.ether_addr_octet
[2] & 0xff,
1954 destination_mac
.ether_addr_octet
[3] & 0xff,
1955 destination_mac
.ether_addr_octet
[4] & 0xff,
1956 destination_mac
.ether_addr_octet
[5] & 0xff);
1958 kdb_printf("Kernel map size is %llu\n", (unsigned long long) get_vmmap_size(kernel_map
));
1959 kdb_printf("Sending write request for %s\n", corename_str
);
1961 if ((panic_error
= kdp_send_crashdump_pkt(KDP_WRQ
, corename_str
, 0 , NULL
)) < 0) {
1962 kdb_printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
1963 goto panic_dump_exit
;
1966 /* Just the panic log requested */
1967 if ((panicstr
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
)) {
1968 kdb_printf_unbuffered("Transmitting panic log, please wait: ");
1969 kdp_send_crashdump_data(KDP_DATA
, corename_str
,
1970 debug_buf_ptr
- debug_buf
,
1972 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
1973 printf("Please file a bug report on this panic, if possible.\n");
1974 goto panic_dump_exit
;
1977 /* maybe we wanted the systemlog */
1978 if (kdp_flag
& SYSTEM_LOG_DUMP
) {
1979 long start_off
= msgbufp
->msg_bufx
;
1982 kdb_printf_unbuffered("Transmitting system log, please wait: ");
1983 if (start_off
>= msgbufp
->msg_bufr
) {
1984 len
= msgbufp
->msg_size
- start_off
;
1985 kdp_send_crashdump_data(KDP_DATA
, corename_str
, len
,
1986 msgbufp
->msg_bufc
+ start_off
);
1987 /* seek to remove trailing bytes */
1988 kdp_send_crashdump_seek(corename_str
, len
);
1992 if (start_off
!= msgbufp
->msg_bufr
) {
1993 len
= msgbufp
->msg_bufr
- start_off
;
1994 kdp_send_crashdump_data(KDP_DATA
, corename_str
, len
,
1995 msgbufp
->msg_bufc
+ start_off
);
1998 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
1999 goto panic_dump_exit
;
2002 /* We want a core dump if we're here */
2006 abort_panic_transfer();
2012 abort_panic_transfer(void)
2014 flag_panic_dump_in_progress
= FALSE
;
2015 flag_dont_abort_panic_dump
= FALSE
;
2019 #if CONFIG_SERIAL_KDP
2021 static boolean_t needs_serial_init
= TRUE
;
2024 kdp_serial_send(void *rpkt
, unsigned int rpkt_len
)
2027 kdp_serialize_packet((unsigned char *)rpkt
, rpkt_len
, pal_serial_putc
);
2031 kdp_serial_receive(void *rpkt
, unsigned int *rpkt_len
, unsigned int timeout
)
2034 uint64_t now
, deadline
;
2036 clock_interval_to_deadline(timeout
, 1000 * 1000 /* milliseconds */, &deadline
);
2039 for(clock_get_uptime(&now
); now
< deadline
; clock_get_uptime(&now
))
2041 readkar
= pal_serial_getc();
2044 unsigned char *packet
;
2045 // printf("got char %02x\n", readkar);
2046 if((packet
= kdp_unserialize_packet(readkar
,rpkt_len
)))
2048 memcpy(rpkt
, packet
, *rpkt_len
);
2057 kdp_serial_setmode(boolean_t active
)
2059 if (active
== FALSE
) /* leaving KDP */
2062 if (!needs_serial_init
)
2066 needs_serial_init
= FALSE
;
2071 static void kdp_serial_callout(__unused
void *arg
, kdp_event_t event
)
2073 /* When we stop KDP, set the bit to re-initialize the console serial port
2074 * the next time we send/receive a KDP packet. We don't do it on
2075 * KDP_EVENT_ENTER directly because it also gets called when we trap to KDP
2076 * for non-external debugging, i.e., stackshot or core dumps.
2078 * Set needs_serial_init on exit (and initialization, see above) and not
2079 * enter because enter is sent multiple times and causes excess reinitialization.
2084 case KDP_EVENT_PANICLOG
:
2085 case KDP_EVENT_ENTER
:
2087 case KDP_EVENT_EXIT
:
2088 needs_serial_init
= TRUE
;
2093 #endif /* CONFIG_SERIAL_KDP */
2098 strlcpy(kdp_kernelversion_string
, version
, sizeof(kdp_kernelversion_string
));
2100 /* Relies on platform layer calling panic_init() before kdp_init() */
2101 if (kernel_uuid
[0] != '\0') {
2103 * Update kdp_kernelversion_string with our UUID
2104 * generated at link time.
2107 strlcat(kdp_kernelversion_string
, "; UUID=", sizeof(kdp_kernelversion_string
));
2108 strlcat(kdp_kernelversion_string
, kernel_uuid
, sizeof(kdp_kernelversion_string
));
2111 #if defined(__x86_64__) || defined(__arm__)
2114 if (vm_kernel_slide
) {
2115 char KASLR_stext
[19];
2116 strlcat(kdp_kernelversion_string
, "; stext=", sizeof(kdp_kernelversion_string
));
2117 snprintf(KASLR_stext
, sizeof(KASLR_stext
), "%p", (void *) vm_kernel_stext
);
2118 strlcat(kdp_kernelversion_string
, KASLR_stext
, sizeof(kdp_kernelversion_string
));
2122 if (debug_boot_arg
& DB_REBOOT_POST_CORE
)
2123 kdp_flag
|= REBOOT_POST_CORE
;
2124 #if defined(__x86_64__)
2128 kdp_timer_callout_init();
2129 kdp_crashdump_feature_mask
= htonl(kdp_crashdump_feature_mask
);
2131 #if CONFIG_SERIAL_KDP
2133 struct in_addr ipaddr
;
2134 struct ether_addr macaddr
;
2138 //serial will be the debugger, unless match name is explicitly provided, and it's not "serial"
2139 if(PE_parse_boot_argn("kdp_match_name", kdpname
, sizeof(kdpname
)) && strncmp(kdpname
, "serial", sizeof(kdpname
)) != 0)
2142 // serial must be explicitly requested
2143 if(!PE_parse_boot_argn("kdp_match_name", kdpname
, sizeof(kdpname
)) || strncmp(kdpname
, "serial", sizeof(kdpname
)) != 0)
2147 kprintf("Initializing serial KDP\n");
2149 kdp_register_callout(kdp_serial_callout
, NULL
);
2150 kdp_register_link(NULL
, kdp_serial_setmode
);
2151 kdp_register_send_receive(kdp_serial_send
, kdp_serial_receive
);
2153 /* fake up an ip and mac for early serial debugging */
2154 macaddr
.ether_addr_octet
[0] = 's';
2155 macaddr
.ether_addr_octet
[1] = 'e';
2156 macaddr
.ether_addr_octet
[2] = 'r';
2157 macaddr
.ether_addr_octet
[3] = 'i';
2158 macaddr
.ether_addr_octet
[4] = 'a';
2159 macaddr
.ether_addr_octet
[5] = 'l';
2160 ipaddr
.s_addr
= KDP_SERIAL_IPADDR
;
2161 kdp_set_ip_and_mac_addresses(&ipaddr
, &macaddr
);
2163 #endif /* CONFIG_SERIAL_KDP */