2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 1982, 1986, 1993
31 * The Regents of the University of California. All rights reserved.
35 * Kernel Debugging Protocol UDP implementation.
38 #include <mach/boolean.h>
39 #include <mach/mach_types.h>
40 #include <mach/exception_types.h>
41 #include <kern/cpu_data.h>
42 #include <kern/debug.h>
43 #include <kern/clock.h>
45 #include <kdp/kdp_core.h>
46 #include <kdp/kdp_internal.h>
47 #include <kdp/kdp_en_debugger.h>
48 #include <kdp/kdp_callout.h>
49 #include <kdp/kdp_udp.h>
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 extern int inet_aton(const char *, struct kdp_in_addr
*); /* in libkern */
75 extern char *inet_ntoa_r(struct kdp_in_addr ina
, char *buf
,
76 size_t buflen
); /* in libkern */
78 #define DO_ALIGN 1 /* align all packet data accesses */
79 #define KDP_SERIAL_IPADDR 0xABADBABE /* IP address used for serial KDP */
80 #define LINK_UP_STATUS (IFM_AVALID | IFM_ACTIVE)
82 extern int kdp_getc(void);
83 extern int reattach_wait
;
85 /* only used by IONetworkingFamily */
86 typedef uint32_t (*kdp_link_t
)(void);
87 typedef boolean_t (*kdp_mode_t
)(boolean_t
);
88 void kdp_register_link(kdp_link_t link
, kdp_mode_t mode
);
89 void kdp_unregister_link(kdp_link_t link
, kdp_mode_t mode
);
91 static u_short ip_id
; /* ip packet ctr, for ids */
93 /* @(#)udp_usrreq.c 2.2 88/05/23 4.0NFSSRC SMI; from UCB 7.1 6/5/86 */
96 * UDP protocol implementation.
97 * Per RFC 768, August, 1980.
99 #define UDP_TTL 60 /* deflt time to live for UDP packets */
100 static int udp_ttl
= UDP_TTL
;
101 static unsigned char exception_seq
;
104 uint32_t ih_next
, ih_prev
; /* for protocol sequence q's */
105 u_char ih_x1
; /* (unused) */
106 u_char ih_pr
; /* protocol */
107 short ih_len
; /* protocol length */
108 struct kdp_in_addr ih_src
; /* source internet address */
109 struct kdp_in_addr ih_dst
; /* destination internet address */
113 u_short uh_sport
; /* source port */
114 u_short uh_dport
; /* destination port */
115 short uh_ulen
; /* udp length */
116 u_short uh_sum
; /* udp checksum */
119 struct kdp_udpiphdr
{
120 struct kdp_ipovly ui_i
; /* overlaid ip structure */
121 struct kdp_udphdr ui_u
; /* udp header */
123 #define ui_next ui_i.ih_next
124 #define ui_prev ui_i.ih_prev
125 #define ui_x1 ui_i.ih_x1
126 #define ui_pr ui_i.ih_pr
127 #define ui_len ui_i.ih_len
128 #define ui_src ui_i.ih_src
129 #define ui_dst ui_i.ih_dst
130 #define ui_sport ui_u.uh_sport
131 #define ui_dport ui_u.uh_dport
132 #define ui_ulen ui_u.uh_ulen
133 #define ui_sum ui_u.uh_sum
140 #ifdef __LITTLE_ENDIAN__
141 ip_xhl
:4, /* header length */
142 ip_xv
:4, /* version */
143 ip_xtos
:8, /* type of service */
144 ip_xlen
:16; /* total length */
146 #ifdef __BIG_ENDIAN__
147 ip_xv
:4, /* version */
148 ip_xhl
:4, /* header length */
149 ip_xtos
:8, /* type of service */
150 ip_xlen
:16; /* total length */
154 u_short ip_id
; /* identification */
155 short ip_off
; /* fragment offset field */
156 #define IP_DF 0x4000 /* dont fragment flag */
157 #define IP_MF 0x2000 /* more fragments flag */
158 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
159 u_char ip_ttl
; /* time to live */
160 u_char ip_p
; /* protocol */
161 u_short ip_sum
; /* checksum */
162 struct kdp_in_addr ip_src
,ip_dst
; /* source and dest address */
164 #define ip_v ip_vhltl.ip_x.ip_xv
165 #define ip_hl ip_vhltl.ip_x.ip_xhl
166 #define ip_tos ip_vhltl.ip_x.ip_xtos
167 #define ip_len ip_vhltl.ip_x.ip_xlen
169 #define IPPROTO_UDP 17
172 #define ETHERTYPE_IP 0x0800 /* IP protocol */
175 * Ethernet Address Resolution Protocol.
177 * See RFC 826 for protocol description. Structure below is adapted
178 * to resolving internet addresses. Field names used correspond to
182 #define ETHERTYPE_ARP 0x0806 /* Addr. resolution protocol */
185 u_short ar_hrd
; /* format of hardware address */
186 #define ARPHRD_ETHER 1 /* ethernet hardware format */
187 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
188 u_short ar_pro
; /* format of protocol address */
189 u_char ar_hln
; /* length of hardware address */
190 u_char ar_pln
; /* length of protocol address */
191 u_short ar_op
; /* one of: */
192 #define ARPOP_REQUEST 1 /* request to resolve address */
193 #define ARPOP_REPLY 2 /* response to previous request */
194 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
195 #define ARPOP_REVREPLY 4 /* response giving protocol address */
196 #define ARPOP_INVREQUEST 8 /* request to identify peer */
197 #define ARPOP_INVREPLY 9 /* response identifying peer */
200 struct kdp_ether_arp
{
201 struct kdp_arphdr ea_hdr
; /* fixed-size header */
202 u_char arp_sha
[ETHER_ADDR_LEN
]; /* sender hardware address */
203 u_char arp_spa
[4]; /* sender protocol address */
204 u_char arp_tha
[ETHER_ADDR_LEN
]; /* target hardware address */
205 u_char arp_tpa
[4]; /* target protocol address */
207 #define arp_hrd ea_hdr.ar_hrd
208 #define arp_pro ea_hdr.ar_pro
209 #define arp_hln ea_hdr.ar_hln
210 #define arp_pln ea_hdr.ar_pln
211 #define arp_op ea_hdr.ar_op
213 #define ETHERMTU 1500
214 #define ETHERHDRSIZE 14
216 #define KDP_MAXPACKET (ETHERHDRSIZE + ETHERMTU + ETHERCRC)
219 unsigned char data
[KDP_MAXPACKET
];
220 unsigned int off
, len
;
224 struct kdp_manual_pkt manual_pkt
;
228 struct kdp_in_addr in
;
229 struct kdp_ether_addr ea
;
232 struct kdp_in_addr in
;
233 struct kdp_ether_addr ea
;
238 *exception_message
[] = {
240 "Memory access", /* EXC_BAD_ACCESS */
241 "Failed instruction", /* EXC_BAD_INSTRUCTION */
242 "Arithmetic", /* EXC_ARITHMETIC */
243 "Emulation", /* EXC_EMULATION */
244 "Software", /* EXC_SOFTWARE */
245 "Breakpoint" /* EXC_BREAKPOINT */
248 volatile int kdp_flag
= 0;
250 static kdp_send_t kdp_en_send_pkt
;
251 static kdp_receive_t kdp_en_recv_pkt
;
252 static kdp_link_t kdp_en_linkstatus
;
253 static kdp_mode_t kdp_en_setmode
;
255 #if CONFIG_SERIAL_KDP
256 static void kdp_serial_send(void *rpkt
, unsigned int rpkt_len
);
257 #define KDP_SERIAL_ENABLED() (kdp_en_send_pkt == kdp_serial_send)
259 #define KDP_SERIAL_ENABLED() (0)
262 static uint32_t kdp_current_ip_address
= 0;
263 static struct kdp_ether_addr kdp_current_mac_address
= {{0, 0, 0, 0, 0, 0}};
264 static void *kdp_current_ifp
;
266 static void kdp_handler( void *);
268 static uint32_t panic_server_ip
= 0;
269 static uint32_t parsed_router_ip
= 0;
270 static uint32_t router_ip
= 0;
271 static uint32_t target_ip
= 0;
273 static boolean_t save_ip_in_nvram
= FALSE
;
275 static volatile boolean_t panicd_specified
= FALSE
;
276 static boolean_t router_specified
= FALSE
;
277 static boolean_t corename_specified
= FALSE
;
278 static unsigned int panicd_port
= CORE_REMOTE_PORT
;
280 static struct kdp_ether_addr etherbroadcastaddr
= {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
282 static struct kdp_ether_addr router_mac
= {{0, 0, 0 , 0, 0, 0}};
283 static struct kdp_ether_addr destination_mac
= {{0, 0, 0 , 0, 0, 0}};
284 static struct kdp_ether_addr temp_mac
= {{0, 0, 0 , 0, 0, 0}};
285 static struct kdp_ether_addr current_resolved_MAC
= {{0, 0, 0 , 0, 0, 0}};
287 static boolean_t flag_panic_dump_in_progress
= FALSE
;
288 static boolean_t flag_router_mac_initialized
= FALSE
;
289 static boolean_t flag_dont_abort_panic_dump
= FALSE
;
291 static boolean_t flag_arp_resolved
= FALSE
;
293 static unsigned int panic_timeout
= 100000;
294 static unsigned int last_panic_port
= CORE_REMOTE_PORT
;
296 #define KDP_THROTTLE_VALUE (10ULL * NSEC_PER_SEC)
298 uint32_t kdp_crashdump_pkt_size
= 512;
299 #define KDP_LARGE_CRASHDUMP_PKT_SIZE (1440 - 6 - sizeof(struct kdp_udpiphdr))
300 static char panicd_ip_str
[20];
301 static char router_ip_str
[20];
302 static char corename_str
[50];
304 static unsigned int panic_block
= 0;
305 volatile unsigned int kdp_trigger_core_dump
= 0;
306 __private_extern__
volatile unsigned int flag_kdp_trigger_reboot
= 0;
308 extern unsigned int not_in_kdp
;
310 extern unsigned int disableConsoleOutput
;
312 extern void kdp_call(void);
313 extern boolean_t
kdp_call_kdb(void);
314 extern int kern_dump(void);
316 void * kdp_get_interface(void);
317 void kdp_set_gateway_mac(void *gatewaymac
);
318 void kdp_set_ip_and_mac_addresses(struct kdp_in_addr
*ipaddr
, struct kdp_ether_addr
*);
319 void kdp_set_interface(void *interface
, const struct kdp_ether_addr
*macaddr
);
321 void kdp_disable_arp(void);
322 static void kdp_arp_reply(struct kdp_ether_arp
*);
323 static void kdp_process_arp_reply(struct kdp_ether_arp
*);
324 static boolean_t
kdp_arp_resolve(uint32_t, struct kdp_ether_addr
*);
326 static volatile unsigned kdp_reentry_deadline
;
328 static uint32_t kdp_crashdump_feature_mask
= KDP_FEATURE_LARGE_CRASHDUMPS
| KDP_FEATURE_LARGE_PKT_SIZE
;
329 uint32_t kdp_feature_large_crashdumps
, kdp_feature_large_pkt_size
;
331 char kdp_kernelversion_string
[256];
333 static boolean_t gKDPDebug
= FALSE
;
334 #define KDP_DEBUG(...) if (gKDPDebug) printf(__VA_ARGS__);
336 int kdp_snapshot
= 0;
337 static int stack_snapshot_ret
= 0;
338 static unsigned stack_snapshot_bytes_traced
= 0;
340 static void *stack_snapshot_buf
;
341 static uint32_t stack_snapshot_bufsize
;
342 static int stack_snapshot_pid
;
343 static uint32_t stack_snapshot_flags
;
344 static uint32_t stack_snapshot_dispatch_offset
;
346 static unsigned int old_debugger
;
348 #define SBLOCKSZ (2048)
349 uint64_t kdp_dump_start_time
= 0;
350 uint64_t kdp_min_superblock_dump_time
= ~1ULL;
351 uint64_t kdp_max_superblock_dump_time
= 0;
352 uint64_t kdp_superblock_dump_time
= 0;
353 uint64_t kdp_superblock_dump_start_time
= 0;
356 kdp_snapshot_preflight(int pid
, void * tracebuf
, uint32_t tracebuf_size
,
357 uint32_t flags
, uint32_t dispatch_offset
);
360 kdp_snapshot_postflight(void);
363 kdp_stackshot(int pid
, void *tracebuf
, uint32_t tracebuf_size
,
364 uint32_t flags
, uint32_t dispatch_offset
, uint32_t *pbytesTraced
);
367 kdp_stack_snapshot_geterror(void);
370 kdp_stack_snapshot_bytes_traced(void);
376 kdp_ml_enter_debugger_wrapper(__unused
void *param0
, __unused
void *param1
) {
377 kdp_ml_enter_debugger();
381 kdp_timer_callout_init(void) {
382 kdp_timer_call
= thread_call_allocate(kdp_ml_enter_debugger_wrapper
, NULL
);
386 /* only send/receive data if the link is up */
387 inline static void wait_for_link(void)
389 static int first
= 0;
391 if (!kdp_en_linkstatus
)
394 while (((*kdp_en_linkstatus
)() & LINK_UP_STATUS
) != LINK_UP_STATUS
) {
399 printf("Waiting for link to become available.\n");
400 kprintf("Waiting for link to become available.\n");
405 inline static void kdp_send_data(void *packet
, unsigned int len
)
408 (*kdp_en_send_pkt
)(packet
, len
);
412 inline static void kdp_receive_data(void *packet
, unsigned int *len
,
413 unsigned int timeout
)
416 (*kdp_en_recv_pkt
)(packet
, len
, timeout
);
420 void kdp_register_link(kdp_link_t link
, kdp_mode_t mode
)
422 kdp_en_linkstatus
= link
;
423 kdp_en_setmode
= mode
;
426 void kdp_unregister_link(__unused kdp_link_t link
, __unused kdp_mode_t mode
)
428 kdp_en_linkstatus
= NULL
;
429 kdp_en_setmode
= NULL
;
433 kdp_register_send_receive(
435 kdp_receive_t receive
)
437 unsigned int debug
= 0;
439 PE_parse_boot_argn("debug", &debug
, sizeof (debug
));
445 kdp_en_send_pkt
= send
;
446 kdp_en_recv_pkt
= receive
;
448 if (debug
& DB_KDP_BP_DIS
)
449 kdp_flag
|= KDP_BP_DIS
;
450 if (debug
& DB_KDP_GETC_ENA
)
451 kdp_flag
|= KDP_GETC_ENA
;
455 if (debug
& DB_KERN_DUMP_ON_PANIC
)
456 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
457 if (debug
& DB_KERN_DUMP_ON_NMI
)
458 kdp_flag
|= PANIC_CORE_ON_NMI
;
460 if (debug
& DB_DBG_POST_CORE
)
461 kdp_flag
|= DBG_POST_CORE
;
463 if (debug
& DB_PANICLOG_DUMP
)
464 kdp_flag
|= PANIC_LOG_DUMP
;
466 if (PE_parse_boot_argn("_panicd_ip", panicd_ip_str
, sizeof (panicd_ip_str
)))
467 panicd_specified
= TRUE
;
469 if ((debug
& DB_REBOOT_POST_CORE
) && (panicd_specified
== TRUE
))
470 kdp_flag
|= REBOOT_POST_CORE
;
472 if (PE_parse_boot_argn("_router_ip", router_ip_str
, sizeof (router_ip_str
)))
473 router_specified
= TRUE
;
475 if (!PE_parse_boot_argn("panicd_port", &panicd_port
, sizeof (panicd_port
)))
476 panicd_port
= CORE_REMOTE_PORT
;
478 if (PE_parse_boot_argn("_panicd_corename", &corename_str
, sizeof (corename_str
)))
479 corename_specified
= TRUE
;
481 kdp_flag
|= KDP_READY
;
482 if (current_debugger
== NO_CUR_DB
)
483 current_debugger
= KDP_CUR_DB
;
484 if ((kdp_current_ip_address
!= 0) && halt_in_debugger
) {
491 kdp_unregister_send_receive(
492 __unused kdp_send_t send
,
493 __unused kdp_receive_t receive
)
495 if (current_debugger
== KDP_CUR_DB
)
496 current_debugger
= NO_CUR_DB
;
497 kdp_flag
&= ~KDP_READY
;
498 kdp_en_send_pkt
= NULL
;
499 kdp_en_recv_pkt
= NULL
;
502 /* Cache stack snapshot parameters in preparation for a trace */
504 kdp_snapshot_preflight(int pid
, void * tracebuf
, uint32_t tracebuf_size
, uint32_t flags
, uint32_t dispatch_offset
)
506 stack_snapshot_pid
= pid
;
507 stack_snapshot_buf
= tracebuf
;
508 stack_snapshot_bufsize
= tracebuf_size
;
509 stack_snapshot_flags
= flags
;
510 stack_snapshot_dispatch_offset
= dispatch_offset
;
512 /* Mark this debugger as active, since the polled mode driver that
513 * ordinarily does this may not be enabled (yet), or since KDB may be
514 * the primary debugger.
516 old_debugger
= current_debugger
;
517 if (old_debugger
!= KDP_CUR_DB
) {
518 current_debugger
= KDP_CUR_DB
;
523 kdp_snapshot_postflight(void)
526 if ((kdp_en_send_pkt
== NULL
) || (old_debugger
== KDB_CUR_DB
))
527 current_debugger
= old_debugger
;
531 kdp_stack_snapshot_geterror(void)
533 return stack_snapshot_ret
;
537 kdp_stack_snapshot_bytes_traced(void)
539 return stack_snapshot_bytes_traced
;
543 kdp_schedule_debugger_reentry(unsigned interval
) {
546 clock_interval_to_deadline(interval
, 1000 * 1000, &deadline
);
547 thread_call_enter_delayed(kdp_timer_call
, deadline
);
556 bcopy((char *)src
, (char *)dst
, sizeof (struct kdp_ether_addr
));
559 static unsigned short
565 unsigned int high
, low
, sum
;
575 sum
= (high
<< 8) + low
;
576 sum
= (sum
>> 16) + (sum
& 65535);
578 return (sum
> 65535 ? sum
- 65535 : sum
);
583 unsigned short reply_port
,
584 const boolean_t sideband
587 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
588 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
589 struct kdp_in_addr tmp_ipaddr
;
590 struct kdp_ether_addr tmp_enaddr
;
591 struct kdp_ether_header
*eh
= NULL
;
594 kdp_panic("kdp_reply");
596 pkt
.off
-= (unsigned int)sizeof (struct kdp_udpiphdr
);
599 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
601 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
603 ui
->ui_next
= ui
->ui_prev
= 0;
605 ui
->ui_pr
= IPPROTO_UDP
;
606 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct kdp_udphdr
));
607 tmp_ipaddr
= ui
->ui_src
;
608 ui
->ui_src
= ui
->ui_dst
;
609 ui
->ui_dst
= tmp_ipaddr
;
610 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
611 ui
->ui_dport
= reply_port
;
612 ui
->ui_ulen
= ui
->ui_len
;
615 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
616 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
618 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
620 ip
->ip_len
= htons(sizeof (struct kdp_udpiphdr
) + pkt
.len
);
621 ip
->ip_v
= IPVERSION
;
622 ip
->ip_id
= htons(ip_id
++);
623 ip
->ip_hl
= sizeof (struct kdp_ip
) >> 2;
624 ip
->ip_ttl
= udp_ttl
;
626 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
628 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
631 pkt
.len
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
633 pkt
.off
-= (unsigned int)sizeof (struct kdp_ether_header
);
635 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
636 enaddr_copy(eh
->ether_shost
, &tmp_enaddr
);
637 enaddr_copy(eh
->ether_dhost
, eh
->ether_shost
);
638 enaddr_copy(&tmp_enaddr
, eh
->ether_dhost
);
639 eh
->ether_type
= htons(ETHERTYPE_IP
);
641 pkt
.len
+= (unsigned int)sizeof (struct kdp_ether_header
);
643 // save reply for possible retransmission
644 assert(pkt
.len
<= KDP_MAXPACKET
);
646 bcopy((char *)&pkt
, (char *)&saved_reply
, sizeof(saved_reply
));
648 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
650 // increment expected sequence number
657 unsigned short remote_port
660 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
661 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
662 struct kdp_ether_header
*eh
;
665 kdp_panic("kdp_send");
667 pkt
.off
-= (unsigned int)sizeof (struct kdp_udpiphdr
);
670 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
672 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
674 ui
->ui_next
= ui
->ui_prev
= 0;
676 ui
->ui_pr
= IPPROTO_UDP
;
677 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct kdp_udphdr
));
678 ui
->ui_src
= adr
.loc
.in
;
679 ui
->ui_dst
= adr
.rmt
.in
;
680 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
681 ui
->ui_dport
= remote_port
;
682 ui
->ui_ulen
= ui
->ui_len
;
685 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
686 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
688 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
690 ip
->ip_len
= htons(sizeof (struct kdp_udpiphdr
) + pkt
.len
);
691 ip
->ip_v
= IPVERSION
;
692 ip
->ip_id
= htons(ip_id
++);
693 ip
->ip_hl
= sizeof (struct kdp_ip
) >> 2;
694 ip
->ip_ttl
= udp_ttl
;
696 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
698 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
701 pkt
.len
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
703 pkt
.off
-= (unsigned int)sizeof (struct kdp_ether_header
);
705 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
706 enaddr_copy(&adr
.loc
.ea
, eh
->ether_shost
);
707 enaddr_copy(&adr
.rmt
.ea
, eh
->ether_dhost
);
708 eh
->ether_type
= htons(ETHERTYPE_IP
);
710 pkt
.len
+= (unsigned int)sizeof (struct kdp_ether_header
);
711 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
715 inline static void debugger_if_necessary(void)
717 if ((current_debugger
== KDP_CUR_DB
) && halt_in_debugger
) {
724 /* We don't interpret this pointer, we just give it to the bsd stack
725 so it can decide when to set the MAC and IP info. We'll
726 early initialize the MAC/IP info if we can so that we can use
727 KDP early in boot. These values may subsequently get over-written
728 when the interface gets initialized for real.
731 kdp_set_interface(void *ifp
, const struct kdp_ether_addr
*macaddr
)
734 struct kdp_in_addr addr
= { 0 };
737 kdp_current_ifp
= ifp
;
739 if (PE_parse_boot_argn("kdp_ip_addr", kdpstr
, sizeof(kdpstr
))) {
740 /* look for a static ip address */
741 if (inet_aton(kdpstr
, &addr
) == FALSE
)
747 /* use saved ip address */
748 save_ip_in_nvram
= TRUE
;
750 len
= sizeof(kdpstr
);
751 if (PEReadNVRAMProperty("_kdp_ipstr", kdpstr
, &len
) == FALSE
)
754 kdpstr
[len
< sizeof(kdpstr
) ? len
: sizeof(kdpstr
) - 1] = '\0';
755 if (inet_aton(kdpstr
, &addr
) == FALSE
)
759 kdp_current_ip_address
= addr
.s_addr
;
761 kdp_current_mac_address
= *macaddr
;
763 /* we can't drop into the debugger at this point because the
764 link will likely not be up. when getDebuggerLinkStatus() support gets
765 added to the appropriate network drivers, adding the
766 following will enable this capability:
767 debugger_if_necessary();
774 kdp_get_interface(void)
776 return kdp_current_ifp
;
780 kdp_set_ip_and_mac_addresses(
781 struct kdp_in_addr
*ipaddr
,
782 struct kdp_ether_addr
*macaddr
)
784 static uint64_t last_time
= (uint64_t) -1;
785 static uint64_t throttle_val
= 0;
789 if (kdp_current_ip_address
== ipaddr
->s_addr
)
792 /* don't replace if serial debugging is configured */
793 if (!KDP_SERIAL_ENABLED() ||
794 (kdp_current_ip_address
!= KDP_SERIAL_IPADDR
)) {
795 kdp_current_mac_address
= *macaddr
;
796 kdp_current_ip_address
= ipaddr
->s_addr
;
799 if (save_ip_in_nvram
== FALSE
)
802 if (inet_ntoa_r(*ipaddr
, addr
, sizeof(addr
)) == NULL
)
805 /* throttle writes if needed */
807 nanoseconds_to_absolutetime(KDP_THROTTLE_VALUE
, &throttle_val
);
809 cur_time
= mach_absolute_time();
810 if (last_time
== (uint64_t) -1 ||
811 ((cur_time
- last_time
) > throttle_val
)) {
812 PEWriteNVRAMProperty("_kdp_ipstr", addr
,
813 (const unsigned int) strlen(addr
));
815 last_time
= cur_time
;
818 debugger_if_necessary();
822 kdp_set_gateway_mac(void *gatewaymac
)
824 router_mac
= *(struct kdp_ether_addr
*)gatewaymac
;
825 flag_router_mac_initialized
= TRUE
;
828 struct kdp_ether_addr
829 kdp_get_mac_addr(void)
831 return kdp_current_mac_address
;
835 kdp_get_ip_address(void)
837 return (unsigned int)kdp_current_ip_address
;
841 kdp_disable_arp(void)
843 kdp_flag
&= ~(DB_ARP
);
847 kdp_arp_dispatch(void)
849 struct kdp_ether_arp aligned_ea
, *ea
= &aligned_ea
;
850 unsigned arp_header_offset
;
852 arp_header_offset
= (unsigned)sizeof(struct kdp_ether_header
) + pkt
.off
;
853 memcpy((void *)ea
, (void *)&pkt
.data
[arp_header_offset
], sizeof(*ea
));
855 switch(ntohs(ea
->arp_op
)) {
860 kdp_process_arp_reply(ea
);
868 kdp_process_arp_reply(struct kdp_ether_arp
*ea
)
870 /* Are we interested in ARP replies? */
871 if (flag_arp_resolved
== TRUE
)
874 /* Did we receive a reply from the right source? */
875 if (((struct kdp_in_addr
*)(ea
->arp_spa
))->s_addr
!= target_ip
)
878 flag_arp_resolved
= TRUE
;
879 current_resolved_MAC
= *(struct kdp_ether_addr
*) (ea
->arp_sha
);
884 /* ARP responses are enabled when the DB_ARP bit of the debug boot arg
889 kdp_arp_reply(struct kdp_ether_arp
*ea
)
891 struct kdp_ether_header
*eh
;
893 struct kdp_in_addr isaddr
, itaddr
, myaddr
;
894 struct kdp_ether_addr my_enaddr
;
896 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
897 pkt
.off
+= (unsigned int)sizeof(struct kdp_ether_header
);
899 if(ntohs(ea
->arp_op
) != ARPOP_REQUEST
)
902 myaddr
.s_addr
= kdp_get_ip_address();
903 my_enaddr
= kdp_get_mac_addr();
905 if ((ntohl(myaddr
.s_addr
) == 0) ||
906 ((my_enaddr
.ether_addr_octet
[0] & 0xff) == 0
907 && (my_enaddr
.ether_addr_octet
[1] & 0xff) == 0
908 && (my_enaddr
.ether_addr_octet
[2] & 0xff) == 0
909 && (my_enaddr
.ether_addr_octet
[3] & 0xff) == 0
910 && (my_enaddr
.ether_addr_octet
[4] & 0xff) == 0
911 && (my_enaddr
.ether_addr_octet
[5] & 0xff) == 0
915 (void)memcpy((void *)&isaddr
, (void *)ea
->arp_spa
, sizeof (isaddr
));
916 (void)memcpy((void *)&itaddr
, (void *)ea
->arp_tpa
, sizeof (itaddr
));
918 if (itaddr
.s_addr
== myaddr
.s_addr
) {
919 (void)memcpy((void *)ea
->arp_tha
, (void *)ea
->arp_sha
, sizeof(ea
->arp_sha
));
920 (void)memcpy((void *)ea
->arp_sha
, (void *)&my_enaddr
, sizeof(ea
->arp_sha
));
922 (void)memcpy((void *)ea
->arp_tpa
, (void *) ea
->arp_spa
, sizeof(ea
->arp_spa
));
923 (void)memcpy((void *)ea
->arp_spa
, (void *) &itaddr
, sizeof(ea
->arp_spa
));
925 ea
->arp_op
= htons(ARPOP_REPLY
);
926 ea
->arp_pro
= htons(ETHERTYPE_IP
);
927 (void)memcpy(eh
->ether_dhost
, ea
->arp_tha
, sizeof(eh
->ether_dhost
));
928 (void)memcpy(eh
->ether_shost
, &my_enaddr
, sizeof(eh
->ether_shost
));
929 eh
->ether_type
= htons(ETHERTYPE_ARP
);
930 (void)memcpy(&pkt
.data
[pkt
.off
], ea
, sizeof(*ea
));
931 pkt
.off
-= (unsigned int)sizeof (struct kdp_ether_header
);
932 /* pkt.len is still the length we want, ether_header+ether_arp */
933 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
940 struct kdp_ether_header
*eh
= NULL
;
941 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
942 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
943 static int msg_printed
;
946 kdp_panic("kdp_poll");
948 if (!kdp_en_recv_pkt
|| !kdp_en_send_pkt
) {
949 if( msg_printed
== 0) {
951 printf("kdp_poll: no debugger device\n");
956 pkt
.off
= pkt
.len
= 0;
957 kdp_receive_data(pkt
.data
, &pkt
.len
, 3/* ms */);
962 if (pkt
.len
>= sizeof(struct kdp_ether_header
))
964 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
966 if (kdp_flag
& KDP_ARP
)
968 if (ntohs(eh
->ether_type
) == ETHERTYPE_ARP
)
976 if (pkt
.len
< (sizeof (struct kdp_ether_header
) + sizeof (struct kdp_udpiphdr
)))
979 pkt
.off
+= (unsigned int)sizeof (struct kdp_ether_header
);
980 if (ntohs(eh
->ether_type
) != ETHERTYPE_IP
) {
985 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
986 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
988 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
989 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
992 pkt
.off
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
993 if (ui
->ui_pr
!= IPPROTO_UDP
) {
997 if (ip
->ip_hl
> (sizeof (struct kdp_ip
) >> 2)) {
1001 if (ntohs(ui
->ui_dport
) != KDP_REMOTE_PORT
) {
1002 if (panicd_port
== (ntohs(ui
->ui_dport
)) &&
1003 flag_panic_dump_in_progress
) {
1004 last_panic_port
= ui
->ui_sport
;
1009 /* If we receive a kernel debugging packet whilst a
1010 * core dump is in progress, abort the transfer and
1011 * enter the debugger if not told otherwise.
1014 if (flag_panic_dump_in_progress
)
1016 if (!flag_dont_abort_panic_dump
) {
1017 abort_panic_transfer();
1022 if (!kdp
.is_conn
&& !flag_panic_dump_in_progress
) {
1023 enaddr_copy(eh
->ether_dhost
, &adr
.loc
.ea
);
1024 adr
.loc
.in
= ui
->ui_dst
;
1026 enaddr_copy(eh
->ether_shost
, &adr
.rmt
.ea
);
1027 adr
.rmt
.in
= ui
->ui_src
;
1031 * Calculate kdp packet length.
1033 pkt
.len
= ntohs((u_short
)ui
->ui_ulen
) - (unsigned int)sizeof (struct kdp_udphdr
);
1037 /* Create and transmit an ARP resolution request for the target IP address.
1038 * This is modeled on ether_inet_arp()/RFC 826.
1042 transmit_ARP_request(uint32_t ip_addr
)
1044 struct kdp_ether_header
*eh
= (struct kdp_ether_header
*) &pkt
.data
[0];
1045 struct kdp_ether_arp
*ea
= (struct kdp_ether_arp
*) &pkt
.data
[sizeof(struct kdp_ether_header
)];
1047 KDP_DEBUG("Transmitting ARP request\n");
1048 /* Populate the ether_header */
1049 eh
->ether_type
= htons(ETHERTYPE_ARP
);
1050 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
1051 enaddr_copy(ðerbroadcastaddr
, eh
->ether_dhost
);
1053 /* Populate the ARP header */
1054 ea
->arp_pro
= htons(ETHERTYPE_IP
);
1055 ea
->arp_hln
= sizeof(ea
->arp_sha
);
1056 ea
->arp_pln
= sizeof(ea
->arp_spa
);
1057 ea
->arp_hrd
= htons(ARPHRD_ETHER
);
1058 ea
->arp_op
= htons(ARPOP_REQUEST
);
1061 enaddr_copy(ðerbroadcastaddr
, ea
->arp_tha
);
1062 memcpy(ea
->arp_tpa
, (void *) &ip_addr
, sizeof(ip_addr
));
1065 enaddr_copy(&kdp_current_mac_address
, ea
->arp_sha
);
1066 memcpy(ea
->arp_spa
, (void *) &kdp_current_ip_address
, sizeof(kdp_current_ip_address
));
1069 pkt
.len
= sizeof(struct kdp_ether_header
) + sizeof(struct kdp_ether_arp
);
1071 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
1075 kdp_arp_resolve(uint32_t arp_target_ip
, struct kdp_ether_addr
*resolved_MAC
)
1077 int poll_count
= 256; /* ~770 ms modulo broadcast/delayed traffic? */
1080 #define NUM_ARP_TX_RETRIES 5
1082 target_ip
= arp_target_ip
;
1083 flag_arp_resolved
= FALSE
;
1086 pkt
.off
= pkt
.len
= 0;
1090 if (tretries
>= NUM_ARP_TX_RETRIES
) {
1094 KDP_DEBUG("ARP TX attempt #%d \n", tretries
);
1096 transmit_ARP_request(arp_target_ip
);
1098 while (!pkt
.input
&& !flag_arp_resolved
&& flag_panic_dump_in_progress
&& --poll_count
) {
1102 if (flag_arp_resolved
) {
1103 *resolved_MAC
= current_resolved_MAC
;
1107 if (!flag_panic_dump_in_progress
|| pkt
.input
) /* we received a debugging packet, bail*/
1109 printf("Received a debugger packet,transferring control to debugger\n");
1110 /* Indicate that we should wait in the debugger when we return */
1111 kdp_flag
|= DBG_POST_CORE
;
1115 else /* We timed out */
1116 if (0 == poll_count
) {
1118 goto TRANSMIT_RETRY
;
1128 unsigned short reply_port
;
1129 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
1131 kdp
.saved_state
= saved_state
; // see comment in kdp_raise_exception
1138 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
1140 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
1143 // ignore replies -- we're not expecting them anyway.
1144 if (hdr
->is_reply
) {
1148 if (hdr
->request
== KDP_REATTACH
)
1149 exception_seq
= hdr
->seq
;
1151 // check for retransmitted request
1152 if (hdr
->seq
== (exception_seq
- 1)) {
1153 /* retransmit last reply */
1154 kdp_send_data(&saved_reply
.data
[saved_reply
.off
],
1157 } else if ((hdr
->seq
!= exception_seq
) &&
1158 (hdr
->request
!= KDP_CONNECT
)) {
1159 printf("kdp: bad sequence %d (want %d)\n",
1160 hdr
->seq
, exception_seq
);
1164 /* This is a manual side-channel to the main KDP protocol.
1165 * A client like GDB/kgmacros can manually construct
1166 * a request, set the input flag, issue a dummy KDP request,
1167 * and then manually collect the result
1169 if (manual_pkt
.input
) {
1170 kdp_hdr_t
*manual_hdr
= (kdp_hdr_t
*)&manual_pkt
.data
;
1171 unsigned short manual_port_unused
= 0;
1172 if (!manual_hdr
->is_reply
) {
1174 kdp_packet((unsigned char *)&manual_pkt
.data
,
1175 (int *)&manual_pkt
.len
,
1176 &manual_port_unused
);
1178 manual_pkt
.input
= 0;
1181 if (kdp_packet((unsigned char*)&pkt
.data
[pkt
.off
],
1183 (unsigned short *)&reply_port
)) {
1184 boolean_t sideband
= FALSE
;
1186 /* if it's an already connected error message,
1187 * send a sideband reply for that. for successful connects,
1188 * make sure the sequence number is correct. */
1189 if (hdr
->request
== KDP_CONNECT
) {
1190 kdp_connect_reply_t
*rp
=
1191 (kdp_connect_reply_t
*) &pkt
.data
[pkt
.off
];
1192 kdp_error_t err
= rp
->error
;
1194 if (err
== KDPERR_NO_ERROR
) {
1195 exception_seq
= hdr
->seq
;
1196 } else if (err
== KDPERR_ALREADY_CONNECTED
) {
1201 kdp_reply(reply_port
, sideband
);
1206 } while (kdp
.is_halted
);
1210 kdp_connection_wait(void)
1212 unsigned short reply_port
;
1213 struct kdp_ether_addr kdp_mac_addr
= kdp_get_mac_addr();
1214 unsigned int ip_addr
= ntohl(kdp_get_ip_address());
1217 * Do both a printf() and a kprintf() of the MAC and IP so that
1218 * they will print out on headless machines but not be added to
1222 if (KDP_SERIAL_ENABLED()) {
1223 printf("Using serial KDP.\n");
1224 kprintf("Using serial KDP.\n");
1226 printf( "ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1227 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
1228 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
1229 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
1230 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
1231 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
1232 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
1234 kprintf( "ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1235 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
1236 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
1237 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
1238 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
1239 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
1240 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
1242 printf( "ip address: %d.%d.%d.%d\n",
1243 (ip_addr
& 0xff000000) >> 24,
1244 (ip_addr
& 0xff0000) >> 16,
1245 (ip_addr
& 0xff00) >> 8,
1248 kprintf( "ip address: %d.%d.%d.%d\n",
1249 (ip_addr
& 0xff000000) >> 24,
1250 (ip_addr
& 0xff0000) >> 16,
1251 (ip_addr
& 0xff00) >> 8,
1255 printf("\nWaiting for remote debugger connection.\n");
1256 kprintf("\nWaiting for remote debugger connection.\n");
1259 if (reattach_wait
== 0) {
1260 if((kdp_flag
& KDP_GETC_ENA
) && (0 != kdp_getc()))
1262 printf("Options..... Type\n");
1263 printf("------------ ----\n");
1264 printf("continue.... 'c'\n");
1265 printf("reboot...... 'r'\n");
1273 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
1275 while (!pkt
.input
) {
1276 if (kdp_flag
& KDP_GETC_ENA
) {
1277 switch(kdp_getc()) {
1279 printf("Continuing...\n");
1282 printf("Rebooting...\n");
1283 kdp_machine_reboot();
1293 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
1295 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
1297 if (hdr
->request
== KDP_HOSTREBOOT
) {
1298 kdp_machine_reboot();
1299 /* should not return! */
1301 if (((hdr
->request
== KDP_CONNECT
) || (hdr
->request
== KDP_REATTACH
)) &&
1302 !hdr
->is_reply
&& (hdr
->seq
== exception_seq
)) {
1303 if (kdp_packet((unsigned char *)&pkt
.data
[pkt
.off
],
1305 (unsigned short *)&reply_port
))
1306 kdp_reply(reply_port
, FALSE
);
1307 if (hdr
->request
== KDP_REATTACH
) {
1309 hdr
->request
=KDP_DISCONNECT
;
1315 } while (!kdp
.is_conn
);
1317 if (current_debugger
== KDP_CUR_DB
)
1319 printf("Connected to remote debugger.\n");
1320 kprintf("Connected to remote debugger.\n");
1325 unsigned int exception
,
1327 unsigned int subcode
1330 unsigned short remote_port
;
1331 unsigned int timeout_count
= 100;
1332 unsigned int poll_timeout
;
1335 pkt
.off
= sizeof (struct kdp_ether_header
) + sizeof (struct kdp_udpiphdr
);
1336 kdp_exception((unsigned char *)&pkt
.data
[pkt
.off
],
1338 (unsigned short *)&remote_port
,
1339 (unsigned int)exception
,
1341 (unsigned int)subcode
);
1343 kdp_send(remote_port
);
1346 while(!pkt
.input
&& poll_timeout
)
1353 if (!kdp_exception_ack(&pkt
.data
[pkt
.off
], pkt
.len
)) {
1360 if (kdp
.exception_ack_needed
)
1361 kdp_us_spin(250000);
1363 } while (kdp
.exception_ack_needed
&& timeout_count
--);
1365 if (kdp
.exception_ack_needed
) {
1366 // give up & disconnect
1367 printf("kdp: exception ack timeout\n");
1368 if (current_debugger
== KDP_CUR_DB
)
1375 kdp_raise_exception(
1376 unsigned int exception
,
1378 unsigned int subcode
,
1383 unsigned int initial_not_in_kdp
= not_in_kdp
;
1386 /* Was a system trace requested ? */
1387 if (kdp_snapshot
&& (!panic_active()) && (panic_caller
== 0)) {
1388 stack_snapshot_ret
= kdp_stackshot(stack_snapshot_pid
,
1389 stack_snapshot_buf
, stack_snapshot_bufsize
,
1390 stack_snapshot_flags
, stack_snapshot_dispatch_offset
,
1391 &stack_snapshot_bytes_traced
);
1392 not_in_kdp
= initial_not_in_kdp
;
1396 disable_preemption();
1398 if (saved_state
== 0)
1399 printf("kdp_raise_exception with NULL state\n");
1402 if (exception
!= EXC_BREAKPOINT
) {
1403 if (exception
> EXC_BREAKPOINT
|| exception
< EXC_BAD_ACCESS
) {
1406 printf("%s exception (%x,%x,%x)\n",
1407 exception_message
[index
],
1408 exception
, code
, subcode
);
1413 /* XXX WMG it seems that sometimes it doesn't work to let kdp_handler
1414 * do this. I think the client and the host can get out of sync.
1416 kdp
.saved_state
= saved_state
;
1417 kdp
.kdp_cpu
= cpu_number();
1418 kdp
.kdp_thread
= current_thread();
1421 (*kdp_en_setmode
)(TRUE
); /* enabling link mode */
1424 kdp_panic("kdp_raise_exception");
1426 if (((kdp_flag
& KDP_PANIC_DUMP_ENABLED
) || (kdp_flag
& PANIC_LOG_DUMP
))
1427 && (panicstr
!= (char *) 0)) {
1429 if (kdp_flag
& REBOOT_POST_CORE
)
1430 kdp_machine_reboot();
1433 if ((kdp_flag
& PANIC_CORE_ON_NMI
) && (panicstr
== (char *) 0) &&
1436 disable_debug_output
= disableConsoleOutput
= FALSE
;
1439 if (!(kdp_flag
& DBG_POST_CORE
))
1440 goto exit_raise_exception
;
1445 kdp_connection_wait();
1447 kdp_send_exception(exception
, code
, subcode
);
1448 if (kdp
.exception_ack_needed
) {
1449 kdp
.exception_ack_needed
= FALSE
;
1450 kdp_remove_all_breakpoints();
1451 printf("Remote debugger disconnected.\n");
1456 kdp
.is_halted
= TRUE
; /* XXX */
1457 kdp_handler(saved_state
);
1460 kdp_remove_all_breakpoints();
1461 printf("Remote debugger disconnected.\n");
1464 /* Allow triggering a panic core dump when connected to the machine
1465 * Continuing after setting kdp_trigger_core_dump should do the
1469 if (1 == kdp_trigger_core_dump
) {
1470 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
1472 if (kdp_flag
& REBOOT_POST_CORE
)
1473 kdp_machine_reboot();
1474 kdp_trigger_core_dump
= 0;
1477 /* Trigger a reboot if the user has set this flag through the
1478 * debugger.Ideally, this would be done through the HOSTREBOOT packet
1479 * in the protocol,but that will need gdb support,and when it's
1480 * available, it should work automatically.
1482 if (1 == flag_kdp_trigger_reboot
) {
1483 kdp_machine_reboot();
1484 /* If we're still around, reset the flag */
1485 flag_kdp_trigger_reboot
= 0;
1488 if (kdp_reentry_deadline
) {
1489 kdp_schedule_debugger_reentry(kdp_reentry_deadline
);
1490 printf("Debugger re-entry scheduled in %d milliseconds\n", kdp_reentry_deadline
);
1491 kdp_reentry_deadline
= 0;
1496 if (reattach_wait
== 1)
1499 exit_raise_exception
:
1501 (*kdp_en_setmode
)(FALSE
); /* link cleanup */
1503 not_in_kdp
= initial_not_in_kdp
;
1505 enable_preemption();
1511 kdp
.reply_port
= kdp
.exception_port
= 0;
1512 kdp
.is_halted
= kdp
.is_conn
= FALSE
;
1513 kdp
.exception_seq
= kdp
.conn_seq
= 0;
1514 kdp
.session_key
= 0;
1515 pkt
.input
= manual_pkt
.input
= FALSE
;
1516 pkt
.len
= pkt
.off
= manual_pkt
.len
= 0;
1520 create_panic_header(unsigned int request
, const char *corename
,
1521 unsigned length
, unsigned int block
)
1523 struct kdp_udpiphdr aligned_ui
, *ui
= &aligned_ui
;
1524 struct kdp_ip aligned_ip
, *ip
= &aligned_ip
;
1525 struct kdp_ether_header
*eh
;
1526 struct corehdr
*coreh
;
1527 const char *mode
= "octet";
1528 char modelen
= strlen(mode
) + 1;
1530 size_t fmask_size
= sizeof(KDP_FEATURE_MASK_STRING
) + sizeof(kdp_crashdump_feature_mask
);
1532 pkt
.off
= sizeof (struct kdp_ether_header
);
1533 pkt
.len
= (unsigned int)(length
+ ((request
== KDP_WRQ
) ? modelen
+ fmask_size
: 0) +
1534 (corename
? (strlen(corename
) + 1 ): 0) + sizeof(struct corehdr
));
1537 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
1539 ui
= (struct kdp_udpiphdr
*)&pkt
.data
[pkt
.off
];
1541 ui
->ui_next
= ui
->ui_prev
= 0;
1543 ui
->ui_pr
= IPPROTO_UDP
;
1544 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct kdp_udphdr
));
1545 ui
->ui_src
.s_addr
= (uint32_t)kdp_current_ip_address
;
1546 /* Already in network byte order via inet_aton() */
1547 ui
->ui_dst
.s_addr
= panic_server_ip
;
1548 ui
->ui_sport
= htons(panicd_port
);
1549 ui
->ui_dport
= ((request
== KDP_WRQ
) ? htons(panicd_port
) : last_panic_port
);
1550 ui
->ui_ulen
= ui
->ui_len
;
1553 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
1554 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
1556 ip
= (struct kdp_ip
*)&pkt
.data
[pkt
.off
];
1558 ip
->ip_len
= htons(sizeof (struct kdp_udpiphdr
) + pkt
.len
);
1559 ip
->ip_v
= IPVERSION
;
1560 ip
->ip_id
= htons(ip_id
++);
1561 ip
->ip_hl
= sizeof (struct kdp_ip
) >> 2;
1562 ip
->ip_ttl
= udp_ttl
;
1564 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
1566 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
1569 pkt
.len
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
1571 pkt
.off
+= (unsigned int)sizeof (struct kdp_udpiphdr
);
1573 coreh
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1574 coreh
->th_opcode
= htons((u_short
)request
);
1576 if (request
== KDP_WRQ
)
1580 cp
= coreh
->th_u
.tu_rpl
;
1581 cp
+= strlcpy (cp
, corename
, KDP_MAXPACKET
);
1583 cp
+= strlcpy (cp
, mode
, KDP_MAXPACKET
- strlen(corename
));
1585 cp
+= strlcpy(cp
, KDP_FEATURE_MASK_STRING
, sizeof(KDP_FEATURE_MASK_STRING
));
1586 *cp
++ = '\0'; /* Redundant */
1587 bcopy(&kdp_crashdump_feature_mask
, cp
, sizeof(kdp_crashdump_feature_mask
));
1588 kdp_crashdump_pkt_size
= KDP_LARGE_CRASHDUMP_PKT_SIZE
;
1589 PE_parse_boot_argn("kdp_crashdump_pkt_size", &kdp_crashdump_pkt_size
, sizeof(kdp_crashdump_pkt_size
));
1590 cp
+= sizeof(kdp_crashdump_feature_mask
);
1591 *(uint32_t *)cp
= htonl(kdp_crashdump_pkt_size
);
1595 coreh
->th_block
= htonl((unsigned int) block
);
1598 pkt
.off
-= (unsigned int)sizeof (struct kdp_udpiphdr
);
1599 pkt
.off
-= (unsigned int)sizeof (struct kdp_ether_header
);
1601 eh
= (struct kdp_ether_header
*)&pkt
.data
[pkt
.off
];
1602 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
1603 enaddr_copy(&destination_mac
, eh
->ether_dhost
);
1604 eh
->ether_type
= htons(ETHERTYPE_IP
);
1606 pkt
.len
+= (unsigned int)sizeof (struct kdp_ether_header
);
1610 static int kdp_send_crashdump_seek(char *corename
, uint64_t seek_off
)
1614 if (kdp_feature_large_crashdumps
) {
1615 panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, corename
,
1619 uint32_t off
= (uint32_t) seek_off
;
1620 panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, corename
,
1624 if (panic_error
< 0) {
1625 printf ("kdp_send_crashdump_pkt failed with error %d\n",
1633 int kdp_send_crashdump_data(unsigned int request
, char *corename
,
1634 int64_t length
, caddr_t txstart
)
1636 int panic_error
= 0;
1638 while (length
> 0) {
1639 uint64_t chunk
= MIN(kdp_crashdump_pkt_size
, length
);
1641 panic_error
= kdp_send_crashdump_pkt(request
, corename
, chunk
,
1643 if (panic_error
< 0) {
1644 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
1654 uint32_t kdp_crashdump_short_pkt
;
1657 kdp_send_crashdump_pkt(unsigned int request
, char *corename
,
1658 uint64_t length
, void *panic_data
)
1661 struct corehdr
*th
= NULL
;
1662 char rretries
, tretries
;
1664 if (kdp_dump_start_time
== 0) {
1665 kdp_dump_start_time
= mach_absolute_time();
1666 kdp_superblock_dump_start_time
= kdp_dump_start_time
;
1669 tretries
= rretries
= 0;
1670 poll_count
= KDP_CRASHDUMP_POLL_COUNT
;
1671 pkt
.off
= pkt
.len
= 0;
1672 if (request
== KDP_WRQ
) /* longer timeout for initial request */
1678 if (tretries
>=15) {
1679 /* The crashdump server is unreachable for some reason. This could be a network
1680 * issue or, if we've been especially unfortunate, we've hit Radar 2760413,
1681 * which is a long standing problem with the IOKit polled mode network driver
1682 * shim which can prevent transmits/receives completely.
1684 printf ("Cannot contact panic server, timing out.\n");
1689 printf("TX retry #%d ", tretries
);
1691 th
= create_panic_header(request
, corename
, (unsigned)length
, panic_block
);
1693 if (request
== KDP_DATA
) {
1694 /* as all packets are kdp_crashdump_pkt_size in length, the last packet
1695 * may end up with trailing bits. make sure that those
1696 * bits aren't confusing. */
1697 if (length
< kdp_crashdump_pkt_size
) {
1698 kdp_crashdump_short_pkt
++;
1699 memset(th
->th_data
+ length
, 'Y',
1700 kdp_crashdump_pkt_size
- (uint32_t) length
);
1703 if (!kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)panic_data
, (caddr_t
) th
->th_data
, length
)) {
1704 uintptr_t next_page
= round_page((uintptr_t)panic_data
);
1705 memset((caddr_t
) th
->th_data
, 'X', (size_t)length
);
1706 if ((next_page
- ((uintptr_t) panic_data
)) < length
) {
1707 uint64_t resid
= length
- (next_page
- (intptr_t) panic_data
);
1708 if (!kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)next_page
, (caddr_t
) th
->th_data
+ (length
- resid
), resid
)) {
1709 memset((caddr_t
) th
->th_data
+ (length
- resid
), 'X', (size_t)resid
);
1714 else if (request
== KDP_SEEK
) {
1715 if (kdp_feature_large_crashdumps
)
1716 *(uint64_t *) th
->th_data
= OSSwapHostToBigInt64((*(uint64_t *) panic_data
));
1718 *(unsigned int *) th
->th_data
= htonl(*(unsigned int *) panic_data
);
1721 kdp_send_data(&pkt
.data
[pkt
.off
], pkt
.len
);
1723 /* Listen for the ACK */
1725 while (!pkt
.input
&& flag_panic_dump_in_progress
&& poll_count
) {
1734 th
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1735 if (request
== KDP_WRQ
) {
1736 uint16_t opcode64
= ntohs(th
->th_opcode
);
1737 uint16_t features64
= (opcode64
& 0xFF00)>>8;
1738 if ((opcode64
& 0xFF) == KDP_ACK
) {
1739 kdp_feature_large_crashdumps
= features64
& KDP_FEATURE_LARGE_CRASHDUMPS
;
1740 if (features64
& KDP_FEATURE_LARGE_PKT_SIZE
) {
1741 kdp_feature_large_pkt_size
= 1;
1744 kdp_feature_large_pkt_size
= 0;
1745 kdp_crashdump_pkt_size
= 512;
1747 printf("Protocol features: 0x%x\n", (uint32_t) features64
);
1748 th
->th_opcode
= htons(KDP_ACK
);
1751 if (ntohs(th
->th_opcode
) == KDP_ACK
&& ntohl(th
->th_block
) == panic_block
) {
1754 if (ntohs(th
->th_opcode
) == KDP_ERROR
) {
1755 printf("Panic server returned error %d, retrying\n", ntohl(th
->th_code
));
1757 goto TRANSMIT_RETRY
;
1760 if (ntohl(th
->th_block
) == (panic_block
- 1)) {
1761 printf("RX retry ");
1763 goto TRANSMIT_RETRY
;
1769 if (!flag_panic_dump_in_progress
) /* we received a debugging packet, bail*/
1771 printf("Received a debugger packet,transferring control to debugger\n");
1772 /* Configure that if not set ..*/
1773 kdp_flag
|= DBG_POST_CORE
;
1776 else /* We timed out */
1777 if (0 == poll_count
) {
1779 kdp_us_spin ((tretries%4
) * panic_timeout
); /* capped linear backoff */
1780 goto TRANSMIT_RETRY
;
1783 if (!(++panic_block
% SBLOCKSZ
)) {
1785 kdb_printf_unbuffered(".");
1786 ctime
= mach_absolute_time();
1787 kdp_superblock_dump_time
= ctime
- kdp_superblock_dump_start_time
;
1788 kdp_superblock_dump_start_time
= ctime
;
1789 if (kdp_superblock_dump_time
> kdp_max_superblock_dump_time
)
1790 kdp_max_superblock_dump_time
= kdp_superblock_dump_time
;
1791 if (kdp_superblock_dump_time
< kdp_min_superblock_dump_time
)
1792 kdp_min_superblock_dump_time
= kdp_superblock_dump_time
;
1795 if (request
== KDP_EOF
) {
1796 printf("\nTotal number of packets transmitted: %d\n", panic_block
);
1797 printf("Avg. superblock transfer abstime 0x%llx\n", ((mach_absolute_time() - kdp_dump_start_time
) / panic_block
) * SBLOCKSZ
);
1798 printf("Minimum superblock transfer abstime: 0x%llx\n", kdp_min_superblock_dump_time
);
1799 printf("Maximum superblock transfer abstime: 0x%llx\n", kdp_max_superblock_dump_time
);
1807 return ((c
> 47) && (c
< 58));
1810 /* Horrid hack to extract xnu version if possible - a much cleaner approach
1811 * would be to have the integrator run a script which would copy the
1812 * xnu version into a string or an int somewhere at project submission
1813 * time - makes assumptions about sizeof(version), but will not fail if
1814 * it changes, but may be incorrect.
1816 /* 2006: Incorporated a change from Darwin user P. Lovell to extract
1817 * the minor kernel version numbers from the version string.
1820 kdp_get_xnu_version(char *versionbuf
)
1827 strlcpy(vstr
, "custom", 10);
1828 if (kdp_machine_vm_read((mach_vm_address_t
)(uintptr_t)version
, versionbuf
, 128)) {
1829 versionbuf
[127] = '\0';
1830 versionpos
= strnstr(versionbuf
, "xnu-", 115);
1832 strncpy(vstr
, versionpos
, sizeof(vstr
));
1833 vstr
[sizeof(vstr
)-1] = '\0';
1834 vptr
= vstr
+ 4; /* Begin after "xnu-" */
1835 while (*vptr
&& (isdigit(*vptr
) || *vptr
== '.'))
1838 /* Remove trailing period, if any */
1839 if (*(--vptr
) == '.')
1844 strlcpy(versionbuf
, vstr
, KDP_MAXPACKET
);
1849 kdp_set_dump_info(const uint32_t flags
, const char *filename
,
1850 const char *destipstr
, const char *routeripstr
,
1851 const uint32_t port
)
1855 if (destipstr
&& (destipstr
[0] != '\0')) {
1856 strlcpy(panicd_ip_str
, destipstr
, sizeof(panicd_ip_str
));
1857 panicd_specified
= 1;
1860 if (routeripstr
&& (routeripstr
[0] != '\0')) {
1861 strlcpy(router_ip_str
, routeripstr
, sizeof(router_ip_str
));
1862 router_specified
= 1;
1865 if (filename
&& (filename
[0] != '\0')) {
1866 strlcpy(corename_str
, filename
, sizeof(corename_str
));
1867 corename_specified
= TRUE
;
1869 corename_specified
= FALSE
;
1875 /* on a disconnect, should we stay in KDP or not? */
1876 noresume_on_disconnect
= (flags
& KDP_DUMPINFO_NORESUME
) ? 1 : 0;
1878 if ((flags
& KDP_DUMPINFO_DUMP
) == 0)
1881 /* the rest of the commands can modify kdp_flags */
1882 cmd
= flags
& KDP_DUMPINFO_MASK
;
1883 if (cmd
== KDP_DUMPINFO_DISABLE
) {
1884 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
1885 panicd_specified
= 0;
1886 kdp_trigger_core_dump
= 0;
1890 kdp_flag
&= ~REBOOT_POST_CORE
;
1891 if (flags
& KDP_DUMPINFO_REBOOT
)
1892 kdp_flag
|= REBOOT_POST_CORE
;
1894 kdp_flag
&= ~PANIC_LOG_DUMP
;
1895 if (cmd
== KDP_DUMPINFO_PANICLOG
)
1896 kdp_flag
|= PANIC_LOG_DUMP
;
1898 kdp_flag
&= ~SYSTEM_LOG_DUMP
;
1899 if (cmd
== KDP_DUMPINFO_SYSTEMLOG
)
1900 kdp_flag
|= SYSTEM_LOG_DUMP
;
1902 /* trigger a dump */
1903 kdp_flag
|= DBG_POST_CORE
;
1905 flag_dont_abort_panic_dump
= (flags
& KDP_DUMPINFO_NOINTR
) ?
1909 logPanicDataToScreen
= 1;
1910 disableConsoleOutput
= 0;
1911 disable_debug_output
= 0;
1912 kdp_trigger_core_dump
= 1;
1916 kdp_get_dump_info(uint32_t *flags
, char *filename
, char *destipstr
,
1917 char *routeripstr
, uint32_t *port
)
1920 if (panicd_specified
)
1921 strlcpy(destipstr
, panicd_ip_str
,
1922 sizeof(panicd_ip_str
));
1924 destipstr
[0] = '\0';
1928 if (router_specified
)
1929 strlcpy(routeripstr
, router_ip_str
,
1930 sizeof(router_ip_str
));
1932 routeripstr
[0] = '\0';
1936 if (corename_specified
)
1937 strlcpy(filename
, corename_str
,
1938 sizeof(corename_str
));
1945 *port
= panicd_port
;
1949 if (!panicd_specified
)
1950 *flags
|= KDP_DUMPINFO_DISABLE
;
1951 else if (kdp_flag
& PANIC_LOG_DUMP
)
1952 *flags
|= KDP_DUMPINFO_PANICLOG
;
1954 *flags
|= KDP_DUMPINFO_CORE
;
1956 if (noresume_on_disconnect
)
1957 *flags
|= KDP_DUMPINFO_NORESUME
;
1962 /* Primary dispatch routine for the system dump */
1964 kdp_panic_dump(void)
1966 char coreprefix
[10];
1970 uint32_t current_ip
= ntohl((uint32_t)kdp_current_ip_address
);
1972 if (flag_panic_dump_in_progress
) {
1973 kdb_printf("System dump aborted.\n");
1974 goto panic_dump_exit
;
1977 printf("Entering system dump routine\n");
1979 if (!kdp_en_recv_pkt
|| !kdp_en_send_pkt
) {
1980 kdb_printf("Error: No transport device registered for kernel crashdump\n");
1984 if (!panicd_specified
) {
1985 kdb_printf("A dump server was not specified in the boot-args, terminating kernel core dump.\n");
1986 goto panic_dump_exit
;
1989 flag_panic_dump_in_progress
= TRUE
;
1992 kdp_panic("kdp_panic_dump: unexpected pending input packet");
1994 kdp_get_xnu_version((char *) &pkt
.data
[0]);
1996 if (!corename_specified
) {
1997 /* Panic log bit takes precedence over core dump bit */
1998 if ((panicstr
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
))
1999 strlcpy(coreprefix
, "paniclog", sizeof(coreprefix
));
2000 else if (kdp_flag
& SYSTEM_LOG_DUMP
)
2001 strlcpy(coreprefix
, "systemlog", sizeof(coreprefix
));
2003 strlcpy(coreprefix
, "core", sizeof(coreprefix
));
2005 abstime
= mach_absolute_time();
2006 pkt
.data
[20] = '\0';
2007 snprintf (corename_str
, sizeof(corename_str
), "%s-%s-%d.%d.%d.%d-%x",
2008 coreprefix
, &pkt
.data
[0],
2009 (current_ip
& 0xff000000) >> 24,
2010 (current_ip
& 0xff0000) >> 16,
2011 (current_ip
& 0xff00) >> 8,
2012 (current_ip
& 0xff),
2013 (unsigned int) (abstime
& 0xffffffff));
2016 if (0 == inet_aton(panicd_ip_str
, (struct kdp_in_addr
*) &panic_server_ip
)) {
2017 kdb_printf("inet_aton() failed interpreting %s as a panic server IP\n", panicd_ip_str
);
2020 kdb_printf("Attempting connection to panic server configured at IP %s, port %d\n", panicd_ip_str
, panicd_port
);
2022 destination_mac
= router_mac
;
2024 if (kdp_arp_resolve(panic_server_ip
, &temp_mac
)) {
2025 kdb_printf("Resolved %s's (or proxy's) link level address\n", panicd_ip_str
);
2026 destination_mac
= temp_mac
;
2029 if (!flag_panic_dump_in_progress
) goto panic_dump_exit
;
2030 if (router_specified
) {
2031 if (0 == inet_aton(router_ip_str
, (struct kdp_in_addr
*) &parsed_router_ip
))
2032 kdb_printf("inet_aton() failed interpreting %s as an IP\n", router_ip_str
);
2034 router_ip
= parsed_router_ip
;
2035 if (kdp_arp_resolve(router_ip
, &temp_mac
)) {
2036 destination_mac
= temp_mac
;
2037 kdb_printf("Routing through specified router IP %s (%d)\n", router_ip_str
, router_ip
);
2043 if (!flag_panic_dump_in_progress
) goto panic_dump_exit
;
2045 kdb_printf("Transmitting packets to link level address: %02x:%02x:%02x:%02x:%02x:%02x\n",
2046 destination_mac
.ether_addr_octet
[0] & 0xff,
2047 destination_mac
.ether_addr_octet
[1] & 0xff,
2048 destination_mac
.ether_addr_octet
[2] & 0xff,
2049 destination_mac
.ether_addr_octet
[3] & 0xff,
2050 destination_mac
.ether_addr_octet
[4] & 0xff,
2051 destination_mac
.ether_addr_octet
[5] & 0xff);
2053 kdb_printf("Kernel map size is %llu\n", (unsigned long long) get_vmmap_size(kernel_map
));
2054 kdb_printf("Sending write request for %s\n", corename_str
);
2056 if ((panic_error
= kdp_send_crashdump_pkt(KDP_WRQ
, corename_str
, 0 , NULL
)) < 0) {
2057 kdb_printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
2058 goto panic_dump_exit
;
2061 /* Just the panic log requested */
2062 if ((panicstr
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
)) {
2063 kdb_printf_unbuffered("Transmitting panic log, please wait: ");
2064 kdp_send_crashdump_data(KDP_DATA
, corename_str
,
2065 debug_buf_ptr
- debug_buf
,
2067 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
2068 printf("Please file a bug report on this panic, if possible.\n");
2069 goto panic_dump_exit
;
2072 /* maybe we wanted the systemlog */
2073 if (kdp_flag
& SYSTEM_LOG_DUMP
) {
2074 long start_off
= msgbufp
->msg_bufx
;
2077 kdb_printf_unbuffered("Transmitting system log, please wait: ");
2078 if (start_off
>= msgbufp
->msg_bufr
) {
2079 len
= msgbufp
->msg_size
- start_off
;
2080 kdp_send_crashdump_data(KDP_DATA
, corename_str
, len
,
2081 msgbufp
->msg_bufc
+ start_off
);
2082 /* seek to remove trailing bytes */
2083 kdp_send_crashdump_seek(corename_str
, len
);
2087 if (start_off
!= msgbufp
->msg_bufr
) {
2088 len
= msgbufp
->msg_bufr
- start_off
;
2089 kdp_send_crashdump_data(KDP_DATA
, corename_str
, len
,
2090 msgbufp
->msg_bufc
+ start_off
);
2093 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
2094 goto panic_dump_exit
;
2097 /* We want a core dump if we're here */
2101 abort_panic_transfer();
2107 abort_panic_transfer(void)
2109 flag_panic_dump_in_progress
= FALSE
;
2110 flag_dont_abort_panic_dump
= FALSE
;
2114 #if CONFIG_SERIAL_KDP
2116 static boolean_t needs_serial_init
= TRUE
;
2119 kdp_serial_send(void *rpkt
, unsigned int rpkt_len
)
2122 kdp_serialize_packet((unsigned char *)rpkt
, rpkt_len
, pal_serial_putc
);
2126 kdp_serial_receive(void *rpkt
, unsigned int *rpkt_len
, unsigned int timeout
)
2129 uint64_t now
, deadline
;
2131 clock_interval_to_deadline(timeout
, 1000 * 1000 /* milliseconds */, &deadline
);
2134 for(clock_get_uptime(&now
); now
< deadline
; clock_get_uptime(&now
))
2136 readkar
= pal_serial_getc();
2139 unsigned char *packet
;
2140 // printf("got char %02x\n", readkar);
2141 if((packet
= kdp_unserialize_packet(readkar
,rpkt_len
)))
2143 memcpy(rpkt
, packet
, *rpkt_len
);
2152 kdp_serial_setmode(boolean_t active
)
2154 if (active
== FALSE
) /* leaving KDP */
2157 if (!needs_serial_init
)
2161 needs_serial_init
= FALSE
;
2166 static void kdp_serial_callout(__unused
void *arg
, kdp_event_t event
)
2168 /* When we stop KDP, set the bit to re-initialize the console serial port
2169 * the next time we send/receive a KDP packet. We don't do it on
2170 * KDP_EVENT_ENTER directly because it also gets called when we trap to KDP
2171 * for non-external debugging, i.e., stackshot or core dumps.
2173 * Set needs_serial_init on exit (and initialization, see above) and not
2174 * enter because enter is sent multiple times and causes excess reinitialization.
2179 case KDP_EVENT_PANICLOG
:
2180 case KDP_EVENT_ENTER
:
2182 case KDP_EVENT_EXIT
:
2183 needs_serial_init
= TRUE
;
2188 #endif /* CONFIG_SERIAL_KDP */
2193 strlcpy(kdp_kernelversion_string
, version
, sizeof(kdp_kernelversion_string
));
2195 /* Relies on platform layer calling panic_init() before kdp_init() */
2196 if (kernel_uuid_string
[0] != '\0') {
2198 * Update kdp_kernelversion_string with our UUID
2199 * generated at link time.
2202 strlcat(kdp_kernelversion_string
, "; UUID=", sizeof(kdp_kernelversion_string
));
2203 strlcat(kdp_kernelversion_string
, kernel_uuid_string
, sizeof(kdp_kernelversion_string
));
2208 #if defined(__x86_64__) || defined(__arm__)
2209 if (vm_kernel_slide
) {
2210 char KASLR_stext
[19];
2211 strlcat(kdp_kernelversion_string
, "; stext=", sizeof(kdp_kernelversion_string
));
2212 snprintf(KASLR_stext
, sizeof(KASLR_stext
), "%p", (void *) vm_kernel_stext
);
2213 strlcat(kdp_kernelversion_string
, KASLR_stext
, sizeof(kdp_kernelversion_string
));
2217 if (debug_boot_arg
& DB_REBOOT_POST_CORE
)
2218 kdp_flag
|= REBOOT_POST_CORE
;
2219 #if defined(__x86_64__)
2223 kdp_timer_callout_init();
2224 kdp_crashdump_feature_mask
= htonl(kdp_crashdump_feature_mask
);
2226 #if CONFIG_SERIAL_KDP
2228 struct kdp_in_addr ipaddr
;
2229 struct kdp_ether_addr macaddr
;
2232 // serial must be explicitly requested
2233 if(!PE_parse_boot_argn("kdp_match_name", kdpname
, sizeof(kdpname
)) || strncmp(kdpname
, "serial", sizeof(kdpname
)) != 0)
2236 kprintf("Initializing serial KDP\n");
2238 kdp_register_callout(kdp_serial_callout
, NULL
);
2239 kdp_register_link(NULL
, kdp_serial_setmode
);
2240 kdp_register_send_receive(kdp_serial_send
, kdp_serial_receive
);
2242 /* fake up an ip and mac for early serial debugging */
2243 macaddr
.ether_addr_octet
[0] = 's';
2244 macaddr
.ether_addr_octet
[1] = 'e';
2245 macaddr
.ether_addr_octet
[2] = 'r';
2246 macaddr
.ether_addr_octet
[3] = 'i';
2247 macaddr
.ether_addr_octet
[4] = 'a';
2248 macaddr
.ether_addr_octet
[5] = 'l';
2249 ipaddr
.s_addr
= KDP_SERIAL_IPADDR
;
2250 kdp_set_ip_and_mac_addresses(&ipaddr
, &macaddr
);
2252 #endif /* CONFIG_SERIAL_KDP */