]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kdp/kdp_udp.c
xnu-2422.90.20.tar.gz
[apple/xnu.git] / osfmk / kdp / kdp_udp.c
1 /*
2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * Copyright (c) 1982, 1986, 1993
31 * The Regents of the University of California. All rights reserved.
32 */
33
34 /*
35 * Kernel Debugging Protocol UDP implementation.
36 */
37
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>
44
45 #include <kdp/kdp_core.h>
46 #include <kdp/kdp_internal.h>
47 #include <kdp/kdp_en_debugger.h>
48 #include <kdp/kdp_callout.h>
49 #include <kdp/kdp_udp.h>
50 #if CONFIG_SERIAL_KDP
51 #include <kdp/kdp_serial.h>
52 #endif
53
54 #include <vm/vm_map.h>
55 #include <vm/vm_protos.h>
56 #include <vm/vm_kern.h> /* kernel_map */
57
58 #include <mach/memory_object_types.h>
59 #include <machine/pal_routines.h>
60
61 #include <sys/msgbuf.h>
62
63 /* we just want the link status flags, so undef KERNEL_PRIVATE for this
64 * header file. */
65 #undef KERNEL_PRIVATE
66 #include <net/if_media.h>
67 #define KERNEL_PRIVATE
68
69 #include <string.h>
70
71 #include <IOKit/IOPlatformExpert.h>
72 #include <libkern/version.h>
73
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 */
77
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)
81
82 extern int kdp_getc(void);
83 extern int reattach_wait;
84
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);
90
91 static u_short ip_id; /* ip packet ctr, for ids */
92
93 /* @(#)udp_usrreq.c 2.2 88/05/23 4.0NFSSRC SMI; from UCB 7.1 6/5/86 */
94
95 /*
96 * UDP protocol implementation.
97 * Per RFC 768, August, 1980.
98 */
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;
102
103 struct kdp_ipovly {
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 */
110 };
111
112 struct kdp_udphdr {
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 */
117 };
118
119 struct kdp_udpiphdr {
120 struct kdp_ipovly ui_i; /* overlaid ip structure */
121 struct kdp_udphdr ui_u; /* udp header */
122 };
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
134
135 struct kdp_ip {
136 union {
137 uint32_t ip_w;
138 struct {
139 unsigned int
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 */
145 #endif
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 */
151 #endif
152 } ip_x;
153 } ip_vhltl;
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 */
163 };
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
168
169 #define IPPROTO_UDP 17
170 #define IPVERSION 4
171
172 #define ETHERTYPE_IP 0x0800 /* IP protocol */
173
174 /*
175 * Ethernet Address Resolution Protocol.
176 *
177 * See RFC 826 for protocol description. Structure below is adapted
178 * to resolving internet addresses. Field names used correspond to
179 * RFC 826.
180 */
181
182 #define ETHERTYPE_ARP 0x0806 /* Addr. resolution protocol */
183
184 struct kdp_arphdr {
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 */
198 };
199
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 */
206 };
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
212
213 #define ETHERMTU 1500
214 #define ETHERHDRSIZE 14
215 #define ETHERCRC 4
216 #define KDP_MAXPACKET (ETHERHDRSIZE + ETHERMTU + ETHERCRC)
217
218 static struct {
219 unsigned char data[KDP_MAXPACKET];
220 unsigned int off, len;
221 boolean_t input;
222 } pkt, saved_reply;
223
224 struct kdp_manual_pkt manual_pkt;
225
226 struct {
227 struct {
228 struct kdp_in_addr in;
229 struct kdp_ether_addr ea;
230 } loc;
231 struct {
232 struct kdp_in_addr in;
233 struct kdp_ether_addr ea;
234 } rmt;
235 } adr;
236
237 static const char
238 *exception_message[] = {
239 "Unknown",
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 */
246 };
247
248 volatile int kdp_flag = 0;
249
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;
254
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)
258 #else
259 #define KDP_SERIAL_ENABLED() (0)
260 #endif
261
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;
265
266 static void kdp_handler( void *);
267
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;
272
273 static boolean_t save_ip_in_nvram = FALSE;
274
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;
279
280 static struct kdp_ether_addr etherbroadcastaddr = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
281
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}};
286
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;
290
291 static boolean_t flag_arp_resolved = FALSE;
292
293 static unsigned int panic_timeout = 100000;
294 static unsigned int last_panic_port = CORE_REMOTE_PORT;
295
296 #define KDP_THROTTLE_VALUE (10ULL * NSEC_PER_SEC)
297
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];
303
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;
307
308 extern unsigned int not_in_kdp;
309
310 extern unsigned int disableConsoleOutput;
311
312 extern void kdp_call(void);
313 extern boolean_t kdp_call_kdb(void);
314 extern int kern_dump(void);
315
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);
320
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 *);
325
326 static volatile unsigned kdp_reentry_deadline;
327
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;
330
331 char kdp_kernelversion_string[256];
332
333 static boolean_t gKDPDebug = FALSE;
334 #define KDP_DEBUG(...) if (gKDPDebug) printf(__VA_ARGS__);
335
336 int kdp_snapshot = 0;
337 static int stack_snapshot_ret = 0;
338 static unsigned stack_snapshot_bytes_traced = 0;
339
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;
345
346 static unsigned int old_debugger;
347
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;
354
355 void
356 kdp_snapshot_preflight(int pid, void * tracebuf, uint32_t tracebuf_size,
357 uint32_t flags, uint32_t dispatch_offset);
358
359 void
360 kdp_snapshot_postflight(void);
361
362 extern int
363 kdp_stackshot(int pid, void *tracebuf, uint32_t tracebuf_size,
364 uint32_t flags, uint32_t dispatch_offset, uint32_t *pbytesTraced);
365
366 int
367 kdp_stack_snapshot_geterror(void);
368
369 int
370 kdp_stack_snapshot_bytes_traced(void);
371
372 static thread_call_t
373 kdp_timer_call;
374
375 static void
376 kdp_ml_enter_debugger_wrapper(__unused void *param0, __unused void *param1) {
377 kdp_ml_enter_debugger();
378 }
379
380 static void
381 kdp_timer_callout_init(void) {
382 kdp_timer_call = thread_call_allocate(kdp_ml_enter_debugger_wrapper, NULL);
383 }
384
385
386 /* only send/receive data if the link is up */
387 inline static void wait_for_link(void)
388 {
389 static int first = 0;
390
391 if (!kdp_en_linkstatus)
392 return;
393
394 while (((*kdp_en_linkstatus)() & LINK_UP_STATUS) != LINK_UP_STATUS) {
395 if (first)
396 continue;
397
398 first = 1;
399 printf("Waiting for link to become available.\n");
400 kprintf("Waiting for link to become available.\n");
401 }
402 }
403
404
405 inline static void kdp_send_data(void *packet, unsigned int len)
406 {
407 wait_for_link();
408 (*kdp_en_send_pkt)(packet, len);
409 }
410
411
412 inline static void kdp_receive_data(void *packet, unsigned int *len,
413 unsigned int timeout)
414 {
415 wait_for_link();
416 (*kdp_en_recv_pkt)(packet, len, timeout);
417 }
418
419
420 void kdp_register_link(kdp_link_t link, kdp_mode_t mode)
421 {
422 kdp_en_linkstatus = link;
423 kdp_en_setmode = mode;
424 }
425
426 void kdp_unregister_link(__unused kdp_link_t link, __unused kdp_mode_t mode)
427 {
428 kdp_en_linkstatus = NULL;
429 kdp_en_setmode = NULL;
430 }
431
432 void
433 kdp_register_send_receive(
434 kdp_send_t send,
435 kdp_receive_t receive)
436 {
437 unsigned int debug = 0;
438
439 PE_parse_boot_argn("debug", &debug, sizeof (debug));
440
441
442 if (!debug)
443 return;
444
445 kdp_en_send_pkt = send;
446 kdp_en_recv_pkt = receive;
447
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;
452 if (debug & DB_ARP)
453 kdp_flag |= KDP_ARP;
454
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;
459
460 if (debug & DB_DBG_POST_CORE)
461 kdp_flag |= DBG_POST_CORE;
462
463 if (debug & DB_PANICLOG_DUMP)
464 kdp_flag |= PANIC_LOG_DUMP;
465
466 if (PE_parse_boot_argn("_panicd_ip", panicd_ip_str, sizeof (panicd_ip_str)))
467 panicd_specified = TRUE;
468
469 if ((debug & DB_REBOOT_POST_CORE) && (panicd_specified == TRUE))
470 kdp_flag |= REBOOT_POST_CORE;
471
472 if (PE_parse_boot_argn("_router_ip", router_ip_str, sizeof (router_ip_str)))
473 router_specified = TRUE;
474
475 if (!PE_parse_boot_argn("panicd_port", &panicd_port, sizeof (panicd_port)))
476 panicd_port = CORE_REMOTE_PORT;
477
478 if (PE_parse_boot_argn("_panicd_corename", &corename_str, sizeof (corename_str)))
479 corename_specified = TRUE;
480
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) {
485 kdp_call();
486 halt_in_debugger=0;
487 }
488 }
489
490 void
491 kdp_unregister_send_receive(
492 __unused kdp_send_t send,
493 __unused kdp_receive_t receive)
494 {
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;
500 }
501
502 /* Cache stack snapshot parameters in preparation for a trace */
503 void
504 kdp_snapshot_preflight(int pid, void * tracebuf, uint32_t tracebuf_size, uint32_t flags, uint32_t dispatch_offset)
505 {
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;
511 kdp_snapshot++;
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.
515 */
516 old_debugger = current_debugger;
517 if (old_debugger != KDP_CUR_DB) {
518 current_debugger = KDP_CUR_DB;
519 }
520 }
521
522 void
523 kdp_snapshot_postflight(void)
524 {
525 kdp_snapshot--;
526 if ((kdp_en_send_pkt == NULL) || (old_debugger == KDB_CUR_DB))
527 current_debugger = old_debugger;
528 }
529
530 int
531 kdp_stack_snapshot_geterror(void)
532 {
533 return stack_snapshot_ret;
534 }
535
536 int
537 kdp_stack_snapshot_bytes_traced(void)
538 {
539 return stack_snapshot_bytes_traced;
540 }
541
542 static void
543 kdp_schedule_debugger_reentry(unsigned interval) {
544 uint64_t deadline;;
545
546 clock_interval_to_deadline(interval, 1000 * 1000, &deadline);
547 thread_call_enter_delayed(kdp_timer_call, deadline);
548 }
549
550 static void
551 enaddr_copy(
552 void *src,
553 void *dst
554 )
555 {
556 bcopy((char *)src, (char *)dst, sizeof (struct kdp_ether_addr));
557 }
558
559 static unsigned short
560 ip_sum(
561 unsigned char *c,
562 unsigned int hlen
563 )
564 {
565 unsigned int high, low, sum;
566
567 high = low = 0;
568 while (hlen-- > 0) {
569 low += c[1] + c[3];
570 high += c[0] + c[2];
571
572 c += sizeof (int);
573 }
574
575 sum = (high << 8) + low;
576 sum = (sum >> 16) + (sum & 65535);
577
578 return (sum > 65535 ? sum - 65535 : sum);
579 }
580
581 static void
582 kdp_reply(
583 unsigned short reply_port,
584 const boolean_t sideband
585 )
586 {
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;
592
593 if (!pkt.input)
594 kdp_panic("kdp_reply");
595
596 pkt.off -= (unsigned int)sizeof (struct kdp_udpiphdr);
597
598 #if DO_ALIGN
599 bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
600 #else
601 ui = (struct kdp_udpiphdr *)&pkt.data[pkt.off];
602 #endif
603 ui->ui_next = ui->ui_prev = 0;
604 ui->ui_x1 = 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;
613 ui->ui_sum = 0;
614 #if DO_ALIGN
615 bcopy((char *)ui, (char *)&pkt.data[pkt.off], sizeof(*ui));
616 bcopy((char *)&pkt.data[pkt.off], (char *)ip, sizeof(*ip));
617 #else
618 ip = (struct kdp_ip *)&pkt.data[pkt.off];
619 #endif
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;
625 ip->ip_sum = 0;
626 ip->ip_sum = htons(~ip_sum((unsigned char *)ip, ip->ip_hl));
627 #if DO_ALIGN
628 bcopy((char *)ip, (char *)&pkt.data[pkt.off], sizeof(*ip));
629 #endif
630
631 pkt.len += (unsigned int)sizeof (struct kdp_udpiphdr);
632
633 pkt.off -= (unsigned int)sizeof (struct kdp_ether_header);
634
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);
640
641 pkt.len += (unsigned int)sizeof (struct kdp_ether_header);
642
643 // save reply for possible retransmission
644 assert(pkt.len <= KDP_MAXPACKET);
645 if (!sideband)
646 bcopy((char *)&pkt, (char *)&saved_reply, sizeof(saved_reply));
647
648 kdp_send_data(&pkt.data[pkt.off], pkt.len);
649
650 // increment expected sequence number
651 if (!sideband)
652 exception_seq++;
653 }
654
655 static void
656 kdp_send(
657 unsigned short remote_port
658 )
659 {
660 struct kdp_udpiphdr aligned_ui, *ui = &aligned_ui;
661 struct kdp_ip aligned_ip, *ip = &aligned_ip;
662 struct kdp_ether_header *eh;
663
664 if (pkt.input)
665 kdp_panic("kdp_send");
666
667 pkt.off -= (unsigned int)sizeof (struct kdp_udpiphdr);
668
669 #if DO_ALIGN
670 bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
671 #else
672 ui = (struct kdp_udpiphdr *)&pkt.data[pkt.off];
673 #endif
674 ui->ui_next = ui->ui_prev = 0;
675 ui->ui_x1 = 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;
683 ui->ui_sum = 0;
684 #if DO_ALIGN
685 bcopy((char *)ui, (char *)&pkt.data[pkt.off], sizeof(*ui));
686 bcopy((char *)&pkt.data[pkt.off], (char *)ip, sizeof(*ip));
687 #else
688 ip = (struct kdp_ip *)&pkt.data[pkt.off];
689 #endif
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;
695 ip->ip_sum = 0;
696 ip->ip_sum = htons(~ip_sum((unsigned char *)ip, ip->ip_hl));
697 #if DO_ALIGN
698 bcopy((char *)ip, (char *)&pkt.data[pkt.off], sizeof(*ip));
699 #endif
700
701 pkt.len += (unsigned int)sizeof (struct kdp_udpiphdr);
702
703 pkt.off -= (unsigned int)sizeof (struct kdp_ether_header);
704
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);
709
710 pkt.len += (unsigned int)sizeof (struct kdp_ether_header);
711 kdp_send_data(&pkt.data[pkt.off], pkt.len);
712 }
713
714
715 inline static void debugger_if_necessary(void)
716 {
717 if ((current_debugger == KDP_CUR_DB) && halt_in_debugger) {
718 kdp_call();
719 halt_in_debugger=0;
720 }
721 }
722
723
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.
729 */
730 void
731 kdp_set_interface(void *ifp, const struct kdp_ether_addr *macaddr)
732 {
733 char kdpstr[80];
734 struct kdp_in_addr addr = { 0 };
735 unsigned int len;
736
737 kdp_current_ifp = ifp;
738
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)
742 goto done;
743
744 goto config_network;
745 }
746
747 /* use saved ip address */
748 save_ip_in_nvram = TRUE;
749
750 len = sizeof(kdpstr);
751 if (PEReadNVRAMProperty("_kdp_ipstr", kdpstr, &len) == FALSE)
752 goto done;
753
754 kdpstr[len < sizeof(kdpstr) ? len : sizeof(kdpstr) - 1] = '\0';
755 if (inet_aton(kdpstr, &addr) == FALSE)
756 goto done;
757
758 config_network:
759 kdp_current_ip_address = addr.s_addr;
760 if (macaddr)
761 kdp_current_mac_address = *macaddr;
762
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();
768 */
769 done:
770 return;
771 }
772
773 void *
774 kdp_get_interface(void)
775 {
776 return kdp_current_ifp;
777 }
778
779 void
780 kdp_set_ip_and_mac_addresses(
781 struct kdp_in_addr *ipaddr,
782 struct kdp_ether_addr *macaddr)
783 {
784 static uint64_t last_time = (uint64_t) -1;
785 static uint64_t throttle_val = 0;
786 uint64_t cur_time;
787 char addr[16];
788
789 if (kdp_current_ip_address == ipaddr->s_addr)
790 goto done;
791
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;
797 }
798
799 if (save_ip_in_nvram == FALSE)
800 goto done;
801
802 if (inet_ntoa_r(*ipaddr, addr, sizeof(addr)) == NULL)
803 goto done;
804
805 /* throttle writes if needed */
806 if (!throttle_val)
807 nanoseconds_to_absolutetime(KDP_THROTTLE_VALUE, &throttle_val);
808
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));
814 }
815 last_time = cur_time;
816
817 done:
818 debugger_if_necessary();
819 }
820
821 void
822 kdp_set_gateway_mac(void *gatewaymac)
823 {
824 router_mac = *(struct kdp_ether_addr *)gatewaymac;
825 flag_router_mac_initialized = TRUE;
826 }
827
828 struct kdp_ether_addr
829 kdp_get_mac_addr(void)
830 {
831 return kdp_current_mac_address;
832 }
833
834 unsigned int
835 kdp_get_ip_address(void)
836 {
837 return (unsigned int)kdp_current_ip_address;
838 }
839
840 void
841 kdp_disable_arp(void)
842 {
843 kdp_flag &= ~(DB_ARP);
844 }
845
846 static void
847 kdp_arp_dispatch(void)
848 {
849 struct kdp_ether_arp aligned_ea, *ea = &aligned_ea;
850 unsigned arp_header_offset;
851
852 arp_header_offset = (unsigned)sizeof(struct kdp_ether_header) + pkt.off;
853 memcpy((void *)ea, (void *)&pkt.data[arp_header_offset], sizeof(*ea));
854
855 switch(ntohs(ea->arp_op)) {
856 case ARPOP_REQUEST:
857 kdp_arp_reply(ea);
858 break;
859 case ARPOP_REPLY:
860 kdp_process_arp_reply(ea);
861 break;
862 default:
863 return;
864 }
865 }
866
867 static void
868 kdp_process_arp_reply(struct kdp_ether_arp *ea)
869 {
870 /* Are we interested in ARP replies? */
871 if (flag_arp_resolved == TRUE)
872 return;
873
874 /* Did we receive a reply from the right source? */
875 if (((struct kdp_in_addr *)(ea->arp_spa))->s_addr != target_ip)
876 return;
877
878 flag_arp_resolved = TRUE;
879 current_resolved_MAC = *(struct kdp_ether_addr *) (ea->arp_sha);
880
881 return;
882 }
883
884 /* ARP responses are enabled when the DB_ARP bit of the debug boot arg
885 * is set.
886 */
887
888 static void
889 kdp_arp_reply(struct kdp_ether_arp *ea)
890 {
891 struct kdp_ether_header *eh;
892
893 struct kdp_in_addr isaddr, itaddr, myaddr;
894 struct kdp_ether_addr my_enaddr;
895
896 eh = (struct kdp_ether_header *)&pkt.data[pkt.off];
897 pkt.off += (unsigned int)sizeof(struct kdp_ether_header);
898
899 if(ntohs(ea->arp_op) != ARPOP_REQUEST)
900 return;
901
902 myaddr.s_addr = kdp_get_ip_address();
903 my_enaddr = kdp_get_mac_addr();
904
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
912 ))
913 return;
914
915 (void)memcpy((void *)&isaddr, (void *)ea->arp_spa, sizeof (isaddr));
916 (void)memcpy((void *)&itaddr, (void *)ea->arp_tpa, sizeof (itaddr));
917
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));
921
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));
924
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);
934 }
935 }
936
937 static void
938 kdp_poll(void)
939 {
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;
944
945 if (pkt.input)
946 kdp_panic("kdp_poll");
947
948 if (!kdp_en_recv_pkt || !kdp_en_send_pkt) {
949 if( msg_printed == 0) {
950 msg_printed = 1;
951 printf("kdp_poll: no debugger device\n");
952 }
953 return;
954 }
955
956 pkt.off = pkt.len = 0;
957 kdp_receive_data(pkt.data, &pkt.len, 3/* ms */);
958
959 if (pkt.len == 0)
960 return;
961
962 if (pkt.len >= sizeof(struct kdp_ether_header))
963 {
964 eh = (struct kdp_ether_header *)&pkt.data[pkt.off];
965
966 if (kdp_flag & KDP_ARP)
967 {
968 if (ntohs(eh->ether_type) == ETHERTYPE_ARP)
969 {
970 kdp_arp_dispatch();
971 return;
972 }
973 }
974 }
975
976 if (pkt.len < (sizeof (struct kdp_ether_header) + sizeof (struct kdp_udpiphdr)))
977 return;
978
979 pkt.off += (unsigned int)sizeof (struct kdp_ether_header);
980 if (ntohs(eh->ether_type) != ETHERTYPE_IP) {
981 return;
982 }
983
984 #if DO_ALIGN
985 bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
986 bcopy((char *)&pkt.data[pkt.off], (char *)ip, sizeof(*ip));
987 #else
988 ui = (struct kdp_udpiphdr *)&pkt.data[pkt.off];
989 ip = (struct kdp_ip *)&pkt.data[pkt.off];
990 #endif
991
992 pkt.off += (unsigned int)sizeof (struct kdp_udpiphdr);
993 if (ui->ui_pr != IPPROTO_UDP) {
994 return;
995 }
996
997 if (ip->ip_hl > (sizeof (struct kdp_ip) >> 2)) {
998 return;
999 }
1000
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;
1005 }
1006 else
1007 return;
1008 }
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.
1012 */
1013 else
1014 if (flag_panic_dump_in_progress)
1015 {
1016 if (!flag_dont_abort_panic_dump) {
1017 abort_panic_transfer();
1018 }
1019 return;
1020 }
1021
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;
1025
1026 enaddr_copy(eh->ether_shost, &adr.rmt.ea);
1027 adr.rmt.in = ui->ui_src;
1028 }
1029
1030 /*
1031 * Calculate kdp packet length.
1032 */
1033 pkt.len = ntohs((u_short)ui->ui_ulen) - (unsigned int)sizeof (struct kdp_udphdr);
1034 pkt.input = TRUE;
1035 }
1036
1037 /* Create and transmit an ARP resolution request for the target IP address.
1038 * This is modeled on ether_inet_arp()/RFC 826.
1039 */
1040
1041 static void
1042 transmit_ARP_request(uint32_t ip_addr)
1043 {
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)];
1046
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(&etherbroadcastaddr, eh->ether_dhost);
1052
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);
1059
1060 /* Target fields */
1061 enaddr_copy(&etherbroadcastaddr, ea->arp_tha);
1062 memcpy(ea->arp_tpa, (void *) &ip_addr, sizeof(ip_addr));
1063
1064 /* Source fields */
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));
1067
1068 pkt.off = 0;
1069 pkt.len = sizeof(struct kdp_ether_header) + sizeof(struct kdp_ether_arp);
1070 /* Transmit */
1071 kdp_send_data(&pkt.data[pkt.off], pkt.len);
1072 }
1073
1074 static boolean_t
1075 kdp_arp_resolve(uint32_t arp_target_ip, struct kdp_ether_addr *resolved_MAC)
1076 {
1077 int poll_count = 256; /* ~770 ms modulo broadcast/delayed traffic? */
1078 char tretries = 0;
1079
1080 #define NUM_ARP_TX_RETRIES 5
1081
1082 target_ip = arp_target_ip;
1083 flag_arp_resolved = FALSE;
1084
1085 TRANSMIT_RETRY:
1086 pkt.off = pkt.len = 0;
1087
1088 tretries++;
1089
1090 if (tretries >= NUM_ARP_TX_RETRIES) {
1091 return FALSE;
1092 }
1093
1094 KDP_DEBUG("ARP TX attempt #%d \n", tretries);
1095
1096 transmit_ARP_request(arp_target_ip);
1097
1098 while (!pkt.input && !flag_arp_resolved && flag_panic_dump_in_progress && --poll_count) {
1099 kdp_poll();
1100 }
1101
1102 if (flag_arp_resolved) {
1103 *resolved_MAC = current_resolved_MAC;
1104 return TRUE;
1105 }
1106
1107 if (!flag_panic_dump_in_progress || pkt.input) /* we received a debugging packet, bail*/
1108 {
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;
1112 pkt.input = FALSE;
1113 return FALSE;
1114 }
1115 else /* We timed out */
1116 if (0 == poll_count) {
1117 poll_count = 256;
1118 goto TRANSMIT_RETRY;
1119 }
1120 return FALSE;
1121 }
1122
1123 static void
1124 kdp_handler(
1125 void *saved_state
1126 )
1127 {
1128 unsigned short reply_port;
1129 kdp_hdr_t aligned_hdr, *hdr = &aligned_hdr;
1130
1131 kdp.saved_state = saved_state; // see comment in kdp_raise_exception
1132
1133 do {
1134 while (!pkt.input)
1135 kdp_poll();
1136
1137 #if DO_ALIGN
1138 bcopy((char *)&pkt.data[pkt.off], (char *)hdr, sizeof(*hdr));
1139 #else
1140 hdr = (kdp_hdr_t *)&pkt.data[pkt.off];
1141 #endif
1142
1143 // ignore replies -- we're not expecting them anyway.
1144 if (hdr->is_reply) {
1145 goto again;
1146 }
1147
1148 if (hdr->request == KDP_REATTACH)
1149 exception_seq = hdr->seq;
1150
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],
1155 saved_reply.len);
1156 goto again;
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);
1161 goto again;
1162 }
1163
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
1168 */
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) {
1173 /* process */
1174 kdp_packet((unsigned char *)&manual_pkt.data,
1175 (int *)&manual_pkt.len,
1176 &manual_port_unused);
1177 }
1178 manual_pkt.input = 0;
1179 }
1180
1181 if (kdp_packet((unsigned char*)&pkt.data[pkt.off],
1182 (int *)&pkt.len,
1183 (unsigned short *)&reply_port)) {
1184 boolean_t sideband = FALSE;
1185
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;
1193
1194 if (err == KDPERR_NO_ERROR) {
1195 exception_seq = hdr->seq;
1196 } else if (err == KDPERR_ALREADY_CONNECTED) {
1197 sideband = TRUE;
1198 }
1199 }
1200
1201 kdp_reply(reply_port, sideband);
1202 }
1203
1204 again:
1205 pkt.input = FALSE;
1206 } while (kdp.is_halted);
1207 }
1208
1209 static void
1210 kdp_connection_wait(void)
1211 {
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());
1215
1216 /*
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
1219 * the panic.log
1220 */
1221
1222 if (KDP_SERIAL_ENABLED()) {
1223 printf("Using serial KDP.\n");
1224 kprintf("Using serial KDP.\n");
1225 } else {
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);
1233
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);
1241
1242 printf( "ip address: %d.%d.%d.%d\n",
1243 (ip_addr & 0xff000000) >> 24,
1244 (ip_addr & 0xff0000) >> 16,
1245 (ip_addr & 0xff00) >> 8,
1246 (ip_addr & 0xff));
1247
1248 kprintf( "ip address: %d.%d.%d.%d\n",
1249 (ip_addr & 0xff000000) >> 24,
1250 (ip_addr & 0xff0000) >> 16,
1251 (ip_addr & 0xff00) >> 8,
1252 (ip_addr & 0xff));
1253 }
1254
1255 printf("\nWaiting for remote debugger connection.\n");
1256 kprintf("\nWaiting for remote debugger connection.\n");
1257
1258
1259 if (reattach_wait == 0) {
1260 if((kdp_flag & KDP_GETC_ENA) && (0 != kdp_getc()))
1261 {
1262 printf("Options..... Type\n");
1263 printf("------------ ----\n");
1264 printf("continue.... 'c'\n");
1265 printf("reboot...... 'r'\n");
1266 }
1267 } else
1268 reattach_wait = 0;
1269
1270 exception_seq = 0;
1271
1272 do {
1273 kdp_hdr_t aligned_hdr, *hdr = &aligned_hdr;
1274
1275 while (!pkt.input) {
1276 if (kdp_flag & KDP_GETC_ENA) {
1277 switch(kdp_getc()) {
1278 case 'c':
1279 printf("Continuing...\n");
1280 return;
1281 case 'r':
1282 printf("Rebooting...\n");
1283 kdp_machine_reboot();
1284 break;
1285 default:
1286 break;
1287 }
1288 }
1289 kdp_poll();
1290 }
1291
1292 #if DO_ALIGN
1293 bcopy((char *)&pkt.data[pkt.off], (char *)hdr, sizeof(*hdr));
1294 #else
1295 hdr = (kdp_hdr_t *)&pkt.data[pkt.off];
1296 #endif
1297 if (hdr->request == KDP_HOSTREBOOT) {
1298 kdp_machine_reboot();
1299 /* should not return! */
1300 }
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],
1304 (int *)&pkt.len,
1305 (unsigned short *)&reply_port))
1306 kdp_reply(reply_port, FALSE);
1307 if (hdr->request == KDP_REATTACH) {
1308 reattach_wait = 0;
1309 hdr->request=KDP_DISCONNECT;
1310 exception_seq = 0;
1311 }
1312 }
1313
1314 pkt.input = FALSE;
1315 } while (!kdp.is_conn);
1316
1317 if (current_debugger == KDP_CUR_DB)
1318 active_debugger=1;
1319 printf("Connected to remote debugger.\n");
1320 kprintf("Connected to remote debugger.\n");
1321 }
1322
1323 static void
1324 kdp_send_exception(
1325 unsigned int exception,
1326 unsigned int code,
1327 unsigned int subcode
1328 )
1329 {
1330 unsigned short remote_port;
1331 unsigned int timeout_count = 100;
1332 unsigned int poll_timeout;
1333
1334 do {
1335 pkt.off = sizeof (struct kdp_ether_header) + sizeof (struct kdp_udpiphdr);
1336 kdp_exception((unsigned char *)&pkt.data[pkt.off],
1337 (int *)&pkt.len,
1338 (unsigned short *)&remote_port,
1339 (unsigned int)exception,
1340 (unsigned int)code,
1341 (unsigned int)subcode);
1342
1343 kdp_send(remote_port);
1344
1345 poll_timeout = 50;
1346 while(!pkt.input && poll_timeout)
1347 {
1348 kdp_poll();
1349 poll_timeout--;
1350 }
1351
1352 if (pkt.input) {
1353 if (!kdp_exception_ack(&pkt.data[pkt.off], pkt.len)) {
1354 pkt.input = FALSE;
1355 }
1356 }
1357
1358 pkt.input = FALSE;
1359
1360 if (kdp.exception_ack_needed)
1361 kdp_us_spin(250000);
1362
1363 } while (kdp.exception_ack_needed && timeout_count--);
1364
1365 if (kdp.exception_ack_needed) {
1366 // give up & disconnect
1367 printf("kdp: exception ack timeout\n");
1368 if (current_debugger == KDP_CUR_DB)
1369 active_debugger=0;
1370 kdp_reset();
1371 }
1372 }
1373
1374 void
1375 kdp_raise_exception(
1376 unsigned int exception,
1377 unsigned int code,
1378 unsigned int subcode,
1379 void *saved_state
1380 )
1381 {
1382 int index;
1383 unsigned int initial_not_in_kdp = not_in_kdp;
1384
1385 not_in_kdp = 0;
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;
1393 return;
1394 }
1395
1396 disable_preemption();
1397
1398 if (saved_state == 0)
1399 printf("kdp_raise_exception with NULL state\n");
1400
1401 index = exception;
1402 if (exception != EXC_BREAKPOINT) {
1403 if (exception > EXC_BREAKPOINT || exception < EXC_BAD_ACCESS) {
1404 index = 0;
1405 }
1406 printf("%s exception (%x,%x,%x)\n",
1407 exception_message[index],
1408 exception, code, subcode);
1409 }
1410
1411 kdp_sync_cache();
1412
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.
1415 */
1416 kdp.saved_state = saved_state;
1417 kdp.kdp_cpu = cpu_number();
1418 kdp.kdp_thread = current_thread();
1419
1420 if (kdp_en_setmode)
1421 (*kdp_en_setmode)(TRUE); /* enabling link mode */
1422
1423 if (pkt.input)
1424 kdp_panic("kdp_raise_exception");
1425
1426 if (((kdp_flag & KDP_PANIC_DUMP_ENABLED) || (kdp_flag & PANIC_LOG_DUMP))
1427 && (panicstr != (char *) 0)) {
1428 kdp_panic_dump();
1429 if (kdp_flag & REBOOT_POST_CORE)
1430 kdp_machine_reboot();
1431 }
1432 else
1433 if ((kdp_flag & PANIC_CORE_ON_NMI) && (panicstr == (char *) 0) &&
1434 !kdp.is_conn) {
1435
1436 disable_debug_output = disableConsoleOutput = FALSE;
1437 kdp_panic_dump();
1438
1439 if (!(kdp_flag & DBG_POST_CORE))
1440 goto exit_raise_exception;
1441 }
1442
1443 again:
1444 if (!kdp.is_conn)
1445 kdp_connection_wait();
1446 else {
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");
1452 }
1453 }
1454
1455 if (kdp.is_conn) {
1456 kdp.is_halted = TRUE; /* XXX */
1457 kdp_handler(saved_state);
1458 if (!kdp.is_conn)
1459 {
1460 kdp_remove_all_breakpoints();
1461 printf("Remote debugger disconnected.\n");
1462 }
1463 }
1464 /* Allow triggering a panic core dump when connected to the machine
1465 * Continuing after setting kdp_trigger_core_dump should do the
1466 * trick.
1467 */
1468
1469 if (1 == kdp_trigger_core_dump) {
1470 kdp_flag |= KDP_PANIC_DUMP_ENABLED;
1471 kdp_panic_dump();
1472 if (kdp_flag & REBOOT_POST_CORE)
1473 kdp_machine_reboot();
1474 kdp_trigger_core_dump = 0;
1475 }
1476
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.
1481 */
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;
1486 }
1487
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;
1492 }
1493
1494 kdp_sync_cache();
1495
1496 if (reattach_wait == 1)
1497 goto again;
1498
1499 exit_raise_exception:
1500 if (kdp_en_setmode)
1501 (*kdp_en_setmode)(FALSE); /* link cleanup */
1502
1503 not_in_kdp = initial_not_in_kdp;
1504
1505 enable_preemption();
1506 }
1507
1508 void
1509 kdp_reset(void)
1510 {
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;
1517 }
1518
1519 struct corehdr *
1520 create_panic_header(unsigned int request, const char *corename,
1521 unsigned length, unsigned int block)
1522 {
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;
1529
1530 size_t fmask_size = sizeof(KDP_FEATURE_MASK_STRING) + sizeof(kdp_crashdump_feature_mask);
1531
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));
1535
1536 #if DO_ALIGN
1537 bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
1538 #else
1539 ui = (struct kdp_udpiphdr *)&pkt.data[pkt.off];
1540 #endif
1541 ui->ui_next = ui->ui_prev = 0;
1542 ui->ui_x1 = 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;
1551 ui->ui_sum = 0;
1552 #if DO_ALIGN
1553 bcopy((char *)ui, (char *)&pkt.data[pkt.off], sizeof(*ui));
1554 bcopy((char *)&pkt.data[pkt.off], (char *)ip, sizeof(*ip));
1555 #else
1556 ip = (struct kdp_ip *)&pkt.data[pkt.off];
1557 #endif
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;
1563 ip->ip_sum = 0;
1564 ip->ip_sum = htons(~ip_sum((unsigned char *)ip, ip->ip_hl));
1565 #if DO_ALIGN
1566 bcopy((char *)ip, (char *)&pkt.data[pkt.off], sizeof(*ip));
1567 #endif
1568
1569 pkt.len += (unsigned int)sizeof (struct kdp_udpiphdr);
1570
1571 pkt.off += (unsigned int)sizeof (struct kdp_udpiphdr);
1572
1573 coreh = (struct corehdr *) &pkt.data[pkt.off];
1574 coreh->th_opcode = htons((u_short)request);
1575
1576 if (request == KDP_WRQ)
1577 {
1578 char *cp;
1579
1580 cp = coreh->th_u.tu_rpl;
1581 cp += strlcpy (cp, corename, KDP_MAXPACKET);
1582 *cp++ = '\0';
1583 cp += strlcpy (cp, mode, KDP_MAXPACKET - strlen(corename));
1584 *cp++ = '\0';
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);
1592 }
1593 else
1594 {
1595 coreh->th_block = htonl((unsigned int) block);
1596 }
1597
1598 pkt.off -= (unsigned int)sizeof (struct kdp_udpiphdr);
1599 pkt.off -= (unsigned int)sizeof (struct kdp_ether_header);
1600
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);
1605
1606 pkt.len += (unsigned int)sizeof (struct kdp_ether_header);
1607 return coreh;
1608 }
1609
1610 static int kdp_send_crashdump_seek(char *corename, uint64_t seek_off)
1611 {
1612 int panic_error;
1613
1614 if (kdp_feature_large_crashdumps) {
1615 panic_error = kdp_send_crashdump_pkt(KDP_SEEK, corename,
1616 sizeof(seek_off),
1617 &seek_off);
1618 } else {
1619 uint32_t off = (uint32_t) seek_off;
1620 panic_error = kdp_send_crashdump_pkt(KDP_SEEK, corename,
1621 sizeof(off), &off);
1622 }
1623
1624 if (panic_error < 0) {
1625 printf ("kdp_send_crashdump_pkt failed with error %d\n",
1626 panic_error);
1627 return panic_error;
1628 }
1629
1630 return 0;
1631 }
1632
1633 int kdp_send_crashdump_data(unsigned int request, char *corename,
1634 int64_t length, caddr_t txstart)
1635 {
1636 int panic_error = 0;
1637
1638 while (length > 0) {
1639 uint64_t chunk = MIN(kdp_crashdump_pkt_size, length);
1640
1641 panic_error = kdp_send_crashdump_pkt(request, corename, chunk,
1642 txstart);
1643 if (panic_error < 0) {
1644 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error);
1645 return panic_error;
1646 }
1647
1648 txstart += chunk;
1649 length -= chunk;
1650 }
1651 return 0;
1652 }
1653
1654 uint32_t kdp_crashdump_short_pkt;
1655
1656 int
1657 kdp_send_crashdump_pkt(unsigned int request, char *corename,
1658 uint64_t length, void *panic_data)
1659 {
1660 int poll_count;
1661 struct corehdr *th = NULL;
1662 char rretries, tretries;
1663
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;
1667 }
1668
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 */
1673 poll_count += 1000;
1674
1675 TRANSMIT_RETRY:
1676 tretries++;
1677
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.
1683 */
1684 printf ("Cannot contact panic server, timing out.\n");
1685 return (-3);
1686 }
1687
1688 if (tretries > 2)
1689 printf("TX retry #%d ", tretries );
1690
1691 th = create_panic_header(request, corename, (unsigned)length, panic_block);
1692
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);
1701 }
1702
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);
1710 }
1711 }
1712 }
1713 }
1714 else if (request == KDP_SEEK) {
1715 if (kdp_feature_large_crashdumps)
1716 *(uint64_t *) th->th_data = OSSwapHostToBigInt64((*(uint64_t *) panic_data));
1717 else
1718 *(unsigned int *) th->th_data = htonl(*(unsigned int *) panic_data);
1719 }
1720
1721 kdp_send_data(&pkt.data[pkt.off], pkt.len);
1722
1723 /* Listen for the ACK */
1724 RECEIVE_RETRY:
1725 while (!pkt.input && flag_panic_dump_in_progress && poll_count) {
1726 kdp_poll();
1727 poll_count--;
1728 }
1729
1730 if (pkt.input) {
1731
1732 pkt.input = FALSE;
1733
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;
1742 }
1743 else {
1744 kdp_feature_large_pkt_size = 0;
1745 kdp_crashdump_pkt_size = 512;
1746 }
1747 printf("Protocol features: 0x%x\n", (uint32_t) features64);
1748 th->th_opcode = htons(KDP_ACK);
1749 }
1750 }
1751 if (ntohs(th->th_opcode) == KDP_ACK && ntohl(th->th_block) == panic_block) {
1752 }
1753 else
1754 if (ntohs(th->th_opcode) == KDP_ERROR) {
1755 printf("Panic server returned error %d, retrying\n", ntohl(th->th_code));
1756 poll_count = 1000;
1757 goto TRANSMIT_RETRY;
1758 }
1759 else
1760 if (ntohl(th->th_block) == (panic_block - 1)) {
1761 printf("RX retry ");
1762 if (++rretries > 1)
1763 goto TRANSMIT_RETRY;
1764 else
1765 goto RECEIVE_RETRY;
1766 }
1767 }
1768 else
1769 if (!flag_panic_dump_in_progress) /* we received a debugging packet, bail*/
1770 {
1771 printf("Received a debugger packet,transferring control to debugger\n");
1772 /* Configure that if not set ..*/
1773 kdp_flag |= DBG_POST_CORE;
1774 return (-2);
1775 }
1776 else /* We timed out */
1777 if (0 == poll_count) {
1778 poll_count = 1000;
1779 kdp_us_spin ((tretries%4) * panic_timeout); /* capped linear backoff */
1780 goto TRANSMIT_RETRY;
1781 }
1782
1783 if (!(++panic_block % SBLOCKSZ)) {
1784 uint64_t ctime;
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;
1793 }
1794
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);
1800 }
1801 return 1;
1802 }
1803
1804 static int
1805 isdigit (char c)
1806 {
1807 return ((c > 47) && (c < 58));
1808 }
1809
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.
1815 */
1816 /* 2006: Incorporated a change from Darwin user P. Lovell to extract
1817 * the minor kernel version numbers from the version string.
1818 */
1819 static int
1820 kdp_get_xnu_version(char *versionbuf)
1821 {
1822 char *versionpos;
1823 char vstr[20];
1824 int retval = -1;
1825 char *vptr;
1826
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);
1831 if (versionpos) {
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 == '.'))
1836 vptr++;
1837 *vptr = '\0';
1838 /* Remove trailing period, if any */
1839 if (*(--vptr) == '.')
1840 *vptr = '\0';
1841 retval = 0;
1842 }
1843 }
1844 strlcpy(versionbuf, vstr, KDP_MAXPACKET);
1845 return retval;
1846 }
1847
1848 void
1849 kdp_set_dump_info(const uint32_t flags, const char *filename,
1850 const char *destipstr, const char *routeripstr,
1851 const uint32_t port)
1852 {
1853 uint32_t cmd;
1854
1855 if (destipstr && (destipstr[0] != '\0')) {
1856 strlcpy(panicd_ip_str, destipstr, sizeof(panicd_ip_str));
1857 panicd_specified = 1;
1858 }
1859
1860 if (routeripstr && (routeripstr[0] != '\0')) {
1861 strlcpy(router_ip_str, routeripstr, sizeof(router_ip_str));
1862 router_specified = 1;
1863 }
1864
1865 if (filename && (filename[0] != '\0')) {
1866 strlcpy(corename_str, filename, sizeof(corename_str));
1867 corename_specified = TRUE;
1868 } else {
1869 corename_specified = FALSE;
1870 }
1871
1872 if (port)
1873 panicd_port = port;
1874
1875 /* on a disconnect, should we stay in KDP or not? */
1876 noresume_on_disconnect = (flags & KDP_DUMPINFO_NORESUME) ? 1 : 0;
1877
1878 if ((flags & KDP_DUMPINFO_DUMP) == 0)
1879 return;
1880
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;
1887 return;
1888 }
1889
1890 kdp_flag &= ~REBOOT_POST_CORE;
1891 if (flags & KDP_DUMPINFO_REBOOT)
1892 kdp_flag |= REBOOT_POST_CORE;
1893
1894 kdp_flag &= ~PANIC_LOG_DUMP;
1895 if (cmd == KDP_DUMPINFO_PANICLOG)
1896 kdp_flag |= PANIC_LOG_DUMP;
1897
1898 kdp_flag &= ~SYSTEM_LOG_DUMP;
1899 if (cmd == KDP_DUMPINFO_SYSTEMLOG)
1900 kdp_flag |= SYSTEM_LOG_DUMP;
1901
1902 /* trigger a dump */
1903 kdp_flag |= DBG_POST_CORE;
1904
1905 flag_dont_abort_panic_dump = (flags & KDP_DUMPINFO_NOINTR) ?
1906 TRUE : FALSE;
1907
1908 reattach_wait = 1;
1909 logPanicDataToScreen = 1;
1910 disableConsoleOutput = 0;
1911 disable_debug_output = 0;
1912 kdp_trigger_core_dump = 1;
1913 }
1914
1915 void
1916 kdp_get_dump_info(uint32_t *flags, char *filename, char *destipstr,
1917 char *routeripstr, uint32_t *port)
1918 {
1919 if (destipstr) {
1920 if (panicd_specified)
1921 strlcpy(destipstr, panicd_ip_str,
1922 sizeof(panicd_ip_str));
1923 else
1924 destipstr[0] = '\0';
1925 }
1926
1927 if (routeripstr) {
1928 if (router_specified)
1929 strlcpy(routeripstr, router_ip_str,
1930 sizeof(router_ip_str));
1931 else
1932 routeripstr[0] = '\0';
1933 }
1934
1935 if (filename) {
1936 if (corename_specified)
1937 strlcpy(filename, corename_str,
1938 sizeof(corename_str));
1939 else
1940 filename[0] = '\0';
1941
1942 }
1943
1944 if (port)
1945 *port = panicd_port;
1946
1947 if (flags) {
1948 *flags = 0;
1949 if (!panicd_specified)
1950 *flags |= KDP_DUMPINFO_DISABLE;
1951 else if (kdp_flag & PANIC_LOG_DUMP)
1952 *flags |= KDP_DUMPINFO_PANICLOG;
1953 else
1954 *flags |= KDP_DUMPINFO_CORE;
1955
1956 if (noresume_on_disconnect)
1957 *flags |= KDP_DUMPINFO_NORESUME;
1958 }
1959 }
1960
1961
1962 /* Primary dispatch routine for the system dump */
1963 void
1964 kdp_panic_dump(void)
1965 {
1966 char coreprefix[10];
1967 int panic_error;
1968
1969 uint64_t abstime;
1970 uint32_t current_ip = ntohl((uint32_t)kdp_current_ip_address);
1971
1972 if (flag_panic_dump_in_progress) {
1973 kdb_printf("System dump aborted.\n");
1974 goto panic_dump_exit;
1975 }
1976
1977 printf("Entering system dump routine\n");
1978
1979 if (!kdp_en_recv_pkt || !kdp_en_send_pkt) {
1980 kdb_printf("Error: No transport device registered for kernel crashdump\n");
1981 return;
1982 }
1983
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;
1987 }
1988
1989 flag_panic_dump_in_progress = TRUE;
1990
1991 if (pkt.input)
1992 kdp_panic("kdp_panic_dump: unexpected pending input packet");
1993
1994 kdp_get_xnu_version((char *) &pkt.data[0]);
1995
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));
2002 else
2003 strlcpy(coreprefix, "core", sizeof(coreprefix));
2004
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));
2014 }
2015
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);
2018 }
2019 else
2020 kdb_printf("Attempting connection to panic server configured at IP %s, port %d\n", panicd_ip_str, panicd_port);
2021
2022 destination_mac = router_mac;
2023
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;
2027 }
2028 else {
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);
2033 else {
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);
2038 }
2039 }
2040 }
2041 }
2042
2043 if (!flag_panic_dump_in_progress) goto panic_dump_exit;
2044
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);
2052
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);
2055
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;
2059 }
2060
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,
2066 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;
2070 }
2071
2072 /* maybe we wanted the systemlog */
2073 if (kdp_flag & SYSTEM_LOG_DUMP) {
2074 long start_off = msgbufp->msg_bufx;
2075 long len;
2076
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);
2084 start_off = 0;
2085 }
2086
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);
2091 }
2092
2093 kdp_send_crashdump_pkt (KDP_EOF, NULL, 0, ((void *) 0));
2094 goto panic_dump_exit;
2095 }
2096
2097 /* We want a core dump if we're here */
2098 kern_dump();
2099
2100 panic_dump_exit:
2101 abort_panic_transfer();
2102 kdp_reset();
2103 return;
2104 }
2105
2106 void
2107 abort_panic_transfer(void)
2108 {
2109 flag_panic_dump_in_progress = FALSE;
2110 flag_dont_abort_panic_dump = FALSE;
2111 panic_block = 0;
2112 }
2113
2114 #if CONFIG_SERIAL_KDP
2115
2116 static boolean_t needs_serial_init = TRUE;
2117
2118 static void
2119 kdp_serial_send(void *rpkt, unsigned int rpkt_len)
2120 {
2121 // printf("tx\n");
2122 kdp_serialize_packet((unsigned char *)rpkt, rpkt_len, pal_serial_putc);
2123 }
2124
2125 static void
2126 kdp_serial_receive(void *rpkt, unsigned int *rpkt_len, unsigned int timeout)
2127 {
2128 int readkar;
2129 uint64_t now, deadline;
2130
2131 clock_interval_to_deadline(timeout, 1000 * 1000 /* milliseconds */, &deadline);
2132
2133 // printf("rx\n");
2134 for(clock_get_uptime(&now); now < deadline; clock_get_uptime(&now))
2135 {
2136 readkar = pal_serial_getc();
2137 if(readkar >= 0)
2138 {
2139 unsigned char *packet;
2140 // printf("got char %02x\n", readkar);
2141 if((packet = kdp_unserialize_packet(readkar,rpkt_len)))
2142 {
2143 memcpy(rpkt, packet, *rpkt_len);
2144 return;
2145 }
2146 }
2147 }
2148 *rpkt_len = 0;
2149 }
2150
2151 static boolean_t
2152 kdp_serial_setmode(boolean_t active)
2153 {
2154 if (active == FALSE) /* leaving KDP */
2155 return TRUE;
2156
2157 if (!needs_serial_init)
2158 return TRUE;
2159
2160 pal_serial_init();
2161 needs_serial_init = FALSE;
2162 return TRUE;
2163 }
2164
2165
2166 static void kdp_serial_callout(__unused void *arg, kdp_event_t event)
2167 {
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.
2172 *
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.
2175 */
2176
2177 switch (event)
2178 {
2179 case KDP_EVENT_PANICLOG:
2180 case KDP_EVENT_ENTER:
2181 break;
2182 case KDP_EVENT_EXIT:
2183 needs_serial_init = TRUE;
2184 break;
2185 }
2186 }
2187
2188 #endif /* CONFIG_SERIAL_KDP */
2189
2190 void
2191 kdp_init(void)
2192 {
2193 strlcpy(kdp_kernelversion_string, version, sizeof(kdp_kernelversion_string));
2194
2195 /* Relies on platform layer calling panic_init() before kdp_init() */
2196 if (kernel_uuid_string[0] != '\0') {
2197 /*
2198 * Update kdp_kernelversion_string with our UUID
2199 * generated at link time.
2200 */
2201
2202 strlcat(kdp_kernelversion_string, "; UUID=", sizeof(kdp_kernelversion_string));
2203 strlcat(kdp_kernelversion_string, kernel_uuid_string, sizeof(kdp_kernelversion_string));
2204 }
2205
2206 debug_log_init();
2207
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));
2214 }
2215 #endif
2216
2217 if (debug_boot_arg & DB_REBOOT_POST_CORE)
2218 kdp_flag |= REBOOT_POST_CORE;
2219 #if defined(__x86_64__)
2220 kdp_machine_init();
2221 #endif
2222
2223 kdp_timer_callout_init();
2224 kdp_crashdump_feature_mask = htonl(kdp_crashdump_feature_mask);
2225
2226 #if CONFIG_SERIAL_KDP
2227 char kdpname[80];
2228 struct kdp_in_addr ipaddr;
2229 struct kdp_ether_addr macaddr;
2230
2231
2232 // serial must be explicitly requested
2233 if(!PE_parse_boot_argn("kdp_match_name", kdpname, sizeof(kdpname)) || strncmp(kdpname, "serial", sizeof(kdpname)) != 0)
2234 return;
2235
2236 kprintf("Initializing serial KDP\n");
2237
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);
2241
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);
2251
2252 #endif /* CONFIG_SERIAL_KDP */
2253 }